working, but need to write test

This commit is contained in:
Simon Schmidt 2021-06-25 18:35:16 +02:00
parent 55ebd21975
commit d059c3d72d
5 changed files with 142 additions and 79 deletions

View File

@ -1,4 +1,4 @@
#include "pwm.h" #include "treppe.h"
uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){ uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){
/* /*
@ -94,46 +94,49 @@ void Treppe::ledsequence(){
void Treppe::elelel() void Treppe::elelel()
{ {
if(state) { if(state) {
if(direction) tick++; if(direction) { // aufwärts
else tick--; if(tick >= ticks_treppe-1) { // ziel erreicht
Serial.println("[Treppe] oberster tick !");
// was soll gemacht werden state = 0;
pwmController.setChannelPWM(stufe, return;
(uint16_t) (differenz_pwm_pro_tick * (tick % ticks_pro_stufe) + idle_brightness) }
); tick++; // eins hoch
Serial.printf("tick %04d, led %02d:%02d, pwm %4.1f\n", }
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,
tick / ticks_pro_stufe, stufe,
(tick % ticks_pro_stufe) , (tick - ticks_pro_stufe*stufe),
(differenz_pwm_pro_tick * (tick % ticks_pro_stufe) + idle_brightness) 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) { // if(stufe > stairs || stufe < 0 || tick < 0 || tick > ticks_treppe-1) {
// Serial.println("[Treppe] ERROR, Something went wrong !"); // Serial.println("[Treppe] ERROR, Something went wrong !");
// state = 0; // state = 0;
// return; // return;
// } // }
void Treppe::setup(){ void Treppe::setup(){
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick); Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
@ -153,6 +156,7 @@ void Treppe::setup(){
pwmController.setChannelPWM(i, idle_brightness); pwmController.setChannelPWM(i, idle_brightness);
} }
} }
void Treppe::task(){ void Treppe::task(){
//ledsequence(); //ledsequence();
elelel(); elelel();
@ -176,7 +180,7 @@ uint16_t Treppe::setTime(uint16_t _time_per_stair){
uint8_t Treppe::setDirection(uint8_t _direction){ uint8_t Treppe::setDirection(uint8_t _direction){
direction = _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 // to do: implement state command variable to determine dimm-state
return direction; return direction;
} }
@ -185,7 +189,7 @@ uint8_t Treppe::setState(uint8_t _state){
if(state == _state) return 1; if(state == _state) return 1;
else{ else{
state = _state; state = _state;
Serial.println("Treppe: State changed!"); Serial.printf("Treppe: State: %d!\n",state);
} }
return 0; return 0;
} }

View File

@ -5,8 +5,8 @@
class Treppe { class Treppe {
private: private:
uint8_t stairs; uint8_t stairs;
uint16_t time_per_stair = 100; // dimmtime per stair [ms] uint16_t time_per_stair = 300; // dimmtime per stair [ms]
uint16_t idle_brightness = 25; uint16_t idle_brightness = 0;
uint16_t active_brightness = 500; uint16_t active_brightness = 500;
uint8_t direction = 0; uint8_t direction = 0;
@ -30,10 +30,10 @@ public:
ticks_pro_stufe = time_per_stair / 20; // [ms] ticks_pro_stufe = time_per_stair / 20; // [ms]
ticks_treppe = ticks_pro_stufe * stairs; ticks_treppe = ticks_pro_stufe * stairs;
differenz_pwm_pro_tick = (active_brightness - idle_brightness) differenz_pwm_pro_tick = (float) (active_brightness - idle_brightness)
/ ticks_pro_stufe; / (float) ticks_pro_stufe;
} }
void task(); // call periodically void task(); // call periodically
void setup(); void setup();
@ -44,6 +44,14 @@ public:
uint16_t setActive(uint16_t _active_brightness); uint16_t setActive(uint16_t _active_brightness);
uint16_t setTime(uint16_t _time_per_stair); 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 // Runtime Parameter section
uint8_t setDirection(uint8_t _direction); uint8_t setDirection(uint8_t _direction);
uint8_t setState(uint8_t _state); uint8_t setState(uint8_t _state);

View File

@ -18,7 +18,7 @@ extern "C" {
#define NODEMCU_LED 16 #define NODEMCU_LED 16
// PWM // PWM
#include "pwm.h" #include "treppe.h"
os_timer_t timer1; os_timer_t timer1;
uint8_t timer_flag = 0; uint8_t timer_flag = 0;
Treppe stairs(16); Treppe stairs(16);
@ -102,25 +102,28 @@ void setup() {
Serial.println("PCA9685 connected !"); Serial.println("PCA9685 connected !");
os_timer_setfn(&timer1, timerCallback, &timer_flag); os_timer_setfn(&timer1, timerCallback, &timer_flag);
os_timer_arm(&timer1, 200, true); os_timer_arm(&timer1, 20, true);
stairs.setState(1); stairs.setState(1);
stairs.setDirection(1); stairs.setDirection(1);
} }
#include <random>
void loop() { void loop() {
if(stairs.getState() == 0) { if(stairs.getState() == 0) {
delay(1000); 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.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(ArduinoOTA.handle(), 10000, "OTA");
TIMEIF_US(httpServer.handleClient(), 10000, "HTTP"); TIMEIF_US(httpServer.handleClient(), 10000, "HTTP");
} }

View File

@ -1,40 +1,40 @@
#include <Arduino.h> // #include <Arduino.h>
#include <unity.h> // #include <unity.h>
String STR_TO_TEST; // String STR_TO_TEST;
void setUp(void) { // // void setUp(void) {
// set stuff up here // // // set stuff up here
STR_TO_TEST = "Hello, world!"; // // STR_TO_TEST = "Hello, world!";
} // // }
void tearDown(void) { // // void tearDown(void) {
// clean stuff up here // // // clean stuff up here
STR_TO_TEST = ""; // // STR_TO_TEST = "";
} // // }
void test_led_builtin_pin_number(void) { // void test_led_builtin_pin_number(void) {
TEST_ASSERT_EQUAL(2, LED_BUILTIN); // TEST_ASSERT_EQUAL(2, LED_BUILTIN);
} // }
void test_string_concat(void) { // void test_string_concat(void) {
String hello = "Hello, "; // String hello = "Hello, ";
String world = "world!"; // String world = "world!";
TEST_ASSERT_EQUAL_STRING(STR_TO_TEST.c_str(), (hello + world).c_str()); // TEST_ASSERT_EQUAL_STRING(STR_TO_TEST.c_str(), (hello + world).c_str());
} // }
void setup() // void setup()
{ // {
delay(2000); // service delay // delay(2000); // service delay
UNITY_BEGIN(); // UNITY_BEGIN();
RUN_TEST(test_led_builtin_pin_number); // RUN_TEST(test_led_builtin_pin_number);
RUN_TEST(test_string_concat); // RUN_TEST(test_string_concat);
UNITY_END(); // stop unit testing // UNITY_END(); // stop unit testing
} // }
void loop() // void loop()
{ // {
} // }

48
test/test_pwm.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <Arduino.h>
#include "treppe.h"
#include <unity.h>
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()
{
}