blob: 73cd8704253b163f36d8677b3b230c4b68051d96 [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.
14 * 3. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This an example program for the Contiki desktop OS
34 *
adamdunkelsc97a60b2003-04-11 20:12:34 +000035 * $Id: calc.c,v 1.2 2003/04/11 20:12:34 adamdunkels Exp $
adamdunkelseb0df012003-04-09 19:16:49 +000036 *
37 */
38
39/* This is an example of how to write programs for Contiki. It
40 displays a window with the famous message "Hello world" and two
41 buttons; one which changes the message slightly and one which quits
42 the application. */
43
44#include "ctk.h"
45#include "dispatcher.h"
46#include "loader.h"
47
48static struct ctk_window window;
49
50static char input[16];
51static struct ctk_label inputlabel =
52 {CTK_LABEL(0, 0, 15, 1, input)};
53
54static struct ctk_button addbutton =
55 {CTK_BUTTON(0, 1, 1, "+")};
56static struct ctk_button subbutton =
57 {CTK_BUTTON(3, 1, 1, "-")};
58static struct ctk_button mulbutton =
59 {CTK_BUTTON(6, 1, 1, "*")};
60static struct ctk_button divbutton =
61 {CTK_BUTTON(9, 1, 1, "/")};
62static struct ctk_button calcbutton =
63 {CTK_BUTTON(12, 1, 1, "=")};
64
65static DISPATCHER_SIGHANDLER(calc_sighandler, s, data);
66static struct dispatcher_proc p =
adamdunkelsc97a60b2003-04-11 20:12:34 +000067 {DISPATCHER_PROC("Calculator", NULL, calc_sighandler, NULL)};
adamdunkelseb0df012003-04-09 19:16:49 +000068static ek_id_t id;
69
70static unsigned int operand1, operand2;
71static unsigned char op;
72#define OP_ADD 1
73#define OP_SUB 2
74#define OP_MUL 3
75#define OP_DIV 4
76
77/*-----------------------------------------------------------------------------------*/
78LOADER_INIT_FUNC(calc_init)
79{
80 unsigned char i;
81
82 if(id == EK_ID_NONE) {
83 id = dispatcher_start(&p);
84
85 ctk_window_new(&window, 15, 2, "Calc");
86
87 CTK_WIDGET_ADD(&window, &inputlabel);
88
89 CTK_WIDGET_ADD(&window, &addbutton);
90 CTK_WIDGET_ADD(&window, &subbutton);
91 CTK_WIDGET_ADD(&window, &mulbutton);
92 CTK_WIDGET_ADD(&window, &divbutton);
93 CTK_WIDGET_ADD(&window, &calcbutton);
94
95 CTK_WIDGET_FOCUS(&window, &addbutton);
96
97 dispatcher_listen(ctk_signal_button_activate);
98 dispatcher_listen(ctk_signal_window_close);
99 dispatcher_listen(ctk_signal_keypress);
100
101 for(i = 0; i < sizeof(input); ++i) {
102 input[i] = ' ';
103 }
104 }
105 ctk_window_open(&window);
106}
107/*-----------------------------------------------------------------------------------*/
108static void
109calc_quit(void)
110{
111 dispatcher_exit(&p);
112 id = EK_ID_NONE;
113 LOADER_UNLOAD();
adamdunkelseb0df012003-04-09 19:16:49 +0000114}
115/*-----------------------------------------------------------------------------------*/
116static void
117input_to_operand1(void)
118{
119 unsigned int m;
120 unsigned char i;
121
122 operand1 = 0;
123 for(m = 1, i = 14;
124 i > 9; --i, m *= 10) {
125 if(input[i] >= '0' &&
126 input[i] <= '9') {
127 operand1 += (input[i] - '0') * m;
128 }
129 input[i] = ' ';
130 }
131 input[14] = ' ';
132}
133/*-----------------------------------------------------------------------------------*/
134static void
135operand2_to_input(void)
136{
137 unsigned char i;
138
139 input[10] = (operand2/10000) % 10 + '0';
140 input[11] = (operand2/1000) % 10 + '0';
141 input[12] = (operand2/100) % 10 + '0';
142 input[13] = (operand2/10) % 10 + '0';
143 input[14] = operand2 % 10 + '0';
144
145 for(i = 0; i < 4; ++i) {
146 if(input[10 + i] == '0') {
147 input[10 + i] = ' ';
148 } else {
149 break;
150 }
151 }
152}
153/*-----------------------------------------------------------------------------------*/
154static void
155calculate(void)
156{
157 operand2 = operand1;
158 input_to_operand1();
159 switch(op) {
160 case OP_ADD:
161 operand2 = operand2 + operand1;
162 break;
163 case OP_SUB:
164 operand2 = operand2 - operand1;
165 break;
166 case OP_MUL:
167 operand2 = operand2 * operand1;
168 break;
169 case OP_DIV:
170 operand2 = operand2 / operand1;
171 break;
172 }
173 operand2_to_input();
174}
175/*-----------------------------------------------------------------------------------*/
176static
177DISPATCHER_SIGHANDLER(calc_sighandler, s, data)
178{
179 unsigned char i;
180 DISPATCHER_SIGHANDLER_ARGS(s, data);
181
182 if(s == ctk_signal_keypress) {
183 if((char)data >= '0' &&
184 (char)data <= '9') {
185 for(i = 0; i < 14; ++i) {
186 input[i] = input[i + 1];
187 }
188 input[14] = (char)data;
189 } else if((char)data == ' ') {
190 for(i = 0; i < sizeof(input); ++i) {
191 input[i] = ' ';
192 }
193 } else if((char)data == '+') {
194 input_to_operand1();
195 op = OP_ADD;
196 } else if((char)data == '-') {
197 input_to_operand1();
198 op = OP_SUB;
199 } else if((char)data == '*') {
200 input_to_operand1();
201 op = OP_MUL;
202 } else if((char)data == '/') {
203 input_to_operand1();
204 op = OP_DIV;
205 } else if((char)data == '=') {
206 calculate();
207 }
208
209 CTK_WIDGET_REDRAW(&inputlabel);
210 } else if(s == ctk_signal_button_activate) {
211 if(data == (ek_data_t)&calcbutton) {
212 calculate();
213 } else if(data == (ek_data_t)&addbutton) {
214 input_to_operand1();
215 op = OP_ADD;
216 } else if(data == (ek_data_t)&subbutton) {
217 input_to_operand1();
218 op = OP_SUB;
219 } else if(data == (ek_data_t)&mulbutton) {
220 input_to_operand1();
221 op = OP_MUL;
222 } else if(data == (ek_data_t)&divbutton) {
223 input_to_operand1();
224 op = OP_DIV;
225 }
226 CTK_WIDGET_REDRAW(&inputlabel);
227 } else if(s == ctk_signal_window_close &&
228 data == (ek_data_t)&window) {
229 calc_quit();
230 }
231}
232/*-----------------------------------------------------------------------------------*/