diff --git a/platformio.ini b/platformio.ini index 996ca53..537a185 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,23 +9,15 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -default_envs = esp12e +default_envs = leonardo name = RemoteTrainControl -[env:esp12e] -platform = espressif8266 +[env:leonardo] +platform = atmelavr framework = arduino -board = esp12e -lib_deps = - adafruit/Adafruit ADS1X15@^2.4.0 - SPI - adafruit/Adafruit SSD1306@^2.5.7 - -[env:nodemcuv2] -platform = espressif8266 -framework = arduino -board = nodemcuv2 +board = leonardo lib_deps = adafruit/Adafruit ADS1X15@^2.4.0 SPI adafruit/Adafruit SSD1306@^2.5.7 + jgromes/RadioLib@^5.7.0 diff --git a/src/controls.cpp b/src/controls.cpp index feff2f4..5979714 100644 --- a/src/controls.cpp +++ b/src/controls.cpp @@ -10,12 +10,11 @@ void controlsInit() { ads1115.setGain(GAIN_ONE); } -float map(float x, float in_min, float in_max, float out_min, float out_max) -{ +float map(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } -void getControlsState(float &throttle, int &direction, bool &horn, bool &bell, bool &lights) { +void getControlsState(float &throttle, int8_t &direction, bool &horn, bool &bell, bool &lights) { float throttle_volts = ads1115.computeVolts(ads1115.readADC_SingleEnded(0)); float direction_volts = ads1115.computeVolts(ads1115.readADC_SingleEnded(1)); diff --git a/src/controls.h b/src/controls.h index 2a862a1..75ff29c 100644 --- a/src/controls.h +++ b/src/controls.h @@ -3,6 +3,6 @@ void controlsInit(); float map(float x, float in_min, float in_max, float out_min, float out_max); -void getControlsState(float &throttle, int &direction, bool &horn, bool &bell, bool &lights); +void getControlsState(float &throttle, int8_t &direction, bool &horn, bool &bell, bool &lights); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 427f4a7..bf7b906 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,29 +1,69 @@ #include "Arduino.h" #include "screen.h" -#include "wifi.h" #include "controls.h" -#include -#include -#include +#include +#include "string.h" using namespace std; +// RF69 has the following connections: +// CS pin: 10 +// DIO0 pin: 2 +// RESET pin: 3 +RF69 radio = new Module(10, 2, 3); + +void radioInit() { + Serial.print(F("[RF69] Initializing ... ")); + int state = radio.begin(918.5, 300.0, 60.0, 250.0, 10, 32); + if (state == RADIOLIB_ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } +} + void setup() { Serial.begin(9600); displayInit(); controlsInit(); - WiFi.softAP(WIFI_SSID, WIFI_PASS); - displayWiFiInformation(WIFI_SSID, WIFI_PASS); + radioInit(); } float throttle = 0.0f; -uint8_t direction = 0; +int8_t direction = 0; bool horn = false; bool bell = false; bool lights = false; +void sendStringToRadio(String str) { + int state = radio.transmit("Hello World!"); + if (state == RADIOLIB_ERR_NONE) { + } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) { + // the supplied packet was longer than 64 bytes + Serial.println(F("too long!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + } +} + +void serializeThrottleDirection(float throttle, int8_t direction, String &str) { + str += String(throttle); + str += ","; + str += String(direction); +} + +void sendControlOutputs(float throttle, int8_t direction, bool horn, bool bell, bool lights) { + String str; + serializeThrottleDirection(throttle, direction, str); + Serial.println(str); + sendStringToRadio(str); +} + void loop() { getControlsState(throttle, direction, horn, bell, lights); displayControlStatus(throttle, direction, horn, bell, lights); - //sendControlOutputs(throttle, direction, horn, bell, lights); - delay(1); -} \ No newline at end of file + sendControlOutputs(throttle, direction, horn, bell, lights); + delay(100); +} diff --git a/src/screen.cpp b/src/screen.cpp index dc56b6d..422889f 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include "string.h" using namespace std; #define SCREEN_WIDTH 128 @@ -21,7 +21,7 @@ void displayInit() { } } -void displayString(string inputString) { +void displayString(String inputString) { if (!displayEnabled) { return; } @@ -30,25 +30,7 @@ void displayString(string inputString) { } } -void displayWiFiInformation(string wifiSSID, string wifiPassword) { - if (!displayEnabled) { - return; - } - display.clearDisplay(); - display.setTextSize(1); - display.setTextColor(SSD1306_WHITE); - display.setCursor(0,0); - displayString("WiFi Information:"); - display.setCursor(0,8); - displayString("SSID: "); - displayString(wifiSSID); - display.setCursor(0,16); - displayString("Pass: "); - displayString(wifiPassword); - display.display(); -} - -void displayDirection(int &direction) { +void displayDirection(int8_t &direction) { if (direction == 0) { displayString("N"); } else if (direction == 1) { @@ -68,7 +50,7 @@ void displayBool(bool &state) { } } -void displayControlStatus(float &throttle, int &direction, bool &horn, bool &bell, bool &lights) { +void displayControlStatus(float &throttle, int8_t &direction, bool &horn, bool &bell, bool &lights) { if (!displayEnabled) { return; } diff --git a/src/screen.h b/src/screen.h index 30dd7ad..4031b87 100644 --- a/src/screen.h +++ b/src/screen.h @@ -1,14 +1,13 @@ #ifndef SCREEN_H #define SCREEN_H -#include +#include "string.h" using namespace std; void displayInit(); -void displayString(string inputString); -void displayWiFiInformation(string wifiSSID, string wifiPassword); -void displayDirection(int &direction); +void displayString(String inputString); +void displayDirection(int8_t &direction); void displayBool(bool &state); -void displayControlStatus(float &throttle, int &direction, bool &horn, bool &bell, bool &lights); +void displayControlStatus(float &throttle, int8_t &direction, bool &horn, bool &bell, bool &lights); #endif \ No newline at end of file