Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Fri Feb 03 20:50:02 2017 +0000
Revision:
76:7f5912b6340e
Parent:
75:677892300e7a
Child:
77:0b96f6867312
Rework flash driver to make it truly stable (hopefully to 100% reliability); host-loaded configuration; performance improvements; more performance diagnostics.

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