Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Mon Jan 11 21:08:36 2016 +0000
Revision:
39:b3815a1c3802
Parent:
38:091e511ce8a0
Child:
40:cc0d9814522b
USB fixes; accelerometer auto un-sticking with watchdog timer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 35:e959ffba78fd 1 /* Copyright 2014, 2015 M J Roberts, MIT License
mjr 5:a70c0bce770d 2 *
mjr 5:a70c0bce770d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mjr 5:a70c0bce770d 4 * and associated documentation files (the "Software"), to deal in the Software without
mjr 5:a70c0bce770d 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mjr 5:a70c0bce770d 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mjr 5:a70c0bce770d 7 * Software is furnished to do so, subject to the following conditions:
mjr 5:a70c0bce770d 8 *
mjr 5:a70c0bce770d 9 * The above copyright notice and this permission notice shall be included in all copies or
mjr 5:a70c0bce770d 10 * substantial portions of the Software.
mjr 5:a70c0bce770d 11 *
mjr 5:a70c0bce770d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mjr 5:a70c0bce770d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 5:a70c0bce770d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 5:a70c0bce770d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 5:a70c0bce770d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 5:a70c0bce770d 17 */
mjr 5:a70c0bce770d 18
mjr 5:a70c0bce770d 19 //
mjr 35:e959ffba78fd 20 // The Pinscape Controller
mjr 35:e959ffba78fd 21 // A comprehensive input/output controller for virtual pinball machines
mjr 5:a70c0bce770d 22 //
mjr 38:091e511ce8a0 23 // This project implements an I/O controller for virtual pinball cabinets. Its
mjr 38:091e511ce8a0 24 // function is to connect Windows pinball software, such as Visual Pinball, with
mjr 38:091e511ce8a0 25 // physical devices in the cabinet: buttons, sensors, and feedback devices that
mjr 38:091e511ce8a0 26 // create visual or mechanical effects during play.
mjr 38:091e511ce8a0 27 //
mjr 38:091e511ce8a0 28 // The software can perform several different functions, which can be used
mjr 38:091e511ce8a0 29 // individually or in any combination:
mjr 5:a70c0bce770d 30 //
mjr 38:091e511ce8a0 31 // - Nudge sensing. This uses the KL25Z's on-board accelerometer to sense the
mjr 38:091e511ce8a0 32 // motion of the cabinet when you nudge it. Visual Pinball and other pinball
mjr 38:091e511ce8a0 33 // emulators on the PC have native handling for this type of input, so that
mjr 38:091e511ce8a0 34 // physical nudges on the cabinet turn into simulated effects on the virtual
mjr 38:091e511ce8a0 35 // ball. The KL25Z measures accelerations as analog readings and is quite
mjr 38:091e511ce8a0 36 // sensitive, so the effect of a nudge on the simulation is proportional
mjr 38:091e511ce8a0 37 // to the strength of the nudge. Accelerations are reported to the PC via a
mjr 38:091e511ce8a0 38 // simulated joystick (using the X and Y axes); you just have to set some
mjr 38:091e511ce8a0 39 // preferences in your pinball software to tell it that an accelerometer
mjr 38:091e511ce8a0 40 // is attached.
mjr 5:a70c0bce770d 41 //
mjr 38:091e511ce8a0 42 // - Plunger position sensing, with mulitple sensor options. To use this feature,
mjr 35:e959ffba78fd 43 // you need to choose a sensor and set it up, connect the sensor electrically to
mjr 35:e959ffba78fd 44 // the KL25Z, and configure the Pinscape software on the KL25Z to let it know how
mjr 35:e959ffba78fd 45 // the sensor is hooked up. The Pinscape software monitors the sensor and sends
mjr 35:e959ffba78fd 46 // readings to Visual Pinball via the joystick Z axis. VP and other PC software
mjr 38:091e511ce8a0 47 // have native support for this type of input; as with the nudge setup, you just
mjr 38:091e511ce8a0 48 // have to set some options in VP to activate the plunger.
mjr 17:ab3cec0c8bf4 49 //
mjr 35:e959ffba78fd 50 // The Pinscape software supports optical sensors (the TAOS TSL1410R and TSL1412R
mjr 35:e959ffba78fd 51 // linear sensor arrays) as well as slide potentiometers. The specific equipment
mjr 35:e959ffba78fd 52 // that's supported, along with physical mounting and wiring details, can be found
mjr 35:e959ffba78fd 53 // in the Build Guide.
mjr 35:e959ffba78fd 54 //
mjr 38:091e511ce8a0 55 // Note VP has built-in support for plunger devices like this one, but some VP
mjr 38:091e511ce8a0 56 // tables can't use it without some additional scripting work. The Build Guide has
mjr 38:091e511ce8a0 57 // advice on adjusting tables to add plunger support when necessary.
mjr 5:a70c0bce770d 58 //
mjr 6:cc35eb643e8f 59 // For best results, the plunger sensor should be calibrated. The calibration
mjr 6:cc35eb643e8f 60 // is stored in non-volatile memory on board the KL25Z, so it's only necessary
mjr 6:cc35eb643e8f 61 // to do the calibration once, when you first install everything. (You might
mjr 6:cc35eb643e8f 62 // also want to re-calibrate if you physically remove and reinstall the CCD
mjr 17:ab3cec0c8bf4 63 // sensor or the mechanical plunger, since their alignment shift change slightly
mjr 17:ab3cec0c8bf4 64 // when you put everything back together.) You can optionally install a
mjr 17:ab3cec0c8bf4 65 // dedicated momentary switch or pushbutton to activate the calibration mode;
mjr 17:ab3cec0c8bf4 66 // this is describe in the project documentation. If you don't want to bother
mjr 17:ab3cec0c8bf4 67 // with the extra button, you can also trigger calibration using the Windows
mjr 17:ab3cec0c8bf4 68 // setup software, which you can find on the Pinscape project page.
mjr 6:cc35eb643e8f 69 //
mjr 17:ab3cec0c8bf4 70 // The calibration procedure is described in the project documentation. Briefly,
mjr 17:ab3cec0c8bf4 71 // when you trigger calibration mode, the software will scan the CCD for about
mjr 17:ab3cec0c8bf4 72 // 15 seconds, during which you should simply pull the physical plunger back
mjr 17:ab3cec0c8bf4 73 // all the way, hold it for a moment, and then slowly return it to the rest
mjr 17:ab3cec0c8bf4 74 // position. (DON'T just release it from the retracted position, since that
mjr 17:ab3cec0c8bf4 75 // let it shoot forward too far. We want to measure the range from the park
mjr 17:ab3cec0c8bf4 76 // position to the fully retracted position only.)
mjr 5:a70c0bce770d 77 //
mjr 13:72dda449c3c0 78 // - Button input wiring. 24 of the KL25Z's GPIO ports are mapped as digital inputs
mjr 38:091e511ce8a0 79 // for buttons and switches. You can wire each input to a physical pinball-style
mjr 38:091e511ce8a0 80 // button or switch, such as flipper buttons, Start buttons, coin chute switches,
mjr 38:091e511ce8a0 81 // tilt bobs, and service buttons. Each button can be configured to be reported
mjr 38:091e511ce8a0 82 // to the PC as a joystick button or as a keyboard key (you can select which key
mjr 38:091e511ce8a0 83 // is used for each button).
mjr 13:72dda449c3c0 84 //
mjr 5:a70c0bce770d 85 // - LedWiz emulation. The KL25Z can appear to the PC as an LedWiz device, and will
mjr 5:a70c0bce770d 86 // accept and process LedWiz commands from the host. The software can turn digital
mjr 5:a70c0bce770d 87 // output ports on and off, and can set varying PWM intensitiy levels on a subset
mjr 5:a70c0bce770d 88 // of ports. (The KL25Z can only provide 6 PWM ports. Intensity level settings on
mjr 5:a70c0bce770d 89 // other ports is ignored, so non-PWM ports can only be used for simple on/off
mjr 5:a70c0bce770d 90 // devices such as contactors and solenoids.) The KL25Z can only supply 4mA on its
mjr 5:a70c0bce770d 91 // output ports, so external hardware is required to take advantage of the LedWiz
mjr 5:a70c0bce770d 92 // emulation. Many different hardware designs are possible, but there's a simple
mjr 5:a70c0bce770d 93 // reference design in the documentation that uses a Darlington array IC to
mjr 5:a70c0bce770d 94 // increase the output from each port to 500mA (the same level as the LedWiz),
mjr 5:a70c0bce770d 95 // plus an extended design that adds an optocoupler and MOSFET to provide very
mjr 5:a70c0bce770d 96 // high power handling, up to about 45A or 150W, with voltages up to 100V.
mjr 5:a70c0bce770d 97 // That will handle just about any DC device directly (wtihout relays or other
mjr 5:a70c0bce770d 98 // amplifiers), and switches fast enough to support PWM devices.
mjr 5:a70c0bce770d 99 //
mjr 5:a70c0bce770d 100 // The device can report any desired LedWiz unit number to the host, which makes
mjr 5:a70c0bce770d 101 // it possible to use the LedWiz emulation on a machine that also has one or more
mjr 5:a70c0bce770d 102 // actual LedWiz devices intalled. The LedWiz design allows for up to 16 units
mjr 5:a70c0bce770d 103 // to be installed in one machine - each one is invidually addressable by its
mjr 5:a70c0bce770d 104 // distinct unit number.
mjr 5:a70c0bce770d 105 //
mjr 5:a70c0bce770d 106 // The LedWiz emulation features are of course optional. There's no need to
mjr 5:a70c0bce770d 107 // build any of the external port hardware (or attach anything to the output
mjr 5:a70c0bce770d 108 // ports at all) if the LedWiz features aren't needed. Most people won't have
mjr 5:a70c0bce770d 109 // any use for the LedWiz features. I built them mostly as a learning exercise,
mjr 5:a70c0bce770d 110 // but with a slight practical need for a handful of extra ports (I'm using the
mjr 5:a70c0bce770d 111 // cutting-edge 10-contactor setup, so my real LedWiz is full!).
mjr 6:cc35eb643e8f 112 //
mjr 26:cb71c4af2912 113 // - Enhanced LedWiz emulation with TLC5940 PWM controller chips. You can attach
mjr 26:cb71c4af2912 114 // external PWM controller chips for controlling device outputs, instead of using
mjr 26:cb71c4af2912 115 // the limited LedWiz emulation through the on-board GPIO ports as described above.
mjr 26:cb71c4af2912 116 // The software can control a set of daisy-chained TLC5940 chips, which provide
mjr 26:cb71c4af2912 117 // 16 PWM outputs per chip. Two of these chips give you the full complement
mjr 26:cb71c4af2912 118 // of 32 output ports of an actual LedWiz, and four give you 64 ports, which
mjr 33:d832bcab089e 119 // should be plenty for nearly any virtual pinball project. A private, extended
mjr 33:d832bcab089e 120 // version of the LedWiz protocol lets the host control the extra outputs, up to
mjr 33:d832bcab089e 121 // 128 outputs per KL25Z (8 TLC5940s). To take advantage of the extra outputs
mjr 33:d832bcab089e 122 // on the PC side, you need software that knows about the protocol extensions,
mjr 33:d832bcab089e 123 // which means you need the latest version of DirectOutput Framework (DOF). VP
mjr 33:d832bcab089e 124 // uses DOF for its output, so VP will be able to use the added ports without any
mjr 33:d832bcab089e 125 // extra work on your part. Older software (e.g., Future Pinball) that doesn't
mjr 33:d832bcab089e 126 // use DOF will still be able to use the LedWiz-compatible protocol, so it'll be
mjr 33:d832bcab089e 127 // able to control your first 32 ports (numbered 1-32 in the LedWiz scheme), but
mjr 33:d832bcab089e 128 // older software won't be able to address higher-numbered ports. That shouldn't
mjr 33:d832bcab089e 129 // be a problem because older software wouldn't know what to do with the extra
mjr 33:d832bcab089e 130 // devices anyway - FP, for example, is limited to a pre-defined set of outputs.
mjr 33:d832bcab089e 131 // As long as you put the most common devices on the first 32 outputs, and use
mjr 33:d832bcab089e 132 // higher numbered ports for the less common devices that older software can't
mjr 33:d832bcab089e 133 // use anyway, you'll get maximum functionality out of software new and old.
mjr 26:cb71c4af2912 134 //
mjr 38:091e511ce8a0 135 // - Night Mode control for output devices. You can connect a switch or button
mjr 38:091e511ce8a0 136 // to the controller to activate "Night Mode", which disables feedback devices
mjr 38:091e511ce8a0 137 // that you designate as noisy. You can designate outputs individually as being
mjr 38:091e511ce8a0 138 // included in this set or not. This is useful if you want to play a game on
mjr 38:091e511ce8a0 139 // your cabinet late at night without waking the kids and annoying the neighbors.
mjr 38:091e511ce8a0 140 //
mjr 38:091e511ce8a0 141 // - TV ON switch. The controller can pulse a relay to turn on your TVs after
mjr 38:091e511ce8a0 142 // power to the cabinet comes on, with a configurable delay timer. This feature
mjr 38:091e511ce8a0 143 // is for TVs that don't turn themselves on automatically when first plugged in.
mjr 38:091e511ce8a0 144 // To use this feature, you have to build some external circuitry to allow the
mjr 38:091e511ce8a0 145 // software to sense the power supply status, and you have to run wires to your
mjr 38:091e511ce8a0 146 // TV's on/off button, which requires opening the case on your TV. The Build
mjr 38:091e511ce8a0 147 // Guide has details on the necessary circuitry and connections to the TV.
mjr 38:091e511ce8a0 148 //
mjr 35:e959ffba78fd 149 //
mjr 35:e959ffba78fd 150 //
mjr 33:d832bcab089e 151 // STATUS LIGHTS: The on-board LED on the KL25Z flashes to indicate the current
mjr 33:d832bcab089e 152 // device status. The flash patterns are:
mjr 6:cc35eb643e8f 153 //
mjr 6:cc35eb643e8f 154 // two short red flashes = the device is powered but hasn't successfully
mjr 6:cc35eb643e8f 155 // connected to the host via USB (either it's not physically connected
mjr 6:cc35eb643e8f 156 // to the USB port, or there was a problem with the software handshake
mjr 6:cc35eb643e8f 157 // with the USB device driver on the computer)
mjr 6:cc35eb643e8f 158 //
mjr 6:cc35eb643e8f 159 // short red flash = the host computer is in sleep/suspend mode
mjr 6:cc35eb643e8f 160 //
mjr 38:091e511ce8a0 161 // long red/yellow = USB connection problem. The device still has a USB
mjr 38:091e511ce8a0 162 // connection to the host, but data transmissions are failing. This
mjr 38:091e511ce8a0 163 // condition shouldn't ever occur; if it does, it probably indicates
mjr 38:091e511ce8a0 164 // a bug in the device's USB software. This display is provided to
mjr 38:091e511ce8a0 165 // flag any occurrences for investigation. You'll probably need to
mjr 38:091e511ce8a0 166 // manually reset the device if this occurs.
mjr 38:091e511ce8a0 167 //
mjr 6:cc35eb643e8f 168 // long yellow/green = everything's working, but the plunger hasn't
mjr 38:091e511ce8a0 169 // been calibrated. Follow the calibration procedure described in
mjr 38:091e511ce8a0 170 // the project documentation. This flash mode won't appear if there's
mjr 38:091e511ce8a0 171 // no plunger sensor configured.
mjr 6:cc35eb643e8f 172 //
mjr 38:091e511ce8a0 173 // alternating blue/green = everything's working normally, and plunger
mjr 38:091e511ce8a0 174 // calibration has been completed (or there's no plunger attached)
mjr 10:976666ffa4ef 175 //
mjr 10:976666ffa4ef 176 //
mjr 35:e959ffba78fd 177 // USB PROTOCOL: please refer to USBProtocol.h for details on the USB
mjr 35:e959ffba78fd 178 // message protocol.
mjr 33:d832bcab089e 179
mjr 33:d832bcab089e 180
mjr 0:5acbbe3f4cf4 181 #include "mbed.h"
mjr 6:cc35eb643e8f 182 #include "math.h"
mjr 0:5acbbe3f4cf4 183 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 184 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 185 #include "tsl1410r.h"
mjr 1:d913e0afb2ac 186 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 187 #include "crc32.h"
mjr 26:cb71c4af2912 188 #include "TLC5940.h"
mjr 34:6b981a2afab7 189 #include "74HC595.h"
mjr 35:e959ffba78fd 190 #include "nvm.h"
mjr 35:e959ffba78fd 191 #include "plunger.h"
mjr 35:e959ffba78fd 192 #include "ccdSensor.h"
mjr 35:e959ffba78fd 193 #include "potSensor.h"
mjr 35:e959ffba78fd 194 #include "nullSensor.h"
mjr 2:c174f9ee414a 195
mjr 21:5048e16cc9ef 196 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 197 #include "config.h"
mjr 17:ab3cec0c8bf4 198
mjr 5:a70c0bce770d 199
mjr 5:a70c0bce770d 200 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 201 //
mjr 38:091e511ce8a0 202 // Forward declarations
mjr 38:091e511ce8a0 203 //
mjr 38:091e511ce8a0 204 void setNightMode(bool on);
mjr 38:091e511ce8a0 205 void toggleNightMode();
mjr 38:091e511ce8a0 206
mjr 38:091e511ce8a0 207 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 208 // utilities
mjr 17:ab3cec0c8bf4 209
mjr 17:ab3cec0c8bf4 210 // number of elements in an array
mjr 17:ab3cec0c8bf4 211 #define countof(x) (sizeof(x)/sizeof((x)[0]))
mjr 17:ab3cec0c8bf4 212
mjr 26:cb71c4af2912 213 // floating point square of a number
mjr 26:cb71c4af2912 214 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 215
mjr 26:cb71c4af2912 216 // floating point rounding
mjr 26:cb71c4af2912 217 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 218
mjr 17:ab3cec0c8bf4 219
mjr 33:d832bcab089e 220 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 221 //
mjr 33:d832bcab089e 222 // USB product version number
mjr 5:a70c0bce770d 223 //
mjr 35:e959ffba78fd 224 const uint16_t USB_VERSION_NO = 0x0008;
mjr 33:d832bcab089e 225
mjr 33:d832bcab089e 226 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 227 //
mjr 6:cc35eb643e8f 228 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 229 //
mjr 6:cc35eb643e8f 230 #define JOYMAX 4096
mjr 6:cc35eb643e8f 231
mjr 9:fd65b0a94720 232
mjr 17:ab3cec0c8bf4 233 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 234 //
mjr 35:e959ffba78fd 235 // Wire protocol value translations. These translate byte values from
mjr 35:e959ffba78fd 236 // the USB protocol to local native format.
mjr 35:e959ffba78fd 237 //
mjr 35:e959ffba78fd 238
mjr 35:e959ffba78fd 239 // unsigned 16-bit integer
mjr 35:e959ffba78fd 240 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 241 {
mjr 35:e959ffba78fd 242 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 243 }
mjr 35:e959ffba78fd 244
mjr 35:e959ffba78fd 245 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 246 {
mjr 35:e959ffba78fd 247 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 248 }
mjr 35:e959ffba78fd 249
mjr 35:e959ffba78fd 250 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 251 {
mjr 35:e959ffba78fd 252 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 253 }
mjr 35:e959ffba78fd 254
mjr 35:e959ffba78fd 255 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 256 {
mjr 35:e959ffba78fd 257 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 258 }
mjr 35:e959ffba78fd 259
mjr 35:e959ffba78fd 260 inline PinName wirePinName(int c)
mjr 35:e959ffba78fd 261 {
mjr 35:e959ffba78fd 262 static const PinName p[] = {
mjr 38:091e511ce8a0 263 NC, PTA1, PTA2, PTA4, PTA5, PTA12, PTA13, PTA16, PTA17, PTB0, // 0-9
mjr 38:091e511ce8a0 264 PTB1, PTB2, PTB3, PTB8, PTB9, PTB10, PTB11, PTB18, PTB19, PTC0, // 10-19
mjr 38:091e511ce8a0 265 PTC1, PTC2, PTC3, PTC4, PTC5, PTC6, PTC7, PTC8, PTC9, PTC10, // 20-29
mjr 38:091e511ce8a0 266 PTC11, PTC12, PTC13, PTC16, PTC17, PTD0, PTD1, PTD2, PTD3, PTD4, // 30-39
mjr 38:091e511ce8a0 267 PTD5, PTD6, PTD7, PTE0, PTE1, PTE2, PTE3, PTE4, PTE5, PTE20, // 40-49
mjr 38:091e511ce8a0 268 PTE21, PTE22, PTE23, PTE29, PTE30, PTE31 // 50-55
mjr 35:e959ffba78fd 269 };
mjr 35:e959ffba78fd 270 return (c < countof(p) ? p[c] : NC);
mjr 35:e959ffba78fd 271 }
mjr 35:e959ffba78fd 272
mjr 35:e959ffba78fd 273
mjr 35:e959ffba78fd 274 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 275 //
mjr 38:091e511ce8a0 276 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 277 //
mjr 38:091e511ce8a0 278 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 279 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 280 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 281 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 282 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 283 // SPI capability.
mjr 38:091e511ce8a0 284 //
mjr 38:091e511ce8a0 285 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 286
mjr 38:091e511ce8a0 287 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 288 // on, and -1 is no change (leaves the current setting intact).
mjr 38:091e511ce8a0 289 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 290 {
mjr 38:091e511ce8a0 291 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 292 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 293 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 294 }
mjr 38:091e511ce8a0 295
mjr 38:091e511ce8a0 296 // check an output port assignment to see if it conflicts with
mjr 38:091e511ce8a0 297 // an on-board LED segment
mjr 38:091e511ce8a0 298 struct LedSeg
mjr 38:091e511ce8a0 299 {
mjr 38:091e511ce8a0 300 bool r, g, b;
mjr 38:091e511ce8a0 301 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 302
mjr 38:091e511ce8a0 303 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 304 {
mjr 38:091e511ce8a0 305 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 306 // our on-board LED segments
mjr 38:091e511ce8a0 307 int t = pc.typ;
mjr 38:091e511ce8a0 308 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 38:091e511ce8a0 309 {
mjr 38:091e511ce8a0 310 // it's a GPIO port - check for a matching pin assignment
mjr 38:091e511ce8a0 311 PinName pin = wirePinName(pc.pin);
mjr 38:091e511ce8a0 312 if (pin == LED1)
mjr 38:091e511ce8a0 313 r = true;
mjr 38:091e511ce8a0 314 else if (pin == LED2)
mjr 38:091e511ce8a0 315 g = true;
mjr 38:091e511ce8a0 316 else if (pin == LED3)
mjr 38:091e511ce8a0 317 b = true;
mjr 38:091e511ce8a0 318 }
mjr 38:091e511ce8a0 319 }
mjr 38:091e511ce8a0 320 };
mjr 38:091e511ce8a0 321
mjr 38:091e511ce8a0 322 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 323 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 324 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 325 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 326 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 327 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 328 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 329 {
mjr 38:091e511ce8a0 330 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 331 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 332 LedSeg l;
mjr 38:091e511ce8a0 333 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 334 l.check(cfg.outPort[i]);
mjr 38:091e511ce8a0 335
mjr 38:091e511ce8a0 336 // check the special ports
mjr 38:091e511ce8a0 337 for (int i = 0 ; i < countof(cfg.specialPort) ; ++i)
mjr 38:091e511ce8a0 338 l.check(cfg.specialPort[i]);
mjr 38:091e511ce8a0 339
mjr 38:091e511ce8a0 340 // We now know which segments are taken for LedWiz use and which
mjr 38:091e511ce8a0 341 // are free. Create diagnostic ports for the ones not claimed for
mjr 38:091e511ce8a0 342 // LedWiz use.
mjr 38:091e511ce8a0 343 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 344 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 345 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 346 }
mjr 38:091e511ce8a0 347
mjr 38:091e511ce8a0 348
mjr 38:091e511ce8a0 349 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 350 //
mjr 29:582472d0bc57 351 // LedWiz emulation, and enhanced TLC5940 output controller
mjr 5:a70c0bce770d 352 //
mjr 26:cb71c4af2912 353 // There are two modes for this feature. The default mode uses the on-board
mjr 26:cb71c4af2912 354 // GPIO ports to implement device outputs - each LedWiz software port is
mjr 26:cb71c4af2912 355 // connected to a physical GPIO pin on the KL25Z. The KL25Z only has 10
mjr 26:cb71c4af2912 356 // PWM channels, so in this mode only 10 LedWiz ports will be dimmable; the
mjr 26:cb71c4af2912 357 // rest are strictly on/off. The KL25Z also has a limited number of GPIO
mjr 26:cb71c4af2912 358 // ports overall - not enough for the full complement of 32 LedWiz ports
mjr 26:cb71c4af2912 359 // and 24 VP joystick inputs, so it's necessary to trade one against the
mjr 26:cb71c4af2912 360 // other if both features are to be used.
mjr 26:cb71c4af2912 361 //
mjr 26:cb71c4af2912 362 // The alternative, enhanced mode uses external TLC5940 PWM controller
mjr 26:cb71c4af2912 363 // chips to control device outputs. In this mode, each LedWiz software
mjr 26:cb71c4af2912 364 // port is mapped to an output on one of the external TLC5940 chips.
mjr 26:cb71c4af2912 365 // Two 5940s is enough for the full set of 32 LedWiz ports, and we can
mjr 26:cb71c4af2912 366 // support even more chips for even more outputs (although doing so requires
mjr 26:cb71c4af2912 367 // breaking LedWiz compatibility, since the LedWiz USB protocol is hardwired
mjr 26:cb71c4af2912 368 // for 32 outputs). Every port in this mode has full PWM support.
mjr 26:cb71c4af2912 369 //
mjr 5:a70c0bce770d 370
mjr 29:582472d0bc57 371
mjr 26:cb71c4af2912 372 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 373 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 374 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 375 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 376 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 377
mjr 26:cb71c4af2912 378 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 379 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 380 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 381 class LwOut
mjr 6:cc35eb643e8f 382 {
mjr 6:cc35eb643e8f 383 public:
mjr 26:cb71c4af2912 384 // Set the output intensity. 'val' is 0.0 for fully off, 1.0 for
mjr 26:cb71c4af2912 385 // fully on, and fractional values for intermediate intensities.
mjr 6:cc35eb643e8f 386 virtual void set(float val) = 0;
mjr 6:cc35eb643e8f 387 };
mjr 26:cb71c4af2912 388
mjr 35:e959ffba78fd 389 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 390 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 391 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 392 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 393 // numbering.
mjr 35:e959ffba78fd 394 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 395 {
mjr 33:d832bcab089e 396 public:
mjr 35:e959ffba78fd 397 LwVirtualOut() { }
mjr 33:d832bcab089e 398 virtual void set(float val) { }
mjr 33:d832bcab089e 399 };
mjr 26:cb71c4af2912 400
mjr 34:6b981a2afab7 401 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 402 // on top of the physical pin interface. This simply inverts the value of
mjr 34:6b981a2afab7 403 // the output value, so that 1.0 means fully off and 0.0 means fully on.
mjr 34:6b981a2afab7 404 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 405 {
mjr 34:6b981a2afab7 406 public:
mjr 34:6b981a2afab7 407 LwInvertedOut(LwOut *o) : out(o) { }
mjr 34:6b981a2afab7 408 virtual void set(float val) { out->set(1.0 - val); }
mjr 34:6b981a2afab7 409
mjr 34:6b981a2afab7 410 private:
mjr 34:6b981a2afab7 411 LwOut *out;
mjr 34:6b981a2afab7 412 };
mjr 34:6b981a2afab7 413
mjr 26:cb71c4af2912 414
mjr 35:e959ffba78fd 415 //
mjr 35:e959ffba78fd 416 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 417 // assignments set in config.h.
mjr 33:d832bcab089e 418 //
mjr 35:e959ffba78fd 419 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 420 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 421 {
mjr 35:e959ffba78fd 422 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 423 {
mjr 35:e959ffba78fd 424 tlc5940 = new TLC5940(cfg.tlc5940.sclk, cfg.tlc5940.sin, cfg.tlc5940.gsclk,
mjr 35:e959ffba78fd 425 cfg.tlc5940.blank, cfg.tlc5940.xlat, cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 426 }
mjr 35:e959ffba78fd 427 }
mjr 26:cb71c4af2912 428
mjr 26:cb71c4af2912 429 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 430 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 431 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 432 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 433 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 434 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 435 {
mjr 26:cb71c4af2912 436 public:
mjr 26:cb71c4af2912 437 Lw5940Out(int idx) : idx(idx) { prv = -1; }
mjr 26:cb71c4af2912 438 virtual void set(float val)
mjr 26:cb71c4af2912 439 {
mjr 26:cb71c4af2912 440 if (val != prv)
mjr 38:091e511ce8a0 441 tlc5940->set(idx, (int)((prv = val) * 4095.0f));
mjr 26:cb71c4af2912 442 }
mjr 26:cb71c4af2912 443 int idx;
mjr 26:cb71c4af2912 444 float prv;
mjr 26:cb71c4af2912 445 };
mjr 26:cb71c4af2912 446
mjr 33:d832bcab089e 447
mjr 34:6b981a2afab7 448 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 449 // config.h.
mjr 35:e959ffba78fd 450 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 451
mjr 35:e959ffba78fd 452 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 453 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 454 {
mjr 35:e959ffba78fd 455 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 456 {
mjr 35:e959ffba78fd 457 hc595 = new HC595(cfg.hc595.nchips, cfg.hc595.sin, cfg.hc595.sclk, cfg.hc595.latch, cfg.hc595.ena);
mjr 35:e959ffba78fd 458 hc595->init();
mjr 35:e959ffba78fd 459 hc595->update();
mjr 35:e959ffba78fd 460 }
mjr 35:e959ffba78fd 461 }
mjr 34:6b981a2afab7 462
mjr 34:6b981a2afab7 463 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 464 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 465 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 466 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 467 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 468 class Lw595Out: public LwOut
mjr 33:d832bcab089e 469 {
mjr 33:d832bcab089e 470 public:
mjr 34:6b981a2afab7 471 Lw595Out(int idx) : idx(idx) { prv = -1; }
mjr 34:6b981a2afab7 472 virtual void set(float val)
mjr 34:6b981a2afab7 473 {
mjr 34:6b981a2afab7 474 if (val != prv)
mjr 35:e959ffba78fd 475 hc595->set(idx, (prv = val) == 0.0 ? 0 : 1);
mjr 34:6b981a2afab7 476 }
mjr 34:6b981a2afab7 477 int idx;
mjr 34:6b981a2afab7 478 float prv;
mjr 33:d832bcab089e 479 };
mjr 33:d832bcab089e 480
mjr 26:cb71c4af2912 481
mjr 26:cb71c4af2912 482 //
mjr 26:cb71c4af2912 483 // Default LedWiz mode - using on-board GPIO ports. In this mode, we
mjr 26:cb71c4af2912 484 // assign a KL25Z GPIO port to each LedWiz output. We have to use a
mjr 26:cb71c4af2912 485 // mix of PWM-capable and Digital-Only ports in this configuration,
mjr 26:cb71c4af2912 486 // since the KL25Z hardware only has 10 PWM channels, which isn't
mjr 26:cb71c4af2912 487 // enough to fill out the full complement of 32 LedWiz outputs.
mjr 26:cb71c4af2912 488 //
mjr 26:cb71c4af2912 489
mjr 26:cb71c4af2912 490 // LwOut class for a PWM-capable GPIO port
mjr 6:cc35eb643e8f 491 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 492 {
mjr 6:cc35eb643e8f 493 public:
mjr 13:72dda449c3c0 494 LwPwmOut(PinName pin) : p(pin) { prv = -1; }
mjr 13:72dda449c3c0 495 virtual void set(float val)
mjr 13:72dda449c3c0 496 {
mjr 13:72dda449c3c0 497 if (val != prv)
mjr 13:72dda449c3c0 498 p.write(prv = val);
mjr 13:72dda449c3c0 499 }
mjr 6:cc35eb643e8f 500 PwmOut p;
mjr 13:72dda449c3c0 501 float prv;
mjr 6:cc35eb643e8f 502 };
mjr 26:cb71c4af2912 503
mjr 26:cb71c4af2912 504 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 505 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 506 {
mjr 6:cc35eb643e8f 507 public:
mjr 13:72dda449c3c0 508 LwDigOut(PinName pin) : p(pin) { prv = -1; }
mjr 13:72dda449c3c0 509 virtual void set(float val)
mjr 13:72dda449c3c0 510 {
mjr 13:72dda449c3c0 511 if (val != prv)
mjr 13:72dda449c3c0 512 p.write((prv = val) == 0.0 ? 0 : 1);
mjr 13:72dda449c3c0 513 }
mjr 6:cc35eb643e8f 514 DigitalOut p;
mjr 13:72dda449c3c0 515 float prv;
mjr 6:cc35eb643e8f 516 };
mjr 26:cb71c4af2912 517
mjr 29:582472d0bc57 518 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 519 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 520 // port n (0-based).
mjr 35:e959ffba78fd 521 //
mjr 35:e959ffba78fd 522 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 523 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 524 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 525 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 526 // 74HC595 ports).
mjr 33:d832bcab089e 527 static int numOutputs;
mjr 33:d832bcab089e 528 static LwOut **lwPin;
mjr 33:d832bcab089e 529
mjr 38:091e511ce8a0 530 // Special output ports:
mjr 38:091e511ce8a0 531 //
mjr 38:091e511ce8a0 532 // [0] = Night Mode indicator light
mjr 38:091e511ce8a0 533 //
mjr 38:091e511ce8a0 534 static LwOut *specialPin[1];
mjr 38:091e511ce8a0 535
mjr 38:091e511ce8a0 536
mjr 35:e959ffba78fd 537 // Number of LedWiz emulation outputs. This is the number of ports
mjr 35:e959ffba78fd 538 // accessible through the standard (non-extended) LedWiz protocol
mjr 35:e959ffba78fd 539 // messages. The protocol has a fixed set of 32 outputs, but we
mjr 35:e959ffba78fd 540 // might have fewer actual outputs. This is therefore set to the
mjr 35:e959ffba78fd 541 // lower of 32 or the actual number of outputs.
mjr 35:e959ffba78fd 542 static int numLwOutputs;
mjr 35:e959ffba78fd 543
mjr 33:d832bcab089e 544 // Current absolute brightness level for an output. This is a float
mjr 38:091e511ce8a0 545 // value from 0.0 for fully off to 1.0 for fully on. This is used
mjr 38:091e511ce8a0 546 // for all extended ports (33 and above), and for any LedWiz port
mjr 38:091e511ce8a0 547 // with wizVal == 255.
mjr 33:d832bcab089e 548 static float *outLevel;
mjr 6:cc35eb643e8f 549
mjr 38:091e511ce8a0 550 // Day/night mode override for an output. For each output, this is
mjr 38:091e511ce8a0 551 // set to 1 if the output is enabled and 0 if the output is disabled
mjr 38:091e511ce8a0 552 // by a global mode control, such as Night Mode (currently Night Mode
mjr 38:091e511ce8a0 553 // is the only such global mode, but the idea could be extended to
mjr 38:091e511ce8a0 554 // other similar controls if other needs emerge). To get the final
mjr 38:091e511ce8a0 555 // output level for each output, we simply multiply the outLevel value
mjr 38:091e511ce8a0 556 // for the port by this override vlaue.
mjr 38:091e511ce8a0 557 static uint8_t *modeLevel;
mjr 38:091e511ce8a0 558
mjr 38:091e511ce8a0 559 // create a single output pin
mjr 38:091e511ce8a0 560 LwOut *createLwPin(LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 561 {
mjr 38:091e511ce8a0 562 // get this item's values
mjr 38:091e511ce8a0 563 int typ = pc.typ;
mjr 38:091e511ce8a0 564 int pin = pc.pin;
mjr 38:091e511ce8a0 565 int flags = pc.flags;
mjr 38:091e511ce8a0 566 int activeLow = flags & PortFlagActiveLow;
mjr 38:091e511ce8a0 567
mjr 38:091e511ce8a0 568 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 569 LwOut *lwp;
mjr 38:091e511ce8a0 570 switch (typ)
mjr 38:091e511ce8a0 571 {
mjr 38:091e511ce8a0 572 case PortTypeGPIOPWM:
mjr 38:091e511ce8a0 573 // PWM GPIO port
mjr 38:091e511ce8a0 574 lwp = new LwPwmOut(wirePinName(pin));
mjr 38:091e511ce8a0 575 break;
mjr 38:091e511ce8a0 576
mjr 38:091e511ce8a0 577 case PortTypeGPIODig:
mjr 38:091e511ce8a0 578 // Digital GPIO port
mjr 38:091e511ce8a0 579 lwp = new LwDigOut(wirePinName(pin));
mjr 38:091e511ce8a0 580 break;
mjr 38:091e511ce8a0 581
mjr 38:091e511ce8a0 582 case PortTypeTLC5940:
mjr 38:091e511ce8a0 583 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 584 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 585 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 38:091e511ce8a0 586 lwp = new Lw5940Out(pin);
mjr 38:091e511ce8a0 587 else
mjr 38:091e511ce8a0 588 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 589 break;
mjr 38:091e511ce8a0 590
mjr 38:091e511ce8a0 591 case PortType74HC595:
mjr 38:091e511ce8a0 592 // 74HC595 port (if we don't have an HC595 controller object, or it's not a valid
mjr 38:091e511ce8a0 593 // output number, create a virtual port)
mjr 38:091e511ce8a0 594 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 595 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 596 else
mjr 38:091e511ce8a0 597 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 598 break;
mjr 38:091e511ce8a0 599
mjr 38:091e511ce8a0 600 case PortTypeVirtual:
mjr 38:091e511ce8a0 601 default:
mjr 38:091e511ce8a0 602 // virtual or unknown
mjr 38:091e511ce8a0 603 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 604 break;
mjr 38:091e511ce8a0 605 }
mjr 38:091e511ce8a0 606
mjr 38:091e511ce8a0 607 // if it's Active Low, layer on an inverter
mjr 38:091e511ce8a0 608 if (activeLow)
mjr 38:091e511ce8a0 609 lwp = new LwInvertedOut(lwp);
mjr 38:091e511ce8a0 610
mjr 38:091e511ce8a0 611 // turn it off initially
mjr 38:091e511ce8a0 612 lwp->set(0);
mjr 38:091e511ce8a0 613
mjr 38:091e511ce8a0 614 // return the pin
mjr 38:091e511ce8a0 615 return lwp;
mjr 38:091e511ce8a0 616 }
mjr 38:091e511ce8a0 617
mjr 6:cc35eb643e8f 618 // initialize the output pin array
mjr 35:e959ffba78fd 619 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 620 {
mjr 35:e959ffba78fd 621 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 622 // total number of ports.
mjr 35:e959ffba78fd 623 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 624 int i;
mjr 35:e959ffba78fd 625 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 626 {
mjr 35:e959ffba78fd 627 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 628 {
mjr 35:e959ffba78fd 629 numOutputs = i;
mjr 34:6b981a2afab7 630 break;
mjr 34:6b981a2afab7 631 }
mjr 33:d832bcab089e 632 }
mjr 33:d832bcab089e 633
mjr 35:e959ffba78fd 634 // the real LedWiz protocol can access at most 32 ports, or the
mjr 35:e959ffba78fd 635 // actual number of outputs, whichever is lower
mjr 35:e959ffba78fd 636 numLwOutputs = (numOutputs < 32 ? numOutputs : 32);
mjr 35:e959ffba78fd 637
mjr 33:d832bcab089e 638 // allocate the pin array
mjr 33:d832bcab089e 639 lwPin = new LwOut*[numOutputs];
mjr 33:d832bcab089e 640
mjr 38:091e511ce8a0 641 // Allocate the current brightness array. For these, allocate at
mjr 38:091e511ce8a0 642 // least 32, so that we have enough for all LedWiz messages, but
mjr 38:091e511ce8a0 643 // allocate the full set of actual ports if we have more than the
mjr 38:091e511ce8a0 644 // LedWiz complement.
mjr 38:091e511ce8a0 645 int minOuts = numOutputs < 32 ? 32 : numOutputs;
mjr 38:091e511ce8a0 646 outLevel = new float[minOuts];
mjr 38:091e511ce8a0 647
mjr 38:091e511ce8a0 648 // Allocate the mode override array
mjr 38:091e511ce8a0 649 modeLevel = new uint8_t[minOuts];
mjr 38:091e511ce8a0 650
mjr 38:091e511ce8a0 651 // start with all modeLevel values set to ON
mjr 38:091e511ce8a0 652 memset(modeLevel, 1, minOuts);
mjr 33:d832bcab089e 653
mjr 35:e959ffba78fd 654 // create the pin interface object for each port
mjr 35:e959ffba78fd 655 for (i = 0 ; i < numOutputs ; ++i)
mjr 38:091e511ce8a0 656 lwPin[i] = createLwPin(cfg.outPort[i], cfg);
mjr 34:6b981a2afab7 657
mjr 38:091e511ce8a0 658 // create the pin interface for each special port
mjr 38:091e511ce8a0 659 for (i = 0 ; i < countof(cfg.specialPort) ; ++i)
mjr 38:091e511ce8a0 660 specialPin[i] = createLwPin(cfg.specialPort[i], cfg);
mjr 6:cc35eb643e8f 661 }
mjr 6:cc35eb643e8f 662
mjr 29:582472d0bc57 663 // LedWiz output states.
mjr 29:582472d0bc57 664 //
mjr 29:582472d0bc57 665 // The LedWiz protocol has two separate control axes for each output.
mjr 29:582472d0bc57 666 // One axis is its on/off state; the other is its "profile" state, which
mjr 29:582472d0bc57 667 // is either a fixed brightness or a blinking pattern for the light.
mjr 29:582472d0bc57 668 // The two axes are independent.
mjr 29:582472d0bc57 669 //
mjr 29:582472d0bc57 670 // Note that the LedWiz protocol can only address 32 outputs, so the
mjr 29:582472d0bc57 671 // wizOn and wizVal arrays have fixed sizes of 32 elements no matter
mjr 29:582472d0bc57 672 // how many physical outputs we're using.
mjr 29:582472d0bc57 673
mjr 0:5acbbe3f4cf4 674 // on/off state for each LedWiz output
mjr 1:d913e0afb2ac 675 static uint8_t wizOn[32];
mjr 0:5acbbe3f4cf4 676
mjr 29:582472d0bc57 677 // Profile (brightness/blink) state for each LedWiz output. If the
mjr 29:582472d0bc57 678 // output was last updated through an LedWiz protocol message, it
mjr 29:582472d0bc57 679 // will have one of these values:
mjr 29:582472d0bc57 680 //
mjr 29:582472d0bc57 681 // 0-48 = fixed brightness 0% to 100%
mjr 29:582472d0bc57 682 // 129 = ramp up / ramp down
mjr 29:582472d0bc57 683 // 130 = flash on / off
mjr 29:582472d0bc57 684 // 131 = on / ramp down
mjr 29:582472d0bc57 685 // 132 = ramp up / on
mjr 29:582472d0bc57 686 //
mjr 29:582472d0bc57 687 // Special value 255: If the output was updated through the
mjr 29:582472d0bc57 688 // extended protocol, we'll set the wizVal entry to 255, which has
mjr 29:582472d0bc57 689 // no meaning in the LedWiz protocol. This tells us that the value
mjr 29:582472d0bc57 690 // in outLevel[] was set directly from the extended protocol, so it
mjr 29:582472d0bc57 691 // shouldn't be derived from wizVal[].
mjr 29:582472d0bc57 692 //
mjr 1:d913e0afb2ac 693 static uint8_t wizVal[32] = {
mjr 13:72dda449c3c0 694 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 695 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 696 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 697 48, 48, 48, 48, 48, 48, 48, 48
mjr 0:5acbbe3f4cf4 698 };
mjr 0:5acbbe3f4cf4 699
mjr 29:582472d0bc57 700 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 29:582472d0bc57 701 // rate for lights in blinking states.
mjr 29:582472d0bc57 702 static uint8_t wizSpeed = 2;
mjr 29:582472d0bc57 703
mjr 29:582472d0bc57 704 // Current LedWiz flash cycle counter.
mjr 29:582472d0bc57 705 static uint8_t wizFlashCounter = 0;
mjr 29:582472d0bc57 706
mjr 29:582472d0bc57 707 // Get the current brightness level for an LedWiz output.
mjr 1:d913e0afb2ac 708 static float wizState(int idx)
mjr 0:5acbbe3f4cf4 709 {
mjr 29:582472d0bc57 710 // if the output was last set with an extended protocol message,
mjr 29:582472d0bc57 711 // use the value set there, ignoring the output's LedWiz state
mjr 29:582472d0bc57 712 if (wizVal[idx] == 255)
mjr 29:582472d0bc57 713 return outLevel[idx];
mjr 29:582472d0bc57 714
mjr 29:582472d0bc57 715 // if it's off, show at zero intensity
mjr 29:582472d0bc57 716 if (!wizOn[idx])
mjr 29:582472d0bc57 717 return 0;
mjr 29:582472d0bc57 718
mjr 29:582472d0bc57 719 // check the state
mjr 29:582472d0bc57 720 uint8_t val = wizVal[idx];
mjr 29:582472d0bc57 721 if (val <= 48)
mjr 29:582472d0bc57 722 {
mjr 29:582472d0bc57 723 // PWM brightness/intensity level. Rescale from the LedWiz
mjr 29:582472d0bc57 724 // 0..48 integer range to our internal PwmOut 0..1 float range.
mjr 29:582472d0bc57 725 // Note that on the actual LedWiz, level 48 is actually about
mjr 29:582472d0bc57 726 // 98% on - contrary to the LedWiz documentation, level 49 is
mjr 29:582472d0bc57 727 // the true 100% level. (In the documentation, level 49 is
mjr 29:582472d0bc57 728 // simply not a valid setting.) Even so, we treat level 48 as
mjr 29:582472d0bc57 729 // 100% on to match the documentation. This won't be perfectly
mjr 29:582472d0bc57 730 // ocmpatible with the actual LedWiz, but it makes for such a
mjr 29:582472d0bc57 731 // small difference in brightness (if the output device is an
mjr 29:582472d0bc57 732 // LED, say) that no one should notice. It seems better to
mjr 29:582472d0bc57 733 // err in this direction, because while the difference in
mjr 29:582472d0bc57 734 // brightness when attached to an LED won't be noticeable, the
mjr 29:582472d0bc57 735 // difference in duty cycle when attached to something like a
mjr 29:582472d0bc57 736 // contactor *can* be noticeable - anything less than 100%
mjr 29:582472d0bc57 737 // can cause a contactor or relay to chatter. There's almost
mjr 29:582472d0bc57 738 // never a situation where you'd want values other than 0% and
mjr 29:582472d0bc57 739 // 100% for a contactor or relay, so treating level 48 as 100%
mjr 29:582472d0bc57 740 // makes us work properly with software that's expecting the
mjr 29:582472d0bc57 741 // documented LedWiz behavior and therefore uses level 48 to
mjr 29:582472d0bc57 742 // turn a contactor or relay fully on.
mjr 38:091e511ce8a0 743 return val/48.0f;
mjr 29:582472d0bc57 744 }
mjr 29:582472d0bc57 745 else if (val == 49)
mjr 13:72dda449c3c0 746 {
mjr 29:582472d0bc57 747 // 49 is undefined in the LedWiz documentation, but actually
mjr 29:582472d0bc57 748 // means 100% on. The documentation says that levels 1-48 are
mjr 29:582472d0bc57 749 // the full PWM range, but empirically it appears that the real
mjr 29:582472d0bc57 750 // range implemented in the firmware is 1-49. Some software on
mjr 29:582472d0bc57 751 // the PC side (notably DOF) is aware of this and uses level 49
mjr 29:582472d0bc57 752 // to mean "100% on". To ensure compatibility with existing
mjr 29:582472d0bc57 753 // PC-side software, we need to recognize level 49.
mjr 38:091e511ce8a0 754 return 1.0f;
mjr 29:582472d0bc57 755 }
mjr 29:582472d0bc57 756 else if (val == 129)
mjr 29:582472d0bc57 757 {
mjr 29:582472d0bc57 758 // 129 = ramp up / ramp down
mjr 30:6e9902f06f48 759 return wizFlashCounter < 128
mjr 38:091e511ce8a0 760 ? wizFlashCounter/128.0f
mjr 38:091e511ce8a0 761 : (256 - wizFlashCounter)/128.0f;
mjr 29:582472d0bc57 762 }
mjr 29:582472d0bc57 763 else if (val == 130)
mjr 29:582472d0bc57 764 {
mjr 29:582472d0bc57 765 // 130 = flash on / off
mjr 38:091e511ce8a0 766 return wizFlashCounter < 128 ? 1.0f : 0.0f;
mjr 29:582472d0bc57 767 }
mjr 29:582472d0bc57 768 else if (val == 131)
mjr 29:582472d0bc57 769 {
mjr 29:582472d0bc57 770 // 131 = on / ramp down
mjr 38:091e511ce8a0 771 return wizFlashCounter < 128 ? 1.0f : (255 - wizFlashCounter)/128.0f;
mjr 0:5acbbe3f4cf4 772 }
mjr 29:582472d0bc57 773 else if (val == 132)
mjr 29:582472d0bc57 774 {
mjr 29:582472d0bc57 775 // 132 = ramp up / on
mjr 38:091e511ce8a0 776 return wizFlashCounter < 128 ? wizFlashCounter/128.0f : 1.0f;
mjr 29:582472d0bc57 777 }
mjr 29:582472d0bc57 778 else
mjr 13:72dda449c3c0 779 {
mjr 29:582472d0bc57 780 // Other values are undefined in the LedWiz documentation. Hosts
mjr 29:582472d0bc57 781 // *should* never send undefined values, since whatever behavior an
mjr 29:582472d0bc57 782 // LedWiz unit exhibits in response is accidental and could change
mjr 29:582472d0bc57 783 // in a future version. We'll treat all undefined values as equivalent
mjr 29:582472d0bc57 784 // to 48 (fully on).
mjr 38:091e511ce8a0 785 return 1.0f;
mjr 0:5acbbe3f4cf4 786 }
mjr 0:5acbbe3f4cf4 787 }
mjr 0:5acbbe3f4cf4 788
mjr 29:582472d0bc57 789 // LedWiz flash timer pulse. This fires periodically to update
mjr 29:582472d0bc57 790 // LedWiz flashing outputs. At the slowest pulse speed set via
mjr 29:582472d0bc57 791 // the SBA command, each waveform cycle has 256 steps, so we
mjr 29:582472d0bc57 792 // choose the pulse time base so that the slowest cycle completes
mjr 29:582472d0bc57 793 // in 2 seconds. This seems to roughly match the real LedWiz
mjr 29:582472d0bc57 794 // behavior. We run the pulse timer at the same rate regardless
mjr 29:582472d0bc57 795 // of the pulse speed; at higher pulse speeds, we simply use
mjr 29:582472d0bc57 796 // larger steps through the cycle on each interrupt. Running
mjr 29:582472d0bc57 797 // every 1/127 of a second = 8ms seems to be a pretty light load.
mjr 29:582472d0bc57 798 Timeout wizPulseTimer;
mjr 38:091e511ce8a0 799 #define WIZ_PULSE_TIME_BASE (1.0f/127.0f)
mjr 29:582472d0bc57 800 static void wizPulse()
mjr 29:582472d0bc57 801 {
mjr 29:582472d0bc57 802 // increase the counter by the speed increment, and wrap at 256
mjr 29:582472d0bc57 803 wizFlashCounter += wizSpeed;
mjr 29:582472d0bc57 804 wizFlashCounter &= 0xff;
mjr 29:582472d0bc57 805
mjr 29:582472d0bc57 806 // if we have any flashing lights, update them
mjr 29:582472d0bc57 807 int ena = false;
mjr 35:e959ffba78fd 808 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 29:582472d0bc57 809 {
mjr 29:582472d0bc57 810 if (wizOn[i])
mjr 29:582472d0bc57 811 {
mjr 29:582472d0bc57 812 uint8_t s = wizVal[i];
mjr 29:582472d0bc57 813 if (s >= 129 && s <= 132)
mjr 29:582472d0bc57 814 {
mjr 38:091e511ce8a0 815 lwPin[i]->set(wizState(i) * modeLevel[i]);
mjr 29:582472d0bc57 816 ena = true;
mjr 29:582472d0bc57 817 }
mjr 29:582472d0bc57 818 }
mjr 29:582472d0bc57 819 }
mjr 29:582472d0bc57 820
mjr 29:582472d0bc57 821 // Set up the next timer pulse only if we found anything flashing.
mjr 29:582472d0bc57 822 // To minimize overhead from this feature, we only enable the interrupt
mjr 29:582472d0bc57 823 // when we need it. This eliminates any performance penalty to other
mjr 29:582472d0bc57 824 // features when the host software doesn't care about the flashing
mjr 29:582472d0bc57 825 // modes. For example, DOF never uses these modes, so there's no
mjr 29:582472d0bc57 826 // need for them when running Visual Pinball.
mjr 29:582472d0bc57 827 if (ena)
mjr 29:582472d0bc57 828 wizPulseTimer.attach(wizPulse, WIZ_PULSE_TIME_BASE);
mjr 29:582472d0bc57 829 }
mjr 29:582472d0bc57 830
mjr 29:582472d0bc57 831 // Update the physical outputs connected to the LedWiz ports. This is
mjr 29:582472d0bc57 832 // called after any update from an LedWiz protocol message.
mjr 1:d913e0afb2ac 833 static void updateWizOuts()
mjr 1:d913e0afb2ac 834 {
mjr 29:582472d0bc57 835 // update each output
mjr 29:582472d0bc57 836 int pulse = false;
mjr 35:e959ffba78fd 837 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 29:582472d0bc57 838 {
mjr 29:582472d0bc57 839 pulse |= (wizVal[i] >= 129 && wizVal[i] <= 132);
mjr 38:091e511ce8a0 840 lwPin[i]->set(wizState(i) * modeLevel[i]);
mjr 29:582472d0bc57 841 }
mjr 29:582472d0bc57 842
mjr 29:582472d0bc57 843 // if any outputs are set to flashing mode, and the pulse timer
mjr 29:582472d0bc57 844 // isn't running, turn it on
mjr 29:582472d0bc57 845 if (pulse)
mjr 29:582472d0bc57 846 wizPulseTimer.attach(wizPulse, WIZ_PULSE_TIME_BASE);
mjr 34:6b981a2afab7 847
mjr 34:6b981a2afab7 848 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 849 if (hc595 != 0)
mjr 35:e959ffba78fd 850 hc595->update();
mjr 1:d913e0afb2ac 851 }
mjr 38:091e511ce8a0 852
mjr 38:091e511ce8a0 853 // Update all physical outputs. This is called after a change to a global
mjr 38:091e511ce8a0 854 // setting that affects all outputs, such as engaging or canceling Night Mode.
mjr 38:091e511ce8a0 855 static void updateAllOuts()
mjr 38:091e511ce8a0 856 {
mjr 38:091e511ce8a0 857 // uddate each LedWiz output
mjr 38:091e511ce8a0 858 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 38:091e511ce8a0 859 lwPin[i]->set(wizState(i) * modeLevel[i]);
mjr 34:6b981a2afab7 860
mjr 38:091e511ce8a0 861 // update each extended output
mjr 38:091e511ce8a0 862 for (int i = 33 ; i < numOutputs ; ++i)
mjr 38:091e511ce8a0 863 lwPin[i]->set(outLevel[i] * modeLevel[i]);
mjr 38:091e511ce8a0 864
mjr 38:091e511ce8a0 865 // flush 74HC595 changes, if necessary
mjr 38:091e511ce8a0 866 if (hc595 != 0)
mjr 38:091e511ce8a0 867 hc595->update();
mjr 38:091e511ce8a0 868 }
mjr 38:091e511ce8a0 869
mjr 11:bd9da7088e6e 870 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 871 //
mjr 11:bd9da7088e6e 872 // Button input
mjr 11:bd9da7088e6e 873 //
mjr 11:bd9da7088e6e 874
mjr 18:5e890ebd0023 875 // button state
mjr 18:5e890ebd0023 876 struct ButtonState
mjr 18:5e890ebd0023 877 {
mjr 38:091e511ce8a0 878 ButtonState()
mjr 38:091e511ce8a0 879 {
mjr 38:091e511ce8a0 880 di = NULL;
mjr 38:091e511ce8a0 881 on = 0;
mjr 38:091e511ce8a0 882 pressed = prev = 0;
mjr 38:091e511ce8a0 883 dbstate = 0;
mjr 38:091e511ce8a0 884 js = 0;
mjr 38:091e511ce8a0 885 keymod = 0;
mjr 38:091e511ce8a0 886 keycode = 0;
mjr 38:091e511ce8a0 887 special = 0;
mjr 38:091e511ce8a0 888 pulseState = 0;
mjr 38:091e511ce8a0 889 pulseTime = 0.0f;
mjr 38:091e511ce8a0 890 }
mjr 35:e959ffba78fd 891
mjr 35:e959ffba78fd 892 // DigitalIn for the button
mjr 35:e959ffba78fd 893 DigitalIn *di;
mjr 38:091e511ce8a0 894
mjr 38:091e511ce8a0 895 // current PHYSICAL on/off state, after debouncing
mjr 38:091e511ce8a0 896 uint8_t on;
mjr 18:5e890ebd0023 897
mjr 38:091e511ce8a0 898 // current LOGICAL on/off state as reported to the host.
mjr 38:091e511ce8a0 899 uint8_t pressed;
mjr 38:091e511ce8a0 900
mjr 38:091e511ce8a0 901 // previous logical on/off state, when keys were last processed for USB
mjr 38:091e511ce8a0 902 // reports and local effects
mjr 38:091e511ce8a0 903 uint8_t prev;
mjr 38:091e511ce8a0 904
mjr 38:091e511ce8a0 905 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 906 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 907 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 908 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 909 // a parameter that determines how long we wait for transients to settle).
mjr 38:091e511ce8a0 910 uint8_t dbstate;
mjr 35:e959ffba78fd 911
mjr 35:e959ffba78fd 912 // joystick button mask for the button, if mapped as a joystick button
mjr 35:e959ffba78fd 913 uint32_t js;
mjr 35:e959ffba78fd 914
mjr 35:e959ffba78fd 915 // keyboard modifier bits and scan code for the button, if mapped as a keyboard key
mjr 35:e959ffba78fd 916 uint8_t keymod;
mjr 35:e959ffba78fd 917 uint8_t keycode;
mjr 35:e959ffba78fd 918
mjr 35:e959ffba78fd 919 // media control key code
mjr 35:e959ffba78fd 920 uint8_t mediakey;
mjr 35:e959ffba78fd 921
mjr 38:091e511ce8a0 922 // special key code
mjr 38:091e511ce8a0 923 uint8_t special;
mjr 38:091e511ce8a0 924
mjr 38:091e511ce8a0 925 // Pulse mode: a button in pulse mode transmits a brief logical button press and
mjr 38:091e511ce8a0 926 // release each time the attached physical switch changes state. This is useful
mjr 38:091e511ce8a0 927 // for cases where the host expects a key press for each change in the state of
mjr 38:091e511ce8a0 928 // the physical switch. The canonical example is the Coin Door switch in VPinMAME,
mjr 38:091e511ce8a0 929 // which requires pressing the END key to toggle the open/closed state. This
mjr 38:091e511ce8a0 930 // software design isn't easily implemented in a physical coin door, though -
mjr 38:091e511ce8a0 931 // the easiest way to sense a physical coin door's state is with a simple on/off
mjr 38:091e511ce8a0 932 // switch. Pulse mode bridges that divide by converting a physical switch state
mjr 38:091e511ce8a0 933 // to on/off toggle key reports to the host.
mjr 38:091e511ce8a0 934 //
mjr 38:091e511ce8a0 935 // Pulse state:
mjr 38:091e511ce8a0 936 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 937 // 1 -> off
mjr 38:091e511ce8a0 938 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 939 // 3 -> on
mjr 38:091e511ce8a0 940 // 4 -> transitioning on-off
mjr 38:091e511ce8a0 941 //
mjr 38:091e511ce8a0 942 // Each state change sticks for a minimum period; when the timer expires,
mjr 38:091e511ce8a0 943 // if the underlying physical switch is in a different state, we switch
mjr 38:091e511ce8a0 944 // to the next state and restart the timer. pulseTime is the amount of
mjr 38:091e511ce8a0 945 // time remaining before we can make another state transition. The state
mjr 38:091e511ce8a0 946 // transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...; this
mjr 38:091e511ce8a0 947 // guarantees that the parity of the pulse count always matches the
mjr 38:091e511ce8a0 948 // current physical switch state when the latter is stable, which makes
mjr 38:091e511ce8a0 949 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 38:091e511ce8a0 950 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 38:091e511ce8a0 951 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 38:091e511ce8a0 952 // This software system can't be fooled that way.)
mjr 38:091e511ce8a0 953 uint8_t pulseState;
mjr 38:091e511ce8a0 954 float pulseTime;
mjr 38:091e511ce8a0 955
mjr 35:e959ffba78fd 956 } buttonState[MAX_BUTTONS];
mjr 18:5e890ebd0023 957
mjr 38:091e511ce8a0 958
mjr 38:091e511ce8a0 959 // Button data
mjr 38:091e511ce8a0 960 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 961
mjr 38:091e511ce8a0 962 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 963 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 964 // modifier keys.
mjr 38:091e511ce8a0 965 struct
mjr 38:091e511ce8a0 966 {
mjr 38:091e511ce8a0 967 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 968 int nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 969 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 970 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 971 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 972
mjr 38:091e511ce8a0 973 // Media key state
mjr 38:091e511ce8a0 974 struct
mjr 38:091e511ce8a0 975 {
mjr 38:091e511ce8a0 976 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 977 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 978 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 979
mjr 38:091e511ce8a0 980 // button scan interrupt ticker
mjr 38:091e511ce8a0 981 Ticker buttonTicker;
mjr 38:091e511ce8a0 982
mjr 38:091e511ce8a0 983 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 984 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 985 void scanButtons()
mjr 38:091e511ce8a0 986 {
mjr 38:091e511ce8a0 987 // scan all button input pins
mjr 38:091e511ce8a0 988 ButtonState *bs = buttonState;
mjr 38:091e511ce8a0 989 for (int i = 0 ; i < MAX_BUTTONS ; ++i, ++bs)
mjr 38:091e511ce8a0 990 {
mjr 38:091e511ce8a0 991 // if it's connected, check its physical state
mjr 38:091e511ce8a0 992 if (bs->di != NULL)
mjr 38:091e511ce8a0 993 {
mjr 38:091e511ce8a0 994 // Shift the new state into the debounce history. Note that
mjr 38:091e511ce8a0 995 // the physical pin inputs are active low (0V/GND = ON), so invert
mjr 38:091e511ce8a0 996 // the reading by XOR'ing the low bit with 1. And of course we
mjr 38:091e511ce8a0 997 // only want the low bit (since the history is effectively a bit
mjr 38:091e511ce8a0 998 // vector), so mask the whole thing with 0x01 as well.
mjr 38:091e511ce8a0 999 uint8_t db = bs->dbstate;
mjr 38:091e511ce8a0 1000 db <<= 1;
mjr 38:091e511ce8a0 1001 db |= (bs->di->read() & 0x01) ^ 0x01;
mjr 38:091e511ce8a0 1002 bs->dbstate = db;
mjr 38:091e511ce8a0 1003
mjr 38:091e511ce8a0 1004 // if we have all 0's or 1's in the history for the required
mjr 38:091e511ce8a0 1005 // debounce period, the key state is stable - check for a change
mjr 38:091e511ce8a0 1006 // to the last stable state
mjr 38:091e511ce8a0 1007 const uint8_t stable = 0x1F; // 00011111b -> 5 stable readings
mjr 38:091e511ce8a0 1008 db &= stable;
mjr 38:091e511ce8a0 1009 if (db == 0 || db == stable)
mjr 38:091e511ce8a0 1010 bs->on = db;
mjr 38:091e511ce8a0 1011 }
mjr 38:091e511ce8a0 1012 }
mjr 38:091e511ce8a0 1013 }
mjr 38:091e511ce8a0 1014
mjr 38:091e511ce8a0 1015 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 1016 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 1017 // in the physical button state.
mjr 38:091e511ce8a0 1018 Timer buttonTimer;
mjr 12:669df364a565 1019
mjr 11:bd9da7088e6e 1020 // initialize the button inputs
mjr 35:e959ffba78fd 1021 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 1022 {
mjr 35:e959ffba78fd 1023 // presume we'll find no keyboard keys
mjr 35:e959ffba78fd 1024 kbKeys = false;
mjr 35:e959ffba78fd 1025
mjr 11:bd9da7088e6e 1026 // create the digital inputs
mjr 35:e959ffba78fd 1027 ButtonState *bs = buttonState;
mjr 35:e959ffba78fd 1028 for (int i = 0 ; i < MAX_BUTTONS ; ++i, ++bs)
mjr 11:bd9da7088e6e 1029 {
mjr 35:e959ffba78fd 1030 PinName pin = wirePinName(cfg.button[i].pin);
mjr 35:e959ffba78fd 1031 if (pin != NC)
mjr 35:e959ffba78fd 1032 {
mjr 35:e959ffba78fd 1033 // set up the GPIO input pin for this button
mjr 35:e959ffba78fd 1034 bs->di = new DigitalIn(pin);
mjr 35:e959ffba78fd 1035
mjr 38:091e511ce8a0 1036 // if it's a pulse mode button, set the initial pulse state to Off
mjr 38:091e511ce8a0 1037 if (cfg.button[i].flags & BtnFlagPulse)
mjr 38:091e511ce8a0 1038 bs->pulseState = 1;
mjr 38:091e511ce8a0 1039
mjr 35:e959ffba78fd 1040 // note if it's a keyboard key of some kind (including media keys)
mjr 35:e959ffba78fd 1041 uint8_t val = cfg.button[i].val;
mjr 35:e959ffba78fd 1042 switch (cfg.button[i].typ)
mjr 35:e959ffba78fd 1043 {
mjr 35:e959ffba78fd 1044 case BtnTypeJoystick:
mjr 35:e959ffba78fd 1045 // joystick button - get the button bit mask
mjr 35:e959ffba78fd 1046 bs->js = 1 << val;
mjr 35:e959ffba78fd 1047 break;
mjr 35:e959ffba78fd 1048
mjr 35:e959ffba78fd 1049 case BtnTypeKey:
mjr 35:e959ffba78fd 1050 // regular keyboard key - note the scan code
mjr 35:e959ffba78fd 1051 bs->keycode = val;
mjr 35:e959ffba78fd 1052 kbKeys = true;
mjr 35:e959ffba78fd 1053 break;
mjr 35:e959ffba78fd 1054
mjr 35:e959ffba78fd 1055 case BtnTypeModKey:
mjr 35:e959ffba78fd 1056 // keyboard mod key - note the modifier mask
mjr 35:e959ffba78fd 1057 bs->keymod = val;
mjr 35:e959ffba78fd 1058 kbKeys = true;
mjr 35:e959ffba78fd 1059 break;
mjr 35:e959ffba78fd 1060
mjr 35:e959ffba78fd 1061 case BtnTypeMedia:
mjr 35:e959ffba78fd 1062 // media key - note the code
mjr 35:e959ffba78fd 1063 bs->mediakey = val;
mjr 35:e959ffba78fd 1064 kbKeys = true;
mjr 35:e959ffba78fd 1065 break;
mjr 39:b3815a1c3802 1066
mjr 39:b3815a1c3802 1067 case BtnTypeSpecial:
mjr 39:b3815a1c3802 1068 // special key
mjr 39:b3815a1c3802 1069 bs->special = val;
mjr 39:b3815a1c3802 1070 break;
mjr 35:e959ffba78fd 1071 }
mjr 35:e959ffba78fd 1072 }
mjr 11:bd9da7088e6e 1073 }
mjr 12:669df364a565 1074
mjr 38:091e511ce8a0 1075 // start the button scan thread
mjr 38:091e511ce8a0 1076 buttonTicker.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 1077
mjr 38:091e511ce8a0 1078 // start the button state transition timer
mjr 12:669df364a565 1079 buttonTimer.start();
mjr 11:bd9da7088e6e 1080 }
mjr 11:bd9da7088e6e 1081
mjr 38:091e511ce8a0 1082 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 1083 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 1084 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 1085 // mapped to special device functions (e.g., Night Mode).
mjr 38:091e511ce8a0 1086 void processButtons()
mjr 35:e959ffba78fd 1087 {
mjr 35:e959ffba78fd 1088 // start with an empty list of USB key codes
mjr 35:e959ffba78fd 1089 uint8_t modkeys = 0;
mjr 35:e959ffba78fd 1090 uint8_t keys[7] = { 0, 0, 0, 0, 0, 0, 0 };
mjr 35:e959ffba78fd 1091 int nkeys = 0;
mjr 11:bd9da7088e6e 1092
mjr 35:e959ffba78fd 1093 // clear the joystick buttons
mjr 36:b9747461331e 1094 uint32_t newjs = 0;
mjr 35:e959ffba78fd 1095
mjr 35:e959ffba78fd 1096 // start with no media keys pressed
mjr 35:e959ffba78fd 1097 uint8_t mediakeys = 0;
mjr 38:091e511ce8a0 1098
mjr 38:091e511ce8a0 1099 // calculate the time since the last run
mjr 35:e959ffba78fd 1100 float dt = buttonTimer.read();
mjr 18:5e890ebd0023 1101 buttonTimer.reset();
mjr 38:091e511ce8a0 1102
mjr 11:bd9da7088e6e 1103 // scan the button list
mjr 18:5e890ebd0023 1104 ButtonState *bs = buttonState;
mjr 35:e959ffba78fd 1105 for (int i = 0 ; i < MAX_BUTTONS ; ++i, ++bs)
mjr 11:bd9da7088e6e 1106 {
mjr 38:091e511ce8a0 1107 // if it's a pulse-mode switch, get the virtual pressed state
mjr 38:091e511ce8a0 1108 if (bs->pulseState != 0)
mjr 18:5e890ebd0023 1109 {
mjr 38:091e511ce8a0 1110 // deduct the time to the next state change
mjr 38:091e511ce8a0 1111 bs->pulseTime -= dt;
mjr 38:091e511ce8a0 1112 if (bs->pulseTime < 0)
mjr 38:091e511ce8a0 1113 bs->pulseTime = 0;
mjr 38:091e511ce8a0 1114
mjr 38:091e511ce8a0 1115 // if the timer has expired, check for state changes
mjr 38:091e511ce8a0 1116 if (bs->pulseTime == 0)
mjr 18:5e890ebd0023 1117 {
mjr 38:091e511ce8a0 1118 const float pulseLength = 0.2;
mjr 38:091e511ce8a0 1119 switch (bs->pulseState)
mjr 18:5e890ebd0023 1120 {
mjr 38:091e511ce8a0 1121 case 1:
mjr 38:091e511ce8a0 1122 // off - if the physical switch is now on, start a button pulse
mjr 38:091e511ce8a0 1123 if (bs->on) {
mjr 38:091e511ce8a0 1124 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1125 bs->pulseState = 2;
mjr 38:091e511ce8a0 1126 bs->pressed = 1;
mjr 38:091e511ce8a0 1127 }
mjr 38:091e511ce8a0 1128 break;
mjr 18:5e890ebd0023 1129
mjr 38:091e511ce8a0 1130 case 2:
mjr 38:091e511ce8a0 1131 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 1132 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 1133 // change in state in the logical button
mjr 38:091e511ce8a0 1134 bs->pulseState = 3;
mjr 38:091e511ce8a0 1135 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1136 bs->pressed = 0;
mjr 38:091e511ce8a0 1137 break;
mjr 38:091e511ce8a0 1138
mjr 38:091e511ce8a0 1139 case 3:
mjr 38:091e511ce8a0 1140 // on - if the physical switch is now off, start a button pulse
mjr 38:091e511ce8a0 1141 if (!bs->on) {
mjr 38:091e511ce8a0 1142 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1143 bs->pulseState = 4;
mjr 38:091e511ce8a0 1144 bs->pressed = 1;
mjr 38:091e511ce8a0 1145 }
mjr 38:091e511ce8a0 1146 break;
mjr 38:091e511ce8a0 1147
mjr 38:091e511ce8a0 1148 case 4:
mjr 38:091e511ce8a0 1149 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 1150 bs->pulseState = 1;
mjr 38:091e511ce8a0 1151 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1152 bs->pressed = 0;
mjr 38:091e511ce8a0 1153 break;
mjr 18:5e890ebd0023 1154 }
mjr 18:5e890ebd0023 1155 }
mjr 38:091e511ce8a0 1156 }
mjr 38:091e511ce8a0 1157 else
mjr 38:091e511ce8a0 1158 {
mjr 38:091e511ce8a0 1159 // not a pulse switch - the logical state is the same as the physical state
mjr 38:091e511ce8a0 1160 bs->pressed = bs->on;
mjr 38:091e511ce8a0 1161 }
mjr 35:e959ffba78fd 1162
mjr 38:091e511ce8a0 1163 // carry out any edge effects from buttons changing states
mjr 38:091e511ce8a0 1164 if (bs->pressed != bs->prev)
mjr 38:091e511ce8a0 1165 {
mjr 38:091e511ce8a0 1166 // check for special key transitions
mjr 38:091e511ce8a0 1167 switch (bs->special)
mjr 35:e959ffba78fd 1168 {
mjr 38:091e511ce8a0 1169 case 1:
mjr 38:091e511ce8a0 1170 // night mode momentary switch - when the button transitions from
mjr 38:091e511ce8a0 1171 // OFF to ON, invert night mode
mjr 38:091e511ce8a0 1172 if (bs->pressed)
mjr 38:091e511ce8a0 1173 toggleNightMode();
mjr 38:091e511ce8a0 1174 break;
mjr 35:e959ffba78fd 1175
mjr 38:091e511ce8a0 1176 case 2:
mjr 38:091e511ce8a0 1177 // night mode toggle switch - when the button changes state, change
mjr 38:091e511ce8a0 1178 // night mode to match the new state
mjr 38:091e511ce8a0 1179 setNightMode(bs->pressed);
mjr 38:091e511ce8a0 1180 break;
mjr 35:e959ffba78fd 1181 }
mjr 38:091e511ce8a0 1182
mjr 38:091e511ce8a0 1183 // remember the new state for comparison on the next run
mjr 38:091e511ce8a0 1184 bs->prev = bs->pressed;
mjr 38:091e511ce8a0 1185 }
mjr 38:091e511ce8a0 1186
mjr 38:091e511ce8a0 1187 // if it's pressed, add it to the appropriate key state list
mjr 38:091e511ce8a0 1188 if (bs->pressed)
mjr 38:091e511ce8a0 1189 {
mjr 38:091e511ce8a0 1190 // OR in the joystick button bit, mod key bits, and media key bits
mjr 38:091e511ce8a0 1191 newjs |= bs->js;
mjr 38:091e511ce8a0 1192 modkeys |= bs->keymod;
mjr 38:091e511ce8a0 1193 mediakeys |= bs->mediakey;
mjr 38:091e511ce8a0 1194
mjr 38:091e511ce8a0 1195 // if it has a keyboard key, add the scan code to the active list
mjr 38:091e511ce8a0 1196 if (bs->keycode != 0 && nkeys < 7)
mjr 38:091e511ce8a0 1197 keys[nkeys++] = bs->keycode;
mjr 18:5e890ebd0023 1198 }
mjr 11:bd9da7088e6e 1199 }
mjr 36:b9747461331e 1200
mjr 36:b9747461331e 1201 // check for joystick button changes
mjr 36:b9747461331e 1202 if (jsButtons != newjs)
mjr 36:b9747461331e 1203 jsButtons = newjs;
mjr 11:bd9da7088e6e 1204
mjr 35:e959ffba78fd 1205 // Check for changes to the keyboard keys
mjr 35:e959ffba78fd 1206 if (kbState.data[0] != modkeys
mjr 35:e959ffba78fd 1207 || kbState.nkeys != nkeys
mjr 35:e959ffba78fd 1208 || memcmp(keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 1209 {
mjr 35:e959ffba78fd 1210 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 1211 kbState.changed = true;
mjr 35:e959ffba78fd 1212 kbState.data[0] = modkeys;
mjr 35:e959ffba78fd 1213 if (nkeys <= 6) {
mjr 35:e959ffba78fd 1214 // 6 or fewer simultaneous keys - report the key codes
mjr 35:e959ffba78fd 1215 kbState.nkeys = nkeys;
mjr 35:e959ffba78fd 1216 memcpy(&kbState.data[2], keys, 6);
mjr 35:e959ffba78fd 1217 }
mjr 35:e959ffba78fd 1218 else {
mjr 35:e959ffba78fd 1219 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 1220 kbState.nkeys = 6;
mjr 35:e959ffba78fd 1221 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 1222 }
mjr 35:e959ffba78fd 1223 }
mjr 35:e959ffba78fd 1224
mjr 35:e959ffba78fd 1225 // Check for changes to media keys
mjr 35:e959ffba78fd 1226 if (mediaState.data != mediakeys)
mjr 35:e959ffba78fd 1227 {
mjr 35:e959ffba78fd 1228 mediaState.changed = true;
mjr 35:e959ffba78fd 1229 mediaState.data = mediakeys;
mjr 35:e959ffba78fd 1230 }
mjr 11:bd9da7088e6e 1231 }
mjr 11:bd9da7088e6e 1232
mjr 5:a70c0bce770d 1233 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1234 //
mjr 5:a70c0bce770d 1235 // Customization joystick subbclass
mjr 5:a70c0bce770d 1236 //
mjr 5:a70c0bce770d 1237
mjr 5:a70c0bce770d 1238 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 1239 {
mjr 5:a70c0bce770d 1240 public:
mjr 35:e959ffba78fd 1241 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 35:e959ffba78fd 1242 bool waitForConnect, bool enableJoystick, bool useKB)
mjr 35:e959ffba78fd 1243 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, useKB)
mjr 5:a70c0bce770d 1244 {
mjr 5:a70c0bce770d 1245 suspended_ = false;
mjr 5:a70c0bce770d 1246 }
mjr 5:a70c0bce770d 1247
mjr 5:a70c0bce770d 1248 // are we connected?
mjr 5:a70c0bce770d 1249 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 1250
mjr 5:a70c0bce770d 1251 // Are we in suspend mode?
mjr 5:a70c0bce770d 1252 int isSuspended() const { return suspended_; }
mjr 5:a70c0bce770d 1253
mjr 5:a70c0bce770d 1254 protected:
mjr 5:a70c0bce770d 1255 virtual void suspendStateChanged(unsigned int suspended)
mjr 5:a70c0bce770d 1256 { suspended_ = suspended; }
mjr 5:a70c0bce770d 1257
mjr 5:a70c0bce770d 1258 // are we suspended?
mjr 5:a70c0bce770d 1259 int suspended_;
mjr 5:a70c0bce770d 1260 };
mjr 5:a70c0bce770d 1261
mjr 5:a70c0bce770d 1262 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1263 //
mjr 5:a70c0bce770d 1264 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 1265 //
mjr 5:a70c0bce770d 1266
mjr 5:a70c0bce770d 1267 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 1268 //
mjr 5:a70c0bce770d 1269 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 1270 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 1271 // automatic calibration.
mjr 5:a70c0bce770d 1272 //
mjr 5:a70c0bce770d 1273 // We install an interrupt handler on the accelerometer "data ready"
mjr 6:cc35eb643e8f 1274 // interrupt to ensure that we fetch each sample immediately when it
mjr 6:cc35eb643e8f 1275 // becomes available. The accelerometer data rate is fiarly high
mjr 6:cc35eb643e8f 1276 // (800 Hz), so it's not practical to keep up with it by polling.
mjr 6:cc35eb643e8f 1277 // Using an interrupt handler lets us respond quickly and read
mjr 6:cc35eb643e8f 1278 // every sample.
mjr 5:a70c0bce770d 1279 //
mjr 6:cc35eb643e8f 1280 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 1281 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 1282 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 1283 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 1284 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 1285 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 1286 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 1287 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 1288 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 1289 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 1290 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 1291 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 1292 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 1293 // of nudging, say).
mjr 5:a70c0bce770d 1294 //
mjr 5:a70c0bce770d 1295
mjr 17:ab3cec0c8bf4 1296 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 1297 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 1298
mjr 17:ab3cec0c8bf4 1299 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 1300 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 1301 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 1302
mjr 17:ab3cec0c8bf4 1303 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 1304 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 1305 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 1306 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 1307
mjr 17:ab3cec0c8bf4 1308
mjr 6:cc35eb643e8f 1309 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 1310 struct AccHist
mjr 5:a70c0bce770d 1311 {
mjr 6:cc35eb643e8f 1312 AccHist() { x = y = d = 0.0; xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 1313 void set(float x, float y, AccHist *prv)
mjr 6:cc35eb643e8f 1314 {
mjr 6:cc35eb643e8f 1315 // save the raw position
mjr 6:cc35eb643e8f 1316 this->x = x;
mjr 6:cc35eb643e8f 1317 this->y = y;
mjr 6:cc35eb643e8f 1318 this->d = distance(prv);
mjr 6:cc35eb643e8f 1319 }
mjr 6:cc35eb643e8f 1320
mjr 6:cc35eb643e8f 1321 // reading for this entry
mjr 5:a70c0bce770d 1322 float x, y;
mjr 5:a70c0bce770d 1323
mjr 6:cc35eb643e8f 1324 // distance from previous entry
mjr 6:cc35eb643e8f 1325 float d;
mjr 5:a70c0bce770d 1326
mjr 6:cc35eb643e8f 1327 // total and count of samples averaged over this period
mjr 6:cc35eb643e8f 1328 float xtot, ytot;
mjr 6:cc35eb643e8f 1329 int cnt;
mjr 6:cc35eb643e8f 1330
mjr 6:cc35eb643e8f 1331 void clearAvg() { xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 1332 void addAvg(float x, float y) { xtot += x; ytot += y; ++cnt; }
mjr 6:cc35eb643e8f 1333 float xAvg() const { return xtot/cnt; }
mjr 6:cc35eb643e8f 1334 float yAvg() const { return ytot/cnt; }
mjr 5:a70c0bce770d 1335
mjr 6:cc35eb643e8f 1336 float distance(AccHist *p)
mjr 6:cc35eb643e8f 1337 { return sqrt(square(p->x - x) + square(p->y - y)); }
mjr 5:a70c0bce770d 1338 };
mjr 5:a70c0bce770d 1339
mjr 5:a70c0bce770d 1340 // accelerometer wrapper class
mjr 3:3514575d4f86 1341 class Accel
mjr 3:3514575d4f86 1342 {
mjr 3:3514575d4f86 1343 public:
mjr 3:3514575d4f86 1344 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin)
mjr 3:3514575d4f86 1345 : mma_(sda, scl, i2cAddr), intIn_(irqPin)
mjr 3:3514575d4f86 1346 {
mjr 5:a70c0bce770d 1347 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 1348 irqPin_ = irqPin;
mjr 5:a70c0bce770d 1349
mjr 5:a70c0bce770d 1350 // reset and initialize
mjr 5:a70c0bce770d 1351 reset();
mjr 5:a70c0bce770d 1352 }
mjr 5:a70c0bce770d 1353
mjr 5:a70c0bce770d 1354 void reset()
mjr 5:a70c0bce770d 1355 {
mjr 6:cc35eb643e8f 1356 // clear the center point
mjr 6:cc35eb643e8f 1357 cx_ = cy_ = 0.0;
mjr 6:cc35eb643e8f 1358
mjr 6:cc35eb643e8f 1359 // start the calibration timer
mjr 5:a70c0bce770d 1360 tCenter_.start();
mjr 5:a70c0bce770d 1361 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 1362
mjr 5:a70c0bce770d 1363 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 1364 mma_.init();
mjr 6:cc35eb643e8f 1365
mjr 6:cc35eb643e8f 1366 // set the initial integrated velocity reading to zero
mjr 6:cc35eb643e8f 1367 vx_ = vy_ = 0;
mjr 3:3514575d4f86 1368
mjr 6:cc35eb643e8f 1369 // set up our accelerometer interrupt handling
mjr 6:cc35eb643e8f 1370 intIn_.rise(this, &Accel::isr);
mjr 5:a70c0bce770d 1371 mma_.setInterruptMode(irqPin_ == PTA14 ? 1 : 2);
mjr 3:3514575d4f86 1372
mjr 3:3514575d4f86 1373 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 1374 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 1375
mjr 3:3514575d4f86 1376 // start our timers
mjr 3:3514575d4f86 1377 tGet_.start();
mjr 3:3514575d4f86 1378 tInt_.start();
mjr 3:3514575d4f86 1379 }
mjr 3:3514575d4f86 1380
mjr 9:fd65b0a94720 1381 void get(int &x, int &y)
mjr 3:3514575d4f86 1382 {
mjr 3:3514575d4f86 1383 // disable interrupts while manipulating the shared data
mjr 3:3514575d4f86 1384 __disable_irq();
mjr 3:3514575d4f86 1385
mjr 3:3514575d4f86 1386 // read the shared data and store locally for calculations
mjr 6:cc35eb643e8f 1387 float ax = ax_, ay = ay_;
mjr 6:cc35eb643e8f 1388 float vx = vx_, vy = vy_;
mjr 5:a70c0bce770d 1389
mjr 6:cc35eb643e8f 1390 // reset the velocity sum for the next run
mjr 6:cc35eb643e8f 1391 vx_ = vy_ = 0;
mjr 3:3514575d4f86 1392
mjr 3:3514575d4f86 1393 // get the time since the last get() sample
mjr 38:091e511ce8a0 1394 float dt = tGet_.read_us()/1.0e6f;
mjr 3:3514575d4f86 1395 tGet_.reset();
mjr 3:3514575d4f86 1396
mjr 3:3514575d4f86 1397 // done manipulating the shared data
mjr 3:3514575d4f86 1398 __enable_irq();
mjr 3:3514575d4f86 1399
mjr 6:cc35eb643e8f 1400 // adjust the readings for the integration time
mjr 6:cc35eb643e8f 1401 vx /= dt;
mjr 6:cc35eb643e8f 1402 vy /= dt;
mjr 6:cc35eb643e8f 1403
mjr 6:cc35eb643e8f 1404 // add this sample to the current calibration interval's running total
mjr 6:cc35eb643e8f 1405 AccHist *p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 1406 p->addAvg(ax, ay);
mjr 6:cc35eb643e8f 1407
mjr 5:a70c0bce770d 1408 // check for auto-centering every so often
mjr 5:a70c0bce770d 1409 if (tCenter_.read_ms() > 1000)
mjr 5:a70c0bce770d 1410 {
mjr 5:a70c0bce770d 1411 // add the latest raw sample to the history list
mjr 6:cc35eb643e8f 1412 AccHist *prv = p;
mjr 5:a70c0bce770d 1413 iAccPrv_ = (iAccPrv_ + 1) % maxAccPrv;
mjr 6:cc35eb643e8f 1414 p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 1415 p->set(ax, ay, prv);
mjr 5:a70c0bce770d 1416
mjr 5:a70c0bce770d 1417 // if we have a full complement, check for stability
mjr 5:a70c0bce770d 1418 if (nAccPrv_ >= maxAccPrv)
mjr 5:a70c0bce770d 1419 {
mjr 5:a70c0bce770d 1420 // check if we've been stable for all recent samples
mjr 6:cc35eb643e8f 1421 static const float accTol = .01;
mjr 6:cc35eb643e8f 1422 AccHist *p0 = accPrv_;
mjr 6:cc35eb643e8f 1423 if (p0[0].d < accTol
mjr 6:cc35eb643e8f 1424 && p0[1].d < accTol
mjr 6:cc35eb643e8f 1425 && p0[2].d < accTol
mjr 6:cc35eb643e8f 1426 && p0[3].d < accTol
mjr 6:cc35eb643e8f 1427 && p0[4].d < accTol)
mjr 5:a70c0bce770d 1428 {
mjr 6:cc35eb643e8f 1429 // Figure the new calibration point as the average of
mjr 6:cc35eb643e8f 1430 // the samples over the rest period
mjr 6:cc35eb643e8f 1431 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5.0;
mjr 6:cc35eb643e8f 1432 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5.0;
mjr 5:a70c0bce770d 1433 }
mjr 5:a70c0bce770d 1434 }
mjr 5:a70c0bce770d 1435 else
mjr 5:a70c0bce770d 1436 {
mjr 5:a70c0bce770d 1437 // not enough samples yet; just up the count
mjr 5:a70c0bce770d 1438 ++nAccPrv_;
mjr 5:a70c0bce770d 1439 }
mjr 6:cc35eb643e8f 1440
mjr 6:cc35eb643e8f 1441 // clear the new item's running totals
mjr 6:cc35eb643e8f 1442 p->clearAvg();
mjr 5:a70c0bce770d 1443
mjr 5:a70c0bce770d 1444 // reset the timer
mjr 5:a70c0bce770d 1445 tCenter_.reset();
mjr 39:b3815a1c3802 1446
mjr 39:b3815a1c3802 1447 // If we haven't seen an interrupt in a while, do an explicit read to
mjr 39:b3815a1c3802 1448 // "unstick" the device. The device can become stuck - which is to say,
mjr 39:b3815a1c3802 1449 // it will stop delivering data-ready interrupts - if we fail to service
mjr 39:b3815a1c3802 1450 // one data-ready interrupt before the next one occurs. Reading a sample
mjr 39:b3815a1c3802 1451 // will clear up this overrun condition and allow normal interrupt
mjr 39:b3815a1c3802 1452 // generation to continue.
mjr 39:b3815a1c3802 1453 //
mjr 39:b3815a1c3802 1454 // Note that this stuck condition *shouldn't* ever occur - if it does,
mjr 39:b3815a1c3802 1455 // it means that we're spending a long period with interrupts disabled
mjr 39:b3815a1c3802 1456 // (either in a critical section or in another interrupt handler), which
mjr 39:b3815a1c3802 1457 // will likely cause other worse problems beyond the sticky accelerometer.
mjr 39:b3815a1c3802 1458 // Even so, it's easy to detect and correct, so we'll do so for the sake
mjr 39:b3815a1c3802 1459 // of making the system more fault-tolerant.
mjr 39:b3815a1c3802 1460 if (tInt_.read() > 1.0f)
mjr 39:b3815a1c3802 1461 {
mjr 39:b3815a1c3802 1462 printf("unwedging the accelerometer\r\n");
mjr 39:b3815a1c3802 1463 float x, y, z;
mjr 39:b3815a1c3802 1464 mma_.getAccXYZ(x, y, z);
mjr 39:b3815a1c3802 1465 }
mjr 5:a70c0bce770d 1466 }
mjr 5:a70c0bce770d 1467
mjr 6:cc35eb643e8f 1468 // report our integrated velocity reading in x,y
mjr 6:cc35eb643e8f 1469 x = rawToReport(vx);
mjr 6:cc35eb643e8f 1470 y = rawToReport(vy);
mjr 5:a70c0bce770d 1471
mjr 6:cc35eb643e8f 1472 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 1473 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 1474 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 1475 #endif
mjr 3:3514575d4f86 1476 }
mjr 29:582472d0bc57 1477
mjr 3:3514575d4f86 1478 private:
mjr 6:cc35eb643e8f 1479 // adjust a raw acceleration figure to a usb report value
mjr 6:cc35eb643e8f 1480 int rawToReport(float v)
mjr 5:a70c0bce770d 1481 {
mjr 6:cc35eb643e8f 1482 // scale to the joystick report range and round to integer
mjr 6:cc35eb643e8f 1483 int i = int(round(v*JOYMAX));
mjr 5:a70c0bce770d 1484
mjr 6:cc35eb643e8f 1485 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 1486 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 1487 static const int filter[] = {
mjr 6:cc35eb643e8f 1488 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 1489 0,
mjr 6:cc35eb643e8f 1490 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 1491 };
mjr 6:cc35eb643e8f 1492 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 1493 }
mjr 5:a70c0bce770d 1494
mjr 3:3514575d4f86 1495 // interrupt handler
mjr 3:3514575d4f86 1496 void isr()
mjr 3:3514575d4f86 1497 {
mjr 3:3514575d4f86 1498 // Read the axes. Note that we have to read all three axes
mjr 3:3514575d4f86 1499 // (even though we only really use x and y) in order to clear
mjr 3:3514575d4f86 1500 // the "data ready" status bit in the accelerometer. The
mjr 3:3514575d4f86 1501 // interrupt only occurs when the "ready" bit transitions from
mjr 3:3514575d4f86 1502 // off to on, so we have to make sure it's off.
mjr 5:a70c0bce770d 1503 float x, y, z;
mjr 5:a70c0bce770d 1504 mma_.getAccXYZ(x, y, z);
mjr 3:3514575d4f86 1505
mjr 3:3514575d4f86 1506 // calculate the time since the last interrupt
mjr 39:b3815a1c3802 1507 float dt = tInt_.read();
mjr 3:3514575d4f86 1508 tInt_.reset();
mjr 6:cc35eb643e8f 1509
mjr 6:cc35eb643e8f 1510 // integrate the time slice from the previous reading to this reading
mjr 6:cc35eb643e8f 1511 vx_ += (x + ax_ - 2*cx_)*dt/2;
mjr 6:cc35eb643e8f 1512 vy_ += (y + ay_ - 2*cy_)*dt/2;
mjr 3:3514575d4f86 1513
mjr 6:cc35eb643e8f 1514 // store the updates
mjr 6:cc35eb643e8f 1515 ax_ = x;
mjr 6:cc35eb643e8f 1516 ay_ = y;
mjr 6:cc35eb643e8f 1517 az_ = z;
mjr 3:3514575d4f86 1518 }
mjr 3:3514575d4f86 1519
mjr 3:3514575d4f86 1520 // underlying accelerometer object
mjr 3:3514575d4f86 1521 MMA8451Q mma_;
mjr 3:3514575d4f86 1522
mjr 5:a70c0bce770d 1523 // last raw acceleration readings
mjr 6:cc35eb643e8f 1524 float ax_, ay_, az_;
mjr 5:a70c0bce770d 1525
mjr 6:cc35eb643e8f 1526 // integrated velocity reading since last get()
mjr 6:cc35eb643e8f 1527 float vx_, vy_;
mjr 6:cc35eb643e8f 1528
mjr 3:3514575d4f86 1529 // timer for measuring time between get() samples
mjr 3:3514575d4f86 1530 Timer tGet_;
mjr 3:3514575d4f86 1531
mjr 3:3514575d4f86 1532 // timer for measuring time between interrupts
mjr 3:3514575d4f86 1533 Timer tInt_;
mjr 5:a70c0bce770d 1534
mjr 6:cc35eb643e8f 1535 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 1536 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 1537 // at rest.
mjr 6:cc35eb643e8f 1538 float cx_, cy_;
mjr 5:a70c0bce770d 1539
mjr 5:a70c0bce770d 1540 // timer for atuo-centering
mjr 5:a70c0bce770d 1541 Timer tCenter_;
mjr 6:cc35eb643e8f 1542
mjr 6:cc35eb643e8f 1543 // Auto-centering history. This is a separate history list that
mjr 6:cc35eb643e8f 1544 // records results spaced out sparesely over time, so that we can
mjr 6:cc35eb643e8f 1545 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 1546 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 1547 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 1548 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 1549 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 1550 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 1551 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 1552 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 1553 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 1554 // accelerometer measurement bias (e.g., from temperature changes).
mjr 5:a70c0bce770d 1555 int iAccPrv_, nAccPrv_;
mjr 5:a70c0bce770d 1556 static const int maxAccPrv = 5;
mjr 6:cc35eb643e8f 1557 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 1558
mjr 5:a70c0bce770d 1559 // interurupt pin name
mjr 5:a70c0bce770d 1560 PinName irqPin_;
mjr 5:a70c0bce770d 1561
mjr 5:a70c0bce770d 1562 // interrupt router
mjr 5:a70c0bce770d 1563 InterruptIn intIn_;
mjr 3:3514575d4f86 1564 };
mjr 3:3514575d4f86 1565
mjr 5:a70c0bce770d 1566
mjr 5:a70c0bce770d 1567 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1568 //
mjr 14:df700b22ca08 1569 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 1570 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 1571 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 1572 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 1573 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 1574 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 1575 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 1576 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 1577 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 1578 //
mjr 14:df700b22ca08 1579 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 1580 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 1581 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 1582 //
mjr 5:a70c0bce770d 1583 void clear_i2c()
mjr 5:a70c0bce770d 1584 {
mjr 38:091e511ce8a0 1585 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 1586 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 1587 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 1588
mjr 5:a70c0bce770d 1589 // clock the SCL 9 times
mjr 5:a70c0bce770d 1590 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 1591 {
mjr 5:a70c0bce770d 1592 scl = 1;
mjr 5:a70c0bce770d 1593 wait_us(20);
mjr 5:a70c0bce770d 1594 scl = 0;
mjr 5:a70c0bce770d 1595 wait_us(20);
mjr 5:a70c0bce770d 1596 }
mjr 5:a70c0bce770d 1597 }
mjr 14:df700b22ca08 1598
mjr 14:df700b22ca08 1599 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 1600 //
mjr 33:d832bcab089e 1601 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 1602 // for a given interval before allowing an update.
mjr 33:d832bcab089e 1603 //
mjr 33:d832bcab089e 1604 class Debouncer
mjr 33:d832bcab089e 1605 {
mjr 33:d832bcab089e 1606 public:
mjr 33:d832bcab089e 1607 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 1608 {
mjr 33:d832bcab089e 1609 t.start();
mjr 33:d832bcab089e 1610 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 1611 this->tmin = tmin;
mjr 33:d832bcab089e 1612 }
mjr 33:d832bcab089e 1613
mjr 33:d832bcab089e 1614 // Get the current stable value
mjr 33:d832bcab089e 1615 bool val() const { return stable; }
mjr 33:d832bcab089e 1616
mjr 33:d832bcab089e 1617 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 1618 // input device.
mjr 33:d832bcab089e 1619 void sampleIn(bool val)
mjr 33:d832bcab089e 1620 {
mjr 33:d832bcab089e 1621 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 1622 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 1623 // on the sample reader.
mjr 33:d832bcab089e 1624 if (val != prv)
mjr 33:d832bcab089e 1625 {
mjr 33:d832bcab089e 1626 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 1627 t.reset();
mjr 33:d832bcab089e 1628
mjr 33:d832bcab089e 1629 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 1630 prv = val;
mjr 33:d832bcab089e 1631 }
mjr 33:d832bcab089e 1632 else if (val != stable)
mjr 33:d832bcab089e 1633 {
mjr 33:d832bcab089e 1634 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 1635 // and different from the stable value. This means that
mjr 33:d832bcab089e 1636 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 1637 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 1638 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 1639 if (t.read() > tmin)
mjr 33:d832bcab089e 1640 stable = val;
mjr 33:d832bcab089e 1641 }
mjr 33:d832bcab089e 1642 }
mjr 33:d832bcab089e 1643
mjr 33:d832bcab089e 1644 private:
mjr 33:d832bcab089e 1645 // current stable value
mjr 33:d832bcab089e 1646 bool stable;
mjr 33:d832bcab089e 1647
mjr 33:d832bcab089e 1648 // last raw sample value
mjr 33:d832bcab089e 1649 bool prv;
mjr 33:d832bcab089e 1650
mjr 33:d832bcab089e 1651 // elapsed time since last raw input change
mjr 33:d832bcab089e 1652 Timer t;
mjr 33:d832bcab089e 1653
mjr 33:d832bcab089e 1654 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 1655 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 1656 float tmin;
mjr 33:d832bcab089e 1657 };
mjr 33:d832bcab089e 1658
mjr 33:d832bcab089e 1659
mjr 33:d832bcab089e 1660 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 1661 //
mjr 33:d832bcab089e 1662 // Turn off all outputs and restore everything to the default LedWiz
mjr 33:d832bcab089e 1663 // state. This sets outputs #1-32 to LedWiz profile value 48 (full
mjr 33:d832bcab089e 1664 // brightness) and switch state Off, sets all extended outputs (#33
mjr 33:d832bcab089e 1665 // and above) to zero brightness, and sets the LedWiz flash rate to 2.
mjr 33:d832bcab089e 1666 // This effectively restores the power-on conditions.
mjr 33:d832bcab089e 1667 //
mjr 33:d832bcab089e 1668 void allOutputsOff()
mjr 33:d832bcab089e 1669 {
mjr 33:d832bcab089e 1670 // reset all LedWiz outputs to OFF/48
mjr 35:e959ffba78fd 1671 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 33:d832bcab089e 1672 {
mjr 33:d832bcab089e 1673 outLevel[i] = 0;
mjr 33:d832bcab089e 1674 wizOn[i] = 0;
mjr 33:d832bcab089e 1675 wizVal[i] = 48;
mjr 33:d832bcab089e 1676 lwPin[i]->set(0);
mjr 33:d832bcab089e 1677 }
mjr 33:d832bcab089e 1678
mjr 33:d832bcab089e 1679 // reset all extended outputs (ports >32) to full off (brightness 0)
mjr 33:d832bcab089e 1680 for (int i = 32 ; i < numOutputs ; ++i)
mjr 33:d832bcab089e 1681 {
mjr 33:d832bcab089e 1682 outLevel[i] = 0;
mjr 33:d832bcab089e 1683 lwPin[i]->set(0);
mjr 33:d832bcab089e 1684 }
mjr 33:d832bcab089e 1685
mjr 33:d832bcab089e 1686 // restore default LedWiz flash rate
mjr 33:d832bcab089e 1687 wizSpeed = 2;
mjr 34:6b981a2afab7 1688
mjr 34:6b981a2afab7 1689 // flush changes to hc595, if applicable
mjr 35:e959ffba78fd 1690 if (hc595 != 0)
mjr 35:e959ffba78fd 1691 hc595->update();
mjr 33:d832bcab089e 1692 }
mjr 33:d832bcab089e 1693
mjr 33:d832bcab089e 1694 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 1695 //
mjr 33:d832bcab089e 1696 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 1697 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 1698 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 1699 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 1700 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 1701 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 1702 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 1703 //
mjr 33:d832bcab089e 1704 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 1705 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 1706 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 1707 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 1708 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 1709 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 1710 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 1711 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 1712 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 1713 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 1714 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 1715 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 1716 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 1717 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 1718 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 1719 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 1720 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 1721 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 1722 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 1723 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 1724 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 1725 //
mjr 33:d832bcab089e 1726 // This scheme might seem a little convoluted, but it neatly handles
mjr 33:d832bcab089e 1727 // all of the different cases that can occur:
mjr 33:d832bcab089e 1728 //
mjr 33:d832bcab089e 1729 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 33:d832bcab089e 1730 // so that the PC goes into "Soft Off" mode (ACPI state S5, in Windows
mjr 33:d832bcab089e 1731 // parlance) when the user turns off the cabinet. In this state, the
mjr 33:d832bcab089e 1732 // motherboard supplies power to USB devices, so the KL25Z continues
mjr 33:d832bcab089e 1733 // running without interruption. The latch system lets us monitor
mjr 33:d832bcab089e 1734 // the power state even when we're never rebooted, since the latch
mjr 33:d832bcab089e 1735 // will turn off when PSU2 is off regardless of what the KL25Z is doing.
mjr 33:d832bcab089e 1736 //
mjr 33:d832bcab089e 1737 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 1738 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 1739 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 1740 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 1741 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 1742 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 1743 // will power up before or after PSU2, so it's not good enough to
mjr 33:d832bcab089e 1744 // observe the *current* state of PSU2 when we first check - if PSU2
mjr 33:d832bcab089e 1745 // were to come on first, checking the current state alone would fool
mjr 33:d832bcab089e 1746 // us into thinking that no action is required, because we would never
mjr 33:d832bcab089e 1747 // have known that PSU2 was ever off. The latch handles this case by
mjr 33:d832bcab089e 1748 // letting us see that PSU2 *was* off before we checked.
mjr 33:d832bcab089e 1749 //
mjr 33:d832bcab089e 1750 // - If the KL25Z is rebooted while the main system is running, or the
mjr 33:d832bcab089e 1751 // KL25Z is unplugged and plugged back in, we will correctly leave the
mjr 33:d832bcab089e 1752 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 1753 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 1754 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 1755 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 1756 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 1757 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 1758 //
mjr 33:d832bcab089e 1759 //
mjr 33:d832bcab089e 1760
mjr 33:d832bcab089e 1761 // Current PSU2 state:
mjr 33:d832bcab089e 1762 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 1763 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 1764 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 1765 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 1766 // 5 -> TV relay on
mjr 33:d832bcab089e 1767 //
mjr 33:d832bcab089e 1768 int psu2_state = 1;
mjr 35:e959ffba78fd 1769
mjr 35:e959ffba78fd 1770 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 1771 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 1772 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 1773
mjr 35:e959ffba78fd 1774 // TV ON switch relay control
mjr 35:e959ffba78fd 1775 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 1776
mjr 35:e959ffba78fd 1777 // Timer interrupt
mjr 35:e959ffba78fd 1778 Ticker tv_ticker;
mjr 35:e959ffba78fd 1779 float tv_delay_time;
mjr 33:d832bcab089e 1780 void TVTimerInt()
mjr 33:d832bcab089e 1781 {
mjr 35:e959ffba78fd 1782 // time since last state change
mjr 35:e959ffba78fd 1783 static Timer tv_timer;
mjr 35:e959ffba78fd 1784
mjr 33:d832bcab089e 1785 // Check our internal state
mjr 33:d832bcab089e 1786 switch (psu2_state)
mjr 33:d832bcab089e 1787 {
mjr 33:d832bcab089e 1788 case 1:
mjr 33:d832bcab089e 1789 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 1790 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 1791 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 1792 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 1793 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 1794 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 1795 {
mjr 33:d832bcab089e 1796 // switch to OFF state
mjr 33:d832bcab089e 1797 psu2_state = 2;
mjr 33:d832bcab089e 1798
mjr 33:d832bcab089e 1799 // try setting the latch
mjr 35:e959ffba78fd 1800 psu2_status_set->write(1);
mjr 33:d832bcab089e 1801 }
mjr 33:d832bcab089e 1802 break;
mjr 33:d832bcab089e 1803
mjr 33:d832bcab089e 1804 case 2:
mjr 33:d832bcab089e 1805 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 1806 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 1807 psu2_status_set->write(0);
mjr 33:d832bcab089e 1808 psu2_state = 3;
mjr 33:d832bcab089e 1809 break;
mjr 33:d832bcab089e 1810
mjr 33:d832bcab089e 1811 case 3:
mjr 33:d832bcab089e 1812 // CHECK state: we pulsed SET, and we're now ready to see
mjr 33:d832bcab089e 1813 // if that stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 1814 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 1815 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 1816 if (psu2_status_sense->read())
mjr 33:d832bcab089e 1817 {
mjr 33:d832bcab089e 1818 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 1819 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 1820 tv_timer.reset();
mjr 33:d832bcab089e 1821 tv_timer.start();
mjr 33:d832bcab089e 1822 psu2_state = 4;
mjr 33:d832bcab089e 1823 }
mjr 33:d832bcab089e 1824 else
mjr 33:d832bcab089e 1825 {
mjr 33:d832bcab089e 1826 // The latch didn't stick, so PSU2 was still off at
mjr 33:d832bcab089e 1827 // our last check. Try pulsing it again in case PSU2
mjr 33:d832bcab089e 1828 // was turned on since the last check.
mjr 35:e959ffba78fd 1829 psu2_status_set->write(1);
mjr 33:d832bcab089e 1830 psu2_state = 2;
mjr 33:d832bcab089e 1831 }
mjr 33:d832bcab089e 1832 break;
mjr 33:d832bcab089e 1833
mjr 33:d832bcab089e 1834 case 4:
mjr 33:d832bcab089e 1835 // TV timer countdown in progress. If we've reached the
mjr 33:d832bcab089e 1836 // delay time, pulse the relay.
mjr 35:e959ffba78fd 1837 if (tv_timer.read() >= tv_delay_time)
mjr 33:d832bcab089e 1838 {
mjr 33:d832bcab089e 1839 // turn on the relay for one timer interval
mjr 35:e959ffba78fd 1840 tv_relay->write(1);
mjr 33:d832bcab089e 1841 psu2_state = 5;
mjr 33:d832bcab089e 1842 }
mjr 33:d832bcab089e 1843 break;
mjr 33:d832bcab089e 1844
mjr 33:d832bcab089e 1845 case 5:
mjr 33:d832bcab089e 1846 // TV timer relay on. We pulse this for one interval, so
mjr 33:d832bcab089e 1847 // it's now time to turn it off and return to the default state.
mjr 35:e959ffba78fd 1848 tv_relay->write(0);
mjr 33:d832bcab089e 1849 psu2_state = 1;
mjr 33:d832bcab089e 1850 break;
mjr 33:d832bcab089e 1851 }
mjr 33:d832bcab089e 1852 }
mjr 33:d832bcab089e 1853
mjr 35:e959ffba78fd 1854 // Start the TV ON checker. If the status sense circuit is enabled in
mjr 35:e959ffba78fd 1855 // the configuration, we'll set up the pin connections and start the
mjr 35:e959ffba78fd 1856 // interrupt handler that periodically checks the status. Does nothing
mjr 35:e959ffba78fd 1857 // if any of the pins are configured as NC.
mjr 35:e959ffba78fd 1858 void startTVTimer(Config &cfg)
mjr 35:e959ffba78fd 1859 {
mjr 35:e959ffba78fd 1860 // only start the timer if the status sense circuit pins are configured
mjr 35:e959ffba78fd 1861 if (cfg.TVON.statusPin != NC && cfg.TVON.latchPin != NC && cfg.TVON.relayPin != NC)
mjr 35:e959ffba78fd 1862 {
mjr 35:e959ffba78fd 1863 psu2_status_sense = new DigitalIn(cfg.TVON.statusPin);
mjr 35:e959ffba78fd 1864 psu2_status_set = new DigitalOut(cfg.TVON.latchPin);
mjr 35:e959ffba78fd 1865 tv_relay = new DigitalOut(cfg.TVON.relayPin);
mjr 35:e959ffba78fd 1866 tv_delay_time = cfg.TVON.delayTime;
mjr 35:e959ffba78fd 1867
mjr 35:e959ffba78fd 1868 // Set up our time routine to run every 1/4 second.
mjr 35:e959ffba78fd 1869 tv_ticker.attach(&TVTimerInt, 0.25);
mjr 35:e959ffba78fd 1870 }
mjr 35:e959ffba78fd 1871 }
mjr 35:e959ffba78fd 1872
mjr 35:e959ffba78fd 1873 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 1874 //
mjr 35:e959ffba78fd 1875 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 1876 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 1877 //
mjr 35:e959ffba78fd 1878 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 1879 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 1880 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 1881 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 1882 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 1883 // again each time the firmware is updated.
mjr 35:e959ffba78fd 1884 //
mjr 35:e959ffba78fd 1885 NVM nvm;
mjr 35:e959ffba78fd 1886
mjr 35:e959ffba78fd 1887 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 1888 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 1889
mjr 35:e959ffba78fd 1890 // flash memory controller interface
mjr 35:e959ffba78fd 1891 FreescaleIAP iap;
mjr 35:e959ffba78fd 1892
mjr 35:e959ffba78fd 1893 // figure the flash address as a pointer along with the number of sectors
mjr 35:e959ffba78fd 1894 // required to store the structure
mjr 35:e959ffba78fd 1895 NVM *configFlashAddr(int &addr, int &numSectors)
mjr 35:e959ffba78fd 1896 {
mjr 35:e959ffba78fd 1897 // figure how many flash sectors we span, rounding up to whole sectors
mjr 35:e959ffba78fd 1898 numSectors = (sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE;
mjr 35:e959ffba78fd 1899
mjr 35:e959ffba78fd 1900 // figure the address - this is the highest flash address where the
mjr 35:e959ffba78fd 1901 // structure will fit with the start aligned on a sector boundary
mjr 35:e959ffba78fd 1902 addr = iap.flash_size() - (numSectors * SECTOR_SIZE);
mjr 35:e959ffba78fd 1903
mjr 35:e959ffba78fd 1904 // return the address as a pointer
mjr 35:e959ffba78fd 1905 return (NVM *)addr;
mjr 35:e959ffba78fd 1906 }
mjr 35:e959ffba78fd 1907
mjr 35:e959ffba78fd 1908 // figure the flash address as a pointer
mjr 35:e959ffba78fd 1909 NVM *configFlashAddr()
mjr 35:e959ffba78fd 1910 {
mjr 35:e959ffba78fd 1911 int addr, numSectors;
mjr 35:e959ffba78fd 1912 return configFlashAddr(addr, numSectors);
mjr 35:e959ffba78fd 1913 }
mjr 35:e959ffba78fd 1914
mjr 35:e959ffba78fd 1915 // Load the config from flash
mjr 35:e959ffba78fd 1916 void loadConfigFromFlash()
mjr 35:e959ffba78fd 1917 {
mjr 35:e959ffba78fd 1918 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 1919 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 1920 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 1921 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 1922 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 1923 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 1924 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 1925 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 1926 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 1927 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 1928 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 1929 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 1930 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 1931 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 1932 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 1933 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 1934
mjr 35:e959ffba78fd 1935 // Figure how many sectors we need for our structure
mjr 35:e959ffba78fd 1936 NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 1937
mjr 35:e959ffba78fd 1938 // if the flash is valid, load it; otherwise initialize to defaults
mjr 35:e959ffba78fd 1939 if (flash->valid())
mjr 35:e959ffba78fd 1940 {
mjr 35:e959ffba78fd 1941 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 1942 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 1943 }
mjr 35:e959ffba78fd 1944 else
mjr 35:e959ffba78fd 1945 {
mjr 35:e959ffba78fd 1946 // flash is invalid - load factory settings nito RAM structure
mjr 35:e959ffba78fd 1947 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 1948 }
mjr 35:e959ffba78fd 1949 }
mjr 35:e959ffba78fd 1950
mjr 35:e959ffba78fd 1951 void saveConfigToFlash()
mjr 33:d832bcab089e 1952 {
mjr 35:e959ffba78fd 1953 int addr, sectors;
mjr 35:e959ffba78fd 1954 configFlashAddr(addr, sectors);
mjr 35:e959ffba78fd 1955 nvm.save(iap, addr);
mjr 35:e959ffba78fd 1956 }
mjr 35:e959ffba78fd 1957
mjr 35:e959ffba78fd 1958 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 1959 //
mjr 38:091e511ce8a0 1960 // NIGHT MODE flag. When night mode is on, we disable all outputs
mjr 38:091e511ce8a0 1961 // marked as "noisemakers" in the output configuration flags.
mjr 38:091e511ce8a0 1962 int nightMode;
mjr 38:091e511ce8a0 1963
mjr 38:091e511ce8a0 1964 // Update the global output mode settings
mjr 38:091e511ce8a0 1965 static void globalOutputModeChange()
mjr 38:091e511ce8a0 1966 {
mjr 38:091e511ce8a0 1967 // set the global modeLevel[]
mjr 38:091e511ce8a0 1968 for (int i = 0 ; i < numOutputs ; ++i)
mjr 38:091e511ce8a0 1969 {
mjr 38:091e511ce8a0 1970 // assume the port will be on
mjr 38:091e511ce8a0 1971 uint8_t f = 1;
mjr 38:091e511ce8a0 1972
mjr 38:091e511ce8a0 1973 // if night mode is in effect, and this is a noisemaker, disable it
mjr 38:091e511ce8a0 1974 if (nightMode && (cfg.outPort[i].flags & PortFlagNoisemaker) != 0)
mjr 38:091e511ce8a0 1975 f = 0;
mjr 38:091e511ce8a0 1976
mjr 38:091e511ce8a0 1977 // set the final output port override value
mjr 38:091e511ce8a0 1978 modeLevel[i] = f;
mjr 38:091e511ce8a0 1979 }
mjr 38:091e511ce8a0 1980
mjr 38:091e511ce8a0 1981 // update all outputs for the mode change
mjr 38:091e511ce8a0 1982 updateAllOuts();
mjr 38:091e511ce8a0 1983 }
mjr 38:091e511ce8a0 1984
mjr 38:091e511ce8a0 1985 // Turn night mode on or off
mjr 38:091e511ce8a0 1986 static void setNightMode(bool on)
mjr 38:091e511ce8a0 1987 {
mjr 38:091e511ce8a0 1988 nightMode = on;
mjr 38:091e511ce8a0 1989 globalOutputModeChange();
mjr 38:091e511ce8a0 1990 specialPin[0]->set(on ? 255.0 : 0.0);
mjr 38:091e511ce8a0 1991 }
mjr 38:091e511ce8a0 1992
mjr 38:091e511ce8a0 1993 // Toggle night mode
mjr 38:091e511ce8a0 1994 static void toggleNightMode()
mjr 38:091e511ce8a0 1995 {
mjr 38:091e511ce8a0 1996 setNightMode(!nightMode);
mjr 38:091e511ce8a0 1997 }
mjr 38:091e511ce8a0 1998
mjr 38:091e511ce8a0 1999
mjr 38:091e511ce8a0 2000 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 2001 //
mjr 35:e959ffba78fd 2002 // Plunger Sensor
mjr 35:e959ffba78fd 2003 //
mjr 35:e959ffba78fd 2004
mjr 35:e959ffba78fd 2005 // the plunger sensor interface object
mjr 35:e959ffba78fd 2006 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 2007
mjr 35:e959ffba78fd 2008 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 2009 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 2010 void createPlunger()
mjr 35:e959ffba78fd 2011 {
mjr 35:e959ffba78fd 2012 // delete any existing sensor object
mjr 35:e959ffba78fd 2013 if (plungerSensor != 0)
mjr 35:e959ffba78fd 2014 delete plungerSensor;
mjr 35:e959ffba78fd 2015
mjr 35:e959ffba78fd 2016 // create the new sensor object according to the type
mjr 35:e959ffba78fd 2017 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 2018 {
mjr 35:e959ffba78fd 2019 case PlungerType_TSL1410RS:
mjr 35:e959ffba78fd 2020 // pins are: SI, CLOCK, AO
mjr 35:e959ffba78fd 2021 plungerSensor = new PlungerSensorTSL1410R(cfg.plunger.sensorPin[0], cfg.plunger.sensorPin[1], cfg.plunger.sensorPin[2], NC);
mjr 35:e959ffba78fd 2022 break;
mjr 35:e959ffba78fd 2023
mjr 35:e959ffba78fd 2024 case PlungerType_TSL1410RP:
mjr 35:e959ffba78fd 2025 // pins are: SI, CLOCK, AO1, AO2
mjr 35:e959ffba78fd 2026 plungerSensor = new PlungerSensorTSL1410R(cfg.plunger.sensorPin[0], cfg.plunger.sensorPin[1], cfg.plunger.sensorPin[2], cfg.plunger.sensorPin[3]);
mjr 35:e959ffba78fd 2027 break;
mjr 35:e959ffba78fd 2028
mjr 35:e959ffba78fd 2029 case PlungerType_TSL1412RS:
mjr 35:e959ffba78fd 2030 // pins are: SI, CLOCK, AO1, AO2
mjr 35:e959ffba78fd 2031 plungerSensor = new PlungerSensorTSL1412R(cfg.plunger.sensorPin[0], cfg.plunger.sensorPin[1], cfg.plunger.sensorPin[2], NC);
mjr 35:e959ffba78fd 2032 break;
mjr 35:e959ffba78fd 2033
mjr 35:e959ffba78fd 2034 case PlungerType_TSL1412RP:
mjr 35:e959ffba78fd 2035 // pins are: SI, CLOCK, AO1, AO2
mjr 35:e959ffba78fd 2036 plungerSensor = new PlungerSensorTSL1412R(cfg.plunger.sensorPin[0], cfg.plunger.sensorPin[1], cfg.plunger.sensorPin[2], cfg.plunger.sensorPin[3]);
mjr 35:e959ffba78fd 2037 break;
mjr 35:e959ffba78fd 2038
mjr 35:e959ffba78fd 2039 case PlungerType_Pot:
mjr 35:e959ffba78fd 2040 // pins are: AO
mjr 35:e959ffba78fd 2041 plungerSensor = new PlungerSensorPot(cfg.plunger.sensorPin[0]);
mjr 35:e959ffba78fd 2042 break;
mjr 35:e959ffba78fd 2043
mjr 35:e959ffba78fd 2044 case PlungerType_None:
mjr 35:e959ffba78fd 2045 default:
mjr 35:e959ffba78fd 2046 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 2047 break;
mjr 35:e959ffba78fd 2048 }
mjr 33:d832bcab089e 2049 }
mjr 33:d832bcab089e 2050
mjr 35:e959ffba78fd 2051 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2052 //
mjr 35:e959ffba78fd 2053 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 2054 //
mjr 35:e959ffba78fd 2055 void reboot(USBJoystick &js)
mjr 35:e959ffba78fd 2056 {
mjr 35:e959ffba78fd 2057 // disconnect from USB
mjr 35:e959ffba78fd 2058 js.disconnect();
mjr 35:e959ffba78fd 2059
mjr 35:e959ffba78fd 2060 // wait a few seconds to make sure the host notices the disconnect
mjr 35:e959ffba78fd 2061 wait(5);
mjr 35:e959ffba78fd 2062
mjr 35:e959ffba78fd 2063 // reset the device
mjr 35:e959ffba78fd 2064 NVIC_SystemReset();
mjr 35:e959ffba78fd 2065 while (true) { }
mjr 35:e959ffba78fd 2066 }
mjr 35:e959ffba78fd 2067
mjr 35:e959ffba78fd 2068 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2069 //
mjr 35:e959ffba78fd 2070 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 2071 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 2072 //
mjr 35:e959ffba78fd 2073 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 2074 {
mjr 35:e959ffba78fd 2075 int tmp;
mjr 35:e959ffba78fd 2076 switch (cfg.orientation)
mjr 35:e959ffba78fd 2077 {
mjr 35:e959ffba78fd 2078 case OrientationFront:
mjr 35:e959ffba78fd 2079 tmp = x;
mjr 35:e959ffba78fd 2080 x = y;
mjr 35:e959ffba78fd 2081 y = tmp;
mjr 35:e959ffba78fd 2082 break;
mjr 35:e959ffba78fd 2083
mjr 35:e959ffba78fd 2084 case OrientationLeft:
mjr 35:e959ffba78fd 2085 x = -x;
mjr 35:e959ffba78fd 2086 break;
mjr 35:e959ffba78fd 2087
mjr 35:e959ffba78fd 2088 case OrientationRight:
mjr 35:e959ffba78fd 2089 y = -y;
mjr 35:e959ffba78fd 2090 break;
mjr 35:e959ffba78fd 2091
mjr 35:e959ffba78fd 2092 case OrientationRear:
mjr 35:e959ffba78fd 2093 tmp = -x;
mjr 35:e959ffba78fd 2094 x = -y;
mjr 35:e959ffba78fd 2095 y = tmp;
mjr 35:e959ffba78fd 2096 break;
mjr 35:e959ffba78fd 2097 }
mjr 35:e959ffba78fd 2098 }
mjr 35:e959ffba78fd 2099
mjr 35:e959ffba78fd 2100 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2101 //
mjr 35:e959ffba78fd 2102 // Device status. We report this on each update so that the host config
mjr 35:e959ffba78fd 2103 // tool can detect our current settings. This is a bit mask consisting
mjr 35:e959ffba78fd 2104 // of these bits:
mjr 35:e959ffba78fd 2105 // 0x0001 -> plunger sensor enabled
mjr 35:e959ffba78fd 2106 // 0x8000 -> RESERVED - must always be zero
mjr 35:e959ffba78fd 2107 //
mjr 35:e959ffba78fd 2108 // Note that the high bit (0x8000) must always be 0, since we use that
mjr 35:e959ffba78fd 2109 // to distinguish special request reply packets.
mjr 35:e959ffba78fd 2110 uint16_t statusFlags;
mjr 35:e959ffba78fd 2111
mjr 35:e959ffba78fd 2112 // flag: send a pixel dump after the next read
mjr 35:e959ffba78fd 2113 bool reportPix = false;
mjr 35:e959ffba78fd 2114
mjr 33:d832bcab089e 2115
mjr 35:e959ffba78fd 2116 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2117 //
mjr 35:e959ffba78fd 2118 // Calibration button state:
mjr 35:e959ffba78fd 2119 // 0 = not pushed
mjr 35:e959ffba78fd 2120 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 2121 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 2122 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 2123 int calBtnState = 0;
mjr 35:e959ffba78fd 2124
mjr 35:e959ffba78fd 2125 // calibration button debounce timer
mjr 35:e959ffba78fd 2126 Timer calBtnTimer;
mjr 35:e959ffba78fd 2127
mjr 35:e959ffba78fd 2128 // calibration button light state
mjr 35:e959ffba78fd 2129 int calBtnLit = false;
mjr 35:e959ffba78fd 2130
mjr 35:e959ffba78fd 2131
mjr 35:e959ffba78fd 2132 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2133 //
mjr 35:e959ffba78fd 2134 // Handle a configuration variable update. 'data' is the USB message we
mjr 35:e959ffba78fd 2135 // received from the host.
mjr 35:e959ffba78fd 2136 //
mjr 35:e959ffba78fd 2137 void configVarMsg(uint8_t *data)
mjr 35:e959ffba78fd 2138 {
mjr 35:e959ffba78fd 2139 switch (data[1])
mjr 35:e959ffba78fd 2140 {
mjr 35:e959ffba78fd 2141 case 1:
mjr 35:e959ffba78fd 2142 // USB identification (Vendor ID, Product ID)
mjr 35:e959ffba78fd 2143 cfg.usbVendorID = wireUI16(data+2);
mjr 35:e959ffba78fd 2144 cfg.usbProductID = wireUI16(data+4);
mjr 35:e959ffba78fd 2145 break;
mjr 35:e959ffba78fd 2146
mjr 35:e959ffba78fd 2147 case 2:
mjr 35:e959ffba78fd 2148 // Pinscape Controller unit number - note that data[2] contains
mjr 35:e959ffba78fd 2149 // the nominal unit number, 1-16
mjr 35:e959ffba78fd 2150 if (data[2] >= 1 && data[2] <= 16)
mjr 35:e959ffba78fd 2151 cfg.psUnitNo = data[2];
mjr 35:e959ffba78fd 2152 break;
mjr 35:e959ffba78fd 2153
mjr 35:e959ffba78fd 2154 case 3:
mjr 35:e959ffba78fd 2155 // Enable/disable joystick
mjr 35:e959ffba78fd 2156 cfg.joystickEnabled = data[2];
mjr 35:e959ffba78fd 2157 break;
mjr 35:e959ffba78fd 2158
mjr 35:e959ffba78fd 2159 case 4:
mjr 35:e959ffba78fd 2160 // Accelerometer orientation
mjr 35:e959ffba78fd 2161 cfg.orientation = data[2];
mjr 35:e959ffba78fd 2162 break;
mjr 35:e959ffba78fd 2163
mjr 35:e959ffba78fd 2164 case 5:
mjr 35:e959ffba78fd 2165 // Plunger sensor type
mjr 35:e959ffba78fd 2166 cfg.plunger.sensorType = data[2];
mjr 35:e959ffba78fd 2167 break;
mjr 35:e959ffba78fd 2168
mjr 35:e959ffba78fd 2169 case 6:
mjr 35:e959ffba78fd 2170 // Set plunger pin assignments
mjr 35:e959ffba78fd 2171 cfg.plunger.sensorPin[0] = wirePinName(data[2]);
mjr 35:e959ffba78fd 2172 cfg.plunger.sensorPin[1] = wirePinName(data[3]);
mjr 35:e959ffba78fd 2173 cfg.plunger.sensorPin[2] = wirePinName(data[4]);
mjr 35:e959ffba78fd 2174 cfg.plunger.sensorPin[3] = wirePinName(data[5]);
mjr 35:e959ffba78fd 2175 break;
mjr 35:e959ffba78fd 2176
mjr 35:e959ffba78fd 2177 case 7:
mjr 35:e959ffba78fd 2178 // Plunger calibration button and indicator light pin assignments
mjr 35:e959ffba78fd 2179 cfg.plunger.cal.btn = wirePinName(data[2]);
mjr 35:e959ffba78fd 2180 cfg.plunger.cal.led = wirePinName(data[3]);
mjr 35:e959ffba78fd 2181 break;
mjr 35:e959ffba78fd 2182
mjr 35:e959ffba78fd 2183 case 8:
mjr 35:e959ffba78fd 2184 // ZB Launch Ball setup
mjr 35:e959ffba78fd 2185 cfg.plunger.zbLaunchBall.port = (int)(unsigned char)data[2];
mjr 35:e959ffba78fd 2186 cfg.plunger.zbLaunchBall.btn = (int)(unsigned char)data[3];
mjr 35:e959ffba78fd 2187 cfg.plunger.zbLaunchBall.pushDistance = (float)wireUI16(data+4) / 1000.0;
mjr 35:e959ffba78fd 2188 break;
mjr 35:e959ffba78fd 2189
mjr 35:e959ffba78fd 2190 case 9:
mjr 35:e959ffba78fd 2191 // TV ON setup
mjr 35:e959ffba78fd 2192 cfg.TVON.statusPin = wirePinName(data[2]);
mjr 35:e959ffba78fd 2193 cfg.TVON.latchPin = wirePinName(data[3]);
mjr 35:e959ffba78fd 2194 cfg.TVON.relayPin = wirePinName(data[4]);
mjr 35:e959ffba78fd 2195 cfg.TVON.delayTime = (float)wireUI16(data+5) / 100.0;
mjr 35:e959ffba78fd 2196 break;
mjr 35:e959ffba78fd 2197
mjr 35:e959ffba78fd 2198 case 10:
mjr 35:e959ffba78fd 2199 // TLC5940NT PWM controller chip setup
mjr 35:e959ffba78fd 2200 cfg.tlc5940.nchips = (int)(unsigned char)data[2];
mjr 35:e959ffba78fd 2201 cfg.tlc5940.sin = wirePinName(data[3]);
mjr 35:e959ffba78fd 2202 cfg.tlc5940.sclk = wirePinName(data[4]);
mjr 35:e959ffba78fd 2203 cfg.tlc5940.xlat = wirePinName(data[5]);
mjr 35:e959ffba78fd 2204 cfg.tlc5940.blank = wirePinName(data[6]);
mjr 35:e959ffba78fd 2205 cfg.tlc5940.gsclk = wirePinName(data[7]);
mjr 35:e959ffba78fd 2206 break;
mjr 35:e959ffba78fd 2207
mjr 35:e959ffba78fd 2208 case 11:
mjr 35:e959ffba78fd 2209 // 74HC595 shift register chip setup
mjr 35:e959ffba78fd 2210 cfg.hc595.nchips = (int)(unsigned char)data[2];
mjr 35:e959ffba78fd 2211 cfg.hc595.sin = wirePinName(data[3]);
mjr 35:e959ffba78fd 2212 cfg.hc595.sclk = wirePinName(data[4]);
mjr 35:e959ffba78fd 2213 cfg.hc595.latch = wirePinName(data[5]);
mjr 35:e959ffba78fd 2214 cfg.hc595.ena = wirePinName(data[6]);
mjr 35:e959ffba78fd 2215 break;
mjr 35:e959ffba78fd 2216
mjr 35:e959ffba78fd 2217 case 12:
mjr 35:e959ffba78fd 2218 // button setup
mjr 35:e959ffba78fd 2219 {
mjr 35:e959ffba78fd 2220 // get the button number
mjr 35:e959ffba78fd 2221 int idx = data[2];
mjr 35:e959ffba78fd 2222
mjr 35:e959ffba78fd 2223 // if it's in range, set the button data
mjr 35:e959ffba78fd 2224 if (idx > 0 && idx <= MAX_BUTTONS)
mjr 35:e959ffba78fd 2225 {
mjr 35:e959ffba78fd 2226 // adjust to an array index
mjr 35:e959ffba78fd 2227 --idx;
mjr 35:e959ffba78fd 2228
mjr 35:e959ffba78fd 2229 // set the values
mjr 35:e959ffba78fd 2230 cfg.button[idx].pin = data[3];
mjr 35:e959ffba78fd 2231 cfg.button[idx].typ = data[4];
mjr 35:e959ffba78fd 2232 cfg.button[idx].val = data[5];
mjr 38:091e511ce8a0 2233 cfg.button[idx].flags = data[6];
mjr 35:e959ffba78fd 2234 }
mjr 35:e959ffba78fd 2235 }
mjr 35:e959ffba78fd 2236 break;
mjr 35:e959ffba78fd 2237
mjr 35:e959ffba78fd 2238 case 13:
mjr 35:e959ffba78fd 2239 // LedWiz output port setup
mjr 35:e959ffba78fd 2240 {
mjr 35:e959ffba78fd 2241 // get the port number
mjr 35:e959ffba78fd 2242 int idx = data[2];
mjr 35:e959ffba78fd 2243
mjr 35:e959ffba78fd 2244 // if it's in range, set the port data
mjr 35:e959ffba78fd 2245 if (idx > 0 && idx <= MAX_OUT_PORTS)
mjr 35:e959ffba78fd 2246 {
mjr 35:e959ffba78fd 2247 // adjust to an array index
mjr 35:e959ffba78fd 2248 --idx;
mjr 35:e959ffba78fd 2249
mjr 35:e959ffba78fd 2250 // set the values
mjr 35:e959ffba78fd 2251 cfg.outPort[idx].typ = data[3];
mjr 35:e959ffba78fd 2252 cfg.outPort[idx].pin = data[4];
mjr 35:e959ffba78fd 2253 cfg.outPort[idx].flags = data[5];
mjr 35:e959ffba78fd 2254 }
mjr 38:091e511ce8a0 2255 else if (idx == 254)
mjr 38:091e511ce8a0 2256 {
mjr 38:091e511ce8a0 2257 // special ports
mjr 38:091e511ce8a0 2258 idx -= 254;
mjr 38:091e511ce8a0 2259 cfg.specialPort[idx].typ = data[3];
mjr 38:091e511ce8a0 2260 cfg.specialPort[idx].pin = data[4];
mjr 38:091e511ce8a0 2261 cfg.specialPort[idx].flags = data[5];
mjr 38:091e511ce8a0 2262 }
mjr 35:e959ffba78fd 2263 }
mjr 35:e959ffba78fd 2264 break;
mjr 38:091e511ce8a0 2265
mjr 38:091e511ce8a0 2266 case 14:
mjr 38:091e511ce8a0 2267 // engage/cancel Night Mode
mjr 38:091e511ce8a0 2268 setNightMode(data[2]);
mjr 38:091e511ce8a0 2269 break;
mjr 35:e959ffba78fd 2270 }
mjr 35:e959ffba78fd 2271 }
mjr 35:e959ffba78fd 2272
mjr 35:e959ffba78fd 2273 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2274 //
mjr 35:e959ffba78fd 2275 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 2276 // LedWiz protocol.
mjr 33:d832bcab089e 2277 //
mjr 39:b3815a1c3802 2278 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js, int &z)
mjr 35:e959ffba78fd 2279 {
mjr 38:091e511ce8a0 2280 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 2281 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 2282 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 2283 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 2284 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 2285 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 2286 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 2287 // So our full protocol is as follows:
mjr 38:091e511ce8a0 2288 //
mjr 38:091e511ce8a0 2289 // first byte =
mjr 38:091e511ce8a0 2290 // 0-48 -> LWZ-PBA
mjr 38:091e511ce8a0 2291 // 64 -> LWZ SBA
mjr 38:091e511ce8a0 2292 // 65 -> private control message; second byte specifies subtype
mjr 38:091e511ce8a0 2293 // 129-132 -> LWZ-PBA
mjr 38:091e511ce8a0 2294 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 2295 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 2296 // other -> reserved for future use
mjr 38:091e511ce8a0 2297 //
mjr 39:b3815a1c3802 2298 uint8_t *data = lwm.data;
mjr 38:091e511ce8a0 2299 if (data[0] == 64)
mjr 35:e959ffba78fd 2300 {
mjr 38:091e511ce8a0 2301 // LWZ-SBA - first four bytes are bit-packed on/off flags
mjr 38:091e511ce8a0 2302 // for the outputs; 5th byte is the pulse speed (1-7)
mjr 38:091e511ce8a0 2303 //printf("LWZ-SBA %02x %02x %02x %02x ; %02x\r\n",
mjr 38:091e511ce8a0 2304 // data[1], data[2], data[3], data[4], data[5]);
mjr 38:091e511ce8a0 2305
mjr 38:091e511ce8a0 2306 // update all on/off states
mjr 38:091e511ce8a0 2307 for (int i = 0, bit = 1, ri = 1 ; i < numLwOutputs ; ++i, bit <<= 1)
mjr 35:e959ffba78fd 2308 {
mjr 38:091e511ce8a0 2309 // figure the on/off state bit for this output
mjr 38:091e511ce8a0 2310 if (bit == 0x100) {
mjr 38:091e511ce8a0 2311 bit = 1;
mjr 38:091e511ce8a0 2312 ++ri;
mjr 35:e959ffba78fd 2313 }
mjr 35:e959ffba78fd 2314
mjr 38:091e511ce8a0 2315 // set the on/off state
mjr 38:091e511ce8a0 2316 wizOn[i] = ((data[ri] & bit) != 0);
mjr 38:091e511ce8a0 2317
mjr 38:091e511ce8a0 2318 // If the wizVal setting is 255, it means that this
mjr 38:091e511ce8a0 2319 // output was last set to a brightness value with the
mjr 38:091e511ce8a0 2320 // extended protocol. Return it to LedWiz control by
mjr 38:091e511ce8a0 2321 // rescaling the brightness setting to the LedWiz range
mjr 38:091e511ce8a0 2322 // and updating wizVal with the result. If it's any
mjr 38:091e511ce8a0 2323 // other value, it was previously set by a PBA message,
mjr 38:091e511ce8a0 2324 // so simply retain the last setting - in the normal
mjr 38:091e511ce8a0 2325 // LedWiz protocol, the "profile" (brightness) and on/off
mjr 38:091e511ce8a0 2326 // states are independent, so an SBA just turns an output
mjr 38:091e511ce8a0 2327 // on or off but retains its last brightness level.
mjr 38:091e511ce8a0 2328 if (wizVal[i] == 255)
mjr 38:091e511ce8a0 2329 wizVal[i] = (uint8_t)round(outLevel[i]*48);
mjr 38:091e511ce8a0 2330 }
mjr 38:091e511ce8a0 2331
mjr 38:091e511ce8a0 2332 // set the flash speed - enforce the value range 1-7
mjr 38:091e511ce8a0 2333 wizSpeed = data[5];
mjr 38:091e511ce8a0 2334 if (wizSpeed < 1)
mjr 38:091e511ce8a0 2335 wizSpeed = 1;
mjr 38:091e511ce8a0 2336 else if (wizSpeed > 7)
mjr 38:091e511ce8a0 2337 wizSpeed = 7;
mjr 38:091e511ce8a0 2338
mjr 38:091e511ce8a0 2339 // update the physical outputs
mjr 38:091e511ce8a0 2340 updateWizOuts();
mjr 38:091e511ce8a0 2341 if (hc595 != 0)
mjr 38:091e511ce8a0 2342 hc595->update();
mjr 38:091e511ce8a0 2343
mjr 38:091e511ce8a0 2344 // reset the PBA counter
mjr 38:091e511ce8a0 2345 pbaIdx = 0;
mjr 38:091e511ce8a0 2346 }
mjr 38:091e511ce8a0 2347 else if (data[0] == 65)
mjr 38:091e511ce8a0 2348 {
mjr 38:091e511ce8a0 2349 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 2350 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 2351 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 2352 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 2353 // message type.
mjr 39:b3815a1c3802 2354 switch (data[1])
mjr 38:091e511ce8a0 2355 {
mjr 39:b3815a1c3802 2356 case 0:
mjr 39:b3815a1c3802 2357 // No Op
mjr 39:b3815a1c3802 2358 break;
mjr 39:b3815a1c3802 2359
mjr 39:b3815a1c3802 2360 case 1:
mjr 38:091e511ce8a0 2361 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 2362 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 2363 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 2364 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 2365 {
mjr 39:b3815a1c3802 2366
mjr 39:b3815a1c3802 2367 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 2368 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 2369 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 2370
mjr 39:b3815a1c3802 2371 // we'll need a reset if the LedWiz unit number is changing
mjr 39:b3815a1c3802 2372 bool needReset = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 2373
mjr 39:b3815a1c3802 2374 // set the configuration parameters from the message
mjr 39:b3815a1c3802 2375 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 2376 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 2377
mjr 39:b3815a1c3802 2378 // update the status flags
mjr 39:b3815a1c3802 2379 statusFlags = (statusFlags & ~0x01) | (data[3] & 0x01);
mjr 39:b3815a1c3802 2380
mjr 39:b3815a1c3802 2381 // if the plunger is no longer enabled, use 0 for z reports
mjr 39:b3815a1c3802 2382 if (!cfg.plunger.enabled)
mjr 39:b3815a1c3802 2383 z = 0;
mjr 39:b3815a1c3802 2384
mjr 39:b3815a1c3802 2385 // save the configuration
mjr 39:b3815a1c3802 2386 saveConfigToFlash();
mjr 39:b3815a1c3802 2387
mjr 39:b3815a1c3802 2388 // reboot if necessary
mjr 39:b3815a1c3802 2389 if (needReset)
mjr 39:b3815a1c3802 2390 reboot(js);
mjr 39:b3815a1c3802 2391 }
mjr 39:b3815a1c3802 2392 break;
mjr 38:091e511ce8a0 2393
mjr 39:b3815a1c3802 2394 case 2:
mjr 38:091e511ce8a0 2395 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 2396 // (No parameters)
mjr 38:091e511ce8a0 2397
mjr 38:091e511ce8a0 2398 // enter calibration mode
mjr 38:091e511ce8a0 2399 calBtnState = 3;
mjr 38:091e511ce8a0 2400 calBtnTimer.reset();
mjr 38:091e511ce8a0 2401 cfg.plunger.cal.reset(plungerSensor->npix);
mjr 39:b3815a1c3802 2402 break;
mjr 39:b3815a1c3802 2403
mjr 39:b3815a1c3802 2404 case 3:
mjr 38:091e511ce8a0 2405 // 3 = pixel dump
mjr 38:091e511ce8a0 2406 // (No parameters)
mjr 38:091e511ce8a0 2407 reportPix = true;
mjr 38:091e511ce8a0 2408
mjr 38:091e511ce8a0 2409 // show purple until we finish sending the report
mjr 38:091e511ce8a0 2410 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 2411 break;
mjr 39:b3815a1c3802 2412
mjr 39:b3815a1c3802 2413 case 4:
mjr 38:091e511ce8a0 2414 // 4 = hardware configuration query
mjr 38:091e511ce8a0 2415 // (No parameters)
mjr 38:091e511ce8a0 2416 js.reportConfig(
mjr 38:091e511ce8a0 2417 numOutputs,
mjr 38:091e511ce8a0 2418 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 38:091e511ce8a0 2419 cfg.plunger.cal.zero, cfg.plunger.cal.max);
mjr 39:b3815a1c3802 2420 break;
mjr 39:b3815a1c3802 2421
mjr 39:b3815a1c3802 2422 case 5:
mjr 38:091e511ce8a0 2423 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 2424 allOutputsOff();
mjr 39:b3815a1c3802 2425 break;
mjr 39:b3815a1c3802 2426
mjr 39:b3815a1c3802 2427 case 6:
mjr 38:091e511ce8a0 2428 // 6 = Save configuration to flash.
mjr 38:091e511ce8a0 2429 saveConfigToFlash();
mjr 38:091e511ce8a0 2430
mjr 38:091e511ce8a0 2431 // Reboot the microcontroller. Nearly all config changes
mjr 38:091e511ce8a0 2432 // require a reset, and a reset only takes a few seconds,
mjr 38:091e511ce8a0 2433 // so we don't bother tracking whether or not a reboot is
mjr 38:091e511ce8a0 2434 // really needed.
mjr 38:091e511ce8a0 2435 reboot(js);
mjr 39:b3815a1c3802 2436 break;
mjr 38:091e511ce8a0 2437 }
mjr 38:091e511ce8a0 2438 }
mjr 38:091e511ce8a0 2439 else if (data[0] == 66)
mjr 38:091e511ce8a0 2440 {
mjr 38:091e511ce8a0 2441 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 2442 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 2443 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 2444 // in a variable-dependent format.
mjr 38:091e511ce8a0 2445 configVarMsg(data);
mjr 38:091e511ce8a0 2446 }
mjr 38:091e511ce8a0 2447 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 2448 {
mjr 38:091e511ce8a0 2449 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 2450 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 2451 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 2452 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 2453 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 2454 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 2455 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 2456 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 2457 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 2458 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 2459 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 2460 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 2461 //
mjr 38:091e511ce8a0 2462 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 2463 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 2464 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 2465 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 2466 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 2467 // address those ports anyway.
mjr 38:091e511ce8a0 2468 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 2469 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 38:091e511ce8a0 2470 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 2471 {
mjr 38:091e511ce8a0 2472 // set the brightness level for the output
mjr 38:091e511ce8a0 2473 float b = data[i-i0+1]/255.0;
mjr 38:091e511ce8a0 2474 outLevel[i] = b;
mjr 38:091e511ce8a0 2475
mjr 38:091e511ce8a0 2476 // if it's in the basic LedWiz output set, set the LedWiz
mjr 38:091e511ce8a0 2477 // profile value to 255, which means "use outLevel"
mjr 38:091e511ce8a0 2478 if (i < 32)
mjr 38:091e511ce8a0 2479 wizVal[i] = 255;
mjr 38:091e511ce8a0 2480
mjr 38:091e511ce8a0 2481 // set the output
mjr 38:091e511ce8a0 2482 lwPin[i]->set(b * modeLevel[i]);
mjr 38:091e511ce8a0 2483 }
mjr 38:091e511ce8a0 2484
mjr 38:091e511ce8a0 2485 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 2486 if (hc595 != 0)
mjr 38:091e511ce8a0 2487 hc595->update();
mjr 38:091e511ce8a0 2488 }
mjr 38:091e511ce8a0 2489 else
mjr 38:091e511ce8a0 2490 {
mjr 38:091e511ce8a0 2491 // Everything else is LWZ-PBA. This is a full "profile"
mjr 38:091e511ce8a0 2492 // dump from the host for one bank of 8 outputs. Each
mjr 38:091e511ce8a0 2493 // byte sets one output in the current bank. The current
mjr 38:091e511ce8a0 2494 // bank is implied; the bank starts at 0 and is reset to 0
mjr 38:091e511ce8a0 2495 // by any LWZ-SBA message, and is incremented to the next
mjr 38:091e511ce8a0 2496 // bank by each LWZ-PBA message. Our variable pbaIdx keeps
mjr 38:091e511ce8a0 2497 // track of our notion of the current bank. There's no direct
mjr 38:091e511ce8a0 2498 // way for the host to select the bank; it just has to count
mjr 38:091e511ce8a0 2499 // on us staying in sync. In practice, the host will always
mjr 38:091e511ce8a0 2500 // send a full set of 4 PBA messages in a row to set all 32
mjr 38:091e511ce8a0 2501 // outputs.
mjr 38:091e511ce8a0 2502 //
mjr 38:091e511ce8a0 2503 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 2504 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 2505 // wizVal[] entry for each output, and that takes precedence
mjr 38:091e511ce8a0 2506 // over the extended protocol settings.
mjr 38:091e511ce8a0 2507 //
mjr 38:091e511ce8a0 2508 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 2509 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 2510
mjr 38:091e511ce8a0 2511 // Update all output profile settings
mjr 38:091e511ce8a0 2512 for (int i = 0 ; i < 8 ; ++i)
mjr 38:091e511ce8a0 2513 wizVal[pbaIdx + i] = data[i];
mjr 38:091e511ce8a0 2514
mjr 38:091e511ce8a0 2515 // Update the physical LED state if this is the last bank.
mjr 38:091e511ce8a0 2516 // Note that hosts always send a full set of four PBA
mjr 38:091e511ce8a0 2517 // messages, so there's no need to do a physical update
mjr 38:091e511ce8a0 2518 // until we've received the last bank's PBA message.
mjr 38:091e511ce8a0 2519 if (pbaIdx == 24)
mjr 38:091e511ce8a0 2520 {
mjr 35:e959ffba78fd 2521 updateWizOuts();
mjr 35:e959ffba78fd 2522 if (hc595 != 0)
mjr 35:e959ffba78fd 2523 hc595->update();
mjr 35:e959ffba78fd 2524 pbaIdx = 0;
mjr 35:e959ffba78fd 2525 }
mjr 38:091e511ce8a0 2526 else
mjr 38:091e511ce8a0 2527 pbaIdx += 8;
mjr 38:091e511ce8a0 2528 }
mjr 38:091e511ce8a0 2529 }
mjr 35:e959ffba78fd 2530
mjr 33:d832bcab089e 2531
mjr 38:091e511ce8a0 2532 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 2533 //
mjr 38:091e511ce8a0 2534 // Pre-connection diagnostic flasher
mjr 38:091e511ce8a0 2535 //
mjr 38:091e511ce8a0 2536 void preConnectFlasher()
mjr 38:091e511ce8a0 2537 {
mjr 38:091e511ce8a0 2538 diagLED(1, 0, 0);
mjr 38:091e511ce8a0 2539 wait(0.05);
mjr 38:091e511ce8a0 2540 diagLED(0, 0, 0);
mjr 35:e959ffba78fd 2541 }
mjr 17:ab3cec0c8bf4 2542
mjr 17:ab3cec0c8bf4 2543 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 2544 //
mjr 5:a70c0bce770d 2545 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 2546 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 2547 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 2548 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 2549 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 2550 // port outputs.
mjr 5:a70c0bce770d 2551 //
mjr 0:5acbbe3f4cf4 2552 int main(void)
mjr 0:5acbbe3f4cf4 2553 {
mjr 39:b3815a1c3802 2554 printf("\r\nPinscape Controller starting\r\n");
mjr 39:b3815a1c3802 2555 // 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 2556
mjr 39:b3815a1c3802 2557 // clear the I2C bus (for the accelerometer)
mjr 35:e959ffba78fd 2558 clear_i2c();
mjr 38:091e511ce8a0 2559
mjr 35:e959ffba78fd 2560 // load the saved configuration
mjr 35:e959ffba78fd 2561 loadConfigFromFlash();
mjr 35:e959ffba78fd 2562
mjr 38:091e511ce8a0 2563 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 2564 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 2565
mjr 38:091e511ce8a0 2566 // set up the pre-connected ticker
mjr 38:091e511ce8a0 2567 Ticker preConnectTicker;
mjr 38:091e511ce8a0 2568 preConnectTicker.attach(preConnectFlasher, 3);
mjr 38:091e511ce8a0 2569
mjr 33:d832bcab089e 2570 // start the TV timer, if applicable
mjr 35:e959ffba78fd 2571 startTVTimer(cfg);
mjr 33:d832bcab089e 2572
mjr 33:d832bcab089e 2573 // we're not connected/awake yet
mjr 33:d832bcab089e 2574 bool connected = false;
mjr 33:d832bcab089e 2575 time_t connectChangeTime = time(0);
mjr 33:d832bcab089e 2576
mjr 35:e959ffba78fd 2577 // create the plunger sensor interface
mjr 35:e959ffba78fd 2578 createPlunger();
mjr 33:d832bcab089e 2579
mjr 35:e959ffba78fd 2580 // set up the TLC5940 interface and start the TLC5940 clock, if applicable
mjr 35:e959ffba78fd 2581 init_tlc5940(cfg);
mjr 34:6b981a2afab7 2582
mjr 34:6b981a2afab7 2583 // enable the 74HC595 chips, if present
mjr 35:e959ffba78fd 2584 init_hc595(cfg);
mjr 6:cc35eb643e8f 2585
mjr 38:091e511ce8a0 2586 // Initialize the LedWiz ports. Note that it's important to wait until
mjr 38:091e511ce8a0 2587 // after initializing the various off-board output port controller chip
mjr 38:091e511ce8a0 2588 // sybsystems (TLC5940, 74HC595), since pins attached to peripheral
mjr 38:091e511ce8a0 2589 // controllers will need to address their respective controller objects,
mjr 38:091e511ce8a0 2590 // which don't exit until we initialize those subsystems.
mjr 35:e959ffba78fd 2591 initLwOut(cfg);
mjr 2:c174f9ee414a 2592
mjr 35:e959ffba78fd 2593 // start the TLC5940 clock
mjr 35:e959ffba78fd 2594 if (tlc5940 != 0)
mjr 35:e959ffba78fd 2595 tlc5940->start();
mjr 35:e959ffba78fd 2596
mjr 35:e959ffba78fd 2597 // initialize the button input ports
mjr 35:e959ffba78fd 2598 bool kbKeys = false;
mjr 35:e959ffba78fd 2599 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 2600
mjr 6:cc35eb643e8f 2601 // Create the joystick USB client. Note that we use the LedWiz unit
mjr 6:cc35eb643e8f 2602 // number from the saved configuration.
mjr 35:e959ffba78fd 2603 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, true, cfg.joystickEnabled, kbKeys);
mjr 38:091e511ce8a0 2604
mjr 38:091e511ce8a0 2605 // we're now connected - kill the pre-connect ticker
mjr 38:091e511ce8a0 2606 preConnectTicker.detach();
mjr 17:ab3cec0c8bf4 2607
mjr 38:091e511ce8a0 2608 // Last report timer for the joytick interface. We use the joystick timer
mjr 38:091e511ce8a0 2609 // to throttle the report rate, because VP doesn't benefit from reports any
mjr 38:091e511ce8a0 2610 // faster than about every 10ms.
mjr 38:091e511ce8a0 2611 Timer jsReportTimer;
mjr 38:091e511ce8a0 2612 jsReportTimer.start();
mjr 38:091e511ce8a0 2613
mjr 38:091e511ce8a0 2614 // Time since we successfully sent a USB report. This is a hacky workaround
mjr 38:091e511ce8a0 2615 // for sporadic problems in the USB stack that I haven't been able to figure
mjr 38:091e511ce8a0 2616 // out. If we go too long without successfully sending a USB report, we'll
mjr 38:091e511ce8a0 2617 // try resetting the connection.
mjr 38:091e511ce8a0 2618 Timer jsOKTimer;
mjr 38:091e511ce8a0 2619 jsOKTimer.start();
mjr 35:e959ffba78fd 2620
mjr 35:e959ffba78fd 2621 // set the initial status flags
mjr 35:e959ffba78fd 2622 statusFlags = (cfg.plunger.enabled ? 0x01 : 0x00);
mjr 17:ab3cec0c8bf4 2623
mjr 17:ab3cec0c8bf4 2624 // initialize the calibration buttons, if present
mjr 35:e959ffba78fd 2625 DigitalIn *calBtn = (cfg.plunger.cal.btn == NC ? 0 : new DigitalIn(cfg.plunger.cal.btn));
mjr 35:e959ffba78fd 2626 DigitalOut *calBtnLed = (cfg.plunger.cal.led == NC ? 0 : new DigitalOut(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 2627
mjr 35:e959ffba78fd 2628 // initialize the calibration button
mjr 1:d913e0afb2ac 2629 calBtnTimer.start();
mjr 35:e959ffba78fd 2630 calBtnState = 0;
mjr 1:d913e0afb2ac 2631
mjr 1:d913e0afb2ac 2632 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 2633 Timer hbTimer;
mjr 1:d913e0afb2ac 2634 hbTimer.start();
mjr 1:d913e0afb2ac 2635 int hb = 0;
mjr 5:a70c0bce770d 2636 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 2637
mjr 1:d913e0afb2ac 2638 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 2639 Timer acTimer;
mjr 1:d913e0afb2ac 2640 acTimer.start();
mjr 1:d913e0afb2ac 2641
mjr 0:5acbbe3f4cf4 2642 // create the accelerometer object
mjr 5:a70c0bce770d 2643 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS, MMA8451_INT_PIN);
mjr 0:5acbbe3f4cf4 2644
mjr 17:ab3cec0c8bf4 2645 // last accelerometer report, in joystick units (we report the nudge
mjr 17:ab3cec0c8bf4 2646 // acceleration via the joystick x & y axes, per the VP convention)
mjr 17:ab3cec0c8bf4 2647 int x = 0, y = 0;
mjr 17:ab3cec0c8bf4 2648
mjr 17:ab3cec0c8bf4 2649 // last plunger report position, in 'npix' normalized pixel units
mjr 17:ab3cec0c8bf4 2650 int pos = 0;
mjr 17:ab3cec0c8bf4 2651
mjr 17:ab3cec0c8bf4 2652 // last plunger report, in joystick units (we report the plunger as the
mjr 17:ab3cec0c8bf4 2653 // "z" axis of the joystick, per the VP convention)
mjr 17:ab3cec0c8bf4 2654 int z = 0;
mjr 17:ab3cec0c8bf4 2655
mjr 17:ab3cec0c8bf4 2656 // most recent prior plunger readings, for tracking release events(z0 is
mjr 17:ab3cec0c8bf4 2657 // reading just before the last one we reported, z1 is the one before that,
mjr 17:ab3cec0c8bf4 2658 // z2 the next before that)
mjr 17:ab3cec0c8bf4 2659 int z0 = 0, z1 = 0, z2 = 0;
mjr 17:ab3cec0c8bf4 2660
mjr 17:ab3cec0c8bf4 2661 // Simulated "bounce" position when firing. We model the bounce off of
mjr 17:ab3cec0c8bf4 2662 // the barrel spring when the plunger is released as proportional to the
mjr 17:ab3cec0c8bf4 2663 // distance it was retracted just before being released.
mjr 17:ab3cec0c8bf4 2664 int zBounce = 0;
mjr 2:c174f9ee414a 2665
mjr 17:ab3cec0c8bf4 2666 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 17:ab3cec0c8bf4 2667 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 17:ab3cec0c8bf4 2668 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 17:ab3cec0c8bf4 2669 // back and releases the plunger, or simply pushes on the plunger from
mjr 17:ab3cec0c8bf4 2670 // the rest position. This allows the plunger to be used in lieu of a
mjr 17:ab3cec0c8bf4 2671 // physical Launch Ball button for tables that don't have plungers.
mjr 17:ab3cec0c8bf4 2672 //
mjr 17:ab3cec0c8bf4 2673 // States:
mjr 17:ab3cec0c8bf4 2674 // 0 = default
mjr 17:ab3cec0c8bf4 2675 // 1 = cocked (plunger has been pulled back about 1" from state 0)
mjr 17:ab3cec0c8bf4 2676 // 2 = uncocked (plunger is pulled back less than 1" from state 1)
mjr 21:5048e16cc9ef 2677 // 3 = launching, plunger is forward beyond park position
mjr 21:5048e16cc9ef 2678 // 4 = launching, plunger is behind park position
mjr 21:5048e16cc9ef 2679 // 5 = pressed and holding (plunger has been pressed forward beyond
mjr 21:5048e16cc9ef 2680 // the park position from state 0)
mjr 17:ab3cec0c8bf4 2681 int lbState = 0;
mjr 6:cc35eb643e8f 2682
mjr 35:e959ffba78fd 2683 // button bit for ZB launch ball button
mjr 35:e959ffba78fd 2684 const uint32_t lbButtonBit = (1 << (cfg.plunger.zbLaunchBall.btn - 1));
mjr 35:e959ffba78fd 2685
mjr 17:ab3cec0c8bf4 2686 // Time since last lbState transition. Some of the states are time-
mjr 17:ab3cec0c8bf4 2687 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 17:ab3cec0c8bf4 2688 // we remain in this state for more than a few milliseconds, since
mjr 17:ab3cec0c8bf4 2689 // it indicates that the plunger is being slowly returned to rest
mjr 17:ab3cec0c8bf4 2690 // rather than released. In the "launching" state, we need to release
mjr 17:ab3cec0c8bf4 2691 // the Launch Ball button after a moment, and we need to wait for
mjr 17:ab3cec0c8bf4 2692 // the plunger to come to rest before returning to state 0.
mjr 17:ab3cec0c8bf4 2693 Timer lbTimer;
mjr 17:ab3cec0c8bf4 2694 lbTimer.start();
mjr 17:ab3cec0c8bf4 2695
mjr 18:5e890ebd0023 2696 // Launch Ball simulated push timer. We start this when we simulate
mjr 18:5e890ebd0023 2697 // the button push, and turn off the simulated button when enough time
mjr 18:5e890ebd0023 2698 // has elapsed.
mjr 18:5e890ebd0023 2699 Timer lbBtnTimer;
mjr 18:5e890ebd0023 2700
mjr 17:ab3cec0c8bf4 2701 // Simulated button states. This is a vector of button states
mjr 17:ab3cec0c8bf4 2702 // for the simulated buttons. We combine this with the physical
mjr 17:ab3cec0c8bf4 2703 // button states on each USB joystick report, so we will report
mjr 17:ab3cec0c8bf4 2704 // a button as pressed if either the physical button is being pressed
mjr 17:ab3cec0c8bf4 2705 // or we're simulating a press on the button. This is used for the
mjr 17:ab3cec0c8bf4 2706 // simulated Launch Ball button.
mjr 17:ab3cec0c8bf4 2707 uint32_t simButtons = 0;
mjr 6:cc35eb643e8f 2708
mjr 6:cc35eb643e8f 2709 // Firing in progress: we set this when we detect the start of rapid
mjr 6:cc35eb643e8f 2710 // plunger movement from a retracted position towards the rest position.
mjr 17:ab3cec0c8bf4 2711 //
mjr 17:ab3cec0c8bf4 2712 // When we detect a firing event, we send VP a series of synthetic
mjr 17:ab3cec0c8bf4 2713 // reports simulating the idealized plunger motion. The actual physical
mjr 17:ab3cec0c8bf4 2714 // motion is much too fast to report to VP; in the time between two USB
mjr 17:ab3cec0c8bf4 2715 // reports, the plunger can shoot all the way forward, rebound off of
mjr 17:ab3cec0c8bf4 2716 // the barrel spring, bounce back part way, and bounce forward again,
mjr 17:ab3cec0c8bf4 2717 // or even do all of this more than once. This means that sampling the
mjr 17:ab3cec0c8bf4 2718 // physical motion at the USB report rate would create a misleading
mjr 17:ab3cec0c8bf4 2719 // picture of the plunger motion, since our samples would catch the
mjr 17:ab3cec0c8bf4 2720 // plunger at random points in this oscillating motion. From the
mjr 17:ab3cec0c8bf4 2721 // user's perspective, the physical action that occurred is simply that
mjr 17:ab3cec0c8bf4 2722 // the plunger was released from a particular distance, so it's this
mjr 17:ab3cec0c8bf4 2723 // high-level event that we want to convey to VP. To do this, we
mjr 17:ab3cec0c8bf4 2724 // synthesize a series of reports to convey an idealized version of
mjr 17:ab3cec0c8bf4 2725 // the release motion that's perfectly synchronized to the VP reports.
mjr 17:ab3cec0c8bf4 2726 // Essentially we pretend that our USB position samples are exactly
mjr 17:ab3cec0c8bf4 2727 // aligned in time with (1) the point of retraction just before the
mjr 17:ab3cec0c8bf4 2728 // user released the plunger, (2) the point of maximum forward motion
mjr 17:ab3cec0c8bf4 2729 // just after the user released the plunger (the point of maximum
mjr 17:ab3cec0c8bf4 2730 // compression as the plunger bounces off of the barrel spring), and
mjr 17:ab3cec0c8bf4 2731 // (3) the plunger coming to rest at the park position. This series
mjr 17:ab3cec0c8bf4 2732 // of reports is synthetic in the sense that it's not what we actually
mjr 17:ab3cec0c8bf4 2733 // see on the CCD at the times of these reports - the true plunger
mjr 17:ab3cec0c8bf4 2734 // position is oscillating at high speed during this period. But at
mjr 17:ab3cec0c8bf4 2735 // the same time it conveys a more faithful picture of the true physical
mjr 17:ab3cec0c8bf4 2736 // motion to VP, and allows VP to reproduce the true physical motion
mjr 17:ab3cec0c8bf4 2737 // more faithfully in its simulation model, by correcting for the
mjr 17:ab3cec0c8bf4 2738 // relatively low sampling rate in the communication path between the
mjr 17:ab3cec0c8bf4 2739 // real plunger and VP's model plunger.
mjr 17:ab3cec0c8bf4 2740 //
mjr 17:ab3cec0c8bf4 2741 // If 'firing' is non-zero, it's the index of our current report in
mjr 17:ab3cec0c8bf4 2742 // the synthetic firing report series.
mjr 9:fd65b0a94720 2743 int firing = 0;
mjr 2:c174f9ee414a 2744
mjr 2:c174f9ee414a 2745 // start the first CCD integration cycle
mjr 35:e959ffba78fd 2746 plungerSensor->init();
mjr 10:976666ffa4ef 2747
mjr 1:d913e0afb2ac 2748 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 2749 // host requests
mjr 0:5acbbe3f4cf4 2750 for (;;)
mjr 0:5acbbe3f4cf4 2751 {
mjr 39:b3815a1c3802 2752 // Process incoming reports on the joystick interface. This channel
mjr 39:b3815a1c3802 2753 // is used for LedWiz commands are our extended protocol commands.
mjr 39:b3815a1c3802 2754 LedWizMsg lwm;
mjr 39:b3815a1c3802 2755 while (js.readLedWizMsg(lwm))
mjr 39:b3815a1c3802 2756 handleInputMsg(lwm, js, z);
mjr 1:d913e0afb2ac 2757
mjr 1:d913e0afb2ac 2758 // check for plunger calibration
mjr 17:ab3cec0c8bf4 2759 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 2760 {
mjr 1:d913e0afb2ac 2761 // check the state
mjr 1:d913e0afb2ac 2762 switch (calBtnState)
mjr 0:5acbbe3f4cf4 2763 {
mjr 1:d913e0afb2ac 2764 case 0:
mjr 1:d913e0afb2ac 2765 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 2766 calBtnTimer.reset();
mjr 1:d913e0afb2ac 2767 calBtnState = 1;
mjr 1:d913e0afb2ac 2768 break;
mjr 1:d913e0afb2ac 2769
mjr 1:d913e0afb2ac 2770 case 1:
mjr 1:d913e0afb2ac 2771 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 2772 // passed, start the hold period
mjr 9:fd65b0a94720 2773 if (calBtnTimer.read_ms() > 50)
mjr 1:d913e0afb2ac 2774 calBtnState = 2;
mjr 1:d913e0afb2ac 2775 break;
mjr 1:d913e0afb2ac 2776
mjr 1:d913e0afb2ac 2777 case 2:
mjr 1:d913e0afb2ac 2778 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 2779 // for the entire hold period, move to calibration mode
mjr 9:fd65b0a94720 2780 if (calBtnTimer.read_ms() > 2050)
mjr 1:d913e0afb2ac 2781 {
mjr 1:d913e0afb2ac 2782 // enter calibration mode
mjr 1:d913e0afb2ac 2783 calBtnState = 3;
mjr 9:fd65b0a94720 2784 calBtnTimer.reset();
mjr 35:e959ffba78fd 2785
mjr 35:e959ffba78fd 2786 // reset the plunger calibration limits
mjr 35:e959ffba78fd 2787 cfg.plunger.cal.reset(plungerSensor->npix);
mjr 1:d913e0afb2ac 2788 }
mjr 1:d913e0afb2ac 2789 break;
mjr 2:c174f9ee414a 2790
mjr 2:c174f9ee414a 2791 case 3:
mjr 9:fd65b0a94720 2792 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 2793 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 2794 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 2795 break;
mjr 0:5acbbe3f4cf4 2796 }
mjr 0:5acbbe3f4cf4 2797 }
mjr 1:d913e0afb2ac 2798 else
mjr 1:d913e0afb2ac 2799 {
mjr 2:c174f9ee414a 2800 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 2801 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 2802 // and save the results to flash.
mjr 2:c174f9ee414a 2803 //
mjr 2:c174f9ee414a 2804 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 2805 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 2806 // mode, it simply cancels the attempt.
mjr 9:fd65b0a94720 2807 if (calBtnState == 3 && calBtnTimer.read_ms() > 15000)
mjr 2:c174f9ee414a 2808 {
mjr 2:c174f9ee414a 2809 // exit calibration mode
mjr 1:d913e0afb2ac 2810 calBtnState = 0;
mjr 2:c174f9ee414a 2811
mjr 6:cc35eb643e8f 2812 // save the updated configuration
mjr 35:e959ffba78fd 2813 cfg.plunger.cal.calibrated = 1;
mjr 35:e959ffba78fd 2814 saveConfigToFlash();
mjr 2:c174f9ee414a 2815 }
mjr 2:c174f9ee414a 2816 else if (calBtnState != 3)
mjr 2:c174f9ee414a 2817 {
mjr 2:c174f9ee414a 2818 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 2819 calBtnState = 0;
mjr 2:c174f9ee414a 2820 }
mjr 1:d913e0afb2ac 2821 }
mjr 1:d913e0afb2ac 2822
mjr 1:d913e0afb2ac 2823 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 2824 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 2825 switch (calBtnState)
mjr 0:5acbbe3f4cf4 2826 {
mjr 1:d913e0afb2ac 2827 case 2:
mjr 1:d913e0afb2ac 2828 // in the hold period - flash the light
mjr 9:fd65b0a94720 2829 newCalBtnLit = ((calBtnTimer.read_ms()/250) & 1);
mjr 1:d913e0afb2ac 2830 break;
mjr 1:d913e0afb2ac 2831
mjr 1:d913e0afb2ac 2832 case 3:
mjr 1:d913e0afb2ac 2833 // calibration mode - show steady on
mjr 1:d913e0afb2ac 2834 newCalBtnLit = true;
mjr 1:d913e0afb2ac 2835 break;
mjr 1:d913e0afb2ac 2836
mjr 1:d913e0afb2ac 2837 default:
mjr 1:d913e0afb2ac 2838 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 2839 newCalBtnLit = false;
mjr 1:d913e0afb2ac 2840 break;
mjr 1:d913e0afb2ac 2841 }
mjr 3:3514575d4f86 2842
mjr 3:3514575d4f86 2843 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 2844 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 2845 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 2846 {
mjr 1:d913e0afb2ac 2847 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 2848 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 2849 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 2850 calBtnLed->write(1);
mjr 38:091e511ce8a0 2851 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 2852 }
mjr 2:c174f9ee414a 2853 else {
mjr 17:ab3cec0c8bf4 2854 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 2855 calBtnLed->write(0);
mjr 38:091e511ce8a0 2856 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 2857 }
mjr 1:d913e0afb2ac 2858 }
mjr 38:091e511ce8a0 2859
mjr 17:ab3cec0c8bf4 2860 // If the plunger is enabled, and we're not already in a firing event,
mjr 17:ab3cec0c8bf4 2861 // and the last plunger reading had the plunger pulled back at least
mjr 17:ab3cec0c8bf4 2862 // a bit, watch for plunger release events until it's time for our next
mjr 17:ab3cec0c8bf4 2863 // USB report.
mjr 35:e959ffba78fd 2864 if (!firing && cfg.plunger.enabled && z >= JOYMAX/6)
mjr 17:ab3cec0c8bf4 2865 {
mjr 17:ab3cec0c8bf4 2866 // monitor the plunger until it's time for our next report
mjr 38:091e511ce8a0 2867 while (jsReportTimer.read_ms() < 15)
mjr 17:ab3cec0c8bf4 2868 {
mjr 17:ab3cec0c8bf4 2869 // do a fast low-res scan; if it's at or past the zero point,
mjr 17:ab3cec0c8bf4 2870 // start a firing event
mjr 35:e959ffba78fd 2871 int pos0;
mjr 35:e959ffba78fd 2872 if (plungerSensor->lowResScan(pos0) && pos0 <= cfg.plunger.cal.zero)
mjr 17:ab3cec0c8bf4 2873 firing = 1;
mjr 17:ab3cec0c8bf4 2874 }
mjr 17:ab3cec0c8bf4 2875 }
mjr 17:ab3cec0c8bf4 2876
mjr 6:cc35eb643e8f 2877 // read the plunger sensor, if it's enabled
mjr 35:e959ffba78fd 2878 if (cfg.plunger.enabled)
mjr 6:cc35eb643e8f 2879 {
mjr 6:cc35eb643e8f 2880 // start with the previous reading, in case we don't have a
mjr 6:cc35eb643e8f 2881 // clear result on this frame
mjr 6:cc35eb643e8f 2882 int znew = z;
mjr 35:e959ffba78fd 2883 if (plungerSensor->highResScan(pos))
mjr 6:cc35eb643e8f 2884 {
mjr 17:ab3cec0c8bf4 2885 // We got a new reading. If we're in calibration mode, use it
mjr 17:ab3cec0c8bf4 2886 // to figure the new calibration, otherwise adjust the new reading
mjr 17:ab3cec0c8bf4 2887 // for the established calibration.
mjr 17:ab3cec0c8bf4 2888 if (calBtnState == 3)
mjr 6:cc35eb643e8f 2889 {
mjr 17:ab3cec0c8bf4 2890 // Calibration mode. If this reading is outside of the current
mjr 17:ab3cec0c8bf4 2891 // calibration bounds, expand the bounds.
mjr 35:e959ffba78fd 2892 if (pos < cfg.plunger.cal.min)
mjr 35:e959ffba78fd 2893 cfg.plunger.cal.min = pos;
mjr 35:e959ffba78fd 2894 if (pos < cfg.plunger.cal.zero)
mjr 35:e959ffba78fd 2895 cfg.plunger.cal.zero = pos;
mjr 35:e959ffba78fd 2896 if (pos > cfg.plunger.cal.max)
mjr 35:e959ffba78fd 2897 cfg.plunger.cal.max = pos;
mjr 6:cc35eb643e8f 2898
mjr 17:ab3cec0c8bf4 2899 // normalize to the full physical range while calibrating
mjr 35:e959ffba78fd 2900 znew = int(round(float(pos)/plungerSensor->npix * JOYMAX));
mjr 17:ab3cec0c8bf4 2901 }
mjr 17:ab3cec0c8bf4 2902 else
mjr 17:ab3cec0c8bf4 2903 {
mjr 17:ab3cec0c8bf4 2904 // Not in calibration mode, so normalize the new reading to the
mjr 17:ab3cec0c8bf4 2905 // established calibration range.
mjr 17:ab3cec0c8bf4 2906 //
mjr 17:ab3cec0c8bf4 2907 // Note that negative values are allowed. Zero represents the
mjr 17:ab3cec0c8bf4 2908 // "park" position, where the plunger sits when at rest. A mechanical
mjr 23:14f8c5004cd0 2909 // plunger has a small amount of travel in the "push" direction,
mjr 17:ab3cec0c8bf4 2910 // since the barrel spring can be compressed slightly. Negative
mjr 17:ab3cec0c8bf4 2911 // values represent travel in the push direction.
mjr 35:e959ffba78fd 2912 if (pos > cfg.plunger.cal.max)
mjr 35:e959ffba78fd 2913 pos = cfg.plunger.cal.max;
mjr 35:e959ffba78fd 2914 znew = int(round(float(pos - cfg.plunger.cal.zero)
mjr 35:e959ffba78fd 2915 / (cfg.plunger.cal.max - cfg.plunger.cal.zero + 1) * JOYMAX));
mjr 6:cc35eb643e8f 2916 }
mjr 6:cc35eb643e8f 2917 }
mjr 7:100a25f8bf56 2918
mjr 17:ab3cec0c8bf4 2919 // If we're not already in a firing event, check to see if the
mjr 17:ab3cec0c8bf4 2920 // new position is forward of the last report. If it is, a firing
mjr 17:ab3cec0c8bf4 2921 // event might have started during the high-res scan. This might
mjr 17:ab3cec0c8bf4 2922 // seem unlikely given that the scan only takes about 5ms, but that
mjr 17:ab3cec0c8bf4 2923 // 5ms represents about 25-30% of our total time between reports,
mjr 17:ab3cec0c8bf4 2924 // there's about a 1 in 4 chance that a release starts during a
mjr 17:ab3cec0c8bf4 2925 // scan.
mjr 17:ab3cec0c8bf4 2926 if (!firing && z0 > 0 && znew < z0)
mjr 17:ab3cec0c8bf4 2927 {
mjr 17:ab3cec0c8bf4 2928 // The plunger has moved forward since the previous report.
mjr 17:ab3cec0c8bf4 2929 // Watch it for a few more ms to see if we can get a stable
mjr 17:ab3cec0c8bf4 2930 // new position.
mjr 35:e959ffba78fd 2931 int pos0;
mjr 35:e959ffba78fd 2932 if (plungerSensor->lowResScan(pos0))
mjr 17:ab3cec0c8bf4 2933 {
mjr 35:e959ffba78fd 2934 int pos1 = pos0;
mjr 35:e959ffba78fd 2935 Timer tw;
mjr 35:e959ffba78fd 2936 tw.start();
mjr 35:e959ffba78fd 2937 while (tw.read_ms() < 6)
mjr 17:ab3cec0c8bf4 2938 {
mjr 35:e959ffba78fd 2939 // read the new position
mjr 35:e959ffba78fd 2940 int pos2;
mjr 35:e959ffba78fd 2941 if (plungerSensor->lowResScan(pos2))
mjr 35:e959ffba78fd 2942 {
mjr 35:e959ffba78fd 2943 // If it's stable over consecutive readings, stop looping.
mjr 35:e959ffba78fd 2944 // (Count it as stable if the position is within about 1/8".
mjr 35:e959ffba78fd 2945 // pos1 and pos2 are reported in pixels, so they range from
mjr 35:e959ffba78fd 2946 // 0 to npix. The overall travel of a standard plunger is
mjr 35:e959ffba78fd 2947 // about 3.2", so we have (npix/3.2) pixels per inch, hence
mjr 35:e959ffba78fd 2948 // 1/8" is (npix/3.2)*(1/8) pixels.)
mjr 35:e959ffba78fd 2949 if (abs(pos2 - pos1) < int(plungerSensor->npix/(3.2*8)))
mjr 35:e959ffba78fd 2950 break;
mjr 35:e959ffba78fd 2951
mjr 35:e959ffba78fd 2952 // If we've crossed the rest position, and we've moved by
mjr 35:e959ffba78fd 2953 // a minimum distance from where we starting this loop, begin
mjr 35:e959ffba78fd 2954 // a firing event. (We require a minimum distance to prevent
mjr 35:e959ffba78fd 2955 // spurious firing from random analog noise in the readings
mjr 35:e959ffba78fd 2956 // when the plunger is actually just sitting still at the
mjr 35:e959ffba78fd 2957 // rest position. If it's at rest, it's normal to see small
mjr 35:e959ffba78fd 2958 // random fluctuations in the analog reading +/- 1% or so
mjr 35:e959ffba78fd 2959 // from the 0 point, especially with a sensor like a
mjr 35:e959ffba78fd 2960 // potentionemeter that reports the position as a single
mjr 35:e959ffba78fd 2961 // analog voltage.) Note that we compare the latest reading
mjr 35:e959ffba78fd 2962 // to the first reading of the loop - we don't require the
mjr 35:e959ffba78fd 2963 // threshold motion over consecutive readings, but any time
mjr 35:e959ffba78fd 2964 // over the stability wait loop.
mjr 35:e959ffba78fd 2965 if (pos1 < cfg.plunger.cal.zero
mjr 35:e959ffba78fd 2966 && abs(pos2 - pos0) > int(plungerSensor->npix/(3.2*8)))
mjr 35:e959ffba78fd 2967 {
mjr 35:e959ffba78fd 2968 firing = 1;
mjr 35:e959ffba78fd 2969 break;
mjr 35:e959ffba78fd 2970 }
mjr 35:e959ffba78fd 2971
mjr 35:e959ffba78fd 2972 // the new reading is now the prior reading
mjr 35:e959ffba78fd 2973 pos1 = pos2;
mjr 35:e959ffba78fd 2974 }
mjr 17:ab3cec0c8bf4 2975 }
mjr 17:ab3cec0c8bf4 2976 }
mjr 17:ab3cec0c8bf4 2977 }
mjr 17:ab3cec0c8bf4 2978
mjr 17:ab3cec0c8bf4 2979 // Check for a simulated Launch Ball button press, if enabled
mjr 35:e959ffba78fd 2980 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 17:ab3cec0c8bf4 2981 {
mjr 18:5e890ebd0023 2982 const int cockThreshold = JOYMAX/3;
mjr 35:e959ffba78fd 2983 const int pushThreshold = int(-JOYMAX/3 * cfg.plunger.zbLaunchBall.pushDistance);
mjr 17:ab3cec0c8bf4 2984 int newState = lbState;
mjr 17:ab3cec0c8bf4 2985 switch (lbState)
mjr 17:ab3cec0c8bf4 2986 {
mjr 17:ab3cec0c8bf4 2987 case 0:
mjr 17:ab3cec0c8bf4 2988 // Base state. If the plunger is pulled back by an inch
mjr 17:ab3cec0c8bf4 2989 // or more, go to "cocked" state. If the plunger is pushed
mjr 21:5048e16cc9ef 2990 // forward by 1/4" or more, go to "pressed" state.
mjr 18:5e890ebd0023 2991 if (znew >= cockThreshold)
mjr 17:ab3cec0c8bf4 2992 newState = 1;
mjr 18:5e890ebd0023 2993 else if (znew <= pushThreshold)
mjr 21:5048e16cc9ef 2994 newState = 5;
mjr 17:ab3cec0c8bf4 2995 break;
mjr 17:ab3cec0c8bf4 2996
mjr 17:ab3cec0c8bf4 2997 case 1:
mjr 17:ab3cec0c8bf4 2998 // Cocked state. If a firing event is now in progress,
mjr 17:ab3cec0c8bf4 2999 // go to "launch" state. Otherwise, if the plunger is less
mjr 17:ab3cec0c8bf4 3000 // than 1" retracted, go to "uncocked" state - the player
mjr 17:ab3cec0c8bf4 3001 // might be slowly returning the plunger to rest so as not
mjr 17:ab3cec0c8bf4 3002 // to trigger a launch.
mjr 17:ab3cec0c8bf4 3003 if (firing || znew <= 0)
mjr 17:ab3cec0c8bf4 3004 newState = 3;
mjr 18:5e890ebd0023 3005 else if (znew < cockThreshold)
mjr 17:ab3cec0c8bf4 3006 newState = 2;
mjr 17:ab3cec0c8bf4 3007 break;
mjr 17:ab3cec0c8bf4 3008
mjr 17:ab3cec0c8bf4 3009 case 2:
mjr 17:ab3cec0c8bf4 3010 // Uncocked state. If the plunger is more than an inch
mjr 17:ab3cec0c8bf4 3011 // retracted, return to cocked state. If we've been in
mjr 17:ab3cec0c8bf4 3012 // the uncocked state for more than half a second, return
mjr 18:5e890ebd0023 3013 // to the base state. This allows the user to return the
mjr 18:5e890ebd0023 3014 // plunger to rest without triggering a launch, by moving
mjr 18:5e890ebd0023 3015 // it at manual speed to the rest position rather than
mjr 18:5e890ebd0023 3016 // releasing it.
mjr 18:5e890ebd0023 3017 if (znew >= cockThreshold)
mjr 17:ab3cec0c8bf4 3018 newState = 1;
mjr 17:ab3cec0c8bf4 3019 else if (lbTimer.read_ms() > 500)
mjr 17:ab3cec0c8bf4 3020 newState = 0;
mjr 17:ab3cec0c8bf4 3021 break;
mjr 17:ab3cec0c8bf4 3022
mjr 17:ab3cec0c8bf4 3023 case 3:
mjr 17:ab3cec0c8bf4 3024 // Launch state. If the plunger is no longer pushed
mjr 17:ab3cec0c8bf4 3025 // forward, switch to launch rest state.
mjr 18:5e890ebd0023 3026 if (znew >= 0)
mjr 17:ab3cec0c8bf4 3027 newState = 4;
mjr 17:ab3cec0c8bf4 3028 break;
mjr 17:ab3cec0c8bf4 3029
mjr 17:ab3cec0c8bf4 3030 case 4:
mjr 17:ab3cec0c8bf4 3031 // Launch rest state. If the plunger is pushed forward
mjr 17:ab3cec0c8bf4 3032 // again, switch back to launch state. If not, and we've
mjr 17:ab3cec0c8bf4 3033 // been in this state for at least 200ms, return to the
mjr 17:ab3cec0c8bf4 3034 // default state.
mjr 18:5e890ebd0023 3035 if (znew <= pushThreshold)
mjr 17:ab3cec0c8bf4 3036 newState = 3;
mjr 17:ab3cec0c8bf4 3037 else if (lbTimer.read_ms() > 200)
mjr 17:ab3cec0c8bf4 3038 newState = 0;
mjr 17:ab3cec0c8bf4 3039 break;
mjr 21:5048e16cc9ef 3040
mjr 21:5048e16cc9ef 3041 case 5:
mjr 21:5048e16cc9ef 3042 // Press-and-Hold state. If the plunger is no longer pushed
mjr 21:5048e16cc9ef 3043 // forward, AND it's been at least 50ms since we generated
mjr 21:5048e16cc9ef 3044 // the simulated Launch Ball button press, return to the base
mjr 21:5048e16cc9ef 3045 // state. The minimum time is to ensure that VP has a chance
mjr 21:5048e16cc9ef 3046 // to see the button press and to avoid transient key bounce
mjr 21:5048e16cc9ef 3047 // effects when the plunger position is right on the threshold.
mjr 21:5048e16cc9ef 3048 if (znew > pushThreshold && lbTimer.read_ms() > 50)
mjr 21:5048e16cc9ef 3049 newState = 0;
mjr 21:5048e16cc9ef 3050 break;
mjr 17:ab3cec0c8bf4 3051 }
mjr 17:ab3cec0c8bf4 3052
mjr 17:ab3cec0c8bf4 3053 // change states if desired
mjr 17:ab3cec0c8bf4 3054 if (newState != lbState)
mjr 17:ab3cec0c8bf4 3055 {
mjr 21:5048e16cc9ef 3056 // If we're entering Launch state OR we're entering the
mjr 21:5048e16cc9ef 3057 // Press-and-Hold state, AND the ZB Launch Ball LedWiz signal
mjr 21:5048e16cc9ef 3058 // is turned on, simulate a Launch Ball button press.
mjr 21:5048e16cc9ef 3059 if (((newState == 3 && lbState != 4) || newState == 5)
mjr 35:e959ffba78fd 3060 && wizOn[cfg.plunger.zbLaunchBall.port-1])
mjr 18:5e890ebd0023 3061 {
mjr 18:5e890ebd0023 3062 lbBtnTimer.reset();
mjr 18:5e890ebd0023 3063 lbBtnTimer.start();
mjr 18:5e890ebd0023 3064 simButtons |= lbButtonBit;
mjr 18:5e890ebd0023 3065 }
mjr 21:5048e16cc9ef 3066
mjr 17:ab3cec0c8bf4 3067 // if we're switching to state 0, release the button
mjr 17:ab3cec0c8bf4 3068 if (newState == 0)
mjr 35:e959ffba78fd 3069 simButtons &= ~(1 << (cfg.plunger.zbLaunchBall.btn - 1));
mjr 17:ab3cec0c8bf4 3070
mjr 17:ab3cec0c8bf4 3071 // switch to the new state
mjr 17:ab3cec0c8bf4 3072 lbState = newState;
mjr 17:ab3cec0c8bf4 3073
mjr 17:ab3cec0c8bf4 3074 // start timing in the new state
mjr 17:ab3cec0c8bf4 3075 lbTimer.reset();
mjr 17:ab3cec0c8bf4 3076 }
mjr 21:5048e16cc9ef 3077
mjr 21:5048e16cc9ef 3078 // If the Launch Ball button press is in effect, but the
mjr 21:5048e16cc9ef 3079 // ZB Launch Ball LedWiz signal is no longer turned on, turn
mjr 21:5048e16cc9ef 3080 // off the button.
mjr 21:5048e16cc9ef 3081 //
mjr 21:5048e16cc9ef 3082 // If we're in one of the Launch states (state #3 or #4),
mjr 21:5048e16cc9ef 3083 // and the button has been on for long enough, turn it off.
mjr 21:5048e16cc9ef 3084 // The Launch mode is triggered by a pull-and-release gesture.
mjr 21:5048e16cc9ef 3085 // From the user's perspective, this is just a single gesture
mjr 21:5048e16cc9ef 3086 // that should trigger just one momentary press on the Launch
mjr 21:5048e16cc9ef 3087 // Ball button. Physically, though, the plunger usually
mjr 21:5048e16cc9ef 3088 // bounces back and forth for 500ms or so before coming to
mjr 21:5048e16cc9ef 3089 // rest after this gesture. That's what the whole state
mjr 21:5048e16cc9ef 3090 // #3-#4 business is all about - we stay in this pair of
mjr 21:5048e16cc9ef 3091 // states until the plunger comes to rest. As long as we're
mjr 21:5048e16cc9ef 3092 // in these states, we won't send duplicate button presses.
mjr 21:5048e16cc9ef 3093 // But we also don't want the one button press to continue
mjr 21:5048e16cc9ef 3094 // the whole time, so we'll time it out now.
mjr 21:5048e16cc9ef 3095 //
mjr 21:5048e16cc9ef 3096 // (This could be written as one big 'if' condition, but
mjr 21:5048e16cc9ef 3097 // I'm breaking it out verbosely like this to make it easier
mjr 21:5048e16cc9ef 3098 // for human readers such as myself to comprehend the logic.)
mjr 21:5048e16cc9ef 3099 if ((simButtons & lbButtonBit) != 0)
mjr 18:5e890ebd0023 3100 {
mjr 21:5048e16cc9ef 3101 int turnOff = false;
mjr 21:5048e16cc9ef 3102
mjr 21:5048e16cc9ef 3103 // turn it off if the ZB Launch Ball signal is off
mjr 35:e959ffba78fd 3104 if (!wizOn[cfg.plunger.zbLaunchBall.port-1])
mjr 21:5048e16cc9ef 3105 turnOff = true;
mjr 21:5048e16cc9ef 3106
mjr 21:5048e16cc9ef 3107 // also turn it off if we're in state 3 or 4 ("Launch"),
mjr 21:5048e16cc9ef 3108 // and the button has been on long enough
mjr 21:5048e16cc9ef 3109 if ((lbState == 3 || lbState == 4) && lbBtnTimer.read_ms() > 250)
mjr 21:5048e16cc9ef 3110 turnOff = true;
mjr 21:5048e16cc9ef 3111
mjr 21:5048e16cc9ef 3112 // if we decided to turn off the button, do so
mjr 21:5048e16cc9ef 3113 if (turnOff)
mjr 21:5048e16cc9ef 3114 {
mjr 21:5048e16cc9ef 3115 lbBtnTimer.stop();
mjr 21:5048e16cc9ef 3116 simButtons &= ~lbButtonBit;
mjr 21:5048e16cc9ef 3117 }
mjr 18:5e890ebd0023 3118 }
mjr 17:ab3cec0c8bf4 3119 }
mjr 17:ab3cec0c8bf4 3120
mjr 17:ab3cec0c8bf4 3121 // If a firing event is in progress, generate synthetic reports to
mjr 17:ab3cec0c8bf4 3122 // describe an idealized version of the plunger motion to VP rather
mjr 17:ab3cec0c8bf4 3123 // than reporting the actual physical plunger position.
mjr 6:cc35eb643e8f 3124 //
mjr 17:ab3cec0c8bf4 3125 // We use the synthetic reports during a release event because the
mjr 17:ab3cec0c8bf4 3126 // physical plunger motion when released is too fast for VP to track.
mjr 17:ab3cec0c8bf4 3127 // VP only syncs its internal physics model with the outside world
mjr 17:ab3cec0c8bf4 3128 // about every 10ms. In that amount of time, the plunger moves
mjr 17:ab3cec0c8bf4 3129 // fast enough when released that it can shoot all the way forward,
mjr 17:ab3cec0c8bf4 3130 // bounce off of the barrel spring, and rebound part of the way
mjr 17:ab3cec0c8bf4 3131 // back. The result is the classic analog-to-digital problem of
mjr 17:ab3cec0c8bf4 3132 // sample aliasing. If we happen to time our sample during the
mjr 17:ab3cec0c8bf4 3133 // release motion so that we catch the plunger at the peak of a
mjr 17:ab3cec0c8bf4 3134 // bounce, the digital signal incorrectly looks like the plunger
mjr 17:ab3cec0c8bf4 3135 // is moving slowly forward - VP thinks we went from fully
mjr 17:ab3cec0c8bf4 3136 // retracted to half retracted in the sample interval, whereas
mjr 17:ab3cec0c8bf4 3137 // we actually traveled all the way forward and half way back,
mjr 17:ab3cec0c8bf4 3138 // so the speed VP infers is about 1/3 of the actual speed.
mjr 9:fd65b0a94720 3139 //
mjr 17:ab3cec0c8bf4 3140 // To correct this, we take advantage of our ability to sample
mjr 17:ab3cec0c8bf4 3141 // the CCD image several times in the course of a VP report. If
mjr 17:ab3cec0c8bf4 3142 // we catch the plunger near the origin after we've seen it
mjr 17:ab3cec0c8bf4 3143 // retracted, we go into Release Event mode. During this mode,
mjr 17:ab3cec0c8bf4 3144 // we stop reporting the true physical plunger position, and
mjr 17:ab3cec0c8bf4 3145 // instead report an idealized pattern: we report the plunger
mjr 17:ab3cec0c8bf4 3146 // immediately shooting forward to a position in front of the
mjr 17:ab3cec0c8bf4 3147 // park position that's in proportion to how far back the plunger
mjr 17:ab3cec0c8bf4 3148 // was just before the release, and we then report it stationary
mjr 17:ab3cec0c8bf4 3149 // at the park position. We continue to report the stationary
mjr 17:ab3cec0c8bf4 3150 // park position until the actual physical plunger motion has
mjr 17:ab3cec0c8bf4 3151 // stabilized on a new position. We then exit Release Event
mjr 17:ab3cec0c8bf4 3152 // mode and return to reporting the true physical position.
mjr 17:ab3cec0c8bf4 3153 if (firing)
mjr 6:cc35eb643e8f 3154 {
mjr 17:ab3cec0c8bf4 3155 // Firing in progress. Keep reporting the park position
mjr 17:ab3cec0c8bf4 3156 // until the physical plunger position comes to rest.
mjr 17:ab3cec0c8bf4 3157 const int restTol = JOYMAX/24;
mjr 17:ab3cec0c8bf4 3158 if (firing == 1)
mjr 6:cc35eb643e8f 3159 {
mjr 17:ab3cec0c8bf4 3160 // For the first couple of frames, show the plunger shooting
mjr 17:ab3cec0c8bf4 3161 // forward past the zero point, to simulate the momentum carrying
mjr 17:ab3cec0c8bf4 3162 // it forward to bounce off of the barrel spring. Show the
mjr 17:ab3cec0c8bf4 3163 // bounce as proportional to the distance it was retracted
mjr 17:ab3cec0c8bf4 3164 // in the prior report.
mjr 17:ab3cec0c8bf4 3165 z = zBounce = -z0/6;
mjr 17:ab3cec0c8bf4 3166 ++firing;
mjr 6:cc35eb643e8f 3167 }
mjr 17:ab3cec0c8bf4 3168 else if (firing == 2)
mjr 9:fd65b0a94720 3169 {
mjr 17:ab3cec0c8bf4 3170 // second frame - keep the bounce a little longer
mjr 17:ab3cec0c8bf4 3171 z = zBounce;
mjr 17:ab3cec0c8bf4 3172 ++firing;
mjr 17:ab3cec0c8bf4 3173 }
mjr 17:ab3cec0c8bf4 3174 else if (firing > 4
mjr 17:ab3cec0c8bf4 3175 && abs(znew - z0) < restTol
mjr 17:ab3cec0c8bf4 3176 && abs(znew - z1) < restTol
mjr 17:ab3cec0c8bf4 3177 && abs(znew - z2) < restTol)
mjr 17:ab3cec0c8bf4 3178 {
mjr 17:ab3cec0c8bf4 3179 // The physical plunger has come to rest. Exit firing
mjr 17:ab3cec0c8bf4 3180 // mode and resume reporting the actual position.
mjr 17:ab3cec0c8bf4 3181 firing = false;
mjr 17:ab3cec0c8bf4 3182 z = znew;
mjr 9:fd65b0a94720 3183 }
mjr 9:fd65b0a94720 3184 else
mjr 9:fd65b0a94720 3185 {
mjr 17:ab3cec0c8bf4 3186 // until the physical plunger comes to rest, simply
mjr 17:ab3cec0c8bf4 3187 // report the park position
mjr 9:fd65b0a94720 3188 z = 0;
mjr 17:ab3cec0c8bf4 3189 ++firing;
mjr 9:fd65b0a94720 3190 }
mjr 6:cc35eb643e8f 3191 }
mjr 6:cc35eb643e8f 3192 else
mjr 6:cc35eb643e8f 3193 {
mjr 17:ab3cec0c8bf4 3194 // not in firing mode - report the true physical position
mjr 17:ab3cec0c8bf4 3195 z = znew;
mjr 6:cc35eb643e8f 3196 }
mjr 17:ab3cec0c8bf4 3197
mjr 17:ab3cec0c8bf4 3198 // shift the new reading into the recent history buffer
mjr 6:cc35eb643e8f 3199 z2 = z1;
mjr 6:cc35eb643e8f 3200 z1 = z0;
mjr 6:cc35eb643e8f 3201 z0 = znew;
mjr 2:c174f9ee414a 3202 }
mjr 6:cc35eb643e8f 3203
mjr 38:091e511ce8a0 3204 // process button updates
mjr 38:091e511ce8a0 3205 processButtons();
mjr 37:ed52738445fc 3206
mjr 38:091e511ce8a0 3207 // send a keyboard report if we have new data
mjr 37:ed52738445fc 3208 if (kbState.changed)
mjr 37:ed52738445fc 3209 {
mjr 38:091e511ce8a0 3210 // send a keyboard report
mjr 37:ed52738445fc 3211 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 3212 kbState.changed = false;
mjr 37:ed52738445fc 3213 }
mjr 38:091e511ce8a0 3214
mjr 38:091e511ce8a0 3215 // likewise for the media controller
mjr 37:ed52738445fc 3216 if (mediaState.changed)
mjr 37:ed52738445fc 3217 {
mjr 38:091e511ce8a0 3218 // send a media report
mjr 37:ed52738445fc 3219 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 3220 mediaState.changed = false;
mjr 37:ed52738445fc 3221 }
mjr 38:091e511ce8a0 3222
mjr 38:091e511ce8a0 3223 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 3224 bool jsOK = false;
mjr 17:ab3cec0c8bf4 3225
mjr 17:ab3cec0c8bf4 3226 // If it's been long enough since our last USB status report,
mjr 17:ab3cec0c8bf4 3227 // send the new report. We throttle the report rate because
mjr 17:ab3cec0c8bf4 3228 // it can overwhelm the PC side if we report too frequently.
mjr 17:ab3cec0c8bf4 3229 // VP only wants to sync with the real world in 10ms intervals,
mjr 35:e959ffba78fd 3230 // so reporting more frequently creates I/O overhead without
mjr 35:e959ffba78fd 3231 // doing anything to improve the simulation.
mjr 38:091e511ce8a0 3232 if (cfg.joystickEnabled && jsReportTimer.read_ms() > 10)
mjr 17:ab3cec0c8bf4 3233 {
mjr 17:ab3cec0c8bf4 3234 // read the accelerometer
mjr 17:ab3cec0c8bf4 3235 int xa, ya;
mjr 17:ab3cec0c8bf4 3236 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 3237
mjr 17:ab3cec0c8bf4 3238 // confine the results to our joystick axis range
mjr 17:ab3cec0c8bf4 3239 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 17:ab3cec0c8bf4 3240 if (xa > JOYMAX) xa = JOYMAX;
mjr 17:ab3cec0c8bf4 3241 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 17:ab3cec0c8bf4 3242 if (ya > JOYMAX) ya = JOYMAX;
mjr 17:ab3cec0c8bf4 3243
mjr 17:ab3cec0c8bf4 3244 // store the updated accelerometer coordinates
mjr 17:ab3cec0c8bf4 3245 x = xa;
mjr 17:ab3cec0c8bf4 3246 y = ya;
mjr 17:ab3cec0c8bf4 3247
mjr 21:5048e16cc9ef 3248 // Report the current plunger position UNLESS the ZB Launch Ball
mjr 21:5048e16cc9ef 3249 // signal is on, in which case just report a constant 0 value.
mjr 21:5048e16cc9ef 3250 // ZB Launch Ball turns off the plunger position because it
mjr 21:5048e16cc9ef 3251 // tells us that the table has a Launch Ball button instead of
mjr 21:5048e16cc9ef 3252 // a traditional plunger.
mjr 35:e959ffba78fd 3253 int zrep = (cfg.plunger.zbLaunchBall.port != 0 && wizOn[cfg.plunger.zbLaunchBall.port-1] ? 0 : z);
mjr 35:e959ffba78fd 3254
mjr 35:e959ffba78fd 3255 // rotate X and Y according to the device orientation in the cabinet
mjr 35:e959ffba78fd 3256 accelRotate(x, y);
mjr 35:e959ffba78fd 3257
mjr 35:e959ffba78fd 3258 // send the joystick report
mjr 38:091e511ce8a0 3259 jsOK = js.update(x, y, zrep, jsButtons | simButtons, statusFlags);
mjr 21:5048e16cc9ef 3260
mjr 17:ab3cec0c8bf4 3261 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 3262 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 3263 }
mjr 21:5048e16cc9ef 3264
mjr 10:976666ffa4ef 3265 // If we're in pixel dump mode, report all pixel exposure values
mjr 10:976666ffa4ef 3266 if (reportPix)
mjr 10:976666ffa4ef 3267 {
mjr 17:ab3cec0c8bf4 3268 // send the report
mjr 35:e959ffba78fd 3269 plungerSensor->sendExposureReport(js);
mjr 17:ab3cec0c8bf4 3270
mjr 10:976666ffa4ef 3271 // we have satisfied this request
mjr 10:976666ffa4ef 3272 reportPix = false;
mjr 10:976666ffa4ef 3273 }
mjr 10:976666ffa4ef 3274
mjr 35:e959ffba78fd 3275 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 3276 // periodically for the sake of the Windows config tool.
mjr 38:091e511ce8a0 3277 if (!cfg.joystickEnabled && jsReportTimer.read_ms() > 200)
mjr 21:5048e16cc9ef 3278 {
mjr 38:091e511ce8a0 3279 jsOK = js.updateStatus(0);
mjr 38:091e511ce8a0 3280 jsReportTimer.reset();
mjr 38:091e511ce8a0 3281 }
mjr 38:091e511ce8a0 3282
mjr 38:091e511ce8a0 3283 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 3284 if (jsOK)
mjr 38:091e511ce8a0 3285 {
mjr 38:091e511ce8a0 3286 jsOKTimer.reset();
mjr 38:091e511ce8a0 3287 jsOKTimer.start();
mjr 21:5048e16cc9ef 3288 }
mjr 21:5048e16cc9ef 3289
mjr 6:cc35eb643e8f 3290 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 3291 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 3292 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 3293 #endif
mjr 6:cc35eb643e8f 3294
mjr 33:d832bcab089e 3295 // check for connection status changes
mjr 33:d832bcab089e 3296 int newConnected = js.isConnected() && !js.isSuspended();
mjr 33:d832bcab089e 3297 if (newConnected != connected)
mjr 33:d832bcab089e 3298 {
mjr 33:d832bcab089e 3299 // give it a few seconds to stabilize
mjr 33:d832bcab089e 3300 time_t tc = time(0);
mjr 33:d832bcab089e 3301 if (tc - connectChangeTime > 3)
mjr 33:d832bcab089e 3302 {
mjr 33:d832bcab089e 3303 // note the new status
mjr 33:d832bcab089e 3304 connected = newConnected;
mjr 33:d832bcab089e 3305 connectChangeTime = tc;
mjr 33:d832bcab089e 3306
mjr 33:d832bcab089e 3307 // if we're no longer connected, turn off all outputs
mjr 33:d832bcab089e 3308 if (!connected)
mjr 33:d832bcab089e 3309 allOutputsOff();
mjr 33:d832bcab089e 3310 }
mjr 33:d832bcab089e 3311 }
mjr 38:091e511ce8a0 3312
mjr 6:cc35eb643e8f 3313 // provide a visual status indication on the on-board LED
mjr 5:a70c0bce770d 3314 if (calBtnState < 2 && hbTimer.read_ms() > 1000)
mjr 1:d913e0afb2ac 3315 {
mjr 33:d832bcab089e 3316 if (!newConnected)
mjr 2:c174f9ee414a 3317 {
mjr 5:a70c0bce770d 3318 // suspended - turn off the LED
mjr 38:091e511ce8a0 3319 diagLED(0, 0, 0);
mjr 5:a70c0bce770d 3320
mjr 5:a70c0bce770d 3321 // show a status flash every so often
mjr 5:a70c0bce770d 3322 if (hbcnt % 3 == 0)
mjr 5:a70c0bce770d 3323 {
mjr 38:091e511ce8a0 3324 // disconnected = short red/red flash
mjr 38:091e511ce8a0 3325 // suspended = short red flash
mjr 5:a70c0bce770d 3326 for (int n = js.isConnected() ? 1 : 2 ; n > 0 ; --n)
mjr 5:a70c0bce770d 3327 {
mjr 38:091e511ce8a0 3328 diagLED(1, 0, 0);
mjr 5:a70c0bce770d 3329 wait(0.05);
mjr 38:091e511ce8a0 3330 diagLED(0, 0, 0);
mjr 5:a70c0bce770d 3331 wait(0.25);
mjr 5:a70c0bce770d 3332 }
mjr 5:a70c0bce770d 3333 }
mjr 2:c174f9ee414a 3334 }
mjr 38:091e511ce8a0 3335 else if (jsOKTimer.read() > 5)
mjr 38:091e511ce8a0 3336 {
mjr 39:b3815a1c3802 3337 // USB freeze - show red/yellow.
mjr 39:b3815a1c3802 3338 // Our outgoing joystick messages aren't going through, even though we
mjr 39:b3815a1c3802 3339 // think we're still connected. This indicates that one or more of our
mjr 39:b3815a1c3802 3340 // USB endpoints have stopped working, which can happen as a result of
mjr 39:b3815a1c3802 3341 // bugs in the USB HAL or latency responding to a USB IRQ. Show a
mjr 39:b3815a1c3802 3342 // distinctive diagnostic flash to signal the error. I haven't found a
mjr 39:b3815a1c3802 3343 // way to recover from this class of error other than rebooting the MCU,
mjr 39:b3815a1c3802 3344 // so the goal is to fix the HAL so that this error never happens. This
mjr 39:b3815a1c3802 3345 // flash pattern is thus for debugging purposes only; hopefully it won't
mjr 39:b3815a1c3802 3346 // ever occur in a real installation.
mjr 38:091e511ce8a0 3347 static bool dumped;
mjr 38:091e511ce8a0 3348 if (!dumped) {
mjr 39:b3815a1c3802 3349 // If we haven't already, dump the USB HAL status to the debug console,
mjr 39:b3815a1c3802 3350 // in case it helps identify the reason for the endpoint failure.
mjr 38:091e511ce8a0 3351 extern void USBDeviceStatusDump(void);
mjr 38:091e511ce8a0 3352 USBDeviceStatusDump();
mjr 38:091e511ce8a0 3353 dumped = true;
mjr 38:091e511ce8a0 3354 }
mjr 38:091e511ce8a0 3355 jsOKTimer.stop();
mjr 38:091e511ce8a0 3356 hb = !hb;
mjr 38:091e511ce8a0 3357 diagLED(1, hb, 0);
mjr 38:091e511ce8a0 3358 }
mjr 35:e959ffba78fd 3359 else if (cfg.plunger.enabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 3360 {
mjr 6:cc35eb643e8f 3361 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 3362 hb = !hb;
mjr 38:091e511ce8a0 3363 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 3364 }
mjr 6:cc35eb643e8f 3365 else
mjr 6:cc35eb643e8f 3366 {
mjr 6:cc35eb643e8f 3367 // connected - flash blue/green
mjr 2:c174f9ee414a 3368 hb = !hb;
mjr 38:091e511ce8a0 3369 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 3370 }
mjr 1:d913e0afb2ac 3371
mjr 1:d913e0afb2ac 3372 // reset the heartbeat timer
mjr 1:d913e0afb2ac 3373 hbTimer.reset();
mjr 5:a70c0bce770d 3374 ++hbcnt;
mjr 1:d913e0afb2ac 3375 }
mjr 1:d913e0afb2ac 3376 }
mjr 0:5acbbe3f4cf4 3377 }