From 513d03a4bf52fc21c1c2b42982a32a5b3af59602 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Fri, 5 Jul 2019 23:48:38 -0700 Subject: [PATCH] Complete overhaul of i2c code, created library structure - Progress on https://github.com/pyr0ball/pyr0piezo/issues/30 - ToDo: Create master library for easy integration with other firmwares --- .../Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp | 55 +++++++++++++++++++ .../Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.h | 44 +++++++++------ 2 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp new file mode 100644 index 0000000..798bafd --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp @@ -0,0 +1,55 @@ +#include "pP_i2c.h" + +void pP_i2c::init() { + Wire.begin(pP_i2c_address) +} + +void pP_i2c::i2cInput(int bytesReceived) { + for (int a = 0; a < bytesReceived; a++) { + if (a < maxBytes) { + cmdRcvd[a] = Wire.receive(); + } + else { + Wire.receive(); + } + } + if (bytesReceived == 1 && (cmdRcvd[0] < regMapSize)) { + return; + } + if (bytesReceived == 1 && (cmdRcvd[0] >= regMapSize)) { + cmdRcvd[0] = 0x00; + return; + } + switch (cmdRcvd[0]) { + case 0x00: + senseInt = (uint8_t) cmdRcvd[1]; + return; + break + case 0x01: + compInt = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x02: + GAIN_FACTOR = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x03: + Hyst = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x04: + LOOP_DUR = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x05: + TRG_DUR = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x06: + voltMeterConstant = (uint8_t) cmdRcvd[1]; + return; + break; + default: + return; + } +} diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.h index ff654b8..d3f3716 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.h @@ -1,25 +1,33 @@ +#ifndef _pP_i2c_h_ +#define _pP_i2c_h_ + #include -/*------------------------------------------------*/ -#ifdef I2C - void i2cReply() { - if (serialIncoming) { - Wire.write("OK"); - } - } -#endif +#define pP_i2c_address 0xa0 // I2C Bus Address + +#define senseInt_Offset 0x00 // Integer of sense threshold in millivolts +#define compInt_Offset 0x01 // Integer of comparator threshold in millivolts +#define gainFactor_Offset 0x02 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x +#define hysteresis_Offset 0x03 // Hysteresis value for ADC measurements +#define loopDuration_Offset 0x04 // duration of time between ADC checks and other loop functions +#define triggerDuration_Offset 0x05 // duration of the Z-axis pulse sent, in ms +#define voltMeterLong_Offset 0x06 // For fine-tuning the input volt master + +/*-------------------------Variables------------------------*/ +#define regMapSize 7 +#define maxBytes 3 +byte regMap[regMapSize]; +byte regMapTemp[regMapSize]; +byte cmdRcvd[maxBytes]; /*------------------------------------------------*/ -#ifdef I2C - void i2cInput() { +class pP_i2c { + public: + pP_i2c(uint8_t address=pP_i2c_address); + + void init(); + void i2cInput(); +}; - // receive data from Serial and save it into inputBuffer - - while(Wire.available()) { - identifyMarkers(); - updateParams(); - i2cReply(); - } - } #endif