/* PIC_i2c_send_data_to_to_plot_ram.c Send data from master to graphic display on PIC (8 command numbers followed by data read from master's file) ------------------------------------------------------------ usage :- type the following arguments after the program name eg ./PIC_i2c_send_data_to_to_plot 81 file 0 0 50 1 1 0 0 0 0 arguments - 1 = PIC address (0xA0 on PIC is 80 on Linux master, 0xA2 is 81) 2 = spare for file name? 3 = number in master's data file to start reading (list_start) - send these to PIC - 4 = pic_command[0] = PIC display starting point on x axis 5 = pic_command[1] = number of data points sent to be displayed 6 = pic_command[2] = 1 - wipe this data area before plot, 0 - leave it for overwrite 7 = pic_command[3] = 1 - we WILL plot some data points, 0 - might just switch lines etc (later) 8 = pic_command[4] = 1 - clear screen, 0 leave it alone 9 = pic_command[5] = 0 free 10 = pic_command[6] = 0 free 11 = pic_command[7] = 0 free e.g. ./PIC_i2c_send_data_to_to_plot_ram 81 file x 22 0 1 0 0 x x x means -> at x = 23, wipe local just that point, do not plot, do not clear screen - now send data values for x/y points - */ #include #include #include #include #include #include #define I2C_SLAVE 0x0703 /* Change slave address */ int i2c; int PicData[8]; /* integers sent by PIC to master*/ unsigned char tx_buf[100]; /* bytes to send */ unsigned char rx_buf[8]; /* received bytes */ unsigned long address; /* i2c bus address */ int numbers[100]; int i, j, list_start; int rc; //~?? int main(int argc, char** argv) { if (argc != 12) /* report error if we are not getting just 11 inputs after the program name */ { printf("Error. usage: %s i2c_chip_address then 10 data items\n", argv[0]); } address = atoi(argv[1]); /* i2c decimal address is the first number after the program name */ //? = atoi(argv[2]); //use for data series file name ? list_start = atoi(argv[3]); // point in master's data file to start reading // tx_buf[0-7] are PIC commands[0-7] that come before the data stream tx_buf[0] = atoi(argv[4]); // starting display x value for this data series tx_buf[1] = atoi(argv[5]); // number of points to read from file and for PIC to plot tx_buf[2] = atoi(argv[6]); // 1 - to wipe new data area before plot, 0 - add new points on top of old tx_buf[3] = atoi(argv[7]); // 1 - to plot some points, 0 - address ports etc later? tx_buf[4] = atoi(argv[8]); // 1 - wipe whole screen, 0 keep old data on screen tx_buf[5] = atoi(argv[9]); // 0 - free tx_buf[6] = atoi(argv[10]); // 0 - free tx_buf[7] = atoi(argv[11]); // 0 - free //--------------------------------------------read data points from file starting at point l FILE *file_ptr; file_ptr = fopen("/var/www/ramdisk/AD.txt", "r"); //file_ptr = fopen("/data/plot_data/plot.txt", "r"); if(file_ptr != NULL) { printf("i2c address %Lu \n",address); printf("file opened.\n"); for ( i = 0; !feof(file_ptr); i++ ) fscanf( file_ptr, "%d", &numbers[i] ); printf("total numbers found : %d\n", i); //-------------------------------------------------------------------data now all in numbers[] for( j = 0 ; j < tx_buf[1] ; j++) { tx_buf[j+8] = numbers[j+list_start];//first 8 tx_buf values are commands - numbers follow them } fclose(file_ptr); } else { printf("unable to open file.\n"); return 1;} printf("We are sending stream - :\n"); for( j = 0 ; j < (8+tx_buf[1]) ; j++) { printf("tx_buf[%u] = %d \n",j , tx_buf[j]); // print the numbers we are sending } printf("liststart=%u xstart=%u number=%u wipe=%u plot=%u clear=%u\n", list_start, tx_buf[0], tx_buf[1], tx_buf[2], tx_buf[3], tx_buf[4]); //-----------------------------------------------------------------get lo byte i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */ rc=ioctl(i2c,I2C_SLAVE,address); /* set the i2c chip address */ write(i2c,tx_buf,(8 + tx_buf[1])); /* we send bytes 8 commands + data stream> */ usleep(10); // read(i2c,rx_buf,1); /* rx_buf(0) now contains 2 data byte from PIC */ // usleep(10); // PicData[1] = rx_buf[0]; // printf("lo byte %d \n", PicData[1]); //-----------------------------------------------------------------get hi byte // tx_buf[0] = tx_buf[0] + 1; /* increment x to get hi byte*/ // write(i2c,tx_buf,6); /* we send data again with x incremented */ // usleep(10); // read(i2c,rx_buf,1); /* rx_buf(0) now contains 1 data byte from PIC */ // usleep(10); // PicData[2] = rx_buf[0]; // printf("hi byte %d \n", PicData[2]); i2c = close(i2c); // printf("double byte rebuilt %d \n", PicData[1] + 256*PicData[2]); } // NB x on the display show value sent + 1