Moved EXTRA_DIST outside of conditionals (as already happened to hotplug)
[comedilib.git] / demo / eeprom_dump.c
1 /*
2    Dumps eeproms
3  */
4
5 #include <stdio.h>
6 #include <comedilib.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <getopt.h>
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include "examples.h"
14
15 int read_eeprom(comedi_t *it,unsigned int **eeprom, struct parsed_options options);
16 void dump_eeprom(unsigned int *eeprom,int len);
17
18 comedi_t *device;
19
20 int main(int argc, char *argv[])
21 {
22         int len;
23         unsigned int *eeprom;
24         struct parsed_options options;
25
26         init_parsed_options(&options);
27         options.subdevice = -1;
28         parse_options(&options, argc, argv);
29
30         device = comedi_open(options.filename);
31         if(!device){
32                 comedi_perror(options.filename);
33                 exit(-1);
34         }
35
36         len = read_eeprom(device, &eeprom, options);
37         dump_eeprom(eeprom,len);
38
39         return 0;
40 }
41
42
43
44
45 int read_eeprom(comedi_t *it, unsigned int **eeprom, struct parsed_options options)
46 {
47         int n,i,ret;
48         lsampl_t data;
49         unsigned int *ptr;
50         lsampl_t maxdata;
51
52         if(options.subdevice < 0)
53         {
54                 options.subdevice = comedi_find_subdevice_by_type(it, COMEDI_SUBD_MEMORY, 0);
55                 if(options.subdevice < 0){
56                         fprintf(stderr,"No memory subdevice\n");
57                         return 0;
58                 }
59         }
60
61         n = comedi_get_n_channels(it, options.subdevice);
62         maxdata = comedi_get_maxdata(it, options.subdevice, 0);
63
64         if(maxdata != 0xff){
65                 fprintf(stderr,"Demo only supports 8-bit memory subdevice has strange maxdata, aborting\n");
66                 exit(-1);
67         }
68
69         ptr = malloc(sizeof(unsigned int) * n);
70
71         for(i = 0; i < n; i++){
72                 ret = comedi_data_read(it, options.subdevice, i, 0, 0, &data);
73                 ptr[i] = data;
74                 if(ret < 0){
75                         comedi_perror("comedi_data_read");
76                         return 0;
77                 }
78         }
79
80         *eeprom=ptr;
81         return n;
82
83 }
84
85 void dump_eeprom(unsigned int *eeprom,int len)
86 {
87         int i, j, c;
88
89         for (i = 0; i < len - 16; i+=16) {
90                 printf("%04x: ",i);
91                 for (j = 0; j < 16; j++) {
92                         printf("%02x", eeprom[i + j] & 0xff);
93                 }
94                 printf("  ");
95                 for (j = 0; j < 16; j++) {
96                         c = eeprom[i + j] & 0xff;
97                         printf("%c", isprint(c) ? c : '.');
98                 }
99                 printf("\n");
100         }
101         if(i==len)return;
102         printf("%04x: ",i);
103         for (j = 0; j < len-i; j++) {
104                 printf("%02x", eeprom[i + j] & 0xff);
105         }
106         for(;j<16;j++){
107                 printf("  ");
108         }
109         printf("  ");
110         for (j = 0; j < len-i; j++) {
111                 c = eeprom[i + j] & 0xff;
112                 printf("%c", isprint(c) ? c : '.');
113         }
114         for(;j<16;j++){
115                 printf(" ");
116         }
117         printf("\n");
118 }
119