/* * RaspberryPi system shutdown * By Boguslaw Kempny kempny@stanpol.com.pl * * LCD display procedures based on i2clcd.txt * by Lewis Loflin www.bristolwatch.com lewis@bvu.net * http://www.bristolwatch.com/rpi/i2clcd.htm * Using wiringPi by Gordon Henderson * * There is no warrenty of any kind use at your own risk. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #define WEWY 25 // connect reset switch to this pin and GND #define I2C_ADDR 0x27 // I2C device address #define LCD_CHR 1 // Mode - Sending data #define LCD_CMD 0 // Mode - Sending command #define ENABLE 0b00000100 // Enable bit static sigset_t signal_mask; /* signals to block */ struct timespec tim; int fdlcd; int sleep_time; int konvline[4]={0x80, 0xC0, 0x94, 0xd4}; //addr. of 1, 2, 3 and 4 line on LCD int lcd_backlight = 0x08; void Delay_ms(int microsec); void lcd_init(void); void lcd_byte(int bits, int mode); void lcd_toggle_enable(int bits); void lcdLoc(int col, int line); //move cursor void typeln(const char *s); void typeChar(char val); /* S T A R T */ void main (argc, argv) int argc; char *argv[]; { // check wiringPi setup if (wiringPiSetup() == -1) { printf("wiringPi-Error\n"); exit(1); } // set reset pin to input, activate pull-up resistor pinMode (WEWY , INPUT) ; pullUpDnControl(WEWY, PUD_UP); int state; int count; while (TRUE) { state=digitalRead(WEWY); if (state == 0) // WEWY pin connected to GND { for(count=30;count>0;count--) //WEWY pin connected to GND for 5 seconds { state=digitalRead(WEWY); if(state == 1) break; Delay_ms(200); } if (state == 0) { system("killall pomiar"); // terminate a process sleep(1); //**** initiate LCD Display fdlcd = wiringPiI2CSetup(I2C_ADDR); lcd_init(); // setup LCD lcdLoc (0, 0) ; // set cursor at line 0, column 0 typeln ("Closing the system") ; // display some info on display //***** mask signals to avoid premature closure of our process sigemptyset (&signal_mask); sigaddset (&signal_mask, SIGINT); sigaddset (&signal_mask, SIGTERM); sigaddset (&signal_mask, SIGHUP); sigprocmask(SIG_BLOCK, &signal_mask, NULL); Delay_ms(900); system("shutdown -h now"); // begin shutdown procedure //**** wait 5 seconds sleep_time = 10; while (sleep_time > 0) sleep_time = sleep (sleep_time); // *** turn off LCD backlight lcd_backlight = 0x00; typeln (" ") ; exit(0); } } } } //***************************************** void Delay_ms(int milisec) { tim.tv_sec = 0; tim.tv_nsec = milisec * 1000000; while(nanosleep(&tim,&tim)==-1) continue; } //************************************* // go to location on LCD void lcdLoc(int col, int line) { lcd_byte(konvline[line] + col, LCD_CMD); } //************************************* // out char to LCD at current position void typeChar(char val) { lcd_byte(val, LCD_CHR); } // this allows use of any size string //************************************* void typeln(const char *s) { while ( *s ) lcd_byte(*(s++), LCD_CHR); } //************************************* void lcd_byte(int bits, int mode) { //Send byte to data pins // bits = the data // mode = 1 for data, 0 for command int bits_high; int bits_low; // uses the two half byte writes to LCD bits_high = mode | (bits & 0xF0) | lcd_backlight ; bits_low = mode | ((bits << 4) & 0xF0) | lcd_backlight ; // High bits wiringPiI2CReadReg8(fdlcd, bits_high); lcd_toggle_enable(bits_high); // Low bits wiringPiI2CReadReg8(fdlcd, bits_low); lcd_toggle_enable(bits_low); } //************************************* void lcd_toggle_enable(int bits) { // Toggle enable pin on LCD display delayMicroseconds(1); wiringPiI2CReadReg8(fdlcd, (bits | ENABLE)); delayMicroseconds(1); wiringPiI2CReadReg8(fdlcd, (bits & ~ENABLE)); delayMicroseconds(1); } //************************************* void lcd_init() { // Initialise display lcd_byte(0x33, LCD_CMD); // Initialise lcd_byte(0x32, LCD_CMD); // Initialise lcd_byte(0x06, LCD_CMD); // Cursor move direction lcd_byte(0x0C, LCD_CMD); // 0x0F On, Blink Off lcd_byte(0x28, LCD_CMD); // Data length, number of lines, font size lcd_byte(0x01, LCD_CMD); // Clear display delayMicroseconds(500); }