2 * Copyright (c) 2003, Adam Dunkels.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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 $
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
;
94 /*-----------------------------------------------------------------------------------*/
98 process_exit(&calc_process
);
101 /*-----------------------------------------------------------------------------------*/
107 for(i
= 0; i
< 11; ++i
) {
108 input
[i
] = input
[i
+ 1];
112 /*-----------------------------------------------------------------------------------*/
118 for(i
= 0; i
< sizeof(input
); ++i
) {
122 /*-----------------------------------------------------------------------------------*/
124 input_to_operand1(void)
131 i
> 7; --i
, m
*= 10) {
132 if(input
[i
] >= '0' &&
134 operand1
+= (input
[i
] - '0') * m
;
139 /*-----------------------------------------------------------------------------------*/
141 operand2_to_input(void)
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') {
159 /*-----------------------------------------------------------------------------------*/
167 operand2
= operand2
+ operand1
;
170 operand2
= operand2
- operand1
;
173 operand2
= operand2
* operand1
;
176 operand2
= operand2
/ operand1
;
181 /*-----------------------------------------------------------------------------------*/
182 PROCESS_THREAD(calc_process
, ev
, data
)
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
);
221 ctk_window_open(&window
);
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
== ' ') {
233 } else if((char)(size_t)data
== '+') {
236 } else if((char)(size_t)data
== '-') {
239 } else if((char)(size_t)data
== '*') {
242 } else if((char)(size_t)data
== '/') {
245 } else if((char)(size_t)data
== '=' ||
246 (char)(size_t)data
== CH_ENTER
) {
250 CTK_WIDGET_REDRAW(&inputlabel
);
251 } else if(ev
== ctk_signal_button_activate
) {
252 if(data
== (process_data_t
)&button0
) {
254 } else if(data
== (process_data_t
)&button1
) {
256 } else if(data
== (process_data_t
)&button2
) {
258 } else if(data
== (process_data_t
)&button3
) {
260 } else if(data
== (process_data_t
)&button4
) {
262 } else if(data
== (process_data_t
)&button5
) {
264 } else if(data
== (process_data_t
)&button6
) {
266 } else if(data
== (process_data_t
)&button7
) {
268 } else if(data
== (process_data_t
)&button8
) {
270 } else if(data
== (process_data_t
)&button9
) {
272 } else if(data
== (process_data_t
)&cbutton
) {
274 } else if(data
== (process_data_t
)&calcbutton
) {
276 } else if(data
== (process_data_t
)&addbutton
) {
279 } else if(data
== (process_data_t
)&subbutton
) {
282 } else if(data
== (process_data_t
)&mulbutton
) {
285 } else if(data
== (process_data_t
)&divbutton
) {
289 CTK_WIDGET_REDRAW(&inputlabel
);
290 } else if(ev
== ctk_signal_window_close
&&
291 data
== (process_data_t
)&window
) {
297 /*-----------------------------------------------------------------------------------*/