]> bicyclesonthemoon.info Git - staf/libstaf/blob - test.c
fix limit check in calculation encoding
[staf/libstaf] / test.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <time.h>
4
5 #include "libstaf.h"
6
7 //just a test of all functions...
8
9 int main (int argc, char *argv[])
10 {
11         int fd;
12         int a;
13         
14         uint8_t cs,second,minute,hour,day,month;
15         uint16_t year;
16         uint8_t event=0;
17         uint8_t mode;
18         uint8_t running;
19         uint8_t alarm;
20         uint8_t value[3]={2,1,0};
21         uint8_t swminor,swmajor;
22         uint8_t pinout[8];
23         uint32_t display;
24         uint8_t digit;
25         float f;
26         uint8_t error;
27         int16_t calibration;
28         
29         time_t t;
30         struct tm *now;
31         
32         
33         fd = stafConnect("/dev/ttyS0");
34         printf("Connect: %d\n",fd);
35         
36         if(fd<0)
37                 return fd;
38         
39         a = stafSetBuzzerMode(fd, NULL, buzzer_disabled);
40         printf("Set buzzer: %d\n",a);
41         
42         a = stafSetCalibration (fd, NULL, -255, 1);
43         printf("Set calibration: %d\n",a);
44         
45         a = stafGetCalibration (fd, NULL, &calibration, 1);
46         printf("Get calibration: %d\n",a);
47         if(!a)
48                 printf("%hd\n",calibration);
49         
50         a = stafSetCalibration (fd, NULL, 0, 1);
51         printf("Set calibration: %d\n",a);
52         
53         a = stafGetCalibration (fd, NULL, &calibration, 1);
54         printf("Get calibration: %d\n",a);
55         if(!a)
56                 printf("%hd\n",calibration);
57         
58         a = stafGetPinout(fd, NULL, pinout);
59         printf("Get pinout: %d\n",a);
60         if(!a)
61                 printf("%02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n",pinout[0],pinout[1],pinout[2],pinout[3],pinout[4],pinout[5],pinout[6],pinout[7]);
62         
63         a = stafSetDisplay(fd, NULL, pinout,
64                 segment_n3 | segment_ne3 | segment_mid3 | segment_nw3 |
65                 segment_n2 | segment_ne2 | segment_mid2 | segment_nw2);
66         printf("Set display: %d\n",a);
67         usleep(300000);
68         
69         a = stafSetDisplay(fd, NULL, pinout,
70                 segment_n1 | segment_ne1 | segment_mid1 | segment_nw1 |
71                 segment_n2 | segment_ne2 | segment_mid2 | segment_nw2);
72         printf("Set display: %d\n",a);
73         usleep(300000);
74         
75         a = stafSetDisplay(fd, NULL, pinout,
76                 segment_n1 | segment_ne1 | segment_mid1 | segment_nw1 |
77                 segment_n0 | segment_ne0 | segment_mid0 | segment_nw0);
78         printf("Set display: %d\n",a);
79         usleep(300000);
80         
81         a = stafSetDisplay(fd, NULL, pinout,
82                 segment_s1 | segment_se1 | segment_mid1 | segment_sw1 |
83                 segment_s0 | segment_se0 | segment_mid0 | segment_sw0);
84         printf("Set display: %d\n",a);
85         usleep(300000);
86         
87         a = stafSetDisplay(fd, NULL, pinout,
88                 segment_s1 | segment_se1 | segment_mid1 | segment_sw1 |
89                 segment_s2 | segment_se2 | segment_mid2 | segment_sw2 |
90                 dot_alarm);
91         printf("Set display: %d\n",a);
92         usleep(300000);
93         
94         a = stafSetDisplay(fd, NULL, pinout,
95                 segment_s3 | segment_se3 | segment_mid3 | segment_sw3 |
96                 segment_s2 | segment_se2 | segment_mid2 | segment_sw2 |
97                 dot_alarm);
98         printf("Set display: %d\n",a);
99         usleep(300000);
100         
101         
102         a = stafFreeDisplay(fd, NULL);
103         printf("Free display: %d\n",a);
104         
105         a = stafSetTime(fd, NULL, 45, 23, 0);
106         printf("Set time: %d\n",a);
107         
108         a = stafSetDate(fd, NULL, 12, 3, 1);
109         printf("Set date: %d\n",a);
110         
111         a = stafSetYear(fd, NULL, 1998, 1);
112         printf("Set year: %d\n",a);
113         
114         a = stafGetTime(fd, NULL, &cs, &second, &minute, &hour, &day, &month, &year);
115         printf("Get time: %d\n",a);
116         if(!a)
117                 printf("%02hhu:%02hhu:%02hhu.%02hhu, %02hhu.%02hhu.%04hu\n",hour,minute,second,cs,day,month,year);
118         
119         a = stafGetMode(fd, NULL, &mode);
120         printf("Get mode: %d\n",a);
121         printf("%02hhx\n",mode);
122         
123         a = stafSetValue(fd, NULL, value);
124         printf("Set value: %d\n",a);
125         
126         a = stafGetValue(fd, NULL, value);
127         printf("Get value: %d\n",a);
128         if(!a)
129                 printf("%02hhx %02hhx %02hhx\n",value[0],value[1],value[2]);
130         
131         a = stafSetCountdown (fd, NULL, 2, 0, 0);
132         printf("Set countdown: %d\n",a);
133         
134         a = stafIsRunning(fd, NULL, &running);
135         printf("Is running: %d\n",a);
136         if(!a)
137                 printf("%s\n",running?"run":"stop");
138         
139         a = stafSetRunning(fd, NULL, !running);
140         printf("Set running: %d\n",a);
141         
142         a = stafIsRunning(fd, NULL, &running);
143         printf("Is running: %d\n",a);
144         if(!a)
145                 printf("%s\n",running?"run":"stop");
146         
147         a = stafGetVersion(fd, NULL, &swminor, &swmajor);
148         printf("Get version: %d\n",a);
149         if(!a)
150                 printf("%hhu.%hhu\n",swmajor,swminor);
151         
152         a = stafGetDisplay(fd, NULL, pinout, &display);
153         printf("Get display: %d\n",a);
154         if(!a)
155         {
156                 printf("    %c   %c     %c   %c\n",(display&segment_n3)?'-':' ',(display&segment_n2)?'-':' ',(display&segment_n1)?'-':' ',(display&segment_n0)?'-':' ');
157                 printf("   %c %c %c %c %c %c %c %c %c\n",(display&segment_nw3)?'|':' ',(display&segment_ne3)?'|':' ',(display&segment_nw2)?'|':' ',(display&segment_ne2)?'|':' ',(display&dot_top)?'.':' ',(display&segment_nw1)?'|':' ',(display&segment_ne1)?'|':' ',(display&segment_nw0)?'|':' ',(display&segment_ne0)?'|':' ');
158                 printf("    %c   %c     %c   %c\n",(display&segment_mid3)?'-':' ',(display&segment_mid2)?'-':' ',(display&segment_mid1)?'-':' ',(display&segment_mid0)?'-':' ');
159                 printf(" %c %c %c %c %c %c %c %c %c %c\n",(display&dot_alarm)?'.':' ',(display&segment_sw3)?'|':' ',(display&segment_se3)?'|':' ',(display&segment_sw2)?'|':' ',(display&segment_se2)?'|':' ',(display&dot_bottom)?'.':' ',(display&segment_sw1)?'|':' ',(display&segment_se1)?'|':' ',(display&segment_sw0)?'|':' ',(display&segment_se0)?'|':' ');
160                 printf("    %c   %c     %c   %c\n",(display&segment_s3)?'-':' ',(display&segment_s2)?'-':' ',(display&segment_s1)?'-':' ',(display&segment_s0)?'-':' ');
161                 printf("%08lx\n",display);
162         }
163         
164         a = stafSubscribeEvent (fd, &event, event_tick | event_button_c);
165         printf("Sub. event: %d\n",a);
166         
167         a = stafTriggerEvent (fd, &event, event_button_c);
168         printf("Trigger event: %d\n",a);
169         
170         a = stafSubscribeEvent (fd, &event, 0);
171         printf("Unsub. event: %d\n",a);
172         if(!a)
173                 printf("%hhx\n",event);
174         
175         a = stafSetDigit(fd, NULL, 2);
176         printf("Set digit: %d\n",a);
177         
178         a = stafGetDigit(fd, NULL, &digit);
179         printf("Get digit: %d\n",a);
180         if(!a)
181                 printf("%hhu\n",digit);
182         
183         a = stafSetDigitValue(fd, NULL, 1);
184         printf("Set digit value: %d\n",a);
185         
186         a = stafGetDigitValue (fd, NULL, &digit);
187         printf("Get digit value: %d\n",a);
188         if(!a)
189                 printf("%hhu\n",digit);
190         
191         sleep(2);
192         
193         a = stafConfirm(fd, NULL);
194         printf("Confirm: %d\n",a);
195         
196                 a = stafSetAlarm(fd, NULL, 34, 13, 1);
197         printf("Set alarm: %d\n",a);
198         
199         a = stafGetAlarm(fd, NULL, &alarm, &minute, &hour);
200         printf("Get alarm: %d\n",a);
201         if(!a)
202                 printf("%hhu %hhu:%hhu\n",alarm,hour,minute);
203         
204         a = stafClearAlarm(fd, NULL, 1);
205         printf("Clear alarm: %d\n",a);
206         
207         a = stafGetAlarm(fd, NULL, &alarm, &minute, &hour);
208         printf("Get alarm: %d\n",a);
209         if(!a)
210                 printf("%hhu %hhu:%hhu\n",alarm,hour,minute);
211         
212         a = stafCalculate(fd, NULL, -10.0, 0.12, calc_multiply, &f, &error, 1);
213         printf("Calculate: %d\n",a);
214         if(!a)
215                 printf("%s %f\n",error?"ERR":"OK",f);
216         
217         a = stafCalculate(fd, NULL, 1234.0, 0.0, calc_divide, &f, &error, 1);
218         printf("Calculate: %d\n",a);
219         if(!a)
220                 printf("%s %f\n",error?"ERR":"OK",f);
221         
222         a = stafSetDebugValue(fd, NULL, 0x31, 0xee, 1);
223         printf("Set dbg: %d\n",a);
224         
225         a = stafGetDebugValue(fd, NULL, 0x31, &digit, 1);
226         printf("Get dbg: %d\n",a);
227         if(!a)
228                 printf("%hhx\n",digit);
229         
230         
231         a = stafSetMode(fd, NULL, mode_alarm);
232         printf("Alarm: %d\n",a);
233         
234         a = stafWriteMemory(fd, NULL, 0x022f, 0x02);
235         printf("Write memory: %d\n",a);
236         
237         a = stafReadMemory(fd, NULL, 0x022f, &digit); //&digit == 0x022f in sw 2.1
238         printf("Read memory: %d\n",a);
239         if(!a)
240                 printf("%hhx\n",digit);
241         
242         //ok now clean up the time:
243         
244         time (&t);
245         now = localtime (&t);
246         
247         stafSetTime(fd, NULL, now->tm_min, now->tm_hour, 0);
248         stafSetDate(fd, NULL, now->tm_mday, now->tm_mon+1, 0);
249         stafSetYear(fd, NULL, now->tm_year+1900, 0);
250         stafClearAlarm(fd, NULL, 0);
251         
252         // usleep(3000000);
253         
254         a = stafSetMode(fd, NULL, mode_reset);
255         printf("Reset: %d\n",a);
256         
257         
258         a = stafDisconnect(fd);
259         printf("Disconnect: %d\n",a);
260         
261         return 0;
262 }