brickOS C++ v0.9.0
dsensor.h
Go to the documentation of this file.
1
6/*
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.0 (the "License"); you may not use this file except in
9 * compliance with the License. You may obtain a copy of the License
10 * at http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS"
13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14 * the License for the specific language governing rights and
15 * limitations under the License.
16 *
17 * The Original Code is legOS code, released October 17, 1999.
18 *
19 * The Initial Developer of the Original Code is Markus L. Noga.
20 * Portions created by Markus L. Noga are Copyright (C) 1999
21 * Markus L. Noga. All Rights Reserved.
22 *
23 * Contributor(s): Markus L. Noga <markus@noga.de>
24 */
25
26/*
27 * 2000.04.30 - Paolo Masetti <paolo.masetti@itlug.org>
28 *
29 * - Some typecast & ()s in macros to avoid strange effects
30 * using them...
31 *
32 * 2000.09.06 - Jochen Hoenicke <jochen@gnu.org>
33 *
34 * - Added velocity calculation for rotation sensor.
35 */
36
37
38#ifndef __dsensor_h__
39#define __dsensor_h__
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45#include <config.h>
46
47#ifdef CONF_DSENSOR
48
49#include <sys/h8.h>
50#include <sys/bitops.h>
51
53//
54// Definitions
55//
57
58//
60//
61#define SENSOR_1 AD_C
62#define SENSOR_2 AD_B
63#define SENSOR_3 AD_A
64#define BATTERY AD_D
65
66//
68//
69#define LIGHT_RAW_BLACK 0xffc0
70#define LIGHT_RAW_WHITE 0x5080
71
72
73//
74// convert raw values to 0 (dark) .. LIGHT_MAX (bright)
75// roughly 0-100.
76//
77#define LIGHT(a) (147 - ds_scale(a)/7)
78#define LIGHT_MAX LIGHT(LIGHT_RAW_WHITE)
79
80//
81// processed active light sensor
82//
83#define LIGHT_1 LIGHT(SENSOR_1)
84#define LIGHT_2 LIGHT(SENSOR_2)
85#define LIGHT_3 LIGHT(SENSOR_3)
86
87#ifdef CONF_DSENSOR_ROTATION
88//
89// processed rotation sensor
90//
91#define ROTATION_1 (ds_rotations[2])
92#define ROTATION_2 (ds_rotations[1])
93#define ROTATION_3 (ds_rotations[0])
94#endif
95
96#ifdef CONF_DSENSOR_VELOCITY
97//
98// processed velocity sensor
99//
100#define VELOCITY_1 (ds_velocities[2])
101#define VELOCITY_2 (ds_velocities[1])
102#define VELOCITY_3 (ds_velocities[0])
103#endif
104
105#ifdef CONF_DSENSOR_MUX
106#define SENSOR_1A (ds_muxs[2][0])
107#define SENSOR_1B (ds_muxs[2][1])
108#define SENSOR_1C (ds_muxs[2][2])
109#define SENSOR_2A (ds_muxs[1][0])
110#define SENSOR_2B (ds_muxs[1][1])
111#define SENSOR_2C (ds_muxs[1][2])
112#define SENSOR_3A (ds_muxs[0][0])
113#define SENSOR_3B (ds_muxs[0][1])
114#define SENSOR_3C (ds_muxs[0][2])
115#endif //CONF_DSENSOR_MUX
116
118#define TOUCH(a) ((unsigned int)(a) < 0x8000)
119
120// Processed touch sensors
121//
122#define TOUCH_1 TOUCH(SENSOR_1)
123#define TOUCH_2 TOUCH(SENSOR_2)
124#define TOUCH_3 TOUCH(SENSOR_3)
125
126
127#define ds_scale(x) ((unsigned int)(x)>>6)
128#define ds_unscale(x) ((unsigned int)(x)<<6)
129
131//
132// Variables
133//
135
136//
137// don't manipulate directly unless you know what you're doing!
138//
139
140extern unsigned char ds_activation;
141
142#ifdef CONF_DSENSOR_ROTATION
143extern unsigned char ds_rotation;
144
145extern volatile int ds_rotations[3];
146
147#endif
148#ifdef CONF_DSENSOR_VELOCITY
149extern volatile int ds_velocities[3];
150#endif
151
152#ifdef CONF_DSENSOR_MUX
153extern unsigned char ds_mux;
154
155extern volatile int ds_muxs[3][3];
156#endif //CONF_DSENSOR_MUX
157
159//
160// Functions
161//
163
165
167extern inline void ds_active(volatile unsigned *sensor)
168{
169 if (sensor == &SENSOR_3)
170 bit_set(&ds_activation, 0);
171 else if (sensor == &SENSOR_2)
172 bit_set(&ds_activation, 1);
173 else if (sensor == &SENSOR_1)
174 bit_set(&ds_activation, 2);
175}
176
178
180extern inline void ds_passive(volatile unsigned *sensor)
181{
182 if (sensor == &SENSOR_3) {
183 bit_clear(&ds_activation, 0);
184 bit_clear(&PORT6, 0);
185 } else if (sensor == &SENSOR_2) {
186 bit_clear(&ds_activation, 1);
187 bit_clear(&PORT6, 1);
188 } else if (sensor == &SENSOR_1) {
189 bit_clear(&ds_activation, 2);
190 bit_clear(&PORT6, 2);
191 }
192}
193
194#ifdef CONF_DSENSOR_ROTATION
196
202extern void ds_rotation_set(volatile unsigned *sensor, int pos);
203
205
207extern inline void ds_rotation_on(volatile unsigned *sensor)
208{
209 if (sensor == &SENSOR_3)
210 bit_set(&ds_rotation, 0);
211 else if (sensor == &SENSOR_2)
212 bit_set(&ds_rotation, 1);
213 else if (sensor == &SENSOR_1)
214 bit_set(&ds_rotation, 2);
215}
216
218
220extern inline void ds_rotation_off(volatile unsigned *sensor)
221{
222 if (sensor == &SENSOR_3)
223 bit_clear(&ds_rotation, 0);
224 else if (sensor == &SENSOR_2)
225 bit_clear(&ds_rotation, 1);
226 else if (sensor == &SENSOR_1)
227 bit_clear(&ds_rotation, 2);
228}
229#endif // CONF_DSENSOR_ROTATION
230
231
232#ifdef CONF_DSENSOR_MUX
233
234#define DS_MUX_POST_SWITCH 150
236
247extern void ds_mux_on(volatile unsigned *sensor,
248 unsigned int ch1,
249 unsigned int ch2,
250 unsigned int ch3);
251
252
254
256extern inline void ds_mux_off(volatile unsigned *sensor)
257{
258 if (sensor == &SENSOR_3)
259 bit_clear(&ds_mux, 0);
260 else if (sensor == &SENSOR_2)
261 bit_clear(&ds_mux, 1);
262 else if (sensor == &SENSOR_1)
263 bit_clear(&ds_mux, 2);
264}//endof ds_mux_off
265#endif // CONF_DSENSOR_MUX
266
267
268
269
270
271#endif // CONF_DSENSOR
272
273#ifdef __cplusplus
274}
275#endif
276
277#endif // __dsensor_h__
#define SENSOR_2
Sensor on input pad 2.
Definition dsensor.h:62
void ds_passive(volatile unsigned *sensor)
set sensor mode to passive (light sensor detects ambient light)
Definition dsensor.h:180
void ds_rotation_set(volatile unsigned *sensor, int pos)
set rotation to an absolute value
void ds_active(volatile unsigned *sensor)
set sensor mode to active (light sensor emits light, rotation works)
Definition dsensor.h:167
void ds_rotation_off(volatile unsigned *sensor)
stop tracking rotation sensor
Definition dsensor.h:220
#define SENSOR_1
< the raw sensors
Definition dsensor.h:61
#define SENSOR_3
Sensor on input pad 3.
Definition dsensor.h:63
void ds_rotation_on(volatile unsigned *sensor)
start tracking rotation sensor
Definition dsensor.h:207
unsigned char ds_activation
activation bitmask
unsigned char ds_rotation
rotation bitmask
volatile int ds_rotations[3]
rotational position