Browse Source

working, but need to write test

tags/FSM1.0
Simon Schmidt 2 years ago
parent
commit
d059c3d72d
5 changed files with 141 additions and 78 deletions
  1. 39
    35
      lib/treppe/treppe.cpp
  2. 13
    5
      lib/treppe/treppe.h
  3. 11
    8
      src/main.cpp
  4. 30
    30
      test/test_main.cpp
  5. 48
    0
      test/test_pwm.cpp

src/pwm.cpp → lib/treppe/treppe.cpp View File

#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){
/* /*








void Treppe::elelel() void Treppe::elelel()
{ {
if(state) { 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",
tick,
tick / ticks_pro_stufe,
(tick % ticks_pro_stufe) ,
(differenz_pwm_pro_tick * (tick % ticks_pro_stufe) + idle_brightness)
);

if(tick < 0) { // ziel erreicht
tick = 0;
state = 0;
return;
if(direction) { // aufwärts
if(tick >= ticks_treppe-1) { // ziel erreicht
Serial.println("[Treppe] oberster tick !");
state = 0;
return;
}
tick++; // eins hoch
} }
if(tick == ticks_treppe) {
tick = ticks_treppe-1;
state = 0;
return;
else { // abwärts
if(tick <= 0) { // ziel erreicht
Serial.println("[Treppe] unterster tick !");
state = 0;
return;
}
tick--; // eins runter
} }


if(tick % ticks_pro_stufe == 0) {
if(direction) stufe++;
else stufe--;
}
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,
stufe,
(tick - ticks_pro_stufe*stufe),
new_pwm
);
} }
} }


// 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(){ 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);
pwmController.setChannelPWM(i, idle_brightness); pwmController.setChannelPWM(i, idle_brightness);
} }
} }

void Treppe::task(){ void Treppe::task(){
//ledsequence(); //ledsequence();
elelel(); elelel();


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;
} }
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;
}
}

include/pwm.h → lib/treppe/treppe.h View File

class Treppe { class Treppe {
private: private:
uint8_t stairs; 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; uint16_t active_brightness = 500;


uint8_t direction = 0; uint8_t direction = 0;
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)
/ ticks_pro_stufe;

differenz_pwm_pro_tick = (float) (active_brightness - idle_brightness)
/ (float) ticks_pro_stufe;
} }

void task(); // call periodically void task(); // call periodically


void setup(); void setup();
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);

+ 11
- 8
src/main.cpp View File

#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);
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");
} }

+ 30
- 30
test/test_main.cpp View File

#include <Arduino.h>
#include <unity.h>
// #include <Arduino.h>
// #include <unity.h>


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()
{
}
// void loop()
// {
// }

+ 48
- 0
test/test_pwm.cpp View File

#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()
{
}

Loading…
Cancel
Save