diff --git a/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde b/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde index edc1820..c77154c 100644 --- a/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde +++ b/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde @@ -166,7 +166,7 @@ void setup() { //myPort = new Serial(this, "COM3", 115200); - myPort = new Serial(this, "/dev/ttyUSB0", 115200); + myPort = new Serial(this, "/dev/ttyACM0", 115200); } void Set() { diff --git a/platformio.ini b/platformio.ini index dc9bdbd..4aeb66f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -26,7 +26,7 @@ lib_deps = build_flags = -D PIXELPIN=4 -D NUMPIXELS=600 - + [env:d1_mini] platform = espressif8266 board = d1_mini diff --git a/src/espnow.cpp b/src/espnow.cpp new file mode 100644 index 0000000..80dd8b5 --- /dev/null +++ b/src/espnow.cpp @@ -0,0 +1,64 @@ +#ifdef DEACTIVATED +#include "espnow.h" + + +//Mac Adress Receiver: 24:58:7c:cd:a8:c4 + +// Create a struct_message +struct_message espnowdata; + +void readMacAddress(){ + uint8_t baseMac[6]; + esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac); + if (ret == ESP_OK) { + Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n", + baseMac[0], baseMac[1], baseMac[2], + baseMac[3], baseMac[4], baseMac[5]); + } else { + Serial.println("Failed to read MAC address"); + } +} + +void initESPNOW() { + WiFi.mode(WIFI_STA); + WiFi.begin(); + + + Serial.print("[DEFAULT] ESP32 Board MAC Address: "); + readMacAddress(); + + + // Init ESP-NOW + if (esp_now_init() != ESP_OK) { + Serial.println("Error initializing ESP-NOW"); + return; + } + + + + // Once ESPNow is successfully Init, we will register for recv CB to + // get recv packer info + esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv)); + +} + + + + + +void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { + memcpy(&espnowdata, incomingData, sizeof(espnowdata)); + Serial.print("Bytes received: "); + Serial.println(len); + Serial.print(" Command: "); + Serial.println(espnowdata.cmd); + + statuspixel.setPixelColor(0, statuspixel.Color((millis()%3==0)*255,(millis()%3==1)*255,(millis()%3==2)*255)); //change status led when packet arrives + statuspixel.show(); + + processCommand(&espnowdata); + + Serial.println(); +} + +#endif \ No newline at end of file diff --git a/src/espnow.h b/src/espnow.h new file mode 100644 index 0000000..047d9c4 --- /dev/null +++ b/src/espnow.h @@ -0,0 +1,45 @@ +#ifdef DEACTIVATED //deactivated to compile again without espnow + +#ifndef espnow_h +#define espnow_h + + + +#include +#include + +#include +#include + +extern Adafruit_NeoPixel statuspixel; +extern void processCommand(struct_message espnowdata); + +void readMacAddress(); + +void initESPNOW(); + + +// Must match the receiver structure +enum command { findled, setheight, resetheightmap, saveheightmap }; +/* +findled: a=lednum, b=unused +setheight: a=lednum,b=height +resetheightmap: a=23, b=42 +saveheightmap: a=24, b=43 +*/ + +typedef struct struct_message { + command cmd; + int a; + int b; +} struct_message; + + +// callback function that will be executed when data is received +void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len); + + + +#endif + +#endif \ No newline at end of file diff --git a/src/fx_flash.cpp b/src/fx_flash.cpp index 41ccb46..74c84bf 100644 --- a/src/fx_flash.cpp +++ b/src/fx_flash.cpp @@ -1,7 +1,7 @@ #include "fx_flash.h" #include "effect.h" -FX_Flash::FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,uint32_t flashcolor) +FX_Flash::FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint16_t *height,uint32_t flashcolor) { _numpixels=numpixels; _strip=strip; diff --git a/src/fx_flash.h b/src/fx_flash.h index 8b9119b..a56e869 100644 --- a/src/fx_flash.h +++ b/src/fx_flash.h @@ -7,7 +7,7 @@ class FX_Flash : public Effect { public: - FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,uint32_t flashcolor); + FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint16_t *height,uint32_t flashcolor); FX_Flash(); void updateRoutine(float updatedelayms); void updateGraphics(); @@ -20,7 +20,7 @@ class FX_Flash : public Effect long _starttime; uint8_t _brightness; long _flashtime; - uint8_t *_height; + uint16_t *_height; }; #endif diff --git a/src/fx_scanner.cpp b/src/fx_scanner.cpp index aadacbb..0c0df55 100644 --- a/src/fx_scanner.cpp +++ b/src/fx_scanner.cpp @@ -1,7 +1,7 @@ #include "fx_scanner.h" #include "effect.h" -FX_Scanner::FX_Scanner(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,int startpos,float scannervel,uint32_t scannercolor) +FX_Scanner::FX_Scanner(int numpixels,Adafruit_NeoPixel *strip,uint16_t *height,int startpos,float scannervel,uint32_t scannercolor) { _numpixels=numpixels; _pos = startpos; diff --git a/src/fx_scanner.h b/src/fx_scanner.h index 2b2153e..c58ecb5 100644 --- a/src/fx_scanner.h +++ b/src/fx_scanner.h @@ -7,7 +7,7 @@ class FX_Scanner : public Effect { public: - FX_Scanner(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,int startpos,float scannervel,uint32_t scannercolor); + FX_Scanner(int numpixels,Adafruit_NeoPixel *strip,uint16_t *height,int startpos,float scannervel,uint32_t scannercolor); FX_Scanner(); void updateRoutine(float updatedelayms); void updateGraphics(); @@ -18,7 +18,7 @@ class FX_Scanner : public Effect Adafruit_NeoPixel *_strip; float _pos; float _vel; - uint8_t *_height; + uint16_t *_height; uint32_t _scannercolor; }; diff --git a/src/fx_shootingstar.cpp b/src/fx_shootingstar.cpp index a0fa614..c996d60 100644 --- a/src/fx_shootingstar.cpp +++ b/src/fx_shootingstar.cpp @@ -1,7 +1,7 @@ #include "fx_shootingstar.h" #include "effect.h" -FX_ShootingStar::FX_ShootingStar(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height) +FX_ShootingStar::FX_ShootingStar(int numpixels,Adafruit_NeoPixel *strip,uint16_t *height) { _numpixels=numpixels; _strip=strip; diff --git a/src/fx_shootingstar.h b/src/fx_shootingstar.h index f0fe0f0..6e2c252 100644 --- a/src/fx_shootingstar.h +++ b/src/fx_shootingstar.h @@ -7,7 +7,7 @@ class FX_ShootingStar : public Effect { public: - FX_ShootingStar(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height); + FX_ShootingStar(int numpixels,Adafruit_NeoPixel *strip,uint16_t *height); FX_ShootingStar(); void updateRoutine(float updatedelayms); void updateGraphics(); @@ -19,7 +19,7 @@ class FX_ShootingStar : public Effect long _starttime; uint8_t _brightness; long _duration; - uint8_t *_height; + uint16_t *_height; int _pos; float _vel; }; diff --git a/src/main.cpp b/src/main.cpp index 31c937a..74856e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,7 @@ #include +//#include "espnow.h" //TODO + #include #ifdef __AVR__ @@ -36,7 +38,6 @@ void saveHeightmapRaw(); #ifdef PIN_NEOPIXEL -#include Adafruit_NeoPixel statuspixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); #endif @@ -68,9 +69,9 @@ unsigned long last_ldrread=0; long loopmillis=0; -uint8_t height[NUMPIXELS]; -uint8_t heightraw[NUMPIXELS]; //uninterpolated values -#define MAXHEIGHT 254 +uint16_t height[NUMPIXELS]; +uint16_t heightraw[NUMPIXELS]; //uninterpolated values +#define MAXHEIGHT 350 std::vector wagon_arr; uint8_t maxid=0; @@ -108,6 +109,8 @@ long last_changePersistancemode=0; void setup() { Serial.begin(115200); + //initESPNOW(); //todo, deactivated to compile + #ifdef PIN_LDR pinMode(PIN_LDR, INPUT); #endif @@ -156,19 +159,40 @@ void setup() { resetHeightmap(); - loadHeightmapRaw(); - + //loadHeightmapRaw(); + + heightraw[0]=0; + heightraw[50]=96; + heightraw[64]=103; + heightraw[87]=100; + heightraw[107]=120; + heightraw[155]=199; + heightraw[185]=220; + heightraw[238]=184; + heightraw[269]=209; + heightraw[290]=187; + heightraw[318]=120; + heightraw[358]=187; + heightraw[385]=135; + heightraw[408]=114; + heightraw[464]=203; + heightraw[494]=240; + heightraw[502]=243; + heightraw[506]=240; + heightraw[550]=203; + heightraw[574]=188; + heightraw[599]=199; interpolateHeightValues(); - /* + Serial.println(); for (int i=0;i0) { Serial.println("String:"+serialstring); - - if (serialstring.equals("run")){ strip.setBrightness(BRIGHTNESS_RUN); configmode=false; @@ -648,7 +677,8 @@ void loop_achterbahn(){ //Check spawning - + #define AUTOSPAWN + #ifdef AUTOSPAWN if (lastCheckspawn+CHECKSPAWNDELAY>8)); //address, value, highbyte + c_eepromaddress++; } Serial.println("Saved to EEPROM"); EEPROM.commit(); //write changes to eeprom. EEPROM.end() will also commit and release the ram copy of eeprom contents diff --git a/src/wagon.cpp b/src/wagon.cpp index 2fb074f..f6b8785 100644 --- a/src/wagon.cpp +++ b/src/wagon.cpp @@ -30,7 +30,7 @@ 191,193,194,196,198,200,202,204,206,208,210,212,214,216,218,220, 222,224,227,229,231,233,235,237,239,241,244,246,248,250,252,255}; */ -Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,float pos, float trainlength,float startvel,float startacc, float wagonmass, uint32_t wagoncolor) +Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint16_t *height,float pos, float trainlength,float startvel,float startacc, float wagonmass, uint32_t wagoncolor) { _id = id; _numpixels=numpixels; diff --git a/src/wagon.h b/src/wagon.h index 2d493b5..0d75428 100644 --- a/src/wagon.h +++ b/src/wagon.h @@ -7,7 +7,7 @@ class Wagon { public: int _id; - Wagon(int id,int numpixels,Adafruit_NeoPixel *strip,uint8_t *height, float pos, float wagonlength, float startvel,float startacc, float wagonmass, uint32_t wagoncolor); + Wagon(int id,int numpixels,Adafruit_NeoPixel *strip,uint16_t *height, float pos, float wagonlength, float startvel,float startacc, float wagonmass, uint32_t wagoncolor); Wagon(); bool operator==(const Wagon &r) const; void updatePhysics(float updatedelayms); @@ -28,7 +28,7 @@ class Wagon float _acc; float _wagonmass; float _trainlength; - uint8_t *_height; + uint16_t *_height; long _spawntime; float _lastvel; float _lastpositivedirchangepos;