main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * Provate variables.
  23. */
  24. onewired_t owdc[ONWIRE_CHANNELS_NUM] = {
  25. {0, 0, onewire_No_Dev, &ow_cfg1},
  26. {0, 0, onewire_No_Dev, &ow_cfg2}
  27. };
  28. /*
  29. * Generic code.
  30. */
  31. /*
  32. * Blinker thread, times are in milliseconds.
  33. */
  34. static THD_WORKING_AREA(waThread1, 128);
  35. static __attribute__((noreturn)) THD_FUNCTION(Thread1, arg) {
  36. (void)arg;
  37. chRegSetThreadName("blinker");
  38. while (true) {
  39. palClearLine(LINE_LED);
  40. chThdSleepMilliseconds(500);
  41. palSetLine(LINE_LED);
  42. chThdSleepMilliseconds(500);
  43. }
  44. }
  45. /*
  46. * Application entry point.
  47. */
  48. int main(void) {
  49. char buf[24];
  50. /*
  51. * System initializations.
  52. * - HAL initialization, this also initializes the configured device drivers
  53. * and performs the board-specific initializations.
  54. * - Kernel initialization, the main() function becomes a thread and the
  55. * RTOS is active.
  56. */
  57. halInit();
  58. chSysInit();
  59. /*
  60. * Initializes a SPI1 driver.
  61. */
  62. spiStart(&SPID1, &spi1cfg);
  63. spiSelectI(&SPID1);
  64. /*
  65. * Creates the blinker thread.
  66. */
  67. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  68. /* Activates the ADC1 driver and the temperature sensor. */
  69. adcStart(&ADCD1, NULL);
  70. adcStartConversionI(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH);
  71. /* Starting GPT3 driver, it is used for triggering the ADC. */
  72. gptStart(&GPTD3, &gptcfg1);
  73. gptStartContinuousI(&GPTD3, 100);
  74. /*
  75. * Buttons
  76. */
  77. buttons_Init(bha);
  78. BTNS_ON;
  79. /*
  80. * LCD
  81. */
  82. ST7735_Init();
  83. lcd_MainScreen();
  84. chThdSleepMilliseconds(1000);
  85. lcd_ClearMain();
  86. /*
  87. * Normal main() thread activity.
  88. */
  89. int32_t t;
  90. int a, b;
  91. ST7735_WriteString(0, LCD_LINE_1, "OneWire1 search...", LiberM_7x10, Blue, Black);
  92. onewireMeasure(&owdc[ow1]);
  93. chsnprintf(buf, 22, "Found1 %d device[s]", owdc[ow1].dev_on_bus);
  94. ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Cyan, Black);
  95. ST7735_WriteString(0, LCD_LINE_2, "OneWire2 search...", LiberM_7x10, Blue, Black);
  96. onewireMeasure(&owdc[ow2]);
  97. chsnprintf(buf, 22, "Found2 %d device[s]", owdc[ow2].dev_on_bus);
  98. ST7735_WriteString(0, LCD_LINE_2, buf, LiberM_7x10, Cyan, Black);
  99. chThdSleepMilliseconds(1000);
  100. while (true) {
  101. onewireMeasure(&owdc[ow1]);
  102. if (owdc[ow1].err_code == onewire_OK) {
  103. t = owdc[ow1].temperature;
  104. t += 50;
  105. t /= 100;
  106. a = t / 10; b = t % 10;
  107. chsnprintf(buf, 22, "T1 = %d.%u °C ", a, b);
  108. ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Yellow, Black);
  109. } else {
  110. chsnprintf(buf, 22, "Onewire1 error: %d", owdc[ow1].err_code);
  111. ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Red, Black);
  112. }
  113. onewireMeasure(&owdc[ow2]);
  114. if (owdc[ow2].err_code == onewire_OK) {
  115. t = owdc[ow2].temperature;
  116. t += 50;
  117. t /= 100;
  118. a = t / 10; b = t % 10;
  119. chsnprintf(buf, 22, "T2 = %d.%u °C ", a, b);
  120. ST7735_WriteString(0, LCD_LINE_2, buf, LiberM_7x10, Yellow, Black);
  121. } else {
  122. chsnprintf(buf, 22, "Onewire2 error: %d", owdc[ow2].err_code);
  123. ST7735_WriteString(0, LCD_LINE_2, buf, LiberM_7x10, Red, Black);
  124. }
  125. chsnprintf(buf, 22, "U = %4u mV", Voltage);
  126. ST7735_WriteString(0, LCD_LINE_4, buf, LiberM_7x10, Orange, Black);
  127. //adcStartConversion(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH);
  128. chThdSleepMilliseconds(500);
  129. }
  130. }
  131. /*
  132. * Private functions
  133. */
  134. /* numbers for screen 160x128
  135. * btn1 - 118, 1..38-FontWidh
  136. * btn2 - 118, 40..78-FontWidh
  137. * btn3 - 118, 80..118-FontWidh
  138. * btn4 - 118, 120..158-FontWidh
  139. *
  140. * main screen 0,20 .. 159,117
  141. */
  142. static void lcd_MainScreen(void) {
  143. ST7735_FillScreen(Black);
  144. ST7735_WriteString(X_centered(6, 11), LCD_LINE_2, "Heater", Font_11x18, Yellow, Black);
  145. ST7735_WriteString(X_centered(5, 11), LCD_LINE_4, "Power", Font_11x18, Yellow, Black);
  146. ST7735_WriteString(X_centered(10, 11), LCD_LINE_6, "Stabilizer", Font_11x18, Yellow, Black);
  147. chThdSleepMilliseconds(1000);
  148. /* top info line */
  149. ST7735_FillRectangle(0, 19, ST7735_WIDTH, 1, White);
  150. ST7735_FillRectangle( 79, 0, 1, 19, White); // separator
  151. ST7735_WriteString(0, 0, " .... V", Font_11x18, Red, Black);
  152. ST7735_WriteString(80, 0, " .... W", Font_11x18, Red, Black);
  153. /* bottom status line */
  154. ST7735_FillRectangle(0, 117, ST7735_WIDTH, 1, White);
  155. ST7735_FillRectangle( 39, 117, 1, 11, White); // separator btn1/btn2
  156. ST7735_FillRectangle( 79, 117, 1, 11, White); // separator btn2/btn3
  157. ST7735_FillRectangle(119, 117, 1, 11, White); // separator btn3/btn4
  158. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  159. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  160. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  161. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  162. }
  163. static void lcd_ClearMain(void) {
  164. ST7735_FillRectangle(0, 20, ST7735_WIDTH, 97, Black);
  165. }
  166. static void btn1_handler(const button_state_t state) {
  167. if (state == BTN_st_Pressed) {
  168. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Black, Grey);
  169. chThdSleepMilliseconds(500);
  170. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  171. }
  172. }
  173. static void btn2_handler(const button_state_t state) {
  174. if (state == BTN_st_Pressed) {
  175. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Black, Grey);
  176. chThdSleepMilliseconds(500);
  177. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  178. }
  179. }
  180. static void btn3_handler(const button_state_t state) {
  181. if (state == BTN_st_Pressed) {
  182. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Black, Grey);
  183. chThdSleepMilliseconds(500);
  184. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  185. }
  186. }
  187. static void btn4_handler(const button_state_t state) {
  188. if (state == BTN_st_Pressed) {
  189. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Black, Grey);
  190. chThdSleepMilliseconds(500);
  191. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  192. }
  193. }
  194. /**
  195. * @brief The function returns the starting X position for text of len characters long,
  196. * to place it in the center of the display.
  197. * @param len Number of characters in the text,
  198. * @param pix Font width in pixels,
  199. */
  200. static uint8_t X_centered(const uint8_t len, const uint8_t pix) {
  201. unsigned wdt = ST7735_WIDTH;
  202. unsigned pl = len * pix;
  203. if (pl > wdt) {
  204. return 0;
  205. } else {
  206. return (uint8_t)((wdt - pl) / 2);
  207. }
  208. }
  209. /* in ADC_Data[] we heave measured data */
  210. static void ADC_cb(ADCDriver *adcp) {
  211. (void)adcp;
  212. int i;
  213. uint32_t adc_buf = 0;
  214. for (i=0; i<ADC_GRP1_BUF_DEPTH; i++) {
  215. adc_buf += ADC_Data[i] * ADC_Data[i];
  216. }
  217. // adc_buf - sum of squares. SQRT:
  218. adc_buf /= ADC_GRP1_BUF_DEPTH;
  219. uint32_t val = 0;
  220. uint32_t step = 1;
  221. while (step < adc_buf) {
  222. adc_buf -= step;
  223. step += 2;
  224. val ++;
  225. }
  226. // transform ADC values to real voltage
  227. val *= ADC_REF_VOLTAGE;
  228. val += 2048;
  229. val /= 4096;
  230. Voltage = val;
  231. //adcStartConversionI(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH);
  232. }