Optionally measure ADC2 along with temperature
[contiki-2.x.git] / apps / calc / calc.c
blobf1fc27d6c14b0fa7a1abd984355b74176ac85a5a
1 /*
2 * Copyright (c) 2003, Adam Dunkels.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * This an example program for the Contiki desktop OS
32 * $Id: calc.c,v 1.5 2008/02/08 22:48:47 oliverschmidt Exp $
36 #include <stddef.h>
38 #include "contiki.h"
39 #include "ctk/ctk.h"
41 static struct ctk_window window;
43 static char input[16];
44 static struct ctk_label inputlabel =
45 {CTK_LABEL(0, 0, 12, 1, input)};
47 static struct ctk_button button7 =
48 {CTK_BUTTON(0, 3, 1, "7")};
49 static struct ctk_button button8 =
50 {CTK_BUTTON(3, 3, 1, "8")};
51 static struct ctk_button button9 =
52 {CTK_BUTTON(6, 3, 1, "9")};
53 static struct ctk_button button4 =
54 {CTK_BUTTON(0, 4, 1, "4")};
55 static struct ctk_button button5 =
56 {CTK_BUTTON(3, 4, 1, "5")};
57 static struct ctk_button button6 =
58 {CTK_BUTTON(6, 4, 1, "6")};
59 static struct ctk_button button1 =
60 {CTK_BUTTON(0, 5, 1, "1")};
61 static struct ctk_button button2 =
62 {CTK_BUTTON(3, 5, 1, "2")};
63 static struct ctk_button button3 =
64 {CTK_BUTTON(6, 5, 1, "3")};
65 static struct ctk_button button0 =
66 {CTK_BUTTON(0, 6, 3, " 0 ")};
68 static struct ctk_button cbutton =
69 {CTK_BUTTON(0, 2, 1, "C")};
71 static struct ctk_button divbutton =
72 {CTK_BUTTON(9, 2, 1, "/")};
73 static struct ctk_button mulbutton =
74 {CTK_BUTTON(9, 3, 1, "*")};
76 static struct ctk_button subbutton =
77 {CTK_BUTTON(9, 4, 1, "-")};
78 static struct ctk_button addbutton =
79 {CTK_BUTTON(9, 5, 1, "+")};
80 static struct ctk_button calcbutton =
81 {CTK_BUTTON(9, 6, 1, "=")};
83 PROCESS(calc_process, "Calculator");
85 AUTOSTART_PROCESSES(&calc_process);
87 static unsigned long operand1, operand2;
88 static unsigned char op;
89 #define OP_ADD 1
90 #define OP_SUB 2
91 #define OP_MUL 3
92 #define OP_DIV 4
94 /*-----------------------------------------------------------------------------------*/
95 static void
96 calc_quit(void)
98 process_exit(&calc_process);
99 LOADER_UNLOAD();
101 /*-----------------------------------------------------------------------------------*/
102 static void
103 add_to_input(char c)
105 unsigned char i;
107 for(i = 0; i < 11; ++i) {
108 input[i] = input[i + 1];
110 input[11] = c;
112 /*-----------------------------------------------------------------------------------*/
113 static void
114 clear_input(void)
116 unsigned char i;
118 for(i = 0; i < sizeof(input); ++i) {
119 input[i] = ' ';
122 /*-----------------------------------------------------------------------------------*/
123 static void
124 input_to_operand1(void)
126 unsigned int m;
127 unsigned char i;
129 operand1 = 0;
130 for(m = 1, i = 11;
131 i > 7; --i, m *= 10) {
132 if(input[i] >= '0' &&
133 input[i] <= '9') {
134 operand1 += (input[i] - '0') * m;
137 clear_input();
139 /*-----------------------------------------------------------------------------------*/
140 static void
141 operand2_to_input(void)
143 unsigned char i;
145 input[7] = (char)((operand2/10000) % 10) + '0';
146 input[8] = (char)((operand2/1000) % 10) + '0';
147 input[9] = (char)((operand2/100) % 10) + '0';
148 input[10] = (char)((operand2/10) % 10) + '0';
149 input[11] = (char)(operand2 % 10) + '0';
151 for(i = 0; i < 4; ++i) {
152 if(input[7 + i] == '0') {
153 input[7 + i] = ' ';
154 } else {
155 break;
159 /*-----------------------------------------------------------------------------------*/
160 static void
161 calculate(void)
163 operand2 = operand1;
164 input_to_operand1();
165 switch(op) {
166 case OP_ADD:
167 operand2 = operand2 + operand1;
168 break;
169 case OP_SUB:
170 operand2 = operand2 - operand1;
171 break;
172 case OP_MUL:
173 operand2 = operand2 * operand1;
174 break;
175 case OP_DIV:
176 operand2 = operand2 / operand1;
177 break;
179 operand2_to_input();
181 /*-----------------------------------------------------------------------------------*/
182 PROCESS_THREAD(calc_process, ev, data)
184 PROCESS_BEGIN();
186 ctk_window_new(&window, 12, 7, "Calc");
188 CTK_WIDGET_ADD(&window, &inputlabel);
189 CTK_WIDGET_SET_FLAG(&inputlabel, CTK_WIDGET_FLAG_MONOSPACE);
191 CTK_WIDGET_ADD(&window, &cbutton);
193 CTK_WIDGET_ADD(&window, &divbutton);
195 CTK_WIDGET_ADD(&window, &button7);
196 CTK_WIDGET_ADD(&window, &button8);
197 CTK_WIDGET_ADD(&window, &button9);
199 CTK_WIDGET_ADD(&window, &mulbutton);
203 CTK_WIDGET_ADD(&window, &button4);
204 CTK_WIDGET_ADD(&window, &button5);
205 CTK_WIDGET_ADD(&window, &button6);
207 CTK_WIDGET_ADD(&window, &subbutton);
209 CTK_WIDGET_ADD(&window, &button1);
210 CTK_WIDGET_ADD(&window, &button2);
211 CTK_WIDGET_ADD(&window, &button3);
213 CTK_WIDGET_ADD(&window, &addbutton);
215 CTK_WIDGET_ADD(&window, &button0);
217 CTK_WIDGET_ADD(&window, &calcbutton);
219 clear_input();
221 ctk_window_open(&window);
223 while(1) {
225 PROCESS_WAIT_EVENT();
227 if(ev == ctk_signal_keypress) {
228 if((char)(size_t)data >= '0' &&
229 (char)(size_t)data <= '9') {
230 add_to_input((char)(size_t)data);
231 } else if((char)(size_t)data == ' ') {
232 clear_input();
233 } else if((char)(size_t)data == '+') {
234 input_to_operand1();
235 op = OP_ADD;
236 } else if((char)(size_t)data == '-') {
237 input_to_operand1();
238 op = OP_SUB;
239 } else if((char)(size_t)data == '*') {
240 input_to_operand1();
241 op = OP_MUL;
242 } else if((char)(size_t)data == '/') {
243 input_to_operand1();
244 op = OP_DIV;
245 } else if((char)(size_t)data == '=' ||
246 (char)(size_t)data == CH_ENTER) {
247 calculate();
250 CTK_WIDGET_REDRAW(&inputlabel);
251 } else if(ev == ctk_signal_button_activate) {
252 if(data == (process_data_t)&button0) {
253 add_to_input('0');
254 } else if(data == (process_data_t)&button1) {
255 add_to_input('1');
256 } else if(data == (process_data_t)&button2) {
257 add_to_input('2');
258 } else if(data == (process_data_t)&button3) {
259 add_to_input('3');
260 } else if(data == (process_data_t)&button4) {
261 add_to_input('4');
262 } else if(data == (process_data_t)&button5) {
263 add_to_input('5');
264 } else if(data == (process_data_t)&button6) {
265 add_to_input('6');
266 } else if(data == (process_data_t)&button7) {
267 add_to_input('7');
268 } else if(data == (process_data_t)&button8) {
269 add_to_input('8');
270 } else if(data == (process_data_t)&button9) {
271 add_to_input('9');
272 } else if(data == (process_data_t)&cbutton) {
273 clear_input();
274 } else if(data == (process_data_t)&calcbutton) {
275 calculate();
276 } else if(data == (process_data_t)&addbutton) {
277 input_to_operand1();
278 op = OP_ADD;
279 } else if(data == (process_data_t)&subbutton) {
280 input_to_operand1();
281 op = OP_SUB;
282 } else if(data == (process_data_t)&mulbutton) {
283 input_to_operand1();
284 op = OP_MUL;
285 } else if(data == (process_data_t)&divbutton) {
286 input_to_operand1();
287 op = OP_DIV;
289 CTK_WIDGET_REDRAW(&inputlabel);
290 } else if(ev == ctk_signal_window_close &&
291 data == (process_data_t)&window) {
292 calc_quit();
295 PROCESS_END();
297 /*-----------------------------------------------------------------------------------*/