Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Fri May 12 17:57:59 2017 +0000
Revision:
89:c43cd923401c
Parent:
88:98bce687e6c0
Child:
90:aa4e571da8e8
Added Flipper Logic to output port options

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 51:57eb311faafa 1 /* Copyright 2014, 2016 M J Roberts, MIT License
mjr 5:a70c0bce770d 2 *
mjr 5:a70c0bce770d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mjr 5:a70c0bce770d 4 * and associated documentation files (the "Software"), to deal in the Software without
mjr 5:a70c0bce770d 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mjr 5:a70c0bce770d 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mjr 5:a70c0bce770d 7 * Software is furnished to do so, subject to the following conditions:
mjr 5:a70c0bce770d 8 *
mjr 5:a70c0bce770d 9 * The above copyright notice and this permission notice shall be included in all copies or
mjr 5:a70c0bce770d 10 * substantial portions of the Software.
mjr 5:a70c0bce770d 11 *
mjr 5:a70c0bce770d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mjr 48:058ace2aed1d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILIT Y, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 5:a70c0bce770d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 5:a70c0bce770d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 5:a70c0bce770d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 5:a70c0bce770d 17 */
mjr 5:a70c0bce770d 18
mjr 5:a70c0bce770d 19 //
mjr 35:e959ffba78fd 20 // The Pinscape Controller
mjr 35:e959ffba78fd 21 // A comprehensive input/output controller for virtual pinball machines
mjr 5:a70c0bce770d 22 //
mjr 48:058ace2aed1d 23 // This project implements an I/O controller for virtual pinball cabinets. The
mjr 48:058ace2aed1d 24 // controller's function is to connect Visual Pinball (and other Windows pinball
mjr 48:058ace2aed1d 25 // emulators) with physical devices in the cabinet: buttons, sensors, and
mjr 48:058ace2aed1d 26 // feedback devices that create visual or mechanical effects during play.
mjr 38:091e511ce8a0 27 //
mjr 48:058ace2aed1d 28 // The controller can perform several different functions, which can be used
mjr 38:091e511ce8a0 29 // individually or in any combination:
mjr 5:a70c0bce770d 30 //
mjr 38:091e511ce8a0 31 // - Nudge sensing. This uses the KL25Z's on-board accelerometer to sense the
mjr 38:091e511ce8a0 32 // motion of the cabinet when you nudge it. Visual Pinball and other pinball
mjr 38:091e511ce8a0 33 // emulators on the PC have native handling for this type of input, so that
mjr 38:091e511ce8a0 34 // physical nudges on the cabinet turn into simulated effects on the virtual
mjr 38:091e511ce8a0 35 // ball. The KL25Z measures accelerations as analog readings and is quite
mjr 38:091e511ce8a0 36 // sensitive, so the effect of a nudge on the simulation is proportional
mjr 38:091e511ce8a0 37 // to the strength of the nudge. Accelerations are reported to the PC via a
mjr 38:091e511ce8a0 38 // simulated joystick (using the X and Y axes); you just have to set some
mjr 38:091e511ce8a0 39 // preferences in your pinball software to tell it that an accelerometer
mjr 38:091e511ce8a0 40 // is attached.
mjr 5:a70c0bce770d 41 //
mjr 74:822a92bc11d2 42 // - Plunger position sensing, with multiple 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 87:8d35c74403af 50 // We support several sensor types:
mjr 35:e959ffba78fd 51 //
mjr 87:8d35c74403af 52 // - AEDR-8300-1K2 optical encoders. These are quadrature encoders with
mjr 87:8d35c74403af 53 // reflective optical sensing and built-in lighting and optics. The sensor
mjr 87:8d35c74403af 54 // is attached to the plunger so that it moves with the plunger, and slides
mjr 87:8d35c74403af 55 // along a guide rail with a reflective pattern of regularly spaces bars
mjr 87:8d35c74403af 56 // for the encoder to read. We read the plunger position by counting the
mjr 87:8d35c74403af 57 // bars the sensor passes as it moves across the rail. This is the newest
mjr 87:8d35c74403af 58 // option, and it's my current favorite because it's highly accurate,
mjr 87:8d35c74403af 59 // precise, and fast, plus it's relatively inexpensive.
mjr 87:8d35c74403af 60 //
mjr 87:8d35c74403af 61 // - Slide potentiometers. There are slide potentioneters available with a
mjr 87:8d35c74403af 62 // long enough travel distance (at least 85mm) to cover the plunger travel.
mjr 87:8d35c74403af 63 // Attach the plunger to the potentiometer knob so that the moving the
mjr 87:8d35c74403af 64 // plunger moves the pot knob. We sense the position by simply reading
mjr 87:8d35c74403af 65 // the analog voltage on the pot brush. A pot with a "linear taper" (that
mjr 87:8d35c74403af 66 // is, the resistance varies linearly with the position) is required.
mjr 87:8d35c74403af 67 // This option is cheap, easy to set up, and works well.
mjr 5:a70c0bce770d 68 //
mjr 87:8d35c74403af 69 // - VL6108X time-of-flight distance sensor. This is an optical distance
mjr 87:8d35c74403af 70 // sensor that measures the distance to a nearby object (within about 10cm)
mjr 87:8d35c74403af 71 // by measuring the travel time for reflected pulses of light. It's fairly
mjr 87:8d35c74403af 72 // cheap and easy to set up, but I don't recommend it because it has very
mjr 87:8d35c74403af 73 // low precision.
mjr 6:cc35eb643e8f 74 //
mjr 87:8d35c74403af 75 // - TSL1410R/TSL1412R linear array optical sensors. These are large optical
mjr 87:8d35c74403af 76 // sensors with the pixels arranged in a single row. The pixel arrays are
mjr 87:8d35c74403af 77 // large enough on these to cover the travel distance of the plunger, so we
mjr 87:8d35c74403af 78 // can set up the sensor near the plunger in such a way that the plunger
mjr 87:8d35c74403af 79 // casts a shadow on the sensor. We detect the plunger position by finding
mjr 87:8d35c74403af 80 // the edge of the sahdow in the image. The optics for this setup are very
mjr 87:8d35c74403af 81 // simple since we don't need any lenses. This was the first sensor we
mjr 87:8d35c74403af 82 // supported, and works very well, but unfortunately the sensor is difficult
mjr 87:8d35c74403af 83 // to find now since it's been discontinued by the manufacturer.
mjr 87:8d35c74403af 84 //
mjr 87:8d35c74403af 85 // The v2 Build Guide has details on how to build and configure all of the
mjr 87:8d35c74403af 86 // sensor options.
mjr 87:8d35c74403af 87 //
mjr 87:8d35c74403af 88 // Visual Pinball has built-in support for plunger devices like this one, but
mjr 87:8d35c74403af 89 // some older VP tables (particularly for VP 9) can't use it without some
mjr 87:8d35c74403af 90 // modifications to their scripting. The Build Guide has advice on how to
mjr 87:8d35c74403af 91 // fix up VP tables to add plunger support when necessary.
mjr 5:a70c0bce770d 92 //
mjr 77:0b96f6867312 93 // - Button input wiring. You can assign GPIO ports as inputs for physical
mjr 77:0b96f6867312 94 // pinball-style buttons, such as flipper buttons, a Start button, coin
mjr 77:0b96f6867312 95 // chute switches, tilt bobs, and service panel buttons. You can configure
mjr 77:0b96f6867312 96 // each button input to report a keyboard key or joystick button press to
mjr 77:0b96f6867312 97 // the PC when the physical button is pushed.
mjr 13:72dda449c3c0 98 //
mjr 53:9b2611964afc 99 // - LedWiz emulation. The KL25Z can pretend to be an LedWiz device. This lets
mjr 53:9b2611964afc 100 // you connect feedback devices (lights, solenoids, motors) to GPIO ports on the
mjr 53:9b2611964afc 101 // KL25Z, and lets PC software (such as Visual Pinball) control them during game
mjr 53:9b2611964afc 102 // play to create a more immersive playing experience. The Pinscape software
mjr 53:9b2611964afc 103 // presents itself to the host as an LedWiz device and accepts the full LedWiz
mjr 53:9b2611964afc 104 // command set, so software on the PC designed for real LedWiz'es can control
mjr 53:9b2611964afc 105 // attached devices without any modifications.
mjr 5:a70c0bce770d 106 //
mjr 53:9b2611964afc 107 // Even though the software provides a very thorough LedWiz emulation, the KL25Z
mjr 53:9b2611964afc 108 // GPIO hardware design imposes some serious limitations. The big one is that
mjr 53:9b2611964afc 109 // the KL25Z only has 10 PWM channels, meaning that only 10 ports can have
mjr 53:9b2611964afc 110 // varying-intensity outputs (e.g., for controlling the brightness level of an
mjr 53:9b2611964afc 111 // LED or the speed or a motor). You can control more than 10 output ports, but
mjr 53:9b2611964afc 112 // only 10 can have PWM control; the rest are simple "digital" ports that can only
mjr 53:9b2611964afc 113 // be switched fully on or fully off. The second limitation is that the KL25Z
mjr 53:9b2611964afc 114 // just doesn't have that many GPIO ports overall. There are enough to populate
mjr 53:9b2611964afc 115 // all 32 button inputs OR all 32 LedWiz outputs, but not both. The default is
mjr 53:9b2611964afc 116 // to assign 24 buttons and 22 LedWiz ports; you can change this balance to trade
mjr 53:9b2611964afc 117 // off more outputs for fewer inputs, or vice versa. The third limitation is that
mjr 53:9b2611964afc 118 // the KL25Z GPIO pins have *very* tiny amperage limits - just 4mA, which isn't
mjr 53:9b2611964afc 119 // even enough to control a small LED. So in order to connect any kind of feedback
mjr 53:9b2611964afc 120 // device to an output, you *must* build some external circuitry to boost the
mjr 53:9b2611964afc 121 // current handing. The Build Guide has a reference circuit design for this
mjr 53:9b2611964afc 122 // purpose that's simple and inexpensive to build.
mjr 6:cc35eb643e8f 123 //
mjr 87:8d35c74403af 124 // - Enhanced LedWiz emulation with TLC5940 and/or TLC59116 PWM controller chips.
mjr 87:8d35c74403af 125 // You can attach external PWM chips for controlling device outputs, instead of
mjr 87:8d35c74403af 126 // using (or in addition to) the on-board GPIO ports as described above. The
mjr 87:8d35c74403af 127 // software can control a set of daisy-chained TLC5940 or TLC59116 chips. Each
mjr 87:8d35c74403af 128 // chip provides 16 PWM outputs, so you just need two of them to get the full
mjr 87:8d35c74403af 129 // complement of 32 output ports of a real LedWiz. You can hook up even more,
mjr 87:8d35c74403af 130 // though. Four chips gives you 64 ports, which should be plenty for nearly any
mjr 87:8d35c74403af 131 // virtual pinball project.
mjr 53:9b2611964afc 132 //
mjr 53:9b2611964afc 133 // The Pinscape Expansion Board project (which appeared in early 2016) provides
mjr 53:9b2611964afc 134 // a reference hardware design, with EAGLE circuit board layouts, that takes full
mjr 53:9b2611964afc 135 // advantage of the TLC5940 capability. It lets you create a customized set of
mjr 53:9b2611964afc 136 // outputs with full PWM control and power handling for high-current devices
mjr 87:8d35c74403af 137 // built in to the boards.
mjr 87:8d35c74403af 138 //
mjr 87:8d35c74403af 139 // To accommodate the larger supply of ports possible with the external chips,
mjr 87:8d35c74403af 140 // the controller software provides a custom, extended version of the LedWiz
mjr 87:8d35c74403af 141 // protocol that can handle up to 128 ports. Legacy PC software designed only
mjr 87:8d35c74403af 142 // for the original LedWiz obviously can't use the extended protocol, and thus
mjr 87:8d35c74403af 143 // can't take advantage of its extra capabilities, but the latest version of
mjr 87:8d35c74403af 144 // DOF (DirectOutput Framework) *does* know the new language and can take full
mjr 87:8d35c74403af 145 // advantage. Older software will still work, though - the new extensions are
mjr 87:8d35c74403af 146 // all backwards compatible, so old software that only knows about the original
mjr 87:8d35c74403af 147 // LedWiz protocol will still work, with the limitation that it can only access
mjr 87:8d35c74403af 148 // the first 32 ports. In addition, we provide a replacement LEDWIZ.DLL that
mjr 87:8d35c74403af 149 // creates virtual LedWiz units representing additional ports beyond the first
mjr 87:8d35c74403af 150 // 32. This allows legacy LedWiz client software to address all ports by
mjr 87:8d35c74403af 151 // making them think that you have several physical LedWiz units installed.
mjr 26:cb71c4af2912 152 //
mjr 38:091e511ce8a0 153 // - Night Mode control for output devices. You can connect a switch or button
mjr 38:091e511ce8a0 154 // to the controller to activate "Night Mode", which disables feedback devices
mjr 38:091e511ce8a0 155 // that you designate as noisy. You can designate outputs individually as being
mjr 38:091e511ce8a0 156 // included in this set or not. This is useful if you want to play a game on
mjr 38:091e511ce8a0 157 // your cabinet late at night without waking the kids and annoying the neighbors.
mjr 38:091e511ce8a0 158 //
mjr 38:091e511ce8a0 159 // - TV ON switch. The controller can pulse a relay to turn on your TVs after
mjr 38:091e511ce8a0 160 // power to the cabinet comes on, with a configurable delay timer. This feature
mjr 38:091e511ce8a0 161 // is for TVs that don't turn themselves on automatically when first plugged in.
mjr 38:091e511ce8a0 162 // To use this feature, you have to build some external circuitry to allow the
mjr 77:0b96f6867312 163 // software to sense the power supply status. The Build Guide has details
mjr 77:0b96f6867312 164 // on the necessary circuitry. You can use this to switch your TV on via a
mjr 77:0b96f6867312 165 // hardwired connection to the TV's "on" button, which requires taking the
mjr 77:0b96f6867312 166 // TV apart to gain access to its internal wiring, or optionally via the IR
mjr 77:0b96f6867312 167 // remote control transmitter feature below.
mjr 77:0b96f6867312 168 //
mjr 77:0b96f6867312 169 // - Infrared (IR) remote control receiver and transmitter. You can attach an
mjr 77:0b96f6867312 170 // IR LED and/or an IR sensor (we recommend the TSOP384xx series) to make the
mjr 77:0b96f6867312 171 // KL25Z capable of sending and/or receiving IR remote control signals. This
mjr 77:0b96f6867312 172 // can be used with the TV ON feature above to turn your TV(s) on when the
mjr 77:0b96f6867312 173 // system power comes on by sending the "on" command to them via IR, as though
mjr 77:0b96f6867312 174 // you pressed the "on" button on the remote control. The sensor lets the
mjr 77:0b96f6867312 175 // Pinscape software learn the IR codes from your existing remotes, in the
mjr 77:0b96f6867312 176 // same manner as a handheld universal remote control, and the IR LED lets
mjr 77:0b96f6867312 177 // it transmit learned codes. The sensor can also be used to receive codes
mjr 77:0b96f6867312 178 // during normal operation and turn them into PC keystrokes; this lets you
mjr 77:0b96f6867312 179 // access extra commands on the PC without adding more buttons to your
mjr 77:0b96f6867312 180 // cabinet. The IR LED can also be used to transmit other codes when you
mjr 77:0b96f6867312 181 // press selected cabinet buttons, allowing you to assign cabinet buttons
mjr 77:0b96f6867312 182 // to send IR commands to your cabinet TV or other devices.
mjr 38:091e511ce8a0 183 //
mjr 35:e959ffba78fd 184 //
mjr 35:e959ffba78fd 185 //
mjr 33:d832bcab089e 186 // STATUS LIGHTS: The on-board LED on the KL25Z flashes to indicate the current
mjr 33:d832bcab089e 187 // device status. The flash patterns are:
mjr 6:cc35eb643e8f 188 //
mjr 48:058ace2aed1d 189 // short yellow flash = waiting to connect
mjr 6:cc35eb643e8f 190 //
mjr 48:058ace2aed1d 191 // short red flash = the connection is suspended (the host is in sleep
mjr 48:058ace2aed1d 192 // or suspend mode, the USB cable is unplugged after a connection
mjr 48:058ace2aed1d 193 // has been established)
mjr 48:058ace2aed1d 194 //
mjr 48:058ace2aed1d 195 // two short red flashes = connection lost (the device should immediately
mjr 48:058ace2aed1d 196 // go back to short-yellow "waiting to reconnect" mode when a connection
mjr 48:058ace2aed1d 197 // is lost, so this display shouldn't normally appear)
mjr 6:cc35eb643e8f 198 //
mjr 38:091e511ce8a0 199 // long red/yellow = USB connection problem. The device still has a USB
mjr 48:058ace2aed1d 200 // connection to the host (or so it appears to the device), but data
mjr 48:058ace2aed1d 201 // transmissions are failing.
mjr 38:091e511ce8a0 202 //
mjr 73:4e8ce0b18915 203 // medium blue flash = TV ON delay timer running. This means that the
mjr 73:4e8ce0b18915 204 // power to the secondary PSU has just been turned on, and the TV ON
mjr 73:4e8ce0b18915 205 // timer is waiting for the configured delay time before pulsing the
mjr 73:4e8ce0b18915 206 // TV power button relay. This is only shown if the TV ON feature is
mjr 73:4e8ce0b18915 207 // enabled.
mjr 73:4e8ce0b18915 208 //
mjr 6:cc35eb643e8f 209 // long yellow/green = everything's working, but the plunger hasn't
mjr 38:091e511ce8a0 210 // been calibrated. Follow the calibration procedure described in
mjr 38:091e511ce8a0 211 // the project documentation. This flash mode won't appear if there's
mjr 38:091e511ce8a0 212 // no plunger sensor configured.
mjr 6:cc35eb643e8f 213 //
mjr 38:091e511ce8a0 214 // alternating blue/green = everything's working normally, and plunger
mjr 38:091e511ce8a0 215 // calibration has been completed (or there's no plunger attached)
mjr 10:976666ffa4ef 216 //
mjr 48:058ace2aed1d 217 // fast red/purple = out of memory. The controller halts and displays
mjr 48:058ace2aed1d 218 // this diagnostic code until you manually reset it. If this happens,
mjr 48:058ace2aed1d 219 // it's probably because the configuration is too complex, in which
mjr 48:058ace2aed1d 220 // case the same error will occur after the reset. If it's stuck
mjr 48:058ace2aed1d 221 // in this cycle, you'll have to restore the default configuration
mjr 48:058ace2aed1d 222 // by re-installing the controller software (the Pinscape .bin file).
mjr 10:976666ffa4ef 223 //
mjr 48:058ace2aed1d 224 //
mjr 48:058ace2aed1d 225 // USB PROTOCOL: Most of our USB messaging is through standard USB HID
mjr 48:058ace2aed1d 226 // classes (joystick, keyboard). We also accept control messages on our
mjr 48:058ace2aed1d 227 // primary HID interface "OUT endpoint" using a custom protocol that's
mjr 48:058ace2aed1d 228 // not defined in any USB standards (we do have to provide a USB HID
mjr 48:058ace2aed1d 229 // Report Descriptor for it, but this just describes the protocol as
mjr 48:058ace2aed1d 230 // opaque vendor-defined bytes). The control protocol incorporates the
mjr 48:058ace2aed1d 231 // LedWiz protocol as a subset, and adds our own private extensions.
mjr 48:058ace2aed1d 232 // For full details, see USBProtocol.h.
mjr 33:d832bcab089e 233
mjr 33:d832bcab089e 234
mjr 0:5acbbe3f4cf4 235 #include "mbed.h"
mjr 6:cc35eb643e8f 236 #include "math.h"
mjr 74:822a92bc11d2 237 #include "diags.h"
mjr 48:058ace2aed1d 238 #include "pinscape.h"
mjr 79:682ae3171a08 239 #include "NewMalloc.h"
mjr 0:5acbbe3f4cf4 240 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 241 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 242 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 243 #include "crc32.h"
mjr 26:cb71c4af2912 244 #include "TLC5940.h"
mjr 87:8d35c74403af 245 #include "TLC59116.h"
mjr 34:6b981a2afab7 246 #include "74HC595.h"
mjr 35:e959ffba78fd 247 #include "nvm.h"
mjr 48:058ace2aed1d 248 #include "TinyDigitalIn.h"
mjr 77:0b96f6867312 249 #include "IRReceiver.h"
mjr 77:0b96f6867312 250 #include "IRTransmitter.h"
mjr 77:0b96f6867312 251 #include "NewPwm.h"
mjr 74:822a92bc11d2 252
mjr 82:4f6209cb5c33 253 // plunger sensors
mjr 82:4f6209cb5c33 254 #include "plunger.h"
mjr 82:4f6209cb5c33 255 #include "edgeSensor.h"
mjr 82:4f6209cb5c33 256 #include "potSensor.h"
mjr 82:4f6209cb5c33 257 #include "quadSensor.h"
mjr 82:4f6209cb5c33 258 #include "nullSensor.h"
mjr 82:4f6209cb5c33 259 #include "barCodeSensor.h"
mjr 82:4f6209cb5c33 260 #include "distanceSensor.h"
mjr 87:8d35c74403af 261 #include "tsl14xxSensor.h"
mjr 82:4f6209cb5c33 262
mjr 2:c174f9ee414a 263
mjr 21:5048e16cc9ef 264 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 265 #include "config.h"
mjr 17:ab3cec0c8bf4 266
mjr 76:7f5912b6340e 267 // forward declarations
mjr 76:7f5912b6340e 268 static void waitPlungerIdle(void);
mjr 53:9b2611964afc 269
mjr 53:9b2611964afc 270 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 271 //
mjr 53:9b2611964afc 272 // OpenSDA module identifier. This is for the benefit of the Windows
mjr 53:9b2611964afc 273 // configuration tool. When the config tool installs a .bin file onto
mjr 53:9b2611964afc 274 // the KL25Z, it will first find the sentinel string within the .bin file,
mjr 53:9b2611964afc 275 // and patch the "\0" bytes that follow the sentinel string with the
mjr 53:9b2611964afc 276 // OpenSDA module ID data. This allows us to report the OpenSDA
mjr 53:9b2611964afc 277 // identifiers back to the host system via USB, which in turn allows the
mjr 53:9b2611964afc 278 // config tool to figure out which OpenSDA MSD (mass storage device - a
mjr 53:9b2611964afc 279 // virtual disk drive) correlates to which Pinscape controller USB
mjr 53:9b2611964afc 280 // interface.
mjr 53:9b2611964afc 281 //
mjr 53:9b2611964afc 282 // This is only important if multiple Pinscape devices are attached to
mjr 53:9b2611964afc 283 // the same host. There doesn't seem to be any other way to figure out
mjr 53:9b2611964afc 284 // which OpenSDA MSD corresponds to which KL25Z USB interface; the OpenSDA
mjr 53:9b2611964afc 285 // MSD doesn't report the KL25Z CPU ID anywhere, and the KL25Z doesn't
mjr 53:9b2611964afc 286 // have any way to learn about the OpenSDA module it's connected to. The
mjr 53:9b2611964afc 287 // only way to pass this information to the KL25Z side that I can come up
mjr 53:9b2611964afc 288 // with is to have the Windows host embed it in the .bin file before
mjr 53:9b2611964afc 289 // downloading it to the OpenSDA MSD.
mjr 53:9b2611964afc 290 //
mjr 53:9b2611964afc 291 // We initialize the const data buffer (the part after the sentinel string)
mjr 53:9b2611964afc 292 // with all "\0" bytes, so that's what will be in the executable image that
mjr 53:9b2611964afc 293 // comes out of the mbed compiler. If you manually install the resulting
mjr 53:9b2611964afc 294 // .bin file onto the KL25Z (via the Windows desktop, say), the "\0" bytes
mjr 53:9b2611964afc 295 // will stay this way and read as all 0's at run-time. Since a real TUID
mjr 53:9b2611964afc 296 // would never be all 0's, that tells us that we were never patched and
mjr 53:9b2611964afc 297 // thus don't have any information on the OpenSDA module.
mjr 53:9b2611964afc 298 //
mjr 53:9b2611964afc 299 const char *getOpenSDAID()
mjr 53:9b2611964afc 300 {
mjr 53:9b2611964afc 301 #define OPENSDA_PREFIX "///Pinscape.OpenSDA.TUID///"
mjr 53:9b2611964afc 302 static const char OpenSDA[] = OPENSDA_PREFIX "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0///";
mjr 53:9b2611964afc 303 const size_t OpenSDA_prefix_length = sizeof(OPENSDA_PREFIX) - 1;
mjr 53:9b2611964afc 304
mjr 53:9b2611964afc 305 return OpenSDA + OpenSDA_prefix_length;
mjr 53:9b2611964afc 306 }
mjr 53:9b2611964afc 307
mjr 53:9b2611964afc 308 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 309 //
mjr 53:9b2611964afc 310 // Build ID. We use the date and time of compiling the program as a build
mjr 53:9b2611964afc 311 // identifier. It would be a little nicer to use a simple serial number
mjr 53:9b2611964afc 312 // instead, but the mbed platform doesn't have a way to automate that. The
mjr 53:9b2611964afc 313 // timestamp is a pretty good proxy for a serial number in that it will
mjr 53:9b2611964afc 314 // naturally increase on each new build, which is the primary property we
mjr 53:9b2611964afc 315 // want from this.
mjr 53:9b2611964afc 316 //
mjr 53:9b2611964afc 317 // As with the embedded OpenSDA ID, we store the build timestamp with a
mjr 53:9b2611964afc 318 // sentinel string prefix, to allow automated tools to find the static data
mjr 53:9b2611964afc 319 // in the .bin file by searching for the sentinel string. In contrast to
mjr 53:9b2611964afc 320 // the OpenSDA ID, the value we store here is for tools to extract rather
mjr 53:9b2611964afc 321 // than store, since we automatically populate it via the preprocessor
mjr 53:9b2611964afc 322 // macros.
mjr 53:9b2611964afc 323 //
mjr 53:9b2611964afc 324 const char *getBuildID()
mjr 53:9b2611964afc 325 {
mjr 53:9b2611964afc 326 #define BUILDID_PREFIX "///Pinscape.Build.ID///"
mjr 53:9b2611964afc 327 static const char BuildID[] = BUILDID_PREFIX __DATE__ " " __TIME__ "///";
mjr 53:9b2611964afc 328 const size_t BuildID_prefix_length = sizeof(BUILDID_PREFIX) - 1;
mjr 53:9b2611964afc 329
mjr 53:9b2611964afc 330 return BuildID + BuildID_prefix_length;
mjr 53:9b2611964afc 331 }
mjr 53:9b2611964afc 332
mjr 74:822a92bc11d2 333 // --------------------------------------------------------------------------
mjr 74:822a92bc11d2 334 // Main loop iteration timing statistics. Collected only if
mjr 74:822a92bc11d2 335 // ENABLE_DIAGNOSTICS is set in diags.h.
mjr 76:7f5912b6340e 336 #if ENABLE_DIAGNOSTICS
mjr 76:7f5912b6340e 337 uint64_t mainLoopIterTime, mainLoopIterCheckpt[15], mainLoopIterCount;
mjr 76:7f5912b6340e 338 uint64_t mainLoopMsgTime, mainLoopMsgCount;
mjr 76:7f5912b6340e 339 Timer mainLoopTimer;
mjr 76:7f5912b6340e 340 #endif
mjr 76:7f5912b6340e 341
mjr 53:9b2611964afc 342
mjr 5:a70c0bce770d 343 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 344 //
mjr 38:091e511ce8a0 345 // Forward declarations
mjr 38:091e511ce8a0 346 //
mjr 38:091e511ce8a0 347 void setNightMode(bool on);
mjr 38:091e511ce8a0 348 void toggleNightMode();
mjr 38:091e511ce8a0 349
mjr 38:091e511ce8a0 350 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 351 // utilities
mjr 17:ab3cec0c8bf4 352
mjr 77:0b96f6867312 353 // int/float point square of a number
mjr 77:0b96f6867312 354 inline int square(int x) { return x*x; }
mjr 26:cb71c4af2912 355 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 356
mjr 26:cb71c4af2912 357 // floating point rounding
mjr 26:cb71c4af2912 358 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 359
mjr 17:ab3cec0c8bf4 360
mjr 33:d832bcab089e 361 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 362 //
mjr 40:cc0d9814522b 363 // Extended verison of Timer class. This adds the ability to interrogate
mjr 40:cc0d9814522b 364 // the running state.
mjr 40:cc0d9814522b 365 //
mjr 77:0b96f6867312 366 class ExtTimer: public Timer
mjr 40:cc0d9814522b 367 {
mjr 40:cc0d9814522b 368 public:
mjr 77:0b96f6867312 369 ExtTimer() : running(false) { }
mjr 40:cc0d9814522b 370
mjr 40:cc0d9814522b 371 void start() { running = true; Timer::start(); }
mjr 40:cc0d9814522b 372 void stop() { running = false; Timer::stop(); }
mjr 40:cc0d9814522b 373
mjr 40:cc0d9814522b 374 bool isRunning() const { return running; }
mjr 40:cc0d9814522b 375
mjr 40:cc0d9814522b 376 private:
mjr 40:cc0d9814522b 377 bool running;
mjr 40:cc0d9814522b 378 };
mjr 40:cc0d9814522b 379
mjr 53:9b2611964afc 380
mjr 53:9b2611964afc 381 // --------------------------------------------------------------------------
mjr 40:cc0d9814522b 382 //
mjr 33:d832bcab089e 383 // USB product version number
mjr 5:a70c0bce770d 384 //
mjr 47:df7a88cd249c 385 const uint16_t USB_VERSION_NO = 0x000A;
mjr 33:d832bcab089e 386
mjr 33:d832bcab089e 387 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 388 //
mjr 6:cc35eb643e8f 389 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 390 //
mjr 6:cc35eb643e8f 391 #define JOYMAX 4096
mjr 6:cc35eb643e8f 392
mjr 9:fd65b0a94720 393
mjr 17:ab3cec0c8bf4 394 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 395 //
mjr 40:cc0d9814522b 396 // Wire protocol value translations. These translate byte values to and
mjr 40:cc0d9814522b 397 // from the USB protocol to local native format.
mjr 35:e959ffba78fd 398 //
mjr 35:e959ffba78fd 399
mjr 35:e959ffba78fd 400 // unsigned 16-bit integer
mjr 35:e959ffba78fd 401 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 402 {
mjr 35:e959ffba78fd 403 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 404 }
mjr 40:cc0d9814522b 405 inline void ui16Wire(uint8_t *b, uint16_t val)
mjr 40:cc0d9814522b 406 {
mjr 40:cc0d9814522b 407 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 408 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 409 }
mjr 35:e959ffba78fd 410
mjr 35:e959ffba78fd 411 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 412 {
mjr 35:e959ffba78fd 413 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 414 }
mjr 40:cc0d9814522b 415 inline void i16Wire(uint8_t *b, int16_t val)
mjr 40:cc0d9814522b 416 {
mjr 40:cc0d9814522b 417 ui16Wire(b, (uint16_t)val);
mjr 40:cc0d9814522b 418 }
mjr 35:e959ffba78fd 419
mjr 35:e959ffba78fd 420 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 421 {
mjr 35:e959ffba78fd 422 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 423 }
mjr 40:cc0d9814522b 424 inline void ui32Wire(uint8_t *b, uint32_t val)
mjr 40:cc0d9814522b 425 {
mjr 40:cc0d9814522b 426 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 427 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 428 b[2] = (uint8_t)((val >> 16) & 0xff);
mjr 40:cc0d9814522b 429 b[3] = (uint8_t)((val >> 24) & 0xff);
mjr 40:cc0d9814522b 430 }
mjr 35:e959ffba78fd 431
mjr 35:e959ffba78fd 432 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 433 {
mjr 35:e959ffba78fd 434 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 435 }
mjr 35:e959ffba78fd 436
mjr 53:9b2611964afc 437 // Convert "wire" (USB) pin codes to/from PinName values.
mjr 53:9b2611964afc 438 //
mjr 53:9b2611964afc 439 // The internal mbed PinName format is
mjr 53:9b2611964afc 440 //
mjr 53:9b2611964afc 441 // ((port) << PORT_SHIFT) | (pin << 2) // MBED FORMAT
mjr 53:9b2611964afc 442 //
mjr 53:9b2611964afc 443 // where 'port' is 0-4 for Port A to Port E, and 'pin' is
mjr 53:9b2611964afc 444 // 0 to 31. E.g., E31 is (4 << PORT_SHIFT) | (31<<2).
mjr 53:9b2611964afc 445 //
mjr 53:9b2611964afc 446 // We remap this to our more compact wire format where each
mjr 53:9b2611964afc 447 // pin name fits in 8 bits:
mjr 53:9b2611964afc 448 //
mjr 53:9b2611964afc 449 // ((port) << 5) | pin) // WIRE FORMAT
mjr 53:9b2611964afc 450 //
mjr 53:9b2611964afc 451 // E.g., E31 is (4 << 5) | 31.
mjr 53:9b2611964afc 452 //
mjr 53:9b2611964afc 453 // Wire code FF corresponds to PinName NC (not connected).
mjr 53:9b2611964afc 454 //
mjr 53:9b2611964afc 455 inline PinName wirePinName(uint8_t c)
mjr 35:e959ffba78fd 456 {
mjr 53:9b2611964afc 457 if (c == 0xFF)
mjr 53:9b2611964afc 458 return NC; // 0xFF -> NC
mjr 53:9b2611964afc 459 else
mjr 53:9b2611964afc 460 return PinName(
mjr 53:9b2611964afc 461 (int(c & 0xE0) << (PORT_SHIFT - 5)) // top three bits are the port
mjr 53:9b2611964afc 462 | (int(c & 0x1F) << 2)); // bottom five bits are pin
mjr 40:cc0d9814522b 463 }
mjr 40:cc0d9814522b 464 inline void pinNameWire(uint8_t *b, PinName n)
mjr 40:cc0d9814522b 465 {
mjr 53:9b2611964afc 466 *b = PINNAME_TO_WIRE(n);
mjr 35:e959ffba78fd 467 }
mjr 35:e959ffba78fd 468
mjr 35:e959ffba78fd 469
mjr 35:e959ffba78fd 470 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 471 //
mjr 38:091e511ce8a0 472 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 473 //
mjr 38:091e511ce8a0 474 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 475 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 476 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 477 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 478 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 479 // SPI capability.
mjr 38:091e511ce8a0 480 //
mjr 38:091e511ce8a0 481 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 482
mjr 73:4e8ce0b18915 483 // Power on timer state for diagnostics. We flash the blue LED when
mjr 77:0b96f6867312 484 // nothing else is going on. State 0-1 = off, 2-3 = on blue. Also
mjr 77:0b96f6867312 485 // show red when transmitting an LED signal, indicated by state 4.
mjr 73:4e8ce0b18915 486 uint8_t powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 487
mjr 38:091e511ce8a0 488 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 489 // on, and -1 is no change (leaves the current setting intact).
mjr 73:4e8ce0b18915 490 static uint8_t diagLEDState = 0;
mjr 38:091e511ce8a0 491 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 492 {
mjr 73:4e8ce0b18915 493 // remember the new state
mjr 73:4e8ce0b18915 494 diagLEDState = r | (g << 1) | (b << 2);
mjr 73:4e8ce0b18915 495
mjr 73:4e8ce0b18915 496 // if turning everything off, use the power timer state instead,
mjr 73:4e8ce0b18915 497 // applying it to the blue LED
mjr 73:4e8ce0b18915 498 if (diagLEDState == 0)
mjr 77:0b96f6867312 499 {
mjr 77:0b96f6867312 500 b = (powerTimerDiagState == 2 || powerTimerDiagState == 3);
mjr 77:0b96f6867312 501 r = (powerTimerDiagState == 4);
mjr 77:0b96f6867312 502 }
mjr 73:4e8ce0b18915 503
mjr 73:4e8ce0b18915 504 // set the new state
mjr 38:091e511ce8a0 505 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 506 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 507 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 508 }
mjr 38:091e511ce8a0 509
mjr 73:4e8ce0b18915 510 // update the LEDs with the current state
mjr 73:4e8ce0b18915 511 void diagLED(void)
mjr 73:4e8ce0b18915 512 {
mjr 73:4e8ce0b18915 513 diagLED(
mjr 73:4e8ce0b18915 514 diagLEDState & 0x01,
mjr 73:4e8ce0b18915 515 (diagLEDState >> 1) & 0x01,
mjr 77:0b96f6867312 516 (diagLEDState >> 2) & 0x01);
mjr 73:4e8ce0b18915 517 }
mjr 73:4e8ce0b18915 518
mjr 38:091e511ce8a0 519 // check an output port assignment to see if it conflicts with
mjr 38:091e511ce8a0 520 // an on-board LED segment
mjr 38:091e511ce8a0 521 struct LedSeg
mjr 38:091e511ce8a0 522 {
mjr 38:091e511ce8a0 523 bool r, g, b;
mjr 38:091e511ce8a0 524 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 525
mjr 38:091e511ce8a0 526 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 527 {
mjr 38:091e511ce8a0 528 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 529 // our on-board LED segments
mjr 38:091e511ce8a0 530 int t = pc.typ;
mjr 38:091e511ce8a0 531 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 38:091e511ce8a0 532 {
mjr 38:091e511ce8a0 533 // it's a GPIO port - check for a matching pin assignment
mjr 38:091e511ce8a0 534 PinName pin = wirePinName(pc.pin);
mjr 38:091e511ce8a0 535 if (pin == LED1)
mjr 38:091e511ce8a0 536 r = true;
mjr 38:091e511ce8a0 537 else if (pin == LED2)
mjr 38:091e511ce8a0 538 g = true;
mjr 38:091e511ce8a0 539 else if (pin == LED3)
mjr 38:091e511ce8a0 540 b = true;
mjr 38:091e511ce8a0 541 }
mjr 38:091e511ce8a0 542 }
mjr 38:091e511ce8a0 543 };
mjr 38:091e511ce8a0 544
mjr 38:091e511ce8a0 545 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 546 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 547 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 548 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 549 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 550 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 551 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 552 {
mjr 38:091e511ce8a0 553 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 554 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 555 LedSeg l;
mjr 38:091e511ce8a0 556 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 557 l.check(cfg.outPort[i]);
mjr 38:091e511ce8a0 558
mjr 38:091e511ce8a0 559 // We now know which segments are taken for LedWiz use and which
mjr 38:091e511ce8a0 560 // are free. Create diagnostic ports for the ones not claimed for
mjr 38:091e511ce8a0 561 // LedWiz use.
mjr 38:091e511ce8a0 562 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 563 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 564 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 565 }
mjr 38:091e511ce8a0 566
mjr 38:091e511ce8a0 567
mjr 38:091e511ce8a0 568 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 569 //
mjr 76:7f5912b6340e 570 // LedWiz emulation
mjr 76:7f5912b6340e 571 //
mjr 76:7f5912b6340e 572
mjr 76:7f5912b6340e 573 // LedWiz output states.
mjr 76:7f5912b6340e 574 //
mjr 76:7f5912b6340e 575 // The LedWiz protocol has two separate control axes for each output.
mjr 76:7f5912b6340e 576 // One axis is its on/off state; the other is its "profile" state, which
mjr 76:7f5912b6340e 577 // is either a fixed brightness or a blinking pattern for the light.
mjr 76:7f5912b6340e 578 // The two axes are independent.
mjr 76:7f5912b6340e 579 //
mjr 76:7f5912b6340e 580 // Even though the original LedWiz protocol can only access 32 ports, we
mjr 76:7f5912b6340e 581 // maintain LedWiz state for every port, even if we have more than 32. Our
mjr 76:7f5912b6340e 582 // extended protocol allows the client to send LedWiz-style messages that
mjr 76:7f5912b6340e 583 // control any set of ports. A replacement LEDWIZ.DLL can make a single
mjr 76:7f5912b6340e 584 // Pinscape unit look like multiple virtual LedWiz units to legacy clients,
mjr 76:7f5912b6340e 585 // allowing them to control all of our ports. The clients will still be
mjr 76:7f5912b6340e 586 // using LedWiz-style states to control the ports, so we need to support
mjr 76:7f5912b6340e 587 // the LedWiz scheme with separate on/off and brightness control per port.
mjr 76:7f5912b6340e 588
mjr 76:7f5912b6340e 589 // On/off state for each LedWiz output
mjr 76:7f5912b6340e 590 static uint8_t *wizOn;
mjr 76:7f5912b6340e 591
mjr 76:7f5912b6340e 592 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 76:7f5912b6340e 593 // for each LedWiz output. If the output was last updated through an
mjr 76:7f5912b6340e 594 // LedWiz protocol message, it will have one of these values:
mjr 76:7f5912b6340e 595 //
mjr 76:7f5912b6340e 596 // 0-48 = fixed brightness 0% to 100%
mjr 76:7f5912b6340e 597 // 49 = fixed brightness 100% (equivalent to 48)
mjr 76:7f5912b6340e 598 // 129 = ramp up / ramp down
mjr 76:7f5912b6340e 599 // 130 = flash on / off
mjr 76:7f5912b6340e 600 // 131 = on / ramp down
mjr 76:7f5912b6340e 601 // 132 = ramp up / on
mjr 5:a70c0bce770d 602 //
mjr 76:7f5912b6340e 603 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 76:7f5912b6340e 604 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 76:7f5912b6340e 605 // it, so we need to accept it for compatibility.)
mjr 76:7f5912b6340e 606 static uint8_t *wizVal;
mjr 76:7f5912b6340e 607
mjr 76:7f5912b6340e 608 // Current actual brightness for each output. This is a simple linear
mjr 76:7f5912b6340e 609 // value on a 0..255 scale. This is EITHER the linear brightness computed
mjr 76:7f5912b6340e 610 // from the LedWiz setting for the port, OR the 0..255 value set explicitly
mjr 76:7f5912b6340e 611 // by the extended protocol:
mjr 76:7f5912b6340e 612 //
mjr 76:7f5912b6340e 613 // - If the last command that updated the port was an extended protocol
mjr 76:7f5912b6340e 614 // SET BRIGHTNESS command, this is the value set by that command. In
mjr 76:7f5912b6340e 615 // addition, wizOn[port] is set to 0 if the brightness is 0, 1 otherwise;
mjr 76:7f5912b6340e 616 // and wizVal[port] is set to the brightness rescaled to the 0..48 range
mjr 76:7f5912b6340e 617 // if the brightness is non-zero.
mjr 76:7f5912b6340e 618 //
mjr 76:7f5912b6340e 619 // - If the last command that updated the port was an LedWiz command
mjr 76:7f5912b6340e 620 // (SBA/PBA/SBX/PBX), this contains the brightness value computed from
mjr 76:7f5912b6340e 621 // the combination of wizOn[port] and wizVal[port]. If wizOn[port] is
mjr 76:7f5912b6340e 622 // zero, this is simply 0, otherwise it's wizVal[port] rescaled to the
mjr 76:7f5912b6340e 623 // 0..255 range.
mjr 26:cb71c4af2912 624 //
mjr 76:7f5912b6340e 625 // - For a port set to wizOn[port]=1 and wizVal[port] in 129..132, this is
mjr 76:7f5912b6340e 626 // also updated continuously to reflect the current flashing brightness
mjr 76:7f5912b6340e 627 // level.
mjr 26:cb71c4af2912 628 //
mjr 76:7f5912b6340e 629 static uint8_t *outLevel;
mjr 76:7f5912b6340e 630
mjr 76:7f5912b6340e 631
mjr 76:7f5912b6340e 632 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 76:7f5912b6340e 633 // rate for lights in blinking states. The LedWiz API doesn't document
mjr 76:7f5912b6340e 634 // what the numbers mean in real time units, but by observation, the
mjr 76:7f5912b6340e 635 // "speed" setting represents the period of the flash cycle in 0.25s
mjr 76:7f5912b6340e 636 // units, so speed 1 = 0.25 period = 4Hz, speed 7 = 1.75s period = 0.57Hz.
mjr 76:7f5912b6340e 637 // The period is the full cycle time of the flash waveform.
mjr 76:7f5912b6340e 638 //
mjr 76:7f5912b6340e 639 // Each bank of 32 lights has its independent own pulse rate, so we need
mjr 76:7f5912b6340e 640 // one entry per bank. Each bank has 32 outputs, so we need a total of
mjr 76:7f5912b6340e 641 // ceil(number_of_physical_outputs/32) entries. Note that we could allocate
mjr 76:7f5912b6340e 642 // this dynamically once we know the number of actual outputs, but the
mjr 76:7f5912b6340e 643 // upper limit is low enough that it's more efficient to use a fixed array
mjr 76:7f5912b6340e 644 // at the maximum size.
mjr 76:7f5912b6340e 645 static const int MAX_LW_BANKS = (MAX_OUT_PORTS+31)/32;
mjr 76:7f5912b6340e 646 static uint8_t wizSpeed[MAX_LW_BANKS];
mjr 29:582472d0bc57 647
mjr 26:cb71c4af2912 648 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 649 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 650 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 651 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 652 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 653
mjr 76:7f5912b6340e 654
mjr 76:7f5912b6340e 655 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 656 //
mjr 76:7f5912b6340e 657 // Output Ports
mjr 76:7f5912b6340e 658 //
mjr 76:7f5912b6340e 659 // There are two way to connect outputs. First, you can use the on-board
mjr 76:7f5912b6340e 660 // GPIO ports to implement device outputs: each LedWiz software port is
mjr 76:7f5912b6340e 661 // connected to a physical GPIO pin on the KL25Z. This has some pretty
mjr 76:7f5912b6340e 662 // strict limits, though. The KL25Z only has 10 PWM channels, so only 10
mjr 76:7f5912b6340e 663 // GPIO LedWiz ports can be made dimmable; the rest are strictly on/off.
mjr 76:7f5912b6340e 664 // The KL25Z also simply doesn't have enough exposed GPIO ports overall to
mjr 76:7f5912b6340e 665 // support all of the features the software supports. The software allows
mjr 76:7f5912b6340e 666 // for up to 128 outputs, 48 button inputs, plunger input (requiring 1-5
mjr 76:7f5912b6340e 667 // GPIO pins), and various other external devices. The KL25Z only exposes
mjr 76:7f5912b6340e 668 // about 50 GPIO pins. So if you want to do everything with GPIO ports,
mjr 76:7f5912b6340e 669 // you have to ration pins among features.
mjr 76:7f5912b6340e 670 //
mjr 87:8d35c74403af 671 // To overcome some of these limitations, we also support several external
mjr 76:7f5912b6340e 672 // peripheral controllers that allow adding many more outputs, using only
mjr 87:8d35c74403af 673 // a small number of GPIO pins to interface with the peripherals:
mjr 87:8d35c74403af 674 //
mjr 87:8d35c74403af 675 // - TLC5940 PWM controller chips. Each TLC5940 provides 16 ports with
mjr 87:8d35c74403af 676 // 12-bit PWM, and multiple TLC5940 chips can be daisy-chained. The
mjr 87:8d35c74403af 677 // chips connect via 5 GPIO pins, and since they're daisy-chainable,
mjr 87:8d35c74403af 678 // one set of 5 pins can control any number of the chips. So this chip
mjr 87:8d35c74403af 679 // effectively converts 5 GPIO pins into almost any number of PWM outputs.
mjr 87:8d35c74403af 680 //
mjr 87:8d35c74403af 681 // - TLC59116 PWM controller chips. These are similar to the TLC5940 but
mjr 87:8d35c74403af 682 // a newer generation with an improved design. These use an I2C bus,
mjr 87:8d35c74403af 683 // allowing up to 14 chips to be connected via 3 GPIO pins.
mjr 87:8d35c74403af 684 //
mjr 87:8d35c74403af 685 // - 74HC595 shift register chips. These provide 8 digital (on/off only)
mjr 87:8d35c74403af 686 // outputs per chip. These need 4 GPIO pins, and like the other can be
mjr 87:8d35c74403af 687 // daisy chained to add more outputs without using more GPIO pins. These
mjr 87:8d35c74403af 688 // are advantageous for outputs that don't require PWM, since the data
mjr 87:8d35c74403af 689 // transfer sizes are so much smaller. The expansion boards use these
mjr 87:8d35c74403af 690 // for the chime board outputs.
mjr 76:7f5912b6340e 691 //
mjr 76:7f5912b6340e 692 // Direct GPIO output ports and peripheral controllers can be mixed and
mjr 76:7f5912b6340e 693 // matched in one system. The assignment of pins to ports and the
mjr 76:7f5912b6340e 694 // configuration of peripheral controllers is all handled in the software
mjr 76:7f5912b6340e 695 // setup, so a physical system can be expanded and updated at any time.
mjr 76:7f5912b6340e 696 //
mjr 76:7f5912b6340e 697 // To handle the diversity of output port types, we start with an abstract
mjr 76:7f5912b6340e 698 // base class for outputs. Each type of physical output interface has a
mjr 76:7f5912b6340e 699 // concrete subclass. During initialization, we create the appropriate
mjr 76:7f5912b6340e 700 // subclass for each software port, mapping it to the assigned GPIO pin
mjr 76:7f5912b6340e 701 // or peripheral port. Most of the rest of the software only cares about
mjr 76:7f5912b6340e 702 // the abstract interface, so once the subclassed port objects are set up,
mjr 76:7f5912b6340e 703 // the rest of the system can control the ports without knowing which types
mjr 76:7f5912b6340e 704 // of physical devices they're connected to.
mjr 76:7f5912b6340e 705
mjr 76:7f5912b6340e 706
mjr 26:cb71c4af2912 707 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 708 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 709 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 710 class LwOut
mjr 6:cc35eb643e8f 711 {
mjr 6:cc35eb643e8f 712 public:
mjr 40:cc0d9814522b 713 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 714 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 715 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 716 };
mjr 26:cb71c4af2912 717
mjr 35:e959ffba78fd 718 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 719 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 720 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 721 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 722 // numbering.
mjr 35:e959ffba78fd 723 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 724 {
mjr 33:d832bcab089e 725 public:
mjr 35:e959ffba78fd 726 LwVirtualOut() { }
mjr 40:cc0d9814522b 727 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 728 };
mjr 26:cb71c4af2912 729
mjr 34:6b981a2afab7 730 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 731 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 732 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 733 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 734 {
mjr 34:6b981a2afab7 735 public:
mjr 34:6b981a2afab7 736 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 737 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 738
mjr 34:6b981a2afab7 739 private:
mjr 53:9b2611964afc 740 // underlying physical output
mjr 34:6b981a2afab7 741 LwOut *out;
mjr 34:6b981a2afab7 742 };
mjr 34:6b981a2afab7 743
mjr 53:9b2611964afc 744 // Global ZB Launch Ball state
mjr 53:9b2611964afc 745 bool zbLaunchOn = false;
mjr 53:9b2611964afc 746
mjr 53:9b2611964afc 747 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 748 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 749 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 750 {
mjr 53:9b2611964afc 751 public:
mjr 53:9b2611964afc 752 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 753 virtual void set(uint8_t val)
mjr 53:9b2611964afc 754 {
mjr 53:9b2611964afc 755 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 756 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 757
mjr 53:9b2611964afc 758 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 759 out->set(val);
mjr 53:9b2611964afc 760 }
mjr 53:9b2611964afc 761
mjr 53:9b2611964afc 762 private:
mjr 53:9b2611964afc 763 // underlying physical or virtual output
mjr 53:9b2611964afc 764 LwOut *out;
mjr 53:9b2611964afc 765 };
mjr 53:9b2611964afc 766
mjr 53:9b2611964afc 767
mjr 40:cc0d9814522b 768 // Gamma correction table for 8-bit input values
mjr 87:8d35c74403af 769 static const uint8_t dof_to_gamma_8bit[] = {
mjr 40:cc0d9814522b 770 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 771 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 772 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 773 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 774 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 775 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 776 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 777 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 778 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 779 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 780 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 781 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 782 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 783 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 784 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 785 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 786 };
mjr 40:cc0d9814522b 787
mjr 40:cc0d9814522b 788 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 789 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 790 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 791 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 792 {
mjr 40:cc0d9814522b 793 public:
mjr 40:cc0d9814522b 794 LwGammaOut(LwOut *o) : out(o) { }
mjr 87:8d35c74403af 795 virtual void set(uint8_t val) { out->set(dof_to_gamma_8bit[val]); }
mjr 40:cc0d9814522b 796
mjr 40:cc0d9814522b 797 private:
mjr 40:cc0d9814522b 798 LwOut *out;
mjr 40:cc0d9814522b 799 };
mjr 40:cc0d9814522b 800
mjr 77:0b96f6867312 801 // Global night mode flag. To minimize overhead when reporting
mjr 77:0b96f6867312 802 // the status, we set this to the status report flag bit for
mjr 77:0b96f6867312 803 // night mode, 0x02, when engaged.
mjr 77:0b96f6867312 804 static uint8_t nightMode = 0x00;
mjr 53:9b2611964afc 805
mjr 40:cc0d9814522b 806 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 807 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 808 // mode is engaged.
mjr 40:cc0d9814522b 809 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 810 {
mjr 40:cc0d9814522b 811 public:
mjr 40:cc0d9814522b 812 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 813 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 814
mjr 53:9b2611964afc 815 private:
mjr 53:9b2611964afc 816 LwOut *out;
mjr 53:9b2611964afc 817 };
mjr 53:9b2611964afc 818
mjr 53:9b2611964afc 819 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 820 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 821 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 822 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 823 {
mjr 53:9b2611964afc 824 public:
mjr 53:9b2611964afc 825 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 89:c43cd923401c 826 virtual void set(uint8_t)
mjr 53:9b2611964afc 827 {
mjr 53:9b2611964afc 828 // ignore the host value and simply show the current
mjr 53:9b2611964afc 829 // night mode setting
mjr 53:9b2611964afc 830 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 831 }
mjr 40:cc0d9814522b 832
mjr 40:cc0d9814522b 833 private:
mjr 40:cc0d9814522b 834 LwOut *out;
mjr 40:cc0d9814522b 835 };
mjr 40:cc0d9814522b 836
mjr 26:cb71c4af2912 837
mjr 89:c43cd923401c 838 // Flipper Logic output. This is a filter object that we layer on
mjr 89:c43cd923401c 839 // top of a physical pin output.
mjr 89:c43cd923401c 840 //
mjr 89:c43cd923401c 841 // A Flipper Logic output is effectively a digital output from the
mjr 89:c43cd923401c 842 // client's perspective, in that it ignores the intensity level and
mjr 89:c43cd923401c 843 // only pays attention to the ON/OFF state. 0 is OFF and any other
mjr 89:c43cd923401c 844 // level is ON.
mjr 89:c43cd923401c 845 //
mjr 89:c43cd923401c 846 // In terms of the physical output, though, we do use varying power.
mjr 89:c43cd923401c 847 // It's just that the varying power isn't under the client's control;
mjr 89:c43cd923401c 848 // we control it according to our flipperLogic settings:
mjr 89:c43cd923401c 849 //
mjr 89:c43cd923401c 850 // - When the software port transitions from OFF (0 brightness) to ON
mjr 89:c43cd923401c 851 // (any non-zero brightness level), we set the physical port to 100%
mjr 89:c43cd923401c 852 // power and start a timer.
mjr 89:c43cd923401c 853 //
mjr 89:c43cd923401c 854 // - When the full power time in our flipperLogic settings elapses,
mjr 89:c43cd923401c 855 // if the software port is still ON, we reduce the physical port to
mjr 89:c43cd923401c 856 // the PWM level in our flipperLogic setting.
mjr 89:c43cd923401c 857 //
mjr 89:c43cd923401c 858 class LwFlipperLogicOut: public LwOut
mjr 89:c43cd923401c 859 {
mjr 89:c43cd923401c 860 public:
mjr 89:c43cd923401c 861 // Set up the output. 'params' is the flipperLogic value from
mjr 89:c43cd923401c 862 // the configuration.
mjr 89:c43cd923401c 863 LwFlipperLogicOut(LwOut *o, uint8_t params)
mjr 89:c43cd923401c 864 : out(o), params(params)
mjr 89:c43cd923401c 865 {
mjr 89:c43cd923401c 866 // initially OFF
mjr 89:c43cd923401c 867 state = 0;
mjr 89:c43cd923401c 868 }
mjr 89:c43cd923401c 869
mjr 89:c43cd923401c 870 virtual void set(uint8_t level)
mjr 89:c43cd923401c 871 {
mjr 89:c43cd923401c 872 // remebmber the new nominal level set by the client
mjr 89:c43cd923401c 873 val = level;
mjr 89:c43cd923401c 874
mjr 89:c43cd923401c 875 // update the physical output according to our current timing state
mjr 89:c43cd923401c 876 switch (state)
mjr 89:c43cd923401c 877 {
mjr 89:c43cd923401c 878 case 0:
mjr 89:c43cd923401c 879 // We're currently off. If the new level is non-zero, switch
mjr 89:c43cd923401c 880 // to state 1 (initial full-power interval) and set the requested
mjr 89:c43cd923401c 881 // level. If the new level is zero, we're switching from off to
mjr 89:c43cd923401c 882 // off, so there's no change.
mjr 89:c43cd923401c 883 if (level != 0)
mjr 89:c43cd923401c 884 {
mjr 89:c43cd923401c 885 // switch to state 1 (initial full-power interval)
mjr 89:c43cd923401c 886 state = 1;
mjr 89:c43cd923401c 887
mjr 89:c43cd923401c 888 // set the requested output level - there's no limit during
mjr 89:c43cd923401c 889 // the initial full-power interval, so set the exact level
mjr 89:c43cd923401c 890 // requested
mjr 89:c43cd923401c 891 out->set(level);
mjr 89:c43cd923401c 892
mjr 89:c43cd923401c 893 // add myself to the pending timer list
mjr 89:c43cd923401c 894 pending[nPending++] = this;
mjr 89:c43cd923401c 895
mjr 89:c43cd923401c 896 // note the starting time
mjr 89:c43cd923401c 897 t0 = timer.read_us();
mjr 89:c43cd923401c 898 }
mjr 89:c43cd923401c 899 break;
mjr 89:c43cd923401c 900
mjr 89:c43cd923401c 901 case 1:
mjr 89:c43cd923401c 902 // Initial full-power interval. If the new level is non-zero,
mjr 89:c43cd923401c 903 // simply apply the new level as requested, since there's no
mjr 89:c43cd923401c 904 // limit during this period. If the new level is zero, shut
mjr 89:c43cd923401c 905 // off the output and cancel the pending timer.
mjr 89:c43cd923401c 906 out->set(level);
mjr 89:c43cd923401c 907 if (level == 0)
mjr 89:c43cd923401c 908 {
mjr 89:c43cd923401c 909 // We're switching off. In state 1, we have a pending timer,
mjr 89:c43cd923401c 910 // so we need to remove it from the list.
mjr 89:c43cd923401c 911 for (int i = 0 ; i < nPending ; ++i)
mjr 89:c43cd923401c 912 {
mjr 89:c43cd923401c 913 // is this us?
mjr 89:c43cd923401c 914 if (pending[i] == this)
mjr 89:c43cd923401c 915 {
mjr 89:c43cd923401c 916 // remove myself by replacing the slot with the
mjr 89:c43cd923401c 917 // last list entry
mjr 89:c43cd923401c 918 pending[i] = pending[--nPending];
mjr 89:c43cd923401c 919
mjr 89:c43cd923401c 920 // no need to look any further
mjr 89:c43cd923401c 921 break;
mjr 89:c43cd923401c 922 }
mjr 89:c43cd923401c 923 }
mjr 89:c43cd923401c 924
mjr 89:c43cd923401c 925 // switch to state 0 (off)
mjr 89:c43cd923401c 926 state = 0;
mjr 89:c43cd923401c 927 }
mjr 89:c43cd923401c 928 break;
mjr 89:c43cd923401c 929
mjr 89:c43cd923401c 930 case 2:
mjr 89:c43cd923401c 931 // Hold interval. If the new level is zero, switch to state
mjr 89:c43cd923401c 932 // 0 (off). If the new level is non-zero, stay in the hold
mjr 89:c43cd923401c 933 // state, and set the new level, applying the hold power setting
mjr 89:c43cd923401c 934 // as the upper bound.
mjr 89:c43cd923401c 935 if (level == 0)
mjr 89:c43cd923401c 936 {
mjr 89:c43cd923401c 937 // switching off - turn off the physical output
mjr 89:c43cd923401c 938 out->set(0);
mjr 89:c43cd923401c 939
mjr 89:c43cd923401c 940 // go to state 0 (off)
mjr 89:c43cd923401c 941 state = 0;
mjr 89:c43cd923401c 942 }
mjr 89:c43cd923401c 943 else
mjr 89:c43cd923401c 944 {
mjr 89:c43cd923401c 945 // staying on - set the new physical output power to the
mjr 89:c43cd923401c 946 // lower of the requested power and the hold power
mjr 89:c43cd923401c 947 uint8_t hold = holdPower();
mjr 89:c43cd923401c 948 out->set(level < hold ? level : hold);
mjr 89:c43cd923401c 949 }
mjr 89:c43cd923401c 950 break;
mjr 89:c43cd923401c 951 }
mjr 89:c43cd923401c 952 }
mjr 89:c43cd923401c 953
mjr 89:c43cd923401c 954 // Class initialization
mjr 89:c43cd923401c 955 static void classInit(Config &cfg)
mjr 89:c43cd923401c 956 {
mjr 89:c43cd923401c 957 // Count the Flipper Logic outputs in the configuration. We
mjr 89:c43cd923401c 958 // need to allocate enough pending timer list space to accommodate
mjr 89:c43cd923401c 959 // all of these outputs.
mjr 89:c43cd923401c 960 int n = 0;
mjr 89:c43cd923401c 961 for (int i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 89:c43cd923401c 962 {
mjr 89:c43cd923401c 963 // if this port is active and marked as Flipper Logic, count it
mjr 89:c43cd923401c 964 if (cfg.outPort[i].typ != PortTypeDisabled
mjr 89:c43cd923401c 965 && (cfg.outPort[i].flags & PortFlagFlipperLogic) != 0)
mjr 89:c43cd923401c 966 ++n;
mjr 89:c43cd923401c 967 }
mjr 89:c43cd923401c 968
mjr 89:c43cd923401c 969 // allocate space for the pending timer list
mjr 89:c43cd923401c 970 pending = new LwFlipperLogicOut*[n];
mjr 89:c43cd923401c 971
mjr 89:c43cd923401c 972 // there's nothing in the pending list yet
mjr 89:c43cd923401c 973 nPending = 0;
mjr 89:c43cd923401c 974
mjr 89:c43cd923401c 975 // Start our shared timer. The epoch is arbitrary, since we only
mjr 89:c43cd923401c 976 // use it to figure elapsed times.
mjr 89:c43cd923401c 977 timer.start();
mjr 89:c43cd923401c 978 }
mjr 89:c43cd923401c 979
mjr 89:c43cd923401c 980 // Check for ports with pending timers. The main routine should
mjr 89:c43cd923401c 981 // call this on each iteration to process our state transitions.
mjr 89:c43cd923401c 982 static void poll()
mjr 89:c43cd923401c 983 {
mjr 89:c43cd923401c 984 // note the current time
mjr 89:c43cd923401c 985 uint32_t t = timer.read_us();
mjr 89:c43cd923401c 986
mjr 89:c43cd923401c 987 // go through the timer list
mjr 89:c43cd923401c 988 for (int i = 0 ; i < nPending ; )
mjr 89:c43cd923401c 989 {
mjr 89:c43cd923401c 990 // get the port
mjr 89:c43cd923401c 991 LwFlipperLogicOut *port = pending[i];
mjr 89:c43cd923401c 992
mjr 89:c43cd923401c 993 // assume we'll keep it
mjr 89:c43cd923401c 994 bool remove = false;
mjr 89:c43cd923401c 995
mjr 89:c43cd923401c 996 // check if the port is still on
mjr 89:c43cd923401c 997 if (port->state != 0)
mjr 89:c43cd923401c 998 {
mjr 89:c43cd923401c 999 // it's still on - check if the initial full power time has elapsed
mjr 89:c43cd923401c 1000 if (uint32_t(t - port->t0) > port->fullPowerTime_us())
mjr 89:c43cd923401c 1001 {
mjr 89:c43cd923401c 1002 // done with the full power interval - switch to hold state
mjr 89:c43cd923401c 1003 port->state = 2;
mjr 89:c43cd923401c 1004
mjr 89:c43cd923401c 1005 // set the physical port to the hold power setting or the
mjr 89:c43cd923401c 1006 // client brightness setting, whichever is lower
mjr 89:c43cd923401c 1007 uint8_t hold = port->holdPower();
mjr 89:c43cd923401c 1008 uint8_t val = port->val;
mjr 89:c43cd923401c 1009 port->out->set(val < hold ? val : hold);
mjr 89:c43cd923401c 1010
mjr 89:c43cd923401c 1011 // we're done with the timer
mjr 89:c43cd923401c 1012 remove = true;
mjr 89:c43cd923401c 1013 }
mjr 89:c43cd923401c 1014 }
mjr 89:c43cd923401c 1015 else
mjr 89:c43cd923401c 1016 {
mjr 89:c43cd923401c 1017 // the port was turned off before the timer expired - remove
mjr 89:c43cd923401c 1018 // it from the timer list
mjr 89:c43cd923401c 1019 remove = true;
mjr 89:c43cd923401c 1020 }
mjr 89:c43cd923401c 1021
mjr 89:c43cd923401c 1022 // if desired, remove the port from the timer list
mjr 89:c43cd923401c 1023 if (remove)
mjr 89:c43cd923401c 1024 {
mjr 89:c43cd923401c 1025 // Remove the list entry by overwriting the slot with
mjr 89:c43cd923401c 1026 // the last entry in the list.
mjr 89:c43cd923401c 1027 pending[i] = pending[--nPending];
mjr 89:c43cd923401c 1028
mjr 89:c43cd923401c 1029 // Note that we don't increment the loop counter, since
mjr 89:c43cd923401c 1030 // we now need to revisit this same slot.
mjr 89:c43cd923401c 1031 }
mjr 89:c43cd923401c 1032 else
mjr 89:c43cd923401c 1033 {
mjr 89:c43cd923401c 1034 // we're keeping this item; move on to the next one
mjr 89:c43cd923401c 1035 ++i;
mjr 89:c43cd923401c 1036 }
mjr 89:c43cd923401c 1037 }
mjr 89:c43cd923401c 1038 }
mjr 89:c43cd923401c 1039
mjr 89:c43cd923401c 1040 protected:
mjr 89:c43cd923401c 1041 // underlying physical output
mjr 89:c43cd923401c 1042 LwOut *out;
mjr 89:c43cd923401c 1043
mjr 89:c43cd923401c 1044 // Timestamp on 'timer' of start of full-power interval. We set this
mjr 89:c43cd923401c 1045 // to the current 'timer' timestamp when entering state 1.
mjr 89:c43cd923401c 1046 uint32_t t0;
mjr 89:c43cd923401c 1047
mjr 89:c43cd923401c 1048 // Nominal output level (brightness) last set by the client. During
mjr 89:c43cd923401c 1049 // the initial full-power interval, we replicate the requested level
mjr 89:c43cd923401c 1050 // exactly on the physical output. During the hold interval, we limit
mjr 89:c43cd923401c 1051 // the physical output to the hold power, but use the caller's value
mjr 89:c43cd923401c 1052 // if it's lower.
mjr 89:c43cd923401c 1053 uint8_t val;
mjr 89:c43cd923401c 1054
mjr 89:c43cd923401c 1055 // Current port state:
mjr 89:c43cd923401c 1056 //
mjr 89:c43cd923401c 1057 // 0 = off
mjr 89:c43cd923401c 1058 // 1 = on at initial full power
mjr 89:c43cd923401c 1059 // 2 = on at hold power
mjr 89:c43cd923401c 1060 uint8_t state;
mjr 89:c43cd923401c 1061
mjr 89:c43cd923401c 1062 // Configuration parameters. The high 4 bits encode the initial full-
mjr 89:c43cd923401c 1063 // power time in 50ms units, starting at 0=50ms. The low 4 bits encode
mjr 89:c43cd923401c 1064 // the hold power (applied after the initial time expires if the output
mjr 89:c43cd923401c 1065 // is still on) in units of 6.66%. The resulting percentage is used
mjr 89:c43cd923401c 1066 // for the PWM duty cycle of the physical output.
mjr 89:c43cd923401c 1067 uint8_t params;
mjr 89:c43cd923401c 1068
mjr 89:c43cd923401c 1069 // Figure the initial full-power time in microseconds
mjr 89:c43cd923401c 1070 inline uint32_t fullPowerTime_us() const { return ((params >> 4) + 1)*50000; }
mjr 89:c43cd923401c 1071
mjr 89:c43cd923401c 1072 // Figure the hold power PWM level (0-255)
mjr 89:c43cd923401c 1073 inline uint8_t holdPower() const { return (params & 0x0F) * 17; }
mjr 89:c43cd923401c 1074
mjr 89:c43cd923401c 1075 // Timer. This is a shared timer for all of the FL ports. When we
mjr 89:c43cd923401c 1076 // transition from OFF to ON, we note the current time on this timer
mjr 89:c43cd923401c 1077 // (which runs continuously).
mjr 89:c43cd923401c 1078 static Timer timer;
mjr 89:c43cd923401c 1079
mjr 89:c43cd923401c 1080 // Flipper logic pending timer list. Whenever a flipper logic output
mjr 89:c43cd923401c 1081 // transitions from OFF to ON, tis timer
mjr 89:c43cd923401c 1082 static LwFlipperLogicOut **pending;
mjr 89:c43cd923401c 1083 static uint8_t nPending;
mjr 89:c43cd923401c 1084 };
mjr 89:c43cd923401c 1085
mjr 89:c43cd923401c 1086 // Flipper Logic statics
mjr 89:c43cd923401c 1087 Timer LwFlipperLogicOut::timer;
mjr 89:c43cd923401c 1088 LwFlipperLogicOut **LwFlipperLogicOut::pending;
mjr 89:c43cd923401c 1089 uint8_t LwFlipperLogicOut::nPending;
mjr 89:c43cd923401c 1090
mjr 35:e959ffba78fd 1091 //
mjr 35:e959ffba78fd 1092 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 1093 // assignments set in config.h.
mjr 33:d832bcab089e 1094 //
mjr 35:e959ffba78fd 1095 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 1096 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 1097 {
mjr 35:e959ffba78fd 1098 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 1099 {
mjr 53:9b2611964afc 1100 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 1101 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 1102 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 1103 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 1104 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 1105 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 1106 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 1107 }
mjr 35:e959ffba78fd 1108 }
mjr 26:cb71c4af2912 1109
mjr 40:cc0d9814522b 1110 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 1111 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 1112 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 1113 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 1114 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 1115 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 1116 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 1117 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 1118 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 1119 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 1120 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 1121 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 1122 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 1123 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 1124 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 1125 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 1126 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 1127 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 1128 };
mjr 40:cc0d9814522b 1129
mjr 40:cc0d9814522b 1130 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 1131 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 1132 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 1133 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 1134 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 1135 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 1136 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 1137 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 1138 // are always 8 bits.
mjr 40:cc0d9814522b 1139 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 1140 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 1141 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 1142 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 1143 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 1144 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 1145 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 1146 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 1147 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 1148 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 1149 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 1150 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 1151 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 1152 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 1153 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 1154 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 1155 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 1156 };
mjr 40:cc0d9814522b 1157
mjr 26:cb71c4af2912 1158 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 1159 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 1160 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 1161 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 1162 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 1163 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 1164 {
mjr 26:cb71c4af2912 1165 public:
mjr 60:f38da020aa13 1166 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1167 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 1168 {
mjr 26:cb71c4af2912 1169 if (val != prv)
mjr 40:cc0d9814522b 1170 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 1171 }
mjr 60:f38da020aa13 1172 uint8_t idx;
mjr 40:cc0d9814522b 1173 uint8_t prv;
mjr 26:cb71c4af2912 1174 };
mjr 26:cb71c4af2912 1175
mjr 40:cc0d9814522b 1176 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 1177 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 1178 {
mjr 40:cc0d9814522b 1179 public:
mjr 60:f38da020aa13 1180 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1181 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 1182 {
mjr 40:cc0d9814522b 1183 if (val != prv)
mjr 40:cc0d9814522b 1184 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 1185 }
mjr 60:f38da020aa13 1186 uint8_t idx;
mjr 40:cc0d9814522b 1187 uint8_t prv;
mjr 40:cc0d9814522b 1188 };
mjr 40:cc0d9814522b 1189
mjr 87:8d35c74403af 1190 //
mjr 87:8d35c74403af 1191 // TLC59116 interface object
mjr 87:8d35c74403af 1192 //
mjr 87:8d35c74403af 1193 TLC59116 *tlc59116 = 0;
mjr 87:8d35c74403af 1194 void init_tlc59116(Config &cfg)
mjr 87:8d35c74403af 1195 {
mjr 87:8d35c74403af 1196 // Create the interface if any chips are enabled
mjr 87:8d35c74403af 1197 if (cfg.tlc59116.chipMask != 0)
mjr 87:8d35c74403af 1198 {
mjr 87:8d35c74403af 1199 // set up the interface
mjr 87:8d35c74403af 1200 tlc59116 = new TLC59116(
mjr 87:8d35c74403af 1201 wirePinName(cfg.tlc59116.sda),
mjr 87:8d35c74403af 1202 wirePinName(cfg.tlc59116.scl),
mjr 87:8d35c74403af 1203 wirePinName(cfg.tlc59116.reset));
mjr 87:8d35c74403af 1204
mjr 87:8d35c74403af 1205 // initialize the chips
mjr 87:8d35c74403af 1206 tlc59116->init();
mjr 87:8d35c74403af 1207 }
mjr 87:8d35c74403af 1208 }
mjr 87:8d35c74403af 1209
mjr 87:8d35c74403af 1210 // LwOut class for TLC59116 outputs. The 'addr' value in the constructor
mjr 87:8d35c74403af 1211 // is low 4 bits of the chip's I2C address; this is the part of the address
mjr 87:8d35c74403af 1212 // that's configurable per chip. 'port' is the output number on the chip
mjr 87:8d35c74403af 1213 // (0-15).
mjr 87:8d35c74403af 1214 //
mjr 87:8d35c74403af 1215 // Note that we don't need a separate gamma-corrected subclass for this
mjr 87:8d35c74403af 1216 // output type, since there's no loss of precision with the standard layered
mjr 87:8d35c74403af 1217 // gamma (it emits 8-bit values, and we take 8-bit inputs).
mjr 87:8d35c74403af 1218 class Lw59116Out: public LwOut
mjr 87:8d35c74403af 1219 {
mjr 87:8d35c74403af 1220 public:
mjr 87:8d35c74403af 1221 Lw59116Out(uint8_t addr, uint8_t port) : addr(addr), port(port) { prv = 0; }
mjr 87:8d35c74403af 1222 virtual void set(uint8_t val)
mjr 87:8d35c74403af 1223 {
mjr 87:8d35c74403af 1224 if (val != prv)
mjr 87:8d35c74403af 1225 tlc59116->set(addr, port, prv = val);
mjr 87:8d35c74403af 1226 }
mjr 87:8d35c74403af 1227
mjr 87:8d35c74403af 1228 protected:
mjr 87:8d35c74403af 1229 uint8_t addr;
mjr 87:8d35c74403af 1230 uint8_t port;
mjr 87:8d35c74403af 1231 uint8_t prv;
mjr 87:8d35c74403af 1232 };
mjr 87:8d35c74403af 1233
mjr 87:8d35c74403af 1234
mjr 87:8d35c74403af 1235 //
mjr 34:6b981a2afab7 1236 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 1237 // config.h.
mjr 87:8d35c74403af 1238 //
mjr 35:e959ffba78fd 1239 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 1240
mjr 35:e959ffba78fd 1241 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 1242 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 1243 {
mjr 35:e959ffba78fd 1244 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 1245 {
mjr 53:9b2611964afc 1246 hc595 = new HC595(
mjr 53:9b2611964afc 1247 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 1248 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 1249 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 1250 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 1251 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 1252 hc595->init();
mjr 35:e959ffba78fd 1253 hc595->update();
mjr 35:e959ffba78fd 1254 }
mjr 35:e959ffba78fd 1255 }
mjr 34:6b981a2afab7 1256
mjr 34:6b981a2afab7 1257 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 1258 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 1259 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 1260 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 1261 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 1262 class Lw595Out: public LwOut
mjr 33:d832bcab089e 1263 {
mjr 33:d832bcab089e 1264 public:
mjr 60:f38da020aa13 1265 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1266 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 1267 {
mjr 34:6b981a2afab7 1268 if (val != prv)
mjr 40:cc0d9814522b 1269 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 1270 }
mjr 60:f38da020aa13 1271 uint8_t idx;
mjr 40:cc0d9814522b 1272 uint8_t prv;
mjr 33:d832bcab089e 1273 };
mjr 33:d832bcab089e 1274
mjr 26:cb71c4af2912 1275
mjr 40:cc0d9814522b 1276
mjr 64:ef7ca92dff36 1277 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 1278 // normalized to 0.0 to 1.0 scale.
mjr 74:822a92bc11d2 1279 static const float dof_to_pwm[] = {
mjr 64:ef7ca92dff36 1280 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 1281 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 1282 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 1283 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 1284 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 1285 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 1286 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 1287 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 1288 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 1289 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 1290 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 1291 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 1292 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 1293 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 1294 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 1295 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 1296 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 1297 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 1298 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 1299 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 1300 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 1301 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 1302 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 1303 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 1304 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 1305 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 1306 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 1307 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 1308 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 1309 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 1310 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 1311 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 1312 };
mjr 26:cb71c4af2912 1313
mjr 64:ef7ca92dff36 1314
mjr 64:ef7ca92dff36 1315 // Conversion table for 8-bit DOF level to pulse width in microseconds,
mjr 64:ef7ca92dff36 1316 // with gamma correction. We could use the layered gamma output on top
mjr 64:ef7ca92dff36 1317 // of the regular LwPwmOut class for this, but we get better precision
mjr 64:ef7ca92dff36 1318 // with a dedicated table, because we apply gamma correction to the
mjr 64:ef7ca92dff36 1319 // 32-bit microsecond values rather than the 8-bit DOF levels.
mjr 64:ef7ca92dff36 1320 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 1321 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 1322 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 1323 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 1324 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 1325 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 1326 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 1327 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 1328 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 1329 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 1330 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 1331 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 1332 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 1333 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 1334 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 1335 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 1336 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 1337 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 1338 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 1339 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 1340 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 1341 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 1342 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 1343 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 1344 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 1345 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 1346 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 1347 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 1348 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 1349 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 1350 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 1351 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 1352 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 1353 };
mjr 64:ef7ca92dff36 1354
mjr 77:0b96f6867312 1355 // Polled-update PWM output list
mjr 74:822a92bc11d2 1356 //
mjr 77:0b96f6867312 1357 // This is a workaround for a KL25Z hardware bug/limitation. The bug (more
mjr 77:0b96f6867312 1358 // about this below) is that we can't write to a PWM output "value" register
mjr 77:0b96f6867312 1359 // more than once per PWM cycle; if we do, outputs after the first are lost.
mjr 77:0b96f6867312 1360 // The value register controls the duty cycle, so it's what you have to write
mjr 77:0b96f6867312 1361 // if you want to update the brightness of an output.
mjr 74:822a92bc11d2 1362 //
mjr 77:0b96f6867312 1363 // Our solution is to simply repeat all PWM updates periodically. If a write
mjr 77:0b96f6867312 1364 // is lost on one cycle, it'll eventually be applied on a subseuqent periodic
mjr 77:0b96f6867312 1365 // update. For low overhead, we do these repeat updates periodically during
mjr 77:0b96f6867312 1366 // the main loop.
mjr 74:822a92bc11d2 1367 //
mjr 77:0b96f6867312 1368 // The mbed library has its own solution to this bug, but it creates a
mjr 77:0b96f6867312 1369 // separate problem of its own. The mbed solution is to write the value
mjr 77:0b96f6867312 1370 // register immediately, and then also reset the "count" register in the
mjr 77:0b96f6867312 1371 // TPM unit containing the output. The count reset truncates the current
mjr 77:0b96f6867312 1372 // PWM cycle, which avoids the hardware problem with more than one write per
mjr 77:0b96f6867312 1373 // cycle. The problem is that the truncated cycle causes visible flicker if
mjr 77:0b96f6867312 1374 // the output is connected to an LED. This is particularly noticeable during
mjr 77:0b96f6867312 1375 // fades, when we're updating the value register repeatedly and rapidly: an
mjr 77:0b96f6867312 1376 // attempt to fade from fully on to fully off causes rapid fluttering and
mjr 77:0b96f6867312 1377 // flashing rather than a smooth brightness fade.
mjr 74:822a92bc11d2 1378 //
mjr 77:0b96f6867312 1379 // The hardware bug is a case of good intentions gone bad. The hardware is
mjr 77:0b96f6867312 1380 // *supposed* to make it easy for software to avoid glitching during PWM
mjr 77:0b96f6867312 1381 // updates, by providing a staging register in front of the real value
mjr 77:0b96f6867312 1382 // register. The software actually writes to the staging register, which
mjr 77:0b96f6867312 1383 // holds updates until the end of the cycle, at which point the hardware
mjr 77:0b96f6867312 1384 // automatically moves the value from the staging register into the real
mjr 77:0b96f6867312 1385 // register. This ensures that the real register is always updated exactly
mjr 77:0b96f6867312 1386 // at a cycle boundary, which in turn ensures that there's no flicker when
mjr 77:0b96f6867312 1387 // values are updated. A great design - except that it doesn't quite work.
mjr 77:0b96f6867312 1388 // The problem is that the staging register actually seems to be implemented
mjr 77:0b96f6867312 1389 // as a one-element FIFO in "stop when full" mode. That is, when you write
mjr 77:0b96f6867312 1390 // the FIFO, it becomes full. When the cycle ends and the hardware reads it
mjr 77:0b96f6867312 1391 // to move the staged value into the real register, the FIFO becomes empty.
mjr 77:0b96f6867312 1392 // But if you try to write the FIFO twice before the hardware reads it and
mjr 77:0b96f6867312 1393 // empties it, the second write fails, leaving the first value in the queue.
mjr 77:0b96f6867312 1394 // There doesn't seem to be any way to clear the FIFO from software, so you
mjr 77:0b96f6867312 1395 // just have to wait for the cycle to end before writing another update.
mjr 77:0b96f6867312 1396 // That more or less defeats the purpose of the staging register, whose whole
mjr 77:0b96f6867312 1397 // point is to free software from worrying about timing considerations with
mjr 77:0b96f6867312 1398 // updates. It frees us of the need to align our timing on cycle boundaries,
mjr 77:0b96f6867312 1399 // but it leaves us with the need to limit writes to once per cycle.
mjr 74:822a92bc11d2 1400 //
mjr 77:0b96f6867312 1401 // So here we have our list of PWM outputs that need to be polled for updates.
mjr 77:0b96f6867312 1402 // The KL25Z hardware only has 10 PWM channels, so we only need a fixed set
mjr 77:0b96f6867312 1403 // of polled items.
mjr 74:822a92bc11d2 1404 static int numPolledPwm;
mjr 74:822a92bc11d2 1405 static class LwPwmOut *polledPwm[10];
mjr 74:822a92bc11d2 1406
mjr 74:822a92bc11d2 1407 // LwOut class for a PWM-capable GPIO port.
mjr 6:cc35eb643e8f 1408 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 1409 {
mjr 6:cc35eb643e8f 1410 public:
mjr 43:7a6364d82a41 1411 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1412 {
mjr 77:0b96f6867312 1413 // add myself to the list of polled outputs for periodic updates
mjr 77:0b96f6867312 1414 if (numPolledPwm < countof(polledPwm))
mjr 74:822a92bc11d2 1415 polledPwm[numPolledPwm++] = this;
mjr 77:0b96f6867312 1416
mjr 77:0b96f6867312 1417 // set the initial value
mjr 77:0b96f6867312 1418 set(initVal);
mjr 43:7a6364d82a41 1419 }
mjr 74:822a92bc11d2 1420
mjr 40:cc0d9814522b 1421 virtual void set(uint8_t val)
mjr 74:822a92bc11d2 1422 {
mjr 77:0b96f6867312 1423 // save the new value
mjr 74:822a92bc11d2 1424 this->val = val;
mjr 77:0b96f6867312 1425
mjr 77:0b96f6867312 1426 // commit it to the hardware
mjr 77:0b96f6867312 1427 commit();
mjr 13:72dda449c3c0 1428 }
mjr 74:822a92bc11d2 1429
mjr 74:822a92bc11d2 1430 // handle periodic update polling
mjr 74:822a92bc11d2 1431 void poll()
mjr 74:822a92bc11d2 1432 {
mjr 77:0b96f6867312 1433 commit();
mjr 74:822a92bc11d2 1434 }
mjr 74:822a92bc11d2 1435
mjr 74:822a92bc11d2 1436 protected:
mjr 77:0b96f6867312 1437 virtual void commit()
mjr 74:822a92bc11d2 1438 {
mjr 74:822a92bc11d2 1439 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1440 p.glitchFreeWrite(dof_to_pwm[val]);
mjr 74:822a92bc11d2 1441 }
mjr 74:822a92bc11d2 1442
mjr 77:0b96f6867312 1443 NewPwmOut p;
mjr 77:0b96f6867312 1444 uint8_t val;
mjr 6:cc35eb643e8f 1445 };
mjr 26:cb71c4af2912 1446
mjr 74:822a92bc11d2 1447 // Gamma corrected PWM GPIO output. This works exactly like the regular
mjr 74:822a92bc11d2 1448 // PWM output, but translates DOF values through the gamma-corrected
mjr 74:822a92bc11d2 1449 // table instead of the regular linear table.
mjr 64:ef7ca92dff36 1450 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1451 {
mjr 64:ef7ca92dff36 1452 public:
mjr 64:ef7ca92dff36 1453 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1454 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1455 {
mjr 64:ef7ca92dff36 1456 }
mjr 74:822a92bc11d2 1457
mjr 74:822a92bc11d2 1458 protected:
mjr 77:0b96f6867312 1459 virtual void commit()
mjr 64:ef7ca92dff36 1460 {
mjr 74:822a92bc11d2 1461 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1462 p.glitchFreeWrite(dof_to_gamma_pwm[val]);
mjr 64:ef7ca92dff36 1463 }
mjr 64:ef7ca92dff36 1464 };
mjr 64:ef7ca92dff36 1465
mjr 74:822a92bc11d2 1466 // poll the PWM outputs
mjr 74:822a92bc11d2 1467 Timer polledPwmTimer;
mjr 76:7f5912b6340e 1468 uint64_t polledPwmTotalTime, polledPwmRunCount;
mjr 74:822a92bc11d2 1469 void pollPwmUpdates()
mjr 74:822a92bc11d2 1470 {
mjr 74:822a92bc11d2 1471 // if it's been at least 25ms since the last update, do another update
mjr 74:822a92bc11d2 1472 if (polledPwmTimer.read_us() >= 25000)
mjr 74:822a92bc11d2 1473 {
mjr 74:822a92bc11d2 1474 // time the run for statistics collection
mjr 74:822a92bc11d2 1475 IF_DIAG(
mjr 74:822a92bc11d2 1476 Timer t;
mjr 74:822a92bc11d2 1477 t.start();
mjr 74:822a92bc11d2 1478 )
mjr 74:822a92bc11d2 1479
mjr 74:822a92bc11d2 1480 // poll each output
mjr 74:822a92bc11d2 1481 for (int i = numPolledPwm ; i > 0 ; )
mjr 74:822a92bc11d2 1482 polledPwm[--i]->poll();
mjr 74:822a92bc11d2 1483
mjr 74:822a92bc11d2 1484 // reset the timer for the next cycle
mjr 74:822a92bc11d2 1485 polledPwmTimer.reset();
mjr 74:822a92bc11d2 1486
mjr 74:822a92bc11d2 1487 // collect statistics
mjr 74:822a92bc11d2 1488 IF_DIAG(
mjr 76:7f5912b6340e 1489 polledPwmTotalTime += t.read_us();
mjr 74:822a92bc11d2 1490 polledPwmRunCount += 1;
mjr 74:822a92bc11d2 1491 )
mjr 74:822a92bc11d2 1492 }
mjr 74:822a92bc11d2 1493 }
mjr 64:ef7ca92dff36 1494
mjr 26:cb71c4af2912 1495 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1496 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1497 {
mjr 6:cc35eb643e8f 1498 public:
mjr 43:7a6364d82a41 1499 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1500 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1501 {
mjr 13:72dda449c3c0 1502 if (val != prv)
mjr 40:cc0d9814522b 1503 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1504 }
mjr 6:cc35eb643e8f 1505 DigitalOut p;
mjr 40:cc0d9814522b 1506 uint8_t prv;
mjr 6:cc35eb643e8f 1507 };
mjr 26:cb71c4af2912 1508
mjr 29:582472d0bc57 1509 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1510 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1511 // port n (0-based).
mjr 35:e959ffba78fd 1512 //
mjr 35:e959ffba78fd 1513 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1514 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1515 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1516 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1517 // 74HC595 ports).
mjr 33:d832bcab089e 1518 static int numOutputs;
mjr 33:d832bcab089e 1519 static LwOut **lwPin;
mjr 33:d832bcab089e 1520
mjr 38:091e511ce8a0 1521 // create a single output pin
mjr 53:9b2611964afc 1522 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1523 {
mjr 38:091e511ce8a0 1524 // get this item's values
mjr 38:091e511ce8a0 1525 int typ = pc.typ;
mjr 38:091e511ce8a0 1526 int pin = pc.pin;
mjr 38:091e511ce8a0 1527 int flags = pc.flags;
mjr 40:cc0d9814522b 1528 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1529 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1530 int gamma = flags & PortFlagGamma;
mjr 89:c43cd923401c 1531 int flipperLogic = flags & PortFlagFlipperLogic;
mjr 89:c43cd923401c 1532
mjr 89:c43cd923401c 1533 // cancel gamma on flipper logic ports
mjr 89:c43cd923401c 1534 if (flipperLogic)
mjr 89:c43cd923401c 1535 gamma = false;
mjr 38:091e511ce8a0 1536
mjr 38:091e511ce8a0 1537 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1538 LwOut *lwp;
mjr 38:091e511ce8a0 1539 switch (typ)
mjr 38:091e511ce8a0 1540 {
mjr 38:091e511ce8a0 1541 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1542 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1543 if (pin != 0)
mjr 64:ef7ca92dff36 1544 {
mjr 64:ef7ca92dff36 1545 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 1546 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 1547 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 1548 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 1549 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 1550 {
mjr 64:ef7ca92dff36 1551 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 1552 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 1553
mjr 64:ef7ca92dff36 1554 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 1555 gamma = false;
mjr 64:ef7ca92dff36 1556 }
mjr 64:ef7ca92dff36 1557 else
mjr 64:ef7ca92dff36 1558 {
mjr 64:ef7ca92dff36 1559 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 1560 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 1561 }
mjr 64:ef7ca92dff36 1562 }
mjr 48:058ace2aed1d 1563 else
mjr 48:058ace2aed1d 1564 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1565 break;
mjr 38:091e511ce8a0 1566
mjr 38:091e511ce8a0 1567 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1568 // Digital GPIO port
mjr 48:058ace2aed1d 1569 if (pin != 0)
mjr 48:058ace2aed1d 1570 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1571 else
mjr 48:058ace2aed1d 1572 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1573 break;
mjr 38:091e511ce8a0 1574
mjr 38:091e511ce8a0 1575 case PortTypeTLC5940:
mjr 38:091e511ce8a0 1576 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 1577 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 1578 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 1579 {
mjr 40:cc0d9814522b 1580 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 1581 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 1582 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 1583 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 1584 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 1585 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 1586 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 1587 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 1588 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 1589 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 1590 // for this unlikely case.
mjr 40:cc0d9814522b 1591 if (gamma && !activeLow)
mjr 40:cc0d9814522b 1592 {
mjr 40:cc0d9814522b 1593 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 1594 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 1595
mjr 40:cc0d9814522b 1596 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 1597 gamma = false;
mjr 40:cc0d9814522b 1598 }
mjr 40:cc0d9814522b 1599 else
mjr 40:cc0d9814522b 1600 {
mjr 40:cc0d9814522b 1601 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 1602 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 1603 }
mjr 40:cc0d9814522b 1604 }
mjr 38:091e511ce8a0 1605 else
mjr 40:cc0d9814522b 1606 {
mjr 40:cc0d9814522b 1607 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 1608 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 1609 }
mjr 38:091e511ce8a0 1610 break;
mjr 38:091e511ce8a0 1611
mjr 38:091e511ce8a0 1612 case PortType74HC595:
mjr 87:8d35c74403af 1613 // 74HC595 port (if we don't have an HC595 controller object, or it's not
mjr 87:8d35c74403af 1614 // a valid output number, create a virtual port)
mjr 38:091e511ce8a0 1615 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 1616 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 1617 else
mjr 38:091e511ce8a0 1618 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1619 break;
mjr 87:8d35c74403af 1620
mjr 87:8d35c74403af 1621 case PortTypeTLC59116:
mjr 87:8d35c74403af 1622 // TLC59116 port. The pin number in the config encodes the chip address
mjr 87:8d35c74403af 1623 // in the high 4 bits and the output number on the chip in the low 4 bits.
mjr 87:8d35c74403af 1624 // There's no gamma-corrected version of this output handler, so we don't
mjr 87:8d35c74403af 1625 // need to worry about that here; just use the layered gamma as needed.
mjr 87:8d35c74403af 1626 if (tlc59116 != 0)
mjr 87:8d35c74403af 1627 lwp = new Lw59116Out((pin >> 4) & 0x0F, pin & 0x0F);
mjr 87:8d35c74403af 1628 break;
mjr 38:091e511ce8a0 1629
mjr 38:091e511ce8a0 1630 case PortTypeVirtual:
mjr 43:7a6364d82a41 1631 case PortTypeDisabled:
mjr 38:091e511ce8a0 1632 default:
mjr 38:091e511ce8a0 1633 // virtual or unknown
mjr 38:091e511ce8a0 1634 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1635 break;
mjr 38:091e511ce8a0 1636 }
mjr 38:091e511ce8a0 1637
mjr 40:cc0d9814522b 1638 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 1639 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 1640 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 1641 if (activeLow)
mjr 38:091e511ce8a0 1642 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 1643
mjr 89:c43cd923401c 1644 // Layer on Flipper Logic if desired
mjr 89:c43cd923401c 1645 if (flipperLogic)
mjr 89:c43cd923401c 1646 lwp = new LwFlipperLogicOut(lwp, pc.flipperLogic);
mjr 89:c43cd923401c 1647
mjr 89:c43cd923401c 1648 // If it's a noisemaker, layer on a night mode switch
mjr 40:cc0d9814522b 1649 if (noisy)
mjr 40:cc0d9814522b 1650 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 1651
mjr 40:cc0d9814522b 1652 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 1653 if (gamma)
mjr 40:cc0d9814522b 1654 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 1655
mjr 53:9b2611964afc 1656 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 1657 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 1658 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 1659 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 1660 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 1661
mjr 53:9b2611964afc 1662 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 1663 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 1664 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 1665
mjr 38:091e511ce8a0 1666 // turn it off initially
mjr 38:091e511ce8a0 1667 lwp->set(0);
mjr 38:091e511ce8a0 1668
mjr 38:091e511ce8a0 1669 // return the pin
mjr 38:091e511ce8a0 1670 return lwp;
mjr 38:091e511ce8a0 1671 }
mjr 38:091e511ce8a0 1672
mjr 6:cc35eb643e8f 1673 // initialize the output pin array
mjr 35:e959ffba78fd 1674 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 1675 {
mjr 89:c43cd923401c 1676 // Initialize the Flipper Logic outputs
mjr 89:c43cd923401c 1677 LwFlipperLogicOut::classInit(cfg);
mjr 89:c43cd923401c 1678
mjr 35:e959ffba78fd 1679 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 1680 // total number of ports.
mjr 35:e959ffba78fd 1681 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 1682 int i;
mjr 35:e959ffba78fd 1683 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 1684 {
mjr 35:e959ffba78fd 1685 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 1686 {
mjr 35:e959ffba78fd 1687 numOutputs = i;
mjr 34:6b981a2afab7 1688 break;
mjr 34:6b981a2afab7 1689 }
mjr 33:d832bcab089e 1690 }
mjr 33:d832bcab089e 1691
mjr 73:4e8ce0b18915 1692 // allocate the pin array
mjr 73:4e8ce0b18915 1693 lwPin = new LwOut*[numOutputs];
mjr 35:e959ffba78fd 1694
mjr 73:4e8ce0b18915 1695 // Allocate the current brightness array
mjr 73:4e8ce0b18915 1696 outLevel = new uint8_t[numOutputs];
mjr 33:d832bcab089e 1697
mjr 73:4e8ce0b18915 1698 // allocate the LedWiz output state arrays
mjr 73:4e8ce0b18915 1699 wizOn = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 1700 wizVal = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 1701
mjr 73:4e8ce0b18915 1702 // initialize all LedWiz outputs to off and brightness 48
mjr 73:4e8ce0b18915 1703 memset(wizOn, 0, numOutputs);
mjr 73:4e8ce0b18915 1704 memset(wizVal, 48, numOutputs);
mjr 73:4e8ce0b18915 1705
mjr 73:4e8ce0b18915 1706 // set all LedWiz virtual unit flash speeds to 2
mjr 73:4e8ce0b18915 1707 for (i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 1708 wizSpeed[i] = 2;
mjr 33:d832bcab089e 1709
mjr 35:e959ffba78fd 1710 // create the pin interface object for each port
mjr 35:e959ffba78fd 1711 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 1712 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 1713 }
mjr 6:cc35eb643e8f 1714
mjr 76:7f5912b6340e 1715 // Translate an LedWiz brightness level (0..49) to a DOF brightness
mjr 76:7f5912b6340e 1716 // level (0..255). Note that brightness level 49 isn't actually valid,
mjr 76:7f5912b6340e 1717 // according to the LedWiz API documentation, but many clients use it
mjr 76:7f5912b6340e 1718 // anyway, and the real LedWiz accepts it and seems to treat it as
mjr 76:7f5912b6340e 1719 // equivalent to 48.
mjr 40:cc0d9814522b 1720 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 1721 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 1722 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 1723 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 1724 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 1725 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 1726 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 1727 255, 255
mjr 40:cc0d9814522b 1728 };
mjr 40:cc0d9814522b 1729
mjr 76:7f5912b6340e 1730 // Translate a DOF brightness level (0..255) to an LedWiz brightness
mjr 76:7f5912b6340e 1731 // level (1..48)
mjr 76:7f5912b6340e 1732 static const uint8_t dof_to_lw[] = {
mjr 76:7f5912b6340e 1733 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
mjr 76:7f5912b6340e 1734 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
mjr 76:7f5912b6340e 1735 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9,
mjr 76:7f5912b6340e 1736 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12,
mjr 76:7f5912b6340e 1737 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15,
mjr 76:7f5912b6340e 1738 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18,
mjr 76:7f5912b6340e 1739 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21,
mjr 76:7f5912b6340e 1740 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24,
mjr 76:7f5912b6340e 1741 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27,
mjr 76:7f5912b6340e 1742 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30,
mjr 76:7f5912b6340e 1743 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
mjr 76:7f5912b6340e 1744 33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36,
mjr 76:7f5912b6340e 1745 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
mjr 76:7f5912b6340e 1746 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
mjr 76:7f5912b6340e 1747 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45,
mjr 76:7f5912b6340e 1748 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48
mjr 76:7f5912b6340e 1749 };
mjr 76:7f5912b6340e 1750
mjr 74:822a92bc11d2 1751 // LedWiz flash cycle tables. For efficiency, we use a lookup table
mjr 74:822a92bc11d2 1752 // rather than calculating these on the fly. The flash cycles are
mjr 74:822a92bc11d2 1753 // generated by the following formulas, where 'c' is the current
mjr 74:822a92bc11d2 1754 // cycle counter, from 0 to 255:
mjr 74:822a92bc11d2 1755 //
mjr 74:822a92bc11d2 1756 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 1757 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 1758 // mode 131 = on/ramp down = (c < 128 ? 255 : (255-c)*2)
mjr 74:822a92bc11d2 1759 // mode 132 = ramp up/on = (c < 128 ? c*2 : 255)
mjr 74:822a92bc11d2 1760 //
mjr 74:822a92bc11d2 1761 // To look up the current output value for a given mode and a given
mjr 74:822a92bc11d2 1762 // cycle counter 'c', index the table with ((mode-129)*256)+c.
mjr 74:822a92bc11d2 1763 static const uint8_t wizFlashLookup[] = {
mjr 74:822a92bc11d2 1764 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 1765 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
mjr 74:822a92bc11d2 1766 0x21, 0x23, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f,
mjr 74:822a92bc11d2 1767 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f,
mjr 74:822a92bc11d2 1768 0x61, 0x63, 0x65, 0x67, 0x69, 0x6b, 0x6d, 0x6f, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d, 0x7f,
mjr 74:822a92bc11d2 1769 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d, 0x8f, 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9d, 0x9f,
mjr 74:822a92bc11d2 1770 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad, 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, 0xbf,
mjr 74:822a92bc11d2 1771 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
mjr 74:822a92bc11d2 1772 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb, 0xed, 0xef, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0xfd, 0xff,
mjr 74:822a92bc11d2 1773 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 1774 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 1775 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 1776 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 1777 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 1778 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 1779 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 1780 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 1781
mjr 74:822a92bc11d2 1782 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 1783 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1784 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1785 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1786 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1787 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1788 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1789 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1790 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1791 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1792 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1793 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1794 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1795 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1796 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1797 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1798 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1799
mjr 74:822a92bc11d2 1800 // mode 131 = on/ramp down = c < 128 ? 255 : (255 - c)*2
mjr 74:822a92bc11d2 1801 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1802 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1803 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1804 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1805 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1806 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1807 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1808 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1809 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 1810 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 1811 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 1812 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 1813 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 1814 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 1815 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 1816 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 1817
mjr 74:822a92bc11d2 1818 // mode 132 = ramp up/on = c < 128 ? c*2 : 255
mjr 74:822a92bc11d2 1819 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
mjr 74:822a92bc11d2 1820 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
mjr 74:822a92bc11d2 1821 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
mjr 74:822a92bc11d2 1822 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
mjr 74:822a92bc11d2 1823 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
mjr 74:822a92bc11d2 1824 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
mjr 74:822a92bc11d2 1825 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
mjr 74:822a92bc11d2 1826 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
mjr 74:822a92bc11d2 1827 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1828 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1829 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1830 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1831 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1832 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1833 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1834 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
mjr 74:822a92bc11d2 1835 };
mjr 74:822a92bc11d2 1836
mjr 74:822a92bc11d2 1837 // LedWiz flash cycle timer. This runs continuously. On each update,
mjr 74:822a92bc11d2 1838 // we use this to figure out where we are on the cycle for each bank.
mjr 74:822a92bc11d2 1839 Timer wizCycleTimer;
mjr 74:822a92bc11d2 1840
mjr 76:7f5912b6340e 1841 // timing statistics for wizPulse()
mjr 76:7f5912b6340e 1842 uint64_t wizPulseTotalTime, wizPulseRunCount;
mjr 76:7f5912b6340e 1843
mjr 76:7f5912b6340e 1844 // LedWiz flash timer pulse. The main loop calls this on each cycle
mjr 76:7f5912b6340e 1845 // to update outputs using LedWiz flash modes. We do one bank of 32
mjr 76:7f5912b6340e 1846 // outputs on each cycle.
mjr 29:582472d0bc57 1847 static void wizPulse()
mjr 29:582472d0bc57 1848 {
mjr 76:7f5912b6340e 1849 // current bank
mjr 76:7f5912b6340e 1850 static int wizPulseBank = 0;
mjr 76:7f5912b6340e 1851
mjr 76:7f5912b6340e 1852 // start a timer for statistics collection
mjr 76:7f5912b6340e 1853 IF_DIAG(
mjr 76:7f5912b6340e 1854 Timer t;
mjr 76:7f5912b6340e 1855 t.start();
mjr 76:7f5912b6340e 1856 )
mjr 76:7f5912b6340e 1857
mjr 76:7f5912b6340e 1858 // Update the current bank's cycle counter: figure the current
mjr 76:7f5912b6340e 1859 // phase of the LedWiz pulse cycle for this bank.
mjr 76:7f5912b6340e 1860 //
mjr 76:7f5912b6340e 1861 // The LedWiz speed setting gives the flash period in 0.25s units
mjr 76:7f5912b6340e 1862 // (speed 1 is a flash period of .25s, speed 7 is a period of 1.75s).
mjr 76:7f5912b6340e 1863 //
mjr 76:7f5912b6340e 1864 // What we're after here is the "phase", which is to say the point
mjr 76:7f5912b6340e 1865 // in the current cycle. If we assume that the cycle has been running
mjr 76:7f5912b6340e 1866 // continuously since some arbitrary time zero in the past, we can
mjr 76:7f5912b6340e 1867 // figure where we are in the current cycle by dividing the time since
mjr 76:7f5912b6340e 1868 // that zero by the cycle period and taking the remainder. E.g., if
mjr 76:7f5912b6340e 1869 // the cycle time is 5 seconds, and the time since t-zero is 17 seconds,
mjr 76:7f5912b6340e 1870 // we divide 17 by 5 to get a remainder of 2. That says we're 2 seconds
mjr 76:7f5912b6340e 1871 // into the current 5-second cycle, or 2/5 of the way through the
mjr 76:7f5912b6340e 1872 // current cycle.
mjr 76:7f5912b6340e 1873 //
mjr 76:7f5912b6340e 1874 // We do this calculation on every iteration of the main loop, so we
mjr 76:7f5912b6340e 1875 // want it to be very fast. To streamline it, we'll use some tricky
mjr 76:7f5912b6340e 1876 // integer arithmetic. The result will be the same as the straightforward
mjr 76:7f5912b6340e 1877 // remainder and fraction calculation we just explained, but we'll get
mjr 76:7f5912b6340e 1878 // there by less-than-obvious means.
mjr 76:7f5912b6340e 1879 //
mjr 76:7f5912b6340e 1880 // Rather than finding the phase as a continuous quantity or floating
mjr 76:7f5912b6340e 1881 // point number, we'll quantize it. We'll divide each cycle into 256
mjr 76:7f5912b6340e 1882 // time units, or quanta. Each quantum is 1/256 of the cycle length,
mjr 76:7f5912b6340e 1883 // so for a 1-second cycle (LedWiz speed 4), each quantum is 1/256 of
mjr 76:7f5912b6340e 1884 // a second, or about 3.9ms. If we express the time since t-zero in
mjr 76:7f5912b6340e 1885 // these units, the time period of one cycle is exactly 256 units, so
mjr 76:7f5912b6340e 1886 // we can calculate our point in the cycle by taking the remainder of
mjr 76:7f5912b6340e 1887 // the time (in our funny units) divided by 256. The special thing
mjr 76:7f5912b6340e 1888 // about making the cycle time equal to 256 units is that "x % 256"
mjr 76:7f5912b6340e 1889 // is exactly the same as "x & 255", which is a much faster operation
mjr 76:7f5912b6340e 1890 // than division on ARM M0+: this CPU has no hardware DIVIDE operation,
mjr 76:7f5912b6340e 1891 // so an integer division takes about 5us. The bit mask operation, in
mjr 76:7f5912b6340e 1892 // contrast, takes only about 60ns - about 100x faster. 5us doesn't
mjr 76:7f5912b6340e 1893 // sound like much, but we do this on every main loop, so every little
mjr 76:7f5912b6340e 1894 // bit counts.
mjr 76:7f5912b6340e 1895 //
mjr 76:7f5912b6340e 1896 // The snag is that our system timer gives us the elapsed time in
mjr 76:7f5912b6340e 1897 // microseconds. We still need to convert this to our special quanta
mjr 76:7f5912b6340e 1898 // of 256 units per cycle. The straightforward way to do that is by
mjr 76:7f5912b6340e 1899 // dividing by (microseconds per quantum). E.g., for LedWiz speed 4,
mjr 76:7f5912b6340e 1900 // we decided that our quantum was 1/256 of a second, or 3906us, so
mjr 76:7f5912b6340e 1901 // dividing the current system time in microseconds by 3906 will give
mjr 76:7f5912b6340e 1902 // us the time in our quantum units. But now we've just substituted
mjr 76:7f5912b6340e 1903 // one division for another!
mjr 76:7f5912b6340e 1904 //
mjr 76:7f5912b6340e 1905 // This is where our really tricky integer math comes in. Dividing
mjr 76:7f5912b6340e 1906 // by X is the same as multiplying by 1/X. In integer math, 1/3906
mjr 76:7f5912b6340e 1907 // is zero, so that won't work. But we can get around that by doing
mjr 76:7f5912b6340e 1908 // the integer math as "fixed point" arithmetic instead. It's still
mjr 76:7f5912b6340e 1909 // actually carried out as integer operations, but we'll scale our
mjr 76:7f5912b6340e 1910 // integers by a scaling factor, then take out the scaling factor
mjr 76:7f5912b6340e 1911 // later to get the final result. The scaling factor we'll use is
mjr 76:7f5912b6340e 1912 // 2^24. So we're going to calculate (time * 2^24/3906), then divide
mjr 76:7f5912b6340e 1913 // the result by 2^24 to get the final answer. I know it seems like
mjr 76:7f5912b6340e 1914 // we're substituting one division for another yet again, but this
mjr 76:7f5912b6340e 1915 // time's the charm, because dividing by 2^24 is a bit shift operation,
mjr 76:7f5912b6340e 1916 // which is another single-cycle operation on M0+. You might also
mjr 76:7f5912b6340e 1917 // wonder how all these tricks don't cause overflows or underflows
mjr 76:7f5912b6340e 1918 // or what not. Well, the multiply by 2^24/3906 will cause an
mjr 76:7f5912b6340e 1919 // overflow, but we don't care, because the overflow will all be in
mjr 76:7f5912b6340e 1920 // the high-order bits that we're going to discard in the final
mjr 76:7f5912b6340e 1921 // remainder calculation anyway.
mjr 76:7f5912b6340e 1922 //
mjr 76:7f5912b6340e 1923 // Each entry in the array below represents 2^24/N for the corresponding
mjr 76:7f5912b6340e 1924 // LedWiz speed, where N is the number of time quanta per cycle at that
mjr 76:7f5912b6340e 1925 // speed. The time quanta are chosen such that 256 quanta add up to
mjr 76:7f5912b6340e 1926 // approximately (LedWiz speed setting * 0.25s).
mjr 76:7f5912b6340e 1927 //
mjr 76:7f5912b6340e 1928 // Note that the calculation has an implicit bit mask (result & 0xFF)
mjr 76:7f5912b6340e 1929 // to get the final result mod 256. But we don't have to actually
mjr 76:7f5912b6340e 1930 // do that work because we're using 32-bit ints and a 2^24 fixed
mjr 76:7f5912b6340e 1931 // point base (X in the narrative above). The final shift right by
mjr 76:7f5912b6340e 1932 // 24 bits to divide out the base will leave us with only 8 bits in
mjr 76:7f5912b6340e 1933 // the result, since we started with 32.
mjr 76:7f5912b6340e 1934 static const uint32_t inv_us_per_quantum[] = { // indexed by LedWiz speed
mjr 76:7f5912b6340e 1935 0, 17172, 8590, 5726, 4295, 3436, 2863, 2454
mjr 76:7f5912b6340e 1936 };
mjr 76:7f5912b6340e 1937 int counter = ((wizCycleTimer.read_us() * inv_us_per_quantum[wizSpeed[wizPulseBank]]) >> 24);
mjr 76:7f5912b6340e 1938
mjr 76:7f5912b6340e 1939 // get the range of 32 output sin this bank
mjr 76:7f5912b6340e 1940 int fromPort = wizPulseBank*32;
mjr 76:7f5912b6340e 1941 int toPort = fromPort+32;
mjr 76:7f5912b6340e 1942 if (toPort > numOutputs)
mjr 76:7f5912b6340e 1943 toPort = numOutputs;
mjr 76:7f5912b6340e 1944
mjr 76:7f5912b6340e 1945 // update all outputs set to flashing values
mjr 76:7f5912b6340e 1946 for (int i = fromPort ; i < toPort ; ++i)
mjr 73:4e8ce0b18915 1947 {
mjr 76:7f5912b6340e 1948 // Update the port only if the LedWiz SBA switch for the port is on
mjr 76:7f5912b6340e 1949 // (wizOn[i]) AND the port is a PBA flash mode in the range 129..132.
mjr 76:7f5912b6340e 1950 // These modes and only these modes have the high bit (0x80) set, so
mjr 76:7f5912b6340e 1951 // we can test for them simply by testing the high bit.
mjr 76:7f5912b6340e 1952 if (wizOn[i])
mjr 29:582472d0bc57 1953 {
mjr 76:7f5912b6340e 1954 uint8_t val = wizVal[i];
mjr 76:7f5912b6340e 1955 if ((val & 0x80) != 0)
mjr 29:582472d0bc57 1956 {
mjr 76:7f5912b6340e 1957 // ook up the value for the mode at the cycle time
mjr 76:7f5912b6340e 1958 lwPin[i]->set(outLevel[i] = wizFlashLookup[((val-129) << 8) + counter]);
mjr 29:582472d0bc57 1959 }
mjr 29:582472d0bc57 1960 }
mjr 76:7f5912b6340e 1961 }
mjr 76:7f5912b6340e 1962
mjr 34:6b981a2afab7 1963 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 1964 if (hc595 != 0)
mjr 35:e959ffba78fd 1965 hc595->update();
mjr 76:7f5912b6340e 1966
mjr 76:7f5912b6340e 1967 // switch to the next bank
mjr 76:7f5912b6340e 1968 if (++wizPulseBank >= MAX_LW_BANKS)
mjr 76:7f5912b6340e 1969 wizPulseBank = 0;
mjr 76:7f5912b6340e 1970
mjr 76:7f5912b6340e 1971 // collect timing statistics
mjr 76:7f5912b6340e 1972 IF_DIAG(
mjr 76:7f5912b6340e 1973 wizPulseTotalTime += t.read_us();
mjr 76:7f5912b6340e 1974 wizPulseRunCount += 1;
mjr 76:7f5912b6340e 1975 )
mjr 1:d913e0afb2ac 1976 }
mjr 38:091e511ce8a0 1977
mjr 76:7f5912b6340e 1978 // Update a port to reflect its new LedWiz SBA+PBA setting.
mjr 76:7f5912b6340e 1979 static void updateLwPort(int port)
mjr 38:091e511ce8a0 1980 {
mjr 76:7f5912b6340e 1981 // check if the SBA switch is on or off
mjr 76:7f5912b6340e 1982 if (wizOn[port])
mjr 76:7f5912b6340e 1983 {
mjr 76:7f5912b6340e 1984 // It's on. If the port is a valid static brightness level,
mjr 76:7f5912b6340e 1985 // set the output port to match. Otherwise leave it as is:
mjr 76:7f5912b6340e 1986 // if it's a flashing mode, the flash mode pulse will update
mjr 76:7f5912b6340e 1987 // it on the next cycle.
mjr 76:7f5912b6340e 1988 int val = wizVal[port];
mjr 76:7f5912b6340e 1989 if (val <= 49)
mjr 76:7f5912b6340e 1990 lwPin[port]->set(outLevel[port] = lw_to_dof[val]);
mjr 76:7f5912b6340e 1991 }
mjr 76:7f5912b6340e 1992 else
mjr 76:7f5912b6340e 1993 {
mjr 76:7f5912b6340e 1994 // the port is off - set absolute brightness zero
mjr 76:7f5912b6340e 1995 lwPin[port]->set(outLevel[port] = 0);
mjr 76:7f5912b6340e 1996 }
mjr 73:4e8ce0b18915 1997 }
mjr 73:4e8ce0b18915 1998
mjr 73:4e8ce0b18915 1999 // Turn off all outputs and restore everything to the default LedWiz
mjr 73:4e8ce0b18915 2000 // state. This sets outputs #1-32 to LedWiz profile value 48 (full
mjr 73:4e8ce0b18915 2001 // brightness) and switch state Off, sets all extended outputs (#33
mjr 73:4e8ce0b18915 2002 // and above) to zero brightness, and sets the LedWiz flash rate to 2.
mjr 73:4e8ce0b18915 2003 // This effectively restores the power-on conditions.
mjr 73:4e8ce0b18915 2004 //
mjr 73:4e8ce0b18915 2005 void allOutputsOff()
mjr 73:4e8ce0b18915 2006 {
mjr 73:4e8ce0b18915 2007 // reset all LedWiz outputs to OFF/48
mjr 73:4e8ce0b18915 2008 for (int i = 0 ; i < numOutputs ; ++i)
mjr 73:4e8ce0b18915 2009 {
mjr 73:4e8ce0b18915 2010 outLevel[i] = 0;
mjr 73:4e8ce0b18915 2011 wizOn[i] = 0;
mjr 73:4e8ce0b18915 2012 wizVal[i] = 48;
mjr 73:4e8ce0b18915 2013 lwPin[i]->set(0);
mjr 73:4e8ce0b18915 2014 }
mjr 73:4e8ce0b18915 2015
mjr 73:4e8ce0b18915 2016 // restore default LedWiz flash rate
mjr 73:4e8ce0b18915 2017 for (int i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2018 wizSpeed[i] = 2;
mjr 38:091e511ce8a0 2019
mjr 73:4e8ce0b18915 2020 // flush changes to hc595, if applicable
mjr 38:091e511ce8a0 2021 if (hc595 != 0)
mjr 38:091e511ce8a0 2022 hc595->update();
mjr 38:091e511ce8a0 2023 }
mjr 38:091e511ce8a0 2024
mjr 74:822a92bc11d2 2025 // Cary out an SBA or SBX message. portGroup is 0 for ports 1-32,
mjr 74:822a92bc11d2 2026 // 1 for ports 33-64, etc. Original protocol SBA messages always
mjr 74:822a92bc11d2 2027 // address port group 0; our private SBX extension messages can
mjr 74:822a92bc11d2 2028 // address any port group.
mjr 74:822a92bc11d2 2029 void sba_sbx(int portGroup, const uint8_t *data)
mjr 74:822a92bc11d2 2030 {
mjr 76:7f5912b6340e 2031 // update all on/off states in the group
mjr 74:822a92bc11d2 2032 for (int i = 0, bit = 1, imsg = 1, port = portGroup*32 ;
mjr 74:822a92bc11d2 2033 i < 32 && port < numOutputs ;
mjr 74:822a92bc11d2 2034 ++i, bit <<= 1, ++port)
mjr 74:822a92bc11d2 2035 {
mjr 74:822a92bc11d2 2036 // figure the on/off state bit for this output
mjr 74:822a92bc11d2 2037 if (bit == 0x100) {
mjr 74:822a92bc11d2 2038 bit = 1;
mjr 74:822a92bc11d2 2039 ++imsg;
mjr 74:822a92bc11d2 2040 }
mjr 74:822a92bc11d2 2041
mjr 74:822a92bc11d2 2042 // set the on/off state
mjr 76:7f5912b6340e 2043 bool on = wizOn[port] = ((data[imsg] & bit) != 0);
mjr 76:7f5912b6340e 2044
mjr 76:7f5912b6340e 2045 // set the output port brightness to match the new setting
mjr 76:7f5912b6340e 2046 updateLwPort(port);
mjr 74:822a92bc11d2 2047 }
mjr 74:822a92bc11d2 2048
mjr 74:822a92bc11d2 2049 // set the flash speed for the port group
mjr 74:822a92bc11d2 2050 if (portGroup < countof(wizSpeed))
mjr 74:822a92bc11d2 2051 wizSpeed[portGroup] = (data[5] < 1 ? 1 : data[5] > 7 ? 7 : data[5]);
mjr 74:822a92bc11d2 2052
mjr 76:7f5912b6340e 2053 // update 74HC959 outputs
mjr 76:7f5912b6340e 2054 if (hc595 != 0)
mjr 76:7f5912b6340e 2055 hc595->update();
mjr 74:822a92bc11d2 2056 }
mjr 74:822a92bc11d2 2057
mjr 74:822a92bc11d2 2058 // Carry out a PBA or PBX message.
mjr 74:822a92bc11d2 2059 void pba_pbx(int basePort, const uint8_t *data)
mjr 74:822a92bc11d2 2060 {
mjr 74:822a92bc11d2 2061 // update each wizVal entry from the brightness data
mjr 76:7f5912b6340e 2062 for (int i = 0, port = basePort ; i < 8 && port < numOutputs ; ++i, ++port)
mjr 74:822a92bc11d2 2063 {
mjr 74:822a92bc11d2 2064 // get the value
mjr 74:822a92bc11d2 2065 uint8_t v = data[i];
mjr 74:822a92bc11d2 2066
mjr 74:822a92bc11d2 2067 // Validate it. The legal values are 0..49 for brightness
mjr 74:822a92bc11d2 2068 // levels, and 128..132 for flash modes. Set anything invalid
mjr 74:822a92bc11d2 2069 // to full brightness (48) instead. Note that 49 isn't actually
mjr 74:822a92bc11d2 2070 // a valid documented value, but in practice some clients send
mjr 74:822a92bc11d2 2071 // this to mean 100% brightness, and the real LedWiz treats it
mjr 74:822a92bc11d2 2072 // as such.
mjr 74:822a92bc11d2 2073 if ((v > 49 && v < 129) || v > 132)
mjr 74:822a92bc11d2 2074 v = 48;
mjr 74:822a92bc11d2 2075
mjr 74:822a92bc11d2 2076 // store it
mjr 76:7f5912b6340e 2077 wizVal[port] = v;
mjr 76:7f5912b6340e 2078
mjr 76:7f5912b6340e 2079 // update the port
mjr 76:7f5912b6340e 2080 updateLwPort(port);
mjr 74:822a92bc11d2 2081 }
mjr 74:822a92bc11d2 2082
mjr 76:7f5912b6340e 2083 // update 74HC595 outputs
mjr 76:7f5912b6340e 2084 if (hc595 != 0)
mjr 76:7f5912b6340e 2085 hc595->update();
mjr 74:822a92bc11d2 2086 }
mjr 74:822a92bc11d2 2087
mjr 77:0b96f6867312 2088 // ---------------------------------------------------------------------------
mjr 77:0b96f6867312 2089 //
mjr 77:0b96f6867312 2090 // IR Remote Control transmitter & receiver
mjr 77:0b96f6867312 2091 //
mjr 77:0b96f6867312 2092
mjr 77:0b96f6867312 2093 // receiver
mjr 77:0b96f6867312 2094 IRReceiver *ir_rx;
mjr 77:0b96f6867312 2095
mjr 77:0b96f6867312 2096 // transmitter
mjr 77:0b96f6867312 2097 IRTransmitter *ir_tx;
mjr 77:0b96f6867312 2098
mjr 77:0b96f6867312 2099 // Mapping from IR commands slots in the configuration to "virtual button"
mjr 77:0b96f6867312 2100 // numbers on the IRTransmitter's "virtual remote". To minimize RAM usage,
mjr 77:0b96f6867312 2101 // we only create virtual buttons on the transmitter object for code slots
mjr 77:0b96f6867312 2102 // that are configured for transmission, which includes slots used for TV
mjr 77:0b96f6867312 2103 // ON commands and slots that can be triggered by button presses. This
mjr 77:0b96f6867312 2104 // means that virtual button numbers won't necessarily match the config
mjr 77:0b96f6867312 2105 // slot numbers. This table provides the mapping:
mjr 77:0b96f6867312 2106 // IRConfigSlotToVirtualButton[n] = ir_tx virtual button number for
mjr 77:0b96f6867312 2107 // configuration slot n
mjr 77:0b96f6867312 2108 uint8_t IRConfigSlotToVirtualButton[MAX_IR_CODES];
mjr 78:1e00b3fa11af 2109
mjr 78:1e00b3fa11af 2110 // IR transmitter virtual button number for ad hoc IR command. We allocate
mjr 78:1e00b3fa11af 2111 // one virtual button for sending ad hoc IR codes, such as through the USB
mjr 78:1e00b3fa11af 2112 // protocol.
mjr 78:1e00b3fa11af 2113 uint8_t IRAdHocBtn;
mjr 78:1e00b3fa11af 2114
mjr 78:1e00b3fa11af 2115 // Staging area for ad hoc IR commands. It takes multiple messages
mjr 78:1e00b3fa11af 2116 // to fill out an IR command, so we store the partial command here
mjr 78:1e00b3fa11af 2117 // while waiting for the rest.
mjr 78:1e00b3fa11af 2118 static struct
mjr 78:1e00b3fa11af 2119 {
mjr 78:1e00b3fa11af 2120 uint8_t protocol; // protocol ID
mjr 78:1e00b3fa11af 2121 uint64_t code; // code
mjr 78:1e00b3fa11af 2122 uint8_t dittos : 1; // using dittos?
mjr 78:1e00b3fa11af 2123 uint8_t ready : 1; // do we have a code ready to transmit?
mjr 78:1e00b3fa11af 2124 } IRAdHocCmd;
mjr 88:98bce687e6c0 2125
mjr 77:0b96f6867312 2126
mjr 77:0b96f6867312 2127 // IR mode timer. In normal mode, this is the time since the last
mjr 77:0b96f6867312 2128 // command received; we use this to handle commands with timed effects,
mjr 77:0b96f6867312 2129 // such as sending a key to the PC. In learning mode, this is the time
mjr 77:0b96f6867312 2130 // since we activated learning mode, which we use to automatically end
mjr 77:0b96f6867312 2131 // learning mode if a decodable command isn't received within a reasonable
mjr 77:0b96f6867312 2132 // amount of time.
mjr 77:0b96f6867312 2133 Timer IRTimer;
mjr 77:0b96f6867312 2134
mjr 77:0b96f6867312 2135 // IR Learning Mode. The PC enters learning mode via special function 65 12.
mjr 77:0b96f6867312 2136 // The states are:
mjr 77:0b96f6867312 2137 //
mjr 77:0b96f6867312 2138 // 0 -> normal operation (not in learning mode)
mjr 77:0b96f6867312 2139 // 1 -> learning mode; reading raw codes, no command read yet
mjr 77:0b96f6867312 2140 // 2 -> learning mode; command received, awaiting auto-repeat
mjr 77:0b96f6867312 2141 // 3 -> learning mode; done, command and repeat mode decoded
mjr 77:0b96f6867312 2142 //
mjr 77:0b96f6867312 2143 // When we enter learning mode, we reset IRTimer to keep track of how long
mjr 77:0b96f6867312 2144 // we've been in the mode. This allows the mode to time out if no code is
mjr 77:0b96f6867312 2145 // received within a reasonable time.
mjr 77:0b96f6867312 2146 uint8_t IRLearningMode = 0;
mjr 77:0b96f6867312 2147
mjr 77:0b96f6867312 2148 // Learning mode command received. This stores the first decoded command
mjr 77:0b96f6867312 2149 // when in learning mode. For some protocols, we can't just report the
mjr 77:0b96f6867312 2150 // first command we receive, because we need to wait for an auto-repeat to
mjr 77:0b96f6867312 2151 // determine what format the remote uses for repeats. This stores the first
mjr 77:0b96f6867312 2152 // command while we await a repeat. This is necessary for protocols that
mjr 77:0b96f6867312 2153 // have "dittos", since some remotes for such protocols use the dittos and
mjr 77:0b96f6867312 2154 // some don't; the only way to find out is to read a repeat code and see if
mjr 77:0b96f6867312 2155 // it's a ditto or just a repeat of the full code.
mjr 77:0b96f6867312 2156 IRCommand learnedIRCode;
mjr 77:0b96f6867312 2157
mjr 78:1e00b3fa11af 2158 // IR command received, as a config slot index, 1..MAX_IR_CODES.
mjr 77:0b96f6867312 2159 // When we receive a command that matches one of our programmed commands,
mjr 77:0b96f6867312 2160 // we note the slot here. We also reset the IR timer so that we know how
mjr 77:0b96f6867312 2161 // long it's been since the command came in. This lets us handle commands
mjr 77:0b96f6867312 2162 // with timed effects, such as PC key input. Note that this is a 1-based
mjr 77:0b96f6867312 2163 // index; 0 represents no command.
mjr 77:0b96f6867312 2164 uint8_t IRCommandIn = 0;
mjr 77:0b96f6867312 2165
mjr 77:0b96f6867312 2166 // "Toggle bit" of last command. Some IR protocols have a toggle bit
mjr 77:0b96f6867312 2167 // that distinguishes an auto-repeating key from a key being pressed
mjr 77:0b96f6867312 2168 // several times in a row. This records the toggle bit of the last
mjr 77:0b96f6867312 2169 // command we received.
mjr 77:0b96f6867312 2170 uint8_t lastIRToggle = 0;
mjr 77:0b96f6867312 2171
mjr 77:0b96f6867312 2172 // Are we in a gap between successive key presses? When we detect that a
mjr 77:0b96f6867312 2173 // key is being pressed multiple times rather than auto-repeated (which we
mjr 77:0b96f6867312 2174 // can detect via a toggle bit in some protocols), we'll briefly stop sending
mjr 77:0b96f6867312 2175 // the associated key to the PC, so that the PC likewise recognizes the
mjr 77:0b96f6867312 2176 // distinct key press.
mjr 77:0b96f6867312 2177 uint8_t IRKeyGap = false;
mjr 77:0b96f6867312 2178
mjr 78:1e00b3fa11af 2179
mjr 77:0b96f6867312 2180 // initialize
mjr 77:0b96f6867312 2181 void init_IR(Config &cfg, bool &kbKeys)
mjr 77:0b96f6867312 2182 {
mjr 77:0b96f6867312 2183 PinName pin;
mjr 77:0b96f6867312 2184
mjr 77:0b96f6867312 2185 // start the IR timer
mjr 77:0b96f6867312 2186 IRTimer.start();
mjr 77:0b96f6867312 2187
mjr 77:0b96f6867312 2188 // if there's a transmitter, set it up
mjr 77:0b96f6867312 2189 if ((pin = wirePinName(cfg.IR.emitter)) != NC)
mjr 77:0b96f6867312 2190 {
mjr 77:0b96f6867312 2191 // no virtual buttons yet
mjr 77:0b96f6867312 2192 int nVirtualButtons = 0;
mjr 77:0b96f6867312 2193 memset(IRConfigSlotToVirtualButton, 0xFF, sizeof(IRConfigSlotToVirtualButton));
mjr 77:0b96f6867312 2194
mjr 77:0b96f6867312 2195 // assign virtual buttons slots for TV ON codes
mjr 77:0b96f6867312 2196 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2197 {
mjr 77:0b96f6867312 2198 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 2199 IRConfigSlotToVirtualButton[i] = nVirtualButtons++;
mjr 77:0b96f6867312 2200 }
mjr 77:0b96f6867312 2201
mjr 77:0b96f6867312 2202 // assign virtual buttons for codes that can be triggered by
mjr 77:0b96f6867312 2203 // real button inputs
mjr 77:0b96f6867312 2204 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 77:0b96f6867312 2205 {
mjr 77:0b96f6867312 2206 // get the button
mjr 77:0b96f6867312 2207 ButtonCfg &b = cfg.button[i];
mjr 77:0b96f6867312 2208
mjr 77:0b96f6867312 2209 // check the unshifted button
mjr 77:0b96f6867312 2210 int c = b.IRCommand - 1;
mjr 77:0b96f6867312 2211 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2212 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2213 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2214
mjr 77:0b96f6867312 2215 // check the shifted button
mjr 77:0b96f6867312 2216 c = b.IRCommand2 - 1;
mjr 77:0b96f6867312 2217 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2218 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2219 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2220 }
mjr 77:0b96f6867312 2221
mjr 77:0b96f6867312 2222 // allocate an additional virtual button for transmitting ad hoc
mjr 77:0b96f6867312 2223 // codes, such as for the "send code" USB API function
mjr 78:1e00b3fa11af 2224 IRAdHocBtn = nVirtualButtons++;
mjr 77:0b96f6867312 2225
mjr 77:0b96f6867312 2226 // create the transmitter
mjr 77:0b96f6867312 2227 ir_tx = new IRTransmitter(pin, nVirtualButtons);
mjr 77:0b96f6867312 2228
mjr 77:0b96f6867312 2229 // program the commands into the virtual button slots
mjr 77:0b96f6867312 2230 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2231 {
mjr 77:0b96f6867312 2232 // if this slot is assigned to a virtual button, program it
mjr 77:0b96f6867312 2233 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 2234 if (vb != 0xFF)
mjr 77:0b96f6867312 2235 {
mjr 77:0b96f6867312 2236 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2237 uint64_t code = cb.code.lo | (uint64_t(cb.code.hi) << 32);
mjr 77:0b96f6867312 2238 bool dittos = (cb.flags & IRFlagDittos) != 0;
mjr 77:0b96f6867312 2239 ir_tx->programButton(vb, cb.protocol, dittos, code);
mjr 77:0b96f6867312 2240 }
mjr 77:0b96f6867312 2241 }
mjr 77:0b96f6867312 2242 }
mjr 77:0b96f6867312 2243
mjr 77:0b96f6867312 2244 // if there's a receiver, set it up
mjr 77:0b96f6867312 2245 if ((pin = wirePinName(cfg.IR.sensor)) != NC)
mjr 77:0b96f6867312 2246 {
mjr 77:0b96f6867312 2247 // create the receiver
mjr 77:0b96f6867312 2248 ir_rx = new IRReceiver(pin, 32);
mjr 77:0b96f6867312 2249
mjr 77:0b96f6867312 2250 // connect the transmitter (if any) to the receiver, so that
mjr 77:0b96f6867312 2251 // the receiver can suppress reception of our own transmissions
mjr 77:0b96f6867312 2252 ir_rx->setTransmitter(ir_tx);
mjr 77:0b96f6867312 2253
mjr 77:0b96f6867312 2254 // enable it
mjr 77:0b96f6867312 2255 ir_rx->enable();
mjr 77:0b96f6867312 2256
mjr 77:0b96f6867312 2257 // Check the IR command slots to see if any slots are configured
mjr 77:0b96f6867312 2258 // to send a keyboard key on receiving an IR command. If any are,
mjr 77:0b96f6867312 2259 // tell the caller that we need a USB keyboard interface.
mjr 77:0b96f6867312 2260 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2261 {
mjr 77:0b96f6867312 2262 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2263 if (cb.protocol != 0
mjr 77:0b96f6867312 2264 && (cb.keytype == BtnTypeKey || cb.keytype == BtnTypeMedia))
mjr 77:0b96f6867312 2265 {
mjr 77:0b96f6867312 2266 kbKeys = true;
mjr 77:0b96f6867312 2267 break;
mjr 77:0b96f6867312 2268 }
mjr 77:0b96f6867312 2269 }
mjr 77:0b96f6867312 2270 }
mjr 77:0b96f6867312 2271 }
mjr 77:0b96f6867312 2272
mjr 77:0b96f6867312 2273 // Press or release a button with an assigned IR function. 'cmd'
mjr 77:0b96f6867312 2274 // is the command slot number (1..MAX_IR_CODES) assigned to the button.
mjr 77:0b96f6867312 2275 void IR_buttonChange(uint8_t cmd, bool pressed)
mjr 77:0b96f6867312 2276 {
mjr 77:0b96f6867312 2277 // only proceed if there's an IR transmitter attached
mjr 77:0b96f6867312 2278 if (ir_tx != 0)
mjr 77:0b96f6867312 2279 {
mjr 77:0b96f6867312 2280 // adjust the command slot to a zero-based index
mjr 77:0b96f6867312 2281 int slot = cmd - 1;
mjr 77:0b96f6867312 2282
mjr 77:0b96f6867312 2283 // press or release the virtual button
mjr 77:0b96f6867312 2284 ir_tx->pushButton(IRConfigSlotToVirtualButton[slot], pressed);
mjr 77:0b96f6867312 2285 }
mjr 77:0b96f6867312 2286 }
mjr 77:0b96f6867312 2287
mjr 78:1e00b3fa11af 2288 // Process IR input and output
mjr 77:0b96f6867312 2289 void process_IR(Config &cfg, USBJoystick &js)
mjr 77:0b96f6867312 2290 {
mjr 78:1e00b3fa11af 2291 // check for transmitter tasks, if there's a transmitter
mjr 78:1e00b3fa11af 2292 if (ir_tx != 0)
mjr 77:0b96f6867312 2293 {
mjr 78:1e00b3fa11af 2294 // If we're not currently sending, and an ad hoc IR command
mjr 78:1e00b3fa11af 2295 // is ready to send, send it.
mjr 78:1e00b3fa11af 2296 if (!ir_tx->isSending() && IRAdHocCmd.ready)
mjr 78:1e00b3fa11af 2297 {
mjr 78:1e00b3fa11af 2298 // program the command into the transmitter virtual button
mjr 78:1e00b3fa11af 2299 // that we reserved for ad hoc commands
mjr 78:1e00b3fa11af 2300 ir_tx->programButton(IRAdHocBtn, IRAdHocCmd.protocol,
mjr 78:1e00b3fa11af 2301 IRAdHocCmd.dittos, IRAdHocCmd.code);
mjr 78:1e00b3fa11af 2302
mjr 78:1e00b3fa11af 2303 // send the command - just pulse the button to send it once
mjr 78:1e00b3fa11af 2304 ir_tx->pushButton(IRAdHocBtn, true);
mjr 78:1e00b3fa11af 2305 ir_tx->pushButton(IRAdHocBtn, false);
mjr 78:1e00b3fa11af 2306
mjr 78:1e00b3fa11af 2307 // we've sent the command, so clear the 'ready' flag
mjr 78:1e00b3fa11af 2308 IRAdHocCmd.ready = false;
mjr 78:1e00b3fa11af 2309 }
mjr 77:0b96f6867312 2310 }
mjr 78:1e00b3fa11af 2311
mjr 78:1e00b3fa11af 2312 // check for receiver tasks, if there's a receiver
mjr 78:1e00b3fa11af 2313 if (ir_rx != 0)
mjr 77:0b96f6867312 2314 {
mjr 78:1e00b3fa11af 2315 // Time out any received command
mjr 78:1e00b3fa11af 2316 if (IRCommandIn != 0)
mjr 78:1e00b3fa11af 2317 {
mjr 80:94dc2946871b 2318 // Time out commands after 200ms without a repeat signal.
mjr 80:94dc2946871b 2319 // Time out the inter-key gap after 50ms.
mjr 78:1e00b3fa11af 2320 uint32_t t = IRTimer.read_us();
mjr 80:94dc2946871b 2321 if (t > 200000)
mjr 78:1e00b3fa11af 2322 IRCommandIn = 0;
mjr 80:94dc2946871b 2323 else if (t > 50000)
mjr 78:1e00b3fa11af 2324 IRKeyGap = false;
mjr 78:1e00b3fa11af 2325 }
mjr 78:1e00b3fa11af 2326
mjr 78:1e00b3fa11af 2327 // Check if we're in learning mode
mjr 78:1e00b3fa11af 2328 if (IRLearningMode != 0)
mjr 78:1e00b3fa11af 2329 {
mjr 78:1e00b3fa11af 2330 // Learning mode. Read raw inputs from the IR sensor and
mjr 78:1e00b3fa11af 2331 // forward them to the PC via USB reports, up to the report
mjr 78:1e00b3fa11af 2332 // limit.
mjr 78:1e00b3fa11af 2333 const int nmax = USBJoystick::maxRawIR;
mjr 78:1e00b3fa11af 2334 uint16_t raw[nmax];
mjr 78:1e00b3fa11af 2335 int n;
mjr 78:1e00b3fa11af 2336 for (n = 0 ; n < nmax && ir_rx->processOne(raw[n]) ; ++n) ;
mjr 77:0b96f6867312 2337
mjr 78:1e00b3fa11af 2338 // if we read any raw samples, report them
mjr 78:1e00b3fa11af 2339 if (n != 0)
mjr 78:1e00b3fa11af 2340 js.reportRawIR(n, raw);
mjr 77:0b96f6867312 2341
mjr 78:1e00b3fa11af 2342 // check for a command
mjr 78:1e00b3fa11af 2343 IRCommand c;
mjr 78:1e00b3fa11af 2344 if (ir_rx->readCommand(c))
mjr 78:1e00b3fa11af 2345 {
mjr 78:1e00b3fa11af 2346 // check the current learning state
mjr 78:1e00b3fa11af 2347 switch (IRLearningMode)
mjr 78:1e00b3fa11af 2348 {
mjr 78:1e00b3fa11af 2349 case 1:
mjr 78:1e00b3fa11af 2350 // Initial state, waiting for the first decoded command.
mjr 78:1e00b3fa11af 2351 // This is it.
mjr 78:1e00b3fa11af 2352 learnedIRCode = c;
mjr 78:1e00b3fa11af 2353
mjr 78:1e00b3fa11af 2354 // Check if we need additional information. If the
mjr 78:1e00b3fa11af 2355 // protocol supports dittos, we have to wait for a repeat
mjr 78:1e00b3fa11af 2356 // to see if the remote actually uses the dittos, since
mjr 78:1e00b3fa11af 2357 // some implementations of such protocols use the dittos
mjr 78:1e00b3fa11af 2358 // while others just send repeated full codes. Otherwise,
mjr 78:1e00b3fa11af 2359 // all we need is the initial code, so we're done.
mjr 78:1e00b3fa11af 2360 IRLearningMode = (c.hasDittos ? 2 : 3);
mjr 78:1e00b3fa11af 2361 break;
mjr 78:1e00b3fa11af 2362
mjr 78:1e00b3fa11af 2363 case 2:
mjr 78:1e00b3fa11af 2364 // Code received, awaiting auto-repeat information. If
mjr 78:1e00b3fa11af 2365 // the protocol has dittos, check to see if we got a ditto:
mjr 78:1e00b3fa11af 2366 //
mjr 78:1e00b3fa11af 2367 // - If we received a ditto in the same protocol as the
mjr 78:1e00b3fa11af 2368 // prior command, the remote uses dittos.
mjr 78:1e00b3fa11af 2369 //
mjr 78:1e00b3fa11af 2370 // - If we received a repeat of the prior command (not a
mjr 78:1e00b3fa11af 2371 // ditto, but a repeat of the full code), the remote
mjr 78:1e00b3fa11af 2372 // doesn't use dittos even though the protocol supports
mjr 78:1e00b3fa11af 2373 // them.
mjr 78:1e00b3fa11af 2374 //
mjr 78:1e00b3fa11af 2375 // - Otherwise, it's not an auto-repeat at all, so we
mjr 78:1e00b3fa11af 2376 // can't decide one way or the other on dittos: start
mjr 78:1e00b3fa11af 2377 // over.
mjr 78:1e00b3fa11af 2378 if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2379 && c.hasDittos
mjr 78:1e00b3fa11af 2380 && c.ditto)
mjr 78:1e00b3fa11af 2381 {
mjr 78:1e00b3fa11af 2382 // success - the remote uses dittos
mjr 78:1e00b3fa11af 2383 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2384 }
mjr 78:1e00b3fa11af 2385 else if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2386 && c.hasDittos
mjr 78:1e00b3fa11af 2387 && !c.ditto
mjr 78:1e00b3fa11af 2388 && c.code == learnedIRCode.code)
mjr 78:1e00b3fa11af 2389 {
mjr 78:1e00b3fa11af 2390 // success - it's a repeat of the last code, so
mjr 78:1e00b3fa11af 2391 // the remote doesn't use dittos even though the
mjr 78:1e00b3fa11af 2392 // protocol supports them
mjr 78:1e00b3fa11af 2393 learnedIRCode.hasDittos = false;
mjr 78:1e00b3fa11af 2394 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2395 }
mjr 78:1e00b3fa11af 2396 else
mjr 78:1e00b3fa11af 2397 {
mjr 78:1e00b3fa11af 2398 // It's not a ditto and not a full repeat of the
mjr 78:1e00b3fa11af 2399 // last code, so it's either a new key, or some kind
mjr 78:1e00b3fa11af 2400 // of multi-code key encoding that we don't recognize.
mjr 78:1e00b3fa11af 2401 // We can't use this code, so start over.
mjr 78:1e00b3fa11af 2402 IRLearningMode = 1;
mjr 78:1e00b3fa11af 2403 }
mjr 78:1e00b3fa11af 2404 break;
mjr 78:1e00b3fa11af 2405 }
mjr 77:0b96f6867312 2406
mjr 78:1e00b3fa11af 2407 // If we ended in state 3, we've successfully decoded
mjr 78:1e00b3fa11af 2408 // the transmission. Report the decoded data and terminate
mjr 78:1e00b3fa11af 2409 // learning mode.
mjr 78:1e00b3fa11af 2410 if (IRLearningMode == 3)
mjr 77:0b96f6867312 2411 {
mjr 78:1e00b3fa11af 2412 // figure the flags:
mjr 78:1e00b3fa11af 2413 // 0x02 -> dittos
mjr 78:1e00b3fa11af 2414 uint8_t flags = 0;
mjr 78:1e00b3fa11af 2415 if (learnedIRCode.hasDittos)
mjr 78:1e00b3fa11af 2416 flags |= 0x02;
mjr 78:1e00b3fa11af 2417
mjr 78:1e00b3fa11af 2418 // report the code
mjr 78:1e00b3fa11af 2419 js.reportIRCode(learnedIRCode.proId, flags, learnedIRCode.code);
mjr 78:1e00b3fa11af 2420
mjr 78:1e00b3fa11af 2421 // exit learning mode
mjr 78:1e00b3fa11af 2422 IRLearningMode = 0;
mjr 77:0b96f6867312 2423 }
mjr 77:0b96f6867312 2424 }
mjr 77:0b96f6867312 2425
mjr 78:1e00b3fa11af 2426 // time out of IR learning mode if it's been too long
mjr 78:1e00b3fa11af 2427 if (IRLearningMode != 0 && IRTimer.read_us() > 10000000L)
mjr 77:0b96f6867312 2428 {
mjr 78:1e00b3fa11af 2429 // report the termination by sending a raw IR report with
mjr 78:1e00b3fa11af 2430 // zero data elements
mjr 78:1e00b3fa11af 2431 js.reportRawIR(0, 0);
mjr 78:1e00b3fa11af 2432
mjr 78:1e00b3fa11af 2433
mjr 78:1e00b3fa11af 2434 // cancel learning mode
mjr 77:0b96f6867312 2435 IRLearningMode = 0;
mjr 77:0b96f6867312 2436 }
mjr 77:0b96f6867312 2437 }
mjr 78:1e00b3fa11af 2438 else
mjr 77:0b96f6867312 2439 {
mjr 78:1e00b3fa11af 2440 // Not in learning mode. We don't care about the raw signals;
mjr 78:1e00b3fa11af 2441 // just run them through the protocol decoders.
mjr 78:1e00b3fa11af 2442 ir_rx->process();
mjr 78:1e00b3fa11af 2443
mjr 78:1e00b3fa11af 2444 // Check for decoded commands. Keep going until all commands
mjr 78:1e00b3fa11af 2445 // have been read.
mjr 78:1e00b3fa11af 2446 IRCommand c;
mjr 78:1e00b3fa11af 2447 while (ir_rx->readCommand(c))
mjr 77:0b96f6867312 2448 {
mjr 78:1e00b3fa11af 2449 // We received a decoded command. Determine if it's a repeat,
mjr 78:1e00b3fa11af 2450 // and if so, try to determine whether it's an auto-repeat (due
mjr 78:1e00b3fa11af 2451 // to the remote key being held down) or a distinct new press
mjr 78:1e00b3fa11af 2452 // on the same key as last time. The distinction is significant
mjr 78:1e00b3fa11af 2453 // because it affects the auto-repeat behavior of the PC key
mjr 78:1e00b3fa11af 2454 // input. An auto-repeat represents a key being held down on
mjr 78:1e00b3fa11af 2455 // the remote, which we want to translate to a (virtual) key
mjr 78:1e00b3fa11af 2456 // being held down on the PC keyboard; a distinct key press on
mjr 78:1e00b3fa11af 2457 // the remote translates to a distinct key press on the PC.
mjr 78:1e00b3fa11af 2458 //
mjr 78:1e00b3fa11af 2459 // It can only be a repeat if there's a prior command that
mjr 78:1e00b3fa11af 2460 // hasn't timed out yet, so start by checking for a previous
mjr 78:1e00b3fa11af 2461 // command.
mjr 78:1e00b3fa11af 2462 bool repeat = false, autoRepeat = false;
mjr 78:1e00b3fa11af 2463 if (IRCommandIn != 0)
mjr 77:0b96f6867312 2464 {
mjr 78:1e00b3fa11af 2465 // We have a command in progress. Check to see if the
mjr 78:1e00b3fa11af 2466 // new command is a repeat of the previous command. Check
mjr 78:1e00b3fa11af 2467 // first to see if it's a "ditto", which explicitly represents
mjr 78:1e00b3fa11af 2468 // an auto-repeat of the last command.
mjr 78:1e00b3fa11af 2469 IRCommandCfg &cmdcfg = cfg.IRCommand[IRCommandIn - 1];
mjr 78:1e00b3fa11af 2470 if (c.ditto)
mjr 78:1e00b3fa11af 2471 {
mjr 78:1e00b3fa11af 2472 // We received a ditto. Dittos are always auto-
mjr 78:1e00b3fa11af 2473 // repeats, so it's an auto-repeat as long as the
mjr 78:1e00b3fa11af 2474 // ditto is in the same protocol as the last command.
mjr 78:1e00b3fa11af 2475 // If the ditto is in a new protocol, the ditto can't
mjr 78:1e00b3fa11af 2476 // be for the last command we saw, because a ditto
mjr 78:1e00b3fa11af 2477 // never changes protocols from its antecedent. In
mjr 78:1e00b3fa11af 2478 // such a case, we must have missed the antecedent
mjr 78:1e00b3fa11af 2479 // command and thus don't know what's being repeated.
mjr 78:1e00b3fa11af 2480 repeat = autoRepeat = (c.proId == cmdcfg.protocol);
mjr 78:1e00b3fa11af 2481 }
mjr 78:1e00b3fa11af 2482 else
mjr 78:1e00b3fa11af 2483 {
mjr 78:1e00b3fa11af 2484 // It's not a ditto. The new command is a repeat if
mjr 78:1e00b3fa11af 2485 // it matches the protocol and command code of the
mjr 78:1e00b3fa11af 2486 // prior command.
mjr 78:1e00b3fa11af 2487 repeat = (c.proId == cmdcfg.protocol
mjr 78:1e00b3fa11af 2488 && uint32_t(c.code) == cmdcfg.code.lo
mjr 78:1e00b3fa11af 2489 && uint32_t(c.code >> 32) == cmdcfg.code.hi);
mjr 78:1e00b3fa11af 2490
mjr 78:1e00b3fa11af 2491 // If the command is a repeat, try to determine whether
mjr 78:1e00b3fa11af 2492 // it's an auto-repeat or a new press on the same key.
mjr 78:1e00b3fa11af 2493 // If the protocol uses dittos, it's definitely a new
mjr 78:1e00b3fa11af 2494 // key press, because an auto-repeat would have used a
mjr 78:1e00b3fa11af 2495 // ditto. For a protocol that doesn't use dittos, both
mjr 78:1e00b3fa11af 2496 // an auto-repeat and a new key press just send the key
mjr 78:1e00b3fa11af 2497 // code again, so we can't tell the difference based on
mjr 78:1e00b3fa11af 2498 // that alone. But if the protocol has a toggle bit, we
mjr 78:1e00b3fa11af 2499 // can tell by the toggle bit value: a new key press has
mjr 78:1e00b3fa11af 2500 // the opposite toggle value as the last key press, while
mjr 78:1e00b3fa11af 2501 // an auto-repeat has the same toggle. Note that if the
mjr 78:1e00b3fa11af 2502 // protocol doesn't use toggle bits, the toggle value
mjr 78:1e00b3fa11af 2503 // will always be the same, so we'll simply always treat
mjr 78:1e00b3fa11af 2504 // any repeat as an auto-repeat. Many protocols simply
mjr 78:1e00b3fa11af 2505 // provide no way to distinguish the two, so in such
mjr 78:1e00b3fa11af 2506 // cases it's consistent with the native implementations
mjr 78:1e00b3fa11af 2507 // to treat any repeat as an auto-repeat.
mjr 78:1e00b3fa11af 2508 autoRepeat =
mjr 78:1e00b3fa11af 2509 repeat
mjr 78:1e00b3fa11af 2510 && !(cmdcfg.flags & IRFlagDittos)
mjr 78:1e00b3fa11af 2511 && c.toggle == lastIRToggle;
mjr 78:1e00b3fa11af 2512 }
mjr 78:1e00b3fa11af 2513 }
mjr 78:1e00b3fa11af 2514
mjr 78:1e00b3fa11af 2515 // Check to see if it's a repeat of any kind
mjr 78:1e00b3fa11af 2516 if (repeat)
mjr 78:1e00b3fa11af 2517 {
mjr 78:1e00b3fa11af 2518 // It's a repeat. If it's not an auto-repeat, it's a
mjr 78:1e00b3fa11af 2519 // new distinct key press, so we need to send the PC a
mjr 78:1e00b3fa11af 2520 // momentary gap where we're not sending the same key,
mjr 78:1e00b3fa11af 2521 // so that the PC also recognizes this as a distinct
mjr 78:1e00b3fa11af 2522 // key press event.
mjr 78:1e00b3fa11af 2523 if (!autoRepeat)
mjr 78:1e00b3fa11af 2524 IRKeyGap = true;
mjr 78:1e00b3fa11af 2525
mjr 78:1e00b3fa11af 2526 // restart the key-up timer
mjr 78:1e00b3fa11af 2527 IRTimer.reset();
mjr 78:1e00b3fa11af 2528 }
mjr 78:1e00b3fa11af 2529 else if (c.ditto)
mjr 78:1e00b3fa11af 2530 {
mjr 78:1e00b3fa11af 2531 // It's a ditto, but not a repeat of the last command.
mjr 78:1e00b3fa11af 2532 // But a ditto doesn't contain any information of its own
mjr 78:1e00b3fa11af 2533 // on the command being repeated, so given that it's not
mjr 78:1e00b3fa11af 2534 // our last command, we can't infer what command the ditto
mjr 78:1e00b3fa11af 2535 // is for and thus can't make sense of it. We have to
mjr 78:1e00b3fa11af 2536 // simply ignore it and wait for the sender to start with
mjr 78:1e00b3fa11af 2537 // a full command for a new key press.
mjr 78:1e00b3fa11af 2538 IRCommandIn = 0;
mjr 77:0b96f6867312 2539 }
mjr 77:0b96f6867312 2540 else
mjr 77:0b96f6867312 2541 {
mjr 78:1e00b3fa11af 2542 // It's not a repeat, so the last command is no longer
mjr 78:1e00b3fa11af 2543 // in effect (regardless of whether we find a match for
mjr 78:1e00b3fa11af 2544 // the new command).
mjr 78:1e00b3fa11af 2545 IRCommandIn = 0;
mjr 77:0b96f6867312 2546
mjr 78:1e00b3fa11af 2547 // Check to see if we recognize the new command, by
mjr 78:1e00b3fa11af 2548 // searching for a match in our learned code list.
mjr 78:1e00b3fa11af 2549 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2550 {
mjr 78:1e00b3fa11af 2551 // if the protocol and command code from the code
mjr 78:1e00b3fa11af 2552 // list both match the input, it's a match
mjr 78:1e00b3fa11af 2553 IRCommandCfg &cmdcfg = cfg.IRCommand[i];
mjr 78:1e00b3fa11af 2554 if (cmdcfg.protocol == c.proId
mjr 78:1e00b3fa11af 2555 && cmdcfg.code.lo == uint32_t(c.code)
mjr 78:1e00b3fa11af 2556 && cmdcfg.code.hi == uint32_t(c.code >> 32))
mjr 78:1e00b3fa11af 2557 {
mjr 78:1e00b3fa11af 2558 // Found it! Make this the last command, and
mjr 78:1e00b3fa11af 2559 // remember the starting time.
mjr 78:1e00b3fa11af 2560 IRCommandIn = i + 1;
mjr 78:1e00b3fa11af 2561 lastIRToggle = c.toggle;
mjr 78:1e00b3fa11af 2562 IRTimer.reset();
mjr 78:1e00b3fa11af 2563
mjr 78:1e00b3fa11af 2564 // no need to keep searching
mjr 78:1e00b3fa11af 2565 break;
mjr 78:1e00b3fa11af 2566 }
mjr 77:0b96f6867312 2567 }
mjr 77:0b96f6867312 2568 }
mjr 77:0b96f6867312 2569 }
mjr 77:0b96f6867312 2570 }
mjr 77:0b96f6867312 2571 }
mjr 77:0b96f6867312 2572 }
mjr 77:0b96f6867312 2573
mjr 74:822a92bc11d2 2574
mjr 11:bd9da7088e6e 2575 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 2576 //
mjr 11:bd9da7088e6e 2577 // Button input
mjr 11:bd9da7088e6e 2578 //
mjr 11:bd9da7088e6e 2579
mjr 18:5e890ebd0023 2580 // button state
mjr 18:5e890ebd0023 2581 struct ButtonState
mjr 18:5e890ebd0023 2582 {
mjr 38:091e511ce8a0 2583 ButtonState()
mjr 38:091e511ce8a0 2584 {
mjr 53:9b2611964afc 2585 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 2586 virtState = 0;
mjr 53:9b2611964afc 2587 dbState = 0;
mjr 38:091e511ce8a0 2588 pulseState = 0;
mjr 53:9b2611964afc 2589 pulseTime = 0;
mjr 38:091e511ce8a0 2590 }
mjr 35:e959ffba78fd 2591
mjr 53:9b2611964afc 2592 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 2593 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 2594 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 2595 //
mjr 53:9b2611964afc 2596 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 2597 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 2598 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 2599 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 2600 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 2601 void virtPress(bool on)
mjr 53:9b2611964afc 2602 {
mjr 53:9b2611964afc 2603 // Increment or decrement the current state
mjr 53:9b2611964afc 2604 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 2605 }
mjr 53:9b2611964afc 2606
mjr 53:9b2611964afc 2607 // DigitalIn for the button, if connected to a physical input
mjr 73:4e8ce0b18915 2608 TinyDigitalIn di;
mjr 38:091e511ce8a0 2609
mjr 65:739875521aae 2610 // Time of last pulse state transition.
mjr 65:739875521aae 2611 //
mjr 65:739875521aae 2612 // Each state change sticks for a minimum period; when the timer expires,
mjr 65:739875521aae 2613 // if the underlying physical switch is in a different state, we switch
mjr 65:739875521aae 2614 // to the next state and restart the timer. pulseTime is the time remaining
mjr 65:739875521aae 2615 // remaining before we can make another state transition, in microseconds.
mjr 65:739875521aae 2616 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 65:739875521aae 2617 // this guarantees that the parity of the pulse count always matches the
mjr 65:739875521aae 2618 // current physical switch state when the latter is stable, which makes
mjr 65:739875521aae 2619 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 65:739875521aae 2620 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 65:739875521aae 2621 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 65:739875521aae 2622 // This software system can't be fooled that way.)
mjr 65:739875521aae 2623 uint32_t pulseTime;
mjr 18:5e890ebd0023 2624
mjr 65:739875521aae 2625 // Config key index. This points to the ButtonCfg structure in the
mjr 65:739875521aae 2626 // configuration that contains the PC key mapping for the button.
mjr 65:739875521aae 2627 uint8_t cfgIndex;
mjr 53:9b2611964afc 2628
mjr 53:9b2611964afc 2629 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 2630 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 2631 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 2632 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 2633 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 2634 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 2635 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 2636 // and physical source states.
mjr 53:9b2611964afc 2637 uint8_t virtState;
mjr 38:091e511ce8a0 2638
mjr 38:091e511ce8a0 2639 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 2640 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 2641 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 2642 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 2643 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 2644 uint8_t dbState;
mjr 38:091e511ce8a0 2645
mjr 65:739875521aae 2646 // current PHYSICAL on/off state, after debouncing
mjr 65:739875521aae 2647 uint8_t physState : 1;
mjr 65:739875521aae 2648
mjr 65:739875521aae 2649 // current LOGICAL on/off state as reported to the host.
mjr 65:739875521aae 2650 uint8_t logState : 1;
mjr 65:739875521aae 2651
mjr 79:682ae3171a08 2652 // Previous logical on/off state, when keys were last processed for USB
mjr 79:682ae3171a08 2653 // reports and local effects. This lets us detect edges (transitions)
mjr 79:682ae3171a08 2654 // in the logical state, for effects that are triggered when the state
mjr 79:682ae3171a08 2655 // changes rather than merely by the button being on or off.
mjr 65:739875521aae 2656 uint8_t prevLogState : 1;
mjr 65:739875521aae 2657
mjr 65:739875521aae 2658 // Pulse state
mjr 65:739875521aae 2659 //
mjr 65:739875521aae 2660 // A button in pulse mode (selected via the config flags for the button)
mjr 65:739875521aae 2661 // transmits a brief logical button press and release each time the attached
mjr 65:739875521aae 2662 // physical switch changes state. This is useful for cases where the host
mjr 65:739875521aae 2663 // expects a key press for each change in the state of the physical switch.
mjr 65:739875521aae 2664 // The canonical example is the Coin Door switch in VPinMAME, which requires
mjr 65:739875521aae 2665 // pressing the END key to toggle the open/closed state. This software design
mjr 65:739875521aae 2666 // isn't easily implemented in a physical coin door, though; the simplest
mjr 65:739875521aae 2667 // physical sensor for the coin door state is a switch that's on when the
mjr 65:739875521aae 2668 // door is open and off when the door is closed (or vice versa, but in either
mjr 65:739875521aae 2669 // case, the switch state corresponds to the current state of the door at any
mjr 65:739875521aae 2670 // given time, rather than pulsing on state changes). The "pulse mode"
mjr 79:682ae3171a08 2671 // option bridges this gap by generating a toggle key event each time
mjr 65:739875521aae 2672 // there's a change to the physical switch's state.
mjr 38:091e511ce8a0 2673 //
mjr 38:091e511ce8a0 2674 // Pulse state:
mjr 38:091e511ce8a0 2675 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 2676 // 1 -> off
mjr 38:091e511ce8a0 2677 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 2678 // 3 -> on
mjr 38:091e511ce8a0 2679 // 4 -> transitioning on-off
mjr 65:739875521aae 2680 uint8_t pulseState : 3; // 5 states -> we need 3 bits
mjr 65:739875521aae 2681
mjr 65:739875521aae 2682 } __attribute__((packed));
mjr 65:739875521aae 2683
mjr 65:739875521aae 2684 ButtonState *buttonState; // live button slots, allocated on startup
mjr 65:739875521aae 2685 int8_t nButtons; // number of live button slots allocated
mjr 65:739875521aae 2686 int8_t zblButtonIndex = -1; // index of ZB Launch button slot; -1 if unused
mjr 18:5e890ebd0023 2687
mjr 66:2e3583fbd2f4 2688 // Shift button state
mjr 66:2e3583fbd2f4 2689 struct
mjr 66:2e3583fbd2f4 2690 {
mjr 66:2e3583fbd2f4 2691 int8_t index; // buttonState[] index of shift button; -1 if none
mjr 78:1e00b3fa11af 2692 uint8_t state; // current state, for "Key OR Shift" mode:
mjr 66:2e3583fbd2f4 2693 // 0 = not shifted
mjr 66:2e3583fbd2f4 2694 // 1 = shift button down, no key pressed yet
mjr 66:2e3583fbd2f4 2695 // 2 = shift button down, key pressed
mjr 78:1e00b3fa11af 2696 // 3 = released, sending pulsed keystroke
mjr 78:1e00b3fa11af 2697 uint32_t pulseTime; // time remaining in pulsed keystroke (state 3)
mjr 66:2e3583fbd2f4 2698 }
mjr 66:2e3583fbd2f4 2699 __attribute__((packed)) shiftButton;
mjr 38:091e511ce8a0 2700
mjr 38:091e511ce8a0 2701 // Button data
mjr 38:091e511ce8a0 2702 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 2703
mjr 38:091e511ce8a0 2704 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 2705 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 2706 // modifier keys.
mjr 38:091e511ce8a0 2707 struct
mjr 38:091e511ce8a0 2708 {
mjr 38:091e511ce8a0 2709 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 2710 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 2711 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 2712 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 2713 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 2714
mjr 38:091e511ce8a0 2715 // Media key state
mjr 38:091e511ce8a0 2716 struct
mjr 38:091e511ce8a0 2717 {
mjr 38:091e511ce8a0 2718 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 2719 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 2720 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 2721
mjr 79:682ae3171a08 2722 // button scan interrupt timer
mjr 79:682ae3171a08 2723 Timeout scanButtonsTimeout;
mjr 38:091e511ce8a0 2724
mjr 38:091e511ce8a0 2725 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 2726 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 2727 void scanButtons()
mjr 38:091e511ce8a0 2728 {
mjr 79:682ae3171a08 2729 // schedule the next interrupt
mjr 79:682ae3171a08 2730 scanButtonsTimeout.attach_us(&scanButtons, 1000);
mjr 79:682ae3171a08 2731
mjr 38:091e511ce8a0 2732 // scan all button input pins
mjr 73:4e8ce0b18915 2733 ButtonState *bs = buttonState, *last = bs + nButtons;
mjr 73:4e8ce0b18915 2734 for ( ; bs < last ; ++bs)
mjr 38:091e511ce8a0 2735 {
mjr 73:4e8ce0b18915 2736 // Shift the new state into the debounce history
mjr 73:4e8ce0b18915 2737 uint8_t db = (bs->dbState << 1) | bs->di.read();
mjr 73:4e8ce0b18915 2738 bs->dbState = db;
mjr 73:4e8ce0b18915 2739
mjr 73:4e8ce0b18915 2740 // If we have all 0's or 1's in the history for the required
mjr 73:4e8ce0b18915 2741 // debounce period, the key state is stable, so apply the new
mjr 73:4e8ce0b18915 2742 // physical state. Note that the pins are active low, so the
mjr 73:4e8ce0b18915 2743 // new button on/off state is the inverse of the GPIO state.
mjr 73:4e8ce0b18915 2744 const uint8_t stable = 0x1F; // 00011111b -> low 5 bits = last 5 readings
mjr 73:4e8ce0b18915 2745 db &= stable;
mjr 73:4e8ce0b18915 2746 if (db == 0 || db == stable)
mjr 73:4e8ce0b18915 2747 bs->physState = !db;
mjr 38:091e511ce8a0 2748 }
mjr 38:091e511ce8a0 2749 }
mjr 38:091e511ce8a0 2750
mjr 38:091e511ce8a0 2751 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 2752 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 2753 // in the physical button state.
mjr 38:091e511ce8a0 2754 Timer buttonTimer;
mjr 12:669df364a565 2755
mjr 65:739875521aae 2756 // Count a button during the initial setup scan
mjr 72:884207c0aab0 2757 void countButton(uint8_t typ, uint8_t shiftTyp, bool &kbKeys)
mjr 65:739875521aae 2758 {
mjr 65:739875521aae 2759 // count it
mjr 65:739875521aae 2760 ++nButtons;
mjr 65:739875521aae 2761
mjr 67:c39e66c4e000 2762 // if it's a keyboard key or media key, note that we need a USB
mjr 67:c39e66c4e000 2763 // keyboard interface
mjr 72:884207c0aab0 2764 if (typ == BtnTypeKey || typ == BtnTypeMedia
mjr 72:884207c0aab0 2765 || shiftTyp == BtnTypeKey || shiftTyp == BtnTypeMedia)
mjr 65:739875521aae 2766 kbKeys = true;
mjr 65:739875521aae 2767 }
mjr 65:739875521aae 2768
mjr 11:bd9da7088e6e 2769 // initialize the button inputs
mjr 35:e959ffba78fd 2770 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 2771 {
mjr 66:2e3583fbd2f4 2772 // presume no shift key
mjr 66:2e3583fbd2f4 2773 shiftButton.index = -1;
mjr 82:4f6209cb5c33 2774 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 2775
mjr 65:739875521aae 2776 // Count up how many button slots we'll need to allocate. Start
mjr 65:739875521aae 2777 // with assigned buttons from the configuration, noting that we
mjr 65:739875521aae 2778 // only need to create slots for buttons that are actually wired.
mjr 65:739875521aae 2779 nButtons = 0;
mjr 65:739875521aae 2780 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 2781 {
mjr 65:739875521aae 2782 // it's valid if it's wired to a real input pin
mjr 65:739875521aae 2783 if (wirePinName(cfg.button[i].pin) != NC)
mjr 72:884207c0aab0 2784 countButton(cfg.button[i].typ, cfg.button[i].typ2, kbKeys);
mjr 65:739875521aae 2785 }
mjr 65:739875521aae 2786
mjr 65:739875521aae 2787 // Count virtual buttons
mjr 65:739875521aae 2788
mjr 65:739875521aae 2789 // ZB Launch
mjr 65:739875521aae 2790 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 65:739875521aae 2791 {
mjr 65:739875521aae 2792 // valid - remember the live button index
mjr 65:739875521aae 2793 zblButtonIndex = nButtons;
mjr 65:739875521aae 2794
mjr 65:739875521aae 2795 // count it
mjr 72:884207c0aab0 2796 countButton(cfg.plunger.zbLaunchBall.keytype, BtnTypeNone, kbKeys);
mjr 65:739875521aae 2797 }
mjr 65:739875521aae 2798
mjr 65:739875521aae 2799 // Allocate the live button slots
mjr 65:739875521aae 2800 ButtonState *bs = buttonState = new ButtonState[nButtons];
mjr 65:739875521aae 2801
mjr 65:739875521aae 2802 // Configure the physical inputs
mjr 65:739875521aae 2803 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 2804 {
mjr 65:739875521aae 2805 PinName pin = wirePinName(cfg.button[i].pin);
mjr 65:739875521aae 2806 if (pin != NC)
mjr 65:739875521aae 2807 {
mjr 65:739875521aae 2808 // point back to the config slot for the keyboard data
mjr 65:739875521aae 2809 bs->cfgIndex = i;
mjr 65:739875521aae 2810
mjr 65:739875521aae 2811 // set up the GPIO input pin for this button
mjr 73:4e8ce0b18915 2812 bs->di.assignPin(pin);
mjr 65:739875521aae 2813
mjr 65:739875521aae 2814 // if it's a pulse mode button, set the initial pulse state to Off
mjr 65:739875521aae 2815 if (cfg.button[i].flags & BtnFlagPulse)
mjr 65:739875521aae 2816 bs->pulseState = 1;
mjr 65:739875521aae 2817
mjr 66:2e3583fbd2f4 2818 // If this is the shift button, note its buttonState[] index.
mjr 66:2e3583fbd2f4 2819 // We have to figure the buttonState[] index separately from
mjr 66:2e3583fbd2f4 2820 // the config index, because the indices can differ if some
mjr 66:2e3583fbd2f4 2821 // config slots are left unused.
mjr 78:1e00b3fa11af 2822 if (cfg.shiftButton.idx == i+1)
mjr 66:2e3583fbd2f4 2823 shiftButton.index = bs - buttonState;
mjr 66:2e3583fbd2f4 2824
mjr 65:739875521aae 2825 // advance to the next button
mjr 65:739875521aae 2826 ++bs;
mjr 65:739875521aae 2827 }
mjr 65:739875521aae 2828 }
mjr 65:739875521aae 2829
mjr 53:9b2611964afc 2830 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 2831 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 2832 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 2833 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 2834
mjr 53:9b2611964afc 2835 // ZB Launch Ball button
mjr 65:739875521aae 2836 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 11:bd9da7088e6e 2837 {
mjr 65:739875521aae 2838 // Point back to the config slot for the keyboard data.
mjr 66:2e3583fbd2f4 2839 // We use a special extra slot for virtual buttons,
mjr 66:2e3583fbd2f4 2840 // so we also need to set up the slot data by copying
mjr 66:2e3583fbd2f4 2841 // the ZBL config data to our virtual button slot.
mjr 65:739875521aae 2842 bs->cfgIndex = ZBL_BUTTON_CFG;
mjr 65:739875521aae 2843 cfg.button[ZBL_BUTTON_CFG].pin = PINNAME_TO_WIRE(NC);
mjr 65:739875521aae 2844 cfg.button[ZBL_BUTTON_CFG].typ = cfg.plunger.zbLaunchBall.keytype;
mjr 65:739875521aae 2845 cfg.button[ZBL_BUTTON_CFG].val = cfg.plunger.zbLaunchBall.keycode;
mjr 65:739875521aae 2846
mjr 66:2e3583fbd2f4 2847 // advance to the next button
mjr 65:739875521aae 2848 ++bs;
mjr 11:bd9da7088e6e 2849 }
mjr 12:669df364a565 2850
mjr 38:091e511ce8a0 2851 // start the button scan thread
mjr 79:682ae3171a08 2852 scanButtonsTimeout.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 2853
mjr 38:091e511ce8a0 2854 // start the button state transition timer
mjr 12:669df364a565 2855 buttonTimer.start();
mjr 11:bd9da7088e6e 2856 }
mjr 11:bd9da7088e6e 2857
mjr 67:c39e66c4e000 2858 // Media key mapping. This maps from an 8-bit USB media key
mjr 67:c39e66c4e000 2859 // code to the corresponding bit in our USB report descriptor.
mjr 67:c39e66c4e000 2860 // The USB key code is the index, and the value at the index
mjr 67:c39e66c4e000 2861 // is the report descriptor bit. See joystick.cpp for the
mjr 67:c39e66c4e000 2862 // media descriptor details. Our currently mapped keys are:
mjr 67:c39e66c4e000 2863 //
mjr 67:c39e66c4e000 2864 // 0xE2 -> Mute -> 0x01
mjr 67:c39e66c4e000 2865 // 0xE9 -> Volume Up -> 0x02
mjr 67:c39e66c4e000 2866 // 0xEA -> Volume Down -> 0x04
mjr 67:c39e66c4e000 2867 // 0xB5 -> Next Track -> 0x08
mjr 67:c39e66c4e000 2868 // 0xB6 -> Previous Track -> 0x10
mjr 67:c39e66c4e000 2869 // 0xB7 -> Stop -> 0x20
mjr 67:c39e66c4e000 2870 // 0xCD -> Play / Pause -> 0x40
mjr 67:c39e66c4e000 2871 //
mjr 67:c39e66c4e000 2872 static const uint8_t mediaKeyMap[] = {
mjr 67:c39e66c4e000 2873 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-0F
mjr 67:c39e66c4e000 2874 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-1F
mjr 67:c39e66c4e000 2875 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-2F
mjr 67:c39e66c4e000 2876 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-3F
mjr 67:c39e66c4e000 2877 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-4F
mjr 67:c39e66c4e000 2878 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-5F
mjr 67:c39e66c4e000 2879 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-6F
mjr 67:c39e66c4e000 2880 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-7F
mjr 67:c39e66c4e000 2881 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
mjr 67:c39e66c4e000 2882 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-9F
mjr 67:c39e66c4e000 2883 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0-AF
mjr 67:c39e66c4e000 2884 0, 0, 0, 0, 0, 8, 16, 32, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
mjr 67:c39e66c4e000 2885 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, // C0-CF
mjr 67:c39e66c4e000 2886 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D0-DF
mjr 67:c39e66c4e000 2887 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, // E0-EF
mjr 67:c39e66c4e000 2888 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-FF
mjr 77:0b96f6867312 2889 };
mjr 77:0b96f6867312 2890
mjr 77:0b96f6867312 2891 // Keyboard key/joystick button state. processButtons() uses this to
mjr 77:0b96f6867312 2892 // build the set of key presses to report to the PC based on the logical
mjr 77:0b96f6867312 2893 // states of the button iputs.
mjr 77:0b96f6867312 2894 struct KeyState
mjr 77:0b96f6867312 2895 {
mjr 77:0b96f6867312 2896 KeyState()
mjr 77:0b96f6867312 2897 {
mjr 77:0b96f6867312 2898 // zero all members
mjr 77:0b96f6867312 2899 memset(this, 0, sizeof(*this));
mjr 77:0b96f6867312 2900 }
mjr 77:0b96f6867312 2901
mjr 77:0b96f6867312 2902 // Keyboard media keys currently pressed. This is a bit vector in
mjr 77:0b96f6867312 2903 // the format used in our USB keyboard reports (see USBJoystick.cpp).
mjr 77:0b96f6867312 2904 uint8_t mediakeys;
mjr 77:0b96f6867312 2905
mjr 77:0b96f6867312 2906 // Keyboard modifier (shift) keys currently pressed. This is a bit
mjr 77:0b96f6867312 2907 // vector in the format used in our USB keyboard reports (see
mjr 77:0b96f6867312 2908 // USBJoystick.cpp).
mjr 77:0b96f6867312 2909 uint8_t modkeys;
mjr 77:0b96f6867312 2910
mjr 77:0b96f6867312 2911 // Regular keyboard keys currently pressed. Each element is a USB
mjr 77:0b96f6867312 2912 // key code, or 0 for empty slots. Note that the USB report format
mjr 77:0b96f6867312 2913 // theoretically allows a flexible size limit, but the Windows KB
mjr 77:0b96f6867312 2914 // drivers have a fixed limit of 6 simultaneous keys (and won't
mjr 77:0b96f6867312 2915 // accept reports with more), so there's no point in making this
mjr 77:0b96f6867312 2916 // flexible; we'll just use the fixed size dictated by Windows.
mjr 77:0b96f6867312 2917 uint8_t keys[7];
mjr 77:0b96f6867312 2918
mjr 77:0b96f6867312 2919 // number of valid entries in keys[] array
mjr 77:0b96f6867312 2920 int nkeys;
mjr 77:0b96f6867312 2921
mjr 77:0b96f6867312 2922 // Joystick buttons pressed, as a bit vector. Bit n (1 << n)
mjr 77:0b96f6867312 2923 // represents joystick button n, n in 0..31, with 0 meaning
mjr 77:0b96f6867312 2924 // unpressed and 1 meaning pressed.
mjr 77:0b96f6867312 2925 uint32_t js;
mjr 77:0b96f6867312 2926
mjr 77:0b96f6867312 2927
mjr 77:0b96f6867312 2928 // Add a key press. 'typ' is the button type code (ButtonTypeXxx),
mjr 77:0b96f6867312 2929 // and 'val' is the value (the meaning of which varies by type code).
mjr 77:0b96f6867312 2930 void addKey(uint8_t typ, uint8_t val)
mjr 77:0b96f6867312 2931 {
mjr 77:0b96f6867312 2932 // add the key according to the type
mjr 77:0b96f6867312 2933 switch (typ)
mjr 77:0b96f6867312 2934 {
mjr 77:0b96f6867312 2935 case BtnTypeJoystick:
mjr 77:0b96f6867312 2936 // joystick button
mjr 77:0b96f6867312 2937 js |= (1 << (val - 1));
mjr 77:0b96f6867312 2938 break;
mjr 77:0b96f6867312 2939
mjr 77:0b96f6867312 2940 case BtnTypeKey:
mjr 77:0b96f6867312 2941 // Keyboard key. The USB keyboard report encodes regular
mjr 77:0b96f6867312 2942 // keys and modifier keys separately, so we need to check
mjr 77:0b96f6867312 2943 // which type we have. Note that past versions mapped the
mjr 77:0b96f6867312 2944 // Keyboard Volume Up, Keyboard Volume Down, and Keyboard
mjr 77:0b96f6867312 2945 // Mute keys to the corresponding Media keys. We no longer
mjr 77:0b96f6867312 2946 // do this; instead, we have the separate BtnTypeMedia for
mjr 77:0b96f6867312 2947 // explicitly using media keys if desired.
mjr 77:0b96f6867312 2948 if (val >= 0xE0 && val <= 0xE7)
mjr 77:0b96f6867312 2949 {
mjr 77:0b96f6867312 2950 // It's a modifier key. These are represented in the USB
mjr 77:0b96f6867312 2951 // reports with a bit mask. We arrange the mask bits in
mjr 77:0b96f6867312 2952 // the same order as the scan codes, so we can figure the
mjr 77:0b96f6867312 2953 // appropriate bit with a simple shift.
mjr 77:0b96f6867312 2954 modkeys |= (1 << (val - 0xE0));
mjr 77:0b96f6867312 2955 }
mjr 77:0b96f6867312 2956 else
mjr 77:0b96f6867312 2957 {
mjr 77:0b96f6867312 2958 // It's a regular key. Make sure it's not already in the
mjr 77:0b96f6867312 2959 // list, and that the list isn't full. If neither of these
mjr 77:0b96f6867312 2960 // apply, add the key to the key array.
mjr 77:0b96f6867312 2961 if (nkeys < 7)
mjr 77:0b96f6867312 2962 {
mjr 77:0b96f6867312 2963 bool found = false;
mjr 77:0b96f6867312 2964 for (int i = 0 ; i < nkeys ; ++i)
mjr 77:0b96f6867312 2965 {
mjr 77:0b96f6867312 2966 if (keys[i] == val)
mjr 77:0b96f6867312 2967 {
mjr 77:0b96f6867312 2968 found = true;
mjr 77:0b96f6867312 2969 break;
mjr 77:0b96f6867312 2970 }
mjr 77:0b96f6867312 2971 }
mjr 77:0b96f6867312 2972 if (!found)
mjr 77:0b96f6867312 2973 keys[nkeys++] = val;
mjr 77:0b96f6867312 2974 }
mjr 77:0b96f6867312 2975 }
mjr 77:0b96f6867312 2976 break;
mjr 77:0b96f6867312 2977
mjr 77:0b96f6867312 2978 case BtnTypeMedia:
mjr 77:0b96f6867312 2979 // Media control key. The media keys are mapped in the USB
mjr 77:0b96f6867312 2980 // report to bits, whereas the key codes are specified in the
mjr 77:0b96f6867312 2981 // config with their USB usage numbers. E.g., the config val
mjr 77:0b96f6867312 2982 // for Media Next Track is 0xB5, but we encode this in the USB
mjr 77:0b96f6867312 2983 // report as bit 0x08. The mediaKeyMap[] table translates
mjr 77:0b96f6867312 2984 // from the USB usage number to the mask bit. If the key isn't
mjr 77:0b96f6867312 2985 // among the subset we support, the mapped bit will be zero, so
mjr 77:0b96f6867312 2986 // the "|=" will have no effect and the key will be ignored.
mjr 77:0b96f6867312 2987 mediakeys |= mediaKeyMap[val];
mjr 77:0b96f6867312 2988 break;
mjr 77:0b96f6867312 2989 }
mjr 77:0b96f6867312 2990 }
mjr 77:0b96f6867312 2991 };
mjr 67:c39e66c4e000 2992
mjr 67:c39e66c4e000 2993
mjr 38:091e511ce8a0 2994 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 2995 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 2996 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 2997 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 2998 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 2999 {
mjr 77:0b96f6867312 3000 // key state
mjr 77:0b96f6867312 3001 KeyState ks;
mjr 38:091e511ce8a0 3002
mjr 38:091e511ce8a0 3003 // calculate the time since the last run
mjr 53:9b2611964afc 3004 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 3005 buttonTimer.reset();
mjr 66:2e3583fbd2f4 3006
mjr 66:2e3583fbd2f4 3007 // check the shift button state
mjr 66:2e3583fbd2f4 3008 if (shiftButton.index != -1)
mjr 66:2e3583fbd2f4 3009 {
mjr 78:1e00b3fa11af 3010 // get the shift button's physical state object
mjr 66:2e3583fbd2f4 3011 ButtonState *sbs = &buttonState[shiftButton.index];
mjr 78:1e00b3fa11af 3012
mjr 78:1e00b3fa11af 3013 // figure what to do based on the shift button mode in the config
mjr 78:1e00b3fa11af 3014 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 3015 {
mjr 66:2e3583fbd2f4 3016 case 0:
mjr 78:1e00b3fa11af 3017 default:
mjr 78:1e00b3fa11af 3018 // "Shift OR Key" mode. The shift button doesn't send its key
mjr 78:1e00b3fa11af 3019 // immediately when pressed. Instead, we wait to see what
mjr 78:1e00b3fa11af 3020 // happens while it's down. Check the current cycle state.
mjr 78:1e00b3fa11af 3021 switch (shiftButton.state)
mjr 78:1e00b3fa11af 3022 {
mjr 78:1e00b3fa11af 3023 case 0:
mjr 78:1e00b3fa11af 3024 // Not shifted. Check if the button is now down: if so,
mjr 78:1e00b3fa11af 3025 // switch to state 1 (shift button down, no key pressed yet).
mjr 78:1e00b3fa11af 3026 if (sbs->physState)
mjr 78:1e00b3fa11af 3027 shiftButton.state = 1;
mjr 78:1e00b3fa11af 3028 break;
mjr 78:1e00b3fa11af 3029
mjr 78:1e00b3fa11af 3030 case 1:
mjr 78:1e00b3fa11af 3031 // Shift button down, no key pressed yet. If the button is
mjr 78:1e00b3fa11af 3032 // now up, it counts as an ordinary button press instead of
mjr 78:1e00b3fa11af 3033 // a shift button press, since the shift function was never
mjr 78:1e00b3fa11af 3034 // used. Return to unshifted state and start a timed key
mjr 78:1e00b3fa11af 3035 // pulse event.
mjr 78:1e00b3fa11af 3036 if (!sbs->physState)
mjr 78:1e00b3fa11af 3037 {
mjr 78:1e00b3fa11af 3038 shiftButton.state = 3;
mjr 78:1e00b3fa11af 3039 shiftButton.pulseTime = 50000+dt; // 50 ms left on the key pulse
mjr 78:1e00b3fa11af 3040 }
mjr 78:1e00b3fa11af 3041 break;
mjr 78:1e00b3fa11af 3042
mjr 78:1e00b3fa11af 3043 case 2:
mjr 78:1e00b3fa11af 3044 // Shift button down, other key was pressed. If the button is
mjr 78:1e00b3fa11af 3045 // now up, simply clear the shift state without sending a key
mjr 78:1e00b3fa11af 3046 // press for the shift button itself to the PC. The shift
mjr 78:1e00b3fa11af 3047 // function was used, so its ordinary key press function is
mjr 78:1e00b3fa11af 3048 // suppressed.
mjr 78:1e00b3fa11af 3049 if (!sbs->physState)
mjr 78:1e00b3fa11af 3050 shiftButton.state = 0;
mjr 78:1e00b3fa11af 3051 break;
mjr 78:1e00b3fa11af 3052
mjr 78:1e00b3fa11af 3053 case 3:
mjr 78:1e00b3fa11af 3054 // Sending pulsed keystroke. Deduct the current time interval
mjr 78:1e00b3fa11af 3055 // from the remaining pulse timer. End the pulse if the time
mjr 78:1e00b3fa11af 3056 // has expired.
mjr 78:1e00b3fa11af 3057 if (shiftButton.pulseTime > dt)
mjr 78:1e00b3fa11af 3058 shiftButton.pulseTime -= dt;
mjr 78:1e00b3fa11af 3059 else
mjr 78:1e00b3fa11af 3060 shiftButton.state = 0;
mjr 78:1e00b3fa11af 3061 break;
mjr 78:1e00b3fa11af 3062 }
mjr 66:2e3583fbd2f4 3063 break;
mjr 66:2e3583fbd2f4 3064
mjr 66:2e3583fbd2f4 3065 case 1:
mjr 78:1e00b3fa11af 3066 // "Shift AND Key" mode. In this mode, the shift button acts
mjr 78:1e00b3fa11af 3067 // like any other button and sends its mapped key immediately.
mjr 78:1e00b3fa11af 3068 // The state cycle in this case simply matches the physical
mjr 78:1e00b3fa11af 3069 // state: ON -> cycle state 1, OFF -> cycle state 0.
mjr 78:1e00b3fa11af 3070 shiftButton.state = (sbs->physState ? 1 : 0);
mjr 66:2e3583fbd2f4 3071 break;
mjr 66:2e3583fbd2f4 3072 }
mjr 66:2e3583fbd2f4 3073 }
mjr 38:091e511ce8a0 3074
mjr 11:bd9da7088e6e 3075 // scan the button list
mjr 18:5e890ebd0023 3076 ButtonState *bs = buttonState;
mjr 65:739875521aae 3077 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 11:bd9da7088e6e 3078 {
mjr 77:0b96f6867312 3079 // get the config entry for the button
mjr 77:0b96f6867312 3080 ButtonCfg *bc = &cfg.button[bs->cfgIndex];
mjr 77:0b96f6867312 3081
mjr 66:2e3583fbd2f4 3082 // Check the button type:
mjr 66:2e3583fbd2f4 3083 // - shift button
mjr 66:2e3583fbd2f4 3084 // - pulsed button
mjr 66:2e3583fbd2f4 3085 // - regular button
mjr 66:2e3583fbd2f4 3086 if (shiftButton.index == i)
mjr 66:2e3583fbd2f4 3087 {
mjr 78:1e00b3fa11af 3088 // This is the shift button. The logical state handling
mjr 78:1e00b3fa11af 3089 // depends on the mode.
mjr 78:1e00b3fa11af 3090 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 3091 {
mjr 78:1e00b3fa11af 3092 case 0:
mjr 78:1e00b3fa11af 3093 default:
mjr 78:1e00b3fa11af 3094 // "Shift OR Key" mode. The logical state is ON only
mjr 78:1e00b3fa11af 3095 // during the timed pulse when the key is released, which
mjr 78:1e00b3fa11af 3096 // is signified by shift button state 3.
mjr 78:1e00b3fa11af 3097 bs->logState = (shiftButton.state == 3);
mjr 78:1e00b3fa11af 3098 break;
mjr 78:1e00b3fa11af 3099
mjr 78:1e00b3fa11af 3100 case 1:
mjr 78:1e00b3fa11af 3101 // "Shif AND Key" mode. The shift button acts like any
mjr 78:1e00b3fa11af 3102 // other button, so it's logically on when physically on.
mjr 78:1e00b3fa11af 3103 bs->logState = bs->physState;
mjr 78:1e00b3fa11af 3104 break;
mjr 66:2e3583fbd2f4 3105 }
mjr 66:2e3583fbd2f4 3106 }
mjr 66:2e3583fbd2f4 3107 else if (bs->pulseState != 0)
mjr 18:5e890ebd0023 3108 {
mjr 38:091e511ce8a0 3109 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 3110 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 3111 {
mjr 53:9b2611964afc 3112 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 3113 bs->pulseTime -= dt;
mjr 53:9b2611964afc 3114 }
mjr 53:9b2611964afc 3115 else
mjr 53:9b2611964afc 3116 {
mjr 53:9b2611964afc 3117 // pulse time expired - check for a state change
mjr 53:9b2611964afc 3118 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 3119 switch (bs->pulseState)
mjr 18:5e890ebd0023 3120 {
mjr 38:091e511ce8a0 3121 case 1:
mjr 38:091e511ce8a0 3122 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 3123 if (bs->physState)
mjr 53:9b2611964afc 3124 {
mjr 38:091e511ce8a0 3125 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 3126 bs->pulseState = 2;
mjr 53:9b2611964afc 3127 bs->logState = 1;
mjr 38:091e511ce8a0 3128 }
mjr 38:091e511ce8a0 3129 break;
mjr 18:5e890ebd0023 3130
mjr 38:091e511ce8a0 3131 case 2:
mjr 38:091e511ce8a0 3132 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 3133 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 3134 // change in state in the logical button
mjr 38:091e511ce8a0 3135 bs->pulseState = 3;
mjr 38:091e511ce8a0 3136 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 3137 bs->logState = 0;
mjr 38:091e511ce8a0 3138 break;
mjr 38:091e511ce8a0 3139
mjr 38:091e511ce8a0 3140 case 3:
mjr 38:091e511ce8a0 3141 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 3142 if (!bs->physState)
mjr 53:9b2611964afc 3143 {
mjr 38:091e511ce8a0 3144 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 3145 bs->pulseState = 4;
mjr 53:9b2611964afc 3146 bs->logState = 1;
mjr 38:091e511ce8a0 3147 }
mjr 38:091e511ce8a0 3148 break;
mjr 38:091e511ce8a0 3149
mjr 38:091e511ce8a0 3150 case 4:
mjr 38:091e511ce8a0 3151 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 3152 bs->pulseState = 1;
mjr 38:091e511ce8a0 3153 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 3154 bs->logState = 0;
mjr 38:091e511ce8a0 3155 break;
mjr 18:5e890ebd0023 3156 }
mjr 18:5e890ebd0023 3157 }
mjr 38:091e511ce8a0 3158 }
mjr 38:091e511ce8a0 3159 else
mjr 38:091e511ce8a0 3160 {
mjr 38:091e511ce8a0 3161 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 3162 bs->logState = bs->physState;
mjr 38:091e511ce8a0 3163 }
mjr 77:0b96f6867312 3164
mjr 77:0b96f6867312 3165 // Determine if we're going to use the shifted version of the
mjr 78:1e00b3fa11af 3166 // button. We're using the shifted version if...
mjr 78:1e00b3fa11af 3167 //
mjr 78:1e00b3fa11af 3168 // - the shift button is down, AND
mjr 78:1e00b3fa11af 3169 // - this button isn't itself the shift button, AND
mjr 78:1e00b3fa11af 3170 // - this button has some kind of shifted meaning
mjr 77:0b96f6867312 3171 //
mjr 78:1e00b3fa11af 3172 // A "shifted meaning" means that we have any of the following
mjr 78:1e00b3fa11af 3173 // assigned to the shifted version of the button: a key assignment,
mjr 78:1e00b3fa11af 3174 // (in typ2,key2), an IR command (in IRCommand2), or Night mode.
mjr 78:1e00b3fa11af 3175 //
mjr 78:1e00b3fa11af 3176 // The test for Night Mode is a bit tricky. The shifted version of
mjr 78:1e00b3fa11af 3177 // the button is the Night Mode toggle if the button matches the
mjr 78:1e00b3fa11af 3178 // Night Mode button index, AND its flags are set with "toggle mode
mjr 78:1e00b3fa11af 3179 // ON" (bit 0x02 is on) and "switch mode OFF" (bit 0x01 is off).
mjr 78:1e00b3fa11af 3180 // So (button flags) & 0x03 must equal 0x02.
mjr 77:0b96f6867312 3181 bool useShift =
mjr 77:0b96f6867312 3182 (shiftButton.state != 0
mjr 78:1e00b3fa11af 3183 && shiftButton.index != i
mjr 77:0b96f6867312 3184 && (bc->typ2 != BtnTypeNone
mjr 77:0b96f6867312 3185 || bc->IRCommand2 != 0
mjr 77:0b96f6867312 3186 || (cfg.nightMode.btn == i+1 && (cfg.nightMode.flags & 0x03) == 0x02)));
mjr 77:0b96f6867312 3187
mjr 77:0b96f6867312 3188 // If we're using the shift function, and no other button has used
mjr 77:0b96f6867312 3189 // the shift function yet (shift state 1: "shift button is down but
mjr 77:0b96f6867312 3190 // no one has used the shift function yet"), then we've "consumed"
mjr 77:0b96f6867312 3191 // the shift button press (so go to shift state 2: "shift button has
mjr 77:0b96f6867312 3192 // been used by some other button press that has a shifted meaning").
mjr 78:1e00b3fa11af 3193 if (useShift && shiftButton.state == 1 && bs->logState)
mjr 77:0b96f6867312 3194 shiftButton.state = 2;
mjr 35:e959ffba78fd 3195
mjr 38:091e511ce8a0 3196 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 3197 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 3198 {
mjr 77:0b96f6867312 3199 // check to see if this is the Night Mode button
mjr 53:9b2611964afc 3200 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 3201 {
mjr 77:0b96f6867312 3202 // Check the switch type in the config flags. If flag 0x01 is
mjr 77:0b96f6867312 3203 // set, it's a persistent on/off switch, so the night mode
mjr 77:0b96f6867312 3204 // state simply tracks the current state of the switch.
mjr 77:0b96f6867312 3205 // Otherwise, it's a momentary button, so each button push
mjr 77:0b96f6867312 3206 // (i.e., each transition from logical state OFF to ON) toggles
mjr 77:0b96f6867312 3207 // the night mode state.
mjr 77:0b96f6867312 3208 //
mjr 77:0b96f6867312 3209 // Note that the "shift" flag (0x02) has no effect in switch
mjr 77:0b96f6867312 3210 // mode. Shifting only works for toggle mode.
mjr 82:4f6209cb5c33 3211 if ((cfg.nightMode.flags & 0x01) != 0)
mjr 53:9b2611964afc 3212 {
mjr 77:0b96f6867312 3213 // It's an on/off switch. Night mode simply tracks the
mjr 77:0b96f6867312 3214 // current switch state.
mjr 53:9b2611964afc 3215 setNightMode(bs->logState);
mjr 53:9b2611964afc 3216 }
mjr 82:4f6209cb5c33 3217 else if (bs->logState)
mjr 53:9b2611964afc 3218 {
mjr 77:0b96f6867312 3219 // It's a momentary toggle switch. Toggle the night mode
mjr 77:0b96f6867312 3220 // state on each distinct press of the button: that is,
mjr 77:0b96f6867312 3221 // whenever the button's logical state transitions from
mjr 77:0b96f6867312 3222 // OFF to ON.
mjr 66:2e3583fbd2f4 3223 //
mjr 77:0b96f6867312 3224 // The "shift" flag (0x02) tells us whether night mode is
mjr 77:0b96f6867312 3225 // assigned to the shifted or unshifted version of the
mjr 77:0b96f6867312 3226 // button.
mjr 77:0b96f6867312 3227 bool pressed;
mjr 66:2e3583fbd2f4 3228 if ((cfg.nightMode.flags & 0x02) != 0)
mjr 66:2e3583fbd2f4 3229 {
mjr 77:0b96f6867312 3230 // Shift bit is set - night mode is assigned to the
mjr 77:0b96f6867312 3231 // shifted version of the button. This is a Night
mjr 77:0b96f6867312 3232 // Mode toggle only if the Shift button is pressed.
mjr 77:0b96f6867312 3233 pressed = (shiftButton.state != 0);
mjr 77:0b96f6867312 3234 }
mjr 77:0b96f6867312 3235 else
mjr 77:0b96f6867312 3236 {
mjr 77:0b96f6867312 3237 // No shift bit - night mode is assigned to the
mjr 77:0b96f6867312 3238 // regular unshifted button. The button press only
mjr 77:0b96f6867312 3239 // applies if the Shift button is NOT pressed.
mjr 77:0b96f6867312 3240 pressed = (shiftButton.state == 0);
mjr 66:2e3583fbd2f4 3241 }
mjr 66:2e3583fbd2f4 3242
mjr 66:2e3583fbd2f4 3243 // if it's pressed (even after considering the shift mode),
mjr 66:2e3583fbd2f4 3244 // toggle night mode
mjr 66:2e3583fbd2f4 3245 if (pressed)
mjr 53:9b2611964afc 3246 toggleNightMode();
mjr 53:9b2611964afc 3247 }
mjr 35:e959ffba78fd 3248 }
mjr 38:091e511ce8a0 3249
mjr 77:0b96f6867312 3250 // press or release IR virtual keys on key state changes
mjr 77:0b96f6867312 3251 uint8_t irc = useShift ? bc->IRCommand2 : bc->IRCommand;
mjr 77:0b96f6867312 3252 if (irc != 0)
mjr 77:0b96f6867312 3253 IR_buttonChange(irc, bs->logState);
mjr 77:0b96f6867312 3254
mjr 38:091e511ce8a0 3255 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 3256 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 3257 }
mjr 38:091e511ce8a0 3258
mjr 53:9b2611964afc 3259 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 3260 // key state list
mjr 53:9b2611964afc 3261 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 3262 {
mjr 70:9f58735a1732 3263 // Get the key type and code. Start by assuming that we're
mjr 70:9f58735a1732 3264 // going to use the normal unshifted meaning.
mjr 77:0b96f6867312 3265 uint8_t typ, val;
mjr 77:0b96f6867312 3266 if (useShift)
mjr 66:2e3583fbd2f4 3267 {
mjr 77:0b96f6867312 3268 typ = bc->typ2;
mjr 77:0b96f6867312 3269 val = bc->val2;
mjr 66:2e3583fbd2f4 3270 }
mjr 77:0b96f6867312 3271 else
mjr 77:0b96f6867312 3272 {
mjr 77:0b96f6867312 3273 typ = bc->typ;
mjr 77:0b96f6867312 3274 val = bc->val;
mjr 77:0b96f6867312 3275 }
mjr 77:0b96f6867312 3276
mjr 70:9f58735a1732 3277 // We've decided on the meaning of the button, so process
mjr 70:9f58735a1732 3278 // the keyboard or joystick event.
mjr 77:0b96f6867312 3279 ks.addKey(typ, val);
mjr 18:5e890ebd0023 3280 }
mjr 11:bd9da7088e6e 3281 }
mjr 77:0b96f6867312 3282
mjr 77:0b96f6867312 3283 // If an IR input command is in effect, add the IR command's
mjr 77:0b96f6867312 3284 // assigned key, if any. If we're in an IR key gap, don't include
mjr 77:0b96f6867312 3285 // the IR key.
mjr 77:0b96f6867312 3286 if (IRCommandIn != 0 && !IRKeyGap)
mjr 77:0b96f6867312 3287 {
mjr 77:0b96f6867312 3288 IRCommandCfg &irc = cfg.IRCommand[IRCommandIn - 1];
mjr 77:0b96f6867312 3289 ks.addKey(irc.keytype, irc.keycode);
mjr 77:0b96f6867312 3290 }
mjr 77:0b96f6867312 3291
mjr 77:0b96f6867312 3292 // We're finished building the new key state. Update the global
mjr 77:0b96f6867312 3293 // key state variables to reflect the new state.
mjr 77:0b96f6867312 3294
mjr 77:0b96f6867312 3295 // set the new joystick buttons (no need to check for changes, as we
mjr 77:0b96f6867312 3296 // report these on every joystick report whether they changed or not)
mjr 77:0b96f6867312 3297 jsButtons = ks.js;
mjr 77:0b96f6867312 3298
mjr 77:0b96f6867312 3299 // check for keyboard key changes (we only send keyboard reports when
mjr 77:0b96f6867312 3300 // something changes)
mjr 77:0b96f6867312 3301 if (kbState.data[0] != ks.modkeys
mjr 77:0b96f6867312 3302 || kbState.nkeys != ks.nkeys
mjr 77:0b96f6867312 3303 || memcmp(ks.keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 3304 {
mjr 35:e959ffba78fd 3305 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3306 kbState.changed = true;
mjr 77:0b96f6867312 3307 kbState.data[0] = ks.modkeys;
mjr 77:0b96f6867312 3308 if (ks.nkeys <= 6) {
mjr 35:e959ffba78fd 3309 // 6 or fewer simultaneous keys - report the key codes
mjr 77:0b96f6867312 3310 kbState.nkeys = ks.nkeys;
mjr 77:0b96f6867312 3311 memcpy(&kbState.data[2], ks.keys, 6);
mjr 35:e959ffba78fd 3312 }
mjr 35:e959ffba78fd 3313 else {
mjr 35:e959ffba78fd 3314 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 3315 kbState.nkeys = 6;
mjr 35:e959ffba78fd 3316 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 3317 }
mjr 35:e959ffba78fd 3318 }
mjr 35:e959ffba78fd 3319
mjr 77:0b96f6867312 3320 // check for media key changes (we only send media key reports when
mjr 77:0b96f6867312 3321 // something changes)
mjr 77:0b96f6867312 3322 if (mediaState.data != ks.mediakeys)
mjr 35:e959ffba78fd 3323 {
mjr 77:0b96f6867312 3324 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3325 mediaState.changed = true;
mjr 77:0b96f6867312 3326 mediaState.data = ks.mediakeys;
mjr 35:e959ffba78fd 3327 }
mjr 11:bd9da7088e6e 3328 }
mjr 11:bd9da7088e6e 3329
mjr 73:4e8ce0b18915 3330 // Send a button status report
mjr 73:4e8ce0b18915 3331 void reportButtonStatus(USBJoystick &js)
mjr 73:4e8ce0b18915 3332 {
mjr 73:4e8ce0b18915 3333 // start with all buttons off
mjr 73:4e8ce0b18915 3334 uint8_t state[(MAX_BUTTONS+7)/8];
mjr 73:4e8ce0b18915 3335 memset(state, 0, sizeof(state));
mjr 73:4e8ce0b18915 3336
mjr 73:4e8ce0b18915 3337 // pack the button states into bytes, one bit per button
mjr 73:4e8ce0b18915 3338 ButtonState *bs = buttonState;
mjr 73:4e8ce0b18915 3339 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 73:4e8ce0b18915 3340 {
mjr 73:4e8ce0b18915 3341 // get the physical state
mjr 73:4e8ce0b18915 3342 int b = bs->physState;
mjr 73:4e8ce0b18915 3343
mjr 73:4e8ce0b18915 3344 // pack it into the appropriate bit
mjr 73:4e8ce0b18915 3345 int idx = bs->cfgIndex;
mjr 73:4e8ce0b18915 3346 int si = idx / 8;
mjr 73:4e8ce0b18915 3347 int shift = idx & 0x07;
mjr 73:4e8ce0b18915 3348 state[si] |= b << shift;
mjr 73:4e8ce0b18915 3349 }
mjr 73:4e8ce0b18915 3350
mjr 73:4e8ce0b18915 3351 // send the report
mjr 73:4e8ce0b18915 3352 js.reportButtonStatus(MAX_BUTTONS, state);
mjr 73:4e8ce0b18915 3353 }
mjr 73:4e8ce0b18915 3354
mjr 5:a70c0bce770d 3355 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3356 //
mjr 5:a70c0bce770d 3357 // Customization joystick subbclass
mjr 5:a70c0bce770d 3358 //
mjr 5:a70c0bce770d 3359
mjr 5:a70c0bce770d 3360 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 3361 {
mjr 5:a70c0bce770d 3362 public:
mjr 35:e959ffba78fd 3363 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 35:e959ffba78fd 3364 bool waitForConnect, bool enableJoystick, bool useKB)
mjr 35:e959ffba78fd 3365 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, useKB)
mjr 5:a70c0bce770d 3366 {
mjr 54:fd77a6b2f76c 3367 sleeping_ = false;
mjr 54:fd77a6b2f76c 3368 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3369 timer_.start();
mjr 54:fd77a6b2f76c 3370 }
mjr 54:fd77a6b2f76c 3371
mjr 54:fd77a6b2f76c 3372 // show diagnostic LED feedback for connect state
mjr 54:fd77a6b2f76c 3373 void diagFlash()
mjr 54:fd77a6b2f76c 3374 {
mjr 54:fd77a6b2f76c 3375 if (!configured() || sleeping_)
mjr 54:fd77a6b2f76c 3376 {
mjr 54:fd77a6b2f76c 3377 // flash once if sleeping or twice if disconnected
mjr 54:fd77a6b2f76c 3378 for (int j = isConnected() ? 1 : 2 ; j > 0 ; --j)
mjr 54:fd77a6b2f76c 3379 {
mjr 54:fd77a6b2f76c 3380 // short red flash
mjr 54:fd77a6b2f76c 3381 diagLED(1, 0, 0);
mjr 54:fd77a6b2f76c 3382 wait_us(50000);
mjr 54:fd77a6b2f76c 3383 diagLED(0, 0, 0);
mjr 54:fd77a6b2f76c 3384 wait_us(50000);
mjr 54:fd77a6b2f76c 3385 }
mjr 54:fd77a6b2f76c 3386 }
mjr 5:a70c0bce770d 3387 }
mjr 5:a70c0bce770d 3388
mjr 5:a70c0bce770d 3389 // are we connected?
mjr 5:a70c0bce770d 3390 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 3391
mjr 54:fd77a6b2f76c 3392 // Are we in sleep mode? If true, this means that the hardware has
mjr 54:fd77a6b2f76c 3393 // detected no activity on the bus for 3ms. This happens when the
mjr 54:fd77a6b2f76c 3394 // cable is physically disconnected, the computer is turned off, or
mjr 54:fd77a6b2f76c 3395 // the connection is otherwise disabled.
mjr 54:fd77a6b2f76c 3396 bool isSleeping() const { return sleeping_; }
mjr 54:fd77a6b2f76c 3397
mjr 54:fd77a6b2f76c 3398 // If necessary, attempt to recover from a broken connection.
mjr 54:fd77a6b2f76c 3399 //
mjr 54:fd77a6b2f76c 3400 // This is a hack, to work around an apparent timing bug in the
mjr 54:fd77a6b2f76c 3401 // KL25Z USB implementation that I haven't been able to solve any
mjr 54:fd77a6b2f76c 3402 // other way.
mjr 54:fd77a6b2f76c 3403 //
mjr 54:fd77a6b2f76c 3404 // The issue: when we have an established connection, and the
mjr 54:fd77a6b2f76c 3405 // connection is broken by physically unplugging the cable or by
mjr 54:fd77a6b2f76c 3406 // rebooting the PC, the KL25Z sometimes fails to reconnect when
mjr 54:fd77a6b2f76c 3407 // the physical connection is re-established. The failure is
mjr 54:fd77a6b2f76c 3408 // sporadic; I'd guess it happens about 25% of the time, but I
mjr 54:fd77a6b2f76c 3409 // haven't collected any real statistics on it.
mjr 54:fd77a6b2f76c 3410 //
mjr 54:fd77a6b2f76c 3411 // The proximate cause of the failure is a deadlock in the SETUP
mjr 54:fd77a6b2f76c 3412 // protocol between the host and device that happens around the
mjr 54:fd77a6b2f76c 3413 // point where the PC is requesting the configuration descriptor.
mjr 54:fd77a6b2f76c 3414 // The exact point in the protocol where this occurs varies slightly;
mjr 54:fd77a6b2f76c 3415 // it can occur a message or two before or after the Get Config
mjr 54:fd77a6b2f76c 3416 // Descriptor packet. No matter where it happens, the nature of
mjr 54:fd77a6b2f76c 3417 // the deadlock is the same: the PC thinks it sees a STALL on EP0
mjr 54:fd77a6b2f76c 3418 // from the device, so it terminates the connection attempt, which
mjr 54:fd77a6b2f76c 3419 // stops further traffic on the cable. The KL25Z USB hardware sees
mjr 54:fd77a6b2f76c 3420 // the lack of traffic and triggers a SLEEP interrupt (a misnomer
mjr 54:fd77a6b2f76c 3421 // for what should have been called a BROKEN CONNECTION interrupt).
mjr 54:fd77a6b2f76c 3422 // Both sides simply stop talking at this point, so the connection
mjr 54:fd77a6b2f76c 3423 // is effectively dead.
mjr 54:fd77a6b2f76c 3424 //
mjr 54:fd77a6b2f76c 3425 // The strange thing is that, as far as I can tell, the KL25Z isn't
mjr 54:fd77a6b2f76c 3426 // doing anything to trigger the STALL on its end. Both the PC
mjr 54:fd77a6b2f76c 3427 // and the KL25Z are happy up until the very point of the failure
mjr 54:fd77a6b2f76c 3428 // and show no signs of anything wrong in the protocol exchange.
mjr 54:fd77a6b2f76c 3429 // In fact, every detail of the protocol exchange up to this point
mjr 54:fd77a6b2f76c 3430 // is identical to every successful exchange that does finish the
mjr 54:fd77a6b2f76c 3431 // whole setup process successfully, on both the KL25Z and Windows
mjr 54:fd77a6b2f76c 3432 // sides of the connection. I can't find any point of difference
mjr 54:fd77a6b2f76c 3433 // between successful and unsuccessful sequences that suggests why
mjr 54:fd77a6b2f76c 3434 // the fateful message fails. This makes me suspect that whatever
mjr 54:fd77a6b2f76c 3435 // is going wrong is inside the KL25Z USB hardware module, which
mjr 54:fd77a6b2f76c 3436 // is a pretty substantial black box - it has a lot of internal
mjr 54:fd77a6b2f76c 3437 // state that's inaccessible to the software. Further bolstering
mjr 54:fd77a6b2f76c 3438 // this theory is a little experiment where I found that I could
mjr 54:fd77a6b2f76c 3439 // reproduce the exact sequence of events of a failed reconnect
mjr 54:fd77a6b2f76c 3440 // attempt in an *initial* connection, which is otherwise 100%
mjr 54:fd77a6b2f76c 3441 // reliable, by inserting a little bit of artifical time padding
mjr 54:fd77a6b2f76c 3442 // (200us per event) into the SETUP interrupt handler. My
mjr 54:fd77a6b2f76c 3443 // hypothesis is that the STALL event happens because the KL25Z
mjr 54:fd77a6b2f76c 3444 // USB hardware is too slow to respond to a message. I'm not
mjr 54:fd77a6b2f76c 3445 // sure why this would only happen after a disconnect and not
mjr 54:fd77a6b2f76c 3446 // during the initial connection; maybe there's some reset work
mjr 54:fd77a6b2f76c 3447 // in the hardware that takes a substantial amount of time after
mjr 54:fd77a6b2f76c 3448 // a disconnect.
mjr 54:fd77a6b2f76c 3449 //
mjr 54:fd77a6b2f76c 3450 // The solution: the problem happens during the SETUP exchange,
mjr 54:fd77a6b2f76c 3451 // after we've been assigned a bus address. It only happens on
mjr 54:fd77a6b2f76c 3452 // some percentage of connection requests, so if we can simply
mjr 54:fd77a6b2f76c 3453 // start over when the failure occurs, we'll eventually succeed
mjr 54:fd77a6b2f76c 3454 // simply because not every attempt fails. The ideal would be
mjr 54:fd77a6b2f76c 3455 // to get the success rate up to 100%, but I can't figure out how
mjr 54:fd77a6b2f76c 3456 // to fix the underlying problem, so this is the next best thing.
mjr 54:fd77a6b2f76c 3457 //
mjr 54:fd77a6b2f76c 3458 // We can detect when the failure occurs by noticing when a SLEEP
mjr 54:fd77a6b2f76c 3459 // interrupt happens while we have an assigned bus address.
mjr 54:fd77a6b2f76c 3460 //
mjr 54:fd77a6b2f76c 3461 // To start a new connection attempt, we have to make the *host*
mjr 54:fd77a6b2f76c 3462 // try again. The logical connection is initiated solely by the
mjr 54:fd77a6b2f76c 3463 // host. Fortunately, it's easy to get the host to initiate the
mjr 54:fd77a6b2f76c 3464 // process: if we disconnect on the device side, it effectively
mjr 54:fd77a6b2f76c 3465 // makes the device look to the PC like it's electrically unplugged.
mjr 54:fd77a6b2f76c 3466 // When we reconnect on the device side, the PC thinks a new device
mjr 54:fd77a6b2f76c 3467 // has been plugged in and initiates the logical connection setup.
mjr 74:822a92bc11d2 3468 // We have to remain disconnected for some minimum interval before
mjr 74:822a92bc11d2 3469 // the host notices; the exact minimum is unclear, but 5ms seems
mjr 74:822a92bc11d2 3470 // reliable in practice.
mjr 54:fd77a6b2f76c 3471 //
mjr 54:fd77a6b2f76c 3472 // Here's the full algorithm:
mjr 54:fd77a6b2f76c 3473 //
mjr 54:fd77a6b2f76c 3474 // 1. In the SLEEP interrupt handler, if we have a bus address,
mjr 54:fd77a6b2f76c 3475 // we disconnect the device. This happens in ISR context, so we
mjr 54:fd77a6b2f76c 3476 // can't wait around for 5ms. Instead, we simply set a flag noting
mjr 54:fd77a6b2f76c 3477 // that the connection has been broken, and we note the time and
mjr 54:fd77a6b2f76c 3478 // return.
mjr 54:fd77a6b2f76c 3479 //
mjr 54:fd77a6b2f76c 3480 // 2. In our main loop, whenever we find that we're disconnected,
mjr 54:fd77a6b2f76c 3481 // we call recoverConnection(). The main loop's job is basically a
mjr 54:fd77a6b2f76c 3482 // bunch of device polling. We're just one more device to poll, so
mjr 54:fd77a6b2f76c 3483 // recoverConnection() will be called soon after a disconnect, and
mjr 54:fd77a6b2f76c 3484 // then will be called in a loop for as long as we're disconnected.
mjr 54:fd77a6b2f76c 3485 //
mjr 54:fd77a6b2f76c 3486 // 3. In recoverConnection(), we check the flag we set in the SLEEP
mjr 54:fd77a6b2f76c 3487 // handler. If set, we wait until 5ms has elapsed from the SLEEP
mjr 54:fd77a6b2f76c 3488 // event time that we noted, then we'll reconnect and clear the flag.
mjr 54:fd77a6b2f76c 3489 // This gives us the required 5ms (or longer) delay between the
mjr 54:fd77a6b2f76c 3490 // disconnect and reconnect, ensuring that the PC will notice and
mjr 54:fd77a6b2f76c 3491 // will start over with the connection protocol.
mjr 54:fd77a6b2f76c 3492 //
mjr 54:fd77a6b2f76c 3493 // 4. The main loop keeps calling recoverConnection() in a loop for
mjr 54:fd77a6b2f76c 3494 // as long as we're disconnected, so if the new connection attempt
mjr 54:fd77a6b2f76c 3495 // triggered in step 3 fails, the SLEEP interrupt will happen again,
mjr 54:fd77a6b2f76c 3496 // we'll disconnect again, the flag will get set again, and
mjr 54:fd77a6b2f76c 3497 // recoverConnection() will reconnect again after another suitable
mjr 54:fd77a6b2f76c 3498 // delay. This will repeat until the connection succeeds or hell
mjr 54:fd77a6b2f76c 3499 // freezes over.
mjr 54:fd77a6b2f76c 3500 //
mjr 54:fd77a6b2f76c 3501 // Each disconnect happens immediately when a reconnect attempt
mjr 54:fd77a6b2f76c 3502 // fails, and an entire successful connection only takes about 25ms,
mjr 54:fd77a6b2f76c 3503 // so our loop can retry at more than 30 attempts per second.
mjr 54:fd77a6b2f76c 3504 // In my testing, lost connections almost always reconnect in
mjr 54:fd77a6b2f76c 3505 // less than second with this code in place.
mjr 54:fd77a6b2f76c 3506 void recoverConnection()
mjr 54:fd77a6b2f76c 3507 {
mjr 54:fd77a6b2f76c 3508 // if a reconnect is pending, reconnect
mjr 54:fd77a6b2f76c 3509 if (reconnectPending_)
mjr 54:fd77a6b2f76c 3510 {
mjr 54:fd77a6b2f76c 3511 // Loop until we reach 5ms after the last sleep event.
mjr 54:fd77a6b2f76c 3512 for (bool done = false ; !done ; )
mjr 54:fd77a6b2f76c 3513 {
mjr 54:fd77a6b2f76c 3514 // If we've reached the target time, reconnect. Do the
mjr 54:fd77a6b2f76c 3515 // time check and flag reset atomically, so that we can't
mjr 54:fd77a6b2f76c 3516 // have another sleep event sneak in after we've verified
mjr 54:fd77a6b2f76c 3517 // the time. If another event occurs, it has to happen
mjr 54:fd77a6b2f76c 3518 // before we check, in which case it'll update the time
mjr 54:fd77a6b2f76c 3519 // before we check it, or after we clear the flag, in
mjr 54:fd77a6b2f76c 3520 // which case it will reset the flag and we'll do another
mjr 54:fd77a6b2f76c 3521 // round the next time we call this routine.
mjr 54:fd77a6b2f76c 3522 __disable_irq();
mjr 54:fd77a6b2f76c 3523 if (uint32_t(timer_.read_us() - lastSleepTime_) > 5000)
mjr 54:fd77a6b2f76c 3524 {
mjr 54:fd77a6b2f76c 3525 connect(false);
mjr 54:fd77a6b2f76c 3526 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3527 done = true;
mjr 54:fd77a6b2f76c 3528 }
mjr 54:fd77a6b2f76c 3529 __enable_irq();
mjr 54:fd77a6b2f76c 3530 }
mjr 54:fd77a6b2f76c 3531 }
mjr 54:fd77a6b2f76c 3532 }
mjr 5:a70c0bce770d 3533
mjr 5:a70c0bce770d 3534 protected:
mjr 54:fd77a6b2f76c 3535 // Handle a USB SLEEP interrupt. This interrupt signifies that the
mjr 54:fd77a6b2f76c 3536 // USB hardware module hasn't seen any token traffic for 3ms, which
mjr 54:fd77a6b2f76c 3537 // means that we're either physically or logically disconnected.
mjr 54:fd77a6b2f76c 3538 //
mjr 54:fd77a6b2f76c 3539 // Important: this runs in ISR context.
mjr 54:fd77a6b2f76c 3540 //
mjr 54:fd77a6b2f76c 3541 // Note that this is a specialized sense of "sleep" that's unrelated
mjr 54:fd77a6b2f76c 3542 // to the similarly named power modes on the PC. This has nothing
mjr 54:fd77a6b2f76c 3543 // to do with suspend/sleep mode on the PC, and it's not a low-power
mjr 54:fd77a6b2f76c 3544 // mode on the KL25Z. They really should have called this interrupt
mjr 54:fd77a6b2f76c 3545 // DISCONNECT or BROKEN CONNECTION.)
mjr 54:fd77a6b2f76c 3546 virtual void sleepStateChanged(unsigned int sleeping)
mjr 54:fd77a6b2f76c 3547 {
mjr 54:fd77a6b2f76c 3548 // note the new state
mjr 54:fd77a6b2f76c 3549 sleeping_ = sleeping;
mjr 54:fd77a6b2f76c 3550
mjr 54:fd77a6b2f76c 3551 // If we have a non-zero bus address, we have at least a partial
mjr 54:fd77a6b2f76c 3552 // connection to the host (we've made it at least as far as the
mjr 54:fd77a6b2f76c 3553 // SETUP stage). Explicitly disconnect, and the pending reconnect
mjr 54:fd77a6b2f76c 3554 // flag, and remember the time of the sleep event.
mjr 54:fd77a6b2f76c 3555 if (USB0->ADDR != 0x00)
mjr 54:fd77a6b2f76c 3556 {
mjr 54:fd77a6b2f76c 3557 disconnect();
mjr 54:fd77a6b2f76c 3558 lastSleepTime_ = timer_.read_us();
mjr 54:fd77a6b2f76c 3559 reconnectPending_ = true;
mjr 54:fd77a6b2f76c 3560 }
mjr 54:fd77a6b2f76c 3561 }
mjr 54:fd77a6b2f76c 3562
mjr 54:fd77a6b2f76c 3563 // is the USB connection asleep?
mjr 54:fd77a6b2f76c 3564 volatile bool sleeping_;
mjr 54:fd77a6b2f76c 3565
mjr 54:fd77a6b2f76c 3566 // flag: reconnect pending after sleep event
mjr 54:fd77a6b2f76c 3567 volatile bool reconnectPending_;
mjr 54:fd77a6b2f76c 3568
mjr 54:fd77a6b2f76c 3569 // time of last sleep event while connected
mjr 54:fd77a6b2f76c 3570 volatile uint32_t lastSleepTime_;
mjr 54:fd77a6b2f76c 3571
mjr 54:fd77a6b2f76c 3572 // timer to keep track of interval since last sleep event
mjr 54:fd77a6b2f76c 3573 Timer timer_;
mjr 5:a70c0bce770d 3574 };
mjr 5:a70c0bce770d 3575
mjr 5:a70c0bce770d 3576 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3577 //
mjr 5:a70c0bce770d 3578 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 3579 //
mjr 5:a70c0bce770d 3580
mjr 5:a70c0bce770d 3581 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 3582 //
mjr 5:a70c0bce770d 3583 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 3584 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 3585 // automatic calibration.
mjr 5:a70c0bce770d 3586 //
mjr 77:0b96f6867312 3587 // We collect data at the device's maximum rate of 800kHz (one sample
mjr 77:0b96f6867312 3588 // every 1.25ms). To keep up with the high data rate, we use the
mjr 77:0b96f6867312 3589 // device's internal FIFO, and drain the FIFO by polling on each
mjr 77:0b96f6867312 3590 // iteration of our main application loop. In the past, we used an
mjr 77:0b96f6867312 3591 // interrupt handler to read the device immediately on the arrival of
mjr 77:0b96f6867312 3592 // each sample, but this created too much latency for the IR remote
mjr 77:0b96f6867312 3593 // receiver, due to the relatively long time it takes to transfer the
mjr 77:0b96f6867312 3594 // accelerometer readings via I2C. The device's on-board FIFO can
mjr 77:0b96f6867312 3595 // store up to 32 samples, which gives us up to about 40ms between
mjr 77:0b96f6867312 3596 // polling iterations before the buffer overflows. Our main loop runs
mjr 77:0b96f6867312 3597 // in under 2ms, so we can easily keep the FIFO far from overflowing.
mjr 77:0b96f6867312 3598 //
mjr 77:0b96f6867312 3599 // The MMA8451Q has three range modes, +/- 2G, 4G, and 8G. The ADC
mjr 77:0b96f6867312 3600 // sample is the same bit width (14 bits) in all modes, so the higher
mjr 77:0b96f6867312 3601 // dynamic range modes trade physical precision for range. For our
mjr 77:0b96f6867312 3602 // purposes, precision is more important than range, so we use the
mjr 77:0b96f6867312 3603 // +/-2G mode. Further, our joystick range is calibrated for only
mjr 77:0b96f6867312 3604 // +/-1G. This was unintentional on my part; I didn't look at the
mjr 77:0b96f6867312 3605 // MMA8451Q library closely enough to realize it was normalizing to
mjr 77:0b96f6867312 3606 // actual "G" units, and assumed that it was normalizing to a -1..+1
mjr 77:0b96f6867312 3607 // scale. In practice, a +/-1G scale seems perfectly adequate for
mjr 77:0b96f6867312 3608 // virtual pinball use, so I'm sticking with that range for now. But
mjr 77:0b96f6867312 3609 // there might be some benefit in renormalizing to a +/-2G range, in
mjr 77:0b96f6867312 3610 // that it would allow for higher dynamic range for very hard nudges.
mjr 77:0b96f6867312 3611 // Everyone would have to tweak their nudge sensitivity in VP if I
mjr 77:0b96f6867312 3612 // made that change, though, so I'm keeping it as is for now; it would
mjr 77:0b96f6867312 3613 // be best to make it a config option ("accelerometer high dynamic range")
mjr 77:0b96f6867312 3614 // rather than change it across the board.
mjr 5:a70c0bce770d 3615 //
mjr 6:cc35eb643e8f 3616 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 3617 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 3618 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 3619 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 3620 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 3621 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 3622 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 3623 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 3624 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 3625 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 3626 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 3627 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 3628 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 3629 // of nudging, say).
mjr 5:a70c0bce770d 3630 //
mjr 5:a70c0bce770d 3631
mjr 17:ab3cec0c8bf4 3632 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 3633 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 3634
mjr 17:ab3cec0c8bf4 3635 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 3636 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 3637 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 3638
mjr 17:ab3cec0c8bf4 3639 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 3640 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 3641 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 3642 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 3643
mjr 17:ab3cec0c8bf4 3644
mjr 6:cc35eb643e8f 3645 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 3646 struct AccHist
mjr 5:a70c0bce770d 3647 {
mjr 77:0b96f6867312 3648 AccHist() { x = y = dsq = 0; xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 3649 void set(int x, int y, AccHist *prv)
mjr 6:cc35eb643e8f 3650 {
mjr 6:cc35eb643e8f 3651 // save the raw position
mjr 6:cc35eb643e8f 3652 this->x = x;
mjr 6:cc35eb643e8f 3653 this->y = y;
mjr 77:0b96f6867312 3654 this->dsq = distanceSquared(prv);
mjr 6:cc35eb643e8f 3655 }
mjr 6:cc35eb643e8f 3656
mjr 6:cc35eb643e8f 3657 // reading for this entry
mjr 77:0b96f6867312 3658 int x, y;
mjr 77:0b96f6867312 3659
mjr 77:0b96f6867312 3660 // (distance from previous entry) squared
mjr 77:0b96f6867312 3661 int dsq;
mjr 5:a70c0bce770d 3662
mjr 6:cc35eb643e8f 3663 // total and count of samples averaged over this period
mjr 77:0b96f6867312 3664 int xtot, ytot;
mjr 6:cc35eb643e8f 3665 int cnt;
mjr 6:cc35eb643e8f 3666
mjr 77:0b96f6867312 3667 void clearAvg() { xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 3668 void addAvg(int x, int y) { xtot += x; ytot += y; ++cnt; }
mjr 77:0b96f6867312 3669 int xAvg() const { return xtot/cnt; }
mjr 77:0b96f6867312 3670 int yAvg() const { return ytot/cnt; }
mjr 77:0b96f6867312 3671
mjr 77:0b96f6867312 3672 int distanceSquared(AccHist *p)
mjr 77:0b96f6867312 3673 { return square(p->x - x) + square(p->y - y); }
mjr 5:a70c0bce770d 3674 };
mjr 5:a70c0bce770d 3675
mjr 5:a70c0bce770d 3676 // accelerometer wrapper class
mjr 3:3514575d4f86 3677 class Accel
mjr 3:3514575d4f86 3678 {
mjr 3:3514575d4f86 3679 public:
mjr 78:1e00b3fa11af 3680 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin,
mjr 78:1e00b3fa11af 3681 int range, int autoCenterMode)
mjr 77:0b96f6867312 3682 : mma_(sda, scl, i2cAddr)
mjr 3:3514575d4f86 3683 {
mjr 5:a70c0bce770d 3684 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 3685 irqPin_ = irqPin;
mjr 77:0b96f6867312 3686
mjr 77:0b96f6867312 3687 // remember the range
mjr 77:0b96f6867312 3688 range_ = range;
mjr 78:1e00b3fa11af 3689
mjr 78:1e00b3fa11af 3690 // set the auto-centering mode
mjr 78:1e00b3fa11af 3691 setAutoCenterMode(autoCenterMode);
mjr 78:1e00b3fa11af 3692
mjr 78:1e00b3fa11af 3693 // no manual centering request has been received
mjr 78:1e00b3fa11af 3694 manualCenterRequest_ = false;
mjr 5:a70c0bce770d 3695
mjr 5:a70c0bce770d 3696 // reset and initialize
mjr 5:a70c0bce770d 3697 reset();
mjr 5:a70c0bce770d 3698 }
mjr 5:a70c0bce770d 3699
mjr 78:1e00b3fa11af 3700 // Request manual centering. This applies the trailing average
mjr 78:1e00b3fa11af 3701 // of recent measurements and applies it as the new center point
mjr 78:1e00b3fa11af 3702 // as soon as we have enough data.
mjr 78:1e00b3fa11af 3703 void manualCenterRequest() { manualCenterRequest_ = true; }
mjr 78:1e00b3fa11af 3704
mjr 78:1e00b3fa11af 3705 // set the auto-centering mode
mjr 78:1e00b3fa11af 3706 void setAutoCenterMode(int mode)
mjr 78:1e00b3fa11af 3707 {
mjr 78:1e00b3fa11af 3708 // remember the mode
mjr 78:1e00b3fa11af 3709 autoCenterMode_ = mode;
mjr 78:1e00b3fa11af 3710
mjr 78:1e00b3fa11af 3711 // Set the time between checks. We check 5 times over the course
mjr 78:1e00b3fa11af 3712 // of the centering time, so the check interval is 1/5 of the total.
mjr 78:1e00b3fa11af 3713 if (mode == 0)
mjr 78:1e00b3fa11af 3714 {
mjr 78:1e00b3fa11af 3715 // mode 0 is the old default of 5 seconds, so check every 1s
mjr 78:1e00b3fa11af 3716 autoCenterCheckTime_ = 1000000;
mjr 78:1e00b3fa11af 3717 }
mjr 78:1e00b3fa11af 3718 else if (mode <= 60)
mjr 78:1e00b3fa11af 3719 {
mjr 78:1e00b3fa11af 3720 // mode 1-60 means reset after 'mode' seconds; the check
mjr 78:1e00b3fa11af 3721 // interval is 1/5 of this
mjr 78:1e00b3fa11af 3722 autoCenterCheckTime_ = mode*200000;
mjr 78:1e00b3fa11af 3723 }
mjr 78:1e00b3fa11af 3724 else
mjr 78:1e00b3fa11af 3725 {
mjr 78:1e00b3fa11af 3726 // Auto-centering is off, but still gather statistics to apply
mjr 78:1e00b3fa11af 3727 // when we get a manual centering request. The check interval
mjr 78:1e00b3fa11af 3728 // in this case is 1/5 of the total time for the trailing average
mjr 78:1e00b3fa11af 3729 // we apply for the manual centering. We want this to be long
mjr 78:1e00b3fa11af 3730 // enough to smooth out the data, but short enough that it only
mjr 78:1e00b3fa11af 3731 // includes recent data.
mjr 78:1e00b3fa11af 3732 autoCenterCheckTime_ = 500000;
mjr 78:1e00b3fa11af 3733 }
mjr 78:1e00b3fa11af 3734 }
mjr 78:1e00b3fa11af 3735
mjr 5:a70c0bce770d 3736 void reset()
mjr 5:a70c0bce770d 3737 {
mjr 6:cc35eb643e8f 3738 // clear the center point
mjr 77:0b96f6867312 3739 cx_ = cy_ = 0;
mjr 6:cc35eb643e8f 3740
mjr 77:0b96f6867312 3741 // start the auto-centering timer
mjr 5:a70c0bce770d 3742 tCenter_.start();
mjr 5:a70c0bce770d 3743 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 3744
mjr 5:a70c0bce770d 3745 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 3746 mma_.init();
mjr 77:0b96f6867312 3747
mjr 77:0b96f6867312 3748 // set the range
mjr 77:0b96f6867312 3749 mma_.setRange(
mjr 77:0b96f6867312 3750 range_ == AccelRange4G ? 4 :
mjr 77:0b96f6867312 3751 range_ == AccelRange8G ? 8 :
mjr 77:0b96f6867312 3752 2);
mjr 6:cc35eb643e8f 3753
mjr 77:0b96f6867312 3754 // set the average accumulators to zero
mjr 77:0b96f6867312 3755 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 3756 nSum_ = 0;
mjr 3:3514575d4f86 3757
mjr 3:3514575d4f86 3758 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 3759 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 3760 }
mjr 3:3514575d4f86 3761
mjr 77:0b96f6867312 3762 void poll()
mjr 76:7f5912b6340e 3763 {
mjr 77:0b96f6867312 3764 // read samples until we clear the FIFO
mjr 77:0b96f6867312 3765 while (mma_.getFIFOCount() != 0)
mjr 77:0b96f6867312 3766 {
mjr 77:0b96f6867312 3767 int x, y, z;
mjr 77:0b96f6867312 3768 mma_.getAccXYZ(x, y, z);
mjr 77:0b96f6867312 3769
mjr 77:0b96f6867312 3770 // add the new reading to the running total for averaging
mjr 77:0b96f6867312 3771 xSum_ += (x - cx_);
mjr 77:0b96f6867312 3772 ySum_ += (y - cy_);
mjr 77:0b96f6867312 3773 ++nSum_;
mjr 77:0b96f6867312 3774
mjr 77:0b96f6867312 3775 // store the updates
mjr 77:0b96f6867312 3776 ax_ = x;
mjr 77:0b96f6867312 3777 ay_ = y;
mjr 77:0b96f6867312 3778 az_ = z;
mjr 77:0b96f6867312 3779 }
mjr 76:7f5912b6340e 3780 }
mjr 77:0b96f6867312 3781
mjr 9:fd65b0a94720 3782 void get(int &x, int &y)
mjr 3:3514575d4f86 3783 {
mjr 77:0b96f6867312 3784 // read the shared data and store locally for calculations
mjr 77:0b96f6867312 3785 int ax = ax_, ay = ay_;
mjr 77:0b96f6867312 3786 int xSum = xSum_, ySum = ySum_;
mjr 77:0b96f6867312 3787 int nSum = nSum_;
mjr 6:cc35eb643e8f 3788
mjr 77:0b96f6867312 3789 // reset the average accumulators for the next run
mjr 77:0b96f6867312 3790 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 3791 nSum_ = 0;
mjr 77:0b96f6867312 3792
mjr 77:0b96f6867312 3793 // add this sample to the current calibration interval's running total
mjr 77:0b96f6867312 3794 AccHist *p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 3795 p->addAvg(ax, ay);
mjr 77:0b96f6867312 3796
mjr 78:1e00b3fa11af 3797 // If we're in auto-centering mode, check for auto-centering
mjr 78:1e00b3fa11af 3798 // at intervals of 1/5 of the overall time. If we're not in
mjr 78:1e00b3fa11af 3799 // auto-centering mode, check anyway at one-second intervals
mjr 78:1e00b3fa11af 3800 // so that we gather averages for manual centering requests.
mjr 78:1e00b3fa11af 3801 if (tCenter_.read_us() > autoCenterCheckTime_)
mjr 77:0b96f6867312 3802 {
mjr 77:0b96f6867312 3803 // add the latest raw sample to the history list
mjr 77:0b96f6867312 3804 AccHist *prv = p;
mjr 77:0b96f6867312 3805 iAccPrv_ = (iAccPrv_ + 1);
mjr 77:0b96f6867312 3806 if (iAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 3807 iAccPrv_ = 0;
mjr 77:0b96f6867312 3808 p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 3809 p->set(ax, ay, prv);
mjr 77:0b96f6867312 3810
mjr 78:1e00b3fa11af 3811 // if we have a full complement, check for auto-centering
mjr 77:0b96f6867312 3812 if (nAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 3813 {
mjr 78:1e00b3fa11af 3814 // Center if:
mjr 78:1e00b3fa11af 3815 //
mjr 78:1e00b3fa11af 3816 // - Auto-centering is on, and we've been stable over the
mjr 78:1e00b3fa11af 3817 // whole sample period at our spot-check points
mjr 78:1e00b3fa11af 3818 //
mjr 78:1e00b3fa11af 3819 // - A manual centering request is pending
mjr 78:1e00b3fa11af 3820 //
mjr 77:0b96f6867312 3821 static const int accTol = 164*164; // 1% of range, squared
mjr 77:0b96f6867312 3822 AccHist *p0 = accPrv_;
mjr 78:1e00b3fa11af 3823 if (manualCenterRequest_
mjr 78:1e00b3fa11af 3824 || (autoCenterMode_ <= 60
mjr 78:1e00b3fa11af 3825 && p0[0].dsq < accTol
mjr 78:1e00b3fa11af 3826 && p0[1].dsq < accTol
mjr 78:1e00b3fa11af 3827 && p0[2].dsq < accTol
mjr 78:1e00b3fa11af 3828 && p0[3].dsq < accTol
mjr 78:1e00b3fa11af 3829 && p0[4].dsq < accTol))
mjr 77:0b96f6867312 3830 {
mjr 77:0b96f6867312 3831 // Figure the new calibration point as the average of
mjr 77:0b96f6867312 3832 // the samples over the rest period
mjr 77:0b96f6867312 3833 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5;
mjr 77:0b96f6867312 3834 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5;
mjr 78:1e00b3fa11af 3835
mjr 78:1e00b3fa11af 3836 // clear any pending manual centering request
mjr 78:1e00b3fa11af 3837 manualCenterRequest_ = false;
mjr 77:0b96f6867312 3838 }
mjr 77:0b96f6867312 3839 }
mjr 77:0b96f6867312 3840 else
mjr 77:0b96f6867312 3841 {
mjr 77:0b96f6867312 3842 // not enough samples yet; just up the count
mjr 77:0b96f6867312 3843 ++nAccPrv_;
mjr 77:0b96f6867312 3844 }
mjr 6:cc35eb643e8f 3845
mjr 77:0b96f6867312 3846 // clear the new item's running totals
mjr 77:0b96f6867312 3847 p->clearAvg();
mjr 5:a70c0bce770d 3848
mjr 77:0b96f6867312 3849 // reset the timer
mjr 77:0b96f6867312 3850 tCenter_.reset();
mjr 77:0b96f6867312 3851 }
mjr 5:a70c0bce770d 3852
mjr 77:0b96f6867312 3853 // report our integrated velocity reading in x,y
mjr 77:0b96f6867312 3854 x = rawToReport(xSum/nSum);
mjr 77:0b96f6867312 3855 y = rawToReport(ySum/nSum);
mjr 5:a70c0bce770d 3856
mjr 6:cc35eb643e8f 3857 #ifdef DEBUG_PRINTF
mjr 77:0b96f6867312 3858 if (x != 0 || y != 0)
mjr 77:0b96f6867312 3859 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 3860 #endif
mjr 77:0b96f6867312 3861 }
mjr 29:582472d0bc57 3862
mjr 3:3514575d4f86 3863 private:
mjr 6:cc35eb643e8f 3864 // adjust a raw acceleration figure to a usb report value
mjr 77:0b96f6867312 3865 int rawToReport(int v)
mjr 5:a70c0bce770d 3866 {
mjr 77:0b96f6867312 3867 // Scale to the joystick report range. The accelerometer
mjr 77:0b96f6867312 3868 // readings use the native 14-bit signed integer representation,
mjr 77:0b96f6867312 3869 // so their scale is 2^13.
mjr 77:0b96f6867312 3870 //
mjr 77:0b96f6867312 3871 // The 1G range is special: it uses the 2G native hardware range,
mjr 77:0b96f6867312 3872 // but rescales the result to a 1G range for the joystick reports.
mjr 77:0b96f6867312 3873 // So for that mode, we divide by 4096 rather than 8192. All of
mjr 77:0b96f6867312 3874 // the other modes map use the hardware scaling directly.
mjr 77:0b96f6867312 3875 int i = v*JOYMAX;
mjr 77:0b96f6867312 3876 i = (range_ == AccelRange1G ? i/4096 : i/8192);
mjr 5:a70c0bce770d 3877
mjr 6:cc35eb643e8f 3878 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 3879 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 3880 static const int filter[] = {
mjr 6:cc35eb643e8f 3881 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 3882 0,
mjr 6:cc35eb643e8f 3883 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 3884 };
mjr 6:cc35eb643e8f 3885 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 3886 }
mjr 5:a70c0bce770d 3887
mjr 3:3514575d4f86 3888 // underlying accelerometer object
mjr 3:3514575d4f86 3889 MMA8451Q mma_;
mjr 3:3514575d4f86 3890
mjr 77:0b96f6867312 3891 // last raw acceleration readings, on the device's signed 14-bit
mjr 77:0b96f6867312 3892 // scale -8192..+8191
mjr 77:0b96f6867312 3893 int ax_, ay_, az_;
mjr 77:0b96f6867312 3894
mjr 77:0b96f6867312 3895 // running sum of readings since last get()
mjr 77:0b96f6867312 3896 int xSum_, ySum_;
mjr 77:0b96f6867312 3897
mjr 77:0b96f6867312 3898 // number of readings since last get()
mjr 77:0b96f6867312 3899 int nSum_;
mjr 6:cc35eb643e8f 3900
mjr 6:cc35eb643e8f 3901 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 3902 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 3903 // at rest.
mjr 77:0b96f6867312 3904 int cx_, cy_;
mjr 77:0b96f6867312 3905
mjr 77:0b96f6867312 3906 // range (AccelRangeXxx value, from config.h)
mjr 77:0b96f6867312 3907 uint8_t range_;
mjr 78:1e00b3fa11af 3908
mjr 78:1e00b3fa11af 3909 // auto-center mode:
mjr 78:1e00b3fa11af 3910 // 0 = default of 5-second auto-centering
mjr 78:1e00b3fa11af 3911 // 1-60 = auto-center after this many seconds
mjr 78:1e00b3fa11af 3912 // 255 = auto-centering off (manual centering only)
mjr 78:1e00b3fa11af 3913 uint8_t autoCenterMode_;
mjr 78:1e00b3fa11af 3914
mjr 78:1e00b3fa11af 3915 // flag: a manual centering request is pending
mjr 78:1e00b3fa11af 3916 bool manualCenterRequest_;
mjr 78:1e00b3fa11af 3917
mjr 78:1e00b3fa11af 3918 // time in us between auto-centering incremental checks
mjr 78:1e00b3fa11af 3919 uint32_t autoCenterCheckTime_;
mjr 78:1e00b3fa11af 3920
mjr 77:0b96f6867312 3921 // atuo-centering timer
mjr 5:a70c0bce770d 3922 Timer tCenter_;
mjr 6:cc35eb643e8f 3923
mjr 6:cc35eb643e8f 3924 // Auto-centering history. This is a separate history list that
mjr 77:0b96f6867312 3925 // records results spaced out sparsely over time, so that we can
mjr 6:cc35eb643e8f 3926 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 3927 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 3928 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 3929 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 3930 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 3931 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 3932 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 3933 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 3934 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 3935 // accelerometer measurement bias (e.g., from temperature changes).
mjr 78:1e00b3fa11af 3936 uint8_t iAccPrv_, nAccPrv_;
mjr 78:1e00b3fa11af 3937 static const uint8_t maxAccPrv = 5;
mjr 6:cc35eb643e8f 3938 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 3939
mjr 5:a70c0bce770d 3940 // interurupt pin name
mjr 5:a70c0bce770d 3941 PinName irqPin_;
mjr 3:3514575d4f86 3942 };
mjr 3:3514575d4f86 3943
mjr 5:a70c0bce770d 3944 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3945 //
mjr 14:df700b22ca08 3946 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 3947 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 3948 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 3949 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 3950 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 3951 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 3952 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 3953 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 3954 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 3955 //
mjr 14:df700b22ca08 3956 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 3957 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 3958 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 3959 //
mjr 5:a70c0bce770d 3960 void clear_i2c()
mjr 5:a70c0bce770d 3961 {
mjr 38:091e511ce8a0 3962 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 3963 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 3964 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 3965
mjr 5:a70c0bce770d 3966 // clock the SCL 9 times
mjr 5:a70c0bce770d 3967 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 3968 {
mjr 5:a70c0bce770d 3969 scl = 1;
mjr 5:a70c0bce770d 3970 wait_us(20);
mjr 5:a70c0bce770d 3971 scl = 0;
mjr 5:a70c0bce770d 3972 wait_us(20);
mjr 5:a70c0bce770d 3973 }
mjr 5:a70c0bce770d 3974 }
mjr 76:7f5912b6340e 3975
mjr 76:7f5912b6340e 3976
mjr 14:df700b22ca08 3977 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 3978 //
mjr 33:d832bcab089e 3979 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 3980 // for a given interval before allowing an update.
mjr 33:d832bcab089e 3981 //
mjr 33:d832bcab089e 3982 class Debouncer
mjr 33:d832bcab089e 3983 {
mjr 33:d832bcab089e 3984 public:
mjr 33:d832bcab089e 3985 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 3986 {
mjr 33:d832bcab089e 3987 t.start();
mjr 33:d832bcab089e 3988 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 3989 this->tmin = tmin;
mjr 33:d832bcab089e 3990 }
mjr 33:d832bcab089e 3991
mjr 33:d832bcab089e 3992 // Get the current stable value
mjr 33:d832bcab089e 3993 bool val() const { return stable; }
mjr 33:d832bcab089e 3994
mjr 33:d832bcab089e 3995 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 3996 // input device.
mjr 33:d832bcab089e 3997 void sampleIn(bool val)
mjr 33:d832bcab089e 3998 {
mjr 33:d832bcab089e 3999 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 4000 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 4001 // on the sample reader.
mjr 33:d832bcab089e 4002 if (val != prv)
mjr 33:d832bcab089e 4003 {
mjr 33:d832bcab089e 4004 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 4005 t.reset();
mjr 33:d832bcab089e 4006
mjr 33:d832bcab089e 4007 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 4008 prv = val;
mjr 33:d832bcab089e 4009 }
mjr 33:d832bcab089e 4010 else if (val != stable)
mjr 33:d832bcab089e 4011 {
mjr 33:d832bcab089e 4012 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 4013 // and different from the stable value. This means that
mjr 33:d832bcab089e 4014 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 4015 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 4016 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 4017 if (t.read() > tmin)
mjr 33:d832bcab089e 4018 stable = val;
mjr 33:d832bcab089e 4019 }
mjr 33:d832bcab089e 4020 }
mjr 33:d832bcab089e 4021
mjr 33:d832bcab089e 4022 private:
mjr 33:d832bcab089e 4023 // current stable value
mjr 33:d832bcab089e 4024 bool stable;
mjr 33:d832bcab089e 4025
mjr 33:d832bcab089e 4026 // last raw sample value
mjr 33:d832bcab089e 4027 bool prv;
mjr 33:d832bcab089e 4028
mjr 33:d832bcab089e 4029 // elapsed time since last raw input change
mjr 33:d832bcab089e 4030 Timer t;
mjr 33:d832bcab089e 4031
mjr 33:d832bcab089e 4032 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 4033 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 4034 float tmin;
mjr 33:d832bcab089e 4035 };
mjr 33:d832bcab089e 4036
mjr 33:d832bcab089e 4037
mjr 33:d832bcab089e 4038 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 4039 //
mjr 33:d832bcab089e 4040 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 4041 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 4042 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 4043 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 4044 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 4045 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 4046 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 4047 //
mjr 33:d832bcab089e 4048 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 4049 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 4050 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 4051 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 4052 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 4053 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 4054 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 4055 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 4056 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 4057 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 4058 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 4059 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 4060 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 4061 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 4062 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 4063 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 4064 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 4065 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 4066 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 4067 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 4068 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 4069 //
mjr 40:cc0d9814522b 4070 // This scheme might seem a little convoluted, but it handles a number
mjr 40:cc0d9814522b 4071 // of tricky but likely scenarios:
mjr 33:d832bcab089e 4072 //
mjr 33:d832bcab089e 4073 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 40:cc0d9814522b 4074 // so that the PC goes into "Soft Off" mode when the user turns off
mjr 40:cc0d9814522b 4075 // the cabinet by pushing the power button or using the Shut Down
mjr 40:cc0d9814522b 4076 // command from within Windows. In Windows parlance, this "soft off"
mjr 40:cc0d9814522b 4077 // condition is called ACPI State S5. In this state, the main CPU
mjr 40:cc0d9814522b 4078 // power is turned off, but the motherboard still provides power to
mjr 40:cc0d9814522b 4079 // USB devices. This means that the KL25Z keeps running. Without
mjr 40:cc0d9814522b 4080 // the external power sensing circuit, the only hint that we're in
mjr 40:cc0d9814522b 4081 // this state is that the USB connection to the host goes into Suspend
mjr 40:cc0d9814522b 4082 // mode, but that could mean other things as well. The latch circuit
mjr 40:cc0d9814522b 4083 // lets us tell for sure that we're in this state.
mjr 33:d832bcab089e 4084 //
mjr 33:d832bcab089e 4085 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 4086 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 4087 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 4088 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 4089 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 4090 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 4091 // will power up before or after PSU2, so it's not good enough to
mjr 40:cc0d9814522b 4092 // observe the current state of PSU2 when we first check. If PSU2
mjr 40:cc0d9814522b 4093 // were to come on first, checking only the current state would fool
mjr 40:cc0d9814522b 4094 // us into thinking that no action is required, because we'd only see
mjr 40:cc0d9814522b 4095 // that PSU2 is turned on any time we check. The latch handles this
mjr 40:cc0d9814522b 4096 // case by letting us see that PSU2 was indeed off some time before our
mjr 40:cc0d9814522b 4097 // first check.
mjr 33:d832bcab089e 4098 //
mjr 33:d832bcab089e 4099 // - If the KL25Z is rebooted while the main system is running, or the
mjr 40:cc0d9814522b 4100 // KL25Z is unplugged and plugged back in, we'll correctly leave the
mjr 33:d832bcab089e 4101 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 4102 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 4103 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 4104 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 4105 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 4106 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 4107 //
mjr 33:d832bcab089e 4108
mjr 77:0b96f6867312 4109 // Current PSU2 power state:
mjr 33:d832bcab089e 4110 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 4111 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 4112 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 4113 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 4114 // 5 -> TV relay on
mjr 77:0b96f6867312 4115 // 6 -> sending IR signals designed as TV ON signals
mjr 73:4e8ce0b18915 4116 uint8_t psu2_state = 1;
mjr 73:4e8ce0b18915 4117
mjr 73:4e8ce0b18915 4118 // TV relay state. The TV relay can be controlled by the power-on
mjr 73:4e8ce0b18915 4119 // timer and directly from the PC (via USB commands), so keep a
mjr 73:4e8ce0b18915 4120 // separate state for each:
mjr 73:4e8ce0b18915 4121 // 0x01 -> turned on by power-on timer
mjr 73:4e8ce0b18915 4122 // 0x02 -> turned on by USB command
mjr 73:4e8ce0b18915 4123 uint8_t tv_relay_state = 0x00;
mjr 73:4e8ce0b18915 4124 const uint8_t TV_RELAY_POWERON = 0x01;
mjr 73:4e8ce0b18915 4125 const uint8_t TV_RELAY_USB = 0x02;
mjr 73:4e8ce0b18915 4126
mjr 79:682ae3171a08 4127 // pulse timer for manual TV relay pulses
mjr 79:682ae3171a08 4128 Timer tvRelayManualTimer;
mjr 79:682ae3171a08 4129
mjr 77:0b96f6867312 4130 // TV ON IR command state. When the main PSU2 power state reaches
mjr 77:0b96f6867312 4131 // the IR phase, we use this sub-state counter to send the TV ON
mjr 77:0b96f6867312 4132 // IR signals. We initialize to state 0 when the main state counter
mjr 77:0b96f6867312 4133 // reaches the IR step. In state 0, we start transmitting the first
mjr 77:0b96f6867312 4134 // (lowest numbered) IR command slot marked as containing a TV ON
mjr 77:0b96f6867312 4135 // code, and advance to state 1. In state 1, we check to see if
mjr 77:0b96f6867312 4136 // the transmitter is still sending; if so, we do nothing, if so
mjr 77:0b96f6867312 4137 // we start transmitting the second TV ON code and advance to state
mjr 77:0b96f6867312 4138 // 2. Continue until we run out of TV ON IR codes, at which point
mjr 77:0b96f6867312 4139 // we advance to the next main psu2_state step.
mjr 77:0b96f6867312 4140 uint8_t tvon_ir_state = 0;
mjr 77:0b96f6867312 4141
mjr 77:0b96f6867312 4142 // TV ON switch relay control output pin
mjr 73:4e8ce0b18915 4143 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 4144
mjr 35:e959ffba78fd 4145 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 4146 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 4147 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 4148
mjr 73:4e8ce0b18915 4149 // Apply the current TV relay state
mjr 73:4e8ce0b18915 4150 void tvRelayUpdate(uint8_t bit, bool state)
mjr 73:4e8ce0b18915 4151 {
mjr 73:4e8ce0b18915 4152 // update the state
mjr 73:4e8ce0b18915 4153 if (state)
mjr 73:4e8ce0b18915 4154 tv_relay_state |= bit;
mjr 73:4e8ce0b18915 4155 else
mjr 73:4e8ce0b18915 4156 tv_relay_state &= ~bit;
mjr 73:4e8ce0b18915 4157
mjr 73:4e8ce0b18915 4158 // set the relay GPIO to the new state
mjr 73:4e8ce0b18915 4159 if (tv_relay != 0)
mjr 73:4e8ce0b18915 4160 tv_relay->write(tv_relay_state != 0);
mjr 73:4e8ce0b18915 4161 }
mjr 35:e959ffba78fd 4162
mjr 86:e30a1f60f783 4163 // Does the current power status allow a reboot? We shouldn't reboot
mjr 86:e30a1f60f783 4164 // in certain power states, because some states are purely internal:
mjr 86:e30a1f60f783 4165 // we can't get enough information from the external power sensor to
mjr 86:e30a1f60f783 4166 // return to the same state later. Code that performs discretionary
mjr 86:e30a1f60f783 4167 // reboots should always check here first, and delay any reboot until
mjr 86:e30a1f60f783 4168 // we say it's okay.
mjr 86:e30a1f60f783 4169 static inline bool powerStatusAllowsReboot()
mjr 86:e30a1f60f783 4170 {
mjr 86:e30a1f60f783 4171 // The only safe state for rebooting is state 1, idle/default.
mjr 86:e30a1f60f783 4172 // In other states, we can't reboot, because the external sensor
mjr 86:e30a1f60f783 4173 // and latch circuit doesn't give us enough information to return
mjr 86:e30a1f60f783 4174 // to the same state later.
mjr 86:e30a1f60f783 4175 return psu2_state == 1;
mjr 86:e30a1f60f783 4176 }
mjr 86:e30a1f60f783 4177
mjr 77:0b96f6867312 4178 // PSU2 Status update routine. The main loop calls this from time
mjr 77:0b96f6867312 4179 // to time to update the power sensing state and carry out TV ON
mjr 77:0b96f6867312 4180 // functions.
mjr 77:0b96f6867312 4181 Timer powerStatusTimer;
mjr 77:0b96f6867312 4182 uint32_t tv_delay_time_us;
mjr 77:0b96f6867312 4183 void powerStatusUpdate(Config &cfg)
mjr 33:d832bcab089e 4184 {
mjr 79:682ae3171a08 4185 // If the manual relay pulse timer is past the pulse time, end the
mjr 79:682ae3171a08 4186 // manual pulse. The timer only runs when a pulse is active, so
mjr 79:682ae3171a08 4187 // it'll never read as past the time limit if a pulse isn't on.
mjr 79:682ae3171a08 4188 if (tvRelayManualTimer.read_us() > 250000)
mjr 79:682ae3171a08 4189 {
mjr 79:682ae3171a08 4190 // turn off the relay and disable the timer
mjr 79:682ae3171a08 4191 tvRelayUpdate(TV_RELAY_USB, false);
mjr 79:682ae3171a08 4192 tvRelayManualTimer.stop();
mjr 79:682ae3171a08 4193 tvRelayManualTimer.reset();
mjr 79:682ae3171a08 4194 }
mjr 79:682ae3171a08 4195
mjr 77:0b96f6867312 4196 // Only update every 1/4 second or so. Note that if the PSU2
mjr 77:0b96f6867312 4197 // circuit isn't configured, the initialization routine won't
mjr 77:0b96f6867312 4198 // start the timer, so it'll always read zero and we'll always
mjr 77:0b96f6867312 4199 // skip this whole routine.
mjr 77:0b96f6867312 4200 if (powerStatusTimer.read_us() < 250000)
mjr 77:0b96f6867312 4201 return;
mjr 77:0b96f6867312 4202
mjr 77:0b96f6867312 4203 // reset the update timer for next time
mjr 77:0b96f6867312 4204 powerStatusTimer.reset();
mjr 77:0b96f6867312 4205
mjr 77:0b96f6867312 4206 // TV ON timer. We start this timer when we detect a change
mjr 77:0b96f6867312 4207 // in the PSU2 status from OFF to ON. When the timer reaches
mjr 77:0b96f6867312 4208 // the configured TV ON delay time, and the PSU2 power is still
mjr 77:0b96f6867312 4209 // on, we'll trigger the TV ON relay and send the TV ON IR codes.
mjr 35:e959ffba78fd 4210 static Timer tv_timer;
mjr 35:e959ffba78fd 4211
mjr 33:d832bcab089e 4212 // Check our internal state
mjr 33:d832bcab089e 4213 switch (psu2_state)
mjr 33:d832bcab089e 4214 {
mjr 33:d832bcab089e 4215 case 1:
mjr 33:d832bcab089e 4216 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 4217 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 4218 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 4219 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 4220 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 4221 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 4222 {
mjr 33:d832bcab089e 4223 // switch to OFF state
mjr 33:d832bcab089e 4224 psu2_state = 2;
mjr 33:d832bcab089e 4225
mjr 33:d832bcab089e 4226 // try setting the latch
mjr 35:e959ffba78fd 4227 psu2_status_set->write(1);
mjr 33:d832bcab089e 4228 }
mjr 77:0b96f6867312 4229 powerTimerDiagState = 0;
mjr 33:d832bcab089e 4230 break;
mjr 33:d832bcab089e 4231
mjr 33:d832bcab089e 4232 case 2:
mjr 33:d832bcab089e 4233 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 4234 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 4235 psu2_status_set->write(0);
mjr 33:d832bcab089e 4236 psu2_state = 3;
mjr 77:0b96f6867312 4237 powerTimerDiagState = 0;
mjr 33:d832bcab089e 4238 break;
mjr 33:d832bcab089e 4239
mjr 33:d832bcab089e 4240 case 3:
mjr 33:d832bcab089e 4241 // CHECK state: we pulsed SET, and we're now ready to see
mjr 40:cc0d9814522b 4242 // if it stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 4243 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 4244 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 4245 if (psu2_status_sense->read())
mjr 33:d832bcab089e 4246 {
mjr 33:d832bcab089e 4247 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 4248 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 4249 tv_timer.reset();
mjr 33:d832bcab089e 4250 tv_timer.start();
mjr 33:d832bcab089e 4251 psu2_state = 4;
mjr 73:4e8ce0b18915 4252
mjr 73:4e8ce0b18915 4253 // start the power timer diagnostic flashes
mjr 73:4e8ce0b18915 4254 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4255 }
mjr 33:d832bcab089e 4256 else
mjr 33:d832bcab089e 4257 {
mjr 33:d832bcab089e 4258 // The latch didn't stick, so PSU2 was still off at
mjr 87:8d35c74403af 4259 // our last check. Return to idle state.
mjr 87:8d35c74403af 4260 psu2_state = 1;
mjr 33:d832bcab089e 4261 }
mjr 33:d832bcab089e 4262 break;
mjr 33:d832bcab089e 4263
mjr 33:d832bcab089e 4264 case 4:
mjr 77:0b96f6867312 4265 // TV timer countdown in progress. The latch has to stay on during
mjr 77:0b96f6867312 4266 // the countdown; if the latch turns off, PSU2 power must have gone
mjr 77:0b96f6867312 4267 // off again before the countdown finished.
mjr 77:0b96f6867312 4268 if (!psu2_status_sense->read())
mjr 77:0b96f6867312 4269 {
mjr 77:0b96f6867312 4270 // power is off - start a new check cycle
mjr 77:0b96f6867312 4271 psu2_status_set->write(1);
mjr 77:0b96f6867312 4272 psu2_state = 2;
mjr 77:0b96f6867312 4273 break;
mjr 77:0b96f6867312 4274 }
mjr 77:0b96f6867312 4275
mjr 77:0b96f6867312 4276 // Flash the power time diagnostic every two cycles
mjr 77:0b96f6867312 4277 powerTimerDiagState = (powerTimerDiagState + 1) & 0x03;
mjr 77:0b96f6867312 4278
mjr 77:0b96f6867312 4279 // if we've reached the delay time, pulse the relay
mjr 77:0b96f6867312 4280 if (tv_timer.read_us() >= tv_delay_time_us)
mjr 33:d832bcab089e 4281 {
mjr 33:d832bcab089e 4282 // turn on the relay for one timer interval
mjr 73:4e8ce0b18915 4283 tvRelayUpdate(TV_RELAY_POWERON, true);
mjr 33:d832bcab089e 4284 psu2_state = 5;
mjr 77:0b96f6867312 4285
mjr 77:0b96f6867312 4286 // show solid blue on the diagnostic LED while the relay is on
mjr 77:0b96f6867312 4287 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4288 }
mjr 33:d832bcab089e 4289 break;
mjr 33:d832bcab089e 4290
mjr 33:d832bcab089e 4291 case 5:
mjr 33:d832bcab089e 4292 // TV timer relay on. We pulse this for one interval, so
mjr 77:0b96f6867312 4293 // it's now time to turn it off.
mjr 73:4e8ce0b18915 4294 tvRelayUpdate(TV_RELAY_POWERON, false);
mjr 77:0b96f6867312 4295
mjr 77:0b96f6867312 4296 // Proceed to sending any TV ON IR commands
mjr 77:0b96f6867312 4297 psu2_state = 6;
mjr 77:0b96f6867312 4298 tvon_ir_state = 0;
mjr 77:0b96f6867312 4299
mjr 77:0b96f6867312 4300 // diagnostic LEDs off for now
mjr 77:0b96f6867312 4301 powerTimerDiagState = 0;
mjr 77:0b96f6867312 4302 break;
mjr 77:0b96f6867312 4303
mjr 77:0b96f6867312 4304 case 6:
mjr 77:0b96f6867312 4305 // Sending TV ON IR signals. Start with the assumption that
mjr 77:0b96f6867312 4306 // we have no IR work to do, in which case we're done with the
mjr 77:0b96f6867312 4307 // whole TV ON sequence. So by default return to state 1.
mjr 33:d832bcab089e 4308 psu2_state = 1;
mjr 77:0b96f6867312 4309 powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 4310
mjr 77:0b96f6867312 4311 // If we have an IR emitter, check for TV ON IR commands
mjr 77:0b96f6867312 4312 if (ir_tx != 0)
mjr 77:0b96f6867312 4313 {
mjr 77:0b96f6867312 4314 // check to see if the last transmission is still in progress
mjr 77:0b96f6867312 4315 if (ir_tx->isSending())
mjr 77:0b96f6867312 4316 {
mjr 77:0b96f6867312 4317 // We're still sending the last transmission. Stay in
mjr 77:0b96f6867312 4318 // state 6.
mjr 77:0b96f6867312 4319 psu2_state = 6;
mjr 77:0b96f6867312 4320 powerTimerDiagState = 4;
mjr 77:0b96f6867312 4321 break;
mjr 77:0b96f6867312 4322 }
mjr 77:0b96f6867312 4323
mjr 77:0b96f6867312 4324 // The last transmission is done, so check for a new one.
mjr 77:0b96f6867312 4325 // Look for the Nth TV ON IR slot, where N is our state
mjr 77:0b96f6867312 4326 // number.
mjr 77:0b96f6867312 4327 for (int i = 0, n = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 4328 {
mjr 77:0b96f6867312 4329 // is this a TV ON command?
mjr 77:0b96f6867312 4330 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 4331 {
mjr 77:0b96f6867312 4332 // It's a TV ON command - check if it's the one we're
mjr 77:0b96f6867312 4333 // looking for.
mjr 77:0b96f6867312 4334 if (n == tvon_ir_state)
mjr 77:0b96f6867312 4335 {
mjr 77:0b96f6867312 4336 // It's the one. Start transmitting it by
mjr 77:0b96f6867312 4337 // pushing its virtual button.
mjr 77:0b96f6867312 4338 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 4339 ir_tx->pushButton(vb, true);
mjr 77:0b96f6867312 4340
mjr 77:0b96f6867312 4341 // Pushing the button starts transmission, and once
mjr 88:98bce687e6c0 4342 // started, the transmission runs to completion even
mjr 88:98bce687e6c0 4343 // if the button is no longer pushed. So we can
mjr 88:98bce687e6c0 4344 // immediately un-push the button, since we only need
mjr 88:98bce687e6c0 4345 // to send the code once.
mjr 77:0b96f6867312 4346 ir_tx->pushButton(vb, false);
mjr 77:0b96f6867312 4347
mjr 77:0b96f6867312 4348 // Advance to the next TV ON IR state, where we'll
mjr 77:0b96f6867312 4349 // await the end of this transmission and move on to
mjr 77:0b96f6867312 4350 // the next one.
mjr 77:0b96f6867312 4351 psu2_state = 6;
mjr 77:0b96f6867312 4352 tvon_ir_state++;
mjr 77:0b96f6867312 4353 break;
mjr 77:0b96f6867312 4354 }
mjr 77:0b96f6867312 4355
mjr 77:0b96f6867312 4356 // it's not ours - count it and keep looking
mjr 77:0b96f6867312 4357 ++n;
mjr 77:0b96f6867312 4358 }
mjr 77:0b96f6867312 4359 }
mjr 77:0b96f6867312 4360 }
mjr 33:d832bcab089e 4361 break;
mjr 33:d832bcab089e 4362 }
mjr 77:0b96f6867312 4363
mjr 77:0b96f6867312 4364 // update the diagnostic LEDs
mjr 77:0b96f6867312 4365 diagLED();
mjr 33:d832bcab089e 4366 }
mjr 33:d832bcab089e 4367
mjr 77:0b96f6867312 4368 // Start the power status timer. If the status sense circuit is enabled
mjr 77:0b96f6867312 4369 // in the configuration, we'll set up the pin connections and start the
mjr 77:0b96f6867312 4370 // timer for our periodic status checks. Does nothing if any of the pins
mjr 77:0b96f6867312 4371 // are configured as NC.
mjr 77:0b96f6867312 4372 void startPowerStatusTimer(Config &cfg)
mjr 35:e959ffba78fd 4373 {
mjr 55:4db125cd11a0 4374 // only start the timer if the pins are configured and the delay
mjr 55:4db125cd11a0 4375 // time is nonzero
mjr 77:0b96f6867312 4376 powerStatusTimer.reset();
mjr 77:0b96f6867312 4377 if (cfg.TVON.statusPin != 0xFF
mjr 77:0b96f6867312 4378 && cfg.TVON.latchPin != 0xFF)
mjr 35:e959ffba78fd 4379 {
mjr 77:0b96f6867312 4380 // set up the power sensing circuit connections
mjr 53:9b2611964afc 4381 psu2_status_sense = new DigitalIn(wirePinName(cfg.TVON.statusPin));
mjr 53:9b2611964afc 4382 psu2_status_set = new DigitalOut(wirePinName(cfg.TVON.latchPin));
mjr 77:0b96f6867312 4383
mjr 77:0b96f6867312 4384 // if there's a TV ON relay, set up its control pin
mjr 77:0b96f6867312 4385 if (cfg.TVON.relayPin != 0xFF)
mjr 77:0b96f6867312 4386 tv_relay = new DigitalOut(wirePinName(cfg.TVON.relayPin));
mjr 77:0b96f6867312 4387
mjr 77:0b96f6867312 4388 // Set the TV ON delay time. We store the time internally in
mjr 77:0b96f6867312 4389 // microseconds, but the configuration stores it in units of
mjr 77:0b96f6867312 4390 // 1/100 second = 10ms = 10000us.
mjr 77:0b96f6867312 4391 tv_delay_time_us = cfg.TVON.delayTime * 10000;;
mjr 77:0b96f6867312 4392
mjr 77:0b96f6867312 4393 // Start the TV timer
mjr 77:0b96f6867312 4394 powerStatusTimer.start();
mjr 35:e959ffba78fd 4395 }
mjr 35:e959ffba78fd 4396 }
mjr 35:e959ffba78fd 4397
mjr 73:4e8ce0b18915 4398 // Operate the TV ON relay. This allows manual control of the relay
mjr 73:4e8ce0b18915 4399 // from the PC. See protocol message 65 submessage 11.
mjr 73:4e8ce0b18915 4400 //
mjr 73:4e8ce0b18915 4401 // Mode:
mjr 73:4e8ce0b18915 4402 // 0 = turn relay off
mjr 73:4e8ce0b18915 4403 // 1 = turn relay on
mjr 73:4e8ce0b18915 4404 // 2 = pulse relay
mjr 73:4e8ce0b18915 4405 void TVRelay(int mode)
mjr 73:4e8ce0b18915 4406 {
mjr 73:4e8ce0b18915 4407 // if there's no TV relay control pin, ignore this
mjr 73:4e8ce0b18915 4408 if (tv_relay == 0)
mjr 73:4e8ce0b18915 4409 return;
mjr 73:4e8ce0b18915 4410
mjr 73:4e8ce0b18915 4411 switch (mode)
mjr 73:4e8ce0b18915 4412 {
mjr 73:4e8ce0b18915 4413 case 0:
mjr 73:4e8ce0b18915 4414 // relay off
mjr 73:4e8ce0b18915 4415 tvRelayUpdate(TV_RELAY_USB, false);
mjr 73:4e8ce0b18915 4416 break;
mjr 73:4e8ce0b18915 4417
mjr 73:4e8ce0b18915 4418 case 1:
mjr 73:4e8ce0b18915 4419 // relay on
mjr 73:4e8ce0b18915 4420 tvRelayUpdate(TV_RELAY_USB, true);
mjr 73:4e8ce0b18915 4421 break;
mjr 73:4e8ce0b18915 4422
mjr 73:4e8ce0b18915 4423 case 2:
mjr 79:682ae3171a08 4424 // Turn the relay on and reset the manual TV pulse timer
mjr 73:4e8ce0b18915 4425 tvRelayUpdate(TV_RELAY_USB, true);
mjr 79:682ae3171a08 4426 tvRelayManualTimer.reset();
mjr 79:682ae3171a08 4427 tvRelayManualTimer.start();
mjr 73:4e8ce0b18915 4428 break;
mjr 73:4e8ce0b18915 4429 }
mjr 73:4e8ce0b18915 4430 }
mjr 73:4e8ce0b18915 4431
mjr 73:4e8ce0b18915 4432
mjr 35:e959ffba78fd 4433 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4434 //
mjr 35:e959ffba78fd 4435 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 4436 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 4437 //
mjr 35:e959ffba78fd 4438 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 4439 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 4440 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 4441 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 4442 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 4443 // again each time the firmware is updated.
mjr 35:e959ffba78fd 4444 //
mjr 35:e959ffba78fd 4445 NVM nvm;
mjr 35:e959ffba78fd 4446
mjr 86:e30a1f60f783 4447 // Save Config followup time, in seconds. After a successful save,
mjr 86:e30a1f60f783 4448 // we leave the success flag on in the status for this interval. At
mjr 86:e30a1f60f783 4449 // the end of the interval, we reboot the device if requested.
mjr 86:e30a1f60f783 4450 uint8_t saveConfigFollowupTime;
mjr 86:e30a1f60f783 4451
mjr 86:e30a1f60f783 4452 // is a reboot pending at the end of the config save followup interval?
mjr 86:e30a1f60f783 4453 uint8_t saveConfigRebootPending;
mjr 77:0b96f6867312 4454
mjr 79:682ae3171a08 4455 // status flag for successful config save - set to 0x40 on success
mjr 79:682ae3171a08 4456 uint8_t saveConfigSucceededFlag;
mjr 79:682ae3171a08 4457
mjr 86:e30a1f60f783 4458 // Timer for configuration change followup timer
mjr 86:e30a1f60f783 4459 ExtTimer saveConfigFollowupTimer;
mjr 86:e30a1f60f783 4460
mjr 86:e30a1f60f783 4461
mjr 35:e959ffba78fd 4462 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 4463 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 4464
mjr 35:e959ffba78fd 4465 // flash memory controller interface
mjr 35:e959ffba78fd 4466 FreescaleIAP iap;
mjr 35:e959ffba78fd 4467
mjr 79:682ae3171a08 4468 // figure the flash address for the config data
mjr 79:682ae3171a08 4469 const NVM *configFlashAddr()
mjr 76:7f5912b6340e 4470 {
mjr 79:682ae3171a08 4471 // figure the number of sectors we need, rounding up
mjr 79:682ae3171a08 4472 int nSectors = (sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE;
mjr 79:682ae3171a08 4473
mjr 79:682ae3171a08 4474 // figure the total size required from the number of sectors
mjr 79:682ae3171a08 4475 int reservedSize = nSectors * SECTOR_SIZE;
mjr 79:682ae3171a08 4476
mjr 79:682ae3171a08 4477 // locate it at the top of memory
mjr 79:682ae3171a08 4478 uint32_t addr = iap.flashSize() - reservedSize;
mjr 79:682ae3171a08 4479
mjr 79:682ae3171a08 4480 // return it as a read-only NVM pointer
mjr 79:682ae3171a08 4481 return (const NVM *)addr;
mjr 35:e959ffba78fd 4482 }
mjr 35:e959ffba78fd 4483
mjr 76:7f5912b6340e 4484 // Load the config from flash. Returns true if a valid non-default
mjr 76:7f5912b6340e 4485 // configuration was loaded, false if we not. If we return false,
mjr 76:7f5912b6340e 4486 // we load the factory defaults, so the configuration object is valid
mjr 76:7f5912b6340e 4487 // in either case.
mjr 76:7f5912b6340e 4488 bool loadConfigFromFlash()
mjr 35:e959ffba78fd 4489 {
mjr 35:e959ffba78fd 4490 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 4491 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 4492 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 4493 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 4494 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 4495 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 4496 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 4497 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 4498 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 4499 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 4500 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 4501 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 4502 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 4503 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 4504 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 4505 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 4506
mjr 35:e959ffba78fd 4507 // Figure how many sectors we need for our structure
mjr 79:682ae3171a08 4508 const NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 4509
mjr 35:e959ffba78fd 4510 // if the flash is valid, load it; otherwise initialize to defaults
mjr 76:7f5912b6340e 4511 bool nvm_valid = flash->valid();
mjr 76:7f5912b6340e 4512 if (nvm_valid)
mjr 35:e959ffba78fd 4513 {
mjr 35:e959ffba78fd 4514 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 4515 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 4516 }
mjr 35:e959ffba78fd 4517 else
mjr 35:e959ffba78fd 4518 {
mjr 76:7f5912b6340e 4519 // flash is invalid - load factory settings into RAM structure
mjr 35:e959ffba78fd 4520 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 4521 }
mjr 76:7f5912b6340e 4522
mjr 76:7f5912b6340e 4523 // tell the caller what happened
mjr 76:7f5912b6340e 4524 return nvm_valid;
mjr 35:e959ffba78fd 4525 }
mjr 35:e959ffba78fd 4526
mjr 86:e30a1f60f783 4527 // Save the config. Returns true on success, false on failure.
mjr 86:e30a1f60f783 4528 // 'tFollowup' is the follow-up time in seconds. If the write is
mjr 86:e30a1f60f783 4529 // successful, we'll turn on the success flag in the status reports
mjr 86:e30a1f60f783 4530 // and leave it on for this interval. If 'reboot' is true, we'll
mjr 86:e30a1f60f783 4531 // also schedule a reboot at the end of the followup interval.
mjr 86:e30a1f60f783 4532 bool saveConfigToFlash(int tFollowup, bool reboot)
mjr 33:d832bcab089e 4533 {
mjr 76:7f5912b6340e 4534 // make sure the plunger sensor isn't busy
mjr 76:7f5912b6340e 4535 waitPlungerIdle();
mjr 76:7f5912b6340e 4536
mjr 76:7f5912b6340e 4537 // get the config block location in the flash memory
mjr 77:0b96f6867312 4538 uint32_t addr = uint32_t(configFlashAddr());
mjr 79:682ae3171a08 4539
mjr 79:682ae3171a08 4540 // save the data
mjr 86:e30a1f60f783 4541 if (nvm.save(iap, addr))
mjr 86:e30a1f60f783 4542 {
mjr 86:e30a1f60f783 4543 // success - report the successful save in the status flags
mjr 86:e30a1f60f783 4544 saveConfigSucceededFlag = 0x40;
mjr 86:e30a1f60f783 4545
mjr 86:e30a1f60f783 4546 // start the followup timer
mjr 87:8d35c74403af 4547 saveConfigFollowupTime = tFollowup;
mjr 87:8d35c74403af 4548 saveConfigFollowupTimer.reset();
mjr 86:e30a1f60f783 4549 saveConfigFollowupTimer.start();
mjr 86:e30a1f60f783 4550
mjr 86:e30a1f60f783 4551 // if a reboot is pending, flag it
mjr 86:e30a1f60f783 4552 saveConfigRebootPending = reboot;
mjr 86:e30a1f60f783 4553
mjr 86:e30a1f60f783 4554 // return success
mjr 86:e30a1f60f783 4555 return true;
mjr 86:e30a1f60f783 4556 }
mjr 86:e30a1f60f783 4557 else
mjr 86:e30a1f60f783 4558 {
mjr 86:e30a1f60f783 4559 // return failure
mjr 86:e30a1f60f783 4560 return false;
mjr 86:e30a1f60f783 4561 }
mjr 76:7f5912b6340e 4562 }
mjr 76:7f5912b6340e 4563
mjr 76:7f5912b6340e 4564 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 4565 //
mjr 76:7f5912b6340e 4566 // Host-loaded configuration. The Flash NVM block above is designed to be
mjr 76:7f5912b6340e 4567 // stored from within the firmware; in contrast, the host-loaded config is
mjr 76:7f5912b6340e 4568 // stored by the host, by patching the firwmare binary (.bin) file before
mjr 76:7f5912b6340e 4569 // downloading it to the device.
mjr 76:7f5912b6340e 4570 //
mjr 76:7f5912b6340e 4571 // Ideally, we'd use the host-loaded memory for all configuration updates,
mjr 76:7f5912b6340e 4572 // because the KL25Z doesn't seem to be 100% reliable writing flash itself.
mjr 76:7f5912b6340e 4573 // There seems to be a chance of memory bus contention while a write is in
mjr 76:7f5912b6340e 4574 // progress, which can either corrupt the write or cause the CPU to lock up
mjr 76:7f5912b6340e 4575 // before the write is completed. It seems more reliable to program the
mjr 76:7f5912b6340e 4576 // flash externally, via the OpenSDA connection. Unfortunately, none of
mjr 76:7f5912b6340e 4577 // the available OpenSDA versions are capable of programming specific flash
mjr 76:7f5912b6340e 4578 // sectors; they always erase the entire flash memory space. We *could*
mjr 76:7f5912b6340e 4579 // make the Windows config program simply re-download the entire firmware
mjr 76:7f5912b6340e 4580 // for every configuration update, but I'd rather not because of the extra
mjr 76:7f5912b6340e 4581 // wear this would put on the flash. So, as a compromise, we'll use the
mjr 76:7f5912b6340e 4582 // host-loaded config whenever the user explicitly updates the firmware,
mjr 76:7f5912b6340e 4583 // but we'll use the on-board writer when only making a config change.
mjr 76:7f5912b6340e 4584 //
mjr 76:7f5912b6340e 4585 // The memory here is stored using the same format as the USB "Set Config
mjr 76:7f5912b6340e 4586 // Variable" command. These messages are 8 bytes long and start with a
mjr 76:7f5912b6340e 4587 // byte value 66, followed by the variable ID, followed by the variable
mjr 76:7f5912b6340e 4588 // value data in a format defined separately for each variable. To load
mjr 76:7f5912b6340e 4589 // the data, we'll start at the first byte after the signature, and
mjr 76:7f5912b6340e 4590 // interpret each 8-byte block as a type 66 message. If the first byte
mjr 76:7f5912b6340e 4591 // of a block is not 66, we'll take it as the end of the data.
mjr 76:7f5912b6340e 4592 //
mjr 76:7f5912b6340e 4593 // We provide a block of storage here big enough for 1,024 variables.
mjr 76:7f5912b6340e 4594 // The header consists of a 30-byte signature followed by two bytes giving
mjr 76:7f5912b6340e 4595 // the available space in the area, in this case 8192 == 0x0200. The
mjr 76:7f5912b6340e 4596 // length is little-endian. Note that the linker will implicitly zero
mjr 76:7f5912b6340e 4597 // the rest of the block, so if the host doesn't populate it, we'll see
mjr 76:7f5912b6340e 4598 // that it's empty by virtue of not containing the required '66' byte
mjr 76:7f5912b6340e 4599 // prefix for the first 8-byte variable block.
mjr 76:7f5912b6340e 4600 static const uint8_t hostLoadedConfig[8192+32]
mjr 76:7f5912b6340e 4601 __attribute__ ((aligned(SECTOR_SIZE))) =
mjr 76:7f5912b6340e 4602 "///Pinscape.HostLoadedConfig//\0\040"; // 30 byte signature + 2 byte length
mjr 76:7f5912b6340e 4603
mjr 76:7f5912b6340e 4604 // Get a pointer to the first byte of the configuration data
mjr 76:7f5912b6340e 4605 const uint8_t *getHostLoadedConfigData()
mjr 76:7f5912b6340e 4606 {
mjr 76:7f5912b6340e 4607 // the first configuration variable byte immediately follows the
mjr 76:7f5912b6340e 4608 // 32-byte signature header
mjr 76:7f5912b6340e 4609 return hostLoadedConfig + 32;
mjr 76:7f5912b6340e 4610 };
mjr 76:7f5912b6340e 4611
mjr 76:7f5912b6340e 4612 // forward reference to config var store function
mjr 76:7f5912b6340e 4613 void configVarSet(const uint8_t *);
mjr 76:7f5912b6340e 4614
mjr 76:7f5912b6340e 4615 // Load the host-loaded configuration data into the active (RAM)
mjr 76:7f5912b6340e 4616 // configuration object.
mjr 76:7f5912b6340e 4617 void loadHostLoadedConfig()
mjr 76:7f5912b6340e 4618 {
mjr 76:7f5912b6340e 4619 // Start at the first configuration variable. Each variable
mjr 76:7f5912b6340e 4620 // block is in the format of a Set Config Variable command in
mjr 76:7f5912b6340e 4621 // the USB protocol, so each block starts with a byte value of
mjr 76:7f5912b6340e 4622 // 66 and is 8 bytes long. Continue as long as we find valid
mjr 76:7f5912b6340e 4623 // variable blocks, or reach end end of the block.
mjr 76:7f5912b6340e 4624 const uint8_t *start = getHostLoadedConfigData();
mjr 76:7f5912b6340e 4625 const uint8_t *end = hostLoadedConfig + sizeof(hostLoadedConfig);
mjr 76:7f5912b6340e 4626 for (const uint8_t *p = getHostLoadedConfigData() ; start < end && *p == 66 ; p += 8)
mjr 76:7f5912b6340e 4627 {
mjr 76:7f5912b6340e 4628 // load this variable
mjr 76:7f5912b6340e 4629 configVarSet(p);
mjr 76:7f5912b6340e 4630 }
mjr 35:e959ffba78fd 4631 }
mjr 35:e959ffba78fd 4632
mjr 35:e959ffba78fd 4633 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4634 //
mjr 55:4db125cd11a0 4635 // Pixel dump mode - the host requested a dump of image sensor pixels
mjr 55:4db125cd11a0 4636 // (helpful for installing and setting up the sensor and light source)
mjr 55:4db125cd11a0 4637 //
mjr 55:4db125cd11a0 4638 bool reportPlungerStat = false;
mjr 55:4db125cd11a0 4639 uint8_t reportPlungerStatFlags; // plunger pixel report flag bits (see ccdSensor.h)
mjr 55:4db125cd11a0 4640 uint8_t reportPlungerStatTime; // extra exposure time for plunger pixel report
mjr 55:4db125cd11a0 4641
mjr 55:4db125cd11a0 4642
mjr 55:4db125cd11a0 4643
mjr 55:4db125cd11a0 4644 // ---------------------------------------------------------------------------
mjr 55:4db125cd11a0 4645 //
mjr 40:cc0d9814522b 4646 // Night mode setting updates
mjr 40:cc0d9814522b 4647 //
mjr 38:091e511ce8a0 4648
mjr 38:091e511ce8a0 4649 // Turn night mode on or off
mjr 38:091e511ce8a0 4650 static void setNightMode(bool on)
mjr 38:091e511ce8a0 4651 {
mjr 77:0b96f6867312 4652 // Set the new night mode flag in the noisy output class. Note
mjr 77:0b96f6867312 4653 // that we use the status report bit flag value 0x02 when on, so
mjr 77:0b96f6867312 4654 // that we can just '|' this into the overall status bits.
mjr 77:0b96f6867312 4655 nightMode = on ? 0x02 : 0x00;
mjr 55:4db125cd11a0 4656
mjr 40:cc0d9814522b 4657 // update the special output pin that shows the night mode state
mjr 53:9b2611964afc 4658 int port = int(cfg.nightMode.port) - 1;
mjr 53:9b2611964afc 4659 if (port >= 0 && port < numOutputs)
mjr 53:9b2611964afc 4660 lwPin[port]->set(nightMode ? 255 : 0);
mjr 76:7f5912b6340e 4661
mjr 76:7f5912b6340e 4662 // Reset all outputs at their current value, so that the underlying
mjr 76:7f5912b6340e 4663 // physical outputs get turned on or off as appropriate for the night
mjr 76:7f5912b6340e 4664 // mode change.
mjr 76:7f5912b6340e 4665 for (int i = 0 ; i < numOutputs ; ++i)
mjr 76:7f5912b6340e 4666 lwPin[i]->set(outLevel[i]);
mjr 76:7f5912b6340e 4667
mjr 76:7f5912b6340e 4668 // update 74HC595 outputs
mjr 76:7f5912b6340e 4669 if (hc595 != 0)
mjr 76:7f5912b6340e 4670 hc595->update();
mjr 38:091e511ce8a0 4671 }
mjr 38:091e511ce8a0 4672
mjr 38:091e511ce8a0 4673 // Toggle night mode
mjr 38:091e511ce8a0 4674 static void toggleNightMode()
mjr 38:091e511ce8a0 4675 {
mjr 53:9b2611964afc 4676 setNightMode(!nightMode);
mjr 38:091e511ce8a0 4677 }
mjr 38:091e511ce8a0 4678
mjr 38:091e511ce8a0 4679
mjr 38:091e511ce8a0 4680 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 4681 //
mjr 35:e959ffba78fd 4682 // Plunger Sensor
mjr 35:e959ffba78fd 4683 //
mjr 35:e959ffba78fd 4684
mjr 35:e959ffba78fd 4685 // the plunger sensor interface object
mjr 35:e959ffba78fd 4686 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 4687
mjr 87:8d35c74403af 4688 // wait for the plunger sensor to complete any outstanding DMA transfer
mjr 76:7f5912b6340e 4689 static void waitPlungerIdle(void)
mjr 76:7f5912b6340e 4690 {
mjr 87:8d35c74403af 4691 while (plungerSensor->dmaBusy()) { }
mjr 76:7f5912b6340e 4692 }
mjr 76:7f5912b6340e 4693
mjr 35:e959ffba78fd 4694 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 4695 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 4696 void createPlunger()
mjr 35:e959ffba78fd 4697 {
mjr 35:e959ffba78fd 4698 // create the new sensor object according to the type
mjr 35:e959ffba78fd 4699 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 4700 {
mjr 82:4f6209cb5c33 4701 case PlungerType_TSL1410R:
mjr 82:4f6209cb5c33 4702 // TSL1410R, shadow edge detector
mjr 35:e959ffba78fd 4703 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 4704 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 4705 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4706 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 4707 wirePinName(cfg.plunger.sensorPin[2]));
mjr 35:e959ffba78fd 4708 break;
mjr 35:e959ffba78fd 4709
mjr 82:4f6209cb5c33 4710 case PlungerType_TSL1412S:
mjr 82:4f6209cb5c33 4711 // TSL1412S, shadow edge detector
mjr 82:4f6209cb5c33 4712 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 4713 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 4714 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 4715 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 4716 wirePinName(cfg.plunger.sensorPin[2]));
mjr 35:e959ffba78fd 4717 break;
mjr 35:e959ffba78fd 4718
mjr 35:e959ffba78fd 4719 case PlungerType_Pot:
mjr 82:4f6209cb5c33 4720 // Potentiometer (or any other sensor with a linear analog voltage
mjr 82:4f6209cb5c33 4721 // reading as the proxy for the position)
mjr 82:4f6209cb5c33 4722 // pins are: AO (analog in)
mjr 53:9b2611964afc 4723 plungerSensor = new PlungerSensorPot(
mjr 53:9b2611964afc 4724 wirePinName(cfg.plunger.sensorPin[0]));
mjr 35:e959ffba78fd 4725 break;
mjr 82:4f6209cb5c33 4726
mjr 82:4f6209cb5c33 4727 case PlungerType_OptQuad:
mjr 82:4f6209cb5c33 4728 // Optical quadrature sensor, AEDR8300-K or similar. The -K is
mjr 82:4f6209cb5c33 4729 // designed for a 75 LPI scale, which translates to 300 pulses/inch.
mjr 82:4f6209cb5c33 4730 // Pins are: CHA, CHB (quadrature pulse inputs).
mjr 82:4f6209cb5c33 4731 plungerSensor = new PlungerSensorQuad(
mjr 82:4f6209cb5c33 4732 300,
mjr 82:4f6209cb5c33 4733 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 4734 wirePinName(cfg.plunger.sensorPin[1]));
mjr 82:4f6209cb5c33 4735 break;
mjr 82:4f6209cb5c33 4736
mjr 82:4f6209cb5c33 4737 case PlungerType_TSL1401CL:
mjr 82:4f6209cb5c33 4738 // TSL1401CL, absolute position encoder with bar code scale
mjr 82:4f6209cb5c33 4739 // pins are: SI, CLOCK, AO
mjr 82:4f6209cb5c33 4740 plungerSensor = new PlungerSensorTSL1401CL(
mjr 82:4f6209cb5c33 4741 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 4742 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 4743 wirePinName(cfg.plunger.sensorPin[2]));
mjr 82:4f6209cb5c33 4744 break;
mjr 82:4f6209cb5c33 4745
mjr 82:4f6209cb5c33 4746 case PlungerType_VL6180X:
mjr 82:4f6209cb5c33 4747 // VL6180X time-of-flight IR distance sensor
mjr 82:4f6209cb5c33 4748 // pins are: SDL, SCL, GPIO0/CE
mjr 82:4f6209cb5c33 4749 plungerSensor = new PlungerSensorVL6180X(
mjr 82:4f6209cb5c33 4750 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 4751 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 4752 wirePinName(cfg.plunger.sensorPin[2]));
mjr 82:4f6209cb5c33 4753 break;
mjr 82:4f6209cb5c33 4754
mjr 35:e959ffba78fd 4755 case PlungerType_None:
mjr 35:e959ffba78fd 4756 default:
mjr 35:e959ffba78fd 4757 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 4758 break;
mjr 35:e959ffba78fd 4759 }
mjr 86:e30a1f60f783 4760
mjr 87:8d35c74403af 4761 // initialize the config variables affecting the plunger
mjr 87:8d35c74403af 4762 plungerSensor->onConfigChange(19, cfg);
mjr 87:8d35c74403af 4763 plungerSensor->onConfigChange(20, cfg);
mjr 33:d832bcab089e 4764 }
mjr 33:d832bcab089e 4765
mjr 52:8298b2a73eb2 4766 // Global plunger calibration mode flag
mjr 52:8298b2a73eb2 4767 bool plungerCalMode;
mjr 52:8298b2a73eb2 4768
mjr 48:058ace2aed1d 4769 // Plunger reader
mjr 51:57eb311faafa 4770 //
mjr 51:57eb311faafa 4771 // This class encapsulates our plunger data processing. At the simplest
mjr 51:57eb311faafa 4772 // level, we read the position from the sensor, adjust it for the
mjr 51:57eb311faafa 4773 // calibration settings, and report the calibrated position to the host.
mjr 51:57eb311faafa 4774 //
mjr 51:57eb311faafa 4775 // In addition, we constantly monitor the data for "firing" motions.
mjr 51:57eb311faafa 4776 // A firing motion is when the user pulls back the plunger and releases
mjr 51:57eb311faafa 4777 // it, allowing it to shoot forward under the force of the main spring.
mjr 51:57eb311faafa 4778 // When we detect that this is happening, we briefly stop reporting the
mjr 51:57eb311faafa 4779 // real physical position that we're reading from the sensor, and instead
mjr 51:57eb311faafa 4780 // report a synthetic series of positions that depicts an idealized
mjr 51:57eb311faafa 4781 // firing motion.
mjr 51:57eb311faafa 4782 //
mjr 51:57eb311faafa 4783 // The point of the synthetic reports is to correct for distortions
mjr 51:57eb311faafa 4784 // created by the joystick interface conventions used by VP and other
mjr 51:57eb311faafa 4785 // PC pinball emulators. The convention they use is simply to have the
mjr 51:57eb311faafa 4786 // plunger device report the instantaneous position of the real plunger.
mjr 51:57eb311faafa 4787 // The PC software polls this reported position periodically, and moves
mjr 51:57eb311faafa 4788 // the on-screen virtual plunger in sync with the real plunger. This
mjr 51:57eb311faafa 4789 // works fine for human-scale motion when the user is manually moving
mjr 51:57eb311faafa 4790 // the plunger. But it doesn't work for the high speed motion of a
mjr 51:57eb311faafa 4791 // release. The plunger simply moves too fast. VP polls in about 10ms
mjr 51:57eb311faafa 4792 // intervals; the plunger takes about 50ms to travel from fully
mjr 51:57eb311faafa 4793 // retracted to the park position when released. The low sampling
mjr 51:57eb311faafa 4794 // rate relative to the rate of change of the sampled data creates
mjr 51:57eb311faafa 4795 // a classic digital aliasing effect.
mjr 51:57eb311faafa 4796 //
mjr 51:57eb311faafa 4797 // The synthetic reporting scheme compensates for the interface
mjr 51:57eb311faafa 4798 // distortions by essentially changing to a coarse enough timescale
mjr 51:57eb311faafa 4799 // that VP can reliably interpret the readings. Conceptually, there
mjr 51:57eb311faafa 4800 // are three steps involved in doing this. First, we analyze the
mjr 51:57eb311faafa 4801 // actual sensor data to detect and characterize the release motion.
mjr 51:57eb311faafa 4802 // Second, once we think we have a release in progress, we fit the
mjr 51:57eb311faafa 4803 // data to a mathematical model of the release. The model we use is
mjr 51:57eb311faafa 4804 // dead simple: we consider the release to have one parameter, namely
mjr 51:57eb311faafa 4805 // the retraction distance at the moment the user lets go. This is an
mjr 51:57eb311faafa 4806 // excellent proxy in the real physical system for the final speed
mjr 51:57eb311faafa 4807 // when the plunger hits the ball, and it also happens to match how
mjr 51:57eb311faafa 4808 // VP models it internally. Third, we construct synthetic reports
mjr 51:57eb311faafa 4809 // that will make VP's internal state match our model. This is also
mjr 51:57eb311faafa 4810 // pretty simple: we just need to send VP the maximum retraction
mjr 51:57eb311faafa 4811 // distance for long enough to be sure that it polls it at least
mjr 51:57eb311faafa 4812 // once, and then send it the park position for long enough to
mjr 51:57eb311faafa 4813 // ensure that VP will complete the same firing motion. The
mjr 51:57eb311faafa 4814 // immediate jump from the maximum point to the zero point will
mjr 51:57eb311faafa 4815 // cause VP to move its simulation model plunger forward from the
mjr 51:57eb311faafa 4816 // starting point at its natural spring acceleration rate, which
mjr 51:57eb311faafa 4817 // is exactly what the real plunger just did.
mjr 51:57eb311faafa 4818 //
mjr 48:058ace2aed1d 4819 class PlungerReader
mjr 48:058ace2aed1d 4820 {
mjr 48:058ace2aed1d 4821 public:
mjr 48:058ace2aed1d 4822 PlungerReader()
mjr 48:058ace2aed1d 4823 {
mjr 48:058ace2aed1d 4824 // not in a firing event yet
mjr 48:058ace2aed1d 4825 firing = 0;
mjr 48:058ace2aed1d 4826 }
mjr 76:7f5912b6340e 4827
mjr 48:058ace2aed1d 4828 // Collect a reading from the plunger sensor. The main loop calls
mjr 48:058ace2aed1d 4829 // this frequently to read the current raw position data from the
mjr 48:058ace2aed1d 4830 // sensor. We analyze the raw data to produce the calibrated
mjr 48:058ace2aed1d 4831 // position that we report to the PC via the joystick interface.
mjr 48:058ace2aed1d 4832 void read()
mjr 48:058ace2aed1d 4833 {
mjr 76:7f5912b6340e 4834 // if the sensor is busy, skip the reading on this round
mjr 76:7f5912b6340e 4835 if (!plungerSensor->ready())
mjr 76:7f5912b6340e 4836 return;
mjr 76:7f5912b6340e 4837
mjr 48:058ace2aed1d 4838 // Read a sample from the sensor
mjr 48:058ace2aed1d 4839 PlungerReading r;
mjr 48:058ace2aed1d 4840 if (plungerSensor->read(r))
mjr 48:058ace2aed1d 4841 {
mjr 53:9b2611964afc 4842 // check for calibration mode
mjr 53:9b2611964afc 4843 if (plungerCalMode)
mjr 53:9b2611964afc 4844 {
mjr 53:9b2611964afc 4845 // Calibration mode. Adjust the calibration bounds to fit
mjr 53:9b2611964afc 4846 // the value. If this value is beyond the current min or max,
mjr 53:9b2611964afc 4847 // expand the envelope to include this new value.
mjr 53:9b2611964afc 4848 if (r.pos > cfg.plunger.cal.max)
mjr 53:9b2611964afc 4849 cfg.plunger.cal.max = r.pos;
mjr 53:9b2611964afc 4850 if (r.pos < cfg.plunger.cal.min)
mjr 53:9b2611964afc 4851 cfg.plunger.cal.min = r.pos;
mjr 76:7f5912b6340e 4852
mjr 76:7f5912b6340e 4853 // update our cached calibration data
mjr 76:7f5912b6340e 4854 onUpdateCal();
mjr 50:40015764bbe6 4855
mjr 53:9b2611964afc 4856 // If we're in calibration state 0, we're waiting for the
mjr 53:9b2611964afc 4857 // plunger to come to rest at the park position so that we
mjr 53:9b2611964afc 4858 // can take a sample of the park position. Check to see if
mjr 53:9b2611964afc 4859 // we've been at rest for a minimum interval.
mjr 53:9b2611964afc 4860 if (calState == 0)
mjr 53:9b2611964afc 4861 {
mjr 53:9b2611964afc 4862 if (abs(r.pos - calZeroStart.pos) < 65535/3/50)
mjr 53:9b2611964afc 4863 {
mjr 53:9b2611964afc 4864 // we're close enough - make sure we've been here long enough
mjr 53:9b2611964afc 4865 if (uint32_t(r.t - calZeroStart.t) > 100000UL)
mjr 53:9b2611964afc 4866 {
mjr 53:9b2611964afc 4867 // we've been at rest long enough - count it
mjr 53:9b2611964afc 4868 calZeroPosSum += r.pos;
mjr 53:9b2611964afc 4869 calZeroPosN += 1;
mjr 53:9b2611964afc 4870
mjr 53:9b2611964afc 4871 // update the zero position from the new average
mjr 53:9b2611964afc 4872 cfg.plunger.cal.zero = uint16_t(calZeroPosSum / calZeroPosN);
mjr 76:7f5912b6340e 4873 onUpdateCal();
mjr 53:9b2611964afc 4874
mjr 53:9b2611964afc 4875 // switch to calibration state 1 - at rest
mjr 53:9b2611964afc 4876 calState = 1;
mjr 53:9b2611964afc 4877 }
mjr 53:9b2611964afc 4878 }
mjr 53:9b2611964afc 4879 else
mjr 53:9b2611964afc 4880 {
mjr 53:9b2611964afc 4881 // we're not close to the last position - start again here
mjr 53:9b2611964afc 4882 calZeroStart = r;
mjr 53:9b2611964afc 4883 }
mjr 53:9b2611964afc 4884 }
mjr 53:9b2611964afc 4885
mjr 53:9b2611964afc 4886 // Rescale to the joystick range, and adjust for the current
mjr 53:9b2611964afc 4887 // park position, but don't calibrate. We don't know the maximum
mjr 53:9b2611964afc 4888 // point yet, so we can't calibrate the range.
mjr 53:9b2611964afc 4889 r.pos = int(
mjr 53:9b2611964afc 4890 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 4891 / (65535 - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 4892 }
mjr 53:9b2611964afc 4893 else
mjr 53:9b2611964afc 4894 {
mjr 53:9b2611964afc 4895 // Not in calibration mode. Apply the existing calibration and
mjr 53:9b2611964afc 4896 // rescale to the joystick range.
mjr 76:7f5912b6340e 4897 r.pos = applyCal(r.pos);
mjr 53:9b2611964afc 4898
mjr 53:9b2611964afc 4899 // limit the result to the valid joystick range
mjr 53:9b2611964afc 4900 if (r.pos > JOYMAX)
mjr 53:9b2611964afc 4901 r.pos = JOYMAX;
mjr 53:9b2611964afc 4902 else if (r.pos < -JOYMAX)
mjr 53:9b2611964afc 4903 r.pos = -JOYMAX;
mjr 53:9b2611964afc 4904 }
mjr 50:40015764bbe6 4905
mjr 87:8d35c74403af 4906 // Look for a firing event - the user releasing the plunger and
mjr 87:8d35c74403af 4907 // allowing it to shoot forward at full speed. Wait at least 5ms
mjr 87:8d35c74403af 4908 // between samples for this, to help distinguish random motion
mjr 87:8d35c74403af 4909 // from the rapid motion of a firing event.
mjr 50:40015764bbe6 4910 //
mjr 87:8d35c74403af 4911 // There's a trade-off in the choice of minimum sampling interval.
mjr 87:8d35c74403af 4912 // The longer we wait, the more certain we can be of the trend.
mjr 87:8d35c74403af 4913 // But if we wait too long, the user will perceive a delay. We
mjr 87:8d35c74403af 4914 // also want to sample frequently enough to see the release motion
mjr 87:8d35c74403af 4915 // at intermediate steps along the way, so the sampling has to be
mjr 87:8d35c74403af 4916 // considerably faster than the whole travel time, which is about
mjr 87:8d35c74403af 4917 // 25-50ms.
mjr 87:8d35c74403af 4918 if (uint32_t(r.t - prv.t) < 5000UL)
mjr 87:8d35c74403af 4919 return;
mjr 87:8d35c74403af 4920
mjr 87:8d35c74403af 4921 // assume that we'll report this reading as-is
mjr 87:8d35c74403af 4922 z = r.pos;
mjr 87:8d35c74403af 4923
mjr 87:8d35c74403af 4924 // Firing event detection.
mjr 87:8d35c74403af 4925 //
mjr 87:8d35c74403af 4926 // A "firing event" is when the player releases the plunger from
mjr 87:8d35c74403af 4927 // a retracted position, allowing it to shoot forward under the
mjr 87:8d35c74403af 4928 // spring tension.
mjr 50:40015764bbe6 4929 //
mjr 87:8d35c74403af 4930 // We monitor the plunger motion for these events, and when they
mjr 87:8d35c74403af 4931 // occur, we report an "idealized" version of the motion to the
mjr 87:8d35c74403af 4932 // PC. The idealized version consists of a series of readings
mjr 87:8d35c74403af 4933 // frozen at the fully retracted position for the whole duration
mjr 87:8d35c74403af 4934 // of the forward travel, followed by a series of readings at the
mjr 87:8d35c74403af 4935 // fully forward position for long enough for the plunger to come
mjr 87:8d35c74403af 4936 // mostly to rest. The series of frozen readings aren't meant to
mjr 87:8d35c74403af 4937 // be perceptible to the player - we try to keep them short enough
mjr 87:8d35c74403af 4938 // that they're not apparent as delay. Instead, they're for the
mjr 87:8d35c74403af 4939 // PC client software's benefit. PC joystick clients use polling,
mjr 87:8d35c74403af 4940 // so they only see an unpredictable subset of the readings we
mjr 87:8d35c74403af 4941 // send. The only way to be sure that the client sees a particular
mjr 87:8d35c74403af 4942 // reading is to hold it for long enough that the client is sure to
mjr 87:8d35c74403af 4943 // poll within the hold interval. In the case of the plunger
mjr 87:8d35c74403af 4944 // firing motion, it's important that the client sees the *ends*
mjr 87:8d35c74403af 4945 // of the travel - the fully retracted starting position in
mjr 87:8d35c74403af 4946 // particular. If the PC client only polls for a sample while the
mjr 87:8d35c74403af 4947 // plunger is somewhere in the middle of the travel, the PC will
mjr 87:8d35c74403af 4948 // think that the firing motion *started* in that middle position,
mjr 87:8d35c74403af 4949 // so it won't be able to model the right amount of momentum when
mjr 87:8d35c74403af 4950 // the plunger hits the ball. We try to ensure that the PC sees
mjr 87:8d35c74403af 4951 // the right starting point by reporting the starting point for
mjr 87:8d35c74403af 4952 // extra time during the forward motion. By the same token, we
mjr 87:8d35c74403af 4953 // want the PC to know that the plunger has moved all the way
mjr 87:8d35c74403af 4954 // forward, rather than mistakenly thinking that it stopped
mjr 87:8d35c74403af 4955 // somewhere in the middle of the travel, so we freeze at the
mjr 87:8d35c74403af 4956 // forward position for a short time.
mjr 76:7f5912b6340e 4957 //
mjr 87:8d35c74403af 4958 // To detect a firing event, we look for forward motion that's
mjr 87:8d35c74403af 4959 // fast enough to be a firing event. To determine how fast is
mjr 87:8d35c74403af 4960 // fast enough, we use a simple model of the plunger motion where
mjr 87:8d35c74403af 4961 // the acceleration is constant. This is only an approximation,
mjr 87:8d35c74403af 4962 // as the spring force actually varies with spring's compression,
mjr 87:8d35c74403af 4963 // but it's close enough for our purposes here.
mjr 87:8d35c74403af 4964 //
mjr 87:8d35c74403af 4965 // Do calculations in fixed-point 2^48 scale with 64-bit ints.
mjr 87:8d35c74403af 4966 // acc2 = acceleration/2 for 50ms release time, units of unit
mjr 87:8d35c74403af 4967 // distances per microsecond squared, where the unit distance
mjr 87:8d35c74403af 4968 // is the overall travel from the starting retracted position
mjr 87:8d35c74403af 4969 // to the park position.
mjr 87:8d35c74403af 4970 const int32_t acc2 = 112590; // 2^48 scale
mjr 50:40015764bbe6 4971 switch (firing)
mjr 50:40015764bbe6 4972 {
mjr 50:40015764bbe6 4973 case 0:
mjr 87:8d35c74403af 4974 // Not in firing mode. If we're retracted a bit, and the
mjr 87:8d35c74403af 4975 // motion is forward at a fast enough rate to look like a
mjr 87:8d35c74403af 4976 // release, enter firing mode.
mjr 87:8d35c74403af 4977 if (r.pos > JOYMAX/6)
mjr 50:40015764bbe6 4978 {
mjr 87:8d35c74403af 4979 const uint32_t dt = uint32_t(r.t - prv.t);
mjr 87:8d35c74403af 4980 const uint32_t dt2 = dt*dt; // dt^2
mjr 87:8d35c74403af 4981 if (r.pos < prv.pos - int((prv.pos*acc2*uint64_t(dt2)) >> 48))
mjr 87:8d35c74403af 4982 {
mjr 87:8d35c74403af 4983 // Tentatively enter firing mode. Use the prior reading
mjr 87:8d35c74403af 4984 // as the starting point, and freeze reports for now.
mjr 87:8d35c74403af 4985 firingMode(1);
mjr 87:8d35c74403af 4986 f0 = prv;
mjr 87:8d35c74403af 4987 z = f0.pos;
mjr 87:8d35c74403af 4988
mjr 87:8d35c74403af 4989 // if in calibration state 1 (at rest), switch to
mjr 87:8d35c74403af 4990 // state 2 (not at rest)
mjr 87:8d35c74403af 4991 if (calState == 1)
mjr 87:8d35c74403af 4992 calState = 2;
mjr 87:8d35c74403af 4993 }
mjr 50:40015764bbe6 4994 }
mjr 50:40015764bbe6 4995 break;
mjr 50:40015764bbe6 4996
mjr 50:40015764bbe6 4997 case 1:
mjr 87:8d35c74403af 4998 // Tentative firing mode: the plunger was moving forward
mjr 87:8d35c74403af 4999 // at last check. To stay in firing mode, the plunger has
mjr 87:8d35c74403af 5000 // to keep moving forward fast enough to look like it's
mjr 87:8d35c74403af 5001 // moving under spring force. To figure out how fast is
mjr 87:8d35c74403af 5002 // fast enough, we use a simple model where the acceleration
mjr 87:8d35c74403af 5003 // is constant over the whole travel distance and the total
mjr 87:8d35c74403af 5004 // travel time is 50ms. The acceleration actually varies
mjr 87:8d35c74403af 5005 // slightly since it comes from the spring force, which
mjr 87:8d35c74403af 5006 // is linear in the displacement; but the plunger spring is
mjr 87:8d35c74403af 5007 // fairly compressed even when the plunger is all the way
mjr 87:8d35c74403af 5008 // forward, so the difference in tension from one end of
mjr 87:8d35c74403af 5009 // the travel to the other is fairly small, so it's not too
mjr 87:8d35c74403af 5010 // far off to model it as constant. And the real travel
mjr 87:8d35c74403af 5011 // time obviously isn't a constant, but all we need for
mjr 87:8d35c74403af 5012 // that is an upper bound. So: we'll figure the time since
mjr 87:8d35c74403af 5013 // we entered firing mode, and figure the distance we should
mjr 87:8d35c74403af 5014 // have traveled to complete the trip within the maximum
mjr 87:8d35c74403af 5015 // time allowed. If we've moved far enough, we'll stay
mjr 87:8d35c74403af 5016 // in firing mode; if not, we'll exit firing mode. And if
mjr 87:8d35c74403af 5017 // we cross the finish line while still in firing mode,
mjr 87:8d35c74403af 5018 // we'll switch to the next phase of the firing event.
mjr 50:40015764bbe6 5019 if (r.pos <= 0)
mjr 50:40015764bbe6 5020 {
mjr 87:8d35c74403af 5021 // We crossed the park position. Switch to the second
mjr 87:8d35c74403af 5022 // phase of the firing event, where we hold the reported
mjr 87:8d35c74403af 5023 // position at the "bounce" position (where the plunger
mjr 87:8d35c74403af 5024 // is all the way forward, compressing the barrel spring).
mjr 87:8d35c74403af 5025 // We'll stick here long enough to ensure that the PC
mjr 87:8d35c74403af 5026 // client (Visual Pinball or whatever) sees the reading
mjr 87:8d35c74403af 5027 // and processes the release motion via the simulated
mjr 87:8d35c74403af 5028 // physics.
mjr 50:40015764bbe6 5029 firingMode(2);
mjr 53:9b2611964afc 5030
mjr 53:9b2611964afc 5031 // if in calibration mode, and we're in state 2 (moving),
mjr 53:9b2611964afc 5032 // collect firing statistics for calibration purposes
mjr 53:9b2611964afc 5033 if (plungerCalMode && calState == 2)
mjr 53:9b2611964afc 5034 {
mjr 53:9b2611964afc 5035 // collect a new zero point for the average when we
mjr 53:9b2611964afc 5036 // come to rest
mjr 53:9b2611964afc 5037 calState = 0;
mjr 53:9b2611964afc 5038
mjr 87:8d35c74403af 5039 // collect average firing time statistics in millseconds,
mjr 87:8d35c74403af 5040 // if it's in range (20 to 255 ms)
mjr 87:8d35c74403af 5041 const int dt = uint32_t(r.t - f0.t)/1000UL;
mjr 87:8d35c74403af 5042 if (dt >= 15 && dt <= 255)
mjr 53:9b2611964afc 5043 {
mjr 53:9b2611964afc 5044 calRlsTimeSum += dt;
mjr 53:9b2611964afc 5045 calRlsTimeN += 1;
mjr 53:9b2611964afc 5046 cfg.plunger.cal.tRelease = uint8_t(calRlsTimeSum / calRlsTimeN);
mjr 53:9b2611964afc 5047 }
mjr 53:9b2611964afc 5048 }
mjr 87:8d35c74403af 5049
mjr 87:8d35c74403af 5050 // Figure the "bounce" position as forward of the park
mjr 87:8d35c74403af 5051 // position by 1/6 of the starting retraction distance.
mjr 87:8d35c74403af 5052 // This simulates the momentum of the plunger compressing
mjr 87:8d35c74403af 5053 // the barrel spring on the rebound. The barrel spring
mjr 87:8d35c74403af 5054 // can compress by about 1/6 of the maximum retraction
mjr 87:8d35c74403af 5055 // distance, so we'll simply treat its compression as
mjr 87:8d35c74403af 5056 // proportional to the retraction. (It might be more
mjr 87:8d35c74403af 5057 // realistic to use a slightly higher value here, maybe
mjr 87:8d35c74403af 5058 // 1/4 or 1/3 or the retraction distance, capping it at
mjr 87:8d35c74403af 5059 // a maximum of 1/6, because the real plunger probably
mjr 87:8d35c74403af 5060 // compresses the barrel spring by 100% with less than
mjr 87:8d35c74403af 5061 // 100% retraction. But that won't affect the physics
mjr 87:8d35c74403af 5062 // meaningfully, just the animation, and the effect is
mjr 87:8d35c74403af 5063 // small in any case.)
mjr 87:8d35c74403af 5064 z = f0.pos = -f0.pos / 6;
mjr 87:8d35c74403af 5065
mjr 87:8d35c74403af 5066 // reset the starting time for this phase
mjr 87:8d35c74403af 5067 f0.t = r.t;
mjr 50:40015764bbe6 5068 }
mjr 50:40015764bbe6 5069 else
mjr 50:40015764bbe6 5070 {
mjr 87:8d35c74403af 5071 // check for motion since the start of the firing event
mjr 87:8d35c74403af 5072 const uint32_t dt = uint32_t(r.t - f0.t);
mjr 87:8d35c74403af 5073 const uint32_t dt2 = dt*dt; // dt^2
mjr 87:8d35c74403af 5074 if (dt < 50000
mjr 87:8d35c74403af 5075 && r.pos < f0.pos - int((f0.pos*acc2*uint64_t(dt2)) >> 48))
mjr 87:8d35c74403af 5076 {
mjr 87:8d35c74403af 5077 // It's moving fast enough to still be in a release
mjr 87:8d35c74403af 5078 // motion. Continue reporting the start position, and
mjr 87:8d35c74403af 5079 // stay in the first release phase.
mjr 87:8d35c74403af 5080 z = f0.pos;
mjr 87:8d35c74403af 5081 }
mjr 87:8d35c74403af 5082 else
mjr 87:8d35c74403af 5083 {
mjr 87:8d35c74403af 5084 // It's not moving fast enough to be a release
mjr 87:8d35c74403af 5085 // motion. Return to the default state.
mjr 87:8d35c74403af 5086 firingMode(0);
mjr 87:8d35c74403af 5087 calState = 1;
mjr 87:8d35c74403af 5088 }
mjr 50:40015764bbe6 5089 }
mjr 50:40015764bbe6 5090 break;
mjr 50:40015764bbe6 5091
mjr 50:40015764bbe6 5092 case 2:
mjr 87:8d35c74403af 5093 // Firing mode, holding at forward compression position.
mjr 87:8d35c74403af 5094 // Hold here for 25ms.
mjr 87:8d35c74403af 5095 if (uint32_t(r.t - f0.t) < 25000)
mjr 50:40015764bbe6 5096 {
mjr 87:8d35c74403af 5097 // stay here for now
mjr 87:8d35c74403af 5098 z = f0.pos;
mjr 50:40015764bbe6 5099 }
mjr 50:40015764bbe6 5100 else
mjr 50:40015764bbe6 5101 {
mjr 87:8d35c74403af 5102 // advance to the next phase, where we report the park
mjr 87:8d35c74403af 5103 // position until the plunger comes to rest
mjr 50:40015764bbe6 5104 firingMode(3);
mjr 50:40015764bbe6 5105 z = 0;
mjr 87:8d35c74403af 5106
mjr 87:8d35c74403af 5107 // remember when we started
mjr 87:8d35c74403af 5108 f0.t = r.t;
mjr 50:40015764bbe6 5109 }
mjr 50:40015764bbe6 5110 break;
mjr 50:40015764bbe6 5111
mjr 50:40015764bbe6 5112 case 3:
mjr 87:8d35c74403af 5113 // Firing event, holding at park position. Stay here for
mjr 87:8d35c74403af 5114 // a few moments so that the PC client can simulate the
mjr 87:8d35c74403af 5115 // full release motion, then return to real readings.
mjr 87:8d35c74403af 5116 if (uint32_t(r.t - f0.t) < 250000)
mjr 50:40015764bbe6 5117 {
mjr 87:8d35c74403af 5118 // stay here a while longer
mjr 87:8d35c74403af 5119 z = 0;
mjr 50:40015764bbe6 5120 }
mjr 50:40015764bbe6 5121 else
mjr 50:40015764bbe6 5122 {
mjr 87:8d35c74403af 5123 // it's been long enough - return to normal mode
mjr 87:8d35c74403af 5124 firingMode(0);
mjr 50:40015764bbe6 5125 }
mjr 50:40015764bbe6 5126 break;
mjr 50:40015764bbe6 5127 }
mjr 50:40015764bbe6 5128
mjr 82:4f6209cb5c33 5129 // Check for auto-zeroing, if enabled
mjr 82:4f6209cb5c33 5130 if ((cfg.plunger.autoZero.flags & PlungerAutoZeroEnabled) != 0)
mjr 82:4f6209cb5c33 5131 {
mjr 82:4f6209cb5c33 5132 // If we moved since the last reading, reset and restart the
mjr 82:4f6209cb5c33 5133 // auto-zero timer. Otherwise, if the timer has reached the
mjr 82:4f6209cb5c33 5134 // auto-zero timeout, it means we've been motionless for that
mjr 82:4f6209cb5c33 5135 // long, so auto-zero now.
mjr 82:4f6209cb5c33 5136 if (r.pos != prv.pos)
mjr 82:4f6209cb5c33 5137 {
mjr 82:4f6209cb5c33 5138 // movement detected - reset the timer
mjr 82:4f6209cb5c33 5139 autoZeroTimer.reset();
mjr 82:4f6209cb5c33 5140 autoZeroTimer.start();
mjr 82:4f6209cb5c33 5141 }
mjr 82:4f6209cb5c33 5142 else if (autoZeroTimer.read_us() > cfg.plunger.autoZero.t * 1000000UL)
mjr 82:4f6209cb5c33 5143 {
mjr 82:4f6209cb5c33 5144 // auto-zero now
mjr 82:4f6209cb5c33 5145 plungerSensor->autoZero();
mjr 82:4f6209cb5c33 5146
mjr 82:4f6209cb5c33 5147 // stop the timer so that we don't keep repeating this
mjr 82:4f6209cb5c33 5148 // if the plunger stays still for a long time
mjr 82:4f6209cb5c33 5149 autoZeroTimer.stop();
mjr 82:4f6209cb5c33 5150 autoZeroTimer.reset();
mjr 82:4f6209cb5c33 5151 }
mjr 82:4f6209cb5c33 5152 }
mjr 82:4f6209cb5c33 5153
mjr 87:8d35c74403af 5154 // this new reading becomes the previous reading for next time
mjr 87:8d35c74403af 5155 prv = r;
mjr 48:058ace2aed1d 5156 }
mjr 48:058ace2aed1d 5157 }
mjr 48:058ace2aed1d 5158
mjr 48:058ace2aed1d 5159 // Get the current value to report through the joystick interface
mjr 58:523fdcffbe6d 5160 int16_t getPosition()
mjr 58:523fdcffbe6d 5161 {
mjr 86:e30a1f60f783 5162 // return the last reading
mjr 86:e30a1f60f783 5163 return z;
mjr 55:4db125cd11a0 5164 }
mjr 58:523fdcffbe6d 5165
mjr 48:058ace2aed1d 5166 // Set calibration mode on or off
mjr 52:8298b2a73eb2 5167 void setCalMode(bool f)
mjr 48:058ace2aed1d 5168 {
mjr 52:8298b2a73eb2 5169 // check to see if we're entering calibration mode
mjr 52:8298b2a73eb2 5170 if (f && !plungerCalMode)
mjr 52:8298b2a73eb2 5171 {
mjr 52:8298b2a73eb2 5172 // reset the calibration in the configuration
mjr 48:058ace2aed1d 5173 cfg.plunger.cal.begin();
mjr 52:8298b2a73eb2 5174
mjr 52:8298b2a73eb2 5175 // start in state 0 (waiting to settle)
mjr 52:8298b2a73eb2 5176 calState = 0;
mjr 52:8298b2a73eb2 5177 calZeroPosSum = 0;
mjr 52:8298b2a73eb2 5178 calZeroPosN = 0;
mjr 52:8298b2a73eb2 5179 calRlsTimeSum = 0;
mjr 52:8298b2a73eb2 5180 calRlsTimeN = 0;
mjr 52:8298b2a73eb2 5181
mjr 82:4f6209cb5c33 5182 // tell the plunger we're starting calibration
mjr 82:4f6209cb5c33 5183 plungerSensor->beginCalibration();
mjr 82:4f6209cb5c33 5184
mjr 52:8298b2a73eb2 5185 // set the initial zero point to the current position
mjr 52:8298b2a73eb2 5186 PlungerReading r;
mjr 52:8298b2a73eb2 5187 if (plungerSensor->read(r))
mjr 52:8298b2a73eb2 5188 {
mjr 52:8298b2a73eb2 5189 // got a reading - use it as the initial zero point
mjr 52:8298b2a73eb2 5190 cfg.plunger.cal.zero = r.pos;
mjr 76:7f5912b6340e 5191 onUpdateCal();
mjr 52:8298b2a73eb2 5192
mjr 52:8298b2a73eb2 5193 // use it as the starting point for the settling watch
mjr 53:9b2611964afc 5194 calZeroStart = r;
mjr 52:8298b2a73eb2 5195 }
mjr 52:8298b2a73eb2 5196 else
mjr 52:8298b2a73eb2 5197 {
mjr 52:8298b2a73eb2 5198 // no reading available - use the default 1/6 position
mjr 52:8298b2a73eb2 5199 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 5200 onUpdateCal();
mjr 52:8298b2a73eb2 5201
mjr 52:8298b2a73eb2 5202 // we don't have a starting point for the setting watch
mjr 53:9b2611964afc 5203 calZeroStart.pos = -65535;
mjr 53:9b2611964afc 5204 calZeroStart.t = 0;
mjr 53:9b2611964afc 5205 }
mjr 53:9b2611964afc 5206 }
mjr 53:9b2611964afc 5207 else if (!f && plungerCalMode)
mjr 53:9b2611964afc 5208 {
mjr 53:9b2611964afc 5209 // Leaving calibration mode. Make sure the max is past the
mjr 53:9b2611964afc 5210 // zero point - if it's not, we'd have a zero or negative
mjr 53:9b2611964afc 5211 // denominator for the scaling calculation, which would be
mjr 53:9b2611964afc 5212 // physically meaningless.
mjr 53:9b2611964afc 5213 if (cfg.plunger.cal.max <= cfg.plunger.cal.zero)
mjr 53:9b2611964afc 5214 {
mjr 53:9b2611964afc 5215 // bad settings - reset to defaults
mjr 53:9b2611964afc 5216 cfg.plunger.cal.max = 0xffff;
mjr 53:9b2611964afc 5217 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 5218 onUpdateCal();
mjr 52:8298b2a73eb2 5219 }
mjr 52:8298b2a73eb2 5220 }
mjr 52:8298b2a73eb2 5221
mjr 48:058ace2aed1d 5222 // remember the new mode
mjr 52:8298b2a73eb2 5223 plungerCalMode = f;
mjr 48:058ace2aed1d 5224 }
mjr 48:058ace2aed1d 5225
mjr 76:7f5912b6340e 5226 // Cached inverse of the calibration range. This is for calculating
mjr 76:7f5912b6340e 5227 // the calibrated plunger position given a raw sensor reading. The
mjr 76:7f5912b6340e 5228 // cached inverse is calculated as
mjr 76:7f5912b6340e 5229 //
mjr 76:7f5912b6340e 5230 // 64K * JOYMAX / (cfg.plunger.cal.max - cfg.plunger.cal.zero)
mjr 76:7f5912b6340e 5231 //
mjr 76:7f5912b6340e 5232 // To convert a raw sensor reading to a calibrated position, calculate
mjr 76:7f5912b6340e 5233 //
mjr 76:7f5912b6340e 5234 // ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16
mjr 76:7f5912b6340e 5235 //
mjr 76:7f5912b6340e 5236 // That yields the calibration result without performing a division.
mjr 76:7f5912b6340e 5237 int invCalRange;
mjr 76:7f5912b6340e 5238
mjr 76:7f5912b6340e 5239 // apply the calibration range to a reading
mjr 76:7f5912b6340e 5240 inline int applyCal(int reading)
mjr 76:7f5912b6340e 5241 {
mjr 76:7f5912b6340e 5242 return ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16;
mjr 76:7f5912b6340e 5243 }
mjr 76:7f5912b6340e 5244
mjr 76:7f5912b6340e 5245 void onUpdateCal()
mjr 76:7f5912b6340e 5246 {
mjr 76:7f5912b6340e 5247 invCalRange = (JOYMAX << 16)/(cfg.plunger.cal.max - cfg.plunger.cal.zero);
mjr 76:7f5912b6340e 5248 }
mjr 76:7f5912b6340e 5249
mjr 48:058ace2aed1d 5250 // is a firing event in progress?
mjr 53:9b2611964afc 5251 bool isFiring() { return firing == 3; }
mjr 76:7f5912b6340e 5252
mjr 48:058ace2aed1d 5253 private:
mjr 87:8d35c74403af 5254 // current reported joystick reading
mjr 87:8d35c74403af 5255 int z;
mjr 87:8d35c74403af 5256
mjr 87:8d35c74403af 5257 // previous reading
mjr 87:8d35c74403af 5258 PlungerReading prv;
mjr 87:8d35c74403af 5259
mjr 52:8298b2a73eb2 5260 // Calibration state. During calibration mode, we watch for release
mjr 52:8298b2a73eb2 5261 // events, to measure the time it takes to complete the release
mjr 52:8298b2a73eb2 5262 // motion; and we watch for the plunger to come to reset after a
mjr 52:8298b2a73eb2 5263 // release, to gather statistics on the rest position.
mjr 52:8298b2a73eb2 5264 // 0 = waiting to settle
mjr 52:8298b2a73eb2 5265 // 1 = at rest
mjr 52:8298b2a73eb2 5266 // 2 = retracting
mjr 52:8298b2a73eb2 5267 // 3 = possibly releasing
mjr 52:8298b2a73eb2 5268 uint8_t calState;
mjr 52:8298b2a73eb2 5269
mjr 52:8298b2a73eb2 5270 // Calibration zero point statistics.
mjr 52:8298b2a73eb2 5271 // During calibration mode, we collect data on the rest position (the
mjr 52:8298b2a73eb2 5272 // zero point) by watching for the plunger to come to rest after each
mjr 52:8298b2a73eb2 5273 // release. We average these rest positions to get the calibrated
mjr 52:8298b2a73eb2 5274 // zero point. We use the average because the real physical plunger
mjr 52:8298b2a73eb2 5275 // itself doesn't come to rest at exactly the same spot every time,
mjr 52:8298b2a73eb2 5276 // largely due to friction in the mechanism. To calculate the average,
mjr 52:8298b2a73eb2 5277 // we keep a sum of the readings and a count of samples.
mjr 53:9b2611964afc 5278 PlungerReading calZeroStart;
mjr 52:8298b2a73eb2 5279 long calZeroPosSum;
mjr 52:8298b2a73eb2 5280 int calZeroPosN;
mjr 52:8298b2a73eb2 5281
mjr 52:8298b2a73eb2 5282 // Calibration release time statistics.
mjr 52:8298b2a73eb2 5283 // During calibration, we collect an average for the release time.
mjr 52:8298b2a73eb2 5284 long calRlsTimeSum;
mjr 52:8298b2a73eb2 5285 int calRlsTimeN;
mjr 52:8298b2a73eb2 5286
mjr 85:3c28aee81cde 5287 // Auto-zeroing timer
mjr 85:3c28aee81cde 5288 Timer autoZeroTimer;
mjr 85:3c28aee81cde 5289
mjr 48:058ace2aed1d 5290 // set a firing mode
mjr 48:058ace2aed1d 5291 inline void firingMode(int m)
mjr 48:058ace2aed1d 5292 {
mjr 48:058ace2aed1d 5293 firing = m;
mjr 48:058ace2aed1d 5294 }
mjr 48:058ace2aed1d 5295
mjr 48:058ace2aed1d 5296 // Firing event state.
mjr 48:058ace2aed1d 5297 //
mjr 87:8d35c74403af 5298 // 0 - Default state: not in firing event. We report the true
mjr 87:8d35c74403af 5299 // instantaneous plunger position to the joystick interface.
mjr 48:058ace2aed1d 5300 //
mjr 87:8d35c74403af 5301 // 1 - Moving forward at release speed
mjr 48:058ace2aed1d 5302 //
mjr 87:8d35c74403af 5303 // 2 - Firing - reporting the bounce position
mjr 87:8d35c74403af 5304 //
mjr 87:8d35c74403af 5305 // 3 - Firing - reporting the park position
mjr 48:058ace2aed1d 5306 //
mjr 48:058ace2aed1d 5307 int firing;
mjr 48:058ace2aed1d 5308
mjr 87:8d35c74403af 5309 // Starting position for current firing mode phase
mjr 87:8d35c74403af 5310 PlungerReading f0;
mjr 48:058ace2aed1d 5311 };
mjr 48:058ace2aed1d 5312
mjr 48:058ace2aed1d 5313 // plunger reader singleton
mjr 48:058ace2aed1d 5314 PlungerReader plungerReader;
mjr 48:058ace2aed1d 5315
mjr 48:058ace2aed1d 5316 // ---------------------------------------------------------------------------
mjr 48:058ace2aed1d 5317 //
mjr 48:058ace2aed1d 5318 // Handle the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5319 //
mjr 48:058ace2aed1d 5320 // The ZB Launch Ball feature, if enabled, lets the mechanical plunger
mjr 48:058ace2aed1d 5321 // serve as a substitute for a physical Launch Ball button. When a table
mjr 48:058ace2aed1d 5322 // is loaded in VP, and the table has the ZB Launch Ball LedWiz port
mjr 48:058ace2aed1d 5323 // turned on, we'll disable mechanical plunger reports through the
mjr 48:058ace2aed1d 5324 // joystick interface and instead use the plunger only to simulate the
mjr 48:058ace2aed1d 5325 // Launch Ball button. When the mode is active, pulling back and
mjr 48:058ace2aed1d 5326 // releasing the plunger causes a brief simulated press of the Launch
mjr 48:058ace2aed1d 5327 // button, and pushing the plunger forward of the rest position presses
mjr 48:058ace2aed1d 5328 // the Launch button as long as the plunger is pressed forward.
mjr 48:058ace2aed1d 5329 //
mjr 48:058ace2aed1d 5330 // This feature has two configuration components:
mjr 48:058ace2aed1d 5331 //
mjr 48:058ace2aed1d 5332 // - An LedWiz port number. This port is a "virtual" port that doesn't
mjr 48:058ace2aed1d 5333 // have to be attached to any actual output. DOF uses it to signal
mjr 48:058ace2aed1d 5334 // that the current table uses a Launch button instead of a plunger.
mjr 48:058ace2aed1d 5335 // DOF simply turns the port on when such a table is loaded and turns
mjr 48:058ace2aed1d 5336 // it off at all other times. We use it to enable and disable the
mjr 48:058ace2aed1d 5337 // plunger/launch button connection.
mjr 48:058ace2aed1d 5338 //
mjr 48:058ace2aed1d 5339 // - A joystick button ID. We simulate pressing this button when the
mjr 48:058ace2aed1d 5340 // launch feature is activated via the LedWiz port and the plunger is
mjr 48:058ace2aed1d 5341 // either pulled back and releasd, or pushed forward past the rest
mjr 48:058ace2aed1d 5342 // position.
mjr 48:058ace2aed1d 5343 //
mjr 48:058ace2aed1d 5344 class ZBLaunchBall
mjr 48:058ace2aed1d 5345 {
mjr 48:058ace2aed1d 5346 public:
mjr 48:058ace2aed1d 5347 ZBLaunchBall()
mjr 48:058ace2aed1d 5348 {
mjr 48:058ace2aed1d 5349 // start in the default state
mjr 48:058ace2aed1d 5350 lbState = 0;
mjr 53:9b2611964afc 5351 btnState = false;
mjr 48:058ace2aed1d 5352 }
mjr 48:058ace2aed1d 5353
mjr 48:058ace2aed1d 5354 // Update state. This checks the current plunger position and
mjr 48:058ace2aed1d 5355 // the timers to see if the plunger is in a position that simulates
mjr 48:058ace2aed1d 5356 // a Launch Ball button press via the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5357 // Updates the simulated button vector according to the current
mjr 48:058ace2aed1d 5358 // launch ball state. The main loop calls this before each
mjr 48:058ace2aed1d 5359 // joystick update to figure the new simulated button state.
mjr 53:9b2611964afc 5360 void update()
mjr 48:058ace2aed1d 5361 {
mjr 53:9b2611964afc 5362 // If the ZB Launch Ball led wiz output is ON, check for a
mjr 53:9b2611964afc 5363 // plunger firing event
mjr 53:9b2611964afc 5364 if (zbLaunchOn)
mjr 48:058ace2aed1d 5365 {
mjr 53:9b2611964afc 5366 // note the new position
mjr 48:058ace2aed1d 5367 int znew = plungerReader.getPosition();
mjr 53:9b2611964afc 5368
mjr 53:9b2611964afc 5369 // figure the push threshold from the configuration data
mjr 51:57eb311faafa 5370 const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
mjr 53:9b2611964afc 5371
mjr 53:9b2611964afc 5372 // check the state
mjr 48:058ace2aed1d 5373 switch (lbState)
mjr 48:058ace2aed1d 5374 {
mjr 48:058ace2aed1d 5375 case 0:
mjr 53:9b2611964afc 5376 // Default state. If a launch event has been detected on
mjr 53:9b2611964afc 5377 // the plunger, activate a timed pulse and switch to state 1.
mjr 53:9b2611964afc 5378 // If the plunger is pushed forward of the threshold, push
mjr 53:9b2611964afc 5379 // the button.
mjr 53:9b2611964afc 5380 if (plungerReader.isFiring())
mjr 53:9b2611964afc 5381 {
mjr 53:9b2611964afc 5382 // firing event - start a timed Launch button pulse
mjr 53:9b2611964afc 5383 lbTimer.reset();
mjr 53:9b2611964afc 5384 lbTimer.start();
mjr 53:9b2611964afc 5385 setButton(true);
mjr 53:9b2611964afc 5386
mjr 53:9b2611964afc 5387 // switch to state 1
mjr 53:9b2611964afc 5388 lbState = 1;
mjr 53:9b2611964afc 5389 }
mjr 48:058ace2aed1d 5390 else if (znew <= pushThreshold)
mjr 53:9b2611964afc 5391 {
mjr 53:9b2611964afc 5392 // pushed forward without a firing event - hold the
mjr 53:9b2611964afc 5393 // button as long as we're pushed forward
mjr 53:9b2611964afc 5394 setButton(true);
mjr 53:9b2611964afc 5395 }
mjr 53:9b2611964afc 5396 else
mjr 53:9b2611964afc 5397 {
mjr 53:9b2611964afc 5398 // not pushed forward - turn off the Launch button
mjr 53:9b2611964afc 5399 setButton(false);
mjr 53:9b2611964afc 5400 }
mjr 48:058ace2aed1d 5401 break;
mjr 48:058ace2aed1d 5402
mjr 48:058ace2aed1d 5403 case 1:
mjr 53:9b2611964afc 5404 // State 1: Timed Launch button pulse in progress after a
mjr 53:9b2611964afc 5405 // firing event. Wait for the timer to expire.
mjr 53:9b2611964afc 5406 if (lbTimer.read_us() > 200000UL)
mjr 53:9b2611964afc 5407 {
mjr 53:9b2611964afc 5408 // timer expired - turn off the button
mjr 53:9b2611964afc 5409 setButton(false);
mjr 53:9b2611964afc 5410
mjr 53:9b2611964afc 5411 // switch to state 2
mjr 53:9b2611964afc 5412 lbState = 2;
mjr 53:9b2611964afc 5413 }
mjr 48:058ace2aed1d 5414 break;
mjr 48:058ace2aed1d 5415
mjr 48:058ace2aed1d 5416 case 2:
mjr 53:9b2611964afc 5417 // State 2: Timed Launch button pulse done. Wait for the
mjr 53:9b2611964afc 5418 // plunger launch event to end.
mjr 53:9b2611964afc 5419 if (!plungerReader.isFiring())
mjr 53:9b2611964afc 5420 {
mjr 53:9b2611964afc 5421 // firing event done - return to default state
mjr 53:9b2611964afc 5422 lbState = 0;
mjr 53:9b2611964afc 5423 }
mjr 48:058ace2aed1d 5424 break;
mjr 48:058ace2aed1d 5425 }
mjr 53:9b2611964afc 5426 }
mjr 53:9b2611964afc 5427 else
mjr 53:9b2611964afc 5428 {
mjr 53:9b2611964afc 5429 // ZB Launch Ball disabled - turn off the button if it was on
mjr 53:9b2611964afc 5430 setButton(false);
mjr 48:058ace2aed1d 5431
mjr 53:9b2611964afc 5432 // return to the default state
mjr 53:9b2611964afc 5433 lbState = 0;
mjr 48:058ace2aed1d 5434 }
mjr 48:058ace2aed1d 5435 }
mjr 53:9b2611964afc 5436
mjr 53:9b2611964afc 5437 // Set the button state
mjr 53:9b2611964afc 5438 void setButton(bool on)
mjr 53:9b2611964afc 5439 {
mjr 53:9b2611964afc 5440 if (btnState != on)
mjr 53:9b2611964afc 5441 {
mjr 53:9b2611964afc 5442 // remember the new state
mjr 53:9b2611964afc 5443 btnState = on;
mjr 53:9b2611964afc 5444
mjr 53:9b2611964afc 5445 // update the virtual button state
mjr 65:739875521aae 5446 buttonState[zblButtonIndex].virtPress(on);
mjr 53:9b2611964afc 5447 }
mjr 53:9b2611964afc 5448 }
mjr 53:9b2611964afc 5449
mjr 48:058ace2aed1d 5450 private:
mjr 48:058ace2aed1d 5451 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 48:058ace2aed1d 5452 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 48:058ace2aed1d 5453 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 48:058ace2aed1d 5454 // back and releases the plunger, or simply pushes on the plunger from
mjr 48:058ace2aed1d 5455 // the rest position. This allows the plunger to be used in lieu of a
mjr 48:058ace2aed1d 5456 // physical Launch Ball button for tables that don't have plungers.
mjr 48:058ace2aed1d 5457 //
mjr 48:058ace2aed1d 5458 // States:
mjr 48:058ace2aed1d 5459 // 0 = default
mjr 53:9b2611964afc 5460 // 1 = firing (firing event has activated a Launch button pulse)
mjr 53:9b2611964afc 5461 // 2 = firing done (Launch button pulse ended, waiting for plunger
mjr 53:9b2611964afc 5462 // firing event to end)
mjr 53:9b2611964afc 5463 uint8_t lbState;
mjr 48:058ace2aed1d 5464
mjr 53:9b2611964afc 5465 // button state
mjr 53:9b2611964afc 5466 bool btnState;
mjr 48:058ace2aed1d 5467
mjr 48:058ace2aed1d 5468 // Time since last lbState transition. Some of the states are time-
mjr 48:058ace2aed1d 5469 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 48:058ace2aed1d 5470 // we remain in this state for more than a few milliseconds, since
mjr 48:058ace2aed1d 5471 // it indicates that the plunger is being slowly returned to rest
mjr 48:058ace2aed1d 5472 // rather than released. In the "launching" state, we need to release
mjr 48:058ace2aed1d 5473 // the Launch Ball button after a moment, and we need to wait for
mjr 48:058ace2aed1d 5474 // the plunger to come to rest before returning to state 0.
mjr 48:058ace2aed1d 5475 Timer lbTimer;
mjr 48:058ace2aed1d 5476 };
mjr 48:058ace2aed1d 5477
mjr 35:e959ffba78fd 5478 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5479 //
mjr 35:e959ffba78fd 5480 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 5481 //
mjr 54:fd77a6b2f76c 5482 void reboot(USBJoystick &js, bool disconnect = true, long pause_us = 2000000L)
mjr 35:e959ffba78fd 5483 {
mjr 35:e959ffba78fd 5484 // disconnect from USB
mjr 54:fd77a6b2f76c 5485 if (disconnect)
mjr 54:fd77a6b2f76c 5486 js.disconnect();
mjr 35:e959ffba78fd 5487
mjr 35:e959ffba78fd 5488 // wait a few seconds to make sure the host notices the disconnect
mjr 54:fd77a6b2f76c 5489 wait_us(pause_us);
mjr 35:e959ffba78fd 5490
mjr 35:e959ffba78fd 5491 // reset the device
mjr 35:e959ffba78fd 5492 NVIC_SystemReset();
mjr 35:e959ffba78fd 5493 while (true) { }
mjr 35:e959ffba78fd 5494 }
mjr 35:e959ffba78fd 5495
mjr 35:e959ffba78fd 5496 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5497 //
mjr 35:e959ffba78fd 5498 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 5499 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 5500 //
mjr 35:e959ffba78fd 5501 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 5502 {
mjr 35:e959ffba78fd 5503 int tmp;
mjr 78:1e00b3fa11af 5504 switch (cfg.accel.orientation)
mjr 35:e959ffba78fd 5505 {
mjr 35:e959ffba78fd 5506 case OrientationFront:
mjr 35:e959ffba78fd 5507 tmp = x;
mjr 35:e959ffba78fd 5508 x = y;
mjr 35:e959ffba78fd 5509 y = tmp;
mjr 35:e959ffba78fd 5510 break;
mjr 35:e959ffba78fd 5511
mjr 35:e959ffba78fd 5512 case OrientationLeft:
mjr 35:e959ffba78fd 5513 x = -x;
mjr 35:e959ffba78fd 5514 break;
mjr 35:e959ffba78fd 5515
mjr 35:e959ffba78fd 5516 case OrientationRight:
mjr 35:e959ffba78fd 5517 y = -y;
mjr 35:e959ffba78fd 5518 break;
mjr 35:e959ffba78fd 5519
mjr 35:e959ffba78fd 5520 case OrientationRear:
mjr 35:e959ffba78fd 5521 tmp = -x;
mjr 35:e959ffba78fd 5522 x = -y;
mjr 35:e959ffba78fd 5523 y = tmp;
mjr 35:e959ffba78fd 5524 break;
mjr 35:e959ffba78fd 5525 }
mjr 35:e959ffba78fd 5526 }
mjr 35:e959ffba78fd 5527
mjr 35:e959ffba78fd 5528 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5529 //
mjr 35:e959ffba78fd 5530 // Calibration button state:
mjr 35:e959ffba78fd 5531 // 0 = not pushed
mjr 35:e959ffba78fd 5532 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 5533 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 5534 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 5535 int calBtnState = 0;
mjr 35:e959ffba78fd 5536
mjr 35:e959ffba78fd 5537 // calibration button debounce timer
mjr 35:e959ffba78fd 5538 Timer calBtnTimer;
mjr 35:e959ffba78fd 5539
mjr 35:e959ffba78fd 5540 // calibration button light state
mjr 35:e959ffba78fd 5541 int calBtnLit = false;
mjr 35:e959ffba78fd 5542
mjr 35:e959ffba78fd 5543
mjr 35:e959ffba78fd 5544 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5545 //
mjr 40:cc0d9814522b 5546 // Configuration variable get/set message handling
mjr 35:e959ffba78fd 5547 //
mjr 40:cc0d9814522b 5548
mjr 40:cc0d9814522b 5549 // Handle SET messages - write configuration variables from USB message data
mjr 40:cc0d9814522b 5550 #define if_msg_valid(test) if (test)
mjr 53:9b2611964afc 5551 #define v_byte(var, ofs) cfg.var = data[ofs]
mjr 53:9b2611964afc 5552 #define v_ui16(var, ofs) cfg.var = wireUI16(data+(ofs))
mjr 77:0b96f6867312 5553 #define v_ui32(var, ofs) cfg.var = wireUI32(data+(ofs))
mjr 53:9b2611964afc 5554 #define v_pin(var, ofs) cfg.var = wirePinName(data[ofs])
mjr 53:9b2611964afc 5555 #define v_byte_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 5556 #define v_ui32_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 5557 #define VAR_MODE_SET 1 // we're in SET mode
mjr 76:7f5912b6340e 5558 #define v_func configVarSet(const uint8_t *data)
mjr 40:cc0d9814522b 5559 #include "cfgVarMsgMap.h"
mjr 35:e959ffba78fd 5560
mjr 40:cc0d9814522b 5561 // redefine everything for the SET messages
mjr 40:cc0d9814522b 5562 #undef if_msg_valid
mjr 40:cc0d9814522b 5563 #undef v_byte
mjr 40:cc0d9814522b 5564 #undef v_ui16
mjr 77:0b96f6867312 5565 #undef v_ui32
mjr 40:cc0d9814522b 5566 #undef v_pin
mjr 53:9b2611964afc 5567 #undef v_byte_ro
mjr 74:822a92bc11d2 5568 #undef v_ui32_ro
mjr 74:822a92bc11d2 5569 #undef VAR_MODE_SET
mjr 40:cc0d9814522b 5570 #undef v_func
mjr 38:091e511ce8a0 5571
mjr 40:cc0d9814522b 5572 // Handle GET messages - read variable values and return in USB message daa
mjr 40:cc0d9814522b 5573 #define if_msg_valid(test)
mjr 53:9b2611964afc 5574 #define v_byte(var, ofs) data[ofs] = cfg.var
mjr 53:9b2611964afc 5575 #define v_ui16(var, ofs) ui16Wire(data+(ofs), cfg.var)
mjr 77:0b96f6867312 5576 #define v_ui32(var, ofs) ui32Wire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 5577 #define v_pin(var, ofs) pinNameWire(data+(ofs), cfg.var)
mjr 73:4e8ce0b18915 5578 #define v_byte_ro(val, ofs) data[ofs] = (val)
mjr 74:822a92bc11d2 5579 #define v_ui32_ro(val, ofs) ui32Wire(data+(ofs), val);
mjr 74:822a92bc11d2 5580 #define VAR_MODE_SET 0 // we're in GET mode
mjr 76:7f5912b6340e 5581 #define v_func configVarGet(uint8_t *data)
mjr 40:cc0d9814522b 5582 #include "cfgVarMsgMap.h"
mjr 40:cc0d9814522b 5583
mjr 35:e959ffba78fd 5584
mjr 35:e959ffba78fd 5585 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5586 //
mjr 35:e959ffba78fd 5587 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 5588 // LedWiz protocol.
mjr 33:d832bcab089e 5589 //
mjr 78:1e00b3fa11af 5590 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js, Accel &accel)
mjr 35:e959ffba78fd 5591 {
mjr 38:091e511ce8a0 5592 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 5593 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 5594 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 5595 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 5596 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 5597 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 5598 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 5599 // So our full protocol is as follows:
mjr 38:091e511ce8a0 5600 //
mjr 38:091e511ce8a0 5601 // first byte =
mjr 74:822a92bc11d2 5602 // 0-48 -> PBA
mjr 74:822a92bc11d2 5603 // 64 -> SBA
mjr 38:091e511ce8a0 5604 // 65 -> private control message; second byte specifies subtype
mjr 74:822a92bc11d2 5605 // 129-132 -> PBA
mjr 38:091e511ce8a0 5606 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 5607 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 5608 // other -> reserved for future use
mjr 38:091e511ce8a0 5609 //
mjr 39:b3815a1c3802 5610 uint8_t *data = lwm.data;
mjr 74:822a92bc11d2 5611 if (data[0] == 64)
mjr 35:e959ffba78fd 5612 {
mjr 74:822a92bc11d2 5613 // 64 = SBA (original LedWiz command to set on/off switches for ports 1-32)
mjr 74:822a92bc11d2 5614 //printf("SBA %02x %02x %02x %02x, speed %02x\r\n",
mjr 38:091e511ce8a0 5615 // data[1], data[2], data[3], data[4], data[5]);
mjr 74:822a92bc11d2 5616 sba_sbx(0, data);
mjr 74:822a92bc11d2 5617
mjr 74:822a92bc11d2 5618 // SBA resets the PBA port group counter
mjr 38:091e511ce8a0 5619 pbaIdx = 0;
mjr 38:091e511ce8a0 5620 }
mjr 38:091e511ce8a0 5621 else if (data[0] == 65)
mjr 38:091e511ce8a0 5622 {
mjr 38:091e511ce8a0 5623 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 5624 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 5625 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 5626 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 5627 // message type.
mjr 39:b3815a1c3802 5628 switch (data[1])
mjr 38:091e511ce8a0 5629 {
mjr 39:b3815a1c3802 5630 case 0:
mjr 39:b3815a1c3802 5631 // No Op
mjr 39:b3815a1c3802 5632 break;
mjr 39:b3815a1c3802 5633
mjr 39:b3815a1c3802 5634 case 1:
mjr 38:091e511ce8a0 5635 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 5636 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 5637 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 5638 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 5639 {
mjr 39:b3815a1c3802 5640
mjr 39:b3815a1c3802 5641 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 5642 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 5643 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 5644
mjr 86:e30a1f60f783 5645 // we'll need a reboot if the LedWiz unit number is changing
mjr 86:e30a1f60f783 5646 bool reboot = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 5647
mjr 39:b3815a1c3802 5648 // set the configuration parameters from the message
mjr 39:b3815a1c3802 5649 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 5650 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 5651
mjr 77:0b96f6867312 5652 // set the flag to do the save
mjr 86:e30a1f60f783 5653 saveConfigToFlash(0, reboot);
mjr 39:b3815a1c3802 5654 }
mjr 39:b3815a1c3802 5655 break;
mjr 38:091e511ce8a0 5656
mjr 39:b3815a1c3802 5657 case 2:
mjr 38:091e511ce8a0 5658 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 5659 // (No parameters)
mjr 38:091e511ce8a0 5660
mjr 38:091e511ce8a0 5661 // enter calibration mode
mjr 38:091e511ce8a0 5662 calBtnState = 3;
mjr 52:8298b2a73eb2 5663 plungerReader.setCalMode(true);
mjr 38:091e511ce8a0 5664 calBtnTimer.reset();
mjr 39:b3815a1c3802 5665 break;
mjr 39:b3815a1c3802 5666
mjr 39:b3815a1c3802 5667 case 3:
mjr 52:8298b2a73eb2 5668 // 3 = plunger sensor status report
mjr 48:058ace2aed1d 5669 // data[2] = flag bits
mjr 53:9b2611964afc 5670 // data[3] = extra exposure time, 100us (.1ms) increments
mjr 52:8298b2a73eb2 5671 reportPlungerStat = true;
mjr 53:9b2611964afc 5672 reportPlungerStatFlags = data[2];
mjr 53:9b2611964afc 5673 reportPlungerStatTime = data[3];
mjr 38:091e511ce8a0 5674
mjr 38:091e511ce8a0 5675 // show purple until we finish sending the report
mjr 38:091e511ce8a0 5676 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 5677 break;
mjr 39:b3815a1c3802 5678
mjr 39:b3815a1c3802 5679 case 4:
mjr 38:091e511ce8a0 5680 // 4 = hardware configuration query
mjr 38:091e511ce8a0 5681 // (No parameters)
mjr 38:091e511ce8a0 5682 js.reportConfig(
mjr 38:091e511ce8a0 5683 numOutputs,
mjr 38:091e511ce8a0 5684 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 52:8298b2a73eb2 5685 cfg.plunger.cal.zero, cfg.plunger.cal.max, cfg.plunger.cal.tRelease,
mjr 75:677892300e7a 5686 nvm.valid(), // a config is loaded if the config memory block is valid
mjr 75:677892300e7a 5687 true, // we support sbx/pbx extensions
mjr 78:1e00b3fa11af 5688 true, // we support the new accelerometer settings
mjr 82:4f6209cb5c33 5689 true, // we support the "flash write ok" status bit in joystick reports
mjr 79:682ae3171a08 5690 mallocBytesFree()); // remaining memory size
mjr 39:b3815a1c3802 5691 break;
mjr 39:b3815a1c3802 5692
mjr 39:b3815a1c3802 5693 case 5:
mjr 38:091e511ce8a0 5694 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 5695 allOutputsOff();
mjr 39:b3815a1c3802 5696 break;
mjr 39:b3815a1c3802 5697
mjr 39:b3815a1c3802 5698 case 6:
mjr 85:3c28aee81cde 5699 // 6 = Save configuration to flash. Optionally reboot after the
mjr 85:3c28aee81cde 5700 // delay time in seconds given in data[2].
mjr 85:3c28aee81cde 5701 //
mjr 85:3c28aee81cde 5702 // data[2] = delay time in seconds
mjr 85:3c28aee81cde 5703 // data[3] = flags:
mjr 85:3c28aee81cde 5704 // 0x01 -> do not reboot
mjr 86:e30a1f60f783 5705 saveConfigToFlash(data[2], !(data[3] & 0x01));
mjr 39:b3815a1c3802 5706 break;
mjr 40:cc0d9814522b 5707
mjr 40:cc0d9814522b 5708 case 7:
mjr 40:cc0d9814522b 5709 // 7 = Device ID report
mjr 53:9b2611964afc 5710 // data[2] = ID index: 1=CPU ID, 2=OpenSDA TUID
mjr 53:9b2611964afc 5711 js.reportID(data[2]);
mjr 40:cc0d9814522b 5712 break;
mjr 40:cc0d9814522b 5713
mjr 40:cc0d9814522b 5714 case 8:
mjr 40:cc0d9814522b 5715 // 8 = Engage/disengage night mode.
mjr 40:cc0d9814522b 5716 // data[2] = 1 to engage, 0 to disengage
mjr 40:cc0d9814522b 5717 setNightMode(data[2]);
mjr 40:cc0d9814522b 5718 break;
mjr 52:8298b2a73eb2 5719
mjr 52:8298b2a73eb2 5720 case 9:
mjr 52:8298b2a73eb2 5721 // 9 = Config variable query.
mjr 52:8298b2a73eb2 5722 // data[2] = config var ID
mjr 52:8298b2a73eb2 5723 // data[3] = array index (for array vars: button assignments, output ports)
mjr 52:8298b2a73eb2 5724 {
mjr 53:9b2611964afc 5725 // set up the reply buffer with the variable ID data, and zero out
mjr 53:9b2611964afc 5726 // the rest of the buffer
mjr 52:8298b2a73eb2 5727 uint8_t reply[8];
mjr 52:8298b2a73eb2 5728 reply[1] = data[2];
mjr 52:8298b2a73eb2 5729 reply[2] = data[3];
mjr 53:9b2611964afc 5730 memset(reply+3, 0, sizeof(reply)-3);
mjr 52:8298b2a73eb2 5731
mjr 52:8298b2a73eb2 5732 // query the value
mjr 52:8298b2a73eb2 5733 configVarGet(reply);
mjr 52:8298b2a73eb2 5734
mjr 52:8298b2a73eb2 5735 // send the reply
mjr 52:8298b2a73eb2 5736 js.reportConfigVar(reply + 1);
mjr 52:8298b2a73eb2 5737 }
mjr 52:8298b2a73eb2 5738 break;
mjr 53:9b2611964afc 5739
mjr 53:9b2611964afc 5740 case 10:
mjr 53:9b2611964afc 5741 // 10 = Build ID query.
mjr 53:9b2611964afc 5742 js.reportBuildInfo(getBuildID());
mjr 53:9b2611964afc 5743 break;
mjr 73:4e8ce0b18915 5744
mjr 73:4e8ce0b18915 5745 case 11:
mjr 73:4e8ce0b18915 5746 // 11 = TV ON relay control.
mjr 73:4e8ce0b18915 5747 // data[2] = operation:
mjr 73:4e8ce0b18915 5748 // 0 = turn relay off
mjr 73:4e8ce0b18915 5749 // 1 = turn relay on
mjr 73:4e8ce0b18915 5750 // 2 = pulse relay (as though the power-on timer fired)
mjr 73:4e8ce0b18915 5751 TVRelay(data[2]);
mjr 73:4e8ce0b18915 5752 break;
mjr 73:4e8ce0b18915 5753
mjr 73:4e8ce0b18915 5754 case 12:
mjr 77:0b96f6867312 5755 // 12 = Learn IR code. This enters IR learning mode. While
mjr 77:0b96f6867312 5756 // in learning mode, we report raw IR signals and the first IR
mjr 77:0b96f6867312 5757 // command decoded through the special IR report format. IR
mjr 77:0b96f6867312 5758 // learning mode automatically ends after a timeout expires if
mjr 77:0b96f6867312 5759 // no command can be decoded within the time limit.
mjr 77:0b96f6867312 5760
mjr 77:0b96f6867312 5761 // enter IR learning mode
mjr 77:0b96f6867312 5762 IRLearningMode = 1;
mjr 77:0b96f6867312 5763
mjr 77:0b96f6867312 5764 // cancel any regular IR input in progress
mjr 77:0b96f6867312 5765 IRCommandIn = 0;
mjr 77:0b96f6867312 5766
mjr 77:0b96f6867312 5767 // reset and start the learning mode timeout timer
mjr 77:0b96f6867312 5768 IRTimer.reset();
mjr 73:4e8ce0b18915 5769 break;
mjr 73:4e8ce0b18915 5770
mjr 73:4e8ce0b18915 5771 case 13:
mjr 73:4e8ce0b18915 5772 // 13 = Send button status report
mjr 73:4e8ce0b18915 5773 reportButtonStatus(js);
mjr 73:4e8ce0b18915 5774 break;
mjr 78:1e00b3fa11af 5775
mjr 78:1e00b3fa11af 5776 case 14:
mjr 78:1e00b3fa11af 5777 // 14 = manually center the accelerometer
mjr 78:1e00b3fa11af 5778 accel.manualCenterRequest();
mjr 78:1e00b3fa11af 5779 break;
mjr 78:1e00b3fa11af 5780
mjr 78:1e00b3fa11af 5781 case 15:
mjr 78:1e00b3fa11af 5782 // 15 = set up ad hoc IR command, part 1. Mark the command
mjr 78:1e00b3fa11af 5783 // as not ready, and save the partial data from the message.
mjr 78:1e00b3fa11af 5784 IRAdHocCmd.ready = 0;
mjr 78:1e00b3fa11af 5785 IRAdHocCmd.protocol = data[2];
mjr 78:1e00b3fa11af 5786 IRAdHocCmd.dittos = (data[3] & IRFlagDittos) != 0;
mjr 78:1e00b3fa11af 5787 IRAdHocCmd.code = wireUI32(&data[4]);
mjr 78:1e00b3fa11af 5788 break;
mjr 78:1e00b3fa11af 5789
mjr 78:1e00b3fa11af 5790 case 16:
mjr 78:1e00b3fa11af 5791 // 16 = send ad hoc IR command, part 2. Fill in the rest
mjr 78:1e00b3fa11af 5792 // of the data from the message and mark the command as
mjr 78:1e00b3fa11af 5793 // ready. The IR polling routine will send this as soon
mjr 78:1e00b3fa11af 5794 // as the IR transmitter is free.
mjr 78:1e00b3fa11af 5795 IRAdHocCmd.code |= (uint64_t(wireUI32(&data[2])) << 32);
mjr 78:1e00b3fa11af 5796 IRAdHocCmd.ready = 1;
mjr 78:1e00b3fa11af 5797 break;
mjr 88:98bce687e6c0 5798
mjr 88:98bce687e6c0 5799 case 17:
mjr 88:98bce687e6c0 5800 // 17 = send pre-programmed IR command. This works just like
mjr 88:98bce687e6c0 5801 // sending an ad hoc command above, but we get the command data
mjr 88:98bce687e6c0 5802 // from an IR slot in the config rather than from the client.
mjr 88:98bce687e6c0 5803 // First make sure we have a valid slot number.
mjr 88:98bce687e6c0 5804 if (data[2] >= 1 && data[2] <= MAX_IR_CODES)
mjr 88:98bce687e6c0 5805 {
mjr 88:98bce687e6c0 5806 // get the IR command slot in the config
mjr 88:98bce687e6c0 5807 IRCommandCfg &cmd = cfg.IRCommand[data[2] - 1];
mjr 88:98bce687e6c0 5808
mjr 88:98bce687e6c0 5809 // copy the IR command data from the config
mjr 88:98bce687e6c0 5810 IRAdHocCmd.protocol = cmd.protocol;
mjr 88:98bce687e6c0 5811 IRAdHocCmd.dittos = (cmd.flags & IRFlagDittos) != 0;
mjr 88:98bce687e6c0 5812 IRAdHocCmd.code = (uint64_t(cmd.code.hi) << 32) | cmd.code.lo;
mjr 88:98bce687e6c0 5813
mjr 88:98bce687e6c0 5814 // mark the command as ready - this will trigger the polling
mjr 88:98bce687e6c0 5815 // routine to send the command as soon as the transmitter
mjr 88:98bce687e6c0 5816 // is free
mjr 88:98bce687e6c0 5817 IRAdHocCmd.ready = 1;
mjr 88:98bce687e6c0 5818 }
mjr 88:98bce687e6c0 5819 break;
mjr 38:091e511ce8a0 5820 }
mjr 38:091e511ce8a0 5821 }
mjr 38:091e511ce8a0 5822 else if (data[0] == 66)
mjr 38:091e511ce8a0 5823 {
mjr 38:091e511ce8a0 5824 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 5825 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 5826 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 5827 // in a variable-dependent format.
mjr 40:cc0d9814522b 5828 configVarSet(data);
mjr 86:e30a1f60f783 5829
mjr 87:8d35c74403af 5830 // notify the plunger, so that it can update relevant variables
mjr 87:8d35c74403af 5831 // dynamically
mjr 87:8d35c74403af 5832 plungerSensor->onConfigChange(data[1], cfg);
mjr 38:091e511ce8a0 5833 }
mjr 74:822a92bc11d2 5834 else if (data[0] == 67)
mjr 74:822a92bc11d2 5835 {
mjr 74:822a92bc11d2 5836 // SBX - extended SBA message. This is the same as SBA, except
mjr 74:822a92bc11d2 5837 // that the 7th byte selects a group of 32 ports, to allow access
mjr 74:822a92bc11d2 5838 // to ports beyond the first 32.
mjr 74:822a92bc11d2 5839 sba_sbx(data[6], data);
mjr 74:822a92bc11d2 5840 }
mjr 74:822a92bc11d2 5841 else if (data[0] == 68)
mjr 74:822a92bc11d2 5842 {
mjr 74:822a92bc11d2 5843 // PBX - extended PBA message. This is similar to PBA, but
mjr 74:822a92bc11d2 5844 // allows access to more than the first 32 ports by encoding
mjr 74:822a92bc11d2 5845 // a port group byte that selects a block of 8 ports.
mjr 74:822a92bc11d2 5846
mjr 74:822a92bc11d2 5847 // get the port group - the first port is 8*group
mjr 74:822a92bc11d2 5848 int portGroup = data[1];
mjr 74:822a92bc11d2 5849
mjr 74:822a92bc11d2 5850 // unpack the brightness values
mjr 74:822a92bc11d2 5851 uint32_t tmp1 = data[2] | (data[3]<<8) | (data[4]<<16);
mjr 74:822a92bc11d2 5852 uint32_t tmp2 = data[5] | (data[6]<<8) | (data[7]<<16);
mjr 74:822a92bc11d2 5853 uint8_t bri[8] = {
mjr 74:822a92bc11d2 5854 tmp1 & 0x3F, (tmp1>>6) & 0x3F, (tmp1>>12) & 0x3F, (tmp1>>18) & 0x3F,
mjr 74:822a92bc11d2 5855 tmp2 & 0x3F, (tmp2>>6) & 0x3F, (tmp2>>12) & 0x3F, (tmp2>>18) & 0x3F
mjr 74:822a92bc11d2 5856 };
mjr 74:822a92bc11d2 5857
mjr 74:822a92bc11d2 5858 // map the flash levels: 60->129, 61->130, 62->131, 63->132
mjr 74:822a92bc11d2 5859 for (int i = 0 ; i < 8 ; ++i)
mjr 74:822a92bc11d2 5860 {
mjr 74:822a92bc11d2 5861 if (bri[i] >= 60)
mjr 74:822a92bc11d2 5862 bri[i] += 129-60;
mjr 74:822a92bc11d2 5863 }
mjr 74:822a92bc11d2 5864
mjr 74:822a92bc11d2 5865 // Carry out the PBA
mjr 74:822a92bc11d2 5866 pba_pbx(portGroup*8, bri);
mjr 74:822a92bc11d2 5867 }
mjr 38:091e511ce8a0 5868 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 5869 {
mjr 38:091e511ce8a0 5870 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 5871 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 5872 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 5873 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 5874 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 5875 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 5876 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 5877 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 5878 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 5879 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 5880 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 5881 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 5882 //
mjr 38:091e511ce8a0 5883 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 5884 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 5885 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 5886 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 5887 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 5888 // address those ports anyway.
mjr 63:5cd1a5f3a41b 5889
mjr 63:5cd1a5f3a41b 5890 // figure the block of 7 ports covered in the message
mjr 38:091e511ce8a0 5891 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 5892 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 63:5cd1a5f3a41b 5893
mjr 63:5cd1a5f3a41b 5894 // update each port
mjr 38:091e511ce8a0 5895 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 5896 {
mjr 38:091e511ce8a0 5897 // set the brightness level for the output
mjr 40:cc0d9814522b 5898 uint8_t b = data[i-i0+1];
mjr 38:091e511ce8a0 5899 outLevel[i] = b;
mjr 38:091e511ce8a0 5900
mjr 74:822a92bc11d2 5901 // set the port's LedWiz state to the nearest equivalent, so
mjr 74:822a92bc11d2 5902 // that it maintains its current setting if we switch back to
mjr 74:822a92bc11d2 5903 // LedWiz mode on a future update
mjr 76:7f5912b6340e 5904 if (b != 0)
mjr 76:7f5912b6340e 5905 {
mjr 76:7f5912b6340e 5906 // Non-zero brightness - set the SBA switch on, and set the
mjr 76:7f5912b6340e 5907 // PBA brightness to the DOF brightness rescaled to the 1..48
mjr 76:7f5912b6340e 5908 // LedWiz range. If the port is subsequently addressed by an
mjr 76:7f5912b6340e 5909 // LedWiz command, this will carry the current DOF setting
mjr 76:7f5912b6340e 5910 // forward unchanged.
mjr 76:7f5912b6340e 5911 wizOn[i] = 1;
mjr 76:7f5912b6340e 5912 wizVal[i] = dof_to_lw[b];
mjr 76:7f5912b6340e 5913 }
mjr 76:7f5912b6340e 5914 else
mjr 76:7f5912b6340e 5915 {
mjr 76:7f5912b6340e 5916 // Zero brightness. Set the SBA switch off, and leave the
mjr 76:7f5912b6340e 5917 // PBA brightness the same as it was.
mjr 76:7f5912b6340e 5918 wizOn[i] = 0;
mjr 76:7f5912b6340e 5919 }
mjr 74:822a92bc11d2 5920
mjr 38:091e511ce8a0 5921 // set the output
mjr 40:cc0d9814522b 5922 lwPin[i]->set(b);
mjr 38:091e511ce8a0 5923 }
mjr 38:091e511ce8a0 5924
mjr 38:091e511ce8a0 5925 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 5926 if (hc595 != 0)
mjr 38:091e511ce8a0 5927 hc595->update();
mjr 38:091e511ce8a0 5928 }
mjr 38:091e511ce8a0 5929 else
mjr 38:091e511ce8a0 5930 {
mjr 74:822a92bc11d2 5931 // Everything else is an LedWiz PBA message. This is a full
mjr 74:822a92bc11d2 5932 // "profile" dump from the host for one bank of 8 outputs. Each
mjr 74:822a92bc11d2 5933 // byte sets one output in the current bank. The current bank
mjr 74:822a92bc11d2 5934 // is implied; the bank starts at 0 and is reset to 0 by any SBA
mjr 74:822a92bc11d2 5935 // message, and is incremented to the next bank by each PBA. Our
mjr 74:822a92bc11d2 5936 // variable pbaIdx keeps track of the current bank. There's no
mjr 74:822a92bc11d2 5937 // direct way for the host to select the bank; it just has to count
mjr 74:822a92bc11d2 5938 // on us staying in sync. In practice, clients always send the
mjr 74:822a92bc11d2 5939 // full set of 4 PBA messages in a row to set all 32 outputs.
mjr 38:091e511ce8a0 5940 //
mjr 38:091e511ce8a0 5941 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 5942 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 5943 // wizVal[] entry for each output, and that takes precedence
mjr 63:5cd1a5f3a41b 5944 // over the extended protocol settings when we're in LedWiz
mjr 63:5cd1a5f3a41b 5945 // protocol mode.
mjr 38:091e511ce8a0 5946 //
mjr 38:091e511ce8a0 5947 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 5948 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 5949
mjr 74:822a92bc11d2 5950 // carry out the PBA
mjr 74:822a92bc11d2 5951 pba_pbx(pbaIdx, data);
mjr 74:822a92bc11d2 5952
mjr 74:822a92bc11d2 5953 // update the PBX index state for the next message
mjr 74:822a92bc11d2 5954 pbaIdx = (pbaIdx + 8) % 32;
mjr 38:091e511ce8a0 5955 }
mjr 38:091e511ce8a0 5956 }
mjr 35:e959ffba78fd 5957
mjr 38:091e511ce8a0 5958 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 5959 //
mjr 5:a70c0bce770d 5960 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 5961 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 5962 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 5963 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 5964 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 5965 // port outputs.
mjr 5:a70c0bce770d 5966 //
mjr 0:5acbbe3f4cf4 5967 int main(void)
mjr 0:5acbbe3f4cf4 5968 {
mjr 60:f38da020aa13 5969 // say hello to the debug console, in case it's connected
mjr 39:b3815a1c3802 5970 printf("\r\nPinscape Controller starting\r\n");
mjr 82:4f6209cb5c33 5971
mjr 76:7f5912b6340e 5972 // clear the I2C connection
mjr 35:e959ffba78fd 5973 clear_i2c();
mjr 82:4f6209cb5c33 5974
mjr 82:4f6209cb5c33 5975 // Elevate GPIO pin interrupt priorities, so that they can preempt
mjr 82:4f6209cb5c33 5976 // other interrupts. This is important for some external peripherals,
mjr 82:4f6209cb5c33 5977 // particularly the quadrature plunger sensors, which can generate
mjr 82:4f6209cb5c33 5978 // high-speed interrupts that need to be serviced quickly to keep
mjr 82:4f6209cb5c33 5979 // proper count of the quadrature position.
mjr 82:4f6209cb5c33 5980 FastInterruptIn::elevatePriority();
mjr 38:091e511ce8a0 5981
mjr 76:7f5912b6340e 5982 // Load the saved configuration. There are two sources of the
mjr 76:7f5912b6340e 5983 // configuration data:
mjr 76:7f5912b6340e 5984 //
mjr 76:7f5912b6340e 5985 // - Look for an NVM (flash non-volatile memory) configuration.
mjr 76:7f5912b6340e 5986 // If this is valid, we'll load it. The NVM is config data that can
mjr 76:7f5912b6340e 5987 // be updated dynamically by the host via USB commands and then stored
mjr 76:7f5912b6340e 5988 // in the flash by the firmware itself. If this exists, it supersedes
mjr 76:7f5912b6340e 5989 // any of the other settings stores. The Windows config tool uses this
mjr 76:7f5912b6340e 5990 // to store user settings updates.
mjr 76:7f5912b6340e 5991 //
mjr 76:7f5912b6340e 5992 // - If there's no NVM, we'll load the factory defaults, then we'll
mjr 76:7f5912b6340e 5993 // load any settings stored in the host-loaded configuration. The
mjr 76:7f5912b6340e 5994 // host can patch a set of configuration variable settings into the
mjr 76:7f5912b6340e 5995 // .bin file when loading new firmware, in the host-loaded config
mjr 76:7f5912b6340e 5996 // area that we reserve for this purpose. This allows the host to
mjr 76:7f5912b6340e 5997 // restore a configuration at the same time it installs firmware,
mjr 76:7f5912b6340e 5998 // without a separate download of the config data.
mjr 76:7f5912b6340e 5999 //
mjr 76:7f5912b6340e 6000 // The NVM supersedes the host-loaded config, since it can be updated
mjr 76:7f5912b6340e 6001 // between firmware updated and is thus presumably more recent if it's
mjr 76:7f5912b6340e 6002 // present. (Note that the NVM and host-loaded config are both in
mjr 76:7f5912b6340e 6003 // flash, so in principle we could just have a single NVM store that
mjr 76:7f5912b6340e 6004 // the host patches. The only reason we don't is that the NVM store
mjr 76:7f5912b6340e 6005 // is an image of our in-memory config structure, which is a native C
mjr 76:7f5912b6340e 6006 // struct, and we don't want the host to have to know the details of
mjr 76:7f5912b6340e 6007 // its byte layout, for obvious reasons. The host-loaded config, in
mjr 76:7f5912b6340e 6008 // contrast, uses the wire protocol format, which has a well-defined
mjr 76:7f5912b6340e 6009 // byte layout that's independent of the firmware version or the
mjr 76:7f5912b6340e 6010 // details of how the C compiler arranges the struct memory.)
mjr 76:7f5912b6340e 6011 if (!loadConfigFromFlash())
mjr 76:7f5912b6340e 6012 loadHostLoadedConfig();
mjr 35:e959ffba78fd 6013
mjr 38:091e511ce8a0 6014 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 6015 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 6016
mjr 33:d832bcab089e 6017 // we're not connected/awake yet
mjr 33:d832bcab089e 6018 bool connected = false;
mjr 40:cc0d9814522b 6019 Timer connectChangeTimer;
mjr 33:d832bcab089e 6020
mjr 35:e959ffba78fd 6021 // create the plunger sensor interface
mjr 35:e959ffba78fd 6022 createPlunger();
mjr 76:7f5912b6340e 6023
mjr 76:7f5912b6340e 6024 // update the plunger reader's cached calibration data
mjr 76:7f5912b6340e 6025 plungerReader.onUpdateCal();
mjr 33:d832bcab089e 6026
mjr 60:f38da020aa13 6027 // set up the TLC5940 interface, if these chips are present
mjr 35:e959ffba78fd 6028 init_tlc5940(cfg);
mjr 34:6b981a2afab7 6029
mjr 87:8d35c74403af 6030 // initialize the TLC5916 interface, if these chips are present
mjr 87:8d35c74403af 6031 init_tlc59116(cfg);
mjr 87:8d35c74403af 6032
mjr 60:f38da020aa13 6033 // set up 74HC595 interface, if these chips are present
mjr 35:e959ffba78fd 6034 init_hc595(cfg);
mjr 6:cc35eb643e8f 6035
mjr 54:fd77a6b2f76c 6036 // Initialize the LedWiz ports. Note that the ordering here is important:
mjr 54:fd77a6b2f76c 6037 // this has to come after we create the TLC5940 and 74HC595 object instances
mjr 54:fd77a6b2f76c 6038 // (which we just did above), since we need to access those objects to set
mjr 54:fd77a6b2f76c 6039 // up ports assigned to the respective chips.
mjr 35:e959ffba78fd 6040 initLwOut(cfg);
mjr 48:058ace2aed1d 6041
mjr 60:f38da020aa13 6042 // start the TLC5940 refresh cycle clock
mjr 35:e959ffba78fd 6043 if (tlc5940 != 0)
mjr 35:e959ffba78fd 6044 tlc5940->start();
mjr 87:8d35c74403af 6045
mjr 77:0b96f6867312 6046 // Assume that nothing uses keyboard keys. We'll check for keyboard
mjr 77:0b96f6867312 6047 // usage when initializing the various subsystems that can send keys
mjr 77:0b96f6867312 6048 // (buttons, IR). If we find anything that does, we'll create the
mjr 77:0b96f6867312 6049 // USB keyboard interface.
mjr 77:0b96f6867312 6050 bool kbKeys = false;
mjr 77:0b96f6867312 6051
mjr 77:0b96f6867312 6052 // set up the IR remote control emitter & receiver, if present
mjr 77:0b96f6867312 6053 init_IR(cfg, kbKeys);
mjr 77:0b96f6867312 6054
mjr 77:0b96f6867312 6055 // start the power status time, if applicable
mjr 77:0b96f6867312 6056 startPowerStatusTimer(cfg);
mjr 48:058ace2aed1d 6057
mjr 35:e959ffba78fd 6058 // initialize the button input ports
mjr 35:e959ffba78fd 6059 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 6060
mjr 60:f38da020aa13 6061 // Create the joystick USB client. Note that the USB vendor/product ID
mjr 60:f38da020aa13 6062 // information comes from the saved configuration. Also note that we have
mjr 60:f38da020aa13 6063 // to wait until after initializing the input buttons (which we just did
mjr 60:f38da020aa13 6064 // above) to set up the interface, since the button setup will determine
mjr 60:f38da020aa13 6065 // whether or not we need to present a USB keyboard interface in addition
mjr 60:f38da020aa13 6066 // to the joystick interface.
mjr 51:57eb311faafa 6067 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false,
mjr 51:57eb311faafa 6068 cfg.joystickEnabled, kbKeys);
mjr 51:57eb311faafa 6069
mjr 60:f38da020aa13 6070 // Wait for the USB connection to start up. Show a distinctive diagnostic
mjr 60:f38da020aa13 6071 // flash pattern while waiting.
mjr 70:9f58735a1732 6072 Timer connTimeoutTimer, connFlashTimer;
mjr 70:9f58735a1732 6073 connTimeoutTimer.start();
mjr 70:9f58735a1732 6074 connFlashTimer.start();
mjr 51:57eb311faafa 6075 while (!js.configured())
mjr 51:57eb311faafa 6076 {
mjr 51:57eb311faafa 6077 // show one short yellow flash at 2-second intervals
mjr 70:9f58735a1732 6078 if (connFlashTimer.read_us() > 2000000)
mjr 51:57eb311faafa 6079 {
mjr 51:57eb311faafa 6080 // short yellow flash
mjr 51:57eb311faafa 6081 diagLED(1, 1, 0);
mjr 54:fd77a6b2f76c 6082 wait_us(50000);
mjr 51:57eb311faafa 6083 diagLED(0, 0, 0);
mjr 51:57eb311faafa 6084
mjr 51:57eb311faafa 6085 // reset the flash timer
mjr 70:9f58735a1732 6086 connFlashTimer.reset();
mjr 51:57eb311faafa 6087 }
mjr 70:9f58735a1732 6088
mjr 77:0b96f6867312 6089 // If we've been disconnected for more than the reboot timeout,
mjr 77:0b96f6867312 6090 // reboot. Some PCs won't reconnect if we were left plugged in
mjr 77:0b96f6867312 6091 // during a power cycle on the PC, but fortunately a reboot on
mjr 77:0b96f6867312 6092 // the KL25Z will make the host notice us and trigger a reconnect.
mjr 86:e30a1f60f783 6093 // Don't do this if we're in a non-recoverable PSU2 power state.
mjr 70:9f58735a1732 6094 if (cfg.disconnectRebootTimeout != 0
mjr 86:e30a1f60f783 6095 && connTimeoutTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 6096 && powerStatusAllowsReboot())
mjr 70:9f58735a1732 6097 reboot(js, false, 0);
mjr 77:0b96f6867312 6098
mjr 77:0b96f6867312 6099 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6100 powerStatusUpdate(cfg);
mjr 51:57eb311faafa 6101 }
mjr 60:f38da020aa13 6102
mjr 60:f38da020aa13 6103 // we're now connected to the host
mjr 54:fd77a6b2f76c 6104 connected = true;
mjr 40:cc0d9814522b 6105
mjr 60:f38da020aa13 6106 // Last report timer for the joytick interface. We use this timer to
mjr 60:f38da020aa13 6107 // throttle the report rate to a pace that's suitable for VP. Without
mjr 60:f38da020aa13 6108 // any artificial delays, we could generate data to send on the joystick
mjr 60:f38da020aa13 6109 // interface on every loop iteration. The loop iteration time depends
mjr 60:f38da020aa13 6110 // on which devices are attached, since most of the work in our main
mjr 60:f38da020aa13 6111 // loop is simply polling our devices. For typical setups, the loop
mjr 60:f38da020aa13 6112 // time ranges from about 0.25ms to 2.5ms; the biggest factor is the
mjr 60:f38da020aa13 6113 // plunger sensor. But VP polls for input about every 10ms, so there's
mjr 60:f38da020aa13 6114 // no benefit in sending data faster than that, and there's some harm,
mjr 60:f38da020aa13 6115 // in that it creates USB overhead (both on the wire and on the host
mjr 60:f38da020aa13 6116 // CPU). We therefore use this timer to pace our reports to roughly
mjr 60:f38da020aa13 6117 // the VP input polling rate. Note that there's no way to actually
mjr 60:f38da020aa13 6118 // synchronize with VP's polling, but there's also no need to, as the
mjr 60:f38da020aa13 6119 // input model is designed to reflect the overall current state at any
mjr 60:f38da020aa13 6120 // given time rather than events or deltas. If VP polls twice between
mjr 60:f38da020aa13 6121 // two updates, it simply sees no state change; if we send two updates
mjr 60:f38da020aa13 6122 // between VP polls, VP simply sees the latest state when it does get
mjr 60:f38da020aa13 6123 // around to polling.
mjr 38:091e511ce8a0 6124 Timer jsReportTimer;
mjr 38:091e511ce8a0 6125 jsReportTimer.start();
mjr 38:091e511ce8a0 6126
mjr 60:f38da020aa13 6127 // Time since we successfully sent a USB report. This is a hacky
mjr 60:f38da020aa13 6128 // workaround to deal with any remaining sporadic problems in the USB
mjr 60:f38da020aa13 6129 // stack. I've been trying to bulletproof the USB code over time to
mjr 60:f38da020aa13 6130 // remove all such problems at their source, but it seems unlikely that
mjr 60:f38da020aa13 6131 // we'll ever get them all. Thus this hack. The idea here is that if
mjr 60:f38da020aa13 6132 // we go too long without successfully sending a USB report, we'll
mjr 60:f38da020aa13 6133 // assume that the connection is broken (and the KL25Z USB hardware
mjr 60:f38da020aa13 6134 // hasn't noticed this), and we'll try taking measures to recover.
mjr 38:091e511ce8a0 6135 Timer jsOKTimer;
mjr 38:091e511ce8a0 6136 jsOKTimer.start();
mjr 35:e959ffba78fd 6137
mjr 55:4db125cd11a0 6138 // Initialize the calibration button and lamp, if enabled. To be enabled,
mjr 55:4db125cd11a0 6139 // the pin has to be assigned to something other than NC (0xFF), AND the
mjr 55:4db125cd11a0 6140 // corresponding feature enable flag has to be set.
mjr 55:4db125cd11a0 6141 DigitalIn *calBtn = 0;
mjr 55:4db125cd11a0 6142 DigitalOut *calBtnLed = 0;
mjr 55:4db125cd11a0 6143
mjr 55:4db125cd11a0 6144 // calibration button input - feature flag 0x01
mjr 55:4db125cd11a0 6145 if ((cfg.plunger.cal.features & 0x01) && cfg.plunger.cal.btn != 0xFF)
mjr 55:4db125cd11a0 6146 calBtn = new DigitalIn(wirePinName(cfg.plunger.cal.btn));
mjr 55:4db125cd11a0 6147
mjr 55:4db125cd11a0 6148 // calibration button indicator lamp output - feature flag 0x02
mjr 55:4db125cd11a0 6149 if ((cfg.plunger.cal.features & 0x02) && cfg.plunger.cal.led != 0xFF)
mjr 55:4db125cd11a0 6150 calBtnLed = new DigitalOut(wirePinName(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 6151
mjr 35:e959ffba78fd 6152 // initialize the calibration button
mjr 1:d913e0afb2ac 6153 calBtnTimer.start();
mjr 35:e959ffba78fd 6154 calBtnState = 0;
mjr 1:d913e0afb2ac 6155
mjr 1:d913e0afb2ac 6156 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 6157 Timer hbTimer;
mjr 1:d913e0afb2ac 6158 hbTimer.start();
mjr 1:d913e0afb2ac 6159 int hb = 0;
mjr 5:a70c0bce770d 6160 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 6161
mjr 1:d913e0afb2ac 6162 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 6163 Timer acTimer;
mjr 1:d913e0afb2ac 6164 acTimer.start();
mjr 1:d913e0afb2ac 6165
mjr 0:5acbbe3f4cf4 6166 // create the accelerometer object
mjr 77:0b96f6867312 6167 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS,
mjr 78:1e00b3fa11af 6168 MMA8451_INT_PIN, cfg.accel.range, cfg.accel.autoCenterTime);
mjr 76:7f5912b6340e 6169
mjr 17:ab3cec0c8bf4 6170 // last accelerometer report, in joystick units (we report the nudge
mjr 17:ab3cec0c8bf4 6171 // acceleration via the joystick x & y axes, per the VP convention)
mjr 17:ab3cec0c8bf4 6172 int x = 0, y = 0;
mjr 17:ab3cec0c8bf4 6173
mjr 48:058ace2aed1d 6174 // initialize the plunger sensor
mjr 35:e959ffba78fd 6175 plungerSensor->init();
mjr 10:976666ffa4ef 6176
mjr 48:058ace2aed1d 6177 // set up the ZB Launch Ball monitor
mjr 48:058ace2aed1d 6178 ZBLaunchBall zbLaunchBall;
mjr 48:058ace2aed1d 6179
mjr 54:fd77a6b2f76c 6180 // enable the peripheral chips
mjr 54:fd77a6b2f76c 6181 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 6182 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 6183 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6184 hc595->enable(true);
mjr 87:8d35c74403af 6185 if (tlc59116 != 0)
mjr 87:8d35c74403af 6186 tlc59116->enable(true);
mjr 74:822a92bc11d2 6187
mjr 76:7f5912b6340e 6188 // start the LedWiz flash cycle timer
mjr 74:822a92bc11d2 6189 wizCycleTimer.start();
mjr 74:822a92bc11d2 6190
mjr 74:822a92bc11d2 6191 // start the PWM update polling timer
mjr 74:822a92bc11d2 6192 polledPwmTimer.start();
mjr 43:7a6364d82a41 6193
mjr 1:d913e0afb2ac 6194 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 6195 // host requests
mjr 0:5acbbe3f4cf4 6196 for (;;)
mjr 0:5acbbe3f4cf4 6197 {
mjr 74:822a92bc11d2 6198 // start the main loop timer for diagnostic data collection
mjr 76:7f5912b6340e 6199 IF_DIAG(mainLoopTimer.reset(); mainLoopTimer.start();)
mjr 74:822a92bc11d2 6200
mjr 48:058ace2aed1d 6201 // Process incoming reports on the joystick interface. The joystick
mjr 48:058ace2aed1d 6202 // "out" (receive) endpoint is used for LedWiz commands and our
mjr 48:058ace2aed1d 6203 // extended protocol commands. Limit processing time to 5ms to
mjr 48:058ace2aed1d 6204 // ensure we don't starve the input side.
mjr 39:b3815a1c3802 6205 LedWizMsg lwm;
mjr 48:058ace2aed1d 6206 Timer lwt;
mjr 48:058ace2aed1d 6207 lwt.start();
mjr 77:0b96f6867312 6208 IF_DIAG(int msgCount = 0;)
mjr 48:058ace2aed1d 6209 while (js.readLedWizMsg(lwm) && lwt.read_us() < 5000)
mjr 74:822a92bc11d2 6210 {
mjr 78:1e00b3fa11af 6211 handleInputMsg(lwm, js, accel);
mjr 74:822a92bc11d2 6212 IF_DIAG(++msgCount;)
mjr 74:822a92bc11d2 6213 }
mjr 74:822a92bc11d2 6214
mjr 74:822a92bc11d2 6215 // collect performance statistics on the message reader, if desired
mjr 74:822a92bc11d2 6216 IF_DIAG(
mjr 74:822a92bc11d2 6217 if (msgCount != 0)
mjr 74:822a92bc11d2 6218 {
mjr 76:7f5912b6340e 6219 mainLoopMsgTime += lwt.read_us();
mjr 74:822a92bc11d2 6220 mainLoopMsgCount++;
mjr 74:822a92bc11d2 6221 }
mjr 74:822a92bc11d2 6222 )
mjr 74:822a92bc11d2 6223
mjr 77:0b96f6867312 6224 // process IR input
mjr 77:0b96f6867312 6225 process_IR(cfg, js);
mjr 77:0b96f6867312 6226
mjr 77:0b96f6867312 6227 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6228 powerStatusUpdate(cfg);
mjr 77:0b96f6867312 6229
mjr 74:822a92bc11d2 6230 // update flashing LedWiz outputs periodically
mjr 74:822a92bc11d2 6231 wizPulse();
mjr 74:822a92bc11d2 6232
mjr 74:822a92bc11d2 6233 // update PWM outputs
mjr 74:822a92bc11d2 6234 pollPwmUpdates();
mjr 77:0b96f6867312 6235
mjr 89:c43cd923401c 6236 // update Flipper Logic outputs
mjr 89:c43cd923401c 6237 LwFlipperLogicOut::poll();
mjr 89:c43cd923401c 6238
mjr 77:0b96f6867312 6239 // poll the accelerometer
mjr 77:0b96f6867312 6240 accel.poll();
mjr 55:4db125cd11a0 6241
mjr 76:7f5912b6340e 6242 // collect diagnostic statistics, checkpoint 0
mjr 76:7f5912b6340e 6243 IF_DIAG(mainLoopIterCheckpt[0] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6244
mjr 55:4db125cd11a0 6245 // send TLC5940 data updates if applicable
mjr 55:4db125cd11a0 6246 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6247 tlc5940->send();
mjr 87:8d35c74403af 6248
mjr 87:8d35c74403af 6249 // send TLC59116 data updates
mjr 87:8d35c74403af 6250 if (tlc59116 != 0)
mjr 87:8d35c74403af 6251 tlc59116->send();
mjr 1:d913e0afb2ac 6252
mjr 76:7f5912b6340e 6253 // collect diagnostic statistics, checkpoint 1
mjr 76:7f5912b6340e 6254 IF_DIAG(mainLoopIterCheckpt[1] += mainLoopTimer.read_us();)
mjr 77:0b96f6867312 6255
mjr 1:d913e0afb2ac 6256 // check for plunger calibration
mjr 17:ab3cec0c8bf4 6257 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 6258 {
mjr 1:d913e0afb2ac 6259 // check the state
mjr 1:d913e0afb2ac 6260 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6261 {
mjr 1:d913e0afb2ac 6262 case 0:
mjr 1:d913e0afb2ac 6263 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 6264 calBtnTimer.reset();
mjr 1:d913e0afb2ac 6265 calBtnState = 1;
mjr 1:d913e0afb2ac 6266 break;
mjr 1:d913e0afb2ac 6267
mjr 1:d913e0afb2ac 6268 case 1:
mjr 1:d913e0afb2ac 6269 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 6270 // passed, start the hold period
mjr 48:058ace2aed1d 6271 if (calBtnTimer.read_us() > 50000)
mjr 1:d913e0afb2ac 6272 calBtnState = 2;
mjr 1:d913e0afb2ac 6273 break;
mjr 1:d913e0afb2ac 6274
mjr 1:d913e0afb2ac 6275 case 2:
mjr 1:d913e0afb2ac 6276 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 6277 // for the entire hold period, move to calibration mode
mjr 48:058ace2aed1d 6278 if (calBtnTimer.read_us() > 2050000)
mjr 1:d913e0afb2ac 6279 {
mjr 1:d913e0afb2ac 6280 // enter calibration mode
mjr 1:d913e0afb2ac 6281 calBtnState = 3;
mjr 9:fd65b0a94720 6282 calBtnTimer.reset();
mjr 35:e959ffba78fd 6283
mjr 44:b5ac89b9cd5d 6284 // begin the plunger calibration limits
mjr 52:8298b2a73eb2 6285 plungerReader.setCalMode(true);
mjr 1:d913e0afb2ac 6286 }
mjr 1:d913e0afb2ac 6287 break;
mjr 2:c174f9ee414a 6288
mjr 2:c174f9ee414a 6289 case 3:
mjr 9:fd65b0a94720 6290 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 6291 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 6292 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 6293 break;
mjr 0:5acbbe3f4cf4 6294 }
mjr 0:5acbbe3f4cf4 6295 }
mjr 1:d913e0afb2ac 6296 else
mjr 1:d913e0afb2ac 6297 {
mjr 2:c174f9ee414a 6298 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 6299 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 6300 // and save the results to flash.
mjr 2:c174f9ee414a 6301 //
mjr 2:c174f9ee414a 6302 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 6303 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 6304 // mode, it simply cancels the attempt.
mjr 48:058ace2aed1d 6305 if (calBtnState == 3 && calBtnTimer.read_us() > 15000000)
mjr 2:c174f9ee414a 6306 {
mjr 2:c174f9ee414a 6307 // exit calibration mode
mjr 1:d913e0afb2ac 6308 calBtnState = 0;
mjr 52:8298b2a73eb2 6309 plungerReader.setCalMode(false);
mjr 2:c174f9ee414a 6310
mjr 6:cc35eb643e8f 6311 // save the updated configuration
mjr 35:e959ffba78fd 6312 cfg.plunger.cal.calibrated = 1;
mjr 86:e30a1f60f783 6313 saveConfigToFlash(0, false);
mjr 2:c174f9ee414a 6314 }
mjr 2:c174f9ee414a 6315 else if (calBtnState != 3)
mjr 2:c174f9ee414a 6316 {
mjr 2:c174f9ee414a 6317 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 6318 calBtnState = 0;
mjr 2:c174f9ee414a 6319 }
mjr 1:d913e0afb2ac 6320 }
mjr 1:d913e0afb2ac 6321
mjr 1:d913e0afb2ac 6322 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 6323 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 6324 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6325 {
mjr 1:d913e0afb2ac 6326 case 2:
mjr 1:d913e0afb2ac 6327 // in the hold period - flash the light
mjr 48:058ace2aed1d 6328 newCalBtnLit = ((calBtnTimer.read_us()/250000) & 1);
mjr 1:d913e0afb2ac 6329 break;
mjr 1:d913e0afb2ac 6330
mjr 1:d913e0afb2ac 6331 case 3:
mjr 1:d913e0afb2ac 6332 // calibration mode - show steady on
mjr 1:d913e0afb2ac 6333 newCalBtnLit = true;
mjr 1:d913e0afb2ac 6334 break;
mjr 1:d913e0afb2ac 6335
mjr 1:d913e0afb2ac 6336 default:
mjr 1:d913e0afb2ac 6337 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 6338 newCalBtnLit = false;
mjr 1:d913e0afb2ac 6339 break;
mjr 1:d913e0afb2ac 6340 }
mjr 3:3514575d4f86 6341
mjr 3:3514575d4f86 6342 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 6343 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 6344 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 6345 {
mjr 1:d913e0afb2ac 6346 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 6347 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 6348 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6349 calBtnLed->write(1);
mjr 38:091e511ce8a0 6350 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 6351 }
mjr 2:c174f9ee414a 6352 else {
mjr 17:ab3cec0c8bf4 6353 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6354 calBtnLed->write(0);
mjr 38:091e511ce8a0 6355 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 6356 }
mjr 1:d913e0afb2ac 6357 }
mjr 35:e959ffba78fd 6358
mjr 76:7f5912b6340e 6359 // collect diagnostic statistics, checkpoint 2
mjr 76:7f5912b6340e 6360 IF_DIAG(mainLoopIterCheckpt[2] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6361
mjr 48:058ace2aed1d 6362 // read the plunger sensor
mjr 48:058ace2aed1d 6363 plungerReader.read();
mjr 48:058ace2aed1d 6364
mjr 76:7f5912b6340e 6365 // collect diagnostic statistics, checkpoint 3
mjr 76:7f5912b6340e 6366 IF_DIAG(mainLoopIterCheckpt[3] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6367
mjr 53:9b2611964afc 6368 // update the ZB Launch Ball status
mjr 53:9b2611964afc 6369 zbLaunchBall.update();
mjr 37:ed52738445fc 6370
mjr 76:7f5912b6340e 6371 // collect diagnostic statistics, checkpoint 4
mjr 76:7f5912b6340e 6372 IF_DIAG(mainLoopIterCheckpt[4] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6373
mjr 53:9b2611964afc 6374 // process button updates
mjr 53:9b2611964afc 6375 processButtons(cfg);
mjr 53:9b2611964afc 6376
mjr 76:7f5912b6340e 6377 // collect diagnostic statistics, checkpoint 5
mjr 76:7f5912b6340e 6378 IF_DIAG(mainLoopIterCheckpt[5] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6379
mjr 38:091e511ce8a0 6380 // send a keyboard report if we have new data
mjr 37:ed52738445fc 6381 if (kbState.changed)
mjr 37:ed52738445fc 6382 {
mjr 38:091e511ce8a0 6383 // send a keyboard report
mjr 37:ed52738445fc 6384 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 6385 kbState.changed = false;
mjr 37:ed52738445fc 6386 }
mjr 38:091e511ce8a0 6387
mjr 38:091e511ce8a0 6388 // likewise for the media controller
mjr 37:ed52738445fc 6389 if (mediaState.changed)
mjr 37:ed52738445fc 6390 {
mjr 38:091e511ce8a0 6391 // send a media report
mjr 37:ed52738445fc 6392 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 6393 mediaState.changed = false;
mjr 37:ed52738445fc 6394 }
mjr 38:091e511ce8a0 6395
mjr 76:7f5912b6340e 6396 // collect diagnostic statistics, checkpoint 6
mjr 76:7f5912b6340e 6397 IF_DIAG(mainLoopIterCheckpt[6] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6398
mjr 38:091e511ce8a0 6399 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 6400 bool jsOK = false;
mjr 55:4db125cd11a0 6401
mjr 55:4db125cd11a0 6402 // figure the current status flags for joystick reports
mjr 77:0b96f6867312 6403 uint16_t statusFlags =
mjr 77:0b96f6867312 6404 cfg.plunger.enabled // 0x01
mjr 77:0b96f6867312 6405 | nightMode // 0x02
mjr 79:682ae3171a08 6406 | ((psu2_state & 0x07) << 2) // 0x04 0x08 0x10
mjr 79:682ae3171a08 6407 | saveConfigSucceededFlag; // 0x40
mjr 77:0b96f6867312 6408 if (IRLearningMode != 0)
mjr 77:0b96f6867312 6409 statusFlags |= 0x20;
mjr 17:ab3cec0c8bf4 6410
mjr 50:40015764bbe6 6411 // If it's been long enough since our last USB status report, send
mjr 50:40015764bbe6 6412 // the new report. VP only polls for input in 10ms intervals, so
mjr 50:40015764bbe6 6413 // there's no benefit in sending reports more frequently than this.
mjr 50:40015764bbe6 6414 // More frequent reporting would only add USB I/O overhead.
mjr 50:40015764bbe6 6415 if (cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 17:ab3cec0c8bf4 6416 {
mjr 17:ab3cec0c8bf4 6417 // read the accelerometer
mjr 17:ab3cec0c8bf4 6418 int xa, ya;
mjr 17:ab3cec0c8bf4 6419 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 6420
mjr 17:ab3cec0c8bf4 6421 // confine the results to our joystick axis range
mjr 17:ab3cec0c8bf4 6422 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 17:ab3cec0c8bf4 6423 if (xa > JOYMAX) xa = JOYMAX;
mjr 17:ab3cec0c8bf4 6424 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 17:ab3cec0c8bf4 6425 if (ya > JOYMAX) ya = JOYMAX;
mjr 17:ab3cec0c8bf4 6426
mjr 17:ab3cec0c8bf4 6427 // store the updated accelerometer coordinates
mjr 17:ab3cec0c8bf4 6428 x = xa;
mjr 17:ab3cec0c8bf4 6429 y = ya;
mjr 17:ab3cec0c8bf4 6430
mjr 48:058ace2aed1d 6431 // Report the current plunger position unless the plunger is
mjr 48:058ace2aed1d 6432 // disabled, or the ZB Launch Ball signal is on. In either of
mjr 48:058ace2aed1d 6433 // those cases, just report a constant 0 value. ZB Launch Ball
mjr 48:058ace2aed1d 6434 // temporarily disables mechanical plunger reporting because it
mjr 21:5048e16cc9ef 6435 // tells us that the table has a Launch Ball button instead of
mjr 48:058ace2aed1d 6436 // a traditional plunger, so we don't want to confuse VP with
mjr 48:058ace2aed1d 6437 // regular plunger inputs.
mjr 48:058ace2aed1d 6438 int z = plungerReader.getPosition();
mjr 53:9b2611964afc 6439 int zrep = (!cfg.plunger.enabled || zbLaunchOn ? 0 : z);
mjr 35:e959ffba78fd 6440
mjr 35:e959ffba78fd 6441 // rotate X and Y according to the device orientation in the cabinet
mjr 35:e959ffba78fd 6442 accelRotate(x, y);
mjr 35:e959ffba78fd 6443
mjr 35:e959ffba78fd 6444 // send the joystick report
mjr 53:9b2611964afc 6445 jsOK = js.update(x, y, zrep, jsButtons, statusFlags);
mjr 21:5048e16cc9ef 6446
mjr 17:ab3cec0c8bf4 6447 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 6448 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 6449 }
mjr 21:5048e16cc9ef 6450
mjr 52:8298b2a73eb2 6451 // If we're in sensor status mode, report all pixel exposure values
mjr 52:8298b2a73eb2 6452 if (reportPlungerStat)
mjr 10:976666ffa4ef 6453 {
mjr 17:ab3cec0c8bf4 6454 // send the report
mjr 53:9b2611964afc 6455 plungerSensor->sendStatusReport(js, reportPlungerStatFlags, reportPlungerStatTime);
mjr 17:ab3cec0c8bf4 6456
mjr 10:976666ffa4ef 6457 // we have satisfied this request
mjr 52:8298b2a73eb2 6458 reportPlungerStat = false;
mjr 10:976666ffa4ef 6459 }
mjr 10:976666ffa4ef 6460
mjr 35:e959ffba78fd 6461 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 6462 // periodically for the sake of the Windows config tool.
mjr 77:0b96f6867312 6463 if (!cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 21:5048e16cc9ef 6464 {
mjr 55:4db125cd11a0 6465 jsOK = js.updateStatus(statusFlags);
mjr 38:091e511ce8a0 6466 jsReportTimer.reset();
mjr 38:091e511ce8a0 6467 }
mjr 38:091e511ce8a0 6468
mjr 38:091e511ce8a0 6469 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 6470 if (jsOK)
mjr 38:091e511ce8a0 6471 {
mjr 38:091e511ce8a0 6472 jsOKTimer.reset();
mjr 38:091e511ce8a0 6473 jsOKTimer.start();
mjr 21:5048e16cc9ef 6474 }
mjr 21:5048e16cc9ef 6475
mjr 76:7f5912b6340e 6476 // collect diagnostic statistics, checkpoint 7
mjr 76:7f5912b6340e 6477 IF_DIAG(mainLoopIterCheckpt[7] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6478
mjr 6:cc35eb643e8f 6479 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 6480 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 6481 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 6482 #endif
mjr 6:cc35eb643e8f 6483
mjr 33:d832bcab089e 6484 // check for connection status changes
mjr 54:fd77a6b2f76c 6485 bool newConnected = js.isConnected() && !js.isSleeping();
mjr 33:d832bcab089e 6486 if (newConnected != connected)
mjr 33:d832bcab089e 6487 {
mjr 54:fd77a6b2f76c 6488 // give it a moment to stabilize
mjr 40:cc0d9814522b 6489 connectChangeTimer.start();
mjr 55:4db125cd11a0 6490 if (connectChangeTimer.read_us() > 1000000)
mjr 33:d832bcab089e 6491 {
mjr 33:d832bcab089e 6492 // note the new status
mjr 33:d832bcab089e 6493 connected = newConnected;
mjr 40:cc0d9814522b 6494
mjr 40:cc0d9814522b 6495 // done with the change timer for this round - reset it for next time
mjr 40:cc0d9814522b 6496 connectChangeTimer.stop();
mjr 40:cc0d9814522b 6497 connectChangeTimer.reset();
mjr 33:d832bcab089e 6498
mjr 54:fd77a6b2f76c 6499 // if we're newly disconnected, clean up for PC suspend mode or power off
mjr 54:fd77a6b2f76c 6500 if (!connected)
mjr 40:cc0d9814522b 6501 {
mjr 54:fd77a6b2f76c 6502 // turn off all outputs
mjr 33:d832bcab089e 6503 allOutputsOff();
mjr 40:cc0d9814522b 6504
mjr 40:cc0d9814522b 6505 // The KL25Z runs off of USB power, so we might (depending on the PC
mjr 40:cc0d9814522b 6506 // and OS configuration) continue to receive power even when the main
mjr 40:cc0d9814522b 6507 // PC power supply is turned off, such as in soft-off or suspend/sleep
mjr 40:cc0d9814522b 6508 // mode. Any external output controller chips (TLC5940, 74HC595) might
mjr 40:cc0d9814522b 6509 // be powered from the PC power supply directly rather than from our
mjr 40:cc0d9814522b 6510 // USB power, so they might be powered off even when we're still running.
mjr 40:cc0d9814522b 6511 // To ensure cleaner startup when the power comes back on, globally
mjr 40:cc0d9814522b 6512 // disable the outputs. The global disable signals come from GPIO lines
mjr 40:cc0d9814522b 6513 // that remain powered as long as the KL25Z is powered, so these modes
mjr 40:cc0d9814522b 6514 // will apply smoothly across power state transitions in the external
mjr 40:cc0d9814522b 6515 // hardware. That is, when the external chips are powered up, they'll
mjr 40:cc0d9814522b 6516 // see the global disable signals as stable voltage inputs immediately,
mjr 40:cc0d9814522b 6517 // which will cause them to suppress any output triggering. This ensures
mjr 40:cc0d9814522b 6518 // that we don't fire any solenoids or flash any lights spuriously when
mjr 40:cc0d9814522b 6519 // the power first comes on.
mjr 40:cc0d9814522b 6520 if (tlc5940 != 0)
mjr 40:cc0d9814522b 6521 tlc5940->enable(false);
mjr 87:8d35c74403af 6522 if (tlc59116 != 0)
mjr 87:8d35c74403af 6523 tlc59116->enable(false);
mjr 40:cc0d9814522b 6524 if (hc595 != 0)
mjr 40:cc0d9814522b 6525 hc595->enable(false);
mjr 40:cc0d9814522b 6526 }
mjr 33:d832bcab089e 6527 }
mjr 33:d832bcab089e 6528 }
mjr 48:058ace2aed1d 6529
mjr 53:9b2611964afc 6530 // if we have a reboot timer pending, check for completion
mjr 86:e30a1f60f783 6531 if (saveConfigFollowupTimer.isRunning()
mjr 87:8d35c74403af 6532 && saveConfigFollowupTimer.read_us() > saveConfigFollowupTime*1000000UL)
mjr 85:3c28aee81cde 6533 {
mjr 85:3c28aee81cde 6534 // if a reboot is pending, execute it now
mjr 86:e30a1f60f783 6535 if (saveConfigRebootPending)
mjr 82:4f6209cb5c33 6536 {
mjr 86:e30a1f60f783 6537 // Only reboot if the PSU2 power state allows it. If it
mjr 86:e30a1f60f783 6538 // doesn't, suppress the reboot for now, but leave the boot
mjr 86:e30a1f60f783 6539 // flags set so that we keep checking on future rounds.
mjr 86:e30a1f60f783 6540 // That way we should eventually reboot when the power
mjr 86:e30a1f60f783 6541 // status allows it.
mjr 86:e30a1f60f783 6542 if (powerStatusAllowsReboot())
mjr 86:e30a1f60f783 6543 reboot(js);
mjr 82:4f6209cb5c33 6544 }
mjr 85:3c28aee81cde 6545 else
mjr 85:3c28aee81cde 6546 {
mjr 86:e30a1f60f783 6547 // No reboot required. Exit the timed post-save state.
mjr 86:e30a1f60f783 6548
mjr 86:e30a1f60f783 6549 // stop and reset the post-save timer
mjr 86:e30a1f60f783 6550 saveConfigFollowupTimer.stop();
mjr 86:e30a1f60f783 6551 saveConfigFollowupTimer.reset();
mjr 86:e30a1f60f783 6552
mjr 86:e30a1f60f783 6553 // clear the post-save success flag
mjr 86:e30a1f60f783 6554 saveConfigSucceededFlag = 0;
mjr 85:3c28aee81cde 6555 }
mjr 77:0b96f6867312 6556 }
mjr 86:e30a1f60f783 6557
mjr 48:058ace2aed1d 6558 // if we're disconnected, initiate a new connection
mjr 51:57eb311faafa 6559 if (!connected)
mjr 48:058ace2aed1d 6560 {
mjr 54:fd77a6b2f76c 6561 // show USB HAL debug events
mjr 54:fd77a6b2f76c 6562 extern void HAL_DEBUG_PRINTEVENTS(const char *prefix);
mjr 54:fd77a6b2f76c 6563 HAL_DEBUG_PRINTEVENTS(">DISC");
mjr 54:fd77a6b2f76c 6564
mjr 54:fd77a6b2f76c 6565 // show immediate diagnostic feedback
mjr 54:fd77a6b2f76c 6566 js.diagFlash();
mjr 54:fd77a6b2f76c 6567
mjr 54:fd77a6b2f76c 6568 // clear any previous diagnostic LED display
mjr 54:fd77a6b2f76c 6569 diagLED(0, 0, 0);
mjr 51:57eb311faafa 6570
mjr 51:57eb311faafa 6571 // set up a timer to monitor the reboot timeout
mjr 70:9f58735a1732 6572 Timer reconnTimeoutTimer;
mjr 70:9f58735a1732 6573 reconnTimeoutTimer.start();
mjr 48:058ace2aed1d 6574
mjr 54:fd77a6b2f76c 6575 // set up a timer for diagnostic displays
mjr 54:fd77a6b2f76c 6576 Timer diagTimer;
mjr 54:fd77a6b2f76c 6577 diagTimer.reset();
mjr 54:fd77a6b2f76c 6578 diagTimer.start();
mjr 74:822a92bc11d2 6579
mjr 74:822a92bc11d2 6580 // turn off the main loop timer while spinning
mjr 74:822a92bc11d2 6581 IF_DIAG(mainLoopTimer.stop();)
mjr 54:fd77a6b2f76c 6582
mjr 54:fd77a6b2f76c 6583 // loop until we get our connection back
mjr 54:fd77a6b2f76c 6584 while (!js.isConnected() || js.isSleeping())
mjr 51:57eb311faafa 6585 {
mjr 54:fd77a6b2f76c 6586 // try to recover the connection
mjr 54:fd77a6b2f76c 6587 js.recoverConnection();
mjr 54:fd77a6b2f76c 6588
mjr 89:c43cd923401c 6589 // update Flipper Logic outputs
mjr 89:c43cd923401c 6590 LwFlipperLogicOut::poll();
mjr 89:c43cd923401c 6591
mjr 55:4db125cd11a0 6592 // send TLC5940 data if necessary
mjr 55:4db125cd11a0 6593 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6594 tlc5940->send();
mjr 87:8d35c74403af 6595
mjr 87:8d35c74403af 6596 // update TLC59116 outputs
mjr 87:8d35c74403af 6597 if (tlc59116 != 0)
mjr 87:8d35c74403af 6598 tlc59116->send();
mjr 55:4db125cd11a0 6599
mjr 54:fd77a6b2f76c 6600 // show a diagnostic flash every couple of seconds
mjr 54:fd77a6b2f76c 6601 if (diagTimer.read_us() > 2000000)
mjr 51:57eb311faafa 6602 {
mjr 54:fd77a6b2f76c 6603 // flush the USB HAL debug events, if in debug mode
mjr 54:fd77a6b2f76c 6604 HAL_DEBUG_PRINTEVENTS(">NC");
mjr 54:fd77a6b2f76c 6605
mjr 54:fd77a6b2f76c 6606 // show diagnostic feedback
mjr 54:fd77a6b2f76c 6607 js.diagFlash();
mjr 51:57eb311faafa 6608
mjr 51:57eb311faafa 6609 // reset the flash timer
mjr 54:fd77a6b2f76c 6610 diagTimer.reset();
mjr 51:57eb311faafa 6611 }
mjr 51:57eb311faafa 6612
mjr 77:0b96f6867312 6613 // If the disconnect reboot timeout has expired, reboot.
mjr 77:0b96f6867312 6614 // Some PC hosts won't reconnect to a device that's left
mjr 77:0b96f6867312 6615 // plugged in through various events on the PC side, such as
mjr 77:0b96f6867312 6616 // rebooting Windows, cycling power on the PC, or just a lost
mjr 77:0b96f6867312 6617 // USB connection. Rebooting the KL25Z seems to be the most
mjr 77:0b96f6867312 6618 // reliable way to get Windows to notice us again after one
mjr 86:e30a1f60f783 6619 // of these events and make it reconnect. Only reboot if
mjr 86:e30a1f60f783 6620 // the PSU2 power status allows it - if not, skip it on this
mjr 86:e30a1f60f783 6621 // round and keep waiting.
mjr 51:57eb311faafa 6622 if (cfg.disconnectRebootTimeout != 0
mjr 86:e30a1f60f783 6623 && reconnTimeoutTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 6624 && powerStatusAllowsReboot())
mjr 54:fd77a6b2f76c 6625 reboot(js, false, 0);
mjr 77:0b96f6867312 6626
mjr 77:0b96f6867312 6627 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6628 powerStatusUpdate(cfg);
mjr 54:fd77a6b2f76c 6629 }
mjr 54:fd77a6b2f76c 6630
mjr 74:822a92bc11d2 6631 // resume the main loop timer
mjr 74:822a92bc11d2 6632 IF_DIAG(mainLoopTimer.start();)
mjr 74:822a92bc11d2 6633
mjr 54:fd77a6b2f76c 6634 // if we made it out of that loop alive, we're connected again!
mjr 54:fd77a6b2f76c 6635 connected = true;
mjr 54:fd77a6b2f76c 6636 HAL_DEBUG_PRINTEVENTS(">C");
mjr 54:fd77a6b2f76c 6637
mjr 54:fd77a6b2f76c 6638 // Enable peripheral chips and update them with current output data
mjr 54:fd77a6b2f76c 6639 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6640 tlc5940->enable(true);
mjr 87:8d35c74403af 6641 if (tlc59116 != 0)
mjr 87:8d35c74403af 6642 tlc59116->enable(true);
mjr 54:fd77a6b2f76c 6643 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6644 {
mjr 55:4db125cd11a0 6645 hc595->enable(true);
mjr 54:fd77a6b2f76c 6646 hc595->update(true);
mjr 51:57eb311faafa 6647 }
mjr 48:058ace2aed1d 6648 }
mjr 43:7a6364d82a41 6649
mjr 6:cc35eb643e8f 6650 // provide a visual status indication on the on-board LED
mjr 48:058ace2aed1d 6651 if (calBtnState < 2 && hbTimer.read_us() > 1000000)
mjr 1:d913e0afb2ac 6652 {
mjr 54:fd77a6b2f76c 6653 if (jsOKTimer.read_us() > 1000000)
mjr 38:091e511ce8a0 6654 {
mjr 39:b3815a1c3802 6655 // USB freeze - show red/yellow.
mjr 40:cc0d9814522b 6656 //
mjr 54:fd77a6b2f76c 6657 // It's been more than a second since we successfully sent a joystick
mjr 54:fd77a6b2f76c 6658 // update message. This must mean that something's wrong on the USB
mjr 54:fd77a6b2f76c 6659 // connection, even though we haven't detected an outright disconnect.
mjr 54:fd77a6b2f76c 6660 // Show a distinctive diagnostic LED pattern when this occurs.
mjr 38:091e511ce8a0 6661 hb = !hb;
mjr 38:091e511ce8a0 6662 diagLED(1, hb, 0);
mjr 54:fd77a6b2f76c 6663
mjr 54:fd77a6b2f76c 6664 // If the reboot-on-disconnect option is in effect, treat this condition
mjr 54:fd77a6b2f76c 6665 // as equivalent to a disconnect, since something is obviously wrong
mjr 54:fd77a6b2f76c 6666 // with the USB connection.
mjr 54:fd77a6b2f76c 6667 if (cfg.disconnectRebootTimeout != 0)
mjr 54:fd77a6b2f76c 6668 {
mjr 54:fd77a6b2f76c 6669 // The reboot timeout is in effect. If we've been incommunicado for
mjr 54:fd77a6b2f76c 6670 // longer than the timeout, reboot. If we haven't reached the time
mjr 54:fd77a6b2f76c 6671 // limit, keep running for now, and leave the OK timer running so
mjr 86:e30a1f60f783 6672 // that we can continue to monitor this. Only reboot if the PSU2
mjr 86:e30a1f60f783 6673 // power status allows it.
mjr 86:e30a1f60f783 6674 if (jsOKTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 6675 && powerStatusAllowsReboot())
mjr 54:fd77a6b2f76c 6676 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 6677 }
mjr 54:fd77a6b2f76c 6678 else
mjr 54:fd77a6b2f76c 6679 {
mjr 54:fd77a6b2f76c 6680 // There's no reboot timer, so just keep running with the diagnostic
mjr 54:fd77a6b2f76c 6681 // pattern displayed. Since we're not waiting for any other timed
mjr 54:fd77a6b2f76c 6682 // conditions in this state, stop the timer so that it doesn't
mjr 54:fd77a6b2f76c 6683 // overflow if this condition persists for a long time.
mjr 54:fd77a6b2f76c 6684 jsOKTimer.stop();
mjr 54:fd77a6b2f76c 6685 }
mjr 38:091e511ce8a0 6686 }
mjr 73:4e8ce0b18915 6687 else if (psu2_state >= 4)
mjr 73:4e8ce0b18915 6688 {
mjr 73:4e8ce0b18915 6689 // We're in the TV timer countdown. Skip the normal heartbeat
mjr 73:4e8ce0b18915 6690 // flashes and show the TV timer flashes instead.
mjr 73:4e8ce0b18915 6691 diagLED(0, 0, 0);
mjr 73:4e8ce0b18915 6692 }
mjr 35:e959ffba78fd 6693 else if (cfg.plunger.enabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 6694 {
mjr 6:cc35eb643e8f 6695 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 6696 hb = !hb;
mjr 38:091e511ce8a0 6697 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 6698 }
mjr 6:cc35eb643e8f 6699 else
mjr 6:cc35eb643e8f 6700 {
mjr 6:cc35eb643e8f 6701 // connected - flash blue/green
mjr 2:c174f9ee414a 6702 hb = !hb;
mjr 38:091e511ce8a0 6703 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 6704 }
mjr 1:d913e0afb2ac 6705
mjr 1:d913e0afb2ac 6706 // reset the heartbeat timer
mjr 1:d913e0afb2ac 6707 hbTimer.reset();
mjr 5:a70c0bce770d 6708 ++hbcnt;
mjr 1:d913e0afb2ac 6709 }
mjr 74:822a92bc11d2 6710
mjr 74:822a92bc11d2 6711 // collect statistics on the main loop time, if desired
mjr 74:822a92bc11d2 6712 IF_DIAG(
mjr 76:7f5912b6340e 6713 mainLoopIterTime += mainLoopTimer.read_us();
mjr 74:822a92bc11d2 6714 mainLoopIterCount++;
mjr 74:822a92bc11d2 6715 )
mjr 1:d913e0afb2ac 6716 }
mjr 0:5acbbe3f4cf4 6717 }