Extension on RotaryEncoder.h, Host.h... Represesnts a room with PWM LED lightning

Committer:
nzupcic
Date:
Tue Sep 21 16:45:59 2021 +0000
Revision:
0:04a5d18ab628
1st commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nzupcic 0:04a5d18ab628 1 #include "Room.h"
nzupcic 0:04a5d18ab628 2
nzupcic 0:04a5d18ab628 3 Room::Room(PinName pRoomLight, PinName pRotaryA, PinName pRotaryB,
nzupcic 0:04a5d18ab628 4 PinName pSwitch, PinName pBtTx, PinName pBtRx,
nzupcic 0:04a5d18ab628 5 PinName pHostTx, PinName pHostRx)
nzupcic 0:04a5d18ab628 6 : roomLight(pRoomLight), re(pRotaryA, pRotaryB),
nzupcic 0:04a5d18ab628 7 swButton(pSwitch), bt(pBtTx, pBtRx), pc(pHostTx, pHostRx){
nzupcic 0:04a5d18ab628 8 Room::Loop();
nzupcic 0:04a5d18ab628 9 }
nzupcic 0:04a5d18ab628 10
nzupcic 0:04a5d18ab628 11 void Room::Init(void){
nzupcic 0:04a5d18ab628 12 Room::pc.Init(50.0);
nzupcic 0:04a5d18ab628 13 Room::re.Init(50.0);
nzupcic 0:04a5d18ab628 14 Room::Enable = false;
nzupcic 0:04a5d18ab628 15 Room::re.LastState = 0;
nzupcic 0:04a5d18ab628 16 Room::pc.LastState = 0;
nzupcic 0:04a5d18ab628 17 Room::LastState = 0;
nzupcic 0:04a5d18ab628 18 Room::debounce.start();
nzupcic 0:04a5d18ab628 19 Room::timeout_pc.start();
nzupcic 0:04a5d18ab628 20 }
nzupcic 0:04a5d18ab628 21
nzupcic 0:04a5d18ab628 22 void Room::Loop(void){
nzupcic 0:04a5d18ab628 23 Room::Init();
nzupcic 0:04a5d18ab628 24 Room::swButton.rise(this, &Room::SwitchStates);
nzupcic 0:04a5d18ab628 25 Room::tickerEnable.attach(this, &Room::CheckEnables, 0.01);
nzupcic 0:04a5d18ab628 26 Room::pc_thread.start(this, &Room::ListenHost);
nzupcic 0:04a5d18ab628 27 while(1){
nzupcic 0:04a5d18ab628 28 Refresh();
nzupcic 0:04a5d18ab628 29 Thread::wait(500);
nzupcic 0:04a5d18ab628 30 }
nzupcic 0:04a5d18ab628 31 }
nzupcic 0:04a5d18ab628 32
nzupcic 0:04a5d18ab628 33 void Room::Refresh(){
nzupcic 0:04a5d18ab628 34 float brightness = Room::SetBrightness();
nzupcic 0:04a5d18ab628 35 if (brightness > 0 && brightness <= 100){
nzupcic 0:04a5d18ab628 36 Room::roomLight = brightness;
nzupcic 0:04a5d18ab628 37 if (Room::LastState != Room::roomLight){
nzupcic 0:04a5d18ab628 38 if(Room::roomLight != 0.0 && Room::LastState == 0.0){
nzupcic 0:04a5d18ab628 39 Room::pc.printf("\nLight is switched on!\n");
nzupcic 0:04a5d18ab628 40 }
nzupcic 0:04a5d18ab628 41 Room::Update();
nzupcic 0:04a5d18ab628 42 }
nzupcic 0:04a5d18ab628 43 Room::LastState = Room::roomLight;
nzupcic 0:04a5d18ab628 44 }
nzupcic 0:04a5d18ab628 45 else{
nzupcic 0:04a5d18ab628 46 Room::roomLight = 0;
nzupcic 0:04a5d18ab628 47 if (Room::roomLight == 0.0 && Room::LastState != 0.0){
nzupcic 0:04a5d18ab628 48 Room::pc.printf("\nLight is switched off!\n");
nzupcic 0:04a5d18ab628 49 }
nzupcic 0:04a5d18ab628 50 Room::LastState = Room::roomLight;
nzupcic 0:04a5d18ab628 51 }
nzupcic 0:04a5d18ab628 52 }
nzupcic 0:04a5d18ab628 53
nzupcic 0:04a5d18ab628 54 void Room::Update(){
nzupcic 0:04a5d18ab628 55 if (Room::LastState != Room::roomLight || 1){
nzupcic 0:04a5d18ab628 56 float printValue = Room::roomLight;
nzupcic 0:04a5d18ab628 57 Room::pc.printf("\nCurrent brightness is: %.2f[%%]\n", printValue*100);
nzupcic 0:04a5d18ab628 58 }
nzupcic 0:04a5d18ab628 59 }
nzupcic 0:04a5d18ab628 60
nzupcic 0:04a5d18ab628 61 float Room::SetBrightness(void){
nzupcic 0:04a5d18ab628 62 float returnValue = 0;
nzupcic 0:04a5d18ab628 63 bool enable = Room::Enable;
nzupcic 0:04a5d18ab628 64 if(enable){
nzupcic 0:04a5d18ab628 65 if(Room::re.Enable && !Room::pc.Enable){
nzupcic 0:04a5d18ab628 66 returnValue = Room::re.Value/100;
nzupcic 0:04a5d18ab628 67 Room::re.LastState = Room::re.Value;
nzupcic 0:04a5d18ab628 68 };
nzupcic 0:04a5d18ab628 69 if(Room::pc.Enable && !Room::re.Enable){
nzupcic 0:04a5d18ab628 70 returnValue = Room::pc.Value/100;
nzupcic 0:04a5d18ab628 71 Room::pc.LastState = Room::pc.Value;
nzupcic 0:04a5d18ab628 72 };
nzupcic 0:04a5d18ab628 73 }
nzupcic 0:04a5d18ab628 74 return returnValue;
nzupcic 0:04a5d18ab628 75 }
nzupcic 0:04a5d18ab628 76
nzupcic 0:04a5d18ab628 77 void Room::CheckEnables(void){
nzupcic 0:04a5d18ab628 78 if (Room::re.Enable || Room::pc.Enable){
nzupcic 0:04a5d18ab628 79 Room::Enable = true;
nzupcic 0:04a5d18ab628 80 }
nzupcic 0:04a5d18ab628 81 else {
nzupcic 0:04a5d18ab628 82 Room::Enable = false;
nzupcic 0:04a5d18ab628 83 };
nzupcic 0:04a5d18ab628 84 }
nzupcic 0:04a5d18ab628 85
nzupcic 0:04a5d18ab628 86 void Room::ListenHost(void){
nzupcic 0:04a5d18ab628 87 while(1){
nzupcic 0:04a5d18ab628 88 Room::pc.printf("\nSet brightness [%%]: ");
nzupcic 0:04a5d18ab628 89 float setValue = atof(Room::pc.GetInput().c_str());
nzupcic 0:04a5d18ab628 90 if(setValue > 0 && setValue <=100){
nzupcic 0:04a5d18ab628 91 Room::pc.Enable = true;
nzupcic 0:04a5d18ab628 92 Room::pc.Value = setValue;
nzupcic 0:04a5d18ab628 93 }
nzupcic 0:04a5d18ab628 94 else if (setValue == 0){
nzupcic 0:04a5d18ab628 95 Room::pc.Enable = false;
nzupcic 0:04a5d18ab628 96 Room::pc.Value = 0.0;
nzupcic 0:04a5d18ab628 97 }else if (setValue == 000){
nzupcic 0:04a5d18ab628 98 //do nothing
nzupcic 0:04a5d18ab628 99 Room::pc.printf("\nTimeout\n");
nzupcic 0:04a5d18ab628 100 }
nzupcic 0:04a5d18ab628 101 }
nzupcic 0:04a5d18ab628 102 }
nzupcic 0:04a5d18ab628 103
nzupcic 0:04a5d18ab628 104 void Room::ListenHardware(void){
nzupcic 0:04a5d18ab628 105 }
nzupcic 0:04a5d18ab628 106
nzupcic 0:04a5d18ab628 107 void Room::ListenBluetooth(void){
nzupcic 0:04a5d18ab628 108 }
nzupcic 0:04a5d18ab628 109
nzupcic 0:04a5d18ab628 110 void Room::SwitchStates(void){
nzupcic 0:04a5d18ab628 111 if(debounce.read_ms() > 200){
nzupcic 0:04a5d18ab628 112 Room::re.Enable = !Room::re.Enable;
nzupcic 0:04a5d18ab628 113 Room::debounce.reset();
nzupcic 0:04a5d18ab628 114 }
nzupcic 0:04a5d18ab628 115 }