all modules seems to work now - switches are some kind of buggy

This commit is contained in:
Jens Noack 2021-11-15 08:25:19 +01:00
parent 5f55011894
commit 2f17318f48
10 changed files with 200 additions and 75 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"functional": "cpp"
}
}

View File

@ -11,12 +11,12 @@ private:
const uint8_t MAX_CONTRAST = 255; const uint8_t MAX_CONTRAST = 255;
SSD1306Wire * _display; SSD1306Wire * _display;
unsigned int _dispaddr; uint8_t _dispaddr;
size_t _pin_sda, _pin_scl; int _pin_sda, _pin_scl;
void _initDisplay();
public: 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(); ~display();
void init();
void header(String title); void header(String title);
void header(String title, bool wificonected, bool localvalues, bool lastdatasent, bool timesynced); void header(String title, bool wificonected, bool localvalues, bool lastdatasent, bool timesynced);
void data(unsigned int x, unsigned int y , float val, String valname); 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_sda = pin_sda;
_pin_scl = pin_scl; _pin_scl = pin_scl;
_dispaddr = display_addr; _dispaddr = dispaddr;
_display = new SSD1306Wire(_dispaddr, _pin_sda, _pin_scl); _display = new SSD1306Wire(_dispaddr, _pin_sda, _pin_scl);
_initDisplay();
} }
display::~display() display::~display()
{ {
} }
void display::_initDisplay() void display::init()
{ {
//initialise OLED and display Welcome Message ... //initialise OLED and display Welcome Message ...
_display->init(); _display->init();
//_display->flipScreenVertically(); _display->flipScreenVertically();
/* _display->setTextAlignment(TEXT_ALIGN_CENTER); _display->setTextAlignment(TEXT_ALIGN_CENTER);
_display->setFont(Roboto_Condensed_Bold_16); _display->setFont(Roboto_Condensed_Bold_16);
_display->clear(); _display->clear();
_display->setContrast(MAX_CONTRAST); _display->setContrast(MAX_CONTRAST);
_display->drawString(64, 32, "Itsblue.de"); _display->drawString(64, 32, "Itsblue.de");
_display->display(); _display->display();
*/
} }
void display::header(String title, bool wificonected, bool localvalues, bool lastdatasent, bool timesynced) void display::header(String title, bool wificonected, bool localvalues, bool lastdatasent, bool timesynced)

View File

