Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
Language: Cpp
BasedOnStyle: LLVM
PointerAlignment: Right
ReferenceAlignment: Pointer
AllowShortIfStatementsOnASingleLine: Never
AllowShortFunctionsOnASingleLine: All
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: false
IndentAccessModifiers: false
AccessModifierOffset: -2
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 100
PenaltyExcessCharacter: 1000000
RequiresClausePosition: OwnLine
76 changes: 76 additions & 0 deletions .github/workflows/githubci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Clang-Format Auto-Fix

on:
pull_request:
branches: [main, dev]
workflow_dispatch:

permissions:
contents: write

jobs:
clang_format:
runs-on: ubuntu-latest

steps:
- name: Checkout (PR head)
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}

- name: Checkout ci-loom scripts
uses: actions/checkout@v4
with:
repository: OPEnSLab-OSU/ci-loom
ref: main
path: ci-loom

- name: Install clang-format
run: |
sudo apt-get update
sudo apt-get install -y clang-format python3

- name: "Format (PR: changed files only)"
if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: |
set -euo pipefail

BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"

mapfile -t files < <(git diff --name-only --diff-filter=ACMR "$BASE_SHA" "$HEAD_SHA" \
| grep -E '\.(c|cc|cpp|cxx|c\+\+|h|hh|hpp|hxx|h\+\+|C|H)$' || true)

