From d059c3d72d1ffaf67ce482171fdbe0cf6c0f86dd Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Fri, 25 Jun 2021 18:35:16 +0200 Subject: [PATCH] working, but need to write test --- src/pwm.cpp => lib/treppe/treppe.cpp | 76 +++++++++++++++------------- include/pwm.h => lib/treppe/treppe.h | 18 +++++-- src/main.cpp | 19 ++++--- test/test_main.cpp | 60 +++++++++++----------- test/test_pwm.cpp | 48 ++++++++++++++++++ 5 files changed, 142 insertions(+), 79 deletions(-) rename src/pwm.cpp => lib/treppe/treppe.cpp (80%) rename include/pwm.h => lib/treppe/treppe.h (73%) create mode 100644 test/test_pwm.cpp diff --git a/src/pwm.cpp b/lib/treppe/treppe.cpp similarity index 80% rename from src/pwm.cpp rename to lib/treppe/treppe.cpp index 0b6fcf0..2fb6612 100644 --- a/src/pwm.cpp +++ b/lib/treppe/treppe.cpp @@ -1,4 +1,4 @@ -#include "pwm.h" +#include "treppe.h" uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){ /* @@ -94,46 +94,49 @@ void Treppe::ledsequence(){ + + void Treppe::elelel() { if(state) { - if(direction) tick++; - else tick--; - - // was soll gemacht werden - pwmController.setChannelPWM(stufe, - (uint16_t) (differenz_pwm_pro_tick * (tick % ticks_pro_stufe) + idle_brightness) - ); - Serial.printf("tick %04d, led %02d:%02d, pwm %4.1f\n", + if(direction) { // aufwärts + if(tick >= ticks_treppe-1) { // ziel erreicht + Serial.println("[Treppe] oberster tick !"); + state = 0; + return; + } + tick++; // eins hoch + } + else { // abwärts + if(tick <= 0) { // ziel erreicht + Serial.println("[Treppe] unterster tick !"); + state = 0; + return; + } + tick--; // eins runter + } + + stufe = tick / ticks_pro_stufe; + float new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe); + if(direction) + new_pwm += differenz_pwm_pro_tick; + new_pwm += idle_brightness; + + pwmController.setChannelPWM(stufe, (uint16_t) new_pwm); + Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n", tick, - tick / ticks_pro_stufe, - (tick % ticks_pro_stufe) , - (differenz_pwm_pro_tick * (tick % ticks_pro_stufe) + idle_brightness) + stufe, + (tick - ticks_pro_stufe*stufe), + new_pwm ); - - if(tick < 0) { // ziel erreicht - tick = 0; - state = 0; - return; - } - if(tick == ticks_treppe) { - tick = ticks_treppe-1; - state = 0; - return; - } - - if(tick % ticks_pro_stufe == 0) { - if(direction) stufe++; - else stufe--; - } } } - // if(stufe > stairs || stufe < 0 || tick < 0 || tick > ticks_treppe-1) { - // Serial.println("[Treppe] ERROR, Something went wrong !"); - // state = 0; - // return; - // } +// if(stufe > stairs || stufe < 0 || tick < 0 || tick > ticks_treppe-1) { +// Serial.println("[Treppe] ERROR, Something went wrong !"); +// state = 0; +// return; +// } void Treppe::setup(){ Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick); @@ -153,6 +156,7 @@ void Treppe::setup(){ pwmController.setChannelPWM(i, idle_brightness); } } + void Treppe::task(){ //ledsequence(); elelel(); @@ -176,7 +180,7 @@ uint16_t Treppe::setTime(uint16_t _time_per_stair){ uint8_t Treppe::setDirection(uint8_t _direction){ direction = _direction; - Serial.println("Treppe: Direction changed!"); + Serial.printf("Treppe: Direction: %d!\n",direction); // to do: implement state command variable to determine dimm-state return direction; } @@ -185,7 +189,7 @@ uint8_t Treppe::setState(uint8_t _state){ if(state == _state) return 1; else{ state = _state; - Serial.println("Treppe: State changed!"); + Serial.printf("Treppe: State: %d!\n",state); } return 0; -} \ No newline at end of file +} diff --git a/include/pwm.h b/lib/treppe/treppe.h similarity index 73% rename from include/pwm.h rename to lib/treppe/treppe.h index 026947b..1246728 100644 --- a/include/pwm.h +++ b/lib/treppe/treppe.h @@ -5,8 +5,8 @@ class Treppe { private: uint8_t stairs; - uint16_t time_per_stair = 100; // dimmtime per stair [ms] - uint16_t idle_brightness = 25; + uint16_t time_per_stair = 300; // dimmtime per stair [ms] + uint16_t idle_brightness = 0; uint16_t active_brightness = 500; uint8_t direction = 0; @@ -30,10 +30,10 @@ public: ticks_pro_stufe = time_per_stair / 20; // [ms] ticks_treppe = ticks_pro_stufe * stairs; - differenz_pwm_pro_tick = (active_brightness - idle_brightness) - / ticks_pro_stufe; - + differenz_pwm_pro_tick = (float) (active_brightness - idle_brightness) + / (float) ticks_pro_stufe; } + void task(); // call periodically void setup(); @@ -44,6 +44,14 @@ public: uint16_t setActive(uint16_t _active_brightness); uint16_t setTime(uint16_t _time_per_stair); + void setTick(uint32_t _tick) { + tick = _tick; + Serial.printf("Treppe: Tick: %u!\n", tick); + } + uint32_t getTicks() { + return ticks_treppe; + } + // Runtime Parameter section uint8_t setDirection(uint8_t _direction); uint8_t setState(uint8_t _state); diff --git a/src/main.cpp b/src/main.cpp index aa439a3..dd8c09e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ extern "C" { #define NODEMCU_LED 16 // PWM -#include "pwm.h" +#include "treppe.h" os_timer_t timer1; uint8_t timer_flag = 0; Treppe stairs(16); @@ -102,25 +102,28 @@ void setup() { Serial.println("PCA9685 connected !"); os_timer_setfn(&timer1, timerCallback, &timer_flag); - os_timer_arm(&timer1, 200, true); + os_timer_arm(&timer1, 20, true); stairs.setState(1); stairs.setDirection(1); } +#include void loop() { if(stairs.getState() == 0) { delay(1000); - stairs.setState(1); + + // uint32_t t = rand() % stairs.getTicks(); + // uint32_t d = rand() % 2; + + // stairs.setTick(t); + // stairs.setDirection(d); + stairs.setDirection(!stairs.getDirection()); + stairs.setState(1); } - // if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0); - // if(millis() > 35000 && stairs.getDirection() == 1){ - // stairs.setState(1); - // stairs.setDirection(0); - // } TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA"); TIMEIF_US(httpServer.handleClient(), 10000, "HTTP"); } \ No newline at end of file diff --git a/test/test_main.cpp b/test/test_main.cpp index 1c44b44..1cad7b5 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -1,40 +1,40 @@ -#include -#include +// #include +// #include -String STR_TO_TEST; +// String STR_TO_TEST; -void setUp(void) { - // set stuff up here - STR_TO_TEST = "Hello, world!"; -} +// // void setUp(void) { +// // // set stuff up here +// // STR_TO_TEST = "Hello, world!"; +// // } -void tearDown(void) { - // clean stuff up here - STR_TO_TEST = ""; -} +// // void tearDown(void) { +// // // clean stuff up here +// // STR_TO_TEST = ""; +// // } -void test_led_builtin_pin_number(void) { - TEST_ASSERT_EQUAL(2, LED_BUILTIN); -} +// void test_led_builtin_pin_number(void) { +// TEST_ASSERT_EQUAL(2, LED_BUILTIN); +// } -void test_string_concat(void) { - String hello = "Hello, "; - String world = "world!"; - TEST_ASSERT_EQUAL_STRING(STR_TO_TEST.c_str(), (hello + world).c_str()); -} +// void test_string_concat(void) { +// String hello = "Hello, "; +// String world = "world!"; +// TEST_ASSERT_EQUAL_STRING(STR_TO_TEST.c_str(), (hello + world).c_str()); +// } -void setup() -{ - delay(2000); // service delay - UNITY_BEGIN(); +// void setup() +// { +// delay(2000); // service delay +// UNITY_BEGIN(); - RUN_TEST(test_led_builtin_pin_number); - RUN_TEST(test_string_concat); +// RUN_TEST(test_led_builtin_pin_number); +// RUN_TEST(test_string_concat); - UNITY_END(); // stop unit testing -} +// UNITY_END(); // stop unit testing +// } -void loop() -{ -} \ No newline at end of file +// void loop() +// { +// } \ No newline at end of file diff --git a/test/test_pwm.cpp b/test/test_pwm.cpp new file mode 100644 index 0000000..e03383d --- /dev/null +++ b/test/test_pwm.cpp @@ -0,0 +1,48 @@ +#include +#include "treppe.h" + +#include + +Treppe stairs(10); + +void setUp(void) { + // run before each test + // set stuff up here + stairs.setup(); + Serial.println("Treppe initialized !"); + Serial.println("PCA9685 connected !"); +} + +void tearDown(void) { + // clean stuff up here +} + +void test_set_state(void) { + stairs.setState(1); + TEST_ASSERT_EQUAL(1, stairs.getState()); + stairs.setState(0); + TEST_ASSERT_EQUAL(0, stairs.getState()); +} + + +void test_set_direction(void) { + stairs.setDirection(1); + TEST_ASSERT_EQUAL(1, stairs.getDirection()); + stairs.setDirection(0); + TEST_ASSERT_EQUAL(0, stairs.getDirection()); +} + +void setup() +{ + delay(2000); // service delay + UNITY_BEGIN(); + + RUN_TEST(test_set_state); + RUN_TEST(test_set_direction); + + UNITY_END(); // stop unit testing +} + +void loop() +{ +} \ No newline at end of file