main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "onewire.h"
  21. /*===========================================================================*/
  22. /* Generic code. */
  23. /*===========================================================================*/
  24. /*
  25. * Blinker thread, times are in milliseconds.
  26. */
  27. static THD_WORKING_AREA(waThread1, 128);
  28. static __attribute__((noreturn)) THD_FUNCTION(Thread1, arg) {
  29. (void)arg;
  30. chRegSetThreadName("blinker");
  31. while (true) {
  32. palClearLine(LINE_LED);
  33. chThdSleepMilliseconds(500);
  34. palSetLine(LINE_LED);
  35. chThdSleepMilliseconds(500);
  36. }
  37. }
  38. /*
  39. * Application entry point.
  40. */
  41. int main(void) {
  42. char buf[24];
  43. /*
  44. * System initializations.
  45. * - HAL initialization, this also initializes the configured device drivers
  46. * and performs the board-specific initializations.
  47. * - Kernel initialization, the main() function becomes a thread and the
  48. * RTOS is active.
  49. */
  50. halInit();
  51. chSysInit();
  52. /*
  53. * Initializes a SPI1 driver.
  54. */
  55. spiStart(&SPID1, &spi1cfg);
  56. spiSelectI(&SPID1);
  57. /*
  58. * Creates the blinker thread.
  59. */
  60. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  61. /*
  62. * Buttons
  63. */
  64. buttons_Init(bha);
  65. BTNS_ON;
  66. /*
  67. * LCD
  68. */
  69. ST7735_Init();
  70. lcd_MainScreen();
  71. chThdSleepMilliseconds(2000);
  72. lcd_ClearMain();
  73. /*
  74. * Normal main() thread activity.
  75. */
  76. ST7735_WriteString(0, LCD_LINE_1, "OneWire search...", LiberM_7x10, Blue, Black);
  77. onewireMeasure();
  78. int32_t t;
  79. int n = onewireGetDevicesNum();
  80. chsnprintf(buf, 22, "Found %d device[s]", n);
  81. ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Cyan, Black);
  82. chThdSleepMilliseconds(2000);
  83. if (n == 0) {
  84. ST7735_WriteString(0, LCD_LINE_1, "DS18B20 not found.", LiberM_7x10, Red, Black);
  85. }
  86. int a, b;
  87. while (true) {
  88. if (n != 0) {
  89. onewireMeasure();
  90. t = onewireGetTemperature(0);
  91. a = t / 1000; b = t % 1000;
  92. chsnprintf(buf, 22, "T = %d.%03u °C ", a, b);
  93. ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Yellow, Black);
  94. }
  95. chThdSleepMilliseconds(1000);
  96. }
  97. }
  98. /*
  99. * Private functions
  100. */
  101. /* numbers for screen 160x128
  102. * btn1 - 118, 1..38-FontWidh
  103. * btn2 - 118, 40..78-FontWidh
  104. * btn3 - 118, 80..118-FontWidh
  105. * btn4 - 118, 120..158-FontWidh
  106. *
  107. * main screen 0,20 .. 159,117
  108. */
  109. static void lcd_MainScreen(void) {
  110. ST7735_FillScreen(Black);
  111. ST7735_WriteString(X_centered(6, 11), LCD_LINE_2, "Heater", Font_11x18, Yellow, Black);
  112. ST7735_WriteString(X_centered(5, 11), LCD_LINE_4, "Power", Font_11x18, Yellow, Black);
  113. ST7735_WriteString(X_centered(10, 11), LCD_LINE_6, "Stabilizer", Font_11x18, Yellow, Black);
  114. chThdSleepMilliseconds(1000);
  115. /* top info line */
  116. ST7735_FillRectangle(0, 19, ST7735_WIDTH, 1, White);
  117. ST7735_FillRectangle( 79, 0, 1, 19, White); // separator
  118. ST7735_WriteString(0, 0, " .... V", Font_11x18, Red, Black);
  119. ST7735_WriteString(80, 0, " .... W", Font_11x18, Red, Black);
  120. /* bottom status line */
  121. ST7735_FillRectangle(0, 117, ST7735_WIDTH, 1, White);
  122. ST7735_FillRectangle( 39, 117, 1, 11, White); // separator btn1/btn2
  123. ST7735_FillRectangle( 79, 117, 1, 11, White); // separator btn2/btn3
  124. ST7735_FillRectangle(119, 117, 1, 11, White); // separator btn3/btn4
  125. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  126. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  127. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  128. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  129. }
  130. static void lcd_ClearMain(void) {
  131. ST7735_FillRectangle(0, 20, ST7735_WIDTH, 97, Black);
  132. }
  133. static void btn1_handler(const button_state_t state) {
  134. if (state == BTN_st_Pressed) {
  135. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Black, Grey);
  136. chThdSleepMilliseconds(500);
  137. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  138. }
  139. }
  140. static void btn2_handler(const button_state_t state) {
  141. if (state == BTN_st_Pressed) {
  142. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Black, Grey);
  143. chThdSleepMilliseconds(500);
  144. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  145. }
  146. }
  147. static void btn3_handler(const button_state_t state) {
  148. if (state == BTN_st_Pressed) {
  149. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Black, Grey);
  150. chThdSleepMilliseconds(500);
  151. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  152. }
  153. }
  154. static void btn4_handler(const button_state_t state) {
  155. if (state == BTN_st_Pressed) {
  156. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Black, Grey);
  157. chThdSleepMilliseconds(500);
  158. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  159. }
  160. }
  161. /**
  162. * @brief The function returns the starting X position for text of len characters long,
  163. * to place it in the center of the display.
  164. * @param len Number of characters in the text,
  165. * @param pix Font width in pixels,
  166. */
  167. static uint8_t X_centered(const uint8_t len, const uint8_t pix) {
  168. unsigned wdt = ST7735_WIDTH;
  169. unsigned pl = len * pix;
  170. if (pl > wdt) {
  171. return 0;
  172. } else {
  173. return (uint8_t)((wdt - pl) / 2);
  174. }
  175. }