Compare commits

...

5 Commits

Author SHA1 Message Date
69e1e869a5 old pwm 2021-06-25 19:14:30 +02:00
1a284080e8 typo 2021-06-25 19:13:04 +02:00
3490e7ba44 Merge branch 'master' into test 2021-06-25 19:11:58 +02:00
d059c3d72d working, but need to write test 2021-06-25 18:35:16 +02:00
55ebd21975 test 2021-06-25 04:53:06 +02:00
6 changed files with 354 additions and 34 deletions

@ -72,3 +72,6 @@ LED Flickering issue
https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
PlatformIO Library veraltet
###TIMER in OTA unterbrechen !!!!!

193
lib/treppe/treppe.cpp Normal file

@ -0,0 +1,193 @@
#include "treppe.h"
uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){
/*
softstart task
- get's called at regular intervals (1ms at the moment)
- dimms single led (0 - 15, PCA9685 outputs) with linear intervals vom startval to stopval
- calculates pwm steps depending on startval, stopval and timeinterval
- -> results in constanst speed
- returns 1 if led dimming is running
- returns 0 if led dimming is finished
*/
static uint8_t lastled = 255;
static float current_pwm = 0;
static float stepsize = 0.5;
if(led != lastled){
pwmController.setChannelPWM(led, (uint16_t)startval);
lastled = led;
current_pwm = startval;
stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
return 1;
}
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize) return 0;
// todo: duty cycle zero!
if(startval > stopval){
current_pwm -= stepsize;
}
else {
current_pwm += stepsize;
}
Serial.println((uint16_t)current_pwm);
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
return 1;
}
void Treppe::ledsequence(){
static int8_t led = 0;
static uint16_t brightness = 0;
static uint16_t lastbrightness = 0;
static uint8_t finish = 1;
static uint16_t status = 0;
uint16_t status_build = 0;
status_build |= direction << 8;
status_build |= state;
if(status_build != status){ // check if any parameter changed
finish = 0; // set state unfinished -> start action
if(direction) led = 0; // reset led counter depending of direction
else led = stairs-1;
if(state){
brightness = active_brightness; // set brightness value depending of on/off
lastbrightness = idle_brightness;
}
else{
brightness = idle_brightness;
lastbrightness = active_brightness;
}
status = status_build; // set parameter memory
Serial.print("----Status Changed! onoff: ");
Serial.print(state);
Serial.print(" dir: ");
Serial.println(direction);
}
if(!finish){ // finish == 0 -> action pending
if(!softstart_led(led,lastbrightness, brightness)){
Serial.print("one LED finished: ");
Serial.print(led);
Serial.print(" last: ");
Serial.print(lastbrightness);
Serial.print(" curr: ");
Serial.println(brightness);
if(direction){
led++;
if(led >= stairs) {
finish = 1;
//lastbrightness = brightness;
}
}
else{
led--;
if(led < 0){
//lastbrightness = brightness;
finish = 1;
}
}
}
}
}
void Treppe::task_2()
{
if(state) {
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,
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;
// }
void Treppe::setup(){
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
pwmController.resetDevices();
// Deactivate PCA9685_PhaseBalancer due to LED Flickering
// https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
// see also lib/PCA9685-Arduin/PCA9685.h:204
pwmController.init(PCA9685_PhaseBalancer_None);
pwmController.setPWMFrequency(200);
Serial.println("Hello from Treppe");
Serial.print("Treppe: initial parameters: stairs=");
Serial.println(stairs);
for(uint8_t i=0; i<stairs; i++) {
pwmController.setChannelPWM(i, idle_brightness);
}
}
void Treppe::task(){
//ledsequence();
task_2();
}
uint16_t Treppe::setIdle(uint16_t _idle_brightness){
idle_brightness = _idle_brightness;
Serial.println("Treppe: idle brightness changed!");
return idle_brightness;
}
uint16_t Treppe::setActive(uint16_t _active_brightness){
active_brightness = _active_brightness;
Serial.println("Treppe: active brightness changed!");
return active_brightness;
}
uint16_t Treppe::setTime(uint16_t _time_per_stair){
time_per_stair = _time_per_stair;
Serial.println("Treppe: time changed!");
return time_per_stair;
}
uint8_t Treppe::setDirection(uint8_t _direction){
direction = _direction;
Serial.printf("Treppe: Direction: %d!\n",direction);
// to do: implement state command variable to determine dimm-state
return direction;
}
uint8_t Treppe::setState(uint8_t _state){
if(state == _state) return 1;
else{
state = _state;
Serial.printf("Treppe: State: %d!\n",state);
}
return 0;
}

61
lib/treppe/treppe.h Normal file

@ -0,0 +1,61 @@
#pragma once
#include "PCA9685.h"
class Treppe {
private:
uint8_t stairs;
uint16_t time_per_stair = 300; // dimmtime per stair [ms]
uint16_t idle_brightness = 0;
uint16_t active_brightness = 500;
uint8_t direction = 0;
uint8_t state = 0;
uint8_t switch_state = 0;
uint32_t tick = 0;
uint32_t stufe = 0;
uint32_t ticks_treppe = 0;
uint32_t ticks_pro_stufe = 0;
float differenz_pwm_pro_tick = 0.0;
// initialize with i2c-Address 0, use Wire Library
PCA9685 pwmController;
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);
void ledsequence();
public:
Treppe(uint8_t _stairs) : stairs(_stairs){
ticks_pro_stufe = time_per_stair / 20; // [ms]
ticks_treppe = ticks_pro_stufe * stairs;
differenz_pwm_pro_tick = (float) (active_brightness - idle_brightness)
/ (float) ticks_pro_stufe;
}
void task(); // call periodically
void setup();
void task_2();
// Parameter section
uint16_t setIdle(uint16_t _idle_brightness);
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);
uint8_t getState() { return state;};
uint8_t getDirection() {return direction;};
};

@ -18,10 +18,10 @@ extern "C" {
#define NODEMCU_LED 16
// PWM
#include "pwm.h"
#include "treppe.h"
os_timer_t timer1;
uint8_t timer_flag = 0;
Treppe stairs(13);
Treppe stairs(16);
// WIFI
const char* ssid = STASSID;
@ -105,7 +105,22 @@ void setup() {
os_timer_arm(&timer1, 20, true);
}
#include <random>
void loop() {
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
if(stairs.getState() == 0) {
delay(1000);
// uint32_t t = rand() % stairs.getTicks();
// uint32_t d = rand() % 2;
// stairs.setTick(t);
// stairs.setDirection(d);
stairs.setDirection(!stairs.getDirection());
stairs.setState(1);
}
TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA");
TIMEIF_US(httpServer.handleClient(), 10000, "HTTP");
}

@ -1,40 +1,40 @@
#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
test/test_pwm.cpp Normal 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()
{
}