Skip to content

Commit

Permalink
Merge pull request #71 from loredan/master
Browse files Browse the repository at this point in the history
Fixes #33 , allows case-insensitive input over UART
  • Loading branch information
pyr0ball committed May 8, 2020
2 parents b45315f + 65dbe60 commit 1ea927e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 34 deletions.
8 changes: 8 additions & 0 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.cpp
@@ -1,6 +1,7 @@
#include "EEPROM.h"
#include "pP_config.h"
#include "pP_function.h"
#include "pP_volatile.h"

/*------------------------------------------------*/

Expand Down Expand Up @@ -88,6 +89,13 @@ void updateConstant(long value) {
}
}

void adjustConstant(int value) {
if (value > 0 && Vin > 0) {
voltMeterConstant = (long)(1.1 * value / Vin * 1023 * 1000);
EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant);
}
}

/*------------------------------------------------*/

void updateDebug(int value) {
Expand Down
1 change: 1 addition & 0 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h
Expand Up @@ -11,6 +11,7 @@ void updateLogic(int value);
void updatePzDet(int value);
void updateVccSwitch(int value);
void updateConstant(long value);
void adjustConstant(int value);
void updateDebug(int value);

#endif // PP_CMD_H
22 changes: 9 additions & 13 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp
Expand Up @@ -5,7 +5,7 @@
#include <Wire1.h>

uint8_t command;
uint16_t value;
uint32_t value;

void i2cWrite(uint8_t *buffer, int offset, int data) {
buffer[offset] = (uint8_t)(data >> 8);
Expand Down Expand Up @@ -33,8 +33,9 @@ void i2cReportConfig() {
i2cWrite(buffer, 10, Hyst);
i2cWrite(buffer, 12, LOGIC);
i2cWrite(buffer, 14, PZDET);
i2cWrite(buffer, 16, voltMeterConstant);
memcpy(buffer + 20, PP_VERSION, length - 20);
i2cWrite(buffer, 16, VCCSW);
i2cWrite(buffer, 18, voltMeterConstant);
memcpy(buffer + 22, PP_VERSION, length - 22);
Wire1.write(buffer, length);
}

Expand All @@ -50,8 +51,6 @@ void i2cReportState() {
}

void i2cReply() {
Serial.print("Requested ");
Serial.println(command);
switch (command) {
case CMD_CONFIG:
case CMD_ERASE:
Expand All @@ -72,18 +71,11 @@ void i2cInput(int bytesReceived) {
command = Wire1.read();
} else if (a == 1) {
value = Wire1.read();
} else if (a == 2) {
value = value << 8 | Wire1.read();
} else {
Wire1.read(); //
value = value << 8 | Wire1.read();
}
}

Serial.print("Command ");
Serial.print(command);
Serial.print(" ");
Serial.println(value);

// Parse commands and apply changes or actions
switch (command) {
case CMD_GAIN_F:
Expand Down Expand Up @@ -122,6 +114,10 @@ void i2cInput(int bytesReceived) {
break;
case CMD_VCCSW:
updateVccSwitch(value);
break;
case CMD_VCCADJUST:
adjustConstant(value);
break;
default:
return;
}
Expand Down
15 changes: 0 additions & 15 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.hpp
Expand Up @@ -4,21 +4,6 @@
#include "pP_config.h"
#include "stdint.h"

#define status_Offset 0x00 // Status register
#define senseInt_Offset 0x01 // Integer of sense threshold in millivolts
#define compInt_Offset 0x02 // Integer of comparator threshold in millivolts
#define gainFactor_Offset 0x03 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
#define hysteresis_Offset 0x04 // Hysteresis value for ADC measurements
#define loopDuration_Offset 0x05 // duration of time between ADC checks and other loop functions
#define logicLevel_Offset 0x06
#define piezoDetect_Offset 0x07
#define triggerDuration_Offset 0x08 // duration of the Z-axis pulse sent, in ms
#define debugEnable_Offset 0x09
#define voltMeterLong_Offset 0x0a // For fine-tuning the input volt master
#define versionRegister_Offset 0x0b
#define configRegister_Offset 0x0c
#define identRegister_Offset 0x0d

void i2cInit();

#endif // _pP_i2c_h_
Expand Up @@ -10,4 +10,5 @@
#define CMD_CONFIG 0x09
#define CMD_ERASE 0x0a
#define CMD_STATE 0x0b
#define CMD_VCCSW 0x0c
#define CMD_VCCSW 0x0c
#define CMD_VCCADJUST 0x0d
5 changes: 5 additions & 0 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h
@@ -1,5 +1,6 @@
#include "pP_cmd.h"
#include "pP_volatile.h"
#include "string.h"

void parseData() {

Expand Down Expand Up @@ -128,6 +129,7 @@ void serialPrintState() {

void updateParams() {
serialIncoming = false;
strupr(serialMessageIn);
if (strcmp(serialMessageIn, "GAIN_F") == 0) {
updateGainFactor(serialLong);
} else if (strcmp(serialMessageIn, "VFOL") == 0) {
Expand All @@ -148,6 +150,8 @@ void updateParams() {
updateVccSwitch(serialLong);
} else if (strcmp(serialMessageIn, "CONST") == 0) {
updateConstant(serialLong);
} else if (strcmp(serialMessageIn, "VCCADJUST") == 0) {
adjustConstant(serialLong);
} else if (strcmp(serialMessageIn, "DEBUG") == 0) {
updateDebug(serialLong);
} else if (strcmp(serialMessageIn, "CONFIG") == 0) {
Expand All @@ -171,6 +175,7 @@ void updateParams() {
Serial.println("To change the main voltage of the circuit: VCCSW [0|1]");
Serial.println(" (0 for 3.3v, 1 for 5v)");
Serial.println("To change ADC hysteresis value: HYST [integer in millivolts]");
Serial.println("To adjust VCC voltage readings: VCCADJUST [integer in millivolts, use value from multimeter]");
Serial.println("To enable or disable debug output: DEBUG [0|1]");
Serial.println("To print current config: CONFIG");
Serial.println("To set config to defaults: ERASE");
Expand Down
14 changes: 11 additions & 3 deletions firmware/AVR-Source/Pyr0_Piezo_i2c_Bridge/src/i2c.cpp
Expand Up @@ -3,11 +3,17 @@
#include "Wire.h"

uint16_t read16() {
return Wire.read() << 8 | Wire.read();
uint8_t value = Wire.read();
uint8_t value1 = Wire.read();
return ((uint16_t)value) << 8 | value1;
}

uint32_t read32() {
return (uint32_t)Wire.read() << 24 | (uint32_t)Wire.read() << 16 | Wire.read() << 8 | Wire.read();
uint8_t value = Wire.read();
uint8_t value1 = Wire.read();
uint8_t value2 = Wire.read();
uint8_t value3 = Wire.read();
return ((uint32_t)value) << 24 | ((uint32_t)value1) << 16 | ((uint16_t)value2) << 8 | value3;
}

void write(uint8_t cmd) {
Expand Down Expand Up @@ -40,7 +46,6 @@ config_t requestConfig() {
Wire.endTransmission();

uint8_t bytes = Wire.requestFrom(ADDRESS, 255);
Serial.println(bytes);

config_t config;
config.GAIN_FACTOR = read16();
Expand All @@ -51,6 +56,7 @@ config_t requestConfig() {
config.Hyst = read16();
config.LOGIC = read16();
config.PZDET = read16();
config.VCCSW = read16();
config.voltMeterConstant = read32();
config.version = Wire.readString();

Expand All @@ -70,4 +76,6 @@ state_t requestState() {
state.VFol = read16();
state.ERR_STATE = read16();
state.PZ_STATE = read16();

return state;
}
2 changes: 2 additions & 0 deletions firmware/AVR-Source/Pyr0_Piezo_i2c_Bridge/src/i2c.h
Expand Up @@ -16,6 +16,7 @@
#define CMD_ERASE 0x0a
#define CMD_STATE 0x0b
#define CMD_VCCSW 0x0c
#define CMD_VCCADJUST 0x0d

#include "WString.h"

Expand All @@ -28,6 +29,7 @@ typedef struct {
uint16_t Hyst;
uint16_t LOGIC;
uint16_t PZDET;
uint16_t VCCSW;
uint32_t voltMeterConstant;
String version;
} config_t;
Expand Down
23 changes: 21 additions & 2 deletions firmware/AVR-Source/Pyr0_Piezo_i2c_Bridge/src/pP_serial.h
@@ -1,5 +1,6 @@
#include "i2c.h"
#include "stdint.h"
#include "string.h"

#define buffSize 40
bool serialIncoming = false;
Expand Down Expand Up @@ -89,6 +90,20 @@ void serialPrintConfig() {
Serial.print("PZDET ");
Serial.println(config.PZDET);

Serial.print("VCCSW ");
Serial.print(config.VCCSW);
switch (config.VCCSW) {
case 0:
Serial.println(" 3.3v");
break;
case 1:
Serial.println(" 5v");
break;
default:
Serial.println(" INVALID");
break;
}

Serial.print("VM_CONST ");
Serial.println(config.voltMeterConstant);

Expand All @@ -106,11 +121,11 @@ void serialPrintState() {
Serial.print(",");

Serial.print("\"VComp\":");
Serial.print((uint32_t)state.VComp * state.Vin / 1023);
Serial.print(state.VComp);
Serial.print(",");

Serial.print("\"VFol\":");
Serial.print((uint32_t)state.VFol * state.Vin / 1023);
Serial.print(state.VFol);
Serial.print(",");

Serial.print("\"Err\":");
Expand All @@ -125,6 +140,7 @@ void serialPrintState() {

void updateParams() {
serialIncoming = false;
strupr(serialMessageIn);
if (strcmp(serialMessageIn, "GAIN_F") == 0) {
write(CMD_GAIN_F, (uint16_t)serialLong);
} else if (strcmp(serialMessageIn, "VFOL") == 0) {
Expand All @@ -145,6 +161,8 @@ void updateParams() {
write(CMD_VCCSW, (uint16_t)serialLong);
} else if (strcmp(serialMessageIn, "CONST") == 0) {
write(CMD_CONST, serialLong);
} else if (strcmp(serialMessageIn, "VCCADJUST") == 0) {
write(CMD_VCCADJUST, (uint16_t)serialLong);
} else if (strcmp(serialMessageIn, "CONFIG") == 0) {
serialPrintConfig();
} else if (strcmp(serialMessageIn, "ERASE") == 0) {
Expand All @@ -166,6 +184,7 @@ void updateParams() {
Serial.println("To change the main voltage of the circuit: VCCSW [0|1]");
Serial.println(" (0 for 3.3v, 1 for 5v)");
Serial.println("To change ADC hysteresis value: HYST [integer in millivolts]");
Serial.println("To adjust VCC voltage readings: VCCADJUST [integer in millivolts, use value from multimeter]");
Serial.println("To enable or disable debug output: DEBUG [0|1]");
Serial.println("To print current config: CONFIG");
Serial.println("To set config to defaults: ERASE");
Expand Down

0 comments on commit 1ea927e

Please sign in to comment.