uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

main.c (2623B)


      1 #include "extern.h"
      2 #include "bme280.h"
      3 #include "i2c.h"
      4 #include "lcd.h"
      5 #include "tmr0.h"
      6 #include "util.h"
      7 
      8 static __code uint16_t __at (_CONFIG) __configword =
      9     _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _LVP_OFF & _WRT_OFF & _BOREN_ON &
     10     _CPD_OFF & _CP_OFF;
     11 
     12 /*
     13  * TODO: turn off unneeded modules (e.g tmr1..) to minimize consumption.
     14  */
     15 
     16 static void	ctx_main(void);
     17 static void	ctx_uptime_maxtp(void);
     18 static void	print_tp(int32_t);
     19 static void	led_blink(void);
     20 static void	button_debounce(void);
     21 
     22 static uint32_t	timecnt = 0;	/* Seconds passed since start */
     23 static int	f_ctx = 0;	/* Change context */
     24 static uint32_t	humid;		/* Current humidity */
     25 static int32_t	tp;		/* Current temperature */
     26 static int32_t	maxtp = -99999;	/* Max temperature */
     27 static char	buf[BUFSIZ+1] = {0}; /* Generic buffer */
     28 
     29 #define LCD_PUTS_INT(buf, v) do {		\
     30 	memset(buf, 0, sizeof(buf));		\
     31 	lcd_puts(itoa(&buf[sizeof(buf)-1], v));	\
     32 } while (0)
     33 
     34 static void
     35 ctx_main(void)
     36 {
     37 	/*lcd_cmd(LCD_CURS_ROW1);*/
     38 	/*lcd_puts("ID: 19390133");*/
     39 
     40 	lcd_cmd(LCD_CURS_ROW1);
     41 	lcd_puts("Temp: ");
     42 	print_tp(tp);
     43 
     44 	lcd_cmd(LCD_CURS_ROW2);
     45 	lcd_puts("Humid: ");
     46 	LCD_PUTS_INT(buf, humid / 1024);
     47 	lcd_putc('.');
     48 	LCD_PUTS_INT(buf, ((humid * 100) / 1024) % 100);
     49 	lcd_putc('%');
     50 }
     51 
     52 static void
     53 ctx_uptime_maxtp(void)
     54 {
     55 	lcd_cmd(LCD_CURS_ROW1);
     56 	lcd_puts("T: ");
     57 	LCD_PUTS_INT(buf, timecnt);
     58 
     59 	lcd_cmd(LCD_CURS_ROW2);
     60 	lcd_puts("Max: ");
     61 	print_tp(maxtp);
     62 }
     63 
     64 static void
     65 print_tp(int32_t tp)
     66 {
     67 	if (tp < 0) {
     68 		tp = -tp;
     69 		lcd_putc('-');
     70 	}
     71 	LCD_PUTS_INT(buf, tp / 100);
     72 	lcd_putc('.');
     73 	LCD_PUTS_INT(buf, tp % 100);
     74 	lcd_puts("\337C   ");
     75 }
     76 
     77 #undef LCD_PUTS_INT
     78 
     79 static void
     80 led_blink(void)
     81 {
     82 	LED_PORT ^= 1;
     83 	/*
     84 	 * Increment here since this function is called every 1 sec.
     85 	 * No need to create another timer callback.
     86 	 */
     87 	timecnt++;
     88 }
     89 
     90 static void
     91 button_debounce(void)
     92 {
     93 	static uint8_t cnt = 0;
     94 
     95 	/* Button is pressed */
     96 	if (BTN_PORT == 0) {
     97 		if (cnt == 0) {
     98 			/* Actual button functionality goes here. */
     99 			f_ctx = 1;
    100 			cnt++;
    101 		}
    102 		cnt = BTN_DEBOUNCE_TIME_MS;
    103 	} else if (cnt != 0) {
    104 		f_ctx = 0;
    105 		cnt--;
    106 	}
    107 }
    108 
    109 void
    110 main(void)
    111 {
    112 	tmr0_init();
    113 	tmr0_set_event(&led_blink, 1000);
    114 	tmr0_set_event(&button_debounce, 1);
    115 	lcd_init();
    116 	i2c_init(I2C_MASTER, I2C_SLEW_OFF, I2C_CLK_1MHZ);
    117 	if (bme280_init() < 0) {
    118 		lcd_puts("BME280 error");
    119 		for (;;); /* Hang */
    120 	}
    121 
    122 	BTN_TRIS = INPUT;
    123 	LED_PORT = 1; /* LED on */
    124 	LED_TRIS = OUTPUT;
    125 
    126 	for (;;) {
    127 		if ((tp = bme280_read_temp()) > maxtp)
    128 			maxtp = tp;
    129 		humid = bme280_read_humid();
    130 		lcd_cmd(LCD_CLEAR);
    131 		if (!f_ctx)
    132 			ctx_main();
    133 		else
    134 			ctx_uptime_maxtp();
    135 		tmr0_delay_ms(1000);
    136 	}
    137 }