main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * LCD
  61. */
  62. ST7735_Init();
  63. lcd_MainScreen();
  64. lcd_ClearMain();
  65. /*
  66. * Normal main() thread activity.
  67. */
  68. while (true) {
  69. chThdSleepMilliseconds(500);
  70. }
  71. }
  72. /*
  73. * Private functions
  74. */
  75. /* numbers for screen 160x128
  76. * btn1 - 118, 1..38-FontWidh
  77. * btn2 - 118, 40..78-FontWidh
  78. * btn3 - 118, 80..118-FontWidh
  79. * btn4 - 118, 120..158-FontWidh
  80. *
  81. * main screen 0,20 .. 159,117
  82. */
  83. static void lcd_MainScreen(void) {
  84. ST7735_FillScreen(Black);
  85. ST7735_WriteString(47, 48, "Heater", Font_11x18, Yellow, Black);
  86. ST7735_WriteString(52, 68, "Power", Font_11x18, Yellow, Black);
  87. ST7735_WriteString(25, 88, "Stabilizer", Font_11x18, Yellow, Black);
  88. chThdSleepMilliseconds(2000);
  89. /* top info line */
  90. ST7735_FillRectangle(0, 18, ST7735_WIDTH, 1, White);
  91. ST7735_FillRectangle( 119, 0, 1, 18, White); // separator
  92. ST7735_WriteString(0, 0, "Line V", Font_11x18, Red, Black);
  93. ST7735_WriteString(120, 0, "Heater W", Font_11x18, Red, Black);
  94. /* bottom status line */
  95. ST7735_FillRectangle(0, 117, ST7735_WIDTH, 1, White);
  96. ST7735_FillRectangle( 39, 117, 1, 11, White); // separator btn1/btn2
  97. ST7735_FillRectangle( 79, 117, 1, 11, White); // separator btn2/btn3
  98. ST7735_FillRectangle(119, 117, 1, 11, White); // separator btn3/btn4
  99. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  100. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  101. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  102. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  103. }
  104. static void lcd_ClearMain(void) {
  105. ST7735_FillRectangle(0, 20, ST7735_WIDTH, 97, Black);
  106. }
  107. static void btn1_handler(const button_state_t) {
  108. if (state == BTN_st_Pressed) {
  109. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Black, Grey);
  110. chThdSleepMilliseconds(500);
  111. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  112. }
  113. }
  114. static void btn2_handler(const button_state_t) {
  115. if (state == BTN_st_Pressed) {
  116. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Black, Grey);
  117. chThdSleepMilliseconds(500);
  118. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  119. }
  120. }
  121. static void btn3_handler(const button_state_t) {
  122. if (state == BTN_st_Pressed) {
  123. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Black, Grey);
  124. chThdSleepMilliseconds(500);
  125. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  126. }
  127. }
  128. static void btn4_handler(const button_state_t) {
  129. if (state == BTN_st_Pressed) {
  130. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Black, Grey);
  131. chThdSleepMilliseconds(500);
  132. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  133. }
  134. }