|
|
|
@ -9,6 +9,8 @@
|
|
|
|
|
#include "mac_config.h"
|
|
|
|
|
#include "graphics.h"
|
|
|
|
|
|
|
|
|
|
#include "accel.h"
|
|
|
|
|
|
|
|
|
|
// Defining variables for OLED display
|
|
|
|
|
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R2, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
|
|
|
|
|
char displayBuffer[20];
|
|
|
|
@ -29,7 +31,7 @@ bool settingsChangeValueFlag = false;
|
|
|
|
|
short hallMeasurement;
|
|
|
|
|
int throttle = 127;
|
|
|
|
|
int sendThrottle = 127;
|
|
|
|
|
byte hallCenterMargin = 4;
|
|
|
|
|
byte hallCenterMargin = 5;
|
|
|
|
|
|
|
|
|
|
byte currentSetting = 0;
|
|
|
|
|
const byte numOfSettings = 11;
|
|
|
|
@ -39,14 +41,14 @@ const float maxVoltage = 4.1;
|
|
|
|
|
const float refVoltage = 3.3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Global copy of slave
|
|
|
|
|
esp_now_peer_info_t slave;
|
|
|
|
|
#define CHANNEL 3
|
|
|
|
|
// Global copy of board
|
|
|
|
|
esp_now_peer_info_t board;
|
|
|
|
|
#define CHANNEL 1
|
|
|
|
|
#define PRINTSCANRESULTS 0
|
|
|
|
|
#define DELETEBEFOREPAIR 0
|
|
|
|
|
#define HAL_MIN 1390
|
|
|
|
|
#define HAL_MAX 2260
|
|
|
|
|
#define HAL_CENTER 1890
|
|
|
|
|
#define HAL_MAX 2230
|
|
|
|
|
#define HAL_CENTER 1880
|
|
|
|
|
#define TRIM_LOW 180
|
|
|
|
|
#define TRIM_HIGH 0
|
|
|
|
|
|
|
|
|
@ -57,13 +59,23 @@ esp_now_peer_info_t slave;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ESPNOW functions ##############################
|
|
|
|
|
// Scan for slaves in AP mode
|
|
|
|
|
// 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 ScanForSlave() {
|
|
|
|
|
void ScanForboard() {
|
|
|
|
|
int8_t scanResults = WiFi.scanNetworks();
|
|
|
|
|
// reset on each scan
|
|
|
|
|
bool slaveFound = 0;
|
|
|
|
|
memset(&slave, 0, sizeof(slave));
|
|
|
|
|
bool boardFound = 0;
|
|
|
|
|
memset(&board, 0, sizeof(board));
|
|
|
|
|
|
|
|
|
|
Serial.println("");
|
|
|
|
|
if (scanResults == 0) {
|
|
|
|
@ -85,35 +97,36 @@ void ScanForSlave() {
|
|
|
|
|
Serial.print(")");
|
|
|
|
|
Serial.println("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delay(10);
|
|
|
|
|
// Check if the current device starts with `Slave`
|
|
|
|
|
// Check if the current device starts with `board`
|
|
|
|
|
if (SSID.indexOf("ESK8") == 0) {
|
|
|
|
|
// SSID of interest
|
|
|
|
|
Serial.println("Found a Slave.");
|
|
|
|
|
Serial.println("Found a board.");
|
|
|
|
|
Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println("");
|
|
|
|
|
// Get BSSID => Mac Address of the Slave
|
|
|
|
|
// Get BSSID => Mac Address of the board
|
|
|
|
|
int mac[6];
|
|
|
|
|
if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) {
|
|
|
|
|
for (int ii = 0; ii < 6; ++ii ) {
|
|
|
|
|
slave.peer_addr[ii] = (uint8_t) mac[ii];
|
|
|
|
|
board.peer_addr[ii] = (uint8_t) mac[ii];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slave.channel = CHANNEL; // pick a channel
|
|
|
|
|
slave.encrypt = 0; // no encryption
|
|
|
|
|
board.channel = CHANNEL; // pick a channel
|
|
|
|
|
board.encrypt = 0; // no encryption
|
|
|
|
|
|
|
|
|
|
slaveFound = 1;
|
|
|
|
|
// we are planning to have only one slave in this example;
|
|
|
|
|
boardFound = 1;
|
|
|
|
|
// we are planning to have only one board in this example;
|
|
|
|
|
// Hence, break after we find one, to be a bit efficient
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slaveFound) {
|
|
|
|
|
Serial.println("Slave Found, processing..");
|
|
|
|
|
if (boardFound) {
|
|
|
|
|
Serial.println("board Found, processing..");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("Slave Not Found, trying again.");
|
|
|
|
|
Serial.println("board Not Found, trying again.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// clean up ram
|
|
|
|
@ -122,10 +135,10 @@ void ScanForSlave() {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void deletePeer() {
|
|
|
|
|
const esp_now_peer_info_t *peer = &slave;
|
|
|
|
|
const uint8_t *peer_addr = slave.peer_addr;
|
|
|
|
|
const esp_now_peer_info_t *peer = &board;
|
|
|
|
|
const uint8_t *peer_addr = board.peer_addr;
|
|
|
|
|
esp_err_t delStatus = esp_now_del_peer(peer_addr);
|
|
|
|
|
Serial.print("Slave Delete Status: ");
|
|
|
|
|
Serial.print("board Delete Status: ");
|
|
|
|
|
if (delStatus == ESP_OK) {
|
|
|
|
|
// Delete success
|
|
|
|
|
Serial.println("Success");
|
|
|
|
@ -141,25 +154,25 @@ void deletePeer() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the slave is already paired with the master.
|
|
|
|
|
// If not, pair the slave with master
|
|
|
|
|
bool manageSlave() {
|
|
|
|
|
if (slave.channel == CHANNEL) {
|
|
|
|
|
// Check if the board is already paired with the master.
|
|
|
|
|
// If not, pair the board with master
|
|
|
|
|
bool manageBoard() {
|
|
|
|
|
if (board.channel == CHANNEL) {
|
|
|
|
|
if (DELETEBEFOREPAIR) {
|
|
|
|
|
deletePeer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.print("Slave Status: ");
|
|
|
|
|
const esp_now_peer_info_t *peer = &slave;
|
|
|
|
|
const uint8_t *peer_addr = slave.peer_addr;
|
|
|
|
|
Serial.print("board Status: ");
|
|
|
|
|
const esp_now_peer_info_t *peer = &board;
|
|
|
|
|
const uint8_t *peer_addr = board.peer_addr;
|
|
|
|
|
// check if the peer exists
|
|
|
|
|
bool exists = esp_now_is_peer_exist(peer_addr);
|
|
|
|
|
if ( exists) {
|
|
|
|
|
// Slave already paired.
|
|
|
|
|
// board already paired.
|
|
|
|
|
Serial.println("Already Paired");
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// Slave not paired, attempt pair
|
|
|
|
|
// board not paired, attempt pair
|
|
|
|
|
esp_err_t addStatus = esp_now_add_peer(peer);
|
|
|
|
|
if (addStatus == ESP_OK) {
|
|
|
|
|
// Pair success
|
|
|
|
@ -187,8 +200,8 @@ bool manageSlave() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// No slave found to process
|
|
|
|
|
Serial.println("No Slave found to process");
|
|
|
|
|
// No board found to process
|
|
|
|
|
Serial.println("No board found to process");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -200,7 +213,7 @@ void sendData() {
|
|
|
|
|
|
|
|
|
|
const uint8_t data[] = { esc1, esc2 }; // no mixture for the normal mode
|
|
|
|
|
|
|
|
|
|
const uint8_t *peer_addr = slave.peer_addr;
|
|
|
|
|
const uint8_t *peer_addr = board.peer_addr;
|
|
|
|
|
Serial.print("Sending: "); Serial.println(esc1);
|
|
|
|
|
esp_err_t result = esp_now_send(peer_addr, data, sizeof(data));
|
|
|
|
|
Serial.print("Send Status: ");
|
|
|
|
@ -222,7 +235,7 @@ void sendData() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// callback when data is sent from Master to Slave
|
|
|
|
|
// callback when data is sent from Master to board
|
|
|
|
|
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
|
|
|
|
|
char macStr[18];
|
|
|
|
|
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
@ -230,10 +243,21 @@ 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 Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// callback when data is recv from board
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//############ End ESP Now
|
|
|
|
|
|
|
|
|
|
//############ Hardware Helpers
|
|
|
|
|
//############
|
|
|
|
|
// Check if an integer is within a min and max value
|
|
|
|
|
bool inRange(int val, int minimum, int maximum) {
|
|
|
|
|
return ((minimum <= val) && (val <= maximum));
|
|
|
|
@ -342,14 +366,14 @@ void drawThrottle() {
|
|
|
|
|
u8g2.drawHLine(x, y + 10, 5);
|
|
|
|
|
u8g2.drawHLine(x + 52 - 4, y + 10, 5);
|
|
|
|
|
|
|
|
|
|
if (sendThrottle >= 127) {
|
|
|
|
|
int width = map(sendThrottle, 127, 255, 0, 49);
|
|
|
|
|
if (throttle >= 127) {
|
|
|
|
|
int width = map(throttle, 127, 255, 0, 49);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
|
u8g2.drawVLine(x + i + 2, y + 2, 7);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
int width = map(sendThrottle, 0, 126, 49, 0);
|
|
|
|
|
int width = map(throttle, 0, 126, 49, 0);
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
|
u8g2.drawVLine(x + 50 - i, y + 2, 7);
|
|
|
|
|
}
|
|
|
|
@ -523,6 +547,8 @@ void setup() {
|
|
|
|
|
// setup other pins
|
|
|
|
|
pinMode(triggerPin, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
initAccel();
|
|
|
|
|
|
|
|
|
|
Serial.println("ESPNowSkate Sender");
|
|
|
|
|
u8g2.begin();
|
|
|
|
|
drawStartScreen();
|
|
|
|
@ -543,18 +569,24 @@ 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);
|
|
|
|
|
//ScanForSlave();
|
|
|
|
|
//ScanForboard();
|
|
|
|
|
|
|
|
|
|
// Once ESPNow is successfully Init, we will register for recv CB to
|
|
|
|
|
// get recv packer info.
|
|
|
|
|
esp_now_register_recv_cb(OnDataRecv);
|
|
|
|
|
|
|
|
|
|
// Retrieve Slave from config:
|
|
|
|
|
|
|
|
|
|
// Retrieve board from config:
|
|
|
|
|
for (int i = 0; i < 6; ++i ) {
|
|
|
|
|
slave.peer_addr[i] = (uint8_t) mac_receiver[i];
|
|
|
|
|
board.peer_addr[i] = (uint8_t) mac_receiver[i];
|
|
|
|
|
}
|
|
|
|
|
slave.channel = CHANNEL; // pick a channel
|
|
|
|
|
slave.encrypt = 0; // no encryption
|
|
|
|
|
board.channel = CHANNEL; // pick a channel
|
|
|
|
|
board.encrypt = 0; // no encryption
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
@ -575,12 +607,12 @@ void loop() {
|
|
|
|
|
sendThrottle = 127;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If Slave is found, it would be populate in `slave` variable
|
|
|
|
|
// We will check if `slave` is defined and then we proceed further
|
|
|
|
|
if (slave.channel == CHANNEL) { // check if slave channel is defined
|
|
|
|
|
// `slave` is defined
|
|
|
|
|
// Add slave as peer if it has not been added already
|
|
|
|
|
bool isPaired = manageSlave();
|
|
|
|
|
// If board is found, it would be populate in `board` variable
|
|
|
|
|
// We will check if `board` is defined and then we proceed further
|
|
|
|
|
if (board.channel == CHANNEL) { // check if board channel is defined
|
|
|
|
|
// `board` is defined
|
|
|
|
|
// 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
|
|
|
|
@ -596,15 +628,20 @@ void loop() {
|
|
|
|
|
} while ( u8g2.nextPage() );
|
|
|
|
|
*/
|
|
|
|
|
} else {
|
|
|
|
|
// slave pair failed
|
|
|
|
|
// board pair failed
|
|
|
|
|
|
|
|
|
|
Serial.println("Slave not found / paired!");
|
|
|
|
|
Serial.println("board not found / paired!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// No slave found to process
|
|
|
|
|
// No board found to process
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delay(20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readAccel();
|
|
|
|
|
Serial.print(AcX);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.println(GyX);
|
|
|
|
|
}
|
|
|
|
|