main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "ch.h"
  16. #include "hal.h"
  17. #include "chprintf.h"
  18. #include "main.h"
  19. #include "st7735.h"
  20. /*===========================================================================*/
  21. /* Generic code. */
  22. /*===========================================================================*/
  23. /*
  24. * Blinker thread, times are in milliseconds.
  25. */
  26. static THD_WORKING_AREA(waThread1, 128);
  27. static __attribute__((noreturn)) THD_FUNCTION(Thread1, arg) {
  28. (void)arg;
  29. chRegSetThreadName("blinker");
  30. while (true) {
  31. palClearLine(LINE_LED);
  32. chThdSleepMilliseconds(500);
  33. palSetLine(LINE_LED);
  34. chThdSleepMilliseconds(500);
  35. }
  36. }
  37. /*
  38. * Application entry point.
  39. */
  40. int main(void) {
  41. /*
  42. * System initializations.
  43. * - HAL initialization, this also initializes the configured device drivers
  44. * and performs the board-specific initializations.
  45. * - Kernel initialization, the main() function becomes a thread and the
  46. * RTOS is active.
  47. */
  48. halInit();
  49. chSysInit();
  50. /*
  51. * Initializes a SPI1 driver.
  52. */
  53. spiStart(&SPID1, &spi1cfg);
  54. spiSelectI(&SPID1);
  55. /*
  56. * Creates the blinker thread.
  57. */
  58. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  59. /*
  60. * Buttons
  61. */
  62. buttons_Init(bha);
  63. BTNS_ON;
  64. /*
  65. * LCD
  66. */
  67. ST7735_Init();
  68. lcd_MainScreen();
  69. chThdSleepMilliseconds(2000);
  70. lcd_ClearMain();
  71. /*
  72. * Normal main() thread activity.
  73. */
  74. while (true) {
  75. chThdSleepMilliseconds(500);
  76. }
  77. }
  78. /*
  79. * Private functions
  80. */
  81. /* numbers for screen 160x128
  82. * btn1 - 118, 1..38-FontWidh
  83. * btn2 - 118, 40..78-FontWidh
  84. * btn3 - 118, 80..118-FontWidh
  85. * btn4 - 118, 120..158-FontWidh
  86. *
  87. * main screen 0,20 .. 159,117
  88. */
  89. static void lcd_MainScreen(void) {
  90. ST7735_FillScreen(Black);
  91. ST7735_WriteString(47, 38, "Heater", Font_11x18, Yellow, Black);
  92. ST7735_WriteString(52, 58, "Power", Font_11x18, Yellow, Black);
  93. ST7735_WriteString(25, 78, "Stabilizer", Font_11x18, Yellow, Black);
  94. chThdSleepMilliseconds(1000);
  95. /* top info line */
  96. ST7735_FillRectangle(0, 19, ST7735_WIDTH, 1, White);
  97. ST7735_FillRectangle( 79, 0, 1, 19, White); // separator
  98. ST7735_WriteString(0, 0, " .... V", Font_11x18, Red, Black);
  99. ST7735_WriteString(80, 0, " .... W", Font_11x18, Red, Black);
  100. /* bottom status line */
  101. ST7735_FillRectangle(0, 117, ST7735_WIDTH, 1, White);
  102. ST7735_FillRectangle( 39, 117, 1, 11, White); // separator btn1/btn2
  103. ST7735_FillRectangle( 79, 117, 1, 11, White); // separator btn2/btn3
  104. ST7735_FillRectangle(119, 117, 1, 11, White); // separator btn3/btn4
  105. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  106. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  107. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  108. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  109. }
  110. static void lcd_ClearMain(void) {
  111. ST7735_FillRectangle(0, 20, ST7735_WIDTH, 97, Black);
  112. }
  113. static void btn1_handler(const button_state_t state) {
  114. if (state == BTN_st_Pressed) {
  115. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Black, Grey);
  116. chThdSleepMilliseconds(500);
  117. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  118. }
  119. }
  120. static void btn2_handler(const button_state_t state) {
  121. if (state == BTN_st_Pressed) {
  122. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Black, Grey);
  123. chThdSleepMilliseconds(500);
  124. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  125. }
  126. }
  127. static void btn3_handler(const button_state_t state) {
  128. if (state == BTN_st_Pressed) {
  129. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Black, Grey);
  130. chThdSleepMilliseconds(500);
  131. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  132. }
  133. }
  134. static void btn4_handler(const button_state_t state) {
  135. if (state == BTN_st_Pressed) {
  136. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Black, Grey);
  137. chThdSleepMilliseconds(500);
  138. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  139. }
  140. }