123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- /*
- 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"
- /*===========================================================================*/
- /* 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);
- /*
- * Buttons
- */
- buttons_Init(bha);
- BTNS_ON;
- /*
- * LCD
- */
- ST7735_Init();
- lcd_MainScreen();
- chThdSleepMilliseconds(2000);
- lcd_ClearMain();
- /*
- * Normal main() thread activity.
- */
- ST7735_WriteString(0, 20, "onewireTest...", LiberM_7x10, Blue, Black);
- onewireTest();
- int32_t t;
- int n = onewireGetDevicesNum();
- chsnprintf(buf, 22, "Found %d device[s]", n);
- ST7735_WriteString(0, 80, buf, LiberM_7x10, Cyan, Black);
- if (n != 0) {
- t = onewireGetTemperature(1);
- chsnprintf(buf, 22, " T = %d °C ", t);
- ST7735_WriteString(0, 20, buf, LiberM_7x10, Yellow, Black);
- } else {
- ST7735_WriteString(0, 20, "DS18B20 not found.", LiberM_7x10, Red, Black);
- }
- int a, b;
- while (true) {
- chThdSleepMilliseconds(1000);
- if (n != 0) {
- onewireTest();
- t = onewireGetTemperature(1);
- a = t / 1000; b = t % 1000;
- chsnprintf(buf, 22, "T = %d.%03u oC ", a, b);
- ST7735_WriteString(0, 40, buf, LiberM_7x10, Yellow, Black);
- }
- }
- }
- /*
- * 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(47, 38, "Heater", Font_11x18, Yellow, Black);
- ST7735_WriteString(52, 58, "Power", Font_11x18, Yellow, Black);
- ST7735_WriteString(25, 78, "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);
- }
- }
|