Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <fcntl.h>
00003 #include <unistd.h>
00004 #include <sys/ioctl.h>
00005 #include <linux/i2c-dev.h>
00006 #include <ncurses.h>
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #define PCF8591_I2C_ADDR 0x48
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 int main(int argc, char *argv[])
00057 {
00058 int i;
00059 int r;
00060 int handle;
00061 char aout;
00062 unsigned char command[2];
00063 unsigned char value[4];
00064 unsigned char str[8];
00065
00066 int j;
00067 int key;
00068
00069 if (gpioInitialise() < 0) return 1;
00070
00071 initscr();
00072 noecho();
00073 cbreak();
00074 nodelay(stdscr, true);
00075 curs_set(0);
00076
00077 printw("PCF8591 + or - to change aout, any other key to quit.");
00078
00079 mvaddstr(10, 0, "Brightness");
00080 mvaddstr(12, 0, "Temperature");
00081 mvaddstr(14, 0, "?");
00082 mvaddstr(16, 0, "Resistor");
00083
00084 refresh();
00085
00086 handle = i2cOpen(1, PCF8591_I2C_ADDR, 0);
00087
00088 command[1] = 0;
00089 aout = 128;
00090
00091 while (1)
00092 {
00093 for (i=0; i<4; i++)
00094 {
00095 command[1] = aout;
00096 command[0] = 0x40 | ((i + 1) & 0x03);
00097
00098 i2cWriteDevice(handle, &command, 2);
00099
00100 usleep(20000);
00101
00102
00103 value[i] = i2cReadByte(handle);
00104
00105 sprintf(str, "%3d", value[i]);
00106 mvaddstr(10+i+i, 12, str);
00107 value[i] = value[i] / 4;
00108 move(10 + i + i, 16);
00109
00110 for(j = 0; j < 64; j++)
00111 if(j < value[i]) addch('*'); else addch(' ');
00112 }
00113
00114 refresh();
00115
00116 key = getch();
00117
00118 if ((key == '+') || (key == '=')) aout++;
00119 else if ((key == '-') || (key == '_')) aout--;
00120 else if (key != -1) break;
00121 }
00122
00123 endwin();
00124
00125 i2cClose(handle);
00126
00127 gpioTerminate();
00128
00129 return (0);
00130 }
00131