123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /*
- 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 <stdio.h>
- #include <string.h>
- #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<ADC_GRP1_BUF_DEPTH; i++) {
- adc_buf += ADC_Data[i] * ADC_Data[i];
- }
- // adc_buf - sum of squares. SQRT:
- adc_buf /= ADC_GRP1_BUF_DEPTH;
- uint32_t val = 0;
- uint32_t step = 1;
- while (step < adc_buf) {
- adc_buf -= step;
- step += 2;
- val ++;
- }
- // transform ADC values to real voltage
- val *= ADC_REF_VOLTAGE;
- val += 2048;
- val /= 4096;
- Voltage = val;
- //adcStartConversionI(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH);
- }
|