Adding pages, moving values back and forth and displaying, fixed voltage display

This commit is contained in:
Lukas Bachschwell 2018-04-24 21:31:32 +02:00
parent 329fdf2ee8
commit 41cc32d73b
2 changed files with 173 additions and 67 deletions

View File

@ -35,7 +35,9 @@ float speed = 0;
uint8_t sendTemperature = 0;
uint8_t sendTemperatureDecimals = 0;
uint8_t sendVoltage = 0;
uint8_t sendSpeed = 0;
uint8_t sendVoltageDecimals = 0;
uint8_t sendRPMupper = 1;
uint8_t sendRPMlower = 2;
//#define pairingMode
#define CONNECTION_TIMEOUT 300
@ -71,7 +73,8 @@ void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
Serial.print("Last Packet Recv Data: "); Serial.println(recData[0]); Serial.print(" "); Serial.print(recData[1]); Serial.print(" len:"); Serial.println(data_len);
// Answer with response
const uint8_t respData[] = { sendTemperature, sendTemperatureDecimals, sendVoltage }; // sendSpeed
const uint8_t respData[] = { sendVoltage, sendVoltageDecimals, sendTemperature, sendTemperatureDecimals, sendRPMupper, sendRPMlower };
Serial.print("Sending RESPONSE.... ");
esp_err_t result = esp_now_send(mac_addr, respData, sizeof(data));
if (result == ESP_OK) {
@ -187,8 +190,8 @@ void checkTemperature() {
Serial.println(temperature);
if(temperature < 35) digitalWrite(fanRelais, LOW);
if(temperature > 40) digitalWrite(fanRelais, HIGH);
sendTemperature = (uint16_t) temperature;
sendTemperatureDecimals = (uint16_t)(temperature - sendTemperature) *100;
sendTemperature = abs(floor(temperature));
sendTemperatureDecimals = (temperature - sendTemperature) * 100;
}
void setup() {

View File

@ -11,12 +11,24 @@
#include "accel.h"
#define B_VOLT 0
#define B_VOLT_D 1
#define B_TEMP 2
#define B_TEMP_D 3
#define B_SPEED 4
#define B_SPEED_D 5
uint8_t boardData[6] = {0, 0, 0, 0, 0, 0};
bool connected = false;
// Defining variables for OLED display
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2 (U8G2_R2, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
char displayBuffer[20];
String displayString;
short displayData = 0;
unsigned long lastSignalBlink;
bool signalBlink = false;
unsigned long lastDataRotation;
// Defining variables for Settings menu
@ -36,9 +48,12 @@ byte hallCenterMargin = 5;
byte currentSetting = 0;
const byte numOfSettings = 11;
const float minVoltage = 3.2;
const float minVoltage = 3.25;
const float maxVoltage = 4.1;
const float refVoltage = 3.3;
// Resistors in Ohms
const float deviderR1 = 1500;
const float deviderR2 = 22000;
// Global copy of board
@ -61,17 +76,8 @@ esp_now_peer_info_t board;
// ESPNOW functions ##############################
// Scan for boards in AP mode
void configDeviceAP(bool hidden) {
bool result = WiFi.softAP("ESK8Remote", "ESK8_Password+vD8z2YAvoDBW?Zx", CHANNEL, hidden);
if (!result) {
Serial.println("AP Config failed.");
} else {
Serial.println("AP Config Success. Broadcasting with AP: " + String("ESK8"));
}
}
#ifdef pairingMode
void ScanForboard() {
void ScanForBoard() {
int8_t scanResults = WiFi.scanNetworks();
// reset on each scan
bool boardFound = 0;
@ -241,7 +247,14 @@ void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print("Last Packet Sent to: "); Serial.println(macStr);
Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
Serial.print("Last Packet Send Status: ");
if(status == ESP_NOW_SEND_SUCCESS) {
connected = true;
Serial.println("Delivery Success");
} else {
connected = false;
Serial.println("Delivery Fail");
}
}
// callback when data is recv from board
@ -249,10 +262,10 @@ void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print("#######################Last Response Recv from: "); Serial.println(macStr);
uint8_t recData[3];
memcpy(recData, data, data_len);
Serial.print("Last Response Recv Data: "); Serial.println(recData[0]); Serial.print(" "); Serial.print(recData[1]); Serial.print(" len:"); Serial.println(data_len);
Serial.print("Last Response Recv from: "); Serial.println(macStr);
memcpy(boardData, data, data_len);
Serial.print("Recieved data! len: ");
Serial.println(data_len);
}
//############ End ESP Now
@ -278,6 +291,7 @@ void calculateThrottlePosition() {
total += analogRead(leverPin);
}
hallMeasurement = total / 10;
Serial.print("HAL: ");
Serial.println(hallMeasurement);
//DEBUG_PRINT( (String)hallMeasurement );
@ -300,8 +314,10 @@ float batteryVoltage() {
for (int i = 0; i < 10; i++) {
total += analogRead(batteryMeasurePin);
}
batteryVoltage = (refVoltage / 4096.0) * ((float)total / 10.0);
batteryVoltage = (refVoltage / 4095.0) * ((float)total / 10.0);
// Now we have the actual Voltage, lets calculate the value befor the devider
batteryVoltage = batteryVoltage / ( deviderR1 / (deviderR1 + deviderR2));
Serial.print("Batt: ");
Serial.println(batteryVoltage);
return batteryVoltage;
}
@ -323,6 +339,7 @@ int batteryLevel() {
int getStrength(int points){
long rssi = 0;
long averageRSSI=0;
if (points == 1) return WiFi.RSSI();
for (int i=0; i < points; i++) {
rssi += WiFi.RSSI();
@ -330,6 +347,8 @@ int getStrength(int points){
}
averageRSSI=rssi/points;
Serial.print("RSSI: ");
Serial.println(averageRSSI);
return averageRSSI;
}
@ -383,7 +402,6 @@ void drawThrottle() {
void drawSignal() {
// Position on OLED
int x = 114; int y = 17;
/*
if (connected == true) {
if (triggerActive()) {
u8g2.drawXBM(x, y, 12, 12, signal_transmitting_bits);
@ -402,7 +420,6 @@ void drawSignal() {
u8g2.drawXBM(x, y, 12, 12, signal_noconnection_bits);
}
}
*/
}
void drawTitleScreen(String title) {
@ -415,13 +432,55 @@ void drawTitleScreen(String title) {
delay(1500);
}
void drawBoardVoltage() {
void drawPage() {
int decimals;
String suffix;
String prefix;
int first, last;
int x = 0;
int y = 16;
String suffix = "V";
String prefix = "BATTERY";
float value = 0.0; // TODO: No info this yet, measure in the board
// Rotate the realtime data each 4s.
if ((millis() - lastDataRotation) >= 4000) {
lastDataRotation = millis();
displayData++;
if (displayData > 2) {
displayData = 0;
}
}
switch (displayData) {
case 0:
//value = ratioRpmSpeed * data.rpm;
first = boardData[B_TEMP];
last = boardData[B_TEMP_D];
suffix = "C";
prefix = "BOX TEMP";
decimals = 2;
break;
case 1:
//value = ratioPulseDistance * data.tachometerAbs;
// TODO : check how that will work once hal is wired
first = boardData[B_SPEED];
last = boardData[B_SPEED_D];
suffix = "KM";
prefix = "DISTANCE";
decimals = 2;
break;
case 2:
first = boardData[B_VOLT];
last = boardData[B_VOLT_D];
suffix = "V";
prefix = "BATTERY";
decimals = 2;
break;
}
// Display prefix (title)
displayString = prefix;
@ -429,15 +488,13 @@ void drawBoardVoltage() {
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(x, y - 1, displayBuffer);
// Split up the float value: a number, b decimals.
int first = abs(floor(value));
int last = value * pow(10, 3) - first * pow(10, 3);
// Add leading zero
if (first <= 9) {
displayString = "0" + (String)first;
if(connected) displayString = "0" + (String)first;
else displayString = "XX";
} else {
displayString = (String)first;
if(connected) displayString = (String)first;
else displayString = "XX";
}
// Display numbers
@ -446,8 +503,17 @@ void drawBoardVoltage() {
u8g2.drawStr(x + 55, y + 13, displayBuffer);
// Display decimals
displayString = "." + (String)last;
displayString.toCharArray(displayBuffer, 3);
// Add leading zero
if (last <= 9) {
if(connected) displayString = ".0" + (String)last;
else displayString = ".XX";
} else {
if(connected) displayString = "." + (String)last;
else displayString = ".XX";
}
// Display decimals
displayString.toCharArray(displayBuffer, decimals + 2);
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(x + 86, y - 1, displayBuffer);
@ -456,6 +522,7 @@ void drawBoardVoltage() {
displayString.toCharArray(displayBuffer, 10);
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(x + 86 + 2, y + 13, displayBuffer);
}
void drawSettingsMenu() {
@ -483,6 +550,58 @@ void drawSettingsMenu() {
}
*/
}
void controlSettingsMenu() {
/*
if (triggerActive()) {
if (settingsChangeFlag == false) {
// Save settings to EEPROM
if (changeSelectedSetting == true) {
updateEEPROMSettings();
}
changeSelectedSetting = !changeSelectedSetting;
settingsChangeFlag = true;
}
} else {
settingsChangeFlag = false;
}
if (hallMeasurement >= (remoteSettings.maxHallValue - 150) && settingsLoopFlag == false) {
// Up
if (changeSelectedSetting == true) {
int val = getSettingValue(currentSetting) + 1;
if (inRange(val, settingRules[currentSetting][1], settingRules[currentSetting][2])) {
setSettingValue(currentSetting, val);
settingsLoopFlag = true;
}
} else {
if (currentSetting != 0) {
currentSetting--;
settingsLoopFlag = true;
}
}
}
else if (hallMeasurement <= (remoteSettings.minHallValue + 150) && settingsLoopFlag == false) {
// Down
if (changeSelectedSetting == true) {
int val = getSettingValue(currentSetting) - 1;
if (inRange(val, settingRules[currentSetting][1], settingRules[currentSetting][2])) {
setSettingValue(currentSetting, val);
settingsLoopFlag = true;
}
} else {
if (currentSetting < (numOfSettings - 1)) {
currentSetting++;
settingsLoopFlag = true;
}
}
} else if (inRange(hallMeasurement, remoteSettings.centerHallValue - 50, remoteSettings.centerHallValue + 50)) {
settingsLoopFlag = false;
}*/
}
void drawSettingNumber() {
// Position on OLED
@ -509,7 +628,7 @@ void updateMainDisplay() {
drawSettingNumber();
} else {
drawThrottle();
drawBoardVoltage();
drawPage();
drawBatteryLevel();
drawSignal();
}
@ -527,7 +646,7 @@ void drawStartScreen() {
u8g2.setFont(u8g2_font_helvR10_tr );
u8g2.drawStr(34, 22, displayBuffer);
} while ( u8g2.nextPage() );
delay(1500);
delay(800);
}
//############ End Drawing Functions
@ -544,6 +663,8 @@ void setup() {
delay(50);
digitalWrite(16, HIGH);
analogSetPinAttenuation(batteryMeasurePin, ADC_6db);
// setup other pins
pinMode(triggerPin, INPUT_PULLUP);
@ -569,12 +690,10 @@ void setup() {
ESP.restart();
}
//configDeviceAP(true);
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
//ScanForboard();
//ScanForBoard();
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info.
@ -590,11 +709,15 @@ void setup() {
}
void loop() {
// Call function to update display and LED
updateMainDisplay();
calculateThrottlePosition();
readAccel();
Serial.print("Accel Values: ");
Serial.print(AcX);
Serial.print(" ");
Serial.println(GyX);
calculateThrottlePosition();
if (changeSettings == true) {
// Use throttle and trigger to change settings
//controlSettingsMenu();
@ -614,34 +737,14 @@ void loop() {
// Add board as peer if it has not been added already
bool isPaired = manageBoard();
if (isPaired) {
// pair success or already paired
// Send data to device
sendData();
/* update Value
char buf[10];
sprintf(buf, "%i", map(analogRead(leverPin), HAL_MIN, HAL_MAX, TRIM_LOW, TRIM_HIGH));
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_10x20_tr );
u8g2.drawStr(0, 20, buf);
} while ( u8g2.nextPage() );
*/
} else {
// board pair failed
Serial.println("board not found / paired!");
}
}
else {
// No board found to process
}
delay(20);
}
readAccel();
Serial.print(AcX);
Serial.print(" ");
Serial.println(GyX);
}