main.c 5.4 KB

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