Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
en:wb_si702x [2015/01/12 19:04] john1117 |
en:wb_si702x [2015/04/27 09:52] (current) john1117 |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <file c wb_si702x.c> | ||
+ | //------------------------------------------------------------------------------ | ||
+ | // | ||
+ | // si702x of Weather-Board with 16x2 lcd Application. | ||
+ | // | ||
+ | // Defined port number is wiringPi port number. | ||
+ | // | ||
+ | // Compile : gcc -o <create excute file name> <source file name> -lwiringPi | ||
+ | // -lwiringPiDev -lpthread | ||
+ | // Run : sudo ./<created excute file name> | ||
+ | // | ||
+ | //------------------------------------------------------------------------------ | ||
+ | |||
+ | #include <fcntl.h> | ||
+ | #include <stdio.h> | ||
+ | #include <stdlib.h> | ||
+ | #include <stdint.h> | ||
+ | |||
+ | #include <unistd.h> | ||
+ | #include <string.h> | ||
+ | #include <time.h> | ||
+ | |||
+ | #include <wiringPi.h> | ||
+ | #include <lcd.h> | ||
+ | |||
+ | #define LCD_ROW 2 // 16 Char | ||
+ | #define LCD_COL 16 // 2 Line | ||
+ | #define LCD_BUS 4 // Interface 4 Bit mode | ||
+ | #define LCD_UPDATE_PERIOD 300 // 300ms | ||
+ | |||
+ | #define PORT_LCD_RS 7 // GPIOY.BIT3(#83) | ||
+ | #define PORT_LCD_E 0 // GPIOY.BIT8(#88) | ||
+ | #define PORT_LCD_D4 2 // GPIOX.BIT19(#116) | ||
+ | #define PORT_LCD_D5 3 // GPIOX.BIT18(#115) | ||
+ | #define PORT_LCD_D6 1 // GPIOY.BIT7(#87) | ||
+ | #define PORT_LCD_D7 4 // GPIOX.BIT7(#104) | ||
+ | static unsigned char lcdFb[LCD_ROW][LCD_COL] = {0, }; | ||
+ | static int lcdHandle = 0; | ||
+ | static int ledPos = 0; | ||
+ | |||
+ | unsigned char temperature[10] = {0, }; | ||
+ | unsigned char humidity[10] = {0, }; | ||
+ | |||
+ | static const char *si702x_node[] = { | ||
+ | "/sys/bus/i2c/drivers/si702x/1-0040/temperature", | ||
+ | "/sys/bus/i2c/drivers/si702x/1-0040/humidity", | ||
+ | }; | ||
+ | |||
+ | static void getSysInfo(void) | ||
+ | { | ||
+ | int n, tempfd, humifd; | ||
+ | |||
+ | tempfd = open(si702x_node[0], O_RDONLY); | ||
+ | read(tempfd, temperature, 10); | ||
+ | humifd = open(si702x_node[1], O_RDONLY); | ||
+ | read(humifd, humidity, 10); | ||
+ | close(tempfd); | ||
+ | close(humifd); | ||
+ | } | ||
+ | |||
+ | static void lcd_update (void) | ||
+ | { | ||
+ | int i, j; | ||
+ | |||
+ | memset((void *)&lcdFb, ' ', sizeof(lcdFb)); | ||
+ | |||
+ | getSysInfo(); | ||
+ | sprintf(lcdFb[0], "TEMP : %.1f *C", atof(temperature)); | ||
+ | sprintf(lcdFb[1], "HUMI : %.2f %%", atof(humidity)); | ||
+ | |||
+ | lcdFb[0][strlen(lcdFb[0])] = ' '; | ||
+ | lcdFb[1][strlen(lcdFb[1])] = ' '; | ||
+ | |||
+ | for(i = 0; i < LCD_ROW; i++) { | ||
+ | lcdPosition (lcdHandle, 0, i); | ||
+ | for(j = 0; j < LCD_COL; j++) | ||
+ | lcdPutchar(lcdHandle, lcdFb[i][j]); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int system_init(void) | ||
+ | { | ||
+ | int i, j; | ||
+ | |||
+ | // LCD Init | ||
+ | lcdHandle = lcdInit(LCD_ROW, LCD_COL, LCD_BUS, | ||
+ | PORT_LCD_RS, PORT_LCD_E, | ||
+ | PORT_LCD_D4, PORT_LCD_D5, | ||
+ | PORT_LCD_D6, PORT_LCD_D7, 0, 0, 0, 0); | ||
+ | |||
+ | if(lcdHandle < 0) { | ||
+ | fprintf(stderr, "%s : lcdInit failed!\n", __func__); | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | int main (int argc, char *argv[]) | ||
+ | { | ||
+ | int timer = 0 ; | ||
+ | |||
+ | wiringPiSetup (); | ||
+ | |||
+ | if (system_init() < 0) { | ||
+ | fprintf (stderr, "%s: System Init failed\n", __func__); | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | for(;;) { | ||
+ | usleep(100000); | ||
+ | if (millis () < timer) | ||
+ | continue ; | ||
+ | timer = millis () + LCD_UPDATE_PERIOD; | ||
+ | |||
+ | // lcd update | ||
+ | lcd_update(); | ||
+ | } | ||
+ | |||
+ | return 0 ; | ||
+ | } | ||
+ | </file> |