i2c support, not tested #feature

This commit is contained in:
Vsevolod Merenkov 2020-03-29 00:25:27 +03:00
parent f2c3843668
commit 02d734b177
9 changed files with 209 additions and 230 deletions

View file

@ -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 // Headers, variables, and functions
#include "LightChrono.h" #include "LightChrono.h"
#include "pP_pins.h" #include "pP_pins.h"
#include <Arduino.h> #include <Arduino.h>
#include <EEPROM.h> #include <EEPROM.h>
// #include "pP_config.h"
#include "pP_function.h" #include "pP_function.h"
#include "pP_i2c.hpp"
#include "pP_serial.h" #include "pP_serial.h"
#include "pP_volatile.h" #include "pP_volatile.h"
// i2c input toggle. Uncomment to enable
#define I2C_INPUT
void setup() { void setup() {
// Setup PWM on voltage follower (PD3) // Setup PWM on voltage follower (PD3)
TCCR2A = (1 << COM2B1) | (0 << COM2B0) | (0 << WGM21) | (1 << WGM20); TCCR2A = (1 << COM2B1) | (0 << COM2B0) | (0 << WGM21) | (1 << WGM20);
@ -124,6 +124,8 @@ void setup() {
Serial.begin(9600); Serial.begin(9600);
Serial.println("Initializing Pyr0-Piezo Sensor..."); Serial.println("Initializing Pyr0-Piezo Sensor...");
i2cInit();
restoreConfig(); restoreConfig();
adjustGain(); adjustGain();

View file

@ -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;
}
}

View file

@ -1,97 +1,15 @@
#ifndef PP_CMD_H #ifndef PP_CMD_H
#define PP_CMD_H #define PP_CMD_H
#include "EEPROM.h" void updateGainFactor(int value);
#include "pP_config.h" void updateVFol(int value);
#include "pP_function.h" void updateVComp(int value);
void updateLoopDuration(int value);
/*------------------------------------------------*/ void updateTrigDuration(int value);
void updateHysteresis(int value);
void updateGainFactor(int value) { void updateLogic(int value);
if (value >= 0) { void updatePzDet(int value);
GAIN_FACTOR = value; void updateConstant(long value);
adjustGain(); void updateDebug(int value);
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;
}
}
#endif // PP_CMD_H #endif // PP_CMD_H

View file

@ -12,6 +12,7 @@ int LOGIC = LOGIC_DEFAULT;
int PZDET = PZDET_DEFAULT; int PZDET = PZDET_DEFAULT;
int Debug = 0; int Debug = 0;
long voltMeterConstant = VM_CONST_DEFAULT; long voltMeterConstant = VM_CONST_DEFAULT;
uint8_t pP_i2c_address = 0x80;
/*------------------------------------------------*/ /*------------------------------------------------*/
void eraseEEPROM() { void eraseEEPROM() {

View file

@ -1,6 +1,8 @@
#ifndef PP_CONFIG_H #ifndef PP_CONFIG_H
#define PP_CONFIG_H #define PP_CONFIG_H
#include "stdint.h"
// Configurable settings: // Configurable settings:
#define GAIN_FACTOR_DEFAULT 2 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x #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 #define VM_CONST_DEFAULT 1125300L
extern long voltMeterConstant; // For fine tuning input voltage sense extern long voltMeterConstant; // For fine tuning input voltage sense
#ifdef I2C_INPUT
#define I2C_SLAVE_ADDRESS 24 #define I2C_SLAVE_ADDRESS 24
uint8_t pP_i2c_address = 0xa0; // I2C Bus Address extern uint8_t pP_i2c_address; // I2C Bus Address (P + 0 -> 0x50 + 0x30 -> 0x80)
#endif // I2C_INPUT
void eraseEEPROM(); void eraseEEPROM();
void setDefaultConfig(); void setDefaultConfig();

View file

@ -1,126 +1,121 @@
#ifdef I2C_INPUT #include "pP_i2c.hpp"
#include "pP_cmd.h"
#include "pP_i2c.h" #include "pP_i2c_config.h"
#include "pP_config.h" #include "pP_volatile.h"
#include <Arduino.h>
#include <Wire.h> #include <Wire.h>
byte registerMap[regMapSize]; uint8_t command;
byte registerMapTemp[regMapSize - 1]; uint16_t value;
byte receivedCommands[maxBytes];
pP_i2c::pP_i2c() { void i2cInit() {
}
void pP_i2c::init() {
Wire.begin(pP_i2c_address); Wire.begin(pP_i2c_address);
Wire.onRequest(i2cReply); Wire.onRequest(i2cReply);
Wire.onReceive(i2cInput); Wire.onReceive(i2cInput);
} }
void pP_i2c::i2cReportStatus() { void i2cReportConfig() {
_i2cResponse = "{" 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() { void i2cInput(int bytesReceived) {
Wire.send()
}
void pP_i2c::i2cInput(int bytesReceived) {
for (int a = 0; a < bytesReceived; a++) { for (int a = 0; a < bytesReceived; a++) {
// Check length of message, drops anything longer than [longBytes] // Check length of message, drops anything longer than [longBytes]
if (a <= maxBytes) { if (a == 0) {
cmdRcvd[a] = Wire.receive(); 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 // Parse commands and apply changes or actions
switch (cmdRcvd[0]) { switch (command) {
case 0x00: case CMD_GAIN_F:
i2cReportStatus(); updateGainFactor(value);
return;
break; break;
case 0x01: case CMD_VFOL:
followerInt = (int)cmdRcvd[1]; updateVFol(value);
return;
break; break;
case 0x02: case CMD_VCOMP:
compInt = (int)cmdRcvd[1]; updateVComp(value);
return;
break; break;
case 0x03: case CMD_LOOP_D:
GAIN_FACTOR = (int)cmdRcvd[1]; updateLoopDuration(value);
return;
break; break;
case 0x04: case CMD_TRG_D:
Hyst = (int)cmdRcvd[1]; updateTrigDuration(value);
return;
break; break;
case 0x05: case CMD_HYST:
LOOP_DUR = (int)cmdRcvd[1]; updateHysteresis(value);
return;
break; break;
case 0x06: case CMD_LOGIC:
LOGIC = (int)cmdRcvd[1]; updateLogic(value);
return;
break; break;
case 0x07: case CMD_PZDET:
PZDET = (int)cmdRcvd[1]; updatePzDet(value);
return;
break; break;
case 0x08: case CMD_CONST:
TRG_DUR = (int)cmdRcvd[1]; updateConstant(value);
return;
break; break;
case 0x09: case CMD_CONFIG:
DEBUG = (int)cmdRcvd[1];
return;
break; break;
case 0x0a: case CMD_ERASE:
voltMeterConstant = longRcvd[0] * 65536 + longRcvd[1] * 256 + longRcvd[2]; eraseEEPROM();
return;
break; break;
case 0x0b: case CMD_STATE:
reportVersion();
return;
break;
case 0x0c:
reportConfig();
return;
break;
case 0x0d:
reportIdentity();
return;
break; break;
default: default:
return; return;
} }
} }
#endif

View file

@ -1,6 +1,8 @@
#ifndef _pP_i2c_h_ #ifndef _pP_i2c_h_
#define _pP_i2c_h_ #define _pP_i2c_h_
#ifdef I2C_INPUT
#include "pP_config.h"
#include "stdint.h"
#define status_Offset 0x00 // Status register #define status_Offset 0x00 // Status register
#define senseInt_Offset 0x01 // Integer of sense threshold in millivolts #define senseInt_Offset 0x01 // Integer of sense threshold in millivolts
@ -17,26 +19,6 @@
#define configRegister_Offset 0x0c #define configRegister_Offset 0x0c
#define identRegister_Offset 0x0d #define identRegister_Offset 0x0d
/*-------------------------Variables------------------------*/ void i2cInit();
#define regMapSize 14
#define maxBytes 2
#define longBytes 4
byte regMap[regMapSize];
byte regMapTemp[regMapSize];
byte cmdRcvd[maxBytes];
byte longRcvd[longBytes];
/*------------------------------------------------*/
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_ #endif // _pP_i2c_h_

View file

@ -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

View file

@ -18,9 +18,6 @@ void parseData() {
void identifyMarkers() { void identifyMarkers() {
char x = Serial.read(); 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; serialIncoming = true;
@ -34,21 +31,6 @@ void identifyMarkers() {
bytesRecvd = buffSize - 1; 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("\"PzCon\":");
Serial.print(PZ_STATE); Serial.print(PZ_STATE);
Serial.print(",");
Serial.print("\"Firm_Ver\":");
Serial.print(PP_VERSION);
Serial.print(",");
Serial.println("}"); Serial.println("}");
} }