ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

treppe.h 3.5KB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef __TREPPE_H
  2. #define __TREPPE_H
  3. #include "FSMTreppe4/FSMTreppe4.h"
  4. #include "PCA9685.h"
  5. #include <EEPROM.h>
  6. // #define LDR_REGELUNG // comment in to activate LDR control
  7. // #define LDRDEBUG // comment in to show LDR measurement
  8. #define LDR_HYS 100 // Hysteresis for switching off FSM [lux]
  9. #define LDR_AVERAGE_SAMPLES 50
  10. #define SENSOR_OBEN 16
  11. #define SENSOR_UNTEN 12
  12. #define OE 14
  13. #define INT_TIME 20 // interrupt intervall [ms]
  14. #define TIME_MS_MAX 5000 // maximum time for animation [ms]
  15. enum vorgabe_typ_t { VORGABE_PROZENT = 0, VORGABE_12BIT = 1 };
  16. #define EEP_START_ADDR 100 // define Start Address for "EEPROM" storage -> depends on size of "wifi_data"!
  17. class Treppe {
  18. private:
  19. const uint8_t stufen;
  20. struct stairway_param_t {
  21. uint16_t time_ldr = 500;
  22. uint16_t time_per_stair = 300; // dimmtime per stair [ms]
  23. uint16_t idle_pwm_max = 100;
  24. uint16_t active_pwm = 2000;
  25. uint16_t ldr_schwelle = 30; // activation value for FSM [lx]
  26. };
  27. stairway_param_t param;
  28. stairway_param_t param_pend; // zwischenspeicher änderungen
  29. bool param_changed = false;
  30. float ldr_average = 0.0;
  31. uint16_t ldr_average_cnt = 0;
  32. uint16_t idle_pwm_ist = 0;
  33. uint16_t idle_pwm_soll = 0;
  34. struct fsm_pending_inputs_t {
  35. bool anim_beendet = true;
  36. bool sensor_unten = false;
  37. bool last_s_unten = false;
  38. bool web_ctrl_s_unten = false;
  39. bool sensor_oben = false;
  40. bool last_s_oben = false;
  41. bool web_ctrl_s_oben = false;
  42. bool ldr_changed = false;
  43. };
  44. fsm_pending_inputs_t fsm_pend;
  45. struct dimmer_t {
  46. uint8_t stufe = 0;
  47. uint16_t ticks = 0;
  48. uint16_t tick = 0;
  49. float delta_pwm = 0.0;
  50. float pwm = 0.0;
  51. uint16_t start_pwm = 0;
  52. uint16_t ziel_pwm = 0;
  53. };
  54. enum dimmer_type_t { DIM_STUFEN = 0, DIM_LDR = 1 };
  55. dimmer_t dimmer_stufen;
  56. dimmer_t dimmer_ldr;
  57. // initialize with i2c-Address 0, use Wire Library
  58. PCA9685 pwmController;
  59. FSMTreppeModelClass FSMTreppe_Obj;
  60. FSMTreppeModelClass::ExtU_FSMTreppe_T fsm_inputs;
  61. FSMTreppeModelClass::ExtY_FSMTreppe_T fsm_outputs;
  62. enum fsm_status_t {
  63. ST_INAKTIV_LDR = 0,
  64. ST_AUFDIMMEN_LDR = 1,
  65. ST_ABDIMMEN_LDR = 2,
  66. ST_RUHEZUSTAND = 3,
  67. ST_AUFDIMMEN_HOCH = 4,
  68. ST_WARTEN_HOCH = 5,
  69. ST_ABDIMMEN_HOCH = 6,
  70. ST_AUFDIMMEN_RUNTER = 7,
  71. ST_WARTEN_RUNTER = 8,
  72. ST_ABDIMMEN_RUNTER = 9,
  73. ST_LDR_CHANGED = 10
  74. };
  75. enum fsm_laufrichtung_t { LR_RUNTER = 0, LR_HOCH = 1 };
  76. enum fsm_dimmrichtung_t { DR_ABDIMMEN = 0, DR_AUFDIMMEN = 1 };
  77. void read_sensors();
  78. void print_state_on_change();
  79. /* DIMM */
  80. bool dimmer_tick(dimmer_t *dimmer, bool dim_type);
  81. void start_animation(dimmer_t *dimmer, bool dim_type, uint16_t on_pwm,
  82. uint16_t off_pwm);
  83. /* LDR */
  84. bool read_sensor(int sensor);
  85. float read_ldr();
  86. void sample_ldr();
  87. bool check_ldr(float ldr_avg);
  88. void update_soll_pwm_with_ldr(float ldr_avg);
  89. public:
  90. Treppe(uint8_t _stufen) : stufen(_stufen) { FSMTreppe_Obj.initialize(); }
  91. ~Treppe() { FSMTreppe_Obj.terminate(); }
  92. void setup();
  93. void task(); // call periodically
  94. // Parameter section
  95. void save_param_to_eeprom();
  96. void param_to_json(char* json_str, size_t sz);
  97. void set_idle_pwm_max(const uint16_t value, const vorgabe_typ_t vorgabe_typ);
  98. void set_active_pwm(const uint16_t value, const vorgabe_typ_t vorgabe_typ);
  99. void set_time_ldr(const uint16_t value);
  100. void set_time_per_stair(const uint16_t value);
  101. void set_ldr_schwelle(const uint16_t value, const vorgabe_typ_t vorgabe_typ);
  102. void overwrite_sensors(bool s_oben, bool s_unten);
  103. };
  104. #endif // __TREPPE_H