Skip to content

Commit

Permalink
Clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickyleitor committed May 30, 2024
1 parent 2202142 commit 93981ea
Show file tree
Hide file tree
Showing 19 changed files with 990 additions and 914 deletions.
43 changes: 43 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
BasedOnStyle: Google
AlignAfterOpenBracket: AlwaysBreak
AlignArrayOfStructures: Right
AlignConsecutiveAssignments:
Enabled: true
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveBitFields:
Enabled: true
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveDeclarations:
Enabled: false
AlignConsecutiveMacros:
Enabled: false
AlignEscapedNewlines: Left
AlignOperands: AlignAfterOperator
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortIfStatementsOnASingleLine: Never
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Attach
ColumnLimit: 100
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
DerivePointerAlignment: false
IncludeBlocks: Regroup
IndentWidth: 4
InsertBraces: true
InsertNewlineAtEOF: true
KeepEmptyLinesAtEOF: true
KeepEmptyLinesAtTheStartOfBlocks: false
LineEnding: DeriveCRLF
MaxEmptyLinesToKeep: 1
PenaltyReturnTypeOnItsOwnLine: 2000
SkipMacroDefinitionBody: true
SpacesBeforeTrailingComments: 1
TabWidth: 4
259 changes: 135 additions & 124 deletions Firmware_Arduino/Firmware_Arduino.ino
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
/**
* @file Domotic Shutter sketch by Mickyleitor
* @brief This sketch is in development and uses ATmega368P.
*
*
* @details
* Libraries used and need to be installed from Arduino IDE:
* - Low-Power by rocketscream/ version 1.6.0: https://github.com/rocketscream/Low-Power
*
* Current protocol for I2C messages with master is addressed by RSCP (Roller Shutter Control Panel) protocol.
*
* Current protocol for I2C messages with master is addressed by RSCP (Roller Shutter Control Panel)
* protocol.
*/

#include <Wire.h>
#include "basic_defines.h"

#include "LowPower.h"
#include "remote.h"
#include "TaskBuffer.h"
#include "basic_defines.h"
#include "moduleConfigs/radioProtocolConfig.h"
#include "remote.h"
#include "rscpProtocol/rscpProtocol.h"


/**
* @enum SystemState
* @brief Enumeration of system states.
*/
enum SystemState {
LOW_POWER, ///< Low power state
IDLE, ///< Idle state
RUNNING, ///< Running state
SWITCH_RELAY ///< Switch relay state
LOW_POWER, ///< Low power state
IDLE, ///< Idle state
RUNNING, ///< Running state
SWITCH_RELAY ///< Switch relay state
};

SystemState currentSystemState = RUNNING;
Expand All @@ -36,169 +39,177 @@ bool isBuzzerRunning = false;
* @brief Setup function for Arduino.
*/
void setup() {
pinMode(PIN_BUZZER, OUTPUT);
pinMode(RADIOPROTOCOL_PINNUMBER_RF_TX, INPUT);
pinMode(RADIOPROTOCOL_PINNUMBER_RF_RX, INPUT);
pinMode(PIN_RELAY, OUTPUT);
initButtonsFunction();

Serial.begin(115200);
Serial.println("Slave initialized");
Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);

currentSystemState = RUNNING;
pinMode(PIN_BUZZER, OUTPUT);
pinMode(RADIOPROTOCOL_PINNUMBER_RF_TX, INPUT);
pinMode(RADIOPROTOCOL_PINNUMBER_RF_RX, INPUT);
pinMode(PIN_RELAY, OUTPUT);
initButtonsFunction();

Serial.begin(115200);
Serial.println("Slave initialized");

Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);

currentSystemState = RUNNING;
}

