/* ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include #include #include "ch.h" #include "hal.h" #include "chprintf.h" #include "main.h" #include "st7735.h" #include "onewire.h" /* * Provate variables. */ onewired_t owdc[ONWIRE_CHANNELS_NUM] = { {0, 0, onewire_No_Dev, &ow_cfg1}, {0, 0, onewire_No_Dev, &ow_cfg2} }; /* * Generic code. */ /* * Blinker thread, times are in milliseconds. */ static THD_WORKING_AREA(waThread1, 128); static __attribute__((noreturn)) THD_FUNCTION(Thread1, arg) { (void)arg; chRegSetThreadName("blinker"); while (true) { palClearLine(LINE_LED); chThdSleepMilliseconds(500); palSetLine(LINE_LED); chThdSleepMilliseconds(500); } } /* * Application entry point. */ int main(void) { char buf[24]; /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Initializes a SPI1 driver. */ spiStart(&SPID1, &spi1cfg); spiSelectI(&SPID1); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* Activates the ADC1 driver and the temperature sensor. */ adcStart(&ADCD1, NULL); adcStartConversionI(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH); /* Starting GPT3 driver, it is used for triggering the ADC. */ gptStart(&GPTD3, &gptcfg1); gptStartContinuousI(&GPTD3, 100); /* * Buttons */ buttons_Init(bha); BTNS_ON; /* * LCD */ ST7735_Init(); lcd_MainScreen(); chThdSleepMilliseconds(1000); lcd_ClearMain(); /* * Normal main() thread activity. */ int32_t t; int a, b; ST7735_WriteString(0, LCD_LINE_1, "OneWire1 search...", LiberM_7x10, Blue, Black); onewireMeasure(&owdc[ow1]); chsnprintf(buf, 22, "Found1 %d device[s]", owdc[ow1].dev_on_bus); ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Cyan, Black); ST7735_WriteString(0, LCD_LINE_2, "OneWire2 search...", LiberM_7x10, Blue, Black); onewireMeasure(&owdc[ow2]); chsnprintf(buf, 22, "Found2 %d device[s]", owdc[ow2].dev_on_bus); ST7735_WriteString(0, LCD_LINE_2, buf, LiberM_7x10, Cyan, Black); chThdSleepMilliseconds(1000); while (true) { onewireMeasure(&owdc[ow1]); if (owdc[ow1].err_code == onewire_OK) { t = owdc[ow1].temperature; t += 50; t /= 100; a = t / 10; b = t % 10; chsnprintf(buf, 22, "T1 = %d.%u °C ", a, b); ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Yellow, Black); } else { chsnprintf(buf, 22, "Onewire1 error: %d", owdc[ow1].err_code); ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Red, Black); } onewireMeasure(&owdc[ow2]); if (owdc[ow2].err_code == onewire_OK) { t = owdc[ow2].temperature; t += 50; t /= 100; a = t / 10; b = t % 10; chsnprintf(buf, 22, "T2 = %d.%u °C ", a, b); ST7735_WriteString(0, LCD_LINE_2, buf, LiberM_7x10, Yellow, Black); } else { chsnprintf(buf, 22, "Onewire2 error: %d", owdc[ow2].err_code); ST7735_WriteString(0, LCD_LINE_2, buf, LiberM_7x10, Red, Black); } chsnprintf(buf, 22, "U = %4u mV", Voltage); ST7735_WriteString(0, LCD_LINE_4, buf, LiberM_7x10, Orange, Black); //adcStartConversion(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH); chThdSleepMilliseconds(500); } } /* * Private functions */ /* numbers for screen 160x128 * btn1 - 118, 1..38-FontWidh * btn2 - 118, 40..78-FontWidh * btn3 - 118, 80..118-FontWidh * btn4 - 118, 120..158-FontWidh * * main screen 0,20 .. 159,117 */ static void lcd_MainScreen(void) { ST7735_FillScreen(Black); ST7735_WriteString(X_centered(6, 11), LCD_LINE_2, "Heater", Font_11x18, Yellow, Black); ST7735_WriteString(X_centered(5, 11), LCD_LINE_4, "Power", Font_11x18, Yellow, Black); ST7735_WriteString(X_centered(10, 11), LCD_LINE_6, "Stabilizer", Font_11x18, Yellow, Black); chThdSleepMilliseconds(1000); /* top info line */ ST7735_FillRectangle(0, 19, ST7735_WIDTH, 1, White); ST7735_FillRectangle( 79, 0, 1, 19, White); // separator ST7735_WriteString(0, 0, " .... V", Font_11x18, Red, Black); ST7735_WriteString(80, 0, " .... W", Font_11x18, Red, Black); /* bottom status line */ ST7735_FillRectangle(0, 117, ST7735_WIDTH, 1, White); ST7735_FillRectangle( 39, 117, 1, 11, White); // separator btn1/btn2 ST7735_FillRectangle( 79, 117, 1, 11, White); // separator btn2/btn3 ST7735_FillRectangle(119, 117, 1, 11, White); // separator btn3/btn4 ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black); ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black); ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black); ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black); } static void lcd_ClearMain(void) { ST7735_FillRectangle(0, 20, ST7735_WIDTH, 97, Black); } static void btn1_handler(const button_state_t state) { if (state == BTN_st_Pressed) { ST7735_WriteString(0, 118, " + ", LiberM_7x10, Black, Grey); chThdSleepMilliseconds(500); ST7735_WriteString(0, 118, " + ", LiberM_7x10, Silver, Black); } } static void btn2_handler(const button_state_t state) { if (state == BTN_st_Pressed) { ST7735_WriteString(40, 118, " - ", LiberM_7x10, Black, Grey); chThdSleepMilliseconds(500); ST7735_WriteString(40, 118, " - ", LiberM_7x10, Silver, Black); } } static void btn3_handler(const button_state_t state) { if (state == BTN_st_Pressed) { ST7735_WriteString(80, 118, " R ", LiberM_7x10, Black, Grey); chThdSleepMilliseconds(500); ST7735_WriteString(80, 118, " R ", LiberM_7x10, Silver, Black); } } static void btn4_handler(const button_state_t state) { if (state == BTN_st_Pressed) { ST7735_WriteString(120, 118, " S ", LiberM_7x10, Black, Grey); chThdSleepMilliseconds(500); ST7735_WriteString(120, 118, " S ", LiberM_7x10, Silver, Black); } } /** * @brief The function returns the starting X position for text of len characters long, * to place it in the center of the display. * @param len Number of characters in the text, * @param pix Font width in pixels, */ static uint8_t X_centered(const uint8_t len, const uint8_t pix) { unsigned wdt = ST7735_WIDTH; unsigned pl = len * pix; if (pl > wdt) { return 0; } else { return (uint8_t)((wdt - pl) / 2); } } /* in ADC_Data[] we heave measured data */ static void ADC_cb(ADCDriver *adcp) { (void)adcp; int i; uint32_t adc_buf = 0; for (i=0; i