brickOS Kernel Developer v0.9.0
remote.c
Go to the documentation of this file.
1
6/*
7 * Copyright (c) 2001 Ross Crawford
8 *
9 * The contents of this file are subject to the Mozilla Public License
10 * Version 1.0 (the "License"); you may not use this file except in
11 * compliance with the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
13 *
14 * Software distributed under the License is distributed on an "AS IS"
15 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
16 * License for the specific language governing rights and limitations
17 * under the License.
18 */
19
20/*
21 * 2002.04.23 - Ted Hess <thess@kitschensync.net>
22 *
23 * - Integrated into legOS 0.2.6. Added lr_startup(), lr_shutdown()
24 * Release input buffer while processing keys
25 *
26 */
27
28#include <remote.h>
29
30#if defined(CONF_LR_HANDLER)
31
32#include <sys/lcd.h>
33#include <unistd.h>
34#include <lnp/lnp.h>
35#include <time.h>
36#include <dmotor.h>
37#include <dsound.h>
38#include <conio.h>
39#include <tm.h>
40
42//
43// Internal Variables
44//
46
47time_t lr_timeoff; // all keys off if no data received before...
48unsigned int lr_curkeys; // mask of keys currently "ON"
49
50unsigned int lr_data; // lnp data byte
51int lr_dataready = 0; // lr_data valid?
52
53lr_handler_t lr_handler; // the user event handler
54tid_t lr_tid; // Button dispatch thread
55
57//
58// Functions
59//
61
63void lr_getdata(unsigned int x)
64{
65 // If previous data hasn't been processed yet, this will be lost
66 if (lr_dataready == 0)
67 {
68 lr_data = x;
69 lr_dataready = 1;
70 }
71
72 // Reset timeout
73 lr_timeoff = get_system_up_time() + LR_TIMEOUT;
74}
75
76
78void lr_process(unsigned int lr_keys)
79{
80 unsigned int keys_on, keys_off, common_keys, k;
81
82 // If keys pressed has changed
83 if (lr_keys != lr_curkeys) {
84
85 // Get mask of keys pressed & released since last event
86 common_keys = (lr_keys & lr_curkeys);
87 keys_on = lr_keys & ~common_keys;
88 keys_off = lr_curkeys & ~common_keys;
89
90 // send event to user handler for each change
91 if (lr_handler) {
92 for (k=1; k; k<<=1) {
93 if (keys_on & k)
95 if (keys_off & k)
97 }
98 }
99
100 // store key mask for next time
101 lr_curkeys = lr_keys;
102 }
103 return;
104}
105
106wakeup_t lr_waitdata(wakeup_t data)
107{
108 // if time runs out, fake "all keys off"
109 if (get_system_up_time() > lr_timeoff && lr_curkeys != 0) {
110 lr_data = 0;
111 lr_dataready = 1;
112 }
113
114 // tell lr_thread whether there's any data available
115 return lr_dataready;
116}
117
119int lr_thread(int argc, char *argv[]) {
120 unsigned int lr_keys;
121 while(!shutdown_requested()) {
122 if (wait_event(&lr_waitdata, 0) != 0) {
123 // Snatch input before calling user handler
124 lr_keys = lr_data;
125 // Have local copy of input data, allow buffer to refill
126 lr_dataready = 0;
127 // Call user handler
128 lr_process(lr_keys);
129 }
130 }
131 return 0;
132}
133
135void lr_init()
136{
137 lnp_remote_set_handler(lr_getdata);
138 return;
139}
140
141
143void lr_startup()
144{
145 // start with all keys off, set initial timeout, clear user handler
146 lr_curkeys = 0;
147 lr_timeoff = get_system_up_time() + LR_TIMEOUT;
149
150 // Start watcher thread, then tell lnp where we want remote data to go
151 lr_tid = execi(&lr_thread,0,0,PRIO_HIGHEST,DEFAULT_STACK_SIZE);
152 lr_init();
153
154 return;
155}
156
158void lr_shutdown()
159{
160 // Disconnect protocol handler
163
164 // terminate thread
165 kill(lr_tid);
166
167 return;
168}
169
170#endif // CONF_LR_HANDLER
Interface: console input / output.
LNP Interface: link networking protocol.
void lnp_remote_set_handler(lnp_remote_handler_t handler)
set the remote packet handler
Definition lnp.h:122
#define LNP_DUMMY_REMOTE
dummy remote packet handler
Definition lnp.h:70
#define NULL
null pointer value
Definition mem.h:35
Interface: LEGO Infrared Remote Control.
int(* lr_handler_t)(unsigned int, unsigned int)
the remote key handler type
Definition remote.h:80
void lr_startup(void)
start the LEGO IR Remote subsystem
lr_handler_t lr_handler
remote handler
#define LR_DUMMY_HANDLER
dummy remote event handler
Definition remote.h:102
void lr_set_handler(lr_handler_t handler)
set a new handler for LEGO IR Remote messages
Definition remote.h:97
@ LREVT_KEYON
a key on the remote was pressed
Definition remote.h:71
@ LREVT_KEYOFF
a key on the remote was released
Definition remote.h:72
void lr_init(void)
initialize the LEGO IR Remote subsystem
void lr_shutdown(void)
stop the LEGO IR Remote subsystem
#define LR_TIMEOUT
timeout value in mSec
Definition remote.h:34
Internal Interface: LCD control and constants.
time_t get_system_up_time(void)
retrieve the current system time
unsigned long time_t
time type
Definition time.h:50
#define shutdown_requested()
test to see if task has been asked to shutdown
Definition tm.h:134
signed int tid_t
task id type
Definition tm.h:143
#define PRIO_HIGHEST
The highest possible task priority.
Definition tm.h:55
#define DEFAULT_STACK_SIZE
that's enough.
Definition tm.h:81
unsigned long wakeup_t
wakeup data area type
Definition tm.h:57
Interface: reduced UNIX standard library.
wakeup_t wait_event(wakeup_t(*wakeup)(wakeup_t), wakeup_t data)
Definition unistd.h:112