Formatting

This commit is contained in:
Vsevolod Merenkov 2020-03-27 11:32:34 +03:00
parent 60a26ad3f7
commit f2c3843668
16 changed files with 343 additions and 433 deletions

View file

@ -0,0 +1,3 @@
BasedOnStyle: LLVM
ColumnLimit: 200
AllowShortFunctionsOnASingleLine: false

View file

@ -32,15 +32,15 @@
#endif
#include "LightChrono.h"
LightChrono::LightChrono()
{
LightChrono::LightChrono() {
restart();
}
void LightChrono::start() { restart(); }
void LightChrono::start() {
restart();
}
void LightChrono::restart()
{
void LightChrono::restart() {
_startTime = millis();
}
@ -48,8 +48,7 @@ LightChrono::chrono_t LightChrono::elapsed() const {
return (millis() - _startTime);
}
bool LightChrono::hasPassed(LightChrono::chrono_t timeout) const
{
bool LightChrono::hasPassed(LightChrono::chrono_t timeout) const {
return (elapsed() >= timeout);
}
@ -58,10 +57,7 @@ bool LightChrono::hasPassed(LightChrono::chrono_t timeout, bool restartIfPassed)
if (restartIfPassed)
restart();
return true;
}
else {
} else {
return false;
}
}

View file

@ -39,8 +39,7 @@
* // do something
* // ...
*/
class LightChrono
{
class LightChrono {
public:
#if defined(ARDUINO_ARC32_TOOLS)
typedef uint64_t chrono_t;
@ -68,5 +67,3 @@ public:
};
#endif

View file

@ -85,25 +85,25 @@ update the voltMeterConstant variable in pP_config.h with the correct value
------------------------------------------------------------*/
// Headers, variables, and functions
#include <Arduino.h>
#include <EEPROM.h>
#include "LightChrono.h"
#include "pP_pins.h"
#include <Arduino.h>
#include <EEPROM.h>
// #include "pP_config.h"
#include "pP_volatile.h"
#include "pP_function.h"
#include "pP_serial.h"
#include "pP_volatile.h"
// i2c input toggle. Uncomment to enable
#define I2C_INPUT
void setup() {
//Setup PWM on voltage follower (PD3)
// Setup PWM on voltage follower (PD3)
TCCR2A = (1 << COM2B1) | (0 << COM2B0) | (0 << WGM21) | (1 << WGM20);
TCCR2B = (0 << WGM22) | (0 << CS22) | (0 << CS21) | (1 << CS20);
DDRD |= (1 << DDD3);
//Setup PWM on comparator (PB1)
// Setup PWM on comparator (PB1)
TCCR1A = (1 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (1 << WGM10);
TCCR1B = (0 << WGM13) | (0 << WGM12) | (0 << CS12) | (0 << CS11) | (1 << CS10);
DDRB |= (1 << DDB1);
@ -140,7 +140,7 @@ void loop() {
if (BlinkCount > 0) {
BlinkState = !BlinkState;
digitalWriteFast(ERR_LED, BlinkState);
//digitalWriteFast(TRG_OUT, BlinkState);
// digitalWriteFast(TRG_OUT, BlinkState);
--BlinkCount;
}
@ -179,11 +179,11 @@ void loop() {
if (BlinkCount > 0) {
BlinkState = !BlinkState;
digitalWriteFast(ERR_LED, BlinkState);
// digitalWriteFast(TRG_OUT, BlinkState);
// digitalWriteFast(TRG_OUT, BlinkState);
--BlinkCount;
// } else {
// } else {
// Check for error state
// checkError();
// checkError();
} else {
digitalWriteFast(ERR_LED, 0);
}
@ -193,7 +193,7 @@ void loop() {
serialPrintState();
}
// Sets trigger output state to false after completing loop
//digitalWriteFast(TRG_OUT, HIGH);
// digitalWriteFast(TRG_OUT, HIGH);
sensorHReading = 0;
}
}

View file

@ -1,16 +1,14 @@
#ifndef PP_CMD_H
#define PP_CMD_H
#include "EEPROM.h"
#include "pP_config.h"
#include "pP_function.h"
#include "EEPROM.h"
/*------------------------------------------------*/
void updateGainFactor(int value)
{
if (value >= 0)
{
void updateGainFactor(int value) {
if (value >= 0) {
GAIN_FACTOR = value;
adjustGain();
EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR);
@ -19,10 +17,8 @@ void updateGainFactor(int value)
/*------------------------------------------------*/
void updateVFol(int value)
{
if (value >= 0)
{
void updateVFol(int value) {
if (value >= 0) {
followerThrs = value;
adjustFollow();
EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs);
@ -30,10 +26,8 @@ void updateVFol(int value)
}
/*------------------------------------------------*/
void updateVComp(int value)
{
if (value >= 0)
{
void updateVComp(int value) {
if (value >= 0) {
compThrs = value;
adjustComp();
EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs);
@ -42,40 +36,32 @@ void updateVComp(int value)
/*------------------------------------------------*/
void updateLoopDuration(int value)
{
if (value >= 0)
{
void updateLoopDuration(int value) {
if (value >= 0) {
LOOP_DUR = value;
EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR);
}
}
/*------------------------------------------------*/
void updateTrigDuration(int value)
{
if (value >= 0)
{
void updateTrigDuration(int value) {
if (value >= 0) {
TRG_DUR = value;
EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR);
}
}
/*------------------------------------------------*/
void updateHysteresis(int value)
{
if (value >= 0)
{
void updateHysteresis(int value) {
if (value >= 0) {
Hyst = value;
EEPROM.put(HYST_ADDRESS, Hyst);
}
}
/*------------------------------------------------*/
void updateLogic(int value)
{
if (value >= 0)
{
void updateLogic(int value) {
if (value >= 0) {
LOGIC = value;
EEPROM.put(LOGIC_ADDRESS, LOGIC);
pulse();
@ -83,20 +69,16 @@ void updateLogic(int value)
}
/*------------------------------------------------*/
void updatePzDet(int value)
{
if (value >= 0)
{
void updatePzDet(int value) {
if (value >= 0) {
PZDET = value;
EEPROM.put(PZDET_ADDRESS, PZDET);
}
}
/*------------------------------------------------*/
void updateConstant(long value)
{
if (value >= 0)
{
void updateConstant(long value) {
if (value >= 0) {
voltMeterConstant = value;
EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant);
}
@ -104,16 +86,12 @@ void updateConstant(long value)
/*------------------------------------------------*/
void updateDebug(int value)
{
if (value > 0)
{
void updateDebug(int value) {
if (value > 0) {
Debug = 1;
}
else if (value == 0)
{
} else if (value == 0) {
Debug = 0;
}
}
#endif //PP_CMD_H
#endif // PP_CMD_H

View file

@ -9,24 +9,19 @@
#include "pP_function.h"
#include "Arduino.h"
#include "pP_config.h"
#include "pP_volatile.h"
#include "pP_pins.h"
#include "pP_volatile.h"
#include "stdint.h"
void digitalWriteFast(uint8_t pin, uint8_t x)
{
if (pin / 8)
{ // pin >= 8
void digitalWriteFast(uint8_t pin, uint8_t x) {
if (pin / 8) { // pin >= 8
PORTB ^= (-x ^ PORTB) & (1 << (pin % 8));
}
else
{
} else {
PORTD ^= (-x ^ PORTD) & (1 << (pin % 8));
}
}
int analogReadFast(uint8_t ADCpin)
{
int analogReadFast(uint8_t ADCpin) {
byte ADCSRAoriginal = ADCSRA;
ADCSRA = (ADCSRA & B11111000) | 4;
int adc = analogRead(ADCpin);
@ -36,15 +31,13 @@ int analogReadFast(uint8_t ADCpin)
/*------------------------------------------------*/
void doubleFlash()
{
void doubleFlash() {
BlinkCount = 4;
}
/*------------------------------------------------*/
void pulse()
{
void pulse() {
digitalWriteFast(TRG_OUT, LOGIC);
sensorHReading = 1;
delay(TRG_DUR);
@ -55,8 +48,7 @@ void pulse()
/*------------------------------------------------*/
long readVcc()
{
long readVcc() {
// Read 1.1V reference against AVcc
// Atmega's Secret Voltmeter setup:
@ -103,8 +95,7 @@ If the scale_constant calculated is different from the default 1125300,
update the voltMeterConstant variable in pP_config.h with the correct value
--------------------------------------------------*/
void readVin()
{
void readVin() {
VOld = Vin;
Vin = readVcc();
followerLong = followerThrs * 1023L;
@ -117,8 +108,7 @@ void readVin()
/*------------------------------------------------*/
void adjustFollow()
{
void adjustFollow() {
/* Compares diffs of threshold vs read value
if positive, adjusts the follower to within
the range set above*/
@ -133,8 +123,7 @@ void adjustFollow()
/*------------------------------------------------*/
void adjustComp()
{
void adjustComp() {
compLong = compThrs * 1023L;
compInt = (long long)compLong / Vin;
compInt = (int)compInt;
@ -143,21 +132,17 @@ void adjustComp()
/*------------------------------------------------*/
void calibrateAlert()
{
void calibrateAlert() {
VLast = VOld - Vin;
if (VLast > Hyst || VLast < -Hyst)
{
if (VLast > Hyst || VLast < -Hyst) {
ERR_STATE = 1;
}
}
/*------------------------------------------------*/
void adjustGain()
{
switch (GAIN_FACTOR)
{
void adjustGain() {
switch (GAIN_FACTOR) {
case 4:
pinMode(GADJ_R0, OUTPUT);
digitalWriteFast(GADJ_R0, LOW);
@ -192,7 +177,7 @@ void adjustGain()
/*------------------------------------------------*/
//void checkError () {
// void checkError () {
// if (ERR_STATE == 1) {
// digitalWriteFast(ERR_LED, BlinkState);
// BlinkState = !BlinkState;
@ -205,12 +190,10 @@ void adjustGain()
/*------------------------------------------------*/
void pzConCheck()
{
void pzConCheck() {
PZ_STATE = digitalRead(PZDET_PIN);
if (PZ_STATE == PZDET)
{
//digitalWriteFast(TRG_OUT, LOGIC);
if (PZ_STATE == PZDET) {
// digitalWriteFast(TRG_OUT, LOGIC);
ERR_STATE = 1;
}
}

View file

@ -38,7 +38,7 @@ void adjustFollow();
void adjustComp();
void calibrateAlert();
void adjustGain();
//void checkError () {
// void checkError () {
// if (ERR_STATE == 1) {
// digitalWriteFast(ERR_LED, BlinkState);
// BlinkState = !BlinkState;
@ -50,4 +50,4 @@ void adjustGain();
//}
void pzConCheck();
#endif //PP_FUNCTION_H
#endif // PP_FUNCTION_H

View file

@ -1,16 +1,15 @@
#ifdef I2C_INPUT
#include <Arduino.h>
#include "pP_config.h"
#include "pP_i2c.h"
#include "pP_config.h"
#include <Arduino.h>
#include <Wire.h>
byte registerMap[regMapSize];
byte registerMapTemp[regMapSize - 1];
byte receivedCommands[maxBytes];
pP_i2c::pP_i2c(){
pP_i2c::pP_i2c() {
}
void pP_i2c::init() {
@ -24,19 +23,15 @@ void pP_i2c::i2cReportStatus() {
}
void pP_i2c::i2cReportVersion() {
}
void pP_i2c::i2cReportConfig() {
}
void pP_i2c::i2cReportIdentity() {
}
void pP_i2c::i2cRequestInput() {
}
void pP_i2c::i2cReply() {
@ -73,43 +68,43 @@ void pP_i2c::i2cInput(int bytesReceived) {
return;
break;
case 0x01:
followerInt = (int) cmdRcvd[1];
followerInt = (int)cmdRcvd[1];
return;
break;
case 0x02:
compInt = (int) cmdRcvd[1];
compInt = (int)cmdRcvd[1];
return;
break;
case 0x03:
GAIN_FACTOR = (int) cmdRcvd[1];
GAIN_FACTOR = (int)cmdRcvd[1];
return;
break;
case 0x04:
Hyst = (int) cmdRcvd[1];
Hyst = (int)cmdRcvd[1];
return;
break;
case 0x05:
LOOP_DUR = (int) cmdRcvd[1];
LOOP_DUR = (int)cmdRcvd[1];
return;
break;
case 0x06:
LOGIC = (int) cmdRcvd[1];
LOGIC = (int)cmdRcvd[1];
return;
break;
case 0x07:
PZDET = (int) cmdRcvd[1];
PZDET = (int)cmdRcvd[1];
return;
break;
case 0x08:
TRG_DUR = (int) cmdRcvd[1];
TRG_DUR = (int)cmdRcvd[1];
return;
break;
case 0x09:
DEBUG = (int) cmdRcvd[1];
DEBUG = (int)cmdRcvd[1];
return;
break;
case 0x0a:
voltMeterConstant = longRcvd[0]*65536+longRcvd[1]*256+longRcvd[2];
voltMeterConstant = longRcvd[0] * 65536 + longRcvd[1] * 256 + longRcvd[2];
return;
break;
case 0x0b:

View file

@ -26,15 +26,15 @@ byte regMapTemp[regMapSize];
byte cmdRcvd[maxBytes];
byte longRcvd[longBytes];
/*------------------------------------------------*/
class pP_i2c {
public:
pP_i2c(uint8_t address=pP_i2c_address);
public:
pP_i2c(uint8_t address = pP_i2c_address);
void init();
void i2cInput(int bytesReceived);
private:
private:
char _i2cResponse;
};

View file

@ -1,7 +1,7 @@
#include "pP_cmd.h"
#include "pP_volatile.h"
void parseData()
{
void parseData() {
// split the data into its parts
@ -15,45 +15,36 @@ void parseData()
}
/*------------------------------------------------*/
void identifyMarkers()
{
void identifyMarkers() {
char x = Serial.read();
#ifdef I2C_INPUT
char y = Wire.read();
#endif // I2C_INPUT
if (x == '\n' || x == '\r')
{
if (x == '\n' || x == '\r') {
serialIncoming = true;
inputBuffer[bytesRecvd] = 0;
parseData();
bytesRecvd = 0;
}
else
{
} else {
inputBuffer[bytesRecvd] = x;
bytesRecvd++;
if (bytesRecvd == buffSize)
{
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
#ifdef I2C_INPUT
if (y == '\n' || y == '\r')
{
if (y == '\n' || y == '\r') {
serialIncoming = true;
inputBuffer[bytesRecvd] = 0;
parseData();
bytesRecvd = 0;
}
else
{
} else {
inputBuffer[bytesRecvd] = y;
bytesRecvd++;
if (bytesRecvd == buffSize)
{
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
@ -62,12 +53,10 @@ void identifyMarkers()
/*------------------------------------------------*/
void serialPrintConfig()
{
void serialPrintConfig() {
Serial.print("GAIN_F ");
Serial.print(GAIN_FACTOR);
switch (GAIN_FACTOR)
{
switch (GAIN_FACTOR) {
case 0:
Serial.println(" 3x");
break;
@ -116,8 +105,7 @@ void serialPrintConfig()
Serial.println(PP_VERSION);
}
void serialPrintState()
{
void serialPrintState() {
Serial.print("{");
Serial.print("\"Vcc\":");
@ -147,64 +135,36 @@ void serialPrintState()
Serial.println("}");
}
void updateParams()
{
void updateParams() {
serialIncoming = false;
if (strcmp(serialMessageIn, "GAIN_F") == 0)
{
if (strcmp(serialMessageIn, "GAIN_F") == 0) {
updateGainFactor(serialLong);
}
else if (strcmp(serialMessageIn, "VFOL") == 0)
{
} else if (strcmp(serialMessageIn, "VFOL") == 0) {
updateVFol(serialLong);
}
else if (strcmp(serialMessageIn, "VCOMP") == 0)
{
} else if (strcmp(serialMessageIn, "VCOMP") == 0) {
updateVComp(serialLong);
}
else if (strcmp(serialMessageIn, "LOOP_D") == 0)
{
} else if (strcmp(serialMessageIn, "LOOP_D") == 0) {
updateLoopDuration(serialLong);
}
else if (strcmp(serialMessageIn, "TRG_D") == 0)
{
} else if (strcmp(serialMessageIn, "TRG_D") == 0) {
updateTrigDuration(serialLong);
}
else if (strcmp(serialMessageIn, "HYST") == 0)
{
} else if (strcmp(serialMessageIn, "HYST") == 0) {
updateHysteresis(serialLong);
}
else if (strcmp(serialMessageIn, "LOGIC") == 0)
{
} else if (strcmp(serialMessageIn, "LOGIC") == 0) {
updateLogic(serialLong);
}
else if (strcmp(serialMessageIn, "PZDET") == 0)
{
} else if (strcmp(serialMessageIn, "PZDET") == 0) {
updatePzDet(serialLong);
}
else if (strcmp(serialMessageIn, "CONST") == 0)
{
} else if (strcmp(serialMessageIn, "CONST") == 0) {
updateConstant(serialLong);
}
else if (strcmp(serialMessageIn, "DEBUG") == 0)
{
} else if (strcmp(serialMessageIn, "DEBUG") == 0) {
updateDebug(serialLong);
}
else if (strcmp(serialMessageIn, "CONFIG") == 0)
{
} else if (strcmp(serialMessageIn, "CONFIG") == 0) {
serialPrintConfig();
}
else if (strcmp(serialMessageIn, "ERASE") == 0)
{
} else if (strcmp(serialMessageIn, "ERASE") == 0) {
eraseEEPROM();
serialPrintConfig();
}
else if (strcmp(serialMessageIn, "STATE") == 0)
{
} else if (strcmp(serialMessageIn, "STATE") == 0) {
serialPrintState();
}
else if (strcmp(serialMessageIn, "HELP") == 0)
{
} else if (strcmp(serialMessageIn, "HELP") == 0) {
#if defined(ARDUINO_AVR_ATmega328PB)
Serial.println("To change gain factor: GAIN_F [integer for gain state - see note*]");
Serial.println("To change voltage follower voltage (low threshold): VFOL [float value]");
@ -235,11 +195,9 @@ void updateParams()
parseData();
}
void serialInput()
{
void serialInput() {
// receive data from Serial and save it into inputBuffer
if (Serial.available() > 0)
{
if (Serial.available() > 0) {
// the order of these IF clauses is significant
identifyMarkers();

View file

@ -48,4 +48,4 @@ extern long serialLong;
// Task scheduler instances
extern LightChrono mainLoop;
#endif //PP_VOLATILE_H
#endif // PP_VOLATILE_H