Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Sun Mar 19 05:30:53 2017 +0000
Revision:
78:1e00b3fa11af
Parent:
77:0b96f6867312
Child:
79:682ae3171a08
Ad hoc IR command send; Shift button 'AND' and 'OR' modes; new accelerometer auto centering options

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 51:57eb311faafa 1 /* Copyright 2014, 2016 M J Roberts, MIT License
mjr 5:a70c0bce770d 2 *
mjr 5:a70c0bce770d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mjr 5:a70c0bce770d 4 * and associated documentation files (the "Software"), to deal in the Software without
mjr 5:a70c0bce770d 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mjr 5:a70c0bce770d 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mjr 5:a70c0bce770d 7 * Software is furnished to do so, subject to the following conditions:
mjr 5:a70c0bce770d 8 *
mjr 5:a70c0bce770d 9 * The above copyright notice and this permission notice shall be included in all copies or
mjr 5:a70c0bce770d 10 * substantial portions of the Software.
mjr 5:a70c0bce770d 11 *
mjr 5:a70c0bce770d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mjr 48:058ace2aed1d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILIT Y, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 5:a70c0bce770d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 5:a70c0bce770d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 5:a70c0bce770d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 5:a70c0bce770d 17 */
mjr 5:a70c0bce770d 18
mjr 5:a70c0bce770d 19 //
mjr 35:e959ffba78fd 20 // The Pinscape Controller
mjr 35:e959ffba78fd 21 // A comprehensive input/output controller for virtual pinball machines
mjr 5:a70c0bce770d 22 //
mjr 48:058ace2aed1d 23 // This project implements an I/O controller for virtual pinball cabinets. The
mjr 48:058ace2aed1d 24 // controller's function is to connect Visual Pinball (and other Windows pinball
mjr 48:058ace2aed1d 25 // emulators) with physical devices in the cabinet: buttons, sensors, and
mjr 48:058ace2aed1d 26 // feedback devices that create visual or mechanical effects during play.
mjr 38:091e511ce8a0 27 //
mjr 48:058ace2aed1d 28 // The controller can perform several different functions, which can be used
mjr 38:091e511ce8a0 29 // individually or in any combination:
mjr 5:a70c0bce770d 30 //
mjr 38:091e511ce8a0 31 // - Nudge sensing. This uses the KL25Z's on-board accelerometer to sense the
mjr 38:091e511ce8a0 32 // motion of the cabinet when you nudge it. Visual Pinball and other pinball
mjr 38:091e511ce8a0 33 // emulators on the PC have native handling for this type of input, so that
mjr 38:091e511ce8a0 34 // physical nudges on the cabinet turn into simulated effects on the virtual
mjr 38:091e511ce8a0 35 // ball. The KL25Z measures accelerations as analog readings and is quite
mjr 38:091e511ce8a0 36 // sensitive, so the effect of a nudge on the simulation is proportional
mjr 38:091e511ce8a0 37 // to the strength of the nudge. Accelerations are reported to the PC via a
mjr 38:091e511ce8a0 38 // simulated joystick (using the X and Y axes); you just have to set some
mjr 38:091e511ce8a0 39 // preferences in your pinball software to tell it that an accelerometer
mjr 38:091e511ce8a0 40 // is attached.
mjr 5:a70c0bce770d 41 //
mjr 74:822a92bc11d2 42 // - Plunger position sensing, with multiple sensor options. To use this feature,
mjr 35:e959ffba78fd 43 // you need to choose a sensor and set it up, connect the sensor electrically to
mjr 35:e959ffba78fd 44 // the KL25Z, and configure the Pinscape software on the KL25Z to let it know how
mjr 35:e959ffba78fd 45 // the sensor is hooked up. The Pinscape software monitors the sensor and sends
mjr 35:e959ffba78fd 46 // readings to Visual Pinball via the joystick Z axis. VP and other PC software
mjr 38:091e511ce8a0 47 // have native support for this type of input; as with the nudge setup, you just
mjr 38:091e511ce8a0 48 // have to set some options in VP to activate the plunger.
mjr 17:ab3cec0c8bf4 49 //
mjr 35:e959ffba78fd 50 // The Pinscape software supports optical sensors (the TAOS TSL1410R and TSL1412R
mjr 35:e959ffba78fd 51 // linear sensor arrays) as well as slide potentiometers. The specific equipment
mjr 35:e959ffba78fd 52 // that's supported, along with physical mounting and wiring details, can be found
mjr 35:e959ffba78fd 53 // in the Build Guide.
mjr 35:e959ffba78fd 54 //
mjr 77:0b96f6867312 55 // Note that VP has built-in support for plunger devices like this one, but
mjr 77:0b96f6867312 56 // some VP tables can't use it without some additional scripting work. The
mjr 77:0b96f6867312 57 // Build Guide has advice on adjusting tables to add plunger support when
mjr 77:0b96f6867312 58 // necessary.
mjr 5:a70c0bce770d 59 //
mjr 6:cc35eb643e8f 60 // For best results, the plunger sensor should be calibrated. The calibration
mjr 6:cc35eb643e8f 61 // is stored in non-volatile memory on board the KL25Z, so it's only necessary
mjr 6:cc35eb643e8f 62 // to do the calibration once, when you first install everything. (You might
mjr 6:cc35eb643e8f 63 // also want to re-calibrate if you physically remove and reinstall the CCD
mjr 17:ab3cec0c8bf4 64 // sensor or the mechanical plunger, since their alignment shift change slightly
mjr 17:ab3cec0c8bf4 65 // when you put everything back together.) You can optionally install a
mjr 17:ab3cec0c8bf4 66 // dedicated momentary switch or pushbutton to activate the calibration mode;
mjr 17:ab3cec0c8bf4 67 // this is describe in the project documentation. If you don't want to bother
mjr 17:ab3cec0c8bf4 68 // with the extra button, you can also trigger calibration using the Windows
mjr 17:ab3cec0c8bf4 69 // setup software, which you can find on the Pinscape project page.
mjr 6:cc35eb643e8f 70 //
mjr 17:ab3cec0c8bf4 71 // The calibration procedure is described in the project documentation. Briefly,
mjr 17:ab3cec0c8bf4 72 // when you trigger calibration mode, the software will scan the CCD for about
mjr 17:ab3cec0c8bf4 73 // 15 seconds, during which you should simply pull the physical plunger back
mjr 17:ab3cec0c8bf4 74 // all the way, hold it for a moment, and then slowly return it to the rest
mjr 17:ab3cec0c8bf4 75 // position. (DON'T just release it from the retracted position, since that
mjr 17:ab3cec0c8bf4 76 // let it shoot forward too far. We want to measure the range from the park
mjr 17:ab3cec0c8bf4 77 // position to the fully retracted position only.)
mjr 5:a70c0bce770d 78 //
mjr 77:0b96f6867312 79 // - Button input wiring. You can assign GPIO ports as inputs for physical
mjr 77:0b96f6867312 80 // pinball-style buttons, such as flipper buttons, a Start button, coin
mjr 77:0b96f6867312 81 // chute switches, tilt bobs, and service panel buttons. You can configure
mjr 77:0b96f6867312 82 // each button input to report a keyboard key or joystick button press to
mjr 77:0b96f6867312 83 // the PC when the physical button is pushed.
mjr 13:72dda449c3c0 84 //
mjr 53:9b2611964afc 85 // - LedWiz emulation. The KL25Z can pretend to be an LedWiz device. This lets
mjr 53:9b2611964afc 86 // you connect feedback devices (lights, solenoids, motors) to GPIO ports on the
mjr 53:9b2611964afc 87 // KL25Z, and lets PC software (such as Visual Pinball) control them during game
mjr 53:9b2611964afc 88 // play to create a more immersive playing experience. The Pinscape software
mjr 53:9b2611964afc 89 // presents itself to the host as an LedWiz device and accepts the full LedWiz
mjr 53:9b2611964afc 90 // command set, so software on the PC designed for real LedWiz'es can control
mjr 53:9b2611964afc 91 // attached devices without any modifications.
mjr 5:a70c0bce770d 92 //
mjr 53:9b2611964afc 93 // Even though the software provides a very thorough LedWiz emulation, the KL25Z
mjr 53:9b2611964afc 94 // GPIO hardware design imposes some serious limitations. The big one is that
mjr 53:9b2611964afc 95 // the KL25Z only has 10 PWM channels, meaning that only 10 ports can have
mjr 53:9b2611964afc 96 // varying-intensity outputs (e.g., for controlling the brightness level of an
mjr 53:9b2611964afc 97 // LED or the speed or a motor). You can control more than 10 output ports, but
mjr 53:9b2611964afc 98 // only 10 can have PWM control; the rest are simple "digital" ports that can only
mjr 53:9b2611964afc 99 // be switched fully on or fully off. The second limitation is that the KL25Z
mjr 53:9b2611964afc 100 // just doesn't have that many GPIO ports overall. There are enough to populate
mjr 53:9b2611964afc 101 // all 32 button inputs OR all 32 LedWiz outputs, but not both. The default is
mjr 53:9b2611964afc 102 // to assign 24 buttons and 22 LedWiz ports; you can change this balance to trade
mjr 53:9b2611964afc 103 // off more outputs for fewer inputs, or vice versa. The third limitation is that
mjr 53:9b2611964afc 104 // the KL25Z GPIO pins have *very* tiny amperage limits - just 4mA, which isn't
mjr 53:9b2611964afc 105 // even enough to control a small LED. So in order to connect any kind of feedback
mjr 53:9b2611964afc 106 // device to an output, you *must* build some external circuitry to boost the
mjr 53:9b2611964afc 107 // current handing. The Build Guide has a reference circuit design for this
mjr 53:9b2611964afc 108 // purpose that's simple and inexpensive to build.
mjr 6:cc35eb643e8f 109 //
mjr 26:cb71c4af2912 110 // - Enhanced LedWiz emulation with TLC5940 PWM controller chips. You can attach
mjr 26:cb71c4af2912 111 // external PWM controller chips for controlling device outputs, instead of using
mjr 53:9b2611964afc 112 // the on-board GPIO ports as described above. The software can control a set of
mjr 53:9b2611964afc 113 // daisy-chained TLC5940 chips. Each chip provides 16 PWM outputs, so you just
mjr 53:9b2611964afc 114 // need two of them to get the full complement of 32 output ports of a real LedWiz.
mjr 53:9b2611964afc 115 // You can hook up even more, though. Four chips gives you 64 ports, which should
mjr 53:9b2611964afc 116 // be plenty for nearly any virtual pinball project. To accommodate the larger
mjr 53:9b2611964afc 117 // supply of ports possible with the PWM chips, the controller software provides
mjr 53:9b2611964afc 118 // a custom, extended version of the LedWiz protocol that can handle up to 128
mjr 53:9b2611964afc 119 // ports. PC software designed only for the real LedWiz obviously won't know
mjr 53:9b2611964afc 120 // about the extended protocol and won't be able to take advantage of its extra
mjr 53:9b2611964afc 121 // capabilities, but the latest version of DOF (DirectOutput Framework) *does*
mjr 53:9b2611964afc 122 // know the new language and can take full advantage. Older software will still
mjr 53:9b2611964afc 123 // work, though - the new extensions are all backward compatible, so old software
mjr 53:9b2611964afc 124 // that only knows about the original LedWiz protocol will still work, with the
mjr 53:9b2611964afc 125 // obvious limitation that it can only access the first 32 ports.
mjr 53:9b2611964afc 126 //
mjr 53:9b2611964afc 127 // The Pinscape Expansion Board project (which appeared in early 2016) provides
mjr 53:9b2611964afc 128 // a reference hardware design, with EAGLE circuit board layouts, that takes full
mjr 53:9b2611964afc 129 // advantage of the TLC5940 capability. It lets you create a customized set of
mjr 53:9b2611964afc 130 // outputs with full PWM control and power handling for high-current devices
mjr 53:9b2611964afc 131 // built in to the boards.
mjr 26:cb71c4af2912 132 //
mjr 38:091e511ce8a0 133 // - Night Mode control for output devices. You can connect a switch or button
mjr 38:091e511ce8a0 134 // to the controller to activate "Night Mode", which disables feedback devices
mjr 38:091e511ce8a0 135 // that you designate as noisy. You can designate outputs individually as being
mjr 38:091e511ce8a0 136 // included in this set or not. This is useful if you want to play a game on
mjr 38:091e511ce8a0 137 // your cabinet late at night without waking the kids and annoying the neighbors.
mjr 38:091e511ce8a0 138 //
mjr 38:091e511ce8a0 139 // - TV ON switch. The controller can pulse a relay to turn on your TVs after
mjr 38:091e511ce8a0 140 // power to the cabinet comes on, with a configurable delay timer. This feature
mjr 38:091e511ce8a0 141 // is for TVs that don't turn themselves on automatically when first plugged in.
mjr 38:091e511ce8a0 142 // To use this feature, you have to build some external circuitry to allow the
mjr 77:0b96f6867312 143 // software to sense the power supply status. The Build Guide has details
mjr 77:0b96f6867312 144 // on the necessary circuitry. You can use this to switch your TV on via a
mjr 77:0b96f6867312 145 // hardwired connection to the TV's "on" button, which requires taking the
mjr 77:0b96f6867312 146 // TV apart to gain access to its internal wiring, or optionally via the IR
mjr 77:0b96f6867312 147 // remote control transmitter feature below.
mjr 77:0b96f6867312 148 //
mjr 77:0b96f6867312 149 // - Infrared (IR) remote control receiver and transmitter. You can attach an
mjr 77:0b96f6867312 150 // IR LED and/or an IR sensor (we recommend the TSOP384xx series) to make the
mjr 77:0b96f6867312 151 // KL25Z capable of sending and/or receiving IR remote control signals. This
mjr 77:0b96f6867312 152 // can be used with the TV ON feature above to turn your TV(s) on when the
mjr 77:0b96f6867312 153 // system power comes on by sending the "on" command to them via IR, as though
mjr 77:0b96f6867312 154 // you pressed the "on" button on the remote control. The sensor lets the
mjr 77:0b96f6867312 155 // Pinscape software learn the IR codes from your existing remotes, in the
mjr 77:0b96f6867312 156 // same manner as a handheld universal remote control, and the IR LED lets
mjr 77:0b96f6867312 157 // it transmit learned codes. The sensor can also be used to receive codes
mjr 77:0b96f6867312 158 // during normal operation and turn them into PC keystrokes; this lets you
mjr 77:0b96f6867312 159 // access extra commands on the PC without adding more buttons to your
mjr 77:0b96f6867312 160 // cabinet. The IR LED can also be used to transmit other codes when you
mjr 77:0b96f6867312 161 // press selected cabinet buttons, allowing you to assign cabinet buttons
mjr 77:0b96f6867312 162 // to send IR commands to your cabinet TV or other devices.
mjr 38:091e511ce8a0 163 //
mjr 35:e959ffba78fd 164 //
mjr 35:e959ffba78fd 165 //
mjr 33:d832bcab089e 166 // STATUS LIGHTS: The on-board LED on the KL25Z flashes to indicate the current
mjr 33:d832bcab089e 167 // device status. The flash patterns are:
mjr 6:cc35eb643e8f 168 //
mjr 48:058ace2aed1d 169 // short yellow flash = waiting to connect
mjr 6:cc35eb643e8f 170 //
mjr 48:058ace2aed1d 171 // short red flash = the connection is suspended (the host is in sleep
mjr 48:058ace2aed1d 172 // or suspend mode, the USB cable is unplugged after a connection
mjr 48:058ace2aed1d 173 // has been established)
mjr 48:058ace2aed1d 174 //
mjr 48:058ace2aed1d 175 // two short red flashes = connection lost (the device should immediately
mjr 48:058ace2aed1d 176 // go back to short-yellow "waiting to reconnect" mode when a connection
mjr 48:058ace2aed1d 177 // is lost, so this display shouldn't normally appear)
mjr 6:cc35eb643e8f 178 //
mjr 38:091e511ce8a0 179 // long red/yellow = USB connection problem. The device still has a USB
mjr 48:058ace2aed1d 180 // connection to the host (or so it appears to the device), but data
mjr 48:058ace2aed1d 181 // transmissions are failing.
mjr 38:091e511ce8a0 182 //
mjr 73:4e8ce0b18915 183 // medium blue flash = TV ON delay timer running. This means that the
mjr 73:4e8ce0b18915 184 // power to the secondary PSU has just been turned on, and the TV ON
mjr 73:4e8ce0b18915 185 // timer is waiting for the configured delay time before pulsing the
mjr 73:4e8ce0b18915 186 // TV power button relay. This is only shown if the TV ON feature is
mjr 73:4e8ce0b18915 187 // enabled.
mjr 73:4e8ce0b18915 188 //
mjr 6:cc35eb643e8f 189 // long yellow/green = everything's working, but the plunger hasn't
mjr 38:091e511ce8a0 190 // been calibrated. Follow the calibration procedure described in
mjr 38:091e511ce8a0 191 // the project documentation. This flash mode won't appear if there's
mjr 38:091e511ce8a0 192 // no plunger sensor configured.
mjr 6:cc35eb643e8f 193 //
mjr 38:091e511ce8a0 194 // alternating blue/green = everything's working normally, and plunger
mjr 38:091e511ce8a0 195 // calibration has been completed (or there's no plunger attached)
mjr 10:976666ffa4ef 196 //
mjr 48:058ace2aed1d 197 // fast red/purple = out of memory. The controller halts and displays
mjr 48:058ace2aed1d 198 // this diagnostic code until you manually reset it. If this happens,
mjr 48:058ace2aed1d 199 // it's probably because the configuration is too complex, in which
mjr 48:058ace2aed1d 200 // case the same error will occur after the reset. If it's stuck
mjr 48:058ace2aed1d 201 // in this cycle, you'll have to restore the default configuration
mjr 48:058ace2aed1d 202 // by re-installing the controller software (the Pinscape .bin file).
mjr 10:976666ffa4ef 203 //
mjr 48:058ace2aed1d 204 //
mjr 48:058ace2aed1d 205 // USB PROTOCOL: Most of our USB messaging is through standard USB HID
mjr 48:058ace2aed1d 206 // classes (joystick, keyboard). We also accept control messages on our
mjr 48:058ace2aed1d 207 // primary HID interface "OUT endpoint" using a custom protocol that's
mjr 48:058ace2aed1d 208 // not defined in any USB standards (we do have to provide a USB HID
mjr 48:058ace2aed1d 209 // Report Descriptor for it, but this just describes the protocol as
mjr 48:058ace2aed1d 210 // opaque vendor-defined bytes). The control protocol incorporates the
mjr 48:058ace2aed1d 211 // LedWiz protocol as a subset, and adds our own private extensions.
mjr 48:058ace2aed1d 212 // For full details, see USBProtocol.h.
mjr 33:d832bcab089e 213
mjr 33:d832bcab089e 214
mjr 0:5acbbe3f4cf4 215 #include "mbed.h"
mjr 6:cc35eb643e8f 216 #include "math.h"
mjr 74:822a92bc11d2 217 #include "diags.h"
mjr 48:058ace2aed1d 218 #include "pinscape.h"
mjr 0:5acbbe3f4cf4 219 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 220 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 221 #include "tsl1410r.h"
mjr 1:d913e0afb2ac 222 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 223 #include "crc32.h"
mjr 26:cb71c4af2912 224 #include "TLC5940.h"
mjr 34:6b981a2afab7 225 #include "74HC595.h"
mjr 35:e959ffba78fd 226 #include "nvm.h"
mjr 35:e959ffba78fd 227 #include "plunger.h"
mjr 35:e959ffba78fd 228 #include "ccdSensor.h"
mjr 35:e959ffba78fd 229 #include "potSensor.h"
mjr 35:e959ffba78fd 230 #include "nullSensor.h"
mjr 48:058ace2aed1d 231 #include "TinyDigitalIn.h"
mjr 77:0b96f6867312 232 #include "IRReceiver.h"
mjr 77:0b96f6867312 233 #include "IRTransmitter.h"
mjr 77:0b96f6867312 234 #include "NewPwm.h"
mjr 74:822a92bc11d2 235
mjr 2:c174f9ee414a 236
mjr 21:5048e16cc9ef 237 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 238 #include "config.h"
mjr 17:ab3cec0c8bf4 239
mjr 76:7f5912b6340e 240 // forward declarations
mjr 76:7f5912b6340e 241 static void waitPlungerIdle(void);
mjr 53:9b2611964afc 242
mjr 53:9b2611964afc 243 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 244 //
mjr 53:9b2611964afc 245 // OpenSDA module identifier. This is for the benefit of the Windows
mjr 53:9b2611964afc 246 // configuration tool. When the config tool installs a .bin file onto
mjr 53:9b2611964afc 247 // the KL25Z, it will first find the sentinel string within the .bin file,
mjr 53:9b2611964afc 248 // and patch the "\0" bytes that follow the sentinel string with the
mjr 53:9b2611964afc 249 // OpenSDA module ID data. This allows us to report the OpenSDA
mjr 53:9b2611964afc 250 // identifiers back to the host system via USB, which in turn allows the
mjr 53:9b2611964afc 251 // config tool to figure out which OpenSDA MSD (mass storage device - a
mjr 53:9b2611964afc 252 // virtual disk drive) correlates to which Pinscape controller USB
mjr 53:9b2611964afc 253 // interface.
mjr 53:9b2611964afc 254 //
mjr 53:9b2611964afc 255 // This is only important if multiple Pinscape devices are attached to
mjr 53:9b2611964afc 256 // the same host. There doesn't seem to be any other way to figure out
mjr 53:9b2611964afc 257 // which OpenSDA MSD corresponds to which KL25Z USB interface; the OpenSDA
mjr 53:9b2611964afc 258 // MSD doesn't report the KL25Z CPU ID anywhere, and the KL25Z doesn't
mjr 53:9b2611964afc 259 // have any way to learn about the OpenSDA module it's connected to. The
mjr 53:9b2611964afc 260 // only way to pass this information to the KL25Z side that I can come up
mjr 53:9b2611964afc 261 // with is to have the Windows host embed it in the .bin file before
mjr 53:9b2611964afc 262 // downloading it to the OpenSDA MSD.
mjr 53:9b2611964afc 263 //
mjr 53:9b2611964afc 264 // We initialize the const data buffer (the part after the sentinel string)
mjr 53:9b2611964afc 265 // with all "\0" bytes, so that's what will be in the executable image that
mjr 53:9b2611964afc 266 // comes out of the mbed compiler. If you manually install the resulting
mjr 53:9b2611964afc 267 // .bin file onto the KL25Z (via the Windows desktop, say), the "\0" bytes
mjr 53:9b2611964afc 268 // will stay this way and read as all 0's at run-time. Since a real TUID
mjr 53:9b2611964afc 269 // would never be all 0's, that tells us that we were never patched and
mjr 53:9b2611964afc 270 // thus don't have any information on the OpenSDA module.
mjr 53:9b2611964afc 271 //
mjr 53:9b2611964afc 272 const char *getOpenSDAID()
mjr 53:9b2611964afc 273 {
mjr 53:9b2611964afc 274 #define OPENSDA_PREFIX "///Pinscape.OpenSDA.TUID///"
mjr 53:9b2611964afc 275 static const char OpenSDA[] = OPENSDA_PREFIX "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0///";
mjr 53:9b2611964afc 276 const size_t OpenSDA_prefix_length = sizeof(OPENSDA_PREFIX) - 1;
mjr 53:9b2611964afc 277
mjr 53:9b2611964afc 278 return OpenSDA + OpenSDA_prefix_length;
mjr 53:9b2611964afc 279 }
mjr 53:9b2611964afc 280
mjr 53:9b2611964afc 281 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 282 //
mjr 53:9b2611964afc 283 // Build ID. We use the date and time of compiling the program as a build
mjr 53:9b2611964afc 284 // identifier. It would be a little nicer to use a simple serial number
mjr 53:9b2611964afc 285 // instead, but the mbed platform doesn't have a way to automate that. The
mjr 53:9b2611964afc 286 // timestamp is a pretty good proxy for a serial number in that it will
mjr 53:9b2611964afc 287 // naturally increase on each new build, which is the primary property we
mjr 53:9b2611964afc 288 // want from this.
mjr 53:9b2611964afc 289 //
mjr 53:9b2611964afc 290 // As with the embedded OpenSDA ID, we store the build timestamp with a
mjr 53:9b2611964afc 291 // sentinel string prefix, to allow automated tools to find the static data
mjr 53:9b2611964afc 292 // in the .bin file by searching for the sentinel string. In contrast to
mjr 53:9b2611964afc 293 // the OpenSDA ID, the value we store here is for tools to extract rather
mjr 53:9b2611964afc 294 // than store, since we automatically populate it via the preprocessor
mjr 53:9b2611964afc 295 // macros.
mjr 53:9b2611964afc 296 //
mjr 53:9b2611964afc 297 const char *getBuildID()
mjr 53:9b2611964afc 298 {
mjr 53:9b2611964afc 299 #define BUILDID_PREFIX "///Pinscape.Build.ID///"
mjr 53:9b2611964afc 300 static const char BuildID[] = BUILDID_PREFIX __DATE__ " " __TIME__ "///";
mjr 53:9b2611964afc 301 const size_t BuildID_prefix_length = sizeof(BUILDID_PREFIX) - 1;
mjr 53:9b2611964afc 302
mjr 53:9b2611964afc 303 return BuildID + BuildID_prefix_length;
mjr 53:9b2611964afc 304 }
mjr 53:9b2611964afc 305
mjr 74:822a92bc11d2 306 // --------------------------------------------------------------------------
mjr 74:822a92bc11d2 307 // Main loop iteration timing statistics. Collected only if
mjr 74:822a92bc11d2 308 // ENABLE_DIAGNOSTICS is set in diags.h.
mjr 76:7f5912b6340e 309 #if ENABLE_DIAGNOSTICS
mjr 76:7f5912b6340e 310 uint64_t mainLoopIterTime, mainLoopIterCheckpt[15], mainLoopIterCount;
mjr 76:7f5912b6340e 311 uint64_t mainLoopMsgTime, mainLoopMsgCount;
mjr 76:7f5912b6340e 312 Timer mainLoopTimer;
mjr 76:7f5912b6340e 313 #endif
mjr 76:7f5912b6340e 314
mjr 53:9b2611964afc 315
mjr 48:058ace2aed1d 316 // --------------------------------------------------------------------------
mjr 48:058ace2aed1d 317 //
mjr 59:94eb9265b6d7 318 // Custom memory allocator. We use our own version of malloc() for more
mjr 59:94eb9265b6d7 319 // efficient memory usage, and to provide diagnostics if we run out of heap.
mjr 48:058ace2aed1d 320 //
mjr 59:94eb9265b6d7 321 // We can implement a more efficient malloc than the library can because we
mjr 59:94eb9265b6d7 322 // can make an assumption that the library can't: allocations are permanent.
mjr 59:94eb9265b6d7 323 // The normal malloc has to assume that allocations can be freed, so it has
mjr 59:94eb9265b6d7 324 // to track blocks individually. For the purposes of this program, though,
mjr 59:94eb9265b6d7 325 // we don't have to do this because virtually all of our allocations are
mjr 59:94eb9265b6d7 326 // de facto permanent. We only allocate dyanmic memory during setup, and
mjr 59:94eb9265b6d7 327 // once we set things up, we never delete anything. This means that we can
mjr 59:94eb9265b6d7 328 // allocate memory in bare blocks without any bookkeeping overhead.
mjr 59:94eb9265b6d7 329 //
mjr 78:1e00b3fa11af 330 // In addition, we can make a larger overall pool of memory available in
mjr 78:1e00b3fa11af 331 // a custom allocator. The RTL malloc() seems to have a pool of about 3K
mjr 78:1e00b3fa11af 332 // to work with, even though there really seems to be at least 8K left after
mjr 78:1e00b3fa11af 333 // reserving a reasonable amount of space for the stack.
mjr 77:0b96f6867312 334
mjr 77:0b96f6867312 335 // halt with a diagnostic display if we run out of memory
mjr 77:0b96f6867312 336 void HaltOutOfMem()
mjr 77:0b96f6867312 337 {
mjr 77:0b96f6867312 338 printf("\r\nOut Of Memory\r\n");
mjr 77:0b96f6867312 339 // halt with the diagnostic display (by looping forever)
mjr 77:0b96f6867312 340 for (;;)
mjr 77:0b96f6867312 341 {
mjr 77:0b96f6867312 342 diagLED(1, 0, 0);
mjr 77:0b96f6867312 343 wait_us(200000);
mjr 77:0b96f6867312 344 diagLED(1, 0, 1);
mjr 77:0b96f6867312 345 wait_us(200000);
mjr 77:0b96f6867312 346 }
mjr 77:0b96f6867312 347 }
mjr 77:0b96f6867312 348
mjr 77:0b96f6867312 349 // For our custom malloc, we take advantage of the known layout of the
mjr 77:0b96f6867312 350 // mbed library memory management. The mbed library puts all of the
mjr 77:0b96f6867312 351 // static read/write data at the low end of RAM; this includes the
mjr 77:0b96f6867312 352 // initialized statics and the "ZI" (zero-initialized) statics. The
mjr 77:0b96f6867312 353 // malloc heap starts just after the last static, growing upwards as
mjr 77:0b96f6867312 354 // memory is allocated. The stack starts at the top of RAM and grows
mjr 77:0b96f6867312 355 // downwards.
mjr 77:0b96f6867312 356 //
mjr 77:0b96f6867312 357 // To figure out where the free memory starts, we simply call the system
mjr 77:0b96f6867312 358 // malloc() to make a dummy allocation the first time we're called, and
mjr 77:0b96f6867312 359 // use the address it returns as the start of our free memory pool. The
mjr 77:0b96f6867312 360 // first malloc() call presumably returns the lowest byte of the pool in
mjr 77:0b96f6867312 361 // the compiler RTL's way of thinking, and from what we know about the
mjr 77:0b96f6867312 362 // mbed heap layout, we know everything above this point should be free,
mjr 77:0b96f6867312 363 // at least until we reach the lowest address used by the stack.
mjr 77:0b96f6867312 364 //
mjr 77:0b96f6867312 365 // The ultimate size of the stack is of course dynamic and unpredictable.
mjr 77:0b96f6867312 366 // In testing, it appears that we currently need a little over 1K. To be
mjr 77:0b96f6867312 367 // conservative, we'll reserve 2K for the stack, by taking it out of the
mjr 77:0b96f6867312 368 // space at top of memory we consider fair game for malloc.
mjr 77:0b96f6867312 369 //
mjr 77:0b96f6867312 370 // Note that we could do this a little more low-level-ly if we wanted.
mjr 77:0b96f6867312 371 // The ARM linker provides a pre-defined extern char[] variable named
mjr 77:0b96f6867312 372 // Image$$RW_IRAM1$$ZI$$Limit, which is always placed just after the
mjr 77:0b96f6867312 373 // last static data variable. In principle, this tells us the start
mjr 77:0b96f6867312 374 // of the available malloc pool. However, in testing, it doesn't seem
mjr 77:0b96f6867312 375 // safe to use this as the start of our malloc pool. I'm not sure why,
mjr 77:0b96f6867312 376 // but probably something in the startup code (either in the C RTL or
mjr 77:0b96f6867312 377 // the mbed library) is allocating from the pool before we get control.
mjr 77:0b96f6867312 378 // So we won't use that approach. Besides, that would tie us even more
mjr 77:0b96f6867312 379 // closely to the ARM compiler. With our malloc() probe approach, we're
mjr 77:0b96f6867312 380 // at least portable to any compiler that uses the same basic memory
mjr 77:0b96f6867312 381 // layout, with the heap above the statics and the stack at top of
mjr 77:0b96f6867312 382 // memory; this isn't universal, but it's very typical.
mjr 77:0b96f6867312 383
mjr 77:0b96f6867312 384 static char *xmalloc_nxt = 0;
mjr 77:0b96f6867312 385 size_t xmalloc_rem = 0;
mjr 77:0b96f6867312 386 void *xmalloc(size_t siz)
mjr 77:0b96f6867312 387 {
mjr 77:0b96f6867312 388 if (xmalloc_nxt == 0)
mjr 77:0b96f6867312 389 {
mjr 77:0b96f6867312 390 xmalloc_nxt = (char *)malloc(4);
mjr 77:0b96f6867312 391 xmalloc_rem = 0x20003000UL - 2*1024 - uint32_t(xmalloc_nxt);
mjr 77:0b96f6867312 392 }
mjr 77:0b96f6867312 393
mjr 77:0b96f6867312 394 siz = (siz + 3) & ~3;
mjr 77:0b96f6867312 395 if (siz > xmalloc_rem)
mjr 77:0b96f6867312 396 HaltOutOfMem();
mjr 77:0b96f6867312 397
mjr 77:0b96f6867312 398 char *ret = xmalloc_nxt;
mjr 77:0b96f6867312 399 xmalloc_nxt += siz;
mjr 77:0b96f6867312 400 xmalloc_rem -= siz;
mjr 77:0b96f6867312 401
mjr 77:0b96f6867312 402 return ret;
mjr 77:0b96f6867312 403 }
mjr 48:058ace2aed1d 404
mjr 59:94eb9265b6d7 405 // Overload operator new to call our custom malloc. This ensures that
mjr 59:94eb9265b6d7 406 // all 'new' allocations throughout the program (including library code)
mjr 59:94eb9265b6d7 407 // go through our private allocator.
mjr 48:058ace2aed1d 408 void *operator new(size_t siz) { return xmalloc(siz); }
mjr 48:058ace2aed1d 409 void *operator new[](size_t siz) { return xmalloc(siz); }
mjr 5:a70c0bce770d 410
mjr 59:94eb9265b6d7 411 // Since we don't do bookkeeping to track released memory, 'delete' does
mjr 59:94eb9265b6d7 412 // nothing. In actual testing, this routine appears to never be called.
mjr 59:94eb9265b6d7 413 // If it *is* ever called, it will simply leave the block in place, which
mjr 59:94eb9265b6d7 414 // will make it unavailable for re-use but will otherwise be harmless.
mjr 59:94eb9265b6d7 415 void operator delete(void *ptr) { }
mjr 59:94eb9265b6d7 416
mjr 59:94eb9265b6d7 417
mjr 5:a70c0bce770d 418 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 419 //
mjr 38:091e511ce8a0 420 // Forward declarations
mjr 38:091e511ce8a0 421 //
mjr 38:091e511ce8a0 422 void setNightMode(bool on);
mjr 38:091e511ce8a0 423 void toggleNightMode();
mjr 38:091e511ce8a0 424
mjr 38:091e511ce8a0 425 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 426 // utilities
mjr 17:ab3cec0c8bf4 427
mjr 77:0b96f6867312 428 // int/float point square of a number
mjr 77:0b96f6867312 429 inline int square(int x) { return x*x; }
mjr 26:cb71c4af2912 430 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 431
mjr 26:cb71c4af2912 432 // floating point rounding
mjr 26:cb71c4af2912 433 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 434
mjr 17:ab3cec0c8bf4 435
mjr 33:d832bcab089e 436 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 437 //
mjr 40:cc0d9814522b 438 // Extended verison of Timer class. This adds the ability to interrogate
mjr 40:cc0d9814522b 439 // the running state.
mjr 40:cc0d9814522b 440 //
mjr 77:0b96f6867312 441 class ExtTimer: public Timer
mjr 40:cc0d9814522b 442 {
mjr 40:cc0d9814522b 443 public:
mjr 77:0b96f6867312 444 ExtTimer() : running(false) { }
mjr 40:cc0d9814522b 445
mjr 40:cc0d9814522b 446 void start() { running = true; Timer::start(); }
mjr 40:cc0d9814522b 447 void stop() { running = false; Timer::stop(); }
mjr 40:cc0d9814522b 448
mjr 40:cc0d9814522b 449 bool isRunning() const { return running; }
mjr 40:cc0d9814522b 450
mjr 40:cc0d9814522b 451 private:
mjr 40:cc0d9814522b 452 bool running;
mjr 40:cc0d9814522b 453 };
mjr 40:cc0d9814522b 454
mjr 53:9b2611964afc 455
mjr 53:9b2611964afc 456 // --------------------------------------------------------------------------
mjr 40:cc0d9814522b 457 //
mjr 33:d832bcab089e 458 // USB product version number
mjr 5:a70c0bce770d 459 //
mjr 47:df7a88cd249c 460 const uint16_t USB_VERSION_NO = 0x000A;
mjr 33:d832bcab089e 461
mjr 33:d832bcab089e 462 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 463 //
mjr 6:cc35eb643e8f 464 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 465 //
mjr 6:cc35eb643e8f 466 #define JOYMAX 4096
mjr 6:cc35eb643e8f 467
mjr 9:fd65b0a94720 468
mjr 17:ab3cec0c8bf4 469 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 470 //
mjr 40:cc0d9814522b 471 // Wire protocol value translations. These translate byte values to and
mjr 40:cc0d9814522b 472 // from the USB protocol to local native format.
mjr 35:e959ffba78fd 473 //
mjr 35:e959ffba78fd 474
mjr 35:e959ffba78fd 475 // unsigned 16-bit integer
mjr 35:e959ffba78fd 476 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 477 {
mjr 35:e959ffba78fd 478 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 479 }
mjr 40:cc0d9814522b 480 inline void ui16Wire(uint8_t *b, uint16_t val)
mjr 40:cc0d9814522b 481 {
mjr 40:cc0d9814522b 482 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 483 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 484 }
mjr 35:e959ffba78fd 485
mjr 35:e959ffba78fd 486 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 487 {
mjr 35:e959ffba78fd 488 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 489 }
mjr 40:cc0d9814522b 490 inline void i16Wire(uint8_t *b, int16_t val)
mjr 40:cc0d9814522b 491 {
mjr 40:cc0d9814522b 492 ui16Wire(b, (uint16_t)val);
mjr 40:cc0d9814522b 493 }
mjr 35:e959ffba78fd 494
mjr 35:e959ffba78fd 495 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 496 {
mjr 35:e959ffba78fd 497 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 498 }
mjr 40:cc0d9814522b 499 inline void ui32Wire(uint8_t *b, uint32_t val)
mjr 40:cc0d9814522b 500 {
mjr 40:cc0d9814522b 501 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 502 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 503 b[2] = (uint8_t)((val >> 16) & 0xff);
mjr 40:cc0d9814522b 504 b[3] = (uint8_t)((val >> 24) & 0xff);
mjr 40:cc0d9814522b 505 }
mjr 35:e959ffba78fd 506
mjr 35:e959ffba78fd 507 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 508 {
mjr 35:e959ffba78fd 509 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 510 }
mjr 35:e959ffba78fd 511
mjr 53:9b2611964afc 512 // Convert "wire" (USB) pin codes to/from PinName values.
mjr 53:9b2611964afc 513 //
mjr 53:9b2611964afc 514 // The internal mbed PinName format is
mjr 53:9b2611964afc 515 //
mjr 53:9b2611964afc 516 // ((port) << PORT_SHIFT) | (pin << 2) // MBED FORMAT
mjr 53:9b2611964afc 517 //
mjr 53:9b2611964afc 518 // where 'port' is 0-4 for Port A to Port E, and 'pin' is
mjr 53:9b2611964afc 519 // 0 to 31. E.g., E31 is (4 << PORT_SHIFT) | (31<<2).
mjr 53:9b2611964afc 520 //
mjr 53:9b2611964afc 521 // We remap this to our more compact wire format where each
mjr 53:9b2611964afc 522 // pin name fits in 8 bits:
mjr 53:9b2611964afc 523 //
mjr 53:9b2611964afc 524 // ((port) << 5) | pin) // WIRE FORMAT
mjr 53:9b2611964afc 525 //
mjr 53:9b2611964afc 526 // E.g., E31 is (4 << 5) | 31.
mjr 53:9b2611964afc 527 //
mjr 53:9b2611964afc 528 // Wire code FF corresponds to PinName NC (not connected).
mjr 53:9b2611964afc 529 //
mjr 53:9b2611964afc 530 inline PinName wirePinName(uint8_t c)
mjr 35:e959ffba78fd 531 {
mjr 53:9b2611964afc 532 if (c == 0xFF)
mjr 53:9b2611964afc 533 return NC; // 0xFF -> NC
mjr 53:9b2611964afc 534 else
mjr 53:9b2611964afc 535 return PinName(
mjr 53:9b2611964afc 536 (int(c & 0xE0) << (PORT_SHIFT - 5)) // top three bits are the port
mjr 53:9b2611964afc 537 | (int(c & 0x1F) << 2)); // bottom five bits are pin
mjr 40:cc0d9814522b 538 }
mjr 40:cc0d9814522b 539 inline void pinNameWire(uint8_t *b, PinName n)
mjr 40:cc0d9814522b 540 {
mjr 53:9b2611964afc 541 *b = PINNAME_TO_WIRE(n);
mjr 35:e959ffba78fd 542 }
mjr 35:e959ffba78fd 543
mjr 35:e959ffba78fd 544
mjr 35:e959ffba78fd 545 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 546 //
mjr 38:091e511ce8a0 547 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 548 //
mjr 38:091e511ce8a0 549 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 550 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 551 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 552 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 553 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 554 // SPI capability.
mjr 38:091e511ce8a0 555 //
mjr 38:091e511ce8a0 556 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 557
mjr 73:4e8ce0b18915 558 // Power on timer state for diagnostics. We flash the blue LED when
mjr 77:0b96f6867312 559 // nothing else is going on. State 0-1 = off, 2-3 = on blue. Also
mjr 77:0b96f6867312 560 // show red when transmitting an LED signal, indicated by state 4.
mjr 73:4e8ce0b18915 561 uint8_t powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 562
mjr 38:091e511ce8a0 563 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 564 // on, and -1 is no change (leaves the current setting intact).
mjr 73:4e8ce0b18915 565 static uint8_t diagLEDState = 0;
mjr 38:091e511ce8a0 566 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 567 {
mjr 73:4e8ce0b18915 568 // remember the new state
mjr 73:4e8ce0b18915 569 diagLEDState = r | (g << 1) | (b << 2);
mjr 73:4e8ce0b18915 570
mjr 73:4e8ce0b18915 571 // if turning everything off, use the power timer state instead,
mjr 73:4e8ce0b18915 572 // applying it to the blue LED
mjr 73:4e8ce0b18915 573 if (diagLEDState == 0)
mjr 77:0b96f6867312 574 {
mjr 77:0b96f6867312 575 b = (powerTimerDiagState == 2 || powerTimerDiagState == 3);
mjr 77:0b96f6867312 576 r = (powerTimerDiagState == 4);
mjr 77:0b96f6867312 577 }
mjr 73:4e8ce0b18915 578
mjr 73:4e8ce0b18915 579 // set the new state
mjr 38:091e511ce8a0 580 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 581 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 582 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 583 }
mjr 38:091e511ce8a0 584
mjr 73:4e8ce0b18915 585 // update the LEDs with the current state
mjr 73:4e8ce0b18915 586 void diagLED(void)
mjr 73:4e8ce0b18915 587 {
mjr 73:4e8ce0b18915 588 diagLED(
mjr 73:4e8ce0b18915 589 diagLEDState & 0x01,
mjr 73:4e8ce0b18915 590 (diagLEDState >> 1) & 0x01,
mjr 77:0b96f6867312 591 (diagLEDState >> 2) & 0x01);
mjr 73:4e8ce0b18915 592 }
mjr 73:4e8ce0b18915 593
mjr 38:091e511ce8a0 594 // check an output port assignment to see if it conflicts with
mjr 38:091e511ce8a0 595 // an on-board LED segment
mjr 38:091e511ce8a0 596 struct LedSeg
mjr 38:091e511ce8a0 597 {
mjr 38:091e511ce8a0 598 bool r, g, b;
mjr 38:091e511ce8a0 599 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 600
mjr 38:091e511ce8a0 601 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 602 {
mjr 38:091e511ce8a0 603 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 604 // our on-board LED segments
mjr 38:091e511ce8a0 605 int t = pc.typ;
mjr 38:091e511ce8a0 606 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 38:091e511ce8a0 607 {
mjr 38:091e511ce8a0 608 // it's a GPIO port - check for a matching pin assignment
mjr 38:091e511ce8a0 609 PinName pin = wirePinName(pc.pin);
mjr 38:091e511ce8a0 610 if (pin == LED1)
mjr 38:091e511ce8a0 611 r = true;
mjr 38:091e511ce8a0 612 else if (pin == LED2)
mjr 38:091e511ce8a0 613 g = true;
mjr 38:091e511ce8a0 614 else if (pin == LED3)
mjr 38:091e511ce8a0 615 b = true;
mjr 38:091e511ce8a0 616 }
mjr 38:091e511ce8a0 617 }
mjr 38:091e511ce8a0 618 };
mjr 38:091e511ce8a0 619
mjr 38:091e511ce8a0 620 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 621 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 622 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 623 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 624 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 625 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 626 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 627 {
mjr 38:091e511ce8a0 628 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 629 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 630 LedSeg l;
mjr 38:091e511ce8a0 631 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 632 l.check(cfg.outPort[i]);
mjr 38:091e511ce8a0 633
mjr 38:091e511ce8a0 634 // We now know which segments are taken for LedWiz use and which
mjr 38:091e511ce8a0 635 // are free. Create diagnostic ports for the ones not claimed for
mjr 38:091e511ce8a0 636 // LedWiz use.
mjr 38:091e511ce8a0 637 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 638 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 639 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 640 }
mjr 38:091e511ce8a0 641
mjr 38:091e511ce8a0 642
mjr 38:091e511ce8a0 643 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 644 //
mjr 76:7f5912b6340e 645 // LedWiz emulation
mjr 76:7f5912b6340e 646 //
mjr 76:7f5912b6340e 647
mjr 76:7f5912b6340e 648 // LedWiz output states.
mjr 76:7f5912b6340e 649 //
mjr 76:7f5912b6340e 650 // The LedWiz protocol has two separate control axes for each output.
mjr 76:7f5912b6340e 651 // One axis is its on/off state; the other is its "profile" state, which
mjr 76:7f5912b6340e 652 // is either a fixed brightness or a blinking pattern for the light.
mjr 76:7f5912b6340e 653 // The two axes are independent.
mjr 76:7f5912b6340e 654 //
mjr 76:7f5912b6340e 655 // Even though the original LedWiz protocol can only access 32 ports, we
mjr 76:7f5912b6340e 656 // maintain LedWiz state for every port, even if we have more than 32. Our
mjr 76:7f5912b6340e 657 // extended protocol allows the client to send LedWiz-style messages that
mjr 76:7f5912b6340e 658 // control any set of ports. A replacement LEDWIZ.DLL can make a single
mjr 76:7f5912b6340e 659 // Pinscape unit look like multiple virtual LedWiz units to legacy clients,
mjr 76:7f5912b6340e 660 // allowing them to control all of our ports. The clients will still be
mjr 76:7f5912b6340e 661 // using LedWiz-style states to control the ports, so we need to support
mjr 76:7f5912b6340e 662 // the LedWiz scheme with separate on/off and brightness control per port.
mjr 76:7f5912b6340e 663
mjr 76:7f5912b6340e 664 // On/off state for each LedWiz output
mjr 76:7f5912b6340e 665 static uint8_t *wizOn;
mjr 76:7f5912b6340e 666
mjr 76:7f5912b6340e 667 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 76:7f5912b6340e 668 // for each LedWiz output. If the output was last updated through an
mjr 76:7f5912b6340e 669 // LedWiz protocol message, it will have one of these values:
mjr 76:7f5912b6340e 670 //
mjr 76:7f5912b6340e 671 // 0-48 = fixed brightness 0% to 100%
mjr 76:7f5912b6340e 672 // 49 = fixed brightness 100% (equivalent to 48)
mjr 76:7f5912b6340e 673 // 129 = ramp up / ramp down
mjr 76:7f5912b6340e 674 // 130 = flash on / off
mjr 76:7f5912b6340e 675 // 131 = on / ramp down
mjr 76:7f5912b6340e 676 // 132 = ramp up / on
mjr 5:a70c0bce770d 677 //
mjr 76:7f5912b6340e 678 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 76:7f5912b6340e 679 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 76:7f5912b6340e 680 // it, so we need to accept it for compatibility.)
mjr 76:7f5912b6340e 681 static uint8_t *wizVal;
mjr 76:7f5912b6340e 682
mjr 76:7f5912b6340e 683 // Current actual brightness for each output. This is a simple linear
mjr 76:7f5912b6340e 684 // value on a 0..255 scale. This is EITHER the linear brightness computed
mjr 76:7f5912b6340e 685 // from the LedWiz setting for the port, OR the 0..255 value set explicitly
mjr 76:7f5912b6340e 686 // by the extended protocol:
mjr 76:7f5912b6340e 687 //
mjr 76:7f5912b6340e 688 // - If the last command that updated the port was an extended protocol
mjr 76:7f5912b6340e 689 // SET BRIGHTNESS command, this is the value set by that command. In
mjr 76:7f5912b6340e 690 // addition, wizOn[port] is set to 0 if the brightness is 0, 1 otherwise;
mjr 76:7f5912b6340e 691 // and wizVal[port] is set to the brightness rescaled to the 0..48 range
mjr 76:7f5912b6340e 692 // if the brightness is non-zero.
mjr 76:7f5912b6340e 693 //
mjr 76:7f5912b6340e 694 // - If the last command that updated the port was an LedWiz command
mjr 76:7f5912b6340e 695 // (SBA/PBA/SBX/PBX), this contains the brightness value computed from
mjr 76:7f5912b6340e 696 // the combination of wizOn[port] and wizVal[port]. If wizOn[port] is
mjr 76:7f5912b6340e 697 // zero, this is simply 0, otherwise it's wizVal[port] rescaled to the
mjr 76:7f5912b6340e 698 // 0..255 range.
mjr 26:cb71c4af2912 699 //
mjr 76:7f5912b6340e 700 // - For a port set to wizOn[port]=1 and wizVal[port] in 129..132, this is
mjr 76:7f5912b6340e 701 // also updated continuously to reflect the current flashing brightness
mjr 76:7f5912b6340e 702 // level.
mjr 26:cb71c4af2912 703 //
mjr 76:7f5912b6340e 704 static uint8_t *outLevel;
mjr 76:7f5912b6340e 705
mjr 76:7f5912b6340e 706
mjr 76:7f5912b6340e 707 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 76:7f5912b6340e 708 // rate for lights in blinking states. The LedWiz API doesn't document
mjr 76:7f5912b6340e 709 // what the numbers mean in real time units, but by observation, the
mjr 76:7f5912b6340e 710 // "speed" setting represents the period of the flash cycle in 0.25s
mjr 76:7f5912b6340e 711 // units, so speed 1 = 0.25 period = 4Hz, speed 7 = 1.75s period = 0.57Hz.
mjr 76:7f5912b6340e 712 // The period is the full cycle time of the flash waveform.
mjr 76:7f5912b6340e 713 //
mjr 76:7f5912b6340e 714 // Each bank of 32 lights has its independent own pulse rate, so we need
mjr 76:7f5912b6340e 715 // one entry per bank. Each bank has 32 outputs, so we need a total of
mjr 76:7f5912b6340e 716 // ceil(number_of_physical_outputs/32) entries. Note that we could allocate
mjr 76:7f5912b6340e 717 // this dynamically once we know the number of actual outputs, but the
mjr 76:7f5912b6340e 718 // upper limit is low enough that it's more efficient to use a fixed array
mjr 76:7f5912b6340e 719 // at the maximum size.
mjr 76:7f5912b6340e 720 static const int MAX_LW_BANKS = (MAX_OUT_PORTS+31)/32;
mjr 76:7f5912b6340e 721 static uint8_t wizSpeed[MAX_LW_BANKS];
mjr 29:582472d0bc57 722
mjr 26:cb71c4af2912 723 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 724 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 725 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 726 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 727 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 728
mjr 76:7f5912b6340e 729
mjr 76:7f5912b6340e 730 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 731 //
mjr 76:7f5912b6340e 732 // Output Ports
mjr 76:7f5912b6340e 733 //
mjr 76:7f5912b6340e 734 // There are two way to connect outputs. First, you can use the on-board
mjr 76:7f5912b6340e 735 // GPIO ports to implement device outputs: each LedWiz software port is
mjr 76:7f5912b6340e 736 // connected to a physical GPIO pin on the KL25Z. This has some pretty
mjr 76:7f5912b6340e 737 // strict limits, though. The KL25Z only has 10 PWM channels, so only 10
mjr 76:7f5912b6340e 738 // GPIO LedWiz ports can be made dimmable; the rest are strictly on/off.
mjr 76:7f5912b6340e 739 // The KL25Z also simply doesn't have enough exposed GPIO ports overall to
mjr 76:7f5912b6340e 740 // support all of the features the software supports. The software allows
mjr 76:7f5912b6340e 741 // for up to 128 outputs, 48 button inputs, plunger input (requiring 1-5
mjr 76:7f5912b6340e 742 // GPIO pins), and various other external devices. The KL25Z only exposes
mjr 76:7f5912b6340e 743 // about 50 GPIO pins. So if you want to do everything with GPIO ports,
mjr 76:7f5912b6340e 744 // you have to ration pins among features.
mjr 76:7f5912b6340e 745 //
mjr 76:7f5912b6340e 746 // To overcome some of these limitations, we also provide two types of
mjr 76:7f5912b6340e 747 // peripheral controllers that allow adding many more outputs, using only
mjr 76:7f5912b6340e 748 // a small number of GPIO pins to interface with the peripherals. First,
mjr 76:7f5912b6340e 749 // we support TLC5940 PWM controller chips. Each TLC5940 provides 16 ports
mjr 76:7f5912b6340e 750 // with full PWM, and multiple TLC5940 chips can be daisy-chained. The
mjr 76:7f5912b6340e 751 // chip only requires 5 GPIO pins for the interface, no matter how many
mjr 76:7f5912b6340e 752 // chips are in the chain, so it effectively converts 5 GPIO pins into
mjr 76:7f5912b6340e 753 // almost any number of PWM outputs. Second, we support 74HC595 chips.
mjr 76:7f5912b6340e 754 // These provide only digital outputs, but like the TLC5940 they can be
mjr 76:7f5912b6340e 755 // daisy-chained to provide almost unlimited outputs with a few GPIO pins
mjr 76:7f5912b6340e 756 // to control the whole chain.
mjr 76:7f5912b6340e 757 //
mjr 76:7f5912b6340e 758 // Direct GPIO output ports and peripheral controllers can be mixed and
mjr 76:7f5912b6340e 759 // matched in one system. The assignment of pins to ports and the
mjr 76:7f5912b6340e 760 // configuration of peripheral controllers is all handled in the software
mjr 76:7f5912b6340e 761 // setup, so a physical system can be expanded and updated at any time.
mjr 76:7f5912b6340e 762 //
mjr 76:7f5912b6340e 763 // To handle the diversity of output port types, we start with an abstract
mjr 76:7f5912b6340e 764 // base class for outputs. Each type of physical output interface has a
mjr 76:7f5912b6340e 765 // concrete subclass. During initialization, we create the appropriate
mjr 76:7f5912b6340e 766 // subclass for each software port, mapping it to the assigned GPIO pin
mjr 76:7f5912b6340e 767 // or peripheral port. Most of the rest of the software only cares about
mjr 76:7f5912b6340e 768 // the abstract interface, so once the subclassed port objects are set up,
mjr 76:7f5912b6340e 769 // the rest of the system can control the ports without knowing which types
mjr 76:7f5912b6340e 770 // of physical devices they're connected to.
mjr 76:7f5912b6340e 771
mjr 76:7f5912b6340e 772
mjr 26:cb71c4af2912 773 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 774 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 775 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 776 class LwOut
mjr 6:cc35eb643e8f 777 {
mjr 6:cc35eb643e8f 778 public:
mjr 40:cc0d9814522b 779 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 780 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 781 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 782 };
mjr 26:cb71c4af2912 783
mjr 35:e959ffba78fd 784 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 785 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 786 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 787 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 788 // numbering.
mjr 35:e959ffba78fd 789 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 790 {
mjr 33:d832bcab089e 791 public:
mjr 35:e959ffba78fd 792 LwVirtualOut() { }
mjr 40:cc0d9814522b 793 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 794 };
mjr 26:cb71c4af2912 795
mjr 34:6b981a2afab7 796 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 797 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 798 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 799 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 800 {
mjr 34:6b981a2afab7 801 public:
mjr 34:6b981a2afab7 802 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 803 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 804
mjr 34:6b981a2afab7 805 private:
mjr 53:9b2611964afc 806 // underlying physical output
mjr 34:6b981a2afab7 807 LwOut *out;
mjr 34:6b981a2afab7 808 };
mjr 34:6b981a2afab7 809
mjr 53:9b2611964afc 810 // Global ZB Launch Ball state
mjr 53:9b2611964afc 811 bool zbLaunchOn = false;
mjr 53:9b2611964afc 812
mjr 53:9b2611964afc 813 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 814 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 815 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 816 {
mjr 53:9b2611964afc 817 public:
mjr 53:9b2611964afc 818 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 819 virtual void set(uint8_t val)
mjr 53:9b2611964afc 820 {
mjr 53:9b2611964afc 821 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 822 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 823
mjr 53:9b2611964afc 824 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 825 out->set(val);
mjr 53:9b2611964afc 826 }
mjr 53:9b2611964afc 827
mjr 53:9b2611964afc 828 private:
mjr 53:9b2611964afc 829 // underlying physical or virtual output
mjr 53:9b2611964afc 830 LwOut *out;
mjr 53:9b2611964afc 831 };
mjr 53:9b2611964afc 832
mjr 53:9b2611964afc 833
mjr 40:cc0d9814522b 834 // Gamma correction table for 8-bit input values
mjr 40:cc0d9814522b 835 static const uint8_t gamma[] = {
mjr 40:cc0d9814522b 836 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 837 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 838 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 839 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 840 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 841 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 842 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 843 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 844 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 845 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 846 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 847 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 848 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 849 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 850 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 851 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 852 };
mjr 40:cc0d9814522b 853
mjr 40:cc0d9814522b 854 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 855 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 856 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 857 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 858 {
mjr 40:cc0d9814522b 859 public:
mjr 40:cc0d9814522b 860 LwGammaOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 861 virtual void set(uint8_t val) { out->set(gamma[val]); }
mjr 40:cc0d9814522b 862
mjr 40:cc0d9814522b 863 private:
mjr 40:cc0d9814522b 864 LwOut *out;
mjr 40:cc0d9814522b 865 };
mjr 40:cc0d9814522b 866
mjr 77:0b96f6867312 867 // Global night mode flag. To minimize overhead when reporting
mjr 77:0b96f6867312 868 // the status, we set this to the status report flag bit for
mjr 77:0b96f6867312 869 // night mode, 0x02, when engaged.
mjr 77:0b96f6867312 870 static uint8_t nightMode = 0x00;
mjr 53:9b2611964afc 871
mjr 40:cc0d9814522b 872 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 873 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 874 // mode is engaged.
mjr 40:cc0d9814522b 875 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 876 {
mjr 40:cc0d9814522b 877 public:
mjr 40:cc0d9814522b 878 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 879 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 880
mjr 53:9b2611964afc 881 private:
mjr 53:9b2611964afc 882 LwOut *out;
mjr 53:9b2611964afc 883 };
mjr 53:9b2611964afc 884
mjr 53:9b2611964afc 885 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 886 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 887 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 888 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 889 {
mjr 53:9b2611964afc 890 public:
mjr 53:9b2611964afc 891 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 892 virtual void set(uint8_t)
mjr 53:9b2611964afc 893 {
mjr 53:9b2611964afc 894 // ignore the host value and simply show the current
mjr 53:9b2611964afc 895 // night mode setting
mjr 53:9b2611964afc 896 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 897 }
mjr 40:cc0d9814522b 898
mjr 40:cc0d9814522b 899 private:
mjr 40:cc0d9814522b 900 LwOut *out;
mjr 40:cc0d9814522b 901 };
mjr 40:cc0d9814522b 902
mjr 26:cb71c4af2912 903
mjr 35:e959ffba78fd 904 //
mjr 35:e959ffba78fd 905 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 906 // assignments set in config.h.
mjr 33:d832bcab089e 907 //
mjr 35:e959ffba78fd 908 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 909 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 910 {
mjr 35:e959ffba78fd 911 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 912 {
mjr 53:9b2611964afc 913 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 914 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 915 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 916 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 917 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 918 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 919 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 920 }
mjr 35:e959ffba78fd 921 }
mjr 26:cb71c4af2912 922
mjr 40:cc0d9814522b 923 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 924 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 925 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 926 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 927 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 928 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 929 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 930 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 931 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 932 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 933 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 934 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 935 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 936 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 937 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 938 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 939 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 940 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 941 };
mjr 40:cc0d9814522b 942
mjr 40:cc0d9814522b 943 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 944 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 945 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 946 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 947 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 948 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 949 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 950 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 951 // are always 8 bits.
mjr 40:cc0d9814522b 952 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 953 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 954 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 955 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 956 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 957 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 958 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 959 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 960 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 961 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 962 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 963 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 964 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 965 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 966 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 967 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 968 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 969 };
mjr 40:cc0d9814522b 970
mjr 26:cb71c4af2912 971 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 972 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 973 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 974 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 975 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 976 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 977 {
mjr 26:cb71c4af2912 978 public:
mjr 60:f38da020aa13 979 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 980 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 981 {
mjr 26:cb71c4af2912 982 if (val != prv)
mjr 40:cc0d9814522b 983 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 984 }
mjr 60:f38da020aa13 985 uint8_t idx;
mjr 40:cc0d9814522b 986 uint8_t prv;
mjr 26:cb71c4af2912 987 };
mjr 26:cb71c4af2912 988
mjr 40:cc0d9814522b 989 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 990 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 991 {
mjr 40:cc0d9814522b 992 public:
mjr 60:f38da020aa13 993 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 994 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 995 {
mjr 40:cc0d9814522b 996 if (val != prv)
mjr 40:cc0d9814522b 997 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 998 }
mjr 60:f38da020aa13 999 uint8_t idx;
mjr 40:cc0d9814522b 1000 uint8_t prv;
mjr 40:cc0d9814522b 1001 };
mjr 40:cc0d9814522b 1002
mjr 40:cc0d9814522b 1003
mjr 33:d832bcab089e 1004
mjr 34:6b981a2afab7 1005 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 1006 // config.h.
mjr 35:e959ffba78fd 1007 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 1008
mjr 35:e959ffba78fd 1009 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 1010 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 1011 {
mjr 35:e959ffba78fd 1012 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 1013 {
mjr 53:9b2611964afc 1014 hc595 = new HC595(
mjr 53:9b2611964afc 1015 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 1016 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 1017 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 1018 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 1019 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 1020 hc595->init();
mjr 35:e959ffba78fd 1021 hc595->update();
mjr 35:e959ffba78fd 1022 }
mjr 35:e959ffba78fd 1023 }
mjr 34:6b981a2afab7 1024
mjr 34:6b981a2afab7 1025 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 1026 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 1027 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 1028 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 1029 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 1030 class Lw595Out: public LwOut
mjr 33:d832bcab089e 1031 {
mjr 33:d832bcab089e 1032 public:
mjr 60:f38da020aa13 1033 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1034 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 1035 {
mjr 34:6b981a2afab7 1036 if (val != prv)
mjr 40:cc0d9814522b 1037 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 1038 }
mjr 60:f38da020aa13 1039 uint8_t idx;
mjr 40:cc0d9814522b 1040 uint8_t prv;
mjr 33:d832bcab089e 1041 };
mjr 33:d832bcab089e 1042
mjr 26:cb71c4af2912 1043
mjr 40:cc0d9814522b 1044
mjr 64:ef7ca92dff36 1045 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 1046 // normalized to 0.0 to 1.0 scale.
mjr 74:822a92bc11d2 1047 static const float dof_to_pwm[] = {
mjr 64:ef7ca92dff36 1048 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 1049 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 1050 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 1051 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 1052 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 1053 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 1054 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 1055 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 1056 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 1057 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 1058 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 1059 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 1060 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 1061 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 1062 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 1063 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 1064 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 1065 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 1066 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 1067 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 1068 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 1069 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 1070 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 1071 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 1072 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 1073 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 1074 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 1075 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 1076 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 1077 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 1078 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 1079 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 1080 };
mjr 26:cb71c4af2912 1081
mjr 64:ef7ca92dff36 1082
mjr 64:ef7ca92dff36 1083 // Conversion table for 8-bit DOF level to pulse width in microseconds,
mjr 64:ef7ca92dff36 1084 // with gamma correction. We could use the layered gamma output on top
mjr 64:ef7ca92dff36 1085 // of the regular LwPwmOut class for this, but we get better precision
mjr 64:ef7ca92dff36 1086 // with a dedicated table, because we apply gamma correction to the
mjr 64:ef7ca92dff36 1087 // 32-bit microsecond values rather than the 8-bit DOF levels.
mjr 64:ef7ca92dff36 1088 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 1089 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 1090 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 1091 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 1092 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 1093 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 1094 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 1095 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 1096 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 1097 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 1098 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 1099 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 1100 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 1101 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 1102 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 1103 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 1104 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 1105 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 1106 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 1107 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 1108 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 1109 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 1110 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 1111 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 1112 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 1113 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 1114 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 1115 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 1116 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 1117 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 1118 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 1119 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 1120 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 1121 };
mjr 64:ef7ca92dff36 1122
mjr 77:0b96f6867312 1123 // Polled-update PWM output list
mjr 74:822a92bc11d2 1124 //
mjr 77:0b96f6867312 1125 // This is a workaround for a KL25Z hardware bug/limitation. The bug (more
mjr 77:0b96f6867312 1126 // about this below) is that we can't write to a PWM output "value" register
mjr 77:0b96f6867312 1127 // more than once per PWM cycle; if we do, outputs after the first are lost.
mjr 77:0b96f6867312 1128 // The value register controls the duty cycle, so it's what you have to write
mjr 77:0b96f6867312 1129 // if you want to update the brightness of an output.
mjr 74:822a92bc11d2 1130 //
mjr 77:0b96f6867312 1131 // Our solution is to simply repeat all PWM updates periodically. If a write
mjr 77:0b96f6867312 1132 // is lost on one cycle, it'll eventually be applied on a subseuqent periodic
mjr 77:0b96f6867312 1133 // update. For low overhead, we do these repeat updates periodically during
mjr 77:0b96f6867312 1134 // the main loop.
mjr 74:822a92bc11d2 1135 //
mjr 77:0b96f6867312 1136 // The mbed library has its own solution to this bug, but it creates a
mjr 77:0b96f6867312 1137 // separate problem of its own. The mbed solution is to write the value
mjr 77:0b96f6867312 1138 // register immediately, and then also reset the "count" register in the
mjr 77:0b96f6867312 1139 // TPM unit containing the output. The count reset truncates the current
mjr 77:0b96f6867312 1140 // PWM cycle, which avoids the hardware problem with more than one write per
mjr 77:0b96f6867312 1141 // cycle. The problem is that the truncated cycle causes visible flicker if
mjr 77:0b96f6867312 1142 // the output is connected to an LED. This is particularly noticeable during
mjr 77:0b96f6867312 1143 // fades, when we're updating the value register repeatedly and rapidly: an
mjr 77:0b96f6867312 1144 // attempt to fade from fully on to fully off causes rapid fluttering and
mjr 77:0b96f6867312 1145 // flashing rather than a smooth brightness fade.
mjr 74:822a92bc11d2 1146 //
mjr 77:0b96f6867312 1147 // The hardware bug is a case of good intentions gone bad. The hardware is
mjr 77:0b96f6867312 1148 // *supposed* to make it easy for software to avoid glitching during PWM
mjr 77:0b96f6867312 1149 // updates, by providing a staging register in front of the real value
mjr 77:0b96f6867312 1150 // register. The software actually writes to the staging register, which
mjr 77:0b96f6867312 1151 // holds updates until the end of the cycle, at which point the hardware
mjr 77:0b96f6867312 1152 // automatically moves the value from the staging register into the real
mjr 77:0b96f6867312 1153 // register. This ensures that the real register is always updated exactly
mjr 77:0b96f6867312 1154 // at a cycle boundary, which in turn ensures that there's no flicker when
mjr 77:0b96f6867312 1155 // values are updated. A great design - except that it doesn't quite work.
mjr 77:0b96f6867312 1156 // The problem is that the staging register actually seems to be implemented
mjr 77:0b96f6867312 1157 // as a one-element FIFO in "stop when full" mode. That is, when you write
mjr 77:0b96f6867312 1158 // the FIFO, it becomes full. When the cycle ends and the hardware reads it
mjr 77:0b96f6867312 1159 // to move the staged value into the real register, the FIFO becomes empty.
mjr 77:0b96f6867312 1160 // But if you try to write the FIFO twice before the hardware reads it and
mjr 77:0b96f6867312 1161 // empties it, the second write fails, leaving the first value in the queue.
mjr 77:0b96f6867312 1162 // There doesn't seem to be any way to clear the FIFO from software, so you
mjr 77:0b96f6867312 1163 // just have to wait for the cycle to end before writing another update.
mjr 77:0b96f6867312 1164 // That more or less defeats the purpose of the staging register, whose whole
mjr 77:0b96f6867312 1165 // point is to free software from worrying about timing considerations with
mjr 77:0b96f6867312 1166 // updates. It frees us of the need to align our timing on cycle boundaries,
mjr 77:0b96f6867312 1167 // but it leaves us with the need to limit writes to once per cycle.
mjr 74:822a92bc11d2 1168 //
mjr 77:0b96f6867312 1169 // So here we have our list of PWM outputs that need to be polled for updates.
mjr 77:0b96f6867312 1170 // The KL25Z hardware only has 10 PWM channels, so we only need a fixed set
mjr 77:0b96f6867312 1171 // of polled items.
mjr 74:822a92bc11d2 1172 static int numPolledPwm;
mjr 74:822a92bc11d2 1173 static class LwPwmOut *polledPwm[10];
mjr 74:822a92bc11d2 1174
mjr 74:822a92bc11d2 1175 // LwOut class for a PWM-capable GPIO port.
mjr 6:cc35eb643e8f 1176 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 1177 {
mjr 6:cc35eb643e8f 1178 public:
mjr 43:7a6364d82a41 1179 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1180 {
mjr 77:0b96f6867312 1181 // add myself to the list of polled outputs for periodic updates
mjr 77:0b96f6867312 1182 if (numPolledPwm < countof(polledPwm))
mjr 74:822a92bc11d2 1183 polledPwm[numPolledPwm++] = this;
mjr 77:0b96f6867312 1184
mjr 77:0b96f6867312 1185 // set the initial value
mjr 77:0b96f6867312 1186 set(initVal);
mjr 43:7a6364d82a41 1187 }
mjr 74:822a92bc11d2 1188
mjr 40:cc0d9814522b 1189 virtual void set(uint8_t val)
mjr 74:822a92bc11d2 1190 {
mjr 77:0b96f6867312 1191 // save the new value
mjr 74:822a92bc11d2 1192 this->val = val;
mjr 77:0b96f6867312 1193
mjr 77:0b96f6867312 1194 // commit it to the hardware
mjr 77:0b96f6867312 1195 commit();
mjr 13:72dda449c3c0 1196 }
mjr 74:822a92bc11d2 1197
mjr 74:822a92bc11d2 1198 // handle periodic update polling
mjr 74:822a92bc11d2 1199 void poll()
mjr 74:822a92bc11d2 1200 {
mjr 77:0b96f6867312 1201 commit();
mjr 74:822a92bc11d2 1202 }
mjr 74:822a92bc11d2 1203
mjr 74:822a92bc11d2 1204 protected:
mjr 77:0b96f6867312 1205 virtual void commit()
mjr 74:822a92bc11d2 1206 {
mjr 74:822a92bc11d2 1207 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1208 p.glitchFreeWrite(dof_to_pwm[val]);
mjr 74:822a92bc11d2 1209 }
mjr 74:822a92bc11d2 1210
mjr 77:0b96f6867312 1211 NewPwmOut p;
mjr 77:0b96f6867312 1212 uint8_t val;
mjr 6:cc35eb643e8f 1213 };
mjr 26:cb71c4af2912 1214
mjr 74:822a92bc11d2 1215 // Gamma corrected PWM GPIO output. This works exactly like the regular
mjr 74:822a92bc11d2 1216 // PWM output, but translates DOF values through the gamma-corrected
mjr 74:822a92bc11d2 1217 // table instead of the regular linear table.
mjr 64:ef7ca92dff36 1218 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1219 {
mjr 64:ef7ca92dff36 1220 public:
mjr 64:ef7ca92dff36 1221 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1222 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1223 {
mjr 64:ef7ca92dff36 1224 }
mjr 74:822a92bc11d2 1225
mjr 74:822a92bc11d2 1226 protected:
mjr 77:0b96f6867312 1227 virtual void commit()
mjr 64:ef7ca92dff36 1228 {
mjr 74:822a92bc11d2 1229 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1230 p.glitchFreeWrite(dof_to_gamma_pwm[val]);
mjr 64:ef7ca92dff36 1231 }
mjr 64:ef7ca92dff36 1232 };
mjr 64:ef7ca92dff36 1233
mjr 74:822a92bc11d2 1234 // poll the PWM outputs
mjr 74:822a92bc11d2 1235 Timer polledPwmTimer;
mjr 76:7f5912b6340e 1236 uint64_t polledPwmTotalTime, polledPwmRunCount;
mjr 74:822a92bc11d2 1237 void pollPwmUpdates()
mjr 74:822a92bc11d2 1238 {
mjr 74:822a92bc11d2 1239 // if it's been at least 25ms since the last update, do another update
mjr 74:822a92bc11d2 1240 if (polledPwmTimer.read_us() >= 25000)
mjr 74:822a92bc11d2 1241 {
mjr 74:822a92bc11d2 1242 // time the run for statistics collection
mjr 74:822a92bc11d2 1243 IF_DIAG(
mjr 74:822a92bc11d2 1244 Timer t;
mjr 74:822a92bc11d2 1245 t.start();
mjr 74:822a92bc11d2 1246 )
mjr 74:822a92bc11d2 1247
mjr 74:822a92bc11d2 1248 // poll each output
mjr 74:822a92bc11d2 1249 for (int i = numPolledPwm ; i > 0 ; )
mjr 74:822a92bc11d2 1250 polledPwm[--i]->poll();
mjr 74:822a92bc11d2 1251
mjr 74:822a92bc11d2 1252 // reset the timer for the next cycle
mjr 74:822a92bc11d2 1253 polledPwmTimer.reset();
mjr 74:822a92bc11d2 1254
mjr 74:822a92bc11d2 1255 // collect statistics
mjr 74:822a92bc11d2 1256 IF_DIAG(
mjr 76:7f5912b6340e 1257 polledPwmTotalTime += t.read_us();
mjr 74:822a92bc11d2 1258 polledPwmRunCount += 1;
mjr 74:822a92bc11d2 1259 )
mjr 74:822a92bc11d2 1260 }
mjr 74:822a92bc11d2 1261 }
mjr 64:ef7ca92dff36 1262
mjr 26:cb71c4af2912 1263 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1264 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1265 {
mjr 6:cc35eb643e8f 1266 public:
mjr 43:7a6364d82a41 1267 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1268 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1269 {
mjr 13:72dda449c3c0 1270 if (val != prv)
mjr 40:cc0d9814522b 1271 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1272 }
mjr 6:cc35eb643e8f 1273 DigitalOut p;
mjr 40:cc0d9814522b 1274 uint8_t prv;
mjr 6:cc35eb643e8f 1275 };
mjr 26:cb71c4af2912 1276
mjr 29:582472d0bc57 1277 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1278 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1279 // port n (0-based).
mjr 35:e959ffba78fd 1280 //
mjr 35:e959ffba78fd 1281 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1282 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1283 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1284 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1285 // 74HC595 ports).
mjr 33:d832bcab089e 1286 static int numOutputs;
mjr 33:d832bcab089e 1287 static LwOut **lwPin;
mjr 33:d832bcab089e 1288
mjr 38:091e511ce8a0 1289 // create a single output pin
mjr 53:9b2611964afc 1290 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1291 {
mjr 38:091e511ce8a0 1292 // get this item's values
mjr 38:091e511ce8a0 1293 int typ = pc.typ;
mjr 38:091e511ce8a0 1294 int pin = pc.pin;
mjr 38:091e511ce8a0 1295 int flags = pc.flags;
mjr 40:cc0d9814522b 1296 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1297 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1298 int gamma = flags & PortFlagGamma;
mjr 38:091e511ce8a0 1299
mjr 38:091e511ce8a0 1300 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1301 LwOut *lwp;
mjr 38:091e511ce8a0 1302 switch (typ)
mjr 38:091e511ce8a0 1303 {
mjr 38:091e511ce8a0 1304 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1305 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1306 if (pin != 0)
mjr 64:ef7ca92dff36 1307 {
mjr 64:ef7ca92dff36 1308 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 1309 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 1310 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 1311 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 1312 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 1313 {
mjr 64:ef7ca92dff36 1314 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 1315 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 1316
mjr 64:ef7ca92dff36 1317 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 1318 gamma = false;
mjr 64:ef7ca92dff36 1319 }
mjr 64:ef7ca92dff36 1320 else
mjr 64:ef7ca92dff36 1321 {
mjr 64:ef7ca92dff36 1322 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 1323 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 1324 }
mjr 64:ef7ca92dff36 1325 }
mjr 48:058ace2aed1d 1326 else
mjr 48:058ace2aed1d 1327 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1328 break;
mjr 38:091e511ce8a0 1329
mjr 38:091e511ce8a0 1330 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1331 // Digital GPIO port
mjr 48:058ace2aed1d 1332 if (pin != 0)
mjr 48:058ace2aed1d 1333 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1334 else
mjr 48:058ace2aed1d 1335 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1336 break;
mjr 38:091e511ce8a0 1337
mjr 38:091e511ce8a0 1338 case PortTypeTLC5940:
mjr 38:091e511ce8a0 1339 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 1340 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 1341 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 1342 {
mjr 40:cc0d9814522b 1343 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 1344 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 1345 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 1346 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 1347 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 1348 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 1349 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 1350 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 1351 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 1352 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 1353 // for this unlikely case.
mjr 40:cc0d9814522b 1354 if (gamma && !activeLow)
mjr 40:cc0d9814522b 1355 {
mjr 40:cc0d9814522b 1356 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 1357 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 1358
mjr 40:cc0d9814522b 1359 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 1360 gamma = false;
mjr 40:cc0d9814522b 1361 }
mjr 40:cc0d9814522b 1362 else
mjr 40:cc0d9814522b 1363 {
mjr 40:cc0d9814522b 1364 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 1365 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 1366 }
mjr 40:cc0d9814522b 1367 }
mjr 38:091e511ce8a0 1368 else
mjr 40:cc0d9814522b 1369 {
mjr 40:cc0d9814522b 1370 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 1371 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 1372 }
mjr 38:091e511ce8a0 1373 break;
mjr 38:091e511ce8a0 1374
mjr 38:091e511ce8a0 1375 case PortType74HC595:
mjr 38:091e511ce8a0 1376 // 74HC595 port (if we don't have an HC595 controller object, or it's not a valid
mjr 38:091e511ce8a0 1377 // output number, create a virtual port)
mjr 38:091e511ce8a0 1378 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 1379 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 1380 else
mjr 38:091e511ce8a0 1381 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1382 break;
mjr 38:091e511ce8a0 1383
mjr 38:091e511ce8a0 1384 case PortTypeVirtual:
mjr 43:7a6364d82a41 1385 case PortTypeDisabled:
mjr 38:091e511ce8a0 1386 default:
mjr 38:091e511ce8a0 1387 // virtual or unknown
mjr 38:091e511ce8a0 1388 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1389 break;
mjr 38:091e511ce8a0 1390 }
mjr 38:091e511ce8a0 1391
mjr 40:cc0d9814522b 1392 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 1393 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 1394 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 1395 if (activeLow)
mjr 38:091e511ce8a0 1396 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 1397
mjr 40:cc0d9814522b 1398 // If it's a noisemaker, layer on a night mode switch. Note that this
mjr 40:cc0d9814522b 1399 // needs to be
mjr 40:cc0d9814522b 1400 if (noisy)
mjr 40:cc0d9814522b 1401 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 1402
mjr 40:cc0d9814522b 1403 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 1404 if (gamma)
mjr 40:cc0d9814522b 1405 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 1406
mjr 53:9b2611964afc 1407 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 1408 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 1409 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 1410 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 1411 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 1412
mjr 53:9b2611964afc 1413 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 1414 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 1415 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 1416
mjr 38:091e511ce8a0 1417 // turn it off initially
mjr 38:091e511ce8a0 1418 lwp->set(0);
mjr 38:091e511ce8a0 1419
mjr 38:091e511ce8a0 1420 // return the pin
mjr 38:091e511ce8a0 1421 return lwp;
mjr 38:091e511ce8a0 1422 }
mjr 38:091e511ce8a0 1423
mjr 6:cc35eb643e8f 1424 // initialize the output pin array
mjr 35:e959ffba78fd 1425 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 1426 {
mjr 35:e959ffba78fd 1427 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 1428 // total number of ports.
mjr 35:e959ffba78fd 1429 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 1430 int i;
mjr 35:e959ffba78fd 1431 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 1432 {
mjr 35:e959ffba78fd 1433 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 1434 {
mjr 35:e959ffba78fd 1435 numOutputs = i;
mjr 34:6b981a2afab7 1436 break;
mjr 34:6b981a2afab7 1437 }
mjr 33:d832bcab089e 1438 }
mjr 33:d832bcab089e 1439
mjr 73:4e8ce0b18915 1440 // allocate the pin array
mjr 73:4e8ce0b18915 1441 lwPin = new LwOut*[numOutputs];
mjr 35:e959ffba78fd 1442
mjr 73:4e8ce0b18915 1443 // Allocate the current brightness array
mjr 73:4e8ce0b18915 1444 outLevel = new uint8_t[numOutputs];
mjr 33:d832bcab089e 1445
mjr 73:4e8ce0b18915 1446 // allocate the LedWiz output state arrays
mjr 73:4e8ce0b18915 1447 wizOn = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 1448 wizVal = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 1449
mjr 73:4e8ce0b18915 1450 // initialize all LedWiz outputs to off and brightness 48
mjr 73:4e8ce0b18915 1451 memset(wizOn, 0, numOutputs);
mjr 73:4e8ce0b18915 1452 memset(wizVal, 48, numOutputs);
mjr 73:4e8ce0b18915 1453
mjr 73:4e8ce0b18915 1454 // set all LedWiz virtual unit flash speeds to 2
mjr 73:4e8ce0b18915 1455 for (i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 1456 wizSpeed[i] = 2;
mjr 33:d832bcab089e 1457
mjr 35:e959ffba78fd 1458 // create the pin interface object for each port
mjr 35:e959ffba78fd 1459 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 1460 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 1461 }
mjr 6:cc35eb643e8f 1462
mjr 76:7f5912b6340e 1463 // Translate an LedWiz brightness level (0..49) to a DOF brightness
mjr 76:7f5912b6340e 1464 // level (0..255). Note that brightness level 49 isn't actually valid,
mjr 76:7f5912b6340e 1465 // according to the LedWiz API documentation, but many clients use it
mjr 76:7f5912b6340e 1466 // anyway, and the real LedWiz accepts it and seems to treat it as
mjr 76:7f5912b6340e 1467 // equivalent to 48.
mjr 40:cc0d9814522b 1468 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 1469 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 1470 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 1471 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 1472 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 1473 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 1474 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 1475 255, 255
mjr 40:cc0d9814522b 1476 };
mjr 40:cc0d9814522b 1477
mjr 76:7f5912b6340e 1478 // Translate a DOF brightness level (0..255) to an LedWiz brightness
mjr 76:7f5912b6340e 1479 // level (1..48)
mjr 76:7f5912b6340e 1480 static const uint8_t dof_to_lw[] = {
mjr 76:7f5912b6340e 1481 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
mjr 76:7f5912b6340e 1482 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
mjr 76:7f5912b6340e 1483 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9,
mjr 76:7f5912b6340e 1484 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12,
mjr 76:7f5912b6340e 1485 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15,
mjr 76:7f5912b6340e 1486 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18,
mjr 76:7f5912b6340e 1487 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21,
mjr 76:7f5912b6340e 1488 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24,
mjr 76:7f5912b6340e 1489 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27,
mjr 76:7f5912b6340e 1490 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30,
mjr 76:7f5912b6340e 1491 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
mjr 76:7f5912b6340e 1492 33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36,
mjr 76:7f5912b6340e 1493 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
mjr 76:7f5912b6340e 1494 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
mjr 76:7f5912b6340e 1495 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45,
mjr 76:7f5912b6340e 1496 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48
mjr 76:7f5912b6340e 1497 };
mjr 76:7f5912b6340e 1498
mjr 74:822a92bc11d2 1499 // LedWiz flash cycle tables. For efficiency, we use a lookup table
mjr 74:822a92bc11d2 1500 // rather than calculating these on the fly. The flash cycles are
mjr 74:822a92bc11d2 1501 // generated by the following formulas, where 'c' is the current
mjr 74:822a92bc11d2 1502 // cycle counter, from 0 to 255:
mjr 74:822a92bc11d2 1503 //
mjr 74:822a92bc11d2 1504 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 1505 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 1506 // mode 131 = on/ramp down = (c < 128 ? 255 : (255-c)*2)
mjr 74:822a92bc11d2 1507 // mode 132 = ramp up/on = (c < 128 ? c*2 : 255)
mjr 74:822a92bc11d2 1508 //
mjr 74:822a92bc11d2 1509 // To look up the current output value for a given mode and a given
mjr 74:822a92bc11d2 1510 // cycle counter 'c', index the table with ((mode-129)*256)+c.
mjr 74:822a92bc11d2 1511 static const uint8_t wizFlashLookup[] = {
mjr 74:822a92bc11d2 1512 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 1513 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
mjr 74:822a92bc11d2 1514 0x21, 0x23, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f,
mjr 74:822a92bc11d2 1515 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f,
mjr 74:822a92bc11d2 1516 0x61, 0x63, 0x65, 0x67, 0x69, 0x6b, 0x6d, 0x6f, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d, 0x7f,
mjr 74:822a92bc11d2 1517 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d, 0x8f, 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9d, 0x9f,
mjr 74:822a92bc11d2 1518 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad, 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, 0xbf,
mjr 74:822a92bc11d2 1519 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
mjr 74:822a92bc11d2 1520 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb, 0xed, 0xef, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0xfd, 0xff,
mjr 74:822a92bc11d2 1521 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 1522 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 1523 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 1524 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 1525 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 1526 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 1527 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 1528 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 1529
mjr 74:822a92bc11d2 1530 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 1531 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1532 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1533 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1534 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1535 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1536 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1537 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1538 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1539 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1540 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1541 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1542 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1543 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1544 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1545 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1546 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1547
mjr 74:822a92bc11d2 1548 // mode 131 = on/ramp down = c < 128 ? 255 : (255 - c)*2
mjr 74:822a92bc11d2 1549 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1550 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1551 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1552 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1553 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1554 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1555 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1556 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1557 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 1558 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 1559 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 1560 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 1561 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 1562 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 1563 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 1564 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 1565
mjr 74:822a92bc11d2 1566 // mode 132 = ramp up/on = c < 128 ? c*2 : 255
mjr 74:822a92bc11d2 1567 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
mjr 74:822a92bc11d2 1568 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
mjr 74:822a92bc11d2 1569 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
mjr 74:822a92bc11d2 1570 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
mjr 74:822a92bc11d2 1571 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
mjr 74:822a92bc11d2 1572 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
mjr 74:822a92bc11d2 1573 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
mjr 74:822a92bc11d2 1574 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
mjr 74:822a92bc11d2 1575 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1576 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1577 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1578 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1579 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1580 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1581 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1582 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
mjr 74:822a92bc11d2 1583 };
mjr 74:822a92bc11d2 1584
mjr 74:822a92bc11d2 1585 // LedWiz flash cycle timer. This runs continuously. On each update,
mjr 74:822a92bc11d2 1586 // we use this to figure out where we are on the cycle for each bank.
mjr 74:822a92bc11d2 1587 Timer wizCycleTimer;
mjr 74:822a92bc11d2 1588
mjr 76:7f5912b6340e 1589 // timing statistics for wizPulse()
mjr 76:7f5912b6340e 1590 uint64_t wizPulseTotalTime, wizPulseRunCount;
mjr 76:7f5912b6340e 1591
mjr 76:7f5912b6340e 1592 // LedWiz flash timer pulse. The main loop calls this on each cycle
mjr 76:7f5912b6340e 1593 // to update outputs using LedWiz flash modes. We do one bank of 32
mjr 76:7f5912b6340e 1594 // outputs on each cycle.
mjr 29:582472d0bc57 1595 static void wizPulse()
mjr 29:582472d0bc57 1596 {
mjr 76:7f5912b6340e 1597 // current bank
mjr 76:7f5912b6340e 1598 static int wizPulseBank = 0;
mjr 76:7f5912b6340e 1599
mjr 76:7f5912b6340e 1600 // start a timer for statistics collection
mjr 76:7f5912b6340e 1601 IF_DIAG(
mjr 76:7f5912b6340e 1602 Timer t;
mjr 76:7f5912b6340e 1603 t.start();
mjr 76:7f5912b6340e 1604 )
mjr 76:7f5912b6340e 1605
mjr 76:7f5912b6340e 1606 // Update the current bank's cycle counter: figure the current
mjr 76:7f5912b6340e 1607 // phase of the LedWiz pulse cycle for this bank.
mjr 76:7f5912b6340e 1608 //
mjr 76:7f5912b6340e 1609 // The LedWiz speed setting gives the flash period in 0.25s units
mjr 76:7f5912b6340e 1610 // (speed 1 is a flash period of .25s, speed 7 is a period of 1.75s).
mjr 76:7f5912b6340e 1611 //
mjr 76:7f5912b6340e 1612 // What we're after here is the "phase", which is to say the point
mjr 76:7f5912b6340e 1613 // in the current cycle. If we assume that the cycle has been running
mjr 76:7f5912b6340e 1614 // continuously since some arbitrary time zero in the past, we can
mjr 76:7f5912b6340e 1615 // figure where we are in the current cycle by dividing the time since
mjr 76:7f5912b6340e 1616 // that zero by the cycle period and taking the remainder. E.g., if
mjr 76:7f5912b6340e 1617 // the cycle time is 5 seconds, and the time since t-zero is 17 seconds,
mjr 76:7f5912b6340e 1618 // we divide 17 by 5 to get a remainder of 2. That says we're 2 seconds
mjr 76:7f5912b6340e 1619 // into the current 5-second cycle, or 2/5 of the way through the
mjr 76:7f5912b6340e 1620 // current cycle.
mjr 76:7f5912b6340e 1621 //
mjr 76:7f5912b6340e 1622 // We do this calculation on every iteration of the main loop, so we
mjr 76:7f5912b6340e 1623 // want it to be very fast. To streamline it, we'll use some tricky
mjr 76:7f5912b6340e 1624 // integer arithmetic. The result will be the same as the straightforward
mjr 76:7f5912b6340e 1625 // remainder and fraction calculation we just explained, but we'll get
mjr 76:7f5912b6340e 1626 // there by less-than-obvious means.
mjr 76:7f5912b6340e 1627 //
mjr 76:7f5912b6340e 1628 // Rather than finding the phase as a continuous quantity or floating
mjr 76:7f5912b6340e 1629 // point number, we'll quantize it. We'll divide each cycle into 256
mjr 76:7f5912b6340e 1630 // time units, or quanta. Each quantum is 1/256 of the cycle length,
mjr 76:7f5912b6340e 1631 // so for a 1-second cycle (LedWiz speed 4), each quantum is 1/256 of
mjr 76:7f5912b6340e 1632 // a second, or about 3.9ms. If we express the time since t-zero in
mjr 76:7f5912b6340e 1633 // these units, the time period of one cycle is exactly 256 units, so
mjr 76:7f5912b6340e 1634 // we can calculate our point in the cycle by taking the remainder of
mjr 76:7f5912b6340e 1635 // the time (in our funny units) divided by 256. The special thing
mjr 76:7f5912b6340e 1636 // about making the cycle time equal to 256 units is that "x % 256"
mjr 76:7f5912b6340e 1637 // is exactly the same as "x & 255", which is a much faster operation
mjr 76:7f5912b6340e 1638 // than division on ARM M0+: this CPU has no hardware DIVIDE operation,
mjr 76:7f5912b6340e 1639 // so an integer division takes about 5us. The bit mask operation, in
mjr 76:7f5912b6340e 1640 // contrast, takes only about 60ns - about 100x faster. 5us doesn't
mjr 76:7f5912b6340e 1641 // sound like much, but we do this on every main loop, so every little
mjr 76:7f5912b6340e 1642 // bit counts.
mjr 76:7f5912b6340e 1643 //
mjr 76:7f5912b6340e 1644 // The snag is that our system timer gives us the elapsed time in
mjr 76:7f5912b6340e 1645 // microseconds. We still need to convert this to our special quanta
mjr 76:7f5912b6340e 1646 // of 256 units per cycle. The straightforward way to do that is by
mjr 76:7f5912b6340e 1647 // dividing by (microseconds per quantum). E.g., for LedWiz speed 4,
mjr 76:7f5912b6340e 1648 // we decided that our quantum was 1/256 of a second, or 3906us, so
mjr 76:7f5912b6340e 1649 // dividing the current system time in microseconds by 3906 will give
mjr 76:7f5912b6340e 1650 // us the time in our quantum units. But now we've just substituted
mjr 76:7f5912b6340e 1651 // one division for another!
mjr 76:7f5912b6340e 1652 //
mjr 76:7f5912b6340e 1653 // This is where our really tricky integer math comes in. Dividing
mjr 76:7f5912b6340e 1654 // by X is the same as multiplying by 1/X. In integer math, 1/3906
mjr 76:7f5912b6340e 1655 // is zero, so that won't work. But we can get around that by doing
mjr 76:7f5912b6340e 1656 // the integer math as "fixed point" arithmetic instead. It's still
mjr 76:7f5912b6340e 1657 // actually carried out as integer operations, but we'll scale our
mjr 76:7f5912b6340e 1658 // integers by a scaling factor, then take out the scaling factor
mjr 76:7f5912b6340e 1659 // later to get the final result. The scaling factor we'll use is
mjr 76:7f5912b6340e 1660 // 2^24. So we're going to calculate (time * 2^24/3906), then divide
mjr 76:7f5912b6340e 1661 // the result by 2^24 to get the final answer. I know it seems like
mjr 76:7f5912b6340e 1662 // we're substituting one division for another yet again, but this
mjr 76:7f5912b6340e 1663 // time's the charm, because dividing by 2^24 is a bit shift operation,
mjr 76:7f5912b6340e 1664 // which is another single-cycle operation on M0+. You might also
mjr 76:7f5912b6340e 1665 // wonder how all these tricks don't cause overflows or underflows
mjr 76:7f5912b6340e 1666 // or what not. Well, the multiply by 2^24/3906 will cause an
mjr 76:7f5912b6340e 1667 // overflow, but we don't care, because the overflow will all be in
mjr 76:7f5912b6340e 1668 // the high-order bits that we're going to discard in the final
mjr 76:7f5912b6340e 1669 // remainder calculation anyway.
mjr 76:7f5912b6340e 1670 //
mjr 76:7f5912b6340e 1671 // Each entry in the array below represents 2^24/N for the corresponding
mjr 76:7f5912b6340e 1672 // LedWiz speed, where N is the number of time quanta per cycle at that
mjr 76:7f5912b6340e 1673 // speed. The time quanta are chosen such that 256 quanta add up to
mjr 76:7f5912b6340e 1674 // approximately (LedWiz speed setting * 0.25s).
mjr 76:7f5912b6340e 1675 //
mjr 76:7f5912b6340e 1676 // Note that the calculation has an implicit bit mask (result & 0xFF)
mjr 76:7f5912b6340e 1677 // to get the final result mod 256. But we don't have to actually
mjr 76:7f5912b6340e 1678 // do that work because we're using 32-bit ints and a 2^24 fixed
mjr 76:7f5912b6340e 1679 // point base (X in the narrative above). The final shift right by
mjr 76:7f5912b6340e 1680 // 24 bits to divide out the base will leave us with only 8 bits in
mjr 76:7f5912b6340e 1681 // the result, since we started with 32.
mjr 76:7f5912b6340e 1682 static const uint32_t inv_us_per_quantum[] = { // indexed by LedWiz speed
mjr 76:7f5912b6340e 1683 0, 17172, 8590, 5726, 4295, 3436, 2863, 2454
mjr 76:7f5912b6340e 1684 };
mjr 76:7f5912b6340e 1685 int counter = ((wizCycleTimer.read_us() * inv_us_per_quantum[wizSpeed[wizPulseBank]]) >> 24);
mjr 76:7f5912b6340e 1686
mjr 76:7f5912b6340e 1687 // get the range of 32 output sin this bank
mjr 76:7f5912b6340e 1688 int fromPort = wizPulseBank*32;
mjr 76:7f5912b6340e 1689 int toPort = fromPort+32;
mjr 76:7f5912b6340e 1690 if (toPort > numOutputs)
mjr 76:7f5912b6340e 1691 toPort = numOutputs;
mjr 76:7f5912b6340e 1692
mjr 76:7f5912b6340e 1693 // update all outputs set to flashing values
mjr 76:7f5912b6340e 1694 for (int i = fromPort ; i < toPort ; ++i)
mjr 73:4e8ce0b18915 1695 {
mjr 76:7f5912b6340e 1696 // Update the port only if the LedWiz SBA switch for the port is on
mjr 76:7f5912b6340e 1697 // (wizOn[i]) AND the port is a PBA flash mode in the range 129..132.
mjr 76:7f5912b6340e 1698 // These modes and only these modes have the high bit (0x80) set, so
mjr 76:7f5912b6340e 1699 // we can test for them simply by testing the high bit.
mjr 76:7f5912b6340e 1700 if (wizOn[i])
mjr 29:582472d0bc57 1701 {
mjr 76:7f5912b6340e 1702 uint8_t val = wizVal[i];
mjr 76:7f5912b6340e 1703 if ((val & 0x80) != 0)
mjr 29:582472d0bc57 1704 {
mjr 76:7f5912b6340e 1705 // ook up the value for the mode at the cycle time
mjr 76:7f5912b6340e 1706 lwPin[i]->set(outLevel[i] = wizFlashLookup[((val-129) << 8) + counter]);
mjr 29:582472d0bc57 1707 }
mjr 29:582472d0bc57 1708 }
mjr 76:7f5912b6340e 1709 }
mjr 76:7f5912b6340e 1710
mjr 34:6b981a2afab7 1711 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 1712 if (hc595 != 0)
mjr 35:e959ffba78fd 1713 hc595->update();
mjr 76:7f5912b6340e 1714
mjr 76:7f5912b6340e 1715 // switch to the next bank
mjr 76:7f5912b6340e 1716 if (++wizPulseBank >= MAX_LW_BANKS)
mjr 76:7f5912b6340e 1717 wizPulseBank = 0;
mjr 76:7f5912b6340e 1718
mjr 76:7f5912b6340e 1719 // collect timing statistics
mjr 76:7f5912b6340e 1720 IF_DIAG(
mjr 76:7f5912b6340e 1721 wizPulseTotalTime += t.read_us();
mjr 76:7f5912b6340e 1722 wizPulseRunCount += 1;
mjr 76:7f5912b6340e 1723 )
mjr 1:d913e0afb2ac 1724 }
mjr 38:091e511ce8a0 1725
mjr 76:7f5912b6340e 1726 // Update a port to reflect its new LedWiz SBA+PBA setting.
mjr 76:7f5912b6340e 1727 static void updateLwPort(int port)
mjr 38:091e511ce8a0 1728 {
mjr 76:7f5912b6340e 1729 // check if the SBA switch is on or off
mjr 76:7f5912b6340e 1730 if (wizOn[port])
mjr 76:7f5912b6340e 1731 {
mjr 76:7f5912b6340e 1732 // It's on. If the port is a valid static brightness level,
mjr 76:7f5912b6340e 1733 // set the output port to match. Otherwise leave it as is:
mjr 76:7f5912b6340e 1734 // if it's a flashing mode, the flash mode pulse will update
mjr 76:7f5912b6340e 1735 // it on the next cycle.
mjr 76:7f5912b6340e 1736 int val = wizVal[port];
mjr 76:7f5912b6340e 1737 if (val <= 49)
mjr 76:7f5912b6340e 1738 lwPin[port]->set(outLevel[port] = lw_to_dof[val]);
mjr 76:7f5912b6340e 1739 }
mjr 76:7f5912b6340e 1740 else
mjr 76:7f5912b6340e 1741 {
mjr 76:7f5912b6340e 1742 // the port is off - set absolute brightness zero
mjr 76:7f5912b6340e 1743 lwPin[port]->set(outLevel[port] = 0);
mjr 76:7f5912b6340e 1744 }
mjr 73:4e8ce0b18915 1745 }
mjr 73:4e8ce0b18915 1746
mjr 73:4e8ce0b18915 1747 // Turn off all outputs and restore everything to the default LedWiz
mjr 73:4e8ce0b18915 1748 // state. This sets outputs #1-32 to LedWiz profile value 48 (full
mjr 73:4e8ce0b18915 1749 // brightness) and switch state Off, sets all extended outputs (#33
mjr 73:4e8ce0b18915 1750 // and above) to zero brightness, and sets the LedWiz flash rate to 2.
mjr 73:4e8ce0b18915 1751 // This effectively restores the power-on conditions.
mjr 73:4e8ce0b18915 1752 //
mjr 73:4e8ce0b18915 1753 void allOutputsOff()
mjr 73:4e8ce0b18915 1754 {
mjr 73:4e8ce0b18915 1755 // reset all LedWiz outputs to OFF/48
mjr 73:4e8ce0b18915 1756 for (int i = 0 ; i < numOutputs ; ++i)
mjr 73:4e8ce0b18915 1757 {
mjr 73:4e8ce0b18915 1758 outLevel[i] = 0;
mjr 73:4e8ce0b18915 1759 wizOn[i] = 0;
mjr 73:4e8ce0b18915 1760 wizVal[i] = 48;
mjr 73:4e8ce0b18915 1761 lwPin[i]->set(0);
mjr 73:4e8ce0b18915 1762 }
mjr 73:4e8ce0b18915 1763
mjr 73:4e8ce0b18915 1764 // restore default LedWiz flash rate
mjr 73:4e8ce0b18915 1765 for (int i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 1766 wizSpeed[i] = 2;
mjr 38:091e511ce8a0 1767
mjr 73:4e8ce0b18915 1768 // flush changes to hc595, if applicable
mjr 38:091e511ce8a0 1769 if (hc595 != 0)
mjr 38:091e511ce8a0 1770 hc595->update();
mjr 38:091e511ce8a0 1771 }
mjr 38:091e511ce8a0 1772
mjr 74:822a92bc11d2 1773 // Cary out an SBA or SBX message. portGroup is 0 for ports 1-32,
mjr 74:822a92bc11d2 1774 // 1 for ports 33-64, etc. Original protocol SBA messages always
mjr 74:822a92bc11d2 1775 // address port group 0; our private SBX extension messages can
mjr 74:822a92bc11d2 1776 // address any port group.
mjr 74:822a92bc11d2 1777 void sba_sbx(int portGroup, const uint8_t *data)
mjr 74:822a92bc11d2 1778 {
mjr 76:7f5912b6340e 1779 // update all on/off states in the group
mjr 74:822a92bc11d2 1780 for (int i = 0, bit = 1, imsg = 1, port = portGroup*32 ;
mjr 74:822a92bc11d2 1781 i < 32 && port < numOutputs ;
mjr 74:822a92bc11d2 1782 ++i, bit <<= 1, ++port)
mjr 74:822a92bc11d2 1783 {
mjr 74:822a92bc11d2 1784 // figure the on/off state bit for this output
mjr 74:822a92bc11d2 1785 if (bit == 0x100) {
mjr 74:822a92bc11d2 1786 bit = 1;
mjr 74:822a92bc11d2 1787 ++imsg;
mjr 74:822a92bc11d2 1788 }
mjr 74:822a92bc11d2 1789
mjr 74:822a92bc11d2 1790 // set the on/off state
mjr 76:7f5912b6340e 1791 bool on = wizOn[port] = ((data[imsg] & bit) != 0);
mjr 76:7f5912b6340e 1792
mjr 76:7f5912b6340e 1793 // set the output port brightness to match the new setting
mjr 76:7f5912b6340e 1794 updateLwPort(port);
mjr 74:822a92bc11d2 1795 }
mjr 74:822a92bc11d2 1796
mjr 74:822a92bc11d2 1797 // set the flash speed for the port group
mjr 74:822a92bc11d2 1798 if (portGroup < countof(wizSpeed))
mjr 74:822a92bc11d2 1799 wizSpeed[portGroup] = (data[5] < 1 ? 1 : data[5] > 7 ? 7 : data[5]);
mjr 74:822a92bc11d2 1800
mjr 76:7f5912b6340e 1801 // update 74HC959 outputs
mjr 76:7f5912b6340e 1802 if (hc595 != 0)
mjr 76:7f5912b6340e 1803 hc595->update();
mjr 74:822a92bc11d2 1804 }
mjr 74:822a92bc11d2 1805
mjr 74:822a92bc11d2 1806 // Carry out a PBA or PBX message.
mjr 74:822a92bc11d2 1807 void pba_pbx(int basePort, const uint8_t *data)
mjr 74:822a92bc11d2 1808 {
mjr 74:822a92bc11d2 1809 // update each wizVal entry from the brightness data
mjr 76:7f5912b6340e 1810 for (int i = 0, port = basePort ; i < 8 && port < numOutputs ; ++i, ++port)
mjr 74:822a92bc11d2 1811 {
mjr 74:822a92bc11d2 1812 // get the value
mjr 74:822a92bc11d2 1813 uint8_t v = data[i];
mjr 74:822a92bc11d2 1814
mjr 74:822a92bc11d2 1815 // Validate it. The legal values are 0..49 for brightness
mjr 74:822a92bc11d2 1816 // levels, and 128..132 for flash modes. Set anything invalid
mjr 74:822a92bc11d2 1817 // to full brightness (48) instead. Note that 49 isn't actually
mjr 74:822a92bc11d2 1818 // a valid documented value, but in practice some clients send
mjr 74:822a92bc11d2 1819 // this to mean 100% brightness, and the real LedWiz treats it
mjr 74:822a92bc11d2 1820 // as such.
mjr 74:822a92bc11d2 1821 if ((v > 49 && v < 129) || v > 132)
mjr 74:822a92bc11d2 1822 v = 48;
mjr 74:822a92bc11d2 1823
mjr 74:822a92bc11d2 1824 // store it
mjr 76:7f5912b6340e 1825 wizVal[port] = v;
mjr 76:7f5912b6340e 1826
mjr 76:7f5912b6340e 1827 // update the port
mjr 76:7f5912b6340e 1828 updateLwPort(port);
mjr 74:822a92bc11d2 1829 }
mjr 74:822a92bc11d2 1830
mjr 76:7f5912b6340e 1831 // update 74HC595 outputs
mjr 76:7f5912b6340e 1832 if (hc595 != 0)
mjr 76:7f5912b6340e 1833 hc595->update();
mjr 74:822a92bc11d2 1834 }
mjr 74:822a92bc11d2 1835
mjr 77:0b96f6867312 1836 // ---------------------------------------------------------------------------
mjr 77:0b96f6867312 1837 //
mjr 77:0b96f6867312 1838 // IR Remote Control transmitter & receiver
mjr 77:0b96f6867312 1839 //
mjr 77:0b96f6867312 1840
mjr 77:0b96f6867312 1841 // receiver
mjr 77:0b96f6867312 1842 IRReceiver *ir_rx;
mjr 77:0b96f6867312 1843
mjr 77:0b96f6867312 1844 // transmitter
mjr 77:0b96f6867312 1845 IRTransmitter *ir_tx;
mjr 77:0b96f6867312 1846
mjr 77:0b96f6867312 1847 // Mapping from IR commands slots in the configuration to "virtual button"
mjr 77:0b96f6867312 1848 // numbers on the IRTransmitter's "virtual remote". To minimize RAM usage,
mjr 77:0b96f6867312 1849 // we only create virtual buttons on the transmitter object for code slots
mjr 77:0b96f6867312 1850 // that are configured for transmission, which includes slots used for TV
mjr 77:0b96f6867312 1851 // ON commands and slots that can be triggered by button presses. This
mjr 77:0b96f6867312 1852 // means that virtual button numbers won't necessarily match the config
mjr 77:0b96f6867312 1853 // slot numbers. This table provides the mapping:
mjr 77:0b96f6867312 1854 // IRConfigSlotToVirtualButton[n] = ir_tx virtual button number for
mjr 77:0b96f6867312 1855 // configuration slot n
mjr 77:0b96f6867312 1856 uint8_t IRConfigSlotToVirtualButton[MAX_IR_CODES];
mjr 78:1e00b3fa11af 1857
mjr 78:1e00b3fa11af 1858 // IR transmitter virtual button number for ad hoc IR command. We allocate
mjr 78:1e00b3fa11af 1859 // one virtual button for sending ad hoc IR codes, such as through the USB
mjr 78:1e00b3fa11af 1860 // protocol.
mjr 78:1e00b3fa11af 1861 uint8_t IRAdHocBtn;
mjr 78:1e00b3fa11af 1862
mjr 78:1e00b3fa11af 1863 // Staging area for ad hoc IR commands. It takes multiple messages
mjr 78:1e00b3fa11af 1864 // to fill out an IR command, so we store the partial command here
mjr 78:1e00b3fa11af 1865 // while waiting for the rest.
mjr 78:1e00b3fa11af 1866 static struct
mjr 78:1e00b3fa11af 1867 {
mjr 78:1e00b3fa11af 1868 uint8_t protocol; // protocol ID
mjr 78:1e00b3fa11af 1869 uint64_t code; // code
mjr 78:1e00b3fa11af 1870 uint8_t dittos : 1; // using dittos?
mjr 78:1e00b3fa11af 1871 uint8_t ready : 1; // do we have a code ready to transmit?
mjr 78:1e00b3fa11af 1872 } IRAdHocCmd;
mjr 78:1e00b3fa11af 1873
mjr 77:0b96f6867312 1874
mjr 77:0b96f6867312 1875 // IR mode timer. In normal mode, this is the time since the last
mjr 77:0b96f6867312 1876 // command received; we use this to handle commands with timed effects,
mjr 77:0b96f6867312 1877 // such as sending a key to the PC. In learning mode, this is the time
mjr 77:0b96f6867312 1878 // since we activated learning mode, which we use to automatically end
mjr 77:0b96f6867312 1879 // learning mode if a decodable command isn't received within a reasonable
mjr 77:0b96f6867312 1880 // amount of time.
mjr 77:0b96f6867312 1881 Timer IRTimer;
mjr 77:0b96f6867312 1882
mjr 77:0b96f6867312 1883 // IR Learning Mode. The PC enters learning mode via special function 65 12.
mjr 77:0b96f6867312 1884 // The states are:
mjr 77:0b96f6867312 1885 //
mjr 77:0b96f6867312 1886 // 0 -> normal operation (not in learning mode)
mjr 77:0b96f6867312 1887 // 1 -> learning mode; reading raw codes, no command read yet
mjr 77:0b96f6867312 1888 // 2 -> learning mode; command received, awaiting auto-repeat
mjr 77:0b96f6867312 1889 // 3 -> learning mode; done, command and repeat mode decoded
mjr 77:0b96f6867312 1890 //
mjr 77:0b96f6867312 1891 // When we enter learning mode, we reset IRTimer to keep track of how long
mjr 77:0b96f6867312 1892 // we've been in the mode. This allows the mode to time out if no code is
mjr 77:0b96f6867312 1893 // received within a reasonable time.
mjr 77:0b96f6867312 1894 uint8_t IRLearningMode = 0;
mjr 77:0b96f6867312 1895
mjr 77:0b96f6867312 1896 // Learning mode command received. This stores the first decoded command
mjr 77:0b96f6867312 1897 // when in learning mode. For some protocols, we can't just report the
mjr 77:0b96f6867312 1898 // first command we receive, because we need to wait for an auto-repeat to
mjr 77:0b96f6867312 1899 // determine what format the remote uses for repeats. This stores the first
mjr 77:0b96f6867312 1900 // command while we await a repeat. This is necessary for protocols that
mjr 77:0b96f6867312 1901 // have "dittos", since some remotes for such protocols use the dittos and
mjr 77:0b96f6867312 1902 // some don't; the only way to find out is to read a repeat code and see if
mjr 77:0b96f6867312 1903 // it's a ditto or just a repeat of the full code.
mjr 77:0b96f6867312 1904 IRCommand learnedIRCode;
mjr 77:0b96f6867312 1905
mjr 78:1e00b3fa11af 1906 // IR command received, as a config slot index, 1..MAX_IR_CODES.
mjr 77:0b96f6867312 1907 // When we receive a command that matches one of our programmed commands,
mjr 77:0b96f6867312 1908 // we note the slot here. We also reset the IR timer so that we know how
mjr 77:0b96f6867312 1909 // long it's been since the command came in. This lets us handle commands
mjr 77:0b96f6867312 1910 // with timed effects, such as PC key input. Note that this is a 1-based
mjr 77:0b96f6867312 1911 // index; 0 represents no command.
mjr 77:0b96f6867312 1912 uint8_t IRCommandIn = 0;
mjr 77:0b96f6867312 1913
mjr 77:0b96f6867312 1914 // "Toggle bit" of last command. Some IR protocols have a toggle bit
mjr 77:0b96f6867312 1915 // that distinguishes an auto-repeating key from a key being pressed
mjr 77:0b96f6867312 1916 // several times in a row. This records the toggle bit of the last
mjr 77:0b96f6867312 1917 // command we received.
mjr 77:0b96f6867312 1918 uint8_t lastIRToggle = 0;
mjr 77:0b96f6867312 1919
mjr 77:0b96f6867312 1920 // Are we in a gap between successive key presses? When we detect that a
mjr 77:0b96f6867312 1921 // key is being pressed multiple times rather than auto-repeated (which we
mjr 77:0b96f6867312 1922 // can detect via a toggle bit in some protocols), we'll briefly stop sending
mjr 77:0b96f6867312 1923 // the associated key to the PC, so that the PC likewise recognizes the
mjr 77:0b96f6867312 1924 // distinct key press.
mjr 77:0b96f6867312 1925 uint8_t IRKeyGap = false;
mjr 77:0b96f6867312 1926
mjr 78:1e00b3fa11af 1927
mjr 77:0b96f6867312 1928 // initialize
mjr 77:0b96f6867312 1929 void init_IR(Config &cfg, bool &kbKeys)
mjr 77:0b96f6867312 1930 {
mjr 77:0b96f6867312 1931 PinName pin;
mjr 77:0b96f6867312 1932
mjr 77:0b96f6867312 1933 // start the IR timer
mjr 77:0b96f6867312 1934 IRTimer.start();
mjr 77:0b96f6867312 1935
mjr 77:0b96f6867312 1936 // if there's a transmitter, set it up
mjr 77:0b96f6867312 1937 if ((pin = wirePinName(cfg.IR.emitter)) != NC)
mjr 77:0b96f6867312 1938 {
mjr 77:0b96f6867312 1939 // no virtual buttons yet
mjr 77:0b96f6867312 1940 int nVirtualButtons = 0;
mjr 77:0b96f6867312 1941 memset(IRConfigSlotToVirtualButton, 0xFF, sizeof(IRConfigSlotToVirtualButton));
mjr 77:0b96f6867312 1942
mjr 77:0b96f6867312 1943 // assign virtual buttons slots for TV ON codes
mjr 77:0b96f6867312 1944 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 1945 {
mjr 77:0b96f6867312 1946 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 1947 IRConfigSlotToVirtualButton[i] = nVirtualButtons++;
mjr 77:0b96f6867312 1948 }
mjr 77:0b96f6867312 1949
mjr 77:0b96f6867312 1950 // assign virtual buttons for codes that can be triggered by
mjr 77:0b96f6867312 1951 // real button inputs
mjr 77:0b96f6867312 1952 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 77:0b96f6867312 1953 {
mjr 77:0b96f6867312 1954 // get the button
mjr 77:0b96f6867312 1955 ButtonCfg &b = cfg.button[i];
mjr 77:0b96f6867312 1956
mjr 77:0b96f6867312 1957 // check the unshifted button
mjr 77:0b96f6867312 1958 int c = b.IRCommand - 1;
mjr 77:0b96f6867312 1959 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 1960 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 1961 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 1962
mjr 77:0b96f6867312 1963 // check the shifted button
mjr 77:0b96f6867312 1964 c = b.IRCommand2 - 1;
mjr 77:0b96f6867312 1965 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 1966 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 1967 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 1968 }
mjr 77:0b96f6867312 1969
mjr 77:0b96f6867312 1970 // allocate an additional virtual button for transmitting ad hoc
mjr 77:0b96f6867312 1971 // codes, such as for the "send code" USB API function
mjr 78:1e00b3fa11af 1972 IRAdHocBtn = nVirtualButtons++;
mjr 77:0b96f6867312 1973
mjr 77:0b96f6867312 1974 // create the transmitter
mjr 77:0b96f6867312 1975 ir_tx = new IRTransmitter(pin, nVirtualButtons);
mjr 77:0b96f6867312 1976
mjr 77:0b96f6867312 1977 // program the commands into the virtual button slots
mjr 77:0b96f6867312 1978 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 1979 {
mjr 77:0b96f6867312 1980 // if this slot is assigned to a virtual button, program it
mjr 77:0b96f6867312 1981 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 1982 if (vb != 0xFF)
mjr 77:0b96f6867312 1983 {
mjr 77:0b96f6867312 1984 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 1985 uint64_t code = cb.code.lo | (uint64_t(cb.code.hi) << 32);
mjr 77:0b96f6867312 1986 bool dittos = (cb.flags & IRFlagDittos) != 0;
mjr 77:0b96f6867312 1987 ir_tx->programButton(vb, cb.protocol, dittos, code);
mjr 77:0b96f6867312 1988 }
mjr 77:0b96f6867312 1989 }
mjr 77:0b96f6867312 1990 }
mjr 77:0b96f6867312 1991
mjr 77:0b96f6867312 1992 // if there's a receiver, set it up
mjr 77:0b96f6867312 1993 if ((pin = wirePinName(cfg.IR.sensor)) != NC)
mjr 77:0b96f6867312 1994 {
mjr 77:0b96f6867312 1995 // create the receiver
mjr 77:0b96f6867312 1996 ir_rx = new IRReceiver(pin, 32);
mjr 77:0b96f6867312 1997
mjr 77:0b96f6867312 1998 // connect the transmitter (if any) to the receiver, so that
mjr 77:0b96f6867312 1999 // the receiver can suppress reception of our own transmissions
mjr 77:0b96f6867312 2000 ir_rx->setTransmitter(ir_tx);
mjr 77:0b96f6867312 2001
mjr 77:0b96f6867312 2002 // enable it
mjr 77:0b96f6867312 2003 ir_rx->enable();
mjr 77:0b96f6867312 2004
mjr 77:0b96f6867312 2005 // Check the IR command slots to see if any slots are configured
mjr 77:0b96f6867312 2006 // to send a keyboard key on receiving an IR command. If any are,
mjr 77:0b96f6867312 2007 // tell the caller that we need a USB keyboard interface.
mjr 77:0b96f6867312 2008 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2009 {
mjr 77:0b96f6867312 2010 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2011 if (cb.protocol != 0
mjr 77:0b96f6867312 2012 && (cb.keytype == BtnTypeKey || cb.keytype == BtnTypeMedia))
mjr 77:0b96f6867312 2013 {
mjr 77:0b96f6867312 2014 kbKeys = true;
mjr 77:0b96f6867312 2015 break;
mjr 77:0b96f6867312 2016 }
mjr 77:0b96f6867312 2017 }
mjr 77:0b96f6867312 2018 }
mjr 77:0b96f6867312 2019 }
mjr 77:0b96f6867312 2020
mjr 77:0b96f6867312 2021 // Press or release a button with an assigned IR function. 'cmd'
mjr 77:0b96f6867312 2022 // is the command slot number (1..MAX_IR_CODES) assigned to the button.
mjr 77:0b96f6867312 2023 void IR_buttonChange(uint8_t cmd, bool pressed)
mjr 77:0b96f6867312 2024 {
mjr 77:0b96f6867312 2025 // only proceed if there's an IR transmitter attached
mjr 77:0b96f6867312 2026 if (ir_tx != 0)
mjr 77:0b96f6867312 2027 {
mjr 77:0b96f6867312 2028 // adjust the command slot to a zero-based index
mjr 77:0b96f6867312 2029 int slot = cmd - 1;
mjr 77:0b96f6867312 2030
mjr 77:0b96f6867312 2031 // press or release the virtual button
mjr 77:0b96f6867312 2032 ir_tx->pushButton(IRConfigSlotToVirtualButton[slot], pressed);
mjr 77:0b96f6867312 2033 }
mjr 77:0b96f6867312 2034 }
mjr 77:0b96f6867312 2035
mjr 78:1e00b3fa11af 2036 // Process IR input and output
mjr 77:0b96f6867312 2037 void process_IR(Config &cfg, USBJoystick &js)
mjr 77:0b96f6867312 2038 {
mjr 78:1e00b3fa11af 2039 // check for transmitter tasks, if there's a transmitter
mjr 78:1e00b3fa11af 2040 if (ir_tx != 0)
mjr 77:0b96f6867312 2041 {
mjr 78:1e00b3fa11af 2042 // If we're not currently sending, and an ad hoc IR command
mjr 78:1e00b3fa11af 2043 // is ready to send, send it.
mjr 78:1e00b3fa11af 2044 if (!ir_tx->isSending() && IRAdHocCmd.ready)
mjr 78:1e00b3fa11af 2045 {
mjr 78:1e00b3fa11af 2046 // program the command into the transmitter virtual button
mjr 78:1e00b3fa11af 2047 // that we reserved for ad hoc commands
mjr 78:1e00b3fa11af 2048 ir_tx->programButton(IRAdHocBtn, IRAdHocCmd.protocol,
mjr 78:1e00b3fa11af 2049 IRAdHocCmd.dittos, IRAdHocCmd.code);
mjr 78:1e00b3fa11af 2050
mjr 78:1e00b3fa11af 2051 // send the command - just pulse the button to send it once
mjr 78:1e00b3fa11af 2052 ir_tx->pushButton(IRAdHocBtn, true);
mjr 78:1e00b3fa11af 2053 ir_tx->pushButton(IRAdHocBtn, false);
mjr 78:1e00b3fa11af 2054
mjr 78:1e00b3fa11af 2055 // we've sent the command, so clear the 'ready' flag
mjr 78:1e00b3fa11af 2056 IRAdHocCmd.ready = false;
mjr 78:1e00b3fa11af 2057 }
mjr 77:0b96f6867312 2058 }
mjr 78:1e00b3fa11af 2059
mjr 78:1e00b3fa11af 2060 // check for receiver tasks, if there's a receiver
mjr 78:1e00b3fa11af 2061 if (ir_rx != 0)
mjr 77:0b96f6867312 2062 {
mjr 78:1e00b3fa11af 2063 // Time out any received command
mjr 78:1e00b3fa11af 2064 if (IRCommandIn != 0)
mjr 78:1e00b3fa11af 2065 {
mjr 78:1e00b3fa11af 2066 // Time out inter-key gap mode after 30ms; time out all
mjr 78:1e00b3fa11af 2067 // commands after 100ms.
mjr 78:1e00b3fa11af 2068 uint32_t t = IRTimer.read_us();
mjr 78:1e00b3fa11af 2069 if (t > 100000)
mjr 78:1e00b3fa11af 2070 IRCommandIn = 0;
mjr 78:1e00b3fa11af 2071 else if (t > 30000)
mjr 78:1e00b3fa11af 2072 IRKeyGap = false;
mjr 78:1e00b3fa11af 2073 }
mjr 78:1e00b3fa11af 2074
mjr 78:1e00b3fa11af 2075 // Check if we're in learning mode
mjr 78:1e00b3fa11af 2076 if (IRLearningMode != 0)
mjr 78:1e00b3fa11af 2077 {
mjr 78:1e00b3fa11af 2078 // Learning mode. Read raw inputs from the IR sensor and
mjr 78:1e00b3fa11af 2079 // forward them to the PC via USB reports, up to the report
mjr 78:1e00b3fa11af 2080 // limit.
mjr 78:1e00b3fa11af 2081 const int nmax = USBJoystick::maxRawIR;
mjr 78:1e00b3fa11af 2082 uint16_t raw[nmax];
mjr 78:1e00b3fa11af 2083 int n;
mjr 78:1e00b3fa11af 2084 for (n = 0 ; n < nmax && ir_rx->processOne(raw[n]) ; ++n) ;
mjr 77:0b96f6867312 2085
mjr 78:1e00b3fa11af 2086 // if we read any raw samples, report them
mjr 78:1e00b3fa11af 2087 if (n != 0)
mjr 78:1e00b3fa11af 2088 js.reportRawIR(n, raw);
mjr 77:0b96f6867312 2089
mjr 78:1e00b3fa11af 2090 // check for a command
mjr 78:1e00b3fa11af 2091 IRCommand c;
mjr 78:1e00b3fa11af 2092 if (ir_rx->readCommand(c))
mjr 78:1e00b3fa11af 2093 {
mjr 78:1e00b3fa11af 2094 // check the current learning state
mjr 78:1e00b3fa11af 2095 switch (IRLearningMode)
mjr 78:1e00b3fa11af 2096 {
mjr 78:1e00b3fa11af 2097 case 1:
mjr 78:1e00b3fa11af 2098 // Initial state, waiting for the first decoded command.
mjr 78:1e00b3fa11af 2099 // This is it.
mjr 78:1e00b3fa11af 2100 learnedIRCode = c;
mjr 78:1e00b3fa11af 2101
mjr 78:1e00b3fa11af 2102 // Check if we need additional information. If the
mjr 78:1e00b3fa11af 2103 // protocol supports dittos, we have to wait for a repeat
mjr 78:1e00b3fa11af 2104 // to see if the remote actually uses the dittos, since
mjr 78:1e00b3fa11af 2105 // some implementations of such protocols use the dittos
mjr 78:1e00b3fa11af 2106 // while others just send repeated full codes. Otherwise,
mjr 78:1e00b3fa11af 2107 // all we need is the initial code, so we're done.
mjr 78:1e00b3fa11af 2108 IRLearningMode = (c.hasDittos ? 2 : 3);
mjr 78:1e00b3fa11af 2109 break;
mjr 78:1e00b3fa11af 2110
mjr 78:1e00b3fa11af 2111 case 2:
mjr 78:1e00b3fa11af 2112 // Code received, awaiting auto-repeat information. If
mjr 78:1e00b3fa11af 2113 // the protocol has dittos, check to see if we got a ditto:
mjr 78:1e00b3fa11af 2114 //
mjr 78:1e00b3fa11af 2115 // - If we received a ditto in the same protocol as the
mjr 78:1e00b3fa11af 2116 // prior command, the remote uses dittos.
mjr 78:1e00b3fa11af 2117 //
mjr 78:1e00b3fa11af 2118 // - If we received a repeat of the prior command (not a
mjr 78:1e00b3fa11af 2119 // ditto, but a repeat of the full code), the remote
mjr 78:1e00b3fa11af 2120 // doesn't use dittos even though the protocol supports
mjr 78:1e00b3fa11af 2121 // them.
mjr 78:1e00b3fa11af 2122 //
mjr 78:1e00b3fa11af 2123 // - Otherwise, it's not an auto-repeat at all, so we
mjr 78:1e00b3fa11af 2124 // can't decide one way or the other on dittos: start
mjr 78:1e00b3fa11af 2125 // over.
mjr 78:1e00b3fa11af 2126 if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2127 && c.hasDittos
mjr 78:1e00b3fa11af 2128 && c.ditto)
mjr 78:1e00b3fa11af 2129 {
mjr 78:1e00b3fa11af 2130 // success - the remote uses dittos
mjr 78:1e00b3fa11af 2131 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2132 }
mjr 78:1e00b3fa11af 2133 else if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2134 && c.hasDittos
mjr 78:1e00b3fa11af 2135 && !c.ditto
mjr 78:1e00b3fa11af 2136 && c.code == learnedIRCode.code)
mjr 78:1e00b3fa11af 2137 {
mjr 78:1e00b3fa11af 2138 // success - it's a repeat of the last code, so
mjr 78:1e00b3fa11af 2139 // the remote doesn't use dittos even though the
mjr 78:1e00b3fa11af 2140 // protocol supports them
mjr 78:1e00b3fa11af 2141 learnedIRCode.hasDittos = false;
mjr 78:1e00b3fa11af 2142 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2143 }
mjr 78:1e00b3fa11af 2144 else
mjr 78:1e00b3fa11af 2145 {
mjr 78:1e00b3fa11af 2146 // It's not a ditto and not a full repeat of the
mjr 78:1e00b3fa11af 2147 // last code, so it's either a new key, or some kind
mjr 78:1e00b3fa11af 2148 // of multi-code key encoding that we don't recognize.
mjr 78:1e00b3fa11af 2149 // We can't use this code, so start over.
mjr 78:1e00b3fa11af 2150 IRLearningMode = 1;
mjr 78:1e00b3fa11af 2151 }
mjr 78:1e00b3fa11af 2152 break;
mjr 78:1e00b3fa11af 2153 }
mjr 77:0b96f6867312 2154
mjr 78:1e00b3fa11af 2155 // If we ended in state 3, we've successfully decoded
mjr 78:1e00b3fa11af 2156 // the transmission. Report the decoded data and terminate
mjr 78:1e00b3fa11af 2157 // learning mode.
mjr 78:1e00b3fa11af 2158 if (IRLearningMode == 3)
mjr 77:0b96f6867312 2159 {
mjr 78:1e00b3fa11af 2160 // figure the flags:
mjr 78:1e00b3fa11af 2161 // 0x02 -> dittos
mjr 78:1e00b3fa11af 2162 uint8_t flags = 0;
mjr 78:1e00b3fa11af 2163 if (learnedIRCode.hasDittos)
mjr 78:1e00b3fa11af 2164 flags |= 0x02;
mjr 78:1e00b3fa11af 2165
mjr 78:1e00b3fa11af 2166 // report the code
mjr 78:1e00b3fa11af 2167 js.reportIRCode(learnedIRCode.proId, flags, learnedIRCode.code);
mjr 78:1e00b3fa11af 2168
mjr 78:1e00b3fa11af 2169 // exit learning mode
mjr 78:1e00b3fa11af 2170 IRLearningMode = 0;
mjr 77:0b96f6867312 2171 }
mjr 77:0b96f6867312 2172 }
mjr 77:0b96f6867312 2173
mjr 78:1e00b3fa11af 2174 // time out of IR learning mode if it's been too long
mjr 78:1e00b3fa11af 2175 if (IRLearningMode != 0 && IRTimer.read_us() > 10000000L)
mjr 77:0b96f6867312 2176 {
mjr 78:1e00b3fa11af 2177 // report the termination by sending a raw IR report with
mjr 78:1e00b3fa11af 2178 // zero data elements
mjr 78:1e00b3fa11af 2179 js.reportRawIR(0, 0);
mjr 78:1e00b3fa11af 2180
mjr 78:1e00b3fa11af 2181
mjr 78:1e00b3fa11af 2182 // cancel learning mode
mjr 77:0b96f6867312 2183 IRLearningMode = 0;
mjr 77:0b96f6867312 2184 }
mjr 77:0b96f6867312 2185 }
mjr 78:1e00b3fa11af 2186 else
mjr 77:0b96f6867312 2187 {
mjr 78:1e00b3fa11af 2188 // Not in learning mode. We don't care about the raw signals;
mjr 78:1e00b3fa11af 2189 // just run them through the protocol decoders.
mjr 78:1e00b3fa11af 2190 ir_rx->process();
mjr 78:1e00b3fa11af 2191
mjr 78:1e00b3fa11af 2192 // Check for decoded commands. Keep going until all commands
mjr 78:1e00b3fa11af 2193 // have been read.
mjr 78:1e00b3fa11af 2194 IRCommand c;
mjr 78:1e00b3fa11af 2195 while (ir_rx->readCommand(c))
mjr 77:0b96f6867312 2196 {
mjr 78:1e00b3fa11af 2197 // We received a decoded command. Determine if it's a repeat,
mjr 78:1e00b3fa11af 2198 // and if so, try to determine whether it's an auto-repeat (due
mjr 78:1e00b3fa11af 2199 // to the remote key being held down) or a distinct new press
mjr 78:1e00b3fa11af 2200 // on the same key as last time. The distinction is significant
mjr 78:1e00b3fa11af 2201 // because it affects the auto-repeat behavior of the PC key
mjr 78:1e00b3fa11af 2202 // input. An auto-repeat represents a key being held down on
mjr 78:1e00b3fa11af 2203 // the remote, which we want to translate to a (virtual) key
mjr 78:1e00b3fa11af 2204 // being held down on the PC keyboard; a distinct key press on
mjr 78:1e00b3fa11af 2205 // the remote translates to a distinct key press on the PC.
mjr 78:1e00b3fa11af 2206 //
mjr 78:1e00b3fa11af 2207 // It can only be a repeat if there's a prior command that
mjr 78:1e00b3fa11af 2208 // hasn't timed out yet, so start by checking for a previous
mjr 78:1e00b3fa11af 2209 // command.
mjr 78:1e00b3fa11af 2210 bool repeat = false, autoRepeat = false;
mjr 78:1e00b3fa11af 2211 if (IRCommandIn != 0)
mjr 77:0b96f6867312 2212 {
mjr 78:1e00b3fa11af 2213 // We have a command in progress. Check to see if the
mjr 78:1e00b3fa11af 2214 // new command is a repeat of the previous command. Check
mjr 78:1e00b3fa11af 2215 // first to see if it's a "ditto", which explicitly represents
mjr 78:1e00b3fa11af 2216 // an auto-repeat of the last command.
mjr 78:1e00b3fa11af 2217 IRCommandCfg &cmdcfg = cfg.IRCommand[IRCommandIn - 1];
mjr 78:1e00b3fa11af 2218 if (c.ditto)
mjr 78:1e00b3fa11af 2219 {
mjr 78:1e00b3fa11af 2220 // We received a ditto. Dittos are always auto-
mjr 78:1e00b3fa11af 2221 // repeats, so it's an auto-repeat as long as the
mjr 78:1e00b3fa11af 2222 // ditto is in the same protocol as the last command.
mjr 78:1e00b3fa11af 2223 // If the ditto is in a new protocol, the ditto can't
mjr 78:1e00b3fa11af 2224 // be for the last command we saw, because a ditto
mjr 78:1e00b3fa11af 2225 // never changes protocols from its antecedent. In
mjr 78:1e00b3fa11af 2226 // such a case, we must have missed the antecedent
mjr 78:1e00b3fa11af 2227 // command and thus don't know what's being repeated.
mjr 78:1e00b3fa11af 2228 repeat = autoRepeat = (c.proId == cmdcfg.protocol);
mjr 78:1e00b3fa11af 2229 }
mjr 78:1e00b3fa11af 2230 else
mjr 78:1e00b3fa11af 2231 {
mjr 78:1e00b3fa11af 2232 // It's not a ditto. The new command is a repeat if
mjr 78:1e00b3fa11af 2233 // it matches the protocol and command code of the
mjr 78:1e00b3fa11af 2234 // prior command.
mjr 78:1e00b3fa11af 2235 repeat = (c.proId == cmdcfg.protocol
mjr 78:1e00b3fa11af 2236 && uint32_t(c.code) == cmdcfg.code.lo
mjr 78:1e00b3fa11af 2237 && uint32_t(c.code >> 32) == cmdcfg.code.hi);
mjr 78:1e00b3fa11af 2238
mjr 78:1e00b3fa11af 2239 // If the command is a repeat, try to determine whether
mjr 78:1e00b3fa11af 2240 // it's an auto-repeat or a new press on the same key.
mjr 78:1e00b3fa11af 2241 // If the protocol uses dittos, it's definitely a new
mjr 78:1e00b3fa11af 2242 // key press, because an auto-repeat would have used a
mjr 78:1e00b3fa11af 2243 // ditto. For a protocol that doesn't use dittos, both
mjr 78:1e00b3fa11af 2244 // an auto-repeat and a new key press just send the key
mjr 78:1e00b3fa11af 2245 // code again, so we can't tell the difference based on
mjr 78:1e00b3fa11af 2246 // that alone. But if the protocol has a toggle bit, we
mjr 78:1e00b3fa11af 2247 // can tell by the toggle bit value: a new key press has
mjr 78:1e00b3fa11af 2248 // the opposite toggle value as the last key press, while
mjr 78:1e00b3fa11af 2249 // an auto-repeat has the same toggle. Note that if the
mjr 78:1e00b3fa11af 2250 // protocol doesn't use toggle bits, the toggle value
mjr 78:1e00b3fa11af 2251 // will always be the same, so we'll simply always treat
mjr 78:1e00b3fa11af 2252 // any repeat as an auto-repeat. Many protocols simply
mjr 78:1e00b3fa11af 2253 // provide no way to distinguish the two, so in such
mjr 78:1e00b3fa11af 2254 // cases it's consistent with the native implementations
mjr 78:1e00b3fa11af 2255 // to treat any repeat as an auto-repeat.
mjr 78:1e00b3fa11af 2256 autoRepeat =
mjr 78:1e00b3fa11af 2257 repeat
mjr 78:1e00b3fa11af 2258 && !(cmdcfg.flags & IRFlagDittos)
mjr 78:1e00b3fa11af 2259 && c.toggle == lastIRToggle;
mjr 78:1e00b3fa11af 2260 }
mjr 78:1e00b3fa11af 2261 }
mjr 78:1e00b3fa11af 2262
mjr 78:1e00b3fa11af 2263 // Check to see if it's a repeat of any kind
mjr 78:1e00b3fa11af 2264 if (repeat)
mjr 78:1e00b3fa11af 2265 {
mjr 78:1e00b3fa11af 2266 // It's a repeat. If it's not an auto-repeat, it's a
mjr 78:1e00b3fa11af 2267 // new distinct key press, so we need to send the PC a
mjr 78:1e00b3fa11af 2268 // momentary gap where we're not sending the same key,
mjr 78:1e00b3fa11af 2269 // so that the PC also recognizes this as a distinct
mjr 78:1e00b3fa11af 2270 // key press event.
mjr 78:1e00b3fa11af 2271 if (!autoRepeat)
mjr 78:1e00b3fa11af 2272 IRKeyGap = true;
mjr 78:1e00b3fa11af 2273
mjr 78:1e00b3fa11af 2274 // restart the key-up timer
mjr 78:1e00b3fa11af 2275 IRTimer.reset();
mjr 78:1e00b3fa11af 2276 }
mjr 78:1e00b3fa11af 2277 else if (c.ditto)
mjr 78:1e00b3fa11af 2278 {
mjr 78:1e00b3fa11af 2279 // It's a ditto, but not a repeat of the last command.
mjr 78:1e00b3fa11af 2280 // But a ditto doesn't contain any information of its own
mjr 78:1e00b3fa11af 2281 // on the command being repeated, so given that it's not
mjr 78:1e00b3fa11af 2282 // our last command, we can't infer what command the ditto
mjr 78:1e00b3fa11af 2283 // is for and thus can't make sense of it. We have to
mjr 78:1e00b3fa11af 2284 // simply ignore it and wait for the sender to start with
mjr 78:1e00b3fa11af 2285 // a full command for a new key press.
mjr 78:1e00b3fa11af 2286 IRCommandIn = 0;
mjr 77:0b96f6867312 2287 }
mjr 77:0b96f6867312 2288 else
mjr 77:0b96f6867312 2289 {
mjr 78:1e00b3fa11af 2290 // It's not a repeat, so the last command is no longer
mjr 78:1e00b3fa11af 2291 // in effect (regardless of whether we find a match for
mjr 78:1e00b3fa11af 2292 // the new command).
mjr 78:1e00b3fa11af 2293 IRCommandIn = 0;
mjr 77:0b96f6867312 2294
mjr 78:1e00b3fa11af 2295 // Check to see if we recognize the new command, by
mjr 78:1e00b3fa11af 2296 // searching for a match in our learned code list.
mjr 78:1e00b3fa11af 2297 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2298 {
mjr 78:1e00b3fa11af 2299 // if the protocol and command code from the code
mjr 78:1e00b3fa11af 2300 // list both match the input, it's a match
mjr 78:1e00b3fa11af 2301 IRCommandCfg &cmdcfg = cfg.IRCommand[i];
mjr 78:1e00b3fa11af 2302 if (cmdcfg.protocol == c.proId
mjr 78:1e00b3fa11af 2303 && cmdcfg.code.lo == uint32_t(c.code)
mjr 78:1e00b3fa11af 2304 && cmdcfg.code.hi == uint32_t(c.code >> 32))
mjr 78:1e00b3fa11af 2305 {
mjr 78:1e00b3fa11af 2306 // Found it! Make this the last command, and
mjr 78:1e00b3fa11af 2307 // remember the starting time.
mjr 78:1e00b3fa11af 2308 IRCommandIn = i + 1;
mjr 78:1e00b3fa11af 2309 lastIRToggle = c.toggle;
mjr 78:1e00b3fa11af 2310 IRTimer.reset();
mjr 78:1e00b3fa11af 2311
mjr 78:1e00b3fa11af 2312 // no need to keep searching
mjr 78:1e00b3fa11af 2313 break;
mjr 78:1e00b3fa11af 2314 }
mjr 77:0b96f6867312 2315 }
mjr 77:0b96f6867312 2316 }
mjr 77:0b96f6867312 2317 }
mjr 77:0b96f6867312 2318 }
mjr 77:0b96f6867312 2319 }
mjr 77:0b96f6867312 2320 }
mjr 77:0b96f6867312 2321
mjr 74:822a92bc11d2 2322
mjr 11:bd9da7088e6e 2323 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 2324 //
mjr 11:bd9da7088e6e 2325 // Button input
mjr 11:bd9da7088e6e 2326 //
mjr 11:bd9da7088e6e 2327
mjr 18:5e890ebd0023 2328 // button state
mjr 18:5e890ebd0023 2329 struct ButtonState
mjr 18:5e890ebd0023 2330 {
mjr 38:091e511ce8a0 2331 ButtonState()
mjr 38:091e511ce8a0 2332 {
mjr 53:9b2611964afc 2333 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 2334 virtState = 0;
mjr 53:9b2611964afc 2335 dbState = 0;
mjr 38:091e511ce8a0 2336 pulseState = 0;
mjr 53:9b2611964afc 2337 pulseTime = 0;
mjr 38:091e511ce8a0 2338 }
mjr 35:e959ffba78fd 2339
mjr 53:9b2611964afc 2340 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 2341 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 2342 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 2343 //
mjr 53:9b2611964afc 2344 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 2345 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 2346 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 2347 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 2348 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 2349 void virtPress(bool on)
mjr 53:9b2611964afc 2350 {
mjr 53:9b2611964afc 2351 // Increment or decrement the current state
mjr 53:9b2611964afc 2352 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 2353 }
mjr 53:9b2611964afc 2354
mjr 53:9b2611964afc 2355 // DigitalIn for the button, if connected to a physical input
mjr 73:4e8ce0b18915 2356 TinyDigitalIn di;
mjr 38:091e511ce8a0 2357
mjr 65:739875521aae 2358 // Time of last pulse state transition.
mjr 65:739875521aae 2359 //
mjr 65:739875521aae 2360 // Each state change sticks for a minimum period; when the timer expires,
mjr 65:739875521aae 2361 // if the underlying physical switch is in a different state, we switch
mjr 65:739875521aae 2362 // to the next state and restart the timer. pulseTime is the time remaining
mjr 65:739875521aae 2363 // remaining before we can make another state transition, in microseconds.
mjr 65:739875521aae 2364 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 65:739875521aae 2365 // this guarantees that the parity of the pulse count always matches the
mjr 65:739875521aae 2366 // current physical switch state when the latter is stable, which makes
mjr 65:739875521aae 2367 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 65:739875521aae 2368 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 65:739875521aae 2369 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 65:739875521aae 2370 // This software system can't be fooled that way.)
mjr 65:739875521aae 2371 uint32_t pulseTime;
mjr 18:5e890ebd0023 2372
mjr 65:739875521aae 2373 // Config key index. This points to the ButtonCfg structure in the
mjr 65:739875521aae 2374 // configuration that contains the PC key mapping for the button.
mjr 65:739875521aae 2375 uint8_t cfgIndex;
mjr 53:9b2611964afc 2376
mjr 53:9b2611964afc 2377 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 2378 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 2379 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 2380 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 2381 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 2382 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 2383 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 2384 // and physical source states.
mjr 53:9b2611964afc 2385 uint8_t virtState;
mjr 38:091e511ce8a0 2386
mjr 38:091e511ce8a0 2387 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 2388 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 2389 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 2390 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 2391 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 2392 uint8_t dbState;
mjr 38:091e511ce8a0 2393
mjr 65:739875521aae 2394 // current PHYSICAL on/off state, after debouncing
mjr 65:739875521aae 2395 uint8_t physState : 1;
mjr 65:739875521aae 2396
mjr 65:739875521aae 2397 // current LOGICAL on/off state as reported to the host.
mjr 65:739875521aae 2398 uint8_t logState : 1;
mjr 65:739875521aae 2399
mjr 65:739875521aae 2400 // previous logical on/off state, when keys were last processed for USB
mjr 65:739875521aae 2401 // reports and local effects
mjr 65:739875521aae 2402 uint8_t prevLogState : 1;
mjr 65:739875521aae 2403
mjr 65:739875521aae 2404 // Pulse state
mjr 65:739875521aae 2405 //
mjr 65:739875521aae 2406 // A button in pulse mode (selected via the config flags for the button)
mjr 65:739875521aae 2407 // transmits a brief logical button press and release each time the attached
mjr 65:739875521aae 2408 // physical switch changes state. This is useful for cases where the host
mjr 65:739875521aae 2409 // expects a key press for each change in the state of the physical switch.
mjr 65:739875521aae 2410 // The canonical example is the Coin Door switch in VPinMAME, which requires
mjr 65:739875521aae 2411 // pressing the END key to toggle the open/closed state. This software design
mjr 65:739875521aae 2412 // isn't easily implemented in a physical coin door, though; the simplest
mjr 65:739875521aae 2413 // physical sensor for the coin door state is a switch that's on when the
mjr 65:739875521aae 2414 // door is open and off when the door is closed (or vice versa, but in either
mjr 65:739875521aae 2415 // case, the switch state corresponds to the current state of the door at any
mjr 65:739875521aae 2416 // given time, rather than pulsing on state changes). The "pulse mode"
mjr 65:739875521aae 2417 // option brdiges this gap by generating a toggle key event each time
mjr 65:739875521aae 2418 // there's a change to the physical switch's state.
mjr 38:091e511ce8a0 2419 //
mjr 38:091e511ce8a0 2420 // Pulse state:
mjr 38:091e511ce8a0 2421 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 2422 // 1 -> off
mjr 38:091e511ce8a0 2423 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 2424 // 3 -> on
mjr 38:091e511ce8a0 2425 // 4 -> transitioning on-off
mjr 65:739875521aae 2426 uint8_t pulseState : 3; // 5 states -> we need 3 bits
mjr 65:739875521aae 2427
mjr 65:739875521aae 2428 } __attribute__((packed));
mjr 65:739875521aae 2429
mjr 65:739875521aae 2430 ButtonState *buttonState; // live button slots, allocated on startup
mjr 65:739875521aae 2431 int8_t nButtons; // number of live button slots allocated
mjr 65:739875521aae 2432 int8_t zblButtonIndex = -1; // index of ZB Launch button slot; -1 if unused
mjr 18:5e890ebd0023 2433
mjr 66:2e3583fbd2f4 2434 // Shift button state
mjr 66:2e3583fbd2f4 2435 struct
mjr 66:2e3583fbd2f4 2436 {
mjr 66:2e3583fbd2f4 2437 int8_t index; // buttonState[] index of shift button; -1 if none
mjr 78:1e00b3fa11af 2438 uint8_t state; // current state, for "Key OR Shift" mode:
mjr 66:2e3583fbd2f4 2439 // 0 = not shifted
mjr 66:2e3583fbd2f4 2440 // 1 = shift button down, no key pressed yet
mjr 66:2e3583fbd2f4 2441 // 2 = shift button down, key pressed
mjr 78:1e00b3fa11af 2442 // 3 = released, sending pulsed keystroke
mjr 78:1e00b3fa11af 2443 uint32_t pulseTime; // time remaining in pulsed keystroke (state 3)
mjr 66:2e3583fbd2f4 2444 }
mjr 66:2e3583fbd2f4 2445 __attribute__((packed)) shiftButton;
mjr 38:091e511ce8a0 2446
mjr 38:091e511ce8a0 2447 // Button data
mjr 38:091e511ce8a0 2448 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 2449
mjr 38:091e511ce8a0 2450 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 2451 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 2452 // modifier keys.
mjr 38:091e511ce8a0 2453 struct
mjr 38:091e511ce8a0 2454 {
mjr 38:091e511ce8a0 2455 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 2456 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 2457 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 2458 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 2459 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 2460
mjr 38:091e511ce8a0 2461 // Media key state
mjr 38:091e511ce8a0 2462 struct
mjr 38:091e511ce8a0 2463 {
mjr 38:091e511ce8a0 2464 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 2465 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 2466 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 2467
mjr 38:091e511ce8a0 2468 // button scan interrupt ticker
mjr 38:091e511ce8a0 2469 Ticker buttonTicker;
mjr 38:091e511ce8a0 2470
mjr 38:091e511ce8a0 2471 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 2472 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 2473 void scanButtons()
mjr 38:091e511ce8a0 2474 {
mjr 38:091e511ce8a0 2475 // scan all button input pins
mjr 73:4e8ce0b18915 2476 ButtonState *bs = buttonState, *last = bs + nButtons;
mjr 73:4e8ce0b18915 2477 for ( ; bs < last ; ++bs)
mjr 38:091e511ce8a0 2478 {
mjr 73:4e8ce0b18915 2479 // Shift the new state into the debounce history
mjr 73:4e8ce0b18915 2480 uint8_t db = (bs->dbState << 1) | bs->di.read();
mjr 73:4e8ce0b18915 2481 bs->dbState = db;
mjr 73:4e8ce0b18915 2482
mjr 73:4e8ce0b18915 2483 // If we have all 0's or 1's in the history for the required
mjr 73:4e8ce0b18915 2484 // debounce period, the key state is stable, so apply the new
mjr 73:4e8ce0b18915 2485 // physical state. Note that the pins are active low, so the
mjr 73:4e8ce0b18915 2486 // new button on/off state is the inverse of the GPIO state.
mjr 73:4e8ce0b18915 2487 const uint8_t stable = 0x1F; // 00011111b -> low 5 bits = last 5 readings
mjr 73:4e8ce0b18915 2488 db &= stable;
mjr 73:4e8ce0b18915 2489 if (db == 0 || db == stable)
mjr 73:4e8ce0b18915 2490 bs->physState = !db;
mjr 38:091e511ce8a0 2491 }
mjr 38:091e511ce8a0 2492 }
mjr 38:091e511ce8a0 2493
mjr 38:091e511ce8a0 2494 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 2495 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 2496 // in the physical button state.
mjr 38:091e511ce8a0 2497 Timer buttonTimer;
mjr 12:669df364a565 2498
mjr 65:739875521aae 2499 // Count a button during the initial setup scan
mjr 72:884207c0aab0 2500 void countButton(uint8_t typ, uint8_t shiftTyp, bool &kbKeys)
mjr 65:739875521aae 2501 {
mjr 65:739875521aae 2502 // count it
mjr 65:739875521aae 2503 ++nButtons;
mjr 65:739875521aae 2504
mjr 67:c39e66c4e000 2505 // if it's a keyboard key or media key, note that we need a USB
mjr 67:c39e66c4e000 2506 // keyboard interface
mjr 72:884207c0aab0 2507 if (typ == BtnTypeKey || typ == BtnTypeMedia
mjr 72:884207c0aab0 2508 || shiftTyp == BtnTypeKey || shiftTyp == BtnTypeMedia)
mjr 65:739875521aae 2509 kbKeys = true;
mjr 65:739875521aae 2510 }
mjr 65:739875521aae 2511
mjr 11:bd9da7088e6e 2512 // initialize the button inputs
mjr 35:e959ffba78fd 2513 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 2514 {
mjr 66:2e3583fbd2f4 2515 // presume no shift key
mjr 66:2e3583fbd2f4 2516 shiftButton.index = -1;
mjr 66:2e3583fbd2f4 2517
mjr 65:739875521aae 2518 // Count up how many button slots we'll need to allocate. Start
mjr 65:739875521aae 2519 // with assigned buttons from the configuration, noting that we
mjr 65:739875521aae 2520 // only need to create slots for buttons that are actually wired.
mjr 65:739875521aae 2521 nButtons = 0;
mjr 65:739875521aae 2522 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 2523 {
mjr 65:739875521aae 2524 // it's valid if it's wired to a real input pin
mjr 65:739875521aae 2525 if (wirePinName(cfg.button[i].pin) != NC)
mjr 72:884207c0aab0 2526 countButton(cfg.button[i].typ, cfg.button[i].typ2, kbKeys);
mjr 65:739875521aae 2527 }
mjr 65:739875521aae 2528
mjr 65:739875521aae 2529 // Count virtual buttons
mjr 65:739875521aae 2530
mjr 65:739875521aae 2531 // ZB Launch
mjr 65:739875521aae 2532 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 65:739875521aae 2533 {
mjr 65:739875521aae 2534 // valid - remember the live button index
mjr 65:739875521aae 2535 zblButtonIndex = nButtons;
mjr 65:739875521aae 2536
mjr 65:739875521aae 2537 // count it
mjr 72:884207c0aab0 2538 countButton(cfg.plunger.zbLaunchBall.keytype, BtnTypeNone, kbKeys);
mjr 65:739875521aae 2539 }
mjr 65:739875521aae 2540
mjr 65:739875521aae 2541 // Allocate the live button slots
mjr 65:739875521aae 2542 ButtonState *bs = buttonState = new ButtonState[nButtons];
mjr 65:739875521aae 2543
mjr 65:739875521aae 2544 // Configure the physical inputs
mjr 65:739875521aae 2545 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 2546 {
mjr 65:739875521aae 2547 PinName pin = wirePinName(cfg.button[i].pin);
mjr 65:739875521aae 2548 if (pin != NC)
mjr 65:739875521aae 2549 {
mjr 65:739875521aae 2550 // point back to the config slot for the keyboard data
mjr 65:739875521aae 2551 bs->cfgIndex = i;
mjr 65:739875521aae 2552
mjr 65:739875521aae 2553 // set up the GPIO input pin for this button
mjr 73:4e8ce0b18915 2554 bs->di.assignPin(pin);
mjr 65:739875521aae 2555
mjr 65:739875521aae 2556 // if it's a pulse mode button, set the initial pulse state to Off
mjr 65:739875521aae 2557 if (cfg.button[i].flags & BtnFlagPulse)
mjr 65:739875521aae 2558 bs->pulseState = 1;
mjr 65:739875521aae 2559
mjr 66:2e3583fbd2f4 2560 // If this is the shift button, note its buttonState[] index.
mjr 66:2e3583fbd2f4 2561 // We have to figure the buttonState[] index separately from
mjr 66:2e3583fbd2f4 2562 // the config index, because the indices can differ if some
mjr 66:2e3583fbd2f4 2563 // config slots are left unused.
mjr 78:1e00b3fa11af 2564 if (cfg.shiftButton.idx == i+1)
mjr 66:2e3583fbd2f4 2565 shiftButton.index = bs - buttonState;
mjr 66:2e3583fbd2f4 2566
mjr 65:739875521aae 2567 // advance to the next button
mjr 65:739875521aae 2568 ++bs;
mjr 65:739875521aae 2569 }
mjr 65:739875521aae 2570 }
mjr 65:739875521aae 2571
mjr 53:9b2611964afc 2572 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 2573 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 2574 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 2575 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 2576
mjr 53:9b2611964afc 2577 // ZB Launch Ball button
mjr 65:739875521aae 2578 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 11:bd9da7088e6e 2579 {
mjr 65:739875521aae 2580 // Point back to the config slot for the keyboard data.
mjr 66:2e3583fbd2f4 2581 // We use a special extra slot for virtual buttons,
mjr 66:2e3583fbd2f4 2582 // so we also need to set up the slot data by copying
mjr 66:2e3583fbd2f4 2583 // the ZBL config data to our virtual button slot.
mjr 65:739875521aae 2584 bs->cfgIndex = ZBL_BUTTON_CFG;
mjr 65:739875521aae 2585 cfg.button[ZBL_BUTTON_CFG].pin = PINNAME_TO_WIRE(NC);
mjr 65:739875521aae 2586 cfg.button[ZBL_BUTTON_CFG].typ = cfg.plunger.zbLaunchBall.keytype;
mjr 65:739875521aae 2587 cfg.button[ZBL_BUTTON_CFG].val = cfg.plunger.zbLaunchBall.keycode;
mjr 65:739875521aae 2588
mjr 66:2e3583fbd2f4 2589 // advance to the next button
mjr 65:739875521aae 2590 ++bs;
mjr 11:bd9da7088e6e 2591 }
mjr 12:669df364a565 2592
mjr 38:091e511ce8a0 2593 // start the button scan thread
mjr 38:091e511ce8a0 2594 buttonTicker.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 2595
mjr 38:091e511ce8a0 2596 // start the button state transition timer
mjr 12:669df364a565 2597 buttonTimer.start();
mjr 11:bd9da7088e6e 2598 }
mjr 11:bd9da7088e6e 2599
mjr 67:c39e66c4e000 2600 // Media key mapping. This maps from an 8-bit USB media key
mjr 67:c39e66c4e000 2601 // code to the corresponding bit in our USB report descriptor.
mjr 67:c39e66c4e000 2602 // The USB key code is the index, and the value at the index
mjr 67:c39e66c4e000 2603 // is the report descriptor bit. See joystick.cpp for the
mjr 67:c39e66c4e000 2604 // media descriptor details. Our currently mapped keys are:
mjr 67:c39e66c4e000 2605 //
mjr 67:c39e66c4e000 2606 // 0xE2 -> Mute -> 0x01
mjr 67:c39e66c4e000 2607 // 0xE9 -> Volume Up -> 0x02
mjr 67:c39e66c4e000 2608 // 0xEA -> Volume Down -> 0x04
mjr 67:c39e66c4e000 2609 // 0xB5 -> Next Track -> 0x08
mjr 67:c39e66c4e000 2610 // 0xB6 -> Previous Track -> 0x10
mjr 67:c39e66c4e000 2611 // 0xB7 -> Stop -> 0x20
mjr 67:c39e66c4e000 2612 // 0xCD -> Play / Pause -> 0x40
mjr 67:c39e66c4e000 2613 //
mjr 67:c39e66c4e000 2614 static const uint8_t mediaKeyMap[] = {
mjr 67:c39e66c4e000 2615 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-0F
mjr 67:c39e66c4e000 2616 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-1F
mjr 67:c39e66c4e000 2617 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-2F
mjr 67:c39e66c4e000 2618 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-3F
mjr 67:c39e66c4e000 2619 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-4F
mjr 67:c39e66c4e000 2620 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-5F
mjr 67:c39e66c4e000 2621 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-6F
mjr 67:c39e66c4e000 2622 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-7F
mjr 67:c39e66c4e000 2623 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
mjr 67:c39e66c4e000 2624 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-9F
mjr 67:c39e66c4e000 2625 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0-AF
mjr 67:c39e66c4e000 2626 0, 0, 0, 0, 0, 8, 16, 32, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
mjr 67:c39e66c4e000 2627 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, // C0-CF
mjr 67:c39e66c4e000 2628 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D0-DF
mjr 67:c39e66c4e000 2629 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, // E0-EF
mjr 67:c39e66c4e000 2630 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-FF
mjr 77:0b96f6867312 2631 };
mjr 77:0b96f6867312 2632
mjr 77:0b96f6867312 2633 // Keyboard key/joystick button state. processButtons() uses this to
mjr 77:0b96f6867312 2634 // build the set of key presses to report to the PC based on the logical
mjr 77:0b96f6867312 2635 // states of the button iputs.
mjr 77:0b96f6867312 2636 struct KeyState
mjr 77:0b96f6867312 2637 {
mjr 77:0b96f6867312 2638 KeyState()
mjr 77:0b96f6867312 2639 {
mjr 77:0b96f6867312 2640 // zero all members
mjr 77:0b96f6867312 2641 memset(this, 0, sizeof(*this));
mjr 77:0b96f6867312 2642 }
mjr 77:0b96f6867312 2643
mjr 77:0b96f6867312 2644 // Keyboard media keys currently pressed. This is a bit vector in
mjr 77:0b96f6867312 2645 // the format used in our USB keyboard reports (see USBJoystick.cpp).
mjr 77:0b96f6867312 2646 uint8_t mediakeys;
mjr 77:0b96f6867312 2647
mjr 77:0b96f6867312 2648 // Keyboard modifier (shift) keys currently pressed. This is a bit
mjr 77:0b96f6867312 2649 // vector in the format used in our USB keyboard reports (see
mjr 77:0b96f6867312 2650 // USBJoystick.cpp).
mjr 77:0b96f6867312 2651 uint8_t modkeys;
mjr 77:0b96f6867312 2652
mjr 77:0b96f6867312 2653 // Regular keyboard keys currently pressed. Each element is a USB
mjr 77:0b96f6867312 2654 // key code, or 0 for empty slots. Note that the USB report format
mjr 77:0b96f6867312 2655 // theoretically allows a flexible size limit, but the Windows KB
mjr 77:0b96f6867312 2656 // drivers have a fixed limit of 6 simultaneous keys (and won't
mjr 77:0b96f6867312 2657 // accept reports with more), so there's no point in making this
mjr 77:0b96f6867312 2658 // flexible; we'll just use the fixed size dictated by Windows.
mjr 77:0b96f6867312 2659 uint8_t keys[7];
mjr 77:0b96f6867312 2660
mjr 77:0b96f6867312 2661 // number of valid entries in keys[] array
mjr 77:0b96f6867312 2662 int nkeys;
mjr 77:0b96f6867312 2663
mjr 77:0b96f6867312 2664 // Joystick buttons pressed, as a bit vector. Bit n (1 << n)
mjr 77:0b96f6867312 2665 // represents joystick button n, n in 0..31, with 0 meaning
mjr 77:0b96f6867312 2666 // unpressed and 1 meaning pressed.
mjr 77:0b96f6867312 2667 uint32_t js;
mjr 77:0b96f6867312 2668
mjr 77:0b96f6867312 2669
mjr 77:0b96f6867312 2670 // Add a key press. 'typ' is the button type code (ButtonTypeXxx),
mjr 77:0b96f6867312 2671 // and 'val' is the value (the meaning of which varies by type code).
mjr 77:0b96f6867312 2672 void addKey(uint8_t typ, uint8_t val)
mjr 77:0b96f6867312 2673 {
mjr 77:0b96f6867312 2674 // add the key according to the type
mjr 77:0b96f6867312 2675 switch (typ)
mjr 77:0b96f6867312 2676 {
mjr 77:0b96f6867312 2677 case BtnTypeJoystick:
mjr 77:0b96f6867312 2678 // joystick button
mjr 77:0b96f6867312 2679 js |= (1 << (val - 1));
mjr 77:0b96f6867312 2680 break;
mjr 77:0b96f6867312 2681
mjr 77:0b96f6867312 2682 case BtnTypeKey:
mjr 77:0b96f6867312 2683 // Keyboard key. The USB keyboard report encodes regular
mjr 77:0b96f6867312 2684 // keys and modifier keys separately, so we need to check
mjr 77:0b96f6867312 2685 // which type we have. Note that past versions mapped the
mjr 77:0b96f6867312 2686 // Keyboard Volume Up, Keyboard Volume Down, and Keyboard
mjr 77:0b96f6867312 2687 // Mute keys to the corresponding Media keys. We no longer
mjr 77:0b96f6867312 2688 // do this; instead, we have the separate BtnTypeMedia for
mjr 77:0b96f6867312 2689 // explicitly using media keys if desired.
mjr 77:0b96f6867312 2690 if (val >= 0xE0 && val <= 0xE7)
mjr 77:0b96f6867312 2691 {
mjr 77:0b96f6867312 2692 // It's a modifier key. These are represented in the USB
mjr 77:0b96f6867312 2693 // reports with a bit mask. We arrange the mask bits in
mjr 77:0b96f6867312 2694 // the same order as the scan codes, so we can figure the
mjr 77:0b96f6867312 2695 // appropriate bit with a simple shift.
mjr 77:0b96f6867312 2696 modkeys |= (1 << (val - 0xE0));
mjr 77:0b96f6867312 2697 }
mjr 77:0b96f6867312 2698 else
mjr 77:0b96f6867312 2699 {
mjr 77:0b96f6867312 2700 // It's a regular key. Make sure it's not already in the
mjr 77:0b96f6867312 2701 // list, and that the list isn't full. If neither of these
mjr 77:0b96f6867312 2702 // apply, add the key to the key array.
mjr 77:0b96f6867312 2703 if (nkeys < 7)
mjr 77:0b96f6867312 2704 {
mjr 77:0b96f6867312 2705 bool found = false;
mjr 77:0b96f6867312 2706 for (int i = 0 ; i < nkeys ; ++i)
mjr 77:0b96f6867312 2707 {
mjr 77:0b96f6867312 2708 if (keys[i] == val)
mjr 77:0b96f6867312 2709 {
mjr 77:0b96f6867312 2710 found = true;
mjr 77:0b96f6867312 2711 break;
mjr 77:0b96f6867312 2712 }
mjr 77:0b96f6867312 2713 }
mjr 77:0b96f6867312 2714 if (!found)
mjr 77:0b96f6867312 2715 keys[nkeys++] = val;
mjr 77:0b96f6867312 2716 }
mjr 77:0b96f6867312 2717 }
mjr 77:0b96f6867312 2718 break;
mjr 77:0b96f6867312 2719
mjr 77:0b96f6867312 2720 case BtnTypeMedia:
mjr 77:0b96f6867312 2721 // Media control key. The media keys are mapped in the USB
mjr 77:0b96f6867312 2722 // report to bits, whereas the key codes are specified in the
mjr 77:0b96f6867312 2723 // config with their USB usage numbers. E.g., the config val
mjr 77:0b96f6867312 2724 // for Media Next Track is 0xB5, but we encode this in the USB
mjr 77:0b96f6867312 2725 // report as bit 0x08. The mediaKeyMap[] table translates
mjr 77:0b96f6867312 2726 // from the USB usage number to the mask bit. If the key isn't
mjr 77:0b96f6867312 2727 // among the subset we support, the mapped bit will be zero, so
mjr 77:0b96f6867312 2728 // the "|=" will have no effect and the key will be ignored.
mjr 77:0b96f6867312 2729 mediakeys |= mediaKeyMap[val];
mjr 77:0b96f6867312 2730 break;
mjr 77:0b96f6867312 2731 }
mjr 77:0b96f6867312 2732 }
mjr 77:0b96f6867312 2733 };
mjr 67:c39e66c4e000 2734
mjr 67:c39e66c4e000 2735
mjr 38:091e511ce8a0 2736 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 2737 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 2738 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 2739 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 2740 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 2741 {
mjr 77:0b96f6867312 2742 // key state
mjr 77:0b96f6867312 2743 KeyState ks;
mjr 38:091e511ce8a0 2744
mjr 38:091e511ce8a0 2745 // calculate the time since the last run
mjr 53:9b2611964afc 2746 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 2747 buttonTimer.reset();
mjr 66:2e3583fbd2f4 2748
mjr 66:2e3583fbd2f4 2749 // check the shift button state
mjr 66:2e3583fbd2f4 2750 if (shiftButton.index != -1)
mjr 66:2e3583fbd2f4 2751 {
mjr 78:1e00b3fa11af 2752 // get the shift button's physical state object
mjr 66:2e3583fbd2f4 2753 ButtonState *sbs = &buttonState[shiftButton.index];
mjr 78:1e00b3fa11af 2754
mjr 78:1e00b3fa11af 2755 // figure what to do based on the shift button mode in the config
mjr 78:1e00b3fa11af 2756 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 2757 {
mjr 66:2e3583fbd2f4 2758 case 0:
mjr 78:1e00b3fa11af 2759 default:
mjr 78:1e00b3fa11af 2760 // "Shift OR Key" mode. The shift button doesn't send its key
mjr 78:1e00b3fa11af 2761 // immediately when pressed. Instead, we wait to see what
mjr 78:1e00b3fa11af 2762 // happens while it's down. Check the current cycle state.
mjr 78:1e00b3fa11af 2763 switch (shiftButton.state)
mjr 78:1e00b3fa11af 2764 {
mjr 78:1e00b3fa11af 2765 case 0:
mjr 78:1e00b3fa11af 2766 // Not shifted. Check if the button is now down: if so,
mjr 78:1e00b3fa11af 2767 // switch to state 1 (shift button down, no key pressed yet).
mjr 78:1e00b3fa11af 2768 if (sbs->physState)
mjr 78:1e00b3fa11af 2769 shiftButton.state = 1;
mjr 78:1e00b3fa11af 2770 break;
mjr 78:1e00b3fa11af 2771
mjr 78:1e00b3fa11af 2772 case 1:
mjr 78:1e00b3fa11af 2773 // Shift button down, no key pressed yet. If the button is
mjr 78:1e00b3fa11af 2774 // now up, it counts as an ordinary button press instead of
mjr 78:1e00b3fa11af 2775 // a shift button press, since the shift function was never
mjr 78:1e00b3fa11af 2776 // used. Return to unshifted state and start a timed key
mjr 78:1e00b3fa11af 2777 // pulse event.
mjr 78:1e00b3fa11af 2778 if (!sbs->physState)
mjr 78:1e00b3fa11af 2779 {
mjr 78:1e00b3fa11af 2780 shiftButton.state = 3;
mjr 78:1e00b3fa11af 2781 shiftButton.pulseTime = 50000+dt; // 50 ms left on the key pulse
mjr 78:1e00b3fa11af 2782 }
mjr 78:1e00b3fa11af 2783 break;
mjr 78:1e00b3fa11af 2784
mjr 78:1e00b3fa11af 2785 case 2:
mjr 78:1e00b3fa11af 2786 // Shift button down, other key was pressed. If the button is
mjr 78:1e00b3fa11af 2787 // now up, simply clear the shift state without sending a key
mjr 78:1e00b3fa11af 2788 // press for the shift button itself to the PC. The shift
mjr 78:1e00b3fa11af 2789 // function was used, so its ordinary key press function is
mjr 78:1e00b3fa11af 2790 // suppressed.
mjr 78:1e00b3fa11af 2791 if (!sbs->physState)
mjr 78:1e00b3fa11af 2792 shiftButton.state = 0;
mjr 78:1e00b3fa11af 2793 break;
mjr 78:1e00b3fa11af 2794
mjr 78:1e00b3fa11af 2795 case 3:
mjr 78:1e00b3fa11af 2796 // Sending pulsed keystroke. Deduct the current time interval
mjr 78:1e00b3fa11af 2797 // from the remaining pulse timer. End the pulse if the time
mjr 78:1e00b3fa11af 2798 // has expired.
mjr 78:1e00b3fa11af 2799 if (shiftButton.pulseTime > dt)
mjr 78:1e00b3fa11af 2800 shiftButton.pulseTime -= dt;
mjr 78:1e00b3fa11af 2801 else
mjr 78:1e00b3fa11af 2802 shiftButton.state = 0;
mjr 78:1e00b3fa11af 2803 break;
mjr 78:1e00b3fa11af 2804 }
mjr 66:2e3583fbd2f4 2805 break;
mjr 66:2e3583fbd2f4 2806
mjr 66:2e3583fbd2f4 2807 case 1:
mjr 78:1e00b3fa11af 2808 // "Shift AND Key" mode. In this mode, the shift button acts
mjr 78:1e00b3fa11af 2809 // like any other button and sends its mapped key immediately.
mjr 78:1e00b3fa11af 2810 // The state cycle in this case simply matches the physical
mjr 78:1e00b3fa11af 2811 // state: ON -> cycle state 1, OFF -> cycle state 0.
mjr 78:1e00b3fa11af 2812 shiftButton.state = (sbs->physState ? 1 : 0);
mjr 66:2e3583fbd2f4 2813 break;
mjr 66:2e3583fbd2f4 2814 }
mjr 66:2e3583fbd2f4 2815 }
mjr 38:091e511ce8a0 2816
mjr 11:bd9da7088e6e 2817 // scan the button list
mjr 18:5e890ebd0023 2818 ButtonState *bs = buttonState;
mjr 65:739875521aae 2819 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 11:bd9da7088e6e 2820 {
mjr 77:0b96f6867312 2821 // get the config entry for the button
mjr 77:0b96f6867312 2822 ButtonCfg *bc = &cfg.button[bs->cfgIndex];
mjr 77:0b96f6867312 2823
mjr 66:2e3583fbd2f4 2824 // Check the button type:
mjr 66:2e3583fbd2f4 2825 // - shift button
mjr 66:2e3583fbd2f4 2826 // - pulsed button
mjr 66:2e3583fbd2f4 2827 // - regular button
mjr 66:2e3583fbd2f4 2828 if (shiftButton.index == i)
mjr 66:2e3583fbd2f4 2829 {
mjr 78:1e00b3fa11af 2830 // This is the shift button. The logical state handling
mjr 78:1e00b3fa11af 2831 // depends on the mode.
mjr 78:1e00b3fa11af 2832 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 2833 {
mjr 78:1e00b3fa11af 2834 case 0:
mjr 78:1e00b3fa11af 2835 default:
mjr 78:1e00b3fa11af 2836 // "Shift OR Key" mode. The logical state is ON only
mjr 78:1e00b3fa11af 2837 // during the timed pulse when the key is released, which
mjr 78:1e00b3fa11af 2838 // is signified by shift button state 3.
mjr 78:1e00b3fa11af 2839 bs->logState = (shiftButton.state == 3);
mjr 78:1e00b3fa11af 2840 break;
mjr 78:1e00b3fa11af 2841
mjr 78:1e00b3fa11af 2842 case 1:
mjr 78:1e00b3fa11af 2843 // "Shif AND Key" mode. The shift button acts like any
mjr 78:1e00b3fa11af 2844 // other button, so it's logically on when physically on.
mjr 78:1e00b3fa11af 2845 bs->logState = bs->physState;
mjr 78:1e00b3fa11af 2846 break;
mjr 66:2e3583fbd2f4 2847 }
mjr 66:2e3583fbd2f4 2848 }
mjr 66:2e3583fbd2f4 2849 else if (bs->pulseState != 0)
mjr 18:5e890ebd0023 2850 {
mjr 38:091e511ce8a0 2851 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 2852 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 2853 {
mjr 53:9b2611964afc 2854 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 2855 bs->pulseTime -= dt;
mjr 53:9b2611964afc 2856 }
mjr 53:9b2611964afc 2857 else
mjr 53:9b2611964afc 2858 {
mjr 53:9b2611964afc 2859 // pulse time expired - check for a state change
mjr 53:9b2611964afc 2860 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 2861 switch (bs->pulseState)
mjr 18:5e890ebd0023 2862 {
mjr 38:091e511ce8a0 2863 case 1:
mjr 38:091e511ce8a0 2864 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 2865 if (bs->physState)
mjr 53:9b2611964afc 2866 {
mjr 38:091e511ce8a0 2867 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 2868 bs->pulseState = 2;
mjr 53:9b2611964afc 2869 bs->logState = 1;
mjr 38:091e511ce8a0 2870 }
mjr 38:091e511ce8a0 2871 break;
mjr 18:5e890ebd0023 2872
mjr 38:091e511ce8a0 2873 case 2:
mjr 38:091e511ce8a0 2874 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 2875 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 2876 // change in state in the logical button
mjr 38:091e511ce8a0 2877 bs->pulseState = 3;
mjr 38:091e511ce8a0 2878 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 2879 bs->logState = 0;
mjr 38:091e511ce8a0 2880 break;
mjr 38:091e511ce8a0 2881
mjr 38:091e511ce8a0 2882 case 3:
mjr 38:091e511ce8a0 2883 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 2884 if (!bs->physState)
mjr 53:9b2611964afc 2885 {
mjr 38:091e511ce8a0 2886 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 2887 bs->pulseState = 4;
mjr 53:9b2611964afc 2888 bs->logState = 1;
mjr 38:091e511ce8a0 2889 }
mjr 38:091e511ce8a0 2890 break;
mjr 38:091e511ce8a0 2891
mjr 38:091e511ce8a0 2892 case 4:
mjr 38:091e511ce8a0 2893 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 2894 bs->pulseState = 1;
mjr 38:091e511ce8a0 2895 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 2896 bs->logState = 0;
mjr 38:091e511ce8a0 2897 break;
mjr 18:5e890ebd0023 2898 }
mjr 18:5e890ebd0023 2899 }
mjr 38:091e511ce8a0 2900 }
mjr 38:091e511ce8a0 2901 else
mjr 38:091e511ce8a0 2902 {
mjr 38:091e511ce8a0 2903 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 2904 bs->logState = bs->physState;
mjr 38:091e511ce8a0 2905 }
mjr 77:0b96f6867312 2906
mjr 77:0b96f6867312 2907 // Determine if we're going to use the shifted version of the
mjr 78:1e00b3fa11af 2908 // button. We're using the shifted version if...
mjr 78:1e00b3fa11af 2909 //
mjr 78:1e00b3fa11af 2910 // - the shift button is down, AND
mjr 78:1e00b3fa11af 2911 // - this button isn't itself the shift button, AND
mjr 78:1e00b3fa11af 2912 // - this button has some kind of shifted meaning
mjr 77:0b96f6867312 2913 //
mjr 78:1e00b3fa11af 2914 // A "shifted meaning" means that we have any of the following
mjr 78:1e00b3fa11af 2915 // assigned to the shifted version of the button: a key assignment,
mjr 78:1e00b3fa11af 2916 // (in typ2,key2), an IR command (in IRCommand2), or Night mode.
mjr 78:1e00b3fa11af 2917 //
mjr 78:1e00b3fa11af 2918 // The test for Night Mode is a bit tricky. The shifted version of
mjr 78:1e00b3fa11af 2919 // the button is the Night Mode toggle if the button matches the
mjr 78:1e00b3fa11af 2920 // Night Mode button index, AND its flags are set with "toggle mode
mjr 78:1e00b3fa11af 2921 // ON" (bit 0x02 is on) and "switch mode OFF" (bit 0x01 is off).
mjr 78:1e00b3fa11af 2922 // So (button flags) & 0x03 must equal 0x02.
mjr 77:0b96f6867312 2923 bool useShift =
mjr 77:0b96f6867312 2924 (shiftButton.state != 0
mjr 78:1e00b3fa11af 2925 && shiftButton.index != i
mjr 77:0b96f6867312 2926 && (bc->typ2 != BtnTypeNone
mjr 77:0b96f6867312 2927 || bc->IRCommand2 != 0
mjr 77:0b96f6867312 2928 || (cfg.nightMode.btn == i+1 && (cfg.nightMode.flags & 0x03) == 0x02)));
mjr 77:0b96f6867312 2929
mjr 77:0b96f6867312 2930 // If we're using the shift function, and no other button has used
mjr 77:0b96f6867312 2931 // the shift function yet (shift state 1: "shift button is down but
mjr 77:0b96f6867312 2932 // no one has used the shift function yet"), then we've "consumed"
mjr 77:0b96f6867312 2933 // the shift button press (so go to shift state 2: "shift button has
mjr 77:0b96f6867312 2934 // been used by some other button press that has a shifted meaning").
mjr 78:1e00b3fa11af 2935 if (useShift && shiftButton.state == 1 && bs->logState)
mjr 77:0b96f6867312 2936 shiftButton.state = 2;
mjr 35:e959ffba78fd 2937
mjr 38:091e511ce8a0 2938 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 2939 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 2940 {
mjr 77:0b96f6867312 2941 // check to see if this is the Night Mode button
mjr 53:9b2611964afc 2942 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 2943 {
mjr 77:0b96f6867312 2944 // Check the switch type in the config flags. If flag 0x01 is
mjr 77:0b96f6867312 2945 // set, it's a persistent on/off switch, so the night mode
mjr 77:0b96f6867312 2946 // state simply tracks the current state of the switch.
mjr 77:0b96f6867312 2947 // Otherwise, it's a momentary button, so each button push
mjr 77:0b96f6867312 2948 // (i.e., each transition from logical state OFF to ON) toggles
mjr 77:0b96f6867312 2949 // the night mode state.
mjr 77:0b96f6867312 2950 //
mjr 77:0b96f6867312 2951 // Note that the "shift" flag (0x02) has no effect in switch
mjr 77:0b96f6867312 2952 // mode. Shifting only works for toggle mode.
mjr 53:9b2611964afc 2953 if (cfg.nightMode.flags & 0x01)
mjr 53:9b2611964afc 2954 {
mjr 77:0b96f6867312 2955 // It's an on/off switch. Night mode simply tracks the
mjr 77:0b96f6867312 2956 // current switch state.
mjr 53:9b2611964afc 2957 setNightMode(bs->logState);
mjr 53:9b2611964afc 2958 }
mjr 53:9b2611964afc 2959 else
mjr 53:9b2611964afc 2960 {
mjr 77:0b96f6867312 2961 // It's a momentary toggle switch. Toggle the night mode
mjr 77:0b96f6867312 2962 // state on each distinct press of the button: that is,
mjr 77:0b96f6867312 2963 // whenever the button's logical state transitions from
mjr 77:0b96f6867312 2964 // OFF to ON.
mjr 66:2e3583fbd2f4 2965 //
mjr 77:0b96f6867312 2966 // The "shift" flag (0x02) tells us whether night mode is
mjr 77:0b96f6867312 2967 // assigned to the shifted or unshifted version of the
mjr 77:0b96f6867312 2968 // button.
mjr 77:0b96f6867312 2969 bool pressed;
mjr 66:2e3583fbd2f4 2970 if ((cfg.nightMode.flags & 0x02) != 0)
mjr 66:2e3583fbd2f4 2971 {
mjr 77:0b96f6867312 2972 // Shift bit is set - night mode is assigned to the
mjr 77:0b96f6867312 2973 // shifted version of the button. This is a Night
mjr 77:0b96f6867312 2974 // Mode toggle only if the Shift button is pressed.
mjr 77:0b96f6867312 2975 pressed = (shiftButton.state != 0);
mjr 77:0b96f6867312 2976 }
mjr 77:0b96f6867312 2977 else
mjr 77:0b96f6867312 2978 {
mjr 77:0b96f6867312 2979 // No shift bit - night mode is assigned to the
mjr 77:0b96f6867312 2980 // regular unshifted button. The button press only
mjr 77:0b96f6867312 2981 // applies if the Shift button is NOT pressed.
mjr 77:0b96f6867312 2982 pressed = (shiftButton.state == 0);
mjr 66:2e3583fbd2f4 2983 }
mjr 66:2e3583fbd2f4 2984
mjr 66:2e3583fbd2f4 2985 // if it's pressed (even after considering the shift mode),
mjr 66:2e3583fbd2f4 2986 // toggle night mode
mjr 66:2e3583fbd2f4 2987 if (pressed)
mjr 53:9b2611964afc 2988 toggleNightMode();
mjr 53:9b2611964afc 2989 }
mjr 35:e959ffba78fd 2990 }
mjr 38:091e511ce8a0 2991
mjr 77:0b96f6867312 2992 // press or release IR virtual keys on key state changes
mjr 77:0b96f6867312 2993 uint8_t irc = useShift ? bc->IRCommand2 : bc->IRCommand;
mjr 77:0b96f6867312 2994 if (irc != 0)
mjr 77:0b96f6867312 2995 IR_buttonChange(irc, bs->logState);
mjr 77:0b96f6867312 2996
mjr 38:091e511ce8a0 2997 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 2998 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 2999 }
mjr 38:091e511ce8a0 3000
mjr 53:9b2611964afc 3001 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 3002 // key state list
mjr 53:9b2611964afc 3003 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 3004 {
mjr 70:9f58735a1732 3005 // Get the key type and code. Start by assuming that we're
mjr 70:9f58735a1732 3006 // going to use the normal unshifted meaning.
mjr 77:0b96f6867312 3007 uint8_t typ, val;
mjr 77:0b96f6867312 3008 if (useShift)
mjr 66:2e3583fbd2f4 3009 {
mjr 77:0b96f6867312 3010 typ = bc->typ2;
mjr 77:0b96f6867312 3011 val = bc->val2;
mjr 66:2e3583fbd2f4 3012 }
mjr 77:0b96f6867312 3013 else
mjr 77:0b96f6867312 3014 {
mjr 77:0b96f6867312 3015 typ = bc->typ;
mjr 77:0b96f6867312 3016 val = bc->val;
mjr 77:0b96f6867312 3017 }
mjr 77:0b96f6867312 3018
mjr 70:9f58735a1732 3019 // We've decided on the meaning of the button, so process
mjr 70:9f58735a1732 3020 // the keyboard or joystick event.
mjr 77:0b96f6867312 3021 ks.addKey(typ, val);
mjr 18:5e890ebd0023 3022 }
mjr 11:bd9da7088e6e 3023 }
mjr 77:0b96f6867312 3024
mjr 77:0b96f6867312 3025 // If an IR input command is in effect, add the IR command's
mjr 77:0b96f6867312 3026 // assigned key, if any. If we're in an IR key gap, don't include
mjr 77:0b96f6867312 3027 // the IR key.
mjr 77:0b96f6867312 3028 if (IRCommandIn != 0 && !IRKeyGap)
mjr 77:0b96f6867312 3029 {
mjr 77:0b96f6867312 3030 IRCommandCfg &irc = cfg.IRCommand[IRCommandIn - 1];
mjr 77:0b96f6867312 3031 ks.addKey(irc.keytype, irc.keycode);
mjr 77:0b96f6867312 3032 }
mjr 77:0b96f6867312 3033
mjr 77:0b96f6867312 3034 // We're finished building the new key state. Update the global
mjr 77:0b96f6867312 3035 // key state variables to reflect the new state.
mjr 77:0b96f6867312 3036
mjr 77:0b96f6867312 3037 // set the new joystick buttons (no need to check for changes, as we
mjr 77:0b96f6867312 3038 // report these on every joystick report whether they changed or not)
mjr 77:0b96f6867312 3039 jsButtons = ks.js;
mjr 77:0b96f6867312 3040
mjr 77:0b96f6867312 3041 // check for keyboard key changes (we only send keyboard reports when
mjr 77:0b96f6867312 3042 // something changes)
mjr 77:0b96f6867312 3043 if (kbState.data[0] != ks.modkeys
mjr 77:0b96f6867312 3044 || kbState.nkeys != ks.nkeys
mjr 77:0b96f6867312 3045 || memcmp(ks.keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 3046 {
mjr 35:e959ffba78fd 3047 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3048 kbState.changed = true;
mjr 77:0b96f6867312 3049 kbState.data[0] = ks.modkeys;
mjr 77:0b96f6867312 3050 if (ks.nkeys <= 6) {
mjr 35:e959ffba78fd 3051 // 6 or fewer simultaneous keys - report the key codes
mjr 77:0b96f6867312 3052 kbState.nkeys = ks.nkeys;
mjr 77:0b96f6867312 3053 memcpy(&kbState.data[2], ks.keys, 6);
mjr 35:e959ffba78fd 3054 }
mjr 35:e959ffba78fd 3055 else {
mjr 35:e959ffba78fd 3056 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 3057 kbState.nkeys = 6;
mjr 35:e959ffba78fd 3058 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 3059 }
mjr 35:e959ffba78fd 3060 }
mjr 35:e959ffba78fd 3061
mjr 77:0b96f6867312 3062 // check for media key changes (we only send media key reports when
mjr 77:0b96f6867312 3063 // something changes)
mjr 77:0b96f6867312 3064 if (mediaState.data != ks.mediakeys)
mjr 35:e959ffba78fd 3065 {
mjr 77:0b96f6867312 3066 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3067 mediaState.changed = true;
mjr 77:0b96f6867312 3068 mediaState.data = ks.mediakeys;
mjr 35:e959ffba78fd 3069 }
mjr 11:bd9da7088e6e 3070 }
mjr 11:bd9da7088e6e 3071
mjr 73:4e8ce0b18915 3072 // Send a button status report
mjr 73:4e8ce0b18915 3073 void reportButtonStatus(USBJoystick &js)
mjr 73:4e8ce0b18915 3074 {
mjr 73:4e8ce0b18915 3075 // start with all buttons off
mjr 73:4e8ce0b18915 3076 uint8_t state[(MAX_BUTTONS+7)/8];
mjr 73:4e8ce0b18915 3077 memset(state, 0, sizeof(state));
mjr 73:4e8ce0b18915 3078
mjr 73:4e8ce0b18915 3079 // pack the button states into bytes, one bit per button
mjr 73:4e8ce0b18915 3080 ButtonState *bs = buttonState;
mjr 73:4e8ce0b18915 3081 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 73:4e8ce0b18915 3082 {
mjr 73:4e8ce0b18915 3083 // get the physical state
mjr 73:4e8ce0b18915 3084 int b = bs->physState;
mjr 73:4e8ce0b18915 3085
mjr 73:4e8ce0b18915 3086 // pack it into the appropriate bit
mjr 73:4e8ce0b18915 3087 int idx = bs->cfgIndex;
mjr 73:4e8ce0b18915 3088 int si = idx / 8;
mjr 73:4e8ce0b18915 3089 int shift = idx & 0x07;
mjr 73:4e8ce0b18915 3090 state[si] |= b << shift;
mjr 73:4e8ce0b18915 3091 }
mjr 73:4e8ce0b18915 3092
mjr 73:4e8ce0b18915 3093 // send the report
mjr 73:4e8ce0b18915 3094 js.reportButtonStatus(MAX_BUTTONS, state);
mjr 73:4e8ce0b18915 3095 }
mjr 73:4e8ce0b18915 3096
mjr 5:a70c0bce770d 3097 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3098 //
mjr 5:a70c0bce770d 3099 // Customization joystick subbclass
mjr 5:a70c0bce770d 3100 //
mjr 5:a70c0bce770d 3101
mjr 5:a70c0bce770d 3102 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 3103 {
mjr 5:a70c0bce770d 3104 public:
mjr 35:e959ffba78fd 3105 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 35:e959ffba78fd 3106 bool waitForConnect, bool enableJoystick, bool useKB)
mjr 35:e959ffba78fd 3107 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, useKB)
mjr 5:a70c0bce770d 3108 {
mjr 54:fd77a6b2f76c 3109 sleeping_ = false;
mjr 54:fd77a6b2f76c 3110 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3111 timer_.start();
mjr 54:fd77a6b2f76c 3112 }
mjr 54:fd77a6b2f76c 3113
mjr 54:fd77a6b2f76c 3114 // show diagnostic LED feedback for connect state
mjr 54:fd77a6b2f76c 3115 void diagFlash()
mjr 54:fd77a6b2f76c 3116 {
mjr 54:fd77a6b2f76c 3117 if (!configured() || sleeping_)
mjr 54:fd77a6b2f76c 3118 {
mjr 54:fd77a6b2f76c 3119 // flash once if sleeping or twice if disconnected
mjr 54:fd77a6b2f76c 3120 for (int j = isConnected() ? 1 : 2 ; j > 0 ; --j)
mjr 54:fd77a6b2f76c 3121 {
mjr 54:fd77a6b2f76c 3122 // short red flash
mjr 54:fd77a6b2f76c 3123 diagLED(1, 0, 0);
mjr 54:fd77a6b2f76c 3124 wait_us(50000);
mjr 54:fd77a6b2f76c 3125 diagLED(0, 0, 0);
mjr 54:fd77a6b2f76c 3126 wait_us(50000);
mjr 54:fd77a6b2f76c 3127 }
mjr 54:fd77a6b2f76c 3128 }
mjr 5:a70c0bce770d 3129 }
mjr 5:a70c0bce770d 3130
mjr 5:a70c0bce770d 3131 // are we connected?
mjr 5:a70c0bce770d 3132 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 3133
mjr 54:fd77a6b2f76c 3134 // Are we in sleep mode? If true, this means that the hardware has
mjr 54:fd77a6b2f76c 3135 // detected no activity on the bus for 3ms. This happens when the
mjr 54:fd77a6b2f76c 3136 // cable is physically disconnected, the computer is turned off, or
mjr 54:fd77a6b2f76c 3137 // the connection is otherwise disabled.
mjr 54:fd77a6b2f76c 3138 bool isSleeping() const { return sleeping_; }
mjr 54:fd77a6b2f76c 3139
mjr 54:fd77a6b2f76c 3140 // If necessary, attempt to recover from a broken connection.
mjr 54:fd77a6b2f76c 3141 //
mjr 54:fd77a6b2f76c 3142 // This is a hack, to work around an apparent timing bug in the
mjr 54:fd77a6b2f76c 3143 // KL25Z USB implementation that I haven't been able to solve any
mjr 54:fd77a6b2f76c 3144 // other way.
mjr 54:fd77a6b2f76c 3145 //
mjr 54:fd77a6b2f76c 3146 // The issue: when we have an established connection, and the
mjr 54:fd77a6b2f76c 3147 // connection is broken by physically unplugging the cable or by
mjr 54:fd77a6b2f76c 3148 // rebooting the PC, the KL25Z sometimes fails to reconnect when
mjr 54:fd77a6b2f76c 3149 // the physical connection is re-established. The failure is
mjr 54:fd77a6b2f76c 3150 // sporadic; I'd guess it happens about 25% of the time, but I
mjr 54:fd77a6b2f76c 3151 // haven't collected any real statistics on it.
mjr 54:fd77a6b2f76c 3152 //
mjr 54:fd77a6b2f76c 3153 // The proximate cause of the failure is a deadlock in the SETUP
mjr 54:fd77a6b2f76c 3154 // protocol between the host and device that happens around the
mjr 54:fd77a6b2f76c 3155 // point where the PC is requesting the configuration descriptor.
mjr 54:fd77a6b2f76c 3156 // The exact point in the protocol where this occurs varies slightly;
mjr 54:fd77a6b2f76c 3157 // it can occur a message or two before or after the Get Config
mjr 54:fd77a6b2f76c 3158 // Descriptor packet. No matter where it happens, the nature of
mjr 54:fd77a6b2f76c 3159 // the deadlock is the same: the PC thinks it sees a STALL on EP0
mjr 54:fd77a6b2f76c 3160 // from the device, so it terminates the connection attempt, which
mjr 54:fd77a6b2f76c 3161 // stops further traffic on the cable. The KL25Z USB hardware sees
mjr 54:fd77a6b2f76c 3162 // the lack of traffic and triggers a SLEEP interrupt (a misnomer
mjr 54:fd77a6b2f76c 3163 // for what should have been called a BROKEN CONNECTION interrupt).
mjr 54:fd77a6b2f76c 3164 // Both sides simply stop talking at this point, so the connection
mjr 54:fd77a6b2f76c 3165 // is effectively dead.
mjr 54:fd77a6b2f76c 3166 //
mjr 54:fd77a6b2f76c 3167 // The strange thing is that, as far as I can tell, the KL25Z isn't
mjr 54:fd77a6b2f76c 3168 // doing anything to trigger the STALL on its end. Both the PC
mjr 54:fd77a6b2f76c 3169 // and the KL25Z are happy up until the very point of the failure
mjr 54:fd77a6b2f76c 3170 // and show no signs of anything wrong in the protocol exchange.
mjr 54:fd77a6b2f76c 3171 // In fact, every detail of the protocol exchange up to this point
mjr 54:fd77a6b2f76c 3172 // is identical to every successful exchange that does finish the
mjr 54:fd77a6b2f76c 3173 // whole setup process successfully, on both the KL25Z and Windows
mjr 54:fd77a6b2f76c 3174 // sides of the connection. I can't find any point of difference
mjr 54:fd77a6b2f76c 3175 // between successful and unsuccessful sequences that suggests why
mjr 54:fd77a6b2f76c 3176 // the fateful message fails. This makes me suspect that whatever
mjr 54:fd77a6b2f76c 3177 // is going wrong is inside the KL25Z USB hardware module, which
mjr 54:fd77a6b2f76c 3178 // is a pretty substantial black box - it has a lot of internal
mjr 54:fd77a6b2f76c 3179 // state that's inaccessible to the software. Further bolstering
mjr 54:fd77a6b2f76c 3180 // this theory is a little experiment where I found that I could
mjr 54:fd77a6b2f76c 3181 // reproduce the exact sequence of events of a failed reconnect
mjr 54:fd77a6b2f76c 3182 // attempt in an *initial* connection, which is otherwise 100%
mjr 54:fd77a6b2f76c 3183 // reliable, by inserting a little bit of artifical time padding
mjr 54:fd77a6b2f76c 3184 // (200us per event) into the SETUP interrupt handler. My
mjr 54:fd77a6b2f76c 3185 // hypothesis is that the STALL event happens because the KL25Z
mjr 54:fd77a6b2f76c 3186 // USB hardware is too slow to respond to a message. I'm not
mjr 54:fd77a6b2f76c 3187 // sure why this would only happen after a disconnect and not
mjr 54:fd77a6b2f76c 3188 // during the initial connection; maybe there's some reset work
mjr 54:fd77a6b2f76c 3189 // in the hardware that takes a substantial amount of time after
mjr 54:fd77a6b2f76c 3190 // a disconnect.
mjr 54:fd77a6b2f76c 3191 //
mjr 54:fd77a6b2f76c 3192 // The solution: the problem happens during the SETUP exchange,
mjr 54:fd77a6b2f76c 3193 // after we've been assigned a bus address. It only happens on
mjr 54:fd77a6b2f76c 3194 // some percentage of connection requests, so if we can simply
mjr 54:fd77a6b2f76c 3195 // start over when the failure occurs, we'll eventually succeed
mjr 54:fd77a6b2f76c 3196 // simply because not every attempt fails. The ideal would be
mjr 54:fd77a6b2f76c 3197 // to get the success rate up to 100%, but I can't figure out how
mjr 54:fd77a6b2f76c 3198 // to fix the underlying problem, so this is the next best thing.
mjr 54:fd77a6b2f76c 3199 //
mjr 54:fd77a6b2f76c 3200 // We can detect when the failure occurs by noticing when a SLEEP
mjr 54:fd77a6b2f76c 3201 // interrupt happens while we have an assigned bus address.
mjr 54:fd77a6b2f76c 3202 //
mjr 54:fd77a6b2f76c 3203 // To start a new connection attempt, we have to make the *host*
mjr 54:fd77a6b2f76c 3204 // try again. The logical connection is initiated solely by the
mjr 54:fd77a6b2f76c 3205 // host. Fortunately, it's easy to get the host to initiate the
mjr 54:fd77a6b2f76c 3206 // process: if we disconnect on the device side, it effectively
mjr 54:fd77a6b2f76c 3207 // makes the device look to the PC like it's electrically unplugged.
mjr 54:fd77a6b2f76c 3208 // When we reconnect on the device side, the PC thinks a new device
mjr 54:fd77a6b2f76c 3209 // has been plugged in and initiates the logical connection setup.
mjr 74:822a92bc11d2 3210 // We have to remain disconnected for some minimum interval before
mjr 74:822a92bc11d2 3211 // the host notices; the exact minimum is unclear, but 5ms seems
mjr 74:822a92bc11d2 3212 // reliable in practice.
mjr 54:fd77a6b2f76c 3213 //
mjr 54:fd77a6b2f76c 3214 // Here's the full algorithm:
mjr 54:fd77a6b2f76c 3215 //
mjr 54:fd77a6b2f76c 3216 // 1. In the SLEEP interrupt handler, if we have a bus address,
mjr 54:fd77a6b2f76c 3217 // we disconnect the device. This happens in ISR context, so we
mjr 54:fd77a6b2f76c 3218 // can't wait around for 5ms. Instead, we simply set a flag noting
mjr 54:fd77a6b2f76c 3219 // that the connection has been broken, and we note the time and
mjr 54:fd77a6b2f76c 3220 // return.
mjr 54:fd77a6b2f76c 3221 //
mjr 54:fd77a6b2f76c 3222 // 2. In our main loop, whenever we find that we're disconnected,
mjr 54:fd77a6b2f76c 3223 // we call recoverConnection(). The main loop's job is basically a
mjr 54:fd77a6b2f76c 3224 // bunch of device polling. We're just one more device to poll, so
mjr 54:fd77a6b2f76c 3225 // recoverConnection() will be called soon after a disconnect, and
mjr 54:fd77a6b2f76c 3226 // then will be called in a loop for as long as we're disconnected.
mjr 54:fd77a6b2f76c 3227 //
mjr 54:fd77a6b2f76c 3228 // 3. In recoverConnection(), we check the flag we set in the SLEEP
mjr 54:fd77a6b2f76c 3229 // handler. If set, we wait until 5ms has elapsed from the SLEEP
mjr 54:fd77a6b2f76c 3230 // event time that we noted, then we'll reconnect and clear the flag.
mjr 54:fd77a6b2f76c 3231 // This gives us the required 5ms (or longer) delay between the
mjr 54:fd77a6b2f76c 3232 // disconnect and reconnect, ensuring that the PC will notice and
mjr 54:fd77a6b2f76c 3233 // will start over with the connection protocol.
mjr 54:fd77a6b2f76c 3234 //
mjr 54:fd77a6b2f76c 3235 // 4. The main loop keeps calling recoverConnection() in a loop for
mjr 54:fd77a6b2f76c 3236 // as long as we're disconnected, so if the new connection attempt
mjr 54:fd77a6b2f76c 3237 // triggered in step 3 fails, the SLEEP interrupt will happen again,
mjr 54:fd77a6b2f76c 3238 // we'll disconnect again, the flag will get set again, and
mjr 54:fd77a6b2f76c 3239 // recoverConnection() will reconnect again after another suitable
mjr 54:fd77a6b2f76c 3240 // delay. This will repeat until the connection succeeds or hell
mjr 54:fd77a6b2f76c 3241 // freezes over.
mjr 54:fd77a6b2f76c 3242 //
mjr 54:fd77a6b2f76c 3243 // Each disconnect happens immediately when a reconnect attempt
mjr 54:fd77a6b2f76c 3244 // fails, and an entire successful connection only takes about 25ms,
mjr 54:fd77a6b2f76c 3245 // so our loop can retry at more than 30 attempts per second.
mjr 54:fd77a6b2f76c 3246 // In my testing, lost connections almost always reconnect in
mjr 54:fd77a6b2f76c 3247 // less than second with this code in place.
mjr 54:fd77a6b2f76c 3248 void recoverConnection()
mjr 54:fd77a6b2f76c 3249 {
mjr 54:fd77a6b2f76c 3250 // if a reconnect is pending, reconnect
mjr 54:fd77a6b2f76c 3251 if (reconnectPending_)
mjr 54:fd77a6b2f76c 3252 {
mjr 54:fd77a6b2f76c 3253 // Loop until we reach 5ms after the last sleep event.
mjr 54:fd77a6b2f76c 3254 for (bool done = false ; !done ; )
mjr 54:fd77a6b2f76c 3255 {
mjr 54:fd77a6b2f76c 3256 // If we've reached the target time, reconnect. Do the
mjr 54:fd77a6b2f76c 3257 // time check and flag reset atomically, so that we can't
mjr 54:fd77a6b2f76c 3258 // have another sleep event sneak in after we've verified
mjr 54:fd77a6b2f76c 3259 // the time. If another event occurs, it has to happen
mjr 54:fd77a6b2f76c 3260 // before we check, in which case it'll update the time
mjr 54:fd77a6b2f76c 3261 // before we check it, or after we clear the flag, in
mjr 54:fd77a6b2f76c 3262 // which case it will reset the flag and we'll do another
mjr 54:fd77a6b2f76c 3263 // round the next time we call this routine.
mjr 54:fd77a6b2f76c 3264 __disable_irq();
mjr 54:fd77a6b2f76c 3265 if (uint32_t(timer_.read_us() - lastSleepTime_) > 5000)
mjr 54:fd77a6b2f76c 3266 {
mjr 54:fd77a6b2f76c 3267 connect(false);
mjr 54:fd77a6b2f76c 3268 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3269 done = true;
mjr 54:fd77a6b2f76c 3270 }
mjr 54:fd77a6b2f76c 3271 __enable_irq();
mjr 54:fd77a6b2f76c 3272 }
mjr 54:fd77a6b2f76c 3273 }
mjr 54:fd77a6b2f76c 3274 }
mjr 5:a70c0bce770d 3275
mjr 5:a70c0bce770d 3276 protected:
mjr 54:fd77a6b2f76c 3277 // Handle a USB SLEEP interrupt. This interrupt signifies that the
mjr 54:fd77a6b2f76c 3278 // USB hardware module hasn't seen any token traffic for 3ms, which
mjr 54:fd77a6b2f76c 3279 // means that we're either physically or logically disconnected.
mjr 54:fd77a6b2f76c 3280 //
mjr 54:fd77a6b2f76c 3281 // Important: this runs in ISR context.
mjr 54:fd77a6b2f76c 3282 //
mjr 54:fd77a6b2f76c 3283 // Note that this is a specialized sense of "sleep" that's unrelated
mjr 54:fd77a6b2f76c 3284 // to the similarly named power modes on the PC. This has nothing
mjr 54:fd77a6b2f76c 3285 // to do with suspend/sleep mode on the PC, and it's not a low-power
mjr 54:fd77a6b2f76c 3286 // mode on the KL25Z. They really should have called this interrupt
mjr 54:fd77a6b2f76c 3287 // DISCONNECT or BROKEN CONNECTION.)
mjr 54:fd77a6b2f76c 3288 virtual void sleepStateChanged(unsigned int sleeping)
mjr 54:fd77a6b2f76c 3289 {
mjr 54:fd77a6b2f76c 3290 // note the new state
mjr 54:fd77a6b2f76c 3291 sleeping_ = sleeping;
mjr 54:fd77a6b2f76c 3292
mjr 54:fd77a6b2f76c 3293 // If we have a non-zero bus address, we have at least a partial
mjr 54:fd77a6b2f76c 3294 // connection to the host (we've made it at least as far as the
mjr 54:fd77a6b2f76c 3295 // SETUP stage). Explicitly disconnect, and the pending reconnect
mjr 54:fd77a6b2f76c 3296 // flag, and remember the time of the sleep event.
mjr 54:fd77a6b2f76c 3297 if (USB0->ADDR != 0x00)
mjr 54:fd77a6b2f76c 3298 {
mjr 54:fd77a6b2f76c 3299 disconnect();
mjr 54:fd77a6b2f76c 3300 lastSleepTime_ = timer_.read_us();
mjr 54:fd77a6b2f76c 3301 reconnectPending_ = true;
mjr 54:fd77a6b2f76c 3302 }
mjr 54:fd77a6b2f76c 3303 }
mjr 54:fd77a6b2f76c 3304
mjr 54:fd77a6b2f76c 3305 // is the USB connection asleep?
mjr 54:fd77a6b2f76c 3306 volatile bool sleeping_;
mjr 54:fd77a6b2f76c 3307
mjr 54:fd77a6b2f76c 3308 // flag: reconnect pending after sleep event
mjr 54:fd77a6b2f76c 3309 volatile bool reconnectPending_;
mjr 54:fd77a6b2f76c 3310
mjr 54:fd77a6b2f76c 3311 // time of last sleep event while connected
mjr 54:fd77a6b2f76c 3312 volatile uint32_t lastSleepTime_;
mjr 54:fd77a6b2f76c 3313
mjr 54:fd77a6b2f76c 3314 // timer to keep track of interval since last sleep event
mjr 54:fd77a6b2f76c 3315 Timer timer_;
mjr 5:a70c0bce770d 3316 };
mjr 5:a70c0bce770d 3317
mjr 5:a70c0bce770d 3318 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3319 //
mjr 5:a70c0bce770d 3320 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 3321 //
mjr 5:a70c0bce770d 3322
mjr 5:a70c0bce770d 3323 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 3324 //
mjr 5:a70c0bce770d 3325 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 3326 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 3327 // automatic calibration.
mjr 5:a70c0bce770d 3328 //
mjr 77:0b96f6867312 3329 // We collect data at the device's maximum rate of 800kHz (one sample
mjr 77:0b96f6867312 3330 // every 1.25ms). To keep up with the high data rate, we use the
mjr 77:0b96f6867312 3331 // device's internal FIFO, and drain the FIFO by polling on each
mjr 77:0b96f6867312 3332 // iteration of our main application loop. In the past, we used an
mjr 77:0b96f6867312 3333 // interrupt handler to read the device immediately on the arrival of
mjr 77:0b96f6867312 3334 // each sample, but this created too much latency for the IR remote
mjr 77:0b96f6867312 3335 // receiver, due to the relatively long time it takes to transfer the
mjr 77:0b96f6867312 3336 // accelerometer readings via I2C. The device's on-board FIFO can
mjr 77:0b96f6867312 3337 // store up to 32 samples, which gives us up to about 40ms between
mjr 77:0b96f6867312 3338 // polling iterations before the buffer overflows. Our main loop runs
mjr 77:0b96f6867312 3339 // in under 2ms, so we can easily keep the FIFO far from overflowing.
mjr 77:0b96f6867312 3340 //
mjr 77:0b96f6867312 3341 // The MMA8451Q has three range modes, +/- 2G, 4G, and 8G. The ADC
mjr 77:0b96f6867312 3342 // sample is the same bit width (14 bits) in all modes, so the higher
mjr 77:0b96f6867312 3343 // dynamic range modes trade physical precision for range. For our
mjr 77:0b96f6867312 3344 // purposes, precision is more important than range, so we use the
mjr 77:0b96f6867312 3345 // +/-2G mode. Further, our joystick range is calibrated for only
mjr 77:0b96f6867312 3346 // +/-1G. This was unintentional on my part; I didn't look at the
mjr 77:0b96f6867312 3347 // MMA8451Q library closely enough to realize it was normalizing to
mjr 77:0b96f6867312 3348 // actual "G" units, and assumed that it was normalizing to a -1..+1
mjr 77:0b96f6867312 3349 // scale. In practice, a +/-1G scale seems perfectly adequate for
mjr 77:0b96f6867312 3350 // virtual pinball use, so I'm sticking with that range for now. But
mjr 77:0b96f6867312 3351 // there might be some benefit in renormalizing to a +/-2G range, in
mjr 77:0b96f6867312 3352 // that it would allow for higher dynamic range for very hard nudges.
mjr 77:0b96f6867312 3353 // Everyone would have to tweak their nudge sensitivity in VP if I
mjr 77:0b96f6867312 3354 // made that change, though, so I'm keeping it as is for now; it would
mjr 77:0b96f6867312 3355 // be best to make it a config option ("accelerometer high dynamic range")
mjr 77:0b96f6867312 3356 // rather than change it across the board.
mjr 5:a70c0bce770d 3357 //
mjr 6:cc35eb643e8f 3358 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 3359 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 3360 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 3361 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 3362 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 3363 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 3364 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 3365 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 3366 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 3367 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 3368 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 3369 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 3370 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 3371 // of nudging, say).
mjr 5:a70c0bce770d 3372 //
mjr 5:a70c0bce770d 3373
mjr 17:ab3cec0c8bf4 3374 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 3375 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 3376
mjr 17:ab3cec0c8bf4 3377 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 3378 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 3379 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 3380
mjr 17:ab3cec0c8bf4 3381 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 3382 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 3383 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 3384 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 3385
mjr 17:ab3cec0c8bf4 3386
mjr 6:cc35eb643e8f 3387 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 3388 struct AccHist
mjr 5:a70c0bce770d 3389 {
mjr 77:0b96f6867312 3390 AccHist() { x = y = dsq = 0; xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 3391 void set(int x, int y, AccHist *prv)
mjr 6:cc35eb643e8f 3392 {
mjr 6:cc35eb643e8f 3393 // save the raw position
mjr 6:cc35eb643e8f 3394 this->x = x;
mjr 6:cc35eb643e8f 3395 this->y = y;
mjr 77:0b96f6867312 3396 this->dsq = distanceSquared(prv);
mjr 6:cc35eb643e8f 3397 }
mjr 6:cc35eb643e8f 3398
mjr 6:cc35eb643e8f 3399 // reading for this entry
mjr 77:0b96f6867312 3400 int x, y;
mjr 77:0b96f6867312 3401
mjr 77:0b96f6867312 3402 // (distance from previous entry) squared
mjr 77:0b96f6867312 3403 int dsq;
mjr 5:a70c0bce770d 3404
mjr 6:cc35eb643e8f 3405 // total and count of samples averaged over this period
mjr 77:0b96f6867312 3406 int xtot, ytot;
mjr 6:cc35eb643e8f 3407 int cnt;
mjr 6:cc35eb643e8f 3408
mjr 77:0b96f6867312 3409 void clearAvg() { xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 3410 void addAvg(int x, int y) { xtot += x; ytot += y; ++cnt; }
mjr 77:0b96f6867312 3411 int xAvg() const { return xtot/cnt; }
mjr 77:0b96f6867312 3412 int yAvg() const { return ytot/cnt; }
mjr 77:0b96f6867312 3413
mjr 77:0b96f6867312 3414 int distanceSquared(AccHist *p)
mjr 77:0b96f6867312 3415 { return square(p->x - x) + square(p->y - y); }
mjr 5:a70c0bce770d 3416 };
mjr 5:a70c0bce770d 3417
mjr 5:a70c0bce770d 3418 // accelerometer wrapper class
mjr 3:3514575d4f86 3419 class Accel
mjr 3:3514575d4f86 3420 {
mjr 3:3514575d4f86 3421 public:
mjr 78:1e00b3fa11af 3422 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin,
mjr 78:1e00b3fa11af 3423 int range, int autoCenterMode)
mjr 77:0b96f6867312 3424 : mma_(sda, scl, i2cAddr)
mjr 3:3514575d4f86 3425 {
mjr 5:a70c0bce770d 3426 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 3427 irqPin_ = irqPin;
mjr 77:0b96f6867312 3428
mjr 77:0b96f6867312 3429 // remember the range
mjr 77:0b96f6867312 3430 range_ = range;
mjr 78:1e00b3fa11af 3431
mjr 78:1e00b3fa11af 3432 // set the auto-centering mode
mjr 78:1e00b3fa11af 3433 setAutoCenterMode(autoCenterMode);
mjr 78:1e00b3fa11af 3434
mjr 78:1e00b3fa11af 3435 // no manual centering request has been received
mjr 78:1e00b3fa11af 3436 manualCenterRequest_ = false;
mjr 5:a70c0bce770d 3437
mjr 5:a70c0bce770d 3438 // reset and initialize
mjr 5:a70c0bce770d 3439 reset();
mjr 5:a70c0bce770d 3440 }
mjr 5:a70c0bce770d 3441
mjr 78:1e00b3fa11af 3442 // Request manual centering. This applies the trailing average
mjr 78:1e00b3fa11af 3443 // of recent measurements and applies it as the new center point
mjr 78:1e00b3fa11af 3444 // as soon as we have enough data.
mjr 78:1e00b3fa11af 3445 void manualCenterRequest() { manualCenterRequest_ = true; }
mjr 78:1e00b3fa11af 3446
mjr 78:1e00b3fa11af 3447 // set the auto-centering mode
mjr 78:1e00b3fa11af 3448 void setAutoCenterMode(int mode)
mjr 78:1e00b3fa11af 3449 {
mjr 78:1e00b3fa11af 3450 // remember the mode
mjr 78:1e00b3fa11af 3451 autoCenterMode_ = mode;
mjr 78:1e00b3fa11af 3452
mjr 78:1e00b3fa11af 3453 // Set the time between checks. We check 5 times over the course
mjr 78:1e00b3fa11af 3454 // of the centering time, so the check interval is 1/5 of the total.
mjr 78:1e00b3fa11af 3455 if (mode == 0)
mjr 78:1e00b3fa11af 3456 {
mjr 78:1e00b3fa11af 3457 // mode 0 is the old default of 5 seconds, so check every 1s
mjr 78:1e00b3fa11af 3458 autoCenterCheckTime_ = 1000000;
mjr 78:1e00b3fa11af 3459 }
mjr 78:1e00b3fa11af 3460 else if (mode <= 60)
mjr 78:1e00b3fa11af 3461 {
mjr 78:1e00b3fa11af 3462 // mode 1-60 means reset after 'mode' seconds; the check
mjr 78:1e00b3fa11af 3463 // interval is 1/5 of this
mjr 78:1e00b3fa11af 3464 autoCenterCheckTime_ = mode*200000;
mjr 78:1e00b3fa11af 3465 }
mjr 78:1e00b3fa11af 3466 else
mjr 78:1e00b3fa11af 3467 {
mjr 78:1e00b3fa11af 3468 // Auto-centering is off, but still gather statistics to apply
mjr 78:1e00b3fa11af 3469 // when we get a manual centering request. The check interval
mjr 78:1e00b3fa11af 3470 // in this case is 1/5 of the total time for the trailing average
mjr 78:1e00b3fa11af 3471 // we apply for the manual centering. We want this to be long
mjr 78:1e00b3fa11af 3472 // enough to smooth out the data, but short enough that it only
mjr 78:1e00b3fa11af 3473 // includes recent data.
mjr 78:1e00b3fa11af 3474 autoCenterCheckTime_ = 500000;
mjr 78:1e00b3fa11af 3475 }
mjr 78:1e00b3fa11af 3476 }
mjr 78:1e00b3fa11af 3477
mjr 5:a70c0bce770d 3478 void reset()
mjr 5:a70c0bce770d 3479 {
mjr 6:cc35eb643e8f 3480 // clear the center point
mjr 77:0b96f6867312 3481 cx_ = cy_ = 0;
mjr 6:cc35eb643e8f 3482
mjr 77:0b96f6867312 3483 // start the auto-centering timer
mjr 5:a70c0bce770d 3484 tCenter_.start();
mjr 5:a70c0bce770d 3485 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 3486
mjr 5:a70c0bce770d 3487 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 3488 mma_.init();
mjr 77:0b96f6867312 3489
mjr 77:0b96f6867312 3490 // set the range
mjr 77:0b96f6867312 3491 mma_.setRange(
mjr 77:0b96f6867312 3492 range_ == AccelRange4G ? 4 :
mjr 77:0b96f6867312 3493 range_ == AccelRange8G ? 8 :
mjr 77:0b96f6867312 3494 2);
mjr 6:cc35eb643e8f 3495
mjr 77:0b96f6867312 3496 // set the average accumulators to zero
mjr 77:0b96f6867312 3497 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 3498 nSum_ = 0;
mjr 3:3514575d4f86 3499
mjr 3:3514575d4f86 3500 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 3501 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 3502 }
mjr 3:3514575d4f86 3503
mjr 77:0b96f6867312 3504 void poll()
mjr 76:7f5912b6340e 3505 {
mjr 77:0b96f6867312 3506 // read samples until we clear the FIFO
mjr 77:0b96f6867312 3507 while (mma_.getFIFOCount() != 0)
mjr 77:0b96f6867312 3508 {
mjr 77:0b96f6867312 3509 int x, y, z;
mjr 77:0b96f6867312 3510 mma_.getAccXYZ(x, y, z);
mjr 77:0b96f6867312 3511
mjr 77:0b96f6867312 3512 // add the new reading to the running total for averaging
mjr 77:0b96f6867312 3513 xSum_ += (x - cx_);
mjr 77:0b96f6867312 3514 ySum_ += (y - cy_);
mjr 77:0b96f6867312 3515 ++nSum_;
mjr 77:0b96f6867312 3516
mjr 77:0b96f6867312 3517 // store the updates
mjr 77:0b96f6867312 3518 ax_ = x;
mjr 77:0b96f6867312 3519 ay_ = y;
mjr 77:0b96f6867312 3520 az_ = z;
mjr 77:0b96f6867312 3521 }
mjr 76:7f5912b6340e 3522 }
mjr 77:0b96f6867312 3523
mjr 9:fd65b0a94720 3524 void get(int &x, int &y)
mjr 3:3514575d4f86 3525 {
mjr 77:0b96f6867312 3526 // read the shared data and store locally for calculations
mjr 77:0b96f6867312 3527 int ax = ax_, ay = ay_;
mjr 77:0b96f6867312 3528 int xSum = xSum_, ySum = ySum_;
mjr 77:0b96f6867312 3529 int nSum = nSum_;
mjr 6:cc35eb643e8f 3530
mjr 77:0b96f6867312 3531 // reset the average accumulators for the next run
mjr 77:0b96f6867312 3532 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 3533 nSum_ = 0;
mjr 77:0b96f6867312 3534
mjr 77:0b96f6867312 3535 // add this sample to the current calibration interval's running total
mjr 77:0b96f6867312 3536 AccHist *p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 3537 p->addAvg(ax, ay);
mjr 77:0b96f6867312 3538
mjr 78:1e00b3fa11af 3539 // If we're in auto-centering mode, check for auto-centering
mjr 78:1e00b3fa11af 3540 // at intervals of 1/5 of the overall time. If we're not in
mjr 78:1e00b3fa11af 3541 // auto-centering mode, check anyway at one-second intervals
mjr 78:1e00b3fa11af 3542 // so that we gather averages for manual centering requests.
mjr 78:1e00b3fa11af 3543 if (tCenter_.read_us() > autoCenterCheckTime_)
mjr 77:0b96f6867312 3544 {
mjr 77:0b96f6867312 3545 // add the latest raw sample to the history list
mjr 77:0b96f6867312 3546 AccHist *prv = p;
mjr 77:0b96f6867312 3547 iAccPrv_ = (iAccPrv_ + 1);
mjr 77:0b96f6867312 3548 if (iAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 3549 iAccPrv_ = 0;
mjr 77:0b96f6867312 3550 p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 3551 p->set(ax, ay, prv);
mjr 77:0b96f6867312 3552
mjr 78:1e00b3fa11af 3553 // if we have a full complement, check for auto-centering
mjr 77:0b96f6867312 3554 if (nAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 3555 {
mjr 78:1e00b3fa11af 3556 // Center if:
mjr 78:1e00b3fa11af 3557 //
mjr 78:1e00b3fa11af 3558 // - Auto-centering is on, and we've been stable over the
mjr 78:1e00b3fa11af 3559 // whole sample period at our spot-check points
mjr 78:1e00b3fa11af 3560 //
mjr 78:1e00b3fa11af 3561 // - A manual centering request is pending
mjr 78:1e00b3fa11af 3562 //
mjr 77:0b96f6867312 3563 static const int accTol = 164*164; // 1% of range, squared
mjr 77:0b96f6867312 3564 AccHist *p0 = accPrv_;
mjr 78:1e00b3fa11af 3565 if (manualCenterRequest_
mjr 78:1e00b3fa11af 3566 || (autoCenterMode_ <= 60
mjr 78:1e00b3fa11af 3567 && p0[0].dsq < accTol
mjr 78:1e00b3fa11af 3568 && p0[1].dsq < accTol
mjr 78:1e00b3fa11af 3569 && p0[2].dsq < accTol
mjr 78:1e00b3fa11af 3570 && p0[3].dsq < accTol
mjr 78:1e00b3fa11af 3571 && p0[4].dsq < accTol))
mjr 77:0b96f6867312 3572 {
mjr 77:0b96f6867312 3573 // Figure the new calibration point as the average of
mjr 77:0b96f6867312 3574 // the samples over the rest period
mjr 77:0b96f6867312 3575 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5;
mjr 77:0b96f6867312 3576 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5;
mjr 78:1e00b3fa11af 3577
mjr 78:1e00b3fa11af 3578 // clear any pending manual centering request
mjr 78:1e00b3fa11af 3579 manualCenterRequest_ = false;
mjr 77:0b96f6867312 3580 }
mjr 77:0b96f6867312 3581 }
mjr 77:0b96f6867312 3582 else
mjr 77:0b96f6867312 3583 {
mjr 77:0b96f6867312 3584 // not enough samples yet; just up the count
mjr 77:0b96f6867312 3585 ++nAccPrv_;
mjr 77:0b96f6867312 3586 }
mjr 6:cc35eb643e8f 3587
mjr 77:0b96f6867312 3588 // clear the new item's running totals
mjr 77:0b96f6867312 3589 p->clearAvg();
mjr 5:a70c0bce770d 3590
mjr 77:0b96f6867312 3591 // reset the timer
mjr 77:0b96f6867312 3592 tCenter_.reset();
mjr 77:0b96f6867312 3593 }
mjr 5:a70c0bce770d 3594
mjr 77:0b96f6867312 3595 // report our integrated velocity reading in x,y
mjr 77:0b96f6867312 3596 x = rawToReport(xSum/nSum);
mjr 77:0b96f6867312 3597 y = rawToReport(ySum/nSum);
mjr 5:a70c0bce770d 3598
mjr 6:cc35eb643e8f 3599 #ifdef DEBUG_PRINTF
mjr 77:0b96f6867312 3600 if (x != 0 || y != 0)
mjr 77:0b96f6867312 3601 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 3602 #endif
mjr 77:0b96f6867312 3603 }
mjr 29:582472d0bc57 3604
mjr 3:3514575d4f86 3605 private:
mjr 6:cc35eb643e8f 3606 // adjust a raw acceleration figure to a usb report value
mjr 77:0b96f6867312 3607 int rawToReport(int v)
mjr 5:a70c0bce770d 3608 {
mjr 77:0b96f6867312 3609 // Scale to the joystick report range. The accelerometer
mjr 77:0b96f6867312 3610 // readings use the native 14-bit signed integer representation,
mjr 77:0b96f6867312 3611 // so their scale is 2^13.
mjr 77:0b96f6867312 3612 //
mjr 77:0b96f6867312 3613 // The 1G range is special: it uses the 2G native hardware range,
mjr 77:0b96f6867312 3614 // but rescales the result to a 1G range for the joystick reports.
mjr 77:0b96f6867312 3615 // So for that mode, we divide by 4096 rather than 8192. All of
mjr 77:0b96f6867312 3616 // the other modes map use the hardware scaling directly.
mjr 77:0b96f6867312 3617 int i = v*JOYMAX;
mjr 77:0b96f6867312 3618 i = (range_ == AccelRange1G ? i/4096 : i/8192);
mjr 5:a70c0bce770d 3619
mjr 6:cc35eb643e8f 3620 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 3621 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 3622 static const int filter[] = {
mjr 6:cc35eb643e8f 3623 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 3624 0,
mjr 6:cc35eb643e8f 3625 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 3626 };
mjr 6:cc35eb643e8f 3627 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 3628 }
mjr 5:a70c0bce770d 3629
mjr 3:3514575d4f86 3630 // underlying accelerometer object
mjr 3:3514575d4f86 3631 MMA8451Q mma_;
mjr 3:3514575d4f86 3632
mjr 77:0b96f6867312 3633 // last raw acceleration readings, on the device's signed 14-bit
mjr 77:0b96f6867312 3634 // scale -8192..+8191
mjr 77:0b96f6867312 3635 int ax_, ay_, az_;
mjr 77:0b96f6867312 3636
mjr 77:0b96f6867312 3637 // running sum of readings since last get()
mjr 77:0b96f6867312 3638 int xSum_, ySum_;
mjr 77:0b96f6867312 3639
mjr 77:0b96f6867312 3640 // number of readings since last get()
mjr 77:0b96f6867312 3641 int nSum_;
mjr 6:cc35eb643e8f 3642
mjr 6:cc35eb643e8f 3643 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 3644 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 3645 // at rest.
mjr 77:0b96f6867312 3646 int cx_, cy_;
mjr 77:0b96f6867312 3647
mjr 77:0b96f6867312 3648 // range (AccelRangeXxx value, from config.h)
mjr 77:0b96f6867312 3649 uint8_t range_;
mjr 78:1e00b3fa11af 3650
mjr 78:1e00b3fa11af 3651 // auto-center mode:
mjr 78:1e00b3fa11af 3652 // 0 = default of 5-second auto-centering
mjr 78:1e00b3fa11af 3653 // 1-60 = auto-center after this many seconds
mjr 78:1e00b3fa11af 3654 // 255 = auto-centering off (manual centering only)
mjr 78:1e00b3fa11af 3655 uint8_t autoCenterMode_;
mjr 78:1e00b3fa11af 3656
mjr 78:1e00b3fa11af 3657 // flag: a manual centering request is pending
mjr 78:1e00b3fa11af 3658 bool manualCenterRequest_;
mjr 78:1e00b3fa11af 3659
mjr 78:1e00b3fa11af 3660 // time in us between auto-centering incremental checks
mjr 78:1e00b3fa11af 3661 uint32_t autoCenterCheckTime_;
mjr 78:1e00b3fa11af 3662
mjr 77:0b96f6867312 3663 // atuo-centering timer
mjr 5:a70c0bce770d 3664 Timer tCenter_;
mjr 6:cc35eb643e8f 3665
mjr 6:cc35eb643e8f 3666 // Auto-centering history. This is a separate history list that
mjr 77:0b96f6867312 3667 // records results spaced out sparsely over time, so that we can
mjr 6:cc35eb643e8f 3668 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 3669 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 3670 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 3671 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 3672 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 3673 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 3674 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 3675 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 3676 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 3677 // accelerometer measurement bias (e.g., from temperature changes).
mjr 78:1e00b3fa11af 3678 uint8_t iAccPrv_, nAccPrv_;
mjr 78:1e00b3fa11af 3679 static const uint8_t maxAccPrv = 5;
mjr 6:cc35eb643e8f 3680 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 3681
mjr 5:a70c0bce770d 3682 // interurupt pin name
mjr 5:a70c0bce770d 3683 PinName irqPin_;
mjr 3:3514575d4f86 3684 };
mjr 3:3514575d4f86 3685
mjr 5:a70c0bce770d 3686 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3687 //
mjr 14:df700b22ca08 3688 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 3689 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 3690 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 3691 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 3692 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 3693 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 3694 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 3695 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 3696 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 3697 //
mjr 14:df700b22ca08 3698 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 3699 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 3700 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 3701 //
mjr 5:a70c0bce770d 3702 void clear_i2c()
mjr 5:a70c0bce770d 3703 {
mjr 38:091e511ce8a0 3704 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 3705 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 3706 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 3707
mjr 5:a70c0bce770d 3708 // clock the SCL 9 times
mjr 5:a70c0bce770d 3709 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 3710 {
mjr 5:a70c0bce770d 3711 scl = 1;
mjr 5:a70c0bce770d 3712 wait_us(20);
mjr 5:a70c0bce770d 3713 scl = 0;
mjr 5:a70c0bce770d 3714 wait_us(20);
mjr 5:a70c0bce770d 3715 }
mjr 5:a70c0bce770d 3716 }
mjr 76:7f5912b6340e 3717
mjr 76:7f5912b6340e 3718
mjr 14:df700b22ca08 3719 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 3720 //
mjr 33:d832bcab089e 3721 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 3722 // for a given interval before allowing an update.
mjr 33:d832bcab089e 3723 //
mjr 33:d832bcab089e 3724 class Debouncer
mjr 33:d832bcab089e 3725 {
mjr 33:d832bcab089e 3726 public:
mjr 33:d832bcab089e 3727 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 3728 {
mjr 33:d832bcab089e 3729 t.start();
mjr 33:d832bcab089e 3730 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 3731 this->tmin = tmin;
mjr 33:d832bcab089e 3732 }
mjr 33:d832bcab089e 3733
mjr 33:d832bcab089e 3734 // Get the current stable value
mjr 33:d832bcab089e 3735 bool val() const { return stable; }
mjr 33:d832bcab089e 3736
mjr 33:d832bcab089e 3737 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 3738 // input device.
mjr 33:d832bcab089e 3739 void sampleIn(bool val)
mjr 33:d832bcab089e 3740 {
mjr 33:d832bcab089e 3741 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 3742 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 3743 // on the sample reader.
mjr 33:d832bcab089e 3744 if (val != prv)
mjr 33:d832bcab089e 3745 {
mjr 33:d832bcab089e 3746 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 3747 t.reset();
mjr 33:d832bcab089e 3748
mjr 33:d832bcab089e 3749 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 3750 prv = val;
mjr 33:d832bcab089e 3751 }
mjr 33:d832bcab089e 3752 else if (val != stable)
mjr 33:d832bcab089e 3753 {
mjr 33:d832bcab089e 3754 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 3755 // and different from the stable value. This means that
mjr 33:d832bcab089e 3756 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 3757 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 3758 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 3759 if (t.read() > tmin)
mjr 33:d832bcab089e 3760 stable = val;
mjr 33:d832bcab089e 3761 }
mjr 33:d832bcab089e 3762 }
mjr 33:d832bcab089e 3763
mjr 33:d832bcab089e 3764 private:
mjr 33:d832bcab089e 3765 // current stable value
mjr 33:d832bcab089e 3766 bool stable;
mjr 33:d832bcab089e 3767
mjr 33:d832bcab089e 3768 // last raw sample value
mjr 33:d832bcab089e 3769 bool prv;
mjr 33:d832bcab089e 3770
mjr 33:d832bcab089e 3771 // elapsed time since last raw input change
mjr 33:d832bcab089e 3772 Timer t;
mjr 33:d832bcab089e 3773
mjr 33:d832bcab089e 3774 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 3775 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 3776 float tmin;
mjr 33:d832bcab089e 3777 };
mjr 33:d832bcab089e 3778
mjr 33:d832bcab089e 3779
mjr 33:d832bcab089e 3780 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 3781 //
mjr 33:d832bcab089e 3782 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 3783 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 3784 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 3785 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 3786 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 3787 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 3788 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 3789 //
mjr 33:d832bcab089e 3790 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 3791 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 3792 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 3793 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 3794 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 3795 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 3796 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 3797 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 3798 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 3799 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 3800 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 3801 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 3802 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 3803 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 3804 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 3805 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 3806 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 3807 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 3808 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 3809 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 3810 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 3811 //
mjr 40:cc0d9814522b 3812 // This scheme might seem a little convoluted, but it handles a number
mjr 40:cc0d9814522b 3813 // of tricky but likely scenarios:
mjr 33:d832bcab089e 3814 //
mjr 33:d832bcab089e 3815 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 40:cc0d9814522b 3816 // so that the PC goes into "Soft Off" mode when the user turns off
mjr 40:cc0d9814522b 3817 // the cabinet by pushing the power button or using the Shut Down
mjr 40:cc0d9814522b 3818 // command from within Windows. In Windows parlance, this "soft off"
mjr 40:cc0d9814522b 3819 // condition is called ACPI State S5. In this state, the main CPU
mjr 40:cc0d9814522b 3820 // power is turned off, but the motherboard still provides power to
mjr 40:cc0d9814522b 3821 // USB devices. This means that the KL25Z keeps running. Without
mjr 40:cc0d9814522b 3822 // the external power sensing circuit, the only hint that we're in
mjr 40:cc0d9814522b 3823 // this state is that the USB connection to the host goes into Suspend
mjr 40:cc0d9814522b 3824 // mode, but that could mean other things as well. The latch circuit
mjr 40:cc0d9814522b 3825 // lets us tell for sure that we're in this state.
mjr 33:d832bcab089e 3826 //
mjr 33:d832bcab089e 3827 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 3828 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 3829 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 3830 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 3831 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 3832 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 3833 // will power up before or after PSU2, so it's not good enough to
mjr 40:cc0d9814522b 3834 // observe the current state of PSU2 when we first check. If PSU2
mjr 40:cc0d9814522b 3835 // were to come on first, checking only the current state would fool
mjr 40:cc0d9814522b 3836 // us into thinking that no action is required, because we'd only see
mjr 40:cc0d9814522b 3837 // that PSU2 is turned on any time we check. The latch handles this
mjr 40:cc0d9814522b 3838 // case by letting us see that PSU2 was indeed off some time before our
mjr 40:cc0d9814522b 3839 // first check.
mjr 33:d832bcab089e 3840 //
mjr 33:d832bcab089e 3841 // - If the KL25Z is rebooted while the main system is running, or the
mjr 40:cc0d9814522b 3842 // KL25Z is unplugged and plugged back in, we'll correctly leave the
mjr 33:d832bcab089e 3843 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 3844 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 3845 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 3846 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 3847 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 3848 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 3849 //
mjr 33:d832bcab089e 3850
mjr 77:0b96f6867312 3851 // Current PSU2 power state:
mjr 33:d832bcab089e 3852 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 3853 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 3854 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 3855 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 3856 // 5 -> TV relay on
mjr 77:0b96f6867312 3857 // 6 -> sending IR signals designed as TV ON signals
mjr 73:4e8ce0b18915 3858 uint8_t psu2_state = 1;
mjr 73:4e8ce0b18915 3859
mjr 73:4e8ce0b18915 3860 // TV relay state. The TV relay can be controlled by the power-on
mjr 73:4e8ce0b18915 3861 // timer and directly from the PC (via USB commands), so keep a
mjr 73:4e8ce0b18915 3862 // separate state for each:
mjr 73:4e8ce0b18915 3863 // 0x01 -> turned on by power-on timer
mjr 73:4e8ce0b18915 3864 // 0x02 -> turned on by USB command
mjr 73:4e8ce0b18915 3865 uint8_t tv_relay_state = 0x00;
mjr 73:4e8ce0b18915 3866 const uint8_t TV_RELAY_POWERON = 0x01;
mjr 73:4e8ce0b18915 3867 const uint8_t TV_RELAY_USB = 0x02;
mjr 73:4e8ce0b18915 3868
mjr 77:0b96f6867312 3869 // TV ON IR command state. When the main PSU2 power state reaches
mjr 77:0b96f6867312 3870 // the IR phase, we use this sub-state counter to send the TV ON
mjr 77:0b96f6867312 3871 // IR signals. We initialize to state 0 when the main state counter
mjr 77:0b96f6867312 3872 // reaches the IR step. In state 0, we start transmitting the first
mjr 77:0b96f6867312 3873 // (lowest numbered) IR command slot marked as containing a TV ON
mjr 77:0b96f6867312 3874 // code, and advance to state 1. In state 1, we check to see if
mjr 77:0b96f6867312 3875 // the transmitter is still sending; if so, we do nothing, if so
mjr 77:0b96f6867312 3876 // we start transmitting the second TV ON code and advance to state
mjr 77:0b96f6867312 3877 // 2. Continue until we run out of TV ON IR codes, at which point
mjr 77:0b96f6867312 3878 // we advance to the next main psu2_state step.
mjr 77:0b96f6867312 3879 uint8_t tvon_ir_state = 0;
mjr 77:0b96f6867312 3880
mjr 77:0b96f6867312 3881 // TV ON switch relay control output pin
mjr 73:4e8ce0b18915 3882 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 3883
mjr 35:e959ffba78fd 3884 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 3885 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 3886 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 3887
mjr 73:4e8ce0b18915 3888 // Apply the current TV relay state
mjr 73:4e8ce0b18915 3889 void tvRelayUpdate(uint8_t bit, bool state)
mjr 73:4e8ce0b18915 3890 {
mjr 73:4e8ce0b18915 3891 // update the state
mjr 73:4e8ce0b18915 3892 if (state)
mjr 73:4e8ce0b18915 3893 tv_relay_state |= bit;
mjr 73:4e8ce0b18915 3894 else
mjr 73:4e8ce0b18915 3895 tv_relay_state &= ~bit;
mjr 73:4e8ce0b18915 3896
mjr 73:4e8ce0b18915 3897 // set the relay GPIO to the new state
mjr 73:4e8ce0b18915 3898 if (tv_relay != 0)
mjr 73:4e8ce0b18915 3899 tv_relay->write(tv_relay_state != 0);
mjr 73:4e8ce0b18915 3900 }
mjr 35:e959ffba78fd 3901
mjr 77:0b96f6867312 3902 // PSU2 Status update routine. The main loop calls this from time
mjr 77:0b96f6867312 3903 // to time to update the power sensing state and carry out TV ON
mjr 77:0b96f6867312 3904 // functions.
mjr 77:0b96f6867312 3905 Timer powerStatusTimer;
mjr 77:0b96f6867312 3906 uint32_t tv_delay_time_us;
mjr 77:0b96f6867312 3907 void powerStatusUpdate(Config &cfg)
mjr 33:d832bcab089e 3908 {
mjr 77:0b96f6867312 3909 // Only update every 1/4 second or so. Note that if the PSU2
mjr 77:0b96f6867312 3910 // circuit isn't configured, the initialization routine won't
mjr 77:0b96f6867312 3911 // start the timer, so it'll always read zero and we'll always
mjr 77:0b96f6867312 3912 // skip this whole routine.
mjr 77:0b96f6867312 3913 if (powerStatusTimer.read_us() < 250000)
mjr 77:0b96f6867312 3914 return;
mjr 77:0b96f6867312 3915
mjr 77:0b96f6867312 3916 // reset the update timer for next time
mjr 77:0b96f6867312 3917 powerStatusTimer.reset();
mjr 77:0b96f6867312 3918
mjr 77:0b96f6867312 3919 // TV ON timer. We start this timer when we detect a change
mjr 77:0b96f6867312 3920 // in the PSU2 status from OFF to ON. When the timer reaches
mjr 77:0b96f6867312 3921 // the configured TV ON delay time, and the PSU2 power is still
mjr 77:0b96f6867312 3922 // on, we'll trigger the TV ON relay and send the TV ON IR codes.
mjr 35:e959ffba78fd 3923 static Timer tv_timer;
mjr 35:e959ffba78fd 3924
mjr 33:d832bcab089e 3925 // Check our internal state
mjr 33:d832bcab089e 3926 switch (psu2_state)
mjr 33:d832bcab089e 3927 {
mjr 33:d832bcab089e 3928 case 1:
mjr 33:d832bcab089e 3929 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 3930 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 3931 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 3932 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 3933 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 3934 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 3935 {
mjr 33:d832bcab089e 3936 // switch to OFF state
mjr 33:d832bcab089e 3937 psu2_state = 2;
mjr 33:d832bcab089e 3938
mjr 33:d832bcab089e 3939 // try setting the latch
mjr 35:e959ffba78fd 3940 psu2_status_set->write(1);
mjr 33:d832bcab089e 3941 }
mjr 77:0b96f6867312 3942 powerTimerDiagState = 0;
mjr 33:d832bcab089e 3943 break;
mjr 33:d832bcab089e 3944
mjr 33:d832bcab089e 3945 case 2:
mjr 33:d832bcab089e 3946 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 3947 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 3948 psu2_status_set->write(0);
mjr 33:d832bcab089e 3949 psu2_state = 3;
mjr 77:0b96f6867312 3950 powerTimerDiagState = 0;
mjr 33:d832bcab089e 3951 break;
mjr 33:d832bcab089e 3952
mjr 33:d832bcab089e 3953 case 3:
mjr 33:d832bcab089e 3954 // CHECK state: we pulsed SET, and we're now ready to see
mjr 40:cc0d9814522b 3955 // if it stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 3956 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 3957 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 3958 if (psu2_status_sense->read())
mjr 33:d832bcab089e 3959 {
mjr 33:d832bcab089e 3960 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 3961 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 3962 tv_timer.reset();
mjr 33:d832bcab089e 3963 tv_timer.start();
mjr 33:d832bcab089e 3964 psu2_state = 4;
mjr 73:4e8ce0b18915 3965
mjr 73:4e8ce0b18915 3966 // start the power timer diagnostic flashes
mjr 73:4e8ce0b18915 3967 powerTimerDiagState = 2;
mjr 33:d832bcab089e 3968 }
mjr 33:d832bcab089e 3969 else
mjr 33:d832bcab089e 3970 {
mjr 33:d832bcab089e 3971 // The latch didn't stick, so PSU2 was still off at
mjr 33:d832bcab089e 3972 // our last check. Try pulsing it again in case PSU2
mjr 33:d832bcab089e 3973 // was turned on since the last check.
mjr 35:e959ffba78fd 3974 psu2_status_set->write(1);
mjr 33:d832bcab089e 3975 psu2_state = 2;
mjr 33:d832bcab089e 3976 }
mjr 33:d832bcab089e 3977 break;
mjr 33:d832bcab089e 3978
mjr 33:d832bcab089e 3979 case 4:
mjr 77:0b96f6867312 3980 // TV timer countdown in progress. The latch has to stay on during
mjr 77:0b96f6867312 3981 // the countdown; if the latch turns off, PSU2 power must have gone
mjr 77:0b96f6867312 3982 // off again before the countdown finished.
mjr 77:0b96f6867312 3983 if (!psu2_status_sense->read())
mjr 77:0b96f6867312 3984 {
mjr 77:0b96f6867312 3985 // power is off - start a new check cycle
mjr 77:0b96f6867312 3986 psu2_status_set->write(1);
mjr 77:0b96f6867312 3987 psu2_state = 2;
mjr 77:0b96f6867312 3988 break;
mjr 77:0b96f6867312 3989 }
mjr 77:0b96f6867312 3990
mjr 77:0b96f6867312 3991 // Flash the power time diagnostic every two cycles
mjr 77:0b96f6867312 3992 powerTimerDiagState = (powerTimerDiagState + 1) & 0x03;
mjr 77:0b96f6867312 3993
mjr 77:0b96f6867312 3994 // if we've reached the delay time, pulse the relay
mjr 77:0b96f6867312 3995 if (tv_timer.read_us() >= tv_delay_time_us)
mjr 33:d832bcab089e 3996 {
mjr 33:d832bcab089e 3997 // turn on the relay for one timer interval
mjr 73:4e8ce0b18915 3998 tvRelayUpdate(TV_RELAY_POWERON, true);
mjr 33:d832bcab089e 3999 psu2_state = 5;
mjr 77:0b96f6867312 4000
mjr 77:0b96f6867312 4001 // show solid blue on the diagnostic LED while the relay is on
mjr 77:0b96f6867312 4002 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4003 }
mjr 33:d832bcab089e 4004 break;
mjr 33:d832bcab089e 4005
mjr 33:d832bcab089e 4006 case 5:
mjr 33:d832bcab089e 4007 // TV timer relay on. We pulse this for one interval, so
mjr 77:0b96f6867312 4008 // it's now time to turn it off.
mjr 73:4e8ce0b18915 4009 tvRelayUpdate(TV_RELAY_POWERON, false);
mjr 77:0b96f6867312 4010
mjr 77:0b96f6867312 4011 // Proceed to sending any TV ON IR commands
mjr 77:0b96f6867312 4012 psu2_state = 6;
mjr 77:0b96f6867312 4013 tvon_ir_state = 0;
mjr 77:0b96f6867312 4014
mjr 77:0b96f6867312 4015 // diagnostic LEDs off for now
mjr 77:0b96f6867312 4016 powerTimerDiagState = 0;
mjr 77:0b96f6867312 4017 break;
mjr 77:0b96f6867312 4018
mjr 77:0b96f6867312 4019 case 6:
mjr 77:0b96f6867312 4020 // Sending TV ON IR signals. Start with the assumption that
mjr 77:0b96f6867312 4021 // we have no IR work to do, in which case we're done with the
mjr 77:0b96f6867312 4022 // whole TV ON sequence. So by default return to state 1.
mjr 33:d832bcab089e 4023 psu2_state = 1;
mjr 77:0b96f6867312 4024 powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 4025
mjr 77:0b96f6867312 4026 // If we have an IR emitter, check for TV ON IR commands
mjr 77:0b96f6867312 4027 if (ir_tx != 0)
mjr 77:0b96f6867312 4028 {
mjr 77:0b96f6867312 4029 // check to see if the last transmission is still in progress
mjr 77:0b96f6867312 4030 if (ir_tx->isSending())
mjr 77:0b96f6867312 4031 {
mjr 77:0b96f6867312 4032 // We're still sending the last transmission. Stay in
mjr 77:0b96f6867312 4033 // state 6.
mjr 77:0b96f6867312 4034 psu2_state = 6;
mjr 77:0b96f6867312 4035 powerTimerDiagState = 4;
mjr 77:0b96f6867312 4036 break;
mjr 77:0b96f6867312 4037 }
mjr 77:0b96f6867312 4038
mjr 77:0b96f6867312 4039 // The last transmission is done, so check for a new one.
mjr 77:0b96f6867312 4040 // Look for the Nth TV ON IR slot, where N is our state
mjr 77:0b96f6867312 4041 // number.
mjr 77:0b96f6867312 4042 for (int i = 0, n = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 4043 {
mjr 77:0b96f6867312 4044 // is this a TV ON command?
mjr 77:0b96f6867312 4045 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 4046 {
mjr 77:0b96f6867312 4047 // It's a TV ON command - check if it's the one we're
mjr 77:0b96f6867312 4048 // looking for.
mjr 77:0b96f6867312 4049 if (n == tvon_ir_state)
mjr 77:0b96f6867312 4050 {
mjr 77:0b96f6867312 4051 // It's the one. Start transmitting it by
mjr 77:0b96f6867312 4052 // pushing its virtual button.
mjr 77:0b96f6867312 4053 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 4054 ir_tx->pushButton(vb, true);
mjr 77:0b96f6867312 4055
mjr 77:0b96f6867312 4056 // Pushing the button starts transmission, and once
mjr 77:0b96f6867312 4057 // started, the transmission will run to completion
mjr 77:0b96f6867312 4058 // even if the button is no longer pushed. So we
mjr 77:0b96f6867312 4059 // can immediately un-push the button, since we only
mjr 77:0b96f6867312 4060 // need to send the code once.
mjr 77:0b96f6867312 4061 ir_tx->pushButton(vb, false);
mjr 77:0b96f6867312 4062
mjr 77:0b96f6867312 4063 // Advance to the next TV ON IR state, where we'll
mjr 77:0b96f6867312 4064 // await the end of this transmission and move on to
mjr 77:0b96f6867312 4065 // the next one.
mjr 77:0b96f6867312 4066 psu2_state = 6;
mjr 77:0b96f6867312 4067 tvon_ir_state++;
mjr 77:0b96f6867312 4068 break;
mjr 77:0b96f6867312 4069 }
mjr 77:0b96f6867312 4070
mjr 77:0b96f6867312 4071 // it's not ours - count it and keep looking
mjr 77:0b96f6867312 4072 ++n;
mjr 77:0b96f6867312 4073 }
mjr 77:0b96f6867312 4074 }
mjr 77:0b96f6867312 4075 }
mjr 33:d832bcab089e 4076 break;
mjr 33:d832bcab089e 4077 }
mjr 77:0b96f6867312 4078
mjr 77:0b96f6867312 4079 // update the diagnostic LEDs
mjr 77:0b96f6867312 4080 diagLED();
mjr 33:d832bcab089e 4081 }
mjr 33:d832bcab089e 4082
mjr 77:0b96f6867312 4083 // Start the power status timer. If the status sense circuit is enabled
mjr 77:0b96f6867312 4084 // in the configuration, we'll set up the pin connections and start the
mjr 77:0b96f6867312 4085 // timer for our periodic status checks. Does nothing if any of the pins
mjr 77:0b96f6867312 4086 // are configured as NC.
mjr 77:0b96f6867312 4087 void startPowerStatusTimer(Config &cfg)
mjr 35:e959ffba78fd 4088 {
mjr 55:4db125cd11a0 4089 // only start the timer if the pins are configured and the delay
mjr 55:4db125cd11a0 4090 // time is nonzero
mjr 77:0b96f6867312 4091 powerStatusTimer.reset();
mjr 77:0b96f6867312 4092 if (cfg.TVON.statusPin != 0xFF
mjr 77:0b96f6867312 4093 && cfg.TVON.latchPin != 0xFF)
mjr 35:e959ffba78fd 4094 {
mjr 77:0b96f6867312 4095 // set up the power sensing circuit connections
mjr 53:9b2611964afc 4096 psu2_status_sense = new DigitalIn(wirePinName(cfg.TVON.statusPin));
mjr 53:9b2611964afc 4097 psu2_status_set = new DigitalOut(wirePinName(cfg.TVON.latchPin));
mjr 77:0b96f6867312 4098
mjr 77:0b96f6867312 4099 // if there's a TV ON relay, set up its control pin
mjr 77:0b96f6867312 4100 if (cfg.TVON.relayPin != 0xFF)
mjr 77:0b96f6867312 4101 tv_relay = new DigitalOut(wirePinName(cfg.TVON.relayPin));
mjr 77:0b96f6867312 4102
mjr 77:0b96f6867312 4103 // Set the TV ON delay time. We store the time internally in
mjr 77:0b96f6867312 4104 // microseconds, but the configuration stores it in units of
mjr 77:0b96f6867312 4105 // 1/100 second = 10ms = 10000us.
mjr 77:0b96f6867312 4106 tv_delay_time_us = cfg.TVON.delayTime * 10000;;
mjr 77:0b96f6867312 4107
mjr 77:0b96f6867312 4108 // Start the TV timer
mjr 77:0b96f6867312 4109 powerStatusTimer.start();
mjr 35:e959ffba78fd 4110 }
mjr 35:e959ffba78fd 4111 }
mjr 35:e959ffba78fd 4112
mjr 73:4e8ce0b18915 4113 // TV relay manual control timer. This lets us pulse the TV relay
mjr 73:4e8ce0b18915 4114 // under manual control, separately from the TV ON timer.
mjr 73:4e8ce0b18915 4115 Ticker tv_manualTicker;
mjr 73:4e8ce0b18915 4116 void TVManualInt()
mjr 73:4e8ce0b18915 4117 {
mjr 73:4e8ce0b18915 4118 tv_manualTicker.detach();
mjr 73:4e8ce0b18915 4119 tvRelayUpdate(TV_RELAY_USB, false);
mjr 73:4e8ce0b18915 4120 }
mjr 73:4e8ce0b18915 4121
mjr 73:4e8ce0b18915 4122 // Operate the TV ON relay. This allows manual control of the relay
mjr 73:4e8ce0b18915 4123 // from the PC. See protocol message 65 submessage 11.
mjr 73:4e8ce0b18915 4124 //
mjr 73:4e8ce0b18915 4125 // Mode:
mjr 73:4e8ce0b18915 4126 // 0 = turn relay off
mjr 73:4e8ce0b18915 4127 // 1 = turn relay on
mjr 73:4e8ce0b18915 4128 // 2 = pulse relay
mjr 73:4e8ce0b18915 4129 void TVRelay(int mode)
mjr 73:4e8ce0b18915 4130 {
mjr 73:4e8ce0b18915 4131 // if there's no TV relay control pin, ignore this
mjr 73:4e8ce0b18915 4132 if (tv_relay == 0)
mjr 73:4e8ce0b18915 4133 return;
mjr 73:4e8ce0b18915 4134
mjr 73:4e8ce0b18915 4135 switch (mode)
mjr 73:4e8ce0b18915 4136 {
mjr 73:4e8ce0b18915 4137 case 0:
mjr 73:4e8ce0b18915 4138 // relay off
mjr 73:4e8ce0b18915 4139 tvRelayUpdate(TV_RELAY_USB, false);
mjr 73:4e8ce0b18915 4140 break;
mjr 73:4e8ce0b18915 4141
mjr 73:4e8ce0b18915 4142 case 1:
mjr 73:4e8ce0b18915 4143 // relay on
mjr 73:4e8ce0b18915 4144 tvRelayUpdate(TV_RELAY_USB, true);
mjr 73:4e8ce0b18915 4145 break;
mjr 73:4e8ce0b18915 4146
mjr 73:4e8ce0b18915 4147 case 2:
mjr 73:4e8ce0b18915 4148 // Pulse the relay. Turn it on, then set our timer for 250ms.
mjr 73:4e8ce0b18915 4149 tvRelayUpdate(TV_RELAY_USB, true);
mjr 73:4e8ce0b18915 4150 tv_manualTicker.attach(&TVManualInt, 0.25);
mjr 73:4e8ce0b18915 4151 break;
mjr 73:4e8ce0b18915 4152 }
mjr 73:4e8ce0b18915 4153 }
mjr 73:4e8ce0b18915 4154
mjr 73:4e8ce0b18915 4155
mjr 35:e959ffba78fd 4156 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4157 //
mjr 35:e959ffba78fd 4158 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 4159 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 4160 //
mjr 35:e959ffba78fd 4161 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 4162 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 4163 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 4164 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 4165 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 4166 // again each time the firmware is updated.
mjr 35:e959ffba78fd 4167 //
mjr 35:e959ffba78fd 4168 NVM nvm;
mjr 35:e959ffba78fd 4169
mjr 77:0b96f6867312 4170 // Flag: configuration save requested. The USB command message handler
mjr 77:0b96f6867312 4171 // sets this flag when a command is sent requesting the save. We don't
mjr 77:0b96f6867312 4172 // do the save inline in the command handler, but handle it on the next
mjr 77:0b96f6867312 4173 // main loop iteration.
mjr 77:0b96f6867312 4174 const uint8_t SAVE_CONFIG_ONLY = 1;
mjr 77:0b96f6867312 4175 const uint8_t SAVE_CONFIG_AND_REBOOT = 2;
mjr 77:0b96f6867312 4176 uint8_t saveConfigPending = 0;
mjr 77:0b96f6867312 4177
mjr 77:0b96f6867312 4178 // If saveConfigPending == SAVE_CONFIG_AND_REBOOT, this specifies the
mjr 77:0b96f6867312 4179 // delay time in seconds before rebooting.
mjr 77:0b96f6867312 4180 uint8_t saveConfigRebootTime;
mjr 77:0b96f6867312 4181
mjr 35:e959ffba78fd 4182 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 4183 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 4184
mjr 35:e959ffba78fd 4185 // flash memory controller interface
mjr 35:e959ffba78fd 4186 FreescaleIAP iap;
mjr 35:e959ffba78fd 4187
mjr 76:7f5912b6340e 4188 // NVM structure in memory. This has to be aliend on a sector boundary,
mjr 76:7f5912b6340e 4189 // since we have to be able to erase its page(s) in order to write it.
mjr 76:7f5912b6340e 4190 // Further, we have to ensure that nothing else occupies any space within
mjr 76:7f5912b6340e 4191 // the same pages, since we'll erase that entire space whenever we write.
mjr 76:7f5912b6340e 4192 static const union
mjr 76:7f5912b6340e 4193 {
mjr 76:7f5912b6340e 4194 NVM nvm; // the NVM structure
mjr 76:7f5912b6340e 4195 char guard[((sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE)*SECTOR_SIZE];
mjr 76:7f5912b6340e 4196 }
mjr 76:7f5912b6340e 4197 flash_nvm_memory __attribute__ ((aligned(SECTOR_SIZE))) = { };
mjr 76:7f5912b6340e 4198
mjr 35:e959ffba78fd 4199 // figure the flash address as a pointer
mjr 35:e959ffba78fd 4200 NVM *configFlashAddr()
mjr 35:e959ffba78fd 4201 {
mjr 77:0b96f6867312 4202 return (NVM *)&flash_nvm_memory;
mjr 35:e959ffba78fd 4203 }
mjr 35:e959ffba78fd 4204
mjr 76:7f5912b6340e 4205 // Load the config from flash. Returns true if a valid non-default
mjr 76:7f5912b6340e 4206 // configuration was loaded, false if we not. If we return false,
mjr 76:7f5912b6340e 4207 // we load the factory defaults, so the configuration object is valid
mjr 76:7f5912b6340e 4208 // in either case.
mjr 76:7f5912b6340e 4209 bool loadConfigFromFlash()
mjr 35:e959ffba78fd 4210 {
mjr 35:e959ffba78fd 4211 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 4212 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 4213 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 4214 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 4215 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 4216 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 4217 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 4218 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 4219 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 4220 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 4221 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 4222 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 4223 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 4224 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 4225 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 4226 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 4227
mjr 35:e959ffba78fd 4228 // Figure how many sectors we need for our structure
mjr 35:e959ffba78fd 4229 NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 4230
mjr 35:e959ffba78fd 4231 // if the flash is valid, load it; otherwise initialize to defaults
mjr 76:7f5912b6340e 4232 bool nvm_valid = flash->valid();
mjr 76:7f5912b6340e 4233 if (nvm_valid)
mjr 35:e959ffba78fd 4234 {
mjr 35:e959ffba78fd 4235 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 4236 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 4237 }
mjr 35:e959ffba78fd 4238 else
mjr 35:e959ffba78fd 4239 {
mjr 76:7f5912b6340e 4240 // flash is invalid - load factory settings into RAM structure
mjr 35:e959ffba78fd 4241 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 4242 }
mjr 76:7f5912b6340e 4243
mjr 76:7f5912b6340e 4244 // tell the caller what happened
mjr 76:7f5912b6340e 4245 return nvm_valid;
mjr 35:e959ffba78fd 4246 }
mjr 35:e959ffba78fd 4247
mjr 35:e959ffba78fd 4248 void saveConfigToFlash()
mjr 33:d832bcab089e 4249 {
mjr 76:7f5912b6340e 4250 // make sure the plunger sensor isn't busy
mjr 76:7f5912b6340e 4251 waitPlungerIdle();
mjr 76:7f5912b6340e 4252
mjr 76:7f5912b6340e 4253 // get the config block location in the flash memory
mjr 77:0b96f6867312 4254 uint32_t addr = uint32_t(configFlashAddr());
mjr 76:7f5912b6340e 4255
mjr 76:7f5912b6340e 4256 // loop until we save it successfully
mjr 76:7f5912b6340e 4257 for (int i = 0 ; i < 5 ; ++i)
mjr 76:7f5912b6340e 4258 {
mjr 76:7f5912b6340e 4259 // show cyan while writing
mjr 76:7f5912b6340e 4260 diagLED(0, 1, 1);
mjr 76:7f5912b6340e 4261
mjr 76:7f5912b6340e 4262 // save the data
mjr 76:7f5912b6340e 4263 nvm.save(iap, addr);
mjr 76:7f5912b6340e 4264
mjr 76:7f5912b6340e 4265 // diagnostic lights off
mjr 76:7f5912b6340e 4266 diagLED(0, 0, 0);
mjr 76:7f5912b6340e 4267
mjr 76:7f5912b6340e 4268 // verify the data
mjr 76:7f5912b6340e 4269 if (nvm.verify(addr))
mjr 76:7f5912b6340e 4270 {
mjr 77:0b96f6867312 4271 // show a diagnostic success flash (rapid green)
mjr 77:0b96f6867312 4272 for (int j = 0 ; j < 4 ; ++j)
mjr 76:7f5912b6340e 4273 {
mjr 77:0b96f6867312 4274 diagLED(0, 1, 0);
mjr 76:7f5912b6340e 4275 wait_us(50000);
mjr 76:7f5912b6340e 4276 diagLED(0, 0, 0);
mjr 76:7f5912b6340e 4277 wait_us(50000);
mjr 76:7f5912b6340e 4278 }
mjr 76:7f5912b6340e 4279
mjr 76:7f5912b6340e 4280 // success - no need to write again
mjr 76:7f5912b6340e 4281 break;
mjr 76:7f5912b6340e 4282 }
mjr 76:7f5912b6340e 4283 else
mjr 76:7f5912b6340e 4284 {
mjr 76:7f5912b6340e 4285 // Write failed. For diagnostic purposes, flash red a few times.
mjr 76:7f5912b6340e 4286 // Then go back through the loop to make another attempt at the
mjr 76:7f5912b6340e 4287 // write.
mjr 76:7f5912b6340e 4288 for (int j = 0 ; j < 5 ; ++j)
mjr 76:7f5912b6340e 4289 {
mjr 76:7f5912b6340e 4290 diagLED(1, 0, 0);
mjr 76:7f5912b6340e 4291 wait_us(50000);
mjr 76:7f5912b6340e 4292 diagLED(0, 0, 0);
mjr 76:7f5912b6340e 4293 wait_us(50000);
mjr 76:7f5912b6340e 4294 }
mjr 76:7f5912b6340e 4295 }
mjr 76:7f5912b6340e 4296 }
mjr 76:7f5912b6340e 4297 }
mjr 76:7f5912b6340e 4298
mjr 76:7f5912b6340e 4299 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 4300 //
mjr 76:7f5912b6340e 4301 // Host-loaded configuration. The Flash NVM block above is designed to be
mjr 76:7f5912b6340e 4302 // stored from within the firmware; in contrast, the host-loaded config is
mjr 76:7f5912b6340e 4303 // stored by the host, by patching the firwmare binary (.bin) file before
mjr 76:7f5912b6340e 4304 // downloading it to the device.
mjr 76:7f5912b6340e 4305 //
mjr 76:7f5912b6340e 4306 // Ideally, we'd use the host-loaded memory for all configuration updates,
mjr 76:7f5912b6340e 4307 // because the KL25Z doesn't seem to be 100% reliable writing flash itself.
mjr 76:7f5912b6340e 4308 // There seems to be a chance of memory bus contention while a write is in
mjr 76:7f5912b6340e 4309 // progress, which can either corrupt the write or cause the CPU to lock up
mjr 76:7f5912b6340e 4310 // before the write is completed. It seems more reliable to program the
mjr 76:7f5912b6340e 4311 // flash externally, via the OpenSDA connection. Unfortunately, none of
mjr 76:7f5912b6340e 4312 // the available OpenSDA versions are capable of programming specific flash
mjr 76:7f5912b6340e 4313 // sectors; they always erase the entire flash memory space. We *could*
mjr 76:7f5912b6340e 4314 // make the Windows config program simply re-download the entire firmware
mjr 76:7f5912b6340e 4315 // for every configuration update, but I'd rather not because of the extra
mjr 76:7f5912b6340e 4316 // wear this would put on the flash. So, as a compromise, we'll use the
mjr 76:7f5912b6340e 4317 // host-loaded config whenever the user explicitly updates the firmware,
mjr 76:7f5912b6340e 4318 // but we'll use the on-board writer when only making a config change.
mjr 76:7f5912b6340e 4319 //
mjr 76:7f5912b6340e 4320 // The memory here is stored using the same format as the USB "Set Config
mjr 76:7f5912b6340e 4321 // Variable" command. These messages are 8 bytes long and start with a
mjr 76:7f5912b6340e 4322 // byte value 66, followed by the variable ID, followed by the variable
mjr 76:7f5912b6340e 4323 // value data in a format defined separately for each variable. To load
mjr 76:7f5912b6340e 4324 // the data, we'll start at the first byte after the signature, and
mjr 76:7f5912b6340e 4325 // interpret each 8-byte block as a type 66 message. If the first byte
mjr 76:7f5912b6340e 4326 // of a block is not 66, we'll take it as the end of the data.
mjr 76:7f5912b6340e 4327 //
mjr 76:7f5912b6340e 4328 // We provide a block of storage here big enough for 1,024 variables.
mjr 76:7f5912b6340e 4329 // The header consists of a 30-byte signature followed by two bytes giving
mjr 76:7f5912b6340e 4330 // the available space in the area, in this case 8192 == 0x0200. The
mjr 76:7f5912b6340e 4331 // length is little-endian. Note that the linker will implicitly zero
mjr 76:7f5912b6340e 4332 // the rest of the block, so if the host doesn't populate it, we'll see
mjr 76:7f5912b6340e 4333 // that it's empty by virtue of not containing the required '66' byte
mjr 76:7f5912b6340e 4334 // prefix for the first 8-byte variable block.
mjr 76:7f5912b6340e 4335 static const uint8_t hostLoadedConfig[8192+32]
mjr 76:7f5912b6340e 4336 __attribute__ ((aligned(SECTOR_SIZE))) =
mjr 76:7f5912b6340e 4337 "///Pinscape.HostLoadedConfig//\0\040"; // 30 byte signature + 2 byte length
mjr 76:7f5912b6340e 4338
mjr 76:7f5912b6340e 4339 // Get a pointer to the first byte of the configuration data
mjr 76:7f5912b6340e 4340 const uint8_t *getHostLoadedConfigData()
mjr 76:7f5912b6340e 4341 {
mjr 76:7f5912b6340e 4342 // the first configuration variable byte immediately follows the
mjr 76:7f5912b6340e 4343 // 32-byte signature header
mjr 76:7f5912b6340e 4344 return hostLoadedConfig + 32;
mjr 76:7f5912b6340e 4345 };
mjr 76:7f5912b6340e 4346
mjr 76:7f5912b6340e 4347 // forward reference to config var store function
mjr 76:7f5912b6340e 4348 void configVarSet(const uint8_t *);
mjr 76:7f5912b6340e 4349
mjr 76:7f5912b6340e 4350 // Load the host-loaded configuration data into the active (RAM)
mjr 76:7f5912b6340e 4351 // configuration object.
mjr 76:7f5912b6340e 4352 void loadHostLoadedConfig()
mjr 76:7f5912b6340e 4353 {
mjr 76:7f5912b6340e 4354 // Start at the first configuration variable. Each variable
mjr 76:7f5912b6340e 4355 // block is in the format of a Set Config Variable command in
mjr 76:7f5912b6340e 4356 // the USB protocol, so each block starts with a byte value of
mjr 76:7f5912b6340e 4357 // 66 and is 8 bytes long. Continue as long as we find valid
mjr 76:7f5912b6340e 4358 // variable blocks, or reach end end of the block.
mjr 76:7f5912b6340e 4359 const uint8_t *start = getHostLoadedConfigData();
mjr 76:7f5912b6340e 4360 const uint8_t *end = hostLoadedConfig + sizeof(hostLoadedConfig);
mjr 76:7f5912b6340e 4361 for (const uint8_t *p = getHostLoadedConfigData() ; start < end && *p == 66 ; p += 8)
mjr 76:7f5912b6340e 4362 {
mjr 76:7f5912b6340e 4363 // load this variable
mjr 76:7f5912b6340e 4364 configVarSet(p);
mjr 76:7f5912b6340e 4365 }
mjr 35:e959ffba78fd 4366 }
mjr 35:e959ffba78fd 4367
mjr 35:e959ffba78fd 4368 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4369 //
mjr 55:4db125cd11a0 4370 // Pixel dump mode - the host requested a dump of image sensor pixels
mjr 55:4db125cd11a0 4371 // (helpful for installing and setting up the sensor and light source)
mjr 55:4db125cd11a0 4372 //
mjr 55:4db125cd11a0 4373 bool reportPlungerStat = false;
mjr 55:4db125cd11a0 4374 uint8_t reportPlungerStatFlags; // plunger pixel report flag bits (see ccdSensor.h)
mjr 55:4db125cd11a0 4375 uint8_t reportPlungerStatTime; // extra exposure time for plunger pixel report
mjr 55:4db125cd11a0 4376
mjr 55:4db125cd11a0 4377
mjr 55:4db125cd11a0 4378
mjr 55:4db125cd11a0 4379 // ---------------------------------------------------------------------------
mjr 55:4db125cd11a0 4380 //
mjr 40:cc0d9814522b 4381 // Night mode setting updates
mjr 40:cc0d9814522b 4382 //
mjr 38:091e511ce8a0 4383
mjr 38:091e511ce8a0 4384 // Turn night mode on or off
mjr 38:091e511ce8a0 4385 static void setNightMode(bool on)
mjr 38:091e511ce8a0 4386 {
mjr 77:0b96f6867312 4387 // Set the new night mode flag in the noisy output class. Note
mjr 77:0b96f6867312 4388 // that we use the status report bit flag value 0x02 when on, so
mjr 77:0b96f6867312 4389 // that we can just '|' this into the overall status bits.
mjr 77:0b96f6867312 4390 nightMode = on ? 0x02 : 0x00;
mjr 55:4db125cd11a0 4391
mjr 40:cc0d9814522b 4392 // update the special output pin that shows the night mode state
mjr 53:9b2611964afc 4393 int port = int(cfg.nightMode.port) - 1;
mjr 53:9b2611964afc 4394 if (port >= 0 && port < numOutputs)
mjr 53:9b2611964afc 4395 lwPin[port]->set(nightMode ? 255 : 0);
mjr 76:7f5912b6340e 4396
mjr 76:7f5912b6340e 4397 // Reset all outputs at their current value, so that the underlying
mjr 76:7f5912b6340e 4398 // physical outputs get turned on or off as appropriate for the night
mjr 76:7f5912b6340e 4399 // mode change.
mjr 76:7f5912b6340e 4400 for (int i = 0 ; i < numOutputs ; ++i)
mjr 76:7f5912b6340e 4401 lwPin[i]->set(outLevel[i]);
mjr 76:7f5912b6340e 4402
mjr 76:7f5912b6340e 4403 // update 74HC595 outputs
mjr 76:7f5912b6340e 4404 if (hc595 != 0)
mjr 76:7f5912b6340e 4405 hc595->update();
mjr 38:091e511ce8a0 4406 }
mjr 38:091e511ce8a0 4407
mjr 38:091e511ce8a0 4408 // Toggle night mode
mjr 38:091e511ce8a0 4409 static void toggleNightMode()
mjr 38:091e511ce8a0 4410 {
mjr 53:9b2611964afc 4411 setNightMode(!nightMode);
mjr 38:091e511ce8a0 4412 }
mjr 38:091e511ce8a0 4413
mjr 38:091e511ce8a0 4414
mjr 38:091e511ce8a0 4415 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 4416 //
mjr 35:e959ffba78fd 4417 // Plunger Sensor
mjr 35:e959ffba78fd 4418 //
mjr 35:e959ffba78fd 4419
mjr 35:e959ffba78fd 4420 // the plunger sensor interface object
mjr 35:e959ffba78fd 4421 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 4422
mjr 76:7f5912b6340e 4423 // wait for the plunger sensor to complete any outstanding read
mjr 76:7f5912b6340e 4424 static void waitPlungerIdle(void)
mjr 76:7f5912b6340e 4425 {
mjr 76:7f5912b6340e 4426 while (!plungerSensor->ready()) { }
mjr 76:7f5912b6340e 4427 }
mjr 76:7f5912b6340e 4428
mjr 35:e959ffba78fd 4429 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 4430 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 4431 void createPlunger()
mjr 35:e959ffba78fd 4432 {
mjr 35:e959ffba78fd 4433 // create the new sensor object according to the type
mjr 35:e959ffba78fd 4434 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 4435 {
mjr 35:e959ffba78fd 4436 case PlungerType_TSL1410RS:
mjr 69:cc5039284fac 4437 // TSL1410R, serial mode (all pixels read in one file)
mjr 35:e959ffba78fd 4438 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 4439 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 4440 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4441 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 4442 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 4443 NC);
mjr 35:e959ffba78fd 4444 break;
mjr 35:e959ffba78fd 4445
mjr 35:e959ffba78fd 4446 case PlungerType_TSL1410RP:
mjr 69:cc5039284fac 4447 // TSL1410R, parallel mode (each half-sensor's pixels read separately)
mjr 35:e959ffba78fd 4448 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 4449 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 4450 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4451 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 4452 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 4453 wirePinName(cfg.plunger.sensorPin[3]));
mjr 35:e959ffba78fd 4454 break;
mjr 35:e959ffba78fd 4455
mjr 69:cc5039284fac 4456 case PlungerType_TSL1412SS:
mjr 69:cc5039284fac 4457 // TSL1412S, serial mode
mjr 35:e959ffba78fd 4458 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 4459 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 4460 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4461 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 4462 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 4463 NC);
mjr 35:e959ffba78fd 4464 break;
mjr 35:e959ffba78fd 4465
mjr 69:cc5039284fac 4466 case PlungerType_TSL1412SP:
mjr 69:cc5039284fac 4467 // TSL1412S, parallel mode
mjr 35:e959ffba78fd 4468 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 4469 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 4470 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4471 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 4472 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 4473 wirePinName(cfg.plunger.sensorPin[3]));
mjr 35:e959ffba78fd 4474 break;
mjr 35:e959ffba78fd 4475
mjr 35:e959ffba78fd 4476 case PlungerType_Pot:
mjr 35:e959ffba78fd 4477 // pins are: AO
mjr 53:9b2611964afc 4478 plungerSensor = new PlungerSensorPot(
mjr 53:9b2611964afc 4479 wirePinName(cfg.plunger.sensorPin[0]));
mjr 35:e959ffba78fd 4480 break;
mjr 35:e959ffba78fd 4481
mjr 35:e959ffba78fd 4482 case PlungerType_None:
mjr 35:e959ffba78fd 4483 default:
mjr 35:e959ffba78fd 4484 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 4485 break;
mjr 35:e959ffba78fd 4486 }
mjr 33:d832bcab089e 4487 }
mjr 33:d832bcab089e 4488
mjr 52:8298b2a73eb2 4489 // Global plunger calibration mode flag
mjr 52:8298b2a73eb2 4490 bool plungerCalMode;
mjr 52:8298b2a73eb2 4491
mjr 48:058ace2aed1d 4492 // Plunger reader
mjr 51:57eb311faafa 4493 //
mjr 51:57eb311faafa 4494 // This class encapsulates our plunger data processing. At the simplest
mjr 51:57eb311faafa 4495 // level, we read the position from the sensor, adjust it for the
mjr 51:57eb311faafa 4496 // calibration settings, and report the calibrated position to the host.
mjr 51:57eb311faafa 4497 //
mjr 51:57eb311faafa 4498 // In addition, we constantly monitor the data for "firing" motions.
mjr 51:57eb311faafa 4499 // A firing motion is when the user pulls back the plunger and releases
mjr 51:57eb311faafa 4500 // it, allowing it to shoot forward under the force of the main spring.
mjr 51:57eb311faafa 4501 // When we detect that this is happening, we briefly stop reporting the
mjr 51:57eb311faafa 4502 // real physical position that we're reading from the sensor, and instead
mjr 51:57eb311faafa 4503 // report a synthetic series of positions that depicts an idealized
mjr 51:57eb311faafa 4504 // firing motion.
mjr 51:57eb311faafa 4505 //
mjr 51:57eb311faafa 4506 // The point of the synthetic reports is to correct for distortions
mjr 51:57eb311faafa 4507 // created by the joystick interface conventions used by VP and other
mjr 51:57eb311faafa 4508 // PC pinball emulators. The convention they use is simply to have the
mjr 51:57eb311faafa 4509 // plunger device report the instantaneous position of the real plunger.
mjr 51:57eb311faafa 4510 // The PC software polls this reported position periodically, and moves
mjr 51:57eb311faafa 4511 // the on-screen virtual plunger in sync with the real plunger. This
mjr 51:57eb311faafa 4512 // works fine for human-scale motion when the user is manually moving
mjr 51:57eb311faafa 4513 // the plunger. But it doesn't work for the high speed motion of a
mjr 51:57eb311faafa 4514 // release. The plunger simply moves too fast. VP polls in about 10ms
mjr 51:57eb311faafa 4515 // intervals; the plunger takes about 50ms to travel from fully
mjr 51:57eb311faafa 4516 // retracted to the park position when released. The low sampling
mjr 51:57eb311faafa 4517 // rate relative to the rate of change of the sampled data creates
mjr 51:57eb311faafa 4518 // a classic digital aliasing effect.
mjr 51:57eb311faafa 4519 //
mjr 51:57eb311faafa 4520 // The synthetic reporting scheme compensates for the interface
mjr 51:57eb311faafa 4521 // distortions by essentially changing to a coarse enough timescale
mjr 51:57eb311faafa 4522 // that VP can reliably interpret the readings. Conceptually, there
mjr 51:57eb311faafa 4523 // are three steps involved in doing this. First, we analyze the
mjr 51:57eb311faafa 4524 // actual sensor data to detect and characterize the release motion.
mjr 51:57eb311faafa 4525 // Second, once we think we have a release in progress, we fit the
mjr 51:57eb311faafa 4526 // data to a mathematical model of the release. The model we use is
mjr 51:57eb311faafa 4527 // dead simple: we consider the release to have one parameter, namely
mjr 51:57eb311faafa 4528 // the retraction distance at the moment the user lets go. This is an
mjr 51:57eb311faafa 4529 // excellent proxy in the real physical system for the final speed
mjr 51:57eb311faafa 4530 // when the plunger hits the ball, and it also happens to match how
mjr 51:57eb311faafa 4531 // VP models it internally. Third, we construct synthetic reports
mjr 51:57eb311faafa 4532 // that will make VP's internal state match our model. This is also
mjr 51:57eb311faafa 4533 // pretty simple: we just need to send VP the maximum retraction
mjr 51:57eb311faafa 4534 // distance for long enough to be sure that it polls it at least
mjr 51:57eb311faafa 4535 // once, and then send it the park position for long enough to
mjr 51:57eb311faafa 4536 // ensure that VP will complete the same firing motion. The
mjr 51:57eb311faafa 4537 // immediate jump from the maximum point to the zero point will
mjr 51:57eb311faafa 4538 // cause VP to move its simulation model plunger forward from the
mjr 51:57eb311faafa 4539 // starting point at its natural spring acceleration rate, which
mjr 51:57eb311faafa 4540 // is exactly what the real plunger just did.
mjr 51:57eb311faafa 4541 //
mjr 48:058ace2aed1d 4542 class PlungerReader
mjr 48:058ace2aed1d 4543 {
mjr 48:058ace2aed1d 4544 public:
mjr 48:058ace2aed1d 4545 PlungerReader()
mjr 48:058ace2aed1d 4546 {
mjr 48:058ace2aed1d 4547 // not in a firing event yet
mjr 48:058ace2aed1d 4548 firing = 0;
mjr 48:058ace2aed1d 4549
mjr 48:058ace2aed1d 4550 // no history yet
mjr 48:058ace2aed1d 4551 histIdx = 0;
mjr 55:4db125cd11a0 4552
mjr 55:4db125cd11a0 4553 // initialize the filter
mjr 55:4db125cd11a0 4554 initFilter();
mjr 48:058ace2aed1d 4555 }
mjr 76:7f5912b6340e 4556
mjr 48:058ace2aed1d 4557 // Collect a reading from the plunger sensor. The main loop calls
mjr 48:058ace2aed1d 4558 // this frequently to read the current raw position data from the
mjr 48:058ace2aed1d 4559 // sensor. We analyze the raw data to produce the calibrated
mjr 48:058ace2aed1d 4560 // position that we report to the PC via the joystick interface.
mjr 48:058ace2aed1d 4561 void read()
mjr 48:058ace2aed1d 4562 {
mjr 76:7f5912b6340e 4563 // if the sensor is busy, skip the reading on this round
mjr 76:7f5912b6340e 4564 if (!plungerSensor->ready())
mjr 76:7f5912b6340e 4565 return;
mjr 76:7f5912b6340e 4566
mjr 48:058ace2aed1d 4567 // Read a sample from the sensor
mjr 48:058ace2aed1d 4568 PlungerReading r;
mjr 48:058ace2aed1d 4569 if (plungerSensor->read(r))
mjr 48:058ace2aed1d 4570 {
mjr 69:cc5039284fac 4571 // filter the raw sensor reading
mjr 69:cc5039284fac 4572 applyPreFilter(r);
mjr 69:cc5039284fac 4573
mjr 51:57eb311faafa 4574 // Pull the previous reading from the history
mjr 50:40015764bbe6 4575 const PlungerReading &prv = nthHist(0);
mjr 48:058ace2aed1d 4576
mjr 69:cc5039284fac 4577 // If the new reading is within 1ms of the previous reading,
mjr 48:058ace2aed1d 4578 // ignore it. We require a minimum time between samples to
mjr 48:058ace2aed1d 4579 // ensure that we have a usable amount of precision in the
mjr 48:058ace2aed1d 4580 // denominator (the time interval) for calculating the plunger
mjr 69:cc5039284fac 4581 // velocity. The CCD sensor hardware takes about 2.5ms to
mjr 69:cc5039284fac 4582 // read, so it will never be affected by this, but other sensor
mjr 69:cc5039284fac 4583 // types don't all have the same hardware cycle time, so we need
mjr 69:cc5039284fac 4584 // to throttle them artificially. E.g., the potentiometer only
mjr 69:cc5039284fac 4585 // needs one ADC sample per reading, which only takes about 15us.
mjr 69:cc5039284fac 4586 // We don't need to check which sensor type we have here; we
mjr 69:cc5039284fac 4587 // just ignore readings until the minimum interval has passed,
mjr 69:cc5039284fac 4588 // so if the sensor is already slower than this, we'll end up
mjr 69:cc5039284fac 4589 // using all of its readings.
mjr 69:cc5039284fac 4590 if (uint32_t(r.t - prv.t) < 1000UL)
mjr 48:058ace2aed1d 4591 return;
mjr 53:9b2611964afc 4592
mjr 53:9b2611964afc 4593 // check for calibration mode
mjr 53:9b2611964afc 4594 if (plungerCalMode)
mjr 53:9b2611964afc 4595 {
mjr 53:9b2611964afc 4596 // Calibration mode. Adjust the calibration bounds to fit
mjr 53:9b2611964afc 4597 // the value. If this value is beyond the current min or max,
mjr 53:9b2611964afc 4598 // expand the envelope to include this new value.
mjr 53:9b2611964afc 4599 if (r.pos > cfg.plunger.cal.max)
mjr 53:9b2611964afc 4600 cfg.plunger.cal.max = r.pos;
mjr 53:9b2611964afc 4601 if (r.pos < cfg.plunger.cal.min)
mjr 53:9b2611964afc 4602 cfg.plunger.cal.min = r.pos;
mjr 76:7f5912b6340e 4603
mjr 76:7f5912b6340e 4604 // update our cached calibration data
mjr 76:7f5912b6340e 4605 onUpdateCal();
mjr 50:40015764bbe6 4606
mjr 53:9b2611964afc 4607 // If we're in calibration state 0, we're waiting for the
mjr 53:9b2611964afc 4608 // plunger to come to rest at the park position so that we
mjr 53:9b2611964afc 4609 // can take a sample of the park position. Check to see if
mjr 53:9b2611964afc 4610 // we've been at rest for a minimum interval.
mjr 53:9b2611964afc 4611 if (calState == 0)
mjr 53:9b2611964afc 4612 {
mjr 53:9b2611964afc 4613 if (abs(r.pos - calZeroStart.pos) < 65535/3/50)
mjr 53:9b2611964afc 4614 {
mjr 53:9b2611964afc 4615 // we're close enough - make sure we've been here long enough
mjr 53:9b2611964afc 4616 if (uint32_t(r.t - calZeroStart.t) > 100000UL)
mjr 53:9b2611964afc 4617 {
mjr 53:9b2611964afc 4618 // we've been at rest long enough - count it
mjr 53:9b2611964afc 4619 calZeroPosSum += r.pos;
mjr 53:9b2611964afc 4620 calZeroPosN += 1;
mjr 53:9b2611964afc 4621
mjr 53:9b2611964afc 4622 // update the zero position from the new average
mjr 53:9b2611964afc 4623 cfg.plunger.cal.zero = uint16_t(calZeroPosSum / calZeroPosN);
mjr 76:7f5912b6340e 4624 onUpdateCal();
mjr 53:9b2611964afc 4625
mjr 53:9b2611964afc 4626 // switch to calibration state 1 - at rest
mjr 53:9b2611964afc 4627 calState = 1;
mjr 53:9b2611964afc 4628 }
mjr 53:9b2611964afc 4629 }
mjr 53:9b2611964afc 4630 else
mjr 53:9b2611964afc 4631 {
mjr 53:9b2611964afc 4632 // we're not close to the last position - start again here
mjr 53:9b2611964afc 4633 calZeroStart = r;
mjr 53:9b2611964afc 4634 }
mjr 53:9b2611964afc 4635 }
mjr 53:9b2611964afc 4636
mjr 53:9b2611964afc 4637 // Rescale to the joystick range, and adjust for the current
mjr 53:9b2611964afc 4638 // park position, but don't calibrate. We don't know the maximum
mjr 53:9b2611964afc 4639 // point yet, so we can't calibrate the range.
mjr 53:9b2611964afc 4640 r.pos = int(
mjr 53:9b2611964afc 4641 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 4642 / (65535 - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 4643 }
mjr 53:9b2611964afc 4644 else
mjr 53:9b2611964afc 4645 {
mjr 53:9b2611964afc 4646 // Not in calibration mode. Apply the existing calibration and
mjr 53:9b2611964afc 4647 // rescale to the joystick range.
mjr 76:7f5912b6340e 4648 r.pos = applyCal(r.pos);
mjr 53:9b2611964afc 4649
mjr 53:9b2611964afc 4650 // limit the result to the valid joystick range
mjr 53:9b2611964afc 4651 if (r.pos > JOYMAX)
mjr 53:9b2611964afc 4652 r.pos = JOYMAX;
mjr 53:9b2611964afc 4653 else if (r.pos < -JOYMAX)
mjr 53:9b2611964afc 4654 r.pos = -JOYMAX;
mjr 53:9b2611964afc 4655 }
mjr 50:40015764bbe6 4656
mjr 50:40015764bbe6 4657 // Calculate the velocity from the second-to-last reading
mjr 50:40015764bbe6 4658 // to here, in joystick distance units per microsecond.
mjr 50:40015764bbe6 4659 // Note that we use the second-to-last reading rather than
mjr 50:40015764bbe6 4660 // the very last reading to give ourselves a little longer
mjr 50:40015764bbe6 4661 // time base. The time base is so short between consecutive
mjr 50:40015764bbe6 4662 // readings that the error bars in the position would be too
mjr 50:40015764bbe6 4663 // large.
mjr 50:40015764bbe6 4664 //
mjr 50:40015764bbe6 4665 // For reference, the physical plunger velocity ranges up
mjr 50:40015764bbe6 4666 // to about 100,000 joystick distance units/sec. This is
mjr 50:40015764bbe6 4667 // based on empirical measurements. The typical time for
mjr 50:40015764bbe6 4668 // a real plunger to travel the full distance when released
mjr 50:40015764bbe6 4669 // from full retraction is about 85ms, so the average velocity
mjr 50:40015764bbe6 4670 // covering this distance is about 56,000 units/sec. The
mjr 50:40015764bbe6 4671 // peak is probably about twice that. In real-world units,
mjr 50:40015764bbe6 4672 // this translates to an average speed of about .75 m/s and
mjr 50:40015764bbe6 4673 // a peak of about 1.5 m/s.
mjr 50:40015764bbe6 4674 //
mjr 50:40015764bbe6 4675 // Note that we actually calculate the value here in units
mjr 50:40015764bbe6 4676 // per *microsecond* - the discussion above is in terms of
mjr 50:40015764bbe6 4677 // units/sec because that's more on a human scale. Our
mjr 50:40015764bbe6 4678 // choice of internal units here really isn't important,
mjr 50:40015764bbe6 4679 // since we only use the velocity for comparison purposes,
mjr 50:40015764bbe6 4680 // to detect acceleration trends. We therefore save ourselves
mjr 50:40015764bbe6 4681 // a little CPU time by using the natural units of our inputs.
mjr 76:7f5912b6340e 4682 //
mjr 76:7f5912b6340e 4683 // We don't care about the absolute velocity; this is a purely
mjr 76:7f5912b6340e 4684 // relative calculation. So to speed things up, calculate it
mjr 76:7f5912b6340e 4685 // in the integer domain, using a fixed-point representation
mjr 76:7f5912b6340e 4686 // with a 64K scale. In other words, with the stored values
mjr 76:7f5912b6340e 4687 // shifted left 16 bits from the actual values: the value 1
mjr 76:7f5912b6340e 4688 // is stored as 1<<16. The position readings are in the range
mjr 76:7f5912b6340e 4689 // -JOYMAX..JOYMAX, which fits in 16 bits, and the time
mjr 76:7f5912b6340e 4690 // differences will generally be on the scale of a few
mjr 76:7f5912b6340e 4691 // milliseconds = thousands of microseconds. So the velocity
mjr 76:7f5912b6340e 4692 // figures will fit nicely into a 32-bit fixed point value with
mjr 76:7f5912b6340e 4693 // a 64K scale factor.
mjr 51:57eb311faafa 4694 const PlungerReading &prv2 = nthHist(1);
mjr 76:7f5912b6340e 4695 int v = ((r.pos - prv2.pos) << 16)/(r.t - prv2.t);
mjr 50:40015764bbe6 4696
mjr 50:40015764bbe6 4697 // presume we'll report the latest instantaneous reading
mjr 50:40015764bbe6 4698 z = r.pos;
mjr 48:058ace2aed1d 4699
mjr 50:40015764bbe6 4700 // Check firing events
mjr 50:40015764bbe6 4701 switch (firing)
mjr 50:40015764bbe6 4702 {
mjr 50:40015764bbe6 4703 case 0:
mjr 50:40015764bbe6 4704 // Default state - not in a firing event.
mjr 50:40015764bbe6 4705
mjr 50:40015764bbe6 4706 // If we have forward motion from a position that's retracted
mjr 50:40015764bbe6 4707 // beyond a threshold, enter phase 1. If we're not pulled back
mjr 50:40015764bbe6 4708 // far enough, don't bother with this, as a release wouldn't
mjr 50:40015764bbe6 4709 // be strong enough to require the synthetic firing treatment.
mjr 50:40015764bbe6 4710 if (v < 0 && r.pos > JOYMAX/6)
mjr 50:40015764bbe6 4711 {
mjr 53:9b2611964afc 4712 // enter firing phase 1
mjr 50:40015764bbe6 4713 firingMode(1);
mjr 50:40015764bbe6 4714
mjr 53:9b2611964afc 4715 // if in calibration state 1 (at rest), switch to state 2 (not
mjr 53:9b2611964afc 4716 // at rest)
mjr 53:9b2611964afc 4717 if (calState == 1)
mjr 53:9b2611964afc 4718 calState = 2;
mjr 53:9b2611964afc 4719
mjr 50:40015764bbe6 4720 // we don't have a freeze position yet, but note the start time
mjr 50:40015764bbe6 4721 f1.pos = 0;
mjr 50:40015764bbe6 4722 f1.t = r.t;
mjr 50:40015764bbe6 4723
mjr 50:40015764bbe6 4724 // Figure the barrel spring "bounce" position in case we complete
mjr 50:40015764bbe6 4725 // the firing event. This is the amount that the forward momentum
mjr 50:40015764bbe6 4726 // of the plunger will compress the barrel spring at the peak of
mjr 50:40015764bbe6 4727 // the forward travel during the release. Assume that this is
mjr 50:40015764bbe6 4728 // linearly proportional to the starting retraction distance.
mjr 50:40015764bbe6 4729 // The barrel spring is about 1/6 the length of the main spring,
mjr 50:40015764bbe6 4730 // so figure it compresses by 1/6 the distance. (This is overly
mjr 53:9b2611964afc 4731 // simplistic and not very accurate, but it seems to give good
mjr 50:40015764bbe6 4732 // visual results, and that's all it's for.)
mjr 50:40015764bbe6 4733 f2.pos = -r.pos/6;
mjr 50:40015764bbe6 4734 }
mjr 50:40015764bbe6 4735 break;
mjr 50:40015764bbe6 4736
mjr 50:40015764bbe6 4737 case 1:
mjr 50:40015764bbe6 4738 // Phase 1 - acceleration. If we cross the zero point, trigger
mjr 50:40015764bbe6 4739 // the firing event. Otherwise, continue monitoring as long as we
mjr 50:40015764bbe6 4740 // see acceleration in the forward direction.
mjr 50:40015764bbe6 4741 if (r.pos <= 0)
mjr 50:40015764bbe6 4742 {
mjr 50:40015764bbe6 4743 // switch to the synthetic firing mode
mjr 50:40015764bbe6 4744 firingMode(2);
mjr 50:40015764bbe6 4745 z = f2.pos;
mjr 50:40015764bbe6 4746
mjr 50:40015764bbe6 4747 // note the start time for the firing phase
mjr 50:40015764bbe6 4748 f2.t = r.t;
mjr 53:9b2611964afc 4749
mjr 53:9b2611964afc 4750 // if in calibration mode, and we're in state 2 (moving),
mjr 53:9b2611964afc 4751 // collect firing statistics for calibration purposes
mjr 53:9b2611964afc 4752 if (plungerCalMode && calState == 2)
mjr 53:9b2611964afc 4753 {
mjr 53:9b2611964afc 4754 // collect a new zero point for the average when we
mjr 53:9b2611964afc 4755 // come to rest
mjr 53:9b2611964afc 4756 calState = 0;
mjr 53:9b2611964afc 4757
mjr 53:9b2611964afc 4758 // collect average firing time statistics in millseconds, if
mjr 53:9b2611964afc 4759 // it's in range (20 to 255 ms)
mjr 53:9b2611964afc 4760 int dt = uint32_t(r.t - f1.t)/1000UL;
mjr 53:9b2611964afc 4761 if (dt >= 20 && dt <= 255)
mjr 53:9b2611964afc 4762 {
mjr 53:9b2611964afc 4763 calRlsTimeSum += dt;
mjr 53:9b2611964afc 4764 calRlsTimeN += 1;
mjr 53:9b2611964afc 4765 cfg.plunger.cal.tRelease = uint8_t(calRlsTimeSum / calRlsTimeN);
mjr 53:9b2611964afc 4766 }
mjr 53:9b2611964afc 4767 }
mjr 50:40015764bbe6 4768 }
mjr 50:40015764bbe6 4769 else if (v < vprv2)
mjr 50:40015764bbe6 4770 {
mjr 50:40015764bbe6 4771 // We're still accelerating, and we haven't crossed the zero
mjr 50:40015764bbe6 4772 // point yet - stay in phase 1. (Note that forward motion is
mjr 50:40015764bbe6 4773 // negative velocity, so accelerating means that the new
mjr 50:40015764bbe6 4774 // velocity is more negative than the previous one, which
mjr 50:40015764bbe6 4775 // is to say numerically less than - that's why the test
mjr 50:40015764bbe6 4776 // for acceleration is the seemingly backwards 'v < vprv'.)
mjr 50:40015764bbe6 4777
mjr 50:40015764bbe6 4778 // If we've been accelerating for at least 20ms, we're probably
mjr 50:40015764bbe6 4779 // really doing a release. Jump back to the recent local
mjr 50:40015764bbe6 4780 // maximum where the release *really* started. This is always
mjr 50:40015764bbe6 4781 // a bit before we started seeing sustained accleration, because
mjr 50:40015764bbe6 4782 // the plunger motion for the first few milliseconds is too slow
mjr 50:40015764bbe6 4783 // for our sensor precision to reliably detect acceleration.
mjr 50:40015764bbe6 4784 if (f1.pos != 0)
mjr 50:40015764bbe6 4785 {
mjr 50:40015764bbe6 4786 // we have a reset point - freeze there
mjr 50:40015764bbe6 4787 z = f1.pos;
mjr 50:40015764bbe6 4788 }
mjr 50:40015764bbe6 4789 else if (uint32_t(r.t - f1.t) >= 20000UL)
mjr 50:40015764bbe6 4790 {
mjr 50:40015764bbe6 4791 // it's been long enough - set a reset point.
mjr 50:40015764bbe6 4792 f1.pos = z = histLocalMax(r.t, 50000UL);
mjr 50:40015764bbe6 4793 }
mjr 50:40015764bbe6 4794 }
mjr 50:40015764bbe6 4795 else
mjr 50:40015764bbe6 4796 {
mjr 50:40015764bbe6 4797 // We're not accelerating. Cancel the firing event.
mjr 50:40015764bbe6 4798 firingMode(0);
mjr 53:9b2611964afc 4799 calState = 1;
mjr 50:40015764bbe6 4800 }
mjr 50:40015764bbe6 4801 break;
mjr 50:40015764bbe6 4802
mjr 50:40015764bbe6 4803 case 2:
mjr 50:40015764bbe6 4804 // Phase 2 - start of synthetic firing event. Report the fake
mjr 50:40015764bbe6 4805 // bounce for 25ms. VP polls the joystick about every 10ms, so
mjr 50:40015764bbe6 4806 // this should be enough time to guarantee that VP sees this
mjr 50:40015764bbe6 4807 // report at least once.
mjr 50:40015764bbe6 4808 if (uint32_t(r.t - f2.t) < 25000UL)
mjr 50:40015764bbe6 4809 {
mjr 50:40015764bbe6 4810 // report the bounce position
mjr 50:40015764bbe6 4811 z = f2.pos;
mjr 50:40015764bbe6 4812 }
mjr 50:40015764bbe6 4813 else
mjr 50:40015764bbe6 4814 {
mjr 50:40015764bbe6 4815 // it's been long enough - switch to phase 3, where we
mjr 50:40015764bbe6 4816 // report the park position until the real plunger comes
mjr 50:40015764bbe6 4817 // to rest
mjr 50:40015764bbe6 4818 firingMode(3);
mjr 50:40015764bbe6 4819 z = 0;
mjr 50:40015764bbe6 4820
mjr 50:40015764bbe6 4821 // set the start of the "stability window" to the rest position
mjr 50:40015764bbe6 4822 f3s.t = r.t;
mjr 50:40015764bbe6 4823 f3s.pos = 0;
mjr 50:40015764bbe6 4824
mjr 50:40015764bbe6 4825 // set the start of the "retraction window" to the actual position
mjr 50:40015764bbe6 4826 f3r = r;
mjr 50:40015764bbe6 4827 }
mjr 50:40015764bbe6 4828 break;
mjr 50:40015764bbe6 4829
mjr 50:40015764bbe6 4830 case 3:
mjr 50:40015764bbe6 4831 // Phase 3 - in synthetic firing event. Report the park position
mjr 50:40015764bbe6 4832 // until the plunger position stabilizes. Left to its own devices,
mjr 50:40015764bbe6 4833 // the plunger will usualy bounce off the barrel spring several
mjr 50:40015764bbe6 4834 // times before coming to rest, so we'll see oscillating motion
mjr 50:40015764bbe6 4835 // for a second or two. In the simplest case, we can aimply wait
mjr 50:40015764bbe6 4836 // for the plunger to stop moving for a short time. However, the
mjr 50:40015764bbe6 4837 // player might intervene by pulling the plunger back again, so
mjr 50:40015764bbe6 4838 // watch for that motion as well. If we're just bouncing freely,
mjr 50:40015764bbe6 4839 // we'll see the direction change frequently. If the player is
mjr 50:40015764bbe6 4840 // moving the plunger manually, the direction will be constant
mjr 50:40015764bbe6 4841 // for longer.
mjr 50:40015764bbe6 4842 if (v >= 0)
mjr 50:40015764bbe6 4843 {
mjr 50:40015764bbe6 4844 // We're moving back (or standing still). If this has been
mjr 50:40015764bbe6 4845 // going on for a while, the user must have taken control.
mjr 50:40015764bbe6 4846 if (uint32_t(r.t - f3r.t) > 65000UL)
mjr 50:40015764bbe6 4847 {
mjr 50:40015764bbe6 4848 // user has taken control - cancel firing mode
mjr 50:40015764bbe6 4849 firingMode(0);
mjr 50:40015764bbe6 4850 break;
mjr 50:40015764bbe6 4851 }
mjr 50:40015764bbe6 4852 }
mjr 50:40015764bbe6 4853 else
mjr 50:40015764bbe6 4854 {
mjr 50:40015764bbe6 4855 // forward motion - reset retraction window
mjr 50:40015764bbe6 4856 f3r.t = r.t;
mjr 50:40015764bbe6 4857 }
mjr 50:40015764bbe6 4858
mjr 53:9b2611964afc 4859 // Check if we're close to the last starting point. The joystick
mjr 53:9b2611964afc 4860 // positive axis range (0..4096) covers the retraction distance of
mjr 53:9b2611964afc 4861 // about 2.5", so 1" is about 1638 joystick units, hence 1/16" is
mjr 53:9b2611964afc 4862 // about 100 units.
mjr 53:9b2611964afc 4863 if (abs(r.pos - f3s.pos) < 100)
mjr 50:40015764bbe6 4864 {
mjr 53:9b2611964afc 4865 // It's at roughly the same position as the starting point.
mjr 53:9b2611964afc 4866 // Consider it stable if this has been true for 300ms.
mjr 50:40015764bbe6 4867 if (uint32_t(r.t - f3s.t) > 30000UL)
mjr 50:40015764bbe6 4868 {
mjr 50:40015764bbe6 4869 // we're done with the firing event
mjr 50:40015764bbe6 4870 firingMode(0);
mjr 50:40015764bbe6 4871 }
mjr 50:40015764bbe6 4872 else
mjr 50:40015764bbe6 4873 {
mjr 50:40015764bbe6 4874 // it's close to the last position but hasn't been
mjr 50:40015764bbe6 4875 // here long enough; stay in firing mode and continue
mjr 50:40015764bbe6 4876 // to report the park position
mjr 50:40015764bbe6 4877 z = 0;
mjr 50:40015764bbe6 4878 }
mjr 50:40015764bbe6 4879 }
mjr 50:40015764bbe6 4880 else
mjr 50:40015764bbe6 4881 {
mjr 50:40015764bbe6 4882 // It's not close enough to the last starting point, so use
mjr 50:40015764bbe6 4883 // this as a new starting point, and stay in firing mode.
mjr 50:40015764bbe6 4884 f3s = r;
mjr 50:40015764bbe6 4885 z = 0;
mjr 50:40015764bbe6 4886 }
mjr 50:40015764bbe6 4887 break;
mjr 50:40015764bbe6 4888 }
mjr 50:40015764bbe6 4889
mjr 50:40015764bbe6 4890 // save the velocity reading for next time
mjr 50:40015764bbe6 4891 vprv2 = vprv;
mjr 50:40015764bbe6 4892 vprv = v;
mjr 50:40015764bbe6 4893
mjr 50:40015764bbe6 4894 // add the new reading to the history
mjr 76:7f5912b6340e 4895 hist[histIdx] = r;
mjr 76:7f5912b6340e 4896 if (++histIdx > countof(hist))
mjr 76:7f5912b6340e 4897 histIdx = 0;
mjr 58:523fdcffbe6d 4898
mjr 69:cc5039284fac 4899 // apply the post-processing filter
mjr 69:cc5039284fac 4900 zf = applyPostFilter();
mjr 48:058ace2aed1d 4901 }
mjr 48:058ace2aed1d 4902 }
mjr 48:058ace2aed1d 4903
mjr 48:058ace2aed1d 4904 // Get the current value to report through the joystick interface
mjr 58:523fdcffbe6d 4905 int16_t getPosition()
mjr 58:523fdcffbe6d 4906 {
mjr 58:523fdcffbe6d 4907 // return the last filtered reading
mjr 58:523fdcffbe6d 4908 return zf;
mjr 55:4db125cd11a0 4909 }
mjr 58:523fdcffbe6d 4910
mjr 48:058ace2aed1d 4911 // get the timestamp of the current joystick report (microseconds)
mjr 50:40015764bbe6 4912 uint32_t getTimestamp() const { return nthHist(0).t; }
mjr 48:058ace2aed1d 4913
mjr 48:058ace2aed1d 4914 // Set calibration mode on or off
mjr 52:8298b2a73eb2 4915 void setCalMode(bool f)
mjr 48:058ace2aed1d 4916 {
mjr 52:8298b2a73eb2 4917 // check to see if we're entering calibration mode
mjr 52:8298b2a73eb2 4918 if (f && !plungerCalMode)
mjr 52:8298b2a73eb2 4919 {
mjr 52:8298b2a73eb2 4920 // reset the calibration in the configuration
mjr 48:058ace2aed1d 4921 cfg.plunger.cal.begin();
mjr 52:8298b2a73eb2 4922
mjr 52:8298b2a73eb2 4923 // start in state 0 (waiting to settle)
mjr 52:8298b2a73eb2 4924 calState = 0;
mjr 52:8298b2a73eb2 4925 calZeroPosSum = 0;
mjr 52:8298b2a73eb2 4926 calZeroPosN = 0;
mjr 52:8298b2a73eb2 4927 calRlsTimeSum = 0;
mjr 52:8298b2a73eb2 4928 calRlsTimeN = 0;
mjr 52:8298b2a73eb2 4929
mjr 52:8298b2a73eb2 4930 // set the initial zero point to the current position
mjr 52:8298b2a73eb2 4931 PlungerReading r;
mjr 52:8298b2a73eb2 4932 if (plungerSensor->read(r))
mjr 52:8298b2a73eb2 4933 {
mjr 52:8298b2a73eb2 4934 // got a reading - use it as the initial zero point
mjr 69:cc5039284fac 4935 applyPreFilter(r);
mjr 52:8298b2a73eb2 4936 cfg.plunger.cal.zero = r.pos;
mjr 76:7f5912b6340e 4937 onUpdateCal();
mjr 52:8298b2a73eb2 4938
mjr 52:8298b2a73eb2 4939 // use it as the starting point for the settling watch
mjr 53:9b2611964afc 4940 calZeroStart = r;
mjr 52:8298b2a73eb2 4941 }
mjr 52:8298b2a73eb2 4942 else
mjr 52:8298b2a73eb2 4943 {
mjr 52:8298b2a73eb2 4944 // no reading available - use the default 1/6 position
mjr 52:8298b2a73eb2 4945 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 4946 onUpdateCal();
mjr 52:8298b2a73eb2 4947
mjr 52:8298b2a73eb2 4948 // we don't have a starting point for the setting watch
mjr 53:9b2611964afc 4949 calZeroStart.pos = -65535;
mjr 53:9b2611964afc 4950 calZeroStart.t = 0;
mjr 53:9b2611964afc 4951 }
mjr 53:9b2611964afc 4952 }
mjr 53:9b2611964afc 4953 else if (!f && plungerCalMode)
mjr 53:9b2611964afc 4954 {
mjr 53:9b2611964afc 4955 // Leaving calibration mode. Make sure the max is past the
mjr 53:9b2611964afc 4956 // zero point - if it's not, we'd have a zero or negative
mjr 53:9b2611964afc 4957 // denominator for the scaling calculation, which would be
mjr 53:9b2611964afc 4958 // physically meaningless.
mjr 53:9b2611964afc 4959 if (cfg.plunger.cal.max <= cfg.plunger.cal.zero)
mjr 53:9b2611964afc 4960 {
mjr 53:9b2611964afc 4961 // bad settings - reset to defaults
mjr 53:9b2611964afc 4962 cfg.plunger.cal.max = 0xffff;
mjr 53:9b2611964afc 4963 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 4964 onUpdateCal();
mjr 52:8298b2a73eb2 4965 }
mjr 52:8298b2a73eb2 4966 }
mjr 52:8298b2a73eb2 4967
mjr 48:058ace2aed1d 4968 // remember the new mode
mjr 52:8298b2a73eb2 4969 plungerCalMode = f;
mjr 48:058ace2aed1d 4970 }
mjr 48:058ace2aed1d 4971
mjr 76:7f5912b6340e 4972 // Cached inverse of the calibration range. This is for calculating
mjr 76:7f5912b6340e 4973 // the calibrated plunger position given a raw sensor reading. The
mjr 76:7f5912b6340e 4974 // cached inverse is calculated as
mjr 76:7f5912b6340e 4975 //
mjr 76:7f5912b6340e 4976 // 64K * JOYMAX / (cfg.plunger.cal.max - cfg.plunger.cal.zero)
mjr 76:7f5912b6340e 4977 //
mjr 76:7f5912b6340e 4978 // To convert a raw sensor reading to a calibrated position, calculate
mjr 76:7f5912b6340e 4979 //
mjr 76:7f5912b6340e 4980 // ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16
mjr 76:7f5912b6340e 4981 //
mjr 76:7f5912b6340e 4982 // That yields the calibration result without performing a division.
mjr 76:7f5912b6340e 4983 int invCalRange;
mjr 76:7f5912b6340e 4984
mjr 76:7f5912b6340e 4985 // apply the calibration range to a reading
mjr 76:7f5912b6340e 4986 inline int applyCal(int reading)
mjr 76:7f5912b6340e 4987 {
mjr 76:7f5912b6340e 4988 return ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16;
mjr 76:7f5912b6340e 4989 }
mjr 76:7f5912b6340e 4990
mjr 76:7f5912b6340e 4991 void onUpdateCal()
mjr 76:7f5912b6340e 4992 {
mjr 76:7f5912b6340e 4993 invCalRange = (JOYMAX << 16)/(cfg.plunger.cal.max - cfg.plunger.cal.zero);
mjr 76:7f5912b6340e 4994 }
mjr 76:7f5912b6340e 4995
mjr 48:058ace2aed1d 4996 // is a firing event in progress?
mjr 53:9b2611964afc 4997 bool isFiring() { return firing == 3; }
mjr 76:7f5912b6340e 4998
mjr 48:058ace2aed1d 4999 private:
mjr 52:8298b2a73eb2 5000
mjr 74:822a92bc11d2 5001 // Plunger data filtering mode: optionally apply filtering to the raw
mjr 74:822a92bc11d2 5002 // plunger sensor readings to try to reduce noise in the signal. This
mjr 74:822a92bc11d2 5003 // is designed for the TSL1410/12 optical sensors, where essentially all
mjr 74:822a92bc11d2 5004 // of the noise in the signal comes from lack of sharpness in the shadow
mjr 74:822a92bc11d2 5005 // edge. When the shadow is blurry, the edge detector has to pick a pixel,
mjr 74:822a92bc11d2 5006 // even though the edge is actually a gradient spanning several pixels.
mjr 74:822a92bc11d2 5007 // The edge detection algorithm decides on the exact pixel, but whatever
mjr 74:822a92bc11d2 5008 // the algorithm, the choice is going to be somewhat arbitrary given that
mjr 74:822a92bc11d2 5009 // there's really no one pixel that's "the edge" when the edge actually
mjr 74:822a92bc11d2 5010 // covers multiple pixels. This can make the choice of pixel sensitive to
mjr 74:822a92bc11d2 5011 // small changes in exposure and pixel respose from frame to frame, which
mjr 74:822a92bc11d2 5012 // means that the reported edge position can move by a pixel or two from
mjr 74:822a92bc11d2 5013 // one frame to the next even when the physical plunger is perfectly still.
mjr 74:822a92bc11d2 5014 // That's the noise we're talking about.
mjr 74:822a92bc11d2 5015 //
mjr 74:822a92bc11d2 5016 // We previously applied a mild hysteresis filter to the signal to try to
mjr 74:822a92bc11d2 5017 // eliminate this noise. The filter tracked the average over the last
mjr 74:822a92bc11d2 5018 // several samples, and rejected readings that wandered within a few
mjr 74:822a92bc11d2 5019 // pixels of the average. If a certain number of readings moved away from
mjr 74:822a92bc11d2 5020 // the average in the same direction, even by small amounts, the filter
mjr 74:822a92bc11d2 5021 // accepted the changes, on the assumption that they represented actual
mjr 74:822a92bc11d2 5022 // slow movement of the plunger. This filter was applied after the firing
mjr 74:822a92bc11d2 5023 // detection.
mjr 74:822a92bc11d2 5024 //
mjr 74:822a92bc11d2 5025 // I also tried a simpler filter that rejected changes that were too fast
mjr 74:822a92bc11d2 5026 // to be physically possible, as well as changes that were very close to
mjr 74:822a92bc11d2 5027 // the last reported position (i.e., simple hysteresis). The "too fast"
mjr 74:822a92bc11d2 5028 // filter was there to reject spurious readings where the edge detector
mjr 74:822a92bc11d2 5029 // mistook a bad pixel value as an edge.
mjr 74:822a92bc11d2 5030 //
mjr 74:822a92bc11d2 5031 // The new "mode 2" edge detector (see ccdSensor.h) seems to do a better
mjr 74:822a92bc11d2 5032 // job of rejecting pixel-level noise by itself than the older "mode 0"
mjr 74:822a92bc11d2 5033 // algorithm did, so I removed the filtering entirely. Any filtering has
mjr 74:822a92bc11d2 5034 // some downsides, so it's better to reduce noise in the underlying signal
mjr 74:822a92bc11d2 5035 // as much as possible first. It seems possible to get a very stable signal
mjr 74:822a92bc11d2 5036 // now with a combination of the mode 2 edge detector and optimizing the
mjr 74:822a92bc11d2 5037 // physical sensor arrangement, especially optimizing the light source to
mjr 74:822a92bc11d2 5038 // cast as sharp as shadow as possible and adjusting the brightness to
mjr 74:822a92bc11d2 5039 // maximize bright/dark contrast in the image.
mjr 74:822a92bc11d2 5040 //
mjr 74:822a92bc11d2 5041 // 0 = No filtering (current default)
mjr 74:822a92bc11d2 5042 // 1 = Filter the data after firing detection using moving average
mjr 74:822a92bc11d2 5043 // hysteresis filter (old version, used in most 2016 releases)
mjr 74:822a92bc11d2 5044 // 2 = Filter the data before firing detection using simple hysteresis
mjr 74:822a92bc11d2 5045 // plus spurious "too fast" motion rejection
mjr 74:822a92bc11d2 5046 //
mjr 73:4e8ce0b18915 5047 #define PLUNGER_FILTERING_MODE 0
mjr 73:4e8ce0b18915 5048
mjr 73:4e8ce0b18915 5049 #if PLUNGER_FILTERING_MODE == 0
mjr 69:cc5039284fac 5050 // Disable all filtering
mjr 74:822a92bc11d2 5051 inline void applyPreFilter(PlungerReading &r) { }
mjr 74:822a92bc11d2 5052 inline int applyPostFilter() { return z; }
mjr 73:4e8ce0b18915 5053 #elif PLUNGER_FILTERING_MODE == 1
mjr 73:4e8ce0b18915 5054 // Apply pre-processing filter. This filter is applied to the raw
mjr 73:4e8ce0b18915 5055 // value coming off the sensor, before calibration or fire-event
mjr 73:4e8ce0b18915 5056 // processing.
mjr 73:4e8ce0b18915 5057 void applyPreFilter(PlungerReading &r)
mjr 73:4e8ce0b18915 5058 {
mjr 73:4e8ce0b18915 5059 }
mjr 73:4e8ce0b18915 5060
mjr 73:4e8ce0b18915 5061 // Figure the next post-processing filtered value. This applies a
mjr 73:4e8ce0b18915 5062 // hysteresis filter to the last raw z value and returns the
mjr 73:4e8ce0b18915 5063 // filtered result.
mjr 73:4e8ce0b18915 5064 int applyPostFilter()
mjr 73:4e8ce0b18915 5065 {
mjr 73:4e8ce0b18915 5066 if (firing <= 1)
mjr 73:4e8ce0b18915 5067 {
mjr 73:4e8ce0b18915 5068 // Filter limit - 5 samples. Once we've been moving
mjr 73:4e8ce0b18915 5069 // in the same direction for this many samples, we'll
mjr 73:4e8ce0b18915 5070 // clear the history and start over.
mjr 73:4e8ce0b18915 5071 const int filterMask = 0x1f;
mjr 73:4e8ce0b18915 5072
mjr 73:4e8ce0b18915 5073 // figure the last average
mjr 73:4e8ce0b18915 5074 int lastAvg = int(filterSum / filterN);
mjr 73:4e8ce0b18915 5075
mjr 73:4e8ce0b18915 5076 // figure the direction of this sample relative to the average,
mjr 73:4e8ce0b18915 5077 // and shift it in to our bit mask of recent direction data
mjr 73:4e8ce0b18915 5078 if (z != lastAvg)
mjr 73:4e8ce0b18915 5079 {
mjr 73:4e8ce0b18915 5080 // shift the new direction bit into the vector
mjr 73:4e8ce0b18915 5081 filterDir <<= 1;
mjr 73:4e8ce0b18915 5082 if (z > lastAvg) filterDir |= 1;
mjr 73:4e8ce0b18915 5083 }
mjr 73:4e8ce0b18915 5084
mjr 73:4e8ce0b18915 5085 // keep only the last N readings, up to the filter limit
mjr 73:4e8ce0b18915 5086 filterDir &= filterMask;
mjr 73:4e8ce0b18915 5087
mjr 73:4e8ce0b18915 5088 // if we've been moving consistently in one direction (all 1's
mjr 73:4e8ce0b18915 5089 // or all 0's in the direction history vector), reset the average
mjr 73:4e8ce0b18915 5090 if (filterDir == 0x00 || filterDir == filterMask)
mjr 73:4e8ce0b18915 5091 {
mjr 73:4e8ce0b18915 5092 // motion away from the average - reset the average
mjr 73:4e8ce0b18915 5093 filterDir = 0x5555;
mjr 73:4e8ce0b18915 5094 filterN = 1;
mjr 73:4e8ce0b18915 5095 filterSum = (lastAvg + z)/2;
mjr 73:4e8ce0b18915 5096 return int16_t(filterSum);
mjr 73:4e8ce0b18915 5097 }
mjr 73:4e8ce0b18915 5098 else
mjr 73:4e8ce0b18915 5099 {
mjr 73:4e8ce0b18915 5100 // we're directionless - return the new average, with the
mjr 73:4e8ce0b18915 5101 // new sample included
mjr 73:4e8ce0b18915 5102 filterSum += z;
mjr 73:4e8ce0b18915 5103 ++filterN;
mjr 73:4e8ce0b18915 5104 return int16_t(filterSum / filterN);
mjr 73:4e8ce0b18915 5105 }
mjr 73:4e8ce0b18915 5106 }
mjr 73:4e8ce0b18915 5107 else
mjr 73:4e8ce0b18915 5108 {
mjr 73:4e8ce0b18915 5109 // firing mode - skip the filter
mjr 73:4e8ce0b18915 5110 filterN = 1;
mjr 73:4e8ce0b18915 5111 filterSum = z;
mjr 73:4e8ce0b18915 5112 filterDir = 0x5555;
mjr 73:4e8ce0b18915 5113 return z;
mjr 73:4e8ce0b18915 5114 }
mjr 73:4e8ce0b18915 5115 }
mjr 73:4e8ce0b18915 5116 #elif PLUNGER_FILTERING_MODE == 2
mjr 69:cc5039284fac 5117 // Apply pre-processing filter. This filter is applied to the raw
mjr 69:cc5039284fac 5118 // value coming off the sensor, before calibration or fire-event
mjr 69:cc5039284fac 5119 // processing.
mjr 69:cc5039284fac 5120 void applyPreFilter(PlungerReading &r)
mjr 69:cc5039284fac 5121 {
mjr 69:cc5039284fac 5122 // get the previous raw reading
mjr 69:cc5039284fac 5123 PlungerReading prv = pre.raw;
mjr 69:cc5039284fac 5124
mjr 69:cc5039284fac 5125 // the new reading is the previous raw reading next time, no
mjr 69:cc5039284fac 5126 // matter how we end up filtering it
mjr 69:cc5039284fac 5127 pre.raw = r;
mjr 69:cc5039284fac 5128
mjr 69:cc5039284fac 5129 // If it's too big an excursion from the previous raw reading,
mjr 69:cc5039284fac 5130 // ignore it and repeat the previous reported reading. This
mjr 69:cc5039284fac 5131 // filters out anomalous spikes where we suddenly jump to a
mjr 69:cc5039284fac 5132 // level that's too far away to be possible. Real plungers
mjr 69:cc5039284fac 5133 // take about 60ms to travel the full distance when released,
mjr 69:cc5039284fac 5134 // so assuming constant acceleration, the maximum realistic
mjr 69:cc5039284fac 5135 // speed is about 2.200 distance units (on our 0..0xffff scale)
mjr 69:cc5039284fac 5136 // per microsecond.
mjr 69:cc5039284fac 5137 //
mjr 69:cc5039284fac 5138 // On the other hand, if the new reading is too *close* to the
mjr 69:cc5039284fac 5139 // previous reading, use the previous reported reading. This
mjr 69:cc5039284fac 5140 // filters out jitter around a stationary position.
mjr 69:cc5039284fac 5141 const float maxDist = 2.184f*uint32_t(r.t - prv.t);
mjr 69:cc5039284fac 5142 const int minDist = 256;
mjr 69:cc5039284fac 5143 const int delta = abs(r.pos - prv.pos);
mjr 69:cc5039284fac 5144 if (maxDist > minDist && delta > maxDist)
mjr 69:cc5039284fac 5145 {
mjr 69:cc5039284fac 5146 // too big an excursion - discard this reading by reporting
mjr 69:cc5039284fac 5147 // the last reported reading instead
mjr 69:cc5039284fac 5148 r.pos = pre.reported;
mjr 69:cc5039284fac 5149 }
mjr 69:cc5039284fac 5150 else if (delta < minDist)
mjr 69:cc5039284fac 5151 {
mjr 69:cc5039284fac 5152 // too close to the prior reading - apply hysteresis
mjr 69:cc5039284fac 5153 r.pos = pre.reported;
mjr 69:cc5039284fac 5154 }
mjr 69:cc5039284fac 5155 else
mjr 69:cc5039284fac 5156 {
mjr 69:cc5039284fac 5157 // the reading is in range - keep it, and remember it as
mjr 69:cc5039284fac 5158 // the last reported reading
mjr 69:cc5039284fac 5159 pre.reported = r.pos;
mjr 69:cc5039284fac 5160 }
mjr 69:cc5039284fac 5161 }
mjr 69:cc5039284fac 5162
mjr 69:cc5039284fac 5163 // pre-filter data
mjr 69:cc5039284fac 5164 struct PreFilterData {
mjr 69:cc5039284fac 5165 PreFilterData()
mjr 69:cc5039284fac 5166 : reported(0)
mjr 69:cc5039284fac 5167 {
mjr 69:cc5039284fac 5168 raw.t = 0;
mjr 69:cc5039284fac 5169 raw.pos = 0;
mjr 69:cc5039284fac 5170 }
mjr 69:cc5039284fac 5171 PlungerReading raw; // previous raw sensor reading
mjr 69:cc5039284fac 5172 int reported; // previous reported reading
mjr 69:cc5039284fac 5173 } pre;
mjr 69:cc5039284fac 5174
mjr 69:cc5039284fac 5175
mjr 69:cc5039284fac 5176 // Apply the post-processing filter. This filter is applied after
mjr 69:cc5039284fac 5177 // the fire-event processing. In the past, this used hysteresis to
mjr 69:cc5039284fac 5178 // try to smooth out jittering readings for a stationary plunger.
mjr 69:cc5039284fac 5179 // We've switched to a different approach that massages the readings
mjr 69:cc5039284fac 5180 // coming off the sensor before
mjr 69:cc5039284fac 5181 int applyPostFilter()
mjr 69:cc5039284fac 5182 {
mjr 69:cc5039284fac 5183 return z;
mjr 69:cc5039284fac 5184 }
mjr 69:cc5039284fac 5185 #endif
mjr 58:523fdcffbe6d 5186
mjr 58:523fdcffbe6d 5187 void initFilter()
mjr 58:523fdcffbe6d 5188 {
mjr 58:523fdcffbe6d 5189 filterSum = 0;
mjr 58:523fdcffbe6d 5190 filterN = 1;
mjr 58:523fdcffbe6d 5191 filterDir = 0x5555;
mjr 58:523fdcffbe6d 5192 }
mjr 58:523fdcffbe6d 5193 int64_t filterSum;
mjr 58:523fdcffbe6d 5194 int64_t filterN;
mjr 58:523fdcffbe6d 5195 uint16_t filterDir;
mjr 58:523fdcffbe6d 5196
mjr 58:523fdcffbe6d 5197
mjr 52:8298b2a73eb2 5198 // Calibration state. During calibration mode, we watch for release
mjr 52:8298b2a73eb2 5199 // events, to measure the time it takes to complete the release
mjr 52:8298b2a73eb2 5200 // motion; and we watch for the plunger to come to reset after a
mjr 52:8298b2a73eb2 5201 // release, to gather statistics on the rest position.
mjr 52:8298b2a73eb2 5202 // 0 = waiting to settle
mjr 52:8298b2a73eb2 5203 // 1 = at rest
mjr 52:8298b2a73eb2 5204 // 2 = retracting
mjr 52:8298b2a73eb2 5205 // 3 = possibly releasing
mjr 52:8298b2a73eb2 5206 uint8_t calState;
mjr 52:8298b2a73eb2 5207
mjr 52:8298b2a73eb2 5208 // Calibration zero point statistics.
mjr 52:8298b2a73eb2 5209 // During calibration mode, we collect data on the rest position (the
mjr 52:8298b2a73eb2 5210 // zero point) by watching for the plunger to come to rest after each
mjr 52:8298b2a73eb2 5211 // release. We average these rest positions to get the calibrated
mjr 52:8298b2a73eb2 5212 // zero point. We use the average because the real physical plunger
mjr 52:8298b2a73eb2 5213 // itself doesn't come to rest at exactly the same spot every time,
mjr 52:8298b2a73eb2 5214 // largely due to friction in the mechanism. To calculate the average,
mjr 52:8298b2a73eb2 5215 // we keep a sum of the readings and a count of samples.
mjr 53:9b2611964afc 5216 PlungerReading calZeroStart;
mjr 52:8298b2a73eb2 5217 long calZeroPosSum;
mjr 52:8298b2a73eb2 5218 int calZeroPosN;
mjr 52:8298b2a73eb2 5219
mjr 52:8298b2a73eb2 5220 // Calibration release time statistics.
mjr 52:8298b2a73eb2 5221 // During calibration, we collect an average for the release time.
mjr 52:8298b2a73eb2 5222 long calRlsTimeSum;
mjr 52:8298b2a73eb2 5223 int calRlsTimeN;
mjr 52:8298b2a73eb2 5224
mjr 48:058ace2aed1d 5225 // set a firing mode
mjr 48:058ace2aed1d 5226 inline void firingMode(int m)
mjr 48:058ace2aed1d 5227 {
mjr 48:058ace2aed1d 5228 firing = m;
mjr 48:058ace2aed1d 5229 }
mjr 48:058ace2aed1d 5230
mjr 48:058ace2aed1d 5231 // Find the most recent local maximum in the history data, up to
mjr 48:058ace2aed1d 5232 // the given time limit.
mjr 48:058ace2aed1d 5233 int histLocalMax(uint32_t tcur, uint32_t dt)
mjr 48:058ace2aed1d 5234 {
mjr 48:058ace2aed1d 5235 // start with the prior entry
mjr 48:058ace2aed1d 5236 int idx = (histIdx == 0 ? countof(hist) : histIdx) - 1;
mjr 48:058ace2aed1d 5237 int hi = hist[idx].pos;
mjr 48:058ace2aed1d 5238
mjr 48:058ace2aed1d 5239 // scan backwards for a local maximum
mjr 48:058ace2aed1d 5240 for (int n = countof(hist) - 1 ; n > 0 ; idx = (idx == 0 ? countof(hist) : idx) - 1)
mjr 48:058ace2aed1d 5241 {
mjr 48:058ace2aed1d 5242 // if this isn't within the time window, stop
mjr 48:058ace2aed1d 5243 if (uint32_t(tcur - hist[idx].t) > dt)
mjr 48:058ace2aed1d 5244 break;
mjr 48:058ace2aed1d 5245
mjr 48:058ace2aed1d 5246 // if this isn't above the current hith, stop
mjr 48:058ace2aed1d 5247 if (hist[idx].pos < hi)
mjr 48:058ace2aed1d 5248 break;
mjr 48:058ace2aed1d 5249
mjr 48:058ace2aed1d 5250 // this is the new high
mjr 48:058ace2aed1d 5251 hi = hist[idx].pos;
mjr 48:058ace2aed1d 5252 }
mjr 48:058ace2aed1d 5253
mjr 48:058ace2aed1d 5254 // return the local maximum
mjr 48:058ace2aed1d 5255 return hi;
mjr 48:058ace2aed1d 5256 }
mjr 48:058ace2aed1d 5257
mjr 50:40015764bbe6 5258 // velocity at previous reading, and the one before that
mjr 76:7f5912b6340e 5259 int vprv, vprv2;
mjr 48:058ace2aed1d 5260
mjr 48:058ace2aed1d 5261 // Circular buffer of recent readings. We keep a short history
mjr 48:058ace2aed1d 5262 // of readings to analyze during firing events. We can only identify
mjr 48:058ace2aed1d 5263 // a firing event once it's somewhat under way, so we need a little
mjr 48:058ace2aed1d 5264 // retrospective information to accurately determine after the fact
mjr 48:058ace2aed1d 5265 // exactly when it started. We throttle our readings to no more
mjr 74:822a92bc11d2 5266 // than one every 1ms, so we have at least N*1ms of history in this
mjr 48:058ace2aed1d 5267 // array.
mjr 74:822a92bc11d2 5268 PlungerReading hist[32];
mjr 48:058ace2aed1d 5269 int histIdx;
mjr 49:37bd97eb7688 5270
mjr 50:40015764bbe6 5271 // get the nth history item (0=last, 1=2nd to last, etc)
mjr 74:822a92bc11d2 5272 inline const PlungerReading &nthHist(int n) const
mjr 50:40015764bbe6 5273 {
mjr 50:40015764bbe6 5274 // histIdx-1 is the last written; go from there
mjr 50:40015764bbe6 5275 n = histIdx - 1 - n;
mjr 50:40015764bbe6 5276
mjr 50:40015764bbe6 5277 // adjust for wrapping
mjr 50:40015764bbe6 5278 if (n < 0)
mjr 50:40015764bbe6 5279 n += countof(hist);
mjr 50:40015764bbe6 5280
mjr 50:40015764bbe6 5281 // return the item
mjr 50:40015764bbe6 5282 return hist[n];
mjr 50:40015764bbe6 5283 }
mjr 48:058ace2aed1d 5284
mjr 48:058ace2aed1d 5285 // Firing event state.
mjr 48:058ace2aed1d 5286 //
mjr 48:058ace2aed1d 5287 // 0 - Default state. We report the real instantaneous plunger
mjr 48:058ace2aed1d 5288 // position to the joystick interface.
mjr 48:058ace2aed1d 5289 //
mjr 53:9b2611964afc 5290 // 1 - Moving forward
mjr 48:058ace2aed1d 5291 //
mjr 53:9b2611964afc 5292 // 2 - Accelerating
mjr 48:058ace2aed1d 5293 //
mjr 53:9b2611964afc 5294 // 3 - Firing. We report the rest position for a minimum interval,
mjr 53:9b2611964afc 5295 // or until the real plunger comes to rest somewhere.
mjr 48:058ace2aed1d 5296 //
mjr 48:058ace2aed1d 5297 int firing;
mjr 48:058ace2aed1d 5298
mjr 51:57eb311faafa 5299 // Position/timestamp at start of firing phase 1. When we see a
mjr 51:57eb311faafa 5300 // sustained forward acceleration, we freeze joystick reports at
mjr 51:57eb311faafa 5301 // the recent local maximum, on the assumption that this was the
mjr 51:57eb311faafa 5302 // start of the release. If this is zero, it means that we're
mjr 51:57eb311faafa 5303 // monitoring accelerating motion but haven't seen it for long
mjr 51:57eb311faafa 5304 // enough yet to be confident that a release is in progress.
mjr 48:058ace2aed1d 5305 PlungerReading f1;
mjr 48:058ace2aed1d 5306
mjr 48:058ace2aed1d 5307 // Position/timestamp at start of firing phase 2. The position is
mjr 48:058ace2aed1d 5308 // the fake "bounce" position we report during this phase, and the
mjr 48:058ace2aed1d 5309 // timestamp tells us when the phase began so that we can end it
mjr 48:058ace2aed1d 5310 // after enough time elapses.
mjr 48:058ace2aed1d 5311 PlungerReading f2;
mjr 48:058ace2aed1d 5312
mjr 48:058ace2aed1d 5313 // Position/timestamp of start of stability window during phase 3.
mjr 48:058ace2aed1d 5314 // We use this to determine when the plunger comes to rest. We set
mjr 51:57eb311faafa 5315 // this at the beginning of phase 3, and then reset it when the
mjr 48:058ace2aed1d 5316 // plunger moves too far from the last position.
mjr 48:058ace2aed1d 5317 PlungerReading f3s;
mjr 48:058ace2aed1d 5318
mjr 48:058ace2aed1d 5319 // Position/timestamp of start of retraction window during phase 3.
mjr 48:058ace2aed1d 5320 // We use this to determine if the user is drawing the plunger back.
mjr 48:058ace2aed1d 5321 // If we see retraction motion for more than about 65ms, we assume
mjr 48:058ace2aed1d 5322 // that the user has taken over, because we should see forward
mjr 48:058ace2aed1d 5323 // motion within this timeframe if the plunger is just bouncing
mjr 48:058ace2aed1d 5324 // freely.
mjr 48:058ace2aed1d 5325 PlungerReading f3r;
mjr 48:058ace2aed1d 5326
mjr 58:523fdcffbe6d 5327 // next raw (unfiltered) Z value to report to the joystick interface
mjr 58:523fdcffbe6d 5328 // (in joystick distance units)
mjr 48:058ace2aed1d 5329 int z;
mjr 48:058ace2aed1d 5330
mjr 58:523fdcffbe6d 5331 // next filtered Z value to report to the joystick interface
mjr 58:523fdcffbe6d 5332 int zf;
mjr 48:058ace2aed1d 5333 };
mjr 48:058ace2aed1d 5334
mjr 48:058ace2aed1d 5335 // plunger reader singleton
mjr 48:058ace2aed1d 5336 PlungerReader plungerReader;
mjr 48:058ace2aed1d 5337
mjr 48:058ace2aed1d 5338 // ---------------------------------------------------------------------------
mjr 48:058ace2aed1d 5339 //
mjr 48:058ace2aed1d 5340 // Handle the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5341 //
mjr 48:058ace2aed1d 5342 // The ZB Launch Ball feature, if enabled, lets the mechanical plunger
mjr 48:058ace2aed1d 5343 // serve as a substitute for a physical Launch Ball button. When a table
mjr 48:058ace2aed1d 5344 // is loaded in VP, and the table has the ZB Launch Ball LedWiz port
mjr 48:058ace2aed1d 5345 // turned on, we'll disable mechanical plunger reports through the
mjr 48:058ace2aed1d 5346 // joystick interface and instead use the plunger only to simulate the
mjr 48:058ace2aed1d 5347 // Launch Ball button. When the mode is active, pulling back and
mjr 48:058ace2aed1d 5348 // releasing the plunger causes a brief simulated press of the Launch
mjr 48:058ace2aed1d 5349 // button, and pushing the plunger forward of the rest position presses
mjr 48:058ace2aed1d 5350 // the Launch button as long as the plunger is pressed forward.
mjr 48:058ace2aed1d 5351 //
mjr 48:058ace2aed1d 5352 // This feature has two configuration components:
mjr 48:058ace2aed1d 5353 //
mjr 48:058ace2aed1d 5354 // - An LedWiz port number. This port is a "virtual" port that doesn't
mjr 48:058ace2aed1d 5355 // have to be attached to any actual output. DOF uses it to signal
mjr 48:058ace2aed1d 5356 // that the current table uses a Launch button instead of a plunger.
mjr 48:058ace2aed1d 5357 // DOF simply turns the port on when such a table is loaded and turns
mjr 48:058ace2aed1d 5358 // it off at all other times. We use it to enable and disable the
mjr 48:058ace2aed1d 5359 // plunger/launch button connection.
mjr 48:058ace2aed1d 5360 //
mjr 48:058ace2aed1d 5361 // - A joystick button ID. We simulate pressing this button when the
mjr 48:058ace2aed1d 5362 // launch feature is activated via the LedWiz port and the plunger is
mjr 48:058ace2aed1d 5363 // either pulled back and releasd, or pushed forward past the rest
mjr 48:058ace2aed1d 5364 // position.
mjr 48:058ace2aed1d 5365 //
mjr 48:058ace2aed1d 5366 class ZBLaunchBall
mjr 48:058ace2aed1d 5367 {
mjr 48:058ace2aed1d 5368 public:
mjr 48:058ace2aed1d 5369 ZBLaunchBall()
mjr 48:058ace2aed1d 5370 {
mjr 48:058ace2aed1d 5371 // start in the default state
mjr 48:058ace2aed1d 5372 lbState = 0;
mjr 53:9b2611964afc 5373 btnState = false;
mjr 48:058ace2aed1d 5374 }
mjr 48:058ace2aed1d 5375
mjr 48:058ace2aed1d 5376 // Update state. This checks the current plunger position and
mjr 48:058ace2aed1d 5377 // the timers to see if the plunger is in a position that simulates
mjr 48:058ace2aed1d 5378 // a Launch Ball button press via the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5379 // Updates the simulated button vector according to the current
mjr 48:058ace2aed1d 5380 // launch ball state. The main loop calls this before each
mjr 48:058ace2aed1d 5381 // joystick update to figure the new simulated button state.
mjr 53:9b2611964afc 5382 void update()
mjr 48:058ace2aed1d 5383 {
mjr 53:9b2611964afc 5384 // If the ZB Launch Ball led wiz output is ON, check for a
mjr 53:9b2611964afc 5385 // plunger firing event
mjr 53:9b2611964afc 5386 if (zbLaunchOn)
mjr 48:058ace2aed1d 5387 {
mjr 53:9b2611964afc 5388 // note the new position
mjr 48:058ace2aed1d 5389 int znew = plungerReader.getPosition();
mjr 53:9b2611964afc 5390
mjr 53:9b2611964afc 5391 // figure the push threshold from the configuration data
mjr 51:57eb311faafa 5392 const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
mjr 53:9b2611964afc 5393
mjr 53:9b2611964afc 5394 // check the state
mjr 48:058ace2aed1d 5395 switch (lbState)
mjr 48:058ace2aed1d 5396 {
mjr 48:058ace2aed1d 5397 case 0:
mjr 53:9b2611964afc 5398 // Default state. If a launch event has been detected on
mjr 53:9b2611964afc 5399 // the plunger, activate a timed pulse and switch to state 1.
mjr 53:9b2611964afc 5400 // If the plunger is pushed forward of the threshold, push
mjr 53:9b2611964afc 5401 // the button.
mjr 53:9b2611964afc 5402 if (plungerReader.isFiring())
mjr 53:9b2611964afc 5403 {
mjr 53:9b2611964afc 5404 // firing event - start a timed Launch button pulse
mjr 53:9b2611964afc 5405 lbTimer.reset();
mjr 53:9b2611964afc 5406 lbTimer.start();
mjr 53:9b2611964afc 5407 setButton(true);
mjr 53:9b2611964afc 5408
mjr 53:9b2611964afc 5409 // switch to state 1
mjr 53:9b2611964afc 5410 lbState = 1;
mjr 53:9b2611964afc 5411 }
mjr 48:058ace2aed1d 5412 else if (znew <= pushThreshold)
mjr 53:9b2611964afc 5413 {
mjr 53:9b2611964afc 5414 // pushed forward without a firing event - hold the
mjr 53:9b2611964afc 5415 // button as long as we're pushed forward
mjr 53:9b2611964afc 5416 setButton(true);
mjr 53:9b2611964afc 5417 }
mjr 53:9b2611964afc 5418 else
mjr 53:9b2611964afc 5419 {
mjr 53:9b2611964afc 5420 // not pushed forward - turn off the Launch button
mjr 53:9b2611964afc 5421 setButton(false);
mjr 53:9b2611964afc 5422 }
mjr 48:058ace2aed1d 5423 break;
mjr 48:058ace2aed1d 5424
mjr 48:058ace2aed1d 5425 case 1:
mjr 53:9b2611964afc 5426 // State 1: Timed Launch button pulse in progress after a
mjr 53:9b2611964afc 5427 // firing event. Wait for the timer to expire.
mjr 53:9b2611964afc 5428 if (lbTimer.read_us() > 200000UL)
mjr 53:9b2611964afc 5429 {
mjr 53:9b2611964afc 5430 // timer expired - turn off the button
mjr 53:9b2611964afc 5431 setButton(false);
mjr 53:9b2611964afc 5432
mjr 53:9b2611964afc 5433 // switch to state 2
mjr 53:9b2611964afc 5434 lbState = 2;
mjr 53:9b2611964afc 5435 }
mjr 48:058ace2aed1d 5436 break;
mjr 48:058ace2aed1d 5437
mjr 48:058ace2aed1d 5438 case 2:
mjr 53:9b2611964afc 5439 // State 2: Timed Launch button pulse done. Wait for the
mjr 53:9b2611964afc 5440 // plunger launch event to end.
mjr 53:9b2611964afc 5441 if (!plungerReader.isFiring())
mjr 53:9b2611964afc 5442 {
mjr 53:9b2611964afc 5443 // firing event done - return to default state
mjr 53:9b2611964afc 5444 lbState = 0;
mjr 53:9b2611964afc 5445 }
mjr 48:058ace2aed1d 5446 break;
mjr 48:058ace2aed1d 5447 }
mjr 53:9b2611964afc 5448 }
mjr 53:9b2611964afc 5449 else
mjr 53:9b2611964afc 5450 {
mjr 53:9b2611964afc 5451 // ZB Launch Ball disabled - turn off the button if it was on
mjr 53:9b2611964afc 5452 setButton(false);
mjr 48:058ace2aed1d 5453
mjr 53:9b2611964afc 5454 // return to the default state
mjr 53:9b2611964afc 5455 lbState = 0;
mjr 48:058ace2aed1d 5456 }
mjr 48:058ace2aed1d 5457 }
mjr 53:9b2611964afc 5458
mjr 53:9b2611964afc 5459 // Set the button state
mjr 53:9b2611964afc 5460 void setButton(bool on)
mjr 53:9b2611964afc 5461 {
mjr 53:9b2611964afc 5462 if (btnState != on)
mjr 53:9b2611964afc 5463 {
mjr 53:9b2611964afc 5464 // remember the new state
mjr 53:9b2611964afc 5465 btnState = on;
mjr 53:9b2611964afc 5466
mjr 53:9b2611964afc 5467 // update the virtual button state
mjr 65:739875521aae 5468 buttonState[zblButtonIndex].virtPress(on);
mjr 53:9b2611964afc 5469 }
mjr 53:9b2611964afc 5470 }
mjr 53:9b2611964afc 5471
mjr 48:058ace2aed1d 5472 private:
mjr 48:058ace2aed1d 5473 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 48:058ace2aed1d 5474 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 48:058ace2aed1d 5475 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 48:058ace2aed1d 5476 // back and releases the plunger, or simply pushes on the plunger from
mjr 48:058ace2aed1d 5477 // the rest position. This allows the plunger to be used in lieu of a
mjr 48:058ace2aed1d 5478 // physical Launch Ball button for tables that don't have plungers.
mjr 48:058ace2aed1d 5479 //
mjr 48:058ace2aed1d 5480 // States:
mjr 48:058ace2aed1d 5481 // 0 = default
mjr 53:9b2611964afc 5482 // 1 = firing (firing event has activated a Launch button pulse)
mjr 53:9b2611964afc 5483 // 2 = firing done (Launch button pulse ended, waiting for plunger
mjr 53:9b2611964afc 5484 // firing event to end)
mjr 53:9b2611964afc 5485 uint8_t lbState;
mjr 48:058ace2aed1d 5486
mjr 53:9b2611964afc 5487 // button state
mjr 53:9b2611964afc 5488 bool btnState;
mjr 48:058ace2aed1d 5489
mjr 48:058ace2aed1d 5490 // Time since last lbState transition. Some of the states are time-
mjr 48:058ace2aed1d 5491 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 48:058ace2aed1d 5492 // we remain in this state for more than a few milliseconds, since
mjr 48:058ace2aed1d 5493 // it indicates that the plunger is being slowly returned to rest
mjr 48:058ace2aed1d 5494 // rather than released. In the "launching" state, we need to release
mjr 48:058ace2aed1d 5495 // the Launch Ball button after a moment, and we need to wait for
mjr 48:058ace2aed1d 5496 // the plunger to come to rest before returning to state 0.
mjr 48:058ace2aed1d 5497 Timer lbTimer;
mjr 48:058ace2aed1d 5498 };
mjr 48:058ace2aed1d 5499
mjr 35:e959ffba78fd 5500 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5501 //
mjr 35:e959ffba78fd 5502 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 5503 //
mjr 54:fd77a6b2f76c 5504 void reboot(USBJoystick &js, bool disconnect = true, long pause_us = 2000000L)
mjr 35:e959ffba78fd 5505 {
mjr 35:e959ffba78fd 5506 // disconnect from USB
mjr 54:fd77a6b2f76c 5507 if (disconnect)
mjr 54:fd77a6b2f76c 5508 js.disconnect();
mjr 35:e959ffba78fd 5509
mjr 35:e959ffba78fd 5510 // wait a few seconds to make sure the host notices the disconnect
mjr 54:fd77a6b2f76c 5511 wait_us(pause_us);
mjr 35:e959ffba78fd 5512
mjr 35:e959ffba78fd 5513 // reset the device
mjr 35:e959ffba78fd 5514 NVIC_SystemReset();
mjr 35:e959ffba78fd 5515 while (true) { }
mjr 35:e959ffba78fd 5516 }
mjr 35:e959ffba78fd 5517
mjr 35:e959ffba78fd 5518 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5519 //
mjr 35:e959ffba78fd 5520 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 5521 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 5522 //
mjr 35:e959ffba78fd 5523 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 5524 {
mjr 35:e959ffba78fd 5525 int tmp;
mjr 78:1e00b3fa11af 5526 switch (cfg.accel.orientation)
mjr 35:e959ffba78fd 5527 {
mjr 35:e959ffba78fd 5528 case OrientationFront:
mjr 35:e959ffba78fd 5529 tmp = x;
mjr 35:e959ffba78fd 5530 x = y;
mjr 35:e959ffba78fd 5531 y = tmp;
mjr 35:e959ffba78fd 5532 break;
mjr 35:e959ffba78fd 5533
mjr 35:e959ffba78fd 5534 case OrientationLeft:
mjr 35:e959ffba78fd 5535 x = -x;
mjr 35:e959ffba78fd 5536 break;
mjr 35:e959ffba78fd 5537
mjr 35:e959ffba78fd 5538 case OrientationRight:
mjr 35:e959ffba78fd 5539 y = -y;
mjr 35:e959ffba78fd 5540 break;
mjr 35:e959ffba78fd 5541
mjr 35:e959ffba78fd 5542 case OrientationRear:
mjr 35:e959ffba78fd 5543 tmp = -x;
mjr 35:e959ffba78fd 5544 x = -y;
mjr 35:e959ffba78fd 5545 y = tmp;
mjr 35:e959ffba78fd 5546 break;
mjr 35:e959ffba78fd 5547 }
mjr 35:e959ffba78fd 5548 }
mjr 35:e959ffba78fd 5549
mjr 35:e959ffba78fd 5550 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5551 //
mjr 35:e959ffba78fd 5552 // Calibration button state:
mjr 35:e959ffba78fd 5553 // 0 = not pushed
mjr 35:e959ffba78fd 5554 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 5555 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 5556 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 5557 int calBtnState = 0;
mjr 35:e959ffba78fd 5558
mjr 35:e959ffba78fd 5559 // calibration button debounce timer
mjr 35:e959ffba78fd 5560 Timer calBtnTimer;
mjr 35:e959ffba78fd 5561
mjr 35:e959ffba78fd 5562 // calibration button light state
mjr 35:e959ffba78fd 5563 int calBtnLit = false;
mjr 35:e959ffba78fd 5564
mjr 35:e959ffba78fd 5565
mjr 35:e959ffba78fd 5566 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5567 //
mjr 40:cc0d9814522b 5568 // Configuration variable get/set message handling
mjr 35:e959ffba78fd 5569 //
mjr 40:cc0d9814522b 5570
mjr 40:cc0d9814522b 5571 // Handle SET messages - write configuration variables from USB message data
mjr 40:cc0d9814522b 5572 #define if_msg_valid(test) if (test)
mjr 53:9b2611964afc 5573 #define v_byte(var, ofs) cfg.var = data[ofs]
mjr 53:9b2611964afc 5574 #define v_ui16(var, ofs) cfg.var = wireUI16(data+(ofs))
mjr 77:0b96f6867312 5575 #define v_ui32(var, ofs) cfg.var = wireUI32(data+(ofs))
mjr 53:9b2611964afc 5576 #define v_pin(var, ofs) cfg.var = wirePinName(data[ofs])
mjr 53:9b2611964afc 5577 #define v_byte_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 5578 #define v_ui32_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 5579 #define VAR_MODE_SET 1 // we're in SET mode
mjr 76:7f5912b6340e 5580 #define v_func configVarSet(const uint8_t *data)
mjr 40:cc0d9814522b 5581 #include "cfgVarMsgMap.h"
mjr 35:e959ffba78fd 5582
mjr 40:cc0d9814522b 5583 // redefine everything for the SET messages
mjr 40:cc0d9814522b 5584 #undef if_msg_valid
mjr 40:cc0d9814522b 5585 #undef v_byte
mjr 40:cc0d9814522b 5586 #undef v_ui16
mjr 77:0b96f6867312 5587 #undef v_ui32
mjr 40:cc0d9814522b 5588 #undef v_pin
mjr 53:9b2611964afc 5589 #undef v_byte_ro
mjr 74:822a92bc11d2 5590 #undef v_ui32_ro
mjr 74:822a92bc11d2 5591 #undef VAR_MODE_SET
mjr 40:cc0d9814522b 5592 #undef v_func
mjr 38:091e511ce8a0 5593
mjr 40:cc0d9814522b 5594 // Handle GET messages - read variable values and return in USB message daa
mjr 40:cc0d9814522b 5595 #define if_msg_valid(test)
mjr 53:9b2611964afc 5596 #define v_byte(var, ofs) data[ofs] = cfg.var
mjr 53:9b2611964afc 5597 #define v_ui16(var, ofs) ui16Wire(data+(ofs), cfg.var)
mjr 77:0b96f6867312 5598 #define v_ui32(var, ofs) ui32Wire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 5599 #define v_pin(var, ofs) pinNameWire(data+(ofs), cfg.var)
mjr 73:4e8ce0b18915 5600 #define v_byte_ro(val, ofs) data[ofs] = (val)
mjr 74:822a92bc11d2 5601 #define v_ui32_ro(val, ofs) ui32Wire(data+(ofs), val);
mjr 74:822a92bc11d2 5602 #define VAR_MODE_SET 0 // we're in GET mode
mjr 76:7f5912b6340e 5603 #define v_func configVarGet(uint8_t *data)
mjr 40:cc0d9814522b 5604 #include "cfgVarMsgMap.h"
mjr 40:cc0d9814522b 5605
mjr 35:e959ffba78fd 5606
mjr 35:e959ffba78fd 5607 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5608 //
mjr 35:e959ffba78fd 5609 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 5610 // LedWiz protocol.
mjr 33:d832bcab089e 5611 //
mjr 78:1e00b3fa11af 5612 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js, Accel &accel)
mjr 35:e959ffba78fd 5613 {
mjr 38:091e511ce8a0 5614 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 5615 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 5616 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 5617 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 5618 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 5619 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 5620 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 5621 // So our full protocol is as follows:
mjr 38:091e511ce8a0 5622 //
mjr 38:091e511ce8a0 5623 // first byte =
mjr 74:822a92bc11d2 5624 // 0-48 -> PBA
mjr 74:822a92bc11d2 5625 // 64 -> SBA
mjr 38:091e511ce8a0 5626 // 65 -> private control message; second byte specifies subtype
mjr 74:822a92bc11d2 5627 // 129-132 -> PBA
mjr 38:091e511ce8a0 5628 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 5629 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 5630 // other -> reserved for future use
mjr 38:091e511ce8a0 5631 //
mjr 39:b3815a1c3802 5632 uint8_t *data = lwm.data;
mjr 74:822a92bc11d2 5633 if (data[0] == 64)
mjr 35:e959ffba78fd 5634 {
mjr 74:822a92bc11d2 5635 // 64 = SBA (original LedWiz command to set on/off switches for ports 1-32)
mjr 74:822a92bc11d2 5636 //printf("SBA %02x %02x %02x %02x, speed %02x\r\n",
mjr 38:091e511ce8a0 5637 // data[1], data[2], data[3], data[4], data[5]);
mjr 74:822a92bc11d2 5638 sba_sbx(0, data);
mjr 74:822a92bc11d2 5639
mjr 74:822a92bc11d2 5640 // SBA resets the PBA port group counter
mjr 38:091e511ce8a0 5641 pbaIdx = 0;
mjr 38:091e511ce8a0 5642 }
mjr 38:091e511ce8a0 5643 else if (data[0] == 65)
mjr 38:091e511ce8a0 5644 {
mjr 38:091e511ce8a0 5645 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 5646 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 5647 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 5648 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 5649 // message type.
mjr 39:b3815a1c3802 5650 switch (data[1])
mjr 38:091e511ce8a0 5651 {
mjr 39:b3815a1c3802 5652 case 0:
mjr 39:b3815a1c3802 5653 // No Op
mjr 39:b3815a1c3802 5654 break;
mjr 39:b3815a1c3802 5655
mjr 39:b3815a1c3802 5656 case 1:
mjr 38:091e511ce8a0 5657 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 5658 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 5659 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 5660 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 5661 {
mjr 39:b3815a1c3802 5662
mjr 39:b3815a1c3802 5663 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 5664 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 5665 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 5666
mjr 39:b3815a1c3802 5667 // we'll need a reset if the LedWiz unit number is changing
mjr 39:b3815a1c3802 5668 bool needReset = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 5669
mjr 39:b3815a1c3802 5670 // set the configuration parameters from the message
mjr 39:b3815a1c3802 5671 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 5672 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 5673
mjr 77:0b96f6867312 5674 // set the flag to do the save
mjr 77:0b96f6867312 5675 saveConfigPending = needReset ? SAVE_CONFIG_AND_REBOOT : SAVE_CONFIG_ONLY;
mjr 77:0b96f6867312 5676 saveConfigRebootTime = 0;
mjr 39:b3815a1c3802 5677 }
mjr 39:b3815a1c3802 5678 break;
mjr 38:091e511ce8a0 5679
mjr 39:b3815a1c3802 5680 case 2:
mjr 38:091e511ce8a0 5681 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 5682 // (No parameters)
mjr 38:091e511ce8a0 5683
mjr 38:091e511ce8a0 5684 // enter calibration mode
mjr 38:091e511ce8a0 5685 calBtnState = 3;
mjr 52:8298b2a73eb2 5686 plungerReader.setCalMode(true);
mjr 38:091e511ce8a0 5687 calBtnTimer.reset();
mjr 39:b3815a1c3802 5688 break;
mjr 39:b3815a1c3802 5689
mjr 39:b3815a1c3802 5690 case 3:
mjr 52:8298b2a73eb2 5691 // 3 = plunger sensor status report
mjr 48:058ace2aed1d 5692 // data[2] = flag bits
mjr 53:9b2611964afc 5693 // data[3] = extra exposure time, 100us (.1ms) increments
mjr 52:8298b2a73eb2 5694 reportPlungerStat = true;
mjr 53:9b2611964afc 5695 reportPlungerStatFlags = data[2];
mjr 53:9b2611964afc 5696 reportPlungerStatTime = data[3];
mjr 38:091e511ce8a0 5697
mjr 38:091e511ce8a0 5698 // show purple until we finish sending the report
mjr 38:091e511ce8a0 5699 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 5700 break;
mjr 39:b3815a1c3802 5701
mjr 39:b3815a1c3802 5702 case 4:
mjr 38:091e511ce8a0 5703 // 4 = hardware configuration query
mjr 38:091e511ce8a0 5704 // (No parameters)
mjr 38:091e511ce8a0 5705 js.reportConfig(
mjr 38:091e511ce8a0 5706 numOutputs,
mjr 38:091e511ce8a0 5707 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 52:8298b2a73eb2 5708 cfg.plunger.cal.zero, cfg.plunger.cal.max, cfg.plunger.cal.tRelease,
mjr 75:677892300e7a 5709 nvm.valid(), // a config is loaded if the config memory block is valid
mjr 75:677892300e7a 5710 true, // we support sbx/pbx extensions
mjr 78:1e00b3fa11af 5711 true, // we support the new accelerometer settings
mjr 75:677892300e7a 5712 xmalloc_rem); // remaining memory size
mjr 39:b3815a1c3802 5713 break;
mjr 39:b3815a1c3802 5714
mjr 39:b3815a1c3802 5715 case 5:
mjr 38:091e511ce8a0 5716 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 5717 allOutputsOff();
mjr 39:b3815a1c3802 5718 break;
mjr 39:b3815a1c3802 5719
mjr 39:b3815a1c3802 5720 case 6:
mjr 77:0b96f6867312 5721 // 6 = Save configuration to flash. Reboot after the delay
mjr 77:0b96f6867312 5722 // time in seconds given in data[2].
mjr 77:0b96f6867312 5723 saveConfigPending = SAVE_CONFIG_AND_REBOOT;
mjr 77:0b96f6867312 5724 saveConfigRebootTime = data[2];
mjr 39:b3815a1c3802 5725 break;
mjr 40:cc0d9814522b 5726
mjr 40:cc0d9814522b 5727 case 7:
mjr 40:cc0d9814522b 5728 // 7 = Device ID report
mjr 53:9b2611964afc 5729 // data[2] = ID index: 1=CPU ID, 2=OpenSDA TUID
mjr 53:9b2611964afc 5730 js.reportID(data[2]);
mjr 40:cc0d9814522b 5731 break;
mjr 40:cc0d9814522b 5732
mjr 40:cc0d9814522b 5733 case 8:
mjr 40:cc0d9814522b 5734 // 8 = Engage/disengage night mode.
mjr 40:cc0d9814522b 5735 // data[2] = 1 to engage, 0 to disengage
mjr 40:cc0d9814522b 5736 setNightMode(data[2]);
mjr 40:cc0d9814522b 5737 break;
mjr 52:8298b2a73eb2 5738
mjr 52:8298b2a73eb2 5739 case 9:
mjr 52:8298b2a73eb2 5740 // 9 = Config variable query.
mjr 52:8298b2a73eb2 5741 // data[2] = config var ID
mjr 52:8298b2a73eb2 5742 // data[3] = array index (for array vars: button assignments, output ports)
mjr 52:8298b2a73eb2 5743 {
mjr 53:9b2611964afc 5744 // set up the reply buffer with the variable ID data, and zero out
mjr 53:9b2611964afc 5745 // the rest of the buffer
mjr 52:8298b2a73eb2 5746 uint8_t reply[8];
mjr 52:8298b2a73eb2 5747 reply[1] = data[2];
mjr 52:8298b2a73eb2 5748 reply[2] = data[3];
mjr 53:9b2611964afc 5749 memset(reply+3, 0, sizeof(reply)-3);
mjr 52:8298b2a73eb2 5750
mjr 52:8298b2a73eb2 5751 // query the value
mjr 52:8298b2a73eb2 5752 configVarGet(reply);
mjr 52:8298b2a73eb2 5753
mjr 52:8298b2a73eb2 5754 // send the reply
mjr 52:8298b2a73eb2 5755 js.reportConfigVar(reply + 1);
mjr 52:8298b2a73eb2 5756 }
mjr 52:8298b2a73eb2 5757 break;
mjr 53:9b2611964afc 5758
mjr 53:9b2611964afc 5759 case 10:
mjr 53:9b2611964afc 5760 // 10 = Build ID query.
mjr 53:9b2611964afc 5761 js.reportBuildInfo(getBuildID());
mjr 53:9b2611964afc 5762 break;
mjr 73:4e8ce0b18915 5763
mjr 73:4e8ce0b18915 5764 case 11:
mjr 73:4e8ce0b18915 5765 // 11 = TV ON relay control.
mjr 73:4e8ce0b18915 5766 // data[2] = operation:
mjr 73:4e8ce0b18915 5767 // 0 = turn relay off
mjr 73:4e8ce0b18915 5768 // 1 = turn relay on
mjr 73:4e8ce0b18915 5769 // 2 = pulse relay (as though the power-on timer fired)
mjr 73:4e8ce0b18915 5770 TVRelay(data[2]);
mjr 73:4e8ce0b18915 5771 break;
mjr 73:4e8ce0b18915 5772
mjr 73:4e8ce0b18915 5773 case 12:
mjr 77:0b96f6867312 5774 // 12 = Learn IR code. This enters IR learning mode. While
mjr 77:0b96f6867312 5775 // in learning mode, we report raw IR signals and the first IR
mjr 77:0b96f6867312 5776 // command decoded through the special IR report format. IR
mjr 77:0b96f6867312 5777 // learning mode automatically ends after a timeout expires if
mjr 77:0b96f6867312 5778 // no command can be decoded within the time limit.
mjr 77:0b96f6867312 5779
mjr 77:0b96f6867312 5780 // enter IR learning mode
mjr 77:0b96f6867312 5781 IRLearningMode = 1;
mjr 77:0b96f6867312 5782
mjr 77:0b96f6867312 5783 // cancel any regular IR input in progress
mjr 77:0b96f6867312 5784 IRCommandIn = 0;
mjr 77:0b96f6867312 5785
mjr 77:0b96f6867312 5786 // reset and start the learning mode timeout timer
mjr 77:0b96f6867312 5787 IRTimer.reset();
mjr 73:4e8ce0b18915 5788 break;
mjr 73:4e8ce0b18915 5789
mjr 73:4e8ce0b18915 5790 case 13:
mjr 73:4e8ce0b18915 5791 // 13 = Send button status report
mjr 73:4e8ce0b18915 5792 reportButtonStatus(js);
mjr 73:4e8ce0b18915 5793 break;
mjr 78:1e00b3fa11af 5794
mjr 78:1e00b3fa11af 5795 case 14:
mjr 78:1e00b3fa11af 5796 // 14 = manually center the accelerometer
mjr 78:1e00b3fa11af 5797 accel.manualCenterRequest();
mjr 78:1e00b3fa11af 5798 break;
mjr 78:1e00b3fa11af 5799
mjr 78:1e00b3fa11af 5800 case 15:
mjr 78:1e00b3fa11af 5801 // 15 = set up ad hoc IR command, part 1. Mark the command
mjr 78:1e00b3fa11af 5802 // as not ready, and save the partial data from the message.
mjr 78:1e00b3fa11af 5803 IRAdHocCmd.ready = 0;
mjr 78:1e00b3fa11af 5804 IRAdHocCmd.protocol = data[2];
mjr 78:1e00b3fa11af 5805 IRAdHocCmd.dittos = (data[3] & IRFlagDittos) != 0;
mjr 78:1e00b3fa11af 5806 IRAdHocCmd.code = wireUI32(&data[4]);
mjr 78:1e00b3fa11af 5807 break;
mjr 78:1e00b3fa11af 5808
mjr 78:1e00b3fa11af 5809 case 16:
mjr 78:1e00b3fa11af 5810 // 16 = send ad hoc IR command, part 2. Fill in the rest
mjr 78:1e00b3fa11af 5811 // of the data from the message and mark the command as
mjr 78:1e00b3fa11af 5812 // ready. The IR polling routine will send this as soon
mjr 78:1e00b3fa11af 5813 // as the IR transmitter is free.
mjr 78:1e00b3fa11af 5814 IRAdHocCmd.code |= (uint64_t(wireUI32(&data[2])) << 32);
mjr 78:1e00b3fa11af 5815 IRAdHocCmd.ready = 1;
mjr 78:1e00b3fa11af 5816 break;
mjr 38:091e511ce8a0 5817 }
mjr 38:091e511ce8a0 5818 }
mjr 38:091e511ce8a0 5819 else if (data[0] == 66)
mjr 38:091e511ce8a0 5820 {
mjr 38:091e511ce8a0 5821 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 5822 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 5823 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 5824 // in a variable-dependent format.
mjr 40:cc0d9814522b 5825 configVarSet(data);
mjr 38:091e511ce8a0 5826 }
mjr 74:822a92bc11d2 5827 else if (data[0] == 67)
mjr 74:822a92bc11d2 5828 {
mjr 74:822a92bc11d2 5829 // SBX - extended SBA message. This is the same as SBA, except
mjr 74:822a92bc11d2 5830 // that the 7th byte selects a group of 32 ports, to allow access
mjr 74:822a92bc11d2 5831 // to ports beyond the first 32.
mjr 74:822a92bc11d2 5832 sba_sbx(data[6], data);
mjr 74:822a92bc11d2 5833 }
mjr 74:822a92bc11d2 5834 else if (data[0] == 68)
mjr 74:822a92bc11d2 5835 {
mjr 74:822a92bc11d2 5836 // PBX - extended PBA message. This is similar to PBA, but
mjr 74:822a92bc11d2 5837 // allows access to more than the first 32 ports by encoding
mjr 74:822a92bc11d2 5838 // a port group byte that selects a block of 8 ports.
mjr 74:822a92bc11d2 5839
mjr 74:822a92bc11d2 5840 // get the port group - the first port is 8*group
mjr 74:822a92bc11d2 5841 int portGroup = data[1];
mjr 74:822a92bc11d2 5842
mjr 74:822a92bc11d2 5843 // unpack the brightness values
mjr 74:822a92bc11d2 5844 uint32_t tmp1 = data[2] | (data[3]<<8) | (data[4]<<16);
mjr 74:822a92bc11d2 5845 uint32_t tmp2 = data[5] | (data[6]<<8) | (data[7]<<16);
mjr 74:822a92bc11d2 5846 uint8_t bri[8] = {
mjr 74:822a92bc11d2 5847 tmp1 & 0x3F, (tmp1>>6) & 0x3F, (tmp1>>12) & 0x3F, (tmp1>>18) & 0x3F,
mjr 74:822a92bc11d2 5848 tmp2 & 0x3F, (tmp2>>6) & 0x3F, (tmp2>>12) & 0x3F, (tmp2>>18) & 0x3F
mjr 74:822a92bc11d2 5849 };
mjr 74:822a92bc11d2 5850
mjr 74:822a92bc11d2 5851 // map the flash levels: 60->129, 61->130, 62->131, 63->132
mjr 74:822a92bc11d2 5852 for (int i = 0 ; i < 8 ; ++i)
mjr 74:822a92bc11d2 5853 {
mjr 74:822a92bc11d2 5854 if (bri[i] >= 60)
mjr 74:822a92bc11d2 5855 bri[i] += 129-60;
mjr 74:822a92bc11d2 5856 }
mjr 74:822a92bc11d2 5857
mjr 74:822a92bc11d2 5858 // Carry out the PBA
mjr 74:822a92bc11d2 5859 pba_pbx(portGroup*8, bri);
mjr 74:822a92bc11d2 5860 }
mjr 38:091e511ce8a0 5861 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 5862 {
mjr 38:091e511ce8a0 5863 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 5864 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 5865 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 5866 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 5867 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 5868 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 5869 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 5870 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 5871 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 5872 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 5873 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 5874 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 5875 //
mjr 38:091e511ce8a0 5876 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 5877 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 5878 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 5879 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 5880 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 5881 // address those ports anyway.
mjr 63:5cd1a5f3a41b 5882
mjr 63:5cd1a5f3a41b 5883 // figure the block of 7 ports covered in the message
mjr 38:091e511ce8a0 5884 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 5885 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 63:5cd1a5f3a41b 5886
mjr 63:5cd1a5f3a41b 5887 // update each port
mjr 38:091e511ce8a0 5888 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 5889 {
mjr 38:091e511ce8a0 5890 // set the brightness level for the output
mjr 40:cc0d9814522b 5891 uint8_t b = data[i-i0+1];
mjr 38:091e511ce8a0 5892 outLevel[i] = b;
mjr 38:091e511ce8a0 5893
mjr 74:822a92bc11d2 5894 // set the port's LedWiz state to the nearest equivalent, so
mjr 74:822a92bc11d2 5895 // that it maintains its current setting if we switch back to
mjr 74:822a92bc11d2 5896 // LedWiz mode on a future update
mjr 76:7f5912b6340e 5897 if (b != 0)
mjr 76:7f5912b6340e 5898 {
mjr 76:7f5912b6340e 5899 // Non-zero brightness - set the SBA switch on, and set the
mjr 76:7f5912b6340e 5900 // PBA brightness to the DOF brightness rescaled to the 1..48
mjr 76:7f5912b6340e 5901 // LedWiz range. If the port is subsequently addressed by an
mjr 76:7f5912b6340e 5902 // LedWiz command, this will carry the current DOF setting
mjr 76:7f5912b6340e 5903 // forward unchanged.
mjr 76:7f5912b6340e 5904 wizOn[i] = 1;
mjr 76:7f5912b6340e 5905 wizVal[i] = dof_to_lw[b];
mjr 76:7f5912b6340e 5906 }
mjr 76:7f5912b6340e 5907 else
mjr 76:7f5912b6340e 5908 {
mjr 76:7f5912b6340e 5909 // Zero brightness. Set the SBA switch off, and leave the
mjr 76:7f5912b6340e 5910 // PBA brightness the same as it was.
mjr 76:7f5912b6340e 5911 wizOn[i] = 0;
mjr 76:7f5912b6340e 5912 }
mjr 74:822a92bc11d2 5913
mjr 38:091e511ce8a0 5914 // set the output
mjr 40:cc0d9814522b 5915 lwPin[i]->set(b);
mjr 38:091e511ce8a0 5916 }
mjr 38:091e511ce8a0 5917
mjr 38:091e511ce8a0 5918 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 5919 if (hc595 != 0)
mjr 38:091e511ce8a0 5920 hc595->update();
mjr 38:091e511ce8a0 5921 }
mjr 38:091e511ce8a0 5922 else
mjr 38:091e511ce8a0 5923 {
mjr 74:822a92bc11d2 5924 // Everything else is an LedWiz PBA message. This is a full
mjr 74:822a92bc11d2 5925 // "profile" dump from the host for one bank of 8 outputs. Each
mjr 74:822a92bc11d2 5926 // byte sets one output in the current bank. The current bank
mjr 74:822a92bc11d2 5927 // is implied; the bank starts at 0 and is reset to 0 by any SBA
mjr 74:822a92bc11d2 5928 // message, and is incremented to the next bank by each PBA. Our
mjr 74:822a92bc11d2 5929 // variable pbaIdx keeps track of the current bank. There's no
mjr 74:822a92bc11d2 5930 // direct way for the host to select the bank; it just has to count
mjr 74:822a92bc11d2 5931 // on us staying in sync. In practice, clients always send the
mjr 74:822a92bc11d2 5932 // full set of 4 PBA messages in a row to set all 32 outputs.
mjr 38:091e511ce8a0 5933 //
mjr 38:091e511ce8a0 5934 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 5935 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 5936 // wizVal[] entry for each output, and that takes precedence
mjr 63:5cd1a5f3a41b 5937 // over the extended protocol settings when we're in LedWiz
mjr 63:5cd1a5f3a41b 5938 // protocol mode.
mjr 38:091e511ce8a0 5939 //
mjr 38:091e511ce8a0 5940 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 5941 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 5942
mjr 74:822a92bc11d2 5943 // carry out the PBA
mjr 74:822a92bc11d2 5944 pba_pbx(pbaIdx, data);
mjr 74:822a92bc11d2 5945
mjr 74:822a92bc11d2 5946 // update the PBX index state for the next message
mjr 74:822a92bc11d2 5947 pbaIdx = (pbaIdx + 8) % 32;
mjr 38:091e511ce8a0 5948 }
mjr 38:091e511ce8a0 5949 }
mjr 35:e959ffba78fd 5950
mjr 38:091e511ce8a0 5951 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 5952 //
mjr 5:a70c0bce770d 5953 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 5954 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 5955 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 5956 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 5957 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 5958 // port outputs.
mjr 5:a70c0bce770d 5959 //
mjr 0:5acbbe3f4cf4 5960 int main(void)
mjr 0:5acbbe3f4cf4 5961 {
mjr 60:f38da020aa13 5962 // say hello to the debug console, in case it's connected
mjr 39:b3815a1c3802 5963 printf("\r\nPinscape Controller starting\r\n");
mjr 77:0b96f6867312 5964
mjr 77:0b96f6867312 5965
mjr 60:f38da020aa13 5966 // debugging: print memory config info
mjr 59:94eb9265b6d7 5967 // -> no longer very useful, since we use our own custom malloc/new allocator (see xmalloc() above)
mjr 60:f38da020aa13 5968 // {int *a = new int; printf("Stack=%lx, heap=%lx, free=%ld\r\n", (long)&a, (long)a, (long)&a - (long)a);}
mjr 1:d913e0afb2ac 5969
mjr 76:7f5912b6340e 5970 // clear the I2C connection
mjr 35:e959ffba78fd 5971 clear_i2c();
mjr 38:091e511ce8a0 5972
mjr 76:7f5912b6340e 5973 // Load the saved configuration. There are two sources of the
mjr 76:7f5912b6340e 5974 // configuration data:
mjr 76:7f5912b6340e 5975 //
mjr 76:7f5912b6340e 5976 // - Look for an NVM (flash non-volatile memory) configuration.
mjr 76:7f5912b6340e 5977 // If this is valid, we'll load it. The NVM is config data that can
mjr 76:7f5912b6340e 5978 // be updated dynamically by the host via USB commands and then stored
mjr 76:7f5912b6340e 5979 // in the flash by the firmware itself. If this exists, it supersedes
mjr 76:7f5912b6340e 5980 // any of the other settings stores. The Windows config tool uses this
mjr 76:7f5912b6340e 5981 // to store user settings updates.
mjr 76:7f5912b6340e 5982 //
mjr 76:7f5912b6340e 5983 // - If there's no NVM, we'll load the factory defaults, then we'll
mjr 76:7f5912b6340e 5984 // load any settings stored in the host-loaded configuration. The
mjr 76:7f5912b6340e 5985 // host can patch a set of configuration variable settings into the
mjr 76:7f5912b6340e 5986 // .bin file when loading new firmware, in the host-loaded config
mjr 76:7f5912b6340e 5987 // area that we reserve for this purpose. This allows the host to
mjr 76:7f5912b6340e 5988 // restore a configuration at the same time it installs firmware,
mjr 76:7f5912b6340e 5989 // without a separate download of the config data.
mjr 76:7f5912b6340e 5990 //
mjr 76:7f5912b6340e 5991 // The NVM supersedes the host-loaded config, since it can be updated
mjr 76:7f5912b6340e 5992 // between firmware updated and is thus presumably more recent if it's
mjr 76:7f5912b6340e 5993 // present. (Note that the NVM and host-loaded config are both in
mjr 76:7f5912b6340e 5994 // flash, so in principle we could just have a single NVM store that
mjr 76:7f5912b6340e 5995 // the host patches. The only reason we don't is that the NVM store
mjr 76:7f5912b6340e 5996 // is an image of our in-memory config structure, which is a native C
mjr 76:7f5912b6340e 5997 // struct, and we don't want the host to have to know the details of
mjr 76:7f5912b6340e 5998 // its byte layout, for obvious reasons. The host-loaded config, in
mjr 76:7f5912b6340e 5999 // contrast, uses the wire protocol format, which has a well-defined
mjr 76:7f5912b6340e 6000 // byte layout that's independent of the firmware version or the
mjr 76:7f5912b6340e 6001 // details of how the C compiler arranges the struct memory.)
mjr 76:7f5912b6340e 6002 if (!loadConfigFromFlash())
mjr 76:7f5912b6340e 6003 loadHostLoadedConfig();
mjr 35:e959ffba78fd 6004
mjr 38:091e511ce8a0 6005 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 6006 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 6007
mjr 33:d832bcab089e 6008 // we're not connected/awake yet
mjr 33:d832bcab089e 6009 bool connected = false;
mjr 40:cc0d9814522b 6010 Timer connectChangeTimer;
mjr 33:d832bcab089e 6011
mjr 35:e959ffba78fd 6012 // create the plunger sensor interface
mjr 35:e959ffba78fd 6013 createPlunger();
mjr 76:7f5912b6340e 6014
mjr 76:7f5912b6340e 6015 // update the plunger reader's cached calibration data
mjr 76:7f5912b6340e 6016 plungerReader.onUpdateCal();
mjr 33:d832bcab089e 6017
mjr 60:f38da020aa13 6018 // set up the TLC5940 interface, if these chips are present
mjr 35:e959ffba78fd 6019 init_tlc5940(cfg);
mjr 34:6b981a2afab7 6020
mjr 60:f38da020aa13 6021 // set up 74HC595 interface, if these chips are present
mjr 35:e959ffba78fd 6022 init_hc595(cfg);
mjr 6:cc35eb643e8f 6023
mjr 54:fd77a6b2f76c 6024 // Initialize the LedWiz ports. Note that the ordering here is important:
mjr 54:fd77a6b2f76c 6025 // this has to come after we create the TLC5940 and 74HC595 object instances
mjr 54:fd77a6b2f76c 6026 // (which we just did above), since we need to access those objects to set
mjr 54:fd77a6b2f76c 6027 // up ports assigned to the respective chips.
mjr 35:e959ffba78fd 6028 initLwOut(cfg);
mjr 48:058ace2aed1d 6029
mjr 60:f38da020aa13 6030 // start the TLC5940 refresh cycle clock
mjr 35:e959ffba78fd 6031 if (tlc5940 != 0)
mjr 35:e959ffba78fd 6032 tlc5940->start();
mjr 77:0b96f6867312 6033
mjr 77:0b96f6867312 6034 // Assume that nothing uses keyboard keys. We'll check for keyboard
mjr 77:0b96f6867312 6035 // usage when initializing the various subsystems that can send keys
mjr 77:0b96f6867312 6036 // (buttons, IR). If we find anything that does, we'll create the
mjr 77:0b96f6867312 6037 // USB keyboard interface.
mjr 77:0b96f6867312 6038 bool kbKeys = false;
mjr 77:0b96f6867312 6039
mjr 77:0b96f6867312 6040 // set up the IR remote control emitter & receiver, if present
mjr 77:0b96f6867312 6041 init_IR(cfg, kbKeys);
mjr 77:0b96f6867312 6042
mjr 77:0b96f6867312 6043 // start the power status time, if applicable
mjr 77:0b96f6867312 6044 startPowerStatusTimer(cfg);
mjr 48:058ace2aed1d 6045
mjr 35:e959ffba78fd 6046 // initialize the button input ports
mjr 35:e959ffba78fd 6047 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 6048
mjr 60:f38da020aa13 6049 // Create the joystick USB client. Note that the USB vendor/product ID
mjr 60:f38da020aa13 6050 // information comes from the saved configuration. Also note that we have
mjr 60:f38da020aa13 6051 // to wait until after initializing the input buttons (which we just did
mjr 60:f38da020aa13 6052 // above) to set up the interface, since the button setup will determine
mjr 60:f38da020aa13 6053 // whether or not we need to present a USB keyboard interface in addition
mjr 60:f38da020aa13 6054 // to the joystick interface.
mjr 51:57eb311faafa 6055 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false,
mjr 51:57eb311faafa 6056 cfg.joystickEnabled, kbKeys);
mjr 51:57eb311faafa 6057
mjr 60:f38da020aa13 6058 // Wait for the USB connection to start up. Show a distinctive diagnostic
mjr 60:f38da020aa13 6059 // flash pattern while waiting.
mjr 70:9f58735a1732 6060 Timer connTimeoutTimer, connFlashTimer;
mjr 70:9f58735a1732 6061 connTimeoutTimer.start();
mjr 70:9f58735a1732 6062 connFlashTimer.start();
mjr 51:57eb311faafa 6063 while (!js.configured())
mjr 51:57eb311faafa 6064 {
mjr 51:57eb311faafa 6065 // show one short yellow flash at 2-second intervals
mjr 70:9f58735a1732 6066 if (connFlashTimer.read_us() > 2000000)
mjr 51:57eb311faafa 6067 {
mjr 51:57eb311faafa 6068 // short yellow flash
mjr 51:57eb311faafa 6069 diagLED(1, 1, 0);
mjr 54:fd77a6b2f76c 6070 wait_us(50000);
mjr 51:57eb311faafa 6071 diagLED(0, 0, 0);
mjr 51:57eb311faafa 6072
mjr 51:57eb311faafa 6073 // reset the flash timer
mjr 70:9f58735a1732 6074 connFlashTimer.reset();
mjr 51:57eb311faafa 6075 }
mjr 70:9f58735a1732 6076
mjr 77:0b96f6867312 6077 // If we've been disconnected for more than the reboot timeout,
mjr 77:0b96f6867312 6078 // reboot. Some PCs won't reconnect if we were left plugged in
mjr 77:0b96f6867312 6079 // during a power cycle on the PC, but fortunately a reboot on
mjr 77:0b96f6867312 6080 // the KL25Z will make the host notice us and trigger a reconnect.
mjr 70:9f58735a1732 6081 if (cfg.disconnectRebootTimeout != 0
mjr 70:9f58735a1732 6082 && connTimeoutTimer.read() > cfg.disconnectRebootTimeout)
mjr 70:9f58735a1732 6083 reboot(js, false, 0);
mjr 77:0b96f6867312 6084
mjr 77:0b96f6867312 6085 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6086 powerStatusUpdate(cfg);
mjr 51:57eb311faafa 6087 }
mjr 60:f38da020aa13 6088
mjr 60:f38da020aa13 6089 // we're now connected to the host
mjr 54:fd77a6b2f76c 6090 connected = true;
mjr 40:cc0d9814522b 6091
mjr 60:f38da020aa13 6092 // Last report timer for the joytick interface. We use this timer to
mjr 60:f38da020aa13 6093 // throttle the report rate to a pace that's suitable for VP. Without
mjr 60:f38da020aa13 6094 // any artificial delays, we could generate data to send on the joystick
mjr 60:f38da020aa13 6095 // interface on every loop iteration. The loop iteration time depends
mjr 60:f38da020aa13 6096 // on which devices are attached, since most of the work in our main
mjr 60:f38da020aa13 6097 // loop is simply polling our devices. For typical setups, the loop
mjr 60:f38da020aa13 6098 // time ranges from about 0.25ms to 2.5ms; the biggest factor is the
mjr 60:f38da020aa13 6099 // plunger sensor. But VP polls for input about every 10ms, so there's
mjr 60:f38da020aa13 6100 // no benefit in sending data faster than that, and there's some harm,
mjr 60:f38da020aa13 6101 // in that it creates USB overhead (both on the wire and on the host
mjr 60:f38da020aa13 6102 // CPU). We therefore use this timer to pace our reports to roughly
mjr 60:f38da020aa13 6103 // the VP input polling rate. Note that there's no way to actually
mjr 60:f38da020aa13 6104 // synchronize with VP's polling, but there's also no need to, as the
mjr 60:f38da020aa13 6105 // input model is designed to reflect the overall current state at any
mjr 60:f38da020aa13 6106 // given time rather than events or deltas. If VP polls twice between
mjr 60:f38da020aa13 6107 // two updates, it simply sees no state change; if we send two updates
mjr 60:f38da020aa13 6108 // between VP polls, VP simply sees the latest state when it does get
mjr 60:f38da020aa13 6109 // around to polling.
mjr 38:091e511ce8a0 6110 Timer jsReportTimer;
mjr 38:091e511ce8a0 6111 jsReportTimer.start();
mjr 38:091e511ce8a0 6112
mjr 60:f38da020aa13 6113 // Time since we successfully sent a USB report. This is a hacky
mjr 60:f38da020aa13 6114 // workaround to deal with any remaining sporadic problems in the USB
mjr 60:f38da020aa13 6115 // stack. I've been trying to bulletproof the USB code over time to
mjr 60:f38da020aa13 6116 // remove all such problems at their source, but it seems unlikely that
mjr 60:f38da020aa13 6117 // we'll ever get them all. Thus this hack. The idea here is that if
mjr 60:f38da020aa13 6118 // we go too long without successfully sending a USB report, we'll
mjr 60:f38da020aa13 6119 // assume that the connection is broken (and the KL25Z USB hardware
mjr 60:f38da020aa13 6120 // hasn't noticed this), and we'll try taking measures to recover.
mjr 38:091e511ce8a0 6121 Timer jsOKTimer;
mjr 38:091e511ce8a0 6122 jsOKTimer.start();
mjr 35:e959ffba78fd 6123
mjr 55:4db125cd11a0 6124 // Initialize the calibration button and lamp, if enabled. To be enabled,
mjr 55:4db125cd11a0 6125 // the pin has to be assigned to something other than NC (0xFF), AND the
mjr 55:4db125cd11a0 6126 // corresponding feature enable flag has to be set.
mjr 55:4db125cd11a0 6127 DigitalIn *calBtn = 0;
mjr 55:4db125cd11a0 6128 DigitalOut *calBtnLed = 0;
mjr 55:4db125cd11a0 6129
mjr 55:4db125cd11a0 6130 // calibration button input - feature flag 0x01
mjr 55:4db125cd11a0 6131 if ((cfg.plunger.cal.features & 0x01) && cfg.plunger.cal.btn != 0xFF)
mjr 55:4db125cd11a0 6132 calBtn = new DigitalIn(wirePinName(cfg.plunger.cal.btn));
mjr 55:4db125cd11a0 6133
mjr 55:4db125cd11a0 6134 // calibration button indicator lamp output - feature flag 0x02
mjr 55:4db125cd11a0 6135 if ((cfg.plunger.cal.features & 0x02) && cfg.plunger.cal.led != 0xFF)
mjr 55:4db125cd11a0 6136 calBtnLed = new DigitalOut(wirePinName(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 6137
mjr 35:e959ffba78fd 6138 // initialize the calibration button
mjr 1:d913e0afb2ac 6139 calBtnTimer.start();
mjr 35:e959ffba78fd 6140 calBtnState = 0;
mjr 1:d913e0afb2ac 6141
mjr 1:d913e0afb2ac 6142 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 6143 Timer hbTimer;
mjr 1:d913e0afb2ac 6144 hbTimer.start();
mjr 1:d913e0afb2ac 6145 int hb = 0;
mjr 5:a70c0bce770d 6146 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 6147
mjr 1:d913e0afb2ac 6148 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 6149 Timer acTimer;
mjr 1:d913e0afb2ac 6150 acTimer.start();
mjr 1:d913e0afb2ac 6151
mjr 0:5acbbe3f4cf4 6152 // create the accelerometer object
mjr 77:0b96f6867312 6153 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS,
mjr 78:1e00b3fa11af 6154 MMA8451_INT_PIN, cfg.accel.range, cfg.accel.autoCenterTime);
mjr 76:7f5912b6340e 6155
mjr 17:ab3cec0c8bf4 6156 // last accelerometer report, in joystick units (we report the nudge
mjr 17:ab3cec0c8bf4 6157 // acceleration via the joystick x & y axes, per the VP convention)
mjr 17:ab3cec0c8bf4 6158 int x = 0, y = 0;
mjr 17:ab3cec0c8bf4 6159
mjr 48:058ace2aed1d 6160 // initialize the plunger sensor
mjr 35:e959ffba78fd 6161 plungerSensor->init();
mjr 10:976666ffa4ef 6162
mjr 48:058ace2aed1d 6163 // set up the ZB Launch Ball monitor
mjr 48:058ace2aed1d 6164 ZBLaunchBall zbLaunchBall;
mjr 48:058ace2aed1d 6165
mjr 54:fd77a6b2f76c 6166 // enable the peripheral chips
mjr 54:fd77a6b2f76c 6167 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 6168 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 6169 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6170 hc595->enable(true);
mjr 74:822a92bc11d2 6171
mjr 76:7f5912b6340e 6172 // start the LedWiz flash cycle timer
mjr 74:822a92bc11d2 6173 wizCycleTimer.start();
mjr 74:822a92bc11d2 6174
mjr 74:822a92bc11d2 6175 // start the PWM update polling timer
mjr 74:822a92bc11d2 6176 polledPwmTimer.start();
mjr 43:7a6364d82a41 6177
mjr 77:0b96f6867312 6178 // Timer for configuration change reboots
mjr 77:0b96f6867312 6179 ExtTimer saveConfigRebootTimer;
mjr 77:0b96f6867312 6180
mjr 1:d913e0afb2ac 6181 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 6182 // host requests
mjr 0:5acbbe3f4cf4 6183 for (;;)
mjr 0:5acbbe3f4cf4 6184 {
mjr 74:822a92bc11d2 6185 // start the main loop timer for diagnostic data collection
mjr 76:7f5912b6340e 6186 IF_DIAG(mainLoopTimer.reset(); mainLoopTimer.start();)
mjr 74:822a92bc11d2 6187
mjr 48:058ace2aed1d 6188 // Process incoming reports on the joystick interface. The joystick
mjr 48:058ace2aed1d 6189 // "out" (receive) endpoint is used for LedWiz commands and our
mjr 48:058ace2aed1d 6190 // extended protocol commands. Limit processing time to 5ms to
mjr 48:058ace2aed1d 6191 // ensure we don't starve the input side.
mjr 39:b3815a1c3802 6192 LedWizMsg lwm;
mjr 48:058ace2aed1d 6193 Timer lwt;
mjr 48:058ace2aed1d 6194 lwt.start();
mjr 77:0b96f6867312 6195 IF_DIAG(int msgCount = 0;)
mjr 48:058ace2aed1d 6196 while (js.readLedWizMsg(lwm) && lwt.read_us() < 5000)
mjr 74:822a92bc11d2 6197 {
mjr 78:1e00b3fa11af 6198 handleInputMsg(lwm, js, accel);
mjr 74:822a92bc11d2 6199 IF_DIAG(++msgCount;)
mjr 74:822a92bc11d2 6200 }
mjr 74:822a92bc11d2 6201
mjr 74:822a92bc11d2 6202 // collect performance statistics on the message reader, if desired
mjr 74:822a92bc11d2 6203 IF_DIAG(
mjr 74:822a92bc11d2 6204 if (msgCount != 0)
mjr 74:822a92bc11d2 6205 {
mjr 76:7f5912b6340e 6206 mainLoopMsgTime += lwt.read_us();
mjr 74:822a92bc11d2 6207 mainLoopMsgCount++;
mjr 74:822a92bc11d2 6208 }
mjr 74:822a92bc11d2 6209 )
mjr 74:822a92bc11d2 6210
mjr 77:0b96f6867312 6211 // process IR input
mjr 77:0b96f6867312 6212 process_IR(cfg, js);
mjr 77:0b96f6867312 6213
mjr 77:0b96f6867312 6214 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6215 powerStatusUpdate(cfg);
mjr 77:0b96f6867312 6216
mjr 74:822a92bc11d2 6217 // update flashing LedWiz outputs periodically
mjr 74:822a92bc11d2 6218 wizPulse();
mjr 74:822a92bc11d2 6219
mjr 74:822a92bc11d2 6220 // update PWM outputs
mjr 74:822a92bc11d2 6221 pollPwmUpdates();
mjr 77:0b96f6867312 6222
mjr 77:0b96f6867312 6223 // poll the accelerometer
mjr 77:0b96f6867312 6224 accel.poll();
mjr 55:4db125cd11a0 6225
mjr 76:7f5912b6340e 6226 // collect diagnostic statistics, checkpoint 0
mjr 76:7f5912b6340e 6227 IF_DIAG(mainLoopIterCheckpt[0] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6228
mjr 55:4db125cd11a0 6229 // send TLC5940 data updates if applicable
mjr 55:4db125cd11a0 6230 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6231 tlc5940->send();
mjr 1:d913e0afb2ac 6232
mjr 76:7f5912b6340e 6233 // collect diagnostic statistics, checkpoint 1
mjr 76:7f5912b6340e 6234 IF_DIAG(mainLoopIterCheckpt[1] += mainLoopTimer.read_us();)
mjr 77:0b96f6867312 6235
mjr 1:d913e0afb2ac 6236 // check for plunger calibration
mjr 17:ab3cec0c8bf4 6237 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 6238 {
mjr 1:d913e0afb2ac 6239 // check the state
mjr 1:d913e0afb2ac 6240 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6241 {
mjr 1:d913e0afb2ac 6242 case 0:
mjr 1:d913e0afb2ac 6243 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 6244 calBtnTimer.reset();
mjr 1:d913e0afb2ac 6245 calBtnState = 1;
mjr 1:d913e0afb2ac 6246 break;
mjr 1:d913e0afb2ac 6247
mjr 1:d913e0afb2ac 6248 case 1:
mjr 1:d913e0afb2ac 6249 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 6250 // passed, start the hold period
mjr 48:058ace2aed1d 6251 if (calBtnTimer.read_us() > 50000)
mjr 1:d913e0afb2ac 6252 calBtnState = 2;
mjr 1:d913e0afb2ac 6253 break;
mjr 1:d913e0afb2ac 6254
mjr 1:d913e0afb2ac 6255 case 2:
mjr 1:d913e0afb2ac 6256 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 6257 // for the entire hold period, move to calibration mode
mjr 48:058ace2aed1d 6258 if (calBtnTimer.read_us() > 2050000)
mjr 1:d913e0afb2ac 6259 {
mjr 1:d913e0afb2ac 6260 // enter calibration mode
mjr 1:d913e0afb2ac 6261 calBtnState = 3;
mjr 9:fd65b0a94720 6262 calBtnTimer.reset();
mjr 35:e959ffba78fd 6263
mjr 44:b5ac89b9cd5d 6264 // begin the plunger calibration limits
mjr 52:8298b2a73eb2 6265 plungerReader.setCalMode(true);
mjr 1:d913e0afb2ac 6266 }
mjr 1:d913e0afb2ac 6267 break;
mjr 2:c174f9ee414a 6268
mjr 2:c174f9ee414a 6269 case 3:
mjr 9:fd65b0a94720 6270 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 6271 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 6272 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 6273 break;
mjr 0:5acbbe3f4cf4 6274 }
mjr 0:5acbbe3f4cf4 6275 }
mjr 1:d913e0afb2ac 6276 else
mjr 1:d913e0afb2ac 6277 {
mjr 2:c174f9ee414a 6278 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 6279 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 6280 // and save the results to flash.
mjr 2:c174f9ee414a 6281 //
mjr 2:c174f9ee414a 6282 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 6283 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 6284 // mode, it simply cancels the attempt.
mjr 48:058ace2aed1d 6285 if (calBtnState == 3 && calBtnTimer.read_us() > 15000000)
mjr 2:c174f9ee414a 6286 {
mjr 2:c174f9ee414a 6287 // exit calibration mode
mjr 1:d913e0afb2ac 6288 calBtnState = 0;
mjr 52:8298b2a73eb2 6289 plungerReader.setCalMode(false);
mjr 2:c174f9ee414a 6290
mjr 6:cc35eb643e8f 6291 // save the updated configuration
mjr 35:e959ffba78fd 6292 cfg.plunger.cal.calibrated = 1;
mjr 35:e959ffba78fd 6293 saveConfigToFlash();
mjr 2:c174f9ee414a 6294 }
mjr 2:c174f9ee414a 6295 else if (calBtnState != 3)
mjr 2:c174f9ee414a 6296 {
mjr 2:c174f9ee414a 6297 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 6298 calBtnState = 0;
mjr 2:c174f9ee414a 6299 }
mjr 1:d913e0afb2ac 6300 }
mjr 1:d913e0afb2ac 6301
mjr 1:d913e0afb2ac 6302 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 6303 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 6304 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6305 {
mjr 1:d913e0afb2ac 6306 case 2:
mjr 1:d913e0afb2ac 6307 // in the hold period - flash the light
mjr 48:058ace2aed1d 6308 newCalBtnLit = ((calBtnTimer.read_us()/250000) & 1);
mjr 1:d913e0afb2ac 6309 break;
mjr 1:d913e0afb2ac 6310
mjr 1:d913e0afb2ac 6311 case 3:
mjr 1:d913e0afb2ac 6312 // calibration mode - show steady on
mjr 1:d913e0afb2ac 6313 newCalBtnLit = true;
mjr 1:d913e0afb2ac 6314 break;
mjr 1:d913e0afb2ac 6315
mjr 1:d913e0afb2ac 6316 default:
mjr 1:d913e0afb2ac 6317 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 6318 newCalBtnLit = false;
mjr 1:d913e0afb2ac 6319 break;
mjr 1:d913e0afb2ac 6320 }
mjr 3:3514575d4f86 6321
mjr 3:3514575d4f86 6322 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 6323 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 6324 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 6325 {
mjr 1:d913e0afb2ac 6326 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 6327 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 6328 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6329 calBtnLed->write(1);
mjr 38:091e511ce8a0 6330 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 6331 }
mjr 2:c174f9ee414a 6332 else {
mjr 17:ab3cec0c8bf4 6333 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6334 calBtnLed->write(0);
mjr 38:091e511ce8a0 6335 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 6336 }
mjr 1:d913e0afb2ac 6337 }
mjr 35:e959ffba78fd 6338
mjr 76:7f5912b6340e 6339 // collect diagnostic statistics, checkpoint 2
mjr 76:7f5912b6340e 6340 IF_DIAG(mainLoopIterCheckpt[2] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6341
mjr 48:058ace2aed1d 6342 // read the plunger sensor
mjr 48:058ace2aed1d 6343 plungerReader.read();
mjr 48:058ace2aed1d 6344
mjr 76:7f5912b6340e 6345 // collect diagnostic statistics, checkpoint 3
mjr 76:7f5912b6340e 6346 IF_DIAG(mainLoopIterCheckpt[3] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6347
mjr 53:9b2611964afc 6348 // update the ZB Launch Ball status
mjr 53:9b2611964afc 6349 zbLaunchBall.update();
mjr 37:ed52738445fc 6350
mjr 76:7f5912b6340e 6351 // collect diagnostic statistics, checkpoint 4
mjr 76:7f5912b6340e 6352 IF_DIAG(mainLoopIterCheckpt[4] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6353
mjr 53:9b2611964afc 6354 // process button updates
mjr 53:9b2611964afc 6355 processButtons(cfg);
mjr 53:9b2611964afc 6356
mjr 76:7f5912b6340e 6357 // collect diagnostic statistics, checkpoint 5
mjr 76:7f5912b6340e 6358 IF_DIAG(mainLoopIterCheckpt[5] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6359
mjr 38:091e511ce8a0 6360 // send a keyboard report if we have new data
mjr 37:ed52738445fc 6361 if (kbState.changed)
mjr 37:ed52738445fc 6362 {
mjr 38:091e511ce8a0 6363 // send a keyboard report
mjr 37:ed52738445fc 6364 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 6365 kbState.changed = false;
mjr 37:ed52738445fc 6366 }
mjr 38:091e511ce8a0 6367
mjr 38:091e511ce8a0 6368 // likewise for the media controller
mjr 37:ed52738445fc 6369 if (mediaState.changed)
mjr 37:ed52738445fc 6370 {
mjr 38:091e511ce8a0 6371 // send a media report
mjr 37:ed52738445fc 6372 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 6373 mediaState.changed = false;
mjr 37:ed52738445fc 6374 }
mjr 38:091e511ce8a0 6375
mjr 76:7f5912b6340e 6376 // collect diagnostic statistics, checkpoint 6
mjr 76:7f5912b6340e 6377 IF_DIAG(mainLoopIterCheckpt[6] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6378
mjr 38:091e511ce8a0 6379 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 6380 bool jsOK = false;
mjr 55:4db125cd11a0 6381
mjr 55:4db125cd11a0 6382 // figure the current status flags for joystick reports
mjr 77:0b96f6867312 6383 uint16_t statusFlags =
mjr 77:0b96f6867312 6384 cfg.plunger.enabled // 0x01
mjr 77:0b96f6867312 6385 | nightMode // 0x02
mjr 77:0b96f6867312 6386 | ((psu2_state & 0x07) << 2); // 0x04 0x08 0x10
mjr 77:0b96f6867312 6387 if (IRLearningMode != 0)
mjr 77:0b96f6867312 6388 statusFlags |= 0x20;
mjr 17:ab3cec0c8bf4 6389
mjr 50:40015764bbe6 6390 // If it's been long enough since our last USB status report, send
mjr 50:40015764bbe6 6391 // the new report. VP only polls for input in 10ms intervals, so
mjr 50:40015764bbe6 6392 // there's no benefit in sending reports more frequently than this.
mjr 50:40015764bbe6 6393 // More frequent reporting would only add USB I/O overhead.
mjr 50:40015764bbe6 6394 if (cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 17:ab3cec0c8bf4 6395 {
mjr 17:ab3cec0c8bf4 6396 // read the accelerometer
mjr 17:ab3cec0c8bf4 6397 int xa, ya;
mjr 17:ab3cec0c8bf4 6398 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 6399
mjr 17:ab3cec0c8bf4 6400 // confine the results to our joystick axis range
mjr 17:ab3cec0c8bf4 6401 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 17:ab3cec0c8bf4 6402 if (xa > JOYMAX) xa = JOYMAX;
mjr 17:ab3cec0c8bf4 6403 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 17:ab3cec0c8bf4 6404 if (ya > JOYMAX) ya = JOYMAX;
mjr 17:ab3cec0c8bf4 6405
mjr 17:ab3cec0c8bf4 6406 // store the updated accelerometer coordinates
mjr 17:ab3cec0c8bf4 6407 x = xa;
mjr 17:ab3cec0c8bf4 6408 y = ya;
mjr 17:ab3cec0c8bf4 6409
mjr 48:058ace2aed1d 6410 // Report the current plunger position unless the plunger is
mjr 48:058ace2aed1d 6411 // disabled, or the ZB Launch Ball signal is on. In either of
mjr 48:058ace2aed1d 6412 // those cases, just report a constant 0 value. ZB Launch Ball
mjr 48:058ace2aed1d 6413 // temporarily disables mechanical plunger reporting because it
mjr 21:5048e16cc9ef 6414 // tells us that the table has a Launch Ball button instead of
mjr 48:058ace2aed1d 6415 // a traditional plunger, so we don't want to confuse VP with
mjr 48:058ace2aed1d 6416 // regular plunger inputs.
mjr 48:058ace2aed1d 6417 int z = plungerReader.getPosition();
mjr 53:9b2611964afc 6418 int zrep = (!cfg.plunger.enabled || zbLaunchOn ? 0 : z);
mjr 35:e959ffba78fd 6419
mjr 35:e959ffba78fd 6420 // rotate X and Y according to the device orientation in the cabinet
mjr 35:e959ffba78fd 6421 accelRotate(x, y);
mjr 35:e959ffba78fd 6422
mjr 35:e959ffba78fd 6423 // send the joystick report
mjr 53:9b2611964afc 6424 jsOK = js.update(x, y, zrep, jsButtons, statusFlags);
mjr 21:5048e16cc9ef 6425
mjr 17:ab3cec0c8bf4 6426 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 6427 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 6428 }
mjr 21:5048e16cc9ef 6429
mjr 52:8298b2a73eb2 6430 // If we're in sensor status mode, report all pixel exposure values
mjr 52:8298b2a73eb2 6431 if (reportPlungerStat)
mjr 10:976666ffa4ef 6432 {
mjr 17:ab3cec0c8bf4 6433 // send the report
mjr 53:9b2611964afc 6434 plungerSensor->sendStatusReport(js, reportPlungerStatFlags, reportPlungerStatTime);
mjr 17:ab3cec0c8bf4 6435
mjr 10:976666ffa4ef 6436 // we have satisfied this request
mjr 52:8298b2a73eb2 6437 reportPlungerStat = false;
mjr 10:976666ffa4ef 6438 }
mjr 10:976666ffa4ef 6439
mjr 35:e959ffba78fd 6440 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 6441 // periodically for the sake of the Windows config tool.
mjr 77:0b96f6867312 6442 if (!cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 21:5048e16cc9ef 6443 {
mjr 55:4db125cd11a0 6444 jsOK = js.updateStatus(statusFlags);
mjr 38:091e511ce8a0 6445 jsReportTimer.reset();
mjr 38:091e511ce8a0 6446 }
mjr 38:091e511ce8a0 6447
mjr 38:091e511ce8a0 6448 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 6449 if (jsOK)
mjr 38:091e511ce8a0 6450 {
mjr 38:091e511ce8a0 6451 jsOKTimer.reset();
mjr 38:091e511ce8a0 6452 jsOKTimer.start();
mjr 21:5048e16cc9ef 6453 }
mjr 21:5048e16cc9ef 6454
mjr 76:7f5912b6340e 6455 // collect diagnostic statistics, checkpoint 7
mjr 76:7f5912b6340e 6456 IF_DIAG(mainLoopIterCheckpt[7] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6457
mjr 6:cc35eb643e8f 6458 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 6459 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 6460 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 6461 #endif
mjr 6:cc35eb643e8f 6462
mjr 33:d832bcab089e 6463 // check for connection status changes
mjr 54:fd77a6b2f76c 6464 bool newConnected = js.isConnected() && !js.isSleeping();
mjr 33:d832bcab089e 6465 if (newConnected != connected)
mjr 33:d832bcab089e 6466 {
mjr 54:fd77a6b2f76c 6467 // give it a moment to stabilize
mjr 40:cc0d9814522b 6468 connectChangeTimer.start();
mjr 55:4db125cd11a0 6469 if (connectChangeTimer.read_us() > 1000000)
mjr 33:d832bcab089e 6470 {
mjr 33:d832bcab089e 6471 // note the new status
mjr 33:d832bcab089e 6472 connected = newConnected;
mjr 40:cc0d9814522b 6473
mjr 40:cc0d9814522b 6474 // done with the change timer for this round - reset it for next time
mjr 40:cc0d9814522b 6475 connectChangeTimer.stop();
mjr 40:cc0d9814522b 6476 connectChangeTimer.reset();
mjr 33:d832bcab089e 6477
mjr 54:fd77a6b2f76c 6478 // if we're newly disconnected, clean up for PC suspend mode or power off
mjr 54:fd77a6b2f76c 6479 if (!connected)
mjr 40:cc0d9814522b 6480 {
mjr 54:fd77a6b2f76c 6481 // turn off all outputs
mjr 33:d832bcab089e 6482 allOutputsOff();
mjr 40:cc0d9814522b 6483
mjr 40:cc0d9814522b 6484 // The KL25Z runs off of USB power, so we might (depending on the PC
mjr 40:cc0d9814522b 6485 // and OS configuration) continue to receive power even when the main
mjr 40:cc0d9814522b 6486 // PC power supply is turned off, such as in soft-off or suspend/sleep
mjr 40:cc0d9814522b 6487 // mode. Any external output controller chips (TLC5940, 74HC595) might
mjr 40:cc0d9814522b 6488 // be powered from the PC power supply directly rather than from our
mjr 40:cc0d9814522b 6489 // USB power, so they might be powered off even when we're still running.
mjr 40:cc0d9814522b 6490 // To ensure cleaner startup when the power comes back on, globally
mjr 40:cc0d9814522b 6491 // disable the outputs. The global disable signals come from GPIO lines
mjr 40:cc0d9814522b 6492 // that remain powered as long as the KL25Z is powered, so these modes
mjr 40:cc0d9814522b 6493 // will apply smoothly across power state transitions in the external
mjr 40:cc0d9814522b 6494 // hardware. That is, when the external chips are powered up, they'll
mjr 40:cc0d9814522b 6495 // see the global disable signals as stable voltage inputs immediately,
mjr 40:cc0d9814522b 6496 // which will cause them to suppress any output triggering. This ensures
mjr 40:cc0d9814522b 6497 // that we don't fire any solenoids or flash any lights spuriously when
mjr 40:cc0d9814522b 6498 // the power first comes on.
mjr 40:cc0d9814522b 6499 if (tlc5940 != 0)
mjr 40:cc0d9814522b 6500 tlc5940->enable(false);
mjr 40:cc0d9814522b 6501 if (hc595 != 0)
mjr 40:cc0d9814522b 6502 hc595->enable(false);
mjr 40:cc0d9814522b 6503 }
mjr 33:d832bcab089e 6504 }
mjr 33:d832bcab089e 6505 }
mjr 48:058ace2aed1d 6506
mjr 53:9b2611964afc 6507 // if we have a reboot timer pending, check for completion
mjr 77:0b96f6867312 6508 if (saveConfigRebootTimer.isRunning()
mjr 77:0b96f6867312 6509 && saveConfigRebootTimer.read() > saveConfigRebootTime)
mjr 53:9b2611964afc 6510 reboot(js);
mjr 77:0b96f6867312 6511
mjr 77:0b96f6867312 6512 // if a config save is pending, do it now
mjr 77:0b96f6867312 6513 if (saveConfigPending != 0)
mjr 77:0b96f6867312 6514 {
mjr 77:0b96f6867312 6515 // save the configuration
mjr 77:0b96f6867312 6516 saveConfigToFlash();
mjr 77:0b96f6867312 6517
mjr 77:0b96f6867312 6518 // if desired, reboot after the specified delay
mjr 77:0b96f6867312 6519 if (saveConfigPending == SAVE_CONFIG_AND_REBOOT)
mjr 77:0b96f6867312 6520 saveConfigRebootTimer.start();
mjr 77:0b96f6867312 6521
mjr 77:0b96f6867312 6522 // the save is no longer pending
mjr 77:0b96f6867312 6523 saveConfigPending = 0;
mjr 77:0b96f6867312 6524 }
mjr 53:9b2611964afc 6525
mjr 48:058ace2aed1d 6526 // if we're disconnected, initiate a new connection
mjr 51:57eb311faafa 6527 if (!connected)
mjr 48:058ace2aed1d 6528 {
mjr 54:fd77a6b2f76c 6529 // show USB HAL debug events
mjr 54:fd77a6b2f76c 6530 extern void HAL_DEBUG_PRINTEVENTS(const char *prefix);
mjr 54:fd77a6b2f76c 6531 HAL_DEBUG_PRINTEVENTS(">DISC");
mjr 54:fd77a6b2f76c 6532
mjr 54:fd77a6b2f76c 6533 // show immediate diagnostic feedback
mjr 54:fd77a6b2f76c 6534 js.diagFlash();
mjr 54:fd77a6b2f76c 6535
mjr 54:fd77a6b2f76c 6536 // clear any previous diagnostic LED display
mjr 54:fd77a6b2f76c 6537 diagLED(0, 0, 0);
mjr 51:57eb311faafa 6538
mjr 51:57eb311faafa 6539 // set up a timer to monitor the reboot timeout
mjr 70:9f58735a1732 6540 Timer reconnTimeoutTimer;
mjr 70:9f58735a1732 6541 reconnTimeoutTimer.start();
mjr 48:058ace2aed1d 6542
mjr 54:fd77a6b2f76c 6543 // set up a timer for diagnostic displays
mjr 54:fd77a6b2f76c 6544 Timer diagTimer;
mjr 54:fd77a6b2f76c 6545 diagTimer.reset();
mjr 54:fd77a6b2f76c 6546 diagTimer.start();
mjr 74:822a92bc11d2 6547
mjr 74:822a92bc11d2 6548 // turn off the main loop timer while spinning
mjr 74:822a92bc11d2 6549 IF_DIAG(mainLoopTimer.stop();)
mjr 54:fd77a6b2f76c 6550
mjr 54:fd77a6b2f76c 6551 // loop until we get our connection back
mjr 54:fd77a6b2f76c 6552 while (!js.isConnected() || js.isSleeping())
mjr 51:57eb311faafa 6553 {
mjr 54:fd77a6b2f76c 6554 // try to recover the connection
mjr 54:fd77a6b2f76c 6555 js.recoverConnection();
mjr 54:fd77a6b2f76c 6556
mjr 55:4db125cd11a0 6557 // send TLC5940 data if necessary
mjr 55:4db125cd11a0 6558 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6559 tlc5940->send();
mjr 55:4db125cd11a0 6560
mjr 54:fd77a6b2f76c 6561 // show a diagnostic flash every couple of seconds
mjr 54:fd77a6b2f76c 6562 if (diagTimer.read_us() > 2000000)
mjr 51:57eb311faafa 6563 {
mjr 54:fd77a6b2f76c 6564 // flush the USB HAL debug events, if in debug mode
mjr 54:fd77a6b2f76c 6565 HAL_DEBUG_PRINTEVENTS(">NC");
mjr 54:fd77a6b2f76c 6566
mjr 54:fd77a6b2f76c 6567 // show diagnostic feedback
mjr 54:fd77a6b2f76c 6568 js.diagFlash();
mjr 51:57eb311faafa 6569
mjr 51:57eb311faafa 6570 // reset the flash timer
mjr 54:fd77a6b2f76c 6571 diagTimer.reset();
mjr 51:57eb311faafa 6572 }
mjr 51:57eb311faafa 6573
mjr 77:0b96f6867312 6574 // If the disconnect reboot timeout has expired, reboot.
mjr 77:0b96f6867312 6575 // Some PC hosts won't reconnect to a device that's left
mjr 77:0b96f6867312 6576 // plugged in through various events on the PC side, such as
mjr 77:0b96f6867312 6577 // rebooting Windows, cycling power on the PC, or just a lost
mjr 77:0b96f6867312 6578 // USB connection. Rebooting the KL25Z seems to be the most
mjr 77:0b96f6867312 6579 // reliable way to get Windows to notice us again after one
mjr 77:0b96f6867312 6580 // of these events and make it reconnect.
mjr 51:57eb311faafa 6581 if (cfg.disconnectRebootTimeout != 0
mjr 70:9f58735a1732 6582 && reconnTimeoutTimer.read() > cfg.disconnectRebootTimeout)
mjr 54:fd77a6b2f76c 6583 reboot(js, false, 0);
mjr 77:0b96f6867312 6584
mjr 77:0b96f6867312 6585 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6586 powerStatusUpdate(cfg);
mjr 54:fd77a6b2f76c 6587 }
mjr 54:fd77a6b2f76c 6588
mjr 74:822a92bc11d2 6589 // resume the main loop timer
mjr 74:822a92bc11d2 6590 IF_DIAG(mainLoopTimer.start();)
mjr 74:822a92bc11d2 6591
mjr 54:fd77a6b2f76c 6592 // if we made it out of that loop alive, we're connected again!
mjr 54:fd77a6b2f76c 6593 connected = true;
mjr 54:fd77a6b2f76c 6594 HAL_DEBUG_PRINTEVENTS(">C");
mjr 54:fd77a6b2f76c 6595
mjr 54:fd77a6b2f76c 6596 // Enable peripheral chips and update them with current output data
mjr 54:fd77a6b2f76c 6597 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 6598 {
mjr 55:4db125cd11a0 6599 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 6600 tlc5940->update(true);
mjr 54:fd77a6b2f76c 6601 }
mjr 54:fd77a6b2f76c 6602 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6603 {
mjr 55:4db125cd11a0 6604 hc595->enable(true);
mjr 54:fd77a6b2f76c 6605 hc595->update(true);
mjr 51:57eb311faafa 6606 }
mjr 48:058ace2aed1d 6607 }
mjr 43:7a6364d82a41 6608
mjr 6:cc35eb643e8f 6609 // provide a visual status indication on the on-board LED
mjr 48:058ace2aed1d 6610 if (calBtnState < 2 && hbTimer.read_us() > 1000000)
mjr 1:d913e0afb2ac 6611 {
mjr 54:fd77a6b2f76c 6612 if (jsOKTimer.read_us() > 1000000)
mjr 38:091e511ce8a0 6613 {
mjr 39:b3815a1c3802 6614 // USB freeze - show red/yellow.
mjr 40:cc0d9814522b 6615 //
mjr 54:fd77a6b2f76c 6616 // It's been more than a second since we successfully sent a joystick
mjr 54:fd77a6b2f76c 6617 // update message. This must mean that something's wrong on the USB
mjr 54:fd77a6b2f76c 6618 // connection, even though we haven't detected an outright disconnect.
mjr 54:fd77a6b2f76c 6619 // Show a distinctive diagnostic LED pattern when this occurs.
mjr 38:091e511ce8a0 6620 hb = !hb;
mjr 38:091e511ce8a0 6621 diagLED(1, hb, 0);
mjr 54:fd77a6b2f76c 6622
mjr 54:fd77a6b2f76c 6623 // If the reboot-on-disconnect option is in effect, treat this condition
mjr 54:fd77a6b2f76c 6624 // as equivalent to a disconnect, since something is obviously wrong
mjr 54:fd77a6b2f76c 6625 // with the USB connection.
mjr 54:fd77a6b2f76c 6626 if (cfg.disconnectRebootTimeout != 0)
mjr 54:fd77a6b2f76c 6627 {
mjr 54:fd77a6b2f76c 6628 // The reboot timeout is in effect. If we've been incommunicado for
mjr 54:fd77a6b2f76c 6629 // longer than the timeout, reboot. If we haven't reached the time
mjr 54:fd77a6b2f76c 6630 // limit, keep running for now, and leave the OK timer running so
mjr 54:fd77a6b2f76c 6631 // that we can continue to monitor this.
mjr 54:fd77a6b2f76c 6632 if (jsOKTimer.read() > cfg.disconnectRebootTimeout)
mjr 54:fd77a6b2f76c 6633 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 6634 }
mjr 54:fd77a6b2f76c 6635 else
mjr 54:fd77a6b2f76c 6636 {
mjr 54:fd77a6b2f76c 6637 // There's no reboot timer, so just keep running with the diagnostic
mjr 54:fd77a6b2f76c 6638 // pattern displayed. Since we're not waiting for any other timed
mjr 54:fd77a6b2f76c 6639 // conditions in this state, stop the timer so that it doesn't
mjr 54:fd77a6b2f76c 6640 // overflow if this condition persists for a long time.
mjr 54:fd77a6b2f76c 6641 jsOKTimer.stop();
mjr 54:fd77a6b2f76c 6642 }
mjr 38:091e511ce8a0 6643 }
mjr 73:4e8ce0b18915 6644 else if (psu2_state >= 4)
mjr 73:4e8ce0b18915 6645 {
mjr 73:4e8ce0b18915 6646 // We're in the TV timer countdown. Skip the normal heartbeat
mjr 73:4e8ce0b18915 6647 // flashes and show the TV timer flashes instead.
mjr 73:4e8ce0b18915 6648 diagLED(0, 0, 0);
mjr 73:4e8ce0b18915 6649 }
mjr 35:e959ffba78fd 6650 else if (cfg.plunger.enabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 6651 {
mjr 6:cc35eb643e8f 6652 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 6653 hb = !hb;
mjr 38:091e511ce8a0 6654 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 6655 }
mjr 6:cc35eb643e8f 6656 else
mjr 6:cc35eb643e8f 6657 {
mjr 6:cc35eb643e8f 6658 // connected - flash blue/green
mjr 2:c174f9ee414a 6659 hb = !hb;
mjr 38:091e511ce8a0 6660 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 6661 }
mjr 1:d913e0afb2ac 6662
mjr 1:d913e0afb2ac 6663 // reset the heartbeat timer
mjr 1:d913e0afb2ac 6664 hbTimer.reset();
mjr 5:a70c0bce770d 6665 ++hbcnt;
mjr 1:d913e0afb2ac 6666 }
mjr 74:822a92bc11d2 6667
mjr 74:822a92bc11d2 6668 // collect statistics on the main loop time, if desired
mjr 74:822a92bc11d2 6669 IF_DIAG(
mjr 76:7f5912b6340e 6670 mainLoopIterTime += mainLoopTimer.read_us();
mjr 74:822a92bc11d2 6671 mainLoopIterCount++;
mjr 74:822a92bc11d2 6672 )
mjr 1:d913e0afb2ac 6673 }
mjr 0:5acbbe3f4cf4 6674 }