keyboard.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * keyboard.ino
  3. *
  4. * This script handle the keyboard related events
  5. * in the task scheduler
  6. */
  7. //Scheduler entry point
  8. void keyboardListenerCallback(){
  9. updateButtonStates();
  10. handButtonLogic();
  11. }
  12. //Update the button pressing state to the global variable
  13. void updateButtonStates(){
  14. int val = 0;
  15. val = digitalRead(D7);
  16. buttonState[0] = (val == 0);
  17. val = digitalRead(D6);
  18. buttonState[1] = (val == 0);
  19. val = digitalRead(D5);
  20. buttonState[2] = (val == 0);
  21. val = digitalRead(D0);
  22. buttonState[3] = (val == 0);
  23. }
  24. //Restore page to currentPage
  25. void restorePage(){
  26. if (currentPage == 0){
  27. changePage("home");
  28. updateTimeInfo();
  29. renderHomepageWeatherInfo();
  30. }else if (currentPage == 1){
  31. changePage("forcast");
  32. currentPage = 1;
  33. }else{
  34. changePage("home");
  35. updateTimeInfo();
  36. renderHomepageWeatherInfo();
  37. }
  38. }
  39. //Use the global variable state to decide which page to swap
  40. void handButtonLogic(){
  41. //Special Buttons Combinations
  42. if (buttonState[0] && buttonState[1]){
  43. //Reload the weather information
  44. if(isNight()){
  45. changePage("togn");
  46. sendCommand("t0.txt=\"Making API request\"");
  47. sendCommand("debug.txt=\"Updating weather forcast\"");
  48. }else{
  49. changePage("togd");
  50. sendCommand("debug.txt=\"Updating weather forcast\"");
  51. }
  52. updateCurrentWeatherInfo();
  53. renderHomepageWeatherInfo();
  54. bool succ = updateAndRenderForcastInfo();
  55. if (!succ){
  56. //Error occured
  57. return;
  58. }
  59. if(isNight()){
  60. changePage("okn");
  61. }else{
  62. changePage("okd");
  63. }
  64. delay(3000);
  65. restorePage();
  66. return;
  67. }
  68. //Normal button presses
  69. if (buttonState[0] && currentPage != 0){
  70. //Back to homepage
  71. changePage("home");
  72. currentPage = 0;
  73. updateTimeInfo();
  74. renderHomepageWeatherInfo();
  75. }else if (buttonState[1] && currentPage != 1){
  76. //Open Weather Forcast
  77. changePage("forcast");
  78. currentPage = 1;
  79. }else if (buttonState[2] && currentPage != 2){
  80. //IoT Action 1
  81. if(isNight()){
  82. changePage("togn");
  83. }else{
  84. changePage("togd");
  85. }
  86. //TODO: Restful call here
  87. delay(5000);
  88. if(isNight()){
  89. changePage("okn");
  90. }else{
  91. changePage("okd");
  92. }
  93. delay(3000);
  94. restorePage();
  95. }else if (buttonState[3]){
  96. //IoT Action 2
  97. if(!isNight()){
  98. changePage("togn");
  99. }else{
  100. changePage("togd");
  101. }
  102. //TODO: Restful call here
  103. delay(5000);
  104. if(!isNight()){
  105. changePage("okn");
  106. }else{
  107. changePage("okd");
  108. }
  109. delay(3000);
  110. restorePage();
  111. }
  112. }