@ -1,6 +1,5 @@
#ifndef __eeprom_H__ #ifndef __eeprom_H__
#define __eeprom_H__ #define __eeprom_H__
#include <TridentTD_ESP32NVS.h> #include <TridentTD_ESP32NVS.h>
class eeprom class eeprom
@ -12,17 +11,22 @@ public:
~eeprom(); ~eeprom();
void set_int(String name , uint64_t val ); void set_int(String name , uint64_t val );
uint64_t get_int(String name ); uint64_t get_int(String name );
void begin();
}; };
eeprom::eeprom() eeprom::eeprom()
{ {
NVS.begin();
} }
eeprom::~eeprom() eeprom::~eeprom()
{ {
} }
void eeprom::begin()
{
NVS.begin();
}
void eeprom::set_int(String name , uint64_t val ) void eeprom::set_int(String name , uint64_t val )
{ {
NVS.setInt(name, val); NVS.setInt(name, val);

View File

@ -53,6 +53,8 @@ class leds
bool colorWipe( uint32_t color ); bool colorWipe( uint32_t color );
void add_button_id(uint16_t mask); void add_button_id(uint16_t mask);
bool do_fade(); bool do_fade();
bool fade_off();
bool fade_on();
void setBrigthness(uint8_t brigthness); void setBrigthness(uint8_t brigthness);
void setHS(uint16_t H, uint8_t S); void setHS(uint16_t H, uint8_t S);
void setColor(uint32_t color); 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_first = pixel_first;
_pixel_nr = 1 + abs(pixel_last - pixel_first); _pixel_nr = 1 + abs(pixel_last - pixel_first);
_pixels = (_pixel *) malloc(sizeof(_pixel) * _pixel_nr); _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; _stripe = stripe;
_R = _calc_R(); _R = _calc_R();
_setLeds(0); _setLeds(0);
_rainbow_pixel = 0; _rainbow_pixel = 0;
_rainbow_color = 0; _rainbow_color = 0;
_prev_state = *_state_flag;
_fading_done = true;
} }
@ -86,7 +96,7 @@ leds::~leds()
void leds::_setLeds() 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); _stripe->setPixelColor(_pixels[nr].lednr, _pixels[nr].color);
} }
@ -95,8 +105,9 @@ void leds::_setLeds()
void leds::_setLeds(uint32_t color) 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); _stripe->setPixelColor(_pixels[nr].lednr, color);
_pixels[nr].color = color; _pixels[nr].color = color;
} }
@ -119,6 +130,7 @@ bool leds::do_fade()
if(_prev_state != curr_state) if(_prev_state != curr_state)
{ {
//Serial.printf("Current state is 0x%08x\n", curr_state);
_prev_state = curr_state; _prev_state = curr_state;
_fading_done = false; _fading_done = false;
} }
@ -129,15 +141,28 @@ bool leds::do_fade()
if(0 == curr_state) if(0 == curr_state)
{ {
_fade_off(); _fade_off();
//Serial.printf("Fading off.\n");
} }
else else
{ {
_fade_on(); _fade_on();
//Serial.printf("Fading ON\n");
} }
} }
return updated; return updated;
} }
bool leds::fade_on()
{
return _fade_on();
}
bool leds::fade_off()
{
return _fade_off();
}
float leds::_calc_R(void) float leds::_calc_R(void)
{ {
return (FADESTEPS * log10(2))/(log10(_local_brightness)); return (FADESTEPS * log10(2))/(log10(_local_brightness));
@ -232,11 +257,11 @@ bool leds::_fade_off()
} }
void leds::rainbowCycle() void leds::rainbowCycle()
{ {
if((millis() - _last_rainbowcycle) > WAIT_RAINBOWCYCLE_MS) if((millis() - _last_rainbowcycle) > WAIT_RAINBOWCYCLE_MS)
{ {
//Serial.printf("Rainbow! Next color\n");
_last_rainbowcycle = millis(); _last_rainbowcycle = millis();
_rainbow_color++; _rainbow_color++;
if(_rainbow_color >= 256*1) if(_rainbow_color >= 256*1)

View File

@ -1,24 +1,25 @@
#include <Arduino.h> #include <Arduino.h>
//#include "train.h" #include "train.h"
#include "relais.h" #include "relais.h"
#include "taster.h" #include "taster.h"
//#include "leds.h" #include "leds.h"
//#include "mp3.h" #include "mp3.h"
#include "display.h" #include "display.h"
#include <TridentTD_ESP32NVS.h>
//#include "eeprom.h" //#include "eeprom.h"
//eeprom save; //eeprom flash;
///---- OLED ---- ///---- OLED ----
const size_t PIN_MONI_SDA = 25 ; //12 const int PIN_MONI_SDA = 21 ; //12
const size_t PIN_MONI_SCL = 26 ; //14 const int PIN_MONI_SCL = 22 ; //14
const unsigned int ADDR_MONI = 0x3c; const uint8_t ADDR_MONI = 0x3c;
display moni(PIN_MONI_SDA, PIN_MONI_SCL, ADDR_MONI); display moni(ADDR_MONI, PIN_MONI_SDA, PIN_MONI_SCL);
///---- MP3 ---- ///---- MP3 ----
const size_t PIN_MP3_RX = 3 ; //D2; const size_t PIN_MP3_RX = 25 ; //D2;
const size_t PIN_MP3_TX = 1 ; //D3; const size_t PIN_MP3_TX = 26 ; //D3;
//mp3 mp3ply(PIN_MP3_RX, PIN_MP3_TX); mp3 mp3ply(PIN_MP3_RX, PIN_MP3_TX);
///---- RELAIS ---- ///---- 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_STERNE = 17;
const size_t PIN_RELAIS_SPIEGELBALL = 16; const size_t PIN_RELAIS_SPIEGELBALL = 16;
const size_t PIN_RELAIS_OTHER = 4; 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; const size_t PIN_TRAIN_OBEN = 33;
#define PWM_CHANNEL_OBEN 5 #define PWM_CHANNEL_OBEN 5
#define PWM_CHANNEL_UNTEN 6 #define PWM_CHANNEL_UNTEN 6
//train zugunten(PIN_TRAIN_UNTEN, PWM_CHANNEL_UNTEN ); train zugunten(PIN_TRAIN_UNTEN, PWM_CHANNEL_UNTEN );
//train zugoben(PIN_TRAIN_OBEN, PWM_CHANNEL_OBEN ); train zugoben(PIN_TRAIN_OBEN, PWM_CHANNEL_OBEN );
///--- Taster ---- ///--- Taster ----
const size_t PIN_TASTER_AUSSEN_LICHT = 36; const size_t PIN_TASTER_AUSSEN_LICHT = 36;
const size_t PIN_TASTER_AUSSEN_MOVE = 39; const size_t PIN_TASTER_AUSSEN_MOVE = 39;
const size_t PIN_TASTER_TRAIN_UNTEN = 34; const size_t PIN_TASTER_TRAIN_UNTEN = 34;
const size_t PIN_TASTER_TRAIN_OBEN = 35; 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_licht(PIN_TASTER_AUSSEN_LICHT);
taster taster_aussen_move(PIN_TASTER_AUSSEN_MOVE); taster taster_aussen_move(PIN_TASTER_AUSSEN_MOVE);
taster taster_train_unten(PIN_TASTER_TRAIN_UNTEN); taster taster_train_unten(PIN_TASTER_TRAIN_UNTEN);
@ -45,9 +92,10 @@ taster taster_train_oben(PIN_TASTER_TRAIN_OBEN);
///--- RGB LEDs --- ///--- RGB LEDs ---
uint16_t rgbled_state_flag = 0; uint16_t rgbled_state_flag = 0;
const size_t PIN_RGBLEDS = 19; const size_t PIN_RGBLEDS = 19;
#define NUMRGBLEDS 20 #define NUMRGBLEDS 25
//Adafruit_NeoPixel rgb_leds(NUMRGBLEDS, PIN_RGBLEDS, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel rgb_leds(NUMRGBLEDS, PIN_RGBLEDS, NEO_GRB + NEO_KHZ800);
enum led_t {LTANNE=1, LBACK=2, LSTERNE=4, LTEICH=8}; 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_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_teich(&rgb_leds, &rgbled_state_flag, LTEICH, 5000,220, 6, 10 );
leds led_sterne(&rgb_leds, &rgbled_state_flag, LSTERNE, 5000,220, 11, 24 );

View File

@ -18,6 +18,9 @@ public:
void stop(); void stop();
void play(size_t nr); void play(size_t nr);
unsigned long is_playing(); 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) 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; _rx_pin = rx_pin;
_tx_pin = tx_pin; _tx_pin = tx_pin;
player = new SerialMP3Player(_rx_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() 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) 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(); _play_since_ms = millis();
} }

View File

@ -6,35 +6,41 @@
class taster class taster
{ {
private: private:
volatile const unsigned long _BOUNCING_TIME_MS = 200; volatile const unsigned long _BOUNCING_TIME_MS = 300;
volatile unsigned long _last_pressed; volatile unsigned long _last_pressed;
size_t _taster_pin; unsigned long _number_pressed;
volatile unsigned long _number_pressed;
unsigned long _number_checked; unsigned long _number_checked;
size_t _taster_pin;
//unsigned long * _ptr_number_pressed;
void IRAM_ATTR _taster_int() 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; _number_pressed += 1;
} }
} }
public: public:
taster(size_t pin); //taster(const size_t pin, void (*isrfunction)(), unsigned long* ptr_number_pressed);
taster(const size_t pin);
~taster(); ~taster();
bool pressed(); bool pressed();
}; };
//taster::taster(size_t pin, void (*isrfunction)(), unsigned long* ptr_number_pressed)
taster::taster(size_t pin) taster::taster(size_t pin)
{ {
_taster_pin = pin; _taster_pin = pin;
_number_pressed = 0; //_ptr_number_pressed = ptr_number_pressed;
_number_checked = 0; _number_checked = 0;
_number_pressed = 0;
_last_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, std::bind(&taster::_taster_int,this), FALLING);
//attachInterrupt(_taster_pin, isrfunction, FALLING);
} }
taster::~taster() taster::~taster()

