Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Fri May 13 21:28:41 2016 +0000
Revision:
60:f38da020aa13
Parent:
59:94eb9265b6d7
Child:
63:5cd1a5f3a41b
Try to bulletproof the Flash programming procedure by using techniques recommended in ARM forums; enhanced comments in main routine

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 38:091e511ce8a0 42 // - Plunger position sensing, with mulitple 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 6:cc35eb643e8f 166 // long yellow/green = everything's working, but the plunger hasn't
mjr 38:091e511ce8a0 167 // been calibrated. Follow the calibration procedure described in
mjr 38:091e511ce8a0 168 // the project documentation. This flash mode won't appear if there's
mjr 38:091e511ce8a0 169 // no plunger sensor configured.
mjr 6:cc35eb643e8f 170 //
mjr 38:091e511ce8a0 171 // alternating blue/green = everything's working normally, and plunger
mjr 38:091e511ce8a0 172 // calibration has been completed (or there's no plunger attached)
mjr 10:976666ffa4ef 173 //
mjr 48:058ace2aed1d 174 // fast red/purple = out of memory. The controller halts and displays
mjr 48:058ace2aed1d 175 // this diagnostic code until you manually reset it. If this happens,
mjr 48:058ace2aed1d 176 // it's probably because the configuration is too complex, in which
mjr 48:058ace2aed1d 177 // case the same error will occur after the reset. If it's stuck
mjr 48:058ace2aed1d 178 // in this cycle, you'll have to restore the default configuration
mjr 48:058ace2aed1d 179 // by re-installing the controller software (the Pinscape .bin file).
mjr 10:976666ffa4ef 180 //
mjr 48:058ace2aed1d 181 //
mjr 48:058ace2aed1d 182 // USB PROTOCOL: Most of our USB messaging is through standard USB HID
mjr 48:058ace2aed1d 183 // classes (joystick, keyboard). We also accept control messages on our
mjr 48:058ace2aed1d 184 // primary HID interface "OUT endpoint" using a custom protocol that's
mjr 48:058ace2aed1d 185 // not defined in any USB standards (we do have to provide a USB HID
mjr 48:058ace2aed1d 186 // Report Descriptor for it, but this just describes the protocol as
mjr 48:058ace2aed1d 187 // opaque vendor-defined bytes). The control protocol incorporates the
mjr 48:058ace2aed1d 188 // LedWiz protocol as a subset, and adds our own private extensions.
mjr 48:058ace2aed1d 189 // For full details, see USBProtocol.h.
mjr 33:d832bcab089e 190
mjr 33:d832bcab089e 191
mjr 0:5acbbe3f4cf4 192 #include "mbed.h"
mjr 6:cc35eb643e8f 193 #include "math.h"
mjr 48:058ace2aed1d 194 #include "pinscape.h"
mjr 0:5acbbe3f4cf4 195 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 196 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 197 #include "tsl1410r.h"
mjr 1:d913e0afb2ac 198 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 199 #include "crc32.h"
mjr 26:cb71c4af2912 200 #include "TLC5940.h"
mjr 34:6b981a2afab7 201 #include "74HC595.h"
mjr 35:e959ffba78fd 202 #include "nvm.h"
mjr 35:e959ffba78fd 203 #include "plunger.h"
mjr 35:e959ffba78fd 204 #include "ccdSensor.h"
mjr 35:e959ffba78fd 205 #include "potSensor.h"
mjr 35:e959ffba78fd 206 #include "nullSensor.h"
mjr 48:058ace2aed1d 207 #include "TinyDigitalIn.h"
mjr 2:c174f9ee414a 208
mjr 21:5048e16cc9ef 209 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 210 #include "config.h"
mjr 17:ab3cec0c8bf4 211
mjr 53:9b2611964afc 212
mjr 53:9b2611964afc 213 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 214 //
mjr 53:9b2611964afc 215 // OpenSDA module identifier. This is for the benefit of the Windows
mjr 53:9b2611964afc 216 // configuration tool. When the config tool installs a .bin file onto
mjr 53:9b2611964afc 217 // the KL25Z, it will first find the sentinel string within the .bin file,
mjr 53:9b2611964afc 218 // and patch the "\0" bytes that follow the sentinel string with the
mjr 53:9b2611964afc 219 // OpenSDA module ID data. This allows us to report the OpenSDA
mjr 53:9b2611964afc 220 // identifiers back to the host system via USB, which in turn allows the
mjr 53:9b2611964afc 221 // config tool to figure out which OpenSDA MSD (mass storage device - a
mjr 53:9b2611964afc 222 // virtual disk drive) correlates to which Pinscape controller USB
mjr 53:9b2611964afc 223 // interface.
mjr 53:9b2611964afc 224 //
mjr 53:9b2611964afc 225 // This is only important if multiple Pinscape devices are attached to
mjr 53:9b2611964afc 226 // the same host. There doesn't seem to be any other way to figure out
mjr 53:9b2611964afc 227 // which OpenSDA MSD corresponds to which KL25Z USB interface; the OpenSDA
mjr 53:9b2611964afc 228 // MSD doesn't report the KL25Z CPU ID anywhere, and the KL25Z doesn't
mjr 53:9b2611964afc 229 // have any way to learn about the OpenSDA module it's connected to. The
mjr 53:9b2611964afc 230 // only way to pass this information to the KL25Z side that I can come up
mjr 53:9b2611964afc 231 // with is to have the Windows host embed it in the .bin file before
mjr 53:9b2611964afc 232 // downloading it to the OpenSDA MSD.
mjr 53:9b2611964afc 233 //
mjr 53:9b2611964afc 234 // We initialize the const data buffer (the part after the sentinel string)
mjr 53:9b2611964afc 235 // with all "\0" bytes, so that's what will be in the executable image that
mjr 53:9b2611964afc 236 // comes out of the mbed compiler. If you manually install the resulting
mjr 53:9b2611964afc 237 // .bin file onto the KL25Z (via the Windows desktop, say), the "\0" bytes
mjr 53:9b2611964afc 238 // will stay this way and read as all 0's at run-time. Since a real TUID
mjr 53:9b2611964afc 239 // would never be all 0's, that tells us that we were never patched and
mjr 53:9b2611964afc 240 // thus don't have any information on the OpenSDA module.
mjr 53:9b2611964afc 241 //
mjr 53:9b2611964afc 242 const char *getOpenSDAID()
mjr 53:9b2611964afc 243 {
mjr 53:9b2611964afc 244 #define OPENSDA_PREFIX "///Pinscape.OpenSDA.TUID///"
mjr 53:9b2611964afc 245 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 246 const size_t OpenSDA_prefix_length = sizeof(OPENSDA_PREFIX) - 1;
mjr 53:9b2611964afc 247
mjr 53:9b2611964afc 248 return OpenSDA + OpenSDA_prefix_length;
mjr 53:9b2611964afc 249 }
mjr 53:9b2611964afc 250
mjr 53:9b2611964afc 251 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 252 //
mjr 53:9b2611964afc 253 // Build ID. We use the date and time of compiling the program as a build
mjr 53:9b2611964afc 254 // identifier. It would be a little nicer to use a simple serial number
mjr 53:9b2611964afc 255 // instead, but the mbed platform doesn't have a way to automate that. The
mjr 53:9b2611964afc 256 // timestamp is a pretty good proxy for a serial number in that it will
mjr 53:9b2611964afc 257 // naturally increase on each new build, which is the primary property we
mjr 53:9b2611964afc 258 // want from this.
mjr 53:9b2611964afc 259 //
mjr 53:9b2611964afc 260 // As with the embedded OpenSDA ID, we store the build timestamp with a
mjr 53:9b2611964afc 261 // sentinel string prefix, to allow automated tools to find the static data
mjr 53:9b2611964afc 262 // in the .bin file by searching for the sentinel string. In contrast to
mjr 53:9b2611964afc 263 // the OpenSDA ID, the value we store here is for tools to extract rather
mjr 53:9b2611964afc 264 // than store, since we automatically populate it via the preprocessor
mjr 53:9b2611964afc 265 // macros.
mjr 53:9b2611964afc 266 //
mjr 53:9b2611964afc 267 const char *getBuildID()
mjr 53:9b2611964afc 268 {
mjr 53:9b2611964afc 269 #define BUILDID_PREFIX "///Pinscape.Build.ID///"
mjr 53:9b2611964afc 270 static const char BuildID[] = BUILDID_PREFIX __DATE__ " " __TIME__ "///";
mjr 53:9b2611964afc 271 const size_t BuildID_prefix_length = sizeof(BUILDID_PREFIX) - 1;
mjr 53:9b2611964afc 272
mjr 53:9b2611964afc 273 return BuildID + BuildID_prefix_length;
mjr 53:9b2611964afc 274 }
mjr 53:9b2611964afc 275
mjr 53:9b2611964afc 276
mjr 48:058ace2aed1d 277 // --------------------------------------------------------------------------
mjr 48:058ace2aed1d 278 //
mjr 59:94eb9265b6d7 279 // Custom memory allocator. We use our own version of malloc() for more
mjr 59:94eb9265b6d7 280 // efficient memory usage, and to provide diagnostics if we run out of heap.
mjr 48:058ace2aed1d 281 //
mjr 59:94eb9265b6d7 282 // We can implement a more efficient malloc than the library can because we
mjr 59:94eb9265b6d7 283 // can make an assumption that the library can't: allocations are permanent.
mjr 59:94eb9265b6d7 284 // The normal malloc has to assume that allocations can be freed, so it has
mjr 59:94eb9265b6d7 285 // to track blocks individually. For the purposes of this program, though,
mjr 59:94eb9265b6d7 286 // we don't have to do this because virtually all of our allocations are
mjr 59:94eb9265b6d7 287 // de facto permanent. We only allocate dyanmic memory during setup, and
mjr 59:94eb9265b6d7 288 // once we set things up, we never delete anything. This means that we can
mjr 59:94eb9265b6d7 289 // allocate memory in bare blocks without any bookkeeping overhead.
mjr 59:94eb9265b6d7 290 //
mjr 59:94eb9265b6d7 291 // In addition, we can make a much larger overall pool of memory available
mjr 59:94eb9265b6d7 292 // in a custom allocator. The mbed library malloc() seems to have a pool
mjr 59:94eb9265b6d7 293 // of about 3K to work with, even though there's really about 9K of RAM
mjr 59:94eb9265b6d7 294 // left over after counting the static writable data and reserving space
mjr 59:94eb9265b6d7 295 // for a reasonable stack. I haven't looked at the mbed malloc to see why
mjr 59:94eb9265b6d7 296 // they're so stingy, but it appears from empirical testing that we can
mjr 59:94eb9265b6d7 297 // create a static array up to about 9K before things get crashy.
mjr 59:94eb9265b6d7 298
mjr 48:058ace2aed1d 299 void *xmalloc(size_t siz)
mjr 48:058ace2aed1d 300 {
mjr 59:94eb9265b6d7 301 // Dynamic memory pool. We'll reserve space for all dynamic
mjr 59:94eb9265b6d7 302 // allocations by creating a simple C array of bytes. The size
mjr 59:94eb9265b6d7 303 // of this array is the maximum number of bytes we can allocate
mjr 59:94eb9265b6d7 304 // with malloc or operator 'new'.
mjr 59:94eb9265b6d7 305 //
mjr 59:94eb9265b6d7 306 // The maximum safe size for this array is, in essence, the
mjr 59:94eb9265b6d7 307 // amount of physical KL25Z RAM left over after accounting for
mjr 59:94eb9265b6d7 308 // static data throughout the rest of the program, the run-time
mjr 59:94eb9265b6d7 309 // stack, and any other space reserved for compiler or MCU
mjr 59:94eb9265b6d7 310 // overhead. Unfortunately, it's not straightforward to
mjr 59:94eb9265b6d7 311 // determine this analytically. The big complication is that
mjr 59:94eb9265b6d7 312 // the minimum stack size isn't easily predictable, as the stack
mjr 59:94eb9265b6d7 313 // grows according to what the program does. In addition, the
mjr 59:94eb9265b6d7 314 // mbed platform tools don't give us detailed data on the
mjr 59:94eb9265b6d7 315 // compiler/linker memory map. All we get is a generic total
mjr 59:94eb9265b6d7 316 // RAM requirement, which doesn't necessarily account for all
mjr 59:94eb9265b6d7 317 // overhead (e.g., gaps inserted to get proper alignment for
mjr 59:94eb9265b6d7 318 // particular memory blocks).
mjr 59:94eb9265b6d7 319 //
mjr 59:94eb9265b6d7 320 // A very rough estimate: the total RAM size reported by the
mjr 59:94eb9265b6d7 321 // linker is about 3.5K (currently - that can obviously change
mjr 59:94eb9265b6d7 322 // as the project evolves) out of 16K total. Assuming about a
mjr 59:94eb9265b6d7 323 // 3K stack, that leaves in the ballpark of 10K. Empirically,
mjr 59:94eb9265b6d7 324 // that seems pretty close. In testing, we start to see some
mjr 59:94eb9265b6d7 325 // instability at 10K, while 9K seems safe. To be conservative,
mjr 59:94eb9265b6d7 326 // we'll reduce this to 8K.
mjr 59:94eb9265b6d7 327 //
mjr 59:94eb9265b6d7 328 // Our measured total usage in the base configuration (22 GPIO
mjr 59:94eb9265b6d7 329 // output ports, TSL1410R plunger sensor) is about 4000 bytes.
mjr 59:94eb9265b6d7 330 // A pretty fully decked-out configuration (121 output ports,
mjr 59:94eb9265b6d7 331 // with 8 TLC5940 chips and 3 74HC595 chips, plus the TSL1412R
mjr 59:94eb9265b6d7 332 // sensor with the higher pixel count, and all expansion board
mjr 59:94eb9265b6d7 333 // features enabled) comes to about 6700 bytes. That leaves
mjr 59:94eb9265b6d7 334 // us with about 1.5K free out of our 8K, so we still have a
mjr 59:94eb9265b6d7 335 // little more headroom for future expansion.
mjr 59:94eb9265b6d7 336 //
mjr 59:94eb9265b6d7 337 // For comparison, the standard mbed malloc() runs out of
mjr 59:94eb9265b6d7 338 // memory at about 6K. That's what led to this custom malloc:
mjr 59:94eb9265b6d7 339 // we can just fit the base configuration into that 4K, but
mjr 59:94eb9265b6d7 340 // it's not enough space for more complex setups. There's
mjr 59:94eb9265b6d7 341 // still a little room for squeezing out unnecessary space
mjr 59:94eb9265b6d7 342 // from the mbed library code, but at this point I'd prefer
mjr 59:94eb9265b6d7 343 // to treat that as a last resort, since it would mean having
mjr 59:94eb9265b6d7 344 // to fork private copies of the libraries.
mjr 59:94eb9265b6d7 345 static char pool[8*1024];
mjr 59:94eb9265b6d7 346 static char *nxt = pool;
mjr 59:94eb9265b6d7 347 static size_t rem = sizeof(pool);
mjr 59:94eb9265b6d7 348
mjr 59:94eb9265b6d7 349 // align to a 4-byte increment
mjr 59:94eb9265b6d7 350 siz = (siz + 3) & ~3;
mjr 59:94eb9265b6d7 351
mjr 59:94eb9265b6d7 352 // If insufficient memory is available, halt and show a fast red/purple
mjr 59:94eb9265b6d7 353 // diagnostic flash. We don't want to return, since we assume throughout
mjr 59:94eb9265b6d7 354 // the program that all memory allocations must succeed. Note that this
mjr 59:94eb9265b6d7 355 // is generally considered bad programming practice in applications on
mjr 59:94eb9265b6d7 356 // "real" computers, but for the purposes of this microcontroller app,
mjr 59:94eb9265b6d7 357 // there's no point in checking for failed allocations individually
mjr 59:94eb9265b6d7 358 // because there's no way to recover from them. It's better in this
mjr 59:94eb9265b6d7 359 // context to handle failed allocations as fatal errors centrally. We
mjr 59:94eb9265b6d7 360 // can't recover from these automatically, so we have to resort to user
mjr 59:94eb9265b6d7 361 // intervention, which we signal with the diagnostic LED flashes.
mjr 59:94eb9265b6d7 362 if (siz > rem)
mjr 59:94eb9265b6d7 363 {
mjr 59:94eb9265b6d7 364 // halt with the diagnostic display (by looping forever)
mjr 59:94eb9265b6d7 365 for (;;)
mjr 59:94eb9265b6d7 366 {
mjr 59:94eb9265b6d7 367 diagLED(1, 0, 0);
mjr 59:94eb9265b6d7 368 wait_us(200000);
mjr 59:94eb9265b6d7 369 diagLED(1, 0, 1);
mjr 59:94eb9265b6d7 370 wait_us(200000);
mjr 59:94eb9265b6d7 371 }
mjr 59:94eb9265b6d7 372 }
mjr 48:058ace2aed1d 373
mjr 59:94eb9265b6d7 374 // get the next free location from the pool to return
mjr 59:94eb9265b6d7 375 char *ret = nxt;
mjr 59:94eb9265b6d7 376
mjr 59:94eb9265b6d7 377 // advance the pool pointer and decrement the remaining size counter
mjr 59:94eb9265b6d7 378 nxt += siz;
mjr 59:94eb9265b6d7 379 rem -= siz;
mjr 59:94eb9265b6d7 380
mjr 59:94eb9265b6d7 381 // return the allocated block
mjr 59:94eb9265b6d7 382 return ret;
mjr 48:058ace2aed1d 383 }
mjr 48:058ace2aed1d 384
mjr 59:94eb9265b6d7 385 // Overload operator new to call our custom malloc. This ensures that
mjr 59:94eb9265b6d7 386 // all 'new' allocations throughout the program (including library code)
mjr 59:94eb9265b6d7 387 // go through our private allocator.
mjr 48:058ace2aed1d 388 void *operator new(size_t siz) { return xmalloc(siz); }
mjr 48:058ace2aed1d 389 void *operator new[](size_t siz) { return xmalloc(siz); }
mjr 5:a70c0bce770d 390
mjr 59:94eb9265b6d7 391 // Since we don't do bookkeeping to track released memory, 'delete' does
mjr 59:94eb9265b6d7 392 // nothing. In actual testing, this routine appears to never be called.
mjr 59:94eb9265b6d7 393 // If it *is* ever called, it will simply leave the block in place, which
mjr 59:94eb9265b6d7 394 // will make it unavailable for re-use but will otherwise be harmless.
mjr 59:94eb9265b6d7 395 void operator delete(void *ptr) { }
mjr 59:94eb9265b6d7 396
mjr 59:94eb9265b6d7 397
mjr 5:a70c0bce770d 398 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 399 //
mjr 38:091e511ce8a0 400 // Forward declarations
mjr 38:091e511ce8a0 401 //
mjr 38:091e511ce8a0 402 void setNightMode(bool on);
mjr 38:091e511ce8a0 403 void toggleNightMode();
mjr 38:091e511ce8a0 404
mjr 38:091e511ce8a0 405 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 406 // utilities
mjr 17:ab3cec0c8bf4 407
mjr 26:cb71c4af2912 408 // floating point square of a number
mjr 26:cb71c4af2912 409 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 410
mjr 26:cb71c4af2912 411 // floating point rounding
mjr 26:cb71c4af2912 412 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 413
mjr 17:ab3cec0c8bf4 414
mjr 33:d832bcab089e 415 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 416 //
mjr 40:cc0d9814522b 417 // Extended verison of Timer class. This adds the ability to interrogate
mjr 40:cc0d9814522b 418 // the running state.
mjr 40:cc0d9814522b 419 //
mjr 40:cc0d9814522b 420 class Timer2: public Timer
mjr 40:cc0d9814522b 421 {
mjr 40:cc0d9814522b 422 public:
mjr 40:cc0d9814522b 423 Timer2() : running(false) { }
mjr 40:cc0d9814522b 424
mjr 40:cc0d9814522b 425 void start() { running = true; Timer::start(); }
mjr 40:cc0d9814522b 426 void stop() { running = false; Timer::stop(); }
mjr 40:cc0d9814522b 427
mjr 40:cc0d9814522b 428 bool isRunning() const { return running; }
mjr 40:cc0d9814522b 429
mjr 40:cc0d9814522b 430 private:
mjr 40:cc0d9814522b 431 bool running;
mjr 40:cc0d9814522b 432 };
mjr 40:cc0d9814522b 433
mjr 53:9b2611964afc 434
mjr 53:9b2611964afc 435 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 436 //
mjr 53:9b2611964afc 437 // Reboot timer. When we have a deferred reboot operation pending, we
mjr 53:9b2611964afc 438 // set the target time and start the timer.
mjr 53:9b2611964afc 439 Timer2 rebootTimer;
mjr 53:9b2611964afc 440 long rebootTime_us;
mjr 53:9b2611964afc 441
mjr 40:cc0d9814522b 442 // --------------------------------------------------------------------------
mjr 40:cc0d9814522b 443 //
mjr 33:d832bcab089e 444 // USB product version number
mjr 5:a70c0bce770d 445 //
mjr 47:df7a88cd249c 446 const uint16_t USB_VERSION_NO = 0x000A;
mjr 33:d832bcab089e 447
mjr 33:d832bcab089e 448 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 449 //
mjr 6:cc35eb643e8f 450 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 451 //
mjr 6:cc35eb643e8f 452 #define JOYMAX 4096
mjr 6:cc35eb643e8f 453
mjr 9:fd65b0a94720 454
mjr 17:ab3cec0c8bf4 455 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 456 //
mjr 40:cc0d9814522b 457 // Wire protocol value translations. These translate byte values to and
mjr 40:cc0d9814522b 458 // from the USB protocol to local native format.
mjr 35:e959ffba78fd 459 //
mjr 35:e959ffba78fd 460
mjr 35:e959ffba78fd 461 // unsigned 16-bit integer
mjr 35:e959ffba78fd 462 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 463 {
mjr 35:e959ffba78fd 464 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 465 }
mjr 40:cc0d9814522b 466 inline void ui16Wire(uint8_t *b, uint16_t val)
mjr 40:cc0d9814522b 467 {
mjr 40:cc0d9814522b 468 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 469 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 470 }
mjr 35:e959ffba78fd 471
mjr 35:e959ffba78fd 472 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 473 {
mjr 35:e959ffba78fd 474 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 475 }
mjr 40:cc0d9814522b 476 inline void i16Wire(uint8_t *b, int16_t val)
mjr 40:cc0d9814522b 477 {
mjr 40:cc0d9814522b 478 ui16Wire(b, (uint16_t)val);
mjr 40:cc0d9814522b 479 }
mjr 35:e959ffba78fd 480
mjr 35:e959ffba78fd 481 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 482 {
mjr 35:e959ffba78fd 483 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 484 }
mjr 40:cc0d9814522b 485 inline void ui32Wire(uint8_t *b, uint32_t val)
mjr 40:cc0d9814522b 486 {
mjr 40:cc0d9814522b 487 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 488 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 489 b[2] = (uint8_t)((val >> 16) & 0xff);
mjr 40:cc0d9814522b 490 b[3] = (uint8_t)((val >> 24) & 0xff);
mjr 40:cc0d9814522b 491 }
mjr 35:e959ffba78fd 492
mjr 35:e959ffba78fd 493 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 494 {
mjr 35:e959ffba78fd 495 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 496 }
mjr 35:e959ffba78fd 497
mjr 53:9b2611964afc 498 // Convert "wire" (USB) pin codes to/from PinName values.
mjr 53:9b2611964afc 499 //
mjr 53:9b2611964afc 500 // The internal mbed PinName format is
mjr 53:9b2611964afc 501 //
mjr 53:9b2611964afc 502 // ((port) << PORT_SHIFT) | (pin << 2) // MBED FORMAT
mjr 53:9b2611964afc 503 //
mjr 53:9b2611964afc 504 // where 'port' is 0-4 for Port A to Port E, and 'pin' is
mjr 53:9b2611964afc 505 // 0 to 31. E.g., E31 is (4 << PORT_SHIFT) | (31<<2).
mjr 53:9b2611964afc 506 //
mjr 53:9b2611964afc 507 // We remap this to our more compact wire format where each
mjr 53:9b2611964afc 508 // pin name fits in 8 bits:
mjr 53:9b2611964afc 509 //
mjr 53:9b2611964afc 510 // ((port) << 5) | pin) // WIRE FORMAT
mjr 53:9b2611964afc 511 //
mjr 53:9b2611964afc 512 // E.g., E31 is (4 << 5) | 31.
mjr 53:9b2611964afc 513 //
mjr 53:9b2611964afc 514 // Wire code FF corresponds to PinName NC (not connected).
mjr 53:9b2611964afc 515 //
mjr 53:9b2611964afc 516 inline PinName wirePinName(uint8_t c)
mjr 35:e959ffba78fd 517 {
mjr 53:9b2611964afc 518 if (c == 0xFF)
mjr 53:9b2611964afc 519 return NC; // 0xFF -> NC
mjr 53:9b2611964afc 520 else
mjr 53:9b2611964afc 521 return PinName(
mjr 53:9b2611964afc 522 (int(c & 0xE0) << (PORT_SHIFT - 5)) // top three bits are the port
mjr 53:9b2611964afc 523 | (int(c & 0x1F) << 2)); // bottom five bits are pin
mjr 40:cc0d9814522b 524 }
mjr 40:cc0d9814522b 525 inline void pinNameWire(uint8_t *b, PinName n)
mjr 40:cc0d9814522b 526 {
mjr 53:9b2611964afc 527 *b = PINNAME_TO_WIRE(n);
mjr 35:e959ffba78fd 528 }
mjr 35:e959ffba78fd 529
mjr 35:e959ffba78fd 530
mjr 35:e959ffba78fd 531 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 532 //
mjr 38:091e511ce8a0 533 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 534 //
mjr 38:091e511ce8a0 535 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 536 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 537 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 538 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 539 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 540 // SPI capability.
mjr 38:091e511ce8a0 541 //
mjr 38:091e511ce8a0 542 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 543
mjr 38:091e511ce8a0 544 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 545 // on, and -1 is no change (leaves the current setting intact).
mjr 38:091e511ce8a0 546 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 547 {
mjr 38:091e511ce8a0 548 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 549 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 550 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 551 }
mjr 38:091e511ce8a0 552
mjr 38:091e511ce8a0 553 // check an output port assignment to see if it conflicts with
mjr 38:091e511ce8a0 554 // an on-board LED segment
mjr 38:091e511ce8a0 555 struct LedSeg
mjr 38:091e511ce8a0 556 {
mjr 38:091e511ce8a0 557 bool r, g, b;
mjr 38:091e511ce8a0 558 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 559
mjr 38:091e511ce8a0 560 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 561 {
mjr 38:091e511ce8a0 562 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 563 // our on-board LED segments
mjr 38:091e511ce8a0 564 int t = pc.typ;
mjr 38:091e511ce8a0 565 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 38:091e511ce8a0 566 {
mjr 38:091e511ce8a0 567 // it's a GPIO port - check for a matching pin assignment
mjr 38:091e511ce8a0 568 PinName pin = wirePinName(pc.pin);
mjr 38:091e511ce8a0 569 if (pin == LED1)
mjr 38:091e511ce8a0 570 r = true;
mjr 38:091e511ce8a0 571 else if (pin == LED2)
mjr 38:091e511ce8a0 572 g = true;
mjr 38:091e511ce8a0 573 else if (pin == LED3)
mjr 38:091e511ce8a0 574 b = true;
mjr 38:091e511ce8a0 575 }
mjr 38:091e511ce8a0 576 }
mjr 38:091e511ce8a0 577 };
mjr 38:091e511ce8a0 578
mjr 38:091e511ce8a0 579 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 580 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 581 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 582 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 583 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 584 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 585 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 586 {
mjr 38:091e511ce8a0 587 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 588 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 589 LedSeg l;
mjr 38:091e511ce8a0 590 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 591 l.check(cfg.outPort[i]);
mjr 38:091e511ce8a0 592
mjr 38:091e511ce8a0 593 // We now know which segments are taken for LedWiz use and which
mjr 38:091e511ce8a0 594 // are free. Create diagnostic ports for the ones not claimed for
mjr 38:091e511ce8a0 595 // LedWiz use.
mjr 38:091e511ce8a0 596 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 597 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 598 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 599 }
mjr 38:091e511ce8a0 600
mjr 38:091e511ce8a0 601
mjr 38:091e511ce8a0 602 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 603 //
mjr 29:582472d0bc57 604 // LedWiz emulation, and enhanced TLC5940 output controller
mjr 5:a70c0bce770d 605 //
mjr 26:cb71c4af2912 606 // There are two modes for this feature. The default mode uses the on-board
mjr 26:cb71c4af2912 607 // GPIO ports to implement device outputs - each LedWiz software port is
mjr 26:cb71c4af2912 608 // connected to a physical GPIO pin on the KL25Z. The KL25Z only has 10
mjr 26:cb71c4af2912 609 // PWM channels, so in this mode only 10 LedWiz ports will be dimmable; the
mjr 26:cb71c4af2912 610 // rest are strictly on/off. The KL25Z also has a limited number of GPIO
mjr 26:cb71c4af2912 611 // ports overall - not enough for the full complement of 32 LedWiz ports
mjr 26:cb71c4af2912 612 // and 24 VP joystick inputs, so it's necessary to trade one against the
mjr 26:cb71c4af2912 613 // other if both features are to be used.
mjr 26:cb71c4af2912 614 //
mjr 26:cb71c4af2912 615 // The alternative, enhanced mode uses external TLC5940 PWM controller
mjr 26:cb71c4af2912 616 // chips to control device outputs. In this mode, each LedWiz software
mjr 26:cb71c4af2912 617 // port is mapped to an output on one of the external TLC5940 chips.
mjr 26:cb71c4af2912 618 // Two 5940s is enough for the full set of 32 LedWiz ports, and we can
mjr 26:cb71c4af2912 619 // support even more chips for even more outputs (although doing so requires
mjr 26:cb71c4af2912 620 // breaking LedWiz compatibility, since the LedWiz USB protocol is hardwired
mjr 26:cb71c4af2912 621 // for 32 outputs). Every port in this mode has full PWM support.
mjr 26:cb71c4af2912 622 //
mjr 5:a70c0bce770d 623
mjr 29:582472d0bc57 624
mjr 26:cb71c4af2912 625 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 626 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 627 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 628 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 629 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 630
mjr 26:cb71c4af2912 631 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 632 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 633 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 634 class LwOut
mjr 6:cc35eb643e8f 635 {
mjr 6:cc35eb643e8f 636 public:
mjr 40:cc0d9814522b 637 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 638 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 639 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 640 };
mjr 26:cb71c4af2912 641
mjr 35:e959ffba78fd 642 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 643 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 644 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 645 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 646 // numbering.
mjr 35:e959ffba78fd 647 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 648 {
mjr 33:d832bcab089e 649 public:
mjr 35:e959ffba78fd 650 LwVirtualOut() { }
mjr 40:cc0d9814522b 651 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 652 };
mjr 26:cb71c4af2912 653
mjr 34:6b981a2afab7 654 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 655 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 656 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 657 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 658 {
mjr 34:6b981a2afab7 659 public:
mjr 34:6b981a2afab7 660 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 661 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 662
mjr 34:6b981a2afab7 663 private:
mjr 53:9b2611964afc 664 // underlying physical output
mjr 34:6b981a2afab7 665 LwOut *out;
mjr 34:6b981a2afab7 666 };
mjr 34:6b981a2afab7 667
mjr 53:9b2611964afc 668 // Global ZB Launch Ball state
mjr 53:9b2611964afc 669 bool zbLaunchOn = false;
mjr 53:9b2611964afc 670
mjr 53:9b2611964afc 671 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 672 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 673 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 674 {
mjr 53:9b2611964afc 675 public:
mjr 53:9b2611964afc 676 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 677 virtual void set(uint8_t val)
mjr 53:9b2611964afc 678 {
mjr 53:9b2611964afc 679 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 680 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 681
mjr 53:9b2611964afc 682 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 683 out->set(val);
mjr 53:9b2611964afc 684 }
mjr 53:9b2611964afc 685
mjr 53:9b2611964afc 686 private:
mjr 53:9b2611964afc 687 // underlying physical or virtual output
mjr 53:9b2611964afc 688 LwOut *out;
mjr 53:9b2611964afc 689 };
mjr 53:9b2611964afc 690
mjr 53:9b2611964afc 691
mjr 40:cc0d9814522b 692 // Gamma correction table for 8-bit input values
mjr 40:cc0d9814522b 693 static const uint8_t gamma[] = {
mjr 40:cc0d9814522b 694 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 695 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 696 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 697 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 698 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 699 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 700 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 701 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 702 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 703 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 704 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 705 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 706 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 707 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 708 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 709 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 710 };
mjr 40:cc0d9814522b 711
mjr 40:cc0d9814522b 712 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 713 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 714 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 715 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 716 {
mjr 40:cc0d9814522b 717 public:
mjr 40:cc0d9814522b 718 LwGammaOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 719 virtual void set(uint8_t val) { out->set(gamma[val]); }
mjr 40:cc0d9814522b 720
mjr 40:cc0d9814522b 721 private:
mjr 40:cc0d9814522b 722 LwOut *out;
mjr 40:cc0d9814522b 723 };
mjr 40:cc0d9814522b 724
mjr 53:9b2611964afc 725 // global night mode flag
mjr 53:9b2611964afc 726 static bool nightMode = false;
mjr 53:9b2611964afc 727
mjr 40:cc0d9814522b 728 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 729 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 730 // mode is engaged.
mjr 40:cc0d9814522b 731 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 732 {
mjr 40:cc0d9814522b 733 public:
mjr 40:cc0d9814522b 734 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 735 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 736
mjr 53:9b2611964afc 737 private:
mjr 53:9b2611964afc 738 LwOut *out;
mjr 53:9b2611964afc 739 };
mjr 53:9b2611964afc 740
mjr 53:9b2611964afc 741 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 742 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 743 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 744 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 745 {
mjr 53:9b2611964afc 746 public:
mjr 53:9b2611964afc 747 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 748 virtual void set(uint8_t)
mjr 53:9b2611964afc 749 {
mjr 53:9b2611964afc 750 // ignore the host value and simply show the current
mjr 53:9b2611964afc 751 // night mode setting
mjr 53:9b2611964afc 752 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 753 }
mjr 40:cc0d9814522b 754
mjr 40:cc0d9814522b 755 private:
mjr 40:cc0d9814522b 756 LwOut *out;
mjr 40:cc0d9814522b 757 };
mjr 40:cc0d9814522b 758
mjr 26:cb71c4af2912 759
mjr 35:e959ffba78fd 760 //
mjr 35:e959ffba78fd 761 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 762 // assignments set in config.h.
mjr 33:d832bcab089e 763 //
mjr 35:e959ffba78fd 764 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 765 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 766 {
mjr 35:e959ffba78fd 767 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 768 {
mjr 53:9b2611964afc 769 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 770 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 771 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 772 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 773 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 774 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 775 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 776 }
mjr 35:e959ffba78fd 777 }
mjr 26:cb71c4af2912 778
mjr 40:cc0d9814522b 779 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 780 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 781 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 782 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 783 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 784 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 785 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 786 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 787 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 788 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 789 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 790 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 791 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 792 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 793 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 794 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 795 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 796 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 797 };
mjr 40:cc0d9814522b 798
mjr 40:cc0d9814522b 799 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 800 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 801 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 802 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 803 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 804 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 805 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 806 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 807 // are always 8 bits.
mjr 40:cc0d9814522b 808 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 809 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 810 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 811 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 812 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 813 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 814 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 815 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 816 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 817 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 818 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 819 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 820 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 821 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 822 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 823 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 824 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 825 };
mjr 40:cc0d9814522b 826
mjr 40:cc0d9814522b 827
mjr 26:cb71c4af2912 828 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 829 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 830 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 831 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 832 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 833 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 834 {
mjr 26:cb71c4af2912 835 public:
mjr 60:f38da020aa13 836 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 837 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 838 {
mjr 26:cb71c4af2912 839 if (val != prv)
mjr 40:cc0d9814522b 840 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 841 }
mjr 60:f38da020aa13 842 uint8_t idx;
mjr 40:cc0d9814522b 843 uint8_t prv;
mjr 26:cb71c4af2912 844 };
mjr 26:cb71c4af2912 845
mjr 40:cc0d9814522b 846 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 847 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 848 {
mjr 40:cc0d9814522b 849 public:
mjr 60:f38da020aa13 850 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 851 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 852 {
mjr 40:cc0d9814522b 853 if (val != prv)
mjr 40:cc0d9814522b 854 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 855 }
mjr 60:f38da020aa13 856 uint8_t idx;
mjr 40:cc0d9814522b 857 uint8_t prv;
mjr 40:cc0d9814522b 858 };
mjr 40:cc0d9814522b 859
mjr 40:cc0d9814522b 860
mjr 33:d832bcab089e 861
mjr 34:6b981a2afab7 862 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 863 // config.h.
mjr 35:e959ffba78fd 864 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 865
mjr 35:e959ffba78fd 866 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 867 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 868 {
mjr 35:e959ffba78fd 869 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 870 {
mjr 53:9b2611964afc 871 hc595 = new HC595(
mjr 53:9b2611964afc 872 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 873 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 874 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 875 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 876 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 877 hc595->init();
mjr 35:e959ffba78fd 878 hc595->update();
mjr 35:e959ffba78fd 879 }
mjr 35:e959ffba78fd 880 }
mjr 34:6b981a2afab7 881
mjr 34:6b981a2afab7 882 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 883 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 884 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 885 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 886 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 887 class Lw595Out: public LwOut
mjr 33:d832bcab089e 888 {
mjr 33:d832bcab089e 889 public:
mjr 60:f38da020aa13 890 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 891 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 892 {
mjr 34:6b981a2afab7 893 if (val != prv)
mjr 40:cc0d9814522b 894 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 895 }
mjr 60:f38da020aa13 896 uint8_t idx;
mjr 40:cc0d9814522b 897 uint8_t prv;
mjr 33:d832bcab089e 898 };
mjr 33:d832bcab089e 899
mjr 26:cb71c4af2912 900
mjr 40:cc0d9814522b 901
mjr 40:cc0d9814522b 902 // Conversion table - 8-bit DOF output level to PWM float level
mjr 40:cc0d9814522b 903 // (normalized to 0.0..1.0 scale)
mjr 40:cc0d9814522b 904 static const float pwm_level[] = {
mjr 40:cc0d9814522b 905 0.000000, 0.003922, 0.007843, 0.011765, 0.015686, 0.019608, 0.023529, 0.027451,
mjr 40:cc0d9814522b 906 0.031373, 0.035294, 0.039216, 0.043137, 0.047059, 0.050980, 0.054902, 0.058824,
mjr 40:cc0d9814522b 907 0.062745, 0.066667, 0.070588, 0.074510, 0.078431, 0.082353, 0.086275, 0.090196,
mjr 40:cc0d9814522b 908 0.094118, 0.098039, 0.101961, 0.105882, 0.109804, 0.113725, 0.117647, 0.121569,
mjr 40:cc0d9814522b 909 0.125490, 0.129412, 0.133333, 0.137255, 0.141176, 0.145098, 0.149020, 0.152941,
mjr 40:cc0d9814522b 910 0.156863, 0.160784, 0.164706, 0.168627, 0.172549, 0.176471, 0.180392, 0.184314,
mjr 40:cc0d9814522b 911 0.188235, 0.192157, 0.196078, 0.200000, 0.203922, 0.207843, 0.211765, 0.215686,
mjr 40:cc0d9814522b 912 0.219608, 0.223529, 0.227451, 0.231373, 0.235294, 0.239216, 0.243137, 0.247059,
mjr 40:cc0d9814522b 913 0.250980, 0.254902, 0.258824, 0.262745, 0.266667, 0.270588, 0.274510, 0.278431,
mjr 40:cc0d9814522b 914 0.282353, 0.286275, 0.290196, 0.294118, 0.298039, 0.301961, 0.305882, 0.309804,
mjr 40:cc0d9814522b 915 0.313725, 0.317647, 0.321569, 0.325490, 0.329412, 0.333333, 0.337255, 0.341176,
mjr 40:cc0d9814522b 916 0.345098, 0.349020, 0.352941, 0.356863, 0.360784, 0.364706, 0.368627, 0.372549,
mjr 40:cc0d9814522b 917 0.376471, 0.380392, 0.384314, 0.388235, 0.392157, 0.396078, 0.400000, 0.403922,
mjr 40:cc0d9814522b 918 0.407843, 0.411765, 0.415686, 0.419608, 0.423529, 0.427451, 0.431373, 0.435294,
mjr 40:cc0d9814522b 919 0.439216, 0.443137, 0.447059, 0.450980, 0.454902, 0.458824, 0.462745, 0.466667,
mjr 40:cc0d9814522b 920 0.470588, 0.474510, 0.478431, 0.482353, 0.486275, 0.490196, 0.494118, 0.498039,
mjr 40:cc0d9814522b 921 0.501961, 0.505882, 0.509804, 0.513725, 0.517647, 0.521569, 0.525490, 0.529412,
mjr 40:cc0d9814522b 922 0.533333, 0.537255, 0.541176, 0.545098, 0.549020, 0.552941, 0.556863, 0.560784,
mjr 40:cc0d9814522b 923 0.564706, 0.568627, 0.572549, 0.576471, 0.580392, 0.584314, 0.588235, 0.592157,
mjr 40:cc0d9814522b 924 0.596078, 0.600000, 0.603922, 0.607843, 0.611765, 0.615686, 0.619608, 0.623529,
mjr 40:cc0d9814522b 925 0.627451, 0.631373, 0.635294, 0.639216, 0.643137, 0.647059, 0.650980, 0.654902,
mjr 40:cc0d9814522b 926 0.658824, 0.662745, 0.666667, 0.670588, 0.674510, 0.678431, 0.682353, 0.686275,
mjr 40:cc0d9814522b 927 0.690196, 0.694118, 0.698039, 0.701961, 0.705882, 0.709804, 0.713725, 0.717647,
mjr 40:cc0d9814522b 928 0.721569, 0.725490, 0.729412, 0.733333, 0.737255, 0.741176, 0.745098, 0.749020,
mjr 40:cc0d9814522b 929 0.752941, 0.756863, 0.760784, 0.764706, 0.768627, 0.772549, 0.776471, 0.780392,
mjr 40:cc0d9814522b 930 0.784314, 0.788235, 0.792157, 0.796078, 0.800000, 0.803922, 0.807843, 0.811765,
mjr 40:cc0d9814522b 931 0.815686, 0.819608, 0.823529, 0.827451, 0.831373, 0.835294, 0.839216, 0.843137,
mjr 40:cc0d9814522b 932 0.847059, 0.850980, 0.854902, 0.858824, 0.862745, 0.866667, 0.870588, 0.874510,
mjr 40:cc0d9814522b 933 0.878431, 0.882353, 0.886275, 0.890196, 0.894118, 0.898039, 0.901961, 0.905882,
mjr 40:cc0d9814522b 934 0.909804, 0.913725, 0.917647, 0.921569, 0.925490, 0.929412, 0.933333, 0.937255,
mjr 40:cc0d9814522b 935 0.941176, 0.945098, 0.949020, 0.952941, 0.956863, 0.960784, 0.964706, 0.968627,
mjr 40:cc0d9814522b 936 0.972549, 0.976471, 0.980392, 0.984314, 0.988235, 0.992157, 0.996078, 1.000000
mjr 40:cc0d9814522b 937 };
mjr 26:cb71c4af2912 938
mjr 26:cb71c4af2912 939 // LwOut class for a PWM-capable GPIO port
mjr 6:cc35eb643e8f 940 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 941 {
mjr 6:cc35eb643e8f 942 public:
mjr 43:7a6364d82a41 943 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 944 {
mjr 43:7a6364d82a41 945 prv = initVal ^ 0xFF;
mjr 43:7a6364d82a41 946 set(initVal);
mjr 43:7a6364d82a41 947 }
mjr 40:cc0d9814522b 948 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 949 {
mjr 13:72dda449c3c0 950 if (val != prv)
mjr 40:cc0d9814522b 951 p.write(pwm_level[prv = val]);
mjr 13:72dda449c3c0 952 }
mjr 6:cc35eb643e8f 953 PwmOut p;
mjr 40:cc0d9814522b 954 uint8_t prv;
mjr 6:cc35eb643e8f 955 };
mjr 26:cb71c4af2912 956
mjr 26:cb71c4af2912 957 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 958 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 959 {
mjr 6:cc35eb643e8f 960 public:
mjr 43:7a6364d82a41 961 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 962 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 963 {
mjr 13:72dda449c3c0 964 if (val != prv)
mjr 40:cc0d9814522b 965 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 966 }
mjr 6:cc35eb643e8f 967 DigitalOut p;
mjr 40:cc0d9814522b 968 uint8_t prv;
mjr 6:cc35eb643e8f 969 };
mjr 26:cb71c4af2912 970
mjr 29:582472d0bc57 971 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 972 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 973 // port n (0-based).
mjr 35:e959ffba78fd 974 //
mjr 35:e959ffba78fd 975 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 976 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 977 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 978 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 979 // 74HC595 ports).
mjr 33:d832bcab089e 980 static int numOutputs;
mjr 33:d832bcab089e 981 static LwOut **lwPin;
mjr 33:d832bcab089e 982
mjr 38:091e511ce8a0 983
mjr 35:e959ffba78fd 984 // Number of LedWiz emulation outputs. This is the number of ports
mjr 35:e959ffba78fd 985 // accessible through the standard (non-extended) LedWiz protocol
mjr 35:e959ffba78fd 986 // messages. The protocol has a fixed set of 32 outputs, but we
mjr 35:e959ffba78fd 987 // might have fewer actual outputs. This is therefore set to the
mjr 35:e959ffba78fd 988 // lower of 32 or the actual number of outputs.
mjr 35:e959ffba78fd 989 static int numLwOutputs;
mjr 35:e959ffba78fd 990
mjr 40:cc0d9814522b 991 // Current absolute brightness level for an output. This is a DOF
mjr 40:cc0d9814522b 992 // brightness level value, from 0 for fully off to 255 for fully on.
mjr 40:cc0d9814522b 993 // This is used for all extended ports (33 and above), and for any
mjr 40:cc0d9814522b 994 // LedWiz port with wizVal == 255.
mjr 40:cc0d9814522b 995 static uint8_t *outLevel;
mjr 38:091e511ce8a0 996
mjr 38:091e511ce8a0 997 // create a single output pin
mjr 53:9b2611964afc 998 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 999 {
mjr 38:091e511ce8a0 1000 // get this item's values
mjr 38:091e511ce8a0 1001 int typ = pc.typ;
mjr 38:091e511ce8a0 1002 int pin = pc.pin;
mjr 38:091e511ce8a0 1003 int flags = pc.flags;
mjr 40:cc0d9814522b 1004 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1005 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1006 int gamma = flags & PortFlagGamma;
mjr 38:091e511ce8a0 1007
mjr 38:091e511ce8a0 1008 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1009 LwOut *lwp;
mjr 38:091e511ce8a0 1010 switch (typ)
mjr 38:091e511ce8a0 1011 {
mjr 38:091e511ce8a0 1012 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1013 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1014 if (pin != 0)
mjr 48:058ace2aed1d 1015 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1016 else
mjr 48:058ace2aed1d 1017 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1018 break;
mjr 38:091e511ce8a0 1019
mjr 38:091e511ce8a0 1020 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1021 // Digital GPIO port
mjr 48:058ace2aed1d 1022 if (pin != 0)
mjr 48:058ace2aed1d 1023 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1024 else
mjr 48:058ace2aed1d 1025 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1026 break;
mjr 38:091e511ce8a0 1027
mjr 38:091e511ce8a0 1028 case PortTypeTLC5940:
mjr 38:091e511ce8a0 1029 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 1030 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 1031 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 1032 {
mjr 40:cc0d9814522b 1033 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 1034 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 1035 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 1036 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 1037 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 1038 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 1039 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 1040 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 1041 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 1042 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 1043 // for this unlikely case.
mjr 40:cc0d9814522b 1044 if (gamma && !activeLow)
mjr 40:cc0d9814522b 1045 {
mjr 40:cc0d9814522b 1046 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 1047 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 1048
mjr 40:cc0d9814522b 1049 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 1050 gamma = false;
mjr 40:cc0d9814522b 1051 }
mjr 40:cc0d9814522b 1052 else
mjr 40:cc0d9814522b 1053 {
mjr 40:cc0d9814522b 1054 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 1055 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 1056 }
mjr 40:cc0d9814522b 1057 }
mjr 38:091e511ce8a0 1058 else
mjr 40:cc0d9814522b 1059 {
mjr 40:cc0d9814522b 1060 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 1061 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 1062 }
mjr 38:091e511ce8a0 1063 break;
mjr 38:091e511ce8a0 1064
mjr 38:091e511ce8a0 1065 case PortType74HC595:
mjr 38:091e511ce8a0 1066 // 74HC595 port (if we don't have an HC595 controller object, or it's not a valid
mjr 38:091e511ce8a0 1067 // output number, create a virtual port)
mjr 38:091e511ce8a0 1068 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 1069 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 1070 else
mjr 38:091e511ce8a0 1071 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1072 break;
mjr 38:091e511ce8a0 1073
mjr 38:091e511ce8a0 1074 case PortTypeVirtual:
mjr 43:7a6364d82a41 1075 case PortTypeDisabled:
mjr 38:091e511ce8a0 1076 default:
mjr 38:091e511ce8a0 1077 // virtual or unknown
mjr 38:091e511ce8a0 1078 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1079 break;
mjr 38:091e511ce8a0 1080 }
mjr 38:091e511ce8a0 1081
mjr 40:cc0d9814522b 1082 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 1083 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 1084 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 1085 if (activeLow)
mjr 38:091e511ce8a0 1086 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 1087
mjr 40:cc0d9814522b 1088 // If it's a noisemaker, layer on a night mode switch. Note that this
mjr 40:cc0d9814522b 1089 // needs to be
mjr 40:cc0d9814522b 1090 if (noisy)
mjr 40:cc0d9814522b 1091 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 1092
mjr 40:cc0d9814522b 1093 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 1094 if (gamma)
mjr 40:cc0d9814522b 1095 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 1096
mjr 53:9b2611964afc 1097 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 53:9b2611964afc 1098 // that the nominal port numbering in the cofnig starts at 1, but we're
mjr 53:9b2611964afc 1099 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 1100 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 1101 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 1102
mjr 53:9b2611964afc 1103 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 1104 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 1105 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 1106
mjr 38:091e511ce8a0 1107 // turn it off initially
mjr 38:091e511ce8a0 1108 lwp->set(0);
mjr 38:091e511ce8a0 1109
mjr 38:091e511ce8a0 1110 // return the pin
mjr 38:091e511ce8a0 1111 return lwp;
mjr 38:091e511ce8a0 1112 }
mjr 38:091e511ce8a0 1113
mjr 6:cc35eb643e8f 1114 // initialize the output pin array
mjr 35:e959ffba78fd 1115 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 1116 {
mjr 35:e959ffba78fd 1117 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 1118 // total number of ports.
mjr 35:e959ffba78fd 1119 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 1120 int i;
mjr 35:e959ffba78fd 1121 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 1122 {
mjr 35:e959ffba78fd 1123 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 1124 {
mjr 35:e959ffba78fd 1125 numOutputs = i;
mjr 34:6b981a2afab7 1126 break;
mjr 34:6b981a2afab7 1127 }
mjr 33:d832bcab089e 1128 }
mjr 33:d832bcab089e 1129
mjr 35:e959ffba78fd 1130 // the real LedWiz protocol can access at most 32 ports, or the
mjr 35:e959ffba78fd 1131 // actual number of outputs, whichever is lower
mjr 35:e959ffba78fd 1132 numLwOutputs = (numOutputs < 32 ? numOutputs : 32);
mjr 35:e959ffba78fd 1133
mjr 33:d832bcab089e 1134 // allocate the pin array
mjr 33:d832bcab089e 1135 lwPin = new LwOut*[numOutputs];
mjr 33:d832bcab089e 1136
mjr 38:091e511ce8a0 1137 // Allocate the current brightness array. For these, allocate at
mjr 38:091e511ce8a0 1138 // least 32, so that we have enough for all LedWiz messages, but
mjr 38:091e511ce8a0 1139 // allocate the full set of actual ports if we have more than the
mjr 38:091e511ce8a0 1140 // LedWiz complement.
mjr 38:091e511ce8a0 1141 int minOuts = numOutputs < 32 ? 32 : numOutputs;
mjr 40:cc0d9814522b 1142 outLevel = new uint8_t[minOuts];
mjr 33:d832bcab089e 1143
mjr 35:e959ffba78fd 1144 // create the pin interface object for each port
mjr 35:e959ffba78fd 1145 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 1146 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 1147 }
mjr 6:cc35eb643e8f 1148
mjr 29:582472d0bc57 1149 // LedWiz output states.
mjr 29:582472d0bc57 1150 //
mjr 29:582472d0bc57 1151 // The LedWiz protocol has two separate control axes for each output.
mjr 29:582472d0bc57 1152 // One axis is its on/off state; the other is its "profile" state, which
mjr 29:582472d0bc57 1153 // is either a fixed brightness or a blinking pattern for the light.
mjr 29:582472d0bc57 1154 // The two axes are independent.
mjr 29:582472d0bc57 1155 //
mjr 29:582472d0bc57 1156 // Note that the LedWiz protocol can only address 32 outputs, so the
mjr 29:582472d0bc57 1157 // wizOn and wizVal arrays have fixed sizes of 32 elements no matter
mjr 29:582472d0bc57 1158 // how many physical outputs we're using.
mjr 29:582472d0bc57 1159
mjr 0:5acbbe3f4cf4 1160 // on/off state for each LedWiz output
mjr 1:d913e0afb2ac 1161 static uint8_t wizOn[32];
mjr 0:5acbbe3f4cf4 1162
mjr 40:cc0d9814522b 1163 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 40:cc0d9814522b 1164 // for each LedWiz output. If the output was last updated through an
mjr 40:cc0d9814522b 1165 // LedWiz protocol message, it will have one of these values:
mjr 29:582472d0bc57 1166 //
mjr 29:582472d0bc57 1167 // 0-48 = fixed brightness 0% to 100%
mjr 40:cc0d9814522b 1168 // 49 = fixed brightness 100% (equivalent to 48)
mjr 29:582472d0bc57 1169 // 129 = ramp up / ramp down
mjr 29:582472d0bc57 1170 // 130 = flash on / off
mjr 29:582472d0bc57 1171 // 131 = on / ramp down
mjr 29:582472d0bc57 1172 // 132 = ramp up / on
mjr 29:582472d0bc57 1173 //
mjr 40:cc0d9814522b 1174 // If the output was last updated through an extended protocol message,
mjr 40:cc0d9814522b 1175 // it will have the special value 255. This means that we use the
mjr 40:cc0d9814522b 1176 // outLevel[] value for the port instead of an LedWiz setting.
mjr 29:582472d0bc57 1177 //
mjr 40:cc0d9814522b 1178 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 40:cc0d9814522b 1179 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 40:cc0d9814522b 1180 // it, so we need to accept it for compatibility.)
mjr 1:d913e0afb2ac 1181 static uint8_t wizVal[32] = {
mjr 13:72dda449c3c0 1182 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 1183 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 1184 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 1185 48, 48, 48, 48, 48, 48, 48, 48
mjr 0:5acbbe3f4cf4 1186 };
mjr 0:5acbbe3f4cf4 1187
mjr 29:582472d0bc57 1188 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 29:582472d0bc57 1189 // rate for lights in blinking states.
mjr 29:582472d0bc57 1190 static uint8_t wizSpeed = 2;
mjr 29:582472d0bc57 1191
mjr 40:cc0d9814522b 1192 // Current LedWiz flash cycle counter. This runs from 0 to 255
mjr 40:cc0d9814522b 1193 // during each cycle.
mjr 29:582472d0bc57 1194 static uint8_t wizFlashCounter = 0;
mjr 29:582472d0bc57 1195
mjr 40:cc0d9814522b 1196 // translate an LedWiz brightness level (0-49) to a DOF brightness
mjr 40:cc0d9814522b 1197 // level (0-255)
mjr 40:cc0d9814522b 1198 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 1199 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 1200 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 1201 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 1202 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 1203 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 1204 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 1205 255, 255
mjr 40:cc0d9814522b 1206 };
mjr 40:cc0d9814522b 1207
mjr 40:cc0d9814522b 1208 // Translate an LedWiz output (ports 1-32) to a DOF brightness level.
mjr 40:cc0d9814522b 1209 static uint8_t wizState(int idx)
mjr 0:5acbbe3f4cf4 1210 {
mjr 29:582472d0bc57 1211 // if the output was last set with an extended protocol message,
mjr 29:582472d0bc57 1212 // use the value set there, ignoring the output's LedWiz state
mjr 29:582472d0bc57 1213 if (wizVal[idx] == 255)
mjr 29:582472d0bc57 1214 return outLevel[idx];
mjr 29:582472d0bc57 1215
mjr 29:582472d0bc57 1216 // if it's off, show at zero intensity
mjr 29:582472d0bc57 1217 if (!wizOn[idx])
mjr 29:582472d0bc57 1218 return 0;
mjr 29:582472d0bc57 1219
mjr 29:582472d0bc57 1220 // check the state
mjr 29:582472d0bc57 1221 uint8_t val = wizVal[idx];
mjr 40:cc0d9814522b 1222 if (val <= 49)
mjr 29:582472d0bc57 1223 {
mjr 29:582472d0bc57 1224 // PWM brightness/intensity level. Rescale from the LedWiz
mjr 29:582472d0bc57 1225 // 0..48 integer range to our internal PwmOut 0..1 float range.
mjr 29:582472d0bc57 1226 // Note that on the actual LedWiz, level 48 is actually about
mjr 29:582472d0bc57 1227 // 98% on - contrary to the LedWiz documentation, level 49 is
mjr 29:582472d0bc57 1228 // the true 100% level. (In the documentation, level 49 is
mjr 29:582472d0bc57 1229 // simply not a valid setting.) Even so, we treat level 48 as
mjr 29:582472d0bc57 1230 // 100% on to match the documentation. This won't be perfectly
mjr 29:582472d0bc57 1231 // ocmpatible with the actual LedWiz, but it makes for such a
mjr 29:582472d0bc57 1232 // small difference in brightness (if the output device is an
mjr 29:582472d0bc57 1233 // LED, say) that no one should notice. It seems better to
mjr 29:582472d0bc57 1234 // err in this direction, because while the difference in
mjr 29:582472d0bc57 1235 // brightness when attached to an LED won't be noticeable, the
mjr 29:582472d0bc57 1236 // difference in duty cycle when attached to something like a
mjr 29:582472d0bc57 1237 // contactor *can* be noticeable - anything less than 100%
mjr 29:582472d0bc57 1238 // can cause a contactor or relay to chatter. There's almost
mjr 29:582472d0bc57 1239 // never a situation where you'd want values other than 0% and
mjr 29:582472d0bc57 1240 // 100% for a contactor or relay, so treating level 48 as 100%
mjr 29:582472d0bc57 1241 // makes us work properly with software that's expecting the
mjr 29:582472d0bc57 1242 // documented LedWiz behavior and therefore uses level 48 to
mjr 29:582472d0bc57 1243 // turn a contactor or relay fully on.
mjr 40:cc0d9814522b 1244 //
mjr 40:cc0d9814522b 1245 // Note that value 49 is undefined in the LedWiz documentation,
mjr 40:cc0d9814522b 1246 // but real LedWiz units treat it as 100%, equivalent to 48.
mjr 40:cc0d9814522b 1247 // Some software on the PC side uses this, so we need to treat
mjr 40:cc0d9814522b 1248 // it the same way for compatibility.
mjr 40:cc0d9814522b 1249 return lw_to_dof[val];
mjr 29:582472d0bc57 1250 }
mjr 29:582472d0bc57 1251 else if (val == 129)
mjr 29:582472d0bc57 1252 {
mjr 40:cc0d9814522b 1253 // 129 = ramp up / ramp down
mjr 30:6e9902f06f48 1254 return wizFlashCounter < 128
mjr 40:cc0d9814522b 1255 ? wizFlashCounter*2 + 1
mjr 40:cc0d9814522b 1256 : (255 - wizFlashCounter)*2;
mjr 29:582472d0bc57 1257 }
mjr 29:582472d0bc57 1258 else if (val == 130)
mjr 29:582472d0bc57 1259 {
mjr 40:cc0d9814522b 1260 // 130 = flash on / off
mjr 40:cc0d9814522b 1261 return wizFlashCounter < 128 ? 255 : 0;
mjr 29:582472d0bc57 1262 }
mjr 29:582472d0bc57 1263 else if (val == 131)
mjr 29:582472d0bc57 1264 {
mjr 40:cc0d9814522b 1265 // 131 = on / ramp down
mjr 40:cc0d9814522b 1266 return wizFlashCounter < 128 ? 255 : (255 - wizFlashCounter)*2;
mjr 0:5acbbe3f4cf4 1267 }
mjr 29:582472d0bc57 1268 else if (val == 132)
mjr 29:582472d0bc57 1269 {
mjr 40:cc0d9814522b 1270 // 132 = ramp up / on
mjr 40:cc0d9814522b 1271 return wizFlashCounter < 128 ? wizFlashCounter*2 : 255;
mjr 29:582472d0bc57 1272 }
mjr 29:582472d0bc57 1273 else
mjr 13:72dda449c3c0 1274 {
mjr 29:582472d0bc57 1275 // Other values are undefined in the LedWiz documentation. Hosts
mjr 29:582472d0bc57 1276 // *should* never send undefined values, since whatever behavior an
mjr 29:582472d0bc57 1277 // LedWiz unit exhibits in response is accidental and could change
mjr 29:582472d0bc57 1278 // in a future version. We'll treat all undefined values as equivalent
mjr 29:582472d0bc57 1279 // to 48 (fully on).
mjr 40:cc0d9814522b 1280 return 255;
mjr 0:5acbbe3f4cf4 1281 }
mjr 0:5acbbe3f4cf4 1282 }
mjr 0:5acbbe3f4cf4 1283
mjr 29:582472d0bc57 1284 // LedWiz flash timer pulse. This fires periodically to update
mjr 29:582472d0bc57 1285 // LedWiz flashing outputs. At the slowest pulse speed set via
mjr 29:582472d0bc57 1286 // the SBA command, each waveform cycle has 256 steps, so we
mjr 29:582472d0bc57 1287 // choose the pulse time base so that the slowest cycle completes
mjr 29:582472d0bc57 1288 // in 2 seconds. This seems to roughly match the real LedWiz
mjr 29:582472d0bc57 1289 // behavior. We run the pulse timer at the same rate regardless
mjr 29:582472d0bc57 1290 // of the pulse speed; at higher pulse speeds, we simply use
mjr 29:582472d0bc57 1291 // larger steps through the cycle on each interrupt. Running
mjr 29:582472d0bc57 1292 // every 1/127 of a second = 8ms seems to be a pretty light load.
mjr 29:582472d0bc57 1293 Timeout wizPulseTimer;
mjr 38:091e511ce8a0 1294 #define WIZ_PULSE_TIME_BASE (1.0f/127.0f)
mjr 29:582472d0bc57 1295 static void wizPulse()
mjr 29:582472d0bc57 1296 {
mjr 29:582472d0bc57 1297 // increase the counter by the speed increment, and wrap at 256
mjr 29:582472d0bc57 1298 wizFlashCounter += wizSpeed;
mjr 29:582472d0bc57 1299 wizFlashCounter &= 0xff;
mjr 29:582472d0bc57 1300
mjr 29:582472d0bc57 1301 // if we have any flashing lights, update them
mjr 29:582472d0bc57 1302 int ena = false;
mjr 35:e959ffba78fd 1303 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 29:582472d0bc57 1304 {
mjr 29:582472d0bc57 1305 if (wizOn[i])
mjr 29:582472d0bc57 1306 {
mjr 29:582472d0bc57 1307 uint8_t s = wizVal[i];
mjr 29:582472d0bc57 1308 if (s >= 129 && s <= 132)
mjr 29:582472d0bc57 1309 {
mjr 40:cc0d9814522b 1310 lwPin[i]->set(wizState(i));
mjr 29:582472d0bc57 1311 ena = true;
mjr 29:582472d0bc57 1312 }
mjr 29:582472d0bc57 1313 }
mjr 29:582472d0bc57 1314 }
mjr 29:582472d0bc57 1315
mjr 29:582472d0bc57 1316 // Set up the next timer pulse only if we found anything flashing.
mjr 29:582472d0bc57 1317 // To minimize overhead from this feature, we only enable the interrupt
mjr 29:582472d0bc57 1318 // when we need it. This eliminates any performance penalty to other
mjr 29:582472d0bc57 1319 // features when the host software doesn't care about the flashing
mjr 29:582472d0bc57 1320 // modes. For example, DOF never uses these modes, so there's no
mjr 29:582472d0bc57 1321 // need for them when running Visual Pinball.
mjr 29:582472d0bc57 1322 if (ena)
mjr 29:582472d0bc57 1323 wizPulseTimer.attach(wizPulse, WIZ_PULSE_TIME_BASE);
mjr 29:582472d0bc57 1324 }
mjr 29:582472d0bc57 1325
mjr 29:582472d0bc57 1326 // Update the physical outputs connected to the LedWiz ports. This is
mjr 29:582472d0bc57 1327 // called after any update from an LedWiz protocol message.
mjr 1:d913e0afb2ac 1328 static void updateWizOuts()
mjr 1:d913e0afb2ac 1329 {
mjr 29:582472d0bc57 1330 // update each output
mjr 29:582472d0bc57 1331 int pulse = false;
mjr 35:e959ffba78fd 1332 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 29:582472d0bc57 1333 {
mjr 29:582472d0bc57 1334 pulse |= (wizVal[i] >= 129 && wizVal[i] <= 132);
mjr 40:cc0d9814522b 1335 lwPin[i]->set(wizState(i));
mjr 29:582472d0bc57 1336 }
mjr 29:582472d0bc57 1337
mjr 29:582472d0bc57 1338 // if any outputs are set to flashing mode, and the pulse timer
mjr 29:582472d0bc57 1339 // isn't running, turn it on
mjr 29:582472d0bc57 1340 if (pulse)
mjr 29:582472d0bc57 1341 wizPulseTimer.attach(wizPulse, WIZ_PULSE_TIME_BASE);
mjr 34:6b981a2afab7 1342
mjr 34:6b981a2afab7 1343 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 1344 if (hc595 != 0)
mjr 35:e959ffba78fd 1345 hc595->update();
mjr 1:d913e0afb2ac 1346 }
mjr 38:091e511ce8a0 1347
mjr 38:091e511ce8a0 1348 // Update all physical outputs. This is called after a change to a global
mjr 38:091e511ce8a0 1349 // setting that affects all outputs, such as engaging or canceling Night Mode.
mjr 38:091e511ce8a0 1350 static void updateAllOuts()
mjr 38:091e511ce8a0 1351 {
mjr 38:091e511ce8a0 1352 // uddate each LedWiz output
mjr 38:091e511ce8a0 1353 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 40:cc0d9814522b 1354 lwPin[i]->set(wizState(i));
mjr 34:6b981a2afab7 1355
mjr 38:091e511ce8a0 1356 // update each extended output
mjr 38:091e511ce8a0 1357 for (int i = 33 ; i < numOutputs ; ++i)
mjr 40:cc0d9814522b 1358 lwPin[i]->set(outLevel[i]);
mjr 38:091e511ce8a0 1359
mjr 38:091e511ce8a0 1360 // flush 74HC595 changes, if necessary
mjr 38:091e511ce8a0 1361 if (hc595 != 0)
mjr 38:091e511ce8a0 1362 hc595->update();
mjr 38:091e511ce8a0 1363 }
mjr 38:091e511ce8a0 1364
mjr 11:bd9da7088e6e 1365 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 1366 //
mjr 11:bd9da7088e6e 1367 // Button input
mjr 11:bd9da7088e6e 1368 //
mjr 11:bd9da7088e6e 1369
mjr 18:5e890ebd0023 1370 // button state
mjr 18:5e890ebd0023 1371 struct ButtonState
mjr 18:5e890ebd0023 1372 {
mjr 38:091e511ce8a0 1373 ButtonState()
mjr 38:091e511ce8a0 1374 {
mjr 38:091e511ce8a0 1375 di = NULL;
mjr 53:9b2611964afc 1376 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 1377 virtState = 0;
mjr 53:9b2611964afc 1378 dbState = 0;
mjr 38:091e511ce8a0 1379 pulseState = 0;
mjr 53:9b2611964afc 1380 pulseTime = 0;
mjr 38:091e511ce8a0 1381 }
mjr 35:e959ffba78fd 1382
mjr 53:9b2611964afc 1383 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 1384 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 1385 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 1386 //
mjr 53:9b2611964afc 1387 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 1388 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 1389 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 1390 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 1391 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 1392 void virtPress(bool on)
mjr 53:9b2611964afc 1393 {
mjr 53:9b2611964afc 1394 // Increment or decrement the current state
mjr 53:9b2611964afc 1395 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 1396 }
mjr 53:9b2611964afc 1397
mjr 53:9b2611964afc 1398 // DigitalIn for the button, if connected to a physical input
mjr 48:058ace2aed1d 1399 TinyDigitalIn *di;
mjr 38:091e511ce8a0 1400
mjr 38:091e511ce8a0 1401 // current PHYSICAL on/off state, after debouncing
mjr 53:9b2611964afc 1402 uint8_t physState : 1;
mjr 18:5e890ebd0023 1403
mjr 38:091e511ce8a0 1404 // current LOGICAL on/off state as reported to the host.
mjr 53:9b2611964afc 1405 uint8_t logState : 1;
mjr 38:091e511ce8a0 1406
mjr 38:091e511ce8a0 1407 // previous logical on/off state, when keys were last processed for USB
mjr 38:091e511ce8a0 1408 // reports and local effects
mjr 53:9b2611964afc 1409 uint8_t prevLogState : 1;
mjr 53:9b2611964afc 1410
mjr 53:9b2611964afc 1411 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 1412 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 1413 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 1414 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 1415 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 1416 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 1417 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 1418 // and physical source states.
mjr 53:9b2611964afc 1419 uint8_t virtState;
mjr 38:091e511ce8a0 1420
mjr 38:091e511ce8a0 1421 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 1422 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 1423 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 1424 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 1425 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 1426 uint8_t dbState;
mjr 38:091e511ce8a0 1427
mjr 38:091e511ce8a0 1428 // Pulse mode: a button in pulse mode transmits a brief logical button press and
mjr 38:091e511ce8a0 1429 // release each time the attached physical switch changes state. This is useful
mjr 38:091e511ce8a0 1430 // for cases where the host expects a key press for each change in the state of
mjr 38:091e511ce8a0 1431 // the physical switch. The canonical example is the Coin Door switch in VPinMAME,
mjr 38:091e511ce8a0 1432 // which requires pressing the END key to toggle the open/closed state. This
mjr 38:091e511ce8a0 1433 // software design isn't easily implemented in a physical coin door, though -
mjr 38:091e511ce8a0 1434 // the easiest way to sense a physical coin door's state is with a simple on/off
mjr 38:091e511ce8a0 1435 // switch. Pulse mode bridges that divide by converting a physical switch state
mjr 38:091e511ce8a0 1436 // to on/off toggle key reports to the host.
mjr 38:091e511ce8a0 1437 //
mjr 38:091e511ce8a0 1438 // Pulse state:
mjr 38:091e511ce8a0 1439 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 1440 // 1 -> off
mjr 38:091e511ce8a0 1441 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 1442 // 3 -> on
mjr 38:091e511ce8a0 1443 // 4 -> transitioning on-off
mjr 38:091e511ce8a0 1444 //
mjr 38:091e511ce8a0 1445 // Each state change sticks for a minimum period; when the timer expires,
mjr 38:091e511ce8a0 1446 // if the underlying physical switch is in a different state, we switch
mjr 53:9b2611964afc 1447 // to the next state and restart the timer. pulseTime is the time remaining
mjr 53:9b2611964afc 1448 // remaining before we can make another state transition, in microseconds.
mjr 53:9b2611964afc 1449 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 53:9b2611964afc 1450 // this guarantees that the parity of the pulse count always matches the
mjr 38:091e511ce8a0 1451 // current physical switch state when the latter is stable, which makes
mjr 38:091e511ce8a0 1452 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 38:091e511ce8a0 1453 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 38:091e511ce8a0 1454 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 38:091e511ce8a0 1455 // This software system can't be fooled that way.)
mjr 38:091e511ce8a0 1456 uint8_t pulseState;
mjr 53:9b2611964afc 1457 uint32_t pulseTime;
mjr 38:091e511ce8a0 1458
mjr 48:058ace2aed1d 1459 } __attribute__((packed)) buttonState[MAX_BUTTONS];
mjr 18:5e890ebd0023 1460
mjr 38:091e511ce8a0 1461
mjr 38:091e511ce8a0 1462 // Button data
mjr 38:091e511ce8a0 1463 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 1464
mjr 38:091e511ce8a0 1465 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 1466 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 1467 // modifier keys.
mjr 38:091e511ce8a0 1468 struct
mjr 38:091e511ce8a0 1469 {
mjr 38:091e511ce8a0 1470 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 1471 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 1472 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 1473 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 1474 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 1475
mjr 38:091e511ce8a0 1476 // Media key state
mjr 38:091e511ce8a0 1477 struct
mjr 38:091e511ce8a0 1478 {
mjr 38:091e511ce8a0 1479 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 1480 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 1481 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 1482
mjr 38:091e511ce8a0 1483 // button scan interrupt ticker
mjr 38:091e511ce8a0 1484 Ticker buttonTicker;
mjr 38:091e511ce8a0 1485
mjr 38:091e511ce8a0 1486 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 1487 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 1488 void scanButtons()
mjr 38:091e511ce8a0 1489 {
mjr 38:091e511ce8a0 1490 // scan all button input pins
mjr 38:091e511ce8a0 1491 ButtonState *bs = buttonState;
mjr 38:091e511ce8a0 1492 for (int i = 0 ; i < MAX_BUTTONS ; ++i, ++bs)
mjr 38:091e511ce8a0 1493 {
mjr 53:9b2611964afc 1494 // if this logical button is connected to a physical input, check
mjr 53:9b2611964afc 1495 // the GPIO pin state
mjr 38:091e511ce8a0 1496 if (bs->di != NULL)
mjr 38:091e511ce8a0 1497 {
mjr 38:091e511ce8a0 1498 // Shift the new state into the debounce history. Note that
mjr 38:091e511ce8a0 1499 // the physical pin inputs are active low (0V/GND = ON), so invert
mjr 38:091e511ce8a0 1500 // the reading by XOR'ing the low bit with 1. And of course we
mjr 38:091e511ce8a0 1501 // only want the low bit (since the history is effectively a bit
mjr 38:091e511ce8a0 1502 // vector), so mask the whole thing with 0x01 as well.
mjr 53:9b2611964afc 1503 uint8_t db = bs->dbState;
mjr 38:091e511ce8a0 1504 db <<= 1;
mjr 38:091e511ce8a0 1505 db |= (bs->di->read() & 0x01) ^ 0x01;
mjr 53:9b2611964afc 1506 bs->dbState = db;
mjr 38:091e511ce8a0 1507
mjr 38:091e511ce8a0 1508 // if we have all 0's or 1's in the history for the required
mjr 38:091e511ce8a0 1509 // debounce period, the key state is stable - check for a change
mjr 38:091e511ce8a0 1510 // to the last stable state
mjr 38:091e511ce8a0 1511 const uint8_t stable = 0x1F; // 00011111b -> 5 stable readings
mjr 38:091e511ce8a0 1512 db &= stable;
mjr 38:091e511ce8a0 1513 if (db == 0 || db == stable)
mjr 53:9b2611964afc 1514 bs->physState = db & 1;
mjr 38:091e511ce8a0 1515 }
mjr 38:091e511ce8a0 1516 }
mjr 38:091e511ce8a0 1517 }
mjr 38:091e511ce8a0 1518
mjr 38:091e511ce8a0 1519 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 1520 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 1521 // in the physical button state.
mjr 38:091e511ce8a0 1522 Timer buttonTimer;
mjr 12:669df364a565 1523
mjr 11:bd9da7088e6e 1524 // initialize the button inputs
mjr 35:e959ffba78fd 1525 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 1526 {
mjr 35:e959ffba78fd 1527 // presume we'll find no keyboard keys
mjr 35:e959ffba78fd 1528 kbKeys = false;
mjr 35:e959ffba78fd 1529
mjr 53:9b2611964afc 1530 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 1531 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 1532 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 1533 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 1534
mjr 53:9b2611964afc 1535 // ZB Launch Ball button
mjr 53:9b2611964afc 1536 cfg.button[ZBL_BUTTON].set(
mjr 53:9b2611964afc 1537 PINNAME_TO_WIRE(NC),
mjr 53:9b2611964afc 1538 cfg.plunger.zbLaunchBall.keytype,
mjr 53:9b2611964afc 1539 cfg.plunger.zbLaunchBall.keycode);
mjr 53:9b2611964afc 1540
mjr 11:bd9da7088e6e 1541 // create the digital inputs
mjr 35:e959ffba78fd 1542 ButtonState *bs = buttonState;
mjr 35:e959ffba78fd 1543 for (int i = 0 ; i < MAX_BUTTONS ; ++i, ++bs)
mjr 11:bd9da7088e6e 1544 {
mjr 35:e959ffba78fd 1545 PinName pin = wirePinName(cfg.button[i].pin);
mjr 35:e959ffba78fd 1546 if (pin != NC)
mjr 35:e959ffba78fd 1547 {
mjr 35:e959ffba78fd 1548 // set up the GPIO input pin for this button
mjr 48:058ace2aed1d 1549 bs->di = new TinyDigitalIn(pin);
mjr 35:e959ffba78fd 1550
mjr 38:091e511ce8a0 1551 // if it's a pulse mode button, set the initial pulse state to Off
mjr 38:091e511ce8a0 1552 if (cfg.button[i].flags & BtnFlagPulse)
mjr 38:091e511ce8a0 1553 bs->pulseState = 1;
mjr 38:091e511ce8a0 1554
mjr 53:9b2611964afc 1555 // Note if it's a keyboard key of some kind. If we find any keyboard
mjr 53:9b2611964afc 1556 // mappings, we'll declare a keyboard interface when we send our HID
mjr 53:9b2611964afc 1557 // configuration to the host during USB connection setup.
mjr 35:e959ffba78fd 1558 switch (cfg.button[i].typ)
mjr 35:e959ffba78fd 1559 {
mjr 35:e959ffba78fd 1560 case BtnTypeKey:
mjr 53:9b2611964afc 1561 // note that we have at least one keyboard key
mjr 35:e959ffba78fd 1562 kbKeys = true;
mjr 35:e959ffba78fd 1563 break;
mjr 35:e959ffba78fd 1564
mjr 53:9b2611964afc 1565 default:
mjr 53:9b2611964afc 1566 // not a keyboard key
mjr 39:b3815a1c3802 1567 break;
mjr 35:e959ffba78fd 1568 }
mjr 35:e959ffba78fd 1569 }
mjr 11:bd9da7088e6e 1570 }
mjr 12:669df364a565 1571
mjr 53:9b2611964afc 1572 // If the ZB Launch Ball feature is enabled, and it uses a keyboard
mjr 53:9b2611964afc 1573 // key, this requires setting up a USB keyboard interface.
mjr 53:9b2611964afc 1574 if (cfg.plunger.zbLaunchBall.port != 0
mjr 53:9b2611964afc 1575 && cfg.plunger.zbLaunchBall.keytype == BtnTypeKey)
mjr 53:9b2611964afc 1576 kbKeys = true;
mjr 53:9b2611964afc 1577
mjr 38:091e511ce8a0 1578 // start the button scan thread
mjr 38:091e511ce8a0 1579 buttonTicker.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 1580
mjr 38:091e511ce8a0 1581 // start the button state transition timer
mjr 12:669df364a565 1582 buttonTimer.start();
mjr 11:bd9da7088e6e 1583 }
mjr 11:bd9da7088e6e 1584
mjr 38:091e511ce8a0 1585 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 1586 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 1587 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 1588 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 1589 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 1590 {
mjr 35:e959ffba78fd 1591 // start with an empty list of USB key codes
mjr 35:e959ffba78fd 1592 uint8_t modkeys = 0;
mjr 35:e959ffba78fd 1593 uint8_t keys[7] = { 0, 0, 0, 0, 0, 0, 0 };
mjr 35:e959ffba78fd 1594 int nkeys = 0;
mjr 11:bd9da7088e6e 1595
mjr 35:e959ffba78fd 1596 // clear the joystick buttons
mjr 36:b9747461331e 1597 uint32_t newjs = 0;
mjr 35:e959ffba78fd 1598
mjr 35:e959ffba78fd 1599 // start with no media keys pressed
mjr 35:e959ffba78fd 1600 uint8_t mediakeys = 0;
mjr 38:091e511ce8a0 1601
mjr 38:091e511ce8a0 1602 // calculate the time since the last run
mjr 53:9b2611964afc 1603 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 1604 buttonTimer.reset();
mjr 38:091e511ce8a0 1605
mjr 11:bd9da7088e6e 1606 // scan the button list
mjr 18:5e890ebd0023 1607 ButtonState *bs = buttonState;
mjr 53:9b2611964afc 1608 ButtonCfg *bc = cfg.button;
mjr 53:9b2611964afc 1609 for (int i = 0 ; i < MAX_BUTTONS ; ++i, ++bs, ++bc)
mjr 11:bd9da7088e6e 1610 {
mjr 38:091e511ce8a0 1611 // if it's a pulse-mode switch, get the virtual pressed state
mjr 38:091e511ce8a0 1612 if (bs->pulseState != 0)
mjr 18:5e890ebd0023 1613 {
mjr 38:091e511ce8a0 1614 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 1615 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 1616 {
mjr 53:9b2611964afc 1617 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 1618 bs->pulseTime -= dt;
mjr 53:9b2611964afc 1619 }
mjr 53:9b2611964afc 1620 else
mjr 53:9b2611964afc 1621 {
mjr 53:9b2611964afc 1622 // pulse time expired - check for a state change
mjr 53:9b2611964afc 1623 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 1624 switch (bs->pulseState)
mjr 18:5e890ebd0023 1625 {
mjr 38:091e511ce8a0 1626 case 1:
mjr 38:091e511ce8a0 1627 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 1628 if (bs->physState)
mjr 53:9b2611964afc 1629 {
mjr 38:091e511ce8a0 1630 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1631 bs->pulseState = 2;
mjr 53:9b2611964afc 1632 bs->logState = 1;
mjr 38:091e511ce8a0 1633 }
mjr 38:091e511ce8a0 1634 break;
mjr 18:5e890ebd0023 1635
mjr 38:091e511ce8a0 1636 case 2:
mjr 38:091e511ce8a0 1637 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 1638 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 1639 // change in state in the logical button
mjr 38:091e511ce8a0 1640 bs->pulseState = 3;
mjr 38:091e511ce8a0 1641 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 1642 bs->logState = 0;
mjr 38:091e511ce8a0 1643 break;
mjr 38:091e511ce8a0 1644
mjr 38:091e511ce8a0 1645 case 3:
mjr 38:091e511ce8a0 1646 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 1647 if (!bs->physState)
mjr 53:9b2611964afc 1648 {
mjr 38:091e511ce8a0 1649 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1650 bs->pulseState = 4;
mjr 53:9b2611964afc 1651 bs->logState = 1;
mjr 38:091e511ce8a0 1652 }
mjr 38:091e511ce8a0 1653 break;
mjr 38:091e511ce8a0 1654
mjr 38:091e511ce8a0 1655 case 4:
mjr 38:091e511ce8a0 1656 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 1657 bs->pulseState = 1;
mjr 38:091e511ce8a0 1658 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 1659 bs->logState = 0;
mjr 38:091e511ce8a0 1660 break;
mjr 18:5e890ebd0023 1661 }
mjr 18:5e890ebd0023 1662 }
mjr 38:091e511ce8a0 1663 }
mjr 38:091e511ce8a0 1664 else
mjr 38:091e511ce8a0 1665 {
mjr 38:091e511ce8a0 1666 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 1667 bs->logState = bs->physState;
mjr 38:091e511ce8a0 1668 }
mjr 35:e959ffba78fd 1669
mjr 38:091e511ce8a0 1670 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 1671 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 1672 {
mjr 38:091e511ce8a0 1673 // check for special key transitions
mjr 53:9b2611964afc 1674 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 1675 {
mjr 53:9b2611964afc 1676 // Check the switch type in the config flags. If flag 0x01 is set,
mjr 53:9b2611964afc 1677 // it's a persistent on/off switch, so the night mode state simply
mjr 53:9b2611964afc 1678 // follows the current state of the switch. Otherwise, it's a
mjr 53:9b2611964afc 1679 // momentary button, so each button push (i.e., each transition from
mjr 53:9b2611964afc 1680 // logical state OFF to ON) toggles the current night mode state.
mjr 53:9b2611964afc 1681 if (cfg.nightMode.flags & 0x01)
mjr 53:9b2611964afc 1682 {
mjr 53:9b2611964afc 1683 // toggle switch - when the button changes state, change
mjr 53:9b2611964afc 1684 // night mode to match the new state
mjr 53:9b2611964afc 1685 setNightMode(bs->logState);
mjr 53:9b2611964afc 1686 }
mjr 53:9b2611964afc 1687 else
mjr 53:9b2611964afc 1688 {
mjr 53:9b2611964afc 1689 // momentary switch - toggle the night mode state when the
mjr 53:9b2611964afc 1690 // physical button is pushed (i.e., when its logical state
mjr 53:9b2611964afc 1691 // transitions from OFF to ON)
mjr 53:9b2611964afc 1692 if (bs->logState)
mjr 53:9b2611964afc 1693 toggleNightMode();
mjr 53:9b2611964afc 1694 }
mjr 35:e959ffba78fd 1695 }
mjr 38:091e511ce8a0 1696
mjr 38:091e511ce8a0 1697 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 1698 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 1699 }
mjr 38:091e511ce8a0 1700
mjr 53:9b2611964afc 1701 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 1702 // key state list
mjr 53:9b2611964afc 1703 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 1704 {
mjr 38:091e511ce8a0 1705 // OR in the joystick button bit, mod key bits, and media key bits
mjr 53:9b2611964afc 1706 uint8_t val = bc->val;
mjr 53:9b2611964afc 1707 switch (bc->typ)
mjr 53:9b2611964afc 1708 {
mjr 53:9b2611964afc 1709 case BtnTypeJoystick:
mjr 53:9b2611964afc 1710 // joystick button
mjr 53:9b2611964afc 1711 newjs |= (1 << (val - 1));
mjr 53:9b2611964afc 1712 break;
mjr 53:9b2611964afc 1713
mjr 53:9b2611964afc 1714 case BtnTypeKey:
mjr 53:9b2611964afc 1715 // Keyboard key. This could be a modifier key (shift, control,
mjr 53:9b2611964afc 1716 // alt, GUI), a media key (mute, volume up, volume down), or a
mjr 53:9b2611964afc 1717 // regular key. Check which one.
mjr 53:9b2611964afc 1718 if (val >= 0x7F && val <= 0x81)
mjr 53:9b2611964afc 1719 {
mjr 53:9b2611964afc 1720 // It's a media key. OR the key into the media key mask.
mjr 53:9b2611964afc 1721 // The media mask bits are mapped in the HID report descriptor
mjr 53:9b2611964afc 1722 // in USBJoystick.cpp. For simplicity, we arrange the mask so
mjr 53:9b2611964afc 1723 // that the ones with regular keyboard equivalents that we catch
mjr 53:9b2611964afc 1724 // here are in the same order as the key scan codes:
mjr 53:9b2611964afc 1725 //
mjr 53:9b2611964afc 1726 // Mute = scan 0x7F = mask bit 0x01
mjr 53:9b2611964afc 1727 // Vol Up = scan 0x80 = mask bit 0x02
mjr 53:9b2611964afc 1728 // Vol Down = scan 0x81 = mask bit 0x04
mjr 53:9b2611964afc 1729 //
mjr 53:9b2611964afc 1730 // So we can translate from scan code to bit mask with some
mjr 53:9b2611964afc 1731 // simple bit shifting:
mjr 53:9b2611964afc 1732 mediakeys |= (1 << (val - 0x7f));
mjr 53:9b2611964afc 1733 }
mjr 53:9b2611964afc 1734 else if (val >= 0xE0 && val <= 0xE7)
mjr 53:9b2611964afc 1735 {
mjr 53:9b2611964afc 1736 // It's a modifier key. Like the media keys, these are represented
mjr 53:9b2611964afc 1737 // in the USB reports with a bit mask, and like the media keys, we
mjr 53:9b2611964afc 1738 // arrange the mask bits in the same order as the scan codes. This
mjr 53:9b2611964afc 1739 // makes figuring the mask a simple bit shift:
mjr 53:9b2611964afc 1740 modkeys |= (1 << (val - 0xE0));
mjr 53:9b2611964afc 1741 }
mjr 53:9b2611964afc 1742 else
mjr 53:9b2611964afc 1743 {
mjr 53:9b2611964afc 1744 // It's a regular key. Make sure it's not already in the list, and
mjr 53:9b2611964afc 1745 // that the list isn't full. If neither of these apply, add the key.
mjr 53:9b2611964afc 1746 if (nkeys < 7)
mjr 53:9b2611964afc 1747 {
mjr 57:cc03231f676b 1748 bool found = false;
mjr 53:9b2611964afc 1749 for (int j = 0 ; j < nkeys ; ++j)
mjr 53:9b2611964afc 1750 {
mjr 53:9b2611964afc 1751 if (keys[j] == val)
mjr 53:9b2611964afc 1752 {
mjr 53:9b2611964afc 1753 found = true;
mjr 53:9b2611964afc 1754 break;
mjr 53:9b2611964afc 1755 }
mjr 53:9b2611964afc 1756 }
mjr 53:9b2611964afc 1757 if (!found)
mjr 53:9b2611964afc 1758 keys[nkeys++] = val;
mjr 53:9b2611964afc 1759 }
mjr 53:9b2611964afc 1760 }
mjr 53:9b2611964afc 1761 break;
mjr 53:9b2611964afc 1762 }
mjr 18:5e890ebd0023 1763 }
mjr 11:bd9da7088e6e 1764 }
mjr 36:b9747461331e 1765
mjr 36:b9747461331e 1766 // check for joystick button changes
mjr 36:b9747461331e 1767 if (jsButtons != newjs)
mjr 36:b9747461331e 1768 jsButtons = newjs;
mjr 11:bd9da7088e6e 1769
mjr 35:e959ffba78fd 1770 // Check for changes to the keyboard keys
mjr 35:e959ffba78fd 1771 if (kbState.data[0] != modkeys
mjr 35:e959ffba78fd 1772 || kbState.nkeys != nkeys
mjr 35:e959ffba78fd 1773 || memcmp(keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 1774 {
mjr 35:e959ffba78fd 1775 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 1776 kbState.changed = true;
mjr 35:e959ffba78fd 1777 kbState.data[0] = modkeys;
mjr 35:e959ffba78fd 1778 if (nkeys <= 6) {
mjr 35:e959ffba78fd 1779 // 6 or fewer simultaneous keys - report the key codes
mjr 35:e959ffba78fd 1780 kbState.nkeys = nkeys;
mjr 35:e959ffba78fd 1781 memcpy(&kbState.data[2], keys, 6);
mjr 35:e959ffba78fd 1782 }
mjr 35:e959ffba78fd 1783 else {
mjr 35:e959ffba78fd 1784 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 1785 kbState.nkeys = 6;
mjr 35:e959ffba78fd 1786 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 1787 }
mjr 35:e959ffba78fd 1788 }
mjr 35:e959ffba78fd 1789
mjr 35:e959ffba78fd 1790 // Check for changes to media keys
mjr 35:e959ffba78fd 1791 if (mediaState.data != mediakeys)
mjr 35:e959ffba78fd 1792 {
mjr 35:e959ffba78fd 1793 mediaState.changed = true;
mjr 35:e959ffba78fd 1794 mediaState.data = mediakeys;
mjr 35:e959ffba78fd 1795 }
mjr 11:bd9da7088e6e 1796 }
mjr 11:bd9da7088e6e 1797
mjr 5:a70c0bce770d 1798 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1799 //
mjr 5:a70c0bce770d 1800 // Customization joystick subbclass
mjr 5:a70c0bce770d 1801 //
mjr 5:a70c0bce770d 1802
mjr 5:a70c0bce770d 1803 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 1804 {
mjr 5:a70c0bce770d 1805 public:
mjr 35:e959ffba78fd 1806 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 35:e959ffba78fd 1807 bool waitForConnect, bool enableJoystick, bool useKB)
mjr 35:e959ffba78fd 1808 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, useKB)
mjr 5:a70c0bce770d 1809 {
mjr 54:fd77a6b2f76c 1810 sleeping_ = false;
mjr 54:fd77a6b2f76c 1811 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 1812 timer_.start();
mjr 54:fd77a6b2f76c 1813 }
mjr 54:fd77a6b2f76c 1814
mjr 54:fd77a6b2f76c 1815 // show diagnostic LED feedback for connect state
mjr 54:fd77a6b2f76c 1816 void diagFlash()
mjr 54:fd77a6b2f76c 1817 {
mjr 54:fd77a6b2f76c 1818 if (!configured() || sleeping_)
mjr 54:fd77a6b2f76c 1819 {
mjr 54:fd77a6b2f76c 1820 // flash once if sleeping or twice if disconnected
mjr 54:fd77a6b2f76c 1821 for (int j = isConnected() ? 1 : 2 ; j > 0 ; --j)
mjr 54:fd77a6b2f76c 1822 {
mjr 54:fd77a6b2f76c 1823 // short red flash
mjr 54:fd77a6b2f76c 1824 diagLED(1, 0, 0);
mjr 54:fd77a6b2f76c 1825 wait_us(50000);
mjr 54:fd77a6b2f76c 1826 diagLED(0, 0, 0);
mjr 54:fd77a6b2f76c 1827 wait_us(50000);
mjr 54:fd77a6b2f76c 1828 }
mjr 54:fd77a6b2f76c 1829 }
mjr 5:a70c0bce770d 1830 }
mjr 5:a70c0bce770d 1831
mjr 5:a70c0bce770d 1832 // are we connected?
mjr 5:a70c0bce770d 1833 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 1834
mjr 54:fd77a6b2f76c 1835 // Are we in sleep mode? If true, this means that the hardware has
mjr 54:fd77a6b2f76c 1836 // detected no activity on the bus for 3ms. This happens when the
mjr 54:fd77a6b2f76c 1837 // cable is physically disconnected, the computer is turned off, or
mjr 54:fd77a6b2f76c 1838 // the connection is otherwise disabled.
mjr 54:fd77a6b2f76c 1839 bool isSleeping() const { return sleeping_; }
mjr 54:fd77a6b2f76c 1840
mjr 54:fd77a6b2f76c 1841 // If necessary, attempt to recover from a broken connection.
mjr 54:fd77a6b2f76c 1842 //
mjr 54:fd77a6b2f76c 1843 // This is a hack, to work around an apparent timing bug in the
mjr 54:fd77a6b2f76c 1844 // KL25Z USB implementation that I haven't been able to solve any
mjr 54:fd77a6b2f76c 1845 // other way.
mjr 54:fd77a6b2f76c 1846 //
mjr 54:fd77a6b2f76c 1847 // The issue: when we have an established connection, and the
mjr 54:fd77a6b2f76c 1848 // connection is broken by physically unplugging the cable or by
mjr 54:fd77a6b2f76c 1849 // rebooting the PC, the KL25Z sometimes fails to reconnect when
mjr 54:fd77a6b2f76c 1850 // the physical connection is re-established. The failure is
mjr 54:fd77a6b2f76c 1851 // sporadic; I'd guess it happens about 25% of the time, but I
mjr 54:fd77a6b2f76c 1852 // haven't collected any real statistics on it.
mjr 54:fd77a6b2f76c 1853 //
mjr 54:fd77a6b2f76c 1854 // The proximate cause of the failure is a deadlock in the SETUP
mjr 54:fd77a6b2f76c 1855 // protocol between the host and device that happens around the
mjr 54:fd77a6b2f76c 1856 // point where the PC is requesting the configuration descriptor.
mjr 54:fd77a6b2f76c 1857 // The exact point in the protocol where this occurs varies slightly;
mjr 54:fd77a6b2f76c 1858 // it can occur a message or two before or after the Get Config
mjr 54:fd77a6b2f76c 1859 // Descriptor packet. No matter where it happens, the nature of
mjr 54:fd77a6b2f76c 1860 // the deadlock is the same: the PC thinks it sees a STALL on EP0
mjr 54:fd77a6b2f76c 1861 // from the device, so it terminates the connection attempt, which
mjr 54:fd77a6b2f76c 1862 // stops further traffic on the cable. The KL25Z USB hardware sees
mjr 54:fd77a6b2f76c 1863 // the lack of traffic and triggers a SLEEP interrupt (a misnomer
mjr 54:fd77a6b2f76c 1864 // for what should have been called a BROKEN CONNECTION interrupt).
mjr 54:fd77a6b2f76c 1865 // Both sides simply stop talking at this point, so the connection
mjr 54:fd77a6b2f76c 1866 // is effectively dead.
mjr 54:fd77a6b2f76c 1867 //
mjr 54:fd77a6b2f76c 1868 // The strange thing is that, as far as I can tell, the KL25Z isn't
mjr 54:fd77a6b2f76c 1869 // doing anything to trigger the STALL on its end. Both the PC
mjr 54:fd77a6b2f76c 1870 // and the KL25Z are happy up until the very point of the failure
mjr 54:fd77a6b2f76c 1871 // and show no signs of anything wrong in the protocol exchange.
mjr 54:fd77a6b2f76c 1872 // In fact, every detail of the protocol exchange up to this point
mjr 54:fd77a6b2f76c 1873 // is identical to every successful exchange that does finish the
mjr 54:fd77a6b2f76c 1874 // whole setup process successfully, on both the KL25Z and Windows
mjr 54:fd77a6b2f76c 1875 // sides of the connection. I can't find any point of difference
mjr 54:fd77a6b2f76c 1876 // between successful and unsuccessful sequences that suggests why
mjr 54:fd77a6b2f76c 1877 // the fateful message fails. This makes me suspect that whatever
mjr 54:fd77a6b2f76c 1878 // is going wrong is inside the KL25Z USB hardware module, which
mjr 54:fd77a6b2f76c 1879 // is a pretty substantial black box - it has a lot of internal
mjr 54:fd77a6b2f76c 1880 // state that's inaccessible to the software. Further bolstering
mjr 54:fd77a6b2f76c 1881 // this theory is a little experiment where I found that I could
mjr 54:fd77a6b2f76c 1882 // reproduce the exact sequence of events of a failed reconnect
mjr 54:fd77a6b2f76c 1883 // attempt in an *initial* connection, which is otherwise 100%
mjr 54:fd77a6b2f76c 1884 // reliable, by inserting a little bit of artifical time padding
mjr 54:fd77a6b2f76c 1885 // (200us per event) into the SETUP interrupt handler. My
mjr 54:fd77a6b2f76c 1886 // hypothesis is that the STALL event happens because the KL25Z
mjr 54:fd77a6b2f76c 1887 // USB hardware is too slow to respond to a message. I'm not
mjr 54:fd77a6b2f76c 1888 // sure why this would only happen after a disconnect and not
mjr 54:fd77a6b2f76c 1889 // during the initial connection; maybe there's some reset work
mjr 54:fd77a6b2f76c 1890 // in the hardware that takes a substantial amount of time after
mjr 54:fd77a6b2f76c 1891 // a disconnect.
mjr 54:fd77a6b2f76c 1892 //
mjr 54:fd77a6b2f76c 1893 // The solution: the problem happens during the SETUP exchange,
mjr 54:fd77a6b2f76c 1894 // after we've been assigned a bus address. It only happens on
mjr 54:fd77a6b2f76c 1895 // some percentage of connection requests, so if we can simply
mjr 54:fd77a6b2f76c 1896 // start over when the failure occurs, we'll eventually succeed
mjr 54:fd77a6b2f76c 1897 // simply because not every attempt fails. The ideal would be
mjr 54:fd77a6b2f76c 1898 // to get the success rate up to 100%, but I can't figure out how
mjr 54:fd77a6b2f76c 1899 // to fix the underlying problem, so this is the next best thing.
mjr 54:fd77a6b2f76c 1900 //
mjr 54:fd77a6b2f76c 1901 // We can detect when the failure occurs by noticing when a SLEEP
mjr 54:fd77a6b2f76c 1902 // interrupt happens while we have an assigned bus address.
mjr 54:fd77a6b2f76c 1903 //
mjr 54:fd77a6b2f76c 1904 // To start a new connection attempt, we have to make the *host*
mjr 54:fd77a6b2f76c 1905 // try again. The logical connection is initiated solely by the
mjr 54:fd77a6b2f76c 1906 // host. Fortunately, it's easy to get the host to initiate the
mjr 54:fd77a6b2f76c 1907 // process: if we disconnect on the device side, it effectively
mjr 54:fd77a6b2f76c 1908 // makes the device look to the PC like it's electrically unplugged.
mjr 54:fd77a6b2f76c 1909 // When we reconnect on the device side, the PC thinks a new device
mjr 54:fd77a6b2f76c 1910 // has been plugged in and initiates the logical connection setup.
mjr 54:fd77a6b2f76c 1911 // We have to remain disconnected for a macroscopic interval for
mjr 54:fd77a6b2f76c 1912 // this to happen - 5ms seems to do the trick.
mjr 54:fd77a6b2f76c 1913 //
mjr 54:fd77a6b2f76c 1914 // Here's the full algorithm:
mjr 54:fd77a6b2f76c 1915 //
mjr 54:fd77a6b2f76c 1916 // 1. In the SLEEP interrupt handler, if we have a bus address,
mjr 54:fd77a6b2f76c 1917 // we disconnect the device. This happens in ISR context, so we
mjr 54:fd77a6b2f76c 1918 // can't wait around for 5ms. Instead, we simply set a flag noting
mjr 54:fd77a6b2f76c 1919 // that the connection has been broken, and we note the time and
mjr 54:fd77a6b2f76c 1920 // return.
mjr 54:fd77a6b2f76c 1921 //
mjr 54:fd77a6b2f76c 1922 // 2. In our main loop, whenever we find that we're disconnected,
mjr 54:fd77a6b2f76c 1923 // we call recoverConnection(). The main loop's job is basically a
mjr 54:fd77a6b2f76c 1924 // bunch of device polling. We're just one more device to poll, so
mjr 54:fd77a6b2f76c 1925 // recoverConnection() will be called soon after a disconnect, and
mjr 54:fd77a6b2f76c 1926 // then will be called in a loop for as long as we're disconnected.
mjr 54:fd77a6b2f76c 1927 //
mjr 54:fd77a6b2f76c 1928 // 3. In recoverConnection(), we check the flag we set in the SLEEP
mjr 54:fd77a6b2f76c 1929 // handler. If set, we wait until 5ms has elapsed from the SLEEP
mjr 54:fd77a6b2f76c 1930 // event time that we noted, then we'll reconnect and clear the flag.
mjr 54:fd77a6b2f76c 1931 // This gives us the required 5ms (or longer) delay between the
mjr 54:fd77a6b2f76c 1932 // disconnect and reconnect, ensuring that the PC will notice and
mjr 54:fd77a6b2f76c 1933 // will start over with the connection protocol.
mjr 54:fd77a6b2f76c 1934 //
mjr 54:fd77a6b2f76c 1935 // 4. The main loop keeps calling recoverConnection() in a loop for
mjr 54:fd77a6b2f76c 1936 // as long as we're disconnected, so if the new connection attempt
mjr 54:fd77a6b2f76c 1937 // triggered in step 3 fails, the SLEEP interrupt will happen again,
mjr 54:fd77a6b2f76c 1938 // we'll disconnect again, the flag will get set again, and
mjr 54:fd77a6b2f76c 1939 // recoverConnection() will reconnect again after another suitable
mjr 54:fd77a6b2f76c 1940 // delay. This will repeat until the connection succeeds or hell
mjr 54:fd77a6b2f76c 1941 // freezes over.
mjr 54:fd77a6b2f76c 1942 //
mjr 54:fd77a6b2f76c 1943 // Each disconnect happens immediately when a reconnect attempt
mjr 54:fd77a6b2f76c 1944 // fails, and an entire successful connection only takes about 25ms,
mjr 54:fd77a6b2f76c 1945 // so our loop can retry at more than 30 attempts per second.
mjr 54:fd77a6b2f76c 1946 // In my testing, lost connections almost always reconnect in
mjr 54:fd77a6b2f76c 1947 // less than second with this code in place.
mjr 54:fd77a6b2f76c 1948 void recoverConnection()
mjr 54:fd77a6b2f76c 1949 {
mjr 54:fd77a6b2f76c 1950 // if a reconnect is pending, reconnect
mjr 54:fd77a6b2f76c 1951 if (reconnectPending_)
mjr 54:fd77a6b2f76c 1952 {
mjr 54:fd77a6b2f76c 1953 // Loop until we reach 5ms after the last sleep event.
mjr 54:fd77a6b2f76c 1954 for (bool done = false ; !done ; )
mjr 54:fd77a6b2f76c 1955 {
mjr 54:fd77a6b2f76c 1956 // If we've reached the target time, reconnect. Do the
mjr 54:fd77a6b2f76c 1957 // time check and flag reset atomically, so that we can't
mjr 54:fd77a6b2f76c 1958 // have another sleep event sneak in after we've verified
mjr 54:fd77a6b2f76c 1959 // the time. If another event occurs, it has to happen
mjr 54:fd77a6b2f76c 1960 // before we check, in which case it'll update the time
mjr 54:fd77a6b2f76c 1961 // before we check it, or after we clear the flag, in
mjr 54:fd77a6b2f76c 1962 // which case it will reset the flag and we'll do another
mjr 54:fd77a6b2f76c 1963 // round the next time we call this routine.
mjr 54:fd77a6b2f76c 1964 __disable_irq();
mjr 54:fd77a6b2f76c 1965 if (uint32_t(timer_.read_us() - lastSleepTime_) > 5000)
mjr 54:fd77a6b2f76c 1966 {
mjr 54:fd77a6b2f76c 1967 connect(false);
mjr 54:fd77a6b2f76c 1968 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 1969 done = true;
mjr 54:fd77a6b2f76c 1970 }
mjr 54:fd77a6b2f76c 1971 __enable_irq();
mjr 54:fd77a6b2f76c 1972 }
mjr 54:fd77a6b2f76c 1973 }
mjr 54:fd77a6b2f76c 1974 }
mjr 5:a70c0bce770d 1975
mjr 5:a70c0bce770d 1976 protected:
mjr 54:fd77a6b2f76c 1977 // Handle a USB SLEEP interrupt. This interrupt signifies that the
mjr 54:fd77a6b2f76c 1978 // USB hardware module hasn't seen any token traffic for 3ms, which
mjr 54:fd77a6b2f76c 1979 // means that we're either physically or logically disconnected.
mjr 54:fd77a6b2f76c 1980 //
mjr 54:fd77a6b2f76c 1981 // Important: this runs in ISR context.
mjr 54:fd77a6b2f76c 1982 //
mjr 54:fd77a6b2f76c 1983 // Note that this is a specialized sense of "sleep" that's unrelated
mjr 54:fd77a6b2f76c 1984 // to the similarly named power modes on the PC. This has nothing
mjr 54:fd77a6b2f76c 1985 // to do with suspend/sleep mode on the PC, and it's not a low-power
mjr 54:fd77a6b2f76c 1986 // mode on the KL25Z. They really should have called this interrupt
mjr 54:fd77a6b2f76c 1987 // DISCONNECT or BROKEN CONNECTION.)
mjr 54:fd77a6b2f76c 1988 virtual void sleepStateChanged(unsigned int sleeping)
mjr 54:fd77a6b2f76c 1989 {
mjr 54:fd77a6b2f76c 1990 // note the new state
mjr 54:fd77a6b2f76c 1991 sleeping_ = sleeping;
mjr 54:fd77a6b2f76c 1992
mjr 54:fd77a6b2f76c 1993 // If we have a non-zero bus address, we have at least a partial
mjr 54:fd77a6b2f76c 1994 // connection to the host (we've made it at least as far as the
mjr 54:fd77a6b2f76c 1995 // SETUP stage). Explicitly disconnect, and the pending reconnect
mjr 54:fd77a6b2f76c 1996 // flag, and remember the time of the sleep event.
mjr 54:fd77a6b2f76c 1997 if (USB0->ADDR != 0x00)
mjr 54:fd77a6b2f76c 1998 {
mjr 54:fd77a6b2f76c 1999 disconnect();
mjr 54:fd77a6b2f76c 2000 lastSleepTime_ = timer_.read_us();
mjr 54:fd77a6b2f76c 2001 reconnectPending_ = true;
mjr 54:fd77a6b2f76c 2002 }
mjr 54:fd77a6b2f76c 2003 }
mjr 54:fd77a6b2f76c 2004
mjr 54:fd77a6b2f76c 2005 // is the USB connection asleep?
mjr 54:fd77a6b2f76c 2006 volatile bool sleeping_;
mjr 54:fd77a6b2f76c 2007
mjr 54:fd77a6b2f76c 2008 // flag: reconnect pending after sleep event
mjr 54:fd77a6b2f76c 2009 volatile bool reconnectPending_;
mjr 54:fd77a6b2f76c 2010
mjr 54:fd77a6b2f76c 2011 // time of last sleep event while connected
mjr 54:fd77a6b2f76c 2012 volatile uint32_t lastSleepTime_;
mjr 54:fd77a6b2f76c 2013
mjr 54:fd77a6b2f76c 2014 // timer to keep track of interval since last sleep event
mjr 54:fd77a6b2f76c 2015 Timer timer_;
mjr 5:a70c0bce770d 2016 };
mjr 5:a70c0bce770d 2017
mjr 5:a70c0bce770d 2018 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 2019 //
mjr 5:a70c0bce770d 2020 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 2021 //
mjr 5:a70c0bce770d 2022
mjr 5:a70c0bce770d 2023 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 2024 //
mjr 5:a70c0bce770d 2025 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 2026 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 2027 // automatic calibration.
mjr 5:a70c0bce770d 2028 //
mjr 5:a70c0bce770d 2029 // We install an interrupt handler on the accelerometer "data ready"
mjr 6:cc35eb643e8f 2030 // interrupt to ensure that we fetch each sample immediately when it
mjr 6:cc35eb643e8f 2031 // becomes available. The accelerometer data rate is fiarly high
mjr 6:cc35eb643e8f 2032 // (800 Hz), so it's not practical to keep up with it by polling.
mjr 6:cc35eb643e8f 2033 // Using an interrupt handler lets us respond quickly and read
mjr 6:cc35eb643e8f 2034 // every sample.
mjr 5:a70c0bce770d 2035 //
mjr 6:cc35eb643e8f 2036 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 2037 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 2038 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 2039 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 2040 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 2041 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 2042 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 2043 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 2044 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 2045 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 2046 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 2047 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 2048 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 2049 // of nudging, say).
mjr 5:a70c0bce770d 2050 //
mjr 5:a70c0bce770d 2051
mjr 17:ab3cec0c8bf4 2052 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 2053 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 2054
mjr 17:ab3cec0c8bf4 2055 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 2056 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 2057 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 2058
mjr 17:ab3cec0c8bf4 2059 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 2060 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 2061 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 2062 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 2063
mjr 17:ab3cec0c8bf4 2064
mjr 6:cc35eb643e8f 2065 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 2066 struct AccHist
mjr 5:a70c0bce770d 2067 {
mjr 6:cc35eb643e8f 2068 AccHist() { x = y = d = 0.0; xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 2069 void set(float x, float y, AccHist *prv)
mjr 6:cc35eb643e8f 2070 {
mjr 6:cc35eb643e8f 2071 // save the raw position
mjr 6:cc35eb643e8f 2072 this->x = x;
mjr 6:cc35eb643e8f 2073 this->y = y;
mjr 6:cc35eb643e8f 2074 this->d = distance(prv);
mjr 6:cc35eb643e8f 2075 }
mjr 6:cc35eb643e8f 2076
mjr 6:cc35eb643e8f 2077 // reading for this entry
mjr 5:a70c0bce770d 2078 float x, y;
mjr 5:a70c0bce770d 2079
mjr 6:cc35eb643e8f 2080 // distance from previous entry
mjr 6:cc35eb643e8f 2081 float d;
mjr 5:a70c0bce770d 2082
mjr 6:cc35eb643e8f 2083 // total and count of samples averaged over this period
mjr 6:cc35eb643e8f 2084 float xtot, ytot;
mjr 6:cc35eb643e8f 2085 int cnt;
mjr 6:cc35eb643e8f 2086
mjr 6:cc35eb643e8f 2087 void clearAvg() { xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 2088 void addAvg(float x, float y) { xtot += x; ytot += y; ++cnt; }
mjr 6:cc35eb643e8f 2089 float xAvg() const { return xtot/cnt; }
mjr 6:cc35eb643e8f 2090 float yAvg() const { return ytot/cnt; }
mjr 5:a70c0bce770d 2091
mjr 6:cc35eb643e8f 2092 float distance(AccHist *p)
mjr 6:cc35eb643e8f 2093 { return sqrt(square(p->x - x) + square(p->y - y)); }
mjr 5:a70c0bce770d 2094 };
mjr 5:a70c0bce770d 2095
mjr 5:a70c0bce770d 2096 // accelerometer wrapper class
mjr 3:3514575d4f86 2097 class Accel
mjr 3:3514575d4f86 2098 {
mjr 3:3514575d4f86 2099 public:
mjr 3:3514575d4f86 2100 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin)
mjr 3:3514575d4f86 2101 : mma_(sda, scl, i2cAddr), intIn_(irqPin)
mjr 3:3514575d4f86 2102 {
mjr 5:a70c0bce770d 2103 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 2104 irqPin_ = irqPin;
mjr 5:a70c0bce770d 2105
mjr 5:a70c0bce770d 2106 // reset and initialize
mjr 5:a70c0bce770d 2107 reset();
mjr 5:a70c0bce770d 2108 }
mjr 5:a70c0bce770d 2109
mjr 5:a70c0bce770d 2110 void reset()
mjr 5:a70c0bce770d 2111 {
mjr 6:cc35eb643e8f 2112 // clear the center point
mjr 6:cc35eb643e8f 2113 cx_ = cy_ = 0.0;
mjr 6:cc35eb643e8f 2114
mjr 6:cc35eb643e8f 2115 // start the calibration timer
mjr 5:a70c0bce770d 2116 tCenter_.start();
mjr 5:a70c0bce770d 2117 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 2118
mjr 5:a70c0bce770d 2119 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 2120 mma_.init();
mjr 6:cc35eb643e8f 2121
mjr 6:cc35eb643e8f 2122 // set the initial integrated velocity reading to zero
mjr 6:cc35eb643e8f 2123 vx_ = vy_ = 0;
mjr 3:3514575d4f86 2124
mjr 6:cc35eb643e8f 2125 // set up our accelerometer interrupt handling
mjr 6:cc35eb643e8f 2126 intIn_.rise(this, &Accel::isr);
mjr 5:a70c0bce770d 2127 mma_.setInterruptMode(irqPin_ == PTA14 ? 1 : 2);
mjr 3:3514575d4f86 2128
mjr 3:3514575d4f86 2129 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 2130 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 2131
mjr 3:3514575d4f86 2132 // start our timers
mjr 3:3514575d4f86 2133 tGet_.start();
mjr 3:3514575d4f86 2134 tInt_.start();
mjr 3:3514575d4f86 2135 }
mjr 3:3514575d4f86 2136
mjr 9:fd65b0a94720 2137 void get(int &x, int &y)
mjr 3:3514575d4f86 2138 {
mjr 3:3514575d4f86 2139 // disable interrupts while manipulating the shared data
mjr 3:3514575d4f86 2140 __disable_irq();
mjr 3:3514575d4f86 2141
mjr 3:3514575d4f86 2142 // read the shared data and store locally for calculations
mjr 6:cc35eb643e8f 2143 float ax = ax_, ay = ay_;
mjr 6:cc35eb643e8f 2144 float vx = vx_, vy = vy_;
mjr 5:a70c0bce770d 2145
mjr 6:cc35eb643e8f 2146 // reset the velocity sum for the next run
mjr 6:cc35eb643e8f 2147 vx_ = vy_ = 0;
mjr 3:3514575d4f86 2148
mjr 3:3514575d4f86 2149 // get the time since the last get() sample
mjr 38:091e511ce8a0 2150 float dt = tGet_.read_us()/1.0e6f;
mjr 3:3514575d4f86 2151 tGet_.reset();
mjr 3:3514575d4f86 2152
mjr 3:3514575d4f86 2153 // done manipulating the shared data
mjr 3:3514575d4f86 2154 __enable_irq();
mjr 3:3514575d4f86 2155
mjr 6:cc35eb643e8f 2156 // adjust the readings for the integration time
mjr 6:cc35eb643e8f 2157 vx /= dt;
mjr 6:cc35eb643e8f 2158 vy /= dt;
mjr 6:cc35eb643e8f 2159
mjr 6:cc35eb643e8f 2160 // add this sample to the current calibration interval's running total
mjr 6:cc35eb643e8f 2161 AccHist *p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 2162 p->addAvg(ax, ay);
mjr 6:cc35eb643e8f 2163
mjr 5:a70c0bce770d 2164 // check for auto-centering every so often
mjr 48:058ace2aed1d 2165 if (tCenter_.read_us() > 1000000)
mjr 5:a70c0bce770d 2166 {
mjr 5:a70c0bce770d 2167 // add the latest raw sample to the history list
mjr 6:cc35eb643e8f 2168 AccHist *prv = p;
mjr 5:a70c0bce770d 2169 iAccPrv_ = (iAccPrv_ + 1) % maxAccPrv;
mjr 6:cc35eb643e8f 2170 p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 2171 p->set(ax, ay, prv);
mjr 5:a70c0bce770d 2172
mjr 5:a70c0bce770d 2173 // if we have a full complement, check for stability
mjr 5:a70c0bce770d 2174 if (nAccPrv_ >= maxAccPrv)
mjr 5:a70c0bce770d 2175 {
mjr 5:a70c0bce770d 2176 // check if we've been stable for all recent samples
mjr 6:cc35eb643e8f 2177 static const float accTol = .01;
mjr 6:cc35eb643e8f 2178 AccHist *p0 = accPrv_;
mjr 6:cc35eb643e8f 2179 if (p0[0].d < accTol
mjr 6:cc35eb643e8f 2180 && p0[1].d < accTol
mjr 6:cc35eb643e8f 2181 && p0[2].d < accTol
mjr 6:cc35eb643e8f 2182 && p0[3].d < accTol
mjr 6:cc35eb643e8f 2183 && p0[4].d < accTol)
mjr 5:a70c0bce770d 2184 {
mjr 6:cc35eb643e8f 2185 // Figure the new calibration point as the average of
mjr 6:cc35eb643e8f 2186 // the samples over the rest period
mjr 6:cc35eb643e8f 2187 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5.0;
mjr 6:cc35eb643e8f 2188 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5.0;
mjr 5:a70c0bce770d 2189 }
mjr 5:a70c0bce770d 2190 }
mjr 5:a70c0bce770d 2191 else
mjr 5:a70c0bce770d 2192 {
mjr 5:a70c0bce770d 2193 // not enough samples yet; just up the count
mjr 5:a70c0bce770d 2194 ++nAccPrv_;
mjr 5:a70c0bce770d 2195 }
mjr 6:cc35eb643e8f 2196
mjr 6:cc35eb643e8f 2197 // clear the new item's running totals
mjr 6:cc35eb643e8f 2198 p->clearAvg();
mjr 5:a70c0bce770d 2199
mjr 5:a70c0bce770d 2200 // reset the timer
mjr 5:a70c0bce770d 2201 tCenter_.reset();
mjr 39:b3815a1c3802 2202
mjr 39:b3815a1c3802 2203 // If we haven't seen an interrupt in a while, do an explicit read to
mjr 39:b3815a1c3802 2204 // "unstick" the device. The device can become stuck - which is to say,
mjr 39:b3815a1c3802 2205 // it will stop delivering data-ready interrupts - if we fail to service
mjr 39:b3815a1c3802 2206 // one data-ready interrupt before the next one occurs. Reading a sample
mjr 39:b3815a1c3802 2207 // will clear up this overrun condition and allow normal interrupt
mjr 39:b3815a1c3802 2208 // generation to continue.
mjr 39:b3815a1c3802 2209 //
mjr 39:b3815a1c3802 2210 // Note that this stuck condition *shouldn't* ever occur - if it does,
mjr 39:b3815a1c3802 2211 // it means that we're spending a long period with interrupts disabled
mjr 39:b3815a1c3802 2212 // (either in a critical section or in another interrupt handler), which
mjr 39:b3815a1c3802 2213 // will likely cause other worse problems beyond the sticky accelerometer.
mjr 39:b3815a1c3802 2214 // Even so, it's easy to detect and correct, so we'll do so for the sake
mjr 39:b3815a1c3802 2215 // of making the system more fault-tolerant.
mjr 39:b3815a1c3802 2216 if (tInt_.read() > 1.0f)
mjr 39:b3815a1c3802 2217 {
mjr 39:b3815a1c3802 2218 float x, y, z;
mjr 39:b3815a1c3802 2219 mma_.getAccXYZ(x, y, z);
mjr 39:b3815a1c3802 2220 }
mjr 5:a70c0bce770d 2221 }
mjr 5:a70c0bce770d 2222
mjr 6:cc35eb643e8f 2223 // report our integrated velocity reading in x,y
mjr 6:cc35eb643e8f 2224 x = rawToReport(vx);
mjr 6:cc35eb643e8f 2225 y = rawToReport(vy);
mjr 5:a70c0bce770d 2226
mjr 6:cc35eb643e8f 2227 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 2228 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 2229 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 2230 #endif
mjr 3:3514575d4f86 2231 }
mjr 29:582472d0bc57 2232
mjr 3:3514575d4f86 2233 private:
mjr 6:cc35eb643e8f 2234 // adjust a raw acceleration figure to a usb report value
mjr 6:cc35eb643e8f 2235 int rawToReport(float v)
mjr 5:a70c0bce770d 2236 {
mjr 6:cc35eb643e8f 2237 // scale to the joystick report range and round to integer
mjr 6:cc35eb643e8f 2238 int i = int(round(v*JOYMAX));
mjr 5:a70c0bce770d 2239
mjr 6:cc35eb643e8f 2240 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 2241 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 2242 static const int filter[] = {
mjr 6:cc35eb643e8f 2243 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 2244 0,
mjr 6:cc35eb643e8f 2245 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 2246 };
mjr 6:cc35eb643e8f 2247 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 2248 }
mjr 5:a70c0bce770d 2249
mjr 3:3514575d4f86 2250 // interrupt handler
mjr 3:3514575d4f86 2251 void isr()
mjr 3:3514575d4f86 2252 {
mjr 3:3514575d4f86 2253 // Read the axes. Note that we have to read all three axes
mjr 3:3514575d4f86 2254 // (even though we only really use x and y) in order to clear
mjr 3:3514575d4f86 2255 // the "data ready" status bit in the accelerometer. The
mjr 3:3514575d4f86 2256 // interrupt only occurs when the "ready" bit transitions from
mjr 3:3514575d4f86 2257 // off to on, so we have to make sure it's off.
mjr 5:a70c0bce770d 2258 float x, y, z;
mjr 5:a70c0bce770d 2259 mma_.getAccXYZ(x, y, z);
mjr 3:3514575d4f86 2260
mjr 3:3514575d4f86 2261 // calculate the time since the last interrupt
mjr 39:b3815a1c3802 2262 float dt = tInt_.read();
mjr 3:3514575d4f86 2263 tInt_.reset();
mjr 6:cc35eb643e8f 2264
mjr 6:cc35eb643e8f 2265 // integrate the time slice from the previous reading to this reading
mjr 6:cc35eb643e8f 2266 vx_ += (x + ax_ - 2*cx_)*dt/2;
mjr 6:cc35eb643e8f 2267 vy_ += (y + ay_ - 2*cy_)*dt/2;
mjr 3:3514575d4f86 2268
mjr 6:cc35eb643e8f 2269 // store the updates
mjr 6:cc35eb643e8f 2270 ax_ = x;
mjr 6:cc35eb643e8f 2271 ay_ = y;
mjr 6:cc35eb643e8f 2272 az_ = z;
mjr 3:3514575d4f86 2273 }
mjr 3:3514575d4f86 2274
mjr 3:3514575d4f86 2275 // underlying accelerometer object
mjr 3:3514575d4f86 2276 MMA8451Q mma_;
mjr 3:3514575d4f86 2277
mjr 5:a70c0bce770d 2278 // last raw acceleration readings
mjr 6:cc35eb643e8f 2279 float ax_, ay_, az_;
mjr 5:a70c0bce770d 2280
mjr 6:cc35eb643e8f 2281 // integrated velocity reading since last get()
mjr 6:cc35eb643e8f 2282 float vx_, vy_;
mjr 6:cc35eb643e8f 2283
mjr 3:3514575d4f86 2284 // timer for measuring time between get() samples
mjr 3:3514575d4f86 2285 Timer tGet_;
mjr 3:3514575d4f86 2286
mjr 3:3514575d4f86 2287 // timer for measuring time between interrupts
mjr 3:3514575d4f86 2288 Timer tInt_;
mjr 5:a70c0bce770d 2289
mjr 6:cc35eb643e8f 2290 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 2291 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 2292 // at rest.
mjr 6:cc35eb643e8f 2293 float cx_, cy_;
mjr 5:a70c0bce770d 2294
mjr 5:a70c0bce770d 2295 // timer for atuo-centering
mjr 5:a70c0bce770d 2296 Timer tCenter_;
mjr 6:cc35eb643e8f 2297
mjr 6:cc35eb643e8f 2298 // Auto-centering history. This is a separate history list that
mjr 6:cc35eb643e8f 2299 // records results spaced out sparesely over time, so that we can
mjr 6:cc35eb643e8f 2300 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 2301 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 2302 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 2303 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 2304 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 2305 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 2306 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 2307 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 2308 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 2309 // accelerometer measurement bias (e.g., from temperature changes).
mjr 5:a70c0bce770d 2310 int iAccPrv_, nAccPrv_;
mjr 5:a70c0bce770d 2311 static const int maxAccPrv = 5;
mjr 6:cc35eb643e8f 2312 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 2313
mjr 5:a70c0bce770d 2314 // interurupt pin name
mjr 5:a70c0bce770d 2315 PinName irqPin_;
mjr 5:a70c0bce770d 2316
mjr 5:a70c0bce770d 2317 // interrupt router
mjr 5:a70c0bce770d 2318 InterruptIn intIn_;
mjr 3:3514575d4f86 2319 };
mjr 3:3514575d4f86 2320
mjr 5:a70c0bce770d 2321
mjr 5:a70c0bce770d 2322 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 2323 //
mjr 14:df700b22ca08 2324 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 2325 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 2326 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 2327 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 2328 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 2329 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 2330 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 2331 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 2332 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 2333 //
mjr 14:df700b22ca08 2334 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 2335 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 2336 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 2337 //
mjr 5:a70c0bce770d 2338 void clear_i2c()
mjr 5:a70c0bce770d 2339 {
mjr 38:091e511ce8a0 2340 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 2341 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 2342 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 2343
mjr 5:a70c0bce770d 2344 // clock the SCL 9 times
mjr 5:a70c0bce770d 2345 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 2346 {
mjr 5:a70c0bce770d 2347 scl = 1;
mjr 5:a70c0bce770d 2348 wait_us(20);
mjr 5:a70c0bce770d 2349 scl = 0;
mjr 5:a70c0bce770d 2350 wait_us(20);
mjr 5:a70c0bce770d 2351 }
mjr 5:a70c0bce770d 2352 }
mjr 14:df700b22ca08 2353
mjr 14:df700b22ca08 2354 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 2355 //
mjr 33:d832bcab089e 2356 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 2357 // for a given interval before allowing an update.
mjr 33:d832bcab089e 2358 //
mjr 33:d832bcab089e 2359 class Debouncer
mjr 33:d832bcab089e 2360 {
mjr 33:d832bcab089e 2361 public:
mjr 33:d832bcab089e 2362 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 2363 {
mjr 33:d832bcab089e 2364 t.start();
mjr 33:d832bcab089e 2365 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 2366 this->tmin = tmin;
mjr 33:d832bcab089e 2367 }
mjr 33:d832bcab089e 2368
mjr 33:d832bcab089e 2369 // Get the current stable value
mjr 33:d832bcab089e 2370 bool val() const { return stable; }
mjr 33:d832bcab089e 2371
mjr 33:d832bcab089e 2372 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 2373 // input device.
mjr 33:d832bcab089e 2374 void sampleIn(bool val)
mjr 33:d832bcab089e 2375 {
mjr 33:d832bcab089e 2376 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 2377 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 2378 // on the sample reader.
mjr 33:d832bcab089e 2379 if (val != prv)
mjr 33:d832bcab089e 2380 {
mjr 33:d832bcab089e 2381 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 2382 t.reset();
mjr 33:d832bcab089e 2383
mjr 33:d832bcab089e 2384 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 2385 prv = val;
mjr 33:d832bcab089e 2386 }
mjr 33:d832bcab089e 2387 else if (val != stable)
mjr 33:d832bcab089e 2388 {
mjr 33:d832bcab089e 2389 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 2390 // and different from the stable value. This means that
mjr 33:d832bcab089e 2391 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 2392 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 2393 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 2394 if (t.read() > tmin)
mjr 33:d832bcab089e 2395 stable = val;
mjr 33:d832bcab089e 2396 }
mjr 33:d832bcab089e 2397 }
mjr 33:d832bcab089e 2398
mjr 33:d832bcab089e 2399 private:
mjr 33:d832bcab089e 2400 // current stable value
mjr 33:d832bcab089e 2401 bool stable;
mjr 33:d832bcab089e 2402
mjr 33:d832bcab089e 2403 // last raw sample value
mjr 33:d832bcab089e 2404 bool prv;
mjr 33:d832bcab089e 2405
mjr 33:d832bcab089e 2406 // elapsed time since last raw input change
mjr 33:d832bcab089e 2407 Timer t;
mjr 33:d832bcab089e 2408
mjr 33:d832bcab089e 2409 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 2410 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 2411 float tmin;
mjr 33:d832bcab089e 2412 };
mjr 33:d832bcab089e 2413
mjr 33:d832bcab089e 2414
mjr 33:d832bcab089e 2415 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 2416 //
mjr 33:d832bcab089e 2417 // Turn off all outputs and restore everything to the default LedWiz
mjr 33:d832bcab089e 2418 // state. This sets outputs #1-32 to LedWiz profile value 48 (full
mjr 33:d832bcab089e 2419 // brightness) and switch state Off, sets all extended outputs (#33
mjr 33:d832bcab089e 2420 // and above) to zero brightness, and sets the LedWiz flash rate to 2.
mjr 33:d832bcab089e 2421 // This effectively restores the power-on conditions.
mjr 33:d832bcab089e 2422 //
mjr 33:d832bcab089e 2423 void allOutputsOff()
mjr 33:d832bcab089e 2424 {
mjr 33:d832bcab089e 2425 // reset all LedWiz outputs to OFF/48
mjr 35:e959ffba78fd 2426 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 33:d832bcab089e 2427 {
mjr 33:d832bcab089e 2428 outLevel[i] = 0;
mjr 33:d832bcab089e 2429 wizOn[i] = 0;
mjr 33:d832bcab089e 2430 wizVal[i] = 48;
mjr 33:d832bcab089e 2431 lwPin[i]->set(0);
mjr 33:d832bcab089e 2432 }
mjr 33:d832bcab089e 2433
mjr 33:d832bcab089e 2434 // reset all extended outputs (ports >32) to full off (brightness 0)
mjr 40:cc0d9814522b 2435 for (int i = numLwOutputs ; i < numOutputs ; ++i)
mjr 33:d832bcab089e 2436 {
mjr 33:d832bcab089e 2437 outLevel[i] = 0;
mjr 33:d832bcab089e 2438 lwPin[i]->set(0);
mjr 33:d832bcab089e 2439 }
mjr 33:d832bcab089e 2440
mjr 33:d832bcab089e 2441 // restore default LedWiz flash rate
mjr 33:d832bcab089e 2442 wizSpeed = 2;
mjr 34:6b981a2afab7 2443
mjr 34:6b981a2afab7 2444 // flush changes to hc595, if applicable
mjr 35:e959ffba78fd 2445 if (hc595 != 0)
mjr 35:e959ffba78fd 2446 hc595->update();
mjr 33:d832bcab089e 2447 }
mjr 33:d832bcab089e 2448
mjr 33:d832bcab089e 2449 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 2450 //
mjr 33:d832bcab089e 2451 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 2452 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 2453 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 2454 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 2455 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 2456 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 2457 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 2458 //
mjr 33:d832bcab089e 2459 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 2460 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 2461 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 2462 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 2463 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 2464 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 2465 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 2466 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 2467 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 2468 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 2469 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 2470 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 2471 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 2472 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 2473 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 2474 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 2475 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 2476 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 2477 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 2478 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 2479 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 2480 //
mjr 40:cc0d9814522b 2481 // This scheme might seem a little convoluted, but it handles a number
mjr 40:cc0d9814522b 2482 // of tricky but likely scenarios:
mjr 33:d832bcab089e 2483 //
mjr 33:d832bcab089e 2484 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 40:cc0d9814522b 2485 // so that the PC goes into "Soft Off" mode when the user turns off
mjr 40:cc0d9814522b 2486 // the cabinet by pushing the power button or using the Shut Down
mjr 40:cc0d9814522b 2487 // command from within Windows. In Windows parlance, this "soft off"
mjr 40:cc0d9814522b 2488 // condition is called ACPI State S5. In this state, the main CPU
mjr 40:cc0d9814522b 2489 // power is turned off, but the motherboard still provides power to
mjr 40:cc0d9814522b 2490 // USB devices. This means that the KL25Z keeps running. Without
mjr 40:cc0d9814522b 2491 // the external power sensing circuit, the only hint that we're in
mjr 40:cc0d9814522b 2492 // this state is that the USB connection to the host goes into Suspend
mjr 40:cc0d9814522b 2493 // mode, but that could mean other things as well. The latch circuit
mjr 40:cc0d9814522b 2494 // lets us tell for sure that we're in this state.
mjr 33:d832bcab089e 2495 //
mjr 33:d832bcab089e 2496 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 2497 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 2498 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 2499 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 2500 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 2501 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 2502 // will power up before or after PSU2, so it's not good enough to
mjr 40:cc0d9814522b 2503 // observe the current state of PSU2 when we first check. If PSU2
mjr 40:cc0d9814522b 2504 // were to come on first, checking only the current state would fool
mjr 40:cc0d9814522b 2505 // us into thinking that no action is required, because we'd only see
mjr 40:cc0d9814522b 2506 // that PSU2 is turned on any time we check. The latch handles this
mjr 40:cc0d9814522b 2507 // case by letting us see that PSU2 was indeed off some time before our
mjr 40:cc0d9814522b 2508 // first check.
mjr 33:d832bcab089e 2509 //
mjr 33:d832bcab089e 2510 // - If the KL25Z is rebooted while the main system is running, or the
mjr 40:cc0d9814522b 2511 // KL25Z is unplugged and plugged back in, we'll correctly leave the
mjr 33:d832bcab089e 2512 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 2513 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 2514 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 2515 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 2516 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 2517 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 2518 //
mjr 33:d832bcab089e 2519
mjr 33:d832bcab089e 2520 // Current PSU2 state:
mjr 33:d832bcab089e 2521 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 2522 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 2523 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 2524 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 2525 // 5 -> TV relay on
mjr 33:d832bcab089e 2526 int psu2_state = 1;
mjr 35:e959ffba78fd 2527
mjr 35:e959ffba78fd 2528 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 2529 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 2530 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 2531
mjr 35:e959ffba78fd 2532 // TV ON switch relay control
mjr 35:e959ffba78fd 2533 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 2534
mjr 35:e959ffba78fd 2535 // Timer interrupt
mjr 35:e959ffba78fd 2536 Ticker tv_ticker;
mjr 35:e959ffba78fd 2537 float tv_delay_time;
mjr 33:d832bcab089e 2538 void TVTimerInt()
mjr 33:d832bcab089e 2539 {
mjr 35:e959ffba78fd 2540 // time since last state change
mjr 35:e959ffba78fd 2541 static Timer tv_timer;
mjr 35:e959ffba78fd 2542
mjr 33:d832bcab089e 2543 // Check our internal state
mjr 33:d832bcab089e 2544 switch (psu2_state)
mjr 33:d832bcab089e 2545 {
mjr 33:d832bcab089e 2546 case 1:
mjr 33:d832bcab089e 2547 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 2548 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 2549 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 2550 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 2551 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 2552 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 2553 {
mjr 33:d832bcab089e 2554 // switch to OFF state
mjr 33:d832bcab089e 2555 psu2_state = 2;
mjr 33:d832bcab089e 2556
mjr 33:d832bcab089e 2557 // try setting the latch
mjr 35:e959ffba78fd 2558 psu2_status_set->write(1);
mjr 33:d832bcab089e 2559 }
mjr 33:d832bcab089e 2560 break;
mjr 33:d832bcab089e 2561
mjr 33:d832bcab089e 2562 case 2:
mjr 33:d832bcab089e 2563 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 2564 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 2565 psu2_status_set->write(0);
mjr 33:d832bcab089e 2566 psu2_state = 3;
mjr 33:d832bcab089e 2567 break;
mjr 33:d832bcab089e 2568
mjr 33:d832bcab089e 2569 case 3:
mjr 33:d832bcab089e 2570 // CHECK state: we pulsed SET, and we're now ready to see
mjr 40:cc0d9814522b 2571 // if it stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 2572 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 2573 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 2574 if (psu2_status_sense->read())
mjr 33:d832bcab089e 2575 {
mjr 33:d832bcab089e 2576 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 2577 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 2578 tv_timer.reset();
mjr 33:d832bcab089e 2579 tv_timer.start();
mjr 33:d832bcab089e 2580 psu2_state = 4;
mjr 33:d832bcab089e 2581 }
mjr 33:d832bcab089e 2582 else
mjr 33:d832bcab089e 2583 {
mjr 33:d832bcab089e 2584 // The latch didn't stick, so PSU2 was still off at
mjr 33:d832bcab089e 2585 // our last check. Try pulsing it again in case PSU2
mjr 33:d832bcab089e 2586 // was turned on since the last check.
mjr 35:e959ffba78fd 2587 psu2_status_set->write(1);
mjr 33:d832bcab089e 2588 psu2_state = 2;
mjr 33:d832bcab089e 2589 }
mjr 33:d832bcab089e 2590 break;
mjr 33:d832bcab089e 2591
mjr 33:d832bcab089e 2592 case 4:
mjr 33:d832bcab089e 2593 // TV timer countdown in progress. If we've reached the
mjr 33:d832bcab089e 2594 // delay time, pulse the relay.
mjr 35:e959ffba78fd 2595 if (tv_timer.read() >= tv_delay_time)
mjr 33:d832bcab089e 2596 {
mjr 33:d832bcab089e 2597 // turn on the relay for one timer interval
mjr 35:e959ffba78fd 2598 tv_relay->write(1);
mjr 33:d832bcab089e 2599 psu2_state = 5;
mjr 33:d832bcab089e 2600 }
mjr 33:d832bcab089e 2601 break;
mjr 33:d832bcab089e 2602
mjr 33:d832bcab089e 2603 case 5:
mjr 33:d832bcab089e 2604 // TV timer relay on. We pulse this for one interval, so
mjr 33:d832bcab089e 2605 // it's now time to turn it off and return to the default state.
mjr 35:e959ffba78fd 2606 tv_relay->write(0);
mjr 33:d832bcab089e 2607 psu2_state = 1;
mjr 33:d832bcab089e 2608 break;
mjr 33:d832bcab089e 2609 }
mjr 33:d832bcab089e 2610 }
mjr 33:d832bcab089e 2611
mjr 35:e959ffba78fd 2612 // Start the TV ON checker. If the status sense circuit is enabled in
mjr 35:e959ffba78fd 2613 // the configuration, we'll set up the pin connections and start the
mjr 35:e959ffba78fd 2614 // interrupt handler that periodically checks the status. Does nothing
mjr 35:e959ffba78fd 2615 // if any of the pins are configured as NC.
mjr 35:e959ffba78fd 2616 void startTVTimer(Config &cfg)
mjr 35:e959ffba78fd 2617 {
mjr 55:4db125cd11a0 2618 // only start the timer if the pins are configured and the delay
mjr 55:4db125cd11a0 2619 // time is nonzero
mjr 55:4db125cd11a0 2620 if (cfg.TVON.delayTime != 0
mjr 55:4db125cd11a0 2621 && cfg.TVON.statusPin != 0xFF
mjr 53:9b2611964afc 2622 && cfg.TVON.latchPin != 0xFF
mjr 53:9b2611964afc 2623 && cfg.TVON.relayPin != 0xFF)
mjr 35:e959ffba78fd 2624 {
mjr 53:9b2611964afc 2625 psu2_status_sense = new DigitalIn(wirePinName(cfg.TVON.statusPin));
mjr 53:9b2611964afc 2626 psu2_status_set = new DigitalOut(wirePinName(cfg.TVON.latchPin));
mjr 53:9b2611964afc 2627 tv_relay = new DigitalOut(wirePinName(cfg.TVON.relayPin));
mjr 40:cc0d9814522b 2628 tv_delay_time = cfg.TVON.delayTime/100.0;
mjr 35:e959ffba78fd 2629
mjr 35:e959ffba78fd 2630 // Set up our time routine to run every 1/4 second.
mjr 35:e959ffba78fd 2631 tv_ticker.attach(&TVTimerInt, 0.25);
mjr 35:e959ffba78fd 2632 }
mjr 35:e959ffba78fd 2633 }
mjr 35:e959ffba78fd 2634
mjr 35:e959ffba78fd 2635 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2636 //
mjr 35:e959ffba78fd 2637 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 2638 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 2639 //
mjr 35:e959ffba78fd 2640 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 2641 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 2642 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 2643 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 2644 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 2645 // again each time the firmware is updated.
mjr 35:e959ffba78fd 2646 //
mjr 35:e959ffba78fd 2647 NVM nvm;
mjr 35:e959ffba78fd 2648
mjr 35:e959ffba78fd 2649 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 2650 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 2651
mjr 35:e959ffba78fd 2652 // flash memory controller interface
mjr 35:e959ffba78fd 2653 FreescaleIAP iap;
mjr 35:e959ffba78fd 2654
mjr 35:e959ffba78fd 2655 // figure the flash address as a pointer along with the number of sectors
mjr 35:e959ffba78fd 2656 // required to store the structure
mjr 35:e959ffba78fd 2657 NVM *configFlashAddr(int &addr, int &numSectors)
mjr 35:e959ffba78fd 2658 {
mjr 35:e959ffba78fd 2659 // figure how many flash sectors we span, rounding up to whole sectors
mjr 35:e959ffba78fd 2660 numSectors = (sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE;
mjr 35:e959ffba78fd 2661
mjr 35:e959ffba78fd 2662 // figure the address - this is the highest flash address where the
mjr 35:e959ffba78fd 2663 // structure will fit with the start aligned on a sector boundary
mjr 35:e959ffba78fd 2664 addr = iap.flash_size() - (numSectors * SECTOR_SIZE);
mjr 35:e959ffba78fd 2665
mjr 35:e959ffba78fd 2666 // return the address as a pointer
mjr 35:e959ffba78fd 2667 return (NVM *)addr;
mjr 35:e959ffba78fd 2668 }
mjr 35:e959ffba78fd 2669
mjr 35:e959ffba78fd 2670 // figure the flash address as a pointer
mjr 35:e959ffba78fd 2671 NVM *configFlashAddr()
mjr 35:e959ffba78fd 2672 {
mjr 35:e959ffba78fd 2673 int addr, numSectors;
mjr 35:e959ffba78fd 2674 return configFlashAddr(addr, numSectors);
mjr 35:e959ffba78fd 2675 }
mjr 35:e959ffba78fd 2676
mjr 35:e959ffba78fd 2677 // Load the config from flash
mjr 35:e959ffba78fd 2678 void loadConfigFromFlash()
mjr 35:e959ffba78fd 2679 {
mjr 35:e959ffba78fd 2680 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 2681 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 2682 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 2683 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 2684 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 2685 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 2686 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 2687 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 2688 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 2689 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 2690 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 2691 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 2692 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 2693 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 2694 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 2695 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 2696
mjr 35:e959ffba78fd 2697 // Figure how many sectors we need for our structure
mjr 35:e959ffba78fd 2698 NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 2699
mjr 35:e959ffba78fd 2700 // if the flash is valid, load it; otherwise initialize to defaults
mjr 35:e959ffba78fd 2701 if (flash->valid())
mjr 35:e959ffba78fd 2702 {
mjr 35:e959ffba78fd 2703 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 2704 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 2705 }
mjr 35:e959ffba78fd 2706 else
mjr 35:e959ffba78fd 2707 {
mjr 35:e959ffba78fd 2708 // flash is invalid - load factory settings nito RAM structure
mjr 35:e959ffba78fd 2709 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 2710 }
mjr 35:e959ffba78fd 2711 }
mjr 35:e959ffba78fd 2712
mjr 35:e959ffba78fd 2713 void saveConfigToFlash()
mjr 33:d832bcab089e 2714 {
mjr 35:e959ffba78fd 2715 int addr, sectors;
mjr 35:e959ffba78fd 2716 configFlashAddr(addr, sectors);
mjr 35:e959ffba78fd 2717 nvm.save(iap, addr);
mjr 35:e959ffba78fd 2718 }
mjr 35:e959ffba78fd 2719
mjr 35:e959ffba78fd 2720 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2721 //
mjr 55:4db125cd11a0 2722 // Pixel dump mode - the host requested a dump of image sensor pixels
mjr 55:4db125cd11a0 2723 // (helpful for installing and setting up the sensor and light source)
mjr 55:4db125cd11a0 2724 //
mjr 55:4db125cd11a0 2725 bool reportPlungerStat = false;
mjr 55:4db125cd11a0 2726 uint8_t reportPlungerStatFlags; // plunger pixel report flag bits (see ccdSensor.h)
mjr 55:4db125cd11a0 2727 uint8_t reportPlungerStatTime; // extra exposure time for plunger pixel report
mjr 55:4db125cd11a0 2728
mjr 55:4db125cd11a0 2729
mjr 55:4db125cd11a0 2730
mjr 55:4db125cd11a0 2731 // ---------------------------------------------------------------------------
mjr 55:4db125cd11a0 2732 //
mjr 40:cc0d9814522b 2733 // Night mode setting updates
mjr 40:cc0d9814522b 2734 //
mjr 38:091e511ce8a0 2735
mjr 38:091e511ce8a0 2736 // Turn night mode on or off
mjr 38:091e511ce8a0 2737 static void setNightMode(bool on)
mjr 38:091e511ce8a0 2738 {
mjr 40:cc0d9814522b 2739 // set the new night mode flag in the noisy output class
mjr 53:9b2611964afc 2740 nightMode = on;
mjr 55:4db125cd11a0 2741
mjr 40:cc0d9814522b 2742 // update the special output pin that shows the night mode state
mjr 53:9b2611964afc 2743 int port = int(cfg.nightMode.port) - 1;
mjr 53:9b2611964afc 2744 if (port >= 0 && port < numOutputs)
mjr 53:9b2611964afc 2745 lwPin[port]->set(nightMode ? 255 : 0);
mjr 40:cc0d9814522b 2746
mjr 40:cc0d9814522b 2747 // update all outputs for the mode change
mjr 40:cc0d9814522b 2748 updateAllOuts();
mjr 38:091e511ce8a0 2749 }
mjr 38:091e511ce8a0 2750
mjr 38:091e511ce8a0 2751 // Toggle night mode
mjr 38:091e511ce8a0 2752 static void toggleNightMode()
mjr 38:091e511ce8a0 2753 {
mjr 53:9b2611964afc 2754 setNightMode(!nightMode);
mjr 38:091e511ce8a0 2755 }
mjr 38:091e511ce8a0 2756
mjr 38:091e511ce8a0 2757
mjr 38:091e511ce8a0 2758 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 2759 //
mjr 35:e959ffba78fd 2760 // Plunger Sensor
mjr 35:e959ffba78fd 2761 //
mjr 35:e959ffba78fd 2762
mjr 35:e959ffba78fd 2763 // the plunger sensor interface object
mjr 35:e959ffba78fd 2764 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 2765
mjr 35:e959ffba78fd 2766 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 2767 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 2768 void createPlunger()
mjr 35:e959ffba78fd 2769 {
mjr 35:e959ffba78fd 2770 // create the new sensor object according to the type
mjr 35:e959ffba78fd 2771 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 2772 {
mjr 35:e959ffba78fd 2773 case PlungerType_TSL1410RS:
mjr 35:e959ffba78fd 2774 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 2775 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 2776 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 2777 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 2778 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 2779 NC);
mjr 35:e959ffba78fd 2780 break;
mjr 35:e959ffba78fd 2781
mjr 35:e959ffba78fd 2782 case PlungerType_TSL1410RP:
mjr 35:e959ffba78fd 2783 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 2784 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 2785 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 2786 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 2787 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 2788 wirePinName(cfg.plunger.sensorPin[3]));
mjr 35:e959ffba78fd 2789 break;
mjr 35:e959ffba78fd 2790
mjr 35:e959ffba78fd 2791 case PlungerType_TSL1412RS:
mjr 35:e959ffba78fd 2792 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 2793 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 2794 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 2795 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 2796 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 2797 NC);
mjr 35:e959ffba78fd 2798 break;
mjr 35:e959ffba78fd 2799
mjr 35:e959ffba78fd 2800 case PlungerType_TSL1412RP:
mjr 35:e959ffba78fd 2801 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 2802 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 2803 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 2804 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 2805 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 2806 wirePinName(cfg.plunger.sensorPin[3]));
mjr 35:e959ffba78fd 2807 break;
mjr 35:e959ffba78fd 2808
mjr 35:e959ffba78fd 2809 case PlungerType_Pot:
mjr 35:e959ffba78fd 2810 // pins are: AO
mjr 53:9b2611964afc 2811 plungerSensor = new PlungerSensorPot(
mjr 53:9b2611964afc 2812 wirePinName(cfg.plunger.sensorPin[0]));
mjr 35:e959ffba78fd 2813 break;
mjr 35:e959ffba78fd 2814
mjr 35:e959ffba78fd 2815 case PlungerType_None:
mjr 35:e959ffba78fd 2816 default:
mjr 35:e959ffba78fd 2817 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 2818 break;
mjr 35:e959ffba78fd 2819 }
mjr 33:d832bcab089e 2820 }
mjr 33:d832bcab089e 2821
mjr 52:8298b2a73eb2 2822 // Global plunger calibration mode flag
mjr 52:8298b2a73eb2 2823 bool plungerCalMode;
mjr 52:8298b2a73eb2 2824
mjr 48:058ace2aed1d 2825 // Plunger reader
mjr 51:57eb311faafa 2826 //
mjr 51:57eb311faafa 2827 // This class encapsulates our plunger data processing. At the simplest
mjr 51:57eb311faafa 2828 // level, we read the position from the sensor, adjust it for the
mjr 51:57eb311faafa 2829 // calibration settings, and report the calibrated position to the host.
mjr 51:57eb311faafa 2830 //
mjr 51:57eb311faafa 2831 // In addition, we constantly monitor the data for "firing" motions.
mjr 51:57eb311faafa 2832 // A firing motion is when the user pulls back the plunger and releases
mjr 51:57eb311faafa 2833 // it, allowing it to shoot forward under the force of the main spring.
mjr 51:57eb311faafa 2834 // When we detect that this is happening, we briefly stop reporting the
mjr 51:57eb311faafa 2835 // real physical position that we're reading from the sensor, and instead
mjr 51:57eb311faafa 2836 // report a synthetic series of positions that depicts an idealized
mjr 51:57eb311faafa 2837 // firing motion.
mjr 51:57eb311faafa 2838 //
mjr 51:57eb311faafa 2839 // The point of the synthetic reports is to correct for distortions
mjr 51:57eb311faafa 2840 // created by the joystick interface conventions used by VP and other
mjr 51:57eb311faafa 2841 // PC pinball emulators. The convention they use is simply to have the
mjr 51:57eb311faafa 2842 // plunger device report the instantaneous position of the real plunger.
mjr 51:57eb311faafa 2843 // The PC software polls this reported position periodically, and moves
mjr 51:57eb311faafa 2844 // the on-screen virtual plunger in sync with the real plunger. This
mjr 51:57eb311faafa 2845 // works fine for human-scale motion when the user is manually moving
mjr 51:57eb311faafa 2846 // the plunger. But it doesn't work for the high speed motion of a
mjr 51:57eb311faafa 2847 // release. The plunger simply moves too fast. VP polls in about 10ms
mjr 51:57eb311faafa 2848 // intervals; the plunger takes about 50ms to travel from fully
mjr 51:57eb311faafa 2849 // retracted to the park position when released. The low sampling
mjr 51:57eb311faafa 2850 // rate relative to the rate of change of the sampled data creates
mjr 51:57eb311faafa 2851 // a classic digital aliasing effect.
mjr 51:57eb311faafa 2852 //
mjr 51:57eb311faafa 2853 // The synthetic reporting scheme compensates for the interface
mjr 51:57eb311faafa 2854 // distortions by essentially changing to a coarse enough timescale
mjr 51:57eb311faafa 2855 // that VP can reliably interpret the readings. Conceptually, there
mjr 51:57eb311faafa 2856 // are three steps involved in doing this. First, we analyze the
mjr 51:57eb311faafa 2857 // actual sensor data to detect and characterize the release motion.
mjr 51:57eb311faafa 2858 // Second, once we think we have a release in progress, we fit the
mjr 51:57eb311faafa 2859 // data to a mathematical model of the release. The model we use is
mjr 51:57eb311faafa 2860 // dead simple: we consider the release to have one parameter, namely
mjr 51:57eb311faafa 2861 // the retraction distance at the moment the user lets go. This is an
mjr 51:57eb311faafa 2862 // excellent proxy in the real physical system for the final speed
mjr 51:57eb311faafa 2863 // when the plunger hits the ball, and it also happens to match how
mjr 51:57eb311faafa 2864 // VP models it internally. Third, we construct synthetic reports
mjr 51:57eb311faafa 2865 // that will make VP's internal state match our model. This is also
mjr 51:57eb311faafa 2866 // pretty simple: we just need to send VP the maximum retraction
mjr 51:57eb311faafa 2867 // distance for long enough to be sure that it polls it at least
mjr 51:57eb311faafa 2868 // once, and then send it the park position for long enough to
mjr 51:57eb311faafa 2869 // ensure that VP will complete the same firing motion. The
mjr 51:57eb311faafa 2870 // immediate jump from the maximum point to the zero point will
mjr 51:57eb311faafa 2871 // cause VP to move its simulation model plunger forward from the
mjr 51:57eb311faafa 2872 // starting point at its natural spring acceleration rate, which
mjr 51:57eb311faafa 2873 // is exactly what the real plunger just did.
mjr 51:57eb311faafa 2874 //
mjr 48:058ace2aed1d 2875 class PlungerReader
mjr 48:058ace2aed1d 2876 {
mjr 48:058ace2aed1d 2877 public:
mjr 48:058ace2aed1d 2878 PlungerReader()
mjr 48:058ace2aed1d 2879 {
mjr 48:058ace2aed1d 2880 // not in a firing event yet
mjr 48:058ace2aed1d 2881 firing = 0;
mjr 48:058ace2aed1d 2882
mjr 48:058ace2aed1d 2883 // no history yet
mjr 48:058ace2aed1d 2884 histIdx = 0;
mjr 55:4db125cd11a0 2885
mjr 55:4db125cd11a0 2886 // initialize the filter
mjr 55:4db125cd11a0 2887 initFilter();
mjr 48:058ace2aed1d 2888 }
mjr 48:058ace2aed1d 2889
mjr 48:058ace2aed1d 2890 // Collect a reading from the plunger sensor. The main loop calls
mjr 48:058ace2aed1d 2891 // this frequently to read the current raw position data from the
mjr 48:058ace2aed1d 2892 // sensor. We analyze the raw data to produce the calibrated
mjr 48:058ace2aed1d 2893 // position that we report to the PC via the joystick interface.
mjr 48:058ace2aed1d 2894 void read()
mjr 48:058ace2aed1d 2895 {
mjr 48:058ace2aed1d 2896 // Read a sample from the sensor
mjr 48:058ace2aed1d 2897 PlungerReading r;
mjr 48:058ace2aed1d 2898 if (plungerSensor->read(r))
mjr 48:058ace2aed1d 2899 {
mjr 51:57eb311faafa 2900 // Pull the previous reading from the history
mjr 50:40015764bbe6 2901 const PlungerReading &prv = nthHist(0);
mjr 48:058ace2aed1d 2902
mjr 48:058ace2aed1d 2903 // If the new reading is within 2ms of the previous reading,
mjr 48:058ace2aed1d 2904 // ignore it. We require a minimum time between samples to
mjr 48:058ace2aed1d 2905 // ensure that we have a usable amount of precision in the
mjr 48:058ace2aed1d 2906 // denominator (the time interval) for calculating the plunger
mjr 48:058ace2aed1d 2907 // velocity. (The CCD sensor can't take readings faster than
mjr 48:058ace2aed1d 2908 // this anyway, but other sensor types, such as potentiometers,
mjr 48:058ace2aed1d 2909 // can, so we have to throttle the rate artifically in case
mjr 48:058ace2aed1d 2910 // we're using a fast sensor like that.)
mjr 48:058ace2aed1d 2911 if (uint32_t(r.t - prv.t) < 2000UL)
mjr 48:058ace2aed1d 2912 return;
mjr 53:9b2611964afc 2913
mjr 53:9b2611964afc 2914 // check for calibration mode
mjr 53:9b2611964afc 2915 if (plungerCalMode)
mjr 53:9b2611964afc 2916 {
mjr 53:9b2611964afc 2917 // Calibration mode. Adjust the calibration bounds to fit
mjr 53:9b2611964afc 2918 // the value. If this value is beyond the current min or max,
mjr 53:9b2611964afc 2919 // expand the envelope to include this new value.
mjr 53:9b2611964afc 2920 if (r.pos > cfg.plunger.cal.max)
mjr 53:9b2611964afc 2921 cfg.plunger.cal.max = r.pos;
mjr 53:9b2611964afc 2922 if (r.pos < cfg.plunger.cal.min)
mjr 53:9b2611964afc 2923 cfg.plunger.cal.min = r.pos;
mjr 50:40015764bbe6 2924
mjr 53:9b2611964afc 2925 // If we're in calibration state 0, we're waiting for the
mjr 53:9b2611964afc 2926 // plunger to come to rest at the park position so that we
mjr 53:9b2611964afc 2927 // can take a sample of the park position. Check to see if
mjr 53:9b2611964afc 2928 // we've been at rest for a minimum interval.
mjr 53:9b2611964afc 2929 if (calState == 0)
mjr 53:9b2611964afc 2930 {
mjr 53:9b2611964afc 2931 if (abs(r.pos - calZeroStart.pos) < 65535/3/50)
mjr 53:9b2611964afc 2932 {
mjr 53:9b2611964afc 2933 // we're close enough - make sure we've been here long enough
mjr 53:9b2611964afc 2934 if (uint32_t(r.t - calZeroStart.t) > 100000UL)
mjr 53:9b2611964afc 2935 {
mjr 53:9b2611964afc 2936 // we've been at rest long enough - count it
mjr 53:9b2611964afc 2937 calZeroPosSum += r.pos;
mjr 53:9b2611964afc 2938 calZeroPosN += 1;
mjr 53:9b2611964afc 2939
mjr 53:9b2611964afc 2940 // update the zero position from the new average
mjr 53:9b2611964afc 2941 cfg.plunger.cal.zero = uint16_t(calZeroPosSum / calZeroPosN);
mjr 53:9b2611964afc 2942
mjr 53:9b2611964afc 2943 // switch to calibration state 1 - at rest
mjr 53:9b2611964afc 2944 calState = 1;
mjr 53:9b2611964afc 2945 }
mjr 53:9b2611964afc 2946 }
mjr 53:9b2611964afc 2947 else
mjr 53:9b2611964afc 2948 {
mjr 53:9b2611964afc 2949 // we're not close to the last position - start again here
mjr 53:9b2611964afc 2950 calZeroStart = r;
mjr 53:9b2611964afc 2951 }
mjr 53:9b2611964afc 2952 }
mjr 53:9b2611964afc 2953
mjr 53:9b2611964afc 2954 // Rescale to the joystick range, and adjust for the current
mjr 53:9b2611964afc 2955 // park position, but don't calibrate. We don't know the maximum
mjr 53:9b2611964afc 2956 // point yet, so we can't calibrate the range.
mjr 53:9b2611964afc 2957 r.pos = int(
mjr 53:9b2611964afc 2958 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 2959 / (65535 - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 2960 }
mjr 53:9b2611964afc 2961 else
mjr 53:9b2611964afc 2962 {
mjr 53:9b2611964afc 2963 // Not in calibration mode. Apply the existing calibration and
mjr 53:9b2611964afc 2964 // rescale to the joystick range.
mjr 53:9b2611964afc 2965 r.pos = int(
mjr 53:9b2611964afc 2966 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 2967 / (cfg.plunger.cal.max - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 2968
mjr 53:9b2611964afc 2969 // limit the result to the valid joystick range
mjr 53:9b2611964afc 2970 if (r.pos > JOYMAX)
mjr 53:9b2611964afc 2971 r.pos = JOYMAX;
mjr 53:9b2611964afc 2972 else if (r.pos < -JOYMAX)
mjr 53:9b2611964afc 2973 r.pos = -JOYMAX;
mjr 53:9b2611964afc 2974 }
mjr 50:40015764bbe6 2975
mjr 50:40015764bbe6 2976 // Calculate the velocity from the second-to-last reading
mjr 50:40015764bbe6 2977 // to here, in joystick distance units per microsecond.
mjr 50:40015764bbe6 2978 // Note that we use the second-to-last reading rather than
mjr 50:40015764bbe6 2979 // the very last reading to give ourselves a little longer
mjr 50:40015764bbe6 2980 // time base. The time base is so short between consecutive
mjr 50:40015764bbe6 2981 // readings that the error bars in the position would be too
mjr 50:40015764bbe6 2982 // large.
mjr 50:40015764bbe6 2983 //
mjr 50:40015764bbe6 2984 // For reference, the physical plunger velocity ranges up
mjr 50:40015764bbe6 2985 // to about 100,000 joystick distance units/sec. This is
mjr 50:40015764bbe6 2986 // based on empirical measurements. The typical time for
mjr 50:40015764bbe6 2987 // a real plunger to travel the full distance when released
mjr 50:40015764bbe6 2988 // from full retraction is about 85ms, so the average velocity
mjr 50:40015764bbe6 2989 // covering this distance is about 56,000 units/sec. The
mjr 50:40015764bbe6 2990 // peak is probably about twice that. In real-world units,
mjr 50:40015764bbe6 2991 // this translates to an average speed of about .75 m/s and
mjr 50:40015764bbe6 2992 // a peak of about 1.5 m/s.
mjr 50:40015764bbe6 2993 //
mjr 50:40015764bbe6 2994 // Note that we actually calculate the value here in units
mjr 50:40015764bbe6 2995 // per *microsecond* - the discussion above is in terms of
mjr 50:40015764bbe6 2996 // units/sec because that's more on a human scale. Our
mjr 50:40015764bbe6 2997 // choice of internal units here really isn't important,
mjr 50:40015764bbe6 2998 // since we only use the velocity for comparison purposes,
mjr 50:40015764bbe6 2999 // to detect acceleration trends. We therefore save ourselves
mjr 50:40015764bbe6 3000 // a little CPU time by using the natural units of our inputs.
mjr 51:57eb311faafa 3001 const PlungerReading &prv2 = nthHist(1);
mjr 50:40015764bbe6 3002 float v = float(r.pos - prv2.pos)/float(r.t - prv2.t);
mjr 50:40015764bbe6 3003
mjr 50:40015764bbe6 3004 // presume we'll report the latest instantaneous reading
mjr 50:40015764bbe6 3005 z = r.pos;
mjr 50:40015764bbe6 3006 vz = v;
mjr 48:058ace2aed1d 3007
mjr 50:40015764bbe6 3008 // Check firing events
mjr 50:40015764bbe6 3009 switch (firing)
mjr 50:40015764bbe6 3010 {
mjr 50:40015764bbe6 3011 case 0:
mjr 50:40015764bbe6 3012 // Default state - not in a firing event.
mjr 50:40015764bbe6 3013
mjr 50:40015764bbe6 3014 // If we have forward motion from a position that's retracted
mjr 50:40015764bbe6 3015 // beyond a threshold, enter phase 1. If we're not pulled back
mjr 50:40015764bbe6 3016 // far enough, don't bother with this, as a release wouldn't
mjr 50:40015764bbe6 3017 // be strong enough to require the synthetic firing treatment.
mjr 50:40015764bbe6 3018 if (v < 0 && r.pos > JOYMAX/6)
mjr 50:40015764bbe6 3019 {
mjr 53:9b2611964afc 3020 // enter firing phase 1
mjr 50:40015764bbe6 3021 firingMode(1);
mjr 50:40015764bbe6 3022
mjr 53:9b2611964afc 3023 // if in calibration state 1 (at rest), switch to state 2 (not
mjr 53:9b2611964afc 3024 // at rest)
mjr 53:9b2611964afc 3025 if (calState == 1)
mjr 53:9b2611964afc 3026 calState = 2;
mjr 53:9b2611964afc 3027
mjr 50:40015764bbe6 3028 // we don't have a freeze position yet, but note the start time
mjr 50:40015764bbe6 3029 f1.pos = 0;
mjr 50:40015764bbe6 3030 f1.t = r.t;
mjr 50:40015764bbe6 3031
mjr 50:40015764bbe6 3032 // Figure the barrel spring "bounce" position in case we complete
mjr 50:40015764bbe6 3033 // the firing event. This is the amount that the forward momentum
mjr 50:40015764bbe6 3034 // of the plunger will compress the barrel spring at the peak of
mjr 50:40015764bbe6 3035 // the forward travel during the release. Assume that this is
mjr 50:40015764bbe6 3036 // linearly proportional to the starting retraction distance.
mjr 50:40015764bbe6 3037 // The barrel spring is about 1/6 the length of the main spring,
mjr 50:40015764bbe6 3038 // so figure it compresses by 1/6 the distance. (This is overly
mjr 53:9b2611964afc 3039 // simplistic and not very accurate, but it seems to give good
mjr 50:40015764bbe6 3040 // visual results, and that's all it's for.)
mjr 50:40015764bbe6 3041 f2.pos = -r.pos/6;
mjr 50:40015764bbe6 3042 }
mjr 50:40015764bbe6 3043 break;
mjr 50:40015764bbe6 3044
mjr 50:40015764bbe6 3045 case 1:
mjr 50:40015764bbe6 3046 // Phase 1 - acceleration. If we cross the zero point, trigger
mjr 50:40015764bbe6 3047 // the firing event. Otherwise, continue monitoring as long as we
mjr 50:40015764bbe6 3048 // see acceleration in the forward direction.
mjr 50:40015764bbe6 3049 if (r.pos <= 0)
mjr 50:40015764bbe6 3050 {
mjr 50:40015764bbe6 3051 // switch to the synthetic firing mode
mjr 50:40015764bbe6 3052 firingMode(2);
mjr 50:40015764bbe6 3053 z = f2.pos;
mjr 50:40015764bbe6 3054
mjr 50:40015764bbe6 3055 // note the start time for the firing phase
mjr 50:40015764bbe6 3056 f2.t = r.t;
mjr 53:9b2611964afc 3057
mjr 53:9b2611964afc 3058 // if in calibration mode, and we're in state 2 (moving),
mjr 53:9b2611964afc 3059 // collect firing statistics for calibration purposes
mjr 53:9b2611964afc 3060 if (plungerCalMode && calState == 2)
mjr 53:9b2611964afc 3061 {
mjr 53:9b2611964afc 3062 // collect a new zero point for the average when we
mjr 53:9b2611964afc 3063 // come to rest
mjr 53:9b2611964afc 3064 calState = 0;
mjr 53:9b2611964afc 3065
mjr 53:9b2611964afc 3066 // collect average firing time statistics in millseconds, if
mjr 53:9b2611964afc 3067 // it's in range (20 to 255 ms)
mjr 53:9b2611964afc 3068 int dt = uint32_t(r.t - f1.t)/1000UL;
mjr 53:9b2611964afc 3069 if (dt >= 20 && dt <= 255)
mjr 53:9b2611964afc 3070 {
mjr 53:9b2611964afc 3071 calRlsTimeSum += dt;
mjr 53:9b2611964afc 3072 calRlsTimeN += 1;
mjr 53:9b2611964afc 3073 cfg.plunger.cal.tRelease = uint8_t(calRlsTimeSum / calRlsTimeN);
mjr 53:9b2611964afc 3074 }
mjr 53:9b2611964afc 3075 }
mjr 50:40015764bbe6 3076 }
mjr 50:40015764bbe6 3077 else if (v < vprv2)
mjr 50:40015764bbe6 3078 {
mjr 50:40015764bbe6 3079 // We're still accelerating, and we haven't crossed the zero
mjr 50:40015764bbe6 3080 // point yet - stay in phase 1. (Note that forward motion is
mjr 50:40015764bbe6 3081 // negative velocity, so accelerating means that the new
mjr 50:40015764bbe6 3082 // velocity is more negative than the previous one, which
mjr 50:40015764bbe6 3083 // is to say numerically less than - that's why the test
mjr 50:40015764bbe6 3084 // for acceleration is the seemingly backwards 'v < vprv'.)
mjr 50:40015764bbe6 3085
mjr 50:40015764bbe6 3086 // If we've been accelerating for at least 20ms, we're probably
mjr 50:40015764bbe6 3087 // really doing a release. Jump back to the recent local
mjr 50:40015764bbe6 3088 // maximum where the release *really* started. This is always
mjr 50:40015764bbe6 3089 // a bit before we started seeing sustained accleration, because
mjr 50:40015764bbe6 3090 // the plunger motion for the first few milliseconds is too slow
mjr 50:40015764bbe6 3091 // for our sensor precision to reliably detect acceleration.
mjr 50:40015764bbe6 3092 if (f1.pos != 0)
mjr 50:40015764bbe6 3093 {
mjr 50:40015764bbe6 3094 // we have a reset point - freeze there
mjr 50:40015764bbe6 3095 z = f1.pos;
mjr 50:40015764bbe6 3096 }
mjr 50:40015764bbe6 3097 else if (uint32_t(r.t - f1.t) >= 20000UL)
mjr 50:40015764bbe6 3098 {
mjr 50:40015764bbe6 3099 // it's been long enough - set a reset point.
mjr 50:40015764bbe6 3100 f1.pos = z = histLocalMax(r.t, 50000UL);
mjr 50:40015764bbe6 3101 }
mjr 50:40015764bbe6 3102 }
mjr 50:40015764bbe6 3103 else
mjr 50:40015764bbe6 3104 {
mjr 50:40015764bbe6 3105 // We're not accelerating. Cancel the firing event.
mjr 50:40015764bbe6 3106 firingMode(0);
mjr 53:9b2611964afc 3107 calState = 1;
mjr 50:40015764bbe6 3108 }
mjr 50:40015764bbe6 3109 break;
mjr 50:40015764bbe6 3110
mjr 50:40015764bbe6 3111 case 2:
mjr 50:40015764bbe6 3112 // Phase 2 - start of synthetic firing event. Report the fake
mjr 50:40015764bbe6 3113 // bounce for 25ms. VP polls the joystick about every 10ms, so
mjr 50:40015764bbe6 3114 // this should be enough time to guarantee that VP sees this
mjr 50:40015764bbe6 3115 // report at least once.
mjr 50:40015764bbe6 3116 if (uint32_t(r.t - f2.t) < 25000UL)
mjr 50:40015764bbe6 3117 {
mjr 50:40015764bbe6 3118 // report the bounce position
mjr 50:40015764bbe6 3119 z = f2.pos;
mjr 50:40015764bbe6 3120 }
mjr 50:40015764bbe6 3121 else
mjr 50:40015764bbe6 3122 {
mjr 50:40015764bbe6 3123 // it's been long enough - switch to phase 3, where we
mjr 50:40015764bbe6 3124 // report the park position until the real plunger comes
mjr 50:40015764bbe6 3125 // to rest
mjr 50:40015764bbe6 3126 firingMode(3);
mjr 50:40015764bbe6 3127 z = 0;
mjr 50:40015764bbe6 3128
mjr 50:40015764bbe6 3129 // set the start of the "stability window" to the rest position
mjr 50:40015764bbe6 3130 f3s.t = r.t;
mjr 50:40015764bbe6 3131 f3s.pos = 0;
mjr 50:40015764bbe6 3132
mjr 50:40015764bbe6 3133 // set the start of the "retraction window" to the actual position
mjr 50:40015764bbe6 3134 f3r = r;
mjr 50:40015764bbe6 3135 }
mjr 50:40015764bbe6 3136 break;
mjr 50:40015764bbe6 3137
mjr 50:40015764bbe6 3138 case 3:
mjr 50:40015764bbe6 3139 // Phase 3 - in synthetic firing event. Report the park position
mjr 50:40015764bbe6 3140 // until the plunger position stabilizes. Left to its own devices,
mjr 50:40015764bbe6 3141 // the plunger will usualy bounce off the barrel spring several
mjr 50:40015764bbe6 3142 // times before coming to rest, so we'll see oscillating motion
mjr 50:40015764bbe6 3143 // for a second or two. In the simplest case, we can aimply wait
mjr 50:40015764bbe6 3144 // for the plunger to stop moving for a short time. However, the
mjr 50:40015764bbe6 3145 // player might intervene by pulling the plunger back again, so
mjr 50:40015764bbe6 3146 // watch for that motion as well. If we're just bouncing freely,
mjr 50:40015764bbe6 3147 // we'll see the direction change frequently. If the player is
mjr 50:40015764bbe6 3148 // moving the plunger manually, the direction will be constant
mjr 50:40015764bbe6 3149 // for longer.
mjr 50:40015764bbe6 3150 if (v >= 0)
mjr 50:40015764bbe6 3151 {
mjr 50:40015764bbe6 3152 // We're moving back (or standing still). If this has been
mjr 50:40015764bbe6 3153 // going on for a while, the user must have taken control.
mjr 50:40015764bbe6 3154 if (uint32_t(r.t - f3r.t) > 65000UL)
mjr 50:40015764bbe6 3155 {
mjr 50:40015764bbe6 3156 // user has taken control - cancel firing mode
mjr 50:40015764bbe6 3157 firingMode(0);
mjr 50:40015764bbe6 3158 break;
mjr 50:40015764bbe6 3159 }
mjr 50:40015764bbe6 3160 }
mjr 50:40015764bbe6 3161 else
mjr 50:40015764bbe6 3162 {
mjr 50:40015764bbe6 3163 // forward motion - reset retraction window
mjr 50:40015764bbe6 3164 f3r.t = r.t;
mjr 50:40015764bbe6 3165 }
mjr 50:40015764bbe6 3166
mjr 53:9b2611964afc 3167 // Check if we're close to the last starting point. The joystick
mjr 53:9b2611964afc 3168 // positive axis range (0..4096) covers the retraction distance of
mjr 53:9b2611964afc 3169 // about 2.5", so 1" is about 1638 joystick units, hence 1/16" is
mjr 53:9b2611964afc 3170 // about 100 units.
mjr 53:9b2611964afc 3171 if (abs(r.pos - f3s.pos) < 100)
mjr 50:40015764bbe6 3172 {
mjr 53:9b2611964afc 3173 // It's at roughly the same position as the starting point.
mjr 53:9b2611964afc 3174 // Consider it stable if this has been true for 300ms.
mjr 50:40015764bbe6 3175 if (uint32_t(r.t - f3s.t) > 30000UL)
mjr 50:40015764bbe6 3176 {
mjr 50:40015764bbe6 3177 // we're done with the firing event
mjr 50:40015764bbe6 3178 firingMode(0);
mjr 50:40015764bbe6 3179 }
mjr 50:40015764bbe6 3180 else
mjr 50:40015764bbe6 3181 {
mjr 50:40015764bbe6 3182 // it's close to the last position but hasn't been
mjr 50:40015764bbe6 3183 // here long enough; stay in firing mode and continue
mjr 50:40015764bbe6 3184 // to report the park position
mjr 50:40015764bbe6 3185 z = 0;
mjr 50:40015764bbe6 3186 }
mjr 50:40015764bbe6 3187 }
mjr 50:40015764bbe6 3188 else
mjr 50:40015764bbe6 3189 {
mjr 50:40015764bbe6 3190 // It's not close enough to the last starting point, so use
mjr 50:40015764bbe6 3191 // this as a new starting point, and stay in firing mode.
mjr 50:40015764bbe6 3192 f3s = r;
mjr 50:40015764bbe6 3193 z = 0;
mjr 50:40015764bbe6 3194 }
mjr 50:40015764bbe6 3195 break;
mjr 50:40015764bbe6 3196 }
mjr 50:40015764bbe6 3197
mjr 50:40015764bbe6 3198 // save the velocity reading for next time
mjr 50:40015764bbe6 3199 vprv2 = vprv;
mjr 50:40015764bbe6 3200 vprv = v;
mjr 50:40015764bbe6 3201
mjr 50:40015764bbe6 3202 // add the new reading to the history
mjr 50:40015764bbe6 3203 hist[histIdx++] = r;
mjr 50:40015764bbe6 3204 histIdx %= countof(hist);
mjr 58:523fdcffbe6d 3205
mjr 58:523fdcffbe6d 3206 // figure the filtered value
mjr 58:523fdcffbe6d 3207 zf = applyFilter();
mjr 48:058ace2aed1d 3208 }
mjr 48:058ace2aed1d 3209 }
mjr 48:058ace2aed1d 3210
mjr 48:058ace2aed1d 3211 // Get the current value to report through the joystick interface
mjr 58:523fdcffbe6d 3212 int16_t getPosition()
mjr 58:523fdcffbe6d 3213 {
mjr 58:523fdcffbe6d 3214 // return the last filtered reading
mjr 58:523fdcffbe6d 3215 return zf;
mjr 55:4db125cd11a0 3216 }
mjr 58:523fdcffbe6d 3217
mjr 48:058ace2aed1d 3218 // Get the current velocity (joystick distance units per microsecond)
mjr 48:058ace2aed1d 3219 float getVelocity() const { return vz; }
mjr 48:058ace2aed1d 3220
mjr 48:058ace2aed1d 3221 // get the timestamp of the current joystick report (microseconds)
mjr 50:40015764bbe6 3222 uint32_t getTimestamp() const { return nthHist(0).t; }
mjr 48:058ace2aed1d 3223
mjr 48:058ace2aed1d 3224 // Set calibration mode on or off
mjr 52:8298b2a73eb2 3225 void setCalMode(bool f)
mjr 48:058ace2aed1d 3226 {
mjr 52:8298b2a73eb2 3227 // check to see if we're entering calibration mode
mjr 52:8298b2a73eb2 3228 if (f && !plungerCalMode)
mjr 52:8298b2a73eb2 3229 {
mjr 52:8298b2a73eb2 3230 // reset the calibration in the configuration
mjr 48:058ace2aed1d 3231 cfg.plunger.cal.begin();
mjr 52:8298b2a73eb2 3232
mjr 52:8298b2a73eb2 3233 // start in state 0 (waiting to settle)
mjr 52:8298b2a73eb2 3234 calState = 0;
mjr 52:8298b2a73eb2 3235 calZeroPosSum = 0;
mjr 52:8298b2a73eb2 3236 calZeroPosN = 0;
mjr 52:8298b2a73eb2 3237 calRlsTimeSum = 0;
mjr 52:8298b2a73eb2 3238 calRlsTimeN = 0;
mjr 52:8298b2a73eb2 3239
mjr 52:8298b2a73eb2 3240 // set the initial zero point to the current position
mjr 52:8298b2a73eb2 3241 PlungerReading r;
mjr 52:8298b2a73eb2 3242 if (plungerSensor->read(r))
mjr 52:8298b2a73eb2 3243 {
mjr 52:8298b2a73eb2 3244 // got a reading - use it as the initial zero point
mjr 52:8298b2a73eb2 3245 cfg.plunger.cal.zero = r.pos;
mjr 52:8298b2a73eb2 3246
mjr 52:8298b2a73eb2 3247 // use it as the starting point for the settling watch
mjr 53:9b2611964afc 3248 calZeroStart = r;
mjr 52:8298b2a73eb2 3249 }
mjr 52:8298b2a73eb2 3250 else
mjr 52:8298b2a73eb2 3251 {
mjr 52:8298b2a73eb2 3252 // no reading available - use the default 1/6 position
mjr 52:8298b2a73eb2 3253 cfg.plunger.cal.zero = 0xffff/6;
mjr 52:8298b2a73eb2 3254
mjr 52:8298b2a73eb2 3255 // we don't have a starting point for the setting watch
mjr 53:9b2611964afc 3256 calZeroStart.pos = -65535;
mjr 53:9b2611964afc 3257 calZeroStart.t = 0;
mjr 53:9b2611964afc 3258 }
mjr 53:9b2611964afc 3259 }
mjr 53:9b2611964afc 3260 else if (!f && plungerCalMode)
mjr 53:9b2611964afc 3261 {
mjr 53:9b2611964afc 3262 // Leaving calibration mode. Make sure the max is past the
mjr 53:9b2611964afc 3263 // zero point - if it's not, we'd have a zero or negative
mjr 53:9b2611964afc 3264 // denominator for the scaling calculation, which would be
mjr 53:9b2611964afc 3265 // physically meaningless.
mjr 53:9b2611964afc 3266 if (cfg.plunger.cal.max <= cfg.plunger.cal.zero)
mjr 53:9b2611964afc 3267 {
mjr 53:9b2611964afc 3268 // bad settings - reset to defaults
mjr 53:9b2611964afc 3269 cfg.plunger.cal.max = 0xffff;
mjr 53:9b2611964afc 3270 cfg.plunger.cal.zero = 0xffff/6;
mjr 52:8298b2a73eb2 3271 }
mjr 52:8298b2a73eb2 3272 }
mjr 52:8298b2a73eb2 3273
mjr 48:058ace2aed1d 3274 // remember the new mode
mjr 52:8298b2a73eb2 3275 plungerCalMode = f;
mjr 48:058ace2aed1d 3276 }
mjr 48:058ace2aed1d 3277
mjr 48:058ace2aed1d 3278 // is a firing event in progress?
mjr 53:9b2611964afc 3279 bool isFiring() { return firing == 3; }
mjr 48:058ace2aed1d 3280
mjr 48:058ace2aed1d 3281 private:
mjr 52:8298b2a73eb2 3282
mjr 58:523fdcffbe6d 3283 // Figure the next filtered value. This applies the hysteresis
mjr 58:523fdcffbe6d 3284 // filter to the last raw z value and returns the filtered result.
mjr 58:523fdcffbe6d 3285 int applyFilter()
mjr 58:523fdcffbe6d 3286 {
mjr 58:523fdcffbe6d 3287 if (firing <= 1)
mjr 58:523fdcffbe6d 3288 {
mjr 58:523fdcffbe6d 3289 // Filter limit - 5 samples. Once we've been moving
mjr 58:523fdcffbe6d 3290 // in the same direction for this many samples, we'll
mjr 58:523fdcffbe6d 3291 // clear the history and start over.
mjr 58:523fdcffbe6d 3292 const int filterMask = 0x1f;
mjr 58:523fdcffbe6d 3293
mjr 58:523fdcffbe6d 3294 // figure the last average
mjr 58:523fdcffbe6d 3295 int lastAvg = int(filterSum / filterN);
mjr 58:523fdcffbe6d 3296
mjr 58:523fdcffbe6d 3297 // figure the direction of this sample relative to the average,
mjr 58:523fdcffbe6d 3298 // and shift it in to our bit mask of recent direction data
mjr 58:523fdcffbe6d 3299 if (z != lastAvg)
mjr 58:523fdcffbe6d 3300 {
mjr 58:523fdcffbe6d 3301 // shift the new direction bit into the vector
mjr 58:523fdcffbe6d 3302 filterDir <<= 1;
mjr 58:523fdcffbe6d 3303 if (z > lastAvg) filterDir |= 1;
mjr 58:523fdcffbe6d 3304 }
mjr 58:523fdcffbe6d 3305
mjr 58:523fdcffbe6d 3306 // keep only the last N readings, up to the filter limit
mjr 58:523fdcffbe6d 3307 filterDir &= filterMask;
mjr 58:523fdcffbe6d 3308
mjr 58:523fdcffbe6d 3309 // if we've been moving consistently in one direction (all 1's
mjr 58:523fdcffbe6d 3310 // or all 0's in the direction history vector), reset the average
mjr 58:523fdcffbe6d 3311 if (filterDir == 0x00 || filterDir == filterMask)
mjr 58:523fdcffbe6d 3312 {
mjr 58:523fdcffbe6d 3313 // motion away from the average - reset the average
mjr 58:523fdcffbe6d 3314 filterDir = 0x5555;
mjr 58:523fdcffbe6d 3315 filterN = 1;
mjr 58:523fdcffbe6d 3316 filterSum = (lastAvg + z)/2;
mjr 58:523fdcffbe6d 3317 return int16_t(filterSum);
mjr 58:523fdcffbe6d 3318 }
mjr 58:523fdcffbe6d 3319 else
mjr 58:523fdcffbe6d 3320 {
mjr 58:523fdcffbe6d 3321 // we're diretionless - return the new average, with the
mjr 58:523fdcffbe6d 3322 // new sample included
mjr 58:523fdcffbe6d 3323 filterSum += z;
mjr 58:523fdcffbe6d 3324 ++filterN;
mjr 58:523fdcffbe6d 3325 return int16_t(filterSum / filterN);
mjr 58:523fdcffbe6d 3326 }
mjr 58:523fdcffbe6d 3327 }
mjr 58:523fdcffbe6d 3328 else
mjr 58:523fdcffbe6d 3329 {
mjr 58:523fdcffbe6d 3330 // firing mode - skip the filter
mjr 58:523fdcffbe6d 3331 filterN = 1;
mjr 58:523fdcffbe6d 3332 filterSum = z;
mjr 58:523fdcffbe6d 3333 filterDir = 0x5555;
mjr 58:523fdcffbe6d 3334 return z;
mjr 58:523fdcffbe6d 3335 }
mjr 58:523fdcffbe6d 3336 }
mjr 58:523fdcffbe6d 3337
mjr 58:523fdcffbe6d 3338 void initFilter()
mjr 58:523fdcffbe6d 3339 {
mjr 58:523fdcffbe6d 3340 filterSum = 0;
mjr 58:523fdcffbe6d 3341 filterN = 1;
mjr 58:523fdcffbe6d 3342 filterDir = 0x5555;
mjr 58:523fdcffbe6d 3343 }
mjr 58:523fdcffbe6d 3344 int64_t filterSum;
mjr 58:523fdcffbe6d 3345 int64_t filterN;
mjr 58:523fdcffbe6d 3346 uint16_t filterDir;
mjr 58:523fdcffbe6d 3347
mjr 58:523fdcffbe6d 3348
mjr 52:8298b2a73eb2 3349 // Calibration state. During calibration mode, we watch for release
mjr 52:8298b2a73eb2 3350 // events, to measure the time it takes to complete the release
mjr 52:8298b2a73eb2 3351 // motion; and we watch for the plunger to come to reset after a
mjr 52:8298b2a73eb2 3352 // release, to gather statistics on the rest position.
mjr 52:8298b2a73eb2 3353 // 0 = waiting to settle
mjr 52:8298b2a73eb2 3354 // 1 = at rest
mjr 52:8298b2a73eb2 3355 // 2 = retracting
mjr 52:8298b2a73eb2 3356 // 3 = possibly releasing
mjr 52:8298b2a73eb2 3357 uint8_t calState;
mjr 52:8298b2a73eb2 3358
mjr 52:8298b2a73eb2 3359 // Calibration zero point statistics.
mjr 52:8298b2a73eb2 3360 // During calibration mode, we collect data on the rest position (the
mjr 52:8298b2a73eb2 3361 // zero point) by watching for the plunger to come to rest after each
mjr 52:8298b2a73eb2 3362 // release. We average these rest positions to get the calibrated
mjr 52:8298b2a73eb2 3363 // zero point. We use the average because the real physical plunger
mjr 52:8298b2a73eb2 3364 // itself doesn't come to rest at exactly the same spot every time,
mjr 52:8298b2a73eb2 3365 // largely due to friction in the mechanism. To calculate the average,
mjr 52:8298b2a73eb2 3366 // we keep a sum of the readings and a count of samples.
mjr 53:9b2611964afc 3367 PlungerReading calZeroStart;
mjr 52:8298b2a73eb2 3368 long calZeroPosSum;
mjr 52:8298b2a73eb2 3369 int calZeroPosN;
mjr 52:8298b2a73eb2 3370
mjr 52:8298b2a73eb2 3371 // Calibration release time statistics.
mjr 52:8298b2a73eb2 3372 // During calibration, we collect an average for the release time.
mjr 52:8298b2a73eb2 3373 long calRlsTimeSum;
mjr 52:8298b2a73eb2 3374 int calRlsTimeN;
mjr 52:8298b2a73eb2 3375
mjr 48:058ace2aed1d 3376 // set a firing mode
mjr 48:058ace2aed1d 3377 inline void firingMode(int m)
mjr 48:058ace2aed1d 3378 {
mjr 48:058ace2aed1d 3379 firing = m;
mjr 48:058ace2aed1d 3380 }
mjr 48:058ace2aed1d 3381
mjr 48:058ace2aed1d 3382 // Find the most recent local maximum in the history data, up to
mjr 48:058ace2aed1d 3383 // the given time limit.
mjr 48:058ace2aed1d 3384 int histLocalMax(uint32_t tcur, uint32_t dt)
mjr 48:058ace2aed1d 3385 {
mjr 48:058ace2aed1d 3386 // start with the prior entry
mjr 48:058ace2aed1d 3387 int idx = (histIdx == 0 ? countof(hist) : histIdx) - 1;
mjr 48:058ace2aed1d 3388 int hi = hist[idx].pos;
mjr 48:058ace2aed1d 3389
mjr 48:058ace2aed1d 3390 // scan backwards for a local maximum
mjr 48:058ace2aed1d 3391 for (int n = countof(hist) - 1 ; n > 0 ; idx = (idx == 0 ? countof(hist) : idx) - 1)
mjr 48:058ace2aed1d 3392 {
mjr 48:058ace2aed1d 3393 // if this isn't within the time window, stop
mjr 48:058ace2aed1d 3394 if (uint32_t(tcur - hist[idx].t) > dt)
mjr 48:058ace2aed1d 3395 break;
mjr 48:058ace2aed1d 3396
mjr 48:058ace2aed1d 3397 // if this isn't above the current hith, stop
mjr 48:058ace2aed1d 3398 if (hist[idx].pos < hi)
mjr 48:058ace2aed1d 3399 break;
mjr 48:058ace2aed1d 3400
mjr 48:058ace2aed1d 3401 // this is the new high
mjr 48:058ace2aed1d 3402 hi = hist[idx].pos;
mjr 48:058ace2aed1d 3403 }
mjr 48:058ace2aed1d 3404
mjr 48:058ace2aed1d 3405 // return the local maximum
mjr 48:058ace2aed1d 3406 return hi;
mjr 48:058ace2aed1d 3407 }
mjr 48:058ace2aed1d 3408
mjr 50:40015764bbe6 3409 // velocity at previous reading, and the one before that
mjr 50:40015764bbe6 3410 float vprv, vprv2;
mjr 48:058ace2aed1d 3411
mjr 48:058ace2aed1d 3412 // Circular buffer of recent readings. We keep a short history
mjr 48:058ace2aed1d 3413 // of readings to analyze during firing events. We can only identify
mjr 48:058ace2aed1d 3414 // a firing event once it's somewhat under way, so we need a little
mjr 48:058ace2aed1d 3415 // retrospective information to accurately determine after the fact
mjr 48:058ace2aed1d 3416 // exactly when it started. We throttle our readings to no more
mjr 48:058ace2aed1d 3417 // than one every 2ms, so we have at least N*2ms of history in this
mjr 48:058ace2aed1d 3418 // array.
mjr 50:40015764bbe6 3419 PlungerReading hist[25];
mjr 48:058ace2aed1d 3420 int histIdx;
mjr 49:37bd97eb7688 3421
mjr 50:40015764bbe6 3422 // get the nth history item (0=last, 1=2nd to last, etc)
mjr 50:40015764bbe6 3423 const PlungerReading &nthHist(int n) const
mjr 50:40015764bbe6 3424 {
mjr 50:40015764bbe6 3425 // histIdx-1 is the last written; go from there
mjr 50:40015764bbe6 3426 n = histIdx - 1 - n;
mjr 50:40015764bbe6 3427
mjr 50:40015764bbe6 3428 // adjust for wrapping
mjr 50:40015764bbe6 3429 if (n < 0)
mjr 50:40015764bbe6 3430 n += countof(hist);
mjr 50:40015764bbe6 3431
mjr 50:40015764bbe6 3432 // return the item
mjr 50:40015764bbe6 3433 return hist[n];
mjr 50:40015764bbe6 3434 }
mjr 48:058ace2aed1d 3435
mjr 48:058ace2aed1d 3436 // Firing event state.
mjr 48:058ace2aed1d 3437 //
mjr 48:058ace2aed1d 3438 // 0 - Default state. We report the real instantaneous plunger
mjr 48:058ace2aed1d 3439 // position to the joystick interface.
mjr 48:058ace2aed1d 3440 //
mjr 53:9b2611964afc 3441 // 1 - Moving forward
mjr 48:058ace2aed1d 3442 //
mjr 53:9b2611964afc 3443 // 2 - Accelerating
mjr 48:058ace2aed1d 3444 //
mjr 53:9b2611964afc 3445 // 3 - Firing. We report the rest position for a minimum interval,
mjr 53:9b2611964afc 3446 // or until the real plunger comes to rest somewhere.
mjr 48:058ace2aed1d 3447 //
mjr 48:058ace2aed1d 3448 int firing;
mjr 48:058ace2aed1d 3449
mjr 51:57eb311faafa 3450 // Position/timestamp at start of firing phase 1. When we see a
mjr 51:57eb311faafa 3451 // sustained forward acceleration, we freeze joystick reports at
mjr 51:57eb311faafa 3452 // the recent local maximum, on the assumption that this was the
mjr 51:57eb311faafa 3453 // start of the release. If this is zero, it means that we're
mjr 51:57eb311faafa 3454 // monitoring accelerating motion but haven't seen it for long
mjr 51:57eb311faafa 3455 // enough yet to be confident that a release is in progress.
mjr 48:058ace2aed1d 3456 PlungerReading f1;
mjr 48:058ace2aed1d 3457
mjr 48:058ace2aed1d 3458 // Position/timestamp at start of firing phase 2. The position is
mjr 48:058ace2aed1d 3459 // the fake "bounce" position we report during this phase, and the
mjr 48:058ace2aed1d 3460 // timestamp tells us when the phase began so that we can end it
mjr 48:058ace2aed1d 3461 // after enough time elapses.
mjr 48:058ace2aed1d 3462 PlungerReading f2;
mjr 48:058ace2aed1d 3463
mjr 48:058ace2aed1d 3464 // Position/timestamp of start of stability window during phase 3.
mjr 48:058ace2aed1d 3465 // We use this to determine when the plunger comes to rest. We set
mjr 51:57eb311faafa 3466 // this at the beginning of phase 3, and then reset it when the
mjr 48:058ace2aed1d 3467 // plunger moves too far from the last position.
mjr 48:058ace2aed1d 3468 PlungerReading f3s;
mjr 48:058ace2aed1d 3469
mjr 48:058ace2aed1d 3470 // Position/timestamp of start of retraction window during phase 3.
mjr 48:058ace2aed1d 3471 // We use this to determine if the user is drawing the plunger back.
mjr 48:058ace2aed1d 3472 // If we see retraction motion for more than about 65ms, we assume
mjr 48:058ace2aed1d 3473 // that the user has taken over, because we should see forward
mjr 48:058ace2aed1d 3474 // motion within this timeframe if the plunger is just bouncing
mjr 48:058ace2aed1d 3475 // freely.
mjr 48:058ace2aed1d 3476 PlungerReading f3r;
mjr 48:058ace2aed1d 3477
mjr 58:523fdcffbe6d 3478 // next raw (unfiltered) Z value to report to the joystick interface
mjr 58:523fdcffbe6d 3479 // (in joystick distance units)
mjr 48:058ace2aed1d 3480 int z;
mjr 48:058ace2aed1d 3481
mjr 48:058ace2aed1d 3482 // velocity of this reading (joystick distance units per microsecond)
mjr 48:058ace2aed1d 3483 float vz;
mjr 58:523fdcffbe6d 3484
mjr 58:523fdcffbe6d 3485 // next filtered Z value to report to the joystick interface
mjr 58:523fdcffbe6d 3486 int zf;
mjr 48:058ace2aed1d 3487 };
mjr 48:058ace2aed1d 3488
mjr 48:058ace2aed1d 3489 // plunger reader singleton
mjr 48:058ace2aed1d 3490 PlungerReader plungerReader;
mjr 48:058ace2aed1d 3491
mjr 48:058ace2aed1d 3492 // ---------------------------------------------------------------------------
mjr 48:058ace2aed1d 3493 //
mjr 48:058ace2aed1d 3494 // Handle the ZB Launch Ball feature.
mjr 48:058ace2aed1d 3495 //
mjr 48:058ace2aed1d 3496 // The ZB Launch Ball feature, if enabled, lets the mechanical plunger
mjr 48:058ace2aed1d 3497 // serve as a substitute for a physical Launch Ball button. When a table
mjr 48:058ace2aed1d 3498 // is loaded in VP, and the table has the ZB Launch Ball LedWiz port
mjr 48:058ace2aed1d 3499 // turned on, we'll disable mechanical plunger reports through the
mjr 48:058ace2aed1d 3500 // joystick interface and instead use the plunger only to simulate the
mjr 48:058ace2aed1d 3501 // Launch Ball button. When the mode is active, pulling back and
mjr 48:058ace2aed1d 3502 // releasing the plunger causes a brief simulated press of the Launch
mjr 48:058ace2aed1d 3503 // button, and pushing the plunger forward of the rest position presses
mjr 48:058ace2aed1d 3504 // the Launch button as long as the plunger is pressed forward.
mjr 48:058ace2aed1d 3505 //
mjr 48:058ace2aed1d 3506 // This feature has two configuration components:
mjr 48:058ace2aed1d 3507 //
mjr 48:058ace2aed1d 3508 // - An LedWiz port number. This port is a "virtual" port that doesn't
mjr 48:058ace2aed1d 3509 // have to be attached to any actual output. DOF uses it to signal
mjr 48:058ace2aed1d 3510 // that the current table uses a Launch button instead of a plunger.
mjr 48:058ace2aed1d 3511 // DOF simply turns the port on when such a table is loaded and turns
mjr 48:058ace2aed1d 3512 // it off at all other times. We use it to enable and disable the
mjr 48:058ace2aed1d 3513 // plunger/launch button connection.
mjr 48:058ace2aed1d 3514 //
mjr 48:058ace2aed1d 3515 // - A joystick button ID. We simulate pressing this button when the
mjr 48:058ace2aed1d 3516 // launch feature is activated via the LedWiz port and the plunger is
mjr 48:058ace2aed1d 3517 // either pulled back and releasd, or pushed forward past the rest
mjr 48:058ace2aed1d 3518 // position.
mjr 48:058ace2aed1d 3519 //
mjr 48:058ace2aed1d 3520 class ZBLaunchBall
mjr 48:058ace2aed1d 3521 {
mjr 48:058ace2aed1d 3522 public:
mjr 48:058ace2aed1d 3523 ZBLaunchBall()
mjr 48:058ace2aed1d 3524 {
mjr 48:058ace2aed1d 3525 // start in the default state
mjr 48:058ace2aed1d 3526 lbState = 0;
mjr 53:9b2611964afc 3527 btnState = false;
mjr 48:058ace2aed1d 3528 }
mjr 48:058ace2aed1d 3529
mjr 48:058ace2aed1d 3530 // Update state. This checks the current plunger position and
mjr 48:058ace2aed1d 3531 // the timers to see if the plunger is in a position that simulates
mjr 48:058ace2aed1d 3532 // a Launch Ball button press via the ZB Launch Ball feature.
mjr 48:058ace2aed1d 3533 // Updates the simulated button vector according to the current
mjr 48:058ace2aed1d 3534 // launch ball state. The main loop calls this before each
mjr 48:058ace2aed1d 3535 // joystick update to figure the new simulated button state.
mjr 53:9b2611964afc 3536 void update()
mjr 48:058ace2aed1d 3537 {
mjr 53:9b2611964afc 3538 // If the ZB Launch Ball led wiz output is ON, check for a
mjr 53:9b2611964afc 3539 // plunger firing event
mjr 53:9b2611964afc 3540 if (zbLaunchOn)
mjr 48:058ace2aed1d 3541 {
mjr 53:9b2611964afc 3542 // note the new position
mjr 48:058ace2aed1d 3543 int znew = plungerReader.getPosition();
mjr 53:9b2611964afc 3544
mjr 53:9b2611964afc 3545 // figure the push threshold from the configuration data
mjr 51:57eb311faafa 3546 const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
mjr 53:9b2611964afc 3547
mjr 53:9b2611964afc 3548 // check the state
mjr 48:058ace2aed1d 3549 switch (lbState)
mjr 48:058ace2aed1d 3550 {
mjr 48:058ace2aed1d 3551 case 0:
mjr 53:9b2611964afc 3552 // Default state. If a launch event has been detected on
mjr 53:9b2611964afc 3553 // the plunger, activate a timed pulse and switch to state 1.
mjr 53:9b2611964afc 3554 // If the plunger is pushed forward of the threshold, push
mjr 53:9b2611964afc 3555 // the button.
mjr 53:9b2611964afc 3556 if (plungerReader.isFiring())
mjr 53:9b2611964afc 3557 {
mjr 53:9b2611964afc 3558 // firing event - start a timed Launch button pulse
mjr 53:9b2611964afc 3559 lbTimer.reset();
mjr 53:9b2611964afc 3560 lbTimer.start();
mjr 53:9b2611964afc 3561 setButton(true);
mjr 53:9b2611964afc 3562
mjr 53:9b2611964afc 3563 // switch to state 1
mjr 53:9b2611964afc 3564 lbState = 1;
mjr 53:9b2611964afc 3565 }
mjr 48:058ace2aed1d 3566 else if (znew <= pushThreshold)
mjr 53:9b2611964afc 3567 {
mjr 53:9b2611964afc 3568 // pushed forward without a firing event - hold the
mjr 53:9b2611964afc 3569 // button as long as we're pushed forward
mjr 53:9b2611964afc 3570 setButton(true);
mjr 53:9b2611964afc 3571 }
mjr 53:9b2611964afc 3572 else
mjr 53:9b2611964afc 3573 {
mjr 53:9b2611964afc 3574 // not pushed forward - turn off the Launch button
mjr 53:9b2611964afc 3575 setButton(false);
mjr 53:9b2611964afc 3576 }
mjr 48:058ace2aed1d 3577 break;
mjr 48:058ace2aed1d 3578
mjr 48:058ace2aed1d 3579 case 1:
mjr 53:9b2611964afc 3580 // State 1: Timed Launch button pulse in progress after a
mjr 53:9b2611964afc 3581 // firing event. Wait for the timer to expire.
mjr 53:9b2611964afc 3582 if (lbTimer.read_us() > 200000UL)
mjr 53:9b2611964afc 3583 {
mjr 53:9b2611964afc 3584 // timer expired - turn off the button
mjr 53:9b2611964afc 3585 setButton(false);
mjr 53:9b2611964afc 3586
mjr 53:9b2611964afc 3587 // switch to state 2
mjr 53:9b2611964afc 3588 lbState = 2;
mjr 53:9b2611964afc 3589 }
mjr 48:058ace2aed1d 3590 break;
mjr 48:058ace2aed1d 3591
mjr 48:058ace2aed1d 3592 case 2:
mjr 53:9b2611964afc 3593 // State 2: Timed Launch button pulse done. Wait for the
mjr 53:9b2611964afc 3594 // plunger launch event to end.
mjr 53:9b2611964afc 3595 if (!plungerReader.isFiring())
mjr 53:9b2611964afc 3596 {
mjr 53:9b2611964afc 3597 // firing event done - return to default state
mjr 53:9b2611964afc 3598 lbState = 0;
mjr 53:9b2611964afc 3599 }
mjr 48:058ace2aed1d 3600 break;
mjr 48:058ace2aed1d 3601 }
mjr 53:9b2611964afc 3602 }
mjr 53:9b2611964afc 3603 else
mjr 53:9b2611964afc 3604 {
mjr 53:9b2611964afc 3605 // ZB Launch Ball disabled - turn off the button if it was on
mjr 53:9b2611964afc 3606 setButton(false);
mjr 48:058ace2aed1d 3607
mjr 53:9b2611964afc 3608 // return to the default state
mjr 53:9b2611964afc 3609 lbState = 0;
mjr 48:058ace2aed1d 3610 }
mjr 48:058ace2aed1d 3611 }
mjr 53:9b2611964afc 3612
mjr 53:9b2611964afc 3613 // Set the button state
mjr 53:9b2611964afc 3614 void setButton(bool on)
mjr 53:9b2611964afc 3615 {
mjr 53:9b2611964afc 3616 if (btnState != on)
mjr 53:9b2611964afc 3617 {
mjr 53:9b2611964afc 3618 // remember the new state
mjr 53:9b2611964afc 3619 btnState = on;
mjr 53:9b2611964afc 3620
mjr 53:9b2611964afc 3621 // update the virtual button state
mjr 53:9b2611964afc 3622 buttonState[ZBL_BUTTON].virtPress(on);
mjr 53:9b2611964afc 3623 }
mjr 53:9b2611964afc 3624 }
mjr 53:9b2611964afc 3625
mjr 48:058ace2aed1d 3626 private:
mjr 48:058ace2aed1d 3627 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 48:058ace2aed1d 3628 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 48:058ace2aed1d 3629 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 48:058ace2aed1d 3630 // back and releases the plunger, or simply pushes on the plunger from
mjr 48:058ace2aed1d 3631 // the rest position. This allows the plunger to be used in lieu of a
mjr 48:058ace2aed1d 3632 // physical Launch Ball button for tables that don't have plungers.
mjr 48:058ace2aed1d 3633 //
mjr 48:058ace2aed1d 3634 // States:
mjr 48:058ace2aed1d 3635 // 0 = default
mjr 53:9b2611964afc 3636 // 1 = firing (firing event has activated a Launch button pulse)
mjr 53:9b2611964afc 3637 // 2 = firing done (Launch button pulse ended, waiting for plunger
mjr 53:9b2611964afc 3638 // firing event to end)
mjr 53:9b2611964afc 3639 uint8_t lbState;
mjr 48:058ace2aed1d 3640
mjr 53:9b2611964afc 3641 // button state
mjr 53:9b2611964afc 3642 bool btnState;
mjr 48:058ace2aed1d 3643
mjr 48:058ace2aed1d 3644 // Time since last lbState transition. Some of the states are time-
mjr 48:058ace2aed1d 3645 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 48:058ace2aed1d 3646 // we remain in this state for more than a few milliseconds, since
mjr 48:058ace2aed1d 3647 // it indicates that the plunger is being slowly returned to rest
mjr 48:058ace2aed1d 3648 // rather than released. In the "launching" state, we need to release
mjr 48:058ace2aed1d 3649 // the Launch Ball button after a moment, and we need to wait for
mjr 48:058ace2aed1d 3650 // the plunger to come to rest before returning to state 0.
mjr 48:058ace2aed1d 3651 Timer lbTimer;
mjr 48:058ace2aed1d 3652 };
mjr 48:058ace2aed1d 3653
mjr 35:e959ffba78fd 3654 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 3655 //
mjr 35:e959ffba78fd 3656 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 3657 //
mjr 54:fd77a6b2f76c 3658 void reboot(USBJoystick &js, bool disconnect = true, long pause_us = 2000000L)
mjr 35:e959ffba78fd 3659 {
mjr 35:e959ffba78fd 3660 // disconnect from USB
mjr 54:fd77a6b2f76c 3661 if (disconnect)
mjr 54:fd77a6b2f76c 3662 js.disconnect();
mjr 35:e959ffba78fd 3663
mjr 35:e959ffba78fd 3664 // wait a few seconds to make sure the host notices the disconnect
mjr 54:fd77a6b2f76c 3665 wait_us(pause_us);
mjr 35:e959ffba78fd 3666
mjr 35:e959ffba78fd 3667 // reset the device
mjr 35:e959ffba78fd 3668 NVIC_SystemReset();
mjr 35:e959ffba78fd 3669 while (true) { }
mjr 35:e959ffba78fd 3670 }
mjr 35:e959ffba78fd 3671
mjr 35:e959ffba78fd 3672 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 3673 //
mjr 35:e959ffba78fd 3674 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 3675 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 3676 //
mjr 35:e959ffba78fd 3677 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 3678 {
mjr 35:e959ffba78fd 3679 int tmp;
mjr 35:e959ffba78fd 3680 switch (cfg.orientation)
mjr 35:e959ffba78fd 3681 {
mjr 35:e959ffba78fd 3682 case OrientationFront:
mjr 35:e959ffba78fd 3683 tmp = x;
mjr 35:e959ffba78fd 3684 x = y;
mjr 35:e959ffba78fd 3685 y = tmp;
mjr 35:e959ffba78fd 3686 break;
mjr 35:e959ffba78fd 3687
mjr 35:e959ffba78fd 3688 case OrientationLeft:
mjr 35:e959ffba78fd 3689 x = -x;
mjr 35:e959ffba78fd 3690 break;
mjr 35:e959ffba78fd 3691
mjr 35:e959ffba78fd 3692 case OrientationRight:
mjr 35:e959ffba78fd 3693 y = -y;
mjr 35:e959ffba78fd 3694 break;
mjr 35:e959ffba78fd 3695
mjr 35:e959ffba78fd 3696 case OrientationRear:
mjr 35:e959ffba78fd 3697 tmp = -x;
mjr 35:e959ffba78fd 3698 x = -y;
mjr 35:e959ffba78fd 3699 y = tmp;
mjr 35:e959ffba78fd 3700 break;
mjr 35:e959ffba78fd 3701 }
mjr 35:e959ffba78fd 3702 }
mjr 35:e959ffba78fd 3703
mjr 35:e959ffba78fd 3704 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 3705 //
mjr 35:e959ffba78fd 3706 // Calibration button state:
mjr 35:e959ffba78fd 3707 // 0 = not pushed
mjr 35:e959ffba78fd 3708 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 3709 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 3710 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 3711 int calBtnState = 0;
mjr 35:e959ffba78fd 3712
mjr 35:e959ffba78fd 3713 // calibration button debounce timer
mjr 35:e959ffba78fd 3714 Timer calBtnTimer;
mjr 35:e959ffba78fd 3715
mjr 35:e959ffba78fd 3716 // calibration button light state
mjr 35:e959ffba78fd 3717 int calBtnLit = false;
mjr 35:e959ffba78fd 3718
mjr 35:e959ffba78fd 3719
mjr 35:e959ffba78fd 3720 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 3721 //
mjr 40:cc0d9814522b 3722 // Configuration variable get/set message handling
mjr 35:e959ffba78fd 3723 //
mjr 40:cc0d9814522b 3724
mjr 40:cc0d9814522b 3725 // Handle SET messages - write configuration variables from USB message data
mjr 40:cc0d9814522b 3726 #define if_msg_valid(test) if (test)
mjr 53:9b2611964afc 3727 #define v_byte(var, ofs) cfg.var = data[ofs]
mjr 53:9b2611964afc 3728 #define v_ui16(var, ofs) cfg.var = wireUI16(data+(ofs))
mjr 53:9b2611964afc 3729 #define v_pin(var, ofs) cfg.var = wirePinName(data[ofs])
mjr 53:9b2611964afc 3730 #define v_byte_ro(val, ofs) // ignore read-only variables on SET
mjr 40:cc0d9814522b 3731 #define v_func configVarSet
mjr 40:cc0d9814522b 3732 #include "cfgVarMsgMap.h"
mjr 35:e959ffba78fd 3733
mjr 40:cc0d9814522b 3734 // redefine everything for the SET messages
mjr 40:cc0d9814522b 3735 #undef if_msg_valid
mjr 40:cc0d9814522b 3736 #undef v_byte
mjr 40:cc0d9814522b 3737 #undef v_ui16
mjr 40:cc0d9814522b 3738 #undef v_pin
mjr 53:9b2611964afc 3739 #undef v_byte_ro
mjr 40:cc0d9814522b 3740 #undef v_func
mjr 38:091e511ce8a0 3741
mjr 40:cc0d9814522b 3742 // Handle GET messages - read variable values and return in USB message daa
mjr 40:cc0d9814522b 3743 #define if_msg_valid(test)
mjr 53:9b2611964afc 3744 #define v_byte(var, ofs) data[ofs] = cfg.var
mjr 53:9b2611964afc 3745 #define v_ui16(var, ofs) ui16Wire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 3746 #define v_pin(var, ofs) pinNameWire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 3747 #define v_byte_ro(val, ofs) data[ofs] = val
mjr 40:cc0d9814522b 3748 #define v_func configVarGet
mjr 40:cc0d9814522b 3749 #include "cfgVarMsgMap.h"
mjr 40:cc0d9814522b 3750
mjr 35:e959ffba78fd 3751
mjr 35:e959ffba78fd 3752 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 3753 //
mjr 35:e959ffba78fd 3754 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 3755 // LedWiz protocol.
mjr 33:d832bcab089e 3756 //
mjr 48:058ace2aed1d 3757 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js)
mjr 35:e959ffba78fd 3758 {
mjr 38:091e511ce8a0 3759 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 3760 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 3761 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 3762 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 3763 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 3764 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 3765 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 3766 // So our full protocol is as follows:
mjr 38:091e511ce8a0 3767 //
mjr 38:091e511ce8a0 3768 // first byte =
mjr 38:091e511ce8a0 3769 // 0-48 -> LWZ-PBA
mjr 38:091e511ce8a0 3770 // 64 -> LWZ SBA
mjr 38:091e511ce8a0 3771 // 65 -> private control message; second byte specifies subtype
mjr 38:091e511ce8a0 3772 // 129-132 -> LWZ-PBA
mjr 38:091e511ce8a0 3773 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 3774 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 3775 // other -> reserved for future use
mjr 38:091e511ce8a0 3776 //
mjr 39:b3815a1c3802 3777 uint8_t *data = lwm.data;
mjr 38:091e511ce8a0 3778 if (data[0] == 64)
mjr 35:e959ffba78fd 3779 {
mjr 38:091e511ce8a0 3780 // LWZ-SBA - first four bytes are bit-packed on/off flags
mjr 38:091e511ce8a0 3781 // for the outputs; 5th byte is the pulse speed (1-7)
mjr 38:091e511ce8a0 3782 //printf("LWZ-SBA %02x %02x %02x %02x ; %02x\r\n",
mjr 38:091e511ce8a0 3783 // data[1], data[2], data[3], data[4], data[5]);
mjr 38:091e511ce8a0 3784
mjr 38:091e511ce8a0 3785 // update all on/off states
mjr 38:091e511ce8a0 3786 for (int i = 0, bit = 1, ri = 1 ; i < numLwOutputs ; ++i, bit <<= 1)
mjr 35:e959ffba78fd 3787 {
mjr 38:091e511ce8a0 3788 // figure the on/off state bit for this output
mjr 38:091e511ce8a0 3789 if (bit == 0x100) {
mjr 38:091e511ce8a0 3790 bit = 1;
mjr 38:091e511ce8a0 3791 ++ri;
mjr 35:e959ffba78fd 3792 }
mjr 35:e959ffba78fd 3793
mjr 38:091e511ce8a0 3794 // set the on/off state
mjr 38:091e511ce8a0 3795 wizOn[i] = ((data[ri] & bit) != 0);
mjr 38:091e511ce8a0 3796
mjr 38:091e511ce8a0 3797 // If the wizVal setting is 255, it means that this
mjr 38:091e511ce8a0 3798 // output was last set to a brightness value with the
mjr 38:091e511ce8a0 3799 // extended protocol. Return it to LedWiz control by
mjr 38:091e511ce8a0 3800 // rescaling the brightness setting to the LedWiz range
mjr 38:091e511ce8a0 3801 // and updating wizVal with the result. If it's any
mjr 38:091e511ce8a0 3802 // other value, it was previously set by a PBA message,
mjr 38:091e511ce8a0 3803 // so simply retain the last setting - in the normal
mjr 38:091e511ce8a0 3804 // LedWiz protocol, the "profile" (brightness) and on/off
mjr 38:091e511ce8a0 3805 // states are independent, so an SBA just turns an output
mjr 38:091e511ce8a0 3806 // on or off but retains its last brightness level.
mjr 38:091e511ce8a0 3807 if (wizVal[i] == 255)
mjr 40:cc0d9814522b 3808 wizVal[i] = (uint8_t)round(outLevel[i]/255.0 * 48.0);
mjr 38:091e511ce8a0 3809 }
mjr 38:091e511ce8a0 3810
mjr 38:091e511ce8a0 3811 // set the flash speed - enforce the value range 1-7
mjr 38:091e511ce8a0 3812 wizSpeed = data[5];
mjr 38:091e511ce8a0 3813 if (wizSpeed < 1)
mjr 38:091e511ce8a0 3814 wizSpeed = 1;
mjr 38:091e511ce8a0 3815 else if (wizSpeed > 7)
mjr 38:091e511ce8a0 3816 wizSpeed = 7;
mjr 38:091e511ce8a0 3817
mjr 38:091e511ce8a0 3818 // update the physical outputs
mjr 38:091e511ce8a0 3819 updateWizOuts();
mjr 38:091e511ce8a0 3820 if (hc595 != 0)
mjr 38:091e511ce8a0 3821 hc595->update();
mjr 38:091e511ce8a0 3822
mjr 38:091e511ce8a0 3823 // reset the PBA counter
mjr 38:091e511ce8a0 3824 pbaIdx = 0;
mjr 38:091e511ce8a0 3825 }
mjr 38:091e511ce8a0 3826 else if (data[0] == 65)
mjr 38:091e511ce8a0 3827 {
mjr 38:091e511ce8a0 3828 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 3829 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 3830 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 3831 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 3832 // message type.
mjr 39:b3815a1c3802 3833 switch (data[1])
mjr 38:091e511ce8a0 3834 {
mjr 39:b3815a1c3802 3835 case 0:
mjr 39:b3815a1c3802 3836 // No Op
mjr 39:b3815a1c3802 3837 break;
mjr 39:b3815a1c3802 3838
mjr 39:b3815a1c3802 3839 case 1:
mjr 38:091e511ce8a0 3840 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 3841 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 3842 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 3843 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 3844 {
mjr 39:b3815a1c3802 3845
mjr 39:b3815a1c3802 3846 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 3847 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 3848 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 3849
mjr 39:b3815a1c3802 3850 // we'll need a reset if the LedWiz unit number is changing
mjr 39:b3815a1c3802 3851 bool needReset = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 3852
mjr 39:b3815a1c3802 3853 // set the configuration parameters from the message
mjr 39:b3815a1c3802 3854 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 3855 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 3856
mjr 39:b3815a1c3802 3857 // save the configuration
mjr 39:b3815a1c3802 3858 saveConfigToFlash();
mjr 39:b3815a1c3802 3859
mjr 39:b3815a1c3802 3860 // reboot if necessary
mjr 39:b3815a1c3802 3861 if (needReset)
mjr 39:b3815a1c3802 3862 reboot(js);
mjr 39:b3815a1c3802 3863 }
mjr 39:b3815a1c3802 3864 break;
mjr 38:091e511ce8a0 3865
mjr 39:b3815a1c3802 3866 case 2:
mjr 38:091e511ce8a0 3867 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 3868 // (No parameters)
mjr 38:091e511ce8a0 3869
mjr 38:091e511ce8a0 3870 // enter calibration mode
mjr 38:091e511ce8a0 3871 calBtnState = 3;
mjr 52:8298b2a73eb2 3872 plungerReader.setCalMode(true);
mjr 38:091e511ce8a0 3873 calBtnTimer.reset();
mjr 39:b3815a1c3802 3874 break;
mjr 39:b3815a1c3802 3875
mjr 39:b3815a1c3802 3876 case 3:
mjr 52:8298b2a73eb2 3877 // 3 = plunger sensor status report
mjr 48:058ace2aed1d 3878 // data[2] = flag bits
mjr 53:9b2611964afc 3879 // data[3] = extra exposure time, 100us (.1ms) increments
mjr 52:8298b2a73eb2 3880 reportPlungerStat = true;
mjr 53:9b2611964afc 3881 reportPlungerStatFlags = data[2];
mjr 53:9b2611964afc 3882 reportPlungerStatTime = data[3];
mjr 38:091e511ce8a0 3883
mjr 38:091e511ce8a0 3884 // show purple until we finish sending the report
mjr 38:091e511ce8a0 3885 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 3886 break;
mjr 39:b3815a1c3802 3887
mjr 39:b3815a1c3802 3888 case 4:
mjr 38:091e511ce8a0 3889 // 4 = hardware configuration query
mjr 38:091e511ce8a0 3890 // (No parameters)
mjr 38:091e511ce8a0 3891 js.reportConfig(
mjr 38:091e511ce8a0 3892 numOutputs,
mjr 38:091e511ce8a0 3893 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 52:8298b2a73eb2 3894 cfg.plunger.cal.zero, cfg.plunger.cal.max, cfg.plunger.cal.tRelease,
mjr 40:cc0d9814522b 3895 nvm.valid());
mjr 39:b3815a1c3802 3896 break;
mjr 39:b3815a1c3802 3897
mjr 39:b3815a1c3802 3898 case 5:
mjr 38:091e511ce8a0 3899 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 3900 allOutputsOff();
mjr 39:b3815a1c3802 3901 break;
mjr 39:b3815a1c3802 3902
mjr 39:b3815a1c3802 3903 case 6:
mjr 38:091e511ce8a0 3904 // 6 = Save configuration to flash.
mjr 38:091e511ce8a0 3905 saveConfigToFlash();
mjr 38:091e511ce8a0 3906
mjr 53:9b2611964afc 3907 // before disconnecting, pause for the delay time specified in
mjr 53:9b2611964afc 3908 // the parameter byte (in seconds)
mjr 53:9b2611964afc 3909 rebootTime_us = data[2] * 1000000L;
mjr 53:9b2611964afc 3910 rebootTimer.start();
mjr 39:b3815a1c3802 3911 break;
mjr 40:cc0d9814522b 3912
mjr 40:cc0d9814522b 3913 case 7:
mjr 40:cc0d9814522b 3914 // 7 = Device ID report
mjr 53:9b2611964afc 3915 // data[2] = ID index: 1=CPU ID, 2=OpenSDA TUID
mjr 53:9b2611964afc 3916 js.reportID(data[2]);
mjr 40:cc0d9814522b 3917 break;
mjr 40:cc0d9814522b 3918
mjr 40:cc0d9814522b 3919 case 8:
mjr 40:cc0d9814522b 3920 // 8 = Engage/disengage night mode.
mjr 40:cc0d9814522b 3921 // data[2] = 1 to engage, 0 to disengage
mjr 40:cc0d9814522b 3922 setNightMode(data[2]);
mjr 40:cc0d9814522b 3923 break;
mjr 52:8298b2a73eb2 3924
mjr 52:8298b2a73eb2 3925 case 9:
mjr 52:8298b2a73eb2 3926 // 9 = Config variable query.
mjr 52:8298b2a73eb2 3927 // data[2] = config var ID
mjr 52:8298b2a73eb2 3928 // data[3] = array index (for array vars: button assignments, output ports)
mjr 52:8298b2a73eb2 3929 {
mjr 53:9b2611964afc 3930 // set up the reply buffer with the variable ID data, and zero out
mjr 53:9b2611964afc 3931 // the rest of the buffer
mjr 52:8298b2a73eb2 3932 uint8_t reply[8];
mjr 52:8298b2a73eb2 3933 reply[1] = data[2];
mjr 52:8298b2a73eb2 3934 reply[2] = data[3];
mjr 53:9b2611964afc 3935 memset(reply+3, 0, sizeof(reply)-3);
mjr 52:8298b2a73eb2 3936
mjr 52:8298b2a73eb2 3937 // query the value
mjr 52:8298b2a73eb2 3938 configVarGet(reply);
mjr 52:8298b2a73eb2 3939
mjr 52:8298b2a73eb2 3940 // send the reply
mjr 52:8298b2a73eb2 3941 js.reportConfigVar(reply + 1);
mjr 52:8298b2a73eb2 3942 }
mjr 52:8298b2a73eb2 3943 break;
mjr 53:9b2611964afc 3944
mjr 53:9b2611964afc 3945 case 10:
mjr 53:9b2611964afc 3946 // 10 = Build ID query.
mjr 53:9b2611964afc 3947 js.reportBuildInfo(getBuildID());
mjr 53:9b2611964afc 3948 break;
mjr 38:091e511ce8a0 3949 }
mjr 38:091e511ce8a0 3950 }
mjr 38:091e511ce8a0 3951 else if (data[0] == 66)
mjr 38:091e511ce8a0 3952 {
mjr 38:091e511ce8a0 3953 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 3954 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 3955 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 3956 // in a variable-dependent format.
mjr 40:cc0d9814522b 3957 configVarSet(data);
mjr 38:091e511ce8a0 3958 }
mjr 38:091e511ce8a0 3959 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 3960 {
mjr 38:091e511ce8a0 3961 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 3962 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 3963 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 3964 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 3965 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 3966 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 3967 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 3968 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 3969 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 3970 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 3971 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 3972 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 3973 //
mjr 38:091e511ce8a0 3974 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 3975 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 3976 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 3977 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 3978 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 3979 // address those ports anyway.
mjr 38:091e511ce8a0 3980 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 3981 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 38:091e511ce8a0 3982 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 3983 {
mjr 38:091e511ce8a0 3984 // set the brightness level for the output
mjr 40:cc0d9814522b 3985 uint8_t b = data[i-i0+1];
mjr 38:091e511ce8a0 3986 outLevel[i] = b;
mjr 38:091e511ce8a0 3987
mjr 38:091e511ce8a0 3988 // if it's in the basic LedWiz output set, set the LedWiz
mjr 38:091e511ce8a0 3989 // profile value to 255, which means "use outLevel"
mjr 38:091e511ce8a0 3990 if (i < 32)
mjr 38:091e511ce8a0 3991 wizVal[i] = 255;
mjr 38:091e511ce8a0 3992
mjr 38:091e511ce8a0 3993 // set the output
mjr 40:cc0d9814522b 3994 lwPin[i]->set(b);
mjr 38:091e511ce8a0 3995 }
mjr 38:091e511ce8a0 3996
mjr 38:091e511ce8a0 3997 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 3998 if (hc595 != 0)
mjr 38:091e511ce8a0 3999 hc595->update();
mjr 38:091e511ce8a0 4000 }
mjr 38:091e511ce8a0 4001 else
mjr 38:091e511ce8a0 4002 {
mjr 38:091e511ce8a0 4003 // Everything else is LWZ-PBA. This is a full "profile"
mjr 38:091e511ce8a0 4004 // dump from the host for one bank of 8 outputs. Each
mjr 38:091e511ce8a0 4005 // byte sets one output in the current bank. The current
mjr 38:091e511ce8a0 4006 // bank is implied; the bank starts at 0 and is reset to 0
mjr 38:091e511ce8a0 4007 // by any LWZ-SBA message, and is incremented to the next
mjr 38:091e511ce8a0 4008 // bank by each LWZ-PBA message. Our variable pbaIdx keeps
mjr 38:091e511ce8a0 4009 // track of our notion of the current bank. There's no direct
mjr 38:091e511ce8a0 4010 // way for the host to select the bank; it just has to count
mjr 38:091e511ce8a0 4011 // on us staying in sync. In practice, the host will always
mjr 38:091e511ce8a0 4012 // send a full set of 4 PBA messages in a row to set all 32
mjr 38:091e511ce8a0 4013 // outputs.
mjr 38:091e511ce8a0 4014 //
mjr 38:091e511ce8a0 4015 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 4016 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 4017 // wizVal[] entry for each output, and that takes precedence
mjr 38:091e511ce8a0 4018 // over the extended protocol settings.
mjr 38:091e511ce8a0 4019 //
mjr 38:091e511ce8a0 4020 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 4021 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 4022
mjr 38:091e511ce8a0 4023 // Update all output profile settings
mjr 38:091e511ce8a0 4024 for (int i = 0 ; i < 8 ; ++i)
mjr 38:091e511ce8a0 4025 wizVal[pbaIdx + i] = data[i];
mjr 38:091e511ce8a0 4026
mjr 38:091e511ce8a0 4027 // Update the physical LED state if this is the last bank.
mjr 38:091e511ce8a0 4028 // Note that hosts always send a full set of four PBA
mjr 38:091e511ce8a0 4029 // messages, so there's no need to do a physical update
mjr 38:091e511ce8a0 4030 // until we've received the last bank's PBA message.
mjr 38:091e511ce8a0 4031 if (pbaIdx == 24)
mjr 38:091e511ce8a0 4032 {
mjr 35:e959ffba78fd 4033 updateWizOuts();
mjr 35:e959ffba78fd 4034 if (hc595 != 0)
mjr 35:e959ffba78fd 4035 hc595->update();
mjr 35:e959ffba78fd 4036 pbaIdx = 0;
mjr 35:e959ffba78fd 4037 }
mjr 38:091e511ce8a0 4038 else
mjr 38:091e511ce8a0 4039 pbaIdx += 8;
mjr 38:091e511ce8a0 4040 }
mjr 38:091e511ce8a0 4041 }
mjr 35:e959ffba78fd 4042
mjr 33:d832bcab089e 4043
mjr 38:091e511ce8a0 4044 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 4045 //
mjr 5:a70c0bce770d 4046 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 4047 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 4048 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 4049 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 4050 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 4051 // port outputs.
mjr 5:a70c0bce770d 4052 //
mjr 0:5acbbe3f4cf4 4053 int main(void)
mjr 0:5acbbe3f4cf4 4054 {
mjr 60:f38da020aa13 4055 // say hello to the debug console, in case it's connected
mjr 39:b3815a1c3802 4056 printf("\r\nPinscape Controller starting\r\n");
mjr 60:f38da020aa13 4057
mjr 60:f38da020aa13 4058 // debugging: print memory config info
mjr 59:94eb9265b6d7 4059 // -> no longer very useful, since we use our own custom malloc/new allocator (see xmalloc() above)
mjr 60:f38da020aa13 4060 // {int *a = new int; printf("Stack=%lx, heap=%lx, free=%ld\r\n", (long)&a, (long)a, (long)&a - (long)a);}
mjr 1:d913e0afb2ac 4061
mjr 39:b3815a1c3802 4062 // clear the I2C bus (for the accelerometer)
mjr 35:e959ffba78fd 4063 clear_i2c();
mjr 38:091e511ce8a0 4064
mjr 43:7a6364d82a41 4065 // load the saved configuration (or set factory defaults if no flash
mjr 43:7a6364d82a41 4066 // configuration has ever been saved)
mjr 35:e959ffba78fd 4067 loadConfigFromFlash();
mjr 35:e959ffba78fd 4068
mjr 38:091e511ce8a0 4069 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 4070 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 4071
mjr 33:d832bcab089e 4072 // we're not connected/awake yet
mjr 33:d832bcab089e 4073 bool connected = false;
mjr 40:cc0d9814522b 4074 Timer connectChangeTimer;
mjr 33:d832bcab089e 4075
mjr 35:e959ffba78fd 4076 // create the plunger sensor interface
mjr 35:e959ffba78fd 4077 createPlunger();
mjr 33:d832bcab089e 4078
mjr 60:f38da020aa13 4079 // set up the TLC5940 interface, if these chips are present
mjr 35:e959ffba78fd 4080 init_tlc5940(cfg);
mjr 34:6b981a2afab7 4081
mjr 60:f38da020aa13 4082 // set up 74HC595 interface, if these chips are present
mjr 35:e959ffba78fd 4083 init_hc595(cfg);
mjr 6:cc35eb643e8f 4084
mjr 54:fd77a6b2f76c 4085 // Initialize the LedWiz ports. Note that the ordering here is important:
mjr 54:fd77a6b2f76c 4086 // this has to come after we create the TLC5940 and 74HC595 object instances
mjr 54:fd77a6b2f76c 4087 // (which we just did above), since we need to access those objects to set
mjr 54:fd77a6b2f76c 4088 // up ports assigned to the respective chips.
mjr 35:e959ffba78fd 4089 initLwOut(cfg);
mjr 48:058ace2aed1d 4090
mjr 60:f38da020aa13 4091 // start the TLC5940 refresh cycle clock
mjr 35:e959ffba78fd 4092 if (tlc5940 != 0)
mjr 35:e959ffba78fd 4093 tlc5940->start();
mjr 35:e959ffba78fd 4094
mjr 40:cc0d9814522b 4095 // start the TV timer, if applicable
mjr 40:cc0d9814522b 4096 startTVTimer(cfg);
mjr 48:058ace2aed1d 4097
mjr 35:e959ffba78fd 4098 // initialize the button input ports
mjr 35:e959ffba78fd 4099 bool kbKeys = false;
mjr 35:e959ffba78fd 4100 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 4101
mjr 60:f38da020aa13 4102 // Create the joystick USB client. Note that the USB vendor/product ID
mjr 60:f38da020aa13 4103 // information comes from the saved configuration. Also note that we have
mjr 60:f38da020aa13 4104 // to wait until after initializing the input buttons (which we just did
mjr 60:f38da020aa13 4105 // above) to set up the interface, since the button setup will determine
mjr 60:f38da020aa13 4106 // whether or not we need to present a USB keyboard interface in addition
mjr 60:f38da020aa13 4107 // to the joystick interface.
mjr 51:57eb311faafa 4108 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false,
mjr 51:57eb311faafa 4109 cfg.joystickEnabled, kbKeys);
mjr 51:57eb311faafa 4110
mjr 60:f38da020aa13 4111 // Wait for the USB connection to start up. Show a distinctive diagnostic
mjr 60:f38da020aa13 4112 // flash pattern while waiting.
mjr 51:57eb311faafa 4113 Timer connectTimer;
mjr 51:57eb311faafa 4114 connectTimer.start();
mjr 51:57eb311faafa 4115 while (!js.configured())
mjr 51:57eb311faafa 4116 {
mjr 51:57eb311faafa 4117 // show one short yellow flash at 2-second intervals
mjr 51:57eb311faafa 4118 if (connectTimer.read_us() > 2000000)
mjr 51:57eb311faafa 4119 {
mjr 51:57eb311faafa 4120 // short yellow flash
mjr 51:57eb311faafa 4121 diagLED(1, 1, 0);
mjr 54:fd77a6b2f76c 4122 wait_us(50000);
mjr 51:57eb311faafa 4123 diagLED(0, 0, 0);
mjr 51:57eb311faafa 4124
mjr 51:57eb311faafa 4125 // reset the flash timer
mjr 51:57eb311faafa 4126 connectTimer.reset();
mjr 51:57eb311faafa 4127 }
mjr 51:57eb311faafa 4128 }
mjr 60:f38da020aa13 4129
mjr 60:f38da020aa13 4130 // we're now connected to the host
mjr 54:fd77a6b2f76c 4131 connected = true;
mjr 40:cc0d9814522b 4132
mjr 60:f38da020aa13 4133 // Last report timer for the joytick interface. We use this timer to
mjr 60:f38da020aa13 4134 // throttle the report rate to a pace that's suitable for VP. Without
mjr 60:f38da020aa13 4135 // any artificial delays, we could generate data to send on the joystick
mjr 60:f38da020aa13 4136 // interface on every loop iteration. The loop iteration time depends
mjr 60:f38da020aa13 4137 // on which devices are attached, since most of the work in our main
mjr 60:f38da020aa13 4138 // loop is simply polling our devices. For typical setups, the loop
mjr 60:f38da020aa13 4139 // time ranges from about 0.25ms to 2.5ms; the biggest factor is the
mjr 60:f38da020aa13 4140 // plunger sensor. But VP polls for input about every 10ms, so there's
mjr 60:f38da020aa13 4141 // no benefit in sending data faster than that, and there's some harm,
mjr 60:f38da020aa13 4142 // in that it creates USB overhead (both on the wire and on the host
mjr 60:f38da020aa13 4143 // CPU). We therefore use this timer to pace our reports to roughly
mjr 60:f38da020aa13 4144 // the VP input polling rate. Note that there's no way to actually
mjr 60:f38da020aa13 4145 // synchronize with VP's polling, but there's also no need to, as the
mjr 60:f38da020aa13 4146 // input model is designed to reflect the overall current state at any
mjr 60:f38da020aa13 4147 // given time rather than events or deltas. If VP polls twice between
mjr 60:f38da020aa13 4148 // two updates, it simply sees no state change; if we send two updates
mjr 60:f38da020aa13 4149 // between VP polls, VP simply sees the latest state when it does get
mjr 60:f38da020aa13 4150 // around to polling.
mjr 38:091e511ce8a0 4151 Timer jsReportTimer;
mjr 38:091e511ce8a0 4152 jsReportTimer.start();
mjr 38:091e511ce8a0 4153
mjr 60:f38da020aa13 4154 // Time since we successfully sent a USB report. This is a hacky
mjr 60:f38da020aa13 4155 // workaround to deal with any remaining sporadic problems in the USB
mjr 60:f38da020aa13 4156 // stack. I've been trying to bulletproof the USB code over time to
mjr 60:f38da020aa13 4157 // remove all such problems at their source, but it seems unlikely that
mjr 60:f38da020aa13 4158 // we'll ever get them all. Thus this hack. The idea here is that if
mjr 60:f38da020aa13 4159 // we go too long without successfully sending a USB report, we'll
mjr 60:f38da020aa13 4160 // assume that the connection is broken (and the KL25Z USB hardware
mjr 60:f38da020aa13 4161 // hasn't noticed this), and we'll try taking measures to recover.
mjr 38:091e511ce8a0 4162 Timer jsOKTimer;
mjr 38:091e511ce8a0 4163 jsOKTimer.start();
mjr 35:e959ffba78fd 4164
mjr 55:4db125cd11a0 4165 // Initialize the calibration button and lamp, if enabled. To be enabled,
mjr 55:4db125cd11a0 4166 // the pin has to be assigned to something other than NC (0xFF), AND the
mjr 55:4db125cd11a0 4167 // corresponding feature enable flag has to be set.
mjr 55:4db125cd11a0 4168 DigitalIn *calBtn = 0;
mjr 55:4db125cd11a0 4169 DigitalOut *calBtnLed = 0;
mjr 55:4db125cd11a0 4170
mjr 55:4db125cd11a0 4171 // calibration button input - feature flag 0x01
mjr 55:4db125cd11a0 4172 if ((cfg.plunger.cal.features & 0x01) && cfg.plunger.cal.btn != 0xFF)
mjr 55:4db125cd11a0 4173 calBtn = new DigitalIn(wirePinName(cfg.plunger.cal.btn));
mjr 55:4db125cd11a0 4174
mjr 55:4db125cd11a0 4175 // calibration button indicator lamp output - feature flag 0x02
mjr 55:4db125cd11a0 4176 if ((cfg.plunger.cal.features & 0x02) && cfg.plunger.cal.led != 0xFF)
mjr 55:4db125cd11a0 4177 calBtnLed = new DigitalOut(wirePinName(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 4178
mjr 35:e959ffba78fd 4179 // initialize the calibration button
mjr 1:d913e0afb2ac 4180 calBtnTimer.start();
mjr 35:e959ffba78fd 4181 calBtnState = 0;
mjr 1:d913e0afb2ac 4182
mjr 1:d913e0afb2ac 4183 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 4184 Timer hbTimer;
mjr 1:d913e0afb2ac 4185 hbTimer.start();
mjr 1:d913e0afb2ac 4186 int hb = 0;
mjr 5:a70c0bce770d 4187 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 4188
mjr 1:d913e0afb2ac 4189 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 4190 Timer acTimer;
mjr 1:d913e0afb2ac 4191 acTimer.start();
mjr 1:d913e0afb2ac 4192
mjr 0:5acbbe3f4cf4 4193 // create the accelerometer object
mjr 5:a70c0bce770d 4194 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS, MMA8451_INT_PIN);
mjr 48:058ace2aed1d 4195
mjr 17:ab3cec0c8bf4 4196 // last accelerometer report, in joystick units (we report the nudge
mjr 17:ab3cec0c8bf4 4197 // acceleration via the joystick x & y axes, per the VP convention)
mjr 17:ab3cec0c8bf4 4198 int x = 0, y = 0;
mjr 17:ab3cec0c8bf4 4199
mjr 48:058ace2aed1d 4200 // initialize the plunger sensor
mjr 35:e959ffba78fd 4201 plungerSensor->init();
mjr 10:976666ffa4ef 4202
mjr 48:058ace2aed1d 4203 // set up the ZB Launch Ball monitor
mjr 48:058ace2aed1d 4204 ZBLaunchBall zbLaunchBall;
mjr 48:058ace2aed1d 4205
mjr 54:fd77a6b2f76c 4206 // enable the peripheral chips
mjr 54:fd77a6b2f76c 4207 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 4208 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 4209 if (hc595 != 0)
mjr 54:fd77a6b2f76c 4210 hc595->enable(true);
mjr 43:7a6364d82a41 4211
mjr 1:d913e0afb2ac 4212 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 4213 // host requests
mjr 0:5acbbe3f4cf4 4214 for (;;)
mjr 0:5acbbe3f4cf4 4215 {
mjr 48:058ace2aed1d 4216 // Process incoming reports on the joystick interface. The joystick
mjr 48:058ace2aed1d 4217 // "out" (receive) endpoint is used for LedWiz commands and our
mjr 48:058ace2aed1d 4218 // extended protocol commands. Limit processing time to 5ms to
mjr 48:058ace2aed1d 4219 // ensure we don't starve the input side.
mjr 39:b3815a1c3802 4220 LedWizMsg lwm;
mjr 48:058ace2aed1d 4221 Timer lwt;
mjr 48:058ace2aed1d 4222 lwt.start();
mjr 48:058ace2aed1d 4223 while (js.readLedWizMsg(lwm) && lwt.read_us() < 5000)
mjr 48:058ace2aed1d 4224 handleInputMsg(lwm, js);
mjr 55:4db125cd11a0 4225
mjr 55:4db125cd11a0 4226 // send TLC5940 data updates if applicable
mjr 55:4db125cd11a0 4227 if (tlc5940 != 0)
mjr 55:4db125cd11a0 4228 tlc5940->send();
mjr 1:d913e0afb2ac 4229
mjr 1:d913e0afb2ac 4230 // check for plunger calibration
mjr 17:ab3cec0c8bf4 4231 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 4232 {
mjr 1:d913e0afb2ac 4233 // check the state
mjr 1:d913e0afb2ac 4234 switch (calBtnState)
mjr 0:5acbbe3f4cf4 4235 {
mjr 1:d913e0afb2ac 4236 case 0:
mjr 1:d913e0afb2ac 4237 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 4238 calBtnTimer.reset();
mjr 1:d913e0afb2ac 4239 calBtnState = 1;
mjr 1:d913e0afb2ac 4240 break;
mjr 1:d913e0afb2ac 4241
mjr 1:d913e0afb2ac 4242 case 1:
mjr 1:d913e0afb2ac 4243 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 4244 // passed, start the hold period
mjr 48:058ace2aed1d 4245 if (calBtnTimer.read_us() > 50000)
mjr 1:d913e0afb2ac 4246 calBtnState = 2;
mjr 1:d913e0afb2ac 4247 break;
mjr 1:d913e0afb2ac 4248
mjr 1:d913e0afb2ac 4249 case 2:
mjr 1:d913e0afb2ac 4250 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 4251 // for the entire hold period, move to calibration mode
mjr 48:058ace2aed1d 4252 if (calBtnTimer.read_us() > 2050000)
mjr 1:d913e0afb2ac 4253 {
mjr 1:d913e0afb2ac 4254 // enter calibration mode
mjr 1:d913e0afb2ac 4255 calBtnState = 3;
mjr 9:fd65b0a94720 4256 calBtnTimer.reset();
mjr 35:e959ffba78fd 4257
mjr 44:b5ac89b9cd5d 4258 // begin the plunger calibration limits
mjr 52:8298b2a73eb2 4259 plungerReader.setCalMode(true);
mjr 1:d913e0afb2ac 4260 }
mjr 1:d913e0afb2ac 4261 break;
mjr 2:c174f9ee414a 4262
mjr 2:c174f9ee414a 4263 case 3:
mjr 9:fd65b0a94720 4264 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 4265 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 4266 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 4267 break;
mjr 0:5acbbe3f4cf4 4268 }
mjr 0:5acbbe3f4cf4 4269 }
mjr 1:d913e0afb2ac 4270 else
mjr 1:d913e0afb2ac 4271 {
mjr 2:c174f9ee414a 4272 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 4273 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 4274 // and save the results to flash.
mjr 2:c174f9ee414a 4275 //
mjr 2:c174f9ee414a 4276 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 4277 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 4278 // mode, it simply cancels the attempt.
mjr 48:058ace2aed1d 4279 if (calBtnState == 3 && calBtnTimer.read_us() > 15000000)
mjr 2:c174f9ee414a 4280 {
mjr 2:c174f9ee414a 4281 // exit calibration mode
mjr 1:d913e0afb2ac 4282 calBtnState = 0;
mjr 52:8298b2a73eb2 4283 plungerReader.setCalMode(false);
mjr 2:c174f9ee414a 4284
mjr 6:cc35eb643e8f 4285 // save the updated configuration
mjr 35:e959ffba78fd 4286 cfg.plunger.cal.calibrated = 1;
mjr 35:e959ffba78fd 4287 saveConfigToFlash();
mjr 2:c174f9ee414a 4288 }
mjr 2:c174f9ee414a 4289 else if (calBtnState != 3)
mjr 2:c174f9ee414a 4290 {
mjr 2:c174f9ee414a 4291 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 4292 calBtnState = 0;
mjr 2:c174f9ee414a 4293 }
mjr 1:d913e0afb2ac 4294 }
mjr 1:d913e0afb2ac 4295
mjr 1:d913e0afb2ac 4296 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 4297 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 4298 switch (calBtnState)
mjr 0:5acbbe3f4cf4 4299 {
mjr 1:d913e0afb2ac 4300 case 2:
mjr 1:d913e0afb2ac 4301 // in the hold period - flash the light
mjr 48:058ace2aed1d 4302 newCalBtnLit = ((calBtnTimer.read_us()/250000) & 1);
mjr 1:d913e0afb2ac 4303 break;
mjr 1:d913e0afb2ac 4304
mjr 1:d913e0afb2ac 4305 case 3:
mjr 1:d913e0afb2ac 4306 // calibration mode - show steady on
mjr 1:d913e0afb2ac 4307 newCalBtnLit = true;
mjr 1:d913e0afb2ac 4308 break;
mjr 1:d913e0afb2ac 4309
mjr 1:d913e0afb2ac 4310 default:
mjr 1:d913e0afb2ac 4311 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 4312 newCalBtnLit = false;
mjr 1:d913e0afb2ac 4313 break;
mjr 1:d913e0afb2ac 4314 }
mjr 3:3514575d4f86 4315
mjr 3:3514575d4f86 4316 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 4317 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 4318 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 4319 {
mjr 1:d913e0afb2ac 4320 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 4321 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 4322 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 4323 calBtnLed->write(1);
mjr 38:091e511ce8a0 4324 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 4325 }
mjr 2:c174f9ee414a 4326 else {
mjr 17:ab3cec0c8bf4 4327 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 4328 calBtnLed->write(0);
mjr 38:091e511ce8a0 4329 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 4330 }
mjr 1:d913e0afb2ac 4331 }
mjr 35:e959ffba78fd 4332
mjr 48:058ace2aed1d 4333 // read the plunger sensor
mjr 48:058ace2aed1d 4334 plungerReader.read();
mjr 48:058ace2aed1d 4335
mjr 53:9b2611964afc 4336 // update the ZB Launch Ball status
mjr 53:9b2611964afc 4337 zbLaunchBall.update();
mjr 37:ed52738445fc 4338
mjr 53:9b2611964afc 4339 // process button updates
mjr 53:9b2611964afc 4340 processButtons(cfg);
mjr 53:9b2611964afc 4341
mjr 38:091e511ce8a0 4342 // send a keyboard report if we have new data
mjr 37:ed52738445fc 4343 if (kbState.changed)
mjr 37:ed52738445fc 4344 {
mjr 38:091e511ce8a0 4345 // send a keyboard report
mjr 37:ed52738445fc 4346 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 4347 kbState.changed = false;
mjr 37:ed52738445fc 4348 }
mjr 38:091e511ce8a0 4349
mjr 38:091e511ce8a0 4350 // likewise for the media controller
mjr 37:ed52738445fc 4351 if (mediaState.changed)
mjr 37:ed52738445fc 4352 {
mjr 38:091e511ce8a0 4353 // send a media report
mjr 37:ed52738445fc 4354 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 4355 mediaState.changed = false;
mjr 37:ed52738445fc 4356 }
mjr 38:091e511ce8a0 4357
mjr 38:091e511ce8a0 4358 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 4359 bool jsOK = false;
mjr 55:4db125cd11a0 4360
mjr 55:4db125cd11a0 4361 // figure the current status flags for joystick reports
mjr 55:4db125cd11a0 4362 uint16_t statusFlags =
mjr 55:4db125cd11a0 4363 (cfg.plunger.enabled ? 0x01 : 0x00)
mjr 55:4db125cd11a0 4364 | (nightMode ? 0x02 : 0x00);
mjr 17:ab3cec0c8bf4 4365
mjr 50:40015764bbe6 4366 // If it's been long enough since our last USB status report, send
mjr 50:40015764bbe6 4367 // the new report. VP only polls for input in 10ms intervals, so
mjr 50:40015764bbe6 4368 // there's no benefit in sending reports more frequently than this.
mjr 50:40015764bbe6 4369 // More frequent reporting would only add USB I/O overhead.
mjr 50:40015764bbe6 4370 if (cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 17:ab3cec0c8bf4 4371 {
mjr 17:ab3cec0c8bf4 4372 // read the accelerometer
mjr 17:ab3cec0c8bf4 4373 int xa, ya;
mjr 17:ab3cec0c8bf4 4374 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 4375
mjr 17:ab3cec0c8bf4 4376 // confine the results to our joystick axis range
mjr 17:ab3cec0c8bf4 4377 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 17:ab3cec0c8bf4 4378 if (xa > JOYMAX) xa = JOYMAX;
mjr 17:ab3cec0c8bf4 4379 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 17:ab3cec0c8bf4 4380 if (ya > JOYMAX) ya = JOYMAX;
mjr 17:ab3cec0c8bf4 4381
mjr 17:ab3cec0c8bf4 4382 // store the updated accelerometer coordinates
mjr 17:ab3cec0c8bf4 4383 x = xa;
mjr 17:ab3cec0c8bf4 4384 y = ya;
mjr 17:ab3cec0c8bf4 4385
mjr 48:058ace2aed1d 4386 // Report the current plunger position unless the plunger is
mjr 48:058ace2aed1d 4387 // disabled, or the ZB Launch Ball signal is on. In either of
mjr 48:058ace2aed1d 4388 // those cases, just report a constant 0 value. ZB Launch Ball
mjr 48:058ace2aed1d 4389 // temporarily disables mechanical plunger reporting because it
mjr 21:5048e16cc9ef 4390 // tells us that the table has a Launch Ball button instead of
mjr 48:058ace2aed1d 4391 // a traditional plunger, so we don't want to confuse VP with
mjr 48:058ace2aed1d 4392 // regular plunger inputs.
mjr 48:058ace2aed1d 4393 int z = plungerReader.getPosition();
mjr 53:9b2611964afc 4394 int zrep = (!cfg.plunger.enabled || zbLaunchOn ? 0 : z);
mjr 35:e959ffba78fd 4395
mjr 35:e959ffba78fd 4396 // rotate X and Y according to the device orientation in the cabinet
mjr 35:e959ffba78fd 4397 accelRotate(x, y);
mjr 35:e959ffba78fd 4398
mjr 35:e959ffba78fd 4399 // send the joystick report
mjr 53:9b2611964afc 4400 jsOK = js.update(x, y, zrep, jsButtons, statusFlags);
mjr 21:5048e16cc9ef 4401
mjr 17:ab3cec0c8bf4 4402 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 4403 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 4404 }
mjr 21:5048e16cc9ef 4405
mjr 52:8298b2a73eb2 4406 // If we're in sensor status mode, report all pixel exposure values
mjr 52:8298b2a73eb2 4407 if (reportPlungerStat)
mjr 10:976666ffa4ef 4408 {
mjr 17:ab3cec0c8bf4 4409 // send the report
mjr 53:9b2611964afc 4410 plungerSensor->sendStatusReport(js, reportPlungerStatFlags, reportPlungerStatTime);
mjr 17:ab3cec0c8bf4 4411
mjr 10:976666ffa4ef 4412 // we have satisfied this request
mjr 52:8298b2a73eb2 4413 reportPlungerStat = false;
mjr 10:976666ffa4ef 4414 }
mjr 10:976666ffa4ef 4415
mjr 35:e959ffba78fd 4416 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 4417 // periodically for the sake of the Windows config tool.
mjr 55:4db125cd11a0 4418 if (!cfg.joystickEnabled && jsReportTimer.read_us() > 5000)
mjr 21:5048e16cc9ef 4419 {
mjr 55:4db125cd11a0 4420 jsOK = js.updateStatus(statusFlags);
mjr 38:091e511ce8a0 4421 jsReportTimer.reset();
mjr 38:091e511ce8a0 4422 }
mjr 38:091e511ce8a0 4423
mjr 38:091e511ce8a0 4424 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 4425 if (jsOK)
mjr 38:091e511ce8a0 4426 {
mjr 38:091e511ce8a0 4427 jsOKTimer.reset();
mjr 38:091e511ce8a0 4428 jsOKTimer.start();
mjr 21:5048e16cc9ef 4429 }
mjr 21:5048e16cc9ef 4430
mjr 6:cc35eb643e8f 4431 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 4432 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 4433 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 4434 #endif
mjr 6:cc35eb643e8f 4435
mjr 33:d832bcab089e 4436 // check for connection status changes
mjr 54:fd77a6b2f76c 4437 bool newConnected = js.isConnected() && !js.isSleeping();
mjr 33:d832bcab089e 4438 if (newConnected != connected)
mjr 33:d832bcab089e 4439 {
mjr 54:fd77a6b2f76c 4440 // give it a moment to stabilize
mjr 40:cc0d9814522b 4441 connectChangeTimer.start();
mjr 55:4db125cd11a0 4442 if (connectChangeTimer.read_us() > 1000000)
mjr 33:d832bcab089e 4443 {
mjr 33:d832bcab089e 4444 // note the new status
mjr 33:d832bcab089e 4445 connected = newConnected;
mjr 40:cc0d9814522b 4446
mjr 40:cc0d9814522b 4447 // done with the change timer for this round - reset it for next time
mjr 40:cc0d9814522b 4448 connectChangeTimer.stop();
mjr 40:cc0d9814522b 4449 connectChangeTimer.reset();
mjr 33:d832bcab089e 4450
mjr 54:fd77a6b2f76c 4451 // if we're newly disconnected, clean up for PC suspend mode or power off
mjr 54:fd77a6b2f76c 4452 if (!connected)
mjr 40:cc0d9814522b 4453 {
mjr 54:fd77a6b2f76c 4454 // turn off all outputs
mjr 33:d832bcab089e 4455 allOutputsOff();
mjr 40:cc0d9814522b 4456
mjr 40:cc0d9814522b 4457 // The KL25Z runs off of USB power, so we might (depending on the PC
mjr 40:cc0d9814522b 4458 // and OS configuration) continue to receive power even when the main
mjr 40:cc0d9814522b 4459 // PC power supply is turned off, such as in soft-off or suspend/sleep
mjr 40:cc0d9814522b 4460 // mode. Any external output controller chips (TLC5940, 74HC595) might
mjr 40:cc0d9814522b 4461 // be powered from the PC power supply directly rather than from our
mjr 40:cc0d9814522b 4462 // USB power, so they might be powered off even when we're still running.
mjr 40:cc0d9814522b 4463 // To ensure cleaner startup when the power comes back on, globally
mjr 40:cc0d9814522b 4464 // disable the outputs. The global disable signals come from GPIO lines
mjr 40:cc0d9814522b 4465 // that remain powered as long as the KL25Z is powered, so these modes
mjr 40:cc0d9814522b 4466 // will apply smoothly across power state transitions in the external
mjr 40:cc0d9814522b 4467 // hardware. That is, when the external chips are powered up, they'll
mjr 40:cc0d9814522b 4468 // see the global disable signals as stable voltage inputs immediately,
mjr 40:cc0d9814522b 4469 // which will cause them to suppress any output triggering. This ensures
mjr 40:cc0d9814522b 4470 // that we don't fire any solenoids or flash any lights spuriously when
mjr 40:cc0d9814522b 4471 // the power first comes on.
mjr 40:cc0d9814522b 4472 if (tlc5940 != 0)
mjr 40:cc0d9814522b 4473 tlc5940->enable(false);
mjr 40:cc0d9814522b 4474 if (hc595 != 0)
mjr 40:cc0d9814522b 4475 hc595->enable(false);
mjr 40:cc0d9814522b 4476 }
mjr 33:d832bcab089e 4477 }
mjr 33:d832bcab089e 4478 }
mjr 48:058ace2aed1d 4479
mjr 53:9b2611964afc 4480 // if we have a reboot timer pending, check for completion
mjr 53:9b2611964afc 4481 if (rebootTimer.isRunning() && rebootTimer.read_us() > rebootTime_us)
mjr 53:9b2611964afc 4482 reboot(js);
mjr 53:9b2611964afc 4483
mjr 48:058ace2aed1d 4484 // if we're disconnected, initiate a new connection
mjr 51:57eb311faafa 4485 if (!connected)
mjr 48:058ace2aed1d 4486 {
mjr 54:fd77a6b2f76c 4487 // show USB HAL debug events
mjr 54:fd77a6b2f76c 4488 extern void HAL_DEBUG_PRINTEVENTS(const char *prefix);
mjr 54:fd77a6b2f76c 4489 HAL_DEBUG_PRINTEVENTS(">DISC");
mjr 54:fd77a6b2f76c 4490
mjr 54:fd77a6b2f76c 4491 // show immediate diagnostic feedback
mjr 54:fd77a6b2f76c 4492 js.diagFlash();
mjr 54:fd77a6b2f76c 4493
mjr 54:fd77a6b2f76c 4494 // clear any previous diagnostic LED display
mjr 54:fd77a6b2f76c 4495 diagLED(0, 0, 0);
mjr 51:57eb311faafa 4496
mjr 51:57eb311faafa 4497 // set up a timer to monitor the reboot timeout
mjr 51:57eb311faafa 4498 Timer rebootTimer;
mjr 51:57eb311faafa 4499 rebootTimer.start();
mjr 48:058ace2aed1d 4500
mjr 54:fd77a6b2f76c 4501 // set up a timer for diagnostic displays
mjr 54:fd77a6b2f76c 4502 Timer diagTimer;
mjr 54:fd77a6b2f76c 4503 diagTimer.reset();
mjr 54:fd77a6b2f76c 4504 diagTimer.start();
mjr 54:fd77a6b2f76c 4505
mjr 54:fd77a6b2f76c 4506 // loop until we get our connection back
mjr 54:fd77a6b2f76c 4507 while (!js.isConnected() || js.isSleeping())
mjr 51:57eb311faafa 4508 {
mjr 54:fd77a6b2f76c 4509 // try to recover the connection
mjr 54:fd77a6b2f76c 4510 js.recoverConnection();
mjr 54:fd77a6b2f76c 4511
mjr 55:4db125cd11a0 4512 // send TLC5940 data if necessary
mjr 55:4db125cd11a0 4513 if (tlc5940 != 0)
mjr 55:4db125cd11a0 4514 tlc5940->send();
mjr 55:4db125cd11a0 4515
mjr 54:fd77a6b2f76c 4516 // show a diagnostic flash every couple of seconds
mjr 54:fd77a6b2f76c 4517 if (diagTimer.read_us() > 2000000)
mjr 51:57eb311faafa 4518 {
mjr 54:fd77a6b2f76c 4519 // flush the USB HAL debug events, if in debug mode
mjr 54:fd77a6b2f76c 4520 HAL_DEBUG_PRINTEVENTS(">NC");
mjr 54:fd77a6b2f76c 4521
mjr 54:fd77a6b2f76c 4522 // show diagnostic feedback
mjr 54:fd77a6b2f76c 4523 js.diagFlash();
mjr 51:57eb311faafa 4524
mjr 51:57eb311faafa 4525 // reset the flash timer
mjr 54:fd77a6b2f76c 4526 diagTimer.reset();
mjr 51:57eb311faafa 4527 }
mjr 51:57eb311faafa 4528
mjr 51:57eb311faafa 4529 // if the disconnect reboot timeout has expired, reboot
mjr 51:57eb311faafa 4530 if (cfg.disconnectRebootTimeout != 0
mjr 51:57eb311faafa 4531 && rebootTimer.read() > cfg.disconnectRebootTimeout)
mjr 54:fd77a6b2f76c 4532 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 4533 }
mjr 54:fd77a6b2f76c 4534
mjr 54:fd77a6b2f76c 4535 // if we made it out of that loop alive, we're connected again!
mjr 54:fd77a6b2f76c 4536 connected = true;
mjr 54:fd77a6b2f76c 4537 HAL_DEBUG_PRINTEVENTS(">C");
mjr 54:fd77a6b2f76c 4538
mjr 54:fd77a6b2f76c 4539 // Enable peripheral chips and update them with current output data
mjr 54:fd77a6b2f76c 4540 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 4541 {
mjr 55:4db125cd11a0 4542 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 4543 tlc5940->update(true);
mjr 54:fd77a6b2f76c 4544 }
mjr 54:fd77a6b2f76c 4545 if (hc595 != 0)
mjr 54:fd77a6b2f76c 4546 {
mjr 55:4db125cd11a0 4547 hc595->enable(true);
mjr 54:fd77a6b2f76c 4548 hc595->update(true);
mjr 51:57eb311faafa 4549 }
mjr 48:058ace2aed1d 4550 }
mjr 43:7a6364d82a41 4551
mjr 6:cc35eb643e8f 4552 // provide a visual status indication on the on-board LED
mjr 48:058ace2aed1d 4553 if (calBtnState < 2 && hbTimer.read_us() > 1000000)
mjr 1:d913e0afb2ac 4554 {
mjr 54:fd77a6b2f76c 4555 if (jsOKTimer.read_us() > 1000000)
mjr 38:091e511ce8a0 4556 {
mjr 39:b3815a1c3802 4557 // USB freeze - show red/yellow.
mjr 40:cc0d9814522b 4558 //
mjr 54:fd77a6b2f76c 4559 // It's been more than a second since we successfully sent a joystick
mjr 54:fd77a6b2f76c 4560 // update message. This must mean that something's wrong on the USB
mjr 54:fd77a6b2f76c 4561 // connection, even though we haven't detected an outright disconnect.
mjr 54:fd77a6b2f76c 4562 // Show a distinctive diagnostic LED pattern when this occurs.
mjr 38:091e511ce8a0 4563 hb = !hb;
mjr 38:091e511ce8a0 4564 diagLED(1, hb, 0);
mjr 54:fd77a6b2f76c 4565
mjr 54:fd77a6b2f76c 4566 // If the reboot-on-disconnect option is in effect, treat this condition
mjr 54:fd77a6b2f76c 4567 // as equivalent to a disconnect, since something is obviously wrong
mjr 54:fd77a6b2f76c 4568 // with the USB connection.
mjr 54:fd77a6b2f76c 4569 if (cfg.disconnectRebootTimeout != 0)
mjr 54:fd77a6b2f76c 4570 {
mjr 54:fd77a6b2f76c 4571 // The reboot timeout is in effect. If we've been incommunicado for
mjr 54:fd77a6b2f76c 4572 // longer than the timeout, reboot. If we haven't reached the time
mjr 54:fd77a6b2f76c 4573 // limit, keep running for now, and leave the OK timer running so
mjr 54:fd77a6b2f76c 4574 // that we can continue to monitor this.
mjr 54:fd77a6b2f76c 4575 if (jsOKTimer.read() > cfg.disconnectRebootTimeout)
mjr 54:fd77a6b2f76c 4576 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 4577 }
mjr 54:fd77a6b2f76c 4578 else
mjr 54:fd77a6b2f76c 4579 {
mjr 54:fd77a6b2f76c 4580 // There's no reboot timer, so just keep running with the diagnostic
mjr 54:fd77a6b2f76c 4581 // pattern displayed. Since we're not waiting for any other timed
mjr 54:fd77a6b2f76c 4582 // conditions in this state, stop the timer so that it doesn't
mjr 54:fd77a6b2f76c 4583 // overflow if this condition persists for a long time.
mjr 54:fd77a6b2f76c 4584 jsOKTimer.stop();
mjr 54:fd77a6b2f76c 4585 }
mjr 38:091e511ce8a0 4586 }
mjr 35:e959ffba78fd 4587 else if (cfg.plunger.enabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 4588 {
mjr 6:cc35eb643e8f 4589 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 4590 hb = !hb;
mjr 38:091e511ce8a0 4591 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 4592 }
mjr 6:cc35eb643e8f 4593 else
mjr 6:cc35eb643e8f 4594 {
mjr 6:cc35eb643e8f 4595 // connected - flash blue/green
mjr 2:c174f9ee414a 4596 hb = !hb;
mjr 38:091e511ce8a0 4597 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 4598 }
mjr 1:d913e0afb2ac 4599
mjr 1:d913e0afb2ac 4600 // reset the heartbeat timer
mjr 1:d913e0afb2ac 4601 hbTimer.reset();
mjr 5:a70c0bce770d 4602 ++hbcnt;
mjr 1:d913e0afb2ac 4603 }
mjr 1:d913e0afb2ac 4604 }
mjr 0:5acbbe3f4cf4 4605 }