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
This commit is contained in:
parent
dc9ef32e5b
commit
513d03a4bf
2 changed files with 81 additions and 18 deletions
55
firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp
Normal file
55
firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +1,33 @@
|
|||
#ifndef _pP_i2c_h_
|
||||
#define _pP_i2c_h_
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
/*------------------------------------------------*/
|
||||
#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
|
||||
|
|
|
|||
Loading…
Reference in a new issue