From 02d734b177014aa8d2fc55b756e8568df9544931 Mon Sep 17 00:00:00 2001 From: Vsevolod Merenkov Date: Sun, 29 Mar 2020 00:25:27 +0300 Subject: [PATCH] i2c support, not tested #feature --- .../src/Pyr0_Piezo_Sensor_V2.x.x.cpp | 10 +- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.cpp | 92 ++++++++++ .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h | 102 ++--------- .../src/pP_config.cpp | 1 + .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h | 6 +- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp | 167 +++++++++--------- .../src/{pP_i2c.h => pP_i2c.hpp} | 26 +-- .../src/pP_i2c_config.h | 12 ++ .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h | 23 --- 9 files changed, 209 insertions(+), 230 deletions(-) create mode 100644 firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.cpp rename firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/{pP_i2c.h => pP_i2c.hpp} (65%) create mode 100644 firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c_config.h diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp index 5c1e315..182cbf8 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp @@ -84,19 +84,19 @@ update the voltMeterConstant variable in pP_config.h with the correct value ------------------------------------------------------------*/ +// i2c input toggle. Uncomment to enable +#define I2C_INPUT + // Headers, variables, and functions #include "LightChrono.h" #include "pP_pins.h" #include #include -// #include "pP_config.h" #include "pP_function.h" +#include "pP_i2c.hpp" #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) TCCR2A = (1 << COM2B1) | (0 << COM2B0) | (0 << WGM21) | (1 << WGM20); @@ -124,6 +124,8 @@ void setup() { Serial.begin(9600); Serial.println("Initializing Pyr0-Piezo Sensor..."); + i2cInit(); + restoreConfig(); adjustGain(); diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.cpp new file mode 100644 index 0000000..43f4ed4 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.cpp @@ -0,0 +1,92 @@ +#include "EEPROM.h" +#include "pP_config.h" +#include "pP_function.h" + +/*------------------------------------------------*/ + +void updateGainFactor(int value) { + if (value >= 0) { + GAIN_FACTOR = value; + adjustGain(); + EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); + } +} + +/*------------------------------------------------*/ + +void updateVFol(int value) { + if (value >= 0) { + followerThrs = value; + adjustFollow(); + EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); + } +} +/*------------------------------------------------*/ + +void updateVComp(int value) { + if (value >= 0) { + compThrs = value; + adjustComp(); + EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); + } +} + +/*------------------------------------------------*/ + +void updateLoopDuration(int value) { + if (value >= 0) { + LOOP_DUR = value; + EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); + } +} +/*------------------------------------------------*/ + +void updateTrigDuration(int value) { + if (value >= 0) { + TRG_DUR = value; + EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); + } +} +/*------------------------------------------------*/ + +void updateHysteresis(int value) { + if (value >= 0) { + Hyst = value; + EEPROM.put(HYST_ADDRESS, Hyst); + } +} +/*------------------------------------------------*/ + +void updateLogic(int value) { + if (value >= 0) { + LOGIC = value; + EEPROM.put(LOGIC_ADDRESS, LOGIC); + pulse(); + } +} +/*------------------------------------------------*/ + +void updatePzDet(int value) { + if (value >= 0) { + PZDET = value; + EEPROM.put(PZDET_ADDRESS, PZDET); + } +} +/*------------------------------------------------*/ + +void updateConstant(long value) { + if (value >= 0) { + voltMeterConstant = value; + EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); + } +} + +/*------------------------------------------------*/ + +void updateDebug(int value) { + if (value > 0) { + Debug = 1; + } else if (value == 0) { + Debug = 0; + } +} \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h index c4de8c0..e3d7263 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h @@ -1,97 +1,15 @@ #ifndef PP_CMD_H #define PP_CMD_H -#include "EEPROM.h" -#include "pP_config.h" -#include "pP_function.h" - -/*------------------------------------------------*/ - -void updateGainFactor(int value) { - if (value >= 0) { - GAIN_FACTOR = value; - adjustGain(); - EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); - } -} - -/*------------------------------------------------*/ - -void updateVFol(int value) { - if (value >= 0) { - followerThrs = value; - adjustFollow(); - EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); - } -} -/*------------------------------------------------*/ - -void updateVComp(int value) { - if (value >= 0) { - compThrs = value; - adjustComp(); - EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); - } -} - -/*------------------------------------------------*/ - -void updateLoopDuration(int value) { - if (value >= 0) { - LOOP_DUR = value; - EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); - } -} -/*------------------------------------------------*/ - -void updateTrigDuration(int value) { - if (value >= 0) { - TRG_DUR = value; - EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); - } -} -/*------------------------------------------------*/ - -void updateHysteresis(int value) { - if (value >= 0) { - Hyst = value; - EEPROM.put(HYST_ADDRESS, Hyst); - } -} -/*------------------------------------------------*/ - -void updateLogic(int value) { - if (value >= 0) { - LOGIC = value; - EEPROM.put(LOGIC_ADDRESS, LOGIC); - pulse(); - } -} -/*------------------------------------------------*/ - -void updatePzDet(int value) { - if (value >= 0) { - PZDET = value; - EEPROM.put(PZDET_ADDRESS, PZDET); - } -} -/*------------------------------------------------*/ - -void updateConstant(long value) { - if (value >= 0) { - voltMeterConstant = value; - EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); - } -} - -/*------------------------------------------------*/ - -void updateDebug(int value) { - if (value > 0) { - Debug = 1; - } else if (value == 0) { - Debug = 0; - } -} +void updateGainFactor(int value); +void updateVFol(int value); +void updateVComp(int value); +void updateLoopDuration(int value); +void updateTrigDuration(int value); +void updateHysteresis(int value); +void updateLogic(int value); +void updatePzDet(int value); +void updateConstant(long value); +void updateDebug(int value); #endif // PP_CMD_H \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp index 054203a..02af5af 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp @@ -12,6 +12,7 @@ int LOGIC = LOGIC_DEFAULT; int PZDET = PZDET_DEFAULT; int Debug = 0; long voltMeterConstant = VM_CONST_DEFAULT; +uint8_t pP_i2c_address = 0x80; /*------------------------------------------------*/ void eraseEEPROM() { diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h index b73697e..0ef3554 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h @@ -1,6 +1,8 @@ #ifndef PP_CONFIG_H #define PP_CONFIG_H +#include "stdint.h" + // Configurable settings: #define GAIN_FACTOR_DEFAULT 2 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x @@ -45,10 +47,8 @@ extern int Debug; #define VM_CONST_DEFAULT 1125300L extern long voltMeterConstant; // For fine tuning input voltage sense -#ifdef I2C_INPUT #define I2C_SLAVE_ADDRESS 24 -uint8_t pP_i2c_address = 0xa0; // I2C Bus Address -#endif // I2C_INPUT +extern uint8_t pP_i2c_address; // I2C Bus Address (P + 0 -> 0x50 + 0x30 -> 0x80) void eraseEEPROM(); void setDefaultConfig(); diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp index a8c2e8c..c2796cc 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp @@ -1,126 +1,121 @@ -#ifdef I2C_INPUT - -#include "pP_i2c.h" -#include "pP_config.h" -#include +#include "pP_i2c.hpp" +#include "pP_cmd.h" +#include "pP_i2c_config.h" +#include "pP_volatile.h" #include -byte registerMap[regMapSize]; -byte registerMapTemp[regMapSize - 1]; -byte receivedCommands[maxBytes]; +uint8_t command; +uint16_t value; -pP_i2c::pP_i2c() { -} - -void pP_i2c::init() { +void i2cInit() { Wire.begin(pP_i2c_address); Wire.onRequest(i2cReply); Wire.onReceive(i2cInput); } -void pP_i2c::i2cReportStatus() { - _i2cResponse = "{" +void i2cReportConfig() { + i2cWrite(GAIN_FACTOR); + i2cWrite(followerThrs); + i2cWrite(compThrs); + i2cWrite(LOOP_DUR); + i2cWrite(TRG_DUR); + i2cWrite(Hyst); + i2cWrite(LOGIC); + i2cWrite(PZDET); + i2cWrite(voltMeterConstant); } -void pP_i2c::i2cReportVersion() { +void i2cReportState() { + i2cWrite(Vin); + i2cWrite((int)((long)VComp * Vin / 1023)); + i2cWrite((int)((long)VFol * Vin / 1023)); + i2cWrite(ERR_STATE); + i2cWrite(PZ_STATE); } -void pP_i2c::i2cReportConfig() { +void i2cWrite(int data) { + Wire.write(data >> 24); + Wire.write(data >> 16); + Wire.write(data >> 8); + Wire.write(data); } -void pP_i2c::i2cReportIdentity() { +void i2cWrite(long data) { + Wire.write(data >> 56); + Wire.write(data >> 48); + Wire.write(data >> 40); + Wire.write(data >> 32); + Wire.write(data >> 24); + Wire.write(data >> 16); + Wire.write(data >> 8); + Wire.write(data); } -void pP_i2c::i2cRequestInput() { +void i2cReply() { + switch (command) { + case CMD_CONFIG: + case CMD_ERASE: + i2cReportConfig(); + break; + case CMD_STATE: + i2cReportState(); + break; + default: + break; + } } -void pP_i2c::i2cReply() { - Wire.send() -} - -void pP_i2c::i2cInput(int bytesReceived) { +void i2cInput(int bytesReceived) { for (int a = 0; a < bytesReceived; a++) { // Check length of message, drops anything longer than [longBytes] - if (a <= maxBytes) { - cmdRcvd[a] = Wire.receive(); + if (a == 0) { + command = Wire.read(); + } else if (a == 1) { + value = Wire.read(); + } else if (a == 2) { + value = value << 8 | Wire.read(); + } else { + Wire.read(); // } - elif (a <= longBytes) { - longRcvd[a] = Wire.receive(); - } - else { - Wire.receive(); // - } - } - - // Check input command corresponds with register map, set 0x00 if not - if (bytesReceived == 1 && (cmdRcvd[0] < regMapSize)) { - return; - } - if (bytesReceived == 1 && (cmdRcvd[0] >= regMapSize)) { - cmdRcvd[0] = 0x00; - return; } // Parse commands and apply changes or actions - switch (cmdRcvd[0]) { - case 0x00: - i2cReportStatus(); - return; + switch (command) { + case CMD_GAIN_F: + updateGainFactor(value); break; - case 0x01: - followerInt = (int)cmdRcvd[1]; - return; + case CMD_VFOL: + updateVFol(value); break; - case 0x02: - compInt = (int)cmdRcvd[1]; - return; + case CMD_VCOMP: + updateVComp(value); break; - case 0x03: - GAIN_FACTOR = (int)cmdRcvd[1]; - return; + case CMD_LOOP_D: + updateLoopDuration(value); break; - case 0x04: - Hyst = (int)cmdRcvd[1]; - return; + case CMD_TRG_D: + updateTrigDuration(value); break; - case 0x05: - LOOP_DUR = (int)cmdRcvd[1]; - return; + case CMD_HYST: + updateHysteresis(value); break; - case 0x06: - LOGIC = (int)cmdRcvd[1]; - return; + case CMD_LOGIC: + updateLogic(value); break; - case 0x07: - PZDET = (int)cmdRcvd[1]; - return; + case CMD_PZDET: + updatePzDet(value); break; - case 0x08: - TRG_DUR = (int)cmdRcvd[1]; - return; + case CMD_CONST: + updateConstant(value); break; - case 0x09: - DEBUG = (int)cmdRcvd[1]; - return; + case CMD_CONFIG: break; - case 0x0a: - voltMeterConstant = longRcvd[0] * 65536 + longRcvd[1] * 256 + longRcvd[2]; - return; + case CMD_ERASE: + eraseEEPROM(); break; - case 0x0b: - reportVersion(); - return; - break; - case 0x0c: - reportConfig(); - return; - break; - case 0x0d: - reportIdentity(); - return; + case CMD_STATE: break; default: return; } } -#endif diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.hpp similarity index 65% rename from firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.h rename to firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.hpp index 22fae9e..1cad709 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.hpp @@ -1,6 +1,8 @@ #ifndef _pP_i2c_h_ #define _pP_i2c_h_ -#ifdef I2C_INPUT + +#include "pP_config.h" +#include "stdint.h" #define status_Offset 0x00 // Status register #define senseInt_Offset 0x01 // Integer of sense threshold in millivolts @@ -17,26 +19,6 @@ #define configRegister_Offset 0x0c #define identRegister_Offset 0x0d -/*-------------------------Variables------------------------*/ -#define regMapSize 14 -#define maxBytes 2 -#define longBytes 4 -byte regMap[regMapSize]; -byte regMapTemp[regMapSize]; -byte cmdRcvd[maxBytes]; -byte longRcvd[longBytes]; +void i2cInit(); -/*------------------------------------------------*/ - -class pP_i2c { -public: - pP_i2c(uint8_t address = pP_i2c_address); - void init(); - void i2cInput(int bytesReceived); - -private: - char _i2cResponse; -}; - -#endif // I2C_INPUT #endif // _pP_i2c_h_ diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c_config.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c_config.h new file mode 100644 index 0000000..62b4f09 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c_config.h @@ -0,0 +1,12 @@ +#define CMD_GAIN_F 0x00 +#define CMD_VFOL 0x01 +#define CMD_VCOMP 0x02 +#define CMD_LOOP_D 0x03 +#define CMD_TRG_D 0x04 +#define CMD_HYST 0x05 +#define CMD_LOGIC 0x06 +#define CMD_PZDET 0x07 +#define CMD_CONST 0x08 +#define CMD_CONFIG 0x09 +#define CMD_ERASE 0x0a +#define CMD_STATE 0x0b \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h index 62d4b0f..44b0844 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h @@ -18,9 +18,6 @@ void parseData() { void identifyMarkers() { char x = Serial.read(); -#ifdef I2C_INPUT - char y = Wire.read(); -#endif // I2C_INPUT if (x == '\n' || x == '\r') { serialIncoming = true; @@ -34,21 +31,6 @@ void identifyMarkers() { bytesRecvd = buffSize - 1; } } - -#ifdef I2C_INPUT - if (y == '\n' || y == '\r') { - serialIncoming = true; - inputBuffer[bytesRecvd] = 0; - parseData(); - bytesRecvd = 0; - } else { - inputBuffer[bytesRecvd] = y; - bytesRecvd++; - if (bytesRecvd == buffSize) { - bytesRecvd = buffSize - 1; - } - } -#endif } /*------------------------------------------------*/ @@ -126,11 +108,6 @@ void serialPrintState() { Serial.print("\"PzCon\":"); Serial.print(PZ_STATE); - Serial.print(","); - - Serial.print("\"Firm_Ver\":"); - Serial.print(PP_VERSION); - Serial.print(","); Serial.println("}"); }