View File

@ -18,7 +18,6 @@ private:
unsigned long _last_faded_ms; unsigned long _last_faded_ms;
unsigned long _delay_ms; unsigned long _delay_ms;
void _set_pwm(uint8_t pwm_value);
bool _fade(bool fade_up); bool _fade(bool fade_up);
public: public:
@ -27,6 +26,7 @@ public:
void stop(); void stop();
bool fade_on(); bool fade_on();
bool fade_off(); 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); ledcWrite(_pwmchannel, pwm_value);
} }
void train::stop() { void train::stop() {
_set_pwm(0); set_pwm(0);
} }
bool train::_fade(bool fade_up) bool train::_fade(bool fade_up)

View File

@ -16,5 +16,5 @@ monitor_speed = 115200
lib_deps = lib_deps =
salvadorrueda/SerialMP3Player@^1.1.0 salvadorrueda/SerialMP3Player@^1.1.0
plerup/EspSoftwareSerial@^6.14.1 plerup/EspSoftwareSerial@^6.14.1
thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.1
tridenttd/TridentTD_ESP32NVS@^1.0 tridenttd/TridentTD_ESP32NVS@^1.0
thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.1

View File

@ -4,44 +4,64 @@
void show_counters(uint64_t counter_licht, uint64_t counter_move); void show_counters(uint64_t counter_licht, uint64_t counter_move);
//variables //variables
uint64_t licht_all_count = 0; uint16_t licht_all_count = 0;
uint64_t move_all_count = 0; uint16_t move_all_count = 0;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
rgb_leds.begin();
NVS.begin();
moni.init();
//load counters //load counters
// save.set_int("licht", 0); //NVS.setInt("licht", 100);
// save.set_int("move", 0); //NVS.setInt("move", 50);
// licht_all_count = save.get_int("licht"); licht_all_count = NVS.getInt("licht");
// move_all_count = save.get_int("move"); move_all_count = NVS.getInt("move");
// all relais off; // all relais off;
relais_haeuser.off(); relais_haeuser.off();
relais_sterne.off(); relais_sterne.off();
relais_spiegel.off(); relais_spiegel.off();
relais_other.off(); relais_other.off();
/*
zugoben.stop(); zugoben.stop();
zugunten.stop(); zugunten.stop();
led_tanne.setColor(0x0f0); led_tanne.setColor(rgb_leds.Color(0, 150, 0));
mp3ply.play(1); rgbled_state_flag = rgbled_state_flag | LSTERNE;
mp3ply.begin();
*/ mp3ply.stop();
} }
uint8_t licht_count = 0; uint8_t licht_count = 0;
bool faded_on = false;
void loop() { void loop() {
if(faded_on == false)
//led_teich.rainbowCycle(); {
//rgb_leds.show(); 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()) if(taster_aussen_licht.pressed())
{
relais_haeuser.toggle(); relais_haeuser.toggle();
licht_all_count++;
NVS.setInt("licht", licht_all_count);
}
if(taster_aussen_move.pressed()) if(taster_aussen_move.pressed())
{
relais_other.toggle(); relais_other.toggle();
move_all_count++;
NVS.setInt("move", move_all_count);
}
if(taster_train_oben.pressed()) if(taster_train_oben.pressed())
relais_sterne.toggle(); relais_sterne.toggle();
@ -49,9 +69,9 @@ void loop() {
if(taster_train_unten.pressed()) if(taster_train_unten.pressed())
relais_spiegel.toggle(); relais_spiegel.toggle();
/*
show_counters(licht_all_count, move_all_count);
show_counters(licht_all_count, move_all_count);
/*
switch (licht_count) switch (licht_count)
{ {
case 1: case 1:
@ -90,13 +110,12 @@ void loop() {
} }
/*
void show_counters(uint64_t counter_licht, uint64_t counter_move) void show_counters(uint64_t counter_licht, uint64_t counter_move)
{ {
moni.clear(); moni.clear();
moni.header("Zaehler"); moni.header("Zaehler");
moni.data(10,15,counter_licht, "Licht:"); moni.data(30,20,counter_licht, "Licht");
moni.data(10,15,counter_move, "Moves:"); moni.data(100,20,counter_move, "Moves");
moni.show(); moni.show();
} }
*/