Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Tue Jan 05 05:23:07 2016 +0000
Revision:
38:091e511ce8a0
Parent:
37:ed52738445fc
Child:
39:b3815a1c3802
USB improvements

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 35:e959ffba78fd 1066 }
mjr 35:e959ffba78fd 1067 }
mjr 11:bd9da7088e6e 1068 }
mjr 12:669df364a565 1069
mjr 38:091e511ce8a0 1070 // start the button scan thread
mjr 38:091e511ce8a0 1071 buttonTicker.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 1072
mjr 38:091e511ce8a0 1073 // start the button state transition timer
mjr 12:669df364a565 1074 buttonTimer.start();
mjr 11:bd9da7088e6e 1075 }
mjr 11:bd9da7088e6e 1076
mjr 38:091e511ce8a0 1077 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 1078 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 1079 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 1080 // mapped to special device functions (e.g., Night Mode).
mjr 38:091e511ce8a0 1081 void processButtons()
mjr 35:e959ffba78fd 1082 {
mjr 35:e959ffba78fd 1083 // start with an empty list of USB key codes
mjr 35:e959ffba78fd 1084 uint8_t modkeys = 0;
mjr 35:e959ffba78fd 1085 uint8_t keys[7] = { 0, 0, 0, 0, 0, 0, 0 };
mjr 35:e959ffba78fd 1086 int nkeys = 0;
mjr 11:bd9da7088e6e 1087
mjr 35:e959ffba78fd 1088 // clear the joystick buttons
mjr 36:b9747461331e 1089 uint32_t newjs = 0;
mjr 35:e959ffba78fd 1090
mjr 35:e959ffba78fd 1091 // start with no media keys pressed
mjr 35:e959ffba78fd 1092 uint8_t mediakeys = 0;
mjr 38:091e511ce8a0 1093
mjr 38:091e511ce8a0 1094 // calculate the time since the last run
mjr 35:e959ffba78fd 1095 float dt = buttonTimer.read();
mjr 18:5e890ebd0023 1096 buttonTimer.reset();
mjr 38:091e511ce8a0 1097
mjr 11:bd9da7088e6e 1098 // scan the button list
mjr 18:5e890ebd0023 1099 ButtonState *bs = buttonState;
mjr 35:e959ffba78fd 1100 for (int i = 0 ; i < MAX_BUTTONS ; ++i, ++bs)
mjr 11:bd9da7088e6e 1101 {
mjr 38:091e511ce8a0 1102 // if it's a pulse-mode switch, get the virtual pressed state
mjr 38:091e511ce8a0 1103 if (bs->pulseState != 0)
mjr 18:5e890ebd0023 1104 {
mjr 38:091e511ce8a0 1105 // deduct the time to the next state change
mjr 38:091e511ce8a0 1106 bs->pulseTime -= dt;
mjr 38:091e511ce8a0 1107 if (bs->pulseTime < 0)
mjr 38:091e511ce8a0 1108 bs->pulseTime = 0;
mjr 38:091e511ce8a0 1109
mjr 38:091e511ce8a0 1110 // if the timer has expired, check for state changes
mjr 38:091e511ce8a0 1111 if (bs->pulseTime == 0)
mjr 18:5e890ebd0023 1112 {
mjr 38:091e511ce8a0 1113 const float pulseLength = 0.2;
mjr 38:091e511ce8a0 1114 switch (bs->pulseState)
mjr 18:5e890ebd0023 1115 {
mjr 38:091e511ce8a0 1116 case 1:
mjr 38:091e511ce8a0 1117 // off - if the physical switch is now on, start a button pulse
mjr 38:091e511ce8a0 1118 if (bs->on) {
mjr 38:091e511ce8a0 1119 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1120 bs->pulseState = 2;
mjr 38:091e511ce8a0 1121 bs->pressed = 1;
mjr 38:091e511ce8a0 1122 }
mjr 38:091e511ce8a0 1123 break;
mjr 18:5e890ebd0023 1124
mjr 38:091e511ce8a0 1125 case 2:
mjr 38:091e511ce8a0 1126 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 1127 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 1128 // change in state in the logical button
mjr 38:091e511ce8a0 1129 bs->pulseState = 3;
mjr 38:091e511ce8a0 1130 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1131 bs->pressed = 0;
mjr 38:091e511ce8a0 1132 break;
mjr 38:091e511ce8a0 1133
mjr 38:091e511ce8a0 1134 case 3:
mjr 38:091e511ce8a0 1135 // on - if the physical switch is now off, start a button pulse
mjr 38:091e511ce8a0 1136 if (!bs->on) {
mjr 38:091e511ce8a0 1137 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1138 bs->pulseState = 4;
mjr 38:091e511ce8a0 1139 bs->pressed = 1;
mjr 38:091e511ce8a0 1140 }
mjr 38:091e511ce8a0 1141 break;
mjr 38:091e511ce8a0 1142
mjr 38:091e511ce8a0 1143 case 4:
mjr 38:091e511ce8a0 1144 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 1145 bs->pulseState = 1;
mjr 38:091e511ce8a0 1146 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1147 bs->pressed = 0;
mjr 38:091e511ce8a0 1148 break;
mjr 18:5e890ebd0023 1149 }
mjr 18:5e890ebd0023 1150 }
mjr 38:091e511ce8a0 1151 }
mjr 38:091e511ce8a0 1152 else
mjr 38:091e511ce8a0 1153 {
mjr 38:091e511ce8a0 1154 // not a pulse switch - the logical state is the same as the physical state
mjr 38:091e511ce8a0 1155 bs->pressed = bs->on;
mjr 38:091e511ce8a0 1156 }
mjr 35:e959ffba78fd 1157
mjr 38:091e511ce8a0 1158 // carry out any edge effects from buttons changing states
mjr 38:091e511ce8a0 1159 if (bs->pressed != bs->prev)
mjr 38:091e511ce8a0 1160 {
mjr 38:091e511ce8a0 1161 // check for special key transitions
mjr 38:091e511ce8a0 1162 switch (bs->special)
mjr 35:e959ffba78fd 1163 {
mjr 38:091e511ce8a0 1164 case 1:
mjr 38:091e511ce8a0 1165 // night mode momentary switch - when the button transitions from
mjr 38:091e511ce8a0 1166 // OFF to ON, invert night mode
mjr 38:091e511ce8a0 1167 if (bs->pressed)
mjr 38:091e511ce8a0 1168 toggleNightMode();
mjr 38:091e511ce8a0 1169 break;
mjr 35:e959ffba78fd 1170
mjr 38:091e511ce8a0 1171 case 2:
mjr 38:091e511ce8a0 1172 // night mode toggle switch - when the button changes state, change
mjr 38:091e511ce8a0 1173 // night mode to match the new state
mjr 38:091e511ce8a0 1174 setNightMode(bs->pressed);
mjr 38:091e511ce8a0 1175 break;
mjr 35:e959ffba78fd 1176 }
mjr 38:091e511ce8a0 1177
mjr 38:091e511ce8a0 1178 // remember the new state for comparison on the next run
mjr 38:091e511ce8a0 1179 bs->prev = bs->pressed;
mjr 38:091e511ce8a0 1180 }
mjr 38:091e511ce8a0 1181
mjr 38:091e511ce8a0 1182 // if it's pressed, add it to the appropriate key state list
mjr 38:091e511ce8a0 1183 if (bs->pressed)
mjr 38:091e511ce8a0 1184 {
mjr 38:091e511ce8a0 1185 // OR in the joystick button bit, mod key bits, and media key bits
mjr 38:091e511ce8a0 1186 newjs |= bs->js;
mjr 38:091e511ce8a0 1187 modkeys |= bs->keymod;
mjr 38:091e511ce8a0 1188 mediakeys |= bs->mediakey;
mjr 38:091e511ce8a0 1189
mjr 38:091e511ce8a0 1190 // if it has a keyboard key, add the scan code to the active list
mjr 38:091e511ce8a0 1191 if (bs->keycode != 0 && nkeys < 7)
mjr 38:091e511ce8a0 1192 keys[nkeys++] = bs->keycode;
mjr 18:5e890ebd0023 1193 }
mjr 11:bd9da7088e6e 1194 }
mjr 36:b9747461331e 1195
mjr 36:b9747461331e 1196 // check for joystick button changes
mjr 36:b9747461331e 1197 if (jsButtons != newjs)
mjr 36:b9747461331e 1198 jsButtons = newjs;
mjr 11:bd9da7088e6e 1199
mjr 35:e959ffba78fd 1200 // Check for changes to the keyboard keys
mjr 35:e959ffba78fd 1201 if (kbState.data[0] != modkeys
mjr 35:e959ffba78fd 1202 || kbState.nkeys != nkeys
mjr 35:e959ffba78fd 1203 || memcmp(keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 1204 {
mjr 35:e959ffba78fd 1205 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 1206 kbState.changed = true;
mjr 35:e959ffba78fd 1207 kbState.data[0] = modkeys;
mjr 35:e959ffba78fd 1208 if (nkeys <= 6) {
mjr 35:e959ffba78fd 1209 // 6 or fewer simultaneous keys - report the key codes
mjr 35:e959ffba78fd 1210 kbState.nkeys = nkeys;
mjr 35:e959ffba78fd 1211 memcpy(&kbState.data[2], keys, 6);
mjr 35:e959ffba78fd 1212 }
mjr 35:e959ffba78fd 1213 else {
mjr 35:e959ffba78fd 1214 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 1215 kbState.nkeys = 6;
mjr 35:e959ffba78fd 1216 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 1217 }
mjr 35:e959ffba78fd 1218 }
mjr 35:e959ffba78fd 1219
mjr 35:e959ffba78fd 1220 // Check for changes to media keys
mjr 35:e959ffba78fd 1221 if (mediaState.data != mediakeys)
mjr 35:e959ffba78fd 1222 {
mjr 35:e959ffba78fd 1223 mediaState.changed = true;
mjr 35:e959ffba78fd 1224 mediaState.data = mediakeys;
mjr 35:e959ffba78fd 1225 }
mjr 11:bd9da7088e6e 1226 }
mjr 11:bd9da7088e6e 1227
mjr 5:a70c0bce770d 1228 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1229 //
mjr 5:a70c0bce770d 1230 // Customization joystick subbclass
mjr 5:a70c0bce770d 1231 //
mjr 5:a70c0bce770d 1232
mjr 5:a70c0bce770d 1233 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 1234 {
mjr 5:a70c0bce770d 1235 public:
mjr 35:e959ffba78fd 1236 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 35:e959ffba78fd 1237 bool waitForConnect, bool enableJoystick, bool useKB)
mjr 35:e959ffba78fd 1238 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, useKB)
mjr 5:a70c0bce770d 1239 {
mjr 5:a70c0bce770d 1240 suspended_ = false;
mjr 5:a70c0bce770d 1241 }
mjr 5:a70c0bce770d 1242
mjr 5:a70c0bce770d 1243 // are we connected?
mjr 5:a70c0bce770d 1244 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 1245
mjr 5:a70c0bce770d 1246 // Are we in suspend mode?
mjr 5:a70c0bce770d 1247 int isSuspended() const { return suspended_; }
mjr 5:a70c0bce770d 1248
mjr 5:a70c0bce770d 1249 protected:
mjr 5:a70c0bce770d 1250 virtual void suspendStateChanged(unsigned int suspended)
mjr 5:a70c0bce770d 1251 { suspended_ = suspended; }
mjr 5:a70c0bce770d 1252
mjr 5:a70c0bce770d 1253 // are we suspended?
mjr 5:a70c0bce770d 1254 int suspended_;
mjr 5:a70c0bce770d 1255 };
mjr 5:a70c0bce770d 1256
mjr 5:a70c0bce770d 1257 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1258 //
mjr 5:a70c0bce770d 1259 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 1260 //
mjr 5:a70c0bce770d 1261
mjr 5:a70c0bce770d 1262 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 1263 //
mjr 5:a70c0bce770d 1264 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 1265 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 1266 // automatic calibration.
mjr 5:a70c0bce770d 1267 //
mjr 5:a70c0bce770d 1268 // We install an interrupt handler on the accelerometer "data ready"
mjr 6:cc35eb643e8f 1269 // interrupt to ensure that we fetch each sample immediately when it
mjr 6:cc35eb643e8f 1270 // becomes available. The accelerometer data rate is fiarly high
mjr 6:cc35eb643e8f 1271 // (800 Hz), so it's not practical to keep up with it by polling.
mjr 6:cc35eb643e8f 1272 // Using an interrupt handler lets us respond quickly and read
mjr 6:cc35eb643e8f 1273 // every sample.
mjr 5:a70c0bce770d 1274 //
mjr 6:cc35eb643e8f 1275 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 1276 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 1277 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 1278 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 1279 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 1280 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 1281 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 1282 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 1283 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 1284 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 1285 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 1286 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 1287 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 1288 // of nudging, say).
mjr 5:a70c0bce770d 1289 //
mjr 5:a70c0bce770d 1290
mjr 17:ab3cec0c8bf4 1291 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 1292 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 1293
mjr 17:ab3cec0c8bf4 1294 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 1295 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 1296 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 1297
mjr 17:ab3cec0c8bf4 1298 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 1299 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 1300 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 1301 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 1302
mjr 17:ab3cec0c8bf4 1303
mjr 6:cc35eb643e8f 1304 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 1305 struct AccHist
mjr 5:a70c0bce770d 1306 {
mjr 6:cc35eb643e8f 1307 AccHist() { x = y = d = 0.0; xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 1308 void set(float x, float y, AccHist *prv)
mjr 6:cc35eb643e8f 1309 {
mjr 6:cc35eb643e8f 1310 // save the raw position
mjr 6:cc35eb643e8f 1311 this->x = x;
mjr 6:cc35eb643e8f 1312 this->y = y;
mjr 6:cc35eb643e8f 1313 this->d = distance(prv);
mjr 6:cc35eb643e8f 1314 }
mjr 6:cc35eb643e8f 1315
mjr 6:cc35eb643e8f 1316 // reading for this entry
mjr 5:a70c0bce770d 1317 float x, y;
mjr 5:a70c0bce770d 1318
mjr 6:cc35eb643e8f 1319 // distance from previous entry
mjr 6:cc35eb643e8f 1320 float d;
mjr 5:a70c0bce770d 1321
mjr 6:cc35eb643e8f 1322 // total and count of samples averaged over this period
mjr 6:cc35eb643e8f 1323 float xtot, ytot;
mjr 6:cc35eb643e8f 1324 int cnt;
mjr 6:cc35eb643e8f 1325
mjr 6:cc35eb643e8f 1326 void clearAvg() { xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 1327 void addAvg(float x, float y) { xtot += x; ytot += y; ++cnt; }
mjr 6:cc35eb643e8f 1328 float xAvg() const { return xtot/cnt; }
mjr 6:cc35eb643e8f 1329 float yAvg() const { return ytot/cnt; }
mjr 5:a70c0bce770d 1330
mjr 6:cc35eb643e8f 1331 float distance(AccHist *p)
mjr 6:cc35eb643e8f 1332 { return sqrt(square(p->x - x) + square(p->y - y)); }
mjr 5:a70c0bce770d 1333 };
mjr 5:a70c0bce770d 1334
mjr 5:a70c0bce770d 1335 // accelerometer wrapper class
mjr 3:3514575d4f86 1336 class Accel
mjr 3:3514575d4f86 1337 {
mjr 3:3514575d4f86 1338 public:
mjr 3:3514575d4f86 1339 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin)
mjr 3:3514575d4f86 1340 : mma_(sda, scl, i2cAddr), intIn_(irqPin)
mjr 3:3514575d4f86 1341 {
mjr 5:a70c0bce770d 1342 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 1343 irqPin_ = irqPin;
mjr 5:a70c0bce770d 1344
mjr 5:a70c0bce770d 1345 // reset and initialize
mjr 5:a70c0bce770d 1346 reset();
mjr 5:a70c0bce770d 1347 }
mjr 5:a70c0bce770d 1348
mjr 5:a70c0bce770d 1349 void reset()
mjr 5:a70c0bce770d 1350 {
mjr 6:cc35eb643e8f 1351 // clear the center point
mjr 6:cc35eb643e8f 1352 cx_ = cy_ = 0.0;
mjr 6:cc35eb643e8f 1353
mjr 6:cc35eb643e8f 1354 // start the calibration timer
mjr 5:a70c0bce770d 1355 tCenter_.start();
mjr 5:a70c0bce770d 1356 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 1357
mjr 5:a70c0bce770d 1358 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 1359 mma_.init();
mjr 6:cc35eb643e8f 1360
mjr 6:cc35eb643e8f 1361 // set the initial integrated velocity reading to zero
mjr 6:cc35eb643e8f 1362 vx_ = vy_ = 0;
mjr 3:3514575d4f86 1363
mjr 6:cc35eb643e8f 1364 // set up our accelerometer interrupt handling
mjr 6:cc35eb643e8f 1365 intIn_.rise(this, &Accel::isr);
mjr 5:a70c0bce770d 1366 mma_.setInterruptMode(irqPin_ == PTA14 ? 1 : 2);
mjr 3:3514575d4f86 1367
mjr 3:3514575d4f86 1368 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 1369 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 1370
mjr 3:3514575d4f86 1371 // start our timers
mjr 3:3514575d4f86 1372 tGet_.start();
mjr 3:3514575d4f86 1373 tInt_.start();
mjr 3:3514575d4f86 1374 }
mjr 3:3514575d4f86 1375
mjr 9:fd65b0a94720 1376 void get(int &x, int &y)
mjr 3:3514575d4f86 1377 {
mjr 3:3514575d4f86 1378 // disable interrupts while manipulating the shared data
mjr 3:3514575d4f86 1379 __disable_irq();
mjr 3:3514575d4f86 1380
mjr 3:3514575d4f86 1381 // read the shared data and store locally for calculations
mjr 6:cc35eb643e8f 1382 float ax = ax_, ay = ay_;
mjr 6:cc35eb643e8f 1383 float vx = vx_, vy = vy_;
mjr 5:a70c0bce770d 1384
mjr 6:cc35eb643e8f 1385 // reset the velocity sum for the next run
mjr 6:cc35eb643e8f 1386 vx_ = vy_ = 0;
mjr 3:3514575d4f86 1387
mjr 3:3514575d4f86 1388 // get the time since the last get() sample
mjr 38:091e511ce8a0 1389 float dt = tGet_.read_us()/1.0e6f;
mjr 3:3514575d4f86 1390 tGet_.reset();
mjr 3:3514575d4f86 1391
mjr 3:3514575d4f86 1392 // done manipulating the shared data
mjr 3:3514575d4f86 1393 __enable_irq();
mjr 3:3514575d4f86 1394
mjr 6:cc35eb643e8f 1395 // adjust the readings for the integration time
mjr 6:cc35eb643e8f 1396 vx /= dt;
mjr 6:cc35eb643e8f 1397 vy /= dt;
mjr 6:cc35eb643e8f 1398
mjr 6:cc35eb643e8f 1399 // add this sample to the current calibration interval's running total
mjr 6:cc35eb643e8f 1400 AccHist *p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 1401 p->addAvg(ax, ay);
mjr 6:cc35eb643e8f 1402
mjr 5:a70c0bce770d 1403 // check for auto-centering every so often
mjr 5:a70c0bce770d 1404 if (tCenter_.read_ms() > 1000)
mjr 5:a70c0bce770d 1405 {
mjr 5:a70c0bce770d 1406 // add the latest raw sample to the history list
mjr 6:cc35eb643e8f 1407 AccHist *prv = p;
mjr 5:a70c0bce770d 1408 iAccPrv_ = (iAccPrv_ + 1) % maxAccPrv;
mjr 6:cc35eb643e8f 1409 p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 1410 p->set(ax, ay, prv);
mjr 5:a70c0bce770d 1411
mjr 5:a70c0bce770d 1412 // if we have a full complement, check for stability
mjr 5:a70c0bce770d 1413 if (nAccPrv_ >= maxAccPrv)
mjr 5:a70c0bce770d 1414 {
mjr 5:a70c0bce770d 1415 // check if we've been stable for all recent samples
mjr 6:cc35eb643e8f 1416 static const float accTol = .01;
mjr 6:cc35eb643e8f 1417 AccHist *p0 = accPrv_;
mjr 6:cc35eb643e8f 1418 if (p0[0].d < accTol
mjr 6:cc35eb643e8f 1419 && p0[1].d < accTol
mjr 6:cc35eb643e8f 1420 && p0[2].d < accTol
mjr 6:cc35eb643e8f 1421 && p0[3].d < accTol
mjr 6:cc35eb643e8f 1422 && p0[4].d < accTol)
mjr 5:a70c0bce770d 1423 {
mjr 6:cc35eb643e8f 1424 // Figure the new calibration point as the average of
mjr 6:cc35eb643e8f 1425 // the samples over the rest period
mjr 6:cc35eb643e8f 1426 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5.0;
mjr 6:cc35eb643e8f 1427 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5.0;
mjr 5:a70c0bce770d 1428 }
mjr 5:a70c0bce770d 1429 }
mjr 5:a70c0bce770d 1430 else
mjr 5:a70c0bce770d 1431 {
mjr 5:a70c0bce770d 1432 // not enough samples yet; just up the count
mjr 5:a70c0bce770d 1433 ++nAccPrv_;
mjr 5:a70c0bce770d 1434 }
mjr 6:cc35eb643e8f 1435
mjr 6:cc35eb643e8f 1436 // clear the new item's running totals
mjr 6:cc35eb643e8f 1437 p->clearAvg();
mjr 5:a70c0bce770d 1438
mjr 5:a70c0bce770d 1439 // reset the timer
mjr 5:a70c0bce770d 1440 tCenter_.reset();
mjr 5:a70c0bce770d 1441 }
mjr 5:a70c0bce770d 1442
mjr 6:cc35eb643e8f 1443 // report our integrated velocity reading in x,y
mjr 6:cc35eb643e8f 1444 x = rawToReport(vx);
mjr 6:cc35eb643e8f 1445 y = rawToReport(vy);
mjr 5:a70c0bce770d 1446
mjr 6:cc35eb643e8f 1447 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 1448 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 1449 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 1450 #endif
mjr 3:3514575d4f86 1451 }
mjr 29:582472d0bc57 1452
mjr 3:3514575d4f86 1453 private:
mjr 6:cc35eb643e8f 1454 // adjust a raw acceleration figure to a usb report value
mjr 6:cc35eb643e8f 1455 int rawToReport(float v)
mjr 5:a70c0bce770d 1456 {
mjr 6:cc35eb643e8f 1457 // scale to the joystick report range and round to integer
mjr 6:cc35eb643e8f 1458 int i = int(round(v*JOYMAX));
mjr 5:a70c0bce770d 1459
mjr 6:cc35eb643e8f 1460 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 1461 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 1462 static const int filter[] = {
mjr 6:cc35eb643e8f 1463 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 1464 0,
mjr 6:cc35eb643e8f 1465 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 1466 };
mjr 6:cc35eb643e8f 1467 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 1468 }
mjr 5:a70c0bce770d 1469
mjr 3:3514575d4f86 1470 // interrupt handler
mjr 3:3514575d4f86 1471 void isr()
mjr 3:3514575d4f86 1472 {
mjr 3:3514575d4f86 1473 // Read the axes. Note that we have to read all three axes
mjr 3:3514575d4f86 1474 // (even though we only really use x and y) in order to clear
mjr 3:3514575d4f86 1475 // the "data ready" status bit in the accelerometer. The
mjr 3:3514575d4f86 1476 // interrupt only occurs when the "ready" bit transitions from
mjr 3:3514575d4f86 1477 // off to on, so we have to make sure it's off.
mjr 5:a70c0bce770d 1478 float x, y, z;
mjr 5:a70c0bce770d 1479 mma_.getAccXYZ(x, y, z);
mjr 3:3514575d4f86 1480
mjr 3:3514575d4f86 1481 // calculate the time since the last interrupt
mjr 3:3514575d4f86 1482 float dt = tInt_.read_us()/1.0e6;
mjr 3:3514575d4f86 1483 tInt_.reset();
mjr 6:cc35eb643e8f 1484
mjr 6:cc35eb643e8f 1485 // integrate the time slice from the previous reading to this reading
mjr 6:cc35eb643e8f 1486 vx_ += (x + ax_ - 2*cx_)*dt/2;
mjr 6:cc35eb643e8f 1487 vy_ += (y + ay_ - 2*cy_)*dt/2;
mjr 3:3514575d4f86 1488
mjr 6:cc35eb643e8f 1489 // store the updates
mjr 6:cc35eb643e8f 1490 ax_ = x;
mjr 6:cc35eb643e8f 1491 ay_ = y;
mjr 6:cc35eb643e8f 1492 az_ = z;
mjr 3:3514575d4f86 1493 }
mjr 3:3514575d4f86 1494
mjr 3:3514575d4f86 1495 // underlying accelerometer object
mjr 3:3514575d4f86 1496 MMA8451Q mma_;
mjr 3:3514575d4f86 1497
mjr 5:a70c0bce770d 1498 // last raw acceleration readings
mjr 6:cc35eb643e8f 1499 float ax_, ay_, az_;
mjr 5:a70c0bce770d 1500
mjr 6:cc35eb643e8f 1501 // integrated velocity reading since last get()
mjr 6:cc35eb643e8f 1502 float vx_, vy_;
mjr 6:cc35eb643e8f 1503
mjr 3:3514575d4f86 1504 // timer for measuring time between get() samples
mjr 3:3514575d4f86 1505 Timer tGet_;
mjr 3:3514575d4f86 1506
mjr 3:3514575d4f86 1507 // timer for measuring time between interrupts
mjr 3:3514575d4f86 1508 Timer tInt_;
mjr 5:a70c0bce770d 1509
mjr 6:cc35eb643e8f 1510 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 1511 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 1512 // at rest.
mjr 6:cc35eb643e8f 1513 float cx_, cy_;
mjr 5:a70c0bce770d 1514
mjr 5:a70c0bce770d 1515 // timer for atuo-centering
mjr 5:a70c0bce770d 1516 Timer tCenter_;
mjr 6:cc35eb643e8f 1517
mjr 6:cc35eb643e8f 1518 // Auto-centering history. This is a separate history list that
mjr 6:cc35eb643e8f 1519 // records results spaced out sparesely over time, so that we can
mjr 6:cc35eb643e8f 1520 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 1521 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 1522 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 1523 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 1524 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 1525 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 1526 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 1527 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 1528 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 1529 // accelerometer measurement bias (e.g., from temperature changes).
mjr 5:a70c0bce770d 1530 int iAccPrv_, nAccPrv_;
mjr 5:a70c0bce770d 1531 static const int maxAccPrv = 5;
mjr 6:cc35eb643e8f 1532 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 1533
mjr 5:a70c0bce770d 1534 // interurupt pin name
mjr 5:a70c0bce770d 1535 PinName irqPin_;
mjr 5:a70c0bce770d 1536
mjr 5:a70c0bce770d 1537 // interrupt router
mjr 5:a70c0bce770d 1538 InterruptIn intIn_;
mjr 3:3514575d4f86 1539 };
mjr 3:3514575d4f86 1540
mjr 5:a70c0bce770d 1541
mjr 5:a70c0bce770d 1542 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1543 //
mjr 14:df700b22ca08 1544 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 1545 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 1546 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 1547 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 1548 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 1549 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 1550 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 1551 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 1552 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 1553 //
mjr 14:df700b22ca08 1554 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 1555 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 1556 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 1557 //
mjr 5:a70c0bce770d 1558 void clear_i2c()
mjr 5:a70c0bce770d 1559 {
mjr 38:091e511ce8a0 1560 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 1561 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 1562 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 1563
mjr 5:a70c0bce770d 1564 // clock the SCL 9 times
mjr 5:a70c0bce770d 1565 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 1566 {
mjr 5:a70c0bce770d 1567 scl = 1;
mjr 5:a70c0bce770d 1568 wait_us(20);
mjr 5:a70c0bce770d 1569 scl = 0;
mjr 5:a70c0bce770d 1570 wait_us(20);
mjr 5:a70c0bce770d 1571 }
mjr 5:a70c0bce770d 1572 }
mjr 14:df700b22ca08 1573
mjr 14:df700b22ca08 1574 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 1575 //
mjr 33:d832bcab089e 1576 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 1577 // for a given interval before allowing an update.
mjr 33:d832bcab089e 1578 //
mjr 33:d832bcab089e 1579 class Debouncer
mjr 33:d832bcab089e 1580 {
mjr 33:d832bcab089e 1581 public:
mjr 33:d832bcab089e 1582 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 1583 {
mjr 33:d832bcab089e 1584 t.start();
mjr 33:d832bcab089e 1585 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 1586 this->tmin = tmin;
mjr 33:d832bcab089e 1587 }
mjr 33:d832bcab089e 1588
mjr 33:d832bcab089e 1589 // Get the current stable value
mjr 33:d832bcab089e 1590 bool val() const { return stable; }
mjr 33:d832bcab089e 1591
mjr 33:d832bcab089e 1592 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 1593 // input device.
mjr 33:d832bcab089e 1594 void sampleIn(bool val)
mjr 33:d832bcab089e 1595 {
mjr 33:d832bcab089e 1596 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 1597 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 1598 // on the sample reader.
mjr 33:d832bcab089e 1599 if (val != prv)
mjr 33:d832bcab089e 1600 {
mjr 33:d832bcab089e 1601 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 1602 t.reset();
mjr 33:d832bcab089e 1603
mjr 33:d832bcab089e 1604 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 1605 prv = val;
mjr 33:d832bcab089e 1606 }
mjr 33:d832bcab089e 1607 else if (val != stable)
mjr 33:d832bcab089e 1608 {
mjr 33:d832bcab089e 1609 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 1610 // and different from the stable value. This means that
mjr 33:d832bcab089e 1611 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 1612 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 1613 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 1614 if (t.read() > tmin)
mjr 33:d832bcab089e 1615 stable = val;
mjr 33:d832bcab089e 1616 }
mjr 33:d832bcab089e 1617 }
mjr 33:d832bcab089e 1618
mjr 33:d832bcab089e 1619 private:
mjr 33:d832bcab089e 1620 // current stable value
mjr 33:d832bcab089e 1621 bool stable;
mjr 33:d832bcab089e 1622
mjr 33:d832bcab089e 1623 // last raw sample value
mjr 33:d832bcab089e 1624 bool prv;
mjr 33:d832bcab089e 1625
mjr 33:d832bcab089e 1626 // elapsed time since last raw input change
mjr 33:d832bcab089e 1627 Timer t;
mjr 33:d832bcab089e 1628
mjr 33:d832bcab089e 1629 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 1630 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 1631 float tmin;
mjr 33:d832bcab089e 1632 };
mjr 33:d832bcab089e 1633
mjr 33:d832bcab089e 1634
mjr 33:d832bcab089e 1635 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 1636 //
mjr 33:d832bcab089e 1637 // Turn off all outputs and restore everything to the default LedWiz
mjr 33:d832bcab089e 1638 // state. This sets outputs #1-32 to LedWiz profile value 48 (full
mjr 33:d832bcab089e 1639 // brightness) and switch state Off, sets all extended outputs (#33
mjr 33:d832bcab089e 1640 // and above) to zero brightness, and sets the LedWiz flash rate to 2.
mjr 33:d832bcab089e 1641 // This effectively restores the power-on conditions.
mjr 33:d832bcab089e 1642 //
mjr 33:d832bcab089e 1643 void allOutputsOff()
mjr 33:d832bcab089e 1644 {
mjr 33:d832bcab089e 1645 // reset all LedWiz outputs to OFF/48
mjr 35:e959ffba78fd 1646 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 33:d832bcab089e 1647 {
mjr 33:d832bcab089e 1648 outLevel[i] = 0;
mjr 33:d832bcab089e 1649 wizOn[i] = 0;
mjr 33:d832bcab089e 1650 wizVal[i] = 48;
mjr 33:d832bcab089e 1651 lwPin[i]->set(0);
mjr 33:d832bcab089e 1652 }
mjr 33:d832bcab089e 1653
mjr 33:d832bcab089e 1654 // reset all extended outputs (ports >32) to full off (brightness 0)
mjr 33:d832bcab089e 1655 for (int i = 32 ; i < numOutputs ; ++i)
mjr 33:d832bcab089e 1656 {
mjr 33:d832bcab089e 1657 outLevel[i] = 0;
mjr 33:d832bcab089e 1658 lwPin[i]->set(0);
mjr 33:d832bcab089e 1659 }
mjr 33:d832bcab089e 1660
mjr 33:d832bcab089e 1661 // restore default LedWiz flash rate
mjr 33:d832bcab089e 1662 wizSpeed = 2;
mjr 34:6b981a2afab7 1663
mjr 34:6b981a2afab7 1664 // flush changes to hc595, if applicable
mjr 35:e959ffba78fd 1665 if (hc595 != 0)
mjr 35:e959ffba78fd 1666 hc595->update();
mjr 33:d832bcab089e 1667 }
mjr 33:d832bcab089e 1668
mjr 33:d832bcab089e 1669 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 1670 //
mjr 33:d832bcab089e 1671 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 1672 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 1673 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 1674 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 1675 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 1676 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 1677 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 1678 //
mjr 33:d832bcab089e 1679 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 1680 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 1681 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 1682 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 1683 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 1684 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 1685 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 1686 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 1687 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 1688 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 1689 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 1690 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 1691 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 1692 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 1693 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 1694 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 1695 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 1696 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 1697 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 1698 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 1699 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 1700 //
mjr 33:d832bcab089e 1701 // This scheme might seem a little convoluted, but it neatly handles
mjr 33:d832bcab089e 1702 // all of the different cases that can occur:
mjr 33:d832bcab089e 1703 //
mjr 33:d832bcab089e 1704 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 33:d832bcab089e 1705 // so that the PC goes into "Soft Off" mode (ACPI state S5, in Windows
mjr 33:d832bcab089e 1706 // parlance) when the user turns off the cabinet. In this state, the
mjr 33:d832bcab089e 1707 // motherboard supplies power to USB devices, so the KL25Z continues
mjr 33:d832bcab089e 1708 // running without interruption. The latch system lets us monitor
mjr 33:d832bcab089e 1709 // the power state even when we're never rebooted, since the latch
mjr 33:d832bcab089e 1710 // will turn off when PSU2 is off regardless of what the KL25Z is doing.
mjr 33:d832bcab089e 1711 //
mjr 33:d832bcab089e 1712 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 1713 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 1714 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 1715 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 1716 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 1717 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 1718 // will power up before or after PSU2, so it's not good enough to
mjr 33:d832bcab089e 1719 // observe the *current* state of PSU2 when we first check - if PSU2
mjr 33:d832bcab089e 1720 // were to come on first, checking the current state alone would fool
mjr 33:d832bcab089e 1721 // us into thinking that no action is required, because we would never
mjr 33:d832bcab089e 1722 // have known that PSU2 was ever off. The latch handles this case by
mjr 33:d832bcab089e 1723 // letting us see that PSU2 *was* off before we checked.
mjr 33:d832bcab089e 1724 //
mjr 33:d832bcab089e 1725 // - If the KL25Z is rebooted while the main system is running, or the
mjr 33:d832bcab089e 1726 // KL25Z is unplugged and plugged back in, we will correctly leave the
mjr 33:d832bcab089e 1727 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 1728 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 1729 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 1730 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 1731 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 1732 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 1733 //
mjr 33:d832bcab089e 1734 //
mjr 33:d832bcab089e 1735
mjr 33:d832bcab089e 1736 // Current PSU2 state:
mjr 33:d832bcab089e 1737 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 1738 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 1739 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 1740 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 1741 // 5 -> TV relay on
mjr 33:d832bcab089e 1742 //
mjr 33:d832bcab089e 1743 int psu2_state = 1;
mjr 35:e959ffba78fd 1744
mjr 35:e959ffba78fd 1745 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 1746 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 1747 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 1748
mjr 35:e959ffba78fd 1749 // TV ON switch relay control
mjr 35:e959ffba78fd 1750 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 1751
mjr 35:e959ffba78fd 1752 // Timer interrupt
mjr 35:e959ffba78fd 1753 Ticker tv_ticker;
mjr 35:e959ffba78fd 1754 float tv_delay_time;
mjr 33:d832bcab089e 1755 void TVTimerInt()
mjr 33:d832bcab089e 1756 {
mjr 35:e959ffba78fd 1757 // time since last state change
mjr 35:e959ffba78fd 1758 static Timer tv_timer;
mjr 35:e959ffba78fd 1759
mjr 33:d832bcab089e 1760 // Check our internal state
mjr 33:d832bcab089e 1761 switch (psu2_state)
mjr 33:d832bcab089e 1762 {
mjr 33:d832bcab089e 1763 case 1:
mjr 33:d832bcab089e 1764 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 1765 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 1766 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 1767 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 1768 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 1769 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 1770 {
mjr 33:d832bcab089e 1771 // switch to OFF state
mjr 33:d832bcab089e 1772 psu2_state = 2;
mjr 33:d832bcab089e 1773
mjr 33:d832bcab089e 1774 // try setting the latch
mjr 35:e959ffba78fd 1775 psu2_status_set->write(1);
mjr 33:d832bcab089e 1776 }
mjr 33:d832bcab089e 1777 break;
mjr 33:d832bcab089e 1778
mjr 33:d832bcab089e 1779 case 2:
mjr 33:d832bcab089e 1780 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 1781 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 1782 psu2_status_set->write(0);
mjr 33:d832bcab089e 1783 psu2_state = 3;
mjr 33:d832bcab089e 1784 break;
mjr 33:d832bcab089e 1785
mjr 33:d832bcab089e 1786 case 3:
mjr 33:d832bcab089e 1787 // CHECK state: we pulsed SET, and we're now ready to see
mjr 33:d832bcab089e 1788 // if that stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 1789 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 1790 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 1791 if (psu2_status_sense->read())
mjr 33:d832bcab089e 1792 {
mjr 33:d832bcab089e 1793 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 1794 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 1795 tv_timer.reset();
mjr 33:d832bcab089e 1796 tv_timer.start();
mjr 33:d832bcab089e 1797 psu2_state = 4;
mjr 33:d832bcab089e 1798 }
mjr 33:d832bcab089e 1799 else
mjr 33:d832bcab089e 1800 {
mjr 33:d832bcab089e 1801 // The latch didn't stick, so PSU2 was still off at
mjr 33:d832bcab089e 1802 // our last check. Try pulsing it again in case PSU2
mjr 33:d832bcab089e 1803 // was turned on since the last check.
mjr 35:e959ffba78fd 1804 psu2_status_set->write(1);
mjr 33:d832bcab089e 1805 psu2_state = 2;
mjr 33:d832bcab089e 1806 }
mjr 33:d832bcab089e 1807 break;
mjr 33:d832bcab089e 1808
mjr 33:d832bcab089e 1809 case 4:
mjr 33:d832bcab089e 1810 // TV timer countdown in progress. If we've reached the
mjr 33:d832bcab089e 1811 // delay time, pulse the relay.
mjr 35:e959ffba78fd 1812 if (tv_timer.read() >= tv_delay_time)
mjr 33:d832bcab089e 1813 {
mjr 33:d832bcab089e 1814 // turn on the relay for one timer interval
mjr 35:e959ffba78fd 1815 tv_relay->write(1);
mjr 33:d832bcab089e 1816 psu2_state = 5;
mjr 33:d832bcab089e 1817 }
mjr 33:d832bcab089e 1818 break;
mjr 33:d832bcab089e 1819
mjr 33:d832bcab089e 1820 case 5:
mjr 33:d832bcab089e 1821 // TV timer relay on. We pulse this for one interval, so
mjr 33:d832bcab089e 1822 // it's now time to turn it off and return to the default state.
mjr 35:e959ffba78fd 1823 tv_relay->write(0);
mjr 33:d832bcab089e 1824 psu2_state = 1;
mjr 33:d832bcab089e 1825 break;
mjr 33:d832bcab089e 1826 }
mjr 33:d832bcab089e 1827 }
mjr 33:d832bcab089e 1828
mjr 35:e959ffba78fd 1829 // Start the TV ON checker. If the status sense circuit is enabled in
mjr 35:e959ffba78fd 1830 // the configuration, we'll set up the pin connections and start the
mjr 35:e959ffba78fd 1831 // interrupt handler that periodically checks the status. Does nothing
mjr 35:e959ffba78fd 1832 // if any of the pins are configured as NC.
mjr 35:e959ffba78fd 1833 void startTVTimer(Config &cfg)
mjr 35:e959ffba78fd 1834 {
mjr 35:e959ffba78fd 1835 // only start the timer if the status sense circuit pins are configured
mjr 35:e959ffba78fd 1836 if (cfg.TVON.statusPin != NC && cfg.TVON.latchPin != NC && cfg.TVON.relayPin != NC)
mjr 35:e959ffba78fd 1837 {
mjr 35:e959ffba78fd 1838 psu2_status_sense = new DigitalIn(cfg.TVON.statusPin);
mjr 35:e959ffba78fd 1839 psu2_status_set = new DigitalOut(cfg.TVON.latchPin);
mjr 35:e959ffba78fd 1840 tv_relay = new DigitalOut(cfg.TVON.relayPin);
mjr 35:e959ffba78fd 1841 tv_delay_time = cfg.TVON.delayTime;
mjr 35:e959ffba78fd 1842
mjr 35:e959ffba78fd 1843 // Set up our time routine to run every 1/4 second.
mjr 35:e959ffba78fd 1844 tv_ticker.attach(&TVTimerInt, 0.25);
mjr 35:e959ffba78fd 1845 }
mjr 35:e959ffba78fd 1846 }
mjr 35:e959ffba78fd 1847
mjr 35:e959ffba78fd 1848 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 1849 //
mjr 35:e959ffba78fd 1850 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 1851 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 1852 //
mjr 35:e959ffba78fd 1853 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 1854 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 1855 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 1856 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 1857 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 1858 // again each time the firmware is updated.
mjr 35:e959ffba78fd 1859 //
mjr 35:e959ffba78fd 1860 NVM nvm;
mjr 35:e959ffba78fd 1861
mjr 35:e959ffba78fd 1862 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 1863 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 1864
mjr 35:e959ffba78fd 1865 // flash memory controller interface
mjr 35:e959ffba78fd 1866 FreescaleIAP iap;
mjr 35:e959ffba78fd 1867
mjr 35:e959ffba78fd 1868 // figure the flash address as a pointer along with the number of sectors
mjr 35:e959ffba78fd 1869 // required to store the structure
mjr 35:e959ffba78fd 1870 NVM *configFlashAddr(int &addr, int &numSectors)
mjr 35:e959ffba78fd 1871 {
mjr 35:e959ffba78fd 1872 // figure how many flash sectors we span, rounding up to whole sectors
mjr 35:e959ffba78fd 1873 numSectors = (sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE;
mjr 35:e959ffba78fd 1874
mjr 35:e959ffba78fd 1875 // figure the address - this is the highest flash address where the
mjr 35:e959ffba78fd 1876 // structure will fit with the start aligned on a sector boundary
mjr 35:e959ffba78fd 1877 addr = iap.flash_size() - (numSectors * SECTOR_SIZE);
mjr 35:e959ffba78fd 1878
mjr 35:e959ffba78fd 1879 // return the address as a pointer
mjr 35:e959ffba78fd 1880 return (NVM *)addr;
mjr 35:e959ffba78fd 1881 }
mjr 35:e959ffba78fd 1882
mjr 35:e959ffba78fd 1883 // figure the flash address as a pointer
mjr 35:e959ffba78fd 1884 NVM *configFlashAddr()
mjr 35:e959ffba78fd 1885 {
mjr 35:e959ffba78fd 1886 int addr, numSectors;
mjr 35:e959ffba78fd 1887 return configFlashAddr(addr, numSectors);
mjr 35:e959ffba78fd 1888 }
mjr 35:e959ffba78fd 1889
mjr 35:e959ffba78fd 1890 // Load the config from flash
mjr 35:e959ffba78fd 1891 void loadConfigFromFlash()
mjr 35:e959ffba78fd 1892 {
mjr 35:e959ffba78fd 1893 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 1894 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 1895 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 1896 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 1897 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 1898 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 1899 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 1900 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 1901 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 1902 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 1903 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 1904 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 1905 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 1906 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 1907 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 1908 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 1909
mjr 35:e959ffba78fd 1910 // Figure how many sectors we need for our structure
mjr 35:e959ffba78fd 1911 NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 1912
mjr 35:e959ffba78fd 1913 // if the flash is valid, load it; otherwise initialize to defaults
mjr 35:e959ffba78fd 1914 if (flash->valid())
mjr 35:e959ffba78fd 1915 {
mjr 35:e959ffba78fd 1916 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 1917 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 1918 }
mjr 35:e959ffba78fd 1919 else
mjr 35:e959ffba78fd 1920 {
mjr 35:e959ffba78fd 1921 // flash is invalid - load factory settings nito RAM structure
mjr 35:e959ffba78fd 1922 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 1923 }
mjr 35:e959ffba78fd 1924 }
mjr 35:e959ffba78fd 1925
mjr 35:e959ffba78fd 1926 void saveConfigToFlash()
mjr 33:d832bcab089e 1927 {
mjr 35:e959ffba78fd 1928 int addr, sectors;
mjr 35:e959ffba78fd 1929 configFlashAddr(addr, sectors);
mjr 35:e959ffba78fd 1930 nvm.save(iap, addr);
mjr 35:e959ffba78fd 1931 }
mjr 35:e959ffba78fd 1932
mjr 35:e959ffba78fd 1933 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 1934 //
mjr 38:091e511ce8a0 1935 // NIGHT MODE flag. When night mode is on, we disable all outputs
mjr 38:091e511ce8a0 1936 // marked as "noisemakers" in the output configuration flags.
mjr 38:091e511ce8a0 1937 int nightMode;
mjr 38:091e511ce8a0 1938
mjr 38:091e511ce8a0 1939 // Update the global output mode settings
mjr 38:091e511ce8a0 1940 static void globalOutputModeChange()
mjr 38:091e511ce8a0 1941 {
mjr 38:091e511ce8a0 1942 // set the global modeLevel[]
mjr 38:091e511ce8a0 1943 for (int i = 0 ; i < numOutputs ; ++i)
mjr 38:091e511ce8a0 1944 {
mjr 38:091e511ce8a0 1945 // assume the port will be on
mjr 38:091e511ce8a0 1946 uint8_t f = 1;
mjr 38:091e511ce8a0 1947
mjr 38:091e511ce8a0 1948 // if night mode is in effect, and this is a noisemaker, disable it
mjr 38:091e511ce8a0 1949 if (nightMode && (cfg.outPort[i].flags & PortFlagNoisemaker) != 0)
mjr 38:091e511ce8a0 1950 f = 0;
mjr 38:091e511ce8a0 1951
mjr 38:091e511ce8a0 1952 // set the final output port override value
mjr 38:091e511ce8a0 1953 modeLevel[i] = f;
mjr 38:091e511ce8a0 1954 }
mjr 38:091e511ce8a0 1955
mjr 38:091e511ce8a0 1956 // update all outputs for the mode change
mjr 38:091e511ce8a0 1957 updateAllOuts();
mjr 38:091e511ce8a0 1958 }
mjr 38:091e511ce8a0 1959
mjr 38:091e511ce8a0 1960 // Turn night mode on or off
mjr 38:091e511ce8a0 1961 static void setNightMode(bool on)
mjr 38:091e511ce8a0 1962 {
mjr 38:091e511ce8a0 1963 nightMode = on;
mjr 38:091e511ce8a0 1964 globalOutputModeChange();
mjr 38:091e511ce8a0 1965 specialPin[0]->set(on ? 255.0 : 0.0);
mjr 38:091e511ce8a0 1966 }
mjr 38:091e511ce8a0 1967
mjr 38:091e511ce8a0 1968 // Toggle night mode
mjr 38:091e511ce8a0 1969 static void toggleNightMode()
mjr 38:091e511ce8a0 1970 {
mjr 38:091e511ce8a0 1971 setNightMode(!nightMode);
mjr 38:091e511ce8a0 1972 }
mjr 38:091e511ce8a0 1973
mjr 38:091e511ce8a0 1974
mjr 38:091e511ce8a0 1975 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 1976 //
mjr 35:e959ffba78fd 1977 // Plunger Sensor
mjr 35:e959ffba78fd 1978 //
mjr 35:e959ffba78fd 1979
mjr 35:e959ffba78fd 1980 // the plunger sensor interface object
mjr 35:e959ffba78fd 1981 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 1982
mjr 35:e959ffba78fd 1983 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 1984 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 1985 void createPlunger()
mjr 35:e959ffba78fd 1986 {
mjr 35:e959ffba78fd 1987 // delete any existing sensor object
mjr 35:e959ffba78fd 1988 if (plungerSensor != 0)
mjr 35:e959ffba78fd 1989 delete plungerSensor;
mjr 35:e959ffba78fd 1990
mjr 35:e959ffba78fd 1991 // create the new sensor object according to the type
mjr 35:e959ffba78fd 1992 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 1993 {
mjr 35:e959ffba78fd 1994 case PlungerType_TSL1410RS:
mjr 35:e959ffba78fd 1995 // pins are: SI, CLOCK, AO
mjr 35:e959ffba78fd 1996 plungerSensor = new PlungerSensorTSL1410R(cfg.plunger.sensorPin[0], cfg.plunger.sensorPin[1], cfg.plunger.sensorPin[2], NC);
mjr 35:e959ffba78fd 1997 break;
mjr 35:e959ffba78fd 1998
mjr 35:e959ffba78fd 1999 case PlungerType_TSL1410RP:
mjr 35:e959ffba78fd 2000 // pins are: SI, CLOCK, AO1, AO2
mjr 35:e959ffba78fd 2001 plungerSensor = new PlungerSensorTSL1410R(cfg.plunger.sensorPin[0], cfg.plunger.sensorPin[1], cfg.plunger.sensorPin[2], cfg.plunger.sensorPin[3]);
mjr 35:e959ffba78fd 2002 break;
mjr 35:e959ffba78fd 2003
mjr 35:e959ffba78fd 2004 case PlungerType_TSL1412RS:
mjr 35:e959ffba78fd 2005 // pins are: SI, CLOCK, AO1, AO2
mjr 35:e959ffba78fd 2006 plungerSensor = new PlungerSensorTSL1412R(cfg.plunger.sensorPin[0], cfg.plunger.sensorPin[1], cfg.plunger.sensorPin[2], NC);
mjr 35:e959ffba78fd 2007 break;
mjr 35:e959ffba78fd 2008
mjr 35:e959ffba78fd 2009 case PlungerType_TSL1412RP:
mjr 35:e959ffba78fd 2010 // pins are: SI, CLOCK, AO1, AO2
mjr 35:e959ffba78fd 2011 plungerSensor = new PlungerSensorTSL1412R(cfg.plunger.sensorPin[0], cfg.plunger.sensorPin[1], cfg.plunger.sensorPin[2], cfg.plunger.sensorPin[3]);
mjr 35:e959ffba78fd 2012 break;
mjr 35:e959ffba78fd 2013
mjr 35:e959ffba78fd 2014 case PlungerType_Pot:
mjr 35:e959ffba78fd 2015 // pins are: AO
mjr 35:e959ffba78fd 2016 plungerSensor = new PlungerSensorPot(cfg.plunger.sensorPin[0]);
mjr 35:e959ffba78fd 2017 break;
mjr 35:e959ffba78fd 2018
mjr 35:e959ffba78fd 2019 case PlungerType_None:
mjr 35:e959ffba78fd 2020 default:
mjr 35:e959ffba78fd 2021 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 2022 break;
mjr 35:e959ffba78fd 2023 }
mjr 33:d832bcab089e 2024 }
mjr 33:d832bcab089e 2025
mjr 35:e959ffba78fd 2026 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2027 //
mjr 35:e959ffba78fd 2028 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 2029 //
mjr 35:e959ffba78fd 2030 void reboot(USBJoystick &js)
mjr 35:e959ffba78fd 2031 {
mjr 35:e959ffba78fd 2032 // disconnect from USB
mjr 35:e959ffba78fd 2033 js.disconnect();
mjr 35:e959ffba78fd 2034
mjr 35:e959ffba78fd 2035 // wait a few seconds to make sure the host notices the disconnect
mjr 35:e959ffba78fd 2036 wait(5);
mjr 35:e959ffba78fd 2037
mjr 35:e959ffba78fd 2038 // reset the device
mjr 35:e959ffba78fd 2039 NVIC_SystemReset();
mjr 35:e959ffba78fd 2040 while (true) { }
mjr 35:e959ffba78fd 2041 }
mjr 35:e959ffba78fd 2042
mjr 35:e959ffba78fd 2043 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2044 //
mjr 35:e959ffba78fd 2045 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 2046 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 2047 //
mjr 35:e959ffba78fd 2048 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 2049 {
mjr 35:e959ffba78fd 2050 int tmp;
mjr 35:e959ffba78fd 2051 switch (cfg.orientation)
mjr 35:e959ffba78fd 2052 {
mjr 35:e959ffba78fd 2053 case OrientationFront:
mjr 35:e959ffba78fd 2054 tmp = x;
mjr 35:e959ffba78fd 2055 x = y;
mjr 35:e959ffba78fd 2056 y = tmp;
mjr 35:e959ffba78fd 2057 break;
mjr 35:e959ffba78fd 2058
mjr 35:e959ffba78fd 2059 case OrientationLeft:
mjr 35:e959ffba78fd 2060 x = -x;
mjr 35:e959ffba78fd 2061 break;
mjr 35:e959ffba78fd 2062
mjr 35:e959ffba78fd 2063 case OrientationRight:
mjr 35:e959ffba78fd 2064 y = -y;
mjr 35:e959ffba78fd 2065 break;
mjr 35:e959ffba78fd 2066
mjr 35:e959ffba78fd 2067 case OrientationRear:
mjr 35:e959ffba78fd 2068 tmp = -x;
mjr 35:e959ffba78fd 2069 x = -y;
mjr 35:e959ffba78fd 2070 y = tmp;
mjr 35:e959ffba78fd 2071 break;
mjr 35:e959ffba78fd 2072 }
mjr 35:e959ffba78fd 2073 }
mjr 35:e959ffba78fd 2074
mjr 35:e959ffba78fd 2075 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2076 //
mjr 35:e959ffba78fd 2077 // Device status. We report this on each update so that the host config
mjr 35:e959ffba78fd 2078 // tool can detect our current settings. This is a bit mask consisting
mjr 35:e959ffba78fd 2079 // of these bits:
mjr 35:e959ffba78fd 2080 // 0x0001 -> plunger sensor enabled
mjr 35:e959ffba78fd 2081 // 0x8000 -> RESERVED - must always be zero
mjr 35:e959ffba78fd 2082 //
mjr 35:e959ffba78fd 2083 // Note that the high bit (0x8000) must always be 0, since we use that
mjr 35:e959ffba78fd 2084 // to distinguish special request reply packets.
mjr 35:e959ffba78fd 2085 uint16_t statusFlags;
mjr 35:e959ffba78fd 2086
mjr 35:e959ffba78fd 2087 // flag: send a pixel dump after the next read
mjr 35:e959ffba78fd 2088 bool reportPix = false;
mjr 35:e959ffba78fd 2089
mjr 33:d832bcab089e 2090
mjr 35:e959ffba78fd 2091 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2092 //
mjr 35:e959ffba78fd 2093 // Calibration button state:
mjr 35:e959ffba78fd 2094 // 0 = not pushed
mjr 35:e959ffba78fd 2095 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 2096 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 2097 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 2098 int calBtnState = 0;
mjr 35:e959ffba78fd 2099
mjr 35:e959ffba78fd 2100 // calibration button debounce timer
mjr 35:e959ffba78fd 2101 Timer calBtnTimer;
mjr 35:e959ffba78fd 2102
mjr 35:e959ffba78fd 2103 // calibration button light state
mjr 35:e959ffba78fd 2104 int calBtnLit = false;
mjr 35:e959ffba78fd 2105
mjr 35:e959ffba78fd 2106
mjr 35:e959ffba78fd 2107 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2108 //
mjr 35:e959ffba78fd 2109 // Handle a configuration variable update. 'data' is the USB message we
mjr 35:e959ffba78fd 2110 // received from the host.
mjr 35:e959ffba78fd 2111 //
mjr 35:e959ffba78fd 2112 void configVarMsg(uint8_t *data)
mjr 35:e959ffba78fd 2113 {
mjr 35:e959ffba78fd 2114 switch (data[1])
mjr 35:e959ffba78fd 2115 {
mjr 35:e959ffba78fd 2116 case 1:
mjr 35:e959ffba78fd 2117 // USB identification (Vendor ID, Product ID)
mjr 35:e959ffba78fd 2118 cfg.usbVendorID = wireUI16(data+2);
mjr 35:e959ffba78fd 2119 cfg.usbProductID = wireUI16(data+4);
mjr 35:e959ffba78fd 2120 break;
mjr 35:e959ffba78fd 2121
mjr 35:e959ffba78fd 2122 case 2:
mjr 35:e959ffba78fd 2123 // Pinscape Controller unit number - note that data[2] contains
mjr 35:e959ffba78fd 2124 // the nominal unit number, 1-16
mjr 35:e959ffba78fd 2125 if (data[2] >= 1 && data[2] <= 16)
mjr 35:e959ffba78fd 2126 cfg.psUnitNo = data[2];
mjr 35:e959ffba78fd 2127 break;
mjr 35:e959ffba78fd 2128
mjr 35:e959ffba78fd 2129 case 3:
mjr 35:e959ffba78fd 2130 // Enable/disable joystick
mjr 35:e959ffba78fd 2131 cfg.joystickEnabled = data[2];
mjr 35:e959ffba78fd 2132 break;
mjr 35:e959ffba78fd 2133
mjr 35:e959ffba78fd 2134 case 4:
mjr 35:e959ffba78fd 2135 // Accelerometer orientation
mjr 35:e959ffba78fd 2136 cfg.orientation = data[2];
mjr 35:e959ffba78fd 2137 break;
mjr 35:e959ffba78fd 2138
mjr 35:e959ffba78fd 2139 case 5:
mjr 35:e959ffba78fd 2140 // Plunger sensor type
mjr 35:e959ffba78fd 2141 cfg.plunger.sensorType = data[2];
mjr 35:e959ffba78fd 2142 break;
mjr 35:e959ffba78fd 2143
mjr 35:e959ffba78fd 2144 case 6:
mjr 35:e959ffba78fd 2145 // Set plunger pin assignments
mjr 35:e959ffba78fd 2146 cfg.plunger.sensorPin[0] = wirePinName(data[2]);
mjr 35:e959ffba78fd 2147 cfg.plunger.sensorPin[1] = wirePinName(data[3]);
mjr 35:e959ffba78fd 2148 cfg.plunger.sensorPin[2] = wirePinName(data[4]);
mjr 35:e959ffba78fd 2149 cfg.plunger.sensorPin[3] = wirePinName(data[5]);
mjr 35:e959ffba78fd 2150 break;
mjr 35:e959ffba78fd 2151
mjr 35:e959ffba78fd 2152 case 7:
mjr 35:e959ffba78fd 2153 // Plunger calibration button and indicator light pin assignments
mjr 35:e959ffba78fd 2154 cfg.plunger.cal.btn = wirePinName(data[2]);
mjr 35:e959ffba78fd 2155 cfg.plunger.cal.led = wirePinName(data[3]);
mjr 35:e959ffba78fd 2156 break;
mjr 35:e959ffba78fd 2157
mjr 35:e959ffba78fd 2158 case 8:
mjr 35:e959ffba78fd 2159 // ZB Launch Ball setup
mjr 35:e959ffba78fd 2160 cfg.plunger.zbLaunchBall.port = (int)(unsigned char)data[2];
mjr 35:e959ffba78fd 2161 cfg.plunger.zbLaunchBall.btn = (int)(unsigned char)data[3];
mjr 35:e959ffba78fd 2162 cfg.plunger.zbLaunchBall.pushDistance = (float)wireUI16(data+4) / 1000.0;
mjr 35:e959ffba78fd 2163 break;
mjr 35:e959ffba78fd 2164
mjr 35:e959ffba78fd 2165 case 9:
mjr 35:e959ffba78fd 2166 // TV ON setup
mjr 35:e959ffba78fd 2167 cfg.TVON.statusPin = wirePinName(data[2]);
mjr 35:e959ffba78fd 2168 cfg.TVON.latchPin = wirePinName(data[3]);
mjr 35:e959ffba78fd 2169 cfg.TVON.relayPin = wirePinName(data[4]);
mjr 35:e959ffba78fd 2170 cfg.TVON.delayTime = (float)wireUI16(data+5) / 100.0;
mjr 35:e959ffba78fd 2171 break;
mjr 35:e959ffba78fd 2172
mjr 35:e959ffba78fd 2173 case 10:
mjr 35:e959ffba78fd 2174 // TLC5940NT PWM controller chip setup
mjr 35:e959ffba78fd 2175 cfg.tlc5940.nchips = (int)(unsigned char)data[2];
mjr 35:e959ffba78fd 2176 cfg.tlc5940.sin = wirePinName(data[3]);
mjr 35:e959ffba78fd 2177 cfg.tlc5940.sclk = wirePinName(data[4]);
mjr 35:e959ffba78fd 2178 cfg.tlc5940.xlat = wirePinName(data[5]);
mjr 35:e959ffba78fd 2179 cfg.tlc5940.blank = wirePinName(data[6]);
mjr 35:e959ffba78fd 2180 cfg.tlc5940.gsclk = wirePinName(data[7]);
mjr 35:e959ffba78fd 2181 break;
mjr 35:e959ffba78fd 2182
mjr 35:e959ffba78fd 2183 case 11:
mjr 35:e959ffba78fd 2184 // 74HC595 shift register chip setup
mjr 35:e959ffba78fd 2185 cfg.hc595.nchips = (int)(unsigned char)data[2];
mjr 35:e959ffba78fd 2186 cfg.hc595.sin = wirePinName(data[3]);
mjr 35:e959ffba78fd 2187 cfg.hc595.sclk = wirePinName(data[4]);
mjr 35:e959ffba78fd 2188 cfg.hc595.latch = wirePinName(data[5]);
mjr 35:e959ffba78fd 2189 cfg.hc595.ena = wirePinName(data[6]);
mjr 35:e959ffba78fd 2190 break;
mjr 35:e959ffba78fd 2191
mjr 35:e959ffba78fd 2192 case 12:
mjr 35:e959ffba78fd 2193 // button setup
mjr 35:e959ffba78fd 2194 {
mjr 35:e959ffba78fd 2195 // get the button number
mjr 35:e959ffba78fd 2196 int idx = data[2];
mjr 35:e959ffba78fd 2197
mjr 35:e959ffba78fd 2198 // if it's in range, set the button data
mjr 35:e959ffba78fd 2199 if (idx > 0 && idx <= MAX_BUTTONS)
mjr 35:e959ffba78fd 2200 {
mjr 35:e959ffba78fd 2201 // adjust to an array index
mjr 35:e959ffba78fd 2202 --idx;
mjr 35:e959ffba78fd 2203
mjr 35:e959ffba78fd 2204 // set the values
mjr 35:e959ffba78fd 2205 cfg.button[idx].pin = data[3];
mjr 35:e959ffba78fd 2206 cfg.button[idx].typ = data[4];
mjr 35:e959ffba78fd 2207 cfg.button[idx].val = data[5];
mjr 38:091e511ce8a0 2208 cfg.button[idx].flags = data[6];
mjr 35:e959ffba78fd 2209 }
mjr 35:e959ffba78fd 2210 }
mjr 35:e959ffba78fd 2211 break;
mjr 35:e959ffba78fd 2212
mjr 35:e959ffba78fd 2213 case 13:
mjr 35:e959ffba78fd 2214 // LedWiz output port setup
mjr 35:e959ffba78fd 2215 {
mjr 35:e959ffba78fd 2216 // get the port number
mjr 35:e959ffba78fd 2217 int idx = data[2];
mjr 35:e959ffba78fd 2218
mjr 35:e959ffba78fd 2219 // if it's in range, set the port data
mjr 35:e959ffba78fd 2220 if (idx > 0 && idx <= MAX_OUT_PORTS)
mjr 35:e959ffba78fd 2221 {
mjr 35:e959ffba78fd 2222 // adjust to an array index
mjr 35:e959ffba78fd 2223 --idx;
mjr 35:e959ffba78fd 2224
mjr 35:e959ffba78fd 2225 // set the values
mjr 35:e959ffba78fd 2226 cfg.outPort[idx].typ = data[3];
mjr 35:e959ffba78fd 2227 cfg.outPort[idx].pin = data[4];
mjr 35:e959ffba78fd 2228 cfg.outPort[idx].flags = data[5];
mjr 35:e959ffba78fd 2229 }
mjr 38:091e511ce8a0 2230 else if (idx == 254)
mjr 38:091e511ce8a0 2231 {
mjr 38:091e511ce8a0 2232 // special ports
mjr 38:091e511ce8a0 2233 idx -= 254;
mjr 38:091e511ce8a0 2234 cfg.specialPort[idx].typ = data[3];
mjr 38:091e511ce8a0 2235 cfg.specialPort[idx].pin = data[4];
mjr 38:091e511ce8a0 2236 cfg.specialPort[idx].flags = data[5];
mjr 38:091e511ce8a0 2237 }
mjr 35:e959ffba78fd 2238 }
mjr 35:e959ffba78fd 2239 break;
mjr 38:091e511ce8a0 2240
mjr 38:091e511ce8a0 2241 case 14:
mjr 38:091e511ce8a0 2242 // engage/cancel Night Mode
mjr 38:091e511ce8a0 2243 setNightMode(data[2]);
mjr 38:091e511ce8a0 2244 break;
mjr 35:e959ffba78fd 2245 }
mjr 35:e959ffba78fd 2246 }
mjr 35:e959ffba78fd 2247
mjr 35:e959ffba78fd 2248 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2249 //
mjr 35:e959ffba78fd 2250 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 2251 // LedWiz protocol.
mjr 33:d832bcab089e 2252 //
mjr 38:091e511ce8a0 2253 void handleInputMsg(uint8_t data[8], USBJoystick &js, int &z)
mjr 35:e959ffba78fd 2254 {
mjr 38:091e511ce8a0 2255 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 2256 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 2257 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 2258 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 2259 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 2260 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 2261 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 2262 // So our full protocol is as follows:
mjr 38:091e511ce8a0 2263 //
mjr 38:091e511ce8a0 2264 // first byte =
mjr 38:091e511ce8a0 2265 // 0-48 -> LWZ-PBA
mjr 38:091e511ce8a0 2266 // 64 -> LWZ SBA
mjr 38:091e511ce8a0 2267 // 65 -> private control message; second byte specifies subtype
mjr 38:091e511ce8a0 2268 // 129-132 -> LWZ-PBA
mjr 38:091e511ce8a0 2269 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 2270 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 2271 // other -> reserved for future use
mjr 38:091e511ce8a0 2272 //
mjr 38:091e511ce8a0 2273 if (data[0] == 64)
mjr 35:e959ffba78fd 2274 {
mjr 38:091e511ce8a0 2275 // LWZ-SBA - first four bytes are bit-packed on/off flags
mjr 38:091e511ce8a0 2276 // for the outputs; 5th byte is the pulse speed (1-7)
mjr 38:091e511ce8a0 2277 //printf("LWZ-SBA %02x %02x %02x %02x ; %02x\r\n",
mjr 38:091e511ce8a0 2278 // data[1], data[2], data[3], data[4], data[5]);
mjr 38:091e511ce8a0 2279
mjr 38:091e511ce8a0 2280 // update all on/off states
mjr 38:091e511ce8a0 2281 for (int i = 0, bit = 1, ri = 1 ; i < numLwOutputs ; ++i, bit <<= 1)
mjr 35:e959ffba78fd 2282 {
mjr 38:091e511ce8a0 2283 // figure the on/off state bit for this output
mjr 38:091e511ce8a0 2284 if (bit == 0x100) {
mjr 38:091e511ce8a0 2285 bit = 1;
mjr 38:091e511ce8a0 2286 ++ri;
mjr 35:e959ffba78fd 2287 }
mjr 35:e959ffba78fd 2288
mjr 38:091e511ce8a0 2289 // set the on/off state
mjr 38:091e511ce8a0 2290 wizOn[i] = ((data[ri] & bit) != 0);
mjr 38:091e511ce8a0 2291
mjr 38:091e511ce8a0 2292 // If the wizVal setting is 255, it means that this
mjr 38:091e511ce8a0 2293 // output was last set to a brightness value with the
mjr 38:091e511ce8a0 2294 // extended protocol. Return it to LedWiz control by
mjr 38:091e511ce8a0 2295 // rescaling the brightness setting to the LedWiz range
mjr 38:091e511ce8a0 2296 // and updating wizVal with the result. If it's any
mjr 38:091e511ce8a0 2297 // other value, it was previously set by a PBA message,
mjr 38:091e511ce8a0 2298 // so simply retain the last setting - in the normal
mjr 38:091e511ce8a0 2299 // LedWiz protocol, the "profile" (brightness) and on/off
mjr 38:091e511ce8a0 2300 // states are independent, so an SBA just turns an output
mjr 38:091e511ce8a0 2301 // on or off but retains its last brightness level.
mjr 38:091e511ce8a0 2302 if (wizVal[i] == 255)
mjr 38:091e511ce8a0 2303 wizVal[i] = (uint8_t)round(outLevel[i]*48);
mjr 38:091e511ce8a0 2304 }
mjr 38:091e511ce8a0 2305
mjr 38:091e511ce8a0 2306 // set the flash speed - enforce the value range 1-7
mjr 38:091e511ce8a0 2307 wizSpeed = data[5];
mjr 38:091e511ce8a0 2308 if (wizSpeed < 1)
mjr 38:091e511ce8a0 2309 wizSpeed = 1;
mjr 38:091e511ce8a0 2310 else if (wizSpeed > 7)
mjr 38:091e511ce8a0 2311 wizSpeed = 7;
mjr 38:091e511ce8a0 2312
mjr 38:091e511ce8a0 2313 // update the physical outputs
mjr 38:091e511ce8a0 2314 updateWizOuts();
mjr 38:091e511ce8a0 2315 if (hc595 != 0)
mjr 38:091e511ce8a0 2316 hc595->update();
mjr 38:091e511ce8a0 2317
mjr 38:091e511ce8a0 2318 // reset the PBA counter
mjr 38:091e511ce8a0 2319 pbaIdx = 0;
mjr 38:091e511ce8a0 2320 }
mjr 38:091e511ce8a0 2321 else if (data[0] == 65)
mjr 38:091e511ce8a0 2322 {
mjr 38:091e511ce8a0 2323 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 2324 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 2325 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 2326 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 2327 // message type.
mjr 38:091e511ce8a0 2328 if (data[1] == 1)
mjr 38:091e511ce8a0 2329 {
mjr 38:091e511ce8a0 2330 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 2331 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 2332 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 2333 // 0x01 = enable plunger sensor
mjr 38:091e511ce8a0 2334
mjr 38:091e511ce8a0 2335 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 38:091e511ce8a0 2336 // we save the *nominal* unit number 1-16 in the config
mjr 38:091e511ce8a0 2337 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 35:e959ffba78fd 2338
mjr 38:091e511ce8a0 2339 // we'll need a reset if the LedWiz unit number is changing
mjr 38:091e511ce8a0 2340 bool needReset = (newUnitNo != cfg.psUnitNo);
mjr 38:091e511ce8a0 2341
mjr 38:091e511ce8a0 2342 // set the configuration parameters from the message
mjr 38:091e511ce8a0 2343 cfg.psUnitNo = newUnitNo;
mjr 38:091e511ce8a0 2344 cfg.plunger.enabled = data[3] & 0x01;
mjr 38:091e511ce8a0 2345
mjr 38:091e511ce8a0 2346 // update the status flags
mjr 38:091e511ce8a0 2347 statusFlags = (statusFlags & ~0x01) | (data[3] & 0x01);
mjr 38:091e511ce8a0 2348
mjr 38:091e511ce8a0 2349 // if the plunger is no longer enabled, use 0 for z reports
mjr 38:091e511ce8a0 2350 if (!cfg.plunger.enabled)
mjr 38:091e511ce8a0 2351 z = 0;
mjr 38:091e511ce8a0 2352
mjr 38:091e511ce8a0 2353 // save the configuration
mjr 38:091e511ce8a0 2354 saveConfigToFlash();
mjr 38:091e511ce8a0 2355
mjr 38:091e511ce8a0 2356 // reboot if necessary
mjr 38:091e511ce8a0 2357 if (needReset)
mjr 38:091e511ce8a0 2358 reboot(js);
mjr 38:091e511ce8a0 2359 }
mjr 38:091e511ce8a0 2360 else if (data[1] == 2)
mjr 38:091e511ce8a0 2361 {
mjr 38:091e511ce8a0 2362 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 2363 // (No parameters)
mjr 38:091e511ce8a0 2364
mjr 38:091e511ce8a0 2365 // enter calibration mode
mjr 38:091e511ce8a0 2366 calBtnState = 3;
mjr 38:091e511ce8a0 2367 calBtnTimer.reset();
mjr 38:091e511ce8a0 2368 cfg.plunger.cal.reset(plungerSensor->npix);
mjr 38:091e511ce8a0 2369 }
mjr 38:091e511ce8a0 2370 else if (data[1] == 3)
mjr 38:091e511ce8a0 2371 {
mjr 38:091e511ce8a0 2372 // 3 = pixel dump
mjr 38:091e511ce8a0 2373 // (No parameters)
mjr 38:091e511ce8a0 2374 reportPix = true;
mjr 38:091e511ce8a0 2375
mjr 38:091e511ce8a0 2376 // show purple until we finish sending the report
mjr 38:091e511ce8a0 2377 diagLED(1, 0, 1);
mjr 38:091e511ce8a0 2378 }
mjr 38:091e511ce8a0 2379 else if (data[1] == 4)
mjr 38:091e511ce8a0 2380 {
mjr 38:091e511ce8a0 2381 // 4 = hardware configuration query
mjr 38:091e511ce8a0 2382 // (No parameters)
mjr 38:091e511ce8a0 2383 wait_ms(1);
mjr 38:091e511ce8a0 2384 js.reportConfig(
mjr 38:091e511ce8a0 2385 numOutputs,
mjr 38:091e511ce8a0 2386 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 38:091e511ce8a0 2387 cfg.plunger.cal.zero, cfg.plunger.cal.max);
mjr 38:091e511ce8a0 2388 }
mjr 38:091e511ce8a0 2389 else if (data[1] == 5)
mjr 38:091e511ce8a0 2390 {
mjr 38:091e511ce8a0 2391 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 2392 allOutputsOff();
mjr 38:091e511ce8a0 2393 }
mjr 38:091e511ce8a0 2394 else if (data[1] == 6)
mjr 38:091e511ce8a0 2395 {
mjr 38:091e511ce8a0 2396 // 6 = Save configuration to flash.
mjr 38:091e511ce8a0 2397 saveConfigToFlash();
mjr 38:091e511ce8a0 2398
mjr 38:091e511ce8a0 2399 // Reboot the microcontroller. Nearly all config changes
mjr 38:091e511ce8a0 2400 // require a reset, and a reset only takes a few seconds,
mjr 38:091e511ce8a0 2401 // so we don't bother tracking whether or not a reboot is
mjr 38:091e511ce8a0 2402 // really needed.
mjr 38:091e511ce8a0 2403 reboot(js);
mjr 38:091e511ce8a0 2404 }
mjr 38:091e511ce8a0 2405 }
mjr 38:091e511ce8a0 2406 else if (data[0] == 66)
mjr 38:091e511ce8a0 2407 {
mjr 38:091e511ce8a0 2408 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 2409 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 2410 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 2411 // in a variable-dependent format.
mjr 38:091e511ce8a0 2412 configVarMsg(data);
mjr 38:091e511ce8a0 2413 }
mjr 38:091e511ce8a0 2414 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 2415 {
mjr 38:091e511ce8a0 2416 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 2417 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 2418 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 2419 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 2420 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 2421 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 2422 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 2423 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 2424 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 2425 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 2426 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 2427 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 2428 //
mjr 38:091e511ce8a0 2429 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 2430 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 2431 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 2432 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 2433 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 2434 // address those ports anyway.
mjr 38:091e511ce8a0 2435 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 2436 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 38:091e511ce8a0 2437 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 2438 {
mjr 38:091e511ce8a0 2439 // set the brightness level for the output
mjr 38:091e511ce8a0 2440 float b = data[i-i0+1]/255.0;
mjr 38:091e511ce8a0 2441 outLevel[i] = b;
mjr 38:091e511ce8a0 2442
mjr 38:091e511ce8a0 2443 // if it's in the basic LedWiz output set, set the LedWiz
mjr 38:091e511ce8a0 2444 // profile value to 255, which means "use outLevel"
mjr 38:091e511ce8a0 2445 if (i < 32)
mjr 38:091e511ce8a0 2446 wizVal[i] = 255;
mjr 38:091e511ce8a0 2447
mjr 38:091e511ce8a0 2448 // set the output
mjr 38:091e511ce8a0 2449 lwPin[i]->set(b * modeLevel[i]);
mjr 38:091e511ce8a0 2450 }
mjr 38:091e511ce8a0 2451
mjr 38:091e511ce8a0 2452 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 2453 if (hc595 != 0)
mjr 38:091e511ce8a0 2454 hc595->update();
mjr 38:091e511ce8a0 2455 }
mjr 38:091e511ce8a0 2456 else
mjr 38:091e511ce8a0 2457 {
mjr 38:091e511ce8a0 2458 // Everything else is LWZ-PBA. This is a full "profile"
mjr 38:091e511ce8a0 2459 // dump from the host for one bank of 8 outputs. Each
mjr 38:091e511ce8a0 2460 // byte sets one output in the current bank. The current
mjr 38:091e511ce8a0 2461 // bank is implied; the bank starts at 0 and is reset to 0
mjr 38:091e511ce8a0 2462 // by any LWZ-SBA message, and is incremented to the next
mjr 38:091e511ce8a0 2463 // bank by each LWZ-PBA message. Our variable pbaIdx keeps
mjr 38:091e511ce8a0 2464 // track of our notion of the current bank. There's no direct
mjr 38:091e511ce8a0 2465 // way for the host to select the bank; it just has to count
mjr 38:091e511ce8a0 2466 // on us staying in sync. In practice, the host will always
mjr 38:091e511ce8a0 2467 // send a full set of 4 PBA messages in a row to set all 32
mjr 38:091e511ce8a0 2468 // outputs.
mjr 38:091e511ce8a0 2469 //
mjr 38:091e511ce8a0 2470 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 2471 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 2472 // wizVal[] entry for each output, and that takes precedence
mjr 38:091e511ce8a0 2473 // over the extended protocol settings.
mjr 38:091e511ce8a0 2474 //
mjr 38:091e511ce8a0 2475 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 2476 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 2477
mjr 38:091e511ce8a0 2478 // Update all output profile settings
mjr 38:091e511ce8a0 2479 for (int i = 0 ; i < 8 ; ++i)
mjr 38:091e511ce8a0 2480 wizVal[pbaIdx + i] = data[i];
mjr 38:091e511ce8a0 2481
mjr 38:091e511ce8a0 2482 // Update the physical LED state if this is the last bank.
mjr 38:091e511ce8a0 2483 // Note that hosts always send a full set of four PBA
mjr 38:091e511ce8a0 2484 // messages, so there's no need to do a physical update
mjr 38:091e511ce8a0 2485 // until we've received the last bank's PBA message.
mjr 38:091e511ce8a0 2486 if (pbaIdx == 24)
mjr 38:091e511ce8a0 2487 {
mjr 35:e959ffba78fd 2488 updateWizOuts();
mjr 35:e959ffba78fd 2489 if (hc595 != 0)
mjr 35:e959ffba78fd 2490 hc595->update();
mjr 35:e959ffba78fd 2491 pbaIdx = 0;
mjr 35:e959ffba78fd 2492 }
mjr 38:091e511ce8a0 2493 else
mjr 38:091e511ce8a0 2494 pbaIdx += 8;
mjr 38:091e511ce8a0 2495 }
mjr 38:091e511ce8a0 2496 }
mjr 35:e959ffba78fd 2497
mjr 33:d832bcab089e 2498
mjr 38:091e511ce8a0 2499 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 2500 //
mjr 38:091e511ce8a0 2501 // Pre-connection diagnostic flasher
mjr 38:091e511ce8a0 2502 //
mjr 38:091e511ce8a0 2503 void preConnectFlasher()
mjr 38:091e511ce8a0 2504 {
mjr 38:091e511ce8a0 2505 diagLED(1, 0, 0);
mjr 38:091e511ce8a0 2506 wait(0.05);
mjr 38:091e511ce8a0 2507 diagLED(0, 0, 0);
mjr 35:e959ffba78fd 2508 }
mjr 17:ab3cec0c8bf4 2509
mjr 17:ab3cec0c8bf4 2510 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 2511 //
mjr 5:a70c0bce770d 2512 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 2513 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 2514 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 2515 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 2516 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 2517 // port outputs.
mjr 5:a70c0bce770d 2518 //
mjr 0:5acbbe3f4cf4 2519 int main(void)
mjr 0:5acbbe3f4cf4 2520 {
mjr 38:091e511ce8a0 2521 printf("\r\nPinscape Controller starting\r\n"); // $$$ debug
mjr 1:d913e0afb2ac 2522
mjr 35:e959ffba78fd 2523 // clear the I2C bus for the accelerometer
mjr 35:e959ffba78fd 2524 clear_i2c();
mjr 38:091e511ce8a0 2525
mjr 35:e959ffba78fd 2526 // load the saved configuration
mjr 35:e959ffba78fd 2527 loadConfigFromFlash();
mjr 35:e959ffba78fd 2528
mjr 38:091e511ce8a0 2529 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 2530 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 2531
mjr 38:091e511ce8a0 2532 // set up the pre-connected ticker
mjr 38:091e511ce8a0 2533 Ticker preConnectTicker;
mjr 38:091e511ce8a0 2534 preConnectTicker.attach(preConnectFlasher, 3);
mjr 38:091e511ce8a0 2535
mjr 33:d832bcab089e 2536 // start the TV timer, if applicable
mjr 35:e959ffba78fd 2537 startTVTimer(cfg);
mjr 33:d832bcab089e 2538
mjr 33:d832bcab089e 2539 // we're not connected/awake yet
mjr 33:d832bcab089e 2540 bool connected = false;
mjr 33:d832bcab089e 2541 time_t connectChangeTime = time(0);
mjr 33:d832bcab089e 2542
mjr 35:e959ffba78fd 2543 // create the plunger sensor interface
mjr 35:e959ffba78fd 2544 createPlunger();
mjr 33:d832bcab089e 2545
mjr 35:e959ffba78fd 2546 // set up the TLC5940 interface and start the TLC5940 clock, if applicable
mjr 35:e959ffba78fd 2547 init_tlc5940(cfg);
mjr 34:6b981a2afab7 2548
mjr 34:6b981a2afab7 2549 // enable the 74HC595 chips, if present
mjr 35:e959ffba78fd 2550 init_hc595(cfg);
mjr 6:cc35eb643e8f 2551
mjr 38:091e511ce8a0 2552 // Initialize the LedWiz ports. Note that it's important to wait until
mjr 38:091e511ce8a0 2553 // after initializing the various off-board output port controller chip
mjr 38:091e511ce8a0 2554 // sybsystems (TLC5940, 74HC595), since pins attached to peripheral
mjr 38:091e511ce8a0 2555 // controllers will need to address their respective controller objects,
mjr 38:091e511ce8a0 2556 // which don't exit until we initialize those subsystems.
mjr 35:e959ffba78fd 2557 initLwOut(cfg);
mjr 2:c174f9ee414a 2558
mjr 35:e959ffba78fd 2559 // start the TLC5940 clock
mjr 35:e959ffba78fd 2560 if (tlc5940 != 0)
mjr 35:e959ffba78fd 2561 tlc5940->start();
mjr 35:e959ffba78fd 2562
mjr 35:e959ffba78fd 2563 // initialize the button input ports
mjr 35:e959ffba78fd 2564 bool kbKeys = false;
mjr 35:e959ffba78fd 2565 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 2566
mjr 6:cc35eb643e8f 2567 // Create the joystick USB client. Note that we use the LedWiz unit
mjr 6:cc35eb643e8f 2568 // number from the saved configuration.
mjr 35:e959ffba78fd 2569 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, true, cfg.joystickEnabled, kbKeys);
mjr 38:091e511ce8a0 2570
mjr 38:091e511ce8a0 2571 // we're now connected - kill the pre-connect ticker
mjr 38:091e511ce8a0 2572 preConnectTicker.detach();
mjr 17:ab3cec0c8bf4 2573
mjr 38:091e511ce8a0 2574 // Last report timer for the joytick interface. We use the joystick timer
mjr 38:091e511ce8a0 2575 // to throttle the report rate, because VP doesn't benefit from reports any
mjr 38:091e511ce8a0 2576 // faster than about every 10ms.
mjr 38:091e511ce8a0 2577 Timer jsReportTimer;
mjr 38:091e511ce8a0 2578 jsReportTimer.start();
mjr 38:091e511ce8a0 2579
mjr 38:091e511ce8a0 2580 // Time since we successfully sent a USB report. This is a hacky workaround
mjr 38:091e511ce8a0 2581 // for sporadic problems in the USB stack that I haven't been able to figure
mjr 38:091e511ce8a0 2582 // out. If we go too long without successfully sending a USB report, we'll
mjr 38:091e511ce8a0 2583 // try resetting the connection.
mjr 38:091e511ce8a0 2584 Timer jsOKTimer;
mjr 38:091e511ce8a0 2585 jsOKTimer.start();
mjr 35:e959ffba78fd 2586
mjr 35:e959ffba78fd 2587 // set the initial status flags
mjr 35:e959ffba78fd 2588 statusFlags = (cfg.plunger.enabled ? 0x01 : 0x00);
mjr 17:ab3cec0c8bf4 2589
mjr 17:ab3cec0c8bf4 2590 // initialize the calibration buttons, if present
mjr 35:e959ffba78fd 2591 DigitalIn *calBtn = (cfg.plunger.cal.btn == NC ? 0 : new DigitalIn(cfg.plunger.cal.btn));
mjr 35:e959ffba78fd 2592 DigitalOut *calBtnLed = (cfg.plunger.cal.led == NC ? 0 : new DigitalOut(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 2593
mjr 35:e959ffba78fd 2594 // initialize the calibration button
mjr 1:d913e0afb2ac 2595 calBtnTimer.start();
mjr 35:e959ffba78fd 2596 calBtnState = 0;
mjr 1:d913e0afb2ac 2597
mjr 1:d913e0afb2ac 2598 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 2599 Timer hbTimer;
mjr 1:d913e0afb2ac 2600 hbTimer.start();
mjr 1:d913e0afb2ac 2601 int hb = 0;
mjr 5:a70c0bce770d 2602 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 2603
mjr 1:d913e0afb2ac 2604 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 2605 Timer acTimer;
mjr 1:d913e0afb2ac 2606 acTimer.start();
mjr 1:d913e0afb2ac 2607
mjr 0:5acbbe3f4cf4 2608 // create the accelerometer object
mjr 5:a70c0bce770d 2609 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS, MMA8451_INT_PIN);
mjr 0:5acbbe3f4cf4 2610
mjr 17:ab3cec0c8bf4 2611 // last accelerometer report, in joystick units (we report the nudge
mjr 17:ab3cec0c8bf4 2612 // acceleration via the joystick x & y axes, per the VP convention)
mjr 17:ab3cec0c8bf4 2613 int x = 0, y = 0;
mjr 17:ab3cec0c8bf4 2614
mjr 17:ab3cec0c8bf4 2615 // last plunger report position, in 'npix' normalized pixel units
mjr 17:ab3cec0c8bf4 2616 int pos = 0;
mjr 17:ab3cec0c8bf4 2617
mjr 17:ab3cec0c8bf4 2618 // last plunger report, in joystick units (we report the plunger as the
mjr 17:ab3cec0c8bf4 2619 // "z" axis of the joystick, per the VP convention)
mjr 17:ab3cec0c8bf4 2620 int z = 0;
mjr 17:ab3cec0c8bf4 2621
mjr 17:ab3cec0c8bf4 2622 // most recent prior plunger readings, for tracking release events(z0 is
mjr 17:ab3cec0c8bf4 2623 // reading just before the last one we reported, z1 is the one before that,
mjr 17:ab3cec0c8bf4 2624 // z2 the next before that)
mjr 17:ab3cec0c8bf4 2625 int z0 = 0, z1 = 0, z2 = 0;
mjr 17:ab3cec0c8bf4 2626
mjr 17:ab3cec0c8bf4 2627 // Simulated "bounce" position when firing. We model the bounce off of
mjr 17:ab3cec0c8bf4 2628 // the barrel spring when the plunger is released as proportional to the
mjr 17:ab3cec0c8bf4 2629 // distance it was retracted just before being released.
mjr 17:ab3cec0c8bf4 2630 int zBounce = 0;
mjr 2:c174f9ee414a 2631
mjr 17:ab3cec0c8bf4 2632 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 17:ab3cec0c8bf4 2633 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 17:ab3cec0c8bf4 2634 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 17:ab3cec0c8bf4 2635 // back and releases the plunger, or simply pushes on the plunger from
mjr 17:ab3cec0c8bf4 2636 // the rest position. This allows the plunger to be used in lieu of a
mjr 17:ab3cec0c8bf4 2637 // physical Launch Ball button for tables that don't have plungers.
mjr 17:ab3cec0c8bf4 2638 //
mjr 17:ab3cec0c8bf4 2639 // States:
mjr 17:ab3cec0c8bf4 2640 // 0 = default
mjr 17:ab3cec0c8bf4 2641 // 1 = cocked (plunger has been pulled back about 1" from state 0)
mjr 17:ab3cec0c8bf4 2642 // 2 = uncocked (plunger is pulled back less than 1" from state 1)
mjr 21:5048e16cc9ef 2643 // 3 = launching, plunger is forward beyond park position
mjr 21:5048e16cc9ef 2644 // 4 = launching, plunger is behind park position
mjr 21:5048e16cc9ef 2645 // 5 = pressed and holding (plunger has been pressed forward beyond
mjr 21:5048e16cc9ef 2646 // the park position from state 0)
mjr 17:ab3cec0c8bf4 2647 int lbState = 0;
mjr 6:cc35eb643e8f 2648
mjr 35:e959ffba78fd 2649 // button bit for ZB launch ball button
mjr 35:e959ffba78fd 2650 const uint32_t lbButtonBit = (1 << (cfg.plunger.zbLaunchBall.btn - 1));
mjr 35:e959ffba78fd 2651
mjr 17:ab3cec0c8bf4 2652 // Time since last lbState transition. Some of the states are time-
mjr 17:ab3cec0c8bf4 2653 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 17:ab3cec0c8bf4 2654 // we remain in this state for more than a few milliseconds, since
mjr 17:ab3cec0c8bf4 2655 // it indicates that the plunger is being slowly returned to rest
mjr 17:ab3cec0c8bf4 2656 // rather than released. In the "launching" state, we need to release
mjr 17:ab3cec0c8bf4 2657 // the Launch Ball button after a moment, and we need to wait for
mjr 17:ab3cec0c8bf4 2658 // the plunger to come to rest before returning to state 0.
mjr 17:ab3cec0c8bf4 2659 Timer lbTimer;
mjr 17:ab3cec0c8bf4 2660 lbTimer.start();
mjr 17:ab3cec0c8bf4 2661
mjr 18:5e890ebd0023 2662 // Launch Ball simulated push timer. We start this when we simulate
mjr 18:5e890ebd0023 2663 // the button push, and turn off the simulated button when enough time
mjr 18:5e890ebd0023 2664 // has elapsed.
mjr 18:5e890ebd0023 2665 Timer lbBtnTimer;
mjr 18:5e890ebd0023 2666
mjr 17:ab3cec0c8bf4 2667 // Simulated button states. This is a vector of button states
mjr 17:ab3cec0c8bf4 2668 // for the simulated buttons. We combine this with the physical
mjr 17:ab3cec0c8bf4 2669 // button states on each USB joystick report, so we will report
mjr 17:ab3cec0c8bf4 2670 // a button as pressed if either the physical button is being pressed
mjr 17:ab3cec0c8bf4 2671 // or we're simulating a press on the button. This is used for the
mjr 17:ab3cec0c8bf4 2672 // simulated Launch Ball button.
mjr 17:ab3cec0c8bf4 2673 uint32_t simButtons = 0;
mjr 6:cc35eb643e8f 2674
mjr 6:cc35eb643e8f 2675 // Firing in progress: we set this when we detect the start of rapid
mjr 6:cc35eb643e8f 2676 // plunger movement from a retracted position towards the rest position.
mjr 17:ab3cec0c8bf4 2677 //
mjr 17:ab3cec0c8bf4 2678 // When we detect a firing event, we send VP a series of synthetic
mjr 17:ab3cec0c8bf4 2679 // reports simulating the idealized plunger motion. The actual physical
mjr 17:ab3cec0c8bf4 2680 // motion is much too fast to report to VP; in the time between two USB
mjr 17:ab3cec0c8bf4 2681 // reports, the plunger can shoot all the way forward, rebound off of
mjr 17:ab3cec0c8bf4 2682 // the barrel spring, bounce back part way, and bounce forward again,
mjr 17:ab3cec0c8bf4 2683 // or even do all of this more than once. This means that sampling the
mjr 17:ab3cec0c8bf4 2684 // physical motion at the USB report rate would create a misleading
mjr 17:ab3cec0c8bf4 2685 // picture of the plunger motion, since our samples would catch the
mjr 17:ab3cec0c8bf4 2686 // plunger at random points in this oscillating motion. From the
mjr 17:ab3cec0c8bf4 2687 // user's perspective, the physical action that occurred is simply that
mjr 17:ab3cec0c8bf4 2688 // the plunger was released from a particular distance, so it's this
mjr 17:ab3cec0c8bf4 2689 // high-level event that we want to convey to VP. To do this, we
mjr 17:ab3cec0c8bf4 2690 // synthesize a series of reports to convey an idealized version of
mjr 17:ab3cec0c8bf4 2691 // the release motion that's perfectly synchronized to the VP reports.
mjr 17:ab3cec0c8bf4 2692 // Essentially we pretend that our USB position samples are exactly
mjr 17:ab3cec0c8bf4 2693 // aligned in time with (1) the point of retraction just before the
mjr 17:ab3cec0c8bf4 2694 // user released the plunger, (2) the point of maximum forward motion
mjr 17:ab3cec0c8bf4 2695 // just after the user released the plunger (the point of maximum
mjr 17:ab3cec0c8bf4 2696 // compression as the plunger bounces off of the barrel spring), and
mjr 17:ab3cec0c8bf4 2697 // (3) the plunger coming to rest at the park position. This series
mjr 17:ab3cec0c8bf4 2698 // of reports is synthetic in the sense that it's not what we actually
mjr 17:ab3cec0c8bf4 2699 // see on the CCD at the times of these reports - the true plunger
mjr 17:ab3cec0c8bf4 2700 // position is oscillating at high speed during this period. But at
mjr 17:ab3cec0c8bf4 2701 // the same time it conveys a more faithful picture of the true physical
mjr 17:ab3cec0c8bf4 2702 // motion to VP, and allows VP to reproduce the true physical motion
mjr 17:ab3cec0c8bf4 2703 // more faithfully in its simulation model, by correcting for the
mjr 17:ab3cec0c8bf4 2704 // relatively low sampling rate in the communication path between the
mjr 17:ab3cec0c8bf4 2705 // real plunger and VP's model plunger.
mjr 17:ab3cec0c8bf4 2706 //
mjr 17:ab3cec0c8bf4 2707 // If 'firing' is non-zero, it's the index of our current report in
mjr 17:ab3cec0c8bf4 2708 // the synthetic firing report series.
mjr 9:fd65b0a94720 2709 int firing = 0;
mjr 2:c174f9ee414a 2710
mjr 2:c174f9ee414a 2711 // start the first CCD integration cycle
mjr 35:e959ffba78fd 2712 plungerSensor->init();
mjr 10:976666ffa4ef 2713
mjr 1:d913e0afb2ac 2714 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 2715 // host requests
mjr 0:5acbbe3f4cf4 2716 for (;;)
mjr 0:5acbbe3f4cf4 2717 {
mjr 38:091e511ce8a0 2718 // Process incoming reports
mjr 38:091e511ce8a0 2719 LedWizMsg lwmsg;
mjr 38:091e511ce8a0 2720 for (int rr = 0 ; rr < 64 && js.readLedWizMsg(lwmsg) ; ++rr)
mjr 38:091e511ce8a0 2721 handleInputMsg(lwmsg.data, js, z);
mjr 1:d913e0afb2ac 2722
mjr 38:091e511ce8a0 2723
mjr 1:d913e0afb2ac 2724 // check for plunger calibration
mjr 17:ab3cec0c8bf4 2725 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 2726 {
mjr 1:d913e0afb2ac 2727 // check the state
mjr 1:d913e0afb2ac 2728 switch (calBtnState)
mjr 0:5acbbe3f4cf4 2729 {
mjr 1:d913e0afb2ac 2730 case 0:
mjr 1:d913e0afb2ac 2731 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 2732 calBtnTimer.reset();
mjr 1:d913e0afb2ac 2733 calBtnState = 1;
mjr 1:d913e0afb2ac 2734 break;
mjr 1:d913e0afb2ac 2735
mjr 1:d913e0afb2ac 2736 case 1:
mjr 1:d913e0afb2ac 2737 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 2738 // passed, start the hold period
mjr 9:fd65b0a94720 2739 if (calBtnTimer.read_ms() > 50)
mjr 1:d913e0afb2ac 2740 calBtnState = 2;
mjr 1:d913e0afb2ac 2741 break;
mjr 1:d913e0afb2ac 2742
mjr 1:d913e0afb2ac 2743 case 2:
mjr 1:d913e0afb2ac 2744 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 2745 // for the entire hold period, move to calibration mode
mjr 9:fd65b0a94720 2746 if (calBtnTimer.read_ms() > 2050)
mjr 1:d913e0afb2ac 2747 {
mjr 1:d913e0afb2ac 2748 // enter calibration mode
mjr 1:d913e0afb2ac 2749 calBtnState = 3;
mjr 9:fd65b0a94720 2750 calBtnTimer.reset();
mjr 35:e959ffba78fd 2751
mjr 35:e959ffba78fd 2752 // reset the plunger calibration limits
mjr 35:e959ffba78fd 2753 cfg.plunger.cal.reset(plungerSensor->npix);
mjr 1:d913e0afb2ac 2754 }
mjr 1:d913e0afb2ac 2755 break;
mjr 2:c174f9ee414a 2756
mjr 2:c174f9ee414a 2757 case 3:
mjr 9:fd65b0a94720 2758 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 2759 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 2760 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 2761 break;
mjr 0:5acbbe3f4cf4 2762 }
mjr 0:5acbbe3f4cf4 2763 }
mjr 1:d913e0afb2ac 2764 else
mjr 1:d913e0afb2ac 2765 {
mjr 2:c174f9ee414a 2766 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 2767 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 2768 // and save the results to flash.
mjr 2:c174f9ee414a 2769 //
mjr 2:c174f9ee414a 2770 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 2771 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 2772 // mode, it simply cancels the attempt.
mjr 9:fd65b0a94720 2773 if (calBtnState == 3 && calBtnTimer.read_ms() > 15000)
mjr 2:c174f9ee414a 2774 {
mjr 2:c174f9ee414a 2775 // exit calibration mode
mjr 1:d913e0afb2ac 2776 calBtnState = 0;
mjr 2:c174f9ee414a 2777
mjr 6:cc35eb643e8f 2778 // save the updated configuration
mjr 35:e959ffba78fd 2779 cfg.plunger.cal.calibrated = 1;
mjr 35:e959ffba78fd 2780 saveConfigToFlash();
mjr 2:c174f9ee414a 2781 }
mjr 2:c174f9ee414a 2782 else if (calBtnState != 3)
mjr 2:c174f9ee414a 2783 {
mjr 2:c174f9ee414a 2784 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 2785 calBtnState = 0;
mjr 2:c174f9ee414a 2786 }
mjr 1:d913e0afb2ac 2787 }
mjr 1:d913e0afb2ac 2788
mjr 1:d913e0afb2ac 2789 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 2790 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 2791 switch (calBtnState)
mjr 0:5acbbe3f4cf4 2792 {
mjr 1:d913e0afb2ac 2793 case 2:
mjr 1:d913e0afb2ac 2794 // in the hold period - flash the light
mjr 9:fd65b0a94720 2795 newCalBtnLit = ((calBtnTimer.read_ms()/250) & 1);
mjr 1:d913e0afb2ac 2796 break;
mjr 1:d913e0afb2ac 2797
mjr 1:d913e0afb2ac 2798 case 3:
mjr 1:d913e0afb2ac 2799 // calibration mode - show steady on
mjr 1:d913e0afb2ac 2800 newCalBtnLit = true;
mjr 1:d913e0afb2ac 2801 break;
mjr 1:d913e0afb2ac 2802
mjr 1:d913e0afb2ac 2803 default:
mjr 1:d913e0afb2ac 2804 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 2805 newCalBtnLit = false;
mjr 1:d913e0afb2ac 2806 break;
mjr 1:d913e0afb2ac 2807 }
mjr 3:3514575d4f86 2808
mjr 3:3514575d4f86 2809 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 2810 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 2811 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 2812 {
mjr 1:d913e0afb2ac 2813 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 2814 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 2815 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 2816 calBtnLed->write(1);
mjr 38:091e511ce8a0 2817 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 2818 }
mjr 2:c174f9ee414a 2819 else {
mjr 17:ab3cec0c8bf4 2820 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 2821 calBtnLed->write(0);
mjr 38:091e511ce8a0 2822 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 2823 }
mjr 1:d913e0afb2ac 2824 }
mjr 38:091e511ce8a0 2825
mjr 17:ab3cec0c8bf4 2826 // If the plunger is enabled, and we're not already in a firing event,
mjr 17:ab3cec0c8bf4 2827 // and the last plunger reading had the plunger pulled back at least
mjr 17:ab3cec0c8bf4 2828 // a bit, watch for plunger release events until it's time for our next
mjr 17:ab3cec0c8bf4 2829 // USB report.
mjr 35:e959ffba78fd 2830 if (!firing && cfg.plunger.enabled && z >= JOYMAX/6)
mjr 17:ab3cec0c8bf4 2831 {
mjr 17:ab3cec0c8bf4 2832 // monitor the plunger until it's time for our next report
mjr 38:091e511ce8a0 2833 while (jsReportTimer.read_ms() < 15)
mjr 17:ab3cec0c8bf4 2834 {
mjr 17:ab3cec0c8bf4 2835 // do a fast low-res scan; if it's at or past the zero point,
mjr 17:ab3cec0c8bf4 2836 // start a firing event
mjr 35:e959ffba78fd 2837 int pos0;
mjr 35:e959ffba78fd 2838 if (plungerSensor->lowResScan(pos0) && pos0 <= cfg.plunger.cal.zero)
mjr 17:ab3cec0c8bf4 2839 firing = 1;
mjr 17:ab3cec0c8bf4 2840 }
mjr 17:ab3cec0c8bf4 2841 }
mjr 17:ab3cec0c8bf4 2842
mjr 6:cc35eb643e8f 2843 // read the plunger sensor, if it's enabled
mjr 35:e959ffba78fd 2844 if (cfg.plunger.enabled)
mjr 6:cc35eb643e8f 2845 {
mjr 6:cc35eb643e8f 2846 // start with the previous reading, in case we don't have a
mjr 6:cc35eb643e8f 2847 // clear result on this frame
mjr 6:cc35eb643e8f 2848 int znew = z;
mjr 35:e959ffba78fd 2849 if (plungerSensor->highResScan(pos))
mjr 6:cc35eb643e8f 2850 {
mjr 17:ab3cec0c8bf4 2851 // We got a new reading. If we're in calibration mode, use it
mjr 17:ab3cec0c8bf4 2852 // to figure the new calibration, otherwise adjust the new reading
mjr 17:ab3cec0c8bf4 2853 // for the established calibration.
mjr 17:ab3cec0c8bf4 2854 if (calBtnState == 3)
mjr 6:cc35eb643e8f 2855 {
mjr 17:ab3cec0c8bf4 2856 // Calibration mode. If this reading is outside of the current
mjr 17:ab3cec0c8bf4 2857 // calibration bounds, expand the bounds.
mjr 35:e959ffba78fd 2858 if (pos < cfg.plunger.cal.min)
mjr 35:e959ffba78fd 2859 cfg.plunger.cal.min = pos;
mjr 35:e959ffba78fd 2860 if (pos < cfg.plunger.cal.zero)
mjr 35:e959ffba78fd 2861 cfg.plunger.cal.zero = pos;
mjr 35:e959ffba78fd 2862 if (pos > cfg.plunger.cal.max)
mjr 35:e959ffba78fd 2863 cfg.plunger.cal.max = pos;
mjr 6:cc35eb643e8f 2864
mjr 17:ab3cec0c8bf4 2865 // normalize to the full physical range while calibrating
mjr 35:e959ffba78fd 2866 znew = int(round(float(pos)/plungerSensor->npix * JOYMAX));
mjr 17:ab3cec0c8bf4 2867 }
mjr 17:ab3cec0c8bf4 2868 else
mjr 17:ab3cec0c8bf4 2869 {
mjr 17:ab3cec0c8bf4 2870 // Not in calibration mode, so normalize the new reading to the
mjr 17:ab3cec0c8bf4 2871 // established calibration range.
mjr 17:ab3cec0c8bf4 2872 //
mjr 17:ab3cec0c8bf4 2873 // Note that negative values are allowed. Zero represents the
mjr 17:ab3cec0c8bf4 2874 // "park" position, where the plunger sits when at rest. A mechanical
mjr 23:14f8c5004cd0 2875 // plunger has a small amount of travel in the "push" direction,
mjr 17:ab3cec0c8bf4 2876 // since the barrel spring can be compressed slightly. Negative
mjr 17:ab3cec0c8bf4 2877 // values represent travel in the push direction.
mjr 35:e959ffba78fd 2878 if (pos > cfg.plunger.cal.max)
mjr 35:e959ffba78fd 2879 pos = cfg.plunger.cal.max;
mjr 35:e959ffba78fd 2880 znew = int(round(float(pos - cfg.plunger.cal.zero)
mjr 35:e959ffba78fd 2881 / (cfg.plunger.cal.max - cfg.plunger.cal.zero + 1) * JOYMAX));
mjr 6:cc35eb643e8f 2882 }
mjr 6:cc35eb643e8f 2883 }
mjr 7:100a25f8bf56 2884
mjr 17:ab3cec0c8bf4 2885 // If we're not already in a firing event, check to see if the
mjr 17:ab3cec0c8bf4 2886 // new position is forward of the last report. If it is, a firing
mjr 17:ab3cec0c8bf4 2887 // event might have started during the high-res scan. This might
mjr 17:ab3cec0c8bf4 2888 // seem unlikely given that the scan only takes about 5ms, but that
mjr 17:ab3cec0c8bf4 2889 // 5ms represents about 25-30% of our total time between reports,
mjr 17:ab3cec0c8bf4 2890 // there's about a 1 in 4 chance that a release starts during a
mjr 17:ab3cec0c8bf4 2891 // scan.
mjr 17:ab3cec0c8bf4 2892 if (!firing && z0 > 0 && znew < z0)
mjr 17:ab3cec0c8bf4 2893 {
mjr 17:ab3cec0c8bf4 2894 // The plunger has moved forward since the previous report.
mjr 17:ab3cec0c8bf4 2895 // Watch it for a few more ms to see if we can get a stable
mjr 17:ab3cec0c8bf4 2896 // new position.
mjr 35:e959ffba78fd 2897 int pos0;
mjr 35:e959ffba78fd 2898 if (plungerSensor->lowResScan(pos0))
mjr 17:ab3cec0c8bf4 2899 {
mjr 35:e959ffba78fd 2900 int pos1 = pos0;
mjr 35:e959ffba78fd 2901 Timer tw;
mjr 35:e959ffba78fd 2902 tw.start();
mjr 35:e959ffba78fd 2903 while (tw.read_ms() < 6)
mjr 17:ab3cec0c8bf4 2904 {
mjr 35:e959ffba78fd 2905 // read the new position
mjr 35:e959ffba78fd 2906 int pos2;
mjr 35:e959ffba78fd 2907 if (plungerSensor->lowResScan(pos2))
mjr 35:e959ffba78fd 2908 {
mjr 35:e959ffba78fd 2909 // If it's stable over consecutive readings, stop looping.
mjr 35:e959ffba78fd 2910 // (Count it as stable if the position is within about 1/8".
mjr 35:e959ffba78fd 2911 // pos1 and pos2 are reported in pixels, so they range from
mjr 35:e959ffba78fd 2912 // 0 to npix. The overall travel of a standard plunger is
mjr 35:e959ffba78fd 2913 // about 3.2", so we have (npix/3.2) pixels per inch, hence
mjr 35:e959ffba78fd 2914 // 1/8" is (npix/3.2)*(1/8) pixels.)
mjr 35:e959ffba78fd 2915 if (abs(pos2 - pos1) < int(plungerSensor->npix/(3.2*8)))
mjr 35:e959ffba78fd 2916 break;
mjr 35:e959ffba78fd 2917
mjr 35:e959ffba78fd 2918 // If we've crossed the rest position, and we've moved by
mjr 35:e959ffba78fd 2919 // a minimum distance from where we starting this loop, begin
mjr 35:e959ffba78fd 2920 // a firing event. (We require a minimum distance to prevent
mjr 35:e959ffba78fd 2921 // spurious firing from random analog noise in the readings
mjr 35:e959ffba78fd 2922 // when the plunger is actually just sitting still at the
mjr 35:e959ffba78fd 2923 // rest position. If it's at rest, it's normal to see small
mjr 35:e959ffba78fd 2924 // random fluctuations in the analog reading +/- 1% or so
mjr 35:e959ffba78fd 2925 // from the 0 point, especially with a sensor like a
mjr 35:e959ffba78fd 2926 // potentionemeter that reports the position as a single
mjr 35:e959ffba78fd 2927 // analog voltage.) Note that we compare the latest reading
mjr 35:e959ffba78fd 2928 // to the first reading of the loop - we don't require the
mjr 35:e959ffba78fd 2929 // threshold motion over consecutive readings, but any time
mjr 35:e959ffba78fd 2930 // over the stability wait loop.
mjr 35:e959ffba78fd 2931 if (pos1 < cfg.plunger.cal.zero
mjr 35:e959ffba78fd 2932 && abs(pos2 - pos0) > int(plungerSensor->npix/(3.2*8)))
mjr 35:e959ffba78fd 2933 {
mjr 35:e959ffba78fd 2934 firing = 1;
mjr 35:e959ffba78fd 2935 break;
mjr 35:e959ffba78fd 2936 }
mjr 35:e959ffba78fd 2937
mjr 35:e959ffba78fd 2938 // the new reading is now the prior reading
mjr 35:e959ffba78fd 2939 pos1 = pos2;
mjr 35:e959ffba78fd 2940 }
mjr 17:ab3cec0c8bf4 2941 }
mjr 17:ab3cec0c8bf4 2942 }
mjr 17:ab3cec0c8bf4 2943 }
mjr 17:ab3cec0c8bf4 2944
mjr 17:ab3cec0c8bf4 2945 // Check for a simulated Launch Ball button press, if enabled
mjr 35:e959ffba78fd 2946 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 17:ab3cec0c8bf4 2947 {
mjr 18:5e890ebd0023 2948 const int cockThreshold = JOYMAX/3;
mjr 35:e959ffba78fd 2949 const int pushThreshold = int(-JOYMAX/3 * cfg.plunger.zbLaunchBall.pushDistance);
mjr 17:ab3cec0c8bf4 2950 int newState = lbState;
mjr 17:ab3cec0c8bf4 2951 switch (lbState)
mjr 17:ab3cec0c8bf4 2952 {
mjr 17:ab3cec0c8bf4 2953 case 0:
mjr 17:ab3cec0c8bf4 2954 // Base state. If the plunger is pulled back by an inch
mjr 17:ab3cec0c8bf4 2955 // or more, go to "cocked" state. If the plunger is pushed
mjr 21:5048e16cc9ef 2956 // forward by 1/4" or more, go to "pressed" state.
mjr 18:5e890ebd0023 2957 if (znew >= cockThreshold)
mjr 17:ab3cec0c8bf4 2958 newState = 1;
mjr 18:5e890ebd0023 2959 else if (znew <= pushThreshold)
mjr 21:5048e16cc9ef 2960 newState = 5;
mjr 17:ab3cec0c8bf4 2961 break;
mjr 17:ab3cec0c8bf4 2962
mjr 17:ab3cec0c8bf4 2963 case 1:
mjr 17:ab3cec0c8bf4 2964 // Cocked state. If a firing event is now in progress,
mjr 17:ab3cec0c8bf4 2965 // go to "launch" state. Otherwise, if the plunger is less
mjr 17:ab3cec0c8bf4 2966 // than 1" retracted, go to "uncocked" state - the player
mjr 17:ab3cec0c8bf4 2967 // might be slowly returning the plunger to rest so as not
mjr 17:ab3cec0c8bf4 2968 // to trigger a launch.
mjr 17:ab3cec0c8bf4 2969 if (firing || znew <= 0)
mjr 17:ab3cec0c8bf4 2970 newState = 3;
mjr 18:5e890ebd0023 2971 else if (znew < cockThreshold)
mjr 17:ab3cec0c8bf4 2972 newState = 2;
mjr 17:ab3cec0c8bf4 2973 break;
mjr 17:ab3cec0c8bf4 2974
mjr 17:ab3cec0c8bf4 2975 case 2:
mjr 17:ab3cec0c8bf4 2976 // Uncocked state. If the plunger is more than an inch
mjr 17:ab3cec0c8bf4 2977 // retracted, return to cocked state. If we've been in
mjr 17:ab3cec0c8bf4 2978 // the uncocked state for more than half a second, return
mjr 18:5e890ebd0023 2979 // to the base state. This allows the user to return the
mjr 18:5e890ebd0023 2980 // plunger to rest without triggering a launch, by moving
mjr 18:5e890ebd0023 2981 // it at manual speed to the rest position rather than
mjr 18:5e890ebd0023 2982 // releasing it.
mjr 18:5e890ebd0023 2983 if (znew >= cockThreshold)
mjr 17:ab3cec0c8bf4 2984 newState = 1;
mjr 17:ab3cec0c8bf4 2985 else if (lbTimer.read_ms() > 500)
mjr 17:ab3cec0c8bf4 2986 newState = 0;
mjr 17:ab3cec0c8bf4 2987 break;
mjr 17:ab3cec0c8bf4 2988
mjr 17:ab3cec0c8bf4 2989 case 3:
mjr 17:ab3cec0c8bf4 2990 // Launch state. If the plunger is no longer pushed
mjr 17:ab3cec0c8bf4 2991 // forward, switch to launch rest state.
mjr 18:5e890ebd0023 2992 if (znew >= 0)
mjr 17:ab3cec0c8bf4 2993 newState = 4;
mjr 17:ab3cec0c8bf4 2994 break;
mjr 17:ab3cec0c8bf4 2995
mjr 17:ab3cec0c8bf4 2996 case 4:
mjr 17:ab3cec0c8bf4 2997 // Launch rest state. If the plunger is pushed forward
mjr 17:ab3cec0c8bf4 2998 // again, switch back to launch state. If not, and we've
mjr 17:ab3cec0c8bf4 2999 // been in this state for at least 200ms, return to the
mjr 17:ab3cec0c8bf4 3000 // default state.
mjr 18:5e890ebd0023 3001 if (znew <= pushThreshold)
mjr 17:ab3cec0c8bf4 3002 newState = 3;
mjr 17:ab3cec0c8bf4 3003 else if (lbTimer.read_ms() > 200)
mjr 17:ab3cec0c8bf4 3004 newState = 0;
mjr 17:ab3cec0c8bf4 3005 break;
mjr 21:5048e16cc9ef 3006
mjr 21:5048e16cc9ef 3007 case 5:
mjr 21:5048e16cc9ef 3008 // Press-and-Hold state. If the plunger is no longer pushed
mjr 21:5048e16cc9ef 3009 // forward, AND it's been at least 50ms since we generated
mjr 21:5048e16cc9ef 3010 // the simulated Launch Ball button press, return to the base
mjr 21:5048e16cc9ef 3011 // state. The minimum time is to ensure that VP has a chance
mjr 21:5048e16cc9ef 3012 // to see the button press and to avoid transient key bounce
mjr 21:5048e16cc9ef 3013 // effects when the plunger position is right on the threshold.
mjr 21:5048e16cc9ef 3014 if (znew > pushThreshold && lbTimer.read_ms() > 50)
mjr 21:5048e16cc9ef 3015 newState = 0;
mjr 21:5048e16cc9ef 3016 break;
mjr 17:ab3cec0c8bf4 3017 }
mjr 17:ab3cec0c8bf4 3018
mjr 17:ab3cec0c8bf4 3019 // change states if desired
mjr 17:ab3cec0c8bf4 3020 if (newState != lbState)
mjr 17:ab3cec0c8bf4 3021 {
mjr 21:5048e16cc9ef 3022 // If we're entering Launch state OR we're entering the
mjr 21:5048e16cc9ef 3023 // Press-and-Hold state, AND the ZB Launch Ball LedWiz signal
mjr 21:5048e16cc9ef 3024 // is turned on, simulate a Launch Ball button press.
mjr 21:5048e16cc9ef 3025 if (((newState == 3 && lbState != 4) || newState == 5)
mjr 35:e959ffba78fd 3026 && wizOn[cfg.plunger.zbLaunchBall.port-1])
mjr 18:5e890ebd0023 3027 {
mjr 18:5e890ebd0023 3028 lbBtnTimer.reset();
mjr 18:5e890ebd0023 3029 lbBtnTimer.start();
mjr 18:5e890ebd0023 3030 simButtons |= lbButtonBit;
mjr 18:5e890ebd0023 3031 }
mjr 21:5048e16cc9ef 3032
mjr 17:ab3cec0c8bf4 3033 // if we're switching to state 0, release the button
mjr 17:ab3cec0c8bf4 3034 if (newState == 0)
mjr 35:e959ffba78fd 3035 simButtons &= ~(1 << (cfg.plunger.zbLaunchBall.btn - 1));
mjr 17:ab3cec0c8bf4 3036
mjr 17:ab3cec0c8bf4 3037 // switch to the new state
mjr 17:ab3cec0c8bf4 3038 lbState = newState;
mjr 17:ab3cec0c8bf4 3039
mjr 17:ab3cec0c8bf4 3040 // start timing in the new state
mjr 17:ab3cec0c8bf4 3041 lbTimer.reset();
mjr 17:ab3cec0c8bf4 3042 }
mjr 21:5048e16cc9ef 3043
mjr 21:5048e16cc9ef 3044 // If the Launch Ball button press is in effect, but the
mjr 21:5048e16cc9ef 3045 // ZB Launch Ball LedWiz signal is no longer turned on, turn
mjr 21:5048e16cc9ef 3046 // off the button.
mjr 21:5048e16cc9ef 3047 //
mjr 21:5048e16cc9ef 3048 // If we're in one of the Launch states (state #3 or #4),
mjr 21:5048e16cc9ef 3049 // and the button has been on for long enough, turn it off.
mjr 21:5048e16cc9ef 3050 // The Launch mode is triggered by a pull-and-release gesture.
mjr 21:5048e16cc9ef 3051 // From the user's perspective, this is just a single gesture
mjr 21:5048e16cc9ef 3052 // that should trigger just one momentary press on the Launch
mjr 21:5048e16cc9ef 3053 // Ball button. Physically, though, the plunger usually
mjr 21:5048e16cc9ef 3054 // bounces back and forth for 500ms or so before coming to
mjr 21:5048e16cc9ef 3055 // rest after this gesture. That's what the whole state
mjr 21:5048e16cc9ef 3056 // #3-#4 business is all about - we stay in this pair of
mjr 21:5048e16cc9ef 3057 // states until the plunger comes to rest. As long as we're
mjr 21:5048e16cc9ef 3058 // in these states, we won't send duplicate button presses.
mjr 21:5048e16cc9ef 3059 // But we also don't want the one button press to continue
mjr 21:5048e16cc9ef 3060 // the whole time, so we'll time it out now.
mjr 21:5048e16cc9ef 3061 //
mjr 21:5048e16cc9ef 3062 // (This could be written as one big 'if' condition, but
mjr 21:5048e16cc9ef 3063 // I'm breaking it out verbosely like this to make it easier
mjr 21:5048e16cc9ef 3064 // for human readers such as myself to comprehend the logic.)
mjr 21:5048e16cc9ef 3065 if ((simButtons & lbButtonBit) != 0)
mjr 18:5e890ebd0023 3066 {
mjr 21:5048e16cc9ef 3067 int turnOff = false;
mjr 21:5048e16cc9ef 3068
mjr 21:5048e16cc9ef 3069 // turn it off if the ZB Launch Ball signal is off
mjr 35:e959ffba78fd 3070 if (!wizOn[cfg.plunger.zbLaunchBall.port-1])
mjr 21:5048e16cc9ef 3071 turnOff = true;
mjr 21:5048e16cc9ef 3072
mjr 21:5048e16cc9ef 3073 // also turn it off if we're in state 3 or 4 ("Launch"),
mjr 21:5048e16cc9ef 3074 // and the button has been on long enough
mjr 21:5048e16cc9ef 3075 if ((lbState == 3 || lbState == 4) && lbBtnTimer.read_ms() > 250)
mjr 21:5048e16cc9ef 3076 turnOff = true;
mjr 21:5048e16cc9ef 3077
mjr 21:5048e16cc9ef 3078 // if we decided to turn off the button, do so
mjr 21:5048e16cc9ef 3079 if (turnOff)
mjr 21:5048e16cc9ef 3080 {
mjr 21:5048e16cc9ef 3081 lbBtnTimer.stop();
mjr 21:5048e16cc9ef 3082 simButtons &= ~lbButtonBit;
mjr 21:5048e16cc9ef 3083 }
mjr 18:5e890ebd0023 3084 }
mjr 17:ab3cec0c8bf4 3085 }
mjr 17:ab3cec0c8bf4 3086
mjr 17:ab3cec0c8bf4 3087 // If a firing event is in progress, generate synthetic reports to
mjr 17:ab3cec0c8bf4 3088 // describe an idealized version of the plunger motion to VP rather
mjr 17:ab3cec0c8bf4 3089 // than reporting the actual physical plunger position.
mjr 6:cc35eb643e8f 3090 //
mjr 17:ab3cec0c8bf4 3091 // We use the synthetic reports during a release event because the
mjr 17:ab3cec0c8bf4 3092 // physical plunger motion when released is too fast for VP to track.
mjr 17:ab3cec0c8bf4 3093 // VP only syncs its internal physics model with the outside world
mjr 17:ab3cec0c8bf4 3094 // about every 10ms. In that amount of time, the plunger moves
mjr 17:ab3cec0c8bf4 3095 // fast enough when released that it can shoot all the way forward,
mjr 17:ab3cec0c8bf4 3096 // bounce off of the barrel spring, and rebound part of the way
mjr 17:ab3cec0c8bf4 3097 // back. The result is the classic analog-to-digital problem of
mjr 17:ab3cec0c8bf4 3098 // sample aliasing. If we happen to time our sample during the
mjr 17:ab3cec0c8bf4 3099 // release motion so that we catch the plunger at the peak of a
mjr 17:ab3cec0c8bf4 3100 // bounce, the digital signal incorrectly looks like the plunger
mjr 17:ab3cec0c8bf4 3101 // is moving slowly forward - VP thinks we went from fully
mjr 17:ab3cec0c8bf4 3102 // retracted to half retracted in the sample interval, whereas
mjr 17:ab3cec0c8bf4 3103 // we actually traveled all the way forward and half way back,
mjr 17:ab3cec0c8bf4 3104 // so the speed VP infers is about 1/3 of the actual speed.
mjr 9:fd65b0a94720 3105 //
mjr 17:ab3cec0c8bf4 3106 // To correct this, we take advantage of our ability to sample
mjr 17:ab3cec0c8bf4 3107 // the CCD image several times in the course of a VP report. If
mjr 17:ab3cec0c8bf4 3108 // we catch the plunger near the origin after we've seen it
mjr 17:ab3cec0c8bf4 3109 // retracted, we go into Release Event mode. During this mode,
mjr 17:ab3cec0c8bf4 3110 // we stop reporting the true physical plunger position, and
mjr 17:ab3cec0c8bf4 3111 // instead report an idealized pattern: we report the plunger
mjr 17:ab3cec0c8bf4 3112 // immediately shooting forward to a position in front of the
mjr 17:ab3cec0c8bf4 3113 // park position that's in proportion to how far back the plunger
mjr 17:ab3cec0c8bf4 3114 // was just before the release, and we then report it stationary
mjr 17:ab3cec0c8bf4 3115 // at the park position. We continue to report the stationary
mjr 17:ab3cec0c8bf4 3116 // park position until the actual physical plunger motion has
mjr 17:ab3cec0c8bf4 3117 // stabilized on a new position. We then exit Release Event
mjr 17:ab3cec0c8bf4 3118 // mode and return to reporting the true physical position.
mjr 17:ab3cec0c8bf4 3119 if (firing)
mjr 6:cc35eb643e8f 3120 {
mjr 17:ab3cec0c8bf4 3121 // Firing in progress. Keep reporting the park position
mjr 17:ab3cec0c8bf4 3122 // until the physical plunger position comes to rest.
mjr 17:ab3cec0c8bf4 3123 const int restTol = JOYMAX/24;
mjr 17:ab3cec0c8bf4 3124 if (firing == 1)
mjr 6:cc35eb643e8f 3125 {
mjr 17:ab3cec0c8bf4 3126 // For the first couple of frames, show the plunger shooting
mjr 17:ab3cec0c8bf4 3127 // forward past the zero point, to simulate the momentum carrying
mjr 17:ab3cec0c8bf4 3128 // it forward to bounce off of the barrel spring. Show the
mjr 17:ab3cec0c8bf4 3129 // bounce as proportional to the distance it was retracted
mjr 17:ab3cec0c8bf4 3130 // in the prior report.
mjr 17:ab3cec0c8bf4 3131 z = zBounce = -z0/6;
mjr 17:ab3cec0c8bf4 3132 ++firing;
mjr 6:cc35eb643e8f 3133 }
mjr 17:ab3cec0c8bf4 3134 else if (firing == 2)
mjr 9:fd65b0a94720 3135 {
mjr 17:ab3cec0c8bf4 3136 // second frame - keep the bounce a little longer
mjr 17:ab3cec0c8bf4 3137 z = zBounce;
mjr 17:ab3cec0c8bf4 3138 ++firing;
mjr 17:ab3cec0c8bf4 3139 }
mjr 17:ab3cec0c8bf4 3140 else if (firing > 4
mjr 17:ab3cec0c8bf4 3141 && abs(znew - z0) < restTol
mjr 17:ab3cec0c8bf4 3142 && abs(znew - z1) < restTol
mjr 17:ab3cec0c8bf4 3143 && abs(znew - z2) < restTol)
mjr 17:ab3cec0c8bf4 3144 {
mjr 17:ab3cec0c8bf4 3145 // The physical plunger has come to rest. Exit firing
mjr 17:ab3cec0c8bf4 3146 // mode and resume reporting the actual position.
mjr 17:ab3cec0c8bf4 3147 firing = false;
mjr 17:ab3cec0c8bf4 3148 z = znew;
mjr 9:fd65b0a94720 3149 }
mjr 9:fd65b0a94720 3150 else
mjr 9:fd65b0a94720 3151 {
mjr 17:ab3cec0c8bf4 3152 // until the physical plunger comes to rest, simply
mjr 17:ab3cec0c8bf4 3153 // report the park position
mjr 9:fd65b0a94720 3154 z = 0;
mjr 17:ab3cec0c8bf4 3155 ++firing;
mjr 9:fd65b0a94720 3156 }
mjr 6:cc35eb643e8f 3157 }
mjr 6:cc35eb643e8f 3158 else
mjr 6:cc35eb643e8f 3159 {
mjr 17:ab3cec0c8bf4 3160 // not in firing mode - report the true physical position
mjr 17:ab3cec0c8bf4 3161 z = znew;
mjr 6:cc35eb643e8f 3162 }
mjr 17:ab3cec0c8bf4 3163
mjr 17:ab3cec0c8bf4 3164 // shift the new reading into the recent history buffer
mjr 6:cc35eb643e8f 3165 z2 = z1;
mjr 6:cc35eb643e8f 3166 z1 = z0;
mjr 6:cc35eb643e8f 3167 z0 = znew;
mjr 2:c174f9ee414a 3168 }
mjr 6:cc35eb643e8f 3169
mjr 38:091e511ce8a0 3170 // process button updates
mjr 38:091e511ce8a0 3171 processButtons();
mjr 37:ed52738445fc 3172
mjr 38:091e511ce8a0 3173 // send a keyboard report if we have new data
mjr 37:ed52738445fc 3174 if (kbState.changed)
mjr 37:ed52738445fc 3175 {
mjr 38:091e511ce8a0 3176 // send a keyboard report
mjr 37:ed52738445fc 3177 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 3178 kbState.changed = false;
mjr 37:ed52738445fc 3179 }
mjr 38:091e511ce8a0 3180
mjr 38:091e511ce8a0 3181 // likewise for the media controller
mjr 37:ed52738445fc 3182 if (mediaState.changed)
mjr 37:ed52738445fc 3183 {
mjr 38:091e511ce8a0 3184 // send a media report
mjr 37:ed52738445fc 3185 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 3186 mediaState.changed = false;
mjr 37:ed52738445fc 3187 }
mjr 38:091e511ce8a0 3188
mjr 38:091e511ce8a0 3189 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 3190 bool jsOK = false;
mjr 17:ab3cec0c8bf4 3191
mjr 17:ab3cec0c8bf4 3192 // If it's been long enough since our last USB status report,
mjr 17:ab3cec0c8bf4 3193 // send the new report. We throttle the report rate because
mjr 17:ab3cec0c8bf4 3194 // it can overwhelm the PC side if we report too frequently.
mjr 17:ab3cec0c8bf4 3195 // VP only wants to sync with the real world in 10ms intervals,
mjr 35:e959ffba78fd 3196 // so reporting more frequently creates I/O overhead without
mjr 35:e959ffba78fd 3197 // doing anything to improve the simulation.
mjr 38:091e511ce8a0 3198 if (cfg.joystickEnabled && jsReportTimer.read_ms() > 10)
mjr 17:ab3cec0c8bf4 3199 {
mjr 17:ab3cec0c8bf4 3200 // read the accelerometer
mjr 17:ab3cec0c8bf4 3201 int xa, ya;
mjr 17:ab3cec0c8bf4 3202 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 3203
mjr 17:ab3cec0c8bf4 3204 // confine the results to our joystick axis range
mjr 17:ab3cec0c8bf4 3205 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 17:ab3cec0c8bf4 3206 if (xa > JOYMAX) xa = JOYMAX;
mjr 17:ab3cec0c8bf4 3207 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 17:ab3cec0c8bf4 3208 if (ya > JOYMAX) ya = JOYMAX;
mjr 17:ab3cec0c8bf4 3209
mjr 17:ab3cec0c8bf4 3210 // store the updated accelerometer coordinates
mjr 17:ab3cec0c8bf4 3211 x = xa;
mjr 17:ab3cec0c8bf4 3212 y = ya;
mjr 17:ab3cec0c8bf4 3213
mjr 21:5048e16cc9ef 3214 // Report the current plunger position UNLESS the ZB Launch Ball
mjr 21:5048e16cc9ef 3215 // signal is on, in which case just report a constant 0 value.
mjr 21:5048e16cc9ef 3216 // ZB Launch Ball turns off the plunger position because it
mjr 21:5048e16cc9ef 3217 // tells us that the table has a Launch Ball button instead of
mjr 21:5048e16cc9ef 3218 // a traditional plunger.
mjr 35:e959ffba78fd 3219 int zrep = (cfg.plunger.zbLaunchBall.port != 0 && wizOn[cfg.plunger.zbLaunchBall.port-1] ? 0 : z);
mjr 35:e959ffba78fd 3220
mjr 35:e959ffba78fd 3221 // rotate X and Y according to the device orientation in the cabinet
mjr 35:e959ffba78fd 3222 accelRotate(x, y);
mjr 35:e959ffba78fd 3223
mjr 35:e959ffba78fd 3224 // send the joystick report
mjr 38:091e511ce8a0 3225 jsOK = js.update(x, y, zrep, jsButtons | simButtons, statusFlags);
mjr 21:5048e16cc9ef 3226
mjr 17:ab3cec0c8bf4 3227 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 3228 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 3229 }
mjr 21:5048e16cc9ef 3230
mjr 10:976666ffa4ef 3231 // If we're in pixel dump mode, report all pixel exposure values
mjr 10:976666ffa4ef 3232 if (reportPix)
mjr 10:976666ffa4ef 3233 {
mjr 17:ab3cec0c8bf4 3234 // send the report
mjr 35:e959ffba78fd 3235 plungerSensor->sendExposureReport(js);
mjr 17:ab3cec0c8bf4 3236
mjr 10:976666ffa4ef 3237 // we have satisfied this request
mjr 10:976666ffa4ef 3238 reportPix = false;
mjr 10:976666ffa4ef 3239 }
mjr 10:976666ffa4ef 3240
mjr 35:e959ffba78fd 3241 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 3242 // periodically for the sake of the Windows config tool.
mjr 38:091e511ce8a0 3243 if (!cfg.joystickEnabled && jsReportTimer.read_ms() > 200)
mjr 21:5048e16cc9ef 3244 {
mjr 38:091e511ce8a0 3245 jsOK = js.updateStatus(0);
mjr 38:091e511ce8a0 3246 jsReportTimer.reset();
mjr 38:091e511ce8a0 3247 }
mjr 38:091e511ce8a0 3248
mjr 38:091e511ce8a0 3249 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 3250 if (jsOK)
mjr 38:091e511ce8a0 3251 {
mjr 38:091e511ce8a0 3252 jsOKTimer.reset();
mjr 38:091e511ce8a0 3253 jsOKTimer.start();
mjr 21:5048e16cc9ef 3254 }
mjr 21:5048e16cc9ef 3255
mjr 6:cc35eb643e8f 3256 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 3257 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 3258 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 3259 #endif
mjr 6:cc35eb643e8f 3260
mjr 33:d832bcab089e 3261 // check for connection status changes
mjr 33:d832bcab089e 3262 int newConnected = js.isConnected() && !js.isSuspended();
mjr 33:d832bcab089e 3263 if (newConnected != connected)
mjr 33:d832bcab089e 3264 {
mjr 33:d832bcab089e 3265 // give it a few seconds to stabilize
mjr 33:d832bcab089e 3266 time_t tc = time(0);
mjr 33:d832bcab089e 3267 if (tc - connectChangeTime > 3)
mjr 33:d832bcab089e 3268 {
mjr 33:d832bcab089e 3269 // note the new status
mjr 33:d832bcab089e 3270 connected = newConnected;
mjr 33:d832bcab089e 3271 connectChangeTime = tc;
mjr 33:d832bcab089e 3272
mjr 33:d832bcab089e 3273 // if we're no longer connected, turn off all outputs
mjr 33:d832bcab089e 3274 if (!connected)
mjr 33:d832bcab089e 3275 allOutputsOff();
mjr 33:d832bcab089e 3276 }
mjr 33:d832bcab089e 3277 }
mjr 38:091e511ce8a0 3278
mjr 6:cc35eb643e8f 3279 // provide a visual status indication on the on-board LED
mjr 5:a70c0bce770d 3280 if (calBtnState < 2 && hbTimer.read_ms() > 1000)
mjr 1:d913e0afb2ac 3281 {
mjr 33:d832bcab089e 3282 if (!newConnected)
mjr 2:c174f9ee414a 3283 {
mjr 5:a70c0bce770d 3284 // suspended - turn off the LED
mjr 38:091e511ce8a0 3285 diagLED(0, 0, 0);
mjr 5:a70c0bce770d 3286
mjr 5:a70c0bce770d 3287 // show a status flash every so often
mjr 5:a70c0bce770d 3288 if (hbcnt % 3 == 0)
mjr 5:a70c0bce770d 3289 {
mjr 38:091e511ce8a0 3290 // disconnected = short red/red flash
mjr 38:091e511ce8a0 3291 // suspended = short red flash
mjr 5:a70c0bce770d 3292 for (int n = js.isConnected() ? 1 : 2 ; n > 0 ; --n)
mjr 5:a70c0bce770d 3293 {
mjr 38:091e511ce8a0 3294 diagLED(1, 0, 0);
mjr 5:a70c0bce770d 3295 wait(0.05);
mjr 38:091e511ce8a0 3296 diagLED(0, 0, 0);
mjr 5:a70c0bce770d 3297 wait(0.25);
mjr 5:a70c0bce770d 3298 }
mjr 5:a70c0bce770d 3299 }
mjr 2:c174f9ee414a 3300 }
mjr 38:091e511ce8a0 3301 else if (jsOKTimer.read() > 5)
mjr 38:091e511ce8a0 3302 {
mjr 38:091e511ce8a0 3303 // too long without a USB report - show red/yellow
mjr 38:091e511ce8a0 3304 static bool dumped;
mjr 38:091e511ce8a0 3305 if (!dumped) {
mjr 38:091e511ce8a0 3306 extern void USBDeviceStatusDump(void);
mjr 38:091e511ce8a0 3307 USBDeviceStatusDump();
mjr 38:091e511ce8a0 3308 dumped = true;
mjr 38:091e511ce8a0 3309 }
mjr 38:091e511ce8a0 3310 extern bool USB_DMAERR;
mjr 38:091e511ce8a0 3311 if (USB_DMAERR) {
mjr 38:091e511ce8a0 3312 printf("USB DMAERR DETECTED!\r\n");
mjr 38:091e511ce8a0 3313 // js.disconnect();
mjr 38:091e511ce8a0 3314 // js.connect();
mjr 38:091e511ce8a0 3315 // USB_DMAERR = false;
mjr 38:091e511ce8a0 3316 }
mjr 38:091e511ce8a0 3317 jsOKTimer.stop();
mjr 38:091e511ce8a0 3318 hb = !hb;
mjr 38:091e511ce8a0 3319 diagLED(1, hb, 0);
mjr 38:091e511ce8a0 3320 }
mjr 35:e959ffba78fd 3321 else if (cfg.plunger.enabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 3322 {
mjr 6:cc35eb643e8f 3323 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 3324 hb = !hb;
mjr 38:091e511ce8a0 3325 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 3326 }
mjr 6:cc35eb643e8f 3327 else
mjr 6:cc35eb643e8f 3328 {
mjr 6:cc35eb643e8f 3329 // connected - flash blue/green
mjr 2:c174f9ee414a 3330 hb = !hb;
mjr 38:091e511ce8a0 3331 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 3332 }
mjr 1:d913e0afb2ac 3333
mjr 1:d913e0afb2ac 3334 // reset the heartbeat timer
mjr 1:d913e0afb2ac 3335 hbTimer.reset();
mjr 5:a70c0bce770d 3336 ++hbcnt;
mjr 1:d913e0afb2ac 3337 }
mjr 1:d913e0afb2ac 3338 }
mjr 0:5acbbe3f4cf4 3339 }