Mike R / Mbed 2 deprecated Pinscape_Controller_V2

Dependencies:   mbed FastIO FastPWM USBDevice

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Fri Mar 17 22:02:08 2017 +0000
Revision:
77:0b96f6867312
Parent:
76:7f5912b6340e
Child:
78:1e00b3fa11af
New memory pool management; keeping old ones as #ifdefs for now for reference.

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 59:94eb9265b6d7 330 // In addition, we can make a much larger overall pool of memory available
mjr 59:94eb9265b6d7 331 // in a custom allocator. The mbed library malloc() seems to have a pool
mjr 59:94eb9265b6d7 332 // of about 3K to work with, even though there's really about 9K of RAM
mjr 59:94eb9265b6d7 333 // left over after counting the static writable data and reserving space
mjr 59:94eb9265b6d7 334 // for a reasonable stack. I haven't looked at the mbed malloc to see why
mjr 59:94eb9265b6d7 335 // they're so stingy, but it appears from empirical testing that we can
mjr 77:0b96f6867312 336 // create a static array up to about 8K before things get crashy.
mjr 77:0b96f6867312 337
mjr 77:0b96f6867312 338
mjr 77:0b96f6867312 339 // halt with a diagnostic display if we run out of memory
mjr 77:0b96f6867312 340 void HaltOutOfMem()
mjr 77:0b96f6867312 341 {
mjr 77:0b96f6867312 342 printf("\r\nOut Of Memory\r\n");
mjr 77:0b96f6867312 343 // halt with the diagnostic display (by looping forever)
mjr 77:0b96f6867312 344 for (;;)
mjr 77:0b96f6867312 345 {
mjr 77:0b96f6867312 346 diagLED(1, 0, 0);
mjr 77:0b96f6867312 347 wait_us(200000);
mjr 77:0b96f6867312 348 diagLED(1, 0, 1);
mjr 77:0b96f6867312 349 wait_us(200000);
mjr 77:0b96f6867312 350 }
mjr 77:0b96f6867312 351 }
mjr 77:0b96f6867312 352
mjr 77:0b96f6867312 353 #if 0//$$$
mjr 77:0b96f6867312 354 // Memory pool. We allocate two blocks at fixed addresses: one for
mjr 77:0b96f6867312 355 // the malloc heap, and one for the native stack.
mjr 73:4e8ce0b18915 356 //
mjr 77:0b96f6867312 357 // We allocate the stack block at the very top of memory. This is what
mjr 77:0b96f6867312 358 // the mbed startup code does anyway, so we don't actually ever move the
mjr 77:0b96f6867312 359 // stack pointer into this area ourselves. The point of this block is
mjr 77:0b96f6867312 360 // to reserve space with the linker, so that it won't put any other static
mjr 77:0b96f6867312 361 // data here in this region.
mjr 73:4e8ce0b18915 362 //
mjr 77:0b96f6867312 363 // The heap block goes just below the stack block. This is a contiguous
mjr 77:0b96f6867312 364 // block of bytes from which we allocate blocks for malloc() and 'operator
mjr 77:0b96f6867312 365 // new' requests.
mjr 73:4e8ce0b18915 366 //
mjr 77:0b96f6867312 367 // WARNING! When adding static data, be sure to check the build statistics
mjr 77:0b96f6867312 368 // to ensure that static data fits in the available RAM. The linker doesn't
mjr 77:0b96f6867312 369 // seem to make such a check on its own, so you might not see an error if
mjr 77:0b96f6867312 370 // added data pushes us past the 16K limit.
mjr 77:0b96f6867312 371
mjr 77:0b96f6867312 372 // KL25Z address of top of RAM (one byte past end of RAM)
mjr 77:0b96f6867312 373 const uint32_t TOP_OF_RAM = 0x20003000UL;
mjr 77:0b96f6867312 374
mjr 77:0b96f6867312 375 // malloc pool size
mjr 77:0b96f6867312 376 const size_t XMALLOC_POOL_SIZE = 8*1024;
mjr 77:0b96f6867312 377
mjr 77:0b96f6867312 378 // stack size
mjr 77:0b96f6867312 379 const size_t XMALLOC_STACK_SIZE = 2*1024;
mjr 77:0b96f6867312 380
mjr 77:0b96f6867312 381 // figure the fixed locations of the malloc pool and stack: the stack goes
mjr 77:0b96f6867312 382 // at the very top of RAM, and the malloc pool goes just below the stack
mjr 77:0b96f6867312 383 const uint32_t XMALLOC_STACK_BASE = TOP_OF_RAM - XMALLOC_STACK_SIZE;
mjr 77:0b96f6867312 384 const uint32_t XMALLOC_POOL_BASE = XMALLOC_STACK_BASE - XMALLOC_POOL_SIZE;
mjr 77:0b96f6867312 385
mjr 77:0b96f6867312 386 // allocate the pools - use __attribute__((at)) to give them fixed addresses
mjr 77:0b96f6867312 387 static char xmalloc_stack[XMALLOC_STACK_SIZE] __attribute__((at(XMALLOC_STACK_BASE)));
mjr 77:0b96f6867312 388 static char xmalloc_pool[XMALLOC_POOL_SIZE] __attribute__((at(XMALLOC_POOL_BASE)));
mjr 77:0b96f6867312 389
mjr 77:0b96f6867312 390 // malloc pool free pointer and space remaining
mjr 73:4e8ce0b18915 391 static char *xmalloc_nxt = xmalloc_pool;
mjr 73:4e8ce0b18915 392 static size_t xmalloc_rem = XMALLOC_POOL_SIZE;
mjr 73:4e8ce0b18915 393
mjr 77:0b96f6867312 394 // allocate from our pool
mjr 48:058ace2aed1d 395 void *xmalloc(size_t siz)
mjr 48:058ace2aed1d 396 {
mjr 59:94eb9265b6d7 397 // align to a 4-byte increment
mjr 59:94eb9265b6d7 398 siz = (siz + 3) & ~3;
mjr 59:94eb9265b6d7 399
mjr 77:0b96f6867312 400 // if we're out of memory, halt with a diagnostic display
mjr 73:4e8ce0b18915 401 if (siz > xmalloc_rem)
mjr 77:0b96f6867312 402 HaltOutOfMem();
mjr 48:058ace2aed1d 403
mjr 59:94eb9265b6d7 404 // get the next free location from the pool to return
mjr 73:4e8ce0b18915 405 char *ret = xmalloc_nxt;
mjr 59:94eb9265b6d7 406
mjr 59:94eb9265b6d7 407 // advance the pool pointer and decrement the remaining size counter
mjr 73:4e8ce0b18915 408 xmalloc_nxt += siz;
mjr 73:4e8ce0b18915 409 xmalloc_rem -= siz;
mjr 59:94eb9265b6d7 410
mjr 59:94eb9265b6d7 411 // return the allocated block
mjr 59:94eb9265b6d7 412 return ret;
mjr 73:4e8ce0b18915 413 };
mjr 77:0b96f6867312 414 #elif 1//$$$
mjr 77:0b96f6867312 415 // For our custom malloc, we take advantage of the known layout of the
mjr 77:0b96f6867312 416 // mbed library memory management. The mbed library puts all of the
mjr 77:0b96f6867312 417 // static read/write data at the low end of RAM; this includes the
mjr 77:0b96f6867312 418 // initialized statics and the "ZI" (zero-initialized) statics. The
mjr 77:0b96f6867312 419 // malloc heap starts just after the last static, growing upwards as
mjr 77:0b96f6867312 420 // memory is allocated. The stack starts at the top of RAM and grows
mjr 77:0b96f6867312 421 // downwards.
mjr 77:0b96f6867312 422 //
mjr 77:0b96f6867312 423 // To figure out where the free memory starts, we simply call the system
mjr 77:0b96f6867312 424 // malloc() to make a dummy allocation the first time we're called, and
mjr 77:0b96f6867312 425 // use the address it returns as the start of our free memory pool. The
mjr 77:0b96f6867312 426 // first malloc() call presumably returns the lowest byte of the pool in
mjr 77:0b96f6867312 427 // the compiler RTL's way of thinking, and from what we know about the
mjr 77:0b96f6867312 428 // mbed heap layout, we know everything above this point should be free,
mjr 77:0b96f6867312 429 // at least until we reach the lowest address used by the stack.
mjr 77:0b96f6867312 430 //
mjr 77:0b96f6867312 431 // The ultimate size of the stack is of course dynamic and unpredictable.
mjr 77:0b96f6867312 432 // In testing, it appears that we currently need a little over 1K. To be
mjr 77:0b96f6867312 433 // conservative, we'll reserve 2K for the stack, by taking it out of the
mjr 77:0b96f6867312 434 // space at top of memory we consider fair game for malloc.
mjr 77:0b96f6867312 435 //
mjr 77:0b96f6867312 436 // Note that we could do this a little more low-level-ly if we wanted.
mjr 77:0b96f6867312 437 // The ARM linker provides a pre-defined extern char[] variable named
mjr 77:0b96f6867312 438 // Image$$RW_IRAM1$$ZI$$Limit, which is always placed just after the
mjr 77:0b96f6867312 439 // last static data variable. In principle, this tells us the start
mjr 77:0b96f6867312 440 // of the available malloc pool. However, in testing, it doesn't seem
mjr 77:0b96f6867312 441 // safe to use this as the start of our malloc pool. I'm not sure why,
mjr 77:0b96f6867312 442 // but probably something in the startup code (either in the C RTL or
mjr 77:0b96f6867312 443 // the mbed library) is allocating from the pool before we get control.
mjr 77:0b96f6867312 444 // So we won't use that approach. Besides, that would tie us even more
mjr 77:0b96f6867312 445 // closely to the ARM compiler. With our malloc() probe approach, we're
mjr 77:0b96f6867312 446 // at least portable to any compiler that uses the same basic memory
mjr 77:0b96f6867312 447 // layout, with the heap above the statics and the stack at top of
mjr 77:0b96f6867312 448 // memory; this isn't universal, but it's very typical.
mjr 77:0b96f6867312 449
mjr 77:0b96f6867312 450 static char *xmalloc_nxt = 0;
mjr 77:0b96f6867312 451 size_t xmalloc_rem = 0;
mjr 77:0b96f6867312 452 void *xmalloc(size_t siz)
mjr 77:0b96f6867312 453 {
mjr 77:0b96f6867312 454 if (xmalloc_nxt == 0)
mjr 77:0b96f6867312 455 {
mjr 77:0b96f6867312 456 xmalloc_nxt = (char *)malloc(4);
mjr 77:0b96f6867312 457 xmalloc_rem = 0x20003000UL - 2*1024 - uint32_t(xmalloc_nxt);
mjr 77:0b96f6867312 458 }
mjr 77:0b96f6867312 459
mjr 77:0b96f6867312 460 siz = (siz + 3) & ~3;
mjr 77:0b96f6867312 461 if (siz > xmalloc_rem)
mjr 77:0b96f6867312 462 HaltOutOfMem();
mjr 77:0b96f6867312 463
mjr 77:0b96f6867312 464 char *ret = xmalloc_nxt;
mjr 77:0b96f6867312 465 xmalloc_nxt += siz;
mjr 77:0b96f6867312 466 xmalloc_rem -= siz;
mjr 77:0b96f6867312 467
mjr 77:0b96f6867312 468 return ret;
mjr 77:0b96f6867312 469 }
mjr 77:0b96f6867312 470 #else //$$$
mjr 77:0b96f6867312 471 extern char Image$$RW_IRAM1$$ZI$$Limit[]; // linker marker for top of ZI region
mjr 77:0b96f6867312 472 static char *xmalloc_nxt = Image$$RW_IRAM1$$ZI$$Limit;
mjr 77:0b96f6867312 473 const uint32_t xmallocMinStack = 2*1024;
mjr 77:0b96f6867312 474 char *const TopOfRAM = (char *)0x20003000UL;
mjr 77:0b96f6867312 475 uint16_t xmalloc_rem = uint16_t(TopOfRAM - Image$$RW_IRAM1$$ZI$$Limit - xmallocMinStack);
mjr 77:0b96f6867312 476 void *xmalloc(size_t siz)
mjr 77:0b96f6867312 477 {
mjr 77:0b96f6867312 478 // align to a 4-byte increment
mjr 77:0b96f6867312 479 siz = (siz + 3) & ~3;
mjr 77:0b96f6867312 480
mjr 77:0b96f6867312 481 // check to ensure we're leaving enough stack free
mjr 77:0b96f6867312 482 if (xmalloc_nxt + siz > TopOfRAM - xmallocMinStack)
mjr 77:0b96f6867312 483 HaltOutOfMem();
mjr 77:0b96f6867312 484
mjr 77:0b96f6867312 485 // get the next free location from the pool to return
mjr 77:0b96f6867312 486 char *ret = xmalloc_nxt;
mjr 77:0b96f6867312 487
mjr 77:0b96f6867312 488 // advance past the allocated memory
mjr 77:0b96f6867312 489 xmalloc_nxt += siz;
mjr 77:0b96f6867312 490 xmalloc_rem -= siz;
mjr 77:0b96f6867312 491
mjr 77:0b96f6867312 492 // return the allocated block
mjr 77:0b96f6867312 493 printf("malloc(%d) -> %lx\r\n", siz, ret);
mjr 77:0b96f6867312 494 return ret;
mjr 77:0b96f6867312 495 }
mjr 77:0b96f6867312 496 #endif//$$$
mjr 48:058ace2aed1d 497
mjr 59:94eb9265b6d7 498 // Overload operator new to call our custom malloc. This ensures that
mjr 59:94eb9265b6d7 499 // all 'new' allocations throughout the program (including library code)
mjr 59:94eb9265b6d7 500 // go through our private allocator.
mjr 48:058ace2aed1d 501 void *operator new(size_t siz) { return xmalloc(siz); }
mjr 48:058ace2aed1d 502 void *operator new[](size_t siz) { return xmalloc(siz); }
mjr 5:a70c0bce770d 503
mjr 59:94eb9265b6d7 504 // Since we don't do bookkeeping to track released memory, 'delete' does
mjr 59:94eb9265b6d7 505 // nothing. In actual testing, this routine appears to never be called.
mjr 59:94eb9265b6d7 506 // If it *is* ever called, it will simply leave the block in place, which
mjr 59:94eb9265b6d7 507 // will make it unavailable for re-use but will otherwise be harmless.
mjr 59:94eb9265b6d7 508 void operator delete(void *ptr) { }
mjr 59:94eb9265b6d7 509
mjr 59:94eb9265b6d7 510
mjr 5:a70c0bce770d 511 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 512 //
mjr 38:091e511ce8a0 513 // Forward declarations
mjr 38:091e511ce8a0 514 //
mjr 38:091e511ce8a0 515 void setNightMode(bool on);
mjr 38:091e511ce8a0 516 void toggleNightMode();
mjr 38:091e511ce8a0 517
mjr 38:091e511ce8a0 518 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 519 // utilities
mjr 17:ab3cec0c8bf4 520
mjr 77:0b96f6867312 521 // int/float point square of a number
mjr 77:0b96f6867312 522 inline int square(int x) { return x*x; }
mjr 26:cb71c4af2912 523 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 524
mjr 26:cb71c4af2912 525 // floating point rounding
mjr 26:cb71c4af2912 526 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 527
mjr 17:ab3cec0c8bf4 528
mjr 33:d832bcab089e 529 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 530 //
mjr 40:cc0d9814522b 531 // Extended verison of Timer class. This adds the ability to interrogate
mjr 40:cc0d9814522b 532 // the running state.
mjr 40:cc0d9814522b 533 //
mjr 77:0b96f6867312 534 class ExtTimer: public Timer
mjr 40:cc0d9814522b 535 {
mjr 40:cc0d9814522b 536 public:
mjr 77:0b96f6867312 537 ExtTimer() : running(false) { }
mjr 40:cc0d9814522b 538
mjr 40:cc0d9814522b 539 void start() { running = true; Timer::start(); }
mjr 40:cc0d9814522b 540 void stop() { running = false; Timer::stop(); }
mjr 40:cc0d9814522b 541
mjr 40:cc0d9814522b 542 bool isRunning() const { return running; }
mjr 40:cc0d9814522b 543
mjr 40:cc0d9814522b 544 private:
mjr 40:cc0d9814522b 545 bool running;
mjr 40:cc0d9814522b 546 };
mjr 40:cc0d9814522b 547
mjr 53:9b2611964afc 548
mjr 53:9b2611964afc 549 // --------------------------------------------------------------------------
mjr 40:cc0d9814522b 550 //
mjr 33:d832bcab089e 551 // USB product version number
mjr 5:a70c0bce770d 552 //
mjr 47:df7a88cd249c 553 const uint16_t USB_VERSION_NO = 0x000A;
mjr 33:d832bcab089e 554
mjr 33:d832bcab089e 555 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 556 //
mjr 6:cc35eb643e8f 557 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 558 //
mjr 6:cc35eb643e8f 559 #define JOYMAX 4096
mjr 6:cc35eb643e8f 560
mjr 9:fd65b0a94720 561
mjr 17:ab3cec0c8bf4 562 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 563 //
mjr 40:cc0d9814522b 564 // Wire protocol value translations. These translate byte values to and
mjr 40:cc0d9814522b 565 // from the USB protocol to local native format.
mjr 35:e959ffba78fd 566 //
mjr 35:e959ffba78fd 567
mjr 35:e959ffba78fd 568 // unsigned 16-bit integer
mjr 35:e959ffba78fd 569 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 570 {
mjr 35:e959ffba78fd 571 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 572 }
mjr 40:cc0d9814522b 573 inline void ui16Wire(uint8_t *b, uint16_t val)
mjr 40:cc0d9814522b 574 {
mjr 40:cc0d9814522b 575 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 576 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 577 }
mjr 35:e959ffba78fd 578
mjr 35:e959ffba78fd 579 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 580 {
mjr 35:e959ffba78fd 581 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 582 }
mjr 40:cc0d9814522b 583 inline void i16Wire(uint8_t *b, int16_t val)
mjr 40:cc0d9814522b 584 {
mjr 40:cc0d9814522b 585 ui16Wire(b, (uint16_t)val);
mjr 40:cc0d9814522b 586 }
mjr 35:e959ffba78fd 587
mjr 35:e959ffba78fd 588 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 589 {
mjr 35:e959ffba78fd 590 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 591 }
mjr 40:cc0d9814522b 592 inline void ui32Wire(uint8_t *b, uint32_t val)
mjr 40:cc0d9814522b 593 {
mjr 40:cc0d9814522b 594 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 595 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 596 b[2] = (uint8_t)((val >> 16) & 0xff);
mjr 40:cc0d9814522b 597 b[3] = (uint8_t)((val >> 24) & 0xff);
mjr 40:cc0d9814522b 598 }
mjr 35:e959ffba78fd 599
mjr 35:e959ffba78fd 600 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 601 {
mjr 35:e959ffba78fd 602 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 603 }
mjr 35:e959ffba78fd 604
mjr 53:9b2611964afc 605 // Convert "wire" (USB) pin codes to/from PinName values.
mjr 53:9b2611964afc 606 //
mjr 53:9b2611964afc 607 // The internal mbed PinName format is
mjr 53:9b2611964afc 608 //
mjr 53:9b2611964afc 609 // ((port) << PORT_SHIFT) | (pin << 2) // MBED FORMAT
mjr 53:9b2611964afc 610 //
mjr 53:9b2611964afc 611 // where 'port' is 0-4 for Port A to Port E, and 'pin' is
mjr 53:9b2611964afc 612 // 0 to 31. E.g., E31 is (4 << PORT_SHIFT) | (31<<2).
mjr 53:9b2611964afc 613 //
mjr 53:9b2611964afc 614 // We remap this to our more compact wire format where each
mjr 53:9b2611964afc 615 // pin name fits in 8 bits:
mjr 53:9b2611964afc 616 //
mjr 53:9b2611964afc 617 // ((port) << 5) | pin) // WIRE FORMAT
mjr 53:9b2611964afc 618 //
mjr 53:9b2611964afc 619 // E.g., E31 is (4 << 5) | 31.
mjr 53:9b2611964afc 620 //
mjr 53:9b2611964afc 621 // Wire code FF corresponds to PinName NC (not connected).
mjr 53:9b2611964afc 622 //
mjr 53:9b2611964afc 623 inline PinName wirePinName(uint8_t c)
mjr 35:e959ffba78fd 624 {
mjr 53:9b2611964afc 625 if (c == 0xFF)
mjr 53:9b2611964afc 626 return NC; // 0xFF -> NC
mjr 53:9b2611964afc 627 else
mjr 53:9b2611964afc 628 return PinName(
mjr 53:9b2611964afc 629 (int(c & 0xE0) << (PORT_SHIFT - 5)) // top three bits are the port
mjr 53:9b2611964afc 630 | (int(c & 0x1F) << 2)); // bottom five bits are pin
mjr 40:cc0d9814522b 631 }
mjr 40:cc0d9814522b 632 inline void pinNameWire(uint8_t *b, PinName n)
mjr 40:cc0d9814522b 633 {
mjr 53:9b2611964afc 634 *b = PINNAME_TO_WIRE(n);
mjr 35:e959ffba78fd 635 }
mjr 35:e959ffba78fd 636
mjr 35:e959ffba78fd 637
mjr 35:e959ffba78fd 638 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 639 //
mjr 38:091e511ce8a0 640 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 641 //
mjr 38:091e511ce8a0 642 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 643 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 644 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 645 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 646 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 647 // SPI capability.
mjr 38:091e511ce8a0 648 //
mjr 38:091e511ce8a0 649 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 650
mjr 73:4e8ce0b18915 651 // Power on timer state for diagnostics. We flash the blue LED when
mjr 77:0b96f6867312 652 // nothing else is going on. State 0-1 = off, 2-3 = on blue. Also
mjr 77:0b96f6867312 653 // show red when transmitting an LED signal, indicated by state 4.
mjr 73:4e8ce0b18915 654 uint8_t powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 655
mjr 38:091e511ce8a0 656 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 657 // on, and -1 is no change (leaves the current setting intact).
mjr 73:4e8ce0b18915 658 static uint8_t diagLEDState = 0;
mjr 38:091e511ce8a0 659 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 660 {
mjr 73:4e8ce0b18915 661 // remember the new state
mjr 73:4e8ce0b18915 662 diagLEDState = r | (g << 1) | (b << 2);
mjr 73:4e8ce0b18915 663
mjr 73:4e8ce0b18915 664 // if turning everything off, use the power timer state instead,
mjr 73:4e8ce0b18915 665 // applying it to the blue LED
mjr 73:4e8ce0b18915 666 if (diagLEDState == 0)
mjr 77:0b96f6867312 667 {
mjr 77:0b96f6867312 668 b = (powerTimerDiagState == 2 || powerTimerDiagState == 3);
mjr 77:0b96f6867312 669 r = (powerTimerDiagState == 4);
mjr 77:0b96f6867312 670 }
mjr 73:4e8ce0b18915 671
mjr 73:4e8ce0b18915 672 // set the new state
mjr 38:091e511ce8a0 673 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 674 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 675 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 676 }
mjr 38:091e511ce8a0 677
mjr 73:4e8ce0b18915 678 // update the LEDs with the current state
mjr 73:4e8ce0b18915 679 void diagLED(void)
mjr 73:4e8ce0b18915 680 {
mjr 73:4e8ce0b18915 681 diagLED(
mjr 73:4e8ce0b18915 682 diagLEDState & 0x01,
mjr 73:4e8ce0b18915 683 (diagLEDState >> 1) & 0x01,
mjr 77:0b96f6867312 684 (diagLEDState >> 2) & 0x01);
mjr 73:4e8ce0b18915 685 }
mjr 73:4e8ce0b18915 686
mjr 38:091e511ce8a0 687 // check an output port assignment to see if it conflicts with
mjr 38:091e511ce8a0 688 // an on-board LED segment
mjr 38:091e511ce8a0 689 struct LedSeg
mjr 38:091e511ce8a0 690 {
mjr 38:091e511ce8a0 691 bool r, g, b;
mjr 38:091e511ce8a0 692 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 693
mjr 38:091e511ce8a0 694 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 695 {
mjr 38:091e511ce8a0 696 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 697 // our on-board LED segments
mjr 38:091e511ce8a0 698 int t = pc.typ;
mjr 38:091e511ce8a0 699 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 38:091e511ce8a0 700 {
mjr 38:091e511ce8a0 701 // it's a GPIO port - check for a matching pin assignment
mjr 38:091e511ce8a0 702 PinName pin = wirePinName(pc.pin);
mjr 38:091e511ce8a0 703 if (pin == LED1)
mjr 38:091e511ce8a0 704 r = true;
mjr 38:091e511ce8a0 705 else if (pin == LED2)
mjr 38:091e511ce8a0 706 g = true;
mjr 38:091e511ce8a0 707 else if (pin == LED3)
mjr 38:091e511ce8a0 708 b = true;
mjr 38:091e511ce8a0 709 }
mjr 38:091e511ce8a0 710 }
mjr 38:091e511ce8a0 711 };
mjr 38:091e511ce8a0 712
mjr 38:091e511ce8a0 713 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 714 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 715 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 716 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 717 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 718 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 719 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 720 {
mjr 38:091e511ce8a0 721 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 722 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 723 LedSeg l;
mjr 38:091e511ce8a0 724 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 725 l.check(cfg.outPort[i]);
mjr 38:091e511ce8a0 726
mjr 38:091e511ce8a0 727 // We now know which segments are taken for LedWiz use and which
mjr 38:091e511ce8a0 728 // are free. Create diagnostic ports for the ones not claimed for
mjr 38:091e511ce8a0 729 // LedWiz use.
mjr 38:091e511ce8a0 730 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 731 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 732 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 733 }
mjr 38:091e511ce8a0 734
mjr 38:091e511ce8a0 735
mjr 38:091e511ce8a0 736 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 737 //
mjr 76:7f5912b6340e 738 // LedWiz emulation
mjr 76:7f5912b6340e 739 //
mjr 76:7f5912b6340e 740
mjr 76:7f5912b6340e 741 // LedWiz output states.
mjr 76:7f5912b6340e 742 //
mjr 76:7f5912b6340e 743 // The LedWiz protocol has two separate control axes for each output.
mjr 76:7f5912b6340e 744 // One axis is its on/off state; the other is its "profile" state, which
mjr 76:7f5912b6340e 745 // is either a fixed brightness or a blinking pattern for the light.
mjr 76:7f5912b6340e 746 // The two axes are independent.
mjr 76:7f5912b6340e 747 //
mjr 76:7f5912b6340e 748 // Even though the original LedWiz protocol can only access 32 ports, we
mjr 76:7f5912b6340e 749 // maintain LedWiz state for every port, even if we have more than 32. Our
mjr 76:7f5912b6340e 750 // extended protocol allows the client to send LedWiz-style messages that
mjr 76:7f5912b6340e 751 // control any set of ports. A replacement LEDWIZ.DLL can make a single
mjr 76:7f5912b6340e 752 // Pinscape unit look like multiple virtual LedWiz units to legacy clients,
mjr 76:7f5912b6340e 753 // allowing them to control all of our ports. The clients will still be
mjr 76:7f5912b6340e 754 // using LedWiz-style states to control the ports, so we need to support
mjr 76:7f5912b6340e 755 // the LedWiz scheme with separate on/off and brightness control per port.
mjr 76:7f5912b6340e 756
mjr 76:7f5912b6340e 757 // On/off state for each LedWiz output
mjr 76:7f5912b6340e 758 static uint8_t *wizOn;
mjr 76:7f5912b6340e 759
mjr 76:7f5912b6340e 760 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 76:7f5912b6340e 761 // for each LedWiz output. If the output was last updated through an
mjr 76:7f5912b6340e 762 // LedWiz protocol message, it will have one of these values:
mjr 76:7f5912b6340e 763 //
mjr 76:7f5912b6340e 764 // 0-48 = fixed brightness 0% to 100%
mjr 76:7f5912b6340e 765 // 49 = fixed brightness 100% (equivalent to 48)
mjr 76:7f5912b6340e 766 // 129 = ramp up / ramp down
mjr 76:7f5912b6340e 767 // 130 = flash on / off
mjr 76:7f5912b6340e 768 // 131 = on / ramp down
mjr 76:7f5912b6340e 769 // 132 = ramp up / on
mjr 5:a70c0bce770d 770 //
mjr 76:7f5912b6340e 771 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 76:7f5912b6340e 772 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 76:7f5912b6340e 773 // it, so we need to accept it for compatibility.)
mjr 76:7f5912b6340e 774 static uint8_t *wizVal;
mjr 76:7f5912b6340e 775
mjr 76:7f5912b6340e 776 // Current actual brightness for each output. This is a simple linear
mjr 76:7f5912b6340e 777 // value on a 0..255 scale. This is EITHER the linear brightness computed
mjr 76:7f5912b6340e 778 // from the LedWiz setting for the port, OR the 0..255 value set explicitly
mjr 76:7f5912b6340e 779 // by the extended protocol:
mjr 76:7f5912b6340e 780 //
mjr 76:7f5912b6340e 781 // - If the last command that updated the port was an extended protocol
mjr 76:7f5912b6340e 782 // SET BRIGHTNESS command, this is the value set by that command. In
mjr 76:7f5912b6340e 783 // addition, wizOn[port] is set to 0 if the brightness is 0, 1 otherwise;
mjr 76:7f5912b6340e 784 // and wizVal[port] is set to the brightness rescaled to the 0..48 range
mjr 76:7f5912b6340e 785 // if the brightness is non-zero.
mjr 76:7f5912b6340e 786 //
mjr 76:7f5912b6340e 787 // - If the last command that updated the port was an LedWiz command
mjr 76:7f5912b6340e 788 // (SBA/PBA/SBX/PBX), this contains the brightness value computed from
mjr 76:7f5912b6340e 789 // the combination of wizOn[port] and wizVal[port]. If wizOn[port] is
mjr 76:7f5912b6340e 790 // zero, this is simply 0, otherwise it's wizVal[port] rescaled to the
mjr 76:7f5912b6340e 791 // 0..255 range.
mjr 26:cb71c4af2912 792 //
mjr 76:7f5912b6340e 793 // - For a port set to wizOn[port]=1 and wizVal[port] in 129..132, this is
mjr 76:7f5912b6340e 794 // also updated continuously to reflect the current flashing brightness
mjr 76:7f5912b6340e 795 // level.
mjr 26:cb71c4af2912 796 //
mjr 76:7f5912b6340e 797 static uint8_t *outLevel;
mjr 76:7f5912b6340e 798
mjr 76:7f5912b6340e 799
mjr 76:7f5912b6340e 800 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 76:7f5912b6340e 801 // rate for lights in blinking states. The LedWiz API doesn't document
mjr 76:7f5912b6340e 802 // what the numbers mean in real time units, but by observation, the
mjr 76:7f5912b6340e 803 // "speed" setting represents the period of the flash cycle in 0.25s
mjr 76:7f5912b6340e 804 // units, so speed 1 = 0.25 period = 4Hz, speed 7 = 1.75s period = 0.57Hz.
mjr 76:7f5912b6340e 805 // The period is the full cycle time of the flash waveform.
mjr 76:7f5912b6340e 806 //
mjr 76:7f5912b6340e 807 // Each bank of 32 lights has its independent own pulse rate, so we need
mjr 76:7f5912b6340e 808 // one entry per bank. Each bank has 32 outputs, so we need a total of
mjr 76:7f5912b6340e 809 // ceil(number_of_physical_outputs/32) entries. Note that we could allocate
mjr 76:7f5912b6340e 810 // this dynamically once we know the number of actual outputs, but the
mjr 76:7f5912b6340e 811 // upper limit is low enough that it's more efficient to use a fixed array
mjr 76:7f5912b6340e 812 // at the maximum size.
mjr 76:7f5912b6340e 813 static const int MAX_LW_BANKS = (MAX_OUT_PORTS+31)/32;
mjr 76:7f5912b6340e 814 static uint8_t wizSpeed[MAX_LW_BANKS];
mjr 29:582472d0bc57 815
mjr 26:cb71c4af2912 816 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 817 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 818 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 819 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 820 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 821
mjr 76:7f5912b6340e 822
mjr 76:7f5912b6340e 823 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 824 //
mjr 76:7f5912b6340e 825 // Output Ports
mjr 76:7f5912b6340e 826 //
mjr 76:7f5912b6340e 827 // There are two way to connect outputs. First, you can use the on-board
mjr 76:7f5912b6340e 828 // GPIO ports to implement device outputs: each LedWiz software port is
mjr 76:7f5912b6340e 829 // connected to a physical GPIO pin on the KL25Z. This has some pretty
mjr 76:7f5912b6340e 830 // strict limits, though. The KL25Z only has 10 PWM channels, so only 10
mjr 76:7f5912b6340e 831 // GPIO LedWiz ports can be made dimmable; the rest are strictly on/off.
mjr 76:7f5912b6340e 832 // The KL25Z also simply doesn't have enough exposed GPIO ports overall to
mjr 76:7f5912b6340e 833 // support all of the features the software supports. The software allows
mjr 76:7f5912b6340e 834 // for up to 128 outputs, 48 button inputs, plunger input (requiring 1-5
mjr 76:7f5912b6340e 835 // GPIO pins), and various other external devices. The KL25Z only exposes
mjr 76:7f5912b6340e 836 // about 50 GPIO pins. So if you want to do everything with GPIO ports,
mjr 76:7f5912b6340e 837 // you have to ration pins among features.
mjr 76:7f5912b6340e 838 //
mjr 76:7f5912b6340e 839 // To overcome some of these limitations, we also provide two types of
mjr 76:7f5912b6340e 840 // peripheral controllers that allow adding many more outputs, using only
mjr 76:7f5912b6340e 841 // a small number of GPIO pins to interface with the peripherals. First,
mjr 76:7f5912b6340e 842 // we support TLC5940 PWM controller chips. Each TLC5940 provides 16 ports
mjr 76:7f5912b6340e 843 // with full PWM, and multiple TLC5940 chips can be daisy-chained. The
mjr 76:7f5912b6340e 844 // chip only requires 5 GPIO pins for the interface, no matter how many
mjr 76:7f5912b6340e 845 // chips are in the chain, so it effectively converts 5 GPIO pins into
mjr 76:7f5912b6340e 846 // almost any number of PWM outputs. Second, we support 74HC595 chips.
mjr 76:7f5912b6340e 847 // These provide only digital outputs, but like the TLC5940 they can be
mjr 76:7f5912b6340e 848 // daisy-chained to provide almost unlimited outputs with a few GPIO pins
mjr 76:7f5912b6340e 849 // to control the whole chain.
mjr 76:7f5912b6340e 850 //
mjr 76:7f5912b6340e 851 // Direct GPIO output ports and peripheral controllers can be mixed and
mjr 76:7f5912b6340e 852 // matched in one system. The assignment of pins to ports and the
mjr 76:7f5912b6340e 853 // configuration of peripheral controllers is all handled in the software
mjr 76:7f5912b6340e 854 // setup, so a physical system can be expanded and updated at any time.
mjr 76:7f5912b6340e 855 //
mjr 76:7f5912b6340e 856 // To handle the diversity of output port types, we start with an abstract
mjr 76:7f5912b6340e 857 // base class for outputs. Each type of physical output interface has a
mjr 76:7f5912b6340e 858 // concrete subclass. During initialization, we create the appropriate
mjr 76:7f5912b6340e 859 // subclass for each software port, mapping it to the assigned GPIO pin
mjr 76:7f5912b6340e 860 // or peripheral port. Most of the rest of the software only cares about
mjr 76:7f5912b6340e 861 // the abstract interface, so once the subclassed port objects are set up,
mjr 76:7f5912b6340e 862 // the rest of the system can control the ports without knowing which types
mjr 76:7f5912b6340e 863 // of physical devices they're connected to.
mjr 76:7f5912b6340e 864
mjr 76:7f5912b6340e 865
mjr 26:cb71c4af2912 866 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 867 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 868 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 869 class LwOut
mjr 6:cc35eb643e8f 870 {
mjr 6:cc35eb643e8f 871 public:
mjr 40:cc0d9814522b 872 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 873 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 874 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 875 };
mjr 26:cb71c4af2912 876
mjr 35:e959ffba78fd 877 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 878 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 879 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 880 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 881 // numbering.
mjr 35:e959ffba78fd 882 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 883 {
mjr 33:d832bcab089e 884 public:
mjr 35:e959ffba78fd 885 LwVirtualOut() { }
mjr 40:cc0d9814522b 886 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 887 };
mjr 26:cb71c4af2912 888
mjr 34:6b981a2afab7 889 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 890 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 891 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 892 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 893 {
mjr 34:6b981a2afab7 894 public:
mjr 34:6b981a2afab7 895 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 896 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 897
mjr 34:6b981a2afab7 898 private:
mjr 53:9b2611964afc 899 // underlying physical output
mjr 34:6b981a2afab7 900 LwOut *out;
mjr 34:6b981a2afab7 901 };
mjr 34:6b981a2afab7 902
mjr 53:9b2611964afc 903 // Global ZB Launch Ball state
mjr 53:9b2611964afc 904 bool zbLaunchOn = false;
mjr 53:9b2611964afc 905
mjr 53:9b2611964afc 906 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 907 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 908 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 909 {
mjr 53:9b2611964afc 910 public:
mjr 53:9b2611964afc 911 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 912 virtual void set(uint8_t val)
mjr 53:9b2611964afc 913 {
mjr 53:9b2611964afc 914 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 915 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 916
mjr 53:9b2611964afc 917 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 918 out->set(val);
mjr 53:9b2611964afc 919 }
mjr 53:9b2611964afc 920
mjr 53:9b2611964afc 921 private:
mjr 53:9b2611964afc 922 // underlying physical or virtual output
mjr 53:9b2611964afc 923 LwOut *out;
mjr 53:9b2611964afc 924 };
mjr 53:9b2611964afc 925
mjr 53:9b2611964afc 926
mjr 40:cc0d9814522b 927 // Gamma correction table for 8-bit input values
mjr 40:cc0d9814522b 928 static const uint8_t gamma[] = {
mjr 40:cc0d9814522b 929 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 930 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 931 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 932 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 933 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 934 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 935 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 936 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 937 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 938 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 939 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 940 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 941 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 942 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 943 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 944 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 945 };
mjr 40:cc0d9814522b 946
mjr 40:cc0d9814522b 947 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 948 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 949 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 950 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 951 {
mjr 40:cc0d9814522b 952 public:
mjr 40:cc0d9814522b 953 LwGammaOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 954 virtual void set(uint8_t val) { out->set(gamma[val]); }
mjr 40:cc0d9814522b 955
mjr 40:cc0d9814522b 956 private:
mjr 40:cc0d9814522b 957 LwOut *out;
mjr 40:cc0d9814522b 958 };
mjr 40:cc0d9814522b 959
mjr 77:0b96f6867312 960 // Global night mode flag. To minimize overhead when reporting
mjr 77:0b96f6867312 961 // the status, we set this to the status report flag bit for
mjr 77:0b96f6867312 962 // night mode, 0x02, when engaged.
mjr 77:0b96f6867312 963 static uint8_t nightMode = 0x00;
mjr 53:9b2611964afc 964
mjr 40:cc0d9814522b 965 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 966 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 967 // mode is engaged.
mjr 40:cc0d9814522b 968 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 969 {
mjr 40:cc0d9814522b 970 public:
mjr 40:cc0d9814522b 971 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 972 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 973
mjr 53:9b2611964afc 974 private:
mjr 53:9b2611964afc 975 LwOut *out;
mjr 53:9b2611964afc 976 };
mjr 53:9b2611964afc 977
mjr 53:9b2611964afc 978 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 979 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 980 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 981 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 982 {
mjr 53:9b2611964afc 983 public:
mjr 53:9b2611964afc 984 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 985 virtual void set(uint8_t)
mjr 53:9b2611964afc 986 {
mjr 53:9b2611964afc 987 // ignore the host value and simply show the current
mjr 53:9b2611964afc 988 // night mode setting
mjr 53:9b2611964afc 989 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 990 }
mjr 40:cc0d9814522b 991
mjr 40:cc0d9814522b 992 private:
mjr 40:cc0d9814522b 993 LwOut *out;
mjr 40:cc0d9814522b 994 };
mjr 40:cc0d9814522b 995
mjr 26:cb71c4af2912 996
mjr 35:e959ffba78fd 997 //
mjr 35:e959ffba78fd 998 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 999 // assignments set in config.h.
mjr 33:d832bcab089e 1000 //
mjr 35:e959ffba78fd 1001 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 1002 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 1003 {
mjr 35:e959ffba78fd 1004 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 1005 {
mjr 53:9b2611964afc 1006 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 1007 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 1008 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 1009 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 1010 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 1011 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 1012 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 1013 }
mjr 35:e959ffba78fd 1014 }
mjr 26:cb71c4af2912 1015
mjr 40:cc0d9814522b 1016 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 1017 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 1018 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 1019 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 1020 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 1021 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 1022 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 1023 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 1024 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 1025 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 1026 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 1027 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 1028 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 1029 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 1030 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 1031 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 1032 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 1033 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 1034 };
mjr 40:cc0d9814522b 1035
mjr 40:cc0d9814522b 1036 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 1037 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 1038 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 1039 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 1040 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 1041 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 1042 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 1043 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 1044 // are always 8 bits.
mjr 40:cc0d9814522b 1045 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 1046 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 1047 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 1048 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 1049 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 1050 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 1051 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 1052 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 1053 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 1054 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 1055 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 1056 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 1057 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 1058 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 1059 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 1060 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 1061 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 1062 };
mjr 40:cc0d9814522b 1063
mjr 26:cb71c4af2912 1064 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 1065 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 1066 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 1067 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 1068 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 1069 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 1070 {
mjr 26:cb71c4af2912 1071 public:
mjr 60:f38da020aa13 1072 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1073 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 1074 {
mjr 26:cb71c4af2912 1075 if (val != prv)
mjr 40:cc0d9814522b 1076 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 1077 }
mjr 60:f38da020aa13 1078 uint8_t idx;
mjr 40:cc0d9814522b 1079 uint8_t prv;
mjr 26:cb71c4af2912 1080 };
mjr 26:cb71c4af2912 1081
mjr 40:cc0d9814522b 1082 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 1083 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 1084 {
mjr 40:cc0d9814522b 1085 public:
mjr 60:f38da020aa13 1086 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1087 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 1088 {
mjr 40:cc0d9814522b 1089 if (val != prv)
mjr 40:cc0d9814522b 1090 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 1091 }
mjr 60:f38da020aa13 1092 uint8_t idx;
mjr 40:cc0d9814522b 1093 uint8_t prv;
mjr 40:cc0d9814522b 1094 };
mjr 40:cc0d9814522b 1095
mjr 40:cc0d9814522b 1096
mjr 33:d832bcab089e 1097
mjr 34:6b981a2afab7 1098 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 1099 // config.h.
mjr 35:e959ffba78fd 1100 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 1101
mjr 35:e959ffba78fd 1102 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 1103 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 1104 {
mjr 35:e959ffba78fd 1105 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 1106 {
mjr 53:9b2611964afc 1107 hc595 = new HC595(
mjr 53:9b2611964afc 1108 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 1109 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 1110 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 1111 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 1112 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 1113 hc595->init();
mjr 35:e959ffba78fd 1114 hc595->update();
mjr 35:e959ffba78fd 1115 }
mjr 35:e959ffba78fd 1116 }
mjr 34:6b981a2afab7 1117
mjr 34:6b981a2afab7 1118 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 1119 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 1120 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 1121 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 1122 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 1123 class Lw595Out: public LwOut
mjr 33:d832bcab089e 1124 {
mjr 33:d832bcab089e 1125 public:
mjr 60:f38da020aa13 1126 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1127 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 1128 {
mjr 34:6b981a2afab7 1129 if (val != prv)
mjr 40:cc0d9814522b 1130 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 1131 }
mjr 60:f38da020aa13 1132 uint8_t idx;
mjr 40:cc0d9814522b 1133 uint8_t prv;
mjr 33:d832bcab089e 1134 };
mjr 33:d832bcab089e 1135
mjr 26:cb71c4af2912 1136
mjr 40:cc0d9814522b 1137
mjr 64:ef7ca92dff36 1138 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 1139 // normalized to 0.0 to 1.0 scale.
mjr 74:822a92bc11d2 1140 static const float dof_to_pwm[] = {
mjr 64:ef7ca92dff36 1141 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 1142 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 1143 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 1144 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 1145 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 1146 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 1147 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 1148 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 1149 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 1150 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 1151 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 1152 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 1153 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 1154 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 1155 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 1156 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 1157 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 1158 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 1159 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 1160 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 1161 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 1162 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 1163 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 1164 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 1165 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 1166 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 1167 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 1168 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 1169 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 1170 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 1171 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 1172 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 1173 };
mjr 26:cb71c4af2912 1174
mjr 64:ef7ca92dff36 1175
mjr 64:ef7ca92dff36 1176 // Conversion table for 8-bit DOF level to pulse width in microseconds,
mjr 64:ef7ca92dff36 1177 // with gamma correction. We could use the layered gamma output on top
mjr 64:ef7ca92dff36 1178 // of the regular LwPwmOut class for this, but we get better precision
mjr 64:ef7ca92dff36 1179 // with a dedicated table, because we apply gamma correction to the
mjr 64:ef7ca92dff36 1180 // 32-bit microsecond values rather than the 8-bit DOF levels.
mjr 64:ef7ca92dff36 1181 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 1182 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 1183 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 1184 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 1185 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 1186 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 1187 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 1188 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 1189 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 1190 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 1191 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 1192 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 1193 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 1194 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 1195 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 1196 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 1197 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 1198 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 1199 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 1200 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 1201 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 1202 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 1203 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 1204 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 1205 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 1206 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 1207 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 1208 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 1209 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 1210 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 1211 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 1212 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 1213 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 1214 };
mjr 64:ef7ca92dff36 1215
mjr 77:0b96f6867312 1216 // Polled-update PWM output list
mjr 74:822a92bc11d2 1217 //
mjr 77:0b96f6867312 1218 // This is a workaround for a KL25Z hardware bug/limitation. The bug (more
mjr 77:0b96f6867312 1219 // about this below) is that we can't write to a PWM output "value" register
mjr 77:0b96f6867312 1220 // more than once per PWM cycle; if we do, outputs after the first are lost.
mjr 77:0b96f6867312 1221 // The value register controls the duty cycle, so it's what you have to write
mjr 77:0b96f6867312 1222 // if you want to update the brightness of an output.
mjr 74:822a92bc11d2 1223 //
mjr 77:0b96f6867312 1224 // Our solution is to simply repeat all PWM updates periodically. If a write
mjr 77:0b96f6867312 1225 // is lost on one cycle, it'll eventually be applied on a subseuqent periodic
mjr 77:0b96f6867312 1226 // update. For low overhead, we do these repeat updates periodically during
mjr 77:0b96f6867312 1227 // the main loop.
mjr 74:822a92bc11d2 1228 //
mjr 77:0b96f6867312 1229 // The mbed library has its own solution to this bug, but it creates a
mjr 77:0b96f6867312 1230 // separate problem of its own. The mbed solution is to write the value
mjr 77:0b96f6867312 1231 // register immediately, and then also reset the "count" register in the
mjr 77:0b96f6867312 1232 // TPM unit containing the output. The count reset truncates the current
mjr 77:0b96f6867312 1233 // PWM cycle, which avoids the hardware problem with more than one write per
mjr 77:0b96f6867312 1234 // cycle. The problem is that the truncated cycle causes visible flicker if
mjr 77:0b96f6867312 1235 // the output is connected to an LED. This is particularly noticeable during
mjr 77:0b96f6867312 1236 // fades, when we're updating the value register repeatedly and rapidly: an
mjr 77:0b96f6867312 1237 // attempt to fade from fully on to fully off causes rapid fluttering and
mjr 77:0b96f6867312 1238 // flashing rather than a smooth brightness fade.
mjr 74:822a92bc11d2 1239 //
mjr 77:0b96f6867312 1240 // The hardware bug is a case of good intentions gone bad. The hardware is
mjr 77:0b96f6867312 1241 // *supposed* to make it easy for software to avoid glitching during PWM
mjr 77:0b96f6867312 1242 // updates, by providing a staging register in front of the real value
mjr 77:0b96f6867312 1243 // register. The software actually writes to the staging register, which
mjr 77:0b96f6867312 1244 // holds updates until the end of the cycle, at which point the hardware
mjr 77:0b96f6867312 1245 // automatically moves the value from the staging register into the real
mjr 77:0b96f6867312 1246 // register. This ensures that the real register is always updated exactly
mjr 77:0b96f6867312 1247 // at a cycle boundary, which in turn ensures that there's no flicker when
mjr 77:0b96f6867312 1248 // values are updated. A great design - except that it doesn't quite work.
mjr 77:0b96f6867312 1249 // The problem is that the staging register actually seems to be implemented
mjr 77:0b96f6867312 1250 // as a one-element FIFO in "stop when full" mode. That is, when you write
mjr 77:0b96f6867312 1251 // the FIFO, it becomes full. When the cycle ends and the hardware reads it
mjr 77:0b96f6867312 1252 // to move the staged value into the real register, the FIFO becomes empty.
mjr 77:0b96f6867312 1253 // But if you try to write the FIFO twice before the hardware reads it and
mjr 77:0b96f6867312 1254 // empties it, the second write fails, leaving the first value in the queue.
mjr 77:0b96f6867312 1255 // There doesn't seem to be any way to clear the FIFO from software, so you
mjr 77:0b96f6867312 1256 // just have to wait for the cycle to end before writing another update.
mjr 77:0b96f6867312 1257 // That more or less defeats the purpose of the staging register, whose whole
mjr 77:0b96f6867312 1258 // point is to free software from worrying about timing considerations with
mjr 77:0b96f6867312 1259 // updates. It frees us of the need to align our timing on cycle boundaries,
mjr 77:0b96f6867312 1260 // but it leaves us with the need to limit writes to once per cycle.
mjr 74:822a92bc11d2 1261 //
mjr 77:0b96f6867312 1262 // So here we have our list of PWM outputs that need to be polled for updates.
mjr 77:0b96f6867312 1263 // The KL25Z hardware only has 10 PWM channels, so we only need a fixed set
mjr 77:0b96f6867312 1264 // of polled items.
mjr 74:822a92bc11d2 1265 static int numPolledPwm;
mjr 74:822a92bc11d2 1266 static class LwPwmOut *polledPwm[10];
mjr 74:822a92bc11d2 1267
mjr 74:822a92bc11d2 1268 // LwOut class for a PWM-capable GPIO port.
mjr 6:cc35eb643e8f 1269 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 1270 {
mjr 6:cc35eb643e8f 1271 public:
mjr 43:7a6364d82a41 1272 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1273 {
mjr 77:0b96f6867312 1274 // add myself to the list of polled outputs for periodic updates
mjr 77:0b96f6867312 1275 if (numPolledPwm < countof(polledPwm))
mjr 74:822a92bc11d2 1276 polledPwm[numPolledPwm++] = this;
mjr 77:0b96f6867312 1277
mjr 77:0b96f6867312 1278 // set the initial value
mjr 77:0b96f6867312 1279 set(initVal);
mjr 43:7a6364d82a41 1280 }
mjr 74:822a92bc11d2 1281
mjr 40:cc0d9814522b 1282 virtual void set(uint8_t val)
mjr 74:822a92bc11d2 1283 {
mjr 77:0b96f6867312 1284 // save the new value
mjr 74:822a92bc11d2 1285 this->val = val;
mjr 77:0b96f6867312 1286
mjr 77:0b96f6867312 1287 // commit it to the hardware
mjr 77:0b96f6867312 1288 commit();
mjr 13:72dda449c3c0 1289 }
mjr 74:822a92bc11d2 1290
mjr 74:822a92bc11d2 1291 // handle periodic update polling
mjr 74:822a92bc11d2 1292 void poll()
mjr 74:822a92bc11d2 1293 {
mjr 77:0b96f6867312 1294 commit();
mjr 74:822a92bc11d2 1295 }
mjr 74:822a92bc11d2 1296
mjr 74:822a92bc11d2 1297 protected:
mjr 77:0b96f6867312 1298 virtual void commit()
mjr 74:822a92bc11d2 1299 {
mjr 74:822a92bc11d2 1300 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1301 p.glitchFreeWrite(dof_to_pwm[val]);
mjr 74:822a92bc11d2 1302 }
mjr 74:822a92bc11d2 1303
mjr 77:0b96f6867312 1304 NewPwmOut p;
mjr 77:0b96f6867312 1305 uint8_t val;
mjr 6:cc35eb643e8f 1306 };
mjr 26:cb71c4af2912 1307
mjr 74:822a92bc11d2 1308 // Gamma corrected PWM GPIO output. This works exactly like the regular
mjr 74:822a92bc11d2 1309 // PWM output, but translates DOF values through the gamma-corrected
mjr 74:822a92bc11d2 1310 // table instead of the regular linear table.
mjr 64:ef7ca92dff36 1311 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1312 {
mjr 64:ef7ca92dff36 1313 public:
mjr 64:ef7ca92dff36 1314 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1315 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1316 {
mjr 64:ef7ca92dff36 1317 }
mjr 74:822a92bc11d2 1318
mjr 74:822a92bc11d2 1319 protected:
mjr 77:0b96f6867312 1320 virtual void commit()
mjr 64:ef7ca92dff36 1321 {
mjr 74:822a92bc11d2 1322 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1323 p.glitchFreeWrite(dof_to_gamma_pwm[val]);
mjr 64:ef7ca92dff36 1324 }
mjr 64:ef7ca92dff36 1325 };
mjr 64:ef7ca92dff36 1326
mjr 74:822a92bc11d2 1327 // poll the PWM outputs
mjr 74:822a92bc11d2 1328 Timer polledPwmTimer;
mjr 76:7f5912b6340e 1329 uint64_t polledPwmTotalTime, polledPwmRunCount;
mjr 74:822a92bc11d2 1330 void pollPwmUpdates()
mjr 74:822a92bc11d2 1331 {
mjr 74:822a92bc11d2 1332 // if it's been at least 25ms since the last update, do another update
mjr 74:822a92bc11d2 1333 if (polledPwmTimer.read_us() >= 25000)
mjr 74:822a92bc11d2 1334 {
mjr 74:822a92bc11d2 1335 // time the run for statistics collection
mjr 74:822a92bc11d2 1336 IF_DIAG(
mjr 74:822a92bc11d2 1337 Timer t;
mjr 74:822a92bc11d2 1338 t.start();
mjr 74:822a92bc11d2 1339 )
mjr 74:822a92bc11d2 1340
mjr 74:822a92bc11d2 1341 // poll each output
mjr 74:822a92bc11d2 1342 for (int i = numPolledPwm ; i > 0 ; )
mjr 74:822a92bc11d2 1343 polledPwm[--i]->poll();
mjr 74:822a92bc11d2 1344
mjr 74:822a92bc11d2 1345 // reset the timer for the next cycle
mjr 74:822a92bc11d2 1346 polledPwmTimer.reset();
mjr 74:822a92bc11d2 1347
mjr 74:822a92bc11d2 1348 // collect statistics
mjr 74:822a92bc11d2 1349 IF_DIAG(
mjr 76:7f5912b6340e 1350 polledPwmTotalTime += t.read_us();
mjr 74:822a92bc11d2 1351 polledPwmRunCount += 1;
mjr 74:822a92bc11d2 1352 )
mjr 74:822a92bc11d2 1353 }
mjr 74:822a92bc11d2 1354 }
mjr 64:ef7ca92dff36 1355
mjr 26:cb71c4af2912 1356 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1357 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1358 {
mjr 6:cc35eb643e8f 1359 public:
mjr 43:7a6364d82a41 1360 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1361 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1362 {
mjr 13:72dda449c3c0 1363 if (val != prv)
mjr 40:cc0d9814522b 1364 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1365 }
mjr 6:cc35eb643e8f 1366 DigitalOut p;
mjr 40:cc0d9814522b 1367 uint8_t prv;
mjr 6:cc35eb643e8f 1368 };
mjr 26:cb71c4af2912 1369
mjr 29:582472d0bc57 1370 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1371 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1372 // port n (0-based).
mjr 35:e959ffba78fd 1373 //
mjr 35:e959ffba78fd 1374 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1375 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1376 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1377 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1378 // 74HC595 ports).
mjr 33:d832bcab089e 1379 static int numOutputs;
mjr 33:d832bcab089e 1380 static LwOut **lwPin;
mjr 33:d832bcab089e 1381
mjr 38:091e511ce8a0 1382 // create a single output pin
mjr 53:9b2611964afc 1383 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1384 {
mjr 38:091e511ce8a0 1385 // get this item's values
mjr 38:091e511ce8a0 1386 int typ = pc.typ;
mjr 38:091e511ce8a0 1387 int pin = pc.pin;
mjr 38:091e511ce8a0 1388 int flags = pc.flags;
mjr 40:cc0d9814522b 1389 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1390 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1391 int gamma = flags & PortFlagGamma;
mjr 38:091e511ce8a0 1392
mjr 38:091e511ce8a0 1393 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1394 LwOut *lwp;
mjr 38:091e511ce8a0 1395 switch (typ)
mjr 38:091e511ce8a0 1396 {
mjr 38:091e511ce8a0 1397 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1398 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1399 if (pin != 0)
mjr 64:ef7ca92dff36 1400 {
mjr 64:ef7ca92dff36 1401 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 1402 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 1403 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 1404 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 1405 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 1406 {
mjr 64:ef7ca92dff36 1407 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 1408 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 1409
mjr 64:ef7ca92dff36 1410 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 1411 gamma = false;
mjr 64:ef7ca92dff36 1412 }
mjr 64:ef7ca92dff36 1413 else
mjr 64:ef7ca92dff36 1414 {
mjr 64:ef7ca92dff36 1415 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 1416 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 1417 }
mjr 64:ef7ca92dff36 1418 }
mjr 48:058ace2aed1d 1419 else
mjr 48:058ace2aed1d 1420 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1421 break;
mjr 38:091e511ce8a0 1422
mjr 38:091e511ce8a0 1423 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1424 // Digital GPIO port
mjr 48:058ace2aed1d 1425 if (pin != 0)
mjr 48:058ace2aed1d 1426 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1427 else
mjr 48:058ace2aed1d 1428 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1429 break;
mjr 38:091e511ce8a0 1430
mjr 38:091e511ce8a0 1431 case PortTypeTLC5940:
mjr 38:091e511ce8a0 1432 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 1433 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 1434 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 1435 {
mjr 40:cc0d9814522b 1436 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 1437 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 1438 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 1439 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 1440 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 1441 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 1442 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 1443 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 1444 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 1445 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 1446 // for this unlikely case.
mjr 40:cc0d9814522b 1447 if (gamma && !activeLow)
mjr 40:cc0d9814522b 1448 {
mjr 40:cc0d9814522b 1449 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 1450 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 1451
mjr 40:cc0d9814522b 1452 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 1453 gamma = false;
mjr 40:cc0d9814522b 1454 }
mjr 40:cc0d9814522b 1455 else
mjr 40:cc0d9814522b 1456 {
mjr 40:cc0d9814522b 1457 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 1458 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 1459 }
mjr 40:cc0d9814522b 1460 }
mjr 38:091e511ce8a0 1461 else
mjr 40:cc0d9814522b 1462 {
mjr 40:cc0d9814522b 1463 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 1464 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 1465 }
mjr 38:091e511ce8a0 1466 break;
mjr 38:091e511ce8a0 1467
mjr 38:091e511ce8a0 1468 case PortType74HC595:
mjr 38:091e511ce8a0 1469 // 74HC595 port (if we don't have an HC595 controller object, or it's not a valid
mjr 38:091e511ce8a0 1470 // output number, create a virtual port)
mjr 38:091e511ce8a0 1471 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 1472 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 1473 else
mjr 38:091e511ce8a0 1474 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1475 break;
mjr 38:091e511ce8a0 1476
mjr 38:091e511ce8a0 1477 case PortTypeVirtual:
mjr 43:7a6364d82a41 1478 case PortTypeDisabled:
mjr 38:091e511ce8a0 1479 default:
mjr 38:091e511ce8a0 1480 // virtual or unknown
mjr 38:091e511ce8a0 1481 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1482 break;
mjr 38:091e511ce8a0 1483 }
mjr 38:091e511ce8a0 1484
mjr 40:cc0d9814522b 1485 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 1486 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 1487 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 1488 if (activeLow)
mjr 38:091e511ce8a0 1489 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 1490
mjr 40:cc0d9814522b 1491 // If it's a noisemaker, layer on a night mode switch. Note that this
mjr 40:cc0d9814522b 1492 // needs to be
mjr 40:cc0d9814522b 1493 if (noisy)
mjr 40:cc0d9814522b 1494 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 1495
mjr 40:cc0d9814522b 1496 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 1497 if (gamma)
mjr 40:cc0d9814522b 1498 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 1499
mjr 53:9b2611964afc 1500 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 1501 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 1502 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 1503 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 1504 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 1505
mjr 53:9b2611964afc 1506 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 1507 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 1508 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 1509
mjr 38:091e511ce8a0 1510 // turn it off initially
mjr 38:091e511ce8a0 1511 lwp->set(0);
mjr 38:091e511ce8a0 1512
mjr 38:091e511ce8a0 1513 // return the pin
mjr 38:091e511ce8a0 1514 return lwp;
mjr 38:091e511ce8a0 1515 }
mjr 38:091e511ce8a0 1516
mjr 6:cc35eb643e8f 1517 // initialize the output pin array
mjr 35:e959ffba78fd 1518 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 1519 {
mjr 35:e959ffba78fd 1520 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 1521 // total number of ports.
mjr 35:e959ffba78fd 1522 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 1523 int i;
mjr 35:e959ffba78fd 1524 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 1525 {
mjr 35:e959ffba78fd 1526 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 1527 {
mjr 35:e959ffba78fd 1528 numOutputs = i;
mjr 34:6b981a2afab7 1529 break;
mjr 34:6b981a2afab7 1530 }
mjr 33:d832bcab089e 1531 }
mjr 33:d832bcab089e 1532
mjr 73:4e8ce0b18915 1533 // allocate the pin array
mjr 73:4e8ce0b18915 1534 lwPin = new LwOut*[numOutputs];
mjr 35:e959ffba78fd 1535
mjr 73:4e8ce0b18915 1536 // Allocate the current brightness array
mjr 73:4e8ce0b18915 1537 outLevel = new uint8_t[numOutputs];
mjr 33:d832bcab089e 1538
mjr 73:4e8ce0b18915 1539 // allocate the LedWiz output state arrays
mjr 73:4e8ce0b18915 1540 wizOn = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 1541 wizVal = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 1542
mjr 73:4e8ce0b18915 1543 // initialize all LedWiz outputs to off and brightness 48
mjr 73:4e8ce0b18915 1544 memset(wizOn, 0, numOutputs);
mjr 73:4e8ce0b18915 1545 memset(wizVal, 48, numOutputs);
mjr 73:4e8ce0b18915 1546
mjr 73:4e8ce0b18915 1547 // set all LedWiz virtual unit flash speeds to 2
mjr 73:4e8ce0b18915 1548 for (i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 1549 wizSpeed[i] = 2;
mjr 33:d832bcab089e 1550
mjr 35:e959ffba78fd 1551 // create the pin interface object for each port
mjr 35:e959ffba78fd 1552 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 1553 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 1554 }
mjr 6:cc35eb643e8f 1555
mjr 76:7f5912b6340e 1556 // Translate an LedWiz brightness level (0..49) to a DOF brightness
mjr 76:7f5912b6340e 1557 // level (0..255). Note that brightness level 49 isn't actually valid,
mjr 76:7f5912b6340e 1558 // according to the LedWiz API documentation, but many clients use it
mjr 76:7f5912b6340e 1559 // anyway, and the real LedWiz accepts it and seems to treat it as
mjr 76:7f5912b6340e 1560 // equivalent to 48.
mjr 40:cc0d9814522b 1561 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 1562 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 1563 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 1564 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 1565 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 1566 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 1567 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 1568 255, 255
mjr 40:cc0d9814522b 1569 };
mjr 40:cc0d9814522b 1570
mjr 76:7f5912b6340e 1571 // Translate a DOF brightness level (0..255) to an LedWiz brightness
mjr 76:7f5912b6340e 1572 // level (1..48)
mjr 76:7f5912b6340e 1573 static const uint8_t dof_to_lw[] = {
mjr 76:7f5912b6340e 1574 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
mjr 76:7f5912b6340e 1575 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
mjr 76:7f5912b6340e 1576 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9,
mjr 76:7f5912b6340e 1577 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12,
mjr 76:7f5912b6340e 1578 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15,
mjr 76:7f5912b6340e 1579 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18,
mjr 76:7f5912b6340e 1580 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21,
mjr 76:7f5912b6340e 1581 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24,
mjr 76:7f5912b6340e 1582 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27,
mjr 76:7f5912b6340e 1583 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30,
mjr 76:7f5912b6340e 1584 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
mjr 76:7f5912b6340e 1585 33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36,
mjr 76:7f5912b6340e 1586 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
mjr 76:7f5912b6340e 1587 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
mjr 76:7f5912b6340e 1588 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45,
mjr 76:7f5912b6340e 1589 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48
mjr 76:7f5912b6340e 1590 };
mjr 76:7f5912b6340e 1591
mjr 74:822a92bc11d2 1592 // LedWiz flash cycle tables. For efficiency, we use a lookup table
mjr 74:822a92bc11d2 1593 // rather than calculating these on the fly. The flash cycles are
mjr 74:822a92bc11d2 1594 // generated by the following formulas, where 'c' is the current
mjr 74:822a92bc11d2 1595 // cycle counter, from 0 to 255:
mjr 74:822a92bc11d2 1596 //
mjr 74:822a92bc11d2 1597 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 1598 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 1599 // mode 131 = on/ramp down = (c < 128 ? 255 : (255-c)*2)
mjr 74:822a92bc11d2 1600 // mode 132 = ramp up/on = (c < 128 ? c*2 : 255)
mjr 74:822a92bc11d2 1601 //
mjr 74:822a92bc11d2 1602 // To look up the current output value for a given mode and a given
mjr 74:822a92bc11d2 1603 // cycle counter 'c', index the table with ((mode-129)*256)+c.
mjr 74:822a92bc11d2 1604 static const uint8_t wizFlashLookup[] = {
mjr 74:822a92bc11d2 1605 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 1606 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
mjr 74:822a92bc11d2 1607 0x21, 0x23, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f,
mjr 74:822a92bc11d2 1608 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f,
mjr 74:822a92bc11d2 1609 0x61, 0x63, 0x65, 0x67, 0x69, 0x6b, 0x6d, 0x6f, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d, 0x7f,
mjr 74:822a92bc11d2 1610 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d, 0x8f, 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9d, 0x9f,
mjr 74:822a92bc11d2 1611 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad, 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, 0xbf,
mjr 74:822a92bc11d2 1612 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
mjr 74:822a92bc11d2 1613 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb, 0xed, 0xef, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0xfd, 0xff,
mjr 74:822a92bc11d2 1614 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 1615 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 1616 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 1617 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 1618 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 1619 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 1620 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 1621 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 1622
mjr 74:822a92bc11d2 1623 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 1624 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1625 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1626 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1627 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1628 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1629 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1630 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1631 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1632 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1633 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1634 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1635 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1636 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1637 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1638 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1639 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1640
mjr 74:822a92bc11d2 1641 // mode 131 = on/ramp down = c < 128 ? 255 : (255 - c)*2
mjr 74:822a92bc11d2 1642 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1643 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1644 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1645 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1646 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1647 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1648 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1649 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1650 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 1651 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 1652 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 1653 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 1654 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 1655 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 1656 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 1657 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 1658
mjr 74:822a92bc11d2 1659 // mode 132 = ramp up/on = c < 128 ? c*2 : 255
mjr 74:822a92bc11d2 1660 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
mjr 74:822a92bc11d2 1661 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
mjr 74:822a92bc11d2 1662 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
mjr 74:822a92bc11d2 1663 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
mjr 74:822a92bc11d2 1664 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
mjr 74:822a92bc11d2 1665 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
mjr 74:822a92bc11d2 1666 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
mjr 74:822a92bc11d2 1667 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
mjr 74:822a92bc11d2 1668 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1669 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1670 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1671 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1672 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1673 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1674 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1675 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
mjr 74:822a92bc11d2 1676 };
mjr 74:822a92bc11d2 1677
mjr 74:822a92bc11d2 1678 // LedWiz flash cycle timer. This runs continuously. On each update,
mjr 74:822a92bc11d2 1679 // we use this to figure out where we are on the cycle for each bank.
mjr 74:822a92bc11d2 1680 Timer wizCycleTimer;
mjr 74:822a92bc11d2 1681
mjr 76:7f5912b6340e 1682 // timing statistics for wizPulse()
mjr 76:7f5912b6340e 1683 uint64_t wizPulseTotalTime, wizPulseRunCount;
mjr 76:7f5912b6340e 1684
mjr 76:7f5912b6340e 1685 // LedWiz flash timer pulse. The main loop calls this on each cycle
mjr 76:7f5912b6340e 1686 // to update outputs using LedWiz flash modes. We do one bank of 32
mjr 76:7f5912b6340e 1687 // outputs on each cycle.
mjr 29:582472d0bc57 1688 static void wizPulse()
mjr 29:582472d0bc57 1689 {
mjr 76:7f5912b6340e 1690 // current bank
mjr 76:7f5912b6340e 1691 static int wizPulseBank = 0;
mjr 76:7f5912b6340e 1692
mjr 76:7f5912b6340e 1693 // start a timer for statistics collection
mjr 76:7f5912b6340e 1694 IF_DIAG(
mjr 76:7f5912b6340e 1695 Timer t;
mjr 76:7f5912b6340e 1696 t.start();
mjr 76:7f5912b6340e 1697 )
mjr 76:7f5912b6340e 1698
mjr 76:7f5912b6340e 1699 // Update the current bank's cycle counter: figure the current
mjr 76:7f5912b6340e 1700 // phase of the LedWiz pulse cycle for this bank.
mjr 76:7f5912b6340e 1701 //
mjr 76:7f5912b6340e 1702 // The LedWiz speed setting gives the flash period in 0.25s units
mjr 76:7f5912b6340e 1703 // (speed 1 is a flash period of .25s, speed 7 is a period of 1.75s).
mjr 76:7f5912b6340e 1704 //
mjr 76:7f5912b6340e 1705 // What we're after here is the "phase", which is to say the point
mjr 76:7f5912b6340e 1706 // in the current cycle. If we assume that the cycle has been running
mjr 76:7f5912b6340e 1707 // continuously since some arbitrary time zero in the past, we can
mjr 76:7f5912b6340e 1708 // figure where we are in the current cycle by dividing the time since
mjr 76:7f5912b6340e 1709 // that zero by the cycle period and taking the remainder. E.g., if
mjr 76:7f5912b6340e 1710 // the cycle time is 5 seconds, and the time since t-zero is 17 seconds,
mjr 76:7f5912b6340e 1711 // we divide 17 by 5 to get a remainder of 2. That says we're 2 seconds
mjr 76:7f5912b6340e 1712 // into the current 5-second cycle, or 2/5 of the way through the
mjr 76:7f5912b6340e 1713 // current cycle.
mjr 76:7f5912b6340e 1714 //
mjr 76:7f5912b6340e 1715 // We do this calculation on every iteration of the main loop, so we
mjr 76:7f5912b6340e 1716 // want it to be very fast. To streamline it, we'll use some tricky
mjr 76:7f5912b6340e 1717 // integer arithmetic. The result will be the same as the straightforward
mjr 76:7f5912b6340e 1718 // remainder and fraction calculation we just explained, but we'll get
mjr 76:7f5912b6340e 1719 // there by less-than-obvious means.
mjr 76:7f5912b6340e 1720 //
mjr 76:7f5912b6340e 1721 // Rather than finding the phase as a continuous quantity or floating
mjr 76:7f5912b6340e 1722 // point number, we'll quantize it. We'll divide each cycle into 256
mjr 76:7f5912b6340e 1723 // time units, or quanta. Each quantum is 1/256 of the cycle length,
mjr 76:7f5912b6340e 1724 // so for a 1-second cycle (LedWiz speed 4), each quantum is 1/256 of
mjr 76:7f5912b6340e 1725 // a second, or about 3.9ms. If we express the time since t-zero in
mjr 76:7f5912b6340e 1726 // these units, the time period of one cycle is exactly 256 units, so
mjr 76:7f5912b6340e 1727 // we can calculate our point in the cycle by taking the remainder of
mjr 76:7f5912b6340e 1728 // the time (in our funny units) divided by 256. The special thing
mjr 76:7f5912b6340e 1729 // about making the cycle time equal to 256 units is that "x % 256"
mjr 76:7f5912b6340e 1730 // is exactly the same as "x & 255", which is a much faster operation
mjr 76:7f5912b6340e 1731 // than division on ARM M0+: this CPU has no hardware DIVIDE operation,
mjr 76:7f5912b6340e 1732 // so an integer division takes about 5us. The bit mask operation, in
mjr 76:7f5912b6340e 1733 // contrast, takes only about 60ns - about 100x faster. 5us doesn't
mjr 76:7f5912b6340e 1734 // sound like much, but we do this on every main loop, so every little
mjr 76:7f5912b6340e 1735 // bit counts.
mjr 76:7f5912b6340e 1736 //
mjr 76:7f5912b6340e 1737 // The snag is that our system timer gives us the elapsed time in
mjr 76:7f5912b6340e 1738 // microseconds. We still need to convert this to our special quanta
mjr 76:7f5912b6340e 1739 // of 256 units per cycle. The straightforward way to do that is by
mjr 76:7f5912b6340e 1740 // dividing by (microseconds per quantum). E.g., for LedWiz speed 4,
mjr 76:7f5912b6340e 1741 // we decided that our quantum was 1/256 of a second, or 3906us, so
mjr 76:7f5912b6340e 1742 // dividing the current system time in microseconds by 3906 will give
mjr 76:7f5912b6340e 1743 // us the time in our quantum units. But now we've just substituted
mjr 76:7f5912b6340e 1744 // one division for another!
mjr 76:7f5912b6340e 1745 //
mjr 76:7f5912b6340e 1746 // This is where our really tricky integer math comes in. Dividing
mjr 76:7f5912b6340e 1747 // by X is the same as multiplying by 1/X. In integer math, 1/3906
mjr 76:7f5912b6340e 1748 // is zero, so that won't work. But we can get around that by doing
mjr 76:7f5912b6340e 1749 // the integer math as "fixed point" arithmetic instead. It's still
mjr 76:7f5912b6340e 1750 // actually carried out as integer operations, but we'll scale our
mjr 76:7f5912b6340e 1751 // integers by a scaling factor, then take out the scaling factor
mjr 76:7f5912b6340e 1752 // later to get the final result. The scaling factor we'll use is
mjr 76:7f5912b6340e 1753 // 2^24. So we're going to calculate (time * 2^24/3906), then divide
mjr 76:7f5912b6340e 1754 // the result by 2^24 to get the final answer. I know it seems like
mjr 76:7f5912b6340e 1755 // we're substituting one division for another yet again, but this
mjr 76:7f5912b6340e 1756 // time's the charm, because dividing by 2^24 is a bit shift operation,
mjr 76:7f5912b6340e 1757 // which is another single-cycle operation on M0+. You might also
mjr 76:7f5912b6340e 1758 // wonder how all these tricks don't cause overflows or underflows
mjr 76:7f5912b6340e 1759 // or what not. Well, the multiply by 2^24/3906 will cause an
mjr 76:7f5912b6340e 1760 // overflow, but we don't care, because the overflow will all be in
mjr 76:7f5912b6340e 1761 // the high-order bits that we're going to discard in the final
mjr 76:7f5912b6340e 1762 // remainder calculation anyway.
mjr 76:7f5912b6340e 1763 //
mjr 76:7f5912b6340e 1764 // Each entry in the array below represents 2^24/N for the corresponding
mjr 76:7f5912b6340e 1765 // LedWiz speed, where N is the number of time quanta per cycle at that
mjr 76:7f5912b6340e 1766 // speed. The time quanta are chosen such that 256 quanta add up to
mjr 76:7f5912b6340e 1767 // approximately (LedWiz speed setting * 0.25s).
mjr 76:7f5912b6340e 1768 //
mjr 76:7f5912b6340e 1769 // Note that the calculation has an implicit bit mask (result & 0xFF)
mjr 76:7f5912b6340e 1770 // to get the final result mod 256. But we don't have to actually
mjr 76:7f5912b6340e 1771 // do that work because we're using 32-bit ints and a 2^24 fixed
mjr 76:7f5912b6340e 1772 // point base (X in the narrative above). The final shift right by
mjr 76:7f5912b6340e 1773 // 24 bits to divide out the base will leave us with only 8 bits in
mjr 76:7f5912b6340e 1774 // the result, since we started with 32.
mjr 76:7f5912b6340e 1775 static const uint32_t inv_us_per_quantum[] = { // indexed by LedWiz speed
mjr 76:7f5912b6340e 1776 0, 17172, 8590, 5726, 4295, 3436, 2863, 2454
mjr 76:7f5912b6340e 1777 };
mjr 76:7f5912b6340e 1778 int counter = ((wizCycleTimer.read_us() * inv_us_per_quantum[wizSpeed[wizPulseBank]]) >> 24);
mjr 76:7f5912b6340e 1779
mjr 76:7f5912b6340e 1780 // get the range of 32 output sin this bank
mjr 76:7f5912b6340e 1781 int fromPort = wizPulseBank*32;
mjr 76:7f5912b6340e 1782 int toPort = fromPort+32;
mjr 76:7f5912b6340e 1783 if (toPort > numOutputs)
mjr 76:7f5912b6340e 1784 toPort = numOutputs;
mjr 76:7f5912b6340e 1785
mjr 76:7f5912b6340e 1786 // update all outputs set to flashing values
mjr 76:7f5912b6340e 1787 for (int i = fromPort ; i < toPort ; ++i)
mjr 73:4e8ce0b18915 1788 {
mjr 76:7f5912b6340e 1789 // Update the port only if the LedWiz SBA switch for the port is on
mjr 76:7f5912b6340e 1790 // (wizOn[i]) AND the port is a PBA flash mode in the range 129..132.
mjr 76:7f5912b6340e 1791 // These modes and only these modes have the high bit (0x80) set, so
mjr 76:7f5912b6340e 1792 // we can test for them simply by testing the high bit.
mjr 76:7f5912b6340e 1793 if (wizOn[i])
mjr 29:582472d0bc57 1794 {
mjr 76:7f5912b6340e 1795 uint8_t val = wizVal[i];
mjr 76:7f5912b6340e 1796 if ((val & 0x80) != 0)
mjr 29:582472d0bc57 1797 {
mjr 76:7f5912b6340e 1798 // ook up the value for the mode at the cycle time
mjr 76:7f5912b6340e 1799 lwPin[i]->set(outLevel[i] = wizFlashLookup[((val-129) << 8) + counter]);
mjr 29:582472d0bc57 1800 }
mjr 29:582472d0bc57 1801 }
mjr 76:7f5912b6340e 1802 }
mjr 76:7f5912b6340e 1803
mjr 34:6b981a2afab7 1804 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 1805 if (hc595 != 0)
mjr 35:e959ffba78fd 1806 hc595->update();
mjr 76:7f5912b6340e 1807
mjr 76:7f5912b6340e 1808 // switch to the next bank
mjr 76:7f5912b6340e 1809 if (++wizPulseBank >= MAX_LW_BANKS)
mjr 76:7f5912b6340e 1810 wizPulseBank = 0;
mjr 76:7f5912b6340e 1811
mjr 76:7f5912b6340e 1812 // collect timing statistics
mjr 76:7f5912b6340e 1813 IF_DIAG(
mjr 76:7f5912b6340e 1814 wizPulseTotalTime += t.read_us();
mjr 76:7f5912b6340e 1815 wizPulseRunCount += 1;
mjr 76:7f5912b6340e 1816 )
mjr 1:d913e0afb2ac 1817 }
mjr 38:091e511ce8a0 1818
mjr 76:7f5912b6340e 1819 // Update a port to reflect its new LedWiz SBA+PBA setting.
mjr 76:7f5912b6340e 1820 static void updateLwPort(int port)
mjr 38:091e511ce8a0 1821 {
mjr 76:7f5912b6340e 1822 // check if the SBA switch is on or off
mjr 76:7f5912b6340e 1823 if (wizOn[port])
mjr 76:7f5912b6340e 1824 {
mjr 76:7f5912b6340e 1825 // It's on. If the port is a valid static brightness level,
mjr 76:7f5912b6340e 1826 // set the output port to match. Otherwise leave it as is:
mjr 76:7f5912b6340e 1827 // if it's a flashing mode, the flash mode pulse will update
mjr 76:7f5912b6340e 1828 // it on the next cycle.
mjr 76:7f5912b6340e 1829 int val = wizVal[port];
mjr 76:7f5912b6340e 1830 if (val <= 49)
mjr 76:7f5912b6340e 1831 lwPin[port]->set(outLevel[port] = lw_to_dof[val]);
mjr 76:7f5912b6340e 1832 }
mjr 76:7f5912b6340e 1833 else
mjr 76:7f5912b6340e 1834 {
mjr 76:7f5912b6340e 1835 // the port is off - set absolute brightness zero
mjr 76:7f5912b6340e 1836 lwPin[port]->set(outLevel[port] = 0);
mjr 76:7f5912b6340e 1837 }
mjr 73:4e8ce0b18915 1838 }
mjr 73:4e8ce0b18915 1839
mjr 73:4e8ce0b18915 1840 // Turn off all outputs and restore everything to the default LedWiz
mjr 73:4e8ce0b18915 1841 // state. This sets outputs #1-32 to LedWiz profile value 48 (full
mjr 73:4e8ce0b18915 1842 // brightness) and switch state Off, sets all extended outputs (#33
mjr 73:4e8ce0b18915 1843 // and above) to zero brightness, and sets the LedWiz flash rate to 2.
mjr 73:4e8ce0b18915 1844 // This effectively restores the power-on conditions.
mjr 73:4e8ce0b18915 1845 //
mjr 73:4e8ce0b18915 1846 void allOutputsOff()
mjr 73:4e8ce0b18915 1847 {
mjr 73:4e8ce0b18915 1848 // reset all LedWiz outputs to OFF/48
mjr 73:4e8ce0b18915 1849 for (int i = 0 ; i < numOutputs ; ++i)
mjr 73:4e8ce0b18915 1850 {
mjr 73:4e8ce0b18915 1851 outLevel[i] = 0;
mjr 73:4e8ce0b18915 1852 wizOn[i] = 0;
mjr 73:4e8ce0b18915 1853 wizVal[i] = 48;
mjr 73:4e8ce0b18915 1854 lwPin[i]->set(0);
mjr 73:4e8ce0b18915 1855 }
mjr 73:4e8ce0b18915 1856
mjr 73:4e8ce0b18915 1857 // restore default LedWiz flash rate
mjr 73:4e8ce0b18915 1858 for (int i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 1859 wizSpeed[i] = 2;
mjr 38:091e511ce8a0 1860
mjr 73:4e8ce0b18915 1861 // flush changes to hc595, if applicable
mjr 38:091e511ce8a0 1862 if (hc595 != 0)
mjr 38:091e511ce8a0 1863 hc595->update();
mjr 38:091e511ce8a0 1864 }
mjr 38:091e511ce8a0 1865
mjr 74:822a92bc11d2 1866 // Cary out an SBA or SBX message. portGroup is 0 for ports 1-32,
mjr 74:822a92bc11d2 1867 // 1 for ports 33-64, etc. Original protocol SBA messages always
mjr 74:822a92bc11d2 1868 // address port group 0; our private SBX extension messages can
mjr 74:822a92bc11d2 1869 // address any port group.
mjr 74:822a92bc11d2 1870 void sba_sbx(int portGroup, const uint8_t *data)
mjr 74:822a92bc11d2 1871 {
mjr 76:7f5912b6340e 1872 // update all on/off states in the group
mjr 74:822a92bc11d2 1873 for (int i = 0, bit = 1, imsg = 1, port = portGroup*32 ;
mjr 74:822a92bc11d2 1874 i < 32 && port < numOutputs ;
mjr 74:822a92bc11d2 1875 ++i, bit <<= 1, ++port)
mjr 74:822a92bc11d2 1876 {
mjr 74:822a92bc11d2 1877 // figure the on/off state bit for this output
mjr 74:822a92bc11d2 1878 if (bit == 0x100) {
mjr 74:822a92bc11d2 1879 bit = 1;
mjr 74:822a92bc11d2 1880 ++imsg;
mjr 74:822a92bc11d2 1881 }
mjr 74:822a92bc11d2 1882
mjr 74:822a92bc11d2 1883 // set the on/off state
mjr 76:7f5912b6340e 1884 bool on = wizOn[port] = ((data[imsg] & bit) != 0);
mjr 76:7f5912b6340e 1885
mjr 76:7f5912b6340e 1886 // set the output port brightness to match the new setting
mjr 76:7f5912b6340e 1887 updateLwPort(port);
mjr 74:822a92bc11d2 1888 }
mjr 74:822a92bc11d2 1889
mjr 74:822a92bc11d2 1890 // set the flash speed for the port group
mjr 74:822a92bc11d2 1891 if (portGroup < countof(wizSpeed))
mjr 74:822a92bc11d2 1892 wizSpeed[portGroup] = (data[5] < 1 ? 1 : data[5] > 7 ? 7 : data[5]);
mjr 74:822a92bc11d2 1893
mjr 76:7f5912b6340e 1894 // update 74HC959 outputs
mjr 76:7f5912b6340e 1895 if (hc595 != 0)
mjr 76:7f5912b6340e 1896 hc595->update();
mjr 74:822a92bc11d2 1897 }
mjr 74:822a92bc11d2 1898
mjr 74:822a92bc11d2 1899 // Carry out a PBA or PBX message.
mjr 74:822a92bc11d2 1900 void pba_pbx(int basePort, const uint8_t *data)
mjr 74:822a92bc11d2 1901 {
mjr 74:822a92bc11d2 1902 // update each wizVal entry from the brightness data
mjr 76:7f5912b6340e 1903 for (int i = 0, port = basePort ; i < 8 && port < numOutputs ; ++i, ++port)
mjr 74:822a92bc11d2 1904 {
mjr 74:822a92bc11d2 1905 // get the value
mjr 74:822a92bc11d2 1906 uint8_t v = data[i];
mjr 74:822a92bc11d2 1907
mjr 74:822a92bc11d2 1908 // Validate it. The legal values are 0..49 for brightness
mjr 74:822a92bc11d2 1909 // levels, and 128..132 for flash modes. Set anything invalid
mjr 74:822a92bc11d2 1910 // to full brightness (48) instead. Note that 49 isn't actually
mjr 74:822a92bc11d2 1911 // a valid documented value, but in practice some clients send
mjr 74:822a92bc11d2 1912 // this to mean 100% brightness, and the real LedWiz treats it
mjr 74:822a92bc11d2 1913 // as such.
mjr 74:822a92bc11d2 1914 if ((v > 49 && v < 129) || v > 132)
mjr 74:822a92bc11d2 1915 v = 48;
mjr 74:822a92bc11d2 1916
mjr 74:822a92bc11d2 1917 // store it
mjr 76:7f5912b6340e 1918 wizVal[port] = v;
mjr 76:7f5912b6340e 1919
mjr 76:7f5912b6340e 1920 // update the port
mjr 76:7f5912b6340e 1921 updateLwPort(port);
mjr 74:822a92bc11d2 1922 }
mjr 74:822a92bc11d2 1923
mjr 76:7f5912b6340e 1924 // update 74HC595 outputs
mjr 76:7f5912b6340e 1925 if (hc595 != 0)
mjr 76:7f5912b6340e 1926 hc595->update();
mjr 74:822a92bc11d2 1927 }
mjr 74:822a92bc11d2 1928
mjr 77:0b96f6867312 1929 // ---------------------------------------------------------------------------
mjr 77:0b96f6867312 1930 //
mjr 77:0b96f6867312 1931 // IR Remote Control transmitter & receiver
mjr 77:0b96f6867312 1932 //
mjr 77:0b96f6867312 1933
mjr 77:0b96f6867312 1934 // receiver
mjr 77:0b96f6867312 1935 IRReceiver *ir_rx;
mjr 77:0b96f6867312 1936
mjr 77:0b96f6867312 1937 // transmitter
mjr 77:0b96f6867312 1938 IRTransmitter *ir_tx;
mjr 77:0b96f6867312 1939
mjr 77:0b96f6867312 1940 // Mapping from IR commands slots in the configuration to "virtual button"
mjr 77:0b96f6867312 1941 // numbers on the IRTransmitter's "virtual remote". To minimize RAM usage,
mjr 77:0b96f6867312 1942 // we only create virtual buttons on the transmitter object for code slots
mjr 77:0b96f6867312 1943 // that are configured for transmission, which includes slots used for TV
mjr 77:0b96f6867312 1944 // ON commands and slots that can be triggered by button presses. This
mjr 77:0b96f6867312 1945 // means that virtual button numbers won't necessarily match the config
mjr 77:0b96f6867312 1946 // slot numbers. This table provides the mapping:
mjr 77:0b96f6867312 1947 // IRConfigSlotToVirtualButton[n] = ir_tx virtual button number for
mjr 77:0b96f6867312 1948 // configuration slot n
mjr 77:0b96f6867312 1949 uint8_t IRConfigSlotToVirtualButton[MAX_IR_CODES];
mjr 77:0b96f6867312 1950 uint8_t IRAdHocSlot;
mjr 77:0b96f6867312 1951
mjr 77:0b96f6867312 1952 // IR mode timer. In normal mode, this is the time since the last
mjr 77:0b96f6867312 1953 // command received; we use this to handle commands with timed effects,
mjr 77:0b96f6867312 1954 // such as sending a key to the PC. In learning mode, this is the time
mjr 77:0b96f6867312 1955 // since we activated learning mode, which we use to automatically end
mjr 77:0b96f6867312 1956 // learning mode if a decodable command isn't received within a reasonable
mjr 77:0b96f6867312 1957 // amount of time.
mjr 77:0b96f6867312 1958 Timer IRTimer;
mjr 77:0b96f6867312 1959
mjr 77:0b96f6867312 1960 // IR Learning Mode. The PC enters learning mode via special function 65 12.
mjr 77:0b96f6867312 1961 // The states are:
mjr 77:0b96f6867312 1962 //
mjr 77:0b96f6867312 1963 // 0 -> normal operation (not in learning mode)
mjr 77:0b96f6867312 1964 // 1 -> learning mode; reading raw codes, no command read yet
mjr 77:0b96f6867312 1965 // 2 -> learning mode; command received, awaiting auto-repeat
mjr 77:0b96f6867312 1966 // 3 -> learning mode; done, command and repeat mode decoded
mjr 77:0b96f6867312 1967 //
mjr 77:0b96f6867312 1968 // When we enter learning mode, we reset IRTimer to keep track of how long
mjr 77:0b96f6867312 1969 // we've been in the mode. This allows the mode to time out if no code is
mjr 77:0b96f6867312 1970 // received within a reasonable time.
mjr 77:0b96f6867312 1971 uint8_t IRLearningMode = 0;
mjr 77:0b96f6867312 1972
mjr 77:0b96f6867312 1973 // Learning mode command received. This stores the first decoded command
mjr 77:0b96f6867312 1974 // when in learning mode. For some protocols, we can't just report the
mjr 77:0b96f6867312 1975 // first command we receive, because we need to wait for an auto-repeat to
mjr 77:0b96f6867312 1976 // determine what format the remote uses for repeats. This stores the first
mjr 77:0b96f6867312 1977 // command while we await a repeat. This is necessary for protocols that
mjr 77:0b96f6867312 1978 // have "dittos", since some remotes for such protocols use the dittos and
mjr 77:0b96f6867312 1979 // some don't; the only way to find out is to read a repeat code and see if
mjr 77:0b96f6867312 1980 // it's a ditto or just a repeat of the full code.
mjr 77:0b96f6867312 1981 IRCommand learnedIRCode;
mjr 77:0b96f6867312 1982
mjr 77:0b96f6867312 1983 // IR comkmand received, as a config slot index, 1..MAX_IR_CODES.
mjr 77:0b96f6867312 1984 // When we receive a command that matches one of our programmed commands,
mjr 77:0b96f6867312 1985 // we note the slot here. We also reset the IR timer so that we know how
mjr 77:0b96f6867312 1986 // long it's been since the command came in. This lets us handle commands
mjr 77:0b96f6867312 1987 // with timed effects, such as PC key input. Note that this is a 1-based
mjr 77:0b96f6867312 1988 // index; 0 represents no command.
mjr 77:0b96f6867312 1989 uint8_t IRCommandIn = 0;
mjr 77:0b96f6867312 1990
mjr 77:0b96f6867312 1991 // "Toggle bit" of last command. Some IR protocols have a toggle bit
mjr 77:0b96f6867312 1992 // that distinguishes an auto-repeating key from a key being pressed
mjr 77:0b96f6867312 1993 // several times in a row. This records the toggle bit of the last
mjr 77:0b96f6867312 1994 // command we received.
mjr 77:0b96f6867312 1995 uint8_t lastIRToggle = 0;
mjr 77:0b96f6867312 1996
mjr 77:0b96f6867312 1997 // Are we in a gap between successive key presses? When we detect that a
mjr 77:0b96f6867312 1998 // key is being pressed multiple times rather than auto-repeated (which we
mjr 77:0b96f6867312 1999 // can detect via a toggle bit in some protocols), we'll briefly stop sending
mjr 77:0b96f6867312 2000 // the associated key to the PC, so that the PC likewise recognizes the
mjr 77:0b96f6867312 2001 // distinct key press.
mjr 77:0b96f6867312 2002 uint8_t IRKeyGap = false;
mjr 77:0b96f6867312 2003
mjr 77:0b96f6867312 2004 // initialize
mjr 77:0b96f6867312 2005 void init_IR(Config &cfg, bool &kbKeys)
mjr 77:0b96f6867312 2006 {
mjr 77:0b96f6867312 2007 PinName pin;
mjr 77:0b96f6867312 2008
mjr 77:0b96f6867312 2009 // start the IR timer
mjr 77:0b96f6867312 2010 IRTimer.start();
mjr 77:0b96f6867312 2011
mjr 77:0b96f6867312 2012 // if there's a transmitter, set it up
mjr 77:0b96f6867312 2013 if ((pin = wirePinName(cfg.IR.emitter)) != NC)
mjr 77:0b96f6867312 2014 {
mjr 77:0b96f6867312 2015 // no virtual buttons yet
mjr 77:0b96f6867312 2016 int nVirtualButtons = 0;
mjr 77:0b96f6867312 2017 memset(IRConfigSlotToVirtualButton, 0xFF, sizeof(IRConfigSlotToVirtualButton));
mjr 77:0b96f6867312 2018
mjr 77:0b96f6867312 2019 // assign virtual buttons slots for TV ON codes
mjr 77:0b96f6867312 2020 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2021 {
mjr 77:0b96f6867312 2022 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 2023 IRConfigSlotToVirtualButton[i] = nVirtualButtons++;
mjr 77:0b96f6867312 2024 }
mjr 77:0b96f6867312 2025
mjr 77:0b96f6867312 2026 // assign virtual buttons for codes that can be triggered by
mjr 77:0b96f6867312 2027 // real button inputs
mjr 77:0b96f6867312 2028 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 77:0b96f6867312 2029 {
mjr 77:0b96f6867312 2030 // get the button
mjr 77:0b96f6867312 2031 ButtonCfg &b = cfg.button[i];
mjr 77:0b96f6867312 2032
mjr 77:0b96f6867312 2033 // check the unshifted button
mjr 77:0b96f6867312 2034 int c = b.IRCommand - 1;
mjr 77:0b96f6867312 2035 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2036 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2037 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2038
mjr 77:0b96f6867312 2039 // check the shifted button
mjr 77:0b96f6867312 2040 c = b.IRCommand2 - 1;
mjr 77:0b96f6867312 2041 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2042 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2043 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2044 }
mjr 77:0b96f6867312 2045
mjr 77:0b96f6867312 2046 // allocate an additional virtual button for transmitting ad hoc
mjr 77:0b96f6867312 2047 // codes, such as for the "send code" USB API function
mjr 77:0b96f6867312 2048 IRAdHocSlot = nVirtualButtons++;
mjr 77:0b96f6867312 2049
mjr 77:0b96f6867312 2050 // create the transmitter
mjr 77:0b96f6867312 2051 ir_tx = new IRTransmitter(pin, nVirtualButtons);
mjr 77:0b96f6867312 2052
mjr 77:0b96f6867312 2053 // program the commands into the virtual button slots
mjr 77:0b96f6867312 2054 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2055 {
mjr 77:0b96f6867312 2056 // if this slot is assigned to a virtual button, program it
mjr 77:0b96f6867312 2057 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 2058 if (vb != 0xFF)
mjr 77:0b96f6867312 2059 {
mjr 77:0b96f6867312 2060 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2061 uint64_t code = cb.code.lo | (uint64_t(cb.code.hi) << 32);
mjr 77:0b96f6867312 2062 bool dittos = (cb.flags & IRFlagDittos) != 0;
mjr 77:0b96f6867312 2063 ir_tx->programButton(vb, cb.protocol, dittos, code);
mjr 77:0b96f6867312 2064 }
mjr 77:0b96f6867312 2065 }
mjr 77:0b96f6867312 2066 }
mjr 77:0b96f6867312 2067
mjr 77:0b96f6867312 2068 // if there's a receiver, set it up
mjr 77:0b96f6867312 2069 if ((pin = wirePinName(cfg.IR.sensor)) != NC)
mjr 77:0b96f6867312 2070 {
mjr 77:0b96f6867312 2071 // create the receiver
mjr 77:0b96f6867312 2072 ir_rx = new IRReceiver(pin, 32);
mjr 77:0b96f6867312 2073
mjr 77:0b96f6867312 2074 // connect the transmitter (if any) to the receiver, so that
mjr 77:0b96f6867312 2075 // the receiver can suppress reception of our own transmissions
mjr 77:0b96f6867312 2076 ir_rx->setTransmitter(ir_tx);
mjr 77:0b96f6867312 2077
mjr 77:0b96f6867312 2078 // enable it
mjr 77:0b96f6867312 2079 ir_rx->enable();
mjr 77:0b96f6867312 2080
mjr 77:0b96f6867312 2081 // Check the IR command slots to see if any slots are configured
mjr 77:0b96f6867312 2082 // to send a keyboard key on receiving an IR command. If any are,
mjr 77:0b96f6867312 2083 // tell the caller that we need a USB keyboard interface.
mjr 77:0b96f6867312 2084 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2085 {
mjr 77:0b96f6867312 2086 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2087 if (cb.protocol != 0
mjr 77:0b96f6867312 2088 && (cb.keytype == BtnTypeKey || cb.keytype == BtnTypeMedia))
mjr 77:0b96f6867312 2089 {
mjr 77:0b96f6867312 2090 kbKeys = true;
mjr 77:0b96f6867312 2091 break;
mjr 77:0b96f6867312 2092 }
mjr 77:0b96f6867312 2093 }
mjr 77:0b96f6867312 2094 }
mjr 77:0b96f6867312 2095 }
mjr 77:0b96f6867312 2096
mjr 77:0b96f6867312 2097 // Press or release a button with an assigned IR function. 'cmd'
mjr 77:0b96f6867312 2098 // is the command slot number (1..MAX_IR_CODES) assigned to the button.
mjr 77:0b96f6867312 2099 void IR_buttonChange(uint8_t cmd, bool pressed)
mjr 77:0b96f6867312 2100 {
mjr 77:0b96f6867312 2101 // only proceed if there's an IR transmitter attached
mjr 77:0b96f6867312 2102 if (ir_tx != 0)
mjr 77:0b96f6867312 2103 {
mjr 77:0b96f6867312 2104 // adjust the command slot to a zero-based index
mjr 77:0b96f6867312 2105 int slot = cmd - 1;
mjr 77:0b96f6867312 2106
mjr 77:0b96f6867312 2107 // press or release the virtual button
mjr 77:0b96f6867312 2108 ir_tx->pushButton(IRConfigSlotToVirtualButton[slot], pressed);
mjr 77:0b96f6867312 2109 }
mjr 77:0b96f6867312 2110 }
mjr 77:0b96f6867312 2111
mjr 77:0b96f6867312 2112 // Process IR input
mjr 77:0b96f6867312 2113 void process_IR(Config &cfg, USBJoystick &js)
mjr 77:0b96f6867312 2114 {
mjr 77:0b96f6867312 2115 // if there's no IR receiver attached, there's nothing to do
mjr 77:0b96f6867312 2116 if (ir_rx == 0)
mjr 77:0b96f6867312 2117 return;
mjr 77:0b96f6867312 2118
mjr 77:0b96f6867312 2119 // Time out any received command
mjr 77:0b96f6867312 2120 if (IRCommandIn != 0)
mjr 77:0b96f6867312 2121 {
mjr 77:0b96f6867312 2122 // Time out inter-key gap mode after 30ms; time out all
mjr 77:0b96f6867312 2123 // commands after 100ms.
mjr 77:0b96f6867312 2124 uint32_t t = IRTimer.read_us();
mjr 77:0b96f6867312 2125 if (t > 100000)
mjr 77:0b96f6867312 2126 IRCommandIn = 0;
mjr 77:0b96f6867312 2127 else if (t > 30000)
mjr 77:0b96f6867312 2128 IRKeyGap = false;
mjr 77:0b96f6867312 2129 }
mjr 77:0b96f6867312 2130
mjr 77:0b96f6867312 2131 // Check if we're in learning mode
mjr 77:0b96f6867312 2132 if (IRLearningMode != 0)
mjr 77:0b96f6867312 2133 {
mjr 77:0b96f6867312 2134 // Learning mode. Read raw inputs from the IR sensor and
mjr 77:0b96f6867312 2135 // forward them to the PC via USB reports, up to the report
mjr 77:0b96f6867312 2136 // limit.
mjr 77:0b96f6867312 2137 const int nmax = USBJoystick::maxRawIR;
mjr 77:0b96f6867312 2138 uint16_t raw[nmax];
mjr 77:0b96f6867312 2139 int n;
mjr 77:0b96f6867312 2140 for (n = 0 ; n < nmax && ir_rx->processOne(raw[n]) ; ++n) ;
mjr 77:0b96f6867312 2141
mjr 77:0b96f6867312 2142 // if we read any raw samples, report them
mjr 77:0b96f6867312 2143 if (n != 0)
mjr 77:0b96f6867312 2144 js.reportRawIR(n, raw);
mjr 77:0b96f6867312 2145
mjr 77:0b96f6867312 2146 // check for a command
mjr 77:0b96f6867312 2147 IRCommand c;
mjr 77:0b96f6867312 2148 if (ir_rx->readCommand(c))
mjr 77:0b96f6867312 2149 {
mjr 77:0b96f6867312 2150 // check the current learning state
mjr 77:0b96f6867312 2151 switch (IRLearningMode)
mjr 77:0b96f6867312 2152 {
mjr 77:0b96f6867312 2153 case 1:
mjr 77:0b96f6867312 2154 // Initial state, waiting for the first decoded command.
mjr 77:0b96f6867312 2155 // This is it.
mjr 77:0b96f6867312 2156 learnedIRCode = c;
mjr 77:0b96f6867312 2157
mjr 77:0b96f6867312 2158 // Check if we need additional information. If the
mjr 77:0b96f6867312 2159 // protocol supports dittos, we have to wait for a repeat
mjr 77:0b96f6867312 2160 // to see if the remote actually uses the dittos, since
mjr 77:0b96f6867312 2161 // some implementations of such protocols use the dittos
mjr 77:0b96f6867312 2162 // while others just send repeated full codes. Otherwise,
mjr 77:0b96f6867312 2163 // all we need is the initial code, so we're done.
mjr 77:0b96f6867312 2164 IRLearningMode = (c.hasDittos ? 2 : 3);
mjr 77:0b96f6867312 2165 break;
mjr 77:0b96f6867312 2166
mjr 77:0b96f6867312 2167 case 2:
mjr 77:0b96f6867312 2168 // Code received, awaiting auto-repeat information. If
mjr 77:0b96f6867312 2169 // the protocol has dittos, check to see if we got a ditto:
mjr 77:0b96f6867312 2170 //
mjr 77:0b96f6867312 2171 // - If we received a ditto in the same protocol as the
mjr 77:0b96f6867312 2172 // prior command, the remote uses dittos.
mjr 77:0b96f6867312 2173 //
mjr 77:0b96f6867312 2174 // - If we received a repeat of the prior command (not a
mjr 77:0b96f6867312 2175 // ditto, but a repeat of the full code), the remote
mjr 77:0b96f6867312 2176 // doesn't use dittos even though the protocol supports
mjr 77:0b96f6867312 2177 // them.
mjr 77:0b96f6867312 2178 //
mjr 77:0b96f6867312 2179 // - Otherwise, it's not an auto-repeat at all, so we
mjr 77:0b96f6867312 2180 // can't decide one way or the other on dittos: start
mjr 77:0b96f6867312 2181 // over.
mjr 77:0b96f6867312 2182 if (c.proId == learnedIRCode.proId
mjr 77:0b96f6867312 2183 && c.hasDittos
mjr 77:0b96f6867312 2184 && c.ditto)
mjr 77:0b96f6867312 2185 {
mjr 77:0b96f6867312 2186 // success - the remote uses dittos
mjr 77:0b96f6867312 2187 IRLearningMode = 3;
mjr 77:0b96f6867312 2188 }
mjr 77:0b96f6867312 2189 else if (c.proId == learnedIRCode.proId
mjr 77:0b96f6867312 2190 && c.hasDittos
mjr 77:0b96f6867312 2191 && !c.ditto
mjr 77:0b96f6867312 2192 && c.code == learnedIRCode.code)
mjr 77:0b96f6867312 2193 {
mjr 77:0b96f6867312 2194 // success - it's a repeat of the last code, so
mjr 77:0b96f6867312 2195 // the remote doesn't use dittos even though the
mjr 77:0b96f6867312 2196 // protocol supports them
mjr 77:0b96f6867312 2197 learnedIRCode.hasDittos = false;
mjr 77:0b96f6867312 2198 IRLearningMode = 3;
mjr 77:0b96f6867312 2199 }
mjr 77:0b96f6867312 2200 else
mjr 77:0b96f6867312 2201 {
mjr 77:0b96f6867312 2202 // It's not a ditto and not a full repeat of the
mjr 77:0b96f6867312 2203 // last code, so it's either a new key, or some kind
mjr 77:0b96f6867312 2204 // of multi-code key encoding that we don't recognize.
mjr 77:0b96f6867312 2205 // We can't use this code, so start over.
mjr 77:0b96f6867312 2206 IRLearningMode = 1;
mjr 77:0b96f6867312 2207 }
mjr 77:0b96f6867312 2208 break;
mjr 77:0b96f6867312 2209 }
mjr 77:0b96f6867312 2210
mjr 77:0b96f6867312 2211 // If we ended in state 3, we've successfully decoded
mjr 77:0b96f6867312 2212 // the transmission. Report the decoded data and terminate
mjr 77:0b96f6867312 2213 // learning mode.
mjr 77:0b96f6867312 2214 if (IRLearningMode == 3)
mjr 77:0b96f6867312 2215 {
mjr 77:0b96f6867312 2216 // figure the flags:
mjr 77:0b96f6867312 2217 // 0x02 -> dittos
mjr 77:0b96f6867312 2218 uint8_t flags = 0;
mjr 77:0b96f6867312 2219 if (learnedIRCode.hasDittos)
mjr 77:0b96f6867312 2220 flags |= 0x02;
mjr 77:0b96f6867312 2221
mjr 77:0b96f6867312 2222 // report the code
mjr 77:0b96f6867312 2223 js.reportIRCode(learnedIRCode.proId, flags, learnedIRCode.code);
mjr 77:0b96f6867312 2224
mjr 77:0b96f6867312 2225 // exit learning mode
mjr 77:0b96f6867312 2226 IRLearningMode = 0;
mjr 77:0b96f6867312 2227 }
mjr 77:0b96f6867312 2228 }
mjr 77:0b96f6867312 2229
mjr 77:0b96f6867312 2230 // time out of IR learning mode if it's been too long
mjr 77:0b96f6867312 2231 if (IRLearningMode != 0 && IRTimer.read_us() > 10000000L)
mjr 77:0b96f6867312 2232 {
mjr 77:0b96f6867312 2233 // report the termination by sending a raw IR report with
mjr 77:0b96f6867312 2234 // zero data elements
mjr 77:0b96f6867312 2235 js.reportRawIR(0, 0);
mjr 77:0b96f6867312 2236
mjr 77:0b96f6867312 2237
mjr 77:0b96f6867312 2238 // cancel learning mode
mjr 77:0b96f6867312 2239 IRLearningMode = 0;
mjr 77:0b96f6867312 2240 }
mjr 77:0b96f6867312 2241 }
mjr 77:0b96f6867312 2242 else
mjr 77:0b96f6867312 2243 {
mjr 77:0b96f6867312 2244 // Not in learning mode. We don't care about the raw signals;
mjr 77:0b96f6867312 2245 // just run them through the protocol decoders.
mjr 77:0b96f6867312 2246 ir_rx->process();
mjr 77:0b96f6867312 2247
mjr 77:0b96f6867312 2248 // Check for decoded commands. Keep going until all commands
mjr 77:0b96f6867312 2249 // have been read.
mjr 77:0b96f6867312 2250 IRCommand c;
mjr 77:0b96f6867312 2251 while (ir_rx->readCommand(c))
mjr 77:0b96f6867312 2252 {
mjr 77:0b96f6867312 2253 // We received a decoded command. Determine if it's a repeat,
mjr 77:0b96f6867312 2254 // and if so, try to determine whether it's an auto-repeat (due
mjr 77:0b96f6867312 2255 // to the remote key being held down) or a distinct new press
mjr 77:0b96f6867312 2256 // on the same key as last time. The distinction is significant
mjr 77:0b96f6867312 2257 // because it affects the auto-repeat behavior of the PC key
mjr 77:0b96f6867312 2258 // input. An auto-repeat represents a key being held down on
mjr 77:0b96f6867312 2259 // the remote, which we want to translate to a (virtual) key
mjr 77:0b96f6867312 2260 // being held down on the PC keyboard; a distinct key press on
mjr 77:0b96f6867312 2261 // the remote translates to a distinct key press on the PC.
mjr 77:0b96f6867312 2262 //
mjr 77:0b96f6867312 2263 // It can only be a repeat if there's a prior command that
mjr 77:0b96f6867312 2264 // hasn't timed out yet, so start by checking for a previous
mjr 77:0b96f6867312 2265 // command.
mjr 77:0b96f6867312 2266 bool repeat = false, autoRepeat = false;
mjr 77:0b96f6867312 2267 if (IRCommandIn != 0)
mjr 77:0b96f6867312 2268 {
mjr 77:0b96f6867312 2269 // We have a command in progress. Check to see if the
mjr 77:0b96f6867312 2270 // new command is a repeat of the previous command. Check
mjr 77:0b96f6867312 2271 // first to see if it's a "ditto", which explicitly represents
mjr 77:0b96f6867312 2272 // an auto-repeat of the last command.
mjr 77:0b96f6867312 2273 IRCommandCfg &cmdcfg = cfg.IRCommand[IRCommandIn - 1];
mjr 77:0b96f6867312 2274 if (c.ditto)
mjr 77:0b96f6867312 2275 {
mjr 77:0b96f6867312 2276 // We received a ditto. Dittos are always auto-
mjr 77:0b96f6867312 2277 // repeats, so it's an auto-repeat as long as the
mjr 77:0b96f6867312 2278 // ditto is in the same protocol as the last command.
mjr 77:0b96f6867312 2279 // If the ditto is in a new protocol, the ditto can't
mjr 77:0b96f6867312 2280 // be for the last command we saw, because a ditto
mjr 77:0b96f6867312 2281 // never changes protocols from its antecedent. In
mjr 77:0b96f6867312 2282 // such a case, we must have missed the antecedent
mjr 77:0b96f6867312 2283 // command and thus don't know what's being repeated.
mjr 77:0b96f6867312 2284 repeat = autoRepeat = (c.proId == cmdcfg.protocol);
mjr 77:0b96f6867312 2285 }
mjr 77:0b96f6867312 2286 else
mjr 77:0b96f6867312 2287 {
mjr 77:0b96f6867312 2288 // It's not a ditto. The new command is a repeat if
mjr 77:0b96f6867312 2289 // it matches the protocol and command code of the
mjr 77:0b96f6867312 2290 // prior command.
mjr 77:0b96f6867312 2291 repeat = (c.proId == cmdcfg.protocol
mjr 77:0b96f6867312 2292 && uint32_t(c.code) == cmdcfg.code.lo
mjr 77:0b96f6867312 2293 && uint32_t(c.code >> 32) == cmdcfg.code.hi);
mjr 77:0b96f6867312 2294
mjr 77:0b96f6867312 2295 // If the command is a repeat, try to determine whether
mjr 77:0b96f6867312 2296 // it's an auto-repeat or a new press on the same key.
mjr 77:0b96f6867312 2297 // If the protocol uses dittos, it's definitely a new
mjr 77:0b96f6867312 2298 // key press, because an auto-repeat would have used a
mjr 77:0b96f6867312 2299 // ditto. For a protocol that doesn't use dittos, both
mjr 77:0b96f6867312 2300 // an auto-repeat and a new key press just send the key
mjr 77:0b96f6867312 2301 // code again, so we can't tell the difference based on
mjr 77:0b96f6867312 2302 // that alone. But if the protocol has a toggle bit, we
mjr 77:0b96f6867312 2303 // can tell by the toggle bit value: a new key press has
mjr 77:0b96f6867312 2304 // the opposite toggle value as the last key press, while
mjr 77:0b96f6867312 2305 // an auto-repeat has the same toggle. Note that if the
mjr 77:0b96f6867312 2306 // protocol doesn't use toggle bits, the toggle value
mjr 77:0b96f6867312 2307 // will always be the same, so we'll simply always treat
mjr 77:0b96f6867312 2308 // any repeat as an auto-repeat. Many protocols simply
mjr 77:0b96f6867312 2309 // provide no way to distinguish the two, so in such
mjr 77:0b96f6867312 2310 // cases it's consistent with the native implementations
mjr 77:0b96f6867312 2311 // to treat any repeat as an auto-repeat.
mjr 77:0b96f6867312 2312 autoRepeat =
mjr 77:0b96f6867312 2313 repeat
mjr 77:0b96f6867312 2314 && !(cmdcfg.flags & IRFlagDittos)
mjr 77:0b96f6867312 2315 && c.toggle == lastIRToggle;
mjr 77:0b96f6867312 2316 }
mjr 77:0b96f6867312 2317 }
mjr 77:0b96f6867312 2318
mjr 77:0b96f6867312 2319 // Check to see if it's a repeat of any kind
mjr 77:0b96f6867312 2320 if (repeat)
mjr 77:0b96f6867312 2321 {
mjr 77:0b96f6867312 2322 // It's a repeat. If it's not an auto-repeat, it's a
mjr 77:0b96f6867312 2323 // new distinct key press, so we need to send the PC a
mjr 77:0b96f6867312 2324 // momentary gap where we're not sending the same key,
mjr 77:0b96f6867312 2325 // so that the PC also recognizes this as a distinct
mjr 77:0b96f6867312 2326 // key press event.
mjr 77:0b96f6867312 2327 if (!autoRepeat)
mjr 77:0b96f6867312 2328 IRKeyGap = true;
mjr 77:0b96f6867312 2329
mjr 77:0b96f6867312 2330 // restart the key-up timer
mjr 77:0b96f6867312 2331 IRTimer.reset();
mjr 77:0b96f6867312 2332 }
mjr 77:0b96f6867312 2333 else if (c.ditto)
mjr 77:0b96f6867312 2334 {
mjr 77:0b96f6867312 2335 // It's a ditto, but not a repeat of the last command.
mjr 77:0b96f6867312 2336 // But a ditto doesn't contain any information of its own
mjr 77:0b96f6867312 2337 // on the command being repeated, so given that it's not
mjr 77:0b96f6867312 2338 // our last command, we can't infer what command the ditto
mjr 77:0b96f6867312 2339 // is for and thus can't make sense of it. We have to
mjr 77:0b96f6867312 2340 // simply ignore it and wait for the sender to start with
mjr 77:0b96f6867312 2341 // a full command for a new key press.
mjr 77:0b96f6867312 2342 IRCommandIn = 0;
mjr 77:0b96f6867312 2343 }
mjr 77:0b96f6867312 2344 else
mjr 77:0b96f6867312 2345 {
mjr 77:0b96f6867312 2346 // It's not a repeat, so the last command is no longer
mjr 77:0b96f6867312 2347 // in effect (regardless of whether we find a match for
mjr 77:0b96f6867312 2348 // the new command).
mjr 77:0b96f6867312 2349 IRCommandIn = 0;
mjr 77:0b96f6867312 2350
mjr 77:0b96f6867312 2351 // Check to see if we recognize the new command, by
mjr 77:0b96f6867312 2352 // searching for a match in our learned code list.
mjr 77:0b96f6867312 2353 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2354 {
mjr 77:0b96f6867312 2355 // if the protocol and command code from the code
mjr 77:0b96f6867312 2356 // list both match the input, it's a match
mjr 77:0b96f6867312 2357 IRCommandCfg &cmdcfg = cfg.IRCommand[i];
mjr 77:0b96f6867312 2358 if (cmdcfg.protocol == c.proId
mjr 77:0b96f6867312 2359 && cmdcfg.code.lo == uint32_t(c.code)
mjr 77:0b96f6867312 2360 && cmdcfg.code.hi == uint32_t(c.code >> 32))
mjr 77:0b96f6867312 2361 {
mjr 77:0b96f6867312 2362 // Found it! Make this the last command, and
mjr 77:0b96f6867312 2363 // remember the starting time.
mjr 77:0b96f6867312 2364 IRCommandIn = i + 1;
mjr 77:0b96f6867312 2365 lastIRToggle = c.toggle;
mjr 77:0b96f6867312 2366 IRTimer.reset();
mjr 77:0b96f6867312 2367
mjr 77:0b96f6867312 2368 // no need to keep searching
mjr 77:0b96f6867312 2369 break;
mjr 77:0b96f6867312 2370 }
mjr 77:0b96f6867312 2371 }
mjr 77:0b96f6867312 2372 }
mjr 77:0b96f6867312 2373 }
mjr 77:0b96f6867312 2374 }
mjr 77:0b96f6867312 2375 }
mjr 77:0b96f6867312 2376
mjr 74:822a92bc11d2 2377
mjr 11:bd9da7088e6e 2378 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 2379 //
mjr 11:bd9da7088e6e 2380 // Button input
mjr 11:bd9da7088e6e 2381 //
mjr 11:bd9da7088e6e 2382
mjr 18:5e890ebd0023 2383 // button state
mjr 18:5e890ebd0023 2384 struct ButtonState
mjr 18:5e890ebd0023 2385 {
mjr 38:091e511ce8a0 2386 ButtonState()
mjr 38:091e511ce8a0 2387 {
mjr 53:9b2611964afc 2388 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 2389 virtState = 0;
mjr 53:9b2611964afc 2390 dbState = 0;
mjr 38:091e511ce8a0 2391 pulseState = 0;
mjr 53:9b2611964afc 2392 pulseTime = 0;
mjr 38:091e511ce8a0 2393 }
mjr 35:e959ffba78fd 2394
mjr 53:9b2611964afc 2395 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 2396 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 2397 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 2398 //
mjr 53:9b2611964afc 2399 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 2400 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 2401 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 2402 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 2403 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 2404 void virtPress(bool on)
mjr 53:9b2611964afc 2405 {
mjr 53:9b2611964afc 2406 // Increment or decrement the current state
mjr 53:9b2611964afc 2407 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 2408 }
mjr 53:9b2611964afc 2409
mjr 53:9b2611964afc 2410 // DigitalIn for the button, if connected to a physical input
mjr 73:4e8ce0b18915 2411 TinyDigitalIn di;
mjr 38:091e511ce8a0 2412
mjr 65:739875521aae 2413 // Time of last pulse state transition.
mjr 65:739875521aae 2414 //
mjr 65:739875521aae 2415 // Each state change sticks for a minimum period; when the timer expires,
mjr 65:739875521aae 2416 // if the underlying physical switch is in a different state, we switch
mjr 65:739875521aae 2417 // to the next state and restart the timer. pulseTime is the time remaining
mjr 65:739875521aae 2418 // remaining before we can make another state transition, in microseconds.
mjr 65:739875521aae 2419 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 65:739875521aae 2420 // this guarantees that the parity of the pulse count always matches the
mjr 65:739875521aae 2421 // current physical switch state when the latter is stable, which makes
mjr 65:739875521aae 2422 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 65:739875521aae 2423 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 65:739875521aae 2424 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 65:739875521aae 2425 // This software system can't be fooled that way.)
mjr 65:739875521aae 2426 uint32_t pulseTime;
mjr 18:5e890ebd0023 2427
mjr 65:739875521aae 2428 // Config key index. This points to the ButtonCfg structure in the
mjr 65:739875521aae 2429 // configuration that contains the PC key mapping for the button.
mjr 65:739875521aae 2430 uint8_t cfgIndex;
mjr 53:9b2611964afc 2431
mjr 53:9b2611964afc 2432 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 2433 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 2434 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 2435 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 2436 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 2437 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 2438 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 2439 // and physical source states.
mjr 53:9b2611964afc 2440 uint8_t virtState;
mjr 38:091e511ce8a0 2441
mjr 38:091e511ce8a0 2442 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 2443 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 2444 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 2445 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 2446 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 2447 uint8_t dbState;
mjr 38:091e511ce8a0 2448
mjr 65:739875521aae 2449 // current PHYSICAL on/off state, after debouncing
mjr 65:739875521aae 2450 uint8_t physState : 1;
mjr 65:739875521aae 2451
mjr 65:739875521aae 2452 // current LOGICAL on/off state as reported to the host.
mjr 65:739875521aae 2453 uint8_t logState : 1;
mjr 65:739875521aae 2454
mjr 65:739875521aae 2455 // previous logical on/off state, when keys were last processed for USB
mjr 65:739875521aae 2456 // reports and local effects
mjr 65:739875521aae 2457 uint8_t prevLogState : 1;
mjr 65:739875521aae 2458
mjr 65:739875521aae 2459 // Pulse state
mjr 65:739875521aae 2460 //
mjr 65:739875521aae 2461 // A button in pulse mode (selected via the config flags for the button)
mjr 65:739875521aae 2462 // transmits a brief logical button press and release each time the attached
mjr 65:739875521aae 2463 // physical switch changes state. This is useful for cases where the host
mjr 65:739875521aae 2464 // expects a key press for each change in the state of the physical switch.
mjr 65:739875521aae 2465 // The canonical example is the Coin Door switch in VPinMAME, which requires
mjr 65:739875521aae 2466 // pressing the END key to toggle the open/closed state. This software design
mjr 65:739875521aae 2467 // isn't easily implemented in a physical coin door, though; the simplest
mjr 65:739875521aae 2468 // physical sensor for the coin door state is a switch that's on when the
mjr 65:739875521aae 2469 // door is open and off when the door is closed (or vice versa, but in either
mjr 65:739875521aae 2470 // case, the switch state corresponds to the current state of the door at any
mjr 65:739875521aae 2471 // given time, rather than pulsing on state changes). The "pulse mode"
mjr 65:739875521aae 2472 // option brdiges this gap by generating a toggle key event each time
mjr 65:739875521aae 2473 // there's a change to the physical switch's state.
mjr 38:091e511ce8a0 2474 //
mjr 38:091e511ce8a0 2475 // Pulse state:
mjr 38:091e511ce8a0 2476 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 2477 // 1 -> off
mjr 38:091e511ce8a0 2478 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 2479 // 3 -> on
mjr 38:091e511ce8a0 2480 // 4 -> transitioning on-off
mjr 65:739875521aae 2481 uint8_t pulseState : 3; // 5 states -> we need 3 bits
mjr 65:739875521aae 2482
mjr 65:739875521aae 2483 } __attribute__((packed));
mjr 65:739875521aae 2484
mjr 65:739875521aae 2485 ButtonState *buttonState; // live button slots, allocated on startup
mjr 65:739875521aae 2486 int8_t nButtons; // number of live button slots allocated
mjr 65:739875521aae 2487 int8_t zblButtonIndex = -1; // index of ZB Launch button slot; -1 if unused
mjr 18:5e890ebd0023 2488
mjr 66:2e3583fbd2f4 2489 // Shift button state
mjr 66:2e3583fbd2f4 2490 struct
mjr 66:2e3583fbd2f4 2491 {
mjr 66:2e3583fbd2f4 2492 int8_t index; // buttonState[] index of shift button; -1 if none
mjr 66:2e3583fbd2f4 2493 uint8_t state : 2; // current shift state:
mjr 66:2e3583fbd2f4 2494 // 0 = not shifted
mjr 66:2e3583fbd2f4 2495 // 1 = shift button down, no key pressed yet
mjr 66:2e3583fbd2f4 2496 // 2 = shift button down, key pressed
mjr 66:2e3583fbd2f4 2497 uint8_t pulse : 1; // sending pulsed keystroke on release
mjr 66:2e3583fbd2f4 2498 uint32_t pulseTime; // time of start of pulsed keystroke
mjr 66:2e3583fbd2f4 2499 }
mjr 66:2e3583fbd2f4 2500 __attribute__((packed)) shiftButton;
mjr 38:091e511ce8a0 2501
mjr 38:091e511ce8a0 2502 // Button data
mjr 38:091e511ce8a0 2503 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 2504
mjr 38:091e511ce8a0 2505 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 2506 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 2507 // modifier keys.
mjr 38:091e511ce8a0 2508 struct
mjr 38:091e511ce8a0 2509 {
mjr 38:091e511ce8a0 2510 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 2511 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 2512 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 2513 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 2514 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 2515
mjr 38:091e511ce8a0 2516 // Media key state
mjr 38:091e511ce8a0 2517 struct
mjr 38:091e511ce8a0 2518 {
mjr 38:091e511ce8a0 2519 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 2520 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 2521 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 2522
mjr 38:091e511ce8a0 2523 // button scan interrupt ticker
mjr 38:091e511ce8a0 2524 Ticker buttonTicker;
mjr 38:091e511ce8a0 2525
mjr 38:091e511ce8a0 2526 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 2527 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 2528 void scanButtons()
mjr 38:091e511ce8a0 2529 {
mjr 38:091e511ce8a0 2530 // scan all button input pins
mjr 73:4e8ce0b18915 2531 ButtonState *bs = buttonState, *last = bs + nButtons;
mjr 73:4e8ce0b18915 2532 for ( ; bs < last ; ++bs)
mjr 38:091e511ce8a0 2533 {
mjr 73:4e8ce0b18915 2534 // Shift the new state into the debounce history
mjr 73:4e8ce0b18915 2535 uint8_t db = (bs->dbState << 1) | bs->di.read();
mjr 73:4e8ce0b18915 2536 bs->dbState = db;
mjr 73:4e8ce0b18915 2537
mjr 73:4e8ce0b18915 2538 // If we have all 0's or 1's in the history for the required
mjr 73:4e8ce0b18915 2539 // debounce period, the key state is stable, so apply the new
mjr 73:4e8ce0b18915 2540 // physical state. Note that the pins are active low, so the
mjr 73:4e8ce0b18915 2541 // new button on/off state is the inverse of the GPIO state.
mjr 73:4e8ce0b18915 2542 const uint8_t stable = 0x1F; // 00011111b -> low 5 bits = last 5 readings
mjr 73:4e8ce0b18915 2543 db &= stable;
mjr 73:4e8ce0b18915 2544 if (db == 0 || db == stable)
mjr 73:4e8ce0b18915 2545 bs->physState = !db;
mjr 38:091e511ce8a0 2546 }
mjr 38:091e511ce8a0 2547 }
mjr 38:091e511ce8a0 2548
mjr 38:091e511ce8a0 2549 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 2550 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 2551 // in the physical button state.
mjr 38:091e511ce8a0 2552 Timer buttonTimer;
mjr 12:669df364a565 2553
mjr 65:739875521aae 2554 // Count a button during the initial setup scan
mjr 72:884207c0aab0 2555 void countButton(uint8_t typ, uint8_t shiftTyp, bool &kbKeys)
mjr 65:739875521aae 2556 {
mjr 65:739875521aae 2557 // count it
mjr 65:739875521aae 2558 ++nButtons;
mjr 65:739875521aae 2559
mjr 67:c39e66c4e000 2560 // if it's a keyboard key or media key, note that we need a USB
mjr 67:c39e66c4e000 2561 // keyboard interface
mjr 72:884207c0aab0 2562 if (typ == BtnTypeKey || typ == BtnTypeMedia
mjr 72:884207c0aab0 2563 || shiftTyp == BtnTypeKey || shiftTyp == BtnTypeMedia)
mjr 65:739875521aae 2564 kbKeys = true;
mjr 65:739875521aae 2565 }
mjr 65:739875521aae 2566
mjr 11:bd9da7088e6e 2567 // initialize the button inputs
mjr 35:e959ffba78fd 2568 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 2569 {
mjr 66:2e3583fbd2f4 2570 // presume no shift key
mjr 66:2e3583fbd2f4 2571 shiftButton.index = -1;
mjr 66:2e3583fbd2f4 2572
mjr 65:739875521aae 2573 // Count up how many button slots we'll need to allocate. Start
mjr 65:739875521aae 2574 // with assigned buttons from the configuration, noting that we
mjr 65:739875521aae 2575 // only need to create slots for buttons that are actually wired.
mjr 65:739875521aae 2576 nButtons = 0;
mjr 65:739875521aae 2577 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 2578 {
mjr 65:739875521aae 2579 // it's valid if it's wired to a real input pin
mjr 65:739875521aae 2580 if (wirePinName(cfg.button[i].pin) != NC)
mjr 72:884207c0aab0 2581 countButton(cfg.button[i].typ, cfg.button[i].typ2, kbKeys);
mjr 65:739875521aae 2582 }
mjr 65:739875521aae 2583
mjr 65:739875521aae 2584 // Count virtual buttons
mjr 65:739875521aae 2585
mjr 65:739875521aae 2586 // ZB Launch
mjr 65:739875521aae 2587 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 65:739875521aae 2588 {
mjr 65:739875521aae 2589 // valid - remember the live button index
mjr 65:739875521aae 2590 zblButtonIndex = nButtons;
mjr 65:739875521aae 2591
mjr 65:739875521aae 2592 // count it
mjr 72:884207c0aab0 2593 countButton(cfg.plunger.zbLaunchBall.keytype, BtnTypeNone, kbKeys);
mjr 65:739875521aae 2594 }
mjr 65:739875521aae 2595
mjr 65:739875521aae 2596 // Allocate the live button slots
mjr 65:739875521aae 2597 ButtonState *bs = buttonState = new ButtonState[nButtons];
mjr 65:739875521aae 2598
mjr 65:739875521aae 2599 // Configure the physical inputs
mjr 65:739875521aae 2600 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 2601 {
mjr 65:739875521aae 2602 PinName pin = wirePinName(cfg.button[i].pin);
mjr 65:739875521aae 2603 if (pin != NC)
mjr 65:739875521aae 2604 {
mjr 65:739875521aae 2605 // point back to the config slot for the keyboard data
mjr 65:739875521aae 2606 bs->cfgIndex = i;
mjr 65:739875521aae 2607
mjr 65:739875521aae 2608 // set up the GPIO input pin for this button
mjr 73:4e8ce0b18915 2609 bs->di.assignPin(pin);
mjr 65:739875521aae 2610
mjr 65:739875521aae 2611 // if it's a pulse mode button, set the initial pulse state to Off
mjr 65:739875521aae 2612 if (cfg.button[i].flags & BtnFlagPulse)
mjr 65:739875521aae 2613 bs->pulseState = 1;
mjr 65:739875521aae 2614
mjr 66:2e3583fbd2f4 2615 // If this is the shift button, note its buttonState[] index.
mjr 66:2e3583fbd2f4 2616 // We have to figure the buttonState[] index separately from
mjr 66:2e3583fbd2f4 2617 // the config index, because the indices can differ if some
mjr 66:2e3583fbd2f4 2618 // config slots are left unused.
mjr 66:2e3583fbd2f4 2619 if (cfg.shiftButton == i+1)
mjr 66:2e3583fbd2f4 2620 shiftButton.index = bs - buttonState;
mjr 66:2e3583fbd2f4 2621
mjr 65:739875521aae 2622 // advance to the next button
mjr 65:739875521aae 2623 ++bs;
mjr 65:739875521aae 2624 }
mjr 65:739875521aae 2625 }
mjr 65:739875521aae 2626
mjr 53:9b2611964afc 2627 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 2628 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 2629 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 2630 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 2631
mjr 53:9b2611964afc 2632 // ZB Launch Ball button
mjr 65:739875521aae 2633 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 11:bd9da7088e6e 2634 {
mjr 65:739875521aae 2635 // Point back to the config slot for the keyboard data.
mjr 66:2e3583fbd2f4 2636 // We use a special extra slot for virtual buttons,
mjr 66:2e3583fbd2f4 2637 // so we also need to set up the slot data by copying
mjr 66:2e3583fbd2f4 2638 // the ZBL config data to our virtual button slot.
mjr 65:739875521aae 2639 bs->cfgIndex = ZBL_BUTTON_CFG;
mjr 65:739875521aae 2640 cfg.button[ZBL_BUTTON_CFG].pin = PINNAME_TO_WIRE(NC);
mjr 65:739875521aae 2641 cfg.button[ZBL_BUTTON_CFG].typ = cfg.plunger.zbLaunchBall.keytype;
mjr 65:739875521aae 2642 cfg.button[ZBL_BUTTON_CFG].val = cfg.plunger.zbLaunchBall.keycode;
mjr 65:739875521aae 2643
mjr 66:2e3583fbd2f4 2644 // advance to the next button
mjr 65:739875521aae 2645 ++bs;
mjr 11:bd9da7088e6e 2646 }
mjr 12:669df364a565 2647
mjr 38:091e511ce8a0 2648 // start the button scan thread
mjr 38:091e511ce8a0 2649 buttonTicker.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 2650
mjr 38:091e511ce8a0 2651 // start the button state transition timer
mjr 12:669df364a565 2652 buttonTimer.start();
mjr 11:bd9da7088e6e 2653 }
mjr 11:bd9da7088e6e 2654
mjr 67:c39e66c4e000 2655 // Media key mapping. This maps from an 8-bit USB media key
mjr 67:c39e66c4e000 2656 // code to the corresponding bit in our USB report descriptor.
mjr 67:c39e66c4e000 2657 // The USB key code is the index, and the value at the index
mjr 67:c39e66c4e000 2658 // is the report descriptor bit. See joystick.cpp for the
mjr 67:c39e66c4e000 2659 // media descriptor details. Our currently mapped keys are:
mjr 67:c39e66c4e000 2660 //
mjr 67:c39e66c4e000 2661 // 0xE2 -> Mute -> 0x01
mjr 67:c39e66c4e000 2662 // 0xE9 -> Volume Up -> 0x02
mjr 67:c39e66c4e000 2663 // 0xEA -> Volume Down -> 0x04
mjr 67:c39e66c4e000 2664 // 0xB5 -> Next Track -> 0x08
mjr 67:c39e66c4e000 2665 // 0xB6 -> Previous Track -> 0x10
mjr 67:c39e66c4e000 2666 // 0xB7 -> Stop -> 0x20
mjr 67:c39e66c4e000 2667 // 0xCD -> Play / Pause -> 0x40
mjr 67:c39e66c4e000 2668 //
mjr 67:c39e66c4e000 2669 static const uint8_t mediaKeyMap[] = {
mjr 67:c39e66c4e000 2670 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-0F
mjr 67:c39e66c4e000 2671 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-1F
mjr 67:c39e66c4e000 2672 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-2F
mjr 67:c39e66c4e000 2673 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-3F
mjr 67:c39e66c4e000 2674 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-4F
mjr 67:c39e66c4e000 2675 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-5F
mjr 67:c39e66c4e000 2676 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-6F
mjr 67:c39e66c4e000 2677 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-7F
mjr 67:c39e66c4e000 2678 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
mjr 67:c39e66c4e000 2679 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-9F
mjr 67:c39e66c4e000 2680 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0-AF
mjr 67:c39e66c4e000 2681 0, 0, 0, 0, 0, 8, 16, 32, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
mjr 67:c39e66c4e000 2682 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, // C0-CF
mjr 67:c39e66c4e000 2683 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D0-DF
mjr 67:c39e66c4e000 2684 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, // E0-EF
mjr 67:c39e66c4e000 2685 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-FF
mjr 77:0b96f6867312 2686 };
mjr 77:0b96f6867312 2687
mjr 77:0b96f6867312 2688 // Keyboard key/joystick button state. processButtons() uses this to
mjr 77:0b96f6867312 2689 // build the set of key presses to report to the PC based on the logical
mjr 77:0b96f6867312 2690 // states of the button iputs.
mjr 77:0b96f6867312 2691 struct KeyState
mjr 77:0b96f6867312 2692 {
mjr 77:0b96f6867312 2693 KeyState()
mjr 77:0b96f6867312 2694 {
mjr 77:0b96f6867312 2695 // zero all members
mjr 77:0b96f6867312 2696 memset(this, 0, sizeof(*this));
mjr 77:0b96f6867312 2697 }
mjr 77:0b96f6867312 2698
mjr 77:0b96f6867312 2699 // Keyboard media keys currently pressed. This is a bit vector in
mjr 77:0b96f6867312 2700 // the format used in our USB keyboard reports (see USBJoystick.cpp).
mjr 77:0b96f6867312 2701 uint8_t mediakeys;
mjr 77:0b96f6867312 2702
mjr 77:0b96f6867312 2703 // Keyboard modifier (shift) keys currently pressed. This is a bit
mjr 77:0b96f6867312 2704 // vector in the format used in our USB keyboard reports (see
mjr 77:0b96f6867312 2705 // USBJoystick.cpp).
mjr 77:0b96f6867312 2706 uint8_t modkeys;
mjr 77:0b96f6867312 2707
mjr 77:0b96f6867312 2708 // Regular keyboard keys currently pressed. Each element is a USB
mjr 77:0b96f6867312 2709 // key code, or 0 for empty slots. Note that the USB report format
mjr 77:0b96f6867312 2710 // theoretically allows a flexible size limit, but the Windows KB
mjr 77:0b96f6867312 2711 // drivers have a fixed limit of 6 simultaneous keys (and won't
mjr 77:0b96f6867312 2712 // accept reports with more), so there's no point in making this
mjr 77:0b96f6867312 2713 // flexible; we'll just use the fixed size dictated by Windows.
mjr 77:0b96f6867312 2714 uint8_t keys[7];
mjr 77:0b96f6867312 2715
mjr 77:0b96f6867312 2716 // number of valid entries in keys[] array
mjr 77:0b96f6867312 2717 int nkeys;
mjr 77:0b96f6867312 2718
mjr 77:0b96f6867312 2719 // Joystick buttons pressed, as a bit vector. Bit n (1 << n)
mjr 77:0b96f6867312 2720 // represents joystick button n, n in 0..31, with 0 meaning
mjr 77:0b96f6867312 2721 // unpressed and 1 meaning pressed.
mjr 77:0b96f6867312 2722 uint32_t js;
mjr 77:0b96f6867312 2723
mjr 77:0b96f6867312 2724
mjr 77:0b96f6867312 2725 // Add a key press. 'typ' is the button type code (ButtonTypeXxx),
mjr 77:0b96f6867312 2726 // and 'val' is the value (the meaning of which varies by type code).
mjr 77:0b96f6867312 2727 void addKey(uint8_t typ, uint8_t val)
mjr 77:0b96f6867312 2728 {
mjr 77:0b96f6867312 2729 // add the key according to the type
mjr 77:0b96f6867312 2730 switch (typ)
mjr 77:0b96f6867312 2731 {
mjr 77:0b96f6867312 2732 case BtnTypeJoystick:
mjr 77:0b96f6867312 2733 // joystick button
mjr 77:0b96f6867312 2734 js |= (1 << (val - 1));
mjr 77:0b96f6867312 2735 break;
mjr 77:0b96f6867312 2736
mjr 77:0b96f6867312 2737 case BtnTypeKey:
mjr 77:0b96f6867312 2738 // Keyboard key. The USB keyboard report encodes regular
mjr 77:0b96f6867312 2739 // keys and modifier keys separately, so we need to check
mjr 77:0b96f6867312 2740 // which type we have. Note that past versions mapped the
mjr 77:0b96f6867312 2741 // Keyboard Volume Up, Keyboard Volume Down, and Keyboard
mjr 77:0b96f6867312 2742 // Mute keys to the corresponding Media keys. We no longer
mjr 77:0b96f6867312 2743 // do this; instead, we have the separate BtnTypeMedia for
mjr 77:0b96f6867312 2744 // explicitly using media keys if desired.
mjr 77:0b96f6867312 2745 if (val >= 0xE0 && val <= 0xE7)
mjr 77:0b96f6867312 2746 {
mjr 77:0b96f6867312 2747 // It's a modifier key. These are represented in the USB
mjr 77:0b96f6867312 2748 // reports with a bit mask. We arrange the mask bits in
mjr 77:0b96f6867312 2749 // the same order as the scan codes, so we can figure the
mjr 77:0b96f6867312 2750 // appropriate bit with a simple shift.
mjr 77:0b96f6867312 2751 modkeys |= (1 << (val - 0xE0));
mjr 77:0b96f6867312 2752 }
mjr 77:0b96f6867312 2753 else
mjr 77:0b96f6867312 2754 {
mjr 77:0b96f6867312 2755 // It's a regular key. Make sure it's not already in the
mjr 77:0b96f6867312 2756 // list, and that the list isn't full. If neither of these
mjr 77:0b96f6867312 2757 // apply, add the key to the key array.
mjr 77:0b96f6867312 2758 if (nkeys < 7)
mjr 77:0b96f6867312 2759 {
mjr 77:0b96f6867312 2760 bool found = false;
mjr 77:0b96f6867312 2761 for (int i = 0 ; i < nkeys ; ++i)
mjr 77:0b96f6867312 2762 {
mjr 77:0b96f6867312 2763 if (keys[i] == val)
mjr 77:0b96f6867312 2764 {
mjr 77:0b96f6867312 2765 found = true;
mjr 77:0b96f6867312 2766 break;
mjr 77:0b96f6867312 2767 }
mjr 77:0b96f6867312 2768 }
mjr 77:0b96f6867312 2769 if (!found)
mjr 77:0b96f6867312 2770 keys[nkeys++] = val;
mjr 77:0b96f6867312 2771 }
mjr 77:0b96f6867312 2772 }
mjr 77:0b96f6867312 2773 break;
mjr 77:0b96f6867312 2774
mjr 77:0b96f6867312 2775 case BtnTypeMedia:
mjr 77:0b96f6867312 2776 // Media control key. The media keys are mapped in the USB
mjr 77:0b96f6867312 2777 // report to bits, whereas the key codes are specified in the
mjr 77:0b96f6867312 2778 // config with their USB usage numbers. E.g., the config val
mjr 77:0b96f6867312 2779 // for Media Next Track is 0xB5, but we encode this in the USB
mjr 77:0b96f6867312 2780 // report as bit 0x08. The mediaKeyMap[] table translates
mjr 77:0b96f6867312 2781 // from the USB usage number to the mask bit. If the key isn't
mjr 77:0b96f6867312 2782 // among the subset we support, the mapped bit will be zero, so
mjr 77:0b96f6867312 2783 // the "|=" will have no effect and the key will be ignored.
mjr 77:0b96f6867312 2784 mediakeys |= mediaKeyMap[val];
mjr 77:0b96f6867312 2785 break;
mjr 77:0b96f6867312 2786 }
mjr 77:0b96f6867312 2787 }
mjr 77:0b96f6867312 2788 };
mjr 67:c39e66c4e000 2789
mjr 67:c39e66c4e000 2790
mjr 38:091e511ce8a0 2791 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 2792 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 2793 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 2794 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 2795 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 2796 {
mjr 77:0b96f6867312 2797 // key state
mjr 77:0b96f6867312 2798 KeyState ks;
mjr 38:091e511ce8a0 2799
mjr 38:091e511ce8a0 2800 // calculate the time since the last run
mjr 53:9b2611964afc 2801 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 2802 buttonTimer.reset();
mjr 66:2e3583fbd2f4 2803
mjr 66:2e3583fbd2f4 2804 // check the shift button state
mjr 66:2e3583fbd2f4 2805 if (shiftButton.index != -1)
mjr 66:2e3583fbd2f4 2806 {
mjr 66:2e3583fbd2f4 2807 ButtonState *sbs = &buttonState[shiftButton.index];
mjr 66:2e3583fbd2f4 2808 switch (shiftButton.state)
mjr 66:2e3583fbd2f4 2809 {
mjr 66:2e3583fbd2f4 2810 case 0:
mjr 66:2e3583fbd2f4 2811 // Not shifted. Check if the button is now down: if so,
mjr 66:2e3583fbd2f4 2812 // switch to state 1 (shift button down, no key pressed yet).
mjr 66:2e3583fbd2f4 2813 if (sbs->physState)
mjr 66:2e3583fbd2f4 2814 shiftButton.state = 1;
mjr 66:2e3583fbd2f4 2815 break;
mjr 66:2e3583fbd2f4 2816
mjr 66:2e3583fbd2f4 2817 case 1:
mjr 66:2e3583fbd2f4 2818 // Shift button down, no key pressed yet. If the button is
mjr 66:2e3583fbd2f4 2819 // now up, it counts as an ordinary button press instead of
mjr 66:2e3583fbd2f4 2820 // a shift button press, since the shift function was never
mjr 66:2e3583fbd2f4 2821 // used. Return to unshifted state and start a timed key
mjr 66:2e3583fbd2f4 2822 // pulse event.
mjr 66:2e3583fbd2f4 2823 if (!sbs->physState)
mjr 66:2e3583fbd2f4 2824 {
mjr 66:2e3583fbd2f4 2825 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 2826 shiftButton.pulse = 1;
mjr 66:2e3583fbd2f4 2827 shiftButton.pulseTime = 50000+dt; // 50 ms left on the key pulse
mjr 66:2e3583fbd2f4 2828 }
mjr 66:2e3583fbd2f4 2829 break;
mjr 66:2e3583fbd2f4 2830
mjr 66:2e3583fbd2f4 2831 case 2:
mjr 66:2e3583fbd2f4 2832 // Shift button down, other key was pressed. If the button is
mjr 66:2e3583fbd2f4 2833 // now up, simply clear the shift state without sending a key
mjr 66:2e3583fbd2f4 2834 // press for the shift button itself to the PC. The shift
mjr 66:2e3583fbd2f4 2835 // function was used, so its ordinary key press function is
mjr 66:2e3583fbd2f4 2836 // suppressed.
mjr 66:2e3583fbd2f4 2837 if (!sbs->physState)
mjr 66:2e3583fbd2f4 2838 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 2839 break;
mjr 66:2e3583fbd2f4 2840 }
mjr 66:2e3583fbd2f4 2841 }
mjr 38:091e511ce8a0 2842
mjr 11:bd9da7088e6e 2843 // scan the button list
mjr 18:5e890ebd0023 2844 ButtonState *bs = buttonState;
mjr 65:739875521aae 2845 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 11:bd9da7088e6e 2846 {
mjr 77:0b96f6867312 2847 // get the config entry for the button
mjr 77:0b96f6867312 2848 ButtonCfg *bc = &cfg.button[bs->cfgIndex];
mjr 77:0b96f6867312 2849
mjr 66:2e3583fbd2f4 2850 // Check the button type:
mjr 66:2e3583fbd2f4 2851 // - shift button
mjr 66:2e3583fbd2f4 2852 // - pulsed button
mjr 66:2e3583fbd2f4 2853 // - regular button
mjr 66:2e3583fbd2f4 2854 if (shiftButton.index == i)
mjr 66:2e3583fbd2f4 2855 {
mjr 66:2e3583fbd2f4 2856 // This is the shift button. Its logical state for key
mjr 66:2e3583fbd2f4 2857 // reporting purposes is controlled by the shift buttton
mjr 66:2e3583fbd2f4 2858 // pulse timer. If we're in a pulse, its logical state
mjr 66:2e3583fbd2f4 2859 // is pressed.
mjr 66:2e3583fbd2f4 2860 if (shiftButton.pulse)
mjr 66:2e3583fbd2f4 2861 {
mjr 66:2e3583fbd2f4 2862 // deduct the current interval from the pulse time, ending
mjr 66:2e3583fbd2f4 2863 // the pulse if the time has expired
mjr 66:2e3583fbd2f4 2864 if (shiftButton.pulseTime > dt)
mjr 66:2e3583fbd2f4 2865 shiftButton.pulseTime -= dt;
mjr 66:2e3583fbd2f4 2866 else
mjr 66:2e3583fbd2f4 2867 shiftButton.pulse = 0;
mjr 66:2e3583fbd2f4 2868 }
mjr 66:2e3583fbd2f4 2869
mjr 66:2e3583fbd2f4 2870 // the button is logically pressed if we're in a pulse
mjr 66:2e3583fbd2f4 2871 bs->logState = shiftButton.pulse;
mjr 66:2e3583fbd2f4 2872 }
mjr 66:2e3583fbd2f4 2873 else if (bs->pulseState != 0)
mjr 18:5e890ebd0023 2874 {
mjr 38:091e511ce8a0 2875 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 2876 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 2877 {
mjr 53:9b2611964afc 2878 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 2879 bs->pulseTime -= dt;
mjr 53:9b2611964afc 2880 }
mjr 53:9b2611964afc 2881 else
mjr 53:9b2611964afc 2882 {
mjr 53:9b2611964afc 2883 // pulse time expired - check for a state change
mjr 53:9b2611964afc 2884 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 2885 switch (bs->pulseState)
mjr 18:5e890ebd0023 2886 {
mjr 38:091e511ce8a0 2887 case 1:
mjr 38:091e511ce8a0 2888 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 2889 if (bs->physState)
mjr 53:9b2611964afc 2890 {
mjr 38:091e511ce8a0 2891 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 2892 bs->pulseState = 2;
mjr 53:9b2611964afc 2893 bs->logState = 1;
mjr 38:091e511ce8a0 2894 }
mjr 38:091e511ce8a0 2895 break;
mjr 18:5e890ebd0023 2896
mjr 38:091e511ce8a0 2897 case 2:
mjr 38:091e511ce8a0 2898 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 2899 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 2900 // change in state in the logical button
mjr 38:091e511ce8a0 2901 bs->pulseState = 3;
mjr 38:091e511ce8a0 2902 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 2903 bs->logState = 0;
mjr 38:091e511ce8a0 2904 break;
mjr 38:091e511ce8a0 2905
mjr 38:091e511ce8a0 2906 case 3:
mjr 38:091e511ce8a0 2907 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 2908 if (!bs->physState)
mjr 53:9b2611964afc 2909 {
mjr 38:091e511ce8a0 2910 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 2911 bs->pulseState = 4;
mjr 53:9b2611964afc 2912 bs->logState = 1;
mjr 38:091e511ce8a0 2913 }
mjr 38:091e511ce8a0 2914 break;
mjr 38:091e511ce8a0 2915
mjr 38:091e511ce8a0 2916 case 4:
mjr 38:091e511ce8a0 2917 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 2918 bs->pulseState = 1;
mjr 38:091e511ce8a0 2919 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 2920 bs->logState = 0;
mjr 38:091e511ce8a0 2921 break;
mjr 18:5e890ebd0023 2922 }
mjr 18:5e890ebd0023 2923 }
mjr 38:091e511ce8a0 2924 }
mjr 38:091e511ce8a0 2925 else
mjr 38:091e511ce8a0 2926 {
mjr 38:091e511ce8a0 2927 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 2928 bs->logState = bs->physState;
mjr 38:091e511ce8a0 2929 }
mjr 77:0b96f6867312 2930
mjr 77:0b96f6867312 2931 // Determine if we're going to use the shifted version of the
mjr 77:0b96f6867312 2932 // button. We're using the shifted version if the shift button
mjr 77:0b96f6867312 2933 // is down AND the button has ANY shifted meaning - a key assignment,
mjr 77:0b96f6867312 2934 // a Night Mode toggle assignment, or an IR code. If the button
mjr 77:0b96f6867312 2935 // doesn't have any meaning at all in shifted mode, the base version
mjr 77:0b96f6867312 2936 // of the button applies whether or not the shift button is down.
mjr 77:0b96f6867312 2937 //
mjr 77:0b96f6867312 2938 // Note that the test for Night Mode is a bit tricky. The shifted
mjr 77:0b96f6867312 2939 // version of the button is the Night Mode toggle if the button matches
mjr 77:0b96f6867312 2940 // the Night Mode button index, AND its flags are set with "toggle
mjr 77:0b96f6867312 2941 // mode ON" (bit 0x02 is on) and "switch mode OFF" (bit 0x01 is off).
mjr 77:0b96f6867312 2942 // That means the button flags & 0x03 must equal 0x02.
mjr 77:0b96f6867312 2943 bool useShift =
mjr 77:0b96f6867312 2944 (shiftButton.state != 0
mjr 77:0b96f6867312 2945 && (bc->typ2 != BtnTypeNone
mjr 77:0b96f6867312 2946 || bc->IRCommand2 != 0
mjr 77:0b96f6867312 2947 || (cfg.nightMode.btn == i+1 && (cfg.nightMode.flags & 0x03) == 0x02)));
mjr 77:0b96f6867312 2948
mjr 77:0b96f6867312 2949 // If we're using the shift function, and no other button has used
mjr 77:0b96f6867312 2950 // the shift function yet (shift state 1: "shift button is down but
mjr 77:0b96f6867312 2951 // no one has used the shift function yet"), then we've "consumed"
mjr 77:0b96f6867312 2952 // the shift button press (so go to shift state 2: "shift button has
mjr 77:0b96f6867312 2953 // been used by some other button press that has a shifted meaning").
mjr 77:0b96f6867312 2954 if (useShift && shiftButton.state == 1)
mjr 77:0b96f6867312 2955 shiftButton.state = 2;
mjr 35:e959ffba78fd 2956
mjr 38:091e511ce8a0 2957 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 2958 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 2959 {
mjr 77:0b96f6867312 2960 // check to see if this is the Night Mode button
mjr 53:9b2611964afc 2961 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 2962 {
mjr 77:0b96f6867312 2963 // Check the switch type in the config flags. If flag 0x01 is
mjr 77:0b96f6867312 2964 // set, it's a persistent on/off switch, so the night mode
mjr 77:0b96f6867312 2965 // state simply tracks the current state of the switch.
mjr 77:0b96f6867312 2966 // Otherwise, it's a momentary button, so each button push
mjr 77:0b96f6867312 2967 // (i.e., each transition from logical state OFF to ON) toggles
mjr 77:0b96f6867312 2968 // the night mode state.
mjr 77:0b96f6867312 2969 //
mjr 77:0b96f6867312 2970 // Note that the "shift" flag (0x02) has no effect in switch
mjr 77:0b96f6867312 2971 // mode. Shifting only works for toggle mode.
mjr 53:9b2611964afc 2972 if (cfg.nightMode.flags & 0x01)
mjr 53:9b2611964afc 2973 {
mjr 77:0b96f6867312 2974 // It's an on/off switch. Night mode simply tracks the
mjr 77:0b96f6867312 2975 // current switch state.
mjr 53:9b2611964afc 2976 setNightMode(bs->logState);
mjr 53:9b2611964afc 2977 }
mjr 53:9b2611964afc 2978 else
mjr 53:9b2611964afc 2979 {
mjr 77:0b96f6867312 2980 // It's a momentary toggle switch. Toggle the night mode
mjr 77:0b96f6867312 2981 // state on each distinct press of the button: that is,
mjr 77:0b96f6867312 2982 // whenever the button's logical state transitions from
mjr 77:0b96f6867312 2983 // OFF to ON.
mjr 66:2e3583fbd2f4 2984 //
mjr 77:0b96f6867312 2985 // The "shift" flag (0x02) tells us whether night mode is
mjr 77:0b96f6867312 2986 // assigned to the shifted or unshifted version of the
mjr 77:0b96f6867312 2987 // button.
mjr 77:0b96f6867312 2988 bool pressed;
mjr 66:2e3583fbd2f4 2989 if ((cfg.nightMode.flags & 0x02) != 0)
mjr 66:2e3583fbd2f4 2990 {
mjr 77:0b96f6867312 2991 // Shift bit is set - night mode is assigned to the
mjr 77:0b96f6867312 2992 // shifted version of the button. This is a Night
mjr 77:0b96f6867312 2993 // Mode toggle only if the Shift button is pressed.
mjr 77:0b96f6867312 2994 pressed = (shiftButton.state != 0);
mjr 77:0b96f6867312 2995 }
mjr 77:0b96f6867312 2996 else
mjr 77:0b96f6867312 2997 {
mjr 77:0b96f6867312 2998 // No shift bit - night mode is assigned to the
mjr 77:0b96f6867312 2999 // regular unshifted button. The button press only
mjr 77:0b96f6867312 3000 // applies if the Shift button is NOT pressed.
mjr 77:0b96f6867312 3001 pressed = (shiftButton.state == 0);
mjr 66:2e3583fbd2f4 3002 }
mjr 66:2e3583fbd2f4 3003
mjr 66:2e3583fbd2f4 3004 // if it's pressed (even after considering the shift mode),
mjr 66:2e3583fbd2f4 3005 // toggle night mode
mjr 66:2e3583fbd2f4 3006 if (pressed)
mjr 53:9b2611964afc 3007 toggleNightMode();
mjr 53:9b2611964afc 3008 }
mjr 35:e959ffba78fd 3009 }
mjr 38:091e511ce8a0 3010
mjr 77:0b96f6867312 3011 // press or release IR virtual keys on key state changes
mjr 77:0b96f6867312 3012 uint8_t irc = useShift ? bc->IRCommand2 : bc->IRCommand;
mjr 77:0b96f6867312 3013 if (irc != 0)
mjr 77:0b96f6867312 3014 IR_buttonChange(irc, bs->logState);
mjr 77:0b96f6867312 3015
mjr 38:091e511ce8a0 3016 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 3017 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 3018 }
mjr 38:091e511ce8a0 3019
mjr 53:9b2611964afc 3020 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 3021 // key state list
mjr 53:9b2611964afc 3022 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 3023 {
mjr 70:9f58735a1732 3024 // Get the key type and code. Start by assuming that we're
mjr 70:9f58735a1732 3025 // going to use the normal unshifted meaning.
mjr 77:0b96f6867312 3026 uint8_t typ, val;
mjr 77:0b96f6867312 3027 if (useShift)
mjr 66:2e3583fbd2f4 3028 {
mjr 77:0b96f6867312 3029 typ = bc->typ2;
mjr 77:0b96f6867312 3030 val = bc->val2;
mjr 66:2e3583fbd2f4 3031 }
mjr 77:0b96f6867312 3032 else
mjr 77:0b96f6867312 3033 {
mjr 77:0b96f6867312 3034 typ = bc->typ;
mjr 77:0b96f6867312 3035 val = bc->val;
mjr 77:0b96f6867312 3036 }
mjr 77:0b96f6867312 3037
mjr 70:9f58735a1732 3038 // We've decided on the meaning of the button, so process
mjr 70:9f58735a1732 3039 // the keyboard or joystick event.
mjr 77:0b96f6867312 3040 ks.addKey(typ, val);
mjr 18:5e890ebd0023 3041 }
mjr 11:bd9da7088e6e 3042 }
mjr 77:0b96f6867312 3043
mjr 77:0b96f6867312 3044 // If an IR input command is in effect, add the IR command's
mjr 77:0b96f6867312 3045 // assigned key, if any. If we're in an IR key gap, don't include
mjr 77:0b96f6867312 3046 // the IR key.
mjr 77:0b96f6867312 3047 if (IRCommandIn != 0 && !IRKeyGap)
mjr 77:0b96f6867312 3048 {
mjr 77:0b96f6867312 3049 IRCommandCfg &irc = cfg.IRCommand[IRCommandIn - 1];
mjr 77:0b96f6867312 3050 ks.addKey(irc.keytype, irc.keycode);
mjr 77:0b96f6867312 3051 }
mjr 77:0b96f6867312 3052
mjr 77:0b96f6867312 3053 // We're finished building the new key state. Update the global
mjr 77:0b96f6867312 3054 // key state variables to reflect the new state.
mjr 77:0b96f6867312 3055
mjr 77:0b96f6867312 3056 // set the new joystick buttons (no need to check for changes, as we
mjr 77:0b96f6867312 3057 // report these on every joystick report whether they changed or not)
mjr 77:0b96f6867312 3058 jsButtons = ks.js;
mjr 77:0b96f6867312 3059
mjr 77:0b96f6867312 3060 // check for keyboard key changes (we only send keyboard reports when
mjr 77:0b96f6867312 3061 // something changes)
mjr 77:0b96f6867312 3062 if (kbState.data[0] != ks.modkeys
mjr 77:0b96f6867312 3063 || kbState.nkeys != ks.nkeys
mjr 77:0b96f6867312 3064 || memcmp(ks.keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 3065 {
mjr 35:e959ffba78fd 3066 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3067 kbState.changed = true;
mjr 77:0b96f6867312 3068 kbState.data[0] = ks.modkeys;
mjr 77:0b96f6867312 3069 if (ks.nkeys <= 6) {
mjr 35:e959ffba78fd 3070 // 6 or fewer simultaneous keys - report the key codes
mjr 77:0b96f6867312 3071 kbState.nkeys = ks.nkeys;
mjr 77:0b96f6867312 3072 memcpy(&kbState.data[2], ks.keys, 6);
mjr 35:e959ffba78fd 3073 }
mjr 35:e959ffba78fd 3074 else {
mjr 35:e959ffba78fd 3075 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 3076 kbState.nkeys = 6;
mjr 35:e959ffba78fd 3077 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 3078 }
mjr 35:e959ffba78fd 3079 }
mjr 35:e959ffba78fd 3080
mjr 77:0b96f6867312 3081 // check for media key changes (we only send media key reports when
mjr 77:0b96f6867312 3082 // something changes)
mjr 77:0b96f6867312 3083 if (mediaState.data != ks.mediakeys)
mjr 35:e959ffba78fd 3084 {
mjr 77:0b96f6867312 3085 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3086 mediaState.changed = true;
mjr 77:0b96f6867312 3087 mediaState.data = ks.mediakeys;
mjr 35:e959ffba78fd 3088 }
mjr 11:bd9da7088e6e 3089 }
mjr 11:bd9da7088e6e 3090
mjr 73:4e8ce0b18915 3091 // Send a button status report
mjr 73:4e8ce0b18915 3092 void reportButtonStatus(USBJoystick &js)
mjr 73:4e8ce0b18915 3093 {
mjr 73:4e8ce0b18915 3094 // start with all buttons off
mjr 73:4e8ce0b18915 3095 uint8_t state[(MAX_BUTTONS+7)/8];
mjr 73:4e8ce0b18915 3096 memset(state, 0, sizeof(state));
mjr 73:4e8ce0b18915 3097
mjr 73:4e8ce0b18915 3098 // pack the button states into bytes, one bit per button
mjr 73:4e8ce0b18915 3099 ButtonState *bs = buttonState;
mjr 73:4e8ce0b18915 3100 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 73:4e8ce0b18915 3101 {
mjr 73:4e8ce0b18915 3102 // get the physical state
mjr 73:4e8ce0b18915 3103 int b = bs->physState;
mjr 73:4e8ce0b18915 3104
mjr 73:4e8ce0b18915 3105 // pack it into the appropriate bit
mjr 73:4e8ce0b18915 3106 int idx = bs->cfgIndex;
mjr 73:4e8ce0b18915 3107 int si = idx / 8;
mjr 73:4e8ce0b18915 3108 int shift = idx & 0x07;
mjr 73:4e8ce0b18915 3109 state[si] |= b << shift;
mjr 73:4e8ce0b18915 3110 }
mjr 73:4e8ce0b18915 3111
mjr 73:4e8ce0b18915 3112 // send the report
mjr 73:4e8ce0b18915 3113 js.reportButtonStatus(MAX_BUTTONS, state);
mjr 73:4e8ce0b18915 3114 }
mjr 73:4e8ce0b18915 3115
mjr 5:a70c0bce770d 3116 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3117 //
mjr 5:a70c0bce770d 3118 // Customization joystick subbclass
mjr 5:a70c0bce770d 3119 //
mjr 5:a70c0bce770d 3120
mjr 5:a70c0bce770d 3121 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 3122 {
mjr 5:a70c0bce770d 3123 public:
mjr 35:e959ffba78fd 3124 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 35:e959ffba78fd 3125 bool waitForConnect, bool enableJoystick, bool useKB)
mjr 35:e959ffba78fd 3126 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, useKB)
mjr 5:a70c0bce770d 3127 {
mjr 54:fd77a6b2f76c 3128 sleeping_ = false;
mjr 54:fd77a6b2f76c 3129 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3130 timer_.start();
mjr 54:fd77a6b2f76c 3131 }
mjr 54:fd77a6b2f76c 3132
mjr 54:fd77a6b2f76c 3133 // show diagnostic LED feedback for connect state
mjr 54:fd77a6b2f76c 3134 void diagFlash()
mjr 54:fd77a6b2f76c 3135 {
mjr 54:fd77a6b2f76c 3136 if (!configured() || sleeping_)
mjr 54:fd77a6b2f76c 3137 {
mjr 54:fd77a6b2f76c 3138 // flash once if sleeping or twice if disconnected
mjr 54:fd77a6b2f76c 3139 for (int j = isConnected() ? 1 : 2 ; j > 0 ; --j)
mjr 54:fd77a6b2f76c 3140 {
mjr 54:fd77a6b2f76c 3141 // short red flash
mjr 54:fd77a6b2f76c 3142 diagLED(1, 0, 0);
mjr 54:fd77a6b2f76c 3143 wait_us(50000);
mjr 54:fd77a6b2f76c 3144 diagLED(0, 0, 0);
mjr 54:fd77a6b2f76c 3145 wait_us(50000);
mjr 54:fd77a6b2f76c 3146 }
mjr 54:fd77a6b2f76c 3147 }
mjr 5:a70c0bce770d 3148 }
mjr 5:a70c0bce770d 3149
mjr 5:a70c0bce770d 3150 // are we connected?
mjr 5:a70c0bce770d 3151 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 3152
mjr 54:fd77a6b2f76c 3153 // Are we in sleep mode? If true, this means that the hardware has
mjr 54:fd77a6b2f76c 3154 // detected no activity on the bus for 3ms. This happens when the
mjr 54:fd77a6b2f76c 3155 // cable is physically disconnected, the computer is turned off, or
mjr 54:fd77a6b2f76c 3156 // the connection is otherwise disabled.
mjr 54:fd77a6b2f76c 3157 bool isSleeping() const { return sleeping_; }
mjr 54:fd77a6b2f76c 3158
mjr 54:fd77a6b2f76c 3159 // If necessary, attempt to recover from a broken connection.
mjr 54:fd77a6b2f76c 3160 //
mjr 54:fd77a6b2f76c 3161 // This is a hack, to work around an apparent timing bug in the
mjr 54:fd77a6b2f76c 3162 // KL25Z USB implementation that I haven't been able to solve any
mjr 54:fd77a6b2f76c 3163 // other way.
mjr 54:fd77a6b2f76c 3164 //
mjr 54:fd77a6b2f76c 3165 // The issue: when we have an established connection, and the
mjr 54:fd77a6b2f76c 3166 // connection is broken by physically unplugging the cable or by
mjr 54:fd77a6b2f76c 3167 // rebooting the PC, the KL25Z sometimes fails to reconnect when
mjr 54:fd77a6b2f76c 3168 // the physical connection is re-established. The failure is
mjr 54:fd77a6b2f76c 3169 // sporadic; I'd guess it happens about 25% of the time, but I
mjr 54:fd77a6b2f76c 3170 // haven't collected any real statistics on it.
mjr 54:fd77a6b2f76c 3171 //
mjr 54:fd77a6b2f76c 3172 // The proximate cause of the failure is a deadlock in the SETUP
mjr 54:fd77a6b2f76c 3173 // protocol between the host and device that happens around the
mjr 54:fd77a6b2f76c 3174 // point where the PC is requesting the configuration descriptor.
mjr 54:fd77a6b2f76c 3175 // The exact point in the protocol where this occurs varies slightly;
mjr 54:fd77a6b2f76c 3176 // it can occur a message or two before or after the Get Config
mjr 54:fd77a6b2f76c 3177 // Descriptor packet. No matter where it happens, the nature of
mjr 54:fd77a6b2f76c 3178 // the deadlock is the same: the PC thinks it sees a STALL on EP0
mjr 54:fd77a6b2f76c 3179 // from the device, so it terminates the connection attempt, which
mjr 54:fd77a6b2f76c 3180 // stops further traffic on the cable. The KL25Z USB hardware sees
mjr 54:fd77a6b2f76c 3181 // the lack of traffic and triggers a SLEEP interrupt (a misnomer
mjr 54:fd77a6b2f76c 3182 // for what should have been called a BROKEN CONNECTION interrupt).
mjr 54:fd77a6b2f76c 3183 // Both sides simply stop talking at this point, so the connection
mjr 54:fd77a6b2f76c 3184 // is effectively dead.
mjr 54:fd77a6b2f76c 3185 //
mjr 54:fd77a6b2f76c 3186 // The strange thing is that, as far as I can tell, the KL25Z isn't
mjr 54:fd77a6b2f76c 3187 // doing anything to trigger the STALL on its end. Both the PC
mjr 54:fd77a6b2f76c 3188 // and the KL25Z are happy up until the very point of the failure
mjr 54:fd77a6b2f76c 3189 // and show no signs of anything wrong in the protocol exchange.
mjr 54:fd77a6b2f76c 3190 // In fact, every detail of the protocol exchange up to this point
mjr 54:fd77a6b2f76c 3191 // is identical to every successful exchange that does finish the
mjr 54:fd77a6b2f76c 3192 // whole setup process successfully, on both the KL25Z and Windows
mjr 54:fd77a6b2f76c 3193 // sides of the connection. I can't find any point of difference
mjr 54:fd77a6b2f76c 3194 // between successful and unsuccessful sequences that suggests why
mjr 54:fd77a6b2f76c 3195 // the fateful message fails. This makes me suspect that whatever
mjr 54:fd77a6b2f76c 3196 // is going wrong is inside the KL25Z USB hardware module, which
mjr 54:fd77a6b2f76c 3197 // is a pretty substantial black box - it has a lot of internal
mjr 54:fd77a6b2f76c 3198 // state that's inaccessible to the software. Further bolstering
mjr 54:fd77a6b2f76c 3199 // this theory is a little experiment where I found that I could
mjr 54:fd77a6b2f76c 3200 // reproduce the exact sequence of events of a failed reconnect
mjr 54:fd77a6b2f76c 3201 // attempt in an *initial* connection, which is otherwise 100%
mjr 54:fd77a6b2f76c 3202 // reliable, by inserting a little bit of artifical time padding
mjr 54:fd77a6b2f76c 3203 // (200us per event) into the SETUP interrupt handler. My
mjr 54:fd77a6b2f76c 3204 // hypothesis is that the STALL event happens because the KL25Z
mjr 54:fd77a6b2f76c 3205 // USB hardware is too slow to respond to a message. I'm not
mjr 54:fd77a6b2f76c 3206 // sure why this would only happen after a disconnect and not
mjr 54:fd77a6b2f76c 3207 // during the initial connection; maybe there's some reset work
mjr 54:fd77a6b2f76c 3208 // in the hardware that takes a substantial amount of time after
mjr 54:fd77a6b2f76c 3209 // a disconnect.
mjr 54:fd77a6b2f76c 3210 //
mjr 54:fd77a6b2f76c 3211 // The solution: the problem happens during the SETUP exchange,
mjr 54:fd77a6b2f76c 3212 // after we've been assigned a bus address. It only happens on
mjr 54:fd77a6b2f76c 3213 // some percentage of connection requests, so if we can simply
mjr 54:fd77a6b2f76c 3214 // start over when the failure occurs, we'll eventually succeed
mjr 54:fd77a6b2f76c 3215 // simply because not every attempt fails. The ideal would be
mjr 54:fd77a6b2f76c 3216 // to get the success rate up to 100%, but I can't figure out how
mjr 54:fd77a6b2f76c 3217 // to fix the underlying problem, so this is the next best thing.
mjr 54:fd77a6b2f76c 3218 //
mjr 54:fd77a6b2f76c 3219 // We can detect when the failure occurs by noticing when a SLEEP
mjr 54:fd77a6b2f76c 3220 // interrupt happens while we have an assigned bus address.
mjr 54:fd77a6b2f76c 3221 //
mjr 54:fd77a6b2f76c 3222 // To start a new connection attempt, we have to make the *host*
mjr 54:fd77a6b2f76c 3223 // try again. The logical connection is initiated solely by the
mjr 54:fd77a6b2f76c 3224 // host. Fortunately, it's easy to get the host to initiate the
mjr 54:fd77a6b2f76c 3225 // process: if we disconnect on the device side, it effectively
mjr 54:fd77a6b2f76c 3226 // makes the device look to the PC like it's electrically unplugged.
mjr 54:fd77a6b2f76c 3227 // When we reconnect on the device side, the PC thinks a new device
mjr 54:fd77a6b2f76c 3228 // has been plugged in and initiates the logical connection setup.
mjr 74:822a92bc11d2 3229 // We have to remain disconnected for some minimum interval before
mjr 74:822a92bc11d2 3230 // the host notices; the exact minimum is unclear, but 5ms seems
mjr 74:822a92bc11d2 3231 // reliable in practice.
mjr 54:fd77a6b2f76c 3232 //
mjr 54:fd77a6b2f76c 3233 // Here's the full algorithm:
mjr 54:fd77a6b2f76c 3234 //
mjr 54:fd77a6b2f76c 3235 // 1. In the SLEEP interrupt handler, if we have a bus address,
mjr 54:fd77a6b2f76c 3236 // we disconnect the device. This happens in ISR context, so we
mjr 54:fd77a6b2f76c 3237 // can't wait around for 5ms. Instead, we simply set a flag noting
mjr 54:fd77a6b2f76c 3238 // that the connection has been broken, and we note the time and
mjr 54:fd77a6b2f76c 3239 // return.
mjr 54:fd77a6b2f76c 3240 //
mjr 54:fd77a6b2f76c 3241 // 2. In our main loop, whenever we find that we're disconnected,
mjr 54:fd77a6b2f76c 3242 // we call recoverConnection(). The main loop's job is basically a
mjr 54:fd77a6b2f76c 3243 // bunch of device polling. We're just one more device to poll, so
mjr 54:fd77a6b2f76c 3244 // recoverConnection() will be called soon after a disconnect, and
mjr 54:fd77a6b2f76c 3245 // then will be called in a loop for as long as we're disconnected.
mjr 54:fd77a6b2f76c 3246 //
mjr 54:fd77a6b2f76c 3247 // 3. In recoverConnection(), we check the flag we set in the SLEEP
mjr 54:fd77a6b2f76c 3248 // handler. If set, we wait until 5ms has elapsed from the SLEEP
mjr 54:fd77a6b2f76c 3249 // event time that we noted, then we'll reconnect and clear the flag.
mjr 54:fd77a6b2f76c 3250 // This gives us the required 5ms (or longer) delay between the
mjr 54:fd77a6b2f76c 3251 // disconnect and reconnect, ensuring that the PC will notice and
mjr 54:fd77a6b2f76c 3252 // will start over with the connection protocol.
mjr 54:fd77a6b2f76c 3253 //
mjr 54:fd77a6b2f76c 3254 // 4. The main loop keeps calling recoverConnection() in a loop for
mjr 54:fd77a6b2f76c 3255 // as long as we're disconnected, so if the new connection attempt
mjr 54:fd77a6b2f76c 3256 // triggered in step 3 fails, the SLEEP interrupt will happen again,
mjr 54:fd77a6b2f76c 3257 // we'll disconnect again, the flag will get set again, and
mjr 54:fd77a6b2f76c 3258 // recoverConnection() will reconnect again after another suitable
mjr 54:fd77a6b2f76c 3259 // delay. This will repeat until the connection succeeds or hell
mjr 54:fd77a6b2f76c 3260 // freezes over.
mjr 54:fd77a6b2f76c 3261 //
mjr 54:fd77a6b2f76c 3262 // Each disconnect happens immediately when a reconnect attempt
mjr 54:fd77a6b2f76c 3263 // fails, and an entire successful connection only takes about 25ms,
mjr 54:fd77a6b2f76c 3264 // so our loop can retry at more than 30 attempts per second.
mjr 54:fd77a6b2f76c 3265 // In my testing, lost connections almost always reconnect in
mjr 54:fd77a6b2f76c 3266 // less than second with this code in place.
mjr 54:fd77a6b2f76c 3267 void recoverConnection()
mjr 54:fd77a6b2f76c 3268 {
mjr 54:fd77a6b2f76c 3269 // if a reconnect is pending, reconnect
mjr 54:fd77a6b2f76c 3270 if (reconnectPending_)
mjr 54:fd77a6b2f76c 3271 {
mjr 54:fd77a6b2f76c 3272 // Loop until we reach 5ms after the last sleep event.
mjr 54:fd77a6b2f76c 3273 for (bool done = false ; !done ; )
mjr 54:fd77a6b2f76c 3274 {
mjr 54:fd77a6b2f76c 3275 // If we've reached the target time, reconnect. Do the
mjr 54:fd77a6b2f76c 3276 // time check and flag reset atomically, so that we can't
mjr 54:fd77a6b2f76c 3277 // have another sleep event sneak in after we've verified
mjr 54:fd77a6b2f76c 3278 // the time. If another event occurs, it has to happen
mjr 54:fd77a6b2f76c 3279 // before we check, in which case it'll update the time
mjr 54:fd77a6b2f76c 3280 // before we check it, or after we clear the flag, in
mjr 54:fd77a6b2f76c 3281 // which case it will reset the flag and we'll do another
mjr 54:fd77a6b2f76c 3282 // round the next time we call this routine.
mjr 54:fd77a6b2f76c 3283 __disable_irq();
mjr 54:fd77a6b2f76c 3284 if (uint32_t(timer_.read_us() - lastSleepTime_) > 5000)
mjr 54:fd77a6b2f76c 3285 {
mjr 54:fd77a6b2f76c 3286 connect(false);
mjr 54:fd77a6b2f76c 3287 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3288 done = true;
mjr 54:fd77a6b2f76c 3289 }
mjr 54:fd77a6b2f76c 3290 __enable_irq();
mjr 54:fd77a6b2f76c 3291 }
mjr 54:fd77a6b2f76c 3292 }
mjr 54:fd77a6b2f76c 3293 }
mjr 5:a70c0bce770d 3294
mjr 5:a70c0bce770d 3295 protected:
mjr 54:fd77a6b2f76c 3296 // Handle a USB SLEEP interrupt. This interrupt signifies that the
mjr 54:fd77a6b2f76c 3297 // USB hardware module hasn't seen any token traffic for 3ms, which
mjr 54:fd77a6b2f76c 3298 // means that we're either physically or logically disconnected.
mjr 54:fd77a6b2f76c 3299 //
mjr 54:fd77a6b2f76c 3300 // Important: this runs in ISR context.
mjr 54:fd77a6b2f76c 3301 //
mjr 54:fd77a6b2f76c 3302 // Note that this is a specialized sense of "sleep" that's unrelated
mjr 54:fd77a6b2f76c 3303 // to the similarly named power modes on the PC. This has nothing
mjr 54:fd77a6b2f76c 3304 // to do with suspend/sleep mode on the PC, and it's not a low-power
mjr 54:fd77a6b2f76c 3305 // mode on the KL25Z. They really should have called this interrupt
mjr 54:fd77a6b2f76c 3306 // DISCONNECT or BROKEN CONNECTION.)
mjr 54:fd77a6b2f76c 3307 virtual void sleepStateChanged(unsigned int sleeping)
mjr 54:fd77a6b2f76c 3308 {
mjr 54:fd77a6b2f76c 3309 // note the new state
mjr 54:fd77a6b2f76c 3310 sleeping_ = sleeping;
mjr 54:fd77a6b2f76c 3311
mjr 54:fd77a6b2f76c 3312 // If we have a non-zero bus address, we have at least a partial
mjr 54:fd77a6b2f76c 3313 // connection to the host (we've made it at least as far as the
mjr 54:fd77a6b2f76c 3314 // SETUP stage). Explicitly disconnect, and the pending reconnect
mjr 54:fd77a6b2f76c 3315 // flag, and remember the time of the sleep event.
mjr 54:fd77a6b2f76c 3316 if (USB0->ADDR != 0x00)
mjr 54:fd77a6b2f76c 3317 {
mjr 54:fd77a6b2f76c 3318 disconnect();
mjr 54:fd77a6b2f76c 3319 lastSleepTime_ = timer_.read_us();
mjr 54:fd77a6b2f76c 3320 reconnectPending_ = true;
mjr 54:fd77a6b2f76c 3321 }
mjr 54:fd77a6b2f76c 3322 }
mjr 54:fd77a6b2f76c 3323
mjr 54:fd77a6b2f76c 3324 // is the USB connection asleep?
mjr 54:fd77a6b2f76c 3325 volatile bool sleeping_;
mjr 54:fd77a6b2f76c 3326
mjr 54:fd77a6b2f76c 3327 // flag: reconnect pending after sleep event
mjr 54:fd77a6b2f76c 3328 volatile bool reconnectPending_;
mjr 54:fd77a6b2f76c 3329
mjr 54:fd77a6b2f76c 3330 // time of last sleep event while connected
mjr 54:fd77a6b2f76c 3331 volatile uint32_t lastSleepTime_;
mjr 54:fd77a6b2f76c 3332
mjr 54:fd77a6b2f76c 3333 // timer to keep track of interval since last sleep event
mjr 54:fd77a6b2f76c 3334 Timer timer_;
mjr 5:a70c0bce770d 3335 };
mjr 5:a70c0bce770d 3336
mjr 5:a70c0bce770d 3337 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3338 //
mjr 5:a70c0bce770d 3339 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 3340 //
mjr 5:a70c0bce770d 3341
mjr 5:a70c0bce770d 3342 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 3343 //
mjr 5:a70c0bce770d 3344 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 3345 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 3346 // automatic calibration.
mjr 5:a70c0bce770d 3347 //
mjr 77:0b96f6867312 3348 // We collect data at the device's maximum rate of 800kHz (one sample
mjr 77:0b96f6867312 3349 // every 1.25ms). To keep up with the high data rate, we use the
mjr 77:0b96f6867312 3350 // device's internal FIFO, and drain the FIFO by polling on each
mjr 77:0b96f6867312 3351 // iteration of our main application loop. In the past, we used an
mjr 77:0b96f6867312 3352 // interrupt handler to read the device immediately on the arrival of
mjr 77:0b96f6867312 3353 // each sample, but this created too much latency for the IR remote
mjr 77:0b96f6867312 3354 // receiver, due to the relatively long time it takes to transfer the
mjr 77:0b96f6867312 3355 // accelerometer readings via I2C. The device's on-board FIFO can
mjr 77:0b96f6867312 3356 // store up to 32 samples, which gives us up to about 40ms between
mjr 77:0b96f6867312 3357 // polling iterations before the buffer overflows. Our main loop runs
mjr 77:0b96f6867312 3358 // in under 2ms, so we can easily keep the FIFO far from overflowing.
mjr 77:0b96f6867312 3359 //
mjr 77:0b96f6867312 3360 // The MMA8451Q has three range modes, +/- 2G, 4G, and 8G. The ADC
mjr 77:0b96f6867312 3361 // sample is the same bit width (14 bits) in all modes, so the higher
mjr 77:0b96f6867312 3362 // dynamic range modes trade physical precision for range. For our
mjr 77:0b96f6867312 3363 // purposes, precision is more important than range, so we use the
mjr 77:0b96f6867312 3364 // +/-2G mode. Further, our joystick range is calibrated for only
mjr 77:0b96f6867312 3365 // +/-1G. This was unintentional on my part; I didn't look at the
mjr 77:0b96f6867312 3366 // MMA8451Q library closely enough to realize it was normalizing to
mjr 77:0b96f6867312 3367 // actual "G" units, and assumed that it was normalizing to a -1..+1
mjr 77:0b96f6867312 3368 // scale. In practice, a +/-1G scale seems perfectly adequate for
mjr 77:0b96f6867312 3369 // virtual pinball use, so I'm sticking with that range for now. But
mjr 77:0b96f6867312 3370 // there might be some benefit in renormalizing to a +/-2G range, in
mjr 77:0b96f6867312 3371 // that it would allow for higher dynamic range for very hard nudges.
mjr 77:0b96f6867312 3372 // Everyone would have to tweak their nudge sensitivity in VP if I
mjr 77:0b96f6867312 3373 // made that change, though, so I'm keeping it as is for now; it would
mjr 77:0b96f6867312 3374 // be best to make it a config option ("accelerometer high dynamic range")
mjr 77:0b96f6867312 3375 // rather than change it across the board.
mjr 5:a70c0bce770d 3376 //
mjr 6:cc35eb643e8f 3377 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 3378 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 3379 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 3380 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 3381 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 3382 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 3383 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 3384 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 3385 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 3386 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 3387 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 3388 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 3389 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 3390 // of nudging, say).
mjr 5:a70c0bce770d 3391 //
mjr 5:a70c0bce770d 3392
mjr 17:ab3cec0c8bf4 3393 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 3394 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 3395
mjr 17:ab3cec0c8bf4 3396 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 3397 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 3398 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 3399
mjr 17:ab3cec0c8bf4 3400 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 3401 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 3402 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 3403 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 3404
mjr 17:ab3cec0c8bf4 3405
mjr 6:cc35eb643e8f 3406 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 3407 struct AccHist
mjr 5:a70c0bce770d 3408 {
mjr 77:0b96f6867312 3409 AccHist() { x = y = dsq = 0; xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 3410 void set(int x, int y, AccHist *prv)
mjr 6:cc35eb643e8f 3411 {
mjr 6:cc35eb643e8f 3412 // save the raw position
mjr 6:cc35eb643e8f 3413 this->x = x;
mjr 6:cc35eb643e8f 3414 this->y = y;
mjr 77:0b96f6867312 3415 this->dsq = distanceSquared(prv);
mjr 6:cc35eb643e8f 3416 }
mjr 6:cc35eb643e8f 3417
mjr 6:cc35eb643e8f 3418 // reading for this entry
mjr 77:0b96f6867312 3419 int x, y;
mjr 77:0b96f6867312 3420
mjr 77:0b96f6867312 3421 // (distance from previous entry) squared
mjr 77:0b96f6867312 3422 int dsq;
mjr 5:a70c0bce770d 3423
mjr 6:cc35eb643e8f 3424 // total and count of samples averaged over this period
mjr 77:0b96f6867312 3425 int xtot, ytot;
mjr 6:cc35eb643e8f 3426 int cnt;
mjr 6:cc35eb643e8f 3427
mjr 77:0b96f6867312 3428 void clearAvg() { xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 3429 void addAvg(int x, int y) { xtot += x; ytot += y; ++cnt; }
mjr 77:0b96f6867312 3430 int xAvg() const { return xtot/cnt; }
mjr 77:0b96f6867312 3431 int yAvg() const { return ytot/cnt; }
mjr 77:0b96f6867312 3432
mjr 77:0b96f6867312 3433 int distanceSquared(AccHist *p)
mjr 77:0b96f6867312 3434 { return square(p->x - x) + square(p->y - y); }
mjr 5:a70c0bce770d 3435 };
mjr 5:a70c0bce770d 3436
mjr 5:a70c0bce770d 3437 // accelerometer wrapper class
mjr 3:3514575d4f86 3438 class Accel
mjr 3:3514575d4f86 3439 {
mjr 3:3514575d4f86 3440 public:
mjr 77:0b96f6867312 3441 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin, int range)
mjr 77:0b96f6867312 3442 : mma_(sda, scl, i2cAddr)
mjr 3:3514575d4f86 3443 {
mjr 5:a70c0bce770d 3444 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 3445 irqPin_ = irqPin;
mjr 77:0b96f6867312 3446
mjr 77:0b96f6867312 3447 // remember the range
mjr 77:0b96f6867312 3448 range_ = range;
mjr 5:a70c0bce770d 3449
mjr 5:a70c0bce770d 3450 // reset and initialize
mjr 5:a70c0bce770d 3451 reset();
mjr 5:a70c0bce770d 3452 }
mjr 5:a70c0bce770d 3453
mjr 5:a70c0bce770d 3454 void reset()
mjr 5:a70c0bce770d 3455 {
mjr 6:cc35eb643e8f 3456 // clear the center point
mjr 77:0b96f6867312 3457 cx_ = cy_ = 0;
mjr 6:cc35eb643e8f 3458
mjr 77:0b96f6867312 3459 // start the auto-centering timer
mjr 5:a70c0bce770d 3460 tCenter_.start();
mjr 5:a70c0bce770d 3461 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 3462
mjr 5:a70c0bce770d 3463 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 3464 mma_.init();
mjr 77:0b96f6867312 3465
mjr 77:0b96f6867312 3466 // set the range
mjr 77:0b96f6867312 3467 mma_.setRange(
mjr 77:0b96f6867312 3468 range_ == AccelRange4G ? 4 :
mjr 77:0b96f6867312 3469 range_ == AccelRange8G ? 8 :
mjr 77:0b96f6867312 3470 2);
mjr 6:cc35eb643e8f 3471
mjr 77:0b96f6867312 3472 // set the average accumulators to zero
mjr 77:0b96f6867312 3473 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 3474 nSum_ = 0;
mjr 3:3514575d4f86 3475
mjr 3:3514575d4f86 3476 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 3477 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 3478 }
mjr 3:3514575d4f86 3479
mjr 77:0b96f6867312 3480 void poll()
mjr 76:7f5912b6340e 3481 {
mjr 77:0b96f6867312 3482 // read samples until we clear the FIFO
mjr 77:0b96f6867312 3483 while (mma_.getFIFOCount() != 0)
mjr 77:0b96f6867312 3484 {
mjr 77:0b96f6867312 3485 int x, y, z;
mjr 77:0b96f6867312 3486 mma_.getAccXYZ(x, y, z);
mjr 77:0b96f6867312 3487
mjr 77:0b96f6867312 3488 // add the new reading to the running total for averaging
mjr 77:0b96f6867312 3489 xSum_ += (x - cx_);
mjr 77:0b96f6867312 3490 ySum_ += (y - cy_);
mjr 77:0b96f6867312 3491 ++nSum_;
mjr 77:0b96f6867312 3492
mjr 77:0b96f6867312 3493 // store the updates
mjr 77:0b96f6867312 3494 ax_ = x;
mjr 77:0b96f6867312 3495 ay_ = y;
mjr 77:0b96f6867312 3496 az_ = z;
mjr 77:0b96f6867312 3497 }
mjr 76:7f5912b6340e 3498 }
mjr 77:0b96f6867312 3499
mjr 9:fd65b0a94720 3500 void get(int &x, int &y)
mjr 3:3514575d4f86 3501 {
mjr 77:0b96f6867312 3502 // read the shared data and store locally for calculations
mjr 77:0b96f6867312 3503 int ax = ax_, ay = ay_;
mjr 77:0b96f6867312 3504 int xSum = xSum_, ySum = ySum_;
mjr 77:0b96f6867312 3505 int nSum = nSum_;
mjr 6:cc35eb643e8f 3506
mjr 77:0b96f6867312 3507 // reset the average accumulators for the next run
mjr 77:0b96f6867312 3508 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 3509 nSum_ = 0;
mjr 77:0b96f6867312 3510
mjr 77:0b96f6867312 3511 // add this sample to the current calibration interval's running total
mjr 77:0b96f6867312 3512 AccHist *p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 3513 p->addAvg(ax, ay);
mjr 77:0b96f6867312 3514
mjr 77:0b96f6867312 3515 // check for auto-centering every so often
mjr 77:0b96f6867312 3516 if (tCenter_.read_us() > 1000000)
mjr 77:0b96f6867312 3517 {
mjr 77:0b96f6867312 3518 // add the latest raw sample to the history list
mjr 77:0b96f6867312 3519 AccHist *prv = p;
mjr 77:0b96f6867312 3520 iAccPrv_ = (iAccPrv_ + 1);
mjr 77:0b96f6867312 3521 if (iAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 3522 iAccPrv_ = 0;
mjr 77:0b96f6867312 3523 p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 3524 p->set(ax, ay, prv);
mjr 77:0b96f6867312 3525
mjr 77:0b96f6867312 3526 // if we have a full complement, check for stability
mjr 77:0b96f6867312 3527 if (nAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 3528 {
mjr 77:0b96f6867312 3529 // check if we've been stable for all recent samples
mjr 77:0b96f6867312 3530 static const int accTol = 164*164; // 1% of range, squared
mjr 77:0b96f6867312 3531 AccHist *p0 = accPrv_;
mjr 77:0b96f6867312 3532 if (p0[0].dsq < accTol
mjr 77:0b96f6867312 3533 && p0[1].dsq < accTol
mjr 77:0b96f6867312 3534 && p0[2].dsq < accTol
mjr 77:0b96f6867312 3535 && p0[3].dsq < accTol
mjr 77:0b96f6867312 3536 && p0[4].dsq < accTol)
mjr 77:0b96f6867312 3537 {
mjr 77:0b96f6867312 3538 // Figure the new calibration point as the average of
mjr 77:0b96f6867312 3539 // the samples over the rest period
mjr 77:0b96f6867312 3540 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5;
mjr 77:0b96f6867312 3541 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5;
mjr 77:0b96f6867312 3542 }
mjr 77:0b96f6867312 3543 }
mjr 77:0b96f6867312 3544 else
mjr 77:0b96f6867312 3545 {
mjr 77:0b96f6867312 3546 // not enough samples yet; just up the count
mjr 77:0b96f6867312 3547 ++nAccPrv_;
mjr 77:0b96f6867312 3548 }
mjr 6:cc35eb643e8f 3549
mjr 77:0b96f6867312 3550 // clear the new item's running totals
mjr 77:0b96f6867312 3551 p->clearAvg();
mjr 5:a70c0bce770d 3552
mjr 77:0b96f6867312 3553 // reset the timer
mjr 77:0b96f6867312 3554 tCenter_.reset();
mjr 77:0b96f6867312 3555 }
mjr 5:a70c0bce770d 3556
mjr 77:0b96f6867312 3557 // report our integrated velocity reading in x,y
mjr 77:0b96f6867312 3558 x = rawToReport(xSum/nSum);
mjr 77:0b96f6867312 3559 y = rawToReport(ySum/nSum);
mjr 5:a70c0bce770d 3560
mjr 6:cc35eb643e8f 3561 #ifdef DEBUG_PRINTF
mjr 77:0b96f6867312 3562 if (x != 0 || y != 0)
mjr 77:0b96f6867312 3563 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 3564 #endif
mjr 77:0b96f6867312 3565 }
mjr 29:582472d0bc57 3566
mjr 3:3514575d4f86 3567 private:
mjr 6:cc35eb643e8f 3568 // adjust a raw acceleration figure to a usb report value
mjr 77:0b96f6867312 3569 int rawToReport(int v)
mjr 5:a70c0bce770d 3570 {
mjr 77:0b96f6867312 3571 // Scale to the joystick report range. The accelerometer
mjr 77:0b96f6867312 3572 // readings use the native 14-bit signed integer representation,
mjr 77:0b96f6867312 3573 // so their scale is 2^13.
mjr 77:0b96f6867312 3574 //
mjr 77:0b96f6867312 3575 // The 1G range is special: it uses the 2G native hardware range,
mjr 77:0b96f6867312 3576 // but rescales the result to a 1G range for the joystick reports.
mjr 77:0b96f6867312 3577 // So for that mode, we divide by 4096 rather than 8192. All of
mjr 77:0b96f6867312 3578 // the other modes map use the hardware scaling directly.
mjr 77:0b96f6867312 3579 int i = v*JOYMAX;
mjr 77:0b96f6867312 3580 i = (range_ == AccelRange1G ? i/4096 : i/8192);
mjr 5:a70c0bce770d 3581
mjr 6:cc35eb643e8f 3582 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 3583 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 3584 static const int filter[] = {
mjr 6:cc35eb643e8f 3585 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 3586 0,
mjr 6:cc35eb643e8f 3587 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 3588 };
mjr 6:cc35eb643e8f 3589 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 3590 }
mjr 5:a70c0bce770d 3591
mjr 3:3514575d4f86 3592 // underlying accelerometer object
mjr 3:3514575d4f86 3593 MMA8451Q mma_;
mjr 3:3514575d4f86 3594
mjr 77:0b96f6867312 3595 // last raw acceleration readings, on the device's signed 14-bit
mjr 77:0b96f6867312 3596 // scale -8192..+8191
mjr 77:0b96f6867312 3597 int ax_, ay_, az_;
mjr 77:0b96f6867312 3598
mjr 77:0b96f6867312 3599 // running sum of readings since last get()
mjr 77:0b96f6867312 3600 int xSum_, ySum_;
mjr 77:0b96f6867312 3601
mjr 77:0b96f6867312 3602 // number of readings since last get()
mjr 77:0b96f6867312 3603 int nSum_;
mjr 6:cc35eb643e8f 3604
mjr 6:cc35eb643e8f 3605 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 3606 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 3607 // at rest.
mjr 77:0b96f6867312 3608 int cx_, cy_;
mjr 77:0b96f6867312 3609
mjr 77:0b96f6867312 3610 // range (AccelRangeXxx value, from config.h)
mjr 77:0b96f6867312 3611 uint8_t range_;
mjr 77:0b96f6867312 3612
mjr 77:0b96f6867312 3613 // atuo-centering timer
mjr 5:a70c0bce770d 3614 Timer tCenter_;
mjr 6:cc35eb643e8f 3615
mjr 6:cc35eb643e8f 3616 // Auto-centering history. This is a separate history list that
mjr 77:0b96f6867312 3617 // records results spaced out sparsely over time, so that we can
mjr 6:cc35eb643e8f 3618 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 3619 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 3620 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 3621 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 3622 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 3623 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 3624 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 3625 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 3626 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 3627 // accelerometer measurement bias (e.g., from temperature changes).
mjr 5:a70c0bce770d 3628 int iAccPrv_, nAccPrv_;
mjr 5:a70c0bce770d 3629 static const int maxAccPrv = 5;
mjr 6:cc35eb643e8f 3630 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 3631
mjr 5:a70c0bce770d 3632 // interurupt pin name
mjr 5:a70c0bce770d 3633 PinName irqPin_;
mjr 3:3514575d4f86 3634 };
mjr 3:3514575d4f86 3635
mjr 5:a70c0bce770d 3636 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3637 //
mjr 14:df700b22ca08 3638 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 3639 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 3640 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 3641 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 3642 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 3643 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 3644 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 3645 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 3646 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 3647 //
mjr 14:df700b22ca08 3648 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 3649 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 3650 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 3651 //
mjr 5:a70c0bce770d 3652 void clear_i2c()
mjr 5:a70c0bce770d 3653 {
mjr 38:091e511ce8a0 3654 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 3655 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 3656 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 3657
mjr 5:a70c0bce770d 3658 // clock the SCL 9 times
mjr 5:a70c0bce770d 3659 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 3660 {
mjr 5:a70c0bce770d 3661 scl = 1;
mjr 5:a70c0bce770d 3662 wait_us(20);
mjr 5:a70c0bce770d 3663 scl = 0;
mjr 5:a70c0bce770d 3664 wait_us(20);
mjr 5:a70c0bce770d 3665 }
mjr 5:a70c0bce770d 3666 }
mjr 76:7f5912b6340e 3667
mjr 76:7f5912b6340e 3668
mjr 14:df700b22ca08 3669 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 3670 //
mjr 33:d832bcab089e 3671 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 3672 // for a given interval before allowing an update.
mjr 33:d832bcab089e 3673 //
mjr 33:d832bcab089e 3674 class Debouncer
mjr 33:d832bcab089e 3675 {
mjr 33:d832bcab089e 3676 public:
mjr 33:d832bcab089e 3677 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 3678 {
mjr 33:d832bcab089e 3679 t.start();
mjr 33:d832bcab089e 3680 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 3681 this->tmin = tmin;
mjr 33:d832bcab089e 3682 }
mjr 33:d832bcab089e 3683
mjr 33:d832bcab089e 3684 // Get the current stable value
mjr 33:d832bcab089e 3685 bool val() const { return stable; }
mjr 33:d832bcab089e 3686
mjr 33:d832bcab089e 3687 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 3688 // input device.
mjr 33:d832bcab089e 3689 void sampleIn(bool val)
mjr 33:d832bcab089e 3690 {
mjr 33:d832bcab089e 3691 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 3692 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 3693 // on the sample reader.
mjr 33:d832bcab089e 3694 if (val != prv)
mjr 33:d832bcab089e 3695 {
mjr 33:d832bcab089e 3696 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 3697 t.reset();
mjr 33:d832bcab089e 3698
mjr 33:d832bcab089e 3699 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 3700 prv = val;
mjr 33:d832bcab089e 3701 }
mjr 33:d832bcab089e 3702 else if (val != stable)
mjr 33:d832bcab089e 3703 {
mjr 33:d832bcab089e 3704 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 3705 // and different from the stable value. This means that
mjr 33:d832bcab089e 3706 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 3707 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 3708 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 3709 if (t.read() > tmin)
mjr 33:d832bcab089e 3710 stable = val;
mjr 33:d832bcab089e 3711 }
mjr 33:d832bcab089e 3712 }
mjr 33:d832bcab089e 3713
mjr 33:d832bcab089e 3714 private:
mjr 33:d832bcab089e 3715 // current stable value
mjr 33:d832bcab089e 3716 bool stable;
mjr 33:d832bcab089e 3717
mjr 33:d832bcab089e 3718 // last raw sample value
mjr 33:d832bcab089e 3719 bool prv;
mjr 33:d832bcab089e 3720
mjr 33:d832bcab089e 3721 // elapsed time since last raw input change
mjr 33:d832bcab089e 3722 Timer t;
mjr 33:d832bcab089e 3723
mjr 33:d832bcab089e 3724 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 3725 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 3726 float tmin;
mjr 33:d832bcab089e 3727 };
mjr 33:d832bcab089e 3728
mjr 33:d832bcab089e 3729
mjr 33:d832bcab089e 3730 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 3731 //
mjr 33:d832bcab089e 3732 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 3733 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 3734 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 3735 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 3736 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 3737 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 3738 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 3739 //
mjr 33:d832bcab089e 3740 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 3741 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 3742 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 3743 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 3744 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 3745 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 3746 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 3747 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 3748 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 3749 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 3750 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 3751 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 3752 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 3753 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 3754 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 3755 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 3756 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 3757 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 3758 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 3759 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 3760 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 3761 //
mjr 40:cc0d9814522b 3762 // This scheme might seem a little convoluted, but it handles a number
mjr 40:cc0d9814522b 3763 // of tricky but likely scenarios:
mjr 33:d832bcab089e 3764 //
mjr 33:d832bcab089e 3765 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 40:cc0d9814522b 3766 // so that the PC goes into "Soft Off" mode when the user turns off
mjr 40:cc0d9814522b 3767 // the cabinet by pushing the power button or using the Shut Down
mjr 40:cc0d9814522b 3768 // command from within Windows. In Windows parlance, this "soft off"
mjr 40:cc0d9814522b 3769 // condition is called ACPI State S5. In this state, the main CPU
mjr 40:cc0d9814522b 3770 // power is turned off, but the motherboard still provides power to
mjr 40:cc0d9814522b 3771 // USB devices. This means that the KL25Z keeps running. Without
mjr 40:cc0d9814522b 3772 // the external power sensing circuit, the only hint that we're in
mjr 40:cc0d9814522b 3773 // this state is that the USB connection to the host goes into Suspend
mjr 40:cc0d9814522b 3774 // mode, but that could mean other things as well. The latch circuit
mjr 40:cc0d9814522b 3775 // lets us tell for sure that we're in this state.
mjr 33:d832bcab089e 3776 //
mjr 33:d832bcab089e 3777 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 3778 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 3779 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 3780 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 3781 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 3782 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 3783 // will power up before or after PSU2, so it's not good enough to
mjr 40:cc0d9814522b 3784 // observe the current state of PSU2 when we first check. If PSU2
mjr 40:cc0d9814522b 3785 // were to come on first, checking only the current state would fool
mjr 40:cc0d9814522b 3786 // us into thinking that no action is required, because we'd only see
mjr 40:cc0d9814522b 3787 // that PSU2 is turned on any time we check. The latch handles this
mjr 40:cc0d9814522b 3788 // case by letting us see that PSU2 was indeed off some time before our
mjr 40:cc0d9814522b 3789 // first check.
mjr 33:d832bcab089e 3790 //
mjr 33:d832bcab089e 3791 // - If the KL25Z is rebooted while the main system is running, or the
mjr 40:cc0d9814522b 3792 // KL25Z is unplugged and plugged back in, we'll correctly leave the
mjr 33:d832bcab089e 3793 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 3794 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 3795 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 3796 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 3797 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 3798 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 3799 //
mjr 33:d832bcab089e 3800
mjr 77:0b96f6867312 3801 // Current PSU2 power state:
mjr 33:d832bcab089e 3802 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 3803 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 3804 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 3805 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 3806 // 5 -> TV relay on
mjr 77:0b96f6867312 3807 // 6 -> sending IR signals designed as TV ON signals
mjr 73:4e8ce0b18915 3808 uint8_t psu2_state = 1;
mjr 73:4e8ce0b18915 3809
mjr 73:4e8ce0b18915 3810 // TV relay state. The TV relay can be controlled by the power-on
mjr 73:4e8ce0b18915 3811 // timer and directly from the PC (via USB commands), so keep a
mjr 73:4e8ce0b18915 3812 // separate state for each:
mjr 73:4e8ce0b18915 3813 // 0x01 -> turned on by power-on timer
mjr 73:4e8ce0b18915 3814 // 0x02 -> turned on by USB command
mjr 73:4e8ce0b18915 3815 uint8_t tv_relay_state = 0x00;
mjr 73:4e8ce0b18915 3816 const uint8_t TV_RELAY_POWERON = 0x01;
mjr 73:4e8ce0b18915 3817 const uint8_t TV_RELAY_USB = 0x02;
mjr 73:4e8ce0b18915 3818
mjr 77:0b96f6867312 3819 // TV ON IR command state. When the main PSU2 power state reaches
mjr 77:0b96f6867312 3820 // the IR phase, we use this sub-state counter to send the TV ON
mjr 77:0b96f6867312 3821 // IR signals. We initialize to state 0 when the main state counter
mjr 77:0b96f6867312 3822 // reaches the IR step. In state 0, we start transmitting the first
mjr 77:0b96f6867312 3823 // (lowest numbered) IR command slot marked as containing a TV ON
mjr 77:0b96f6867312 3824 // code, and advance to state 1. In state 1, we check to see if
mjr 77:0b96f6867312 3825 // the transmitter is still sending; if so, we do nothing, if so
mjr 77:0b96f6867312 3826 // we start transmitting the second TV ON code and advance to state
mjr 77:0b96f6867312 3827 // 2. Continue until we run out of TV ON IR codes, at which point
mjr 77:0b96f6867312 3828 // we advance to the next main psu2_state step.
mjr 77:0b96f6867312 3829 uint8_t tvon_ir_state = 0;
mjr 77:0b96f6867312 3830
mjr 77:0b96f6867312 3831 // TV ON switch relay control output pin
mjr 73:4e8ce0b18915 3832 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 3833
mjr 35:e959ffba78fd 3834 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 3835 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 3836 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 3837
mjr 73:4e8ce0b18915 3838 // Apply the current TV relay state
mjr 73:4e8ce0b18915 3839 void tvRelayUpdate(uint8_t bit, bool state)
mjr 73:4e8ce0b18915 3840 {
mjr 73:4e8ce0b18915 3841 // update the state
mjr 73:4e8ce0b18915 3842 if (state)
mjr 73:4e8ce0b18915 3843 tv_relay_state |= bit;
mjr 73:4e8ce0b18915 3844 else
mjr 73:4e8ce0b18915 3845 tv_relay_state &= ~bit;
mjr 73:4e8ce0b18915 3846
mjr 73:4e8ce0b18915 3847 // set the relay GPIO to the new state
mjr 73:4e8ce0b18915 3848 if (tv_relay != 0)
mjr 73:4e8ce0b18915 3849 tv_relay->write(tv_relay_state != 0);
mjr 73:4e8ce0b18915 3850 }
mjr 35:e959ffba78fd 3851
mjr 77:0b96f6867312 3852 // PSU2 Status update routine. The main loop calls this from time
mjr 77:0b96f6867312 3853 // to time to update the power sensing state and carry out TV ON
mjr 77:0b96f6867312 3854 // functions.
mjr 77:0b96f6867312 3855 Timer powerStatusTimer;
mjr 77:0b96f6867312 3856 uint32_t tv_delay_time_us;
mjr 77:0b96f6867312 3857 void powerStatusUpdate(Config &cfg)
mjr 33:d832bcab089e 3858 {
mjr 77:0b96f6867312 3859 // Only update every 1/4 second or so. Note that if the PSU2
mjr 77:0b96f6867312 3860 // circuit isn't configured, the initialization routine won't
mjr 77:0b96f6867312 3861 // start the timer, so it'll always read zero and we'll always
mjr 77:0b96f6867312 3862 // skip this whole routine.
mjr 77:0b96f6867312 3863 if (powerStatusTimer.read_us() < 250000)
mjr 77:0b96f6867312 3864 return;
mjr 77:0b96f6867312 3865
mjr 77:0b96f6867312 3866 // reset the update timer for next time
mjr 77:0b96f6867312 3867 powerStatusTimer.reset();
mjr 77:0b96f6867312 3868
mjr 77:0b96f6867312 3869 // TV ON timer. We start this timer when we detect a change
mjr 77:0b96f6867312 3870 // in the PSU2 status from OFF to ON. When the timer reaches
mjr 77:0b96f6867312 3871 // the configured TV ON delay time, and the PSU2 power is still
mjr 77:0b96f6867312 3872 // on, we'll trigger the TV ON relay and send the TV ON IR codes.
mjr 35:e959ffba78fd 3873 static Timer tv_timer;
mjr 35:e959ffba78fd 3874
mjr 33:d832bcab089e 3875 // Check our internal state
mjr 33:d832bcab089e 3876 switch (psu2_state)
mjr 33:d832bcab089e 3877 {
mjr 33:d832bcab089e 3878 case 1:
mjr 33:d832bcab089e 3879 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 3880 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 3881 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 3882 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 3883 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 3884 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 3885 {
mjr 33:d832bcab089e 3886 // switch to OFF state
mjr 33:d832bcab089e 3887 psu2_state = 2;
mjr 33:d832bcab089e 3888
mjr 33:d832bcab089e 3889 // try setting the latch
mjr 35:e959ffba78fd 3890 psu2_status_set->write(1);
mjr 33:d832bcab089e 3891 }
mjr 77:0b96f6867312 3892 powerTimerDiagState = 0;
mjr 33:d832bcab089e 3893 break;
mjr 33:d832bcab089e 3894
mjr 33:d832bcab089e 3895 case 2:
mjr 33:d832bcab089e 3896 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 3897 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 3898 psu2_status_set->write(0);
mjr 33:d832bcab089e 3899 psu2_state = 3;
mjr 77:0b96f6867312 3900 powerTimerDiagState = 0;
mjr 33:d832bcab089e 3901 break;
mjr 33:d832bcab089e 3902
mjr 33:d832bcab089e 3903 case 3:
mjr 33:d832bcab089e 3904 // CHECK state: we pulsed SET, and we're now ready to see
mjr 40:cc0d9814522b 3905 // if it stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 3906 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 3907 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 3908 if (psu2_status_sense->read())
mjr 33:d832bcab089e 3909 {
mjr 33:d832bcab089e 3910 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 3911 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 3912 tv_timer.reset();
mjr 33:d832bcab089e 3913 tv_timer.start();
mjr 33:d832bcab089e 3914 psu2_state = 4;
mjr 73:4e8ce0b18915 3915
mjr 73:4e8ce0b18915 3916 // start the power timer diagnostic flashes
mjr 73:4e8ce0b18915 3917 powerTimerDiagState = 2;
mjr 33:d832bcab089e 3918 }
mjr 33:d832bcab089e 3919 else
mjr 33:d832bcab089e 3920 {
mjr 33:d832bcab089e 3921 // The latch didn't stick, so PSU2 was still off at
mjr 33:d832bcab089e 3922 // our last check. Try pulsing it again in case PSU2
mjr 33:d832bcab089e 3923 // was turned on since the last check.
mjr 35:e959ffba78fd 3924 psu2_status_set->write(1);
mjr 33:d832bcab089e 3925 psu2_state = 2;
mjr 33:d832bcab089e 3926 }
mjr 33:d832bcab089e 3927 break;
mjr 33:d832bcab089e 3928
mjr 33:d832bcab089e 3929 case 4:
mjr 77:0b96f6867312 3930 // TV timer countdown in progress. The latch has to stay on during
mjr 77:0b96f6867312 3931 // the countdown; if the latch turns off, PSU2 power must have gone
mjr 77:0b96f6867312 3932 // off again before the countdown finished.
mjr 77:0b96f6867312 3933 if (!psu2_status_sense->read())
mjr 77:0b96f6867312 3934 {
mjr 77:0b96f6867312 3935 // power is off - start a new check cycle
mjr 77:0b96f6867312 3936 psu2_status_set->write(1);
mjr 77:0b96f6867312 3937 psu2_state = 2;
mjr 77:0b96f6867312 3938 break;
mjr 77:0b96f6867312 3939 }
mjr 77:0b96f6867312 3940
mjr 77:0b96f6867312 3941 // Flash the power time diagnostic every two cycles
mjr 77:0b96f6867312 3942 powerTimerDiagState = (powerTimerDiagState + 1) & 0x03;
mjr 77:0b96f6867312 3943
mjr 77:0b96f6867312 3944 // if we've reached the delay time, pulse the relay
mjr 77:0b96f6867312 3945 if (tv_timer.read_us() >= tv_delay_time_us)
mjr 33:d832bcab089e 3946 {
mjr 33:d832bcab089e 3947 // turn on the relay for one timer interval
mjr 73:4e8ce0b18915 3948 tvRelayUpdate(TV_RELAY_POWERON, true);
mjr 33:d832bcab089e 3949 psu2_state = 5;
mjr 77:0b96f6867312 3950
mjr 77:0b96f6867312 3951 // show solid blue on the diagnostic LED while the relay is on
mjr 77:0b96f6867312 3952 powerTimerDiagState = 2;
mjr 33:d832bcab089e 3953 }
mjr 33:d832bcab089e 3954 break;
mjr 33:d832bcab089e 3955
mjr 33:d832bcab089e 3956 case 5:
mjr 33:d832bcab089e 3957 // TV timer relay on. We pulse this for one interval, so
mjr 77:0b96f6867312 3958 // it's now time to turn it off.
mjr 73:4e8ce0b18915 3959 tvRelayUpdate(TV_RELAY_POWERON, false);
mjr 77:0b96f6867312 3960
mjr 77:0b96f6867312 3961 // Proceed to sending any TV ON IR commands
mjr 77:0b96f6867312 3962 psu2_state = 6;
mjr 77:0b96f6867312 3963 tvon_ir_state = 0;
mjr 77:0b96f6867312 3964
mjr 77:0b96f6867312 3965 // diagnostic LEDs off for now
mjr 77:0b96f6867312 3966 powerTimerDiagState = 0;
mjr 77:0b96f6867312 3967 break;
mjr 77:0b96f6867312 3968
mjr 77:0b96f6867312 3969 case 6:
mjr 77:0b96f6867312 3970 // Sending TV ON IR signals. Start with the assumption that
mjr 77:0b96f6867312 3971 // we have no IR work to do, in which case we're done with the
mjr 77:0b96f6867312 3972 // whole TV ON sequence. So by default return to state 1.
mjr 33:d832bcab089e 3973 psu2_state = 1;
mjr 77:0b96f6867312 3974 powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 3975
mjr 77:0b96f6867312 3976 // If we have an IR emitter, check for TV ON IR commands
mjr 77:0b96f6867312 3977 if (ir_tx != 0)
mjr 77:0b96f6867312 3978 {
mjr 77:0b96f6867312 3979 // check to see if the last transmission is still in progress
mjr 77:0b96f6867312 3980 if (ir_tx->isSending())
mjr 77:0b96f6867312 3981 {
mjr 77:0b96f6867312 3982 // We're still sending the last transmission. Stay in
mjr 77:0b96f6867312 3983 // state 6.
mjr 77:0b96f6867312 3984 psu2_state = 6;
mjr 77:0b96f6867312 3985 powerTimerDiagState = 4;
mjr 77:0b96f6867312 3986 break;
mjr 77:0b96f6867312 3987 }
mjr 77:0b96f6867312 3988
mjr 77:0b96f6867312 3989 // The last transmission is done, so check for a new one.
mjr 77:0b96f6867312 3990 // Look for the Nth TV ON IR slot, where N is our state
mjr 77:0b96f6867312 3991 // number.
mjr 77:0b96f6867312 3992 for (int i = 0, n = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 3993 {
mjr 77:0b96f6867312 3994 // is this a TV ON command?
mjr 77:0b96f6867312 3995 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 3996 {
mjr 77:0b96f6867312 3997 // It's a TV ON command - check if it's the one we're
mjr 77:0b96f6867312 3998 // looking for.
mjr 77:0b96f6867312 3999 if (n == tvon_ir_state)
mjr 77:0b96f6867312 4000 {
mjr 77:0b96f6867312 4001 // It's the one. Start transmitting it by
mjr 77:0b96f6867312 4002 // pushing its virtual button.
mjr 77:0b96f6867312 4003 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 4004 ir_tx->pushButton(vb, true);
mjr 77:0b96f6867312 4005
mjr 77:0b96f6867312 4006 // Pushing the button starts transmission, and once
mjr 77:0b96f6867312 4007 // started, the transmission will run to completion
mjr 77:0b96f6867312 4008 // even if the button is no longer pushed. So we
mjr 77:0b96f6867312 4009 // can immediately un-push the button, since we only
mjr 77:0b96f6867312 4010 // need to send the code once.
mjr 77:0b96f6867312 4011 ir_tx->pushButton(vb, false);
mjr 77:0b96f6867312 4012
mjr 77:0b96f6867312 4013 // Advance to the next TV ON IR state, where we'll
mjr 77:0b96f6867312 4014 // await the end of this transmission and move on to
mjr 77:0b96f6867312 4015 // the next one.
mjr 77:0b96f6867312 4016 psu2_state = 6;
mjr 77:0b96f6867312 4017 tvon_ir_state++;
mjr 77:0b96f6867312 4018 break;
mjr 77:0b96f6867312 4019 }
mjr 77:0b96f6867312 4020
mjr 77:0b96f6867312 4021 // it's not ours - count it and keep looking
mjr 77:0b96f6867312 4022 ++n;
mjr 77:0b96f6867312 4023 }
mjr 77:0b96f6867312 4024 }
mjr 77:0b96f6867312 4025 }
mjr 33:d832bcab089e 4026 break;
mjr 33:d832bcab089e 4027 }
mjr 77:0b96f6867312 4028
mjr 77:0b96f6867312 4029 // update the diagnostic LEDs
mjr 77:0b96f6867312 4030 diagLED();
mjr 33:d832bcab089e 4031 }
mjr 33:d832bcab089e 4032
mjr 77:0b96f6867312 4033 // Start the power status timer. If the status sense circuit is enabled
mjr 77:0b96f6867312 4034 // in the configuration, we'll set up the pin connections and start the
mjr 77:0b96f6867312 4035 // timer for our periodic status checks. Does nothing if any of the pins
mjr 77:0b96f6867312 4036 // are configured as NC.
mjr 77:0b96f6867312 4037 void startPowerStatusTimer(Config &cfg)
mjr 35:e959ffba78fd 4038 {
mjr 55:4db125cd11a0 4039 // only start the timer if the pins are configured and the delay
mjr 55:4db125cd11a0 4040 // time is nonzero
mjr 77:0b96f6867312 4041 powerStatusTimer.reset();
mjr 77:0b96f6867312 4042 if (cfg.TVON.statusPin != 0xFF
mjr 77:0b96f6867312 4043 && cfg.TVON.latchPin != 0xFF)
mjr 35:e959ffba78fd 4044 {
mjr 77:0b96f6867312 4045 // set up the power sensing circuit connections
mjr 53:9b2611964afc 4046 psu2_status_sense = new DigitalIn(wirePinName(cfg.TVON.statusPin));
mjr 53:9b2611964afc 4047 psu2_status_set = new DigitalOut(wirePinName(cfg.TVON.latchPin));
mjr 77:0b96f6867312 4048
mjr 77:0b96f6867312 4049 // if there's a TV ON relay, set up its control pin
mjr 77:0b96f6867312 4050 if (cfg.TVON.relayPin != 0xFF)
mjr 77:0b96f6867312 4051 tv_relay = new DigitalOut(wirePinName(cfg.TVON.relayPin));
mjr 77:0b96f6867312 4052
mjr 77:0b96f6867312 4053 // Set the TV ON delay time. We store the time internally in
mjr 77:0b96f6867312 4054 // microseconds, but the configuration stores it in units of
mjr 77:0b96f6867312 4055 // 1/100 second = 10ms = 10000us.
mjr 77:0b96f6867312 4056 tv_delay_time_us = cfg.TVON.delayTime * 10000;;
mjr 77:0b96f6867312 4057
mjr 77:0b96f6867312 4058 // Start the TV timer
mjr 77:0b96f6867312 4059 powerStatusTimer.start();
mjr 35:e959ffba78fd 4060 }
mjr 35:e959ffba78fd 4061 }
mjr 35:e959ffba78fd 4062
mjr 73:4e8ce0b18915 4063 // TV relay manual control timer. This lets us pulse the TV relay
mjr 73:4e8ce0b18915 4064 // under manual control, separately from the TV ON timer.
mjr 73:4e8ce0b18915 4065 Ticker tv_manualTicker;
mjr 73:4e8ce0b18915 4066 void TVManualInt()
mjr 73:4e8ce0b18915 4067 {
mjr 73:4e8ce0b18915 4068 tv_manualTicker.detach();
mjr 73:4e8ce0b18915 4069 tvRelayUpdate(TV_RELAY_USB, false);
mjr 73:4e8ce0b18915 4070 }
mjr 73:4e8ce0b18915 4071
mjr 73:4e8ce0b18915 4072 // Operate the TV ON relay. This allows manual control of the relay
mjr 73:4e8ce0b18915 4073 // from the PC. See protocol message 65 submessage 11.
mjr 73:4e8ce0b18915 4074 //
mjr 73:4e8ce0b18915 4075 // Mode:
mjr 73:4e8ce0b18915 4076 // 0 = turn relay off
mjr 73:4e8ce0b18915 4077 // 1 = turn relay on
mjr 73:4e8ce0b18915 4078 // 2 = pulse relay
mjr 73:4e8ce0b18915 4079 void TVRelay(int mode)
mjr 73:4e8ce0b18915 4080 {
mjr 73:4e8ce0b18915 4081 // if there's no TV relay control pin, ignore this
mjr 73:4e8ce0b18915 4082 if (tv_relay == 0)
mjr 73:4e8ce0b18915 4083 return;
mjr 73:4e8ce0b18915 4084
mjr 73:4e8ce0b18915 4085 switch (mode)
mjr 73:4e8ce0b18915 4086 {
mjr 73:4e8ce0b18915 4087 case 0:
mjr 73:4e8ce0b18915 4088 // relay off
mjr 73:4e8ce0b18915 4089 tvRelayUpdate(TV_RELAY_USB, false);
mjr 73:4e8ce0b18915 4090 break;
mjr 73:4e8ce0b18915 4091
mjr 73:4e8ce0b18915 4092 case 1:
mjr 73:4e8ce0b18915 4093 // relay on
mjr 73:4e8ce0b18915 4094 tvRelayUpdate(TV_RELAY_USB, true);
mjr 73:4e8ce0b18915 4095 break;
mjr 73:4e8ce0b18915 4096
mjr 73:4e8ce0b18915 4097 case 2:
mjr 73:4e8ce0b18915 4098 // Pulse the relay. Turn it on, then set our timer for 250ms.
mjr 73:4e8ce0b18915 4099 tvRelayUpdate(TV_RELAY_USB, true);
mjr 73:4e8ce0b18915 4100 tv_manualTicker.attach(&TVManualInt, 0.25);
mjr 73:4e8ce0b18915 4101 break;
mjr 73:4e8ce0b18915 4102 }
mjr 73:4e8ce0b18915 4103 }
mjr 73:4e8ce0b18915 4104
mjr 73:4e8ce0b18915 4105
mjr 35:e959ffba78fd 4106 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4107 //
mjr 35:e959ffba78fd 4108 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 4109 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 4110 //
mjr 35:e959ffba78fd 4111 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 4112 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 4113 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 4114 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 4115 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 4116 // again each time the firmware is updated.
mjr 35:e959ffba78fd 4117 //
mjr 35:e959ffba78fd 4118 NVM nvm;
mjr 35:e959ffba78fd 4119
mjr 77:0b96f6867312 4120 // Flag: configuration save requested. The USB command message handler
mjr 77:0b96f6867312 4121 // sets this flag when a command is sent requesting the save. We don't
mjr 77:0b96f6867312 4122 // do the save inline in the command handler, but handle it on the next
mjr 77:0b96f6867312 4123 // main loop iteration.
mjr 77:0b96f6867312 4124 const uint8_t SAVE_CONFIG_ONLY = 1;
mjr 77:0b96f6867312 4125 const uint8_t SAVE_CONFIG_AND_REBOOT = 2;
mjr 77:0b96f6867312 4126 uint8_t saveConfigPending = 0;
mjr 77:0b96f6867312 4127
mjr 77:0b96f6867312 4128 // If saveConfigPending == SAVE_CONFIG_AND_REBOOT, this specifies the
mjr 77:0b96f6867312 4129 // delay time in seconds before rebooting.
mjr 77:0b96f6867312 4130 uint8_t saveConfigRebootTime;
mjr 77:0b96f6867312 4131
mjr 35:e959ffba78fd 4132 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 4133 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 4134
mjr 35:e959ffba78fd 4135 // flash memory controller interface
mjr 35:e959ffba78fd 4136 FreescaleIAP iap;
mjr 35:e959ffba78fd 4137
mjr 76:7f5912b6340e 4138 // NVM structure in memory. This has to be aliend on a sector boundary,
mjr 76:7f5912b6340e 4139 // since we have to be able to erase its page(s) in order to write it.
mjr 76:7f5912b6340e 4140 // Further, we have to ensure that nothing else occupies any space within
mjr 76:7f5912b6340e 4141 // the same pages, since we'll erase that entire space whenever we write.
mjr 76:7f5912b6340e 4142 static const union
mjr 76:7f5912b6340e 4143 {
mjr 76:7f5912b6340e 4144 NVM nvm; // the NVM structure
mjr 76:7f5912b6340e 4145 char guard[((sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE)*SECTOR_SIZE];
mjr 76:7f5912b6340e 4146 }
mjr 76:7f5912b6340e 4147 flash_nvm_memory __attribute__ ((aligned(SECTOR_SIZE))) = { };
mjr 76:7f5912b6340e 4148
mjr 35:e959ffba78fd 4149 // figure the flash address as a pointer
mjr 35:e959ffba78fd 4150 NVM *configFlashAddr()
mjr 35:e959ffba78fd 4151 {
mjr 77:0b96f6867312 4152 return (NVM *)&flash_nvm_memory;
mjr 35:e959ffba78fd 4153 }
mjr 35:e959ffba78fd 4154
mjr 76:7f5912b6340e 4155 // Load the config from flash. Returns true if a valid non-default
mjr 76:7f5912b6340e 4156 // configuration was loaded, false if we not. If we return false,
mjr 76:7f5912b6340e 4157 // we load the factory defaults, so the configuration object is valid
mjr 76:7f5912b6340e 4158 // in either case.
mjr 76:7f5912b6340e 4159 bool loadConfigFromFlash()
mjr 35:e959ffba78fd 4160 {
mjr 35:e959ffba78fd 4161 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 4162 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 4163 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 4164 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 4165 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 4166 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 4167 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 4168 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 4169 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 4170 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 4171 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 4172 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 4173 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 4174 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 4175 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 4176 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 4177
mjr 35:e959ffba78fd 4178 // Figure how many sectors we need for our structure
mjr 35:e959ffba78fd 4179 NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 4180
mjr 35:e959ffba78fd 4181 // if the flash is valid, load it; otherwise initialize to defaults
mjr 76:7f5912b6340e 4182 bool nvm_valid = flash->valid();
mjr 76:7f5912b6340e 4183 if (nvm_valid)
mjr 35:e959ffba78fd 4184 {
mjr 35:e959ffba78fd 4185 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 4186 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 4187 }
mjr 35:e959ffba78fd 4188 else
mjr 35:e959ffba78fd 4189 {
mjr 76:7f5912b6340e 4190 // flash is invalid - load factory settings into RAM structure
mjr 35:e959ffba78fd 4191 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 4192 }
mjr 76:7f5912b6340e 4193
mjr 76:7f5912b6340e 4194 // tell the caller what happened
mjr 76:7f5912b6340e 4195 return nvm_valid;
mjr 35:e959ffba78fd 4196 }
mjr 35:e959ffba78fd 4197
mjr 35:e959ffba78fd 4198 void saveConfigToFlash()
mjr 33:d832bcab089e 4199 {
mjr 76:7f5912b6340e 4200 // make sure the plunger sensor isn't busy
mjr 76:7f5912b6340e 4201 waitPlungerIdle();
mjr 76:7f5912b6340e 4202
mjr 76:7f5912b6340e 4203 // get the config block location in the flash memory
mjr 77:0b96f6867312 4204 uint32_t addr = uint32_t(configFlashAddr());
mjr 76:7f5912b6340e 4205
mjr 76:7f5912b6340e 4206 // loop until we save it successfully
mjr 76:7f5912b6340e 4207 for (int i = 0 ; i < 5 ; ++i)
mjr 76:7f5912b6340e 4208 {
mjr 76:7f5912b6340e 4209 // show cyan while writing
mjr 76:7f5912b6340e 4210 diagLED(0, 1, 1);
mjr 76:7f5912b6340e 4211
mjr 76:7f5912b6340e 4212 // save the data
mjr 76:7f5912b6340e 4213 nvm.save(iap, addr);
mjr 76:7f5912b6340e 4214
mjr 76:7f5912b6340e 4215 // diagnostic lights off
mjr 76:7f5912b6340e 4216 diagLED(0, 0, 0);
mjr 76:7f5912b6340e 4217
mjr 76:7f5912b6340e 4218 // verify the data
mjr 76:7f5912b6340e 4219 if (nvm.verify(addr))
mjr 76:7f5912b6340e 4220 {
mjr 77:0b96f6867312 4221 // show a diagnostic success flash (rapid green)
mjr 77:0b96f6867312 4222 for (int j = 0 ; j < 4 ; ++j)
mjr 76:7f5912b6340e 4223 {
mjr 77:0b96f6867312 4224 diagLED(0, 1, 0);
mjr 76:7f5912b6340e 4225 wait_us(50000);
mjr 76:7f5912b6340e 4226 diagLED(0, 0, 0);
mjr 76:7f5912b6340e 4227 wait_us(50000);
mjr 76:7f5912b6340e 4228 }
mjr 76:7f5912b6340e 4229
mjr 76:7f5912b6340e 4230 // success - no need to write again
mjr 76:7f5912b6340e 4231 break;
mjr 76:7f5912b6340e 4232 }
mjr 76:7f5912b6340e 4233 else
mjr 76:7f5912b6340e 4234 {
mjr 76:7f5912b6340e 4235 // Write failed. For diagnostic purposes, flash red a few times.
mjr 76:7f5912b6340e 4236 // Then go back through the loop to make another attempt at the
mjr 76:7f5912b6340e 4237 // write.
mjr 76:7f5912b6340e 4238 for (int j = 0 ; j < 5 ; ++j)
mjr 76:7f5912b6340e 4239 {
mjr 76:7f5912b6340e 4240 diagLED(1, 0, 0);
mjr 76:7f5912b6340e 4241 wait_us(50000);
mjr 76:7f5912b6340e 4242 diagLED(0, 0, 0);
mjr 76:7f5912b6340e 4243 wait_us(50000);
mjr 76:7f5912b6340e 4244 }
mjr 76:7f5912b6340e 4245 }
mjr 76:7f5912b6340e 4246 }
mjr 76:7f5912b6340e 4247 }
mjr 76:7f5912b6340e 4248
mjr 76:7f5912b6340e 4249 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 4250 //
mjr 76:7f5912b6340e 4251 // Host-loaded configuration. The Flash NVM block above is designed to be
mjr 76:7f5912b6340e 4252 // stored from within the firmware; in contrast, the host-loaded config is
mjr 76:7f5912b6340e 4253 // stored by the host, by patching the firwmare binary (.bin) file before
mjr 76:7f5912b6340e 4254 // downloading it to the device.
mjr 76:7f5912b6340e 4255 //
mjr 76:7f5912b6340e 4256 // Ideally, we'd use the host-loaded memory for all configuration updates,
mjr 76:7f5912b6340e 4257 // because the KL25Z doesn't seem to be 100% reliable writing flash itself.
mjr 76:7f5912b6340e 4258 // There seems to be a chance of memory bus contention while a write is in
mjr 76:7f5912b6340e 4259 // progress, which can either corrupt the write or cause the CPU to lock up
mjr 76:7f5912b6340e 4260 // before the write is completed. It seems more reliable to program the
mjr 76:7f5912b6340e 4261 // flash externally, via the OpenSDA connection. Unfortunately, none of
mjr 76:7f5912b6340e 4262 // the available OpenSDA versions are capable of programming specific flash
mjr 76:7f5912b6340e 4263 // sectors; they always erase the entire flash memory space. We *could*
mjr 76:7f5912b6340e 4264 // make the Windows config program simply re-download the entire firmware
mjr 76:7f5912b6340e 4265 // for every configuration update, but I'd rather not because of the extra
mjr 76:7f5912b6340e 4266 // wear this would put on the flash. So, as a compromise, we'll use the
mjr 76:7f5912b6340e 4267 // host-loaded config whenever the user explicitly updates the firmware,
mjr 76:7f5912b6340e 4268 // but we'll use the on-board writer when only making a config change.
mjr 76:7f5912b6340e 4269 //
mjr 76:7f5912b6340e 4270 // The memory here is stored using the same format as the USB "Set Config
mjr 76:7f5912b6340e 4271 // Variable" command. These messages are 8 bytes long and start with a
mjr 76:7f5912b6340e 4272 // byte value 66, followed by the variable ID, followed by the variable
mjr 76:7f5912b6340e 4273 // value data in a format defined separately for each variable. To load
mjr 76:7f5912b6340e 4274 // the data, we'll start at the first byte after the signature, and
mjr 76:7f5912b6340e 4275 // interpret each 8-byte block as a type 66 message. If the first byte
mjr 76:7f5912b6340e 4276 // of a block is not 66, we'll take it as the end of the data.
mjr 76:7f5912b6340e 4277 //
mjr 76:7f5912b6340e 4278 // We provide a block of storage here big enough for 1,024 variables.
mjr 76:7f5912b6340e 4279 // The header consists of a 30-byte signature followed by two bytes giving
mjr 76:7f5912b6340e 4280 // the available space in the area, in this case 8192 == 0x0200. The
mjr 76:7f5912b6340e 4281 // length is little-endian. Note that the linker will implicitly zero
mjr 76:7f5912b6340e 4282 // the rest of the block, so if the host doesn't populate it, we'll see
mjr 76:7f5912b6340e 4283 // that it's empty by virtue of not containing the required '66' byte
mjr 76:7f5912b6340e 4284 // prefix for the first 8-byte variable block.
mjr 76:7f5912b6340e 4285 static const uint8_t hostLoadedConfig[8192+32]
mjr 76:7f5912b6340e 4286 __attribute__ ((aligned(SECTOR_SIZE))) =
mjr 76:7f5912b6340e 4287 "///Pinscape.HostLoadedConfig//\0\040"; // 30 byte signature + 2 byte length
mjr 76:7f5912b6340e 4288
mjr 76:7f5912b6340e 4289 // Get a pointer to the first byte of the configuration data
mjr 76:7f5912b6340e 4290 const uint8_t *getHostLoadedConfigData()
mjr 76:7f5912b6340e 4291 {
mjr 76:7f5912b6340e 4292 // the first configuration variable byte immediately follows the
mjr 76:7f5912b6340e 4293 // 32-byte signature header
mjr 76:7f5912b6340e 4294 return hostLoadedConfig + 32;
mjr 76:7f5912b6340e 4295 };
mjr 76:7f5912b6340e 4296
mjr 76:7f5912b6340e 4297 // forward reference to config var store function
mjr 76:7f5912b6340e 4298 void configVarSet(const uint8_t *);
mjr 76:7f5912b6340e 4299
mjr 76:7f5912b6340e 4300 // Load the host-loaded configuration data into the active (RAM)
mjr 76:7f5912b6340e 4301 // configuration object.
mjr 76:7f5912b6340e 4302 void loadHostLoadedConfig()
mjr 76:7f5912b6340e 4303 {
mjr 76:7f5912b6340e 4304 // Start at the first configuration variable. Each variable
mjr 76:7f5912b6340e 4305 // block is in the format of a Set Config Variable command in
mjr 76:7f5912b6340e 4306 // the USB protocol, so each block starts with a byte value of
mjr 76:7f5912b6340e 4307 // 66 and is 8 bytes long. Continue as long as we find valid
mjr 76:7f5912b6340e 4308 // variable blocks, or reach end end of the block.
mjr 76:7f5912b6340e 4309 const uint8_t *start = getHostLoadedConfigData();
mjr 76:7f5912b6340e 4310 const uint8_t *end = hostLoadedConfig + sizeof(hostLoadedConfig);
mjr 76:7f5912b6340e 4311 for (const uint8_t *p = getHostLoadedConfigData() ; start < end && *p == 66 ; p += 8)
mjr 76:7f5912b6340e 4312 {
mjr 76:7f5912b6340e 4313 // load this variable
mjr 76:7f5912b6340e 4314 configVarSet(p);
mjr 76:7f5912b6340e 4315 }
mjr 35:e959ffba78fd 4316 }
mjr 35:e959ffba78fd 4317
mjr 35:e959ffba78fd 4318 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4319 //
mjr 55:4db125cd11a0 4320 // Pixel dump mode - the host requested a dump of image sensor pixels
mjr 55:4db125cd11a0 4321 // (helpful for installing and setting up the sensor and light source)
mjr 55:4db125cd11a0 4322 //
mjr 55:4db125cd11a0 4323 bool reportPlungerStat = false;
mjr 55:4db125cd11a0 4324 uint8_t reportPlungerStatFlags; // plunger pixel report flag bits (see ccdSensor.h)
mjr 55:4db125cd11a0 4325 uint8_t reportPlungerStatTime; // extra exposure time for plunger pixel report
mjr 55:4db125cd11a0 4326
mjr 55:4db125cd11a0 4327
mjr 55:4db125cd11a0 4328
mjr 55:4db125cd11a0 4329 // ---------------------------------------------------------------------------
mjr 55:4db125cd11a0 4330 //
mjr 40:cc0d9814522b 4331 // Night mode setting updates
mjr 40:cc0d9814522b 4332 //
mjr 38:091e511ce8a0 4333
mjr 38:091e511ce8a0 4334 // Turn night mode on or off
mjr 38:091e511ce8a0 4335 static void setNightMode(bool on)
mjr 38:091e511ce8a0 4336 {
mjr 77:0b96f6867312 4337 // Set the new night mode flag in the noisy output class. Note
mjr 77:0b96f6867312 4338 // that we use the status report bit flag value 0x02 when on, so
mjr 77:0b96f6867312 4339 // that we can just '|' this into the overall status bits.
mjr 77:0b96f6867312 4340 nightMode = on ? 0x02 : 0x00;
mjr 55:4db125cd11a0 4341
mjr 40:cc0d9814522b 4342 // update the special output pin that shows the night mode state
mjr 53:9b2611964afc 4343 int port = int(cfg.nightMode.port) - 1;
mjr 53:9b2611964afc 4344 if (port >= 0 && port < numOutputs)
mjr 53:9b2611964afc 4345 lwPin[port]->set(nightMode ? 255 : 0);
mjr 76:7f5912b6340e 4346
mjr 76:7f5912b6340e 4347 // Reset all outputs at their current value, so that the underlying
mjr 76:7f5912b6340e 4348 // physical outputs get turned on or off as appropriate for the night
mjr 76:7f5912b6340e 4349 // mode change.
mjr 76:7f5912b6340e 4350 for (int i = 0 ; i < numOutputs ; ++i)
mjr 76:7f5912b6340e 4351 lwPin[i]->set(outLevel[i]);
mjr 76:7f5912b6340e 4352
mjr 76:7f5912b6340e 4353 // update 74HC595 outputs
mjr 76:7f5912b6340e 4354 if (hc595 != 0)
mjr 76:7f5912b6340e 4355 hc595->update();
mjr 38:091e511ce8a0 4356 }
mjr 38:091e511ce8a0 4357
mjr 38:091e511ce8a0 4358 // Toggle night mode
mjr 38:091e511ce8a0 4359 static void toggleNightMode()
mjr 38:091e511ce8a0 4360 {
mjr 53:9b2611964afc 4361 setNightMode(!nightMode);
mjr 38:091e511ce8a0 4362 }
mjr 38:091e511ce8a0 4363
mjr 38:091e511ce8a0 4364
mjr 38:091e511ce8a0 4365 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 4366 //
mjr 35:e959ffba78fd 4367 // Plunger Sensor
mjr 35:e959ffba78fd 4368 //
mjr 35:e959ffba78fd 4369
mjr 35:e959ffba78fd 4370 // the plunger sensor interface object
mjr 35:e959ffba78fd 4371 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 4372
mjr 76:7f5912b6340e 4373 // wait for the plunger sensor to complete any outstanding read
mjr 76:7f5912b6340e 4374 static void waitPlungerIdle(void)
mjr 76:7f5912b6340e 4375 {
mjr 76:7f5912b6340e 4376 while (!plungerSensor->ready()) { }
mjr 76:7f5912b6340e 4377 }
mjr 76:7f5912b6340e 4378
mjr 35:e959ffba78fd 4379 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 4380 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 4381 void createPlunger()
mjr 35:e959ffba78fd 4382 {
mjr 35:e959ffba78fd 4383 // create the new sensor object according to the type
mjr 35:e959ffba78fd 4384 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 4385 {
mjr 35:e959ffba78fd 4386 case PlungerType_TSL1410RS:
mjr 69:cc5039284fac 4387 // TSL1410R, serial mode (all pixels read in one file)
mjr 35:e959ffba78fd 4388 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 4389 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 4390 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4391 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 4392 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 4393 NC);
mjr 35:e959ffba78fd 4394 break;
mjr 35:e959ffba78fd 4395
mjr 35:e959ffba78fd 4396 case PlungerType_TSL1410RP:
mjr 69:cc5039284fac 4397 // TSL1410R, parallel mode (each half-sensor's pixels read separately)
mjr 35:e959ffba78fd 4398 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 4399 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 4400 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4401 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 4402 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 4403 wirePinName(cfg.plunger.sensorPin[3]));
mjr 35:e959ffba78fd 4404 break;
mjr 35:e959ffba78fd 4405
mjr 69:cc5039284fac 4406 case PlungerType_TSL1412SS:
mjr 69:cc5039284fac 4407 // TSL1412S, serial mode
mjr 35:e959ffba78fd 4408 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 4409 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 4410 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4411 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 4412 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 4413 NC);
mjr 35:e959ffba78fd 4414 break;
mjr 35:e959ffba78fd 4415
mjr 69:cc5039284fac 4416 case PlungerType_TSL1412SP:
mjr 69:cc5039284fac 4417 // TSL1412S, parallel mode
mjr 35:e959ffba78fd 4418 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 4419 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 4420 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4421 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 4422 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 4423 wirePinName(cfg.plunger.sensorPin[3]));
mjr 35:e959ffba78fd 4424 break;
mjr 35:e959ffba78fd 4425
mjr 35:e959ffba78fd 4426 case PlungerType_Pot:
mjr 35:e959ffba78fd 4427 // pins are: AO
mjr 53:9b2611964afc 4428 plungerSensor = new PlungerSensorPot(
mjr 53:9b2611964afc 4429 wirePinName(cfg.plunger.sensorPin[0]));
mjr 35:e959ffba78fd 4430 break;
mjr 35:e959ffba78fd 4431
mjr 35:e959ffba78fd 4432 case PlungerType_None:
mjr 35:e959ffba78fd 4433 default:
mjr 35:e959ffba78fd 4434 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 4435 break;
mjr 35:e959ffba78fd 4436 }
mjr 33:d832bcab089e 4437 }
mjr 33:d832bcab089e 4438
mjr 52:8298b2a73eb2 4439 // Global plunger calibration mode flag
mjr 52:8298b2a73eb2 4440 bool plungerCalMode;
mjr 52:8298b2a73eb2 4441
mjr 48:058ace2aed1d 4442 // Plunger reader
mjr 51:57eb311faafa 4443 //
mjr 51:57eb311faafa 4444 // This class encapsulates our plunger data processing. At the simplest
mjr 51:57eb311faafa 4445 // level, we read the position from the sensor, adjust it for the
mjr 51:57eb311faafa 4446 // calibration settings, and report the calibrated position to the host.
mjr 51:57eb311faafa 4447 //
mjr 51:57eb311faafa 4448 // In addition, we constantly monitor the data for "firing" motions.
mjr 51:57eb311faafa 4449 // A firing motion is when the user pulls back the plunger and releases
mjr 51:57eb311faafa 4450 // it, allowing it to shoot forward under the force of the main spring.
mjr 51:57eb311faafa 4451 // When we detect that this is happening, we briefly stop reporting the
mjr 51:57eb311faafa 4452 // real physical position that we're reading from the sensor, and instead
mjr 51:57eb311faafa 4453 // report a synthetic series of positions that depicts an idealized
mjr 51:57eb311faafa 4454 // firing motion.
mjr 51:57eb311faafa 4455 //
mjr 51:57eb311faafa 4456 // The point of the synthetic reports is to correct for distortions
mjr 51:57eb311faafa 4457 // created by the joystick interface conventions used by VP and other
mjr 51:57eb311faafa 4458 // PC pinball emulators. The convention they use is simply to have the
mjr 51:57eb311faafa 4459 // plunger device report the instantaneous position of the real plunger.
mjr 51:57eb311faafa 4460 // The PC software polls this reported position periodically, and moves
mjr 51:57eb311faafa 4461 // the on-screen virtual plunger in sync with the real plunger. This
mjr 51:57eb311faafa 4462 // works fine for human-scale motion when the user is manually moving
mjr 51:57eb311faafa 4463 // the plunger. But it doesn't work for the high speed motion of a
mjr 51:57eb311faafa 4464 // release. The plunger simply moves too fast. VP polls in about 10ms
mjr 51:57eb311faafa 4465 // intervals; the plunger takes about 50ms to travel from fully
mjr 51:57eb311faafa 4466 // retracted to the park position when released. The low sampling
mjr 51:57eb311faafa 4467 // rate relative to the rate of change of the sampled data creates
mjr 51:57eb311faafa 4468 // a classic digital aliasing effect.
mjr 51:57eb311faafa 4469 //
mjr 51:57eb311faafa 4470 // The synthetic reporting scheme compensates for the interface
mjr 51:57eb311faafa 4471 // distortions by essentially changing to a coarse enough timescale
mjr 51:57eb311faafa 4472 // that VP can reliably interpret the readings. Conceptually, there
mjr 51:57eb311faafa 4473 // are three steps involved in doing this. First, we analyze the
mjr 51:57eb311faafa 4474 // actual sensor data to detect and characterize the release motion.
mjr 51:57eb311faafa 4475 // Second, once we think we have a release in progress, we fit the
mjr 51:57eb311faafa 4476 // data to a mathematical model of the release. The model we use is
mjr 51:57eb311faafa 4477 // dead simple: we consider the release to have one parameter, namely
mjr 51:57eb311faafa 4478 // the retraction distance at the moment the user lets go. This is an
mjr 51:57eb311faafa 4479 // excellent proxy in the real physical system for the final speed
mjr 51:57eb311faafa 4480 // when the plunger hits the ball, and it also happens to match how
mjr 51:57eb311faafa 4481 // VP models it internally. Third, we construct synthetic reports
mjr 51:57eb311faafa 4482 // that will make VP's internal state match our model. This is also
mjr 51:57eb311faafa 4483 // pretty simple: we just need to send VP the maximum retraction
mjr 51:57eb311faafa 4484 // distance for long enough to be sure that it polls it at least
mjr 51:57eb311faafa 4485 // once, and then send it the park position for long enough to
mjr 51:57eb311faafa 4486 // ensure that VP will complete the same firing motion. The
mjr 51:57eb311faafa 4487 // immediate jump from the maximum point to the zero point will
mjr 51:57eb311faafa 4488 // cause VP to move its simulation model plunger forward from the
mjr 51:57eb311faafa 4489 // starting point at its natural spring acceleration rate, which
mjr 51:57eb311faafa 4490 // is exactly what the real plunger just did.
mjr 51:57eb311faafa 4491 //
mjr 48:058ace2aed1d 4492 class PlungerReader
mjr 48:058ace2aed1d 4493 {
mjr 48:058ace2aed1d 4494 public:
mjr 48:058ace2aed1d 4495 PlungerReader()
mjr 48:058ace2aed1d 4496 {
mjr 48:058ace2aed1d 4497 // not in a firing event yet
mjr 48:058ace2aed1d 4498 firing = 0;
mjr 48:058ace2aed1d 4499
mjr 48:058ace2aed1d 4500 // no history yet
mjr 48:058ace2aed1d 4501 histIdx = 0;
mjr 55:4db125cd11a0 4502
mjr 55:4db125cd11a0 4503 // initialize the filter
mjr 55:4db125cd11a0 4504 initFilter();
mjr 48:058ace2aed1d 4505 }
mjr 76:7f5912b6340e 4506
mjr 48:058ace2aed1d 4507 // Collect a reading from the plunger sensor. The main loop calls
mjr 48:058ace2aed1d 4508 // this frequently to read the current raw position data from the
mjr 48:058ace2aed1d 4509 // sensor. We analyze the raw data to produce the calibrated
mjr 48:058ace2aed1d 4510 // position that we report to the PC via the joystick interface.
mjr 48:058ace2aed1d 4511 void read()
mjr 48:058ace2aed1d 4512 {
mjr 76:7f5912b6340e 4513 // if the sensor is busy, skip the reading on this round
mjr 76:7f5912b6340e 4514 if (!plungerSensor->ready())
mjr 76:7f5912b6340e 4515 return;
mjr 76:7f5912b6340e 4516
mjr 48:058ace2aed1d 4517 // Read a sample from the sensor
mjr 48:058ace2aed1d 4518 PlungerReading r;
mjr 48:058ace2aed1d 4519 if (plungerSensor->read(r))
mjr 48:058ace2aed1d 4520 {
mjr 69:cc5039284fac 4521 // filter the raw sensor reading
mjr 69:cc5039284fac 4522 applyPreFilter(r);
mjr 69:cc5039284fac 4523
mjr 51:57eb311faafa 4524 // Pull the previous reading from the history
mjr 50:40015764bbe6 4525 const PlungerReading &prv = nthHist(0);
mjr 48:058ace2aed1d 4526
mjr 69:cc5039284fac 4527 // If the new reading is within 1ms of the previous reading,
mjr 48:058ace2aed1d 4528 // ignore it. We require a minimum time between samples to
mjr 48:058ace2aed1d 4529 // ensure that we have a usable amount of precision in the
mjr 48:058ace2aed1d 4530 // denominator (the time interval) for calculating the plunger
mjr 69:cc5039284fac 4531 // velocity. The CCD sensor hardware takes about 2.5ms to
mjr 69:cc5039284fac 4532 // read, so it will never be affected by this, but other sensor
mjr 69:cc5039284fac 4533 // types don't all have the same hardware cycle time, so we need
mjr 69:cc5039284fac 4534 // to throttle them artificially. E.g., the potentiometer only
mjr 69:cc5039284fac 4535 // needs one ADC sample per reading, which only takes about 15us.
mjr 69:cc5039284fac 4536 // We don't need to check which sensor type we have here; we
mjr 69:cc5039284fac 4537 // just ignore readings until the minimum interval has passed,
mjr 69:cc5039284fac 4538 // so if the sensor is already slower than this, we'll end up
mjr 69:cc5039284fac 4539 // using all of its readings.
mjr 69:cc5039284fac 4540 if (uint32_t(r.t - prv.t) < 1000UL)
mjr 48:058ace2aed1d 4541 return;
mjr 53:9b2611964afc 4542
mjr 53:9b2611964afc 4543 // check for calibration mode
mjr 53:9b2611964afc 4544 if (plungerCalMode)
mjr 53:9b2611964afc 4545 {
mjr 53:9b2611964afc 4546 // Calibration mode. Adjust the calibration bounds to fit
mjr 53:9b2611964afc 4547 // the value. If this value is beyond the current min or max,
mjr 53:9b2611964afc 4548 // expand the envelope to include this new value.
mjr 53:9b2611964afc 4549 if (r.pos > cfg.plunger.cal.max)
mjr 53:9b2611964afc 4550 cfg.plunger.cal.max = r.pos;
mjr 53:9b2611964afc 4551 if (r.pos < cfg.plunger.cal.min)
mjr 53:9b2611964afc 4552 cfg.plunger.cal.min = r.pos;
mjr 76:7f5912b6340e 4553
mjr 76:7f5912b6340e 4554 // update our cached calibration data
mjr 76:7f5912b6340e 4555 onUpdateCal();
mjr 50:40015764bbe6 4556
mjr 53:9b2611964afc 4557 // If we're in calibration state 0, we're waiting for the
mjr 53:9b2611964afc 4558 // plunger to come to rest at the park position so that we
mjr 53:9b2611964afc 4559 // can take a sample of the park position. Check to see if
mjr 53:9b2611964afc 4560 // we've been at rest for a minimum interval.
mjr 53:9b2611964afc 4561 if (calState == 0)
mjr 53:9b2611964afc 4562 {
mjr 53:9b2611964afc 4563 if (abs(r.pos - calZeroStart.pos) < 65535/3/50)
mjr 53:9b2611964afc 4564 {
mjr 53:9b2611964afc 4565 // we're close enough - make sure we've been here long enough
mjr 53:9b2611964afc 4566 if (uint32_t(r.t - calZeroStart.t) > 100000UL)
mjr 53:9b2611964afc 4567 {
mjr 53:9b2611964afc 4568 // we've been at rest long enough - count it
mjr 53:9b2611964afc 4569 calZeroPosSum += r.pos;
mjr 53:9b2611964afc 4570 calZeroPosN += 1;
mjr 53:9b2611964afc 4571
mjr 53:9b2611964afc 4572 // update the zero position from the new average
mjr 53:9b2611964afc 4573 cfg.plunger.cal.zero = uint16_t(calZeroPosSum / calZeroPosN);
mjr 76:7f5912b6340e 4574 onUpdateCal();
mjr 53:9b2611964afc 4575
mjr 53:9b2611964afc 4576 // switch to calibration state 1 - at rest
mjr 53:9b2611964afc 4577 calState = 1;
mjr 53:9b2611964afc 4578 }
mjr 53:9b2611964afc 4579 }
mjr 53:9b2611964afc 4580 else
mjr 53:9b2611964afc 4581 {
mjr 53:9b2611964afc 4582 // we're not close to the last position - start again here
mjr 53:9b2611964afc 4583 calZeroStart = r;
mjr 53:9b2611964afc 4584 }
mjr 53:9b2611964afc 4585 }
mjr 53:9b2611964afc 4586
mjr 53:9b2611964afc 4587 // Rescale to the joystick range, and adjust for the current
mjr 53:9b2611964afc 4588 // park position, but don't calibrate. We don't know the maximum
mjr 53:9b2611964afc 4589 // point yet, so we can't calibrate the range.
mjr 53:9b2611964afc 4590 r.pos = int(
mjr 53:9b2611964afc 4591 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 4592 / (65535 - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 4593 }
mjr 53:9b2611964afc 4594 else
mjr 53:9b2611964afc 4595 {
mjr 53:9b2611964afc 4596 // Not in calibration mode. Apply the existing calibration and
mjr 53:9b2611964afc 4597 // rescale to the joystick range.
mjr 76:7f5912b6340e 4598 r.pos = applyCal(r.pos);
mjr 53:9b2611964afc 4599
mjr 53:9b2611964afc 4600 // limit the result to the valid joystick range
mjr 53:9b2611964afc 4601 if (r.pos > JOYMAX)
mjr 53:9b2611964afc 4602 r.pos = JOYMAX;
mjr 53:9b2611964afc 4603 else if (r.pos < -JOYMAX)
mjr 53:9b2611964afc 4604 r.pos = -JOYMAX;
mjr 53:9b2611964afc 4605 }
mjr 50:40015764bbe6 4606
mjr 50:40015764bbe6 4607 // Calculate the velocity from the second-to-last reading
mjr 50:40015764bbe6 4608 // to here, in joystick distance units per microsecond.
mjr 50:40015764bbe6 4609 // Note that we use the second-to-last reading rather than
mjr 50:40015764bbe6 4610 // the very last reading to give ourselves a little longer
mjr 50:40015764bbe6 4611 // time base. The time base is so short between consecutive
mjr 50:40015764bbe6 4612 // readings that the error bars in the position would be too
mjr 50:40015764bbe6 4613 // large.
mjr 50:40015764bbe6 4614 //
mjr 50:40015764bbe6 4615 // For reference, the physical plunger velocity ranges up
mjr 50:40015764bbe6 4616 // to about 100,000 joystick distance units/sec. This is
mjr 50:40015764bbe6 4617 // based on empirical measurements. The typical time for
mjr 50:40015764bbe6 4618 // a real plunger to travel the full distance when released
mjr 50:40015764bbe6 4619 // from full retraction is about 85ms, so the average velocity
mjr 50:40015764bbe6 4620 // covering this distance is about 56,000 units/sec. The
mjr 50:40015764bbe6 4621 // peak is probably about twice that. In real-world units,
mjr 50:40015764bbe6 4622 // this translates to an average speed of about .75 m/s and
mjr 50:40015764bbe6 4623 // a peak of about 1.5 m/s.
mjr 50:40015764bbe6 4624 //
mjr 50:40015764bbe6 4625 // Note that we actually calculate the value here in units
mjr 50:40015764bbe6 4626 // per *microsecond* - the discussion above is in terms of
mjr 50:40015764bbe6 4627 // units/sec because that's more on a human scale. Our
mjr 50:40015764bbe6 4628 // choice of internal units here really isn't important,
mjr 50:40015764bbe6 4629 // since we only use the velocity for comparison purposes,
mjr 50:40015764bbe6 4630 // to detect acceleration trends. We therefore save ourselves
mjr 50:40015764bbe6 4631 // a little CPU time by using the natural units of our inputs.
mjr 76:7f5912b6340e 4632 //
mjr 76:7f5912b6340e 4633 // We don't care about the absolute velocity; this is a purely
mjr 76:7f5912b6340e 4634 // relative calculation. So to speed things up, calculate it
mjr 76:7f5912b6340e 4635 // in the integer domain, using a fixed-point representation
mjr 76:7f5912b6340e 4636 // with a 64K scale. In other words, with the stored values
mjr 76:7f5912b6340e 4637 // shifted left 16 bits from the actual values: the value 1
mjr 76:7f5912b6340e 4638 // is stored as 1<<16. The position readings are in the range
mjr 76:7f5912b6340e 4639 // -JOYMAX..JOYMAX, which fits in 16 bits, and the time
mjr 76:7f5912b6340e 4640 // differences will generally be on the scale of a few
mjr 76:7f5912b6340e 4641 // milliseconds = thousands of microseconds. So the velocity
mjr 76:7f5912b6340e 4642 // figures will fit nicely into a 32-bit fixed point value with
mjr 76:7f5912b6340e 4643 // a 64K scale factor.
mjr 51:57eb311faafa 4644 const PlungerReading &prv2 = nthHist(1);
mjr 76:7f5912b6340e 4645 int v = ((r.pos - prv2.pos) << 16)/(r.t - prv2.t);
mjr 50:40015764bbe6 4646
mjr 50:40015764bbe6 4647 // presume we'll report the latest instantaneous reading
mjr 50:40015764bbe6 4648 z = r.pos;
mjr 48:058ace2aed1d 4649
mjr 50:40015764bbe6 4650 // Check firing events
mjr 50:40015764bbe6 4651 switch (firing)
mjr 50:40015764bbe6 4652 {
mjr 50:40015764bbe6 4653 case 0:
mjr 50:40015764bbe6 4654 // Default state - not in a firing event.
mjr 50:40015764bbe6 4655
mjr 50:40015764bbe6 4656 // If we have forward motion from a position that's retracted
mjr 50:40015764bbe6 4657 // beyond a threshold, enter phase 1. If we're not pulled back
mjr 50:40015764bbe6 4658 // far enough, don't bother with this, as a release wouldn't
mjr 50:40015764bbe6 4659 // be strong enough to require the synthetic firing treatment.
mjr 50:40015764bbe6 4660 if (v < 0 && r.pos > JOYMAX/6)
mjr 50:40015764bbe6 4661 {
mjr 53:9b2611964afc 4662 // enter firing phase 1
mjr 50:40015764bbe6 4663 firingMode(1);
mjr 50:40015764bbe6 4664
mjr 53:9b2611964afc 4665 // if in calibration state 1 (at rest), switch to state 2 (not
mjr 53:9b2611964afc 4666 // at rest)
mjr 53:9b2611964afc 4667 if (calState == 1)
mjr 53:9b2611964afc 4668 calState = 2;
mjr 53:9b2611964afc 4669
mjr 50:40015764bbe6 4670 // we don't have a freeze position yet, but note the start time
mjr 50:40015764bbe6 4671 f1.pos = 0;
mjr 50:40015764bbe6 4672 f1.t = r.t;
mjr 50:40015764bbe6 4673
mjr 50:40015764bbe6 4674 // Figure the barrel spring "bounce" position in case we complete
mjr 50:40015764bbe6 4675 // the firing event. This is the amount that the forward momentum
mjr 50:40015764bbe6 4676 // of the plunger will compress the barrel spring at the peak of
mjr 50:40015764bbe6 4677 // the forward travel during the release. Assume that this is
mjr 50:40015764bbe6 4678 // linearly proportional to the starting retraction distance.
mjr 50:40015764bbe6 4679 // The barrel spring is about 1/6 the length of the main spring,
mjr 50:40015764bbe6 4680 // so figure it compresses by 1/6 the distance. (This is overly
mjr 53:9b2611964afc 4681 // simplistic and not very accurate, but it seems to give good
mjr 50:40015764bbe6 4682 // visual results, and that's all it's for.)
mjr 50:40015764bbe6 4683 f2.pos = -r.pos/6;
mjr 50:40015764bbe6 4684 }
mjr 50:40015764bbe6 4685 break;
mjr 50:40015764bbe6 4686
mjr 50:40015764bbe6 4687 case 1:
mjr 50:40015764bbe6 4688 // Phase 1 - acceleration. If we cross the zero point, trigger
mjr 50:40015764bbe6 4689 // the firing event. Otherwise, continue monitoring as long as we
mjr 50:40015764bbe6 4690 // see acceleration in the forward direction.
mjr 50:40015764bbe6 4691 if (r.pos <= 0)
mjr 50:40015764bbe6 4692 {
mjr 50:40015764bbe6 4693 // switch to the synthetic firing mode
mjr 50:40015764bbe6 4694 firingMode(2);
mjr 50:40015764bbe6 4695 z = f2.pos;
mjr 50:40015764bbe6 4696
mjr 50:40015764bbe6 4697 // note the start time for the firing phase
mjr 50:40015764bbe6 4698 f2.t = r.t;
mjr 53:9b2611964afc 4699
mjr 53:9b2611964afc 4700 // if in calibration mode, and we're in state 2 (moving),
mjr 53:9b2611964afc 4701 // collect firing statistics for calibration purposes
mjr 53:9b2611964afc 4702 if (plungerCalMode && calState == 2)
mjr 53:9b2611964afc 4703 {
mjr 53:9b2611964afc 4704 // collect a new zero point for the average when we
mjr 53:9b2611964afc 4705 // come to rest
mjr 53:9b2611964afc 4706 calState = 0;
mjr 53:9b2611964afc 4707
mjr 53:9b2611964afc 4708 // collect average firing time statistics in millseconds, if
mjr 53:9b2611964afc 4709 // it's in range (20 to 255 ms)
mjr 53:9b2611964afc 4710 int dt = uint32_t(r.t - f1.t)/1000UL;
mjr 53:9b2611964afc 4711 if (dt >= 20 && dt <= 255)
mjr 53:9b2611964afc 4712 {
mjr 53:9b2611964afc 4713 calRlsTimeSum += dt;
mjr 53:9b2611964afc 4714 calRlsTimeN += 1;
mjr 53:9b2611964afc 4715 cfg.plunger.cal.tRelease = uint8_t(calRlsTimeSum / calRlsTimeN);
mjr 53:9b2611964afc 4716 }
mjr 53:9b2611964afc 4717 }
mjr 50:40015764bbe6 4718 }
mjr 50:40015764bbe6 4719 else if (v < vprv2)
mjr 50:40015764bbe6 4720 {
mjr 50:40015764bbe6 4721 // We're still accelerating, and we haven't crossed the zero
mjr 50:40015764bbe6 4722 // point yet - stay in phase 1. (Note that forward motion is
mjr 50:40015764bbe6 4723 // negative velocity, so accelerating means that the new
mjr 50:40015764bbe6 4724 // velocity is more negative than the previous one, which
mjr 50:40015764bbe6 4725 // is to say numerically less than - that's why the test
mjr 50:40015764bbe6 4726 // for acceleration is the seemingly backwards 'v < vprv'.)
mjr 50:40015764bbe6 4727
mjr 50:40015764bbe6 4728 // If we've been accelerating for at least 20ms, we're probably
mjr 50:40015764bbe6 4729 // really doing a release. Jump back to the recent local
mjr 50:40015764bbe6 4730 // maximum where the release *really* started. This is always
mjr 50:40015764bbe6 4731 // a bit before we started seeing sustained accleration, because
mjr 50:40015764bbe6 4732 // the plunger motion for the first few milliseconds is too slow
mjr 50:40015764bbe6 4733 // for our sensor precision to reliably detect acceleration.
mjr 50:40015764bbe6 4734 if (f1.pos != 0)
mjr 50:40015764bbe6 4735 {
mjr 50:40015764bbe6 4736 // we have a reset point - freeze there
mjr 50:40015764bbe6 4737 z = f1.pos;
mjr 50:40015764bbe6 4738 }
mjr 50:40015764bbe6 4739 else if (uint32_t(r.t - f1.t) >= 20000UL)
mjr 50:40015764bbe6 4740 {
mjr 50:40015764bbe6 4741 // it's been long enough - set a reset point.
mjr 50:40015764bbe6 4742 f1.pos = z = histLocalMax(r.t, 50000UL);
mjr 50:40015764bbe6 4743 }
mjr 50:40015764bbe6 4744 }
mjr 50:40015764bbe6 4745 else
mjr 50:40015764bbe6 4746 {
mjr 50:40015764bbe6 4747 // We're not accelerating. Cancel the firing event.
mjr 50:40015764bbe6 4748 firingMode(0);
mjr 53:9b2611964afc 4749 calState = 1;
mjr 50:40015764bbe6 4750 }
mjr 50:40015764bbe6 4751 break;
mjr 50:40015764bbe6 4752
mjr 50:40015764bbe6 4753 case 2:
mjr 50:40015764bbe6 4754 // Phase 2 - start of synthetic firing event. Report the fake
mjr 50:40015764bbe6 4755 // bounce for 25ms. VP polls the joystick about every 10ms, so
mjr 50:40015764bbe6 4756 // this should be enough time to guarantee that VP sees this
mjr 50:40015764bbe6 4757 // report at least once.
mjr 50:40015764bbe6 4758 if (uint32_t(r.t - f2.t) < 25000UL)
mjr 50:40015764bbe6 4759 {
mjr 50:40015764bbe6 4760 // report the bounce position
mjr 50:40015764bbe6 4761 z = f2.pos;
mjr 50:40015764bbe6 4762 }
mjr 50:40015764bbe6 4763 else
mjr 50:40015764bbe6 4764 {
mjr 50:40015764bbe6 4765 // it's been long enough - switch to phase 3, where we
mjr 50:40015764bbe6 4766 // report the park position until the real plunger comes
mjr 50:40015764bbe6 4767 // to rest
mjr 50:40015764bbe6 4768 firingMode(3);
mjr 50:40015764bbe6 4769 z = 0;
mjr 50:40015764bbe6 4770
mjr 50:40015764bbe6 4771 // set the start of the "stability window" to the rest position
mjr 50:40015764bbe6 4772 f3s.t = r.t;
mjr 50:40015764bbe6 4773 f3s.pos = 0;
mjr 50:40015764bbe6 4774
mjr 50:40015764bbe6 4775 // set the start of the "retraction window" to the actual position
mjr 50:40015764bbe6 4776 f3r = r;
mjr 50:40015764bbe6 4777 }
mjr 50:40015764bbe6 4778 break;
mjr 50:40015764bbe6 4779
mjr 50:40015764bbe6 4780 case 3:
mjr 50:40015764bbe6 4781 // Phase 3 - in synthetic firing event. Report the park position
mjr 50:40015764bbe6 4782 // until the plunger position stabilizes. Left to its own devices,
mjr 50:40015764bbe6 4783 // the plunger will usualy bounce off the barrel spring several
mjr 50:40015764bbe6 4784 // times before coming to rest, so we'll see oscillating motion
mjr 50:40015764bbe6 4785 // for a second or two. In the simplest case, we can aimply wait
mjr 50:40015764bbe6 4786 // for the plunger to stop moving for a short time. However, the
mjr 50:40015764bbe6 4787 // player might intervene by pulling the plunger back again, so
mjr 50:40015764bbe6 4788 // watch for that motion as well. If we're just bouncing freely,
mjr 50:40015764bbe6 4789 // we'll see the direction change frequently. If the player is
mjr 50:40015764bbe6 4790 // moving the plunger manually, the direction will be constant
mjr 50:40015764bbe6 4791 // for longer.
mjr 50:40015764bbe6 4792 if (v >= 0)
mjr 50:40015764bbe6 4793 {
mjr 50:40015764bbe6 4794 // We're moving back (or standing still). If this has been
mjr 50:40015764bbe6 4795 // going on for a while, the user must have taken control.
mjr 50:40015764bbe6 4796 if (uint32_t(r.t - f3r.t) > 65000UL)
mjr 50:40015764bbe6 4797 {
mjr 50:40015764bbe6 4798 // user has taken control - cancel firing mode
mjr 50:40015764bbe6 4799 firingMode(0);
mjr 50:40015764bbe6 4800 break;
mjr 50:40015764bbe6 4801 }
mjr 50:40015764bbe6 4802 }
mjr 50:40015764bbe6 4803 else
mjr 50:40015764bbe6 4804 {
mjr 50:40015764bbe6 4805 // forward motion - reset retraction window
mjr 50:40015764bbe6 4806 f3r.t = r.t;
mjr 50:40015764bbe6 4807 }
mjr 50:40015764bbe6 4808
mjr 53:9b2611964afc 4809 // Check if we're close to the last starting point. The joystick
mjr 53:9b2611964afc 4810 // positive axis range (0..4096) covers the retraction distance of
mjr 53:9b2611964afc 4811 // about 2.5", so 1" is about 1638 joystick units, hence 1/16" is
mjr 53:9b2611964afc 4812 // about 100 units.
mjr 53:9b2611964afc 4813 if (abs(r.pos - f3s.pos) < 100)
mjr 50:40015764bbe6 4814 {
mjr 53:9b2611964afc 4815 // It's at roughly the same position as the starting point.
mjr 53:9b2611964afc 4816 // Consider it stable if this has been true for 300ms.
mjr 50:40015764bbe6 4817 if (uint32_t(r.t - f3s.t) > 30000UL)
mjr 50:40015764bbe6 4818 {
mjr 50:40015764bbe6 4819 // we're done with the firing event
mjr 50:40015764bbe6 4820 firingMode(0);
mjr 50:40015764bbe6 4821 }
mjr 50:40015764bbe6 4822 else
mjr 50:40015764bbe6 4823 {
mjr 50:40015764bbe6 4824 // it's close to the last position but hasn't been
mjr 50:40015764bbe6 4825 // here long enough; stay in firing mode and continue
mjr 50:40015764bbe6 4826 // to report the park position
mjr 50:40015764bbe6 4827 z = 0;
mjr 50:40015764bbe6 4828 }
mjr 50:40015764bbe6 4829 }
mjr 50:40015764bbe6 4830 else
mjr 50:40015764bbe6 4831 {
mjr 50:40015764bbe6 4832 // It's not close enough to the last starting point, so use
mjr 50:40015764bbe6 4833 // this as a new starting point, and stay in firing mode.
mjr 50:40015764bbe6 4834 f3s = r;
mjr 50:40015764bbe6 4835 z = 0;
mjr 50:40015764bbe6 4836 }
mjr 50:40015764bbe6 4837 break;
mjr 50:40015764bbe6 4838 }
mjr 50:40015764bbe6 4839
mjr 50:40015764bbe6 4840 // save the velocity reading for next time
mjr 50:40015764bbe6 4841 vprv2 = vprv;
mjr 50:40015764bbe6 4842 vprv = v;
mjr 50:40015764bbe6 4843
mjr 50:40015764bbe6 4844 // add the new reading to the history
mjr 76:7f5912b6340e 4845 hist[histIdx] = r;
mjr 76:7f5912b6340e 4846 if (++histIdx > countof(hist))
mjr 76:7f5912b6340e 4847 histIdx = 0;
mjr 58:523fdcffbe6d 4848
mjr 69:cc5039284fac 4849 // apply the post-processing filter
mjr 69:cc5039284fac 4850 zf = applyPostFilter();
mjr 48:058ace2aed1d 4851 }
mjr 48:058ace2aed1d 4852 }
mjr 48:058ace2aed1d 4853
mjr 48:058ace2aed1d 4854 // Get the current value to report through the joystick interface
mjr 58:523fdcffbe6d 4855 int16_t getPosition()
mjr 58:523fdcffbe6d 4856 {
mjr 58:523fdcffbe6d 4857 // return the last filtered reading
mjr 58:523fdcffbe6d 4858 return zf;
mjr 55:4db125cd11a0 4859 }
mjr 58:523fdcffbe6d 4860
mjr 48:058ace2aed1d 4861 // get the timestamp of the current joystick report (microseconds)
mjr 50:40015764bbe6 4862 uint32_t getTimestamp() const { return nthHist(0).t; }
mjr 48:058ace2aed1d 4863
mjr 48:058ace2aed1d 4864 // Set calibration mode on or off
mjr 52:8298b2a73eb2 4865 void setCalMode(bool f)
mjr 48:058ace2aed1d 4866 {
mjr 52:8298b2a73eb2 4867 // check to see if we're entering calibration mode
mjr 52:8298b2a73eb2 4868 if (f && !plungerCalMode)
mjr 52:8298b2a73eb2 4869 {
mjr 52:8298b2a73eb2 4870 // reset the calibration in the configuration
mjr 48:058ace2aed1d 4871 cfg.plunger.cal.begin();
mjr 52:8298b2a73eb2 4872
mjr 52:8298b2a73eb2 4873 // start in state 0 (waiting to settle)
mjr 52:8298b2a73eb2 4874 calState = 0;
mjr 52:8298b2a73eb2 4875 calZeroPosSum = 0;
mjr 52:8298b2a73eb2 4876 calZeroPosN = 0;
mjr 52:8298b2a73eb2 4877 calRlsTimeSum = 0;
mjr 52:8298b2a73eb2 4878 calRlsTimeN = 0;
mjr 52:8298b2a73eb2 4879
mjr 52:8298b2a73eb2 4880 // set the initial zero point to the current position
mjr 52:8298b2a73eb2 4881 PlungerReading r;
mjr 52:8298b2a73eb2 4882 if (plungerSensor->read(r))
mjr 52:8298b2a73eb2 4883 {
mjr 52:8298b2a73eb2 4884 // got a reading - use it as the initial zero point
mjr 69:cc5039284fac 4885 applyPreFilter(r);
mjr 52:8298b2a73eb2 4886 cfg.plunger.cal.zero = r.pos;
mjr 76:7f5912b6340e 4887 onUpdateCal();
mjr 52:8298b2a73eb2 4888
mjr 52:8298b2a73eb2 4889 // use it as the starting point for the settling watch
mjr 53:9b2611964afc 4890 calZeroStart = r;
mjr 52:8298b2a73eb2 4891 }
mjr 52:8298b2a73eb2 4892 else
mjr 52:8298b2a73eb2 4893 {
mjr 52:8298b2a73eb2 4894 // no reading available - use the default 1/6 position
mjr 52:8298b2a73eb2 4895 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 4896 onUpdateCal();
mjr 52:8298b2a73eb2 4897
mjr 52:8298b2a73eb2 4898 // we don't have a starting point for the setting watch
mjr 53:9b2611964afc 4899 calZeroStart.pos = -65535;
mjr 53:9b2611964afc 4900 calZeroStart.t = 0;
mjr 53:9b2611964afc 4901 }
mjr 53:9b2611964afc 4902 }
mjr 53:9b2611964afc 4903 else if (!f && plungerCalMode)
mjr 53:9b2611964afc 4904 {
mjr 53:9b2611964afc 4905 // Leaving calibration mode. Make sure the max is past the
mjr 53:9b2611964afc 4906 // zero point - if it's not, we'd have a zero or negative
mjr 53:9b2611964afc 4907 // denominator for the scaling calculation, which would be
mjr 53:9b2611964afc 4908 // physically meaningless.
mjr 53:9b2611964afc 4909 if (cfg.plunger.cal.max <= cfg.plunger.cal.zero)
mjr 53:9b2611964afc 4910 {
mjr 53:9b2611964afc 4911 // bad settings - reset to defaults
mjr 53:9b2611964afc 4912 cfg.plunger.cal.max = 0xffff;
mjr 53:9b2611964afc 4913 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 4914 onUpdateCal();
mjr 52:8298b2a73eb2 4915 }
mjr 52:8298b2a73eb2 4916 }
mjr 52:8298b2a73eb2 4917
mjr 48:058ace2aed1d 4918 // remember the new mode
mjr 52:8298b2a73eb2 4919 plungerCalMode = f;
mjr 48:058ace2aed1d 4920 }
mjr 48:058ace2aed1d 4921
mjr 76:7f5912b6340e 4922 // Cached inverse of the calibration range. This is for calculating
mjr 76:7f5912b6340e 4923 // the calibrated plunger position given a raw sensor reading. The
mjr 76:7f5912b6340e 4924 // cached inverse is calculated as
mjr 76:7f5912b6340e 4925 //
mjr 76:7f5912b6340e 4926 // 64K * JOYMAX / (cfg.plunger.cal.max - cfg.plunger.cal.zero)
mjr 76:7f5912b6340e 4927 //
mjr 76:7f5912b6340e 4928 // To convert a raw sensor reading to a calibrated position, calculate
mjr 76:7f5912b6340e 4929 //
mjr 76:7f5912b6340e 4930 // ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16
mjr 76:7f5912b6340e 4931 //
mjr 76:7f5912b6340e 4932 // That yields the calibration result without performing a division.
mjr 76:7f5912b6340e 4933 int invCalRange;
mjr 76:7f5912b6340e 4934
mjr 76:7f5912b6340e 4935 // apply the calibration range to a reading
mjr 76:7f5912b6340e 4936 inline int applyCal(int reading)
mjr 76:7f5912b6340e 4937 {
mjr 76:7f5912b6340e 4938 return ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16;
mjr 76:7f5912b6340e 4939 }
mjr 76:7f5912b6340e 4940
mjr 76:7f5912b6340e 4941 void onUpdateCal()
mjr 76:7f5912b6340e 4942 {
mjr 76:7f5912b6340e 4943 invCalRange = (JOYMAX << 16)/(cfg.plunger.cal.max - cfg.plunger.cal.zero);
mjr 76:7f5912b6340e 4944 }
mjr 76:7f5912b6340e 4945
mjr 48:058ace2aed1d 4946 // is a firing event in progress?
mjr 53:9b2611964afc 4947 bool isFiring() { return firing == 3; }
mjr 76:7f5912b6340e 4948
mjr 48:058ace2aed1d 4949 private:
mjr 52:8298b2a73eb2 4950
mjr 74:822a92bc11d2 4951 // Plunger data filtering mode: optionally apply filtering to the raw
mjr 74:822a92bc11d2 4952 // plunger sensor readings to try to reduce noise in the signal. This
mjr 74:822a92bc11d2 4953 // is designed for the TSL1410/12 optical sensors, where essentially all
mjr 74:822a92bc11d2 4954 // of the noise in the signal comes from lack of sharpness in the shadow
mjr 74:822a92bc11d2 4955 // edge. When the shadow is blurry, the edge detector has to pick a pixel,
mjr 74:822a92bc11d2 4956 // even though the edge is actually a gradient spanning several pixels.
mjr 74:822a92bc11d2 4957 // The edge detection algorithm decides on the exact pixel, but whatever
mjr 74:822a92bc11d2 4958 // the algorithm, the choice is going to be somewhat arbitrary given that
mjr 74:822a92bc11d2 4959 // there's really no one pixel that's "the edge" when the edge actually
mjr 74:822a92bc11d2 4960 // covers multiple pixels. This can make the choice of pixel sensitive to
mjr 74:822a92bc11d2 4961 // small changes in exposure and pixel respose from frame to frame, which
mjr 74:822a92bc11d2 4962 // means that the reported edge position can move by a pixel or two from
mjr 74:822a92bc11d2 4963 // one frame to the next even when the physical plunger is perfectly still.
mjr 74:822a92bc11d2 4964 // That's the noise we're talking about.
mjr 74:822a92bc11d2 4965 //
mjr 74:822a92bc11d2 4966 // We previously applied a mild hysteresis filter to the signal to try to
mjr 74:822a92bc11d2 4967 // eliminate this noise. The filter tracked the average over the last
mjr 74:822a92bc11d2 4968 // several samples, and rejected readings that wandered within a few
mjr 74:822a92bc11d2 4969 // pixels of the average. If a certain number of readings moved away from
mjr 74:822a92bc11d2 4970 // the average in the same direction, even by small amounts, the filter
mjr 74:822a92bc11d2 4971 // accepted the changes, on the assumption that they represented actual
mjr 74:822a92bc11d2 4972 // slow movement of the plunger. This filter was applied after the firing
mjr 74:822a92bc11d2 4973 // detection.
mjr 74:822a92bc11d2 4974 //
mjr 74:822a92bc11d2 4975 // I also tried a simpler filter that rejected changes that were too fast
mjr 74:822a92bc11d2 4976 // to be physically possible, as well as changes that were very close to
mjr 74:822a92bc11d2 4977 // the last reported position (i.e., simple hysteresis). The "too fast"
mjr 74:822a92bc11d2 4978 // filter was there to reject spurious readings where the edge detector
mjr 74:822a92bc11d2 4979 // mistook a bad pixel value as an edge.
mjr 74:822a92bc11d2 4980 //
mjr 74:822a92bc11d2 4981 // The new "mode 2" edge detector (see ccdSensor.h) seems to do a better
mjr 74:822a92bc11d2 4982 // job of rejecting pixel-level noise by itself than the older "mode 0"
mjr 74:822a92bc11d2 4983 // algorithm did, so I removed the filtering entirely. Any filtering has
mjr 74:822a92bc11d2 4984 // some downsides, so it's better to reduce noise in the underlying signal
mjr 74:822a92bc11d2 4985 // as much as possible first. It seems possible to get a very stable signal
mjr 74:822a92bc11d2 4986 // now with a combination of the mode 2 edge detector and optimizing the
mjr 74:822a92bc11d2 4987 // physical sensor arrangement, especially optimizing the light source to
mjr 74:822a92bc11d2 4988 // cast as sharp as shadow as possible and adjusting the brightness to
mjr 74:822a92bc11d2 4989 // maximize bright/dark contrast in the image.
mjr 74:822a92bc11d2 4990 //
mjr 74:822a92bc11d2 4991 // 0 = No filtering (current default)
mjr 74:822a92bc11d2 4992 // 1 = Filter the data after firing detection using moving average
mjr 74:822a92bc11d2 4993 // hysteresis filter (old version, used in most 2016 releases)
mjr 74:822a92bc11d2 4994 // 2 = Filter the data before firing detection using simple hysteresis
mjr 74:822a92bc11d2 4995 // plus spurious "too fast" motion rejection
mjr 74:822a92bc11d2 4996 //
mjr 73:4e8ce0b18915 4997 #define PLUNGER_FILTERING_MODE 0
mjr 73:4e8ce0b18915 4998
mjr 73:4e8ce0b18915 4999 #if PLUNGER_FILTERING_MODE == 0
mjr 69:cc5039284fac 5000 // Disable all filtering
mjr 74:822a92bc11d2 5001 inline void applyPreFilter(PlungerReading &r) { }
mjr 74:822a92bc11d2 5002 inline int applyPostFilter() { return z; }
mjr 73:4e8ce0b18915 5003 #elif PLUNGER_FILTERING_MODE == 1
mjr 73:4e8ce0b18915 5004 // Apply pre-processing filter. This filter is applied to the raw
mjr 73:4e8ce0b18915 5005 // value coming off the sensor, before calibration or fire-event
mjr 73:4e8ce0b18915 5006 // processing.
mjr 73:4e8ce0b18915 5007 void applyPreFilter(PlungerReading &r)
mjr 73:4e8ce0b18915 5008 {
mjr 73:4e8ce0b18915 5009 }
mjr 73:4e8ce0b18915 5010
mjr 73:4e8ce0b18915 5011 // Figure the next post-processing filtered value. This applies a
mjr 73:4e8ce0b18915 5012 // hysteresis filter to the last raw z value and returns the
mjr 73:4e8ce0b18915 5013 // filtered result.
mjr 73:4e8ce0b18915 5014 int applyPostFilter()
mjr 73:4e8ce0b18915 5015 {
mjr 73:4e8ce0b18915 5016 if (firing <= 1)
mjr 73:4e8ce0b18915 5017 {
mjr 73:4e8ce0b18915 5018 // Filter limit - 5 samples. Once we've been moving
mjr 73:4e8ce0b18915 5019 // in the same direction for this many samples, we'll
mjr 73:4e8ce0b18915 5020 // clear the history and start over.
mjr 73:4e8ce0b18915 5021 const int filterMask = 0x1f;
mjr 73:4e8ce0b18915 5022
mjr 73:4e8ce0b18915 5023 // figure the last average
mjr 73:4e8ce0b18915 5024 int lastAvg = int(filterSum / filterN);
mjr 73:4e8ce0b18915 5025
mjr 73:4e8ce0b18915 5026 // figure the direction of this sample relative to the average,
mjr 73:4e8ce0b18915 5027 // and shift it in to our bit mask of recent direction data
mjr 73:4e8ce0b18915 5028 if (z != lastAvg)
mjr 73:4e8ce0b18915 5029 {
mjr 73:4e8ce0b18915 5030 // shift the new direction bit into the vector
mjr 73:4e8ce0b18915 5031 filterDir <<= 1;
mjr 73:4e8ce0b18915 5032 if (z > lastAvg) filterDir |= 1;
mjr 73:4e8ce0b18915 5033 }
mjr 73:4e8ce0b18915 5034
mjr 73:4e8ce0b18915 5035 // keep only the last N readings, up to the filter limit
mjr 73:4e8ce0b18915 5036 filterDir &= filterMask;
mjr 73:4e8ce0b18915 5037
mjr 73:4e8ce0b18915 5038 // if we've been moving consistently in one direction (all 1's
mjr 73:4e8ce0b18915 5039 // or all 0's in the direction history vector), reset the average
mjr 73:4e8ce0b18915 5040 if (filterDir == 0x00 || filterDir == filterMask)
mjr 73:4e8ce0b18915 5041 {
mjr 73:4e8ce0b18915 5042 // motion away from the average - reset the average
mjr 73:4e8ce0b18915 5043 filterDir = 0x5555;
mjr 73:4e8ce0b18915 5044 filterN = 1;
mjr 73:4e8ce0b18915 5045 filterSum = (lastAvg + z)/2;
mjr 73:4e8ce0b18915 5046 return int16_t(filterSum);
mjr 73:4e8ce0b18915 5047 }
mjr 73:4e8ce0b18915 5048 else
mjr 73:4e8ce0b18915 5049 {
mjr 73:4e8ce0b18915 5050 // we're directionless - return the new average, with the
mjr 73:4e8ce0b18915 5051 // new sample included
mjr 73:4e8ce0b18915 5052 filterSum += z;
mjr 73:4e8ce0b18915 5053 ++filterN;
mjr 73:4e8ce0b18915 5054 return int16_t(filterSum / filterN);
mjr 73:4e8ce0b18915 5055 }
mjr 73:4e8ce0b18915 5056 }
mjr 73:4e8ce0b18915 5057 else
mjr 73:4e8ce0b18915 5058 {
mjr 73:4e8ce0b18915 5059 // firing mode - skip the filter
mjr 73:4e8ce0b18915 5060 filterN = 1;
mjr 73:4e8ce0b18915 5061 filterSum = z;
mjr 73:4e8ce0b18915 5062 filterDir = 0x5555;
mjr 73:4e8ce0b18915 5063 return z;
mjr 73:4e8ce0b18915 5064 }
mjr 73:4e8ce0b18915 5065 }
mjr 73:4e8ce0b18915 5066 #elif PLUNGER_FILTERING_MODE == 2
mjr 69:cc5039284fac 5067 // Apply pre-processing filter. This filter is applied to the raw
mjr 69:cc5039284fac 5068 // value coming off the sensor, before calibration or fire-event
mjr 69:cc5039284fac 5069 // processing.
mjr 69:cc5039284fac 5070 void applyPreFilter(PlungerReading &r)
mjr 69:cc5039284fac 5071 {
mjr 69:cc5039284fac 5072 // get the previous raw reading
mjr 69:cc5039284fac 5073 PlungerReading prv = pre.raw;
mjr 69:cc5039284fac 5074
mjr 69:cc5039284fac 5075 // the new reading is the previous raw reading next time, no
mjr 69:cc5039284fac 5076 // matter how we end up filtering it
mjr 69:cc5039284fac 5077 pre.raw = r;
mjr 69:cc5039284fac 5078
mjr 69:cc5039284fac 5079 // If it's too big an excursion from the previous raw reading,
mjr 69:cc5039284fac 5080 // ignore it and repeat the previous reported reading. This
mjr 69:cc5039284fac 5081 // filters out anomalous spikes where we suddenly jump to a
mjr 69:cc5039284fac 5082 // level that's too far away to be possible. Real plungers
mjr 69:cc5039284fac 5083 // take about 60ms to travel the full distance when released,
mjr 69:cc5039284fac 5084 // so assuming constant acceleration, the maximum realistic
mjr 69:cc5039284fac 5085 // speed is about 2.200 distance units (on our 0..0xffff scale)
mjr 69:cc5039284fac 5086 // per microsecond.
mjr 69:cc5039284fac 5087 //
mjr 69:cc5039284fac 5088 // On the other hand, if the new reading is too *close* to the
mjr 69:cc5039284fac 5089 // previous reading, use the previous reported reading. This
mjr 69:cc5039284fac 5090 // filters out jitter around a stationary position.
mjr 69:cc5039284fac 5091 const float maxDist = 2.184f*uint32_t(r.t - prv.t);
mjr 69:cc5039284fac 5092 const int minDist = 256;
mjr 69:cc5039284fac 5093 const int delta = abs(r.pos - prv.pos);
mjr 69:cc5039284fac 5094 if (maxDist > minDist && delta > maxDist)
mjr 69:cc5039284fac 5095 {
mjr 69:cc5039284fac 5096 // too big an excursion - discard this reading by reporting
mjr 69:cc5039284fac 5097 // the last reported reading instead
mjr 69:cc5039284fac 5098 r.pos = pre.reported;
mjr 69:cc5039284fac 5099 }
mjr 69:cc5039284fac 5100 else if (delta < minDist)
mjr 69:cc5039284fac 5101 {
mjr 69:cc5039284fac 5102 // too close to the prior reading - apply hysteresis
mjr 69:cc5039284fac 5103 r.pos = pre.reported;
mjr 69:cc5039284fac 5104 }
mjr 69:cc5039284fac 5105 else
mjr 69:cc5039284fac 5106 {
mjr 69:cc5039284fac 5107 // the reading is in range - keep it, and remember it as
mjr 69:cc5039284fac 5108 // the last reported reading
mjr 69:cc5039284fac 5109 pre.reported = r.pos;
mjr 69:cc5039284fac 5110 }
mjr 69:cc5039284fac 5111 }
mjr 69:cc5039284fac 5112
mjr 69:cc5039284fac 5113 // pre-filter data
mjr 69:cc5039284fac 5114 struct PreFilterData {
mjr 69:cc5039284fac 5115 PreFilterData()
mjr 69:cc5039284fac 5116 : reported(0)
mjr 69:cc5039284fac 5117 {
mjr 69:cc5039284fac 5118 raw.t = 0;
mjr 69:cc5039284fac 5119 raw.pos = 0;
mjr 69:cc5039284fac 5120 }
mjr 69:cc5039284fac 5121 PlungerReading raw; // previous raw sensor reading
mjr 69:cc5039284fac 5122 int reported; // previous reported reading
mjr 69:cc5039284fac 5123 } pre;
mjr 69:cc5039284fac 5124
mjr 69:cc5039284fac 5125
mjr 69:cc5039284fac 5126 // Apply the post-processing filter. This filter is applied after
mjr 69:cc5039284fac 5127 // the fire-event processing. In the past, this used hysteresis to
mjr 69:cc5039284fac 5128 // try to smooth out jittering readings for a stationary plunger.
mjr 69:cc5039284fac 5129 // We've switched to a different approach that massages the readings
mjr 69:cc5039284fac 5130 // coming off the sensor before
mjr 69:cc5039284fac 5131 int applyPostFilter()
mjr 69:cc5039284fac 5132 {
mjr 69:cc5039284fac 5133 return z;
mjr 69:cc5039284fac 5134 }
mjr 69:cc5039284fac 5135 #endif
mjr 58:523fdcffbe6d 5136
mjr 58:523fdcffbe6d 5137 void initFilter()
mjr 58:523fdcffbe6d 5138 {
mjr 58:523fdcffbe6d 5139 filterSum = 0;
mjr 58:523fdcffbe6d 5140 filterN = 1;
mjr 58:523fdcffbe6d 5141 filterDir = 0x5555;
mjr 58:523fdcffbe6d 5142 }
mjr 58:523fdcffbe6d 5143 int64_t filterSum;
mjr 58:523fdcffbe6d 5144 int64_t filterN;
mjr 58:523fdcffbe6d 5145 uint16_t filterDir;
mjr 58:523fdcffbe6d 5146
mjr 58:523fdcffbe6d 5147
mjr 52:8298b2a73eb2 5148 // Calibration state. During calibration mode, we watch for release
mjr 52:8298b2a73eb2 5149 // events, to measure the time it takes to complete the release
mjr 52:8298b2a73eb2 5150 // motion; and we watch for the plunger to come to reset after a
mjr 52:8298b2a73eb2 5151 // release, to gather statistics on the rest position.
mjr 52:8298b2a73eb2 5152 // 0 = waiting to settle
mjr 52:8298b2a73eb2 5153 // 1 = at rest
mjr 52:8298b2a73eb2 5154 // 2 = retracting
mjr 52:8298b2a73eb2 5155 // 3 = possibly releasing
mjr 52:8298b2a73eb2 5156 uint8_t calState;
mjr 52:8298b2a73eb2 5157
mjr 52:8298b2a73eb2 5158 // Calibration zero point statistics.
mjr 52:8298b2a73eb2 5159 // During calibration mode, we collect data on the rest position (the
mjr 52:8298b2a73eb2 5160 // zero point) by watching for the plunger to come to rest after each
mjr 52:8298b2a73eb2 5161 // release. We average these rest positions to get the calibrated
mjr 52:8298b2a73eb2 5162 // zero point. We use the average because the real physical plunger
mjr 52:8298b2a73eb2 5163 // itself doesn't come to rest at exactly the same spot every time,
mjr 52:8298b2a73eb2 5164 // largely due to friction in the mechanism. To calculate the average,
mjr 52:8298b2a73eb2 5165 // we keep a sum of the readings and a count of samples.
mjr 53:9b2611964afc 5166 PlungerReading calZeroStart;
mjr 52:8298b2a73eb2 5167 long calZeroPosSum;
mjr 52:8298b2a73eb2 5168 int calZeroPosN;
mjr 52:8298b2a73eb2 5169
mjr 52:8298b2a73eb2 5170 // Calibration release time statistics.
mjr 52:8298b2a73eb2 5171 // During calibration, we collect an average for the release time.
mjr 52:8298b2a73eb2 5172 long calRlsTimeSum;
mjr 52:8298b2a73eb2 5173 int calRlsTimeN;
mjr 52:8298b2a73eb2 5174
mjr 48:058ace2aed1d 5175 // set a firing mode
mjr 48:058ace2aed1d 5176 inline void firingMode(int m)
mjr 48:058ace2aed1d 5177 {
mjr 48:058ace2aed1d 5178 firing = m;
mjr 48:058ace2aed1d 5179 }
mjr 48:058ace2aed1d 5180
mjr 48:058ace2aed1d 5181 // Find the most recent local maximum in the history data, up to
mjr 48:058ace2aed1d 5182 // the given time limit.
mjr 48:058ace2aed1d 5183 int histLocalMax(uint32_t tcur, uint32_t dt)
mjr 48:058ace2aed1d 5184 {
mjr 48:058ace2aed1d 5185 // start with the prior entry
mjr 48:058ace2aed1d 5186 int idx = (histIdx == 0 ? countof(hist) : histIdx) - 1;
mjr 48:058ace2aed1d 5187 int hi = hist[idx].pos;
mjr 48:058ace2aed1d 5188
mjr 48:058ace2aed1d 5189 // scan backwards for a local maximum
mjr 48:058ace2aed1d 5190 for (int n = countof(hist) - 1 ; n > 0 ; idx = (idx == 0 ? countof(hist) : idx) - 1)
mjr 48:058ace2aed1d 5191 {
mjr 48:058ace2aed1d 5192 // if this isn't within the time window, stop
mjr 48:058ace2aed1d 5193 if (uint32_t(tcur - hist[idx].t) > dt)
mjr 48:058ace2aed1d 5194 break;
mjr 48:058ace2aed1d 5195
mjr 48:058ace2aed1d 5196 // if this isn't above the current hith, stop
mjr 48:058ace2aed1d 5197 if (hist[idx].pos < hi)
mjr 48:058ace2aed1d 5198 break;
mjr 48:058ace2aed1d 5199
mjr 48:058ace2aed1d 5200 // this is the new high
mjr 48:058ace2aed1d 5201 hi = hist[idx].pos;
mjr 48:058ace2aed1d 5202 }
mjr 48:058ace2aed1d 5203
mjr 48:058ace2aed1d 5204 // return the local maximum
mjr 48:058ace2aed1d 5205 return hi;
mjr 48:058ace2aed1d 5206 }
mjr 48:058ace2aed1d 5207
mjr 50:40015764bbe6 5208 // velocity at previous reading, and the one before that
mjr 76:7f5912b6340e 5209 int vprv, vprv2;
mjr 48:058ace2aed1d 5210
mjr 48:058ace2aed1d 5211 // Circular buffer of recent readings. We keep a short history
mjr 48:058ace2aed1d 5212 // of readings to analyze during firing events. We can only identify
mjr 48:058ace2aed1d 5213 // a firing event once it's somewhat under way, so we need a little
mjr 48:058ace2aed1d 5214 // retrospective information to accurately determine after the fact
mjr 48:058ace2aed1d 5215 // exactly when it started. We throttle our readings to no more
mjr 74:822a92bc11d2 5216 // than one every 1ms, so we have at least N*1ms of history in this
mjr 48:058ace2aed1d 5217 // array.
mjr 74:822a92bc11d2 5218 PlungerReading hist[32];
mjr 48:058ace2aed1d 5219 int histIdx;
mjr 49:37bd97eb7688 5220
mjr 50:40015764bbe6 5221 // get the nth history item (0=last, 1=2nd to last, etc)
mjr 74:822a92bc11d2 5222 inline const PlungerReading &nthHist(int n) const
mjr 50:40015764bbe6 5223 {
mjr 50:40015764bbe6 5224 // histIdx-1 is the last written; go from there
mjr 50:40015764bbe6 5225 n = histIdx - 1 - n;
mjr 50:40015764bbe6 5226
mjr 50:40015764bbe6 5227 // adjust for wrapping
mjr 50:40015764bbe6 5228 if (n < 0)
mjr 50:40015764bbe6 5229 n += countof(hist);
mjr 50:40015764bbe6 5230
mjr 50:40015764bbe6 5231 // return the item
mjr 50:40015764bbe6 5232 return hist[n];
mjr 50:40015764bbe6 5233 }
mjr 48:058ace2aed1d 5234
mjr 48:058ace2aed1d 5235 // Firing event state.
mjr 48:058ace2aed1d 5236 //
mjr 48:058ace2aed1d 5237 // 0 - Default state. We report the real instantaneous plunger
mjr 48:058ace2aed1d 5238 // position to the joystick interface.
mjr 48:058ace2aed1d 5239 //
mjr 53:9b2611964afc 5240 // 1 - Moving forward
mjr 48:058ace2aed1d 5241 //
mjr 53:9b2611964afc 5242 // 2 - Accelerating
mjr 48:058ace2aed1d 5243 //
mjr 53:9b2611964afc 5244 // 3 - Firing. We report the rest position for a minimum interval,
mjr 53:9b2611964afc 5245 // or until the real plunger comes to rest somewhere.
mjr 48:058ace2aed1d 5246 //
mjr 48:058ace2aed1d 5247 int firing;
mjr 48:058ace2aed1d 5248
mjr 51:57eb311faafa 5249 // Position/timestamp at start of firing phase 1. When we see a
mjr 51:57eb311faafa 5250 // sustained forward acceleration, we freeze joystick reports at
mjr 51:57eb311faafa 5251 // the recent local maximum, on the assumption that this was the
mjr 51:57eb311faafa 5252 // start of the release. If this is zero, it means that we're
mjr 51:57eb311faafa 5253 // monitoring accelerating motion but haven't seen it for long
mjr 51:57eb311faafa 5254 // enough yet to be confident that a release is in progress.
mjr 48:058ace2aed1d 5255 PlungerReading f1;
mjr 48:058ace2aed1d 5256
mjr 48:058ace2aed1d 5257 // Position/timestamp at start of firing phase 2. The position is
mjr 48:058ace2aed1d 5258 // the fake "bounce" position we report during this phase, and the
mjr 48:058ace2aed1d 5259 // timestamp tells us when the phase began so that we can end it
mjr 48:058ace2aed1d 5260 // after enough time elapses.
mjr 48:058ace2aed1d 5261 PlungerReading f2;
mjr 48:058ace2aed1d 5262
mjr 48:058ace2aed1d 5263 // Position/timestamp of start of stability window during phase 3.
mjr 48:058ace2aed1d 5264 // We use this to determine when the plunger comes to rest. We set
mjr 51:57eb311faafa 5265 // this at the beginning of phase 3, and then reset it when the
mjr 48:058ace2aed1d 5266 // plunger moves too far from the last position.
mjr 48:058ace2aed1d 5267 PlungerReading f3s;
mjr 48:058ace2aed1d 5268
mjr 48:058ace2aed1d 5269 // Position/timestamp of start of retraction window during phase 3.
mjr 48:058ace2aed1d 5270 // We use this to determine if the user is drawing the plunger back.
mjr 48:058ace2aed1d 5271 // If we see retraction motion for more than about 65ms, we assume
mjr 48:058ace2aed1d 5272 // that the user has taken over, because we should see forward
mjr 48:058ace2aed1d 5273 // motion within this timeframe if the plunger is just bouncing
mjr 48:058ace2aed1d 5274 // freely.
mjr 48:058ace2aed1d 5275 PlungerReading f3r;
mjr 48:058ace2aed1d 5276
mjr 58:523fdcffbe6d 5277 // next raw (unfiltered) Z value to report to the joystick interface
mjr 58:523fdcffbe6d 5278 // (in joystick distance units)
mjr 48:058ace2aed1d 5279 int z;
mjr 48:058ace2aed1d 5280
mjr 58:523fdcffbe6d 5281 // next filtered Z value to report to the joystick interface
mjr 58:523fdcffbe6d 5282 int zf;
mjr 48:058ace2aed1d 5283 };
mjr 48:058ace2aed1d 5284
mjr 48:058ace2aed1d 5285 // plunger reader singleton
mjr 48:058ace2aed1d 5286 PlungerReader plungerReader;
mjr 48:058ace2aed1d 5287
mjr 48:058ace2aed1d 5288 // ---------------------------------------------------------------------------
mjr 48:058ace2aed1d 5289 //
mjr 48:058ace2aed1d 5290 // Handle the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5291 //
mjr 48:058ace2aed1d 5292 // The ZB Launch Ball feature, if enabled, lets the mechanical plunger
mjr 48:058ace2aed1d 5293 // serve as a substitute for a physical Launch Ball button. When a table
mjr 48:058ace2aed1d 5294 // is loaded in VP, and the table has the ZB Launch Ball LedWiz port
mjr 48:058ace2aed1d 5295 // turned on, we'll disable mechanical plunger reports through the
mjr 48:058ace2aed1d 5296 // joystick interface and instead use the plunger only to simulate the
mjr 48:058ace2aed1d 5297 // Launch Ball button. When the mode is active, pulling back and
mjr 48:058ace2aed1d 5298 // releasing the plunger causes a brief simulated press of the Launch
mjr 48:058ace2aed1d 5299 // button, and pushing the plunger forward of the rest position presses
mjr 48:058ace2aed1d 5300 // the Launch button as long as the plunger is pressed forward.
mjr 48:058ace2aed1d 5301 //
mjr 48:058ace2aed1d 5302 // This feature has two configuration components:
mjr 48:058ace2aed1d 5303 //
mjr 48:058ace2aed1d 5304 // - An LedWiz port number. This port is a "virtual" port that doesn't
mjr 48:058ace2aed1d 5305 // have to be attached to any actual output. DOF uses it to signal
mjr 48:058ace2aed1d 5306 // that the current table uses a Launch button instead of a plunger.
mjr 48:058ace2aed1d 5307 // DOF simply turns the port on when such a table is loaded and turns
mjr 48:058ace2aed1d 5308 // it off at all other times. We use it to enable and disable the
mjr 48:058ace2aed1d 5309 // plunger/launch button connection.
mjr 48:058ace2aed1d 5310 //
mjr 48:058ace2aed1d 5311 // - A joystick button ID. We simulate pressing this button when the
mjr 48:058ace2aed1d 5312 // launch feature is activated via the LedWiz port and the plunger is
mjr 48:058ace2aed1d 5313 // either pulled back and releasd, or pushed forward past the rest
mjr 48:058ace2aed1d 5314 // position.
mjr 48:058ace2aed1d 5315 //
mjr 48:058ace2aed1d 5316 class ZBLaunchBall
mjr 48:058ace2aed1d 5317 {
mjr 48:058ace2aed1d 5318 public:
mjr 48:058ace2aed1d 5319 ZBLaunchBall()
mjr 48:058ace2aed1d 5320 {
mjr 48:058ace2aed1d 5321 // start in the default state
mjr 48:058ace2aed1d 5322 lbState = 0;
mjr 53:9b2611964afc 5323 btnState = false;
mjr 48:058ace2aed1d 5324 }
mjr 48:058ace2aed1d 5325
mjr 48:058ace2aed1d 5326 // Update state. This checks the current plunger position and
mjr 48:058ace2aed1d 5327 // the timers to see if the plunger is in a position that simulates
mjr 48:058ace2aed1d 5328 // a Launch Ball button press via the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5329 // Updates the simulated button vector according to the current
mjr 48:058ace2aed1d 5330 // launch ball state. The main loop calls this before each
mjr 48:058ace2aed1d 5331 // joystick update to figure the new simulated button state.
mjr 53:9b2611964afc 5332 void update()
mjr 48:058ace2aed1d 5333 {
mjr 53:9b2611964afc 5334 // If the ZB Launch Ball led wiz output is ON, check for a
mjr 53:9b2611964afc 5335 // plunger firing event
mjr 53:9b2611964afc 5336 if (zbLaunchOn)
mjr 48:058ace2aed1d 5337 {
mjr 53:9b2611964afc 5338 // note the new position
mjr 48:058ace2aed1d 5339 int znew = plungerReader.getPosition();
mjr 53:9b2611964afc 5340
mjr 53:9b2611964afc 5341 // figure the push threshold from the configuration data
mjr 51:57eb311faafa 5342 const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
mjr 53:9b2611964afc 5343
mjr 53:9b2611964afc 5344 // check the state
mjr 48:058ace2aed1d 5345 switch (lbState)
mjr 48:058ace2aed1d 5346 {
mjr 48:058ace2aed1d 5347 case 0:
mjr 53:9b2611964afc 5348 // Default state. If a launch event has been detected on
mjr 53:9b2611964afc 5349 // the plunger, activate a timed pulse and switch to state 1.
mjr 53:9b2611964afc 5350 // If the plunger is pushed forward of the threshold, push
mjr 53:9b2611964afc 5351 // the button.
mjr 53:9b2611964afc 5352 if (plungerReader.isFiring())
mjr 53:9b2611964afc 5353 {
mjr 53:9b2611964afc 5354 // firing event - start a timed Launch button pulse
mjr 53:9b2611964afc 5355 lbTimer.reset();
mjr 53:9b2611964afc 5356 lbTimer.start();
mjr 53:9b2611964afc 5357 setButton(true);
mjr 53:9b2611964afc 5358
mjr 53:9b2611964afc 5359 // switch to state 1
mjr 53:9b2611964afc 5360 lbState = 1;
mjr 53:9b2611964afc 5361 }
mjr 48:058ace2aed1d 5362 else if (znew <= pushThreshold)
mjr 53:9b2611964afc 5363 {
mjr 53:9b2611964afc 5364 // pushed forward without a firing event - hold the
mjr 53:9b2611964afc 5365 // button as long as we're pushed forward
mjr 53:9b2611964afc 5366 setButton(true);
mjr 53:9b2611964afc 5367 }
mjr 53:9b2611964afc 5368 else
mjr 53:9b2611964afc 5369 {
mjr 53:9b2611964afc 5370 // not pushed forward - turn off the Launch button
mjr 53:9b2611964afc 5371 setButton(false);
mjr 53:9b2611964afc 5372 }
mjr 48:058ace2aed1d 5373 break;
mjr 48:058ace2aed1d 5374
mjr 48:058ace2aed1d 5375 case 1:
mjr 53:9b2611964afc 5376 // State 1: Timed Launch button pulse in progress after a
mjr 53:9b2611964afc 5377 // firing event. Wait for the timer to expire.
mjr 53:9b2611964afc 5378 if (lbTimer.read_us() > 200000UL)
mjr 53:9b2611964afc 5379 {
mjr 53:9b2611964afc 5380 // timer expired - turn off the button
mjr 53:9b2611964afc 5381 setButton(false);
mjr 53:9b2611964afc 5382
mjr 53:9b2611964afc 5383 // switch to state 2
mjr 53:9b2611964afc 5384 lbState = 2;
mjr 53:9b2611964afc 5385 }
mjr 48:058ace2aed1d 5386 break;
mjr 48:058ace2aed1d 5387
mjr 48:058ace2aed1d 5388 case 2:
mjr 53:9b2611964afc 5389 // State 2: Timed Launch button pulse done. Wait for the
mjr 53:9b2611964afc 5390 // plunger launch event to end.
mjr 53:9b2611964afc 5391 if (!plungerReader.isFiring())
mjr 53:9b2611964afc 5392 {
mjr 53:9b2611964afc 5393 // firing event done - return to default state
mjr 53:9b2611964afc 5394 lbState = 0;
mjr 53:9b2611964afc 5395 }
mjr 48:058ace2aed1d 5396 break;
mjr 48:058ace2aed1d 5397 }
mjr 53:9b2611964afc 5398 }
mjr 53:9b2611964afc 5399 else
mjr 53:9b2611964afc 5400 {
mjr 53:9b2611964afc 5401 // ZB Launch Ball disabled - turn off the button if it was on
mjr 53:9b2611964afc 5402 setButton(false);
mjr 48:058ace2aed1d 5403
mjr 53:9b2611964afc 5404 // return to the default state
mjr 53:9b2611964afc 5405 lbState = 0;
mjr 48:058ace2aed1d 5406 }
mjr 48:058ace2aed1d 5407 }
mjr 53:9b2611964afc 5408
mjr 53:9b2611964afc 5409 // Set the button state
mjr 53:9b2611964afc 5410 void setButton(bool on)
mjr 53:9b2611964afc 5411 {
mjr 53:9b2611964afc 5412 if (btnState != on)
mjr 53:9b2611964afc 5413 {
mjr 53:9b2611964afc 5414 // remember the new state
mjr 53:9b2611964afc 5415 btnState = on;
mjr 53:9b2611964afc 5416
mjr 53:9b2611964afc 5417 // update the virtual button state
mjr 65:739875521aae 5418 buttonState[zblButtonIndex].virtPress(on);
mjr 53:9b2611964afc 5419 }
mjr 53:9b2611964afc 5420 }
mjr 53:9b2611964afc 5421
mjr 48:058ace2aed1d 5422 private:
mjr 48:058ace2aed1d 5423 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 48:058ace2aed1d 5424 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 48:058ace2aed1d 5425 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 48:058ace2aed1d 5426 // back and releases the plunger, or simply pushes on the plunger from
mjr 48:058ace2aed1d 5427 // the rest position. This allows the plunger to be used in lieu of a
mjr 48:058ace2aed1d 5428 // physical Launch Ball button for tables that don't have plungers.
mjr 48:058ace2aed1d 5429 //
mjr 48:058ace2aed1d 5430 // States:
mjr 48:058ace2aed1d 5431 // 0 = default
mjr 53:9b2611964afc 5432 // 1 = firing (firing event has activated a Launch button pulse)
mjr 53:9b2611964afc 5433 // 2 = firing done (Launch button pulse ended, waiting for plunger
mjr 53:9b2611964afc 5434 // firing event to end)
mjr 53:9b2611964afc 5435 uint8_t lbState;
mjr 48:058ace2aed1d 5436
mjr 53:9b2611964afc 5437 // button state
mjr 53:9b2611964afc 5438 bool btnState;
mjr 48:058ace2aed1d 5439
mjr 48:058ace2aed1d 5440 // Time since last lbState transition. Some of the states are time-
mjr 48:058ace2aed1d 5441 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 48:058ace2aed1d 5442 // we remain in this state for more than a few milliseconds, since
mjr 48:058ace2aed1d 5443 // it indicates that the plunger is being slowly returned to rest
mjr 48:058ace2aed1d 5444 // rather than released. In the "launching" state, we need to release
mjr 48:058ace2aed1d 5445 // the Launch Ball button after a moment, and we need to wait for
mjr 48:058ace2aed1d 5446 // the plunger to come to rest before returning to state 0.
mjr 48:058ace2aed1d 5447 Timer lbTimer;
mjr 48:058ace2aed1d 5448 };
mjr 48:058ace2aed1d 5449
mjr 35:e959ffba78fd 5450 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5451 //
mjr 35:e959ffba78fd 5452 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 5453 //
mjr 54:fd77a6b2f76c 5454 void reboot(USBJoystick &js, bool disconnect = true, long pause_us = 2000000L)
mjr 35:e959ffba78fd 5455 {
mjr 35:e959ffba78fd 5456 // disconnect from USB
mjr 54:fd77a6b2f76c 5457 if (disconnect)
mjr 54:fd77a6b2f76c 5458 js.disconnect();
mjr 35:e959ffba78fd 5459
mjr 35:e959ffba78fd 5460 // wait a few seconds to make sure the host notices the disconnect
mjr 54:fd77a6b2f76c 5461 wait_us(pause_us);
mjr 35:e959ffba78fd 5462
mjr 35:e959ffba78fd 5463 // reset the device
mjr 35:e959ffba78fd 5464 NVIC_SystemReset();
mjr 35:e959ffba78fd 5465 while (true) { }
mjr 35:e959ffba78fd 5466 }
mjr 35:e959ffba78fd 5467
mjr 35:e959ffba78fd 5468 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5469 //
mjr 35:e959ffba78fd 5470 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 5471 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 5472 //
mjr 35:e959ffba78fd 5473 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 5474 {
mjr 35:e959ffba78fd 5475 int tmp;
mjr 35:e959ffba78fd 5476 switch (cfg.orientation)
mjr 35:e959ffba78fd 5477 {
mjr 35:e959ffba78fd 5478 case OrientationFront:
mjr 35:e959ffba78fd 5479 tmp = x;
mjr 35:e959ffba78fd 5480 x = y;
mjr 35:e959ffba78fd 5481 y = tmp;
mjr 35:e959ffba78fd 5482 break;
mjr 35:e959ffba78fd 5483
mjr 35:e959ffba78fd 5484 case OrientationLeft:
mjr 35:e959ffba78fd 5485 x = -x;
mjr 35:e959ffba78fd 5486 break;
mjr 35:e959ffba78fd 5487
mjr 35:e959ffba78fd 5488 case OrientationRight:
mjr 35:e959ffba78fd 5489 y = -y;
mjr 35:e959ffba78fd 5490 break;
mjr 35:e959ffba78fd 5491
mjr 35:e959ffba78fd 5492 case OrientationRear:
mjr 35:e959ffba78fd 5493 tmp = -x;
mjr 35:e959ffba78fd 5494 x = -y;
mjr 35:e959ffba78fd 5495 y = tmp;
mjr 35:e959ffba78fd 5496 break;
mjr 35:e959ffba78fd 5497 }
mjr 35:e959ffba78fd 5498 }
mjr 35:e959ffba78fd 5499
mjr 35:e959ffba78fd 5500 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5501 //
mjr 35:e959ffba78fd 5502 // Calibration button state:
mjr 35:e959ffba78fd 5503 // 0 = not pushed
mjr 35:e959ffba78fd 5504 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 5505 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 5506 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 5507 int calBtnState = 0;
mjr 35:e959ffba78fd 5508
mjr 35:e959ffba78fd 5509 // calibration button debounce timer
mjr 35:e959ffba78fd 5510 Timer calBtnTimer;
mjr 35:e959ffba78fd 5511
mjr 35:e959ffba78fd 5512 // calibration button light state
mjr 35:e959ffba78fd 5513 int calBtnLit = false;
mjr 35:e959ffba78fd 5514
mjr 35:e959ffba78fd 5515
mjr 35:e959ffba78fd 5516 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5517 //
mjr 40:cc0d9814522b 5518 // Configuration variable get/set message handling
mjr 35:e959ffba78fd 5519 //
mjr 40:cc0d9814522b 5520
mjr 40:cc0d9814522b 5521 // Handle SET messages - write configuration variables from USB message data
mjr 40:cc0d9814522b 5522 #define if_msg_valid(test) if (test)
mjr 53:9b2611964afc 5523 #define v_byte(var, ofs) cfg.var = data[ofs]
mjr 53:9b2611964afc 5524 #define v_ui16(var, ofs) cfg.var = wireUI16(data+(ofs))
mjr 77:0b96f6867312 5525 #define v_ui32(var, ofs) cfg.var = wireUI32(data+(ofs))
mjr 53:9b2611964afc 5526 #define v_pin(var, ofs) cfg.var = wirePinName(data[ofs])
mjr 53:9b2611964afc 5527 #define v_byte_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 5528 #define v_ui32_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 5529 #define VAR_MODE_SET 1 // we're in SET mode
mjr 76:7f5912b6340e 5530 #define v_func configVarSet(const uint8_t *data)
mjr 40:cc0d9814522b 5531 #include "cfgVarMsgMap.h"
mjr 35:e959ffba78fd 5532
mjr 40:cc0d9814522b 5533 // redefine everything for the SET messages
mjr 40:cc0d9814522b 5534 #undef if_msg_valid
mjr 40:cc0d9814522b 5535 #undef v_byte
mjr 40:cc0d9814522b 5536 #undef v_ui16
mjr 77:0b96f6867312 5537 #undef v_ui32
mjr 40:cc0d9814522b 5538 #undef v_pin
mjr 53:9b2611964afc 5539 #undef v_byte_ro
mjr 74:822a92bc11d2 5540 #undef v_ui32_ro
mjr 74:822a92bc11d2 5541 #undef VAR_MODE_SET
mjr 40:cc0d9814522b 5542 #undef v_func
mjr 38:091e511ce8a0 5543
mjr 40:cc0d9814522b 5544 // Handle GET messages - read variable values and return in USB message daa
mjr 40:cc0d9814522b 5545 #define if_msg_valid(test)
mjr 53:9b2611964afc 5546 #define v_byte(var, ofs) data[ofs] = cfg.var
mjr 53:9b2611964afc 5547 #define v_ui16(var, ofs) ui16Wire(data+(ofs), cfg.var)
mjr 77:0b96f6867312 5548 #define v_ui32(var, ofs) ui32Wire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 5549 #define v_pin(var, ofs) pinNameWire(data+(ofs), cfg.var)
mjr 73:4e8ce0b18915 5550 #define v_byte_ro(val, ofs) data[ofs] = (val)
mjr 74:822a92bc11d2 5551 #define v_ui32_ro(val, ofs) ui32Wire(data+(ofs), val);
mjr 74:822a92bc11d2 5552 #define VAR_MODE_SET 0 // we're in GET mode
mjr 76:7f5912b6340e 5553 #define v_func configVarGet(uint8_t *data)
mjr 40:cc0d9814522b 5554 #include "cfgVarMsgMap.h"
mjr 40:cc0d9814522b 5555
mjr 35:e959ffba78fd 5556
mjr 35:e959ffba78fd 5557 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5558 //
mjr 35:e959ffba78fd 5559 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 5560 // LedWiz protocol.
mjr 33:d832bcab089e 5561 //
mjr 48:058ace2aed1d 5562 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js)
mjr 35:e959ffba78fd 5563 {
mjr 38:091e511ce8a0 5564 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 5565 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 5566 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 5567 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 5568 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 5569 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 5570 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 5571 // So our full protocol is as follows:
mjr 38:091e511ce8a0 5572 //
mjr 38:091e511ce8a0 5573 // first byte =
mjr 74:822a92bc11d2 5574 // 0-48 -> PBA
mjr 74:822a92bc11d2 5575 // 64 -> SBA
mjr 38:091e511ce8a0 5576 // 65 -> private control message; second byte specifies subtype
mjr 74:822a92bc11d2 5577 // 129-132 -> PBA
mjr 38:091e511ce8a0 5578 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 5579 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 5580 // other -> reserved for future use
mjr 38:091e511ce8a0 5581 //
mjr 39:b3815a1c3802 5582 uint8_t *data = lwm.data;
mjr 74:822a92bc11d2 5583 if (data[0] == 64)
mjr 35:e959ffba78fd 5584 {
mjr 74:822a92bc11d2 5585 // 64 = SBA (original LedWiz command to set on/off switches for ports 1-32)
mjr 74:822a92bc11d2 5586 //printf("SBA %02x %02x %02x %02x, speed %02x\r\n",
mjr 38:091e511ce8a0 5587 // data[1], data[2], data[3], data[4], data[5]);
mjr 74:822a92bc11d2 5588 sba_sbx(0, data);
mjr 74:822a92bc11d2 5589
mjr 74:822a92bc11d2 5590 // SBA resets the PBA port group counter
mjr 38:091e511ce8a0 5591 pbaIdx = 0;
mjr 38:091e511ce8a0 5592 }
mjr 38:091e511ce8a0 5593 else if (data[0] == 65)
mjr 38:091e511ce8a0 5594 {
mjr 38:091e511ce8a0 5595 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 5596 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 5597 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 5598 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 5599 // message type.
mjr 39:b3815a1c3802 5600 switch (data[1])
mjr 38:091e511ce8a0 5601 {
mjr 39:b3815a1c3802 5602 case 0:
mjr 39:b3815a1c3802 5603 // No Op
mjr 39:b3815a1c3802 5604 break;
mjr 39:b3815a1c3802 5605
mjr 39:b3815a1c3802 5606 case 1:
mjr 38:091e511ce8a0 5607 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 5608 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 5609 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 5610 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 5611 {
mjr 39:b3815a1c3802 5612
mjr 39:b3815a1c3802 5613 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 5614 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 5615 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 5616
mjr 39:b3815a1c3802 5617 // we'll need a reset if the LedWiz unit number is changing
mjr 39:b3815a1c3802 5618 bool needReset = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 5619
mjr 39:b3815a1c3802 5620 // set the configuration parameters from the message
mjr 39:b3815a1c3802 5621 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 5622 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 5623
mjr 77:0b96f6867312 5624 // set the flag to do the save
mjr 77:0b96f6867312 5625 saveConfigPending = needReset ? SAVE_CONFIG_AND_REBOOT : SAVE_CONFIG_ONLY;
mjr 77:0b96f6867312 5626 saveConfigRebootTime = 0;
mjr 39:b3815a1c3802 5627 }
mjr 39:b3815a1c3802 5628 break;
mjr 38:091e511ce8a0 5629
mjr 39:b3815a1c3802 5630 case 2:
mjr 38:091e511ce8a0 5631 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 5632 // (No parameters)
mjr 38:091e511ce8a0 5633
mjr 38:091e511ce8a0 5634 // enter calibration mode
mjr 38:091e511ce8a0 5635 calBtnState = 3;
mjr 52:8298b2a73eb2 5636 plungerReader.setCalMode(true);
mjr 38:091e511ce8a0 5637 calBtnTimer.reset();
mjr 39:b3815a1c3802 5638 break;
mjr 39:b3815a1c3802 5639
mjr 39:b3815a1c3802 5640 case 3:
mjr 52:8298b2a73eb2 5641 // 3 = plunger sensor status report
mjr 48:058ace2aed1d 5642 // data[2] = flag bits
mjr 53:9b2611964afc 5643 // data[3] = extra exposure time, 100us (.1ms) increments
mjr 52:8298b2a73eb2 5644 reportPlungerStat = true;
mjr 53:9b2611964afc 5645 reportPlungerStatFlags = data[2];
mjr 53:9b2611964afc 5646 reportPlungerStatTime = data[3];
mjr 38:091e511ce8a0 5647
mjr 38:091e511ce8a0 5648 // show purple until we finish sending the report
mjr 38:091e511ce8a0 5649 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 5650 break;
mjr 39:b3815a1c3802 5651
mjr 39:b3815a1c3802 5652 case 4:
mjr 38:091e511ce8a0 5653 // 4 = hardware configuration query
mjr 38:091e511ce8a0 5654 // (No parameters)
mjr 38:091e511ce8a0 5655 js.reportConfig(
mjr 38:091e511ce8a0 5656 numOutputs,
mjr 38:091e511ce8a0 5657 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 52:8298b2a73eb2 5658 cfg.plunger.cal.zero, cfg.plunger.cal.max, cfg.plunger.cal.tRelease,
mjr 75:677892300e7a 5659 nvm.valid(), // a config is loaded if the config memory block is valid
mjr 75:677892300e7a 5660 true, // we support sbx/pbx extensions
mjr 75:677892300e7a 5661 xmalloc_rem); // remaining memory size
mjr 39:b3815a1c3802 5662 break;
mjr 39:b3815a1c3802 5663
mjr 39:b3815a1c3802 5664 case 5:
mjr 38:091e511ce8a0 5665 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 5666 allOutputsOff();
mjr 39:b3815a1c3802 5667 break;
mjr 39:b3815a1c3802 5668
mjr 39:b3815a1c3802 5669 case 6:
mjr 77:0b96f6867312 5670 // 6 = Save configuration to flash. Reboot after the delay
mjr 77:0b96f6867312 5671 // time in seconds given in data[2].
mjr 77:0b96f6867312 5672 saveConfigPending = SAVE_CONFIG_AND_REBOOT;
mjr 77:0b96f6867312 5673 saveConfigRebootTime = data[2];
mjr 39:b3815a1c3802 5674 break;
mjr 40:cc0d9814522b 5675
mjr 40:cc0d9814522b 5676 case 7:
mjr 40:cc0d9814522b 5677 // 7 = Device ID report
mjr 53:9b2611964afc 5678 // data[2] = ID index: 1=CPU ID, 2=OpenSDA TUID
mjr 53:9b2611964afc 5679 js.reportID(data[2]);
mjr 40:cc0d9814522b 5680 break;
mjr 40:cc0d9814522b 5681
mjr 40:cc0d9814522b 5682 case 8:
mjr 40:cc0d9814522b 5683 // 8 = Engage/disengage night mode.
mjr 40:cc0d9814522b 5684 // data[2] = 1 to engage, 0 to disengage
mjr 40:cc0d9814522b 5685 setNightMode(data[2]);
mjr 40:cc0d9814522b 5686 break;
mjr 52:8298b2a73eb2 5687
mjr 52:8298b2a73eb2 5688 case 9:
mjr 52:8298b2a73eb2 5689 // 9 = Config variable query.
mjr 52:8298b2a73eb2 5690 // data[2] = config var ID
mjr 52:8298b2a73eb2 5691 // data[3] = array index (for array vars: button assignments, output ports)
mjr 52:8298b2a73eb2 5692 {
mjr 53:9b2611964afc 5693 // set up the reply buffer with the variable ID data, and zero out
mjr 53:9b2611964afc 5694 // the rest of the buffer
mjr 52:8298b2a73eb2 5695 uint8_t reply[8];
mjr 52:8298b2a73eb2 5696 reply[1] = data[2];
mjr 52:8298b2a73eb2 5697 reply[2] = data[3];
mjr 53:9b2611964afc 5698 memset(reply+3, 0, sizeof(reply)-3);
mjr 52:8298b2a73eb2 5699
mjr 52:8298b2a73eb2 5700 // query the value
mjr 52:8298b2a73eb2 5701 configVarGet(reply);
mjr 52:8298b2a73eb2 5702
mjr 52:8298b2a73eb2 5703 // send the reply
mjr 52:8298b2a73eb2 5704 js.reportConfigVar(reply + 1);
mjr 52:8298b2a73eb2 5705 }
mjr 52:8298b2a73eb2 5706 break;
mjr 53:9b2611964afc 5707
mjr 53:9b2611964afc 5708 case 10:
mjr 53:9b2611964afc 5709 // 10 = Build ID query.
mjr 53:9b2611964afc 5710 js.reportBuildInfo(getBuildID());
mjr 53:9b2611964afc 5711 break;
mjr 73:4e8ce0b18915 5712
mjr 73:4e8ce0b18915 5713 case 11:
mjr 73:4e8ce0b18915 5714 // 11 = TV ON relay control.
mjr 73:4e8ce0b18915 5715 // data[2] = operation:
mjr 73:4e8ce0b18915 5716 // 0 = turn relay off
mjr 73:4e8ce0b18915 5717 // 1 = turn relay on
mjr 73:4e8ce0b18915 5718 // 2 = pulse relay (as though the power-on timer fired)
mjr 73:4e8ce0b18915 5719 TVRelay(data[2]);
mjr 73:4e8ce0b18915 5720 break;
mjr 73:4e8ce0b18915 5721
mjr 73:4e8ce0b18915 5722 case 12:
mjr 77:0b96f6867312 5723 // 12 = Learn IR code. This enters IR learning mode. While
mjr 77:0b96f6867312 5724 // in learning mode, we report raw IR signals and the first IR
mjr 77:0b96f6867312 5725 // command decoded through the special IR report format. IR
mjr 77:0b96f6867312 5726 // learning mode automatically ends after a timeout expires if
mjr 77:0b96f6867312 5727 // no command can be decoded within the time limit.
mjr 77:0b96f6867312 5728
mjr 77:0b96f6867312 5729 // enter IR learning mode
mjr 77:0b96f6867312 5730 IRLearningMode = 1;
mjr 77:0b96f6867312 5731
mjr 77:0b96f6867312 5732 // cancel any regular IR input in progress
mjr 77:0b96f6867312 5733 IRCommandIn = 0;
mjr 77:0b96f6867312 5734
mjr 77:0b96f6867312 5735 // reset and start the learning mode timeout timer
mjr 77:0b96f6867312 5736 IRTimer.reset();
mjr 73:4e8ce0b18915 5737 break;
mjr 73:4e8ce0b18915 5738
mjr 73:4e8ce0b18915 5739 case 13:
mjr 73:4e8ce0b18915 5740 // 13 = Send button status report
mjr 73:4e8ce0b18915 5741 reportButtonStatus(js);
mjr 73:4e8ce0b18915 5742 break;
mjr 38:091e511ce8a0 5743 }
mjr 38:091e511ce8a0 5744 }
mjr 38:091e511ce8a0 5745 else if (data[0] == 66)
mjr 38:091e511ce8a0 5746 {
mjr 38:091e511ce8a0 5747 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 5748 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 5749 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 5750 // in a variable-dependent format.
mjr 40:cc0d9814522b 5751 configVarSet(data);
mjr 38:091e511ce8a0 5752 }
mjr 74:822a92bc11d2 5753 else if (data[0] == 67)
mjr 74:822a92bc11d2 5754 {
mjr 74:822a92bc11d2 5755 // SBX - extended SBA message. This is the same as SBA, except
mjr 74:822a92bc11d2 5756 // that the 7th byte selects a group of 32 ports, to allow access
mjr 74:822a92bc11d2 5757 // to ports beyond the first 32.
mjr 74:822a92bc11d2 5758 sba_sbx(data[6], data);
mjr 74:822a92bc11d2 5759 }
mjr 74:822a92bc11d2 5760 else if (data[0] == 68)
mjr 74:822a92bc11d2 5761 {
mjr 74:822a92bc11d2 5762 // PBX - extended PBA message. This is similar to PBA, but
mjr 74:822a92bc11d2 5763 // allows access to more than the first 32 ports by encoding
mjr 74:822a92bc11d2 5764 // a port group byte that selects a block of 8 ports.
mjr 74:822a92bc11d2 5765
mjr 74:822a92bc11d2 5766 // get the port group - the first port is 8*group
mjr 74:822a92bc11d2 5767 int portGroup = data[1];
mjr 74:822a92bc11d2 5768
mjr 74:822a92bc11d2 5769 // unpack the brightness values
mjr 74:822a92bc11d2 5770 uint32_t tmp1 = data[2] | (data[3]<<8) | (data[4]<<16);
mjr 74:822a92bc11d2 5771 uint32_t tmp2 = data[5] | (data[6]<<8) | (data[7]<<16);
mjr 74:822a92bc11d2 5772 uint8_t bri[8] = {
mjr 74:822a92bc11d2 5773 tmp1 & 0x3F, (tmp1>>6) & 0x3F, (tmp1>>12) & 0x3F, (tmp1>>18) & 0x3F,
mjr 74:822a92bc11d2 5774 tmp2 & 0x3F, (tmp2>>6) & 0x3F, (tmp2>>12) & 0x3F, (tmp2>>18) & 0x3F
mjr 74:822a92bc11d2 5775 };
mjr 74:822a92bc11d2 5776
mjr 74:822a92bc11d2 5777 // map the flash levels: 60->129, 61->130, 62->131, 63->132
mjr 74:822a92bc11d2 5778 for (int i = 0 ; i < 8 ; ++i)
mjr 74:822a92bc11d2 5779 {
mjr 74:822a92bc11d2 5780 if (bri[i] >= 60)
mjr 74:822a92bc11d2 5781 bri[i] += 129-60;
mjr 74:822a92bc11d2 5782 }
mjr 74:822a92bc11d2 5783
mjr 74:822a92bc11d2 5784 // Carry out the PBA
mjr 74:822a92bc11d2 5785 pba_pbx(portGroup*8, bri);
mjr 74:822a92bc11d2 5786 }
mjr 38:091e511ce8a0 5787 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 5788 {
mjr 38:091e511ce8a0 5789 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 5790 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 5791 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 5792 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 5793 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 5794 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 5795 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 5796 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 5797 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 5798 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 5799 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 5800 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 5801 //
mjr 38:091e511ce8a0 5802 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 5803 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 5804 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 5805 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 5806 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 5807 // address those ports anyway.
mjr 63:5cd1a5f3a41b 5808
mjr 63:5cd1a5f3a41b 5809 // figure the block of 7 ports covered in the message
mjr 38:091e511ce8a0 5810 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 5811 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 63:5cd1a5f3a41b 5812
mjr 63:5cd1a5f3a41b 5813 // update each port
mjr 38:091e511ce8a0 5814 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 5815 {
mjr 38:091e511ce8a0 5816 // set the brightness level for the output
mjr 40:cc0d9814522b 5817 uint8_t b = data[i-i0+1];
mjr 38:091e511ce8a0 5818 outLevel[i] = b;
mjr 38:091e511ce8a0 5819
mjr 74:822a92bc11d2 5820 // set the port's LedWiz state to the nearest equivalent, so
mjr 74:822a92bc11d2 5821 // that it maintains its current setting if we switch back to
mjr 74:822a92bc11d2 5822 // LedWiz mode on a future update
mjr 76:7f5912b6340e 5823 if (b != 0)
mjr 76:7f5912b6340e 5824 {
mjr 76:7f5912b6340e 5825 // Non-zero brightness - set the SBA switch on, and set the
mjr 76:7f5912b6340e 5826 // PBA brightness to the DOF brightness rescaled to the 1..48
mjr 76:7f5912b6340e 5827 // LedWiz range. If the port is subsequently addressed by an
mjr 76:7f5912b6340e 5828 // LedWiz command, this will carry the current DOF setting
mjr 76:7f5912b6340e 5829 // forward unchanged.
mjr 76:7f5912b6340e 5830 wizOn[i] = 1;
mjr 76:7f5912b6340e 5831 wizVal[i] = dof_to_lw[b];
mjr 76:7f5912b6340e 5832 }
mjr 76:7f5912b6340e 5833 else
mjr 76:7f5912b6340e 5834 {
mjr 76:7f5912b6340e 5835 // Zero brightness. Set the SBA switch off, and leave the
mjr 76:7f5912b6340e 5836 // PBA brightness the same as it was.
mjr 76:7f5912b6340e 5837 wizOn[i] = 0;
mjr 76:7f5912b6340e 5838 }
mjr 74:822a92bc11d2 5839
mjr 38:091e511ce8a0 5840 // set the output
mjr 40:cc0d9814522b 5841 lwPin[i]->set(b);
mjr 38:091e511ce8a0 5842 }
mjr 38:091e511ce8a0 5843
mjr 38:091e511ce8a0 5844 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 5845 if (hc595 != 0)
mjr 38:091e511ce8a0 5846 hc595->update();
mjr 38:091e511ce8a0 5847 }
mjr 38:091e511ce8a0 5848 else
mjr 38:091e511ce8a0 5849 {
mjr 74:822a92bc11d2 5850 // Everything else is an LedWiz PBA message. This is a full
mjr 74:822a92bc11d2 5851 // "profile" dump from the host for one bank of 8 outputs. Each
mjr 74:822a92bc11d2 5852 // byte sets one output in the current bank. The current bank
mjr 74:822a92bc11d2 5853 // is implied; the bank starts at 0 and is reset to 0 by any SBA
mjr 74:822a92bc11d2 5854 // message, and is incremented to the next bank by each PBA. Our
mjr 74:822a92bc11d2 5855 // variable pbaIdx keeps track of the current bank. There's no
mjr 74:822a92bc11d2 5856 // direct way for the host to select the bank; it just has to count
mjr 74:822a92bc11d2 5857 // on us staying in sync. In practice, clients always send the
mjr 74:822a92bc11d2 5858 // full set of 4 PBA messages in a row to set all 32 outputs.
mjr 38:091e511ce8a0 5859 //
mjr 38:091e511ce8a0 5860 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 5861 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 5862 // wizVal[] entry for each output, and that takes precedence
mjr 63:5cd1a5f3a41b 5863 // over the extended protocol settings when we're in LedWiz
mjr 63:5cd1a5f3a41b 5864 // protocol mode.
mjr 38:091e511ce8a0 5865 //
mjr 38:091e511ce8a0 5866 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 5867 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 5868
mjr 74:822a92bc11d2 5869 // carry out the PBA
mjr 74:822a92bc11d2 5870 pba_pbx(pbaIdx, data);
mjr 74:822a92bc11d2 5871
mjr 74:822a92bc11d2 5872 // update the PBX index state for the next message
mjr 74:822a92bc11d2 5873 pbaIdx = (pbaIdx + 8) % 32;
mjr 38:091e511ce8a0 5874 }
mjr 38:091e511ce8a0 5875 }
mjr 35:e959ffba78fd 5876
mjr 38:091e511ce8a0 5877 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 5878 //
mjr 5:a70c0bce770d 5879 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 5880 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 5881 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 5882 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 5883 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 5884 // port outputs.
mjr 5:a70c0bce770d 5885 //
mjr 0:5acbbe3f4cf4 5886 int main(void)
mjr 0:5acbbe3f4cf4 5887 {
mjr 60:f38da020aa13 5888 // say hello to the debug console, in case it's connected
mjr 39:b3815a1c3802 5889 printf("\r\nPinscape Controller starting\r\n");
mjr 77:0b96f6867312 5890
mjr 77:0b96f6867312 5891
mjr 60:f38da020aa13 5892 // debugging: print memory config info
mjr 59:94eb9265b6d7 5893 // -> no longer very useful, since we use our own custom malloc/new allocator (see xmalloc() above)
mjr 60:f38da020aa13 5894 // {int *a = new int; printf("Stack=%lx, heap=%lx, free=%ld\r\n", (long)&a, (long)a, (long)&a - (long)a);}
mjr 1:d913e0afb2ac 5895
mjr 76:7f5912b6340e 5896 // clear the I2C connection
mjr 35:e959ffba78fd 5897 clear_i2c();
mjr 38:091e511ce8a0 5898
mjr 76:7f5912b6340e 5899 // Load the saved configuration. There are two sources of the
mjr 76:7f5912b6340e 5900 // configuration data:
mjr 76:7f5912b6340e 5901 //
mjr 76:7f5912b6340e 5902 // - Look for an NVM (flash non-volatile memory) configuration.
mjr 76:7f5912b6340e 5903 // If this is valid, we'll load it. The NVM is config data that can
mjr 76:7f5912b6340e 5904 // be updated dynamically by the host via USB commands and then stored
mjr 76:7f5912b6340e 5905 // in the flash by the firmware itself. If this exists, it supersedes
mjr 76:7f5912b6340e 5906 // any of the other settings stores. The Windows config tool uses this
mjr 76:7f5912b6340e 5907 // to store user settings updates.
mjr 76:7f5912b6340e 5908 //
mjr 76:7f5912b6340e 5909 // - If there's no NVM, we'll load the factory defaults, then we'll
mjr 76:7f5912b6340e 5910 // load any settings stored in the host-loaded configuration. The
mjr 76:7f5912b6340e 5911 // host can patch a set of configuration variable settings into the
mjr 76:7f5912b6340e 5912 // .bin file when loading new firmware, in the host-loaded config
mjr 76:7f5912b6340e 5913 // area that we reserve for this purpose. This allows the host to
mjr 76:7f5912b6340e 5914 // restore a configuration at the same time it installs firmware,
mjr 76:7f5912b6340e 5915 // without a separate download of the config data.
mjr 76:7f5912b6340e 5916 //
mjr 76:7f5912b6340e 5917 // The NVM supersedes the host-loaded config, since it can be updated
mjr 76:7f5912b6340e 5918 // between firmware updated and is thus presumably more recent if it's
mjr 76:7f5912b6340e 5919 // present. (Note that the NVM and host-loaded config are both in
mjr 76:7f5912b6340e 5920 // flash, so in principle we could just have a single NVM store that
mjr 76:7f5912b6340e 5921 // the host patches. The only reason we don't is that the NVM store
mjr 76:7f5912b6340e 5922 // is an image of our in-memory config structure, which is a native C
mjr 76:7f5912b6340e 5923 // struct, and we don't want the host to have to know the details of
mjr 76:7f5912b6340e 5924 // its byte layout, for obvious reasons. The host-loaded config, in
mjr 76:7f5912b6340e 5925 // contrast, uses the wire protocol format, which has a well-defined
mjr 76:7f5912b6340e 5926 // byte layout that's independent of the firmware version or the
mjr 76:7f5912b6340e 5927 // details of how the C compiler arranges the struct memory.)
mjr 76:7f5912b6340e 5928 if (!loadConfigFromFlash())
mjr 76:7f5912b6340e 5929 loadHostLoadedConfig();
mjr 35:e959ffba78fd 5930
mjr 38:091e511ce8a0 5931 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 5932 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 5933
mjr 33:d832bcab089e 5934 // we're not connected/awake yet
mjr 33:d832bcab089e 5935 bool connected = false;
mjr 40:cc0d9814522b 5936 Timer connectChangeTimer;
mjr 33:d832bcab089e 5937
mjr 35:e959ffba78fd 5938 // create the plunger sensor interface
mjr 35:e959ffba78fd 5939 createPlunger();
mjr 76:7f5912b6340e 5940
mjr 76:7f5912b6340e 5941 // update the plunger reader's cached calibration data
mjr 76:7f5912b6340e 5942 plungerReader.onUpdateCal();
mjr 33:d832bcab089e 5943
mjr 60:f38da020aa13 5944 // set up the TLC5940 interface, if these chips are present
mjr 35:e959ffba78fd 5945 init_tlc5940(cfg);
mjr 34:6b981a2afab7 5946
mjr 60:f38da020aa13 5947 // set up 74HC595 interface, if these chips are present
mjr 35:e959ffba78fd 5948 init_hc595(cfg);
mjr 6:cc35eb643e8f 5949
mjr 54:fd77a6b2f76c 5950 // Initialize the LedWiz ports. Note that the ordering here is important:
mjr 54:fd77a6b2f76c 5951 // this has to come after we create the TLC5940 and 74HC595 object instances
mjr 54:fd77a6b2f76c 5952 // (which we just did above), since we need to access those objects to set
mjr 54:fd77a6b2f76c 5953 // up ports assigned to the respective chips.
mjr 35:e959ffba78fd 5954 initLwOut(cfg);
mjr 48:058ace2aed1d 5955
mjr 60:f38da020aa13 5956 // start the TLC5940 refresh cycle clock
mjr 35:e959ffba78fd 5957 if (tlc5940 != 0)
mjr 35:e959ffba78fd 5958 tlc5940->start();
mjr 77:0b96f6867312 5959
mjr 77:0b96f6867312 5960 // Assume that nothing uses keyboard keys. We'll check for keyboard
mjr 77:0b96f6867312 5961 // usage when initializing the various subsystems that can send keys
mjr 77:0b96f6867312 5962 // (buttons, IR). If we find anything that does, we'll create the
mjr 77:0b96f6867312 5963 // USB keyboard interface.
mjr 77:0b96f6867312 5964 bool kbKeys = false;
mjr 77:0b96f6867312 5965
mjr 77:0b96f6867312 5966 // set up the IR remote control emitter & receiver, if present
mjr 77:0b96f6867312 5967 init_IR(cfg, kbKeys);
mjr 77:0b96f6867312 5968
mjr 77:0b96f6867312 5969 // start the power status time, if applicable
mjr 77:0b96f6867312 5970 startPowerStatusTimer(cfg);
mjr 48:058ace2aed1d 5971
mjr 35:e959ffba78fd 5972 // initialize the button input ports
mjr 35:e959ffba78fd 5973 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 5974
mjr 60:f38da020aa13 5975 // Create the joystick USB client. Note that the USB vendor/product ID
mjr 60:f38da020aa13 5976 // information comes from the saved configuration. Also note that we have
mjr 60:f38da020aa13 5977 // to wait until after initializing the input buttons (which we just did
mjr 60:f38da020aa13 5978 // above) to set up the interface, since the button setup will determine
mjr 60:f38da020aa13 5979 // whether or not we need to present a USB keyboard interface in addition
mjr 60:f38da020aa13 5980 // to the joystick interface.
mjr 51:57eb311faafa 5981 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false,
mjr 51:57eb311faafa 5982 cfg.joystickEnabled, kbKeys);
mjr 51:57eb311faafa 5983
mjr 60:f38da020aa13 5984 // Wait for the USB connection to start up. Show a distinctive diagnostic
mjr 60:f38da020aa13 5985 // flash pattern while waiting.
mjr 70:9f58735a1732 5986 Timer connTimeoutTimer, connFlashTimer;
mjr 70:9f58735a1732 5987 connTimeoutTimer.start();
mjr 70:9f58735a1732 5988 connFlashTimer.start();
mjr 51:57eb311faafa 5989 while (!js.configured())
mjr 51:57eb311faafa 5990 {
mjr 51:57eb311faafa 5991 // show one short yellow flash at 2-second intervals
mjr 70:9f58735a1732 5992 if (connFlashTimer.read_us() > 2000000)
mjr 51:57eb311faafa 5993 {
mjr 51:57eb311faafa 5994 // short yellow flash
mjr 51:57eb311faafa 5995 diagLED(1, 1, 0);
mjr 54:fd77a6b2f76c 5996 wait_us(50000);
mjr 51:57eb311faafa 5997 diagLED(0, 0, 0);
mjr 51:57eb311faafa 5998
mjr 51:57eb311faafa 5999 // reset the flash timer
mjr 70:9f58735a1732 6000 connFlashTimer.reset();
mjr 51:57eb311faafa 6001 }
mjr 70:9f58735a1732 6002
mjr 77:0b96f6867312 6003 // If we've been disconnected for more than the reboot timeout,
mjr 77:0b96f6867312 6004 // reboot. Some PCs won't reconnect if we were left plugged in
mjr 77:0b96f6867312 6005 // during a power cycle on the PC, but fortunately a reboot on
mjr 77:0b96f6867312 6006 // the KL25Z will make the host notice us and trigger a reconnect.
mjr 70:9f58735a1732 6007 if (cfg.disconnectRebootTimeout != 0
mjr 70:9f58735a1732 6008 && connTimeoutTimer.read() > cfg.disconnectRebootTimeout)
mjr 70:9f58735a1732 6009 reboot(js, false, 0);
mjr 77:0b96f6867312 6010
mjr 77:0b96f6867312 6011 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6012 powerStatusUpdate(cfg);
mjr 51:57eb311faafa 6013 }
mjr 60:f38da020aa13 6014
mjr 60:f38da020aa13 6015 // we're now connected to the host
mjr 54:fd77a6b2f76c 6016 connected = true;
mjr 40:cc0d9814522b 6017
mjr 60:f38da020aa13 6018 // Last report timer for the joytick interface. We use this timer to
mjr 60:f38da020aa13 6019 // throttle the report rate to a pace that's suitable for VP. Without
mjr 60:f38da020aa13 6020 // any artificial delays, we could generate data to send on the joystick
mjr 60:f38da020aa13 6021 // interface on every loop iteration. The loop iteration time depends
mjr 60:f38da020aa13 6022 // on which devices are attached, since most of the work in our main
mjr 60:f38da020aa13 6023 // loop is simply polling our devices. For typical setups, the loop
mjr 60:f38da020aa13 6024 // time ranges from about 0.25ms to 2.5ms; the biggest factor is the
mjr 60:f38da020aa13 6025 // plunger sensor. But VP polls for input about every 10ms, so there's
mjr 60:f38da020aa13 6026 // no benefit in sending data faster than that, and there's some harm,
mjr 60:f38da020aa13 6027 // in that it creates USB overhead (both on the wire and on the host
mjr 60:f38da020aa13 6028 // CPU). We therefore use this timer to pace our reports to roughly
mjr 60:f38da020aa13 6029 // the VP input polling rate. Note that there's no way to actually
mjr 60:f38da020aa13 6030 // synchronize with VP's polling, but there's also no need to, as the
mjr 60:f38da020aa13 6031 // input model is designed to reflect the overall current state at any
mjr 60:f38da020aa13 6032 // given time rather than events or deltas. If VP polls twice between
mjr 60:f38da020aa13 6033 // two updates, it simply sees no state change; if we send two updates
mjr 60:f38da020aa13 6034 // between VP polls, VP simply sees the latest state when it does get
mjr 60:f38da020aa13 6035 // around to polling.
mjr 38:091e511ce8a0 6036 Timer jsReportTimer;
mjr 38:091e511ce8a0 6037 jsReportTimer.start();
mjr 38:091e511ce8a0 6038
mjr 60:f38da020aa13 6039 // Time since we successfully sent a USB report. This is a hacky
mjr 60:f38da020aa13 6040 // workaround to deal with any remaining sporadic problems in the USB
mjr 60:f38da020aa13 6041 // stack. I've been trying to bulletproof the USB code over time to
mjr 60:f38da020aa13 6042 // remove all such problems at their source, but it seems unlikely that
mjr 60:f38da020aa13 6043 // we'll ever get them all. Thus this hack. The idea here is that if
mjr 60:f38da020aa13 6044 // we go too long without successfully sending a USB report, we'll
mjr 60:f38da020aa13 6045 // assume that the connection is broken (and the KL25Z USB hardware
mjr 60:f38da020aa13 6046 // hasn't noticed this), and we'll try taking measures to recover.
mjr 38:091e511ce8a0 6047 Timer jsOKTimer;
mjr 38:091e511ce8a0 6048 jsOKTimer.start();
mjr 35:e959ffba78fd 6049
mjr 55:4db125cd11a0 6050 // Initialize the calibration button and lamp, if enabled. To be enabled,
mjr 55:4db125cd11a0 6051 // the pin has to be assigned to something other than NC (0xFF), AND the
mjr 55:4db125cd11a0 6052 // corresponding feature enable flag has to be set.
mjr 55:4db125cd11a0 6053 DigitalIn *calBtn = 0;
mjr 55:4db125cd11a0 6054 DigitalOut *calBtnLed = 0;
mjr 55:4db125cd11a0 6055
mjr 55:4db125cd11a0 6056 // calibration button input - feature flag 0x01
mjr 55:4db125cd11a0 6057 if ((cfg.plunger.cal.features & 0x01) && cfg.plunger.cal.btn != 0xFF)
mjr 55:4db125cd11a0 6058 calBtn = new DigitalIn(wirePinName(cfg.plunger.cal.btn));
mjr 55:4db125cd11a0 6059
mjr 55:4db125cd11a0 6060 // calibration button indicator lamp output - feature flag 0x02
mjr 55:4db125cd11a0 6061 if ((cfg.plunger.cal.features & 0x02) && cfg.plunger.cal.led != 0xFF)
mjr 55:4db125cd11a0 6062 calBtnLed = new DigitalOut(wirePinName(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 6063
mjr 35:e959ffba78fd 6064 // initialize the calibration button
mjr 1:d913e0afb2ac 6065 calBtnTimer.start();
mjr 35:e959ffba78fd 6066 calBtnState = 0;
mjr 1:d913e0afb2ac 6067
mjr 1:d913e0afb2ac 6068 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 6069 Timer hbTimer;
mjr 1:d913e0afb2ac 6070 hbTimer.start();
mjr 1:d913e0afb2ac 6071 int hb = 0;
mjr 5:a70c0bce770d 6072 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 6073
mjr 1:d913e0afb2ac 6074 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 6075 Timer acTimer;
mjr 1:d913e0afb2ac 6076 acTimer.start();
mjr 1:d913e0afb2ac 6077
mjr 0:5acbbe3f4cf4 6078 // create the accelerometer object
mjr 77:0b96f6867312 6079 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS,
mjr 77:0b96f6867312 6080 MMA8451_INT_PIN, cfg.accelRange);
mjr 76:7f5912b6340e 6081
mjr 17:ab3cec0c8bf4 6082 // last accelerometer report, in joystick units (we report the nudge
mjr 17:ab3cec0c8bf4 6083 // acceleration via the joystick x & y axes, per the VP convention)
mjr 17:ab3cec0c8bf4 6084 int x = 0, y = 0;
mjr 17:ab3cec0c8bf4 6085
mjr 48:058ace2aed1d 6086 // initialize the plunger sensor
mjr 35:e959ffba78fd 6087 plungerSensor->init();
mjr 10:976666ffa4ef 6088
mjr 48:058ace2aed1d 6089 // set up the ZB Launch Ball monitor
mjr 48:058ace2aed1d 6090 ZBLaunchBall zbLaunchBall;
mjr 48:058ace2aed1d 6091
mjr 54:fd77a6b2f76c 6092 // enable the peripheral chips
mjr 54:fd77a6b2f76c 6093 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 6094 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 6095 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6096 hc595->enable(true);
mjr 74:822a92bc11d2 6097
mjr 76:7f5912b6340e 6098 // start the LedWiz flash cycle timer
mjr 74:822a92bc11d2 6099 wizCycleTimer.start();
mjr 74:822a92bc11d2 6100
mjr 74:822a92bc11d2 6101 // start the PWM update polling timer
mjr 74:822a92bc11d2 6102 polledPwmTimer.start();
mjr 43:7a6364d82a41 6103
mjr 77:0b96f6867312 6104 // Timer for configuration change reboots
mjr 77:0b96f6867312 6105 ExtTimer saveConfigRebootTimer;
mjr 77:0b96f6867312 6106
mjr 1:d913e0afb2ac 6107 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 6108 // host requests
mjr 0:5acbbe3f4cf4 6109 for (;;)
mjr 0:5acbbe3f4cf4 6110 {
mjr 74:822a92bc11d2 6111 // start the main loop timer for diagnostic data collection
mjr 76:7f5912b6340e 6112 IF_DIAG(mainLoopTimer.reset(); mainLoopTimer.start();)
mjr 74:822a92bc11d2 6113
mjr 48:058ace2aed1d 6114 // Process incoming reports on the joystick interface. The joystick
mjr 48:058ace2aed1d 6115 // "out" (receive) endpoint is used for LedWiz commands and our
mjr 48:058ace2aed1d 6116 // extended protocol commands. Limit processing time to 5ms to
mjr 48:058ace2aed1d 6117 // ensure we don't starve the input side.
mjr 39:b3815a1c3802 6118 LedWizMsg lwm;
mjr 48:058ace2aed1d 6119 Timer lwt;
mjr 48:058ace2aed1d 6120 lwt.start();
mjr 77:0b96f6867312 6121 IF_DIAG(int msgCount = 0;)
mjr 48:058ace2aed1d 6122 while (js.readLedWizMsg(lwm) && lwt.read_us() < 5000)
mjr 74:822a92bc11d2 6123 {
mjr 48:058ace2aed1d 6124 handleInputMsg(lwm, js);
mjr 74:822a92bc11d2 6125 IF_DIAG(++msgCount;)
mjr 74:822a92bc11d2 6126 }
mjr 74:822a92bc11d2 6127
mjr 74:822a92bc11d2 6128 // collect performance statistics on the message reader, if desired
mjr 74:822a92bc11d2 6129 IF_DIAG(
mjr 74:822a92bc11d2 6130 if (msgCount != 0)
mjr 74:822a92bc11d2 6131 {
mjr 76:7f5912b6340e 6132 mainLoopMsgTime += lwt.read_us();
mjr 74:822a92bc11d2 6133 mainLoopMsgCount++;
mjr 74:822a92bc11d2 6134 }
mjr 74:822a92bc11d2 6135 )
mjr 74:822a92bc11d2 6136
mjr 77:0b96f6867312 6137 // process IR input
mjr 77:0b96f6867312 6138 process_IR(cfg, js);
mjr 77:0b96f6867312 6139
mjr 77:0b96f6867312 6140 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6141 powerStatusUpdate(cfg);
mjr 77:0b96f6867312 6142
mjr 74:822a92bc11d2 6143 // update flashing LedWiz outputs periodically
mjr 74:822a92bc11d2 6144 wizPulse();
mjr 74:822a92bc11d2 6145
mjr 74:822a92bc11d2 6146 // update PWM outputs
mjr 74:822a92bc11d2 6147 pollPwmUpdates();
mjr 77:0b96f6867312 6148
mjr 77:0b96f6867312 6149 // poll the accelerometer
mjr 77:0b96f6867312 6150 accel.poll();
mjr 55:4db125cd11a0 6151
mjr 76:7f5912b6340e 6152 // collect diagnostic statistics, checkpoint 0
mjr 76:7f5912b6340e 6153 IF_DIAG(mainLoopIterCheckpt[0] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6154
mjr 55:4db125cd11a0 6155 // send TLC5940 data updates if applicable
mjr 55:4db125cd11a0 6156 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6157 tlc5940->send();
mjr 1:d913e0afb2ac 6158
mjr 76:7f5912b6340e 6159 // collect diagnostic statistics, checkpoint 1
mjr 76:7f5912b6340e 6160 IF_DIAG(mainLoopIterCheckpt[1] += mainLoopTimer.read_us();)
mjr 77:0b96f6867312 6161
mjr 1:d913e0afb2ac 6162 // check for plunger calibration
mjr 17:ab3cec0c8bf4 6163 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 6164 {
mjr 1:d913e0afb2ac 6165 // check the state
mjr 1:d913e0afb2ac 6166 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6167 {
mjr 1:d913e0afb2ac 6168 case 0:
mjr 1:d913e0afb2ac 6169 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 6170 calBtnTimer.reset();
mjr 1:d913e0afb2ac 6171 calBtnState = 1;
mjr 1:d913e0afb2ac 6172 break;
mjr 1:d913e0afb2ac 6173
mjr 1:d913e0afb2ac 6174 case 1:
mjr 1:d913e0afb2ac 6175 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 6176 // passed, start the hold period
mjr 48:058ace2aed1d 6177 if (calBtnTimer.read_us() > 50000)
mjr 1:d913e0afb2ac 6178 calBtnState = 2;
mjr 1:d913e0afb2ac 6179 break;
mjr 1:d913e0afb2ac 6180
mjr 1:d913e0afb2ac 6181 case 2:
mjr 1:d913e0afb2ac 6182 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 6183 // for the entire hold period, move to calibration mode
mjr 48:058ace2aed1d 6184 if (calBtnTimer.read_us() > 2050000)
mjr 1:d913e0afb2ac 6185 {
mjr 1:d913e0afb2ac 6186 // enter calibration mode
mjr 1:d913e0afb2ac 6187 calBtnState = 3;
mjr 9:fd65b0a94720 6188 calBtnTimer.reset();
mjr 35:e959ffba78fd 6189
mjr 44:b5ac89b9cd5d 6190 // begin the plunger calibration limits
mjr 52:8298b2a73eb2 6191 plungerReader.setCalMode(true);
mjr 1:d913e0afb2ac 6192 }
mjr 1:d913e0afb2ac 6193 break;
mjr 2:c174f9ee414a 6194
mjr 2:c174f9ee414a 6195 case 3:
mjr 9:fd65b0a94720 6196 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 6197 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 6198 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 6199 break;
mjr 0:5acbbe3f4cf4 6200 }
mjr 0:5acbbe3f4cf4 6201 }
mjr 1:d913e0afb2ac 6202 else
mjr 1:d913e0afb2ac 6203 {
mjr 2:c174f9ee414a 6204 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 6205 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 6206 // and save the results to flash.
mjr 2:c174f9ee414a 6207 //
mjr 2:c174f9ee414a 6208 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 6209 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 6210 // mode, it simply cancels the attempt.
mjr 77:0b96f6867312 6211 diagLED(1,1,1);
mjr 48:058ace2aed1d 6212 if (calBtnState == 3 && calBtnTimer.read_us() > 15000000)
mjr 2:c174f9ee414a 6213 {
mjr 77:0b96f6867312 6214 diagLED(0,0,0);
mjr 2:c174f9ee414a 6215 // exit calibration mode
mjr 1:d913e0afb2ac 6216 calBtnState = 0;
mjr 52:8298b2a73eb2 6217 plungerReader.setCalMode(false);
mjr 2:c174f9ee414a 6218
mjr 6:cc35eb643e8f 6219 // save the updated configuration
mjr 35:e959ffba78fd 6220 cfg.plunger.cal.calibrated = 1;
mjr 35:e959ffba78fd 6221 saveConfigToFlash();
mjr 2:c174f9ee414a 6222 }
mjr 2:c174f9ee414a 6223 else if (calBtnState != 3)
mjr 2:c174f9ee414a 6224 {
mjr 77:0b96f6867312 6225 diagLED(0,1,1);
mjr 2:c174f9ee414a 6226 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 6227 calBtnState = 0;
mjr 2:c174f9ee414a 6228 }
mjr 1:d913e0afb2ac 6229 }
mjr 1:d913e0afb2ac 6230
mjr 1:d913e0afb2ac 6231 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 6232 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 6233 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6234 {
mjr 1:d913e0afb2ac 6235 case 2:
mjr 1:d913e0afb2ac 6236 // in the hold period - flash the light
mjr 48:058ace2aed1d 6237 newCalBtnLit = ((calBtnTimer.read_us()/250000) & 1);
mjr 1:d913e0afb2ac 6238 break;
mjr 1:d913e0afb2ac 6239
mjr 1:d913e0afb2ac 6240 case 3:
mjr 1:d913e0afb2ac 6241 // calibration mode - show steady on
mjr 1:d913e0afb2ac 6242 newCalBtnLit = true;
mjr 1:d913e0afb2ac 6243 break;
mjr 1:d913e0afb2ac 6244
mjr 1:d913e0afb2ac 6245 default:
mjr 1:d913e0afb2ac 6246 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 6247 newCalBtnLit = false;
mjr 1:d913e0afb2ac 6248 break;
mjr 1:d913e0afb2ac 6249 }
mjr 3:3514575d4f86 6250
mjr 3:3514575d4f86 6251 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 6252 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 6253 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 6254 {
mjr 1:d913e0afb2ac 6255 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 6256 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 6257 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6258 calBtnLed->write(1);
mjr 38:091e511ce8a0 6259 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 6260 }
mjr 2:c174f9ee414a 6261 else {
mjr 17:ab3cec0c8bf4 6262 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6263 calBtnLed->write(0);
mjr 38:091e511ce8a0 6264 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 6265 }
mjr 1:d913e0afb2ac 6266 }
mjr 35:e959ffba78fd 6267
mjr 76:7f5912b6340e 6268 // collect diagnostic statistics, checkpoint 2
mjr 76:7f5912b6340e 6269 IF_DIAG(mainLoopIterCheckpt[2] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6270
mjr 48:058ace2aed1d 6271 // read the plunger sensor
mjr 48:058ace2aed1d 6272 plungerReader.read();
mjr 48:058ace2aed1d 6273
mjr 76:7f5912b6340e 6274 // collect diagnostic statistics, checkpoint 3
mjr 76:7f5912b6340e 6275 IF_DIAG(mainLoopIterCheckpt[3] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6276
mjr 53:9b2611964afc 6277 // update the ZB Launch Ball status
mjr 53:9b2611964afc 6278 zbLaunchBall.update();
mjr 37:ed52738445fc 6279
mjr 76:7f5912b6340e 6280 // collect diagnostic statistics, checkpoint 4
mjr 76:7f5912b6340e 6281 IF_DIAG(mainLoopIterCheckpt[4] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6282
mjr 53:9b2611964afc 6283 // process button updates
mjr 53:9b2611964afc 6284 processButtons(cfg);
mjr 53:9b2611964afc 6285
mjr 76:7f5912b6340e 6286 // collect diagnostic statistics, checkpoint 5
mjr 76:7f5912b6340e 6287 IF_DIAG(mainLoopIterCheckpt[5] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6288
mjr 38:091e511ce8a0 6289 // send a keyboard report if we have new data
mjr 37:ed52738445fc 6290 if (kbState.changed)
mjr 37:ed52738445fc 6291 {
mjr 38:091e511ce8a0 6292 // send a keyboard report
mjr 37:ed52738445fc 6293 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 6294 kbState.changed = false;
mjr 37:ed52738445fc 6295 }
mjr 38:091e511ce8a0 6296
mjr 38:091e511ce8a0 6297 // likewise for the media controller
mjr 37:ed52738445fc 6298 if (mediaState.changed)
mjr 37:ed52738445fc 6299 {
mjr 38:091e511ce8a0 6300 // send a media report
mjr 37:ed52738445fc 6301 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 6302 mediaState.changed = false;
mjr 37:ed52738445fc 6303 }
mjr 38:091e511ce8a0 6304
mjr 76:7f5912b6340e 6305 // collect diagnostic statistics, checkpoint 6
mjr 76:7f5912b6340e 6306 IF_DIAG(mainLoopIterCheckpt[6] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6307
mjr 38:091e511ce8a0 6308 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 6309 bool jsOK = false;
mjr 55:4db125cd11a0 6310
mjr 55:4db125cd11a0 6311 // figure the current status flags for joystick reports
mjr 77:0b96f6867312 6312 uint16_t statusFlags =
mjr 77:0b96f6867312 6313 cfg.plunger.enabled // 0x01
mjr 77:0b96f6867312 6314 | nightMode // 0x02
mjr 77:0b96f6867312 6315 | ((psu2_state & 0x07) << 2); // 0x04 0x08 0x10
mjr 77:0b96f6867312 6316 if (IRLearningMode != 0)
mjr 77:0b96f6867312 6317 statusFlags |= 0x20;
mjr 17:ab3cec0c8bf4 6318
mjr 50:40015764bbe6 6319 // If it's been long enough since our last USB status report, send
mjr 50:40015764bbe6 6320 // the new report. VP only polls for input in 10ms intervals, so
mjr 50:40015764bbe6 6321 // there's no benefit in sending reports more frequently than this.
mjr 50:40015764bbe6 6322 // More frequent reporting would only add USB I/O overhead.
mjr 50:40015764bbe6 6323 if (cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 17:ab3cec0c8bf4 6324 {
mjr 17:ab3cec0c8bf4 6325 // read the accelerometer
mjr 17:ab3cec0c8bf4 6326 int xa, ya;
mjr 17:ab3cec0c8bf4 6327 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 6328
mjr 17:ab3cec0c8bf4 6329 // confine the results to our joystick axis range
mjr 17:ab3cec0c8bf4 6330 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 17:ab3cec0c8bf4 6331 if (xa > JOYMAX) xa = JOYMAX;
mjr 17:ab3cec0c8bf4 6332 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 17:ab3cec0c8bf4 6333 if (ya > JOYMAX) ya = JOYMAX;
mjr 17:ab3cec0c8bf4 6334
mjr 17:ab3cec0c8bf4 6335 // store the updated accelerometer coordinates
mjr 17:ab3cec0c8bf4 6336 x = xa;
mjr 17:ab3cec0c8bf4 6337 y = ya;
mjr 17:ab3cec0c8bf4 6338
mjr 48:058ace2aed1d 6339 // Report the current plunger position unless the plunger is
mjr 48:058ace2aed1d 6340 // disabled, or the ZB Launch Ball signal is on. In either of
mjr 48:058ace2aed1d 6341 // those cases, just report a constant 0 value. ZB Launch Ball
mjr 48:058ace2aed1d 6342 // temporarily disables mechanical plunger reporting because it
mjr 21:5048e16cc9ef 6343 // tells us that the table has a Launch Ball button instead of
mjr 48:058ace2aed1d 6344 // a traditional plunger, so we don't want to confuse VP with
mjr 48:058ace2aed1d 6345 // regular plunger inputs.
mjr 48:058ace2aed1d 6346 int z = plungerReader.getPosition();
mjr 53:9b2611964afc 6347 int zrep = (!cfg.plunger.enabled || zbLaunchOn ? 0 : z);
mjr 35:e959ffba78fd 6348
mjr 35:e959ffba78fd 6349 // rotate X and Y according to the device orientation in the cabinet
mjr 35:e959ffba78fd 6350 accelRotate(x, y);
mjr 35:e959ffba78fd 6351
mjr 35:e959ffba78fd 6352 // send the joystick report
mjr 53:9b2611964afc 6353 jsOK = js.update(x, y, zrep, jsButtons, statusFlags);
mjr 21:5048e16cc9ef 6354
mjr 17:ab3cec0c8bf4 6355 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 6356 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 6357 }
mjr 21:5048e16cc9ef 6358
mjr 52:8298b2a73eb2 6359 // If we're in sensor status mode, report all pixel exposure values
mjr 52:8298b2a73eb2 6360 if (reportPlungerStat)
mjr 10:976666ffa4ef 6361 {
mjr 17:ab3cec0c8bf4 6362 // send the report
mjr 53:9b2611964afc 6363 plungerSensor->sendStatusReport(js, reportPlungerStatFlags, reportPlungerStatTime);
mjr 17:ab3cec0c8bf4 6364
mjr 10:976666ffa4ef 6365 // we have satisfied this request
mjr 52:8298b2a73eb2 6366 reportPlungerStat = false;
mjr 10:976666ffa4ef 6367 }
mjr 10:976666ffa4ef 6368
mjr 35:e959ffba78fd 6369 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 6370 // periodically for the sake of the Windows config tool.
mjr 77:0b96f6867312 6371 if (!cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 21:5048e16cc9ef 6372 {
mjr 55:4db125cd11a0 6373 jsOK = js.updateStatus(statusFlags);
mjr 38:091e511ce8a0 6374 jsReportTimer.reset();
mjr 38:091e511ce8a0 6375 }
mjr 38:091e511ce8a0 6376
mjr 38:091e511ce8a0 6377 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 6378 if (jsOK)
mjr 38:091e511ce8a0 6379 {
mjr 38:091e511ce8a0 6380 jsOKTimer.reset();
mjr 38:091e511ce8a0 6381 jsOKTimer.start();
mjr 21:5048e16cc9ef 6382 }
mjr 21:5048e16cc9ef 6383
mjr 76:7f5912b6340e 6384 // collect diagnostic statistics, checkpoint 7
mjr 76:7f5912b6340e 6385 IF_DIAG(mainLoopIterCheckpt[7] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6386
mjr 6:cc35eb643e8f 6387 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 6388 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 6389 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 6390 #endif
mjr 6:cc35eb643e8f 6391
mjr 33:d832bcab089e 6392 // check for connection status changes
mjr 54:fd77a6b2f76c 6393 bool newConnected = js.isConnected() && !js.isSleeping();
mjr 33:d832bcab089e 6394 if (newConnected != connected)
mjr 33:d832bcab089e 6395 {
mjr 54:fd77a6b2f76c 6396 // give it a moment to stabilize
mjr 40:cc0d9814522b 6397 connectChangeTimer.start();
mjr 55:4db125cd11a0 6398 if (connectChangeTimer.read_us() > 1000000)
mjr 33:d832bcab089e 6399 {
mjr 33:d832bcab089e 6400 // note the new status
mjr 33:d832bcab089e 6401 connected = newConnected;
mjr 40:cc0d9814522b 6402
mjr 40:cc0d9814522b 6403 // done with the change timer for this round - reset it for next time
mjr 40:cc0d9814522b 6404 connectChangeTimer.stop();
mjr 40:cc0d9814522b 6405 connectChangeTimer.reset();
mjr 33:d832bcab089e 6406
mjr 54:fd77a6b2f76c 6407 // if we're newly disconnected, clean up for PC suspend mode or power off
mjr 54:fd77a6b2f76c 6408 if (!connected)
mjr 40:cc0d9814522b 6409 {
mjr 54:fd77a6b2f76c 6410 // turn off all outputs
mjr 33:d832bcab089e 6411 allOutputsOff();
mjr 40:cc0d9814522b 6412
mjr 40:cc0d9814522b 6413 // The KL25Z runs off of USB power, so we might (depending on the PC
mjr 40:cc0d9814522b 6414 // and OS configuration) continue to receive power even when the main
mjr 40:cc0d9814522b 6415 // PC power supply is turned off, such as in soft-off or suspend/sleep
mjr 40:cc0d9814522b 6416 // mode. Any external output controller chips (TLC5940, 74HC595) might
mjr 40:cc0d9814522b 6417 // be powered from the PC power supply directly rather than from our
mjr 40:cc0d9814522b 6418 // USB power, so they might be powered off even when we're still running.
mjr 40:cc0d9814522b 6419 // To ensure cleaner startup when the power comes back on, globally
mjr 40:cc0d9814522b 6420 // disable the outputs. The global disable signals come from GPIO lines
mjr 40:cc0d9814522b 6421 // that remain powered as long as the KL25Z is powered, so these modes
mjr 40:cc0d9814522b 6422 // will apply smoothly across power state transitions in the external
mjr 40:cc0d9814522b 6423 // hardware. That is, when the external chips are powered up, they'll
mjr 40:cc0d9814522b 6424 // see the global disable signals as stable voltage inputs immediately,
mjr 40:cc0d9814522b 6425 // which will cause them to suppress any output triggering. This ensures
mjr 40:cc0d9814522b 6426 // that we don't fire any solenoids or flash any lights spuriously when
mjr 40:cc0d9814522b 6427 // the power first comes on.
mjr 40:cc0d9814522b 6428 if (tlc5940 != 0)
mjr 40:cc0d9814522b 6429 tlc5940->enable(false);
mjr 40:cc0d9814522b 6430 if (hc595 != 0)
mjr 40:cc0d9814522b 6431 hc595->enable(false);
mjr 40:cc0d9814522b 6432 }
mjr 33:d832bcab089e 6433 }
mjr 33:d832bcab089e 6434 }
mjr 48:058ace2aed1d 6435
mjr 53:9b2611964afc 6436 // if we have a reboot timer pending, check for completion
mjr 77:0b96f6867312 6437 if (saveConfigRebootTimer.isRunning()
mjr 77:0b96f6867312 6438 && saveConfigRebootTimer.read() > saveConfigRebootTime)
mjr 53:9b2611964afc 6439 reboot(js);
mjr 77:0b96f6867312 6440
mjr 77:0b96f6867312 6441 // if a config save is pending, do it now
mjr 77:0b96f6867312 6442 if (saveConfigPending != 0)
mjr 77:0b96f6867312 6443 {
mjr 77:0b96f6867312 6444 // save the configuration
mjr 77:0b96f6867312 6445 saveConfigToFlash();
mjr 77:0b96f6867312 6446
mjr 77:0b96f6867312 6447 // if desired, reboot after the specified delay
mjr 77:0b96f6867312 6448 if (saveConfigPending == SAVE_CONFIG_AND_REBOOT)
mjr 77:0b96f6867312 6449 saveConfigRebootTimer.start();
mjr 77:0b96f6867312 6450
mjr 77:0b96f6867312 6451 // the save is no longer pending
mjr 77:0b96f6867312 6452 saveConfigPending = 0;
mjr 77:0b96f6867312 6453 }
mjr 53:9b2611964afc 6454
mjr 48:058ace2aed1d 6455 // if we're disconnected, initiate a new connection
mjr 51:57eb311faafa 6456 if (!connected)
mjr 48:058ace2aed1d 6457 {
mjr 54:fd77a6b2f76c 6458 // show USB HAL debug events
mjr 54:fd77a6b2f76c 6459 extern void HAL_DEBUG_PRINTEVENTS(const char *prefix);
mjr 54:fd77a6b2f76c 6460 HAL_DEBUG_PRINTEVENTS(">DISC");
mjr 54:fd77a6b2f76c 6461
mjr 54:fd77a6b2f76c 6462 // show immediate diagnostic feedback
mjr 54:fd77a6b2f76c 6463 js.diagFlash();
mjr 54:fd77a6b2f76c 6464
mjr 54:fd77a6b2f76c 6465 // clear any previous diagnostic LED display
mjr 54:fd77a6b2f76c 6466 diagLED(0, 0, 0);
mjr 51:57eb311faafa 6467
mjr 51:57eb311faafa 6468 // set up a timer to monitor the reboot timeout
mjr 70:9f58735a1732 6469 Timer reconnTimeoutTimer;
mjr 70:9f58735a1732 6470 reconnTimeoutTimer.start();
mjr 48:058ace2aed1d 6471
mjr 54:fd77a6b2f76c 6472 // set up a timer for diagnostic displays
mjr 54:fd77a6b2f76c 6473 Timer diagTimer;
mjr 54:fd77a6b2f76c 6474 diagTimer.reset();
mjr 54:fd77a6b2f76c 6475 diagTimer.start();
mjr 74:822a92bc11d2 6476
mjr 74:822a92bc11d2 6477 // turn off the main loop timer while spinning
mjr 74:822a92bc11d2 6478 IF_DIAG(mainLoopTimer.stop();)
mjr 54:fd77a6b2f76c 6479
mjr 54:fd77a6b2f76c 6480 // loop until we get our connection back
mjr 54:fd77a6b2f76c 6481 while (!js.isConnected() || js.isSleeping())
mjr 51:57eb311faafa 6482 {
mjr 54:fd77a6b2f76c 6483 // try to recover the connection
mjr 54:fd77a6b2f76c 6484 js.recoverConnection();
mjr 54:fd77a6b2f76c 6485
mjr 55:4db125cd11a0 6486 // send TLC5940 data if necessary
mjr 55:4db125cd11a0 6487 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6488 tlc5940->send();
mjr 55:4db125cd11a0 6489
mjr 54:fd77a6b2f76c 6490 // show a diagnostic flash every couple of seconds
mjr 54:fd77a6b2f76c 6491 if (diagTimer.read_us() > 2000000)
mjr 51:57eb311faafa 6492 {
mjr 54:fd77a6b2f76c 6493 // flush the USB HAL debug events, if in debug mode
mjr 54:fd77a6b2f76c 6494 HAL_DEBUG_PRINTEVENTS(">NC");
mjr 54:fd77a6b2f76c 6495
mjr 54:fd77a6b2f76c 6496 // show diagnostic feedback
mjr 54:fd77a6b2f76c 6497 js.diagFlash();
mjr 51:57eb311faafa 6498
mjr 51:57eb311faafa 6499 // reset the flash timer
mjr 54:fd77a6b2f76c 6500 diagTimer.reset();
mjr 51:57eb311faafa 6501 }
mjr 51:57eb311faafa 6502
mjr 77:0b96f6867312 6503 // If the disconnect reboot timeout has expired, reboot.
mjr 77:0b96f6867312 6504 // Some PC hosts won't reconnect to a device that's left
mjr 77:0b96f6867312 6505 // plugged in through various events on the PC side, such as
mjr 77:0b96f6867312 6506 // rebooting Windows, cycling power on the PC, or just a lost
mjr 77:0b96f6867312 6507 // USB connection. Rebooting the KL25Z seems to be the most
mjr 77:0b96f6867312 6508 // reliable way to get Windows to notice us again after one
mjr 77:0b96f6867312 6509 // of these events and make it reconnect.
mjr 51:57eb311faafa 6510 if (cfg.disconnectRebootTimeout != 0
mjr 70:9f58735a1732 6511 && reconnTimeoutTimer.read() > cfg.disconnectRebootTimeout)
mjr 54:fd77a6b2f76c 6512 reboot(js, false, 0);
mjr 77:0b96f6867312 6513
mjr 77:0b96f6867312 6514 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6515 powerStatusUpdate(cfg);
mjr 54:fd77a6b2f76c 6516 }
mjr 54:fd77a6b2f76c 6517
mjr 74:822a92bc11d2 6518 // resume the main loop timer
mjr 74:822a92bc11d2 6519 IF_DIAG(mainLoopTimer.start();)
mjr 74:822a92bc11d2 6520
mjr 54:fd77a6b2f76c 6521 // if we made it out of that loop alive, we're connected again!
mjr 54:fd77a6b2f76c 6522 connected = true;
mjr 54:fd77a6b2f76c 6523 HAL_DEBUG_PRINTEVENTS(">C");
mjr 54:fd77a6b2f76c 6524
mjr 54:fd77a6b2f76c 6525 // Enable peripheral chips and update them with current output data
mjr 54:fd77a6b2f76c 6526 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 6527 {
mjr 55:4db125cd11a0 6528 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 6529 tlc5940->update(true);
mjr 54:fd77a6b2f76c 6530 }
mjr 54:fd77a6b2f76c 6531 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6532 {
mjr 55:4db125cd11a0 6533 hc595->enable(true);
mjr 54:fd77a6b2f76c 6534 hc595->update(true);
mjr 51:57eb311faafa 6535 }
mjr 48:058ace2aed1d 6536 }
mjr 43:7a6364d82a41 6537
mjr 6:cc35eb643e8f 6538 // provide a visual status indication on the on-board LED
mjr 48:058ace2aed1d 6539 if (calBtnState < 2 && hbTimer.read_us() > 1000000)
mjr 1:d913e0afb2ac 6540 {
mjr 54:fd77a6b2f76c 6541 if (jsOKTimer.read_us() > 1000000)
mjr 38:091e511ce8a0 6542 {
mjr 39:b3815a1c3802 6543 // USB freeze - show red/yellow.
mjr 40:cc0d9814522b 6544 //
mjr 54:fd77a6b2f76c 6545 // It's been more than a second since we successfully sent a joystick
mjr 54:fd77a6b2f76c 6546 // update message. This must mean that something's wrong on the USB
mjr 54:fd77a6b2f76c 6547 // connection, even though we haven't detected an outright disconnect.
mjr 54:fd77a6b2f76c 6548 // Show a distinctive diagnostic LED pattern when this occurs.
mjr 38:091e511ce8a0 6549 hb = !hb;
mjr 38:091e511ce8a0 6550 diagLED(1, hb, 0);
mjr 54:fd77a6b2f76c 6551
mjr 54:fd77a6b2f76c 6552 // If the reboot-on-disconnect option is in effect, treat this condition
mjr 54:fd77a6b2f76c 6553 // as equivalent to a disconnect, since something is obviously wrong
mjr 54:fd77a6b2f76c 6554 // with the USB connection.
mjr 54:fd77a6b2f76c 6555 if (cfg.disconnectRebootTimeout != 0)
mjr 54:fd77a6b2f76c 6556 {
mjr 54:fd77a6b2f76c 6557 // The reboot timeout is in effect. If we've been incommunicado for
mjr 54:fd77a6b2f76c 6558 // longer than the timeout, reboot. If we haven't reached the time
mjr 54:fd77a6b2f76c 6559 // limit, keep running for now, and leave the OK timer running so
mjr 54:fd77a6b2f76c 6560 // that we can continue to monitor this.
mjr 54:fd77a6b2f76c 6561 if (jsOKTimer.read() > cfg.disconnectRebootTimeout)
mjr 54:fd77a6b2f76c 6562 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 6563 }
mjr 54:fd77a6b2f76c 6564 else
mjr 54:fd77a6b2f76c 6565 {
mjr 54:fd77a6b2f76c 6566 // There's no reboot timer, so just keep running with the diagnostic
mjr 54:fd77a6b2f76c 6567 // pattern displayed. Since we're not waiting for any other timed
mjr 54:fd77a6b2f76c 6568 // conditions in this state, stop the timer so that it doesn't
mjr 54:fd77a6b2f76c 6569 // overflow if this condition persists for a long time.
mjr 54:fd77a6b2f76c 6570 jsOKTimer.stop();
mjr 54:fd77a6b2f76c 6571 }
mjr 38:091e511ce8a0 6572 }
mjr 73:4e8ce0b18915 6573 else if (psu2_state >= 4)
mjr 73:4e8ce0b18915 6574 {
mjr 73:4e8ce0b18915 6575 // We're in the TV timer countdown. Skip the normal heartbeat
mjr 73:4e8ce0b18915 6576 // flashes and show the TV timer flashes instead.
mjr 73:4e8ce0b18915 6577 diagLED(0, 0, 0);
mjr 73:4e8ce0b18915 6578 }
mjr 35:e959ffba78fd 6579 else if (cfg.plunger.enabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 6580 {
mjr 6:cc35eb643e8f 6581 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 6582 hb = !hb;
mjr 38:091e511ce8a0 6583 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 6584 }
mjr 6:cc35eb643e8f 6585 else
mjr 6:cc35eb643e8f 6586 {
mjr 6:cc35eb643e8f 6587 // connected - flash blue/green
mjr 2:c174f9ee414a 6588 hb = !hb;
mjr 38:091e511ce8a0 6589 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 6590 }
mjr 1:d913e0afb2ac 6591
mjr 1:d913e0afb2ac 6592 // reset the heartbeat timer
mjr 1:d913e0afb2ac 6593 hbTimer.reset();
mjr 5:a70c0bce770d 6594 ++hbcnt;
mjr 1:d913e0afb2ac 6595 }
mjr 74:822a92bc11d2 6596
mjr 74:822a92bc11d2 6597 // collect statistics on the main loop time, if desired
mjr 74:822a92bc11d2 6598 IF_DIAG(
mjr 76:7f5912b6340e 6599 mainLoopIterTime += mainLoopTimer.read_us();
mjr 74:822a92bc11d2 6600 mainLoopIterCount++;
mjr 74:822a92bc11d2 6601 )
mjr 1:d913e0afb2ac 6602 }
mjr 0:5acbbe3f4cf4 6603 }