main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "testhal_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. onewireTest();
  77. int n = onewireGetDevicesNum();
  78. if (n != 0) {
  79. int32_t t = onewireGetTemperature(1);
  80. chsnprintf(buf, 22, " T = %d °C ", t);
  81. ST7735_WriteString(0, 20, buf, LiberM_7x10, Yellow, Black);
  82. } else {
  83. ST7735_WriteString(0, 20, "DS18B20 not found.", LiberM_7x10, Red, Black);
  84. }
  85. while (true) {
  86. chThdSleepMilliseconds(500);
  87. }
  88. }
  89. /*
  90. * Private functions
  91. */
  92. /* numbers for screen 160x128
  93. * btn1 - 118, 1..38-FontWidh
  94. * btn2 - 118, 40..78-FontWidh
  95. * btn3 - 118, 80..118-FontWidh
  96. * btn4 - 118, 120..158-FontWidh
  97. *
  98. * main screen 0,20 .. 159,117
  99. */
  100. static void lcd_MainScreen(void) {
  101. ST7735_FillScreen(Black);
  102. ST7735_WriteString(47, 38, "Heater", Font_11x18, Yellow, Black);
  103. ST7735_WriteString(52, 58, "Power", Font_11x18, Yellow, Black);
  104. ST7735_WriteString(25, 78, "Stabilizer", Font_11x18, Yellow, Black);
  105. chThdSleepMilliseconds(1000);
  106. /* top info line */
  107. ST7735_FillRectangle(0, 19, ST7735_WIDTH, 1, White);
  108. ST7735_FillRectangle( 79, 0, 1, 19, White); // separator
  109. ST7735_WriteString(0, 0, " .... V", Font_11x18, Red, Black);
  110. ST7735_WriteString(80, 0, " .... W", Font_11x18, Red, Black);
  111. /* bottom status line */
  112. ST7735_FillRectangle(0, 117, ST7735_WIDTH, 1, White);
  113. ST7735_FillRectangle( 39, 117, 1, 11, White); // separator btn1/btn2
  114. ST7735_FillRectangle( 79, 117, 1, 11, White); // separator btn2/btn3
  115. ST7735_FillRectangle(119, 117, 1, 11, White); // separator btn3/btn4
  116. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  117. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  118. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  119. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  120. }
  121. static void lcd_ClearMain(void) {
  122. ST7735_FillRectangle(0, 20, ST7735_WIDTH, 97, Black);
  123. }
  124. static void btn1_handler(const button_state_t state) {
  125. if (state == BTN_st_Pressed) {
  126. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Black, Grey);
  127. chThdSleepMilliseconds(500);
  128. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  129. }
  130. }
  131. static void btn2_handler(const button_state_t state) {
  132. if (state == BTN_st_Pressed) {
  133. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Black, Grey);
  134. chThdSleepMilliseconds(500);
  135. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  136. }
  137. }
  138. static void btn3_handler(const button_state_t state) {
  139. if (state == BTN_st_Pressed) {
  140. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Black, Grey);
  141. chThdSleepMilliseconds(500);
  142. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  143. }
  144. }
  145. static void btn4_handler(const button_state_t state) {
  146. if (state == BTN_st_Pressed) {
  147. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Black, Grey);
  148. chThdSleepMilliseconds(500);
  149. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  150. }
  151. }