main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* Activates the ADC1 driver and the temperature sensor. */
  62. adcStart(&ADCD1, NULL);
  63. adcStartConversion(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH);
  64. /* Starting GPT3 driver, it is used for triggering the ADC. */
  65. gptStart(&GPTD3, &gptcfg1);
  66. gptStartContinuousI(&GPTD3, 100);
  67. // GPTD1.tim->CCMR1 = 100; // 1Mhz/100=10kHz=100 us
  68. // GPTD1.tim->EGR = STM32_TIM_EGR_CC1G; // Capture/Compare 1 generation
  69. // GPTD1.tim->CCMR1= (STM32_TIM_CCMR1_OC1CE | STM32_TIM_CCMR1_OC1M(3));
  70. // GPTD1.tim->CCER = STM32_TIM_CCER_CC1E;
  71. //?GPTD1.tim->BDTR = STM32_TIM_BDTR_MOE;
  72. /*
  73. * Buttons
  74. */
  75. buttons_Init(bha);
  76. BTNS_ON;
  77. /*
  78. * LCD
  79. */
  80. ST7735_Init();
  81. lcd_MainScreen();
  82. chThdSleepMilliseconds(2000);
  83. lcd_ClearMain();
  84. /*
  85. * Normal main() thread activity.
  86. */
  87. /*
  88. ST7735_WriteString(0, LCD_LINE_1, "OneWire search...", LiberM_7x10, Blue, Black);
  89. onewireMeasure();
  90. int32_t t;
  91. int n = onewireGetDevicesNum();
  92. chsnprintf(buf, 22, "Found %d device[s]", n);
  93. ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Cyan, Black);
  94. chThdSleepMilliseconds(2000);
  95. if (n == 0) {
  96. ST7735_WriteString(0, LCD_LINE_1, "DS18B20 not found.", LiberM_7x10, Red, Black);
  97. }
  98. */
  99. int a, b;
  100. while (true) {
  101. /*
  102. if (n != 0) {
  103. onewireMeasure();
  104. onewire_error_t err = onewireGetErrorCode();
  105. if (err == onewire_OK) {
  106. t = onewireGetTemperature(0);
  107. t += 50;
  108. t /= 100;
  109. a = t / 10; b = t % 10;
  110. chsnprintf(buf, 22, "T = %d.%u °C ", a, b);
  111. ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Yellow, Black);
  112. } else {
  113. chsnprintf(buf, 22, "Onewire error: %d", err);
  114. ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Red, Black);
  115. }
  116. }
  117. */
  118. chsnprintf(buf, 22, "U = %u mV", Voltage);
  119. ST7735_WriteString(0, LCD_LINE_3, buf, LiberM_7x10, Orange, Black);
  120. adcStartConversion(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH);
  121. chThdSleepMilliseconds(500);
  122. }
  123. }
  124. /*
  125. * Private functions
  126. */
  127. /* numbers for screen 160x128
  128. * btn1 - 118, 1..38-FontWidh
  129. * btn2 - 118, 40..78-FontWidh
  130. * btn3 - 118, 80..118-FontWidh
  131. * btn4 - 118, 120..158-FontWidh
  132. *
  133. * main screen 0,20 .. 159,117
  134. */
  135. static void lcd_MainScreen(void) {
  136. ST7735_FillScreen(Black);
  137. ST7735_WriteString(X_centered(6, 11), LCD_LINE_2, "Heater", Font_11x18, Yellow, Black);
  138. ST7735_WriteString(X_centered(5, 11), LCD_LINE_4, "Power", Font_11x18, Yellow, Black);
  139. ST7735_WriteString(X_centered(10, 11), LCD_LINE_6, "Stabilizer", Font_11x18, Yellow, Black);
  140. chThdSleepMilliseconds(1000);
  141. /* top info line */
  142. ST7735_FillRectangle(0, 19, ST7735_WIDTH, 1, White);
  143. ST7735_FillRectangle( 79, 0, 1, 19, White); // separator
  144. ST7735_WriteString(0, 0, " .... V", Font_11x18, Red, Black);
  145. ST7735_WriteString(80, 0, " .... W", Font_11x18, Red, Black);
  146. /* bottom status line */
  147. ST7735_FillRectangle(0, 117, ST7735_WIDTH, 1, White);
  148. ST7735_FillRectangle( 39, 117, 1, 11, White); // separator btn1/btn2
  149. ST7735_FillRectangle( 79, 117, 1, 11, White); // separator btn2/btn3
  150. ST7735_FillRectangle(119, 117, 1, 11, White); // separator btn3/btn4
  151. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  152. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  153. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  154. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  155. }
  156. static void lcd_ClearMain(void) {
  157. ST7735_FillRectangle(0, 20, ST7735_WIDTH, 97, Black);
  158. }
  159. static void btn1_handler(const button_state_t state) {
  160. if (state == BTN_st_Pressed) {
  161. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Black, Grey);
  162. chThdSleepMilliseconds(500);
  163. ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black);
  164. }
  165. }
  166. static void btn2_handler(const button_state_t state) {
  167. if (state == BTN_st_Pressed) {
  168. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Black, Grey);
  169. chThdSleepMilliseconds(500);
  170. ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black);
  171. }
  172. }
  173. static void btn3_handler(const button_state_t state) {
  174. if (state == BTN_st_Pressed) {
  175. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Black, Grey);
  176. chThdSleepMilliseconds(500);
  177. ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black);
  178. }
  179. }
  180. static void btn4_handler(const button_state_t state) {
  181. if (state == BTN_st_Pressed) {
  182. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Black, Grey);
  183. chThdSleepMilliseconds(500);
  184. ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black);
  185. }
  186. }
  187. /**
  188. * @brief The function returns the starting X position for text of len characters long,
  189. * to place it in the center of the display.
  190. * @param len Number of characters in the text,
  191. * @param pix Font width in pixels,
  192. */
  193. static uint8_t X_centered(const uint8_t len, const uint8_t pix) {
  194. unsigned wdt = ST7735_WIDTH;
  195. unsigned pl = len * pix;
  196. if (pl > wdt) {
  197. return 0;
  198. } else {
  199. return (uint8_t)((wdt - pl) / 2);
  200. }
  201. }
  202. /* in ADC_Data[] we heave measured data */
  203. static void ADC_cb(ADCDriver *adcp) {
  204. (void)adcp;
  205. int i;
  206. uint32_t adc_buf = 0;
  207. for (i=0; i<ADC_GRP1_BUF_DEPTH; i++) {
  208. // adc_buf += ADC_Data[i] * ADC_Data[i];
  209. adc_buf += ADC_Data[i];
  210. }
  211. /*
  212. // adc_buf - sum of squares. SQRT:
  213. uint32_t val = 0;
  214. uint32_t step = 1;
  215. while (step < adc_buf) {
  216. adc_buf -= step;
  217. step += 2;
  218. val ++;
  219. }
  220. */
  221. // transform ADC values to real voltage
  222. adc_buf /= ADC_GRP1_BUF_DEPTH;
  223. adc_buf *= ADC_REF_VOLTAGE;
  224. adc_buf /= 4096;
  225. Voltage = adc_buf;
  226. }