A project that aims at making a LED based light system controlled by microcontroller and with BLE (soon) with smooth color transitions.
Revision 0:b350f8395bff, committed 2017-08-18
- Comitter:
- ledonger
- Date:
- Fri Aug 18 08:39:43 2017 +0000
- Child:
- 1:8b80523d8959
- Commit message:
- FlowerColor v0.1 problem with L073 : LED not synchronized
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDManager.h Fri Aug 18 08:39:43 2017 +0000
@@ -0,0 +1,90 @@
+#ifndef LEDMANAGER_H
+#define LEDMANAGER_H
+
+#include "mbed.h"
+
+class LEDManager
+{
+ public:
+ LEDManager(PinName pin, int period_us)
+ {
+ this->io = new DigitalOut(pin);
+ this->period_us = period_us;
+ this->prevUpdate = 0;
+ this->highTime = this->period_us*0.5;
+ this->lowTime = this->period_us - this->highTime;
+ this->fadeFactor = 100;
+ this->situation = 1;
+ this->fadeUpdatePeriod = 10000;
+ }
+
+ LEDManager(DigitalOut *io, int period_us)
+ {
+ this->io = io;
+ this->period_us = period_us;
+ this->prevUpdate = 0;
+ this->highTime = this->period_us*0.5;
+ this->lowTime = this->period_us - this->highTime;
+ this->fadeFactor = 100;
+ this->situation = 1;
+ this->fadeUpdatePeriod = 10000;
+ }
+
+ void processLED(int time)
+ {
+ if(situation == 1){
+ if((time - prevUpdate) < highTime){
+ io->write(1);
+ }
+ else{
+ situation = 2;
+ prevUpdate = time;
+ }
+ }
+ if(situation == 2){
+ if((time - prevUpdate) < lowTime){
+ io->write(0);
+ }
+ else{
+ situation = 1;
+ prevUpdate = time;
+ }
+ }
+
+
+ if(time - prevFadeUpdate > fadeUpdatePeriod){
+ highTime = highTime + fadeFactor;
+ lowTime = period_us - highTime;
+ if(highTime >= period_us){
+ fadeFactor = -fadeFactor;
+ }
+ else if(highTime <= 0){
+ fadeFactor = -fadeFactor;
+ }
+ prevFadeUpdate = time;
+ }
+ }
+
+ /*void setFadeUpdatePeriod(int period)
+ {
+ this->fadeUpdatePeriod = period;
+ }
+ void setFadeFactor(int fadeFactor)
+ {
+ this->fadeFactor = fadeFactor;
+ }*/
+
+ private:
+ DigitalOut *io;
+ int period_us;
+ int prevUpdate;
+ int highTime;
+ int lowTime;
+ int situation;
+
+ int prevFadeUpdate;
+ int fadeFactor;
+ int fadeUpdatePeriod;
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri Aug 18 08:39:43 2017 +0000
@@ -0,0 +1,79 @@
+#include "mbed.h"
+#include "LEDManager.h"
+
+PinName pin_array[] = {LED1,D11,D4,D2,D5,D6,D7,D8,D9,D10};
+//int pin_array_size = 10/*sizeof(pin_array)/sizeof(PinName)*/;
+
+//DigitalOut** io_array;
+Timer timer;
+//Serial pc(USBTX,USBRX,921600);
+
+//void set_pin_high();
+//void set_pin_low();
+
+
+int main()
+{
+ //Init
+ //pc.printf("Starting...\n");
+ //printf("pin_array_size : %d\n",pin_array_size);
+
+ //io_array = new DigitalOut*[pin_array_size];
+
+ /*int i;
+ for(i = 0; i < pin_array_size; i++)
+ {
+ io_array[i] = new DigitalOut(pin_array[i]);
+ }
+ printf("Init : OK\n");
+
+ printf("Test procedure\n");
+ for(i = 0; i < pin_array_size; i++)
+ {
+ *(io_array[i]) = 1;
+ wait(0.1);
+ *(io_array[i]) = 0;
+ wait(0.1);
+ }*/
+ timer.start();
+
+ /*int period_us = 10000;
+ float highTime1 = period_us*0.5;//50%
+ float lowTime1 = (period_us-highTime1);
+ int prevTime = timer.read_us();
+
+ int prevUpdateTime = timer.read_us();
+ int updateTime = 10000;
+ int fadeFactor = 75;
+
+ int situation1 = 1;
+ int situation2 = 1;*/
+
+ LEDManager *led1 = new LEDManager(D11,10000);
+ LEDManager *led2 = new LEDManager(D4,10000);
+ LEDManager *led3 = new LEDManager(D2,10000);
+ LEDManager *led4 = new LEDManager(D5,10000);
+ LEDManager *led5 = new LEDManager(D6,10000);
+ LEDManager *led6 = new LEDManager(D7,10000);
+ LEDManager *led7 = new LEDManager(D8,10000);
+ LEDManager *led8 = new LEDManager(D9,10000);
+ LEDManager *led9 = new LEDManager(D10,10000);
+ //pc.printf("Test : OK\n");
+ int time = timer.read_us();
+ while(1)
+ {
+ led1->processLED(time);
+ led2->processLED(time);
+ led3->processLED(time);
+ led4->processLED(time);
+ led5->processLED(time);
+ led6->processLED(time);
+ led7->processLED(time);
+ led8->processLED(time);
+ led9->processLED(time);
+
+ //printf("%d\n",timer.read_us()-time);
+ time = timer.read_us();
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Aug 18 08:39:43 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/fd96258d940d \ No newline at end of file