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 
47 time_t lr_timeoff; // all keys off if no data received before...
48 unsigned int lr_curkeys; // mask of keys currently "ON"
49 
50 unsigned int lr_data; // lnp data byte
51 int lr_dataready = 0; // lr_data valid?
52 
53 lr_handler_t lr_handler; // the user event handler
54 tid_t lr_tid; // Button dispatch thread
55 
57 //
58 // Functions
59 //
61 
63 void 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 
78 void 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 
106 wakeup_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 
119 int 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 
135 void lr_init()
136 {
137  lnp_remote_set_handler(lr_getdata);
138  return;
139 }
140 
141 
143 void 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;
148  lr_handler = NULL;
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 
158 void 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
LNP Interface: link networking protocol.
int(* lr_handler_t)(unsigned int, unsigned int)
the remote key handler type
Definition: remote.h:80
a key on the remote was released
Definition: remote.h:72
void lnp_remote_set_handler(lnp_remote_handler_t handler)
set the remote packet handler
Definition: lnp.h:122
void lr_set_handler(lr_handler_t handler)
set a new handler for LEGO IR Remote messages
Definition: remote.h:97
Interface: console input / output.
#define LR_DUMMY_HANDLER
dummy remote event handler
Definition: remote.h:102
#define NULL
null pointer value
Definition: mem.h:35
time_t get_system_up_time(void)
retrieve the current system time
a key on the remote was pressed
Definition: remote.h:71
signed int tid_t
task id type
Definition: tm.h:143
void lr_init(void)
initialize the LEGO IR Remote subsystem
Interface: reduced UNIX standard library.
#define LR_TIMEOUT
timeout value in mSec
Definition: remote.h:34
void lr_shutdown(void)
stop the LEGO IR Remote subsystem
#define PRIO_HIGHEST
The highest possible task priority.
Definition: tm.h:55
unsigned long wakeup_t
wakeup data area type
Definition: tm.h:57
lr_handler_t lr_handler
remote handler
tid_t execi(int(*code_start)(int, char **), int argc, char **argv, priority_t priority, size_t stack_size)
void kill(tid_t tid)
unsigned long time_t
time type
Definition: time.h:50
#define LNP_DUMMY_REMOTE
dummy remote packet handler
Definition: lnp.h:70
void lr_startup(void)
start the LEGO IR Remote subsystem
Interface: LEGO Infrared Remote Control.
Internal Interface: LCD control and constants.
wakeup_t wait_event(wakeup_t(*wakeup)(wakeup_t), wakeup_t data)
#define shutdown_requested()
test to see if task has been asked to shutdown
Definition: tm.h:134
#define DEFAULT_STACK_SIZE
that's enough.
Definition: tm.h:81

brickOS is released under the Mozilla Public License.
Original code copyright 1998-2005 by the authors.

Generated for brickOS Kernel Developer by doxygen 1.8.9.1