Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Sat Feb 27 06:41:17 2016 +0000
Revision:
50:40015764bbe6
Parent:
49:37bd97eb7688
Child:
51:57eb311faafa
New plunger scheme seems to be working solidly.

Who changed what in which revision?

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