Skip to content

Commit

Permalink
Add clang format (#8)
Browse files Browse the repository at this point in the history
* Add Mozilla clang-format style

* Custom `.clang-format`

to have the least diff to current style

* Apply `clang-format`

* Add CI for `clang-format`

* Fix Github actions copy/paste

* Fix CI again

* Also `clang-format` ino files

* Add emptyline at EOF

* Temporarily disable NewLine
  • Loading branch information
dmohns authored Feb 26, 2024
1 parent d7fda35 commit 10cdc7e
Show file tree
Hide file tree
Showing 25 changed files with 609 additions and 590 deletions.
21 changes: 21 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html

BasedOnStyle: Google
Standard: Cpp03
AllowShortFunctionsOnASingleLine: Empty
IncludeBlocks: Preserve
IndentPPDirectives: AfterHash
DerivePointerAlignment: false
# This is only available in Clang-16, see
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html#insertnewlineateof
# However, Github runners currently only ship with Clang-15, see
# https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2204-Readme.md
# InsertNewlineAtEOF: true

# Always break after if to get accurate coverage
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false

# custom
IndentWidth: 4
ColumnLimit: 160
18 changes: 18 additions & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Check

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
clang-format:
name: Lint clang-format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run clang-format lint
run: find . \( -name '*.[ch]pp' -o -name '*.h' -o -name '*.ino' \) | xargs clang-format --verbose --style=file -n --Werror
11 changes: 5 additions & 6 deletions factory_setup.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include "factory_setup.h"


bool isFactorySetupDone() {
return setupComplete == SETUP_COMPLETE_MAGIC_NUMBER;
}


void handleReceivedSetupData() {
if (Serial.read() == '#') {
// Read the serial number until ';' is encountered and convert to int
String input = Serial.readStringUntil(';');
if(input == "SETUP") {
if (input == "SETUP") {
input = Serial.readStringUntil(';');
sscanf(input.c_str(), "%d", &serialNumber);
// Read the starting code until ';' is encountered and convert to int
// Read the starting code until ';' is encountered and convert to
// int
input = Serial.readStringUntil(';');
sscanf(input.c_str(), "%d", &startingCode);
// Read the next part (hexadecimal string)
Expand All @@ -36,9 +35,9 @@ void handleReceivedSetupData() {
}
}


void factorySetupLoop() {
if (Serial.available() > 0) { // send data only when you receive data; condition in setupComplete in case the eeprom was badly initialized
if (Serial.available() > 0) { // send data only when you receive data; condition in
// setupComplete in case the eeprom was badly initialized
handleReceivedSetupData();
}

Expand Down
4 changes: 1 addition & 3 deletions factory_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
void factorySetupLoop();
bool isFactorySetupDone();


// Variables


#endif
#endif
20 changes: 7 additions & 13 deletions hal.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "hal.h"


ESP32Time rtc(0);


void setupPeripherals() {
pinMode(LOAD_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
Expand All @@ -13,20 +11,17 @@ void setupPeripherals() {
rtc.setTime();
}


void setupSerial() {
Serial.begin(SERIAL_RATE);
}


uint32_t getTimeInSeconds() {
return rtc.getEpoch();
}


void blinkLED(int numberOfBlinks, int LEDPin) {
int i;
for (i = 0; i < numberOfBlinks; i++){
int i;
for (i = 0; i < numberOfBlinks; i++) {
digitalWrite(LEDPin, HIGH);
// Wait for 1 second
delay(LED_BLINK_TIME);
Expand All @@ -50,29 +45,28 @@ void blinkBlueLED(int numberOfBlinks) {
}

void switchLoad(bool active) {
if(active) {
if (active) {
digitalWrite(LOAD_PIN, HIGH);
} else {
digitalWrite(LOAD_PIN, LOW);
}
}


// ----- Metrics collection functions ----- //

// For this example it uses random data, change it for actual measurements
uint8_t getNumberOfDaysOfMetrics() {
return random(1, 31);
return random(1, 31);
}

float getPowerGenerated(uint8_t day) {
return random(1000, 6000)/100.0f;
return random(1000, 6000) / 100.0f;
}

float getHoursOfLighting(uint8_t day) {
return random(100, 800)/100.0f;
return random(100, 800) / 100.0f;
}

float getAverageBatteryVoltage(uint8_t day) {
return random(1200, 1440)/100.0f;
return random(1200, 1440) / 100.0f;
}
17 changes: 8 additions & 9 deletions hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#define SERIAL_RATE 115200
// Token Settings
#define TIME_DIVIDER 1
//#define RESTRICTED_DIGIT_SET_MODE // Enable if you only have digits from 1-4
// #define RESTRICTED_DIGIT_SET_MODE // Enable if you only have digits from 1-4
#ifdef RESTRICTED_DIGIT_SET_MODE
#define TOKEN_LENGTH 15
# define TOKEN_LENGTH 15
#else
#define TOKEN_LENGTH 9
# define TOKEN_LENGTH 9
#endif

// Hardware defines
Expand All @@ -29,11 +29,11 @@

// Macros
#ifdef DEBUG_MODE
#define debugPrint(...) Serial.print(__VA_ARGS__)
#define debugPrintln(...) Serial.println(__VA_ARGS__)
# define debugPrint(...) Serial.print(__VA_ARGS__)
# define debugPrintln(...) Serial.println(__VA_ARGS__)
#else
#define debugPrint(...)
#define debugPrintln(...)
# define debugPrint(...)
# define debugPrintln(...)
#endif

// Functions
Expand All @@ -54,5 +54,4 @@ float getAverageBatteryVoltage(uint8_t day);

// Variables


#endif
#endif
154 changes: 76 additions & 78 deletions openpaygo-hdk.ino
Original file line number Diff line number Diff line change
@@ -1,99 +1,97 @@
#include "hal.h"
#include "factory_setup.h"
#include "hal.h"
#include "openpaygo_pass.h"
#include "openpaygo_token.h"


// -------- SETUP ---------

void setup() {
// Setup serial first
setupSerial();
debugPrintln("INFO: OpenPAYGO-HDK Starting...");
// Setup other peripherals (LEDs, RTC, etc.)
setupPeripherals();
// We load the variables from memory
loadSetupData();
// We check if setup done and load all data if done
if(isFactorySetupDone()) {
loadAllData();
// Setup Metrics
setupMetrics();
// Setup Pass
#ifdef PASS_ENABLED
setupPass();
#endif
debugPrintln("INFO: Factory setup done");
debugPrintln("INFO: Waiting for commands or NFC taps...");
} else {
debugPrintln("INFO: Factory setup NOT done");
debugPrintln("INFO: Waiting for Factory setup instruction...");
}
debugPrintln("INFO: OpenPAYGO-HDK Started!");
// If you want to add extra features, add their setup here
// Setup serial first
setupSerial();
debugPrintln("INFO: OpenPAYGO-HDK Starting...");
// Setup other peripherals (LEDs, RTC, etc.)
setupPeripherals();
// We load the variables from memory
loadSetupData();
// We check if setup done and load all data if done
if (isFactorySetupDone()) {
loadAllData();
// Setup Metrics
setupMetrics();
// Setup Pass
#ifdef PASS_ENABLED
setupPass();
#endif
debugPrintln("INFO: Factory setup done");
debugPrintln("INFO: Waiting for commands or NFC taps...");
} else {
debugPrintln("INFO: Factory setup NOT done");
debugPrintln("INFO: Waiting for Factory setup instruction...");
}
debugPrintln("INFO: OpenPAYGO-HDK Started!");
// If you want to add extra features, add their setup here
}

// -------- LOOP ---------

void loop() {
if (isFactorySetupDone()) {
loopToken();
loopCommands();
loopMetrics();
#ifdef PASS_ENABLED
loopPass();
#endif
// If you want to add extra features, add their loop here
} else {
factorySetupLoop();
}
if (isFactorySetupDone()) {
loopToken();
loopCommands();
loopMetrics();
#ifdef PASS_ENABLED
loopPass();
#endif
// If you want to add extra features, add their loop here
} else {
factorySetupLoop();
}
}


// -------- SERIAL COMMAND HANDLING ---------

void loopCommands() {
if (Serial.available() > 0) { // send data only when you receive data; condition in setupComplete in case the eeprom was badly initialized
handleSerialCommand();
}
if (Serial.available() > 0) { // send data only when you receive data; condition in setupComplete in case the eeprom was badly initialized
handleSerialCommand();
}
}

void handleSerialCommand() {
if (Serial.read() == '#') {
String input = Serial.readStringUntil(';');
debugPrint("INFO: Received Command: ");
debugPrintln(input);
if(input == "TOKEN") {
uint32_t inputToken;
input = Serial.readStringUntil('\n');
sscanf(input.c_str(), "%d", &inputToken);
int tokenStatus = tokenReceived(inputToken);
switch (tokenStatus) {
case TOKEN_INVALID:
Serial.println("#TOKEN;INVALID");
break;
case TOKEN_ALREADY_USED:
Serial.println("#TOKEN;ALREADY_USED");
break;
case TOKEN_PAYG_DISABLED:
Serial.println("#TOKEN;PAYG_DISABLED");
break;
default:
Serial.print("#TOKEN;VALID;");
Serial.print(getActivationTimeLeft());
break;
}
} else if (input == "STATUS") {
if(paygDisabled) {
Serial.println("#STATUS;PAYG_DISABLED");
} else if (isActive()) {
Serial.print("#STATUS;ACTIVE;");
Serial.println(getActivationTimeLeft());
} else {
Serial.print("#STATUS;INACTIVE");
}
} else {
Serial.println("#INVALID");
if (Serial.read() == '#') {
String input = Serial.readStringUntil(';');
debugPrint("INFO: Received Command: ");
debugPrintln(input);
if (input == "TOKEN") {
uint32_t inputToken;
input = Serial.readStringUntil('\n');
sscanf(input.c_str(), "%d", &inputToken);
int tokenStatus = tokenReceived(inputToken);
switch (tokenStatus) {
case TOKEN_INVALID:
Serial.println("#TOKEN;INVALID");
break;
case TOKEN_ALREADY_USED:
Serial.println("#TOKEN;ALREADY_USED");
break;
case TOKEN_PAYG_DISABLED:
Serial.println("#TOKEN;PAYG_DISABLED");
break;
default:
Serial.print("#TOKEN;VALID;");
Serial.print(getActivationTimeLeft());
break;
}
} else if (input == "STATUS") {
if (paygDisabled) {
Serial.println("#STATUS;PAYG_DISABLED");
} else if (isActive()) {
Serial.print("#STATUS;ACTIVE;");
Serial.println(getActivationTimeLeft());
} else {
Serial.print("#STATUS;INACTIVE");
}
} else {
Serial.println("#INVALID");
}
}
}
}
}
Loading

0 comments on commit 10cdc7e

Please sign in to comment.