Skip to content

Commit

Permalink
[core] Refactor realtek-ambz, cleanup, reformat, add missing copyrights
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed May 6, 2022
1 parent 5aba2eb commit 589d3ef
Show file tree
Hide file tree
Showing 103 changed files with 1,228 additions and 9,936 deletions.
29 changes: 29 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Language: Cpp
BasedOnStyle: LLVM
AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: Left
AlignConsecutiveAssignments: true
AlignConsecutiveMacros: AcrossComments
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortFunctionsOnASingleLine: Empty
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BreakBeforeTernaryOperators: true
ColumnLimit: 120
ContinuationIndentWidth: 4
EmptyLineBeforeAccessModifier: Always
FixNamespaceComments: true
IndentAccessModifiers: false
IndentCaseLabels: true
IndentWidth: 4
LambdaBodyIndentation: Signature
MaxEmptyLinesToKeep: 1
# PointerAlignment: Left # TODO enable this and reformat project
QualifierAlignment: Left
ReflowComments: true
SeparateDefinitionBlocks: Always
TabWidth: 4
UseTab: Always
1 change: 0 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ jobs:
lint-clang-format:
name: Lint with clang-format
runs-on: ubuntu-latest
if: ${{ false }} # disable for now
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions arduino/libretuya/api/Flash.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-24. */

#pragma once

#include <stdbool.h>
Expand Down
2 changes: 2 additions & 0 deletions arduino/libretuya/api/LibreTuyaAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-29. */

#include "LibreTuyaAPI.h"

__weak char *strdup(const char *s) {
Expand Down
2 changes: 2 additions & 0 deletions arduino/libretuya/api/LibreTuyaAPI.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-28. */

#pragma once

// LibreTuya version macros
Expand Down
2 changes: 2 additions & 0 deletions arduino/libretuya/api/LibreTuyaConfig.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-28. */

#pragma once

// see docs/API Configuration
Expand Down
2 changes: 2 additions & 0 deletions arduino/libretuya/api/lt_logger.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-28. */

#include "lt_logger.h"

#include <Arduino.h>
Expand Down
2 changes: 2 additions & 0 deletions arduino/libretuya/api/lt_logger.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-28. */

#pragma once

#include "LibreTuyaConfig.h"
Expand Down
55 changes: 55 additions & 0 deletions arduino/libretuya/common/WMath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright (c) 2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>

void randomSeed(uint32_t dwSeed) {
if (dwSeed != 0) {
srand(dwSeed);
}
}

long random(long howbig) {
if (howbig == 0) {
return 0;
}

return rand() % howbig;
}

long random(long howsmall, long howbig) {
if (howsmall >= howbig) {
return howsmall;
}

long diff = howbig - howsmall;

return random(diff) + howsmall;
}

extern long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

extern uint16_t makeWord(uint16_t w) {
return w;
}

extern uint16_t makeWord(uint8_t h, uint8_t l) {
return (h << 8) | l;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2012 Arduino. All right reserved.
Copyright (c) 2014 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -16,19 +16,21 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef RESET_H
#define RESET_H
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif
extern "C" void __cxa_pure_virtual(void) __attribute__((__noreturn__));
extern "C" void __cxa_deleted_virtual(void) __attribute__((__noreturn__));

void initiateReset(int ms);
void tickReset();
void cancelReset();

#ifdef __cplusplus
void __cxa_pure_virtual(void) {
// We might want to write some diagnostics to uart in this case
// std::terminate();
while (1)
;
}
#endif

#endif
void __cxa_deleted_virtual(void) {
// We might want to write some diagnostics to uart in this case
// std::terminate();
while (1)
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifdef ARDUINO_AMEBA
#include <Arduino.h>
#else
#include <stdio.h>
#endif

char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
char *dtostrf(double val, signed char width, unsigned char prec, char *sout) {
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2012 Arduino. All right reserved.
Copyright (c) 2015 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -29,12 +29,7 @@ static void __empty() {
// Empty
}

#include "cmsis_os.h"

void yield(void) {
vTaskDelay(1);
taskYIELD();
}
void yield(void) __attribute__((weak, alias("__empty")));

/**
* SysTick hook
Expand All @@ -46,7 +41,8 @@ static int __false() {
// Return false
return 0;
}
int sysTickHook(void) __attribute__ ((weak, alias("__false")));

int sysTickHook(void) __attribute__((weak, alias("__false")));

/**
* SVC hook
Expand All @@ -60,5 +56,6 @@ static void __halt() {
while (1)
;
}
void svcHook(void) __attribute__ ((weak, alias("__halt")));
void pendSVHook(void) __attribute__ ((weak, alias("__halt")));

void svcHook(void) __attribute__((weak, alias("__halt")));
void pendSVHook(void) __attribute__((weak, alias("__halt")));
111 changes: 111 additions & 0 deletions arduino/libretuya/common/itoa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright (c) 2014 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

char *ltoa(long value, char *string, int radix) {
char tmp[33];
char *tp = tmp;
long i;
unsigned long v;
int sign;
char *sp;

if (string == NULL) {
return 0;
}

if (radix > 36 || radix <= 1) {
return 0;
}

sign = (radix == 10 && value < 0);
if (sign) {
v = -value;
} else {
v = (unsigned long)value;
}

while (v || tp == tmp) {
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i + '0';
else
*tp++ = i + 'a' - 10;
}

sp = string;

if (sign)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;

return string;
}

char *ultoa(unsigned long value, char *string, int radix) {
char tmp[33];
char *tp = tmp;
long i;
unsigned long v = value;
char *sp;

if (string == NULL) {
return 0;
}

if (radix > 36 || radix <= 1) {
return 0;
}

while (v || tp == tmp) {
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i + '0';
else
*tp++ = i + 'a' - 10;
}

sp = string;

while (tp > tmp)
*sp++ = *--tp;
*sp = 0;

return string;
}

char *itoa(int value, char *string, int radix) {
return ltoa(value, string, radix);
}

char *utoa(unsigned int value, char *string, int radix) {
return ultoa(value, string, radix);
}

#ifdef __cplusplus
} // extern "C"
#endif
54 changes: 54 additions & 0 deletions arduino/libretuya/common/wiring_shift.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright (c) 2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include <stdint.h>

uint8_t shiftIn(pin_size_t ulDataPin, pin_size_t ulClockPin, BitOrder ulBitOrder) {
uint8_t value = 0;
uint8_t i;

for (i = 0; i < 8; ++i) {
digitalWrite(ulClockPin, HIGH);

if (ulBitOrder == LSBFIRST) {
value |= digitalRead(ulDataPin) << i;
} else {
value |= digitalRead(ulDataPin) << (7 - i);
}

digitalWrite(ulClockPin, LOW);
}

return value;
}

void shiftOut(pin_size_t ulDataPin, pin_size_t ulClockPin, BitOrder ulBitOrder, uint8_t ulVal) {
uint8_t i;

for (i = 0; i < 8; i++) {
if (ulBitOrder == LSBFIRST) {
digitalWrite(ulDataPin, !!(ulVal & (1 << i)));
} else {
digitalWrite(ulDataPin, !!(ulVal & (1 << (7 - i))));
}

digitalWrite(ulClockPin, HIGH);
digitalWrite(ulClockPin, LOW);
}
}
Loading

0 comments on commit 589d3ef

Please sign in to comment.