/**
* @brief Main loop for Arduino.
*/
void loop() {
static uint32_t timeout_ms;
rscpTaskType task;

switch (currentSystemState) {
case LOW_POWER: {
Serial.println("Entering low-power mode...");
Serial.flush();
LowPower.idle(SLEEP_FOREVER, ADC_OFF, TIMER2_OFF, TIMER1_ON, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_ON);
currentSystemState = RUNNING;
break;
}
case SWITCH_RELAY: {
digitalWrite(PIN_RELAY, digitalRead(PIN_BUTTON_USER));
Serial.println("Button pressed");
currentSystemState = RUNNING;
break;
}
case RUNNING: {
if (peekTaskFromBuffer(&task) < 0) {
timeout_ms = millis();
currentSystemState = IDLE;
break;
}

bool removeTask = true;

switch (task.command) {
case RSCP_CMD_SET_SHUTTER_ACTION: {
struct RSCP_Arg_rollershutter* arg = (struct RSCP_Arg_rollershutter*)task.arg;
if (arg->shutter <= 2 && arg->action <= 3) {
for (int retries = 0; retries < arg->retries; retries++) {
sendCommand(arg->shutter, arg->action - 1);
}
}
break;
static uint32_t timeout_ms;
rscpTaskType task;

switch (currentSystemState) {
case LOW_POWER: {
Serial.println("Entering low-power mode...");
Serial.flush();
LowPower.idle(
SLEEP_FOREVER,
ADC_OFF,
TIMER2_OFF,
TIMER1_ON,
TIMER0_OFF,
SPI_OFF,
USART0_OFF,
TWI_ON);
currentSystemState = RUNNING;
break;
}
case RSCP_CMD_SET_SWITCH_RELAY: {
struct RSCP_Arg_switchrelay* arg = (struct RSCP_Arg_switchrelay*)task.arg;
digitalWrite(PIN_RELAY, (arg->status == RSCP_DEF_SWITCH_RELAY_ON) ? HIGH : LOW);
break;
case SWITCH_RELAY: {
digitalWrite(PIN_RELAY, digitalRead(PIN_BUTTON_USER));
Serial.println("Button pressed");
currentSystemState = RUNNING;
break;
}
case RSCP_CMD_SET_BUZZER_ACTION: {
struct RSCP_Arg_buzzer_action* arg = (struct RSCP_Arg_buzzer_action*)task.arg;
switch (arg->action) {
case RSCP_DEF_BUZZER_ACTION_ON: {
removeTask = false;
if (isBuzzerRunning) {
if ((millis() - timeout_ms) > arg->duration_ms) {
noTone(PIN_BUZZER);
isBuzzerRunning = false;
removeTask = true;
break;
}
case RUNNING: {
if (peekTaskFromBuffer(&task) < 0) {
timeout_ms = millis();
currentSystemState = IDLE;
break;
}
tone(PIN_BUZZER, arg->volume, arg->duration_ms);
timeout_ms = millis();
isBuzzerRunning = true;
break;
}
case RSCP_DEF_BUZZER_ACTION_OFF: {
noTone(PIN_BUZZER);
isBuzzerRunning = false;
break;

bool removeTask = true;

switch (task.command) {
case RSCP_CMD_SET_SHUTTER_ACTION: {
struct RSCP_Arg_rollershutter* arg = (struct RSCP_Arg_rollershutter*)task.arg;
if (arg->shutter <= 2 && arg->action <= 3) {
for (int retries = 0; retries < arg->retries; retries++) {
sendCommand(arg->shutter, arg->action - 1);
}
}
break;
}
case RSCP_CMD_SET_SWITCH_RELAY: {
struct RSCP_Arg_switchrelay* arg = (struct RSCP_Arg_switchrelay*)task.arg;
digitalWrite(PIN_RELAY, (arg->status == RSCP_DEF_SWITCH_RELAY_ON) ? HIGH : LOW);
break;
}
case RSCP_CMD_SET_BUZZER_ACTION: {
struct RSCP_Arg_buzzer_action* arg = (struct RSCP_Arg_buzzer_action*)task.arg;
switch (arg->action) {
case RSCP_DEF_BUZZER_ACTION_ON: {
removeTask = false;
if (isBuzzerRunning) {
if ((millis() - timeout_ms) > arg->duration_ms) {
noTone(PIN_BUZZER);
isBuzzerRunning = false;
removeTask = true;
break;
}
break;
}
tone(PIN_BUZZER, arg->volume, arg->duration_ms);
timeout_ms = millis();
isBuzzerRunning = true;
break;
}
case RSCP_DEF_BUZZER_ACTION_OFF: {
noTone(PIN_BUZZER);
isBuzzerRunning = false;
break;
}
}
break;
}
}
}
break;
}
}

if (removeTask) {
popTaskFromBuffer(&task);
}
if (removeTask) {
popTaskFromBuffer(&task);
}

break;
}
case IDLE: {
if ((millis() - timeout_ms) > IDLE_TIMEOUT_MS) {
currentSystemState = LOW_POWER;
}
break;
break;
}
case IDLE: {
if ((millis() - timeout_ms) > IDLE_TIMEOUT_MS) {
currentSystemState = LOW_POWER;
}
break;
}
}
}
}

/**
* @brief Function that executes whenever data is received from the master.
*
*
* @param howMany Number of bytes received.
*/
void receiveEvent(int howMany) {
// Read all bytes from I2C later in requestEvent
// Read all bytes from I2C later in requestEvent
}

/**
* @brief Function that executes whenever data is requested by the master.
*/
void requestEvent() {
int8_t err;
if ((err = rscpHandle(I2C_TIMEOUT_MS)) < 0) {
Serial.println("[ERROR] rscpHandle: " + String(err));
}
currentSystemState = RUNNING;
int8_t err;
if ((err = rscpHandle(I2C_TIMEOUT_MS)) < 0) {
Serial.println("[ERROR] rscpHandle: " + String(err));
}
currentSystemState = RUNNING;
}

/**
* @brief Interrupt service routine for the button.
*/
void isrButton() {
// Detach the interrupt to avoid retriggering during debounce
detachInterrupt(digitalPinToInterrupt(PIN_BUTTON_USER));
// Detach the interrupt to avoid retriggering during debounce
detachInterrupt(digitalPinToInterrupt(PIN_BUTTON_USER));

// Reset Timer 1 counter to 0 and start counting (CTC mode, scale to clock / 256)
TCNT1 = 0;
TCCR1B = bit(WGM12) | bit(CS12);
// Reset Timer 1 counter to 0 and start counting (CTC mode, scale to clock / 256)
TCNT1 = 0;
TCCR1B = bit(WGM12) | bit(CS12);
}

/**
* @brief Interrupt service routine for Timer 1 Compare A Match.
*/
ISR(TIMER1_COMPA_vect) {
currentSystemState = SWITCH_RELAY;
currentSystemState = SWITCH_RELAY;

// Stop Timer 1 (clear prescaler) and reattach the button interrupt on changing edge
TCCR1B = 0;
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON_USER), isrButton, CHANGE);
// Stop Timer 1 (clear prescaler) and reattach the button interrupt on changing edge
TCCR1B = 0;
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON_USER), isrButton, CHANGE);
}

/**
* @brief Initialize button functions.
*/
void initButtonsFunction() {
pinMode(PIN_BUTTON_USER, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON_USER), isrButton, CHANGE);
pinMode(PIN_BUTTON_USER, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON_USER), isrButton, CHANGE);

// Set up Timer 1 for debounce
TCCR1A = 0; // Normal operation
TCCR1B = 0; // Stop the timer initially
// Set up Timer 1 for debounce
TCCR1A = 0; // Normal operation
TCCR1B = 0; // Stop the timer initially

// Calculate the compare A register value for debounce time
uint32_t debounce_cycles = (F_CPU / (256UL * 2 * 1000));
OCR1A = (debounce_cycles * DEBOUNCE_TIME_MILLIS) - 1;
// Calculate the compare A register value for debounce time
uint32_t debounce_cycles = (F_CPU / (256UL * 2 * 1000));
OCR1A = (debounce_cycles * DEBOUNCE_TIME_MILLIS) - 1;

// Enable the interrupt on Compare A Match (Timer 1)
TIMSK1 = bit(OCIE1A);
// Enable the interrupt on Compare A Match (Timer 1)
TIMSK1 = bit(OCIE1A);
}
Loading

0 comments on commit 93981ea

Please sign in to comment.