Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

kernel/HOLD/remote.c

Go to the documentation of this file.
00001 
00006 /*
00007  * Copyright (c) 2001 Ross Crawford
00008  *
00009  *  The contents of this file are subject to the Mozilla Public License
00010  *  Version 1.0 (the "License"); you may not use this file except in
00011  *  compliance with the License. You may obtain a copy of the License at
00012  *  http://www.mozilla.org/MPL/
00013  *
00014  *  Software distributed under the License is distributed on an "AS IS"
00015  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
00016  *  License for the specific language governing rights and limitations
00017  *  under the License.
00018  */
00019 
00020 /*
00021  *  2002.04.23 - Ted Hess <thess@kitschensync.net>
00022  *
00023  *  - Integrated into legOS 0.2.6. Added lr_startup(), lr_shutdown()
00024  *    Release input buffer while processing keys
00025  *
00026  */
00027 
00028 #include <remote.h>
00029 
00030 #if defined(CONF_LR_HANDLER)
00031 
00032 #include <sys/lcd.h>
00033 #include <unistd.h>
00034 #include <lnp/lnp.h>
00035 #include <time.h>
00036 #include <dmotor.h>
00037 #include <dsound.h>
00038 #include <conio.h>
00039 #include <tm.h>
00040 
00042 //
00043 // Internal Variables
00044 //
00046 
00047 time_t lr_timeoff;         // all keys off if no data received before...
00048 unsigned int lr_curkeys;   // mask of keys currently "ON"
00049 
00050 unsigned int lr_data;      // lnp data byte
00051 int lr_dataready = 0;      // lr_data valid?
00052 
00053 lr_handler_t lr_handler;   // the user event handler
00054 tid_t lr_tid;     // Button dispatch thread
00055 
00057 //
00058 // Functions
00059 //
00061 
00063 void lr_getdata(unsigned int x)
00064 {
00065   // If previous data hasn't been processed yet, this will be lost
00066   if (lr_dataready == 0)
00067   {
00068     lr_data = x;
00069     lr_dataready = 1;
00070   }
00071 
00072   // Reset timeout
00073   lr_timeoff = get_system_up_time() + LR_TIMEOUT;
00074 }
00075 
00076 
00078 void lr_process(unsigned int lr_keys)
00079 {
00080   unsigned int keys_on, keys_off, common_keys, k;
00081 
00082   // If keys pressed has changed
00083   if (lr_keys != lr_curkeys) {
00084 
00085     // Get mask of keys pressed & released since last event
00086     common_keys = (lr_keys & lr_curkeys);
00087     keys_on = lr_keys & ~common_keys;
00088     keys_off = lr_curkeys & ~common_keys;
00089 
00090     // send event to user handler for each change
00091     if (lr_handler) {
00092       for (k=1; k; k<<=1) {
00093         if (keys_on & k)
00094           lr_handler(LREVT_KEYON,k);
00095         if (keys_off & k)
00096           lr_handler(LREVT_KEYOFF,k);
00097       }
00098     }
00099 
00100     // store key mask for next time
00101     lr_curkeys = lr_keys;
00102   }
00103   return;
00104 }
00105  
00106 wakeup_t lr_waitdata(wakeup_t data)
00107 {
00108   // if time runs out, fake "all keys off"
00109   if (get_system_up_time() > lr_timeoff && lr_curkeys != 0) {
00110     lr_data = 0;
00111     lr_dataready = 1;
00112   }
00113 
00114   // tell lr_thread whether there's any data available
00115   return lr_dataready;
00116 }
00117 
00119 int lr_thread(int argc, char *argv[]) {
00120   unsigned int lr_keys;
00121   while(!shutdown_requested()) {
00122     if (wait_event(&lr_waitdata, 0) != 0) {
00123       // Snatch input before calling user handler
00124       lr_keys = lr_data;
00125       // Have local copy of input data, allow buffer to refill
00126       lr_dataready = 0;
00127       // Call user handler
00128       lr_process(lr_keys);
00129     }
00130   }
00131   return 0;
00132 }
00133 
00135 void lr_init()
00136 {
00137   lnp_remote_set_handler(lr_getdata);
00138   return;
00139 }
00140 
00141 
00143 void lr_startup()
00144 {
00145   // start with all keys off, set initial timeout, clear user handler
00146   lr_curkeys = 0;
00147   lr_timeoff = get_system_up_time() + LR_TIMEOUT;
00148   lr_handler = NULL;
00149 
00150   // Start watcher thread, then tell lnp where we want remote data to go
00151   lr_tid = execi(&lr_thread,0,0,PRIO_HIGHEST,DEFAULT_STACK_SIZE);
00152   lr_init();
00153 
00154   return;
00155 }
00156 
00158 void lr_shutdown()
00159 {
00160   // Disconnect protocol handler
00161   lnp_remote_set_handler(LNP_DUMMY_REMOTE);
00162   lr_set_handler(LR_DUMMY_HANDLER);
00163 
00164   // terminate thread
00165   kill(lr_tid);
00166 
00167   return;
00168 }
00169 
00171 
00175 void lr_set_handler(lr_handler_t handler) {
00176         lr_handler = handler;
00177 }
00178 
00179 #endif  // CONF_LR_HANDLER

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

Generated on Mon Feb 16 21:02:10 2004 for brickOS Kernel Developer by doxygen 1.3.5