Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Thu Feb 18 07:32:20 2016 +0000
Revision:
47:df7a88cd249c
Parent:
45:c42166b2878c
Child:
48:058ace2aed1d
3-channel linked DMA scheme for CCD image capture working

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