if [ ${#files[@]} -eq 0 ]; then
echo "No C/C++ files changed."
exit 0
fi

rc=0
python3 ci-loom/run-clang-format.py "${files[@]}" -i || rc=$?

if [ "$rc" -gt 1 ]; then
exit "$rc"
fi

- name: "Check format (act: whole repo)"
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
# Always succeeds on local test
python3 ci-loom/run-clang-format.py -r . -e "ci-loom/*" -e "bin/*" || true

- name: Commit fixes
if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A -- ':!ci-loom'
if git diff --staged --quiet; then
echo "No formatting changes to commit."
exit 0
fi

git commit -m "Auto-format with clang-format"
git push origin HEAD:${{ github.head_ref }}
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
.vscode
.DS_Store
.clang-format
.clangd
.justfile
.ci
clang-format-results.txt

# Credentials/secrets
**/arduino_secrets.h
**/wifi_creds.json
**/mqtt_creds.json
.secrets
35 changes: 35 additions & 0 deletions examples/Hypnos/Hypnos_vCheck/Hypnos_vCheck.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Voltage check example with minimum acceptable voltage and voltage flagging.
* Uses analog class as the basis for voltage checks
*
*/

#include <Loom_Manager.h>
#include <Logger.h>
#include <Hardware/Loom_Hypnos/Loom_Hypnos.h>


Manager manager("Device", 1);

Loom_Hypnos hypnos(manager, HYPNOS_VERSION::V3_3, TIME_ZONE::PST, false, false);

void setup(){
ENABLE_SD_LOGGING;
ENABLE_FUNC_SUMMARIES;

// Start the serial monitor
manager.beginSerial();

// Begin Initilization
manager.initialize();
}

void loop(){

// Run voltage check with default args (voltage_min = 0.0, pin = A7, scale = 2.0)
// Scale refers to the voltage divisor that reduces a higher voltage to a lower acceptable voltage.
// Vout = Vsource x Resistance2 / (Resistance1 + Resistance2)
hypnos.checkVoltage(4.0f, A7, 2.0);

manager.pause(5000);
}
70 changes: 23 additions & 47 deletions examples/Lab Examples/Dendrometer/node/AS5311.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ AS5311::AS5311(uint8_t cs_pin, uint8_t clk_pin, uint8_t do_pin)
/**
* Initialize pins for serial read procedure
*/
void AS5311::initializePins()
{
void AS5311::initializePins() {
// initalize pins
digitalWrite(CS_PIN, HIGH);
pinMode(CS_PIN, OUTPUT);
Expand All @@ -25,8 +24,7 @@ void AS5311::initializePins()
/**
* deinitialize pins after serial read
*/
void AS5311::deinitializePins()
{
void AS5311::deinitializePins() {
pinMode(CS_PIN, INPUT);
digitalWrite(CS_PIN, LOW);
pinMode(CLK_PIN, INPUT);
Expand All @@ -38,16 +36,12 @@ void AS5311::deinitializePins()
* Returns the serial output from AS533
* @return 32 bit value, of which the 18 least signifcant bits contain the sensor data
*/
uint32_t AS5311::bitbang(bool angleData = true)
{
uint32_t AS5311::bitbang(bool angleData = true) {
initializePins();

if (angleData)
{
if (angleData) {
digitalWrite(CLK_PIN, HIGH); // write clock high to select the angular position data
}
else
{
} else {
digitalWrite(CLK_PIN, LOW); // write clock high to select the magnetic field strength data
}

Expand All @@ -61,23 +55,20 @@ uint32_t AS5311::bitbang(bool angleData = true)

uint32_t data = 0;
const uint8_t BITS = 18;
for (int i = 0; i < BITS; i++)
{
for (int i = 0; i < BITS; i++) {
delayMicroseconds(DATA_TIMING_US);
digitalWrite(CLK_PIN, HIGH);

// don't set clock low on last bit
if (i < (BITS - 1))
{
if (i < (BITS - 1)) {
delayMicroseconds(DATA_TIMING_US);
digitalWrite(CLK_PIN, LOW);
}

delayMicroseconds(DATA_TIMING_US);

auto readval = digitalRead(DO_PIN);
if (readval == HIGH)
{
if (readval == HIGH) {
data |= 1 << (BITS - 1) - i;
}
}
Expand All @@ -95,12 +86,12 @@ uint32_t AS5311::bitbang(bool angleData = true)
* See pages 12 to 15 of the AS5311 datasheet for more information
* @return magnetStatus enum
*/
magnetStatus AS5311::getMagnetStatus()
{
magnetStatus AS5311::getMagnetStatus() {
uint32_t data = bitbang();

// invalid data
if (!(data & (1 << OCF)) || data & (1 << COF) || __builtin_parity(data)) //__builtin_parity returns 1 if odd parity
if (!(data & (1 << OCF)) || data & (1 << COF) ||
__builtin_parity(data)) //__builtin_parity returns 1 if odd parity
return magnetStatus::error;

// magnetic field out of range
Expand All @@ -118,38 +109,27 @@ magnetStatus AS5311::getMagnetStatus()
* Return the raw sensor binary data
* @return raw sensor data
*/
uint32_t AS5311::getRawData()
{
return bitbang(true);
}
uint32_t AS5311::getRawData() { return bitbang(true); }

/**
* Right shift the raw sensor data to isolate the absolute position component
* @return 12-bit absolute postion value
*/
uint16_t AS5311::getPosition()
{
return bitbang(true) >> DATAOFFSET;
}
uint16_t AS5311::getPosition() { return bitbang(true) >> DATAOFFSET; }

/**
* Right shift the raw sensor data to isolate the field strength component
* @return 12-bit magnetic field strength value
*/
uint16_t AS5311::getFieldStrength()
{
return bitbang(false) >> DATAOFFSET;
}
uint16_t AS5311::getFieldStrength() { return bitbang(false) >> DATAOFFSET; }

/**
* Takes multiple position measurements and average them
* @return averaged 12-bit absolute position value
*/
uint16_t AS5311::getFilteredPosition()
{
uint16_t AS5311::getFilteredPosition() {
uint16_t average = 0;
for (int i = 0; i < AVERAGE_MEASUREMENTS; i++)
{
for (int i = 0; i < AVERAGE_MEASUREMENTS; i++) {
average += getPosition();
}
average /= AVERAGE_MEASUREMENTS;
Expand All @@ -159,8 +139,7 @@ uint16_t AS5311::getFilteredPosition()
/**
* Record the data from the magnet sensor, process it, and add it to the manager's packet.
*/
void AS5311::measure(Manager &manager)
{
void AS5311::measure(Manager &manager) {
int filteredPosition = (int)getFilteredPosition();

recordMagnetStatus(manager);
Expand All @@ -173,10 +152,10 @@ void AS5311::measure(Manager &manager)
/**
* Calculate the displacement of the magnet given a position.
* Keeps a persistent count of sensor range overflows
* Moving the sensor too much (about 1mm) in between calls to this function will result in invalid data.
* Moving the sensor too much (about 1mm) in between calls to this function will result in invalid
* data.
*/
float AS5311::measureDisplacement(int pos)
{
float AS5311::measureDisplacement(int pos) {
static const int WRAP_THRESHOLD = 2048;
static const int TICKS = 4096; // 2^12 == 4096 see datasheet page 10
static const float POLE_PAIR_LENGTH_UM = 2000.0; // 2mm == 2000um
Expand All @@ -189,8 +168,7 @@ float AS5311::measureDisplacement(int pos)
int magnetPosition = pos;

int difference = magnetPosition - lastPosition;
if (abs(difference) > WRAP_THRESHOLD)
{
if (abs(difference) > WRAP_THRESHOLD) {
if (difference < 0) // high to low overflow
overflows += 1;
else // low to high overflow
Expand All @@ -204,11 +182,9 @@ float AS5311::measureDisplacement(int pos)
/**
* Record the alignment status of the magnet sensor
*/
void AS5311::recordMagnetStatus(Manager &manager)
{
void AS5311::recordMagnetStatus(Manager &manager) {
magnetStatus status = getMagnetStatus();
switch (status)
{
switch (status) {
case magnetStatus::red:
manager.addData("AS5311", "Alignment", "Red");
break;
Expand Down
20 changes: 7 additions & 13 deletions examples/Lab Examples/Dendrometer/node/AS5311.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,29 @@
#include <Arduino.h>
#include <Loom_Manager.h>

enum class magnetStatus
{
red,
green,
yellow,
error
};
enum class magnetStatus { red, green, yellow, error };

class AS5311
{
public:
class AS5311 {
public:
AS5311(uint8_t cs_pin, uint8_t clk_pin, uint8_t do_pin);
magnetStatus getMagnetStatus();
uint16_t getFilteredPosition();
uint16_t getFieldStrength();
uint32_t getRawData();

void measure(Manager &);
float measureDisplacement(int);

private:
private:
const uint8_t CS_PIN;
const uint8_t CLK_PIN;
const uint8_t DO_PIN;

static const int DATA_TIMING_US;
static const int AVERAGE_MEASUREMENTS;

int initialPosition = -1; //negative number indicates that initial position has not been measured
int initialPosition =
-1; // negative number indicates that initial position has not been measured
int lastPosition = 0;
int overflows = 0;

Expand Down
Loading