int main(void) { int lcd; //if(wiringPiSetupGpio() == -1) return EXIT_FAIL; lcd = lcd_start(); setbuf(stdout,NULL); pinMode(LSW, INPUT); pinMode(RSW, INPUT); pinMode(TGSW, INPUT); pullUpDnControl(LSW, PUD_UP); pullUpDnControl(RSW, PUD_UP); pullUpDnControl(TGSW, PUD_UP); int curTab=0; int lsw,rsw,tgsw; int tabStat[4]={0}; int occupied=0; char s[100]; // ex) lsw == 0 => switch pushed.. while(1) { delay(100); lsw = digitalRead(LSW); rsw = digitalRead(RSW); tgsw = digitalRead(TGSW); printf("lsw, rsw tgsw :%d %d %d\n",lsw,rsw,tgsw); if(!occupied) { if(lsw == 0) occupied = 1; else if(rsw == 0) occupied = 2; else if(tgsw == 0) occupied = 3; } else { if(lsw== 1 && occupied == 1) occupied = 0; else if(rsw == 1 && occupied == 2) occupied = 0; else if(tgsw == 1 && occupied == 3) occupied = 0; } if(occupied == 1 || occupied == 2) { if(occupied == 1) curTab = (curTab-1+MAXTAB)%MAXTAB; if(occupied == 2) curTab = (curTab+1)%MAXTAB; int ret = get_relay_value(curTab); tabStat[curTab] = ret; printf("ret:%d\n",ret); printf("lcd :%d\n",lcd); sprintf(s,"%dth tab is %3s%18s",curTab,ret?"ON":"OFF",""); lcdPosition(lcd,0,0); lcdPuts(lcd,s); delay(200); } else if(occupied==3) { set_relay_value(curTab,!tabStat[curTab]); tabStat[curTab]= !tabStat[curTab]; sprintf(s,"%dth tab is %3s%18s",curTab, tabStat[curTab]?"ON":"OFF",""); lcdPosition(lcd,0,0); lcdPuts(lcd,s); delay(200); } } return EXIT_SUCC; }
int cls(int fd) { lcdPosition (fd, 0, 0) ; lcdPuts (fd, " ") ; //清空第一行 lcdPosition (fd, 0, 1) ; lcdPuts (fd, " ") ; //清空第二行 return 0; }
int main (int args, char *argv[]) { if (wiringPiSetup () == -1) exit (1) ; int fd = lcdInit (2, 16, 4, RS, EN, D0,D1,D2,D3,D0,D1,D2,D3) ; if (fd == -1) { printf ("lcdInit 1 failed\n") ; return 1 ; } sleep (1) ; //显示屏初始化 lcdPosition (fd, 0, 0); lcdPuts (fd, " Raspberry Pi!"); //启动信息 sleep(1); if(argv[1]) { lcdPosition (fd, 0, 0) ; lcdPuts (fd, " ") ; //清空第一行 lcdPosition (fd, 0, 0) ; lcdPuts (fd, argv[1]) ; //命令行参数显示至第一行 } int start,now; while(1) { show_date(fd); cls(fd); show_sys_info(fd); sleep(5); cls(fd); start = show_run_time(fd); while(now - start < 5){ now = show_run_time(fd); sleep(1); } cls(fd); show_net_info(fd); sleep(5); cls(fd); show_client_count(fd); sleep(3); cls(fd); show_client_info(fd); cls(fd); show_temp(fd); sleep(5); cls(fd); } return 0; }
void main(){ wiringPiSetup(); int fd = lcdInit(2, 16, 4, 6,5, 4,2,1,3, 0,0,0,0); lcdPosition(fd, 0, 0); lcdPuts(fd, "Steve Bertrand"); lcdPosition(fd, 0, 1); lcdPuts(fd, "perlmonks.org"); waitForEnter(); lcdClear(fd); }
int main() { int lcd; wiringPiSetup(); lcd = lcdInit (2, 16, 4, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0); lcdPuts(lcd, "Hello, world!"); }
int show_net_info (int fd) { struct ifaddrs * ifAddrStruct=NULL; void * tmpAddrPtr=NULL; char * s1="eth0"; char buf[60]; lcdPosition (fd, 0, 0); lcdPuts (fd, "Network Info:"); getifaddrs(&ifAddrStruct); while (ifAddrStruct != NULL) { if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { // check it is IP4 sprintf(buf, "%s", ifAddrStruct->ifa_name); if(strcmp(buf, s1) == 0){ // is a valid IP4 Address tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; char addressBuffer[INET_ADDRSTRLEN]; inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); lcdPosition (fd, 0, 1);lcdPrintf (fd, "%s", addressBuffer) ; } } ifAddrStruct=ifAddrStruct->ifa_next; } return 0; }
int show_sys_info (int fd) { FILE *fp; char temp_char[15]; //树莓派温度 char Total[20]; //总内存量 char Free[20]; //空闲内存量 lcdPosition (fd, 0, 0); lcdPuts (fd, "System Info:"); fp=fopen("/sys/class/thermal/thermal_zone0/temp","r"); //读取树莓派温度 fgets(temp_char,9,fp); float Temp; Temp=atof(temp_char)/1000; lcdPosition (fd, 0, 1);lcdPrintf (fd, "%3.1fC", Temp) ; fclose(fp); fp=fopen("/proc/meminfo","r"); //读取内存信息 fgets(Total,19,fp); fgets(Total,4,fp); fgets(Free,9,fp); fgets(Free,19,fp); fgets(Free,4,fp); lcdPosition (fd, 7, 1); lcdPrintf (fd, "%3d/%3dMB", atoi(Free),atoi(Total)) ; fclose(fp); }
void displayString(){ wiringPiSetupGpio(); int fd = lcdInit( //LCDCONFIG //rows 4, //colums 20, //bits 4, //PINLAYOUT //RS (function select 12, //strobe, E (Enable Signal 16, //d0 - d7, the data pints 26,17,21,19,0,0,0,0 //0,0,0,0,5,6,13,19 ); lcdHome(fd); lcdClear(fd); lcdPuts(fd, "GORGABAL"); }
static long write_lcdout(lcdoutRecord *pstrout) { struct Pin_Info *pin_info = pstrout->dpvt; int handle = pin_info->handle; int bits = pin_info->bits; lcdHome(handle); lcdClear(handle); int dirs = pstrout->dirs; if(dirs == 1) { char *val = pstrout->val; lcdPuts(handle, val); // lcdPrintf(handle, "%s", val); } else { double inpa = 0.0; double inpb = 0.0; double inpc = 0.0; double inpd = 0.0; long status; status = dbGetLink(&(pstrout->inpa), DBF_DOUBLE, &inpa, 0, 0); status = dbGetLink(&(pstrout->inpb), DBF_DOUBLE, &inpb, 0, 0); status = dbGetLink(&(pstrout->inpc), DBF_DOUBLE, &inpc, 0, 0); status = dbGetLink(&(pstrout->inpd), DBF_DOUBLE, &inpd, 0, 0); char *stra = pstrout->stra; char *strb = pstrout->strb; char *strc = pstrout->strc; char *strd = pstrout->strd; char *unia = pstrout->unia; char *unib = pstrout->unib; char *unic = pstrout->unic; char *unid = pstrout->unid; lcdPosition(handle, 0,0); lcdPrintf(handle, "%s%.2f%s", stra, inpa, unia); lcdPosition(handle, 0,1); lcdPrintf(handle, "%s%.2f%s", strb, inpb, unib); if(bits > 2) { lcdPosition(handle, 0,2); lcdPrintf(handle, "%s%.2f%s", strc, inpc, unic); lcdPosition(handle, 0,3); lcdPrintf(handle, "%s%.2f%s", strd, inpd, unid); } } return 0; }
//prior.. wiringPiSetup() needed. int lcd_start() { int lcd; //Handle for LCD wiringPiSetup(); //Initialise LCD(int rows, int cols, int bits, int rs, //int enable, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) if ( lcd = lcdInit (2, 16,4, LCD_RS, LCD_E ,LCD_D4 , LCD_D5, LCD_D6,LCD_D7,0,0,0,0) ){ printf ("lcd init failed! \n"); return -1 ; } lcdPosition(lcd,0,0); //Position cursor on the first line in the first column lcdPuts(lcd, "*^^* HELLO THIS IS JOSS TAB CTRL"); /* char s[100]; int i; for(i=0; i<4; i++) { int ret = get_relay_value(i); printf("ret:%d\n",ret); sprintf(s,"[%d]tab status :%d\n",i,ret); lcdPosition(lcd,0,0); lcdPuts(lcd,s); delay(2000); } */ return lcd; }
int lcd_init(){ wiringPiSetupSys () ; mcp23017Setup (AF_BASE, 0x20) ; adafruitLCDSetup (105) ; //setze Farbe lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Warmwasserproben") ; lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " Daten Aufnahme "); sleep (2); lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Start ---> ") ; lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, "Press Select... "); waitForEnter () ; lcdClear (lcdHandle) ; adafruitLCDSetup (103) ; //setze Farbe return(0); }
/* * Function: ioBoardLcdTest * Description: be used to test Lcd interface on the board */ void ioBoardLcdTest(int bits, int rows, int cols) { int lcdHandle; lcdHandle = lcdInit (rows, cols, 8, 21, 23, 0, 1, 2, 3, 4, 5, 6, 7) ; if(lcdHandle < 0) { printf("Lcd init failed!\n"); return; } printf("LCD: the LCD will display character, please view!\n"); lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Lcd Test OK") ; lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, "www.lemaker.org") ; }
int lcd_print(int x,int y,int col,char *str){ //wiringPiSetupSys () ; //mcp23017Setup (AF_BASE, 0x20) ; setBacklightColour (col); //str[16]=0x00; //ende des Strings bei Pos. 16 :) printf("x=%d;y=%d;str='%s'\n",x,y,str); lcdPosition (lcdHandle, x, y) ; lcdPuts (lcdHandle, str) ; return(0); }
/***************************************************************************** * * Description: * Implements a menu * ****************************************************************************/ tU8 drawMenu(tMenu newMenu) { tU8 anyKey; menu = newMenu; //draw boarder lcdRect(menu.xPos, menu.yPos, menu.xLen, menu.yLen, menu.borderColor); lcdRect(menu.xPos+1, menu.yPos+16, menu.xLen-2, menu.yLen-17, menu.bgColor); //write header text lcdGotoxy(menu.headerTextXpos,menu.yPos+1); lcdColor(menu.borderColor,menu.headerColor); lcdPuts(menu.pHeaderText); //write choices cursor = menu.initialChoice; drawMenuCursor(); //dummy call just to reset previous key strokes checkKey(); while(1) { anyKey = checkKey(); if (anyKey != KEY_NOTHING) { //select specific function if (anyKey == KEY_CENTER) { return cursor; } else if (anyKey == KEY_UP) { if (cursor > 0) cursor--; else cursor = menu.noOfChoices - 1; drawMenuCursor(); } else if (anyKey == KEY_DOWN) { if (cursor < menu.noOfChoices - 1) cursor++; else cursor = 0; drawMenuCursor(); } } else osSleep(1); } }
int main(void) { int fd; int i; if (wiringPiSetup() == -1){ exit(1); } fd = lcdInit(2,16,4, 2,3, 6,5,4,1,0,0,0,0); //see /usr/local/include/lcd.h printf("%d", fd); if (fd == -1){ printf("lcdInit 1 failed\n") ; return 1; } sleep(1); lcdClear(fd); lcdPosition(fd, 0, 0); lcdPuts(fd, "Welcom To--->"); lcdPosition(fd, 0, 1); lcdPuts(fd, " sunfounder.com"); sleep(1); lcdClear(fd); while(1){ for(i=0;i<sizeof(Buf)-1;i++){ lcdPosition(fd, i, 1); lcdPutchar(fd, *(Buf+i)); delay(200); } lcdPosition(fd, 0, 1); lcdClear(fd); sleep(0.5); for(i=0; i<16; i++){ lcdPosition(fd, i, 0); lcdPutchar(fd, *(myBuf+i)); delay(100); } } return 0; }
//----------------------------------------------- void scrollMessage(int lcd, int line, int width) { char buf[32]; static int position = 0; strncpy (buf, &message[position], width); buf[width]=0; lcdPosition(lcd, 0,line); lcdPuts(lcd, buf); if(++position == (strlen(message)-width)) position = 0; }
int main(int argc, char const *argv[]) { if(argc > 1) { if(strcmp("-dv", argv[1]) == 0) { debug = 2; printf("RUNNING IN VERBOSE DEBUG MODE.\n"); } if(strcmp("-d", argv[1]) == 0) { debug = 1; printf("RUNNING IN DEBUG MODE.\n"); } } mmap_setup(); gpio_setup(); lcd = getLCD(); lcdClear(lcd); lcdPuts(lcd, " MASTERMIND "); delay(500); printf("\nBeginning Game\n"); int g = game(); if(g) { printf("You win!\n"); // display win LED writePin(YELLOW_LED, HIGH); blink(RED_LED, 3, 300); writePin(YELLOW_LED, LOW); lcdWin(); } else { printf("You Lose, Sorry!\n"); lcdLose(); } delay(5000); lcdClear(lcd); unmap(); return 0; }
/* * Application entry point. */ int main(void) { /* * 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(); /* * Activates the serial driver 2 using the driver default configuration. */ sdStart(&SD2, NULL); /* * This initialization requires the OS already active because it uses delay * APIs inside. */ lcdInit(); lcdCmd(LCD_CLEAR); lcdPuts(LCD_LINE1, " ChibiOS/RT "); lcdPuts(LCD_LINE2, " Hello World! "); /* * Starts the LED blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); while(TRUE) { if (!palReadPad(IOPORT1, PORTA_BUTTON1)) TestThread(&SD2); chThdSleepMilliseconds(500); } }
/***************************************************************************** * * Description: * Draw cursor in main menu * * Params: * [in] cursor - Cursor positions * ****************************************************************************/ static void drawMenuCursor(void) { tU32 row; for(row=0; row<menu.noOfChoices; row++) { lcdGotoxy(menu.xPos+4,menu.yPos+17+(14*row)); if(row == cursor) lcdColor(menu.bgColor+1,menu.selectedColor); else lcdColor(menu.bgColor,menu.choicesColor); lcdPuts(menu.pChoice[row]); } }
static ERL_NIF_TERM lcd_puts_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { int handle, str_len; if (!enif_get_int(env, argv[0], &handle) || !enif_get_int(env, argv[1], &str_len)) { return enif_make_badarg(env); } char str[str_len+1]; if (!enif_get_string(env, argv[2], str, sizeof(str), ERL_NIF_LATIN1)) { return enif_make_badarg(env); } lcdPuts(handle, str); return atom_ok; }
/***************************************************************************** * * Description: * Draw the current score * ****************************************************************************/ static void drawScore(void) { tU8 str[4]; str[0] = score/100 + '0'; str[1] = (score/10)%10 + '0'; str[2] = score%10 + '0'; str[3] = 0; if (str[0] == '0') { str[0] = ' '; if (str[1] == '0') str[1] = ' '; } lcdGotoxy(80,116); lcdPuts(str); }
void scrollMessage (int line, int width) { char buf [32] ; static int position = 0 ; static int timer = 0 ; if (millis () < timer) return ; timer = millis () + 200 ; strncpy (buf, &message [position], width) ; buf [width] = 0 ; lcdPosition (lcdHandle, 0, line) ; lcdPuts (lcdHandle, buf) ; if (++position == (strlen (message) - width)) position = 0 ; }
int show_temp(fd){ lcdPosition (fd, 0, 0); lcdPuts (fd, "Temperature:"); int fdata = -1, ret; char *tmp1, tmp2[10], ch='t'; char dev_name[100] = "/sys/devices/w1_bus_master1/28-000005e38995/w1_slave"; long value; int integer, decimal; char buffer[100]; int i,j; float temp; if ((fdata = open(dev_name, O_RDONLY)) < 0) { //perror("open error"); lcdPosition (fd, 0, 1); lcdPrintf (fd, "open error"); return 0; } ret = read(fdata, buffer, sizeof(buffer)); if (ret < 0) { //perror("read error"); lcdPosition (fd, 0, 1); lcdPrintf (fd, "read error"); exit(1); } tmp1 = strchr(buffer, ch); sscanf(tmp1, "t=%s", tmp2); value = atoi(tmp2); integer = value / 1000; decimal = value % 1000; //printf("temperature is %d.%d\n", integer, decimal); sprintf(buffer, "%d.%d", integer, decimal); temp = atof(buffer); lcdPosition (fd, 0, 1); lcdPrintf (fd, "%.1fC", temp); close(fdata); return 0; }
int show_client_count(int fd) { FILE *fp; fp = popen("hostapd_cli all_sta", "r"); char mid; int i = 0; lcdPosition (fd, 0, 0); lcdPuts (fd, "Client Info:"); while(!feof(fp)) { mid = fgetc(fp);//从txt文本中读取一个字符赋值给mid if(mid == '\n') //如果这个字符为换行符 i++; } //printf("\n行數:\n",i); lcdPosition (fd, 0, 1);lcdPrintf (fd, "Count: %d", (i-1)/8) ; fclose(fp); return 0; }
void scrollMessage(int row, int width, const char* message) { char buf[32] = {0}; memset(buf, 0, sizeof(buf)); static int position = 0; static int timer = 0; if(millis() < timer) { return; } timer = millis() + 200; strncpy(buf, &message[position], width); buf[width] = 0; lcdPosition(lcdHandle, 0, row); lcdPuts(lcdHandle, buf); if (++position == (strlen(message) - width)) { position = 0; } }
/***************************************************************************** * * Description: * Draw game background and game board, initialize all variables * ****************************************************************************/ static void setupGame (tBool drawBoard) { tS32 i,j; //initialize random generator and reset score srand(ms); score = 0; #ifdef INCLUDE_EXAMPLE_GAME_BACKGROUND //draw background picture lcdIcon(0, 0, 130, 130, _brickWall_130x130c[2], _brickWall_130x130c[3], &_brickWall_130x130c[4]); #endif //draw game board if (drawBoard == TRUE) { lcdColor(1, 0xe0); lcdGotoxy(16, 116); lcdPuts(" Score: 0 "); lcdRect(34, 14, 64, 100, 3); lcdRect(36, 16, 60, 96, 0); } //initialise game board for (i=0; i<BOARD_WIDTH; i++) for (j=0; j<BOARD_HEIGHT; j++) board[i][j] = 0; currFigure = rand() % NUM_OF_FIGURE; currXpos = 3; currYpos = 0; insertFigure(currFigure, currXpos, currYpos); lastUpdate = ms; }
int show_run_time (int fd) { lcdPosition (fd, 0, 0); lcdPuts (fd, "Already Run:"); FILE *fp; char buf[50]; char *buf2; int timestamp; fp=fopen("/proc/uptime","r"); //读取内存信息 fgets(buf,sizeof(buf)-1,fp); buf2 = strtok(buf, " "); timestamp = atoi(buf2); int day = timestamp / 86400; int hour = (timestamp % 86400) / 3600; int minute = (timestamp % 3600) / 60; int second = timestamp % 60; lcdPosition (fd, 0, 1); lcdPrintf (fd, "%dD %dh %dm %ds", day, hour, minute, second); fclose(fp); return timestamp; }
void inits() { errno = 0; logfile.open(LOGFILEPATH, std::fstream::out | std::fstream::app); logger << "RasPiProg logging started." << endl; sa.sa_handler = &sighandler; sa.sa_flags = SA_RESTART; sigfillset(&sa.sa_mask); if(sigaction(SIGTERM, &sa, NULL) == -1) { logger << "Error: cannot handle SIGTERM" << endl; } if(sigaction(SIGHUP, &sa, NULL) == -1) { logger << "Error: cannot handle SIGHUP" << endl; } if(sigaction(SIGUSR1, &sa, NULL) == -1) { logger << "Error: cannot handle SIGUSR1" << endl; } if(sigaction(SIGINT, &sa, NULL) == -1) { logger << "Error: cannot handle SIGINT" << endl; } if(wiringPiSetup() == -1) { logger << "WiringPi initialization failed!" << endl; exit(1); } lcdHandle = lcdInit(lcdSettings[0],lcdSettings[1],lcdSettings[2],lcdSettings[3],lcdSettings[4],lcdSettings[5],lcdSettings[6] ,lcdSettings[7],lcdSettings[8],lcdSettings[9],lcdSettings[10],lcdSettings[11],lcdSettings[12]); if(lcdHandle < 0) { logger << "LCD-display initialization failed!" << endl; exit(2); } lcdClear(lcdHandle); lcdPosition(lcdHandle, 0, 0) ; lcdPuts(lcdHandle, "RasPiProg By PTapioK"); sleep(1); lcdClear(lcdHandle); /* int lirc = lirc_init(sqlDBname, 1); if(lirc == -1) { logger << "Lirc initialization failed!" << endl; exit(3); } if(lirc_readconfig(NULL, &config, NULL) == -1) { logger << "Lirc initialization failed!" << endl; exit(4); } // Don't wait IR signals fcntl(lirc, F_SETOWN, getpid()); int flags = fcntl(lirc, F_GETFL, 0); if (flags == -1) deinits(); fcntl(lirc, F_SETFL, flags | O_NONBLOCK); */ sqlCon = mysql_init(NULL); if(sqlCon == NULL) { logger << "MySQL initialization failed!" << endl; exit(5); } if(mysql_real_connect(sqlCon, sqlHost.c_str(), sqlUsername.c_str(), sqlPassword.c_str(), sqlDBname.c_str(), 0, NULL, 0) == NULL) { // Try again if(mysql_real_connect(sqlCon, sqlHost.c_str(), sqlUsername.c_str(), sqlPassword.c_str(), sqlDBname.c_str(), 0, NULL, 0) == NULL) { logger << "MySQL connection initialization failed!" << endl; mysql_close(sqlCon); exit(6); } } if(!TableExists(sqlDBname, "temps")) { if(mysql_query(sqlCon, "CREATE TABLE temps(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT, st TIMESTAMP DEFAULT CURRENT_TIMESTAMP, temp FLOAT)")) { logger << "MySQL error! Errno: " << mysql_errno(sqlCon) << endl; mysql_close(sqlCon); exit(8); } } if(!TableExists(sqlDBname, "humis")) { if(mysql_query(sqlCon, "CREATE TABLE humis(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT, st TIMESTAMP DEFAULT CURRENT_TIMESTAMP, humi FLOAT)")) { logger << "MySQL error! Errno: " << mysql_errno(sqlCon) << endl; mysql_close(sqlCon); exit(8); } } if(!TableExists(sqlDBname, "presss")) { if(mysql_query(sqlCon, "CREATE TABLE presss(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT, st TIMESTAMP DEFAULT CURRENT_TIMESTAMP, press FLOAT)")) { logger << "MySQL error! Errno: " << mysql_errno(sqlCon) << endl; mysql_close(sqlCon); exit(8); } } if(!TableExists(sqlDBname, "latestrecords")) { if(mysql_query(sqlCon, "CREATE TABLE latestrecords(name VARCHAR(30), type VARCHAR(6), st TIMESTAMP DEFAULT CURRENT_TIMESTAMP, data FLOAT, PRIMARY KEY (name, type))")) { logger << "MySQL error! Errno: " << mysql_errno(sqlCon) << endl; mysql_close(sqlCon); exit(8); } } if(mysql_query(sqlCon, "SET CHARACTER SET utf8")) { logger << "MySQL error! Errno: " << mysql_errno(sqlCon) << endl; mysql_close(sqlCon); exit(10); } /* if(mcp3004Setup(BASE, SPI_CHAN) == -1) { logger << "MCP3008 chip initialization failed!" << endl; exit(7); } */ if ((i2cfile = open(I2CBus, O_RDWR)) < 0) { logger << endl; logger << "Failed to open the i2c bus! Errno: " << errno << endl; logger << endl; exit(11); } // Pin 20 is DHT22 power pin. pinMode(20, OUTPUT); digitalWrite(20, HIGH); sleep(5); }
int main (int argc, char *argv[]) { int i ; int lcd ; int bits, rows, cols ; struct tm *t ; time_t tim ; char buf [32] ; if (argc != 4) return usage (argv [0]) ; printf ("Raspberry Pi LCD test\n") ; printf ("=====================\n") ; bits = atoi (argv [1]) ; cols = atoi (argv [2]) ; rows = atoi (argv [3]) ; if (!((rows == 1) || (rows == 2) || (rows == 4))) { fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ; return EXIT_FAILURE ; } if (!((cols == 16) || (cols == 20))) { fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ; return EXIT_FAILURE ; } wiringPiSetup () ; if (bits == 4) lcdHandle = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ; else lcdHandle = lcdInit (rows, cols, 8, 11, 10, 0,1,2,3,4,5,6,7) ; if (lcdHandle < 0) { fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ; return -1 ; } lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ; lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ; waitForEnter () ; if (rows > 1) { lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ; if (rows == 4) { lcdPosition (lcdHandle, 0, 2) ; for (i = 0 ; i < ((cols - 1) / 2) ; ++i) lcdPuts (lcdHandle, "=-") ; lcdPuts (lcdHandle, "=3") ; lcdPosition (lcdHandle, 0, 3) ; for (i = 0 ; i < ((cols - 1) / 2) ; ++i) lcdPuts (lcdHandle, "-=") ; lcdPuts (lcdHandle, "-4") ; } } waitForEnter () ; lcdCharDef (lcdHandle, 2, newChar) ; lcdClear (lcdHandle) ; lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "User Char: ") ; lcdPutchar (lcdHandle, 2) ; lcdCursor (lcdHandle, TRUE) ; lcdCursorBlink (lcdHandle, TRUE) ; waitForEnter () ; lcdCursor (lcdHandle, FALSE) ; lcdCursorBlink (lcdHandle, FALSE) ; lcdClear (lcdHandle) ; for (;;) { scrollMessage (0, cols) ; if (rows == 1) continue ; tim = time (NULL) ; t = localtime (&tim) ; sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ; lcdPosition (lcdHandle, (cols - 8) / 2, 1) ; lcdPuts (lcdHandle, buf) ; if (rows == 2) continue ; sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ; lcdPosition (lcdHandle, (cols - 10) / 2, 2) ; lcdPuts (lcdHandle, buf) ; pingPong (lcd, cols) ; } return 0 ; }
int main (void) { FILE *fp; int fd; struct sysinfo sys_info; char uptimeInfo[15]; char tempInfo[10]; char cpuUsageInfo[10]; char freeRamInfo[10]; char buf[10]; int buf_size = 10; float shiftfloat = (float)(1<<SI_LOAD_SHIFT); if (wiringPiSetupGpio() == -1) return 1 ; fd=lcdInit(2,16,4,RS,EN,DB4,DB5,DB6,DB7,0,0,0,0); pinMode(FAN, OUTPUT) ; while (1) { lcdClear(fd); if(sysinfo(&sys_info) != 0) printf("sysinfo Error\n"); unsigned int days = sys_info.uptime / 86400; unsigned int hours = (sys_info.uptime / 3600) - (days * 24); unsigned int mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60); sprintf(uptimeInfo, "Run %dD%dH%dM", days, hours, mins); if (fp = fopen( "/sys/class/thermal/thermal_zone0/temp", "r")) { fgets(buf, buf_size, fp); fclose(fp); } else printf("fopen Error\n"); unsigned int temperature = atoi(buf)/1000; sprintf(tempInfo, "%d'C",temperature); float cpuUsage = (((float)sys_info.loads[0]) / shiftfloat); sprintf(cpuUsageInfo, "Load%.2f", cpuUsage); unsigned long freeRam = sys_info.freeram / 1024 / 1024; sprintf(freeRamInfo, "RAM%ldMB", freeRam); lcdPosition(fd, 0,0); lcdPuts(fd,uptimeInfo); lcdPosition(fd,12,0); lcdPuts(fd,tempInfo); lcdPosition(fd, 0,1); lcdPuts(fd,cpuUsageInfo); lcdPosition(fd, 8,1); lcdPuts(fd,freeRamInfo); if(temperature > 50) digitalWrite(FAN, 1); else digitalWrite(FAN, 0); delay(1000); } return 0 ; }