/*************************************************************************** * Copyright (C) 02/2012 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include #include #include #include #include #include "event.h" #include "lcd.h" #include "logging.h" #include "pic.h" struct picdev { int fd; struct termios oldtio; int fan_speed; int fan_error; int temperature; struct event_fd *read_event; }; static int pic_close_device(struct picdev *dev) { tcsetattr(dev->fd, TCSANOW, &dev->oldtio); close(dev->fd); return 0; } static int pic_open_device(struct picdev *dev, const char *device) { dev->fd = open(device, O_RDWR | O_NOCTTY); if (dev->fd < 0) { log_print(LOG_ERROR, "%s(): failed to open '%s'", __FUNCTION__, device); return -1; } if (fcntl(dev->fd, F_SETFD, FD_CLOEXEC) < 0) { log_print(LOG_ERROR, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__); close(dev->fd); return -1; } tcgetattr(dev->fd, &dev->oldtio); struct termios newtio; memset(&newtio, 0, sizeof(newtio)); newtio.c_iflag |= IGNBRK; newtio.c_lflag &= ~(ISIG | ICANON | ECHO); newtio.c_cflag = B19200 | CS8 | CLOCAL | CREAD; newtio.c_cc[VMIN] = 1; newtio.c_cc[VTIME] = 0; cfsetospeed(&newtio, B19200); cfsetispeed(&newtio, B19200); int err = tcsetattr(dev->fd, TCSAFLUSH, &newtio); if (err < 0) { log_print(LOG_ERROR, "%s(): failed to set termios", __FUNCTION__); close(dev->fd); return -1; } return 0; } int pic_set_fanspeed(struct picdev *dev, char value) { if (value < PIC_CMD_FANSPEED_0 || value > PIC_CMD_FANSPEED_5) return -1; dev->fan_speed = value - PIC_CMD_FANSPEED_0; write(dev->fd, &value, 1); return 0; } int pic_set_buzzer(struct picdev *dev, char value) { if (value < PIC_CMD_BUZZ_SHORT || value > PIC_CMD_BUZZ_LONG) return -1; write(dev->fd, &value, 1); return 0; } int pic_set_status_led(struct picdev *dev, char value) { if (value < PIC_CMD_STATUSLED_RED_2HZ || value > PIC_CMD_STATUSLED_REDGREEN_1HZ) return -1; write(dev->fd, &value, 1); return 0; } int pic_set_usb_led(struct picdev *dev, char value) { if (value < PIC_CMD_USBLED_ON || value > PIC_CMD_USBLED_OFF) return -1; write(dev->fd, &value, 1); return 0; } static int pic_read_cb(int fd, void *privdata) { struct picdev *dev = (struct picdev *)privdata; unsigned char event; int len = read(dev->fd, &event, 1); if (len < 0) return -1; switch (event) { case PIC_EVENT_FAN1_ERR: pic_set_buzzer(dev, PIC_CMD_BUZZ_SHORT); dev->fan_error = 1; break; case PIC_EVENT_FAN1_OK: dev->fan_error = 0; break; case PIC_EVENT_FAN2_ERR: case PIC_EVENT_FAN2_OK: break; case PIC_EVENT_TEMP_RANGE_MIN ... PIC_EVENT_TEMP_RANGE_MAX: dev->temperature = event - 128; break; case PIC_EVENT_TEMP_WARN: pic_set_buzzer(dev, PIC_CMD_BUZZ_SHORT); dev->temperature = 75; break; case PIC_EVENT_TEMP_CRIT: dev->temperature = 80; break; default: log_print(LOG_DEBUG, "%s(): unknown event 0x%02x", __FUNCTION__, event); break; } return 0; } static int lcdpage_case(struct lcddev *lcd, int event, void *privdata) { struct picdev *dev = (struct picdev *)privdata; switch (event) { case LCDPAGE_EVENT_BUTTON1: return LCDPAGE_COMMAND_NEXT; case LCDPAGE_EVENT_BUTTON2: { int fan_speed = PIC_CMD_FANSPEED_0 + dev->fan_speed +1; if (fan_speed > PIC_CMD_FANSPEED_5) fan_speed = PIC_CMD_FANSPEED_0; pic_set_fanspeed(dev, fan_speed); lcd_set_backlight(lcd, 1); break; } case LCDPAGE_EVENT_ENTER: lcd_set_backlight(lcd, 1); break; case LCDPAGE_EVENT_BACKLIGHT: case LCDPAGE_EVENT_EXIT: return 0; default: break; } char line1_ok[] = "FAN(OK): TEMP:"; char line1_err[] = "FAN(ERR): TEMP:"; char line2[20]; snprintf(line2, sizeof(line2), "[ ] %02d C ", dev->temperature); int i; for (i = 1; i <= dev->fan_speed; i++) { line2[i] = '#'; } lcd_setlines(lcd, (dev->fan_error ? line1_err : line1_ok), line2); return 1000; } struct picdev * pic_open(const char *devicename, struct lcddev *lcd) { struct picdev *dev = malloc(sizeof(struct picdev)); if (dev == NULL) { log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__); return NULL; } memset(dev, 0, sizeof(struct picdev)); if (pic_open_device(dev, devicename) < 0) { free(dev); return NULL; } dev->read_event = event_add_readfd(NULL, dev->fd, pic_read_cb, dev); if (lcd != NULL) { lcd_addpage_cb(lcd, 190, lcdpage_case, dev); } pic_set_fanspeed(dev, PIC_CMD_FANSPEED_1); pic_set_status_led(dev, PIC_CMD_STATUSLED_GREENON); return dev; } void pic_close(struct picdev *dev) { if (dev->read_event != NULL) { event_remove_fd(dev->read_event); dev->read_event = NULL; } pic_close_device(dev); free(dev); }