From 2f17318f48aaaa0f3182e16cc045c906aa8e5f59 Mon Sep 17 00:00:00 2001 From: Jens Noack Date: Mon, 15 Nov 2021 08:25:19 +0100 Subject: [PATCH] all modules seems to work now - switches are some kind of buggy --- .vscode/settings.json | 5 +++ include/display.h | 21 +++++------ include/eeprom.h | 8 +++-- include/leds.h | 31 ++++++++++++++-- include/main.h | 84 +++++++++++++++++++++++++++++++++---------- include/mp3.h | 33 +++++++++++++---- include/taster.h | 22 +++++++----- include/train.h | 6 ++-- platformio.ini | 2 +- src/main.cpp | 63 ++++++++++++++++++++------------ 10 files changed, 200 insertions(+), 75 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9e37c85 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "functional": "cpp" + } +} \ No newline at end of file diff --git a/include/display.h b/include/display.h index a9c8650..c92e1e6 100644 --- a/include/display.h +++ b/include/display.h @@ -11,12 +11,12 @@ private: const uint8_t MAX_CONTRAST = 255; SSD1306Wire * _display; - unsigned int _dispaddr; - size_t _pin_sda, _pin_scl; - void _initDisplay(); + uint8_t _dispaddr; + int _pin_sda, _pin_scl; public: - display(size_t pin_sda, size_t pin_scl, unsigned int display_addr); + display(uint8_t dispaddr, int pin_sda, int pin_scl); ~display(); + void init(); void header(String title); void header(String title, bool wificonected, bool localvalues, bool lastdatasent, bool timesynced); void data(unsigned int x, unsigned int y , float val, String valname); @@ -27,32 +27,29 @@ public: }; -display::display(size_t pin_sda, size_t pin_scl, unsigned int display_addr) +display::display(uint8_t dispaddr, int pin_sda, int pin_scl) { _pin_sda = pin_sda; _pin_scl = pin_scl; - _dispaddr = display_addr; + _dispaddr = dispaddr; _display = new SSD1306Wire(_dispaddr, _pin_sda, _pin_scl); - _initDisplay(); } display::~display() { } -void display::_initDisplay() +void display::init() { //initialise OLED and display Welcome Message ... _display->init(); - //_display->flipScreenVertically(); -/* _display->setTextAlignment(TEXT_ALIGN_CENTER); + _display->flipScreenVertically(); + _display->setTextAlignment(TEXT_ALIGN_CENTER); _display->setFont(Roboto_Condensed_Bold_16); _display->clear(); _display->setContrast(MAX_CONTRAST); _display->drawString(64, 32, "Itsblue.de"); _display->display(); - - */ } void display::header(String title, bool wificonected, bool localvalues, bool lastdatasent, bool timesynced) diff --git a/include/eeprom.h b/include/eeprom.h index 0a8a8dd..ddb9221 100644 --- a/include/eeprom.h +++ b/include/eeprom.h @@ -1,6 +1,5 @@ #ifndef __eeprom_H__ #define __eeprom_H__ - #include class eeprom @@ -12,17 +11,22 @@ public: ~eeprom(); void set_int(String name , uint64_t val ); uint64_t get_int(String name ); + void begin(); }; eeprom::eeprom() { - NVS.begin(); } eeprom::~eeprom() { } +void eeprom::begin() +{ + NVS.begin(); +} + void eeprom::set_int(String name , uint64_t val ) { NVS.setInt(name, val); diff --git a/include/leds.h b/include/leds.h index f1955fe..282d671 100644 --- a/include/leds.h +++ b/include/leds.h @@ -53,6 +53,8 @@ class leds bool colorWipe( uint32_t color ); void add_button_id(uint16_t mask); bool do_fade(); + bool fade_off(); + bool fade_on(); void setBrigthness(uint8_t brigthness); void setHS(uint16_t H, uint8_t S); void setColor(uint32_t color); @@ -69,11 +71,19 @@ leds::leds(Adafruit_NeoPixel * stripe, uint16_t * state_flag, uint16_t state_mas _pixel_first = pixel_first; _pixel_nr = 1 + abs(pixel_last - pixel_first); _pixels = (_pixel *) malloc(sizeof(_pixel) * _pixel_nr); + size_t index = 0; + for(size_t nr=pixel_first; nr<=pixel_last;nr++) + { + _pixels[index].lednr = nr; + index++; + } _stripe = stripe; _R = _calc_R(); _setLeds(0); _rainbow_pixel = 0; _rainbow_color = 0; + _prev_state = *_state_flag; + _fading_done = true; } @@ -86,7 +96,7 @@ leds::~leds() void leds::_setLeds() { - for(uint8_t nr = 0; nr < _pixel_nr; nr++) + for(unsigned int nr = 0; nr < _pixel_nr; nr++) { _stripe->setPixelColor(_pixels[nr].lednr, _pixels[nr].color); } @@ -95,8 +105,9 @@ void leds::_setLeds() void leds::_setLeds(uint32_t color) { - for(uint8_t nr = 0; nr < _pixel_nr; nr++) + for(unsigned int nr = 0; nr < _pixel_nr; nr++) { + //Serial.printf("setting led %d with nr %d in stripe to color %d\n", nr, _pixels[nr].lednr, color); _stripe->setPixelColor(_pixels[nr].lednr, color); _pixels[nr].color = color; } @@ -119,6 +130,7 @@ bool leds::do_fade() if(_prev_state != curr_state) { + //Serial.printf("Current state is 0x%08x\n", curr_state); _prev_state = curr_state; _fading_done = false; } @@ -129,15 +141,28 @@ bool leds::do_fade() if(0 == curr_state) { _fade_off(); + //Serial.printf("Fading off.\n"); } else { _fade_on(); + //Serial.printf("Fading ON\n"); } } return updated; } +bool leds::fade_on() +{ + return _fade_on(); +} + +bool leds::fade_off() +{ + return _fade_off(); +} + + float leds::_calc_R(void) { return (FADESTEPS * log10(2))/(log10(_local_brightness)); @@ -232,11 +257,11 @@ bool leds::_fade_off() } - void leds::rainbowCycle() { if((millis() - _last_rainbowcycle) > WAIT_RAINBOWCYCLE_MS) { + //Serial.printf("Rainbow! Next color\n"); _last_rainbowcycle = millis(); _rainbow_color++; if(_rainbow_color >= 256*1) diff --git a/include/main.h b/include/main.h index 5ac047d..9dcf5be 100644 --- a/include/main.h +++ b/include/main.h @@ -1,24 +1,25 @@ #include -//#include "train.h" +#include "train.h" #include "relais.h" #include "taster.h" -//#include "leds.h" -//#include "mp3.h" +#include "leds.h" +#include "mp3.h" #include "display.h" +#include //#include "eeprom.h" -//eeprom save; +//eeprom flash; ///---- OLED ---- -const size_t PIN_MONI_SDA = 25 ; //12 -const size_t PIN_MONI_SCL = 26 ; //14 -const unsigned int ADDR_MONI = 0x3c; -display moni(PIN_MONI_SDA, PIN_MONI_SCL, ADDR_MONI); +const int PIN_MONI_SDA = 21 ; //12 +const int PIN_MONI_SCL = 22 ; //14 +const uint8_t ADDR_MONI = 0x3c; +display moni(ADDR_MONI, PIN_MONI_SDA, PIN_MONI_SCL); ///---- MP3 ---- -const size_t PIN_MP3_RX = 3 ; //D2; -const size_t PIN_MP3_TX = 1 ; //D3; -//mp3 mp3ply(PIN_MP3_RX, PIN_MP3_TX); +const size_t PIN_MP3_RX = 25 ; //D2; +const size_t PIN_MP3_TX = 26 ; //D3; +mp3 mp3ply(PIN_MP3_RX, PIN_MP3_TX); ///---- RELAIS ---- -const size_t PIN_RELAIS_HAUESER = 18; +const size_t PIN_RELAIS_HAUESER = 5; const size_t PIN_RELAIS_STERNE = 17; const size_t PIN_RELAIS_SPIEGELBALL = 16; const size_t PIN_RELAIS_OTHER = 4; @@ -31,13 +32,59 @@ const size_t PIN_TRAIN_UNTEN = 32; const size_t PIN_TRAIN_OBEN = 33; #define PWM_CHANNEL_OBEN 5 #define PWM_CHANNEL_UNTEN 6 -//train zugunten(PIN_TRAIN_UNTEN, PWM_CHANNEL_UNTEN ); -//train zugoben(PIN_TRAIN_OBEN, PWM_CHANNEL_OBEN ); +train zugunten(PIN_TRAIN_UNTEN, PWM_CHANNEL_UNTEN ); +train zugoben(PIN_TRAIN_OBEN, PWM_CHANNEL_OBEN ); ///--- Taster ---- const size_t PIN_TASTER_AUSSEN_LICHT = 36; const size_t PIN_TASTER_AUSSEN_MOVE = 39; const size_t PIN_TASTER_TRAIN_UNTEN = 34; const size_t PIN_TASTER_TRAIN_OBEN = 35; + +const unsigned long BOUNCING_TIME_MS = 300; +unsigned long last_pressed_1, last_pressed_2, last_pressed_3, last_pressed_4; +unsigned long number_pressed_1, number_pressed_2, number_pressed_3, number_pressed_4; +/* +void IRAM_ATTR ISR_1() +{ + if(millis()-last_pressed_1 > BOUNCING_TIME_MS) + { + last_pressed_1 = millis(); + number_pressed_1 += 1; + } +} + +void IRAM_ATTR ISR_2() +{ + if(millis()-last_pressed_2 > BOUNCING_TIME_MS) + { + last_pressed_2 = millis(); + number_pressed_2 += 1; + } +} + +void IRAM_ATTR ISR_3() +{ + if(millis()-last_pressed_3 > BOUNCING_TIME_MS) + { + last_pressed_3 = millis(); + number_pressed_3 += 1; + } +} + +void IRAM_ATTR ISR_4() +{ + if(millis()-last_pressed_4 > BOUNCING_TIME_MS) + { + last_pressed_4 = millis(); + number_pressed_4 += 1; + } +} + +taster taster_aussen_licht(PIN_TASTER_AUSSEN_LICHT, &ISR_1, &number_pressed_1); +taster taster_aussen_move(PIN_TASTER_AUSSEN_MOVE, &ISR_2, &number_pressed_2); +taster taster_train_unten(PIN_TASTER_TRAIN_UNTEN, &ISR_3, &number_pressed_3); +taster taster_train_oben(PIN_TASTER_TRAIN_OBEN, &ISR_4, &number_pressed_4); +*/ taster taster_aussen_licht(PIN_TASTER_AUSSEN_LICHT); taster taster_aussen_move(PIN_TASTER_AUSSEN_MOVE); taster taster_train_unten(PIN_TASTER_TRAIN_UNTEN); @@ -45,9 +92,10 @@ taster taster_train_oben(PIN_TASTER_TRAIN_OBEN); ///--- RGB LEDs --- uint16_t rgbled_state_flag = 0; const size_t PIN_RGBLEDS = 19; -#define NUMRGBLEDS 20 -//Adafruit_NeoPixel rgb_leds(NUMRGBLEDS, PIN_RGBLEDS, NEO_GRB + NEO_KHZ800); +#define NUMRGBLEDS 25 +Adafruit_NeoPixel rgb_leds(NUMRGBLEDS, PIN_RGBLEDS, NEO_GRB + NEO_KHZ800); enum led_t {LTANNE=1, LBACK=2, LSTERNE=4, LTEICH=8}; -//leds led_tanne(&rgb_leds, &rgbled_state_flag, LTANNE, 5000,220, 0, 5 ); -//leds led_teich(&rgb_leds, &rgbled_state_flag, LTEICH, 5000,220, 6, 10 ); \ No newline at end of file +leds led_tanne(&rgb_leds, &rgbled_state_flag, LTANNE, 5000,220, 0, 5 ); +leds led_teich(&rgb_leds, &rgbled_state_flag, LTEICH, 5000,220, 6, 10 ); +leds led_sterne(&rgb_leds, &rgbled_state_flag, LSTERNE, 5000,220, 11, 24 ); diff --git a/include/mp3.h b/include/mp3.h index e81a15b..cfa5340 100644 --- a/include/mp3.h +++ b/include/mp3.h @@ -18,6 +18,9 @@ public: void stop(); void play(size_t nr); unsigned long is_playing(); + void begin(byte vol=30); + void play_vol(size_t nr, byte vol); + void vol(byte val); }; mp3::mp3(size_t rx_pin, size_t tx_pin) @@ -25,20 +28,38 @@ mp3::mp3(size_t rx_pin, size_t tx_pin) _rx_pin = rx_pin; _tx_pin = tx_pin; player = new SerialMP3Player(_rx_pin, _tx_pin); - player->begin(9600); - delay(500); - player->sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card - delay(500); - player->setVol(30); } mp3::~mp3() { } +void mp3::begin(byte vol) +{ + player->begin(9600); + delay(500); + player->sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card + delay(500); + player->setVol(vol); +} + +void mp3::play_vol(size_t nr, byte vol) +{ + player->setVol(vol); + player->play(nr); + _play_since_ms = millis(); +} + + void mp3::play(size_t nr) { - player->play(nr); + player->playSL(nr); + _play_since_ms = millis(); +} + +void mp3::vol(byte val) +{ + player->setVol(val); _play_since_ms = millis(); } diff --git a/include/taster.h b/include/taster.h index bffa16b..2443956 100644 --- a/include/taster.h +++ b/include/taster.h @@ -6,35 +6,41 @@ class taster { private: - volatile const unsigned long _BOUNCING_TIME_MS = 200; + volatile const unsigned long _BOUNCING_TIME_MS = 300; volatile unsigned long _last_pressed; - size_t _taster_pin; - volatile unsigned long _number_pressed; + unsigned long _number_pressed; unsigned long _number_checked; + size_t _taster_pin; + //unsigned long * _ptr_number_pressed; void IRAM_ATTR _taster_int() { - if(millis()-_last_pressed > _BOUNCING_TIME_MS) + unsigned long msecs = millis(); + if(msecs -_last_pressed > _BOUNCING_TIME_MS) { - _last_pressed = millis(); + _last_pressed = msecs; _number_pressed += 1; } } public: - taster(size_t pin); + //taster(const size_t pin, void (*isrfunction)(), unsigned long* ptr_number_pressed); + taster(const size_t pin); ~taster(); bool pressed(); }; +//taster::taster(size_t pin, void (*isrfunction)(), unsigned long* ptr_number_pressed) taster::taster(size_t pin) { _taster_pin = pin; - _number_pressed = 0; + //_ptr_number_pressed = ptr_number_pressed; _number_checked = 0; + _number_pressed = 0; _last_pressed = 0; - pinMode(_taster_pin, INPUT_PULLUP); + pinMode(_taster_pin, INPUT); attachInterrupt(_taster_pin, std::bind(&taster::_taster_int,this), FALLING); + //attachInterrupt(_taster_pin, isrfunction, FALLING); } taster::~taster() diff --git a/include/train.h b/include/train.h index a3cb7e7..21d79a6 100644 --- a/include/train.h +++ b/include/train.h @@ -18,7 +18,6 @@ private: unsigned long _last_faded_ms; unsigned long _delay_ms; - void _set_pwm(uint8_t pwm_value); bool _fade(bool fade_up); public: @@ -27,6 +26,7 @@ public: void stop(); bool fade_on(); bool fade_off(); + void set_pwm(uint8_t pwm_value); }; @@ -46,12 +46,12 @@ train::~train() { } -void train::_set_pwm(uint8_t pwm_value) { +void train::set_pwm(uint8_t pwm_value) { ledcWrite(_pwmchannel, pwm_value); } void train::stop() { - _set_pwm(0); + set_pwm(0); } bool train::_fade(bool fade_up) diff --git a/platformio.ini b/platformio.ini index 4e59b7a..d77784f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,5 +16,5 @@ monitor_speed = 115200 lib_deps = salvadorrueda/SerialMP3Player@^1.1.0 plerup/EspSoftwareSerial@^6.14.1 - thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.1 tridenttd/TridentTD_ESP32NVS@^1.0 + thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.1 diff --git a/src/main.cpp b/src/main.cpp index 1fc1281..0e10b5d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,44 +4,64 @@ void show_counters(uint64_t counter_licht, uint64_t counter_move); //variables -uint64_t licht_all_count = 0; -uint64_t move_all_count = 0; +uint16_t licht_all_count = 0; +uint16_t move_all_count = 0; void setup() { Serial.begin(115200); + rgb_leds.begin(); + NVS.begin(); + moni.init(); //load counters - // save.set_int("licht", 0); - // save.set_int("move", 0); - // licht_all_count = save.get_int("licht"); - // move_all_count = save.get_int("move"); + //NVS.setInt("licht", 100); + //NVS.setInt("move", 50); + licht_all_count = NVS.getInt("licht"); + move_all_count = NVS.getInt("move"); // all relais off; relais_haeuser.off(); relais_sterne.off(); relais_spiegel.off(); relais_other.off(); -/* + zugoben.stop(); zugunten.stop(); - - led_tanne.setColor(0x0f0); - mp3ply.play(1); - - */ - + + led_tanne.setColor(rgb_leds.Color(0, 150, 0)); + rgbled_state_flag = rgbled_state_flag | LSTERNE; + mp3ply.begin(); + mp3ply.stop(); } uint8_t licht_count = 0; +bool faded_on = false; void loop() { - - //led_teich.rainbowCycle(); - //rgb_leds.show(); + if(faded_on == false) + { + if(true == led_sterne.fade_on()) + faded_on = true; + } + else + { + if(true == led_sterne.fade_off()) + faded_on = false; + } + led_teich.rainbowCycle(); + rgb_leds.show(); if(taster_aussen_licht.pressed()) + { relais_haeuser.toggle(); + licht_all_count++; + NVS.setInt("licht", licht_all_count); + } if(taster_aussen_move.pressed()) + { relais_other.toggle(); + move_all_count++; + NVS.setInt("move", move_all_count); + } if(taster_train_oben.pressed()) relais_sterne.toggle(); @@ -49,9 +69,9 @@ void loop() { if(taster_train_unten.pressed()) relais_spiegel.toggle(); - /* - show_counters(licht_all_count, move_all_count); + show_counters(licht_all_count, move_all_count); + /* switch (licht_count) { case 1: @@ -90,13 +110,12 @@ void loop() { } -/* + void show_counters(uint64_t counter_licht, uint64_t counter_move) { moni.clear(); moni.header("Zaehler"); - moni.data(10,15,counter_licht, "Licht:"); - moni.data(10,15,counter_move, "Moves:"); + moni.data(30,20,counter_licht, "Licht"); + moni.data(100,20,counter_move, "Moves"); moni.show(); } -*/ \ No newline at end of file