123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /*
- * keyboard.ino
- *
- * This script handle the keyboard related events
- * in the task scheduler
- */
- //Scheduler entry point
- void keyboardListenerCallback(){
- updateButtonStates();
- handButtonLogic();
- }
- //Update the button pressing state to the global variable
- void updateButtonStates(){
- int val = 0;
- val = digitalRead(D7);
- buttonState[0] = (val == 0);
- val = digitalRead(D6);
- buttonState[1] = (val == 0);
- val = digitalRead(D5);
- buttonState[2] = (val == 0);
- val = digitalRead(D0);
- buttonState[3] = (val == 0);
- }
- //Restore page to currentPage
- void restorePage(){
- if (currentPage == 0){
- changePage("home");
- updateTimeInfo();
- renderHomepageWeatherInfo();
- }else if (currentPage == 1){
- changePage("forcast");
- currentPage = 1;
- }else{
- changePage("home");
- updateTimeInfo();
- renderHomepageWeatherInfo();
- }
- }
- //Use the global variable state to decide which page to swap
- void handButtonLogic(){
- //Special Buttons Combinations
- if (buttonState[0] && buttonState[1]){
- //Reload the weather information
- if(isNight()){
- changePage("togn");
- sendCommand("t0.txt=\"Making API request\"");
- sendCommand("debug.txt=\"Updating weather forcast\"");
- }else{
- changePage("togd");
- sendCommand("debug.txt=\"Updating weather forcast\"");
- }
-
- updateCurrentWeatherInfo();
- renderHomepageWeatherInfo();
- bool succ = updateAndRenderForcastInfo();
- if (!succ){
- //Error occured
- return;
- }
- if(isNight()){
- changePage("okn");
- }else{
- changePage("okd");
- }
- delay(3000);
- restorePage();
- return;
- }
- //Normal button presses
- if (buttonState[0] && currentPage != 0){
- //Back to homepage
- changePage("home");
- currentPage = 0;
- updateTimeInfo();
- renderHomepageWeatherInfo();
- }else if (buttonState[1] && currentPage != 1){
- //Open Weather Forcast
- changePage("forcast");
- currentPage = 1;
- }else if (buttonState[2] && currentPage != 2){
- //IoT Action 1
- if(isNight()){
- changePage("togn");
- }else{
- changePage("togd");
- }
- //TODO: Restful call here
- delay(5000);
- if(isNight()){
- changePage("okn");
- }else{
- changePage("okd");
- }
- delay(3000);
- restorePage();
- }else if (buttonState[3]){
- //IoT Action 2
- if(!isNight()){
- changePage("togn");
- }else{
- changePage("togd");
- }
- //TODO: Restful call here
- delay(5000);
- if(!isNight()){
- changePage("okn");
- }else{
- changePage("okd");
- }
- delay(3000);
- restorePage();
-
- }
- }
|