1 /* Simulator's extensions to the kernel
2 Copyright (C) 2005-2008, Jonathan Bachrach, Jacob Beal, and contributors
3 listed in the AUTHORS file in the MIT Proto distribution's top directory.
5 This file is part of MIT Proto, and is distributed under the terms of
6 the GNU General Public License, with a linking exception, as described
7 in the file LICENSE in the MIT Proto distribution's top directory. */
13 #include "platform_ops.h"
15 INLINE
void slider_exec (VOID
) {
16 int dkey
= (int)NUM_PEEK(5);
17 int ikey
= (int)NUM_PEEK(4);
18 NUM_VAL init
= NUM_PEEK(3);
19 NUM_VAL incr
= NUM_PEEK(2);
20 NUM_VAL min
= NUM_PEEK(1);
21 NUM_VAL max
= NUM_PEEK(0);
22 NPOP(6); NUM_PUSH(read_slider(dkey
, ikey
, init
, incr
, min
, max
));
25 #define CLIP(x, min, max) MAX(min, MIN(max, x))
27 static void hsv_to_rgb (flo h
, flo s
, flo v
, flo
*r
, flo
*g
, flo
*b
) {
28 flo rt
= 0, gt
= 0, bt
= 0;
34 flo h_temp
= (h
== 360.0) ? 0.0 : h
;
43 case 0: rt
= v
; gt
= t
; bt
= p
; break;
44 case 1: rt
= q
; gt
= v
; bt
= p
; break;
45 case 2: rt
= p
; gt
= v
; bt
= t
; break;
46 case 3: rt
= p
; gt
= q
; bt
= v
; break;
47 case 4: rt
= t
; gt
= p
; bt
= v
; break;
48 case 5: rt
= v
; gt
= p
; bt
= q
; break;
51 *r
= rt
; *g
= gt
; *b
= bt
;
54 MAYBE_INLINE DATA
*vec_elt (VEC_VAL
*vec
, int i
) {
55 if (i
< 0 || i
>= vec
->n
)
56 uerror("UNBOUND VEC ELT %d > %d\n", i
, vec
->n
);
60 INLINE NUM_VAL
num_vec_elt (VEC_VAL
*v
, int i
) { return NUM_GET(vec_elt(v
, i
)); }
61 INLINE NUM_VAL
num_vec_elt_set (VEC_VAL
*v
, int i
, NUM_VAL x
) { return NUM_SET(vec_elt(v
, i
), x
); }
63 void hsv_exec (int off
) {
64 VEC_VAL
*rgb
= VEC_GET(GLO_GET(off
));
65 VEC_VAL
*hsv
= VEC_PEEK(0);
68 (num_vec_elt(hsv
, 0), num_vec_elt(hsv
, 1), num_vec_elt(hsv
, 2), &r
, &g
, &b
);
69 num_vec_elt_set(rgb
, 0, r
);
70 num_vec_elt_set(rgb
, 1, g
);
71 num_vec_elt_set(rgb
, 2, b
);
72 NPOP(1); VEC_PUSH(rgb
);
75 void my_platform_operation(uint8_t op
) {
79 die(NUM_PEEK(0)); break;
81 clone_machine(NUM_PEEK(0)); break;
83 VEC_PUSH(read_coord_sensor()); break;
85 VEC_PUSH(read_ranger()); break;
87 NUM_PUSH(read_sensor((int)NUM_POP())); break;
89 NUM_PUSH(read_button((int)NUM_POP())); break;
93 NUM_PUSH(read_light_sensor()); break;
95 NUM_PUSH(read_microphone()); break;
97 set_speak(NUM_PEEK(0)); break;
99 NUM_PUSH(read_temp()); break;
101 VEC_PUSH(read_mouse_sensor()); break;
103 NUM_PUSH(read_short()); break;
104 case LOCAL_FOLD_OP
: {
105 int k
= (int)NUM_PEEK(0);
106 int val
= (BOOL
)NUM_PEEK(1);
107 set_is_folding((int)NUM_PEEK(1), k
);
108 NPOP(2); NUM_PUSH(val
); break; }
109 case FOLD_COMPLETE_OP
: {
110 int k
= (int)NUM_PEEK(0);
111 NPOP(1); NUM_PUSH(read_fold_complete(k
)); break; }
113 NUM_VAL val
= NUM_PEEK(0);
114 set_b_led((val
> 0.25) != 0 ? 1.0 : 0);
115 set_g_led((val
> 0.50) != 0 ? 1.0 : 0);
116 set_r_led((val
> 0.75) != 0 ? 1.0 : 0);
119 set_r_led(NUM_PEEK(0)); break;
121 set_g_led(NUM_PEEK(0)); break;
123 set_b_led(NUM_PEEK(0)); break;
125 VEC_VAL
*vec
= VEC_PEEK(0);
126 set_r_led(num_vec_elt(vec
, 0));
127 set_g_led(num_vec_elt(vec
, 1));
128 set_b_led(num_vec_elt(vec
, 2));
131 hsv_exec(NXT_OP(m
)); break;
133 radius_set(NUM_PEEK(0)); break;
135 NUM_PUSH(radius_get()); break;
137 NUM_PUSH(read_bump()); break;
140 set_channel(NUM_POP(), n
); NUM_PUSH(n
); break; }
142 NUM_VAL a
= NUM_PEEK(1);
143 NUM_VAL c
= NUM_PEEK(0);
144 NPOP(2); NUM_PUSH(drip_channel(a
, (int)c
));
146 case CONCENTRATION_OP
:
147 NUM_PUSH(read_channel((int)NUM_POP())); break;
148 case CHANNEL_GRAD_OP
:
149 VEC_PUSH(grad_channel((int)NUM_POP())); break;
151 NUM_PUSH(cam_get((int)NUM_POP())); break;
153 uerror("UNKNOWN OPCODE %d\n", op
);