blob: 603e610f52bc753be8465301b99c49cee12301d7 [file] [log] [blame]
adamdunkelseb0df012003-04-09 19:16:49 +00001/*
2 * Copyright (c) 2003, Adam Dunkels.
3 * All rights reserved.
4 *
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.
adamdunkels06f897e2004-06-06 05:59:20 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkelseb0df012003-04-09 19:16:49 +000015 * products derived from this software without specific prior
16 * written permission.
17 *
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.
29 *
30 * This an example program for the Contiki desktop OS
31 *
oliverschmidt10f4b8d2005-02-07 23:13:00 +000032 * $Id: calc.c,v 1.8 2005/02/07 23:13:00 oliverschmidt Exp $
adamdunkelseb0df012003-04-09 19:16:49 +000033 *
34 */
35
36/* This is an example of how to write programs for Contiki. It
37 displays a window with the famous message "Hello world" and two
38 buttons; one which changes the message slightly and one which quits
39 the application. */
40
adamdunkels04d2e372004-07-04 11:27:07 +000041#include "ek.h"
adamdunkelseb0df012003-04-09 19:16:49 +000042#include "ctk.h"
adamdunkelseb0df012003-04-09 19:16:49 +000043#include "loader.h"
44
45static struct ctk_window window;
46
47static char input[16];
48static struct ctk_label inputlabel =
adamdunkels4e441cb2003-04-24 17:19:23 +000049 {CTK_LABEL(0, 0, 12, 1, input)};
adamdunkelseb0df012003-04-09 19:16:49 +000050
adamdunkels4e441cb2003-04-24 17:19:23 +000051static struct ctk_button button7 =
adamdunkels04d2e372004-07-04 11:27:07 +000052 {CTK_BUTTON(0, 3, 1, "7")};
adamdunkels4e441cb2003-04-24 17:19:23 +000053static struct ctk_button button8 =
adamdunkels04d2e372004-07-04 11:27:07 +000054 {CTK_BUTTON(3, 3, 1, "8")};
adamdunkels4e441cb2003-04-24 17:19:23 +000055static struct ctk_button button9 =
adamdunkels04d2e372004-07-04 11:27:07 +000056 {CTK_BUTTON(6, 3, 1, "9")};
adamdunkels4e441cb2003-04-24 17:19:23 +000057static struct ctk_button button4 =
adamdunkels04d2e372004-07-04 11:27:07 +000058 {CTK_BUTTON(0, 4, 1, "4")};
adamdunkels4e441cb2003-04-24 17:19:23 +000059static struct ctk_button button5 =
adamdunkels04d2e372004-07-04 11:27:07 +000060 {CTK_BUTTON(3, 4, 1, "5")};
adamdunkels4e441cb2003-04-24 17:19:23 +000061static struct ctk_button button6 =
adamdunkels04d2e372004-07-04 11:27:07 +000062 {CTK_BUTTON(6, 4, 1, "6")};
adamdunkels4e441cb2003-04-24 17:19:23 +000063static struct ctk_button button1 =
adamdunkels04d2e372004-07-04 11:27:07 +000064 {CTK_BUTTON(0, 5, 1, "1")};
adamdunkels4e441cb2003-04-24 17:19:23 +000065static struct ctk_button button2 =
adamdunkels04d2e372004-07-04 11:27:07 +000066 {CTK_BUTTON(3, 5, 1, "2")};
adamdunkels4e441cb2003-04-24 17:19:23 +000067static struct ctk_button button3 =
adamdunkels04d2e372004-07-04 11:27:07 +000068 {CTK_BUTTON(6, 5, 1, "3")};
adamdunkels4e441cb2003-04-24 17:19:23 +000069static struct ctk_button button0 =
adamdunkels04d2e372004-07-04 11:27:07 +000070 {CTK_BUTTON(0, 6, 3, " 0 ")};
adamdunkels4e441cb2003-04-24 17:19:23 +000071
72static struct ctk_button cbutton =
adamdunkels04d2e372004-07-04 11:27:07 +000073 {CTK_BUTTON(0, 2, 1, "C")};
adamdunkels4e441cb2003-04-24 17:19:23 +000074
adamdunkelseb0df012003-04-09 19:16:49 +000075static struct ctk_button divbutton =
adamdunkels04d2e372004-07-04 11:27:07 +000076 {CTK_BUTTON(9, 2, 1, "/")};
adamdunkels4e441cb2003-04-24 17:19:23 +000077static struct ctk_button mulbutton =
adamdunkels04d2e372004-07-04 11:27:07 +000078 {CTK_BUTTON(9, 3, 1, "*")};
adamdunkels4e441cb2003-04-24 17:19:23 +000079
80static struct ctk_button subbutton =
adamdunkels04d2e372004-07-04 11:27:07 +000081 {CTK_BUTTON(9, 4, 1, "-")};
adamdunkels4e441cb2003-04-24 17:19:23 +000082static struct ctk_button addbutton =
adamdunkels04d2e372004-07-04 11:27:07 +000083 {CTK_BUTTON(9, 5, 1, "+")};
adamdunkelseb0df012003-04-09 19:16:49 +000084static struct ctk_button calcbutton =
adamdunkels04d2e372004-07-04 11:27:07 +000085 {CTK_BUTTON(9, 6, 1, "=")};
adamdunkelseb0df012003-04-09 19:16:49 +000086
adamdunkels04d2e372004-07-04 11:27:07 +000087EK_EVENTHANDLER(calc_eventhandler, ev, data);
88EK_PROCESS(p, "Calculator", EK_PRIO_NORMAL,
89 calc_eventhandler, NULL, NULL);
90static ek_id_t id = EK_ID_NONE;
91
92/*
adamdunkelseb0df012003-04-09 19:16:49 +000093static DISPATCHER_SIGHANDLER(calc_sighandler, s, data);
94static struct dispatcher_proc p =
adamdunkelsc97a60b2003-04-11 20:12:34 +000095 {DISPATCHER_PROC("Calculator", NULL, calc_sighandler, NULL)};
adamdunkelseb0df012003-04-09 19:16:49 +000096static ek_id_t id;
adamdunkels04d2e372004-07-04 11:27:07 +000097*/
adamdunkelseb0df012003-04-09 19:16:49 +000098
adamdunkelsf180e342003-08-20 20:51:31 +000099static unsigned long operand1, operand2;
adamdunkelseb0df012003-04-09 19:16:49 +0000100static unsigned char op;
101#define OP_ADD 1
102#define OP_SUB 2
103#define OP_MUL 3
104#define OP_DIV 4
105
106/*-----------------------------------------------------------------------------------*/
adamdunkels8bb5cca2003-08-24 22:41:31 +0000107LOADER_INIT_FUNC(calc_init, arg)
adamdunkelseb0df012003-04-09 19:16:49 +0000108{
adamdunkels8bb5cca2003-08-24 22:41:31 +0000109 arg_free(arg);
adamdunkelseb0df012003-04-09 19:16:49 +0000110
111 if(id == EK_ID_NONE) {
adamdunkels04d2e372004-07-04 11:27:07 +0000112 id = ek_start(&p);
113 }
adamdunkelseb0df012003-04-09 19:16:49 +0000114}
115/*-----------------------------------------------------------------------------------*/
116static void
117calc_quit(void)
118{
adamdunkels04d2e372004-07-04 11:27:07 +0000119 ek_exit();
adamdunkelseb0df012003-04-09 19:16:49 +0000120 id = EK_ID_NONE;
121 LOADER_UNLOAD();
adamdunkelseb0df012003-04-09 19:16:49 +0000122}
123/*-----------------------------------------------------------------------------------*/
124static void
adamdunkels04d2e372004-07-04 11:27:07 +0000125add_to_input(char c)
126{
127 unsigned char i;
128
129 for(i = 0; i < 11; ++i) {
130 input[i] = input[i + 1];
131 }
132 input[11] = c;
133}
134/*-----------------------------------------------------------------------------------*/
135static void
136clear_input(void)
137{
138 unsigned char i;
139
140 for(i = 0; i < sizeof(input); ++i) {
141 input[i] = ' ';
142 }
143}
144/*-----------------------------------------------------------------------------------*/
145static void
adamdunkelseb0df012003-04-09 19:16:49 +0000146input_to_operand1(void)
147{
148 unsigned int m;
149 unsigned char i;
150
151 operand1 = 0;
adamdunkels4e441cb2003-04-24 17:19:23 +0000152 for(m = 1, i = 11;
adamdunkels04d2e372004-07-04 11:27:07 +0000153 i > 7; --i, m *= 10) {
adamdunkelseb0df012003-04-09 19:16:49 +0000154 if(input[i] >= '0' &&
155 input[i] <= '9') {
156 operand1 += (input[i] - '0') * m;
157 }
adamdunkelseb0df012003-04-09 19:16:49 +0000158 }
adamdunkels04d2e372004-07-04 11:27:07 +0000159 clear_input();
adamdunkelseb0df012003-04-09 19:16:49 +0000160}
161/*-----------------------------------------------------------------------------------*/
162static void
163operand2_to_input(void)
164{
165 unsigned char i;
166
adamdunkels4e441cb2003-04-24 17:19:23 +0000167 input[7] = (operand2/10000) % 10 + '0';
168 input[8] = (operand2/1000) % 10 + '0';
169 input[9] = (operand2/100) % 10 + '0';
170 input[10] = (operand2/10) % 10 + '0';
171 input[11] = operand2 % 10 + '0';
adamdunkelseb0df012003-04-09 19:16:49 +0000172
173 for(i = 0; i < 4; ++i) {
adamdunkels4e441cb2003-04-24 17:19:23 +0000174 if(input[7 + i] == '0') {
175 input[7 + i] = ' ';
adamdunkelseb0df012003-04-09 19:16:49 +0000176 } else {
177 break;
178 }
179 }
180}
181/*-----------------------------------------------------------------------------------*/
182static void
183calculate(void)
184{
185 operand2 = operand1;
186 input_to_operand1();
187 switch(op) {
188 case OP_ADD:
189 operand2 = operand2 + operand1;
190 break;
191 case OP_SUB:
192 operand2 = operand2 - operand1;
193 break;
194 case OP_MUL:
195 operand2 = operand2 * operand1;
196 break;
197 case OP_DIV:
198 operand2 = operand2 / operand1;
199 break;
200 }
201 operand2_to_input();
202}
203/*-----------------------------------------------------------------------------------*/
adamdunkels04d2e372004-07-04 11:27:07 +0000204EK_EVENTHANDLER(calc_eventhandler, ev, data)
adamdunkelseb0df012003-04-09 19:16:49 +0000205{
adamdunkels04d2e372004-07-04 11:27:07 +0000206 EK_EVENTHANDLER_ARGS(ev, data);
adamdunkelseb0df012003-04-09 19:16:49 +0000207
adamdunkels04d2e372004-07-04 11:27:07 +0000208 if(ev == EK_EVENT_INIT) {
209 ctk_window_new(&window, 12, 7, "Calc");
210
211 CTK_WIDGET_ADD(&window, &inputlabel);
212 CTK_WIDGET_SET_FLAG(&inputlabel, CTK_WIDGET_FLAG_MONOSPACE);
213
214 CTK_WIDGET_ADD(&window, &cbutton);
215
216 CTK_WIDGET_ADD(&window, &divbutton);
217
218 CTK_WIDGET_ADD(&window, &button7);
219 CTK_WIDGET_ADD(&window, &button8);
220 CTK_WIDGET_ADD(&window, &button9);
221
222 CTK_WIDGET_ADD(&window, &mulbutton);
223
224
225
226 CTK_WIDGET_ADD(&window, &button4);
227 CTK_WIDGET_ADD(&window, &button5);
228 CTK_WIDGET_ADD(&window, &button6);
229
230 CTK_WIDGET_ADD(&window, &subbutton);
231
232 CTK_WIDGET_ADD(&window, &button1);
233 CTK_WIDGET_ADD(&window, &button2);
234 CTK_WIDGET_ADD(&window, &button3);
235
236 CTK_WIDGET_ADD(&window, &addbutton);
237
238 CTK_WIDGET_ADD(&window, &button0);
239
240 CTK_WIDGET_ADD(&window, &calcbutton);
241
242 clear_input();
243
244 ctk_window_open(&window);
245
246 } else if(ev == ctk_signal_keypress) {
adamdunkelseb0df012003-04-09 19:16:49 +0000247 if((char)data >= '0' &&
248 (char)data <= '9') {
adamdunkels04d2e372004-07-04 11:27:07 +0000249 add_to_input((char)data);
adamdunkelseb0df012003-04-09 19:16:49 +0000250 } else if((char)data == ' ') {
adamdunkels04d2e372004-07-04 11:27:07 +0000251 clear_input();
adamdunkelseb0df012003-04-09 19:16:49 +0000252 } else if((char)data == '+') {
253 input_to_operand1();
254 op = OP_ADD;
255 } else if((char)data == '-') {
256 input_to_operand1();
257 op = OP_SUB;
258 } else if((char)data == '*') {
259 input_to_operand1();
260 op = OP_MUL;
261 } else if((char)data == '/') {
262 input_to_operand1();
263 op = OP_DIV;
adamdunkels04d2e372004-07-04 11:27:07 +0000264 } else if((char)data == '=' ||
265 (char)data == CH_ENTER) {
adamdunkelseb0df012003-04-09 19:16:49 +0000266 calculate();
267 }
268
269 CTK_WIDGET_REDRAW(&inputlabel);
adamdunkels04d2e372004-07-04 11:27:07 +0000270 } else if(ev == ctk_signal_button_activate) {
271 if(data == (ek_data_t)&button0) {
272 add_to_input('0');
273 } else if(data == (ek_data_t)&button1) {
274 add_to_input('1');
275 } else if(data == (ek_data_t)&button2) {
276 add_to_input('2');
277 } else if(data == (ek_data_t)&button3) {
278 add_to_input('3');
279 } else if(data == (ek_data_t)&button4) {
280 add_to_input('4');
281 } else if(data == (ek_data_t)&button5) {
282 add_to_input('5');
283 } else if(data == (ek_data_t)&button6) {
284 add_to_input('6');
285 } else if(data == (ek_data_t)&button7) {
286 add_to_input('7');
287 } else if(data == (ek_data_t)&button8) {
288 add_to_input('8');
289 } else if(data == (ek_data_t)&button9) {
290 add_to_input('9');
291 } else if(data == (ek_data_t)&cbutton) {
292 clear_input();
293 } else if(data == (ek_data_t)&calcbutton) {
adamdunkelseb0df012003-04-09 19:16:49 +0000294 calculate();
295 } else if(data == (ek_data_t)&addbutton) {
296 input_to_operand1();
297 op = OP_ADD;
298 } else if(data == (ek_data_t)&subbutton) {
299 input_to_operand1();
300 op = OP_SUB;
301 } else if(data == (ek_data_t)&mulbutton) {
302 input_to_operand1();
303 op = OP_MUL;
304 } else if(data == (ek_data_t)&divbutton) {
305 input_to_operand1();
306 op = OP_DIV;
307 }
308 CTK_WIDGET_REDRAW(&inputlabel);
adamdunkels04d2e372004-07-04 11:27:07 +0000309 } else if(ev == ctk_signal_window_close &&
adamdunkelseb0df012003-04-09 19:16:49 +0000310 data == (ek_data_t)&window) {
311 calc_quit();
312 }
313}
314/*-----------------------------------------------------------------------------------*/