/* compile: cc -o pcf8591 pcf8591.c public domain by Shaddack do what you wish */ #include #include #include #include #include #include #include #define BASE_ADDR 0x48 // 0b 100 1xxx xxx=A2,A1,A0 #define I2C_DEVICE "/dev/i2c-1" // raspberry pi default int verbose=0; char i2cdevname[256]; int dev_addr=BASE_ADDR; int pcfdac=-1; int enabledac=0; int pcfaddr=0; int pcfmux=0; int outjson=0; int outhex=0; int outchan=-1; void pcf_setaddr(){ if(pcfaddr<0)pcfaddr=0; if(pcfaddr>7)pcfaddr=7; dev_addr=BASE_ADDR+pcfaddr; } void pcf_i2cwrite(int file,int cfg,int dac){ char buf[2]; int cnt=1; if(dac>=0)cnt=2; // if(dac>255)dac=255; buf[0]=cfg; buf[1]=dac; if(verbose){fprintf(stderr,"raw write:");for(int t=0;t3)outchan&=3; if(outchan<0){ for(int t=0;t<4;t++){ if(t)printf(" "); outnum(buf[t+2]); } } else outnum(buf[outchan+2]); printf("\n"); } void help(){ printf("PCF8591 sensor read\n"); printf("Usage: pcf8591 [-v] [-x] [-O] [-o ] [-c ] [-a ] [-d ]\n"); printf("ADC:\n"); printf(" -c display ADC channel (0..3), all if not specified\n"); printf(" -m set the input mux (0..3) (bits 4,5 of config; default 0, four separate inputs)\n"); printf(" -x hex output\n"); printf(" -j json format\n"); printf("DAC:\n"); printf(" -O force DAC output (do not unset when DAC value not specified)\n"); printf(" -o set DAC value (0..255)\n"); printf("I2C:\n"); printf(" -a address, 0..7, default=0; adds to base address 0x%02x\n",BASE_ADDR); printf(" -d specify I2C device, default %s\n",I2C_DEVICE); printf("general:\n"); printf(" -v verbose mode\n"); printf(" -h,--help this help\n"); printf("\n"); } int main(int argc,char*argv[]){ float lux; int t; strcpy(i2cdevname,I2C_DEVICE); for(t=1;t