Skip to content

Commit

Permalink
Add cpp format (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
sktometometo authored Jan 25, 2024
1 parent 038282e commit 6429609
Show file tree
Hide file tree
Showing 17 changed files with 869 additions and 972 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: Google
ColumnLimit: 0
24 changes: 24 additions & 0 deletions .github/workflows/clang_format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Clang formatting

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
clang-format-check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Clang Format
run: sudo apt-get install -y clang-format
- name: Check Clang Format for arduino_lib
run: |
find arduino_lib -name '*.cpp' -o -name '*.h' | xargs clang-format -i
git diff --exit-code
- name: Check Clang Format for sketches
run: |
find arduino_lib -name '*.cpp' -o -name '*.h' | xargs clang-format -i
git diff --exit-code
48 changes: 13 additions & 35 deletions arduino_lib/ArduinoAtomS3Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,35 @@

#define SERIAL_CLASS HWCDC

class ArduinoHardware
{
public:
ArduinoHardware(SERIAL_CLASS* io, long baud = 57600)
{
class ArduinoHardware {
public:
ArduinoHardware(SERIAL_CLASS* io, long baud = 57600) {
iostream = io;
baud_ = baud;
}
ArduinoHardware()
{
ArduinoHardware() {
iostream = &Serial;
baud_ = 57600;
}
ArduinoHardware(ArduinoHardware& h)
{
ArduinoHardware(ArduinoHardware& h) {
this->iostream = h.iostream;
this->baud_ = h.baud_;
}

void setBaud(long baud)
{
this->baud_ = baud;
}
void setBaud(long baud) { this->baud_ = baud; }

int getBaud()
{
return baud_;
}
int getBaud() { return baud_; }

void init()
{
iostream->begin(baud_);
}
void init() { iostream->begin(baud_); }

int read()
{
return iostream->read();
};
void write(uint8_t* data, int length)
{
for (int i = 0; i < length; i++)
iostream->write(data[i]);
int read() { return iostream->read(); };
void write(uint8_t* data, int length) {
for (int i = 0; i < length; i++) iostream->write(data[i]);
}

unsigned long time()
{
return millis();
}
unsigned long time() { return millis(); }

protected:
protected:
SERIAL_CLASS* iostream;
long baud_;
};

42 changes: 20 additions & 22 deletions arduino_lib/devices/stickv2_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@
#define STICKV2_UTIL_H

#include <Arduino.h>

#include <ArduinoJson.h>

#define BUFSIZE 2048

bool send_data_to_serial(HardwareSerial &serial, StaticJsonDocument<BUFSIZE> &doc)
{
String request;
serializeJson(doc, request);
serial.println(request);
return true;
bool send_data_to_serial(HardwareSerial &serial,
StaticJsonDocument<BUFSIZE> &doc) {
String request;
serializeJson(doc, request);
serial.println(request);
return true;
}

bool read_data_from_serial(HardwareSerial &serial, StaticJsonDocument<BUFSIZE> &doc)
{
String response = serial.readStringUntil('\n');
DeserializationError error = deserializeJson(doc, response);
if (error)
{
return false;
}
return true;
bool read_data_from_serial(HardwareSerial &serial,
StaticJsonDocument<BUFSIZE> &doc) {
String response = serial.readStringUntil('\n');
DeserializationError error = deserializeJson(doc, response);
if (error) {
return false;
}
return true;
}

bool set_object_recognition_model(HardwareSerial &serial, const String &model_path)
{
StaticJsonDocument<BUFSIZE> doc;
doc["function"] = "object_recognition";
doc["args"][0] = model_path;
return send_data_to_serial(serial, doc);
bool set_object_recognition_model(HardwareSerial &serial,
const String &model_path) {
StaticJsonDocument<BUFSIZE> doc;
doc["function"] = "object_recognition";
doc["args"][0] = model_path;
return send_data_to_serial(serial, doc);
}

#endif
61 changes: 20 additions & 41 deletions arduino_lib/devices/uwb_module_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define UWB_MODULE_UTIL_H

#include <Arduino.h>

#include <optional>

/**
Expand All @@ -14,30 +15,24 @@ String testUWB(HardwareSerial& serial);
bool resetUWB(HardwareSerial& serial);
bool initUWB(bool tag, int id, HardwareSerial& serial);

std::optional<String> readUWB(HardwareSerial& serial, int timeout = 1000)
{
std::optional<String> readUWB(HardwareSerial& serial, int timeout = 1000) {
String DATA;
auto start = millis();
while (timeout > millis() - start)
{
if (serial.available())
{
while (timeout > millis() - start) {
if (serial.available()) {
DATA = serial.readStringUntil('\n');
return DATA;
}
}
return std::nullopt;
}

std::optional<std::tuple<int, float>> getDistanceUWB(HardwareSerial& serial)
{
std::optional<std::tuple<int, float>> getDistanceUWB(HardwareSerial& serial) {
std::optional<String> ret = readUWB(serial);
if (ret)
{
if (ret) {
auto data = *ret;
auto index = data.indexOf(':');
if (index != -1)
{
if (index != -1) {
auto id_str = data.substring(0, index);
id_str.replace(String("an"), String(""));
auto id = id_str.toInt();
Expand All @@ -50,59 +45,45 @@ std::optional<std::tuple<int, float>> getDistanceUWB(HardwareSerial& serial)
return std::nullopt;
}

void clearReadUWB(HardwareSerial& serial, int timeout = 1000)
{
void clearReadUWB(HardwareSerial& serial, int timeout = 1000) {
auto start = millis();
while (timeout > millis() - start)
{
if (serial.available())
{
while (timeout > millis() - start) {
if (serial.available()) {
serial.readStringUntil('\n');
}
}
}

String testUWB(HardwareSerial& serial)
{
String testUWB(HardwareSerial& serial) {
serial.write("AT\r\n");
delay(100);
auto ret = readUWB(serial);
if (ret)
{
if (ret) {
return *ret;
}
else
{
} else {
return "No response";
}
}

bool resetUWB(HardwareSerial& serial)
{
bool resetUWB(HardwareSerial& serial) {
serial.write("AT+RST\r\n");
delay(500);
auto ret = readUWB(serial);
clearReadUWB(serial);
if (ret)
{
if (ret) {
return true;
}
else
{
} else {
return false;
}
}

bool initUWB(bool tag, int id, HardwareSerial& serial)
{
bool initUWB(bool tag, int id, HardwareSerial& serial) {
String DATA;
auto ret = resetUWB(serial);
if (!ret)
{
if (!ret) {
return false;
}
if (tag)
{
if (tag) {
serial.printf("AT+anchor_tag=0\r\n", id);
delay(100);
readUWB(serial);
Expand All @@ -116,9 +97,7 @@ bool initUWB(bool tag, int id, HardwareSerial& serial)
serial.write("AT+switchdis=1\r\n");
delay(100);
readUWB(serial);
}
else
{
} else {
serial.printf("AT+anchor_tag=1,%d\r\n", id);
delay(100);
readUWB(serial);
Expand Down
Loading

0 comments on commit 6429609

Please sign in to comment.