Adding Graphics, changing display

This commit is contained in:
Lukas Bachschwell 2018-04-17 11:52:38 +02:00
parent f13f85df37
commit 585660685c

View File

@ -3,22 +3,50 @@
#include <Arduino.h> #include <Arduino.h>
#include <esp_now.h> #include <esp_now.h>
#include <WiFi.h> #include <WiFi.h>
#include <SSD1306.h> #include <U8g2lib.h>
#include <EEPROM.h> // ESP32 ?
#include "mac_config.h" #include "mac_config.h"
// Defining the type of display used (128x32)
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R2, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
// Defining variables for OLED display
char displayBuffer[20];
String displayString;
short displayData = 0;
unsigned long lastSignalBlink;
unsigned long lastDataRotation;
static unsigned char logo_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x80, 0x3c, 0x01, 0xe0, 0x00, 0x07, 0x70, 0x18, 0x0e, 0x30, 0x18, 0x0c, 0x98, 0x99, 0x19, 0x80, 0xff, 0x01, 0x04, 0xc3, 0x20, 0x0c, 0x99, 0x30, 0xec, 0xa5, 0x37, 0xec, 0xa5, 0x37, 0x0c, 0x99, 0x30, 0x04, 0xc3, 0x20, 0x80, 0xff, 0x01, 0x98, 0x99, 0x19, 0x30, 0x18, 0x0c, 0x70, 0x18, 0x0e, 0xe0, 0x00, 0x07, 0x80, 0x3c, 0x01, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static unsigned char signal_transmitting_bits[] = {
0x18, 0x00, 0x0c, 0x00, 0xc6, 0x00, 0x66, 0x00, 0x23, 0x06, 0x33, 0x0f,
0x33, 0x0f, 0x23, 0x06, 0x66, 0x00, 0xc6, 0x00, 0x0c, 0x00, 0x18, 0x00
};
static unsigned char signal_connected_bits[] = {
0x18, 0x00, 0x0c, 0x00, 0xc6, 0x00, 0x66, 0x00, 0x23, 0x06, 0x33, 0x09,
0x33, 0x09, 0x23, 0x06, 0x66, 0x00, 0xc6, 0x00, 0x0c, 0x00, 0x18, 0x00
};
static unsigned char signal_noconnection_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x09,
0x00, 0x09, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Global copy of slave // Global copy of slave
esp_now_peer_info_t slave; esp_now_peer_info_t slave;
#define CHANNEL 3 #define CHANNEL 3
#define PRINTSCANRESULTS 0 #define PRINTSCANRESULTS 0
#define DELETEBEFOREPAIR 0 #define DELETEBEFOREPAIR 0
#define POT_MAX 3900 #define HAL_MIN 1290
#define HAL_MAX 2285
#define TRIM_LOW 180 #define TRIM_LOW 180
#define TRIM_HIGH 31 #define TRIM_HIGH 0
//#define pairingMode //#define pairingMode
#define leverPin 36 #define leverPin 36
SSD1306 display(0x3c, 4, 15); #define batteryMeasurePin 35
// ESPNOW functions ############################## // ESPNOW functions ##############################
// Scan for slaves in AP mode // Scan for slaves in AP mode
@ -159,7 +187,7 @@ bool manageSlave() {
// send data // send data
void sendData() { void sendData() {
uint8_t esc1 = map(analogRead(leverPin), 0, POT_MAX, TRIM_LOW, TRIM_HIGH); uint8_t esc1 = map(analogRead(leverPin), HAL_MIN, HAL_MAX, TRIM_LOW, TRIM_HIGH);
uint8_t esc2 = esc1; uint8_t esc2 = esc1;
const uint8_t data[] = { esc1, esc2 }; // no mixture for the normal mode const uint8_t data[] = { esc1, esc2 }; // no mixture for the normal mode
@ -194,6 +222,77 @@ void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Last Packet Sent to: "); Serial.println(macStr); 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: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
} }
//############ End ESP Now
// Return true if trigger is activated, false otherwice
boolean triggerActive() {
if (digitalRead(triggerPin) == LOW)
return true;
else
return false;
}
// Function used to indicate the remotes battery level.
int batteryLevel() {
float voltage = batteryVoltage();
if (voltage <= minVoltage) {
return 0;
} else if (voltage >= maxVoltage) {
return 100;
} else {
return (voltage - minVoltage) * 100 / (maxVoltage - minVoltage);
}
}
// Function to calculate and return the remotes battery voltage.
float batteryVoltage() {
float batteryVoltage = 0.0;
int total = 0;
return 3.6; // for now always full
for (int i = 0; i < 10; i++) {
total += analogRead(batteryMeasurePin);
}
batteryVoltage = (refVoltage / 1024.0) * ((float)total / 10.0);
return batteryVoltage;
}
// DRAWING
void updateMainDisplay() {
u8g2.firstPage();
do {
if (changeSettings == true) {
drawSettingsMenu();
drawSettingNumber();
} else {
drawThrottle();
drawPage();
drawBatteryLevel();
drawSignal();
}
} while ( u8g2.nextPage() );
}
void drawStartScreen() {
u8g2.firstPage();
do {
u8g2.drawXBM( 4, 4, 24, 24, logo_bits);
displayString = "Esk8 remote";
displayString.toCharArray(displayBuffer, 12);
u8g2.setFont(u8g2_font_helvR10_tr );
u8g2.drawStr(34, 22, displayBuffer);
} while ( u8g2.nextPage() );
delay(1500);
}
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@ -208,12 +307,8 @@ void setup() {
digitalWrite(16, HIGH); digitalWrite(16, HIGH);
Serial.println("ESPNowSkate Sender"); Serial.println("ESPNowSkate Sender");
display.init(); u8g2.begin();
display.flipScreenVertically(); drawStartScreen();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(5, 5, "ESPNOW Test");
display.display();
// This is the mac address of the Master in Station Mode // This is the mac address of the Master in Station Mode
Serial.print("STA MAC: "); Serial.println(WiFi.macAddress()); Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
@ -234,7 +329,7 @@ void setup() {
// Retrieve Slave from config: // Retrieve Slave from config:
for (int i = 0; i < 6; ++i ) { for (int i = 0; i < 6; ++i ) {
slave.peer_addr[i] = (uint8_t) mac_reciever[i]; slave.peer_addr[i] = (uint8_t) mac_receiver[i];
} }
slave.channel = CHANNEL; // pick a channel slave.channel = CHANNEL; // pick a channel
slave.encrypt = 0; // no encryption slave.encrypt = 0; // no encryption
@ -242,11 +337,13 @@ void setup() {
void loop() { void loop() {
char buf[10]; char buf[10];
sprintf(buf, "%i", map(analogRead(leverPin), 0, POT_MAX, TRIM_LOW, TRIM_HIGH)); sprintf(buf, "%i", map(analogRead(leverPin), HAL_MIN, HAL_MAX, TRIM_LOW, TRIM_HIGH));
//Serial.println(analogRead(leverPin)); u8g2.firstPage();
display.clear(); do {
display.drawString(3, 0, buf); u8g2.setFont(u8g2_font_10x20_tr );
display.display(); u8g2.drawStr(0, 20, buf);
} while ( u8g2.nextPage() );
// If Slave is found, it would be populate in `slave` variable // If Slave is found, it would be populate in `slave` variable
// We will check if `slave` is defined and then we proceed further // We will check if `slave` is defined and then we proceed further