Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Sat Mar 02 21:05:43 2019 +0000
Revision:
99:8139b0c274f4
Parent:
98:4df3c0f7e707
Child:
100:1ff35c07217c
Added Chime Logic

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 99:8139b0c274f4 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 5:a70c0bce770d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 5:a70c0bce770d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 5:a70c0bce770d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 5:a70c0bce770d 17 */
mjr 5:a70c0bce770d 18
mjr 5:a70c0bce770d 19 //
mjr 35:e959ffba78fd 20 // The Pinscape Controller
mjr 35:e959ffba78fd 21 // A comprehensive input/output controller for virtual pinball machines
mjr 5:a70c0bce770d 22 //
mjr 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 98:4df3c0f7e707 872 // remember 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 99:8139b0c274f4 1069 // Figure the initial full-power time in microseconds: 50ms * (1+N),
mjr 99:8139b0c274f4 1070 // where N is the high 4 bits of the parameter byte.
mjr 99:8139b0c274f4 1071 inline uint32_t fullPowerTime_us() const { return 50000*(1 + ((params >> 4) & 0x0F)); }
mjr 89:c43cd923401c 1072
mjr 89:c43cd923401c 1073 // Figure the hold power PWM level (0-255)
mjr 89:c43cd923401c 1074 inline uint8_t holdPower() const { return (params & 0x0F) * 17; }
mjr 89:c43cd923401c 1075
mjr 89:c43cd923401c 1076 // Timer. This is a shared timer for all of the FL ports. When we
mjr 89:c43cd923401c 1077 // transition from OFF to ON, we note the current time on this timer
mjr 89:c43cd923401c 1078 // (which runs continuously).
mjr 89:c43cd923401c 1079 static Timer timer;
mjr 89:c43cd923401c 1080
mjr 89:c43cd923401c 1081 // Flipper logic pending timer list. Whenever a flipper logic output
mjr 98:4df3c0f7e707 1082 // transitions from OFF to ON, we add it to this list. We scan the
mjr 98:4df3c0f7e707 1083 // list in our polling routine to find ports that have reached the
mjr 98:4df3c0f7e707 1084 // expiration of their initial full-power intervals.
mjr 89:c43cd923401c 1085 static LwFlipperLogicOut **pending;
mjr 89:c43cd923401c 1086 static uint8_t nPending;
mjr 89:c43cd923401c 1087 };
mjr 89:c43cd923401c 1088
mjr 89:c43cd923401c 1089 // Flipper Logic statics
mjr 89:c43cd923401c 1090 Timer LwFlipperLogicOut::timer;
mjr 89:c43cd923401c 1091 LwFlipperLogicOut **LwFlipperLogicOut::pending;
mjr 89:c43cd923401c 1092 uint8_t LwFlipperLogicOut::nPending;
mjr 99:8139b0c274f4 1093
mjr 99:8139b0c274f4 1094 // Chime Logic. This is a filter output that we layer on a physical
mjr 99:8139b0c274f4 1095 // output to set a minimum and maximum ON time for the output.
mjr 99:8139b0c274f4 1096 class LwChimeLogicOut: public LwOut
mjr 98:4df3c0f7e707 1097 {
mjr 98:4df3c0f7e707 1098 public:
mjr 99:8139b0c274f4 1099 // Set up the output. 'params' encodes the minimum and maximum time.
mjr 99:8139b0c274f4 1100 LwChimeLogicOut(LwOut *o, uint8_t params)
mjr 99:8139b0c274f4 1101 : out(o), params(params)
mjr 98:4df3c0f7e707 1102 {
mjr 98:4df3c0f7e707 1103 // initially OFF
mjr 98:4df3c0f7e707 1104 state = 0;
mjr 98:4df3c0f7e707 1105 }
mjr 98:4df3c0f7e707 1106
mjr 98:4df3c0f7e707 1107 virtual void set(uint8_t level)
mjr 98:4df3c0f7e707 1108 {
mjr 98:4df3c0f7e707 1109 // update the physical output according to our current timing state
mjr 98:4df3c0f7e707 1110 switch (state)
mjr 98:4df3c0f7e707 1111 {
mjr 98:4df3c0f7e707 1112 case 0:
mjr 98:4df3c0f7e707 1113 // We're currently off. If the new level is non-zero, switch
mjr 98:4df3c0f7e707 1114 // to state 1 (initial minimum interval) and set the requested
mjr 98:4df3c0f7e707 1115 // level. If the new level is zero, we're switching from off to
mjr 98:4df3c0f7e707 1116 // off, so there's no change.
mjr 98:4df3c0f7e707 1117 if (level != 0)
mjr 98:4df3c0f7e707 1118 {
mjr 98:4df3c0f7e707 1119 // switch to state 1 (initial minimum interval, port is
mjr 98:4df3c0f7e707 1120 // logically on)
mjr 98:4df3c0f7e707 1121 state = 1;
mjr 98:4df3c0f7e707 1122
mjr 98:4df3c0f7e707 1123 // set the requested output level
mjr 98:4df3c0f7e707 1124 out->set(level);
mjr 98:4df3c0f7e707 1125
mjr 98:4df3c0f7e707 1126 // add myself to the pending timer list
mjr 98:4df3c0f7e707 1127 pending[nPending++] = this;
mjr 98:4df3c0f7e707 1128
mjr 98:4df3c0f7e707 1129 // note the starting time
mjr 98:4df3c0f7e707 1130 t0 = timer.read_us();
mjr 98:4df3c0f7e707 1131 }
mjr 98:4df3c0f7e707 1132 break;
mjr 98:4df3c0f7e707 1133
mjr 98:4df3c0f7e707 1134 case 1: // min ON interval, port on
mjr 98:4df3c0f7e707 1135 case 2: // min ON interval, port off
mjr 98:4df3c0f7e707 1136 // We're in the initial minimum ON interval. If the new power
mjr 98:4df3c0f7e707 1137 // level is non-zero, pass it through to the physical port, since
mjr 98:4df3c0f7e707 1138 // the client is allowed to change the power level during the
mjr 98:4df3c0f7e707 1139 // initial ON interval - they just can't turn it off entirely.
mjr 98:4df3c0f7e707 1140 // Set the state to 1 to indicate that the logical port is on.
mjr 98:4df3c0f7e707 1141 //
mjr 98:4df3c0f7e707 1142 // If the new level is zero, leave the underlying port at its
mjr 98:4df3c0f7e707 1143 // current power level, since we're not allowed to turn it off
mjr 98:4df3c0f7e707 1144 // during this period. Set the state to 2 to indicate that the
mjr 98:4df3c0f7e707 1145 // logical port is off even though the physical port has to stay
mjr 98:4df3c0f7e707 1146 // on for the remainder of the interval.
mjr 98:4df3c0f7e707 1147 if (level != 0)
mjr 98:4df3c0f7e707 1148 {
mjr 98:4df3c0f7e707 1149 // client is leaving the port on - pass through the new
mjr 98:4df3c0f7e707 1150 // power level and set state 1 (logically on)
mjr 98:4df3c0f7e707 1151 out->set(level);
mjr 98:4df3c0f7e707 1152 state = 1;
mjr 98:4df3c0f7e707 1153 }
mjr 98:4df3c0f7e707 1154 else
mjr 98:4df3c0f7e707 1155 {
mjr 98:4df3c0f7e707 1156 // Client is turning off the port - leave the underlying port
mjr 98:4df3c0f7e707 1157 // on at its current level and set state 2 (logically off).
mjr 98:4df3c0f7e707 1158 // When the minimum ON time expires, the polling routine will
mjr 98:4df3c0f7e707 1159 // see that we're logically off and will pass that through to
mjr 98:4df3c0f7e707 1160 // the underlying physical port. Until then, though, we have
mjr 98:4df3c0f7e707 1161 // to leave the physical port on to satisfy the minimum ON
mjr 98:4df3c0f7e707 1162 // time requirement.
mjr 98:4df3c0f7e707 1163 state = 2;
mjr 98:4df3c0f7e707 1164 }
mjr 98:4df3c0f7e707 1165 break;
mjr 98:4df3c0f7e707 1166
mjr 98:4df3c0f7e707 1167 case 3:
mjr 99:8139b0c274f4 1168 // We're after the minimum ON interval and before the maximum
mjr 99:8139b0c274f4 1169 // ON time limit. We can set any new level, including fully off.
mjr 99:8139b0c274f4 1170 // Pass the new power level through to the port.
mjr 98:4df3c0f7e707 1171 out->set(level);
mjr 98:4df3c0f7e707 1172
mjr 98:4df3c0f7e707 1173 // if the port is now off, return to state 0 (OFF)
mjr 98:4df3c0f7e707 1174 if (level == 0)
mjr 99:8139b0c274f4 1175 {
mjr 99:8139b0c274f4 1176 // return to the OFF state
mjr 99:8139b0c274f4 1177 state = 0;
mjr 99:8139b0c274f4 1178
mjr 99:8139b0c274f4 1179 // If we have a timer pending, remove it. A timer will be
mjr 99:8139b0c274f4 1180 // pending if we have a non-infinite maximum on time for the
mjr 99:8139b0c274f4 1181 // port.
mjr 99:8139b0c274f4 1182 for (int i = 0 ; i < nPending ; ++i)
mjr 99:8139b0c274f4 1183 {
mjr 99:8139b0c274f4 1184 // is this us?
mjr 99:8139b0c274f4 1185 if (pending[i] == this)
mjr 99:8139b0c274f4 1186 {
mjr 99:8139b0c274f4 1187 // remove myself by replacing the slot with the
mjr 99:8139b0c274f4 1188 // last list entry
mjr 99:8139b0c274f4 1189 pending[i] = pending[--nPending];
mjr 99:8139b0c274f4 1190
mjr 99:8139b0c274f4 1191 // no need to look any further
mjr 99:8139b0c274f4 1192 break;
mjr 99:8139b0c274f4 1193 }
mjr 99:8139b0c274f4 1194 }
mjr 99:8139b0c274f4 1195 }
mjr 99:8139b0c274f4 1196 break;
mjr 99:8139b0c274f4 1197
mjr 99:8139b0c274f4 1198 case 4:
mjr 99:8139b0c274f4 1199 // We're after the maximum ON time. The physical port stays off
mjr 99:8139b0c274f4 1200 // during this interval, so we don't pass any changes through to
mjr 99:8139b0c274f4 1201 // the physical port. When the client sets the level to 0, we
mjr 99:8139b0c274f4 1202 // turn off the logical port and reset to state 0.
mjr 99:8139b0c274f4 1203 if (level == 0)
mjr 98:4df3c0f7e707 1204 state = 0;
mjr 98:4df3c0f7e707 1205 break;
mjr 98:4df3c0f7e707 1206 }
mjr 98:4df3c0f7e707 1207 }
mjr 98:4df3c0f7e707 1208
mjr 98:4df3c0f7e707 1209 // Class initialization
mjr 98:4df3c0f7e707 1210 static void classInit(Config &cfg)
mjr 98:4df3c0f7e707 1211 {
mjr 98:4df3c0f7e707 1212 // Count the Minimum On Time outputs in the configuration. We
mjr 98:4df3c0f7e707 1213 // need to allocate enough pending timer list space to accommodate
mjr 98:4df3c0f7e707 1214 // all of these outputs.
mjr 98:4df3c0f7e707 1215 int n = 0;
mjr 98:4df3c0f7e707 1216 for (int i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 98:4df3c0f7e707 1217 {
mjr 98:4df3c0f7e707 1218 // if this port is active and marked as Flipper Logic, count it
mjr 98:4df3c0f7e707 1219 if (cfg.outPort[i].typ != PortTypeDisabled
mjr 99:8139b0c274f4 1220 && (cfg.outPort[i].flags & PortFlagChimeLogic) != 0)
mjr 98:4df3c0f7e707 1221 ++n;
mjr 98:4df3c0f7e707 1222 }
mjr 98:4df3c0f7e707 1223
mjr 98:4df3c0f7e707 1224 // allocate space for the pending timer list
mjr 99:8139b0c274f4 1225 pending = new LwChimeLogicOut*[n];
mjr 98:4df3c0f7e707 1226
mjr 98:4df3c0f7e707 1227 // there's nothing in the pending list yet
mjr 98:4df3c0f7e707 1228 nPending = 0;
mjr 98:4df3c0f7e707 1229
mjr 98:4df3c0f7e707 1230 // Start our shared timer. The epoch is arbitrary, since we only
mjr 98:4df3c0f7e707 1231 // use it to figure elapsed times.
mjr 98:4df3c0f7e707 1232 timer.start();
mjr 98:4df3c0f7e707 1233 }
mjr 98:4df3c0f7e707 1234
mjr 98:4df3c0f7e707 1235 // Check for ports with pending timers. The main routine should
mjr 98:4df3c0f7e707 1236 // call this on each iteration to process our state transitions.
mjr 98:4df3c0f7e707 1237 static void poll()
mjr 98:4df3c0f7e707 1238 {
mjr 98:4df3c0f7e707 1239 // note the current time
mjr 98:4df3c0f7e707 1240 uint32_t t = timer.read_us();
mjr 98:4df3c0f7e707 1241
mjr 98:4df3c0f7e707 1242 // go through the timer list
mjr 98:4df3c0f7e707 1243 for (int i = 0 ; i < nPending ; )
mjr 98:4df3c0f7e707 1244 {
mjr 98:4df3c0f7e707 1245 // get the port
mjr 99:8139b0c274f4 1246 LwChimeLogicOut *port = pending[i];
mjr 98:4df3c0f7e707 1247
mjr 98:4df3c0f7e707 1248 // assume we'll keep it
mjr 98:4df3c0f7e707 1249 bool remove = false;
mjr 98:4df3c0f7e707 1250
mjr 99:8139b0c274f4 1251 // check our state
mjr 99:8139b0c274f4 1252 switch (port->state)
mjr 98:4df3c0f7e707 1253 {
mjr 99:8139b0c274f4 1254 case 1: // initial minimum ON time, port logically on
mjr 99:8139b0c274f4 1255 case 2: // initial minimum ON time, port logically off
mjr 99:8139b0c274f4 1256 // check if the minimum ON time has elapsed
mjr 98:4df3c0f7e707 1257 if (uint32_t(t - port->t0) > port->minOnTime_us())
mjr 98:4df3c0f7e707 1258 {
mjr 98:4df3c0f7e707 1259 // This port has completed its initial ON interval, so
mjr 98:4df3c0f7e707 1260 // it advances to the next state.
mjr 98:4df3c0f7e707 1261 if (port->state == 1)
mjr 98:4df3c0f7e707 1262 {
mjr 99:8139b0c274f4 1263 // The port is logically on, so advance to state 3.
mjr 99:8139b0c274f4 1264 // The underlying port is already at its proper level,
mjr 99:8139b0c274f4 1265 // since we pass through non-zero power settings to the
mjr 99:8139b0c274f4 1266 // underlying port throughout the initial minimum time.
mjr 99:8139b0c274f4 1267 // The timer stays active into state 3.
mjr 98:4df3c0f7e707 1268 port->state = 3;
mjr 99:8139b0c274f4 1269
mjr 99:8139b0c274f4 1270 // Special case: maximum on time 0 means "infinite".
mjr 99:8139b0c274f4 1271 // There's no need for a timer in this case; we'll
mjr 99:8139b0c274f4 1272 // just stay in state 3 until the client turns the
mjr 99:8139b0c274f4 1273 // port off.
mjr 99:8139b0c274f4 1274 if (port->maxOnTime_us() == 0)
mjr 99:8139b0c274f4 1275 remove = true;
mjr 98:4df3c0f7e707 1276 }
mjr 98:4df3c0f7e707 1277 else
mjr 98:4df3c0f7e707 1278 {
mjr 98:4df3c0f7e707 1279 // The port was switched off by the client during the
mjr 98:4df3c0f7e707 1280 // minimum ON period. We haven't passed the OFF state
mjr 98:4df3c0f7e707 1281 // to the underlying port yet, because the port has to
mjr 98:4df3c0f7e707 1282 // stay on throughout the minimum ON period. So turn
mjr 98:4df3c0f7e707 1283 // the port off now.
mjr 98:4df3c0f7e707 1284 port->out->set(0);
mjr 98:4df3c0f7e707 1285
mjr 98:4df3c0f7e707 1286 // return to state 0 (OFF)
mjr 98:4df3c0f7e707 1287 port->state = 0;
mjr 99:8139b0c274f4 1288
mjr 99:8139b0c274f4 1289 // we're done with the timer
mjr 99:8139b0c274f4 1290 remove = true;
mjr 98:4df3c0f7e707 1291 }
mjr 99:8139b0c274f4 1292 }
mjr 99:8139b0c274f4 1293 break;
mjr 99:8139b0c274f4 1294
mjr 99:8139b0c274f4 1295 case 3: // between minimum ON time and maximum ON time
mjr 99:8139b0c274f4 1296 // check if the maximum ON time has expired
mjr 99:8139b0c274f4 1297 if (uint32_t(t - port->t0) > port->maxOnTime_us())
mjr 99:8139b0c274f4 1298 {
mjr 99:8139b0c274f4 1299 // The maximum ON time has expired. Turn off the physical
mjr 99:8139b0c274f4 1300 // port.
mjr 99:8139b0c274f4 1301 port->out->set(0);
mjr 98:4df3c0f7e707 1302
mjr 99:8139b0c274f4 1303 // Switch to state 4 (logically ON past maximum time)
mjr 99:8139b0c274f4 1304 port->state = 4;
mjr 99:8139b0c274f4 1305
mjr 99:8139b0c274f4 1306 // Remove the timer on this port. This port simply stays
mjr 99:8139b0c274f4 1307 // in state 4 until the client turns off the port.
mjr 98:4df3c0f7e707 1308 remove = true;
mjr 98:4df3c0f7e707 1309 }
mjr 99:8139b0c274f4 1310 break;
mjr 98:4df3c0f7e707 1311 }
mjr 98:4df3c0f7e707 1312
mjr 98:4df3c0f7e707 1313 // if desired, remove the port from the timer list
mjr 98:4df3c0f7e707 1314 if (remove)
mjr 98:4df3c0f7e707 1315 {
mjr 98:4df3c0f7e707 1316 // Remove the list entry by overwriting the slot with
mjr 98:4df3c0f7e707 1317 // the last entry in the list.
mjr 98:4df3c0f7e707 1318 pending[i] = pending[--nPending];
mjr 98:4df3c0f7e707 1319
mjr 98:4df3c0f7e707 1320 // Note that we don't increment the loop counter, since
mjr 98:4df3c0f7e707 1321 // we now need to revisit this same slot.
mjr 98:4df3c0f7e707 1322 }
mjr 98:4df3c0f7e707 1323 else
mjr 98:4df3c0f7e707 1324 {
mjr 98:4df3c0f7e707 1325 // we're keeping this item; move on to the next one
mjr 98:4df3c0f7e707 1326 ++i;
mjr 98:4df3c0f7e707 1327 }
mjr 98:4df3c0f7e707 1328 }
mjr 98:4df3c0f7e707 1329 }
mjr 98:4df3c0f7e707 1330
mjr 98:4df3c0f7e707 1331 protected:
mjr 98:4df3c0f7e707 1332 // underlying physical output
mjr 98:4df3c0f7e707 1333 LwOut *out;
mjr 98:4df3c0f7e707 1334
mjr 98:4df3c0f7e707 1335 // Timestamp on 'timer' of start of full-power interval. We set this
mjr 98:4df3c0f7e707 1336 // to the current 'timer' timestamp when entering state 1.
mjr 98:4df3c0f7e707 1337 uint32_t t0;
mjr 98:4df3c0f7e707 1338
mjr 98:4df3c0f7e707 1339 // Current port state:
mjr 98:4df3c0f7e707 1340 //
mjr 98:4df3c0f7e707 1341 // 0 = off
mjr 99:8139b0c274f4 1342 // 1 = in initial minimum ON interval, logical port is on
mjr 99:8139b0c274f4 1343 // 2 = in initial minimum ON interval, logical port is off
mjr 99:8139b0c274f4 1344 // 3 = in interval between minimum and maximum ON times
mjr 99:8139b0c274f4 1345 // 4 = after the maximum ON interval
mjr 99:8139b0c274f4 1346 //
mjr 99:8139b0c274f4 1347 // The "logical" on/off state of the port is the state set by the
mjr 99:8139b0c274f4 1348 // client. The "physical" state is the state of the underlying port.
mjr 99:8139b0c274f4 1349 // The relationships between logical and physical port state, and the
mjr 99:8139b0c274f4 1350 // effects of updates by the client, are as follows:
mjr 99:8139b0c274f4 1351 //
mjr 99:8139b0c274f4 1352 // State | Logical | Physical | Client set on | Client set off
mjr 99:8139b0c274f4 1353 // -----------------------------------------------------------
mjr 99:8139b0c274f4 1354 // 0 | Off | Off | phys on, -> 1 | no effect
mjr 99:8139b0c274f4 1355 // 1 | On | On | no effect | -> 2
mjr 99:8139b0c274f4 1356 // 2 | Off | On | -> 1 | no effect
mjr 99:8139b0c274f4 1357 // 3 | On | On | no effect | phys off, -> 0
mjr 99:8139b0c274f4 1358 // 4 | On | On | no effect | phys off, -> 0
mjr 99:8139b0c274f4 1359 //
mjr 99:8139b0c274f4 1360 // The polling routine makes the following transitions when the current
mjr 99:8139b0c274f4 1361 // time limit expires:
mjr 99:8139b0c274f4 1362 //
mjr 99:8139b0c274f4 1363 // 1: at end of minimum ON, -> 3 (or 4 if max == infinity)
mjr 99:8139b0c274f4 1364 // 2: at end of minimum ON, port off, -> 0
mjr 99:8139b0c274f4 1365 // 3: at end of maximum ON, port off, -> 4
mjr 98:4df3c0f7e707 1366 //
mjr 98:4df3c0f7e707 1367 uint8_t state;
mjr 98:4df3c0f7e707 1368
mjr 99:8139b0c274f4 1369 // Configuration parameters byte. This encodes the minimum and maximum
mjr 99:8139b0c274f4 1370 // ON times.
mjr 99:8139b0c274f4 1371 uint8_t params;
mjr 98:4df3c0f7e707 1372
mjr 98:4df3c0f7e707 1373 // Timer. This is a shared timer for all of the minimum ON time ports.
mjr 98:4df3c0f7e707 1374 // When we transition from OFF to ON, we note the current time on this
mjr 98:4df3c0f7e707 1375 // timer to establish the start of our minimum ON period.
mjr 98:4df3c0f7e707 1376 static Timer timer;
mjr 98:4df3c0f7e707 1377
mjr 98:4df3c0f7e707 1378 // translaton table from timing parameter in config to minimum ON time
mjr 98:4df3c0f7e707 1379 static const uint32_t paramToTime_us[];
mjr 98:4df3c0f7e707 1380
mjr 99:8139b0c274f4 1381 // Figure the minimum ON time. The minimum ON time is given by the
mjr 99:8139b0c274f4 1382 // low-order 4 bits of the parameters byte, which serves as an index
mjr 99:8139b0c274f4 1383 // into our time table.
mjr 99:8139b0c274f4 1384 inline uint32_t minOnTime_us() const { return paramToTime_us[params & 0x0F]; }
mjr 99:8139b0c274f4 1385
mjr 99:8139b0c274f4 1386 // Figure the maximum ON time. The maximum time is the high 4 bits
mjr 99:8139b0c274f4 1387 // of the parameters byte. This is an index into our time table, but
mjr 99:8139b0c274f4 1388 // 0 has the special meaning "infinite".
mjr 99:8139b0c274f4 1389 inline uint32_t maxOnTime_us() const { return paramToTime_us[((params >> 4) & 0x0F)]; }
mjr 98:4df3c0f7e707 1390
mjr 98:4df3c0f7e707 1391 // Pending timer list. Whenever one of our ports transitions from OFF
mjr 98:4df3c0f7e707 1392 // to ON, we add it to this list. We scan this list in our polling
mjr 98:4df3c0f7e707 1393 // routine to find ports that have reached the ends of their initial
mjr 98:4df3c0f7e707 1394 // ON intervals.
mjr 99:8139b0c274f4 1395 static LwChimeLogicOut **pending;
mjr 98:4df3c0f7e707 1396 static uint8_t nPending;
mjr 98:4df3c0f7e707 1397 };
mjr 98:4df3c0f7e707 1398
mjr 98:4df3c0f7e707 1399 // Min Time Out statics
mjr 99:8139b0c274f4 1400 Timer LwChimeLogicOut::timer;
mjr 99:8139b0c274f4 1401 LwChimeLogicOut **LwChimeLogicOut::pending;
mjr 99:8139b0c274f4 1402 uint8_t LwChimeLogicOut::nPending;
mjr 99:8139b0c274f4 1403 const uint32_t LwChimeLogicOut::paramToTime_us[] = {
mjr 99:8139b0c274f4 1404 0, // for the max time, this means "infinite"
mjr 98:4df3c0f7e707 1405 1000,
mjr 98:4df3c0f7e707 1406 2000,
mjr 98:4df3c0f7e707 1407 5000,
mjr 98:4df3c0f7e707 1408 10000,
mjr 98:4df3c0f7e707 1409 20000,
mjr 98:4df3c0f7e707 1410 40000,
mjr 98:4df3c0f7e707 1411 80000,
mjr 98:4df3c0f7e707 1412 100000,
mjr 98:4df3c0f7e707 1413 200000,
mjr 98:4df3c0f7e707 1414 300000,
mjr 98:4df3c0f7e707 1415 400000,
mjr 98:4df3c0f7e707 1416 500000,
mjr 98:4df3c0f7e707 1417 600000,
mjr 98:4df3c0f7e707 1418 700000,
mjr 98:4df3c0f7e707 1419 800000
mjr 98:4df3c0f7e707 1420 };
mjr 89:c43cd923401c 1421
mjr 35:e959ffba78fd 1422 //
mjr 35:e959ffba78fd 1423 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 1424 // assignments set in config.h.
mjr 33:d832bcab089e 1425 //
mjr 35:e959ffba78fd 1426 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 1427 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 1428 {
mjr 35:e959ffba78fd 1429 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 1430 {
mjr 53:9b2611964afc 1431 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 1432 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 1433 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 1434 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 1435 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 1436 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 1437 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 1438 }
mjr 35:e959ffba78fd 1439 }
mjr 26:cb71c4af2912 1440
mjr 40:cc0d9814522b 1441 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 1442 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 1443 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 1444 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 1445 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 1446 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 1447 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 1448 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 1449 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 1450 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 1451 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 1452 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 1453 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 1454 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 1455 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 1456 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 1457 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 1458 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 1459 };
mjr 40:cc0d9814522b 1460
mjr 40:cc0d9814522b 1461 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 1462 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 1463 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 1464 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 1465 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 1466 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 1467 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 1468 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 1469 // are always 8 bits.
mjr 40:cc0d9814522b 1470 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 1471 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 1472 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 1473 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 1474 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 1475 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 1476 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 1477 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 1478 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 1479 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 1480 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 1481 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 1482 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 1483 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 1484 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 1485 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 1486 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 1487 };
mjr 40:cc0d9814522b 1488
mjr 26:cb71c4af2912 1489 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 1490 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 1491 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 1492 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 1493 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 1494 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 1495 {
mjr 26:cb71c4af2912 1496 public:
mjr 60:f38da020aa13 1497 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1498 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 1499 {
mjr 26:cb71c4af2912 1500 if (val != prv)
mjr 40:cc0d9814522b 1501 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 1502 }
mjr 60:f38da020aa13 1503 uint8_t idx;
mjr 40:cc0d9814522b 1504 uint8_t prv;
mjr 26:cb71c4af2912 1505 };
mjr 26:cb71c4af2912 1506
mjr 40:cc0d9814522b 1507 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 1508 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 1509 {
mjr 40:cc0d9814522b 1510 public:
mjr 60:f38da020aa13 1511 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1512 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 1513 {
mjr 40:cc0d9814522b 1514 if (val != prv)
mjr 40:cc0d9814522b 1515 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 1516 }
mjr 60:f38da020aa13 1517 uint8_t idx;
mjr 40:cc0d9814522b 1518 uint8_t prv;
mjr 40:cc0d9814522b 1519 };
mjr 40:cc0d9814522b 1520
mjr 87:8d35c74403af 1521 //
mjr 87:8d35c74403af 1522 // TLC59116 interface object
mjr 87:8d35c74403af 1523 //
mjr 87:8d35c74403af 1524 TLC59116 *tlc59116 = 0;
mjr 87:8d35c74403af 1525 void init_tlc59116(Config &cfg)
mjr 87:8d35c74403af 1526 {
mjr 87:8d35c74403af 1527 // Create the interface if any chips are enabled
mjr 87:8d35c74403af 1528 if (cfg.tlc59116.chipMask != 0)
mjr 87:8d35c74403af 1529 {
mjr 87:8d35c74403af 1530 // set up the interface
mjr 87:8d35c74403af 1531 tlc59116 = new TLC59116(
mjr 87:8d35c74403af 1532 wirePinName(cfg.tlc59116.sda),
mjr 87:8d35c74403af 1533 wirePinName(cfg.tlc59116.scl),
mjr 87:8d35c74403af 1534 wirePinName(cfg.tlc59116.reset));
mjr 87:8d35c74403af 1535
mjr 87:8d35c74403af 1536 // initialize the chips
mjr 87:8d35c74403af 1537 tlc59116->init();
mjr 87:8d35c74403af 1538 }
mjr 87:8d35c74403af 1539 }
mjr 87:8d35c74403af 1540
mjr 87:8d35c74403af 1541 // LwOut class for TLC59116 outputs. The 'addr' value in the constructor
mjr 87:8d35c74403af 1542 // is low 4 bits of the chip's I2C address; this is the part of the address
mjr 87:8d35c74403af 1543 // that's configurable per chip. 'port' is the output number on the chip
mjr 87:8d35c74403af 1544 // (0-15).
mjr 87:8d35c74403af 1545 //
mjr 87:8d35c74403af 1546 // Note that we don't need a separate gamma-corrected subclass for this
mjr 87:8d35c74403af 1547 // output type, since there's no loss of precision with the standard layered
mjr 87:8d35c74403af 1548 // gamma (it emits 8-bit values, and we take 8-bit inputs).
mjr 87:8d35c74403af 1549 class Lw59116Out: public LwOut
mjr 87:8d35c74403af 1550 {
mjr 87:8d35c74403af 1551 public:
mjr 87:8d35c74403af 1552 Lw59116Out(uint8_t addr, uint8_t port) : addr(addr), port(port) { prv = 0; }
mjr 87:8d35c74403af 1553 virtual void set(uint8_t val)
mjr 87:8d35c74403af 1554 {
mjr 87:8d35c74403af 1555 if (val != prv)
mjr 87:8d35c74403af 1556 tlc59116->set(addr, port, prv = val);
mjr 87:8d35c74403af 1557 }
mjr 87:8d35c74403af 1558
mjr 87:8d35c74403af 1559 protected:
mjr 87:8d35c74403af 1560 uint8_t addr;
mjr 87:8d35c74403af 1561 uint8_t port;
mjr 87:8d35c74403af 1562 uint8_t prv;
mjr 87:8d35c74403af 1563 };
mjr 87:8d35c74403af 1564
mjr 87:8d35c74403af 1565
mjr 87:8d35c74403af 1566 //
mjr 34:6b981a2afab7 1567 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 1568 // config.h.
mjr 87:8d35c74403af 1569 //
mjr 35:e959ffba78fd 1570 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 1571
mjr 35:e959ffba78fd 1572 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 1573 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 1574 {
mjr 35:e959ffba78fd 1575 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 1576 {
mjr 53:9b2611964afc 1577 hc595 = new HC595(
mjr 53:9b2611964afc 1578 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 1579 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 1580 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 1581 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 1582 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 1583 hc595->init();
mjr 35:e959ffba78fd 1584 hc595->update();
mjr 35:e959ffba78fd 1585 }
mjr 35:e959ffba78fd 1586 }
mjr 34:6b981a2afab7 1587
mjr 34:6b981a2afab7 1588 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 1589 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 1590 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 1591 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 1592 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 1593 class Lw595Out: public LwOut
mjr 33:d832bcab089e 1594 {
mjr 33:d832bcab089e 1595 public:
mjr 60:f38da020aa13 1596 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1597 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 1598 {
mjr 34:6b981a2afab7 1599 if (val != prv)
mjr 40:cc0d9814522b 1600 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 1601 }
mjr 60:f38da020aa13 1602 uint8_t idx;
mjr 40:cc0d9814522b 1603 uint8_t prv;
mjr 33:d832bcab089e 1604 };
mjr 33:d832bcab089e 1605
mjr 26:cb71c4af2912 1606
mjr 40:cc0d9814522b 1607
mjr 64:ef7ca92dff36 1608 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 1609 // normalized to 0.0 to 1.0 scale.
mjr 74:822a92bc11d2 1610 static const float dof_to_pwm[] = {
mjr 64:ef7ca92dff36 1611 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 1612 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 1613 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 1614 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 1615 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 1616 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 1617 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 1618 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 1619 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 1620 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 1621 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 1622 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 1623 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 1624 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 1625 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 1626 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 1627 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 1628 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 1629 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 1630 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 1631 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 1632 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 1633 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 1634 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 1635 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 1636 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 1637 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 1638 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 1639 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 1640 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 1641 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 1642 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 1643 };
mjr 26:cb71c4af2912 1644
mjr 64:ef7ca92dff36 1645
mjr 92:f264fbaa1be5 1646 // Conversion table for 8-bit DOF level to pulse width, with gamma correction
mjr 92:f264fbaa1be5 1647 // pre-calculated. The values are normalized duty cycles from 0.0 to 1.0.
mjr 92:f264fbaa1be5 1648 // Note that we could use the layered gamma output on top of the regular
mjr 92:f264fbaa1be5 1649 // LwPwmOut class for this instead of a separate table, but we get much better
mjr 92:f264fbaa1be5 1650 // precision with a dedicated table, because we apply gamma correction to the
mjr 92:f264fbaa1be5 1651 // actual duty cycle values (as 'float') rather than the 8-bit DOF values.
mjr 64:ef7ca92dff36 1652 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 1653 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 1654 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 1655 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 1656 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 1657 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 1658 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 1659 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 1660 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 1661 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 1662 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 1663 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 1664 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 1665 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 1666 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 1667 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 1668 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 1669 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 1670 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 1671 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 1672 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 1673 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 1674 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 1675 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 1676 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 1677 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 1678 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 1679 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 1680 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 1681 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 1682 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 1683 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 1684 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 1685 };
mjr 64:ef7ca92dff36 1686
mjr 77:0b96f6867312 1687 // Polled-update PWM output list
mjr 74:822a92bc11d2 1688 //
mjr 77:0b96f6867312 1689 // This is a workaround for a KL25Z hardware bug/limitation. The bug (more
mjr 77:0b96f6867312 1690 // about this below) is that we can't write to a PWM output "value" register
mjr 77:0b96f6867312 1691 // more than once per PWM cycle; if we do, outputs after the first are lost.
mjr 77:0b96f6867312 1692 // The value register controls the duty cycle, so it's what you have to write
mjr 77:0b96f6867312 1693 // if you want to update the brightness of an output.
mjr 74:822a92bc11d2 1694 //
mjr 92:f264fbaa1be5 1695 // The symptom of the problem, if it's not worked around somehow, is that
mjr 92:f264fbaa1be5 1696 // an output will get "stuck" due to a missed write. This is especially
mjr 92:f264fbaa1be5 1697 // noticeable during a series of updates such as a fade. If the last
mjr 92:f264fbaa1be5 1698 // couple of updates in a fade are lost, the output will get stuck at some
mjr 92:f264fbaa1be5 1699 // value above or below the desired final value. The stuck setting will
mjr 92:f264fbaa1be5 1700 // persist until the output is deliberately changed again later.
mjr 92:f264fbaa1be5 1701 //
mjr 92:f264fbaa1be5 1702 // Our solution: Simply repeat all PWM updates periodically. This way, any
mjr 92:f264fbaa1be5 1703 // lost write will *eventually* take hold on one of the repeats. Repeats of
mjr 92:f264fbaa1be5 1704 // the same value won't change anything and thus won't be noticeable. We do
mjr 92:f264fbaa1be5 1705 // these periodic updates during the main loop, which makes them very low
mjr 92:f264fbaa1be5 1706 // overhead (there's no interrupt overhead; we just do them when convenient
mjr 92:f264fbaa1be5 1707 // in the main loop), and also makes them very frequent. The frequency
mjr 92:f264fbaa1be5 1708 // is crucial because it ensures that updates will never be lost for long
mjr 92:f264fbaa1be5 1709 // enough to become noticeable.
mjr 92:f264fbaa1be5 1710 //
mjr 92:f264fbaa1be5 1711 // The mbed library has its own, different solution to this bug, but the
mjr 92:f264fbaa1be5 1712 // mbed solution isn't really a solution at all because it creates a separate
mjr 92:f264fbaa1be5 1713 // problem of its own. The mbed approach is reset the TPM "count" register
mjr 92:f264fbaa1be5 1714 // on every value register write. The count reset truncates the current
mjr 92:f264fbaa1be5 1715 // PWM cycle, which bypasses the hardware problem. Remember, the hardware
mjr 92:f264fbaa1be5 1716 // problem is that you can only write once per cycle; the mbed "solution" gets
mjr 92:f264fbaa1be5 1717 // around that by making sure the cycle ends immediately after the write.
mjr 92:f264fbaa1be5 1718 // The problem with this approach is that the truncated cycle causes visible
mjr 92:f264fbaa1be5 1719 // flicker if the output is connected to an LED. This is particularly
mjr 92:f264fbaa1be5 1720 // noticeable during fades, when we're updating the value register repeatedly
mjr 92:f264fbaa1be5 1721 // and rapidly: an attempt to fade from fully on to fully off causes rapid
mjr 92:f264fbaa1be5 1722 // fluttering and flashing rather than a smooth brightness fade. That's why
mjr 92:f264fbaa1be5 1723 // I had to come up with something different - the mbed solution just trades
mjr 92:f264fbaa1be5 1724 // one annoying bug for another that's just as bad.
mjr 92:f264fbaa1be5 1725 //
mjr 92:f264fbaa1be5 1726 // The hardware bug, by the way, is a case of good intentions gone bad.
mjr 92:f264fbaa1be5 1727 // The whole point of the staging register is to make things easier for
mjr 92:f264fbaa1be5 1728 // us software writers. In most PWM hardware, software has to coordinate
mjr 92:f264fbaa1be5 1729 // with the PWM duty cycle when updating registers to avoid a glitch that
mjr 92:f264fbaa1be5 1730 // you'd get by scribbling to the duty cycle register mid-cycle. The
mjr 92:f264fbaa1be5 1731 // staging register solves this by letting the software write an update at
mjr 92:f264fbaa1be5 1732 // any time, knowing that the hardware will apply the update at exactly the
mjr 92:f264fbaa1be5 1733 // end of the cycle, ensuring glitch-free updates. It's a great design,
mjr 92:f264fbaa1be5 1734 // except that it doesn't quite work. The problem is that they implemented
mjr 92:f264fbaa1be5 1735 // this clever staging register as a one-element FIFO that refuses any more
mjr 92:f264fbaa1be5 1736 // writes when full. That is, writing a value to the FIFO fills it; once
mjr 92:f264fbaa1be5 1737 // full, it ignores writes until it gets emptied out. How's it emptied out?
mjr 92:f264fbaa1be5 1738 // By the hardware moving the staged value to the real register. Sadly, they
mjr 92:f264fbaa1be5 1739 // didn't provide any way for the software to clear the register, and no way
mjr 92:f264fbaa1be5 1740 // to even tell that it's full. So we don't have glitches on write, but we're
mjr 92:f264fbaa1be5 1741 // back to the original problem that the software has to be aware of the PWM
mjr 92:f264fbaa1be5 1742 // cycle timing, because the only way for the software to know that a write
mjr 92:f264fbaa1be5 1743 // actually worked is to know that it's been at least one PWM cycle since the
mjr 92:f264fbaa1be5 1744 // last write. That largely defeats the whole purpose of the staging register,
mjr 92:f264fbaa1be5 1745 // since the whole point was to free software writers of these timing
mjr 92:f264fbaa1be5 1746 // considerations. It's still an improvement over no staging register at
mjr 92:f264fbaa1be5 1747 // all, since we at least don't have to worry about glitches, but it leaves
mjr 92:f264fbaa1be5 1748 // us with this somewhat similar hassle.
mjr 74:822a92bc11d2 1749 //
mjr 77:0b96f6867312 1750 // So here we have our list of PWM outputs that need to be polled for updates.
mjr 77:0b96f6867312 1751 // The KL25Z hardware only has 10 PWM channels, so we only need a fixed set
mjr 77:0b96f6867312 1752 // of polled items.
mjr 74:822a92bc11d2 1753 static int numPolledPwm;
mjr 74:822a92bc11d2 1754 static class LwPwmOut *polledPwm[10];
mjr 74:822a92bc11d2 1755
mjr 74:822a92bc11d2 1756 // LwOut class for a PWM-capable GPIO port.
mjr 6:cc35eb643e8f 1757 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 1758 {
mjr 6:cc35eb643e8f 1759 public:
mjr 43:7a6364d82a41 1760 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1761 {
mjr 77:0b96f6867312 1762 // add myself to the list of polled outputs for periodic updates
mjr 77:0b96f6867312 1763 if (numPolledPwm < countof(polledPwm))
mjr 74:822a92bc11d2 1764 polledPwm[numPolledPwm++] = this;
mjr 93:177832c29041 1765
mjr 94:0476b3e2b996 1766 // IMPORTANT: Do not set the PWM period (frequency) here explicitly.
mjr 94:0476b3e2b996 1767 // We instead want to accept the current setting for the TPM unit
mjr 94:0476b3e2b996 1768 // we're assigned to. The KL25Z hardware can only set the period at
mjr 94:0476b3e2b996 1769 // the TPM unit level, not per channel, so if we changed the frequency
mjr 94:0476b3e2b996 1770 // here, we'd change it for everything attached to our TPM unit. LW
mjr 94:0476b3e2b996 1771 // outputs don't care about frequency other than that it's fast enough
mjr 94:0476b3e2b996 1772 // that attached LEDs won't flicker. Some other PWM users (IR remote,
mjr 94:0476b3e2b996 1773 // TLC5940) DO care about exact frequencies, because they use the PWM
mjr 94:0476b3e2b996 1774 // as a signal generator rather than merely for brightness control.
mjr 94:0476b3e2b996 1775 // If we changed the frequency here, we could clobber one of those
mjr 94:0476b3e2b996 1776 // carefully chosen frequencies and break the other subsystem. So
mjr 94:0476b3e2b996 1777 // we need to be the "free variable" here and accept whatever setting
mjr 94:0476b3e2b996 1778 // is currently on our assigned unit. To minimize flicker, the main()
mjr 94:0476b3e2b996 1779 // entrypoint sets a default PWM rate of 1kHz on all channels. All
mjr 94:0476b3e2b996 1780 // of the other subsystems that might set specific frequencies will
mjr 94:0476b3e2b996 1781 // set much high frequencies, so that should only be good for us.
mjr 94:0476b3e2b996 1782
mjr 94:0476b3e2b996 1783 // set the initial brightness value
mjr 77:0b96f6867312 1784 set(initVal);
mjr 43:7a6364d82a41 1785 }
mjr 74:822a92bc11d2 1786
mjr 40:cc0d9814522b 1787 virtual void set(uint8_t val)
mjr 74:822a92bc11d2 1788 {
mjr 77:0b96f6867312 1789 // save the new value
mjr 74:822a92bc11d2 1790 this->val = val;
mjr 77:0b96f6867312 1791
mjr 77:0b96f6867312 1792 // commit it to the hardware
mjr 77:0b96f6867312 1793 commit();
mjr 13:72dda449c3c0 1794 }
mjr 74:822a92bc11d2 1795
mjr 74:822a92bc11d2 1796 // handle periodic update polling
mjr 74:822a92bc11d2 1797 void poll()
mjr 74:822a92bc11d2 1798 {
mjr 77:0b96f6867312 1799 commit();
mjr 74:822a92bc11d2 1800 }
mjr 74:822a92bc11d2 1801
mjr 74:822a92bc11d2 1802 protected:
mjr 77:0b96f6867312 1803 virtual void commit()
mjr 74:822a92bc11d2 1804 {
mjr 74:822a92bc11d2 1805 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1806 p.glitchFreeWrite(dof_to_pwm[val]);
mjr 74:822a92bc11d2 1807 }
mjr 74:822a92bc11d2 1808
mjr 77:0b96f6867312 1809 NewPwmOut p;
mjr 77:0b96f6867312 1810 uint8_t val;
mjr 6:cc35eb643e8f 1811 };
mjr 26:cb71c4af2912 1812
mjr 74:822a92bc11d2 1813 // Gamma corrected PWM GPIO output. This works exactly like the regular
mjr 74:822a92bc11d2 1814 // PWM output, but translates DOF values through the gamma-corrected
mjr 74:822a92bc11d2 1815 // table instead of the regular linear table.
mjr 64:ef7ca92dff36 1816 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1817 {
mjr 64:ef7ca92dff36 1818 public:
mjr 64:ef7ca92dff36 1819 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1820 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1821 {
mjr 64:ef7ca92dff36 1822 }
mjr 74:822a92bc11d2 1823
mjr 74:822a92bc11d2 1824 protected:
mjr 77:0b96f6867312 1825 virtual void commit()
mjr 64:ef7ca92dff36 1826 {
mjr 74:822a92bc11d2 1827 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1828 p.glitchFreeWrite(dof_to_gamma_pwm[val]);
mjr 64:ef7ca92dff36 1829 }
mjr 64:ef7ca92dff36 1830 };
mjr 64:ef7ca92dff36 1831
mjr 74:822a92bc11d2 1832 // poll the PWM outputs
mjr 74:822a92bc11d2 1833 Timer polledPwmTimer;
mjr 76:7f5912b6340e 1834 uint64_t polledPwmTotalTime, polledPwmRunCount;
mjr 74:822a92bc11d2 1835 void pollPwmUpdates()
mjr 74:822a92bc11d2 1836 {
mjr 94:0476b3e2b996 1837 // If it's been long enough since the last update, do another update.
mjr 94:0476b3e2b996 1838 // Note that the time limit is fairly arbitrary: it has to be at least
mjr 94:0476b3e2b996 1839 // 1.5X the PWM period, so that we can be sure that at least one PWM
mjr 94:0476b3e2b996 1840 // period has elapsed since the last update, but there's no hard upper
mjr 94:0476b3e2b996 1841 // bound. Instead, it only has to be short enough that fades don't
mjr 94:0476b3e2b996 1842 // become noticeably chunky. The competing interest is that we don't
mjr 94:0476b3e2b996 1843 // want to do this more often than necessary to provide incremental
mjr 94:0476b3e2b996 1844 // benefit, because the polling adds overhead to the main loop and
mjr 94:0476b3e2b996 1845 // takes time away from other tasks we could be performing. The
mjr 94:0476b3e2b996 1846 // shortest time with practical benefit is probably around 50-60Hz,
mjr 94:0476b3e2b996 1847 // since that gives us "video rate" granularity in fades. Anything
mjr 94:0476b3e2b996 1848 // faster wouldn't probably make fades look any smoother to a human
mjr 94:0476b3e2b996 1849 // viewer.
mjr 94:0476b3e2b996 1850 if (polledPwmTimer.read_us() >= 15000)
mjr 74:822a92bc11d2 1851 {
mjr 74:822a92bc11d2 1852 // time the run for statistics collection
mjr 74:822a92bc11d2 1853 IF_DIAG(
mjr 74:822a92bc11d2 1854 Timer t;
mjr 74:822a92bc11d2 1855 t.start();
mjr 74:822a92bc11d2 1856 )
mjr 74:822a92bc11d2 1857
mjr 74:822a92bc11d2 1858 // poll each output
mjr 74:822a92bc11d2 1859 for (int i = numPolledPwm ; i > 0 ; )
mjr 74:822a92bc11d2 1860 polledPwm[--i]->poll();
mjr 74:822a92bc11d2 1861
mjr 74:822a92bc11d2 1862 // reset the timer for the next cycle
mjr 74:822a92bc11d2 1863 polledPwmTimer.reset();
mjr 74:822a92bc11d2 1864
mjr 74:822a92bc11d2 1865 // collect statistics
mjr 74:822a92bc11d2 1866 IF_DIAG(
mjr 76:7f5912b6340e 1867 polledPwmTotalTime += t.read_us();
mjr 74:822a92bc11d2 1868 polledPwmRunCount += 1;
mjr 74:822a92bc11d2 1869 )
mjr 74:822a92bc11d2 1870 }
mjr 74:822a92bc11d2 1871 }
mjr 64:ef7ca92dff36 1872
mjr 26:cb71c4af2912 1873 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1874 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1875 {
mjr 6:cc35eb643e8f 1876 public:
mjr 43:7a6364d82a41 1877 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1878 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1879 {
mjr 13:72dda449c3c0 1880 if (val != prv)
mjr 40:cc0d9814522b 1881 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1882 }
mjr 6:cc35eb643e8f 1883 DigitalOut p;
mjr 40:cc0d9814522b 1884 uint8_t prv;
mjr 6:cc35eb643e8f 1885 };
mjr 26:cb71c4af2912 1886
mjr 29:582472d0bc57 1887 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1888 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1889 // port n (0-based).
mjr 35:e959ffba78fd 1890 //
mjr 35:e959ffba78fd 1891 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1892 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1893 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1894 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1895 // 74HC595 ports).
mjr 33:d832bcab089e 1896 static int numOutputs;
mjr 33:d832bcab089e 1897 static LwOut **lwPin;
mjr 33:d832bcab089e 1898
mjr 38:091e511ce8a0 1899 // create a single output pin
mjr 53:9b2611964afc 1900 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1901 {
mjr 38:091e511ce8a0 1902 // get this item's values
mjr 38:091e511ce8a0 1903 int typ = pc.typ;
mjr 38:091e511ce8a0 1904 int pin = pc.pin;
mjr 38:091e511ce8a0 1905 int flags = pc.flags;
mjr 40:cc0d9814522b 1906 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1907 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1908 int gamma = flags & PortFlagGamma;
mjr 89:c43cd923401c 1909 int flipperLogic = flags & PortFlagFlipperLogic;
mjr 99:8139b0c274f4 1910 int chimeLogic = flags & PortFlagChimeLogic;
mjr 89:c43cd923401c 1911
mjr 89:c43cd923401c 1912 // cancel gamma on flipper logic ports
mjr 89:c43cd923401c 1913 if (flipperLogic)
mjr 89:c43cd923401c 1914 gamma = false;
mjr 38:091e511ce8a0 1915
mjr 38:091e511ce8a0 1916 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1917 LwOut *lwp;
mjr 38:091e511ce8a0 1918 switch (typ)
mjr 38:091e511ce8a0 1919 {
mjr 38:091e511ce8a0 1920 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1921 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1922 if (pin != 0)
mjr 64:ef7ca92dff36 1923 {
mjr 64:ef7ca92dff36 1924 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 1925 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 1926 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 1927 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 1928 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 1929 {
mjr 64:ef7ca92dff36 1930 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 1931 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 1932
mjr 64:ef7ca92dff36 1933 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 1934 gamma = false;
mjr 64:ef7ca92dff36 1935 }
mjr 64:ef7ca92dff36 1936 else
mjr 64:ef7ca92dff36 1937 {
mjr 64:ef7ca92dff36 1938 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 1939 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 1940 }
mjr 64:ef7ca92dff36 1941 }
mjr 48:058ace2aed1d 1942 else
mjr 48:058ace2aed1d 1943 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1944 break;
mjr 38:091e511ce8a0 1945
mjr 38:091e511ce8a0 1946 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1947 // Digital GPIO port
mjr 48:058ace2aed1d 1948 if (pin != 0)
mjr 48:058ace2aed1d 1949 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1950 else
mjr 48:058ace2aed1d 1951 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1952 break;
mjr 38:091e511ce8a0 1953
mjr 38:091e511ce8a0 1954 case PortTypeTLC5940:
mjr 38:091e511ce8a0 1955 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 1956 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 1957 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 1958 {
mjr 40:cc0d9814522b 1959 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 1960 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 1961 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 1962 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 1963 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 1964 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 1965 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 1966 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 1967 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 1968 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 1969 // for this unlikely case.
mjr 40:cc0d9814522b 1970 if (gamma && !activeLow)
mjr 40:cc0d9814522b 1971 {
mjr 40:cc0d9814522b 1972 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 1973 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 1974
mjr 40:cc0d9814522b 1975 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 1976 gamma = false;
mjr 40:cc0d9814522b 1977 }
mjr 40:cc0d9814522b 1978 else
mjr 40:cc0d9814522b 1979 {
mjr 40:cc0d9814522b 1980 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 1981 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 1982 }
mjr 40:cc0d9814522b 1983 }
mjr 38:091e511ce8a0 1984 else
mjr 40:cc0d9814522b 1985 {
mjr 40:cc0d9814522b 1986 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 1987 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 1988 }
mjr 38:091e511ce8a0 1989 break;
mjr 38:091e511ce8a0 1990
mjr 38:091e511ce8a0 1991 case PortType74HC595:
mjr 87:8d35c74403af 1992 // 74HC595 port (if we don't have an HC595 controller object, or it's not
mjr 87:8d35c74403af 1993 // a valid output number, create a virtual port)
mjr 38:091e511ce8a0 1994 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 1995 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 1996 else
mjr 38:091e511ce8a0 1997 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1998 break;
mjr 87:8d35c74403af 1999
mjr 87:8d35c74403af 2000 case PortTypeTLC59116:
mjr 87:8d35c74403af 2001 // TLC59116 port. The pin number in the config encodes the chip address
mjr 87:8d35c74403af 2002 // in the high 4 bits and the output number on the chip in the low 4 bits.
mjr 87:8d35c74403af 2003 // There's no gamma-corrected version of this output handler, so we don't
mjr 87:8d35c74403af 2004 // need to worry about that here; just use the layered gamma as needed.
mjr 87:8d35c74403af 2005 if (tlc59116 != 0)
mjr 87:8d35c74403af 2006 lwp = new Lw59116Out((pin >> 4) & 0x0F, pin & 0x0F);
mjr 87:8d35c74403af 2007 break;
mjr 38:091e511ce8a0 2008
mjr 38:091e511ce8a0 2009 case PortTypeVirtual:
mjr 43:7a6364d82a41 2010 case PortTypeDisabled:
mjr 38:091e511ce8a0 2011 default:
mjr 38:091e511ce8a0 2012 // virtual or unknown
mjr 38:091e511ce8a0 2013 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2014 break;
mjr 38:091e511ce8a0 2015 }
mjr 38:091e511ce8a0 2016
mjr 40:cc0d9814522b 2017 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 2018 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 2019 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 2020 if (activeLow)
mjr 38:091e511ce8a0 2021 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 2022
mjr 89:c43cd923401c 2023 // Layer on Flipper Logic if desired
mjr 89:c43cd923401c 2024 if (flipperLogic)
mjr 89:c43cd923401c 2025 lwp = new LwFlipperLogicOut(lwp, pc.flipperLogic);
mjr 89:c43cd923401c 2026
mjr 99:8139b0c274f4 2027 // Layer on Chime Logic if desired. Note that Chime Logic and
mjr 99:8139b0c274f4 2028 // Flipper Logic are mutually exclusive, and Flipper Logic takes
mjr 99:8139b0c274f4 2029 // precedence, so ignore the Chime Logic bit if both are set.
mjr 99:8139b0c274f4 2030 if (chimeLogic && !flipperLogic)
mjr 99:8139b0c274f4 2031 lwp = new LwChimeLogicOut(lwp, pc.flipperLogic);
mjr 98:4df3c0f7e707 2032
mjr 89:c43cd923401c 2033 // If it's a noisemaker, layer on a night mode switch
mjr 40:cc0d9814522b 2034 if (noisy)
mjr 40:cc0d9814522b 2035 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 2036
mjr 40:cc0d9814522b 2037 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 2038 if (gamma)
mjr 40:cc0d9814522b 2039 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 2040
mjr 53:9b2611964afc 2041 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 2042 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 2043 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 2044 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 2045 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 2046
mjr 53:9b2611964afc 2047 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 2048 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 2049 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 2050
mjr 38:091e511ce8a0 2051 // turn it off initially
mjr 38:091e511ce8a0 2052 lwp->set(0);
mjr 38:091e511ce8a0 2053
mjr 38:091e511ce8a0 2054 // return the pin
mjr 38:091e511ce8a0 2055 return lwp;
mjr 38:091e511ce8a0 2056 }
mjr 38:091e511ce8a0 2057
mjr 6:cc35eb643e8f 2058 // initialize the output pin array
mjr 35:e959ffba78fd 2059 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 2060 {
mjr 99:8139b0c274f4 2061 // Initialize the Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 2062 LwFlipperLogicOut::classInit(cfg);
mjr 99:8139b0c274f4 2063 LwChimeLogicOut::classInit(cfg);
mjr 89:c43cd923401c 2064
mjr 35:e959ffba78fd 2065 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 2066 // total number of ports.
mjr 35:e959ffba78fd 2067 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 2068 int i;
mjr 35:e959ffba78fd 2069 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 2070 {
mjr 35:e959ffba78fd 2071 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 2072 {
mjr 35:e959ffba78fd 2073 numOutputs = i;
mjr 34:6b981a2afab7 2074 break;
mjr 34:6b981a2afab7 2075 }
mjr 33:d832bcab089e 2076 }
mjr 33:d832bcab089e 2077
mjr 73:4e8ce0b18915 2078 // allocate the pin array
mjr 73:4e8ce0b18915 2079 lwPin = new LwOut*[numOutputs];
mjr 35:e959ffba78fd 2080
mjr 73:4e8ce0b18915 2081 // Allocate the current brightness array
mjr 73:4e8ce0b18915 2082 outLevel = new uint8_t[numOutputs];
mjr 33:d832bcab089e 2083
mjr 73:4e8ce0b18915 2084 // allocate the LedWiz output state arrays
mjr 73:4e8ce0b18915 2085 wizOn = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 2086 wizVal = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 2087
mjr 73:4e8ce0b18915 2088 // initialize all LedWiz outputs to off and brightness 48
mjr 73:4e8ce0b18915 2089 memset(wizOn, 0, numOutputs);
mjr 73:4e8ce0b18915 2090 memset(wizVal, 48, numOutputs);
mjr 73:4e8ce0b18915 2091
mjr 73:4e8ce0b18915 2092 // set all LedWiz virtual unit flash speeds to 2
mjr 73:4e8ce0b18915 2093 for (i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2094 wizSpeed[i] = 2;
mjr 33:d832bcab089e 2095
mjr 35:e959ffba78fd 2096 // create the pin interface object for each port
mjr 35:e959ffba78fd 2097 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 2098 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 2099 }
mjr 6:cc35eb643e8f 2100
mjr 76:7f5912b6340e 2101 // Translate an LedWiz brightness level (0..49) to a DOF brightness
mjr 76:7f5912b6340e 2102 // level (0..255). Note that brightness level 49 isn't actually valid,
mjr 76:7f5912b6340e 2103 // according to the LedWiz API documentation, but many clients use it
mjr 76:7f5912b6340e 2104 // anyway, and the real LedWiz accepts it and seems to treat it as
mjr 76:7f5912b6340e 2105 // equivalent to 48.
mjr 40:cc0d9814522b 2106 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 2107 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 2108 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 2109 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 2110 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 2111 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 2112 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 2113 255, 255
mjr 40:cc0d9814522b 2114 };
mjr 40:cc0d9814522b 2115
mjr 76:7f5912b6340e 2116 // Translate a DOF brightness level (0..255) to an LedWiz brightness
mjr 76:7f5912b6340e 2117 // level (1..48)
mjr 76:7f5912b6340e 2118 static const uint8_t dof_to_lw[] = {
mjr 76:7f5912b6340e 2119 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
mjr 76:7f5912b6340e 2120 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
mjr 76:7f5912b6340e 2121 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9,
mjr 76:7f5912b6340e 2122 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12,
mjr 76:7f5912b6340e 2123 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15,
mjr 76:7f5912b6340e 2124 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18,
mjr 76:7f5912b6340e 2125 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21,
mjr 76:7f5912b6340e 2126 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24,
mjr 76:7f5912b6340e 2127 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27,
mjr 76:7f5912b6340e 2128 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30,
mjr 76:7f5912b6340e 2129 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
mjr 76:7f5912b6340e 2130 33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36,
mjr 76:7f5912b6340e 2131 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
mjr 76:7f5912b6340e 2132 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
mjr 76:7f5912b6340e 2133 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45,
mjr 76:7f5912b6340e 2134 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48
mjr 76:7f5912b6340e 2135 };
mjr 76:7f5912b6340e 2136
mjr 74:822a92bc11d2 2137 // LedWiz flash cycle tables. For efficiency, we use a lookup table
mjr 74:822a92bc11d2 2138 // rather than calculating these on the fly. The flash cycles are
mjr 74:822a92bc11d2 2139 // generated by the following formulas, where 'c' is the current
mjr 74:822a92bc11d2 2140 // cycle counter, from 0 to 255:
mjr 74:822a92bc11d2 2141 //
mjr 74:822a92bc11d2 2142 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 2143 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 2144 // mode 131 = on/ramp down = (c < 128 ? 255 : (255-c)*2)
mjr 74:822a92bc11d2 2145 // mode 132 = ramp up/on = (c < 128 ? c*2 : 255)
mjr 74:822a92bc11d2 2146 //
mjr 74:822a92bc11d2 2147 // To look up the current output value for a given mode and a given
mjr 74:822a92bc11d2 2148 // cycle counter 'c', index the table with ((mode-129)*256)+c.
mjr 74:822a92bc11d2 2149 static const uint8_t wizFlashLookup[] = {
mjr 74:822a92bc11d2 2150 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 2151 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
mjr 74:822a92bc11d2 2152 0x21, 0x23, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f,
mjr 74:822a92bc11d2 2153 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f,
mjr 74:822a92bc11d2 2154 0x61, 0x63, 0x65, 0x67, 0x69, 0x6b, 0x6d, 0x6f, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d, 0x7f,
mjr 74:822a92bc11d2 2155 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d, 0x8f, 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9d, 0x9f,
mjr 74:822a92bc11d2 2156 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad, 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, 0xbf,
mjr 74:822a92bc11d2 2157 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
mjr 74:822a92bc11d2 2158 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb, 0xed, 0xef, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0xfd, 0xff,
mjr 74:822a92bc11d2 2159 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 2160 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 2161 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 2162 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 2163 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 2164 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 2165 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 2166 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 2167
mjr 74:822a92bc11d2 2168 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 2169 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2170 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2171 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2172 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2173 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2174 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2176 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2178 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2179 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2180 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2181 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2182 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2183 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2184 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2185
mjr 74:822a92bc11d2 2186 // mode 131 = on/ramp down = c < 128 ? 255 : (255 - c)*2
mjr 74:822a92bc11d2 2187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2189 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2191 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2192 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2193 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2194 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2195 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 2196 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 2197 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 2198 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 2199 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 2200 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 2201 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 2202 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 2203
mjr 74:822a92bc11d2 2204 // mode 132 = ramp up/on = c < 128 ? c*2 : 255
mjr 74:822a92bc11d2 2205 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
mjr 74:822a92bc11d2 2206 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
mjr 74:822a92bc11d2 2207 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
mjr 74:822a92bc11d2 2208 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
mjr 74:822a92bc11d2 2209 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
mjr 74:822a92bc11d2 2210 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
mjr 74:822a92bc11d2 2211 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
mjr 74:822a92bc11d2 2212 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
mjr 74:822a92bc11d2 2213 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2214 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2215 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2216 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2217 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2218 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2219 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2220 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
mjr 74:822a92bc11d2 2221 };
mjr 74:822a92bc11d2 2222
mjr 74:822a92bc11d2 2223 // LedWiz flash cycle timer. This runs continuously. On each update,
mjr 74:822a92bc11d2 2224 // we use this to figure out where we are on the cycle for each bank.
mjr 74:822a92bc11d2 2225 Timer wizCycleTimer;
mjr 74:822a92bc11d2 2226
mjr 76:7f5912b6340e 2227 // timing statistics for wizPulse()
mjr 76:7f5912b6340e 2228 uint64_t wizPulseTotalTime, wizPulseRunCount;
mjr 76:7f5912b6340e 2229
mjr 76:7f5912b6340e 2230 // LedWiz flash timer pulse. The main loop calls this on each cycle
mjr 76:7f5912b6340e 2231 // to update outputs using LedWiz flash modes. We do one bank of 32
mjr 76:7f5912b6340e 2232 // outputs on each cycle.
mjr 29:582472d0bc57 2233 static void wizPulse()
mjr 29:582472d0bc57 2234 {
mjr 76:7f5912b6340e 2235 // current bank
mjr 76:7f5912b6340e 2236 static int wizPulseBank = 0;
mjr 76:7f5912b6340e 2237
mjr 76:7f5912b6340e 2238 // start a timer for statistics collection
mjr 76:7f5912b6340e 2239 IF_DIAG(
mjr 76:7f5912b6340e 2240 Timer t;
mjr 76:7f5912b6340e 2241 t.start();
mjr 76:7f5912b6340e 2242 )
mjr 76:7f5912b6340e 2243
mjr 76:7f5912b6340e 2244 // Update the current bank's cycle counter: figure the current
mjr 76:7f5912b6340e 2245 // phase of the LedWiz pulse cycle for this bank.
mjr 76:7f5912b6340e 2246 //
mjr 76:7f5912b6340e 2247 // The LedWiz speed setting gives the flash period in 0.25s units
mjr 76:7f5912b6340e 2248 // (speed 1 is a flash period of .25s, speed 7 is a period of 1.75s).
mjr 76:7f5912b6340e 2249 //
mjr 76:7f5912b6340e 2250 // What we're after here is the "phase", which is to say the point
mjr 76:7f5912b6340e 2251 // in the current cycle. If we assume that the cycle has been running
mjr 76:7f5912b6340e 2252 // continuously since some arbitrary time zero in the past, we can
mjr 76:7f5912b6340e 2253 // figure where we are in the current cycle by dividing the time since
mjr 76:7f5912b6340e 2254 // that zero by the cycle period and taking the remainder. E.g., if
mjr 76:7f5912b6340e 2255 // the cycle time is 5 seconds, and the time since t-zero is 17 seconds,
mjr 76:7f5912b6340e 2256 // we divide 17 by 5 to get a remainder of 2. That says we're 2 seconds
mjr 76:7f5912b6340e 2257 // into the current 5-second cycle, or 2/5 of the way through the
mjr 76:7f5912b6340e 2258 // current cycle.
mjr 76:7f5912b6340e 2259 //
mjr 76:7f5912b6340e 2260 // We do this calculation on every iteration of the main loop, so we
mjr 76:7f5912b6340e 2261 // want it to be very fast. To streamline it, we'll use some tricky
mjr 76:7f5912b6340e 2262 // integer arithmetic. The result will be the same as the straightforward
mjr 76:7f5912b6340e 2263 // remainder and fraction calculation we just explained, but we'll get
mjr 76:7f5912b6340e 2264 // there by less-than-obvious means.
mjr 76:7f5912b6340e 2265 //
mjr 76:7f5912b6340e 2266 // Rather than finding the phase as a continuous quantity or floating
mjr 76:7f5912b6340e 2267 // point number, we'll quantize it. We'll divide each cycle into 256
mjr 76:7f5912b6340e 2268 // time units, or quanta. Each quantum is 1/256 of the cycle length,
mjr 76:7f5912b6340e 2269 // so for a 1-second cycle (LedWiz speed 4), each quantum is 1/256 of
mjr 76:7f5912b6340e 2270 // a second, or about 3.9ms. If we express the time since t-zero in
mjr 76:7f5912b6340e 2271 // these units, the time period of one cycle is exactly 256 units, so
mjr 76:7f5912b6340e 2272 // we can calculate our point in the cycle by taking the remainder of
mjr 76:7f5912b6340e 2273 // the time (in our funny units) divided by 256. The special thing
mjr 76:7f5912b6340e 2274 // about making the cycle time equal to 256 units is that "x % 256"
mjr 76:7f5912b6340e 2275 // is exactly the same as "x & 255", which is a much faster operation
mjr 76:7f5912b6340e 2276 // than division on ARM M0+: this CPU has no hardware DIVIDE operation,
mjr 76:7f5912b6340e 2277 // so an integer division takes about 5us. The bit mask operation, in
mjr 76:7f5912b6340e 2278 // contrast, takes only about 60ns - about 100x faster. 5us doesn't
mjr 76:7f5912b6340e 2279 // sound like much, but we do this on every main loop, so every little
mjr 76:7f5912b6340e 2280 // bit counts.
mjr 76:7f5912b6340e 2281 //
mjr 76:7f5912b6340e 2282 // The snag is that our system timer gives us the elapsed time in
mjr 76:7f5912b6340e 2283 // microseconds. We still need to convert this to our special quanta
mjr 76:7f5912b6340e 2284 // of 256 units per cycle. The straightforward way to do that is by
mjr 76:7f5912b6340e 2285 // dividing by (microseconds per quantum). E.g., for LedWiz speed 4,
mjr 76:7f5912b6340e 2286 // we decided that our quantum was 1/256 of a second, or 3906us, so
mjr 76:7f5912b6340e 2287 // dividing the current system time in microseconds by 3906 will give
mjr 76:7f5912b6340e 2288 // us the time in our quantum units. But now we've just substituted
mjr 76:7f5912b6340e 2289 // one division for another!
mjr 76:7f5912b6340e 2290 //
mjr 76:7f5912b6340e 2291 // This is where our really tricky integer math comes in. Dividing
mjr 76:7f5912b6340e 2292 // by X is the same as multiplying by 1/X. In integer math, 1/3906
mjr 76:7f5912b6340e 2293 // is zero, so that won't work. But we can get around that by doing
mjr 76:7f5912b6340e 2294 // the integer math as "fixed point" arithmetic instead. It's still
mjr 76:7f5912b6340e 2295 // actually carried out as integer operations, but we'll scale our
mjr 76:7f5912b6340e 2296 // integers by a scaling factor, then take out the scaling factor
mjr 76:7f5912b6340e 2297 // later to get the final result. The scaling factor we'll use is
mjr 76:7f5912b6340e 2298 // 2^24. So we're going to calculate (time * 2^24/3906), then divide
mjr 76:7f5912b6340e 2299 // the result by 2^24 to get the final answer. I know it seems like
mjr 76:7f5912b6340e 2300 // we're substituting one division for another yet again, but this
mjr 76:7f5912b6340e 2301 // time's the charm, because dividing by 2^24 is a bit shift operation,
mjr 76:7f5912b6340e 2302 // which is another single-cycle operation on M0+. You might also
mjr 76:7f5912b6340e 2303 // wonder how all these tricks don't cause overflows or underflows
mjr 76:7f5912b6340e 2304 // or what not. Well, the multiply by 2^24/3906 will cause an
mjr 76:7f5912b6340e 2305 // overflow, but we don't care, because the overflow will all be in
mjr 76:7f5912b6340e 2306 // the high-order bits that we're going to discard in the final
mjr 76:7f5912b6340e 2307 // remainder calculation anyway.
mjr 76:7f5912b6340e 2308 //
mjr 76:7f5912b6340e 2309 // Each entry in the array below represents 2^24/N for the corresponding
mjr 76:7f5912b6340e 2310 // LedWiz speed, where N is the number of time quanta per cycle at that
mjr 76:7f5912b6340e 2311 // speed. The time quanta are chosen such that 256 quanta add up to
mjr 76:7f5912b6340e 2312 // approximately (LedWiz speed setting * 0.25s).
mjr 76:7f5912b6340e 2313 //
mjr 76:7f5912b6340e 2314 // Note that the calculation has an implicit bit mask (result & 0xFF)
mjr 76:7f5912b6340e 2315 // to get the final result mod 256. But we don't have to actually
mjr 76:7f5912b6340e 2316 // do that work because we're using 32-bit ints and a 2^24 fixed
mjr 76:7f5912b6340e 2317 // point base (X in the narrative above). The final shift right by
mjr 76:7f5912b6340e 2318 // 24 bits to divide out the base will leave us with only 8 bits in
mjr 76:7f5912b6340e 2319 // the result, since we started with 32.
mjr 76:7f5912b6340e 2320 static const uint32_t inv_us_per_quantum[] = { // indexed by LedWiz speed
mjr 76:7f5912b6340e 2321 0, 17172, 8590, 5726, 4295, 3436, 2863, 2454
mjr 76:7f5912b6340e 2322 };
mjr 76:7f5912b6340e 2323 int counter = ((wizCycleTimer.read_us() * inv_us_per_quantum[wizSpeed[wizPulseBank]]) >> 24);
mjr 76:7f5912b6340e 2324
mjr 76:7f5912b6340e 2325 // get the range of 32 output sin this bank
mjr 76:7f5912b6340e 2326 int fromPort = wizPulseBank*32;
mjr 76:7f5912b6340e 2327 int toPort = fromPort+32;
mjr 76:7f5912b6340e 2328 if (toPort > numOutputs)
mjr 76:7f5912b6340e 2329 toPort = numOutputs;
mjr 76:7f5912b6340e 2330
mjr 76:7f5912b6340e 2331 // update all outputs set to flashing values
mjr 76:7f5912b6340e 2332 for (int i = fromPort ; i < toPort ; ++i)
mjr 73:4e8ce0b18915 2333 {
mjr 76:7f5912b6340e 2334 // Update the port only if the LedWiz SBA switch for the port is on
mjr 76:7f5912b6340e 2335 // (wizOn[i]) AND the port is a PBA flash mode in the range 129..132.
mjr 76:7f5912b6340e 2336 // These modes and only these modes have the high bit (0x80) set, so
mjr 76:7f5912b6340e 2337 // we can test for them simply by testing the high bit.
mjr 76:7f5912b6340e 2338 if (wizOn[i])
mjr 29:582472d0bc57 2339 {
mjr 76:7f5912b6340e 2340 uint8_t val = wizVal[i];
mjr 76:7f5912b6340e 2341 if ((val & 0x80) != 0)
mjr 29:582472d0bc57 2342 {
mjr 76:7f5912b6340e 2343 // ook up the value for the mode at the cycle time
mjr 76:7f5912b6340e 2344 lwPin[i]->set(outLevel[i] = wizFlashLookup[((val-129) << 8) + counter]);
mjr 29:582472d0bc57 2345 }
mjr 29:582472d0bc57 2346 }
mjr 76:7f5912b6340e 2347 }
mjr 76:7f5912b6340e 2348
mjr 34:6b981a2afab7 2349 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 2350 if (hc595 != 0)
mjr 35:e959ffba78fd 2351 hc595->update();
mjr 76:7f5912b6340e 2352
mjr 76:7f5912b6340e 2353 // switch to the next bank
mjr 76:7f5912b6340e 2354 if (++wizPulseBank >= MAX_LW_BANKS)
mjr 76:7f5912b6340e 2355 wizPulseBank = 0;
mjr 76:7f5912b6340e 2356
mjr 76:7f5912b6340e 2357 // collect timing statistics
mjr 76:7f5912b6340e 2358 IF_DIAG(
mjr 76:7f5912b6340e 2359 wizPulseTotalTime += t.read_us();
mjr 76:7f5912b6340e 2360 wizPulseRunCount += 1;
mjr 76:7f5912b6340e 2361 )
mjr 1:d913e0afb2ac 2362 }
mjr 38:091e511ce8a0 2363
mjr 76:7f5912b6340e 2364 // Update a port to reflect its new LedWiz SBA+PBA setting.
mjr 76:7f5912b6340e 2365 static void updateLwPort(int port)
mjr 38:091e511ce8a0 2366 {
mjr 76:7f5912b6340e 2367 // check if the SBA switch is on or off
mjr 76:7f5912b6340e 2368 if (wizOn[port])
mjr 76:7f5912b6340e 2369 {
mjr 76:7f5912b6340e 2370 // It's on. If the port is a valid static brightness level,
mjr 76:7f5912b6340e 2371 // set the output port to match. Otherwise leave it as is:
mjr 76:7f5912b6340e 2372 // if it's a flashing mode, the flash mode pulse will update
mjr 76:7f5912b6340e 2373 // it on the next cycle.
mjr 76:7f5912b6340e 2374 int val = wizVal[port];
mjr 76:7f5912b6340e 2375 if (val <= 49)
mjr 76:7f5912b6340e 2376 lwPin[port]->set(outLevel[port] = lw_to_dof[val]);
mjr 76:7f5912b6340e 2377 }
mjr 76:7f5912b6340e 2378 else
mjr 76:7f5912b6340e 2379 {
mjr 76:7f5912b6340e 2380 // the port is off - set absolute brightness zero
mjr 76:7f5912b6340e 2381 lwPin[port]->set(outLevel[port] = 0);
mjr 76:7f5912b6340e 2382 }
mjr 73:4e8ce0b18915 2383 }
mjr 73:4e8ce0b18915 2384
mjr 73:4e8ce0b18915 2385 // Turn off all outputs and restore everything to the default LedWiz
mjr 92:f264fbaa1be5 2386 // state. This sets all outputs to LedWiz profile value 48 (full
mjr 92:f264fbaa1be5 2387 // brightness) and switch state Off, and sets the LedWiz flash rate
mjr 92:f264fbaa1be5 2388 // to 2. This effectively restores the power-on conditions.
mjr 73:4e8ce0b18915 2389 //
mjr 73:4e8ce0b18915 2390 void allOutputsOff()
mjr 73:4e8ce0b18915 2391 {
mjr 92:f264fbaa1be5 2392 // reset all outputs to OFF/48
mjr 73:4e8ce0b18915 2393 for (int i = 0 ; i < numOutputs ; ++i)
mjr 73:4e8ce0b18915 2394 {
mjr 73:4e8ce0b18915 2395 outLevel[i] = 0;
mjr 73:4e8ce0b18915 2396 wizOn[i] = 0;
mjr 73:4e8ce0b18915 2397 wizVal[i] = 48;
mjr 73:4e8ce0b18915 2398 lwPin[i]->set(0);
mjr 73:4e8ce0b18915 2399 }
mjr 73:4e8ce0b18915 2400
mjr 73:4e8ce0b18915 2401 // restore default LedWiz flash rate
mjr 73:4e8ce0b18915 2402 for (int i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2403 wizSpeed[i] = 2;
mjr 38:091e511ce8a0 2404
mjr 73:4e8ce0b18915 2405 // flush changes to hc595, if applicable
mjr 38:091e511ce8a0 2406 if (hc595 != 0)
mjr 38:091e511ce8a0 2407 hc595->update();
mjr 38:091e511ce8a0 2408 }
mjr 38:091e511ce8a0 2409
mjr 74:822a92bc11d2 2410 // Cary out an SBA or SBX message. portGroup is 0 for ports 1-32,
mjr 74:822a92bc11d2 2411 // 1 for ports 33-64, etc. Original protocol SBA messages always
mjr 74:822a92bc11d2 2412 // address port group 0; our private SBX extension messages can
mjr 74:822a92bc11d2 2413 // address any port group.
mjr 74:822a92bc11d2 2414 void sba_sbx(int portGroup, const uint8_t *data)
mjr 74:822a92bc11d2 2415 {
mjr 76:7f5912b6340e 2416 // update all on/off states in the group
mjr 74:822a92bc11d2 2417 for (int i = 0, bit = 1, imsg = 1, port = portGroup*32 ;
mjr 74:822a92bc11d2 2418 i < 32 && port < numOutputs ;
mjr 74:822a92bc11d2 2419 ++i, bit <<= 1, ++port)
mjr 74:822a92bc11d2 2420 {
mjr 74:822a92bc11d2 2421 // figure the on/off state bit for this output
mjr 74:822a92bc11d2 2422 if (bit == 0x100) {
mjr 74:822a92bc11d2 2423 bit = 1;
mjr 74:822a92bc11d2 2424 ++imsg;
mjr 74:822a92bc11d2 2425 }
mjr 74:822a92bc11d2 2426
mjr 74:822a92bc11d2 2427 // set the on/off state
mjr 76:7f5912b6340e 2428 bool on = wizOn[port] = ((data[imsg] & bit) != 0);
mjr 76:7f5912b6340e 2429
mjr 76:7f5912b6340e 2430 // set the output port brightness to match the new setting
mjr 76:7f5912b6340e 2431 updateLwPort(port);
mjr 74:822a92bc11d2 2432 }
mjr 74:822a92bc11d2 2433
mjr 74:822a92bc11d2 2434 // set the flash speed for the port group
mjr 74:822a92bc11d2 2435 if (portGroup < countof(wizSpeed))
mjr 74:822a92bc11d2 2436 wizSpeed[portGroup] = (data[5] < 1 ? 1 : data[5] > 7 ? 7 : data[5]);
mjr 74:822a92bc11d2 2437
mjr 76:7f5912b6340e 2438 // update 74HC959 outputs
mjr 76:7f5912b6340e 2439 if (hc595 != 0)
mjr 76:7f5912b6340e 2440 hc595->update();
mjr 74:822a92bc11d2 2441 }
mjr 74:822a92bc11d2 2442
mjr 74:822a92bc11d2 2443 // Carry out a PBA or PBX message.
mjr 74:822a92bc11d2 2444 void pba_pbx(int basePort, const uint8_t *data)
mjr 74:822a92bc11d2 2445 {
mjr 74:822a92bc11d2 2446 // update each wizVal entry from the brightness data
mjr 76:7f5912b6340e 2447 for (int i = 0, port = basePort ; i < 8 && port < numOutputs ; ++i, ++port)
mjr 74:822a92bc11d2 2448 {
mjr 74:822a92bc11d2 2449 // get the value
mjr 74:822a92bc11d2 2450 uint8_t v = data[i];
mjr 74:822a92bc11d2 2451
mjr 74:822a92bc11d2 2452 // Validate it. The legal values are 0..49 for brightness
mjr 74:822a92bc11d2 2453 // levels, and 128..132 for flash modes. Set anything invalid
mjr 74:822a92bc11d2 2454 // to full brightness (48) instead. Note that 49 isn't actually
mjr 74:822a92bc11d2 2455 // a valid documented value, but in practice some clients send
mjr 74:822a92bc11d2 2456 // this to mean 100% brightness, and the real LedWiz treats it
mjr 74:822a92bc11d2 2457 // as such.
mjr 74:822a92bc11d2 2458 if ((v > 49 && v < 129) || v > 132)
mjr 74:822a92bc11d2 2459 v = 48;
mjr 74:822a92bc11d2 2460
mjr 74:822a92bc11d2 2461 // store it
mjr 76:7f5912b6340e 2462 wizVal[port] = v;
mjr 76:7f5912b6340e 2463
mjr 76:7f5912b6340e 2464 // update the port
mjr 76:7f5912b6340e 2465 updateLwPort(port);
mjr 74:822a92bc11d2 2466 }
mjr 74:822a92bc11d2 2467
mjr 76:7f5912b6340e 2468 // update 74HC595 outputs
mjr 76:7f5912b6340e 2469 if (hc595 != 0)
mjr 76:7f5912b6340e 2470 hc595->update();
mjr 74:822a92bc11d2 2471 }
mjr 74:822a92bc11d2 2472
mjr 77:0b96f6867312 2473 // ---------------------------------------------------------------------------
mjr 77:0b96f6867312 2474 //
mjr 77:0b96f6867312 2475 // IR Remote Control transmitter & receiver
mjr 77:0b96f6867312 2476 //
mjr 77:0b96f6867312 2477
mjr 77:0b96f6867312 2478 // receiver
mjr 77:0b96f6867312 2479 IRReceiver *ir_rx;
mjr 77:0b96f6867312 2480
mjr 77:0b96f6867312 2481 // transmitter
mjr 77:0b96f6867312 2482 IRTransmitter *ir_tx;
mjr 77:0b96f6867312 2483
mjr 77:0b96f6867312 2484 // Mapping from IR commands slots in the configuration to "virtual button"
mjr 77:0b96f6867312 2485 // numbers on the IRTransmitter's "virtual remote". To minimize RAM usage,
mjr 77:0b96f6867312 2486 // we only create virtual buttons on the transmitter object for code slots
mjr 77:0b96f6867312 2487 // that are configured for transmission, which includes slots used for TV
mjr 77:0b96f6867312 2488 // ON commands and slots that can be triggered by button presses. This
mjr 77:0b96f6867312 2489 // means that virtual button numbers won't necessarily match the config
mjr 77:0b96f6867312 2490 // slot numbers. This table provides the mapping:
mjr 77:0b96f6867312 2491 // IRConfigSlotToVirtualButton[n] = ir_tx virtual button number for
mjr 77:0b96f6867312 2492 // configuration slot n
mjr 77:0b96f6867312 2493 uint8_t IRConfigSlotToVirtualButton[MAX_IR_CODES];
mjr 78:1e00b3fa11af 2494
mjr 78:1e00b3fa11af 2495 // IR transmitter virtual button number for ad hoc IR command. We allocate
mjr 78:1e00b3fa11af 2496 // one virtual button for sending ad hoc IR codes, such as through the USB
mjr 78:1e00b3fa11af 2497 // protocol.
mjr 78:1e00b3fa11af 2498 uint8_t IRAdHocBtn;
mjr 78:1e00b3fa11af 2499
mjr 78:1e00b3fa11af 2500 // Staging area for ad hoc IR commands. It takes multiple messages
mjr 78:1e00b3fa11af 2501 // to fill out an IR command, so we store the partial command here
mjr 78:1e00b3fa11af 2502 // while waiting for the rest.
mjr 78:1e00b3fa11af 2503 static struct
mjr 78:1e00b3fa11af 2504 {
mjr 78:1e00b3fa11af 2505 uint8_t protocol; // protocol ID
mjr 78:1e00b3fa11af 2506 uint64_t code; // code
mjr 78:1e00b3fa11af 2507 uint8_t dittos : 1; // using dittos?
mjr 78:1e00b3fa11af 2508 uint8_t ready : 1; // do we have a code ready to transmit?
mjr 78:1e00b3fa11af 2509 } IRAdHocCmd;
mjr 88:98bce687e6c0 2510
mjr 77:0b96f6867312 2511
mjr 77:0b96f6867312 2512 // IR mode timer. In normal mode, this is the time since the last
mjr 77:0b96f6867312 2513 // command received; we use this to handle commands with timed effects,
mjr 77:0b96f6867312 2514 // such as sending a key to the PC. In learning mode, this is the time
mjr 77:0b96f6867312 2515 // since we activated learning mode, which we use to automatically end
mjr 77:0b96f6867312 2516 // learning mode if a decodable command isn't received within a reasonable
mjr 77:0b96f6867312 2517 // amount of time.
mjr 77:0b96f6867312 2518 Timer IRTimer;
mjr 77:0b96f6867312 2519
mjr 77:0b96f6867312 2520 // IR Learning Mode. The PC enters learning mode via special function 65 12.
mjr 77:0b96f6867312 2521 // The states are:
mjr 77:0b96f6867312 2522 //
mjr 77:0b96f6867312 2523 // 0 -> normal operation (not in learning mode)
mjr 77:0b96f6867312 2524 // 1 -> learning mode; reading raw codes, no command read yet
mjr 77:0b96f6867312 2525 // 2 -> learning mode; command received, awaiting auto-repeat
mjr 77:0b96f6867312 2526 // 3 -> learning mode; done, command and repeat mode decoded
mjr 77:0b96f6867312 2527 //
mjr 77:0b96f6867312 2528 // When we enter learning mode, we reset IRTimer to keep track of how long
mjr 77:0b96f6867312 2529 // we've been in the mode. This allows the mode to time out if no code is
mjr 77:0b96f6867312 2530 // received within a reasonable time.
mjr 77:0b96f6867312 2531 uint8_t IRLearningMode = 0;
mjr 77:0b96f6867312 2532
mjr 77:0b96f6867312 2533 // Learning mode command received. This stores the first decoded command
mjr 77:0b96f6867312 2534 // when in learning mode. For some protocols, we can't just report the
mjr 77:0b96f6867312 2535 // first command we receive, because we need to wait for an auto-repeat to
mjr 77:0b96f6867312 2536 // determine what format the remote uses for repeats. This stores the first
mjr 77:0b96f6867312 2537 // command while we await a repeat. This is necessary for protocols that
mjr 77:0b96f6867312 2538 // have "dittos", since some remotes for such protocols use the dittos and
mjr 77:0b96f6867312 2539 // some don't; the only way to find out is to read a repeat code and see if
mjr 77:0b96f6867312 2540 // it's a ditto or just a repeat of the full code.
mjr 77:0b96f6867312 2541 IRCommand learnedIRCode;
mjr 77:0b96f6867312 2542
mjr 78:1e00b3fa11af 2543 // IR command received, as a config slot index, 1..MAX_IR_CODES.
mjr 77:0b96f6867312 2544 // When we receive a command that matches one of our programmed commands,
mjr 77:0b96f6867312 2545 // we note the slot here. We also reset the IR timer so that we know how
mjr 77:0b96f6867312 2546 // long it's been since the command came in. This lets us handle commands
mjr 77:0b96f6867312 2547 // with timed effects, such as PC key input. Note that this is a 1-based
mjr 77:0b96f6867312 2548 // index; 0 represents no command.
mjr 77:0b96f6867312 2549 uint8_t IRCommandIn = 0;
mjr 77:0b96f6867312 2550
mjr 77:0b96f6867312 2551 // "Toggle bit" of last command. Some IR protocols have a toggle bit
mjr 77:0b96f6867312 2552 // that distinguishes an auto-repeating key from a key being pressed
mjr 77:0b96f6867312 2553 // several times in a row. This records the toggle bit of the last
mjr 77:0b96f6867312 2554 // command we received.
mjr 77:0b96f6867312 2555 uint8_t lastIRToggle = 0;
mjr 77:0b96f6867312 2556
mjr 77:0b96f6867312 2557 // Are we in a gap between successive key presses? When we detect that a
mjr 77:0b96f6867312 2558 // key is being pressed multiple times rather than auto-repeated (which we
mjr 77:0b96f6867312 2559 // can detect via a toggle bit in some protocols), we'll briefly stop sending
mjr 77:0b96f6867312 2560 // the associated key to the PC, so that the PC likewise recognizes the
mjr 77:0b96f6867312 2561 // distinct key press.
mjr 77:0b96f6867312 2562 uint8_t IRKeyGap = false;
mjr 77:0b96f6867312 2563
mjr 78:1e00b3fa11af 2564
mjr 77:0b96f6867312 2565 // initialize
mjr 77:0b96f6867312 2566 void init_IR(Config &cfg, bool &kbKeys)
mjr 77:0b96f6867312 2567 {
mjr 77:0b96f6867312 2568 PinName pin;
mjr 77:0b96f6867312 2569
mjr 77:0b96f6867312 2570 // start the IR timer
mjr 77:0b96f6867312 2571 IRTimer.start();
mjr 77:0b96f6867312 2572
mjr 77:0b96f6867312 2573 // if there's a transmitter, set it up
mjr 77:0b96f6867312 2574 if ((pin = wirePinName(cfg.IR.emitter)) != NC)
mjr 77:0b96f6867312 2575 {
mjr 77:0b96f6867312 2576 // no virtual buttons yet
mjr 77:0b96f6867312 2577 int nVirtualButtons = 0;
mjr 77:0b96f6867312 2578 memset(IRConfigSlotToVirtualButton, 0xFF, sizeof(IRConfigSlotToVirtualButton));
mjr 77:0b96f6867312 2579
mjr 77:0b96f6867312 2580 // assign virtual buttons slots for TV ON codes
mjr 77:0b96f6867312 2581 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2582 {
mjr 77:0b96f6867312 2583 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 2584 IRConfigSlotToVirtualButton[i] = nVirtualButtons++;
mjr 77:0b96f6867312 2585 }
mjr 77:0b96f6867312 2586
mjr 77:0b96f6867312 2587 // assign virtual buttons for codes that can be triggered by
mjr 77:0b96f6867312 2588 // real button inputs
mjr 77:0b96f6867312 2589 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 77:0b96f6867312 2590 {
mjr 77:0b96f6867312 2591 // get the button
mjr 77:0b96f6867312 2592 ButtonCfg &b = cfg.button[i];
mjr 77:0b96f6867312 2593
mjr 77:0b96f6867312 2594 // check the unshifted button
mjr 77:0b96f6867312 2595 int c = b.IRCommand - 1;
mjr 77:0b96f6867312 2596 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2597 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2598 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2599
mjr 77:0b96f6867312 2600 // check the shifted button
mjr 77:0b96f6867312 2601 c = b.IRCommand2 - 1;
mjr 77:0b96f6867312 2602 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2603 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2604 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2605 }
mjr 77:0b96f6867312 2606
mjr 77:0b96f6867312 2607 // allocate an additional virtual button for transmitting ad hoc
mjr 77:0b96f6867312 2608 // codes, such as for the "send code" USB API function
mjr 78:1e00b3fa11af 2609 IRAdHocBtn = nVirtualButtons++;
mjr 77:0b96f6867312 2610
mjr 77:0b96f6867312 2611 // create the transmitter
mjr 77:0b96f6867312 2612 ir_tx = new IRTransmitter(pin, nVirtualButtons);
mjr 77:0b96f6867312 2613
mjr 77:0b96f6867312 2614 // program the commands into the virtual button slots
mjr 77:0b96f6867312 2615 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2616 {
mjr 77:0b96f6867312 2617 // if this slot is assigned to a virtual button, program it
mjr 77:0b96f6867312 2618 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 2619 if (vb != 0xFF)
mjr 77:0b96f6867312 2620 {
mjr 77:0b96f6867312 2621 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2622 uint64_t code = cb.code.lo | (uint64_t(cb.code.hi) << 32);
mjr 77:0b96f6867312 2623 bool dittos = (cb.flags & IRFlagDittos) != 0;
mjr 77:0b96f6867312 2624 ir_tx->programButton(vb, cb.protocol, dittos, code);
mjr 77:0b96f6867312 2625 }
mjr 77:0b96f6867312 2626 }
mjr 77:0b96f6867312 2627 }
mjr 77:0b96f6867312 2628
mjr 77:0b96f6867312 2629 // if there's a receiver, set it up
mjr 77:0b96f6867312 2630 if ((pin = wirePinName(cfg.IR.sensor)) != NC)
mjr 77:0b96f6867312 2631 {
mjr 77:0b96f6867312 2632 // create the receiver
mjr 77:0b96f6867312 2633 ir_rx = new IRReceiver(pin, 32);
mjr 77:0b96f6867312 2634
mjr 77:0b96f6867312 2635 // connect the transmitter (if any) to the receiver, so that
mjr 77:0b96f6867312 2636 // the receiver can suppress reception of our own transmissions
mjr 77:0b96f6867312 2637 ir_rx->setTransmitter(ir_tx);
mjr 77:0b96f6867312 2638
mjr 77:0b96f6867312 2639 // enable it
mjr 77:0b96f6867312 2640 ir_rx->enable();
mjr 77:0b96f6867312 2641
mjr 77:0b96f6867312 2642 // Check the IR command slots to see if any slots are configured
mjr 77:0b96f6867312 2643 // to send a keyboard key on receiving an IR command. If any are,
mjr 77:0b96f6867312 2644 // tell the caller that we need a USB keyboard interface.
mjr 77:0b96f6867312 2645 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2646 {
mjr 77:0b96f6867312 2647 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2648 if (cb.protocol != 0
mjr 77:0b96f6867312 2649 && (cb.keytype == BtnTypeKey || cb.keytype == BtnTypeMedia))
mjr 77:0b96f6867312 2650 {
mjr 77:0b96f6867312 2651 kbKeys = true;
mjr 77:0b96f6867312 2652 break;
mjr 77:0b96f6867312 2653 }
mjr 77:0b96f6867312 2654 }
mjr 77:0b96f6867312 2655 }
mjr 77:0b96f6867312 2656 }
mjr 77:0b96f6867312 2657
mjr 77:0b96f6867312 2658 // Press or release a button with an assigned IR function. 'cmd'
mjr 77:0b96f6867312 2659 // is the command slot number (1..MAX_IR_CODES) assigned to the button.
mjr 77:0b96f6867312 2660 void IR_buttonChange(uint8_t cmd, bool pressed)
mjr 77:0b96f6867312 2661 {
mjr 77:0b96f6867312 2662 // only proceed if there's an IR transmitter attached
mjr 77:0b96f6867312 2663 if (ir_tx != 0)
mjr 77:0b96f6867312 2664 {
mjr 77:0b96f6867312 2665 // adjust the command slot to a zero-based index
mjr 77:0b96f6867312 2666 int slot = cmd - 1;
mjr 77:0b96f6867312 2667
mjr 77:0b96f6867312 2668 // press or release the virtual button
mjr 77:0b96f6867312 2669 ir_tx->pushButton(IRConfigSlotToVirtualButton[slot], pressed);
mjr 77:0b96f6867312 2670 }
mjr 77:0b96f6867312 2671 }
mjr 77:0b96f6867312 2672
mjr 78:1e00b3fa11af 2673 // Process IR input and output
mjr 77:0b96f6867312 2674 void process_IR(Config &cfg, USBJoystick &js)
mjr 77:0b96f6867312 2675 {
mjr 78:1e00b3fa11af 2676 // check for transmitter tasks, if there's a transmitter
mjr 78:1e00b3fa11af 2677 if (ir_tx != 0)
mjr 77:0b96f6867312 2678 {
mjr 78:1e00b3fa11af 2679 // If we're not currently sending, and an ad hoc IR command
mjr 78:1e00b3fa11af 2680 // is ready to send, send it.
mjr 78:1e00b3fa11af 2681 if (!ir_tx->isSending() && IRAdHocCmd.ready)
mjr 78:1e00b3fa11af 2682 {
mjr 78:1e00b3fa11af 2683 // program the command into the transmitter virtual button
mjr 78:1e00b3fa11af 2684 // that we reserved for ad hoc commands
mjr 78:1e00b3fa11af 2685 ir_tx->programButton(IRAdHocBtn, IRAdHocCmd.protocol,
mjr 78:1e00b3fa11af 2686 IRAdHocCmd.dittos, IRAdHocCmd.code);
mjr 78:1e00b3fa11af 2687
mjr 78:1e00b3fa11af 2688 // send the command - just pulse the button to send it once
mjr 78:1e00b3fa11af 2689 ir_tx->pushButton(IRAdHocBtn, true);
mjr 78:1e00b3fa11af 2690 ir_tx->pushButton(IRAdHocBtn, false);
mjr 78:1e00b3fa11af 2691
mjr 78:1e00b3fa11af 2692 // we've sent the command, so clear the 'ready' flag
mjr 78:1e00b3fa11af 2693 IRAdHocCmd.ready = false;
mjr 78:1e00b3fa11af 2694 }
mjr 77:0b96f6867312 2695 }
mjr 78:1e00b3fa11af 2696
mjr 78:1e00b3fa11af 2697 // check for receiver tasks, if there's a receiver
mjr 78:1e00b3fa11af 2698 if (ir_rx != 0)
mjr 77:0b96f6867312 2699 {
mjr 78:1e00b3fa11af 2700 // Time out any received command
mjr 78:1e00b3fa11af 2701 if (IRCommandIn != 0)
mjr 78:1e00b3fa11af 2702 {
mjr 80:94dc2946871b 2703 // Time out commands after 200ms without a repeat signal.
mjr 80:94dc2946871b 2704 // Time out the inter-key gap after 50ms.
mjr 78:1e00b3fa11af 2705 uint32_t t = IRTimer.read_us();
mjr 80:94dc2946871b 2706 if (t > 200000)
mjr 78:1e00b3fa11af 2707 IRCommandIn = 0;
mjr 80:94dc2946871b 2708 else if (t > 50000)
mjr 78:1e00b3fa11af 2709 IRKeyGap = false;
mjr 78:1e00b3fa11af 2710 }
mjr 78:1e00b3fa11af 2711
mjr 78:1e00b3fa11af 2712 // Check if we're in learning mode
mjr 78:1e00b3fa11af 2713 if (IRLearningMode != 0)
mjr 78:1e00b3fa11af 2714 {
mjr 78:1e00b3fa11af 2715 // Learning mode. Read raw inputs from the IR sensor and
mjr 78:1e00b3fa11af 2716 // forward them to the PC via USB reports, up to the report
mjr 78:1e00b3fa11af 2717 // limit.
mjr 78:1e00b3fa11af 2718 const int nmax = USBJoystick::maxRawIR;
mjr 78:1e00b3fa11af 2719 uint16_t raw[nmax];
mjr 78:1e00b3fa11af 2720 int n;
mjr 78:1e00b3fa11af 2721 for (n = 0 ; n < nmax && ir_rx->processOne(raw[n]) ; ++n) ;
mjr 77:0b96f6867312 2722
mjr 78:1e00b3fa11af 2723 // if we read any raw samples, report them
mjr 78:1e00b3fa11af 2724 if (n != 0)
mjr 78:1e00b3fa11af 2725 js.reportRawIR(n, raw);
mjr 77:0b96f6867312 2726
mjr 78:1e00b3fa11af 2727 // check for a command
mjr 78:1e00b3fa11af 2728 IRCommand c;
mjr 78:1e00b3fa11af 2729 if (ir_rx->readCommand(c))
mjr 78:1e00b3fa11af 2730 {
mjr 78:1e00b3fa11af 2731 // check the current learning state
mjr 78:1e00b3fa11af 2732 switch (IRLearningMode)
mjr 78:1e00b3fa11af 2733 {
mjr 78:1e00b3fa11af 2734 case 1:
mjr 78:1e00b3fa11af 2735 // Initial state, waiting for the first decoded command.
mjr 78:1e00b3fa11af 2736 // This is it.
mjr 78:1e00b3fa11af 2737 learnedIRCode = c;
mjr 78:1e00b3fa11af 2738
mjr 78:1e00b3fa11af 2739 // Check if we need additional information. If the
mjr 78:1e00b3fa11af 2740 // protocol supports dittos, we have to wait for a repeat
mjr 78:1e00b3fa11af 2741 // to see if the remote actually uses the dittos, since
mjr 78:1e00b3fa11af 2742 // some implementations of such protocols use the dittos
mjr 78:1e00b3fa11af 2743 // while others just send repeated full codes. Otherwise,
mjr 78:1e00b3fa11af 2744 // all we need is the initial code, so we're done.
mjr 78:1e00b3fa11af 2745 IRLearningMode = (c.hasDittos ? 2 : 3);
mjr 78:1e00b3fa11af 2746 break;
mjr 78:1e00b3fa11af 2747
mjr 78:1e00b3fa11af 2748 case 2:
mjr 78:1e00b3fa11af 2749 // Code received, awaiting auto-repeat information. If
mjr 78:1e00b3fa11af 2750 // the protocol has dittos, check to see if we got a ditto:
mjr 78:1e00b3fa11af 2751 //
mjr 78:1e00b3fa11af 2752 // - If we received a ditto in the same protocol as the
mjr 78:1e00b3fa11af 2753 // prior command, the remote uses dittos.
mjr 78:1e00b3fa11af 2754 //
mjr 78:1e00b3fa11af 2755 // - If we received a repeat of the prior command (not a
mjr 78:1e00b3fa11af 2756 // ditto, but a repeat of the full code), the remote
mjr 78:1e00b3fa11af 2757 // doesn't use dittos even though the protocol supports
mjr 78:1e00b3fa11af 2758 // them.
mjr 78:1e00b3fa11af 2759 //
mjr 78:1e00b3fa11af 2760 // - Otherwise, it's not an auto-repeat at all, so we
mjr 78:1e00b3fa11af 2761 // can't decide one way or the other on dittos: start
mjr 78:1e00b3fa11af 2762 // over.
mjr 78:1e00b3fa11af 2763 if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2764 && c.hasDittos
mjr 78:1e00b3fa11af 2765 && c.ditto)
mjr 78:1e00b3fa11af 2766 {
mjr 78:1e00b3fa11af 2767 // success - the remote uses dittos
mjr 78:1e00b3fa11af 2768 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2769 }
mjr 78:1e00b3fa11af 2770 else if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2771 && c.hasDittos
mjr 78:1e00b3fa11af 2772 && !c.ditto
mjr 78:1e00b3fa11af 2773 && c.code == learnedIRCode.code)
mjr 78:1e00b3fa11af 2774 {
mjr 78:1e00b3fa11af 2775 // success - it's a repeat of the last code, so
mjr 78:1e00b3fa11af 2776 // the remote doesn't use dittos even though the
mjr 78:1e00b3fa11af 2777 // protocol supports them
mjr 78:1e00b3fa11af 2778 learnedIRCode.hasDittos = false;
mjr 78:1e00b3fa11af 2779 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2780 }
mjr 78:1e00b3fa11af 2781 else
mjr 78:1e00b3fa11af 2782 {
mjr 78:1e00b3fa11af 2783 // It's not a ditto and not a full repeat of the
mjr 78:1e00b3fa11af 2784 // last code, so it's either a new key, or some kind
mjr 78:1e00b3fa11af 2785 // of multi-code key encoding that we don't recognize.
mjr 78:1e00b3fa11af 2786 // We can't use this code, so start over.
mjr 78:1e00b3fa11af 2787 IRLearningMode = 1;
mjr 78:1e00b3fa11af 2788 }
mjr 78:1e00b3fa11af 2789 break;
mjr 78:1e00b3fa11af 2790 }
mjr 77:0b96f6867312 2791
mjr 78:1e00b3fa11af 2792 // If we ended in state 3, we've successfully decoded
mjr 78:1e00b3fa11af 2793 // the transmission. Report the decoded data and terminate
mjr 78:1e00b3fa11af 2794 // learning mode.
mjr 78:1e00b3fa11af 2795 if (IRLearningMode == 3)
mjr 77:0b96f6867312 2796 {
mjr 78:1e00b3fa11af 2797 // figure the flags:
mjr 78:1e00b3fa11af 2798 // 0x02 -> dittos
mjr 78:1e00b3fa11af 2799 uint8_t flags = 0;
mjr 78:1e00b3fa11af 2800 if (learnedIRCode.hasDittos)
mjr 78:1e00b3fa11af 2801 flags |= 0x02;
mjr 78:1e00b3fa11af 2802
mjr 78:1e00b3fa11af 2803 // report the code
mjr 78:1e00b3fa11af 2804 js.reportIRCode(learnedIRCode.proId, flags, learnedIRCode.code);
mjr 78:1e00b3fa11af 2805
mjr 78:1e00b3fa11af 2806 // exit learning mode
mjr 78:1e00b3fa11af 2807 IRLearningMode = 0;
mjr 77:0b96f6867312 2808 }
mjr 77:0b96f6867312 2809 }
mjr 77:0b96f6867312 2810
mjr 78:1e00b3fa11af 2811 // time out of IR learning mode if it's been too long
mjr 78:1e00b3fa11af 2812 if (IRLearningMode != 0 && IRTimer.read_us() > 10000000L)
mjr 77:0b96f6867312 2813 {
mjr 78:1e00b3fa11af 2814 // report the termination by sending a raw IR report with
mjr 78:1e00b3fa11af 2815 // zero data elements
mjr 78:1e00b3fa11af 2816 js.reportRawIR(0, 0);
mjr 78:1e00b3fa11af 2817
mjr 78:1e00b3fa11af 2818
mjr 78:1e00b3fa11af 2819 // cancel learning mode
mjr 77:0b96f6867312 2820 IRLearningMode = 0;
mjr 77:0b96f6867312 2821 }
mjr 77:0b96f6867312 2822 }
mjr 78:1e00b3fa11af 2823 else
mjr 77:0b96f6867312 2824 {
mjr 78:1e00b3fa11af 2825 // Not in learning mode. We don't care about the raw signals;
mjr 78:1e00b3fa11af 2826 // just run them through the protocol decoders.
mjr 78:1e00b3fa11af 2827 ir_rx->process();
mjr 78:1e00b3fa11af 2828
mjr 78:1e00b3fa11af 2829 // Check for decoded commands. Keep going until all commands
mjr 78:1e00b3fa11af 2830 // have been read.
mjr 78:1e00b3fa11af 2831 IRCommand c;
mjr 78:1e00b3fa11af 2832 while (ir_rx->readCommand(c))
mjr 77:0b96f6867312 2833 {
mjr 78:1e00b3fa11af 2834 // We received a decoded command. Determine if it's a repeat,
mjr 78:1e00b3fa11af 2835 // and if so, try to determine whether it's an auto-repeat (due
mjr 78:1e00b3fa11af 2836 // to the remote key being held down) or a distinct new press
mjr 78:1e00b3fa11af 2837 // on the same key as last time. The distinction is significant
mjr 78:1e00b3fa11af 2838 // because it affects the auto-repeat behavior of the PC key
mjr 78:1e00b3fa11af 2839 // input. An auto-repeat represents a key being held down on
mjr 78:1e00b3fa11af 2840 // the remote, which we want to translate to a (virtual) key
mjr 78:1e00b3fa11af 2841 // being held down on the PC keyboard; a distinct key press on
mjr 78:1e00b3fa11af 2842 // the remote translates to a distinct key press on the PC.
mjr 78:1e00b3fa11af 2843 //
mjr 78:1e00b3fa11af 2844 // It can only be a repeat if there's a prior command that
mjr 78:1e00b3fa11af 2845 // hasn't timed out yet, so start by checking for a previous
mjr 78:1e00b3fa11af 2846 // command.
mjr 78:1e00b3fa11af 2847 bool repeat = false, autoRepeat = false;
mjr 78:1e00b3fa11af 2848 if (IRCommandIn != 0)
mjr 77:0b96f6867312 2849 {
mjr 78:1e00b3fa11af 2850 // We have a command in progress. Check to see if the
mjr 78:1e00b3fa11af 2851 // new command is a repeat of the previous command. Check
mjr 78:1e00b3fa11af 2852 // first to see if it's a "ditto", which explicitly represents
mjr 78:1e00b3fa11af 2853 // an auto-repeat of the last command.
mjr 78:1e00b3fa11af 2854 IRCommandCfg &cmdcfg = cfg.IRCommand[IRCommandIn - 1];
mjr 78:1e00b3fa11af 2855 if (c.ditto)
mjr 78:1e00b3fa11af 2856 {
mjr 78:1e00b3fa11af 2857 // We received a ditto. Dittos are always auto-
mjr 78:1e00b3fa11af 2858 // repeats, so it's an auto-repeat as long as the
mjr 78:1e00b3fa11af 2859 // ditto is in the same protocol as the last command.
mjr 78:1e00b3fa11af 2860 // If the ditto is in a new protocol, the ditto can't
mjr 78:1e00b3fa11af 2861 // be for the last command we saw, because a ditto
mjr 78:1e00b3fa11af 2862 // never changes protocols from its antecedent. In
mjr 78:1e00b3fa11af 2863 // such a case, we must have missed the antecedent
mjr 78:1e00b3fa11af 2864 // command and thus don't know what's being repeated.
mjr 78:1e00b3fa11af 2865 repeat = autoRepeat = (c.proId == cmdcfg.protocol);
mjr 78:1e00b3fa11af 2866 }
mjr 78:1e00b3fa11af 2867 else
mjr 78:1e00b3fa11af 2868 {
mjr 78:1e00b3fa11af 2869 // It's not a ditto. The new command is a repeat if
mjr 78:1e00b3fa11af 2870 // it matches the protocol and command code of the
mjr 78:1e00b3fa11af 2871 // prior command.
mjr 78:1e00b3fa11af 2872 repeat = (c.proId == cmdcfg.protocol
mjr 78:1e00b3fa11af 2873 && uint32_t(c.code) == cmdcfg.code.lo
mjr 78:1e00b3fa11af 2874 && uint32_t(c.code >> 32) == cmdcfg.code.hi);
mjr 78:1e00b3fa11af 2875
mjr 78:1e00b3fa11af 2876 // If the command is a repeat, try to determine whether
mjr 78:1e00b3fa11af 2877 // it's an auto-repeat or a new press on the same key.
mjr 78:1e00b3fa11af 2878 // If the protocol uses dittos, it's definitely a new
mjr 78:1e00b3fa11af 2879 // key press, because an auto-repeat would have used a
mjr 78:1e00b3fa11af 2880 // ditto. For a protocol that doesn't use dittos, both
mjr 78:1e00b3fa11af 2881 // an auto-repeat and a new key press just send the key
mjr 78:1e00b3fa11af 2882 // code again, so we can't tell the difference based on
mjr 78:1e00b3fa11af 2883 // that alone. But if the protocol has a toggle bit, we
mjr 78:1e00b3fa11af 2884 // can tell by the toggle bit value: a new key press has
mjr 78:1e00b3fa11af 2885 // the opposite toggle value as the last key press, while
mjr 78:1e00b3fa11af 2886 // an auto-repeat has the same toggle. Note that if the
mjr 78:1e00b3fa11af 2887 // protocol doesn't use toggle bits, the toggle value
mjr 78:1e00b3fa11af 2888 // will always be the same, so we'll simply always treat
mjr 78:1e00b3fa11af 2889 // any repeat as an auto-repeat. Many protocols simply
mjr 78:1e00b3fa11af 2890 // provide no way to distinguish the two, so in such
mjr 78:1e00b3fa11af 2891 // cases it's consistent with the native implementations
mjr 78:1e00b3fa11af 2892 // to treat any repeat as an auto-repeat.
mjr 78:1e00b3fa11af 2893 autoRepeat =
mjr 78:1e00b3fa11af 2894 repeat
mjr 78:1e00b3fa11af 2895 && !(cmdcfg.flags & IRFlagDittos)
mjr 78:1e00b3fa11af 2896 && c.toggle == lastIRToggle;
mjr 78:1e00b3fa11af 2897 }
mjr 78:1e00b3fa11af 2898 }
mjr 78:1e00b3fa11af 2899
mjr 78:1e00b3fa11af 2900 // Check to see if it's a repeat of any kind
mjr 78:1e00b3fa11af 2901 if (repeat)
mjr 78:1e00b3fa11af 2902 {
mjr 78:1e00b3fa11af 2903 // It's a repeat. If it's not an auto-repeat, it's a
mjr 78:1e00b3fa11af 2904 // new distinct key press, so we need to send the PC a
mjr 78:1e00b3fa11af 2905 // momentary gap where we're not sending the same key,
mjr 78:1e00b3fa11af 2906 // so that the PC also recognizes this as a distinct
mjr 78:1e00b3fa11af 2907 // key press event.
mjr 78:1e00b3fa11af 2908 if (!autoRepeat)
mjr 78:1e00b3fa11af 2909 IRKeyGap = true;
mjr 78:1e00b3fa11af 2910
mjr 78:1e00b3fa11af 2911 // restart the key-up timer
mjr 78:1e00b3fa11af 2912 IRTimer.reset();
mjr 78:1e00b3fa11af 2913 }
mjr 78:1e00b3fa11af 2914 else if (c.ditto)
mjr 78:1e00b3fa11af 2915 {
mjr 78:1e00b3fa11af 2916 // It's a ditto, but not a repeat of the last command.
mjr 78:1e00b3fa11af 2917 // But a ditto doesn't contain any information of its own
mjr 78:1e00b3fa11af 2918 // on the command being repeated, so given that it's not
mjr 78:1e00b3fa11af 2919 // our last command, we can't infer what command the ditto
mjr 78:1e00b3fa11af 2920 // is for and thus can't make sense of it. We have to
mjr 78:1e00b3fa11af 2921 // simply ignore it and wait for the sender to start with
mjr 78:1e00b3fa11af 2922 // a full command for a new key press.
mjr 78:1e00b3fa11af 2923 IRCommandIn = 0;
mjr 77:0b96f6867312 2924 }
mjr 77:0b96f6867312 2925 else
mjr 77:0b96f6867312 2926 {
mjr 78:1e00b3fa11af 2927 // It's not a repeat, so the last command is no longer
mjr 78:1e00b3fa11af 2928 // in effect (regardless of whether we find a match for
mjr 78:1e00b3fa11af 2929 // the new command).
mjr 78:1e00b3fa11af 2930 IRCommandIn = 0;
mjr 77:0b96f6867312 2931
mjr 78:1e00b3fa11af 2932 // Check to see if we recognize the new command, by
mjr 78:1e00b3fa11af 2933 // searching for a match in our learned code list.
mjr 78:1e00b3fa11af 2934 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2935 {
mjr 78:1e00b3fa11af 2936 // if the protocol and command code from the code
mjr 78:1e00b3fa11af 2937 // list both match the input, it's a match
mjr 78:1e00b3fa11af 2938 IRCommandCfg &cmdcfg = cfg.IRCommand[i];
mjr 78:1e00b3fa11af 2939 if (cmdcfg.protocol == c.proId
mjr 78:1e00b3fa11af 2940 && cmdcfg.code.lo == uint32_t(c.code)
mjr 78:1e00b3fa11af 2941 && cmdcfg.code.hi == uint32_t(c.code >> 32))
mjr 78:1e00b3fa11af 2942 {
mjr 78:1e00b3fa11af 2943 // Found it! Make this the last command, and
mjr 78:1e00b3fa11af 2944 // remember the starting time.
mjr 78:1e00b3fa11af 2945 IRCommandIn = i + 1;
mjr 78:1e00b3fa11af 2946 lastIRToggle = c.toggle;
mjr 78:1e00b3fa11af 2947 IRTimer.reset();
mjr 78:1e00b3fa11af 2948
mjr 78:1e00b3fa11af 2949 // no need to keep searching
mjr 78:1e00b3fa11af 2950 break;
mjr 78:1e00b3fa11af 2951 }
mjr 77:0b96f6867312 2952 }
mjr 77:0b96f6867312 2953 }
mjr 77:0b96f6867312 2954 }
mjr 77:0b96f6867312 2955 }
mjr 77:0b96f6867312 2956 }
mjr 77:0b96f6867312 2957 }
mjr 77:0b96f6867312 2958
mjr 74:822a92bc11d2 2959
mjr 11:bd9da7088e6e 2960 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 2961 //
mjr 11:bd9da7088e6e 2962 // Button input
mjr 11:bd9da7088e6e 2963 //
mjr 11:bd9da7088e6e 2964
mjr 18:5e890ebd0023 2965 // button state
mjr 18:5e890ebd0023 2966 struct ButtonState
mjr 18:5e890ebd0023 2967 {
mjr 38:091e511ce8a0 2968 ButtonState()
mjr 38:091e511ce8a0 2969 {
mjr 53:9b2611964afc 2970 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 2971 virtState = 0;
mjr 53:9b2611964afc 2972 dbState = 0;
mjr 38:091e511ce8a0 2973 pulseState = 0;
mjr 53:9b2611964afc 2974 pulseTime = 0;
mjr 38:091e511ce8a0 2975 }
mjr 35:e959ffba78fd 2976
mjr 53:9b2611964afc 2977 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 2978 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 2979 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 2980 //
mjr 53:9b2611964afc 2981 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 2982 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 2983 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 2984 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 2985 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 2986 void virtPress(bool on)
mjr 53:9b2611964afc 2987 {
mjr 53:9b2611964afc 2988 // Increment or decrement the current state
mjr 53:9b2611964afc 2989 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 2990 }
mjr 53:9b2611964afc 2991
mjr 53:9b2611964afc 2992 // DigitalIn for the button, if connected to a physical input
mjr 73:4e8ce0b18915 2993 TinyDigitalIn di;
mjr 38:091e511ce8a0 2994
mjr 65:739875521aae 2995 // Time of last pulse state transition.
mjr 65:739875521aae 2996 //
mjr 65:739875521aae 2997 // Each state change sticks for a minimum period; when the timer expires,
mjr 65:739875521aae 2998 // if the underlying physical switch is in a different state, we switch
mjr 65:739875521aae 2999 // to the next state and restart the timer. pulseTime is the time remaining
mjr 65:739875521aae 3000 // remaining before we can make another state transition, in microseconds.
mjr 65:739875521aae 3001 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 65:739875521aae 3002 // this guarantees that the parity of the pulse count always matches the
mjr 65:739875521aae 3003 // current physical switch state when the latter is stable, which makes
mjr 65:739875521aae 3004 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 65:739875521aae 3005 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 65:739875521aae 3006 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 65:739875521aae 3007 // This software system can't be fooled that way.)
mjr 65:739875521aae 3008 uint32_t pulseTime;
mjr 18:5e890ebd0023 3009
mjr 65:739875521aae 3010 // Config key index. This points to the ButtonCfg structure in the
mjr 65:739875521aae 3011 // configuration that contains the PC key mapping for the button.
mjr 65:739875521aae 3012 uint8_t cfgIndex;
mjr 53:9b2611964afc 3013
mjr 53:9b2611964afc 3014 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 3015 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 3016 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 3017 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 3018 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 3019 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 3020 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 3021 // and physical source states.
mjr 53:9b2611964afc 3022 uint8_t virtState;
mjr 38:091e511ce8a0 3023
mjr 38:091e511ce8a0 3024 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 3025 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 3026 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 3027 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 3028 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 3029 uint8_t dbState;
mjr 38:091e511ce8a0 3030
mjr 65:739875521aae 3031 // current PHYSICAL on/off state, after debouncing
mjr 65:739875521aae 3032 uint8_t physState : 1;
mjr 65:739875521aae 3033
mjr 65:739875521aae 3034 // current LOGICAL on/off state as reported to the host.
mjr 65:739875521aae 3035 uint8_t logState : 1;
mjr 65:739875521aae 3036
mjr 79:682ae3171a08 3037 // Previous logical on/off state, when keys were last processed for USB
mjr 79:682ae3171a08 3038 // reports and local effects. This lets us detect edges (transitions)
mjr 79:682ae3171a08 3039 // in the logical state, for effects that are triggered when the state
mjr 79:682ae3171a08 3040 // changes rather than merely by the button being on or off.
mjr 65:739875521aae 3041 uint8_t prevLogState : 1;
mjr 65:739875521aae 3042
mjr 65:739875521aae 3043 // Pulse state
mjr 65:739875521aae 3044 //
mjr 65:739875521aae 3045 // A button in pulse mode (selected via the config flags for the button)
mjr 65:739875521aae 3046 // transmits a brief logical button press and release each time the attached
mjr 65:739875521aae 3047 // physical switch changes state. This is useful for cases where the host
mjr 65:739875521aae 3048 // expects a key press for each change in the state of the physical switch.
mjr 65:739875521aae 3049 // The canonical example is the Coin Door switch in VPinMAME, which requires
mjr 65:739875521aae 3050 // pressing the END key to toggle the open/closed state. This software design
mjr 65:739875521aae 3051 // isn't easily implemented in a physical coin door, though; the simplest
mjr 65:739875521aae 3052 // physical sensor for the coin door state is a switch that's on when the
mjr 65:739875521aae 3053 // door is open and off when the door is closed (or vice versa, but in either
mjr 65:739875521aae 3054 // case, the switch state corresponds to the current state of the door at any
mjr 65:739875521aae 3055 // given time, rather than pulsing on state changes). The "pulse mode"
mjr 79:682ae3171a08 3056 // option bridges this gap by generating a toggle key event each time
mjr 65:739875521aae 3057 // there's a change to the physical switch's state.
mjr 38:091e511ce8a0 3058 //
mjr 38:091e511ce8a0 3059 // Pulse state:
mjr 38:091e511ce8a0 3060 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 3061 // 1 -> off
mjr 38:091e511ce8a0 3062 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 3063 // 3 -> on
mjr 38:091e511ce8a0 3064 // 4 -> transitioning on-off
mjr 65:739875521aae 3065 uint8_t pulseState : 3; // 5 states -> we need 3 bits
mjr 65:739875521aae 3066
mjr 65:739875521aae 3067 } __attribute__((packed));
mjr 65:739875521aae 3068
mjr 65:739875521aae 3069 ButtonState *buttonState; // live button slots, allocated on startup
mjr 65:739875521aae 3070 int8_t nButtons; // number of live button slots allocated
mjr 65:739875521aae 3071 int8_t zblButtonIndex = -1; // index of ZB Launch button slot; -1 if unused
mjr 18:5e890ebd0023 3072
mjr 66:2e3583fbd2f4 3073 // Shift button state
mjr 66:2e3583fbd2f4 3074 struct
mjr 66:2e3583fbd2f4 3075 {
mjr 66:2e3583fbd2f4 3076 int8_t index; // buttonState[] index of shift button; -1 if none
mjr 78:1e00b3fa11af 3077 uint8_t state; // current state, for "Key OR Shift" mode:
mjr 66:2e3583fbd2f4 3078 // 0 = not shifted
mjr 66:2e3583fbd2f4 3079 // 1 = shift button down, no key pressed yet
mjr 66:2e3583fbd2f4 3080 // 2 = shift button down, key pressed
mjr 78:1e00b3fa11af 3081 // 3 = released, sending pulsed keystroke
mjr 78:1e00b3fa11af 3082 uint32_t pulseTime; // time remaining in pulsed keystroke (state 3)
mjr 66:2e3583fbd2f4 3083 }
mjr 66:2e3583fbd2f4 3084 __attribute__((packed)) shiftButton;
mjr 38:091e511ce8a0 3085
mjr 38:091e511ce8a0 3086 // Button data
mjr 38:091e511ce8a0 3087 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 3088
mjr 38:091e511ce8a0 3089 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 3090 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 3091 // modifier keys.
mjr 38:091e511ce8a0 3092 struct
mjr 38:091e511ce8a0 3093 {
mjr 38:091e511ce8a0 3094 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 3095 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 3096 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 3097 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 3098 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 3099
mjr 38:091e511ce8a0 3100 // Media key state
mjr 38:091e511ce8a0 3101 struct
mjr 38:091e511ce8a0 3102 {
mjr 38:091e511ce8a0 3103 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 3104 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 3105 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 3106
mjr 79:682ae3171a08 3107 // button scan interrupt timer
mjr 79:682ae3171a08 3108 Timeout scanButtonsTimeout;
mjr 38:091e511ce8a0 3109
mjr 38:091e511ce8a0 3110 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 3111 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 3112 void scanButtons()
mjr 38:091e511ce8a0 3113 {
mjr 79:682ae3171a08 3114 // schedule the next interrupt
mjr 79:682ae3171a08 3115 scanButtonsTimeout.attach_us(&scanButtons, 1000);
mjr 79:682ae3171a08 3116
mjr 38:091e511ce8a0 3117 // scan all button input pins
mjr 73:4e8ce0b18915 3118 ButtonState *bs = buttonState, *last = bs + nButtons;
mjr 73:4e8ce0b18915 3119 for ( ; bs < last ; ++bs)
mjr 38:091e511ce8a0 3120 {
mjr 73:4e8ce0b18915 3121 // Shift the new state into the debounce history
mjr 73:4e8ce0b18915 3122 uint8_t db = (bs->dbState << 1) | bs->di.read();
mjr 73:4e8ce0b18915 3123 bs->dbState = db;
mjr 73:4e8ce0b18915 3124
mjr 73:4e8ce0b18915 3125 // If we have all 0's or 1's in the history for the required
mjr 73:4e8ce0b18915 3126 // debounce period, the key state is stable, so apply the new
mjr 73:4e8ce0b18915 3127 // physical state. Note that the pins are active low, so the
mjr 73:4e8ce0b18915 3128 // new button on/off state is the inverse of the GPIO state.
mjr 73:4e8ce0b18915 3129 const uint8_t stable = 0x1F; // 00011111b -> low 5 bits = last 5 readings
mjr 73:4e8ce0b18915 3130 db &= stable;
mjr 73:4e8ce0b18915 3131 if (db == 0 || db == stable)
mjr 73:4e8ce0b18915 3132 bs->physState = !db;
mjr 38:091e511ce8a0 3133 }
mjr 38:091e511ce8a0 3134 }
mjr 38:091e511ce8a0 3135
mjr 38:091e511ce8a0 3136 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 3137 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 3138 // in the physical button state.
mjr 38:091e511ce8a0 3139 Timer buttonTimer;
mjr 12:669df364a565 3140
mjr 65:739875521aae 3141 // Count a button during the initial setup scan
mjr 72:884207c0aab0 3142 void countButton(uint8_t typ, uint8_t shiftTyp, bool &kbKeys)
mjr 65:739875521aae 3143 {
mjr 65:739875521aae 3144 // count it
mjr 65:739875521aae 3145 ++nButtons;
mjr 65:739875521aae 3146
mjr 67:c39e66c4e000 3147 // if it's a keyboard key or media key, note that we need a USB
mjr 67:c39e66c4e000 3148 // keyboard interface
mjr 72:884207c0aab0 3149 if (typ == BtnTypeKey || typ == BtnTypeMedia
mjr 72:884207c0aab0 3150 || shiftTyp == BtnTypeKey || shiftTyp == BtnTypeMedia)
mjr 65:739875521aae 3151 kbKeys = true;
mjr 65:739875521aae 3152 }
mjr 65:739875521aae 3153
mjr 11:bd9da7088e6e 3154 // initialize the button inputs
mjr 35:e959ffba78fd 3155 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 3156 {
mjr 66:2e3583fbd2f4 3157 // presume no shift key
mjr 66:2e3583fbd2f4 3158 shiftButton.index = -1;
mjr 82:4f6209cb5c33 3159 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 3160
mjr 65:739875521aae 3161 // Count up how many button slots we'll need to allocate. Start
mjr 65:739875521aae 3162 // with assigned buttons from the configuration, noting that we
mjr 65:739875521aae 3163 // only need to create slots for buttons that are actually wired.
mjr 65:739875521aae 3164 nButtons = 0;
mjr 65:739875521aae 3165 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 3166 {
mjr 65:739875521aae 3167 // it's valid if it's wired to a real input pin
mjr 65:739875521aae 3168 if (wirePinName(cfg.button[i].pin) != NC)
mjr 72:884207c0aab0 3169 countButton(cfg.button[i].typ, cfg.button[i].typ2, kbKeys);
mjr 65:739875521aae 3170 }
mjr 65:739875521aae 3171
mjr 65:739875521aae 3172 // Count virtual buttons
mjr 65:739875521aae 3173
mjr 65:739875521aae 3174 // ZB Launch
mjr 65:739875521aae 3175 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 65:739875521aae 3176 {
mjr 65:739875521aae 3177 // valid - remember the live button index
mjr 65:739875521aae 3178 zblButtonIndex = nButtons;
mjr 65:739875521aae 3179
mjr 65:739875521aae 3180 // count it
mjr 72:884207c0aab0 3181 countButton(cfg.plunger.zbLaunchBall.keytype, BtnTypeNone, kbKeys);
mjr 65:739875521aae 3182 }
mjr 65:739875521aae 3183
mjr 65:739875521aae 3184 // Allocate the live button slots
mjr 65:739875521aae 3185 ButtonState *bs = buttonState = new ButtonState[nButtons];
mjr 65:739875521aae 3186
mjr 65:739875521aae 3187 // Configure the physical inputs
mjr 65:739875521aae 3188 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 3189 {
mjr 65:739875521aae 3190 PinName pin = wirePinName(cfg.button[i].pin);
mjr 65:739875521aae 3191 if (pin != NC)
mjr 65:739875521aae 3192 {
mjr 65:739875521aae 3193 // point back to the config slot for the keyboard data
mjr 65:739875521aae 3194 bs->cfgIndex = i;
mjr 65:739875521aae 3195
mjr 65:739875521aae 3196 // set up the GPIO input pin for this button
mjr 73:4e8ce0b18915 3197 bs->di.assignPin(pin);
mjr 65:739875521aae 3198
mjr 65:739875521aae 3199 // if it's a pulse mode button, set the initial pulse state to Off
mjr 65:739875521aae 3200 if (cfg.button[i].flags & BtnFlagPulse)
mjr 65:739875521aae 3201 bs->pulseState = 1;
mjr 65:739875521aae 3202
mjr 66:2e3583fbd2f4 3203 // If this is the shift button, note its buttonState[] index.
mjr 66:2e3583fbd2f4 3204 // We have to figure the buttonState[] index separately from
mjr 66:2e3583fbd2f4 3205 // the config index, because the indices can differ if some
mjr 66:2e3583fbd2f4 3206 // config slots are left unused.
mjr 78:1e00b3fa11af 3207 if (cfg.shiftButton.idx == i+1)
mjr 66:2e3583fbd2f4 3208 shiftButton.index = bs - buttonState;
mjr 66:2e3583fbd2f4 3209
mjr 65:739875521aae 3210 // advance to the next button
mjr 65:739875521aae 3211 ++bs;
mjr 65:739875521aae 3212 }
mjr 65:739875521aae 3213 }
mjr 65:739875521aae 3214
mjr 53:9b2611964afc 3215 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 3216 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 3217 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 3218 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 3219
mjr 53:9b2611964afc 3220 // ZB Launch Ball button
mjr 65:739875521aae 3221 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 11:bd9da7088e6e 3222 {
mjr 65:739875521aae 3223 // Point back to the config slot for the keyboard data.
mjr 66:2e3583fbd2f4 3224 // We use a special extra slot for virtual buttons,
mjr 66:2e3583fbd2f4 3225 // so we also need to set up the slot data by copying
mjr 66:2e3583fbd2f4 3226 // the ZBL config data to our virtual button slot.
mjr 65:739875521aae 3227 bs->cfgIndex = ZBL_BUTTON_CFG;
mjr 65:739875521aae 3228 cfg.button[ZBL_BUTTON_CFG].pin = PINNAME_TO_WIRE(NC);
mjr 65:739875521aae 3229 cfg.button[ZBL_BUTTON_CFG].typ = cfg.plunger.zbLaunchBall.keytype;
mjr 65:739875521aae 3230 cfg.button[ZBL_BUTTON_CFG].val = cfg.plunger.zbLaunchBall.keycode;
mjr 65:739875521aae 3231
mjr 66:2e3583fbd2f4 3232 // advance to the next button
mjr 65:739875521aae 3233 ++bs;
mjr 11:bd9da7088e6e 3234 }
mjr 12:669df364a565 3235
mjr 38:091e511ce8a0 3236 // start the button scan thread
mjr 79:682ae3171a08 3237 scanButtonsTimeout.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 3238
mjr 38:091e511ce8a0 3239 // start the button state transition timer
mjr 12:669df364a565 3240 buttonTimer.start();
mjr 11:bd9da7088e6e 3241 }
mjr 11:bd9da7088e6e 3242
mjr 67:c39e66c4e000 3243 // Media key mapping. This maps from an 8-bit USB media key
mjr 67:c39e66c4e000 3244 // code to the corresponding bit in our USB report descriptor.
mjr 67:c39e66c4e000 3245 // The USB key code is the index, and the value at the index
mjr 67:c39e66c4e000 3246 // is the report descriptor bit. See joystick.cpp for the
mjr 67:c39e66c4e000 3247 // media descriptor details. Our currently mapped keys are:
mjr 67:c39e66c4e000 3248 //
mjr 67:c39e66c4e000 3249 // 0xE2 -> Mute -> 0x01
mjr 67:c39e66c4e000 3250 // 0xE9 -> Volume Up -> 0x02
mjr 67:c39e66c4e000 3251 // 0xEA -> Volume Down -> 0x04
mjr 67:c39e66c4e000 3252 // 0xB5 -> Next Track -> 0x08
mjr 67:c39e66c4e000 3253 // 0xB6 -> Previous Track -> 0x10
mjr 67:c39e66c4e000 3254 // 0xB7 -> Stop -> 0x20
mjr 67:c39e66c4e000 3255 // 0xCD -> Play / Pause -> 0x40
mjr 67:c39e66c4e000 3256 //
mjr 67:c39e66c4e000 3257 static const uint8_t mediaKeyMap[] = {
mjr 67:c39e66c4e000 3258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-0F
mjr 67:c39e66c4e000 3259 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-1F
mjr 67:c39e66c4e000 3260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-2F
mjr 67:c39e66c4e000 3261 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-3F
mjr 67:c39e66c4e000 3262 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-4F
mjr 67:c39e66c4e000 3263 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-5F
mjr 67:c39e66c4e000 3264 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-6F
mjr 67:c39e66c4e000 3265 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-7F
mjr 67:c39e66c4e000 3266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
mjr 67:c39e66c4e000 3267 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-9F
mjr 67:c39e66c4e000 3268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0-AF
mjr 67:c39e66c4e000 3269 0, 0, 0, 0, 0, 8, 16, 32, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
mjr 67:c39e66c4e000 3270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, // C0-CF
mjr 67:c39e66c4e000 3271 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D0-DF
mjr 67:c39e66c4e000 3272 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, // E0-EF
mjr 67:c39e66c4e000 3273 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-FF
mjr 77:0b96f6867312 3274 };
mjr 77:0b96f6867312 3275
mjr 77:0b96f6867312 3276 // Keyboard key/joystick button state. processButtons() uses this to
mjr 77:0b96f6867312 3277 // build the set of key presses to report to the PC based on the logical
mjr 77:0b96f6867312 3278 // states of the button iputs.
mjr 77:0b96f6867312 3279 struct KeyState
mjr 77:0b96f6867312 3280 {
mjr 77:0b96f6867312 3281 KeyState()
mjr 77:0b96f6867312 3282 {
mjr 77:0b96f6867312 3283 // zero all members
mjr 77:0b96f6867312 3284 memset(this, 0, sizeof(*this));
mjr 77:0b96f6867312 3285 }
mjr 77:0b96f6867312 3286
mjr 77:0b96f6867312 3287 // Keyboard media keys currently pressed. This is a bit vector in
mjr 77:0b96f6867312 3288 // the format used in our USB keyboard reports (see USBJoystick.cpp).
mjr 77:0b96f6867312 3289 uint8_t mediakeys;
mjr 77:0b96f6867312 3290
mjr 77:0b96f6867312 3291 // Keyboard modifier (shift) keys currently pressed. This is a bit
mjr 77:0b96f6867312 3292 // vector in the format used in our USB keyboard reports (see
mjr 77:0b96f6867312 3293 // USBJoystick.cpp).
mjr 77:0b96f6867312 3294 uint8_t modkeys;
mjr 77:0b96f6867312 3295
mjr 77:0b96f6867312 3296 // Regular keyboard keys currently pressed. Each element is a USB
mjr 77:0b96f6867312 3297 // key code, or 0 for empty slots. Note that the USB report format
mjr 77:0b96f6867312 3298 // theoretically allows a flexible size limit, but the Windows KB
mjr 77:0b96f6867312 3299 // drivers have a fixed limit of 6 simultaneous keys (and won't
mjr 77:0b96f6867312 3300 // accept reports with more), so there's no point in making this
mjr 77:0b96f6867312 3301 // flexible; we'll just use the fixed size dictated by Windows.
mjr 77:0b96f6867312 3302 uint8_t keys[7];
mjr 77:0b96f6867312 3303
mjr 77:0b96f6867312 3304 // number of valid entries in keys[] array
mjr 77:0b96f6867312 3305 int nkeys;
mjr 77:0b96f6867312 3306
mjr 77:0b96f6867312 3307 // Joystick buttons pressed, as a bit vector. Bit n (1 << n)
mjr 77:0b96f6867312 3308 // represents joystick button n, n in 0..31, with 0 meaning
mjr 77:0b96f6867312 3309 // unpressed and 1 meaning pressed.
mjr 77:0b96f6867312 3310 uint32_t js;
mjr 77:0b96f6867312 3311
mjr 77:0b96f6867312 3312
mjr 77:0b96f6867312 3313 // Add a key press. 'typ' is the button type code (ButtonTypeXxx),
mjr 77:0b96f6867312 3314 // and 'val' is the value (the meaning of which varies by type code).
mjr 77:0b96f6867312 3315 void addKey(uint8_t typ, uint8_t val)
mjr 77:0b96f6867312 3316 {
mjr 77:0b96f6867312 3317 // add the key according to the type
mjr 77:0b96f6867312 3318 switch (typ)
mjr 77:0b96f6867312 3319 {
mjr 77:0b96f6867312 3320 case BtnTypeJoystick:
mjr 77:0b96f6867312 3321 // joystick button
mjr 77:0b96f6867312 3322 js |= (1 << (val - 1));
mjr 77:0b96f6867312 3323 break;
mjr 77:0b96f6867312 3324
mjr 77:0b96f6867312 3325 case BtnTypeKey:
mjr 77:0b96f6867312 3326 // Keyboard key. The USB keyboard report encodes regular
mjr 77:0b96f6867312 3327 // keys and modifier keys separately, so we need to check
mjr 77:0b96f6867312 3328 // which type we have. Note that past versions mapped the
mjr 77:0b96f6867312 3329 // Keyboard Volume Up, Keyboard Volume Down, and Keyboard
mjr 77:0b96f6867312 3330 // Mute keys to the corresponding Media keys. We no longer
mjr 77:0b96f6867312 3331 // do this; instead, we have the separate BtnTypeMedia for
mjr 77:0b96f6867312 3332 // explicitly using media keys if desired.
mjr 77:0b96f6867312 3333 if (val >= 0xE0 && val <= 0xE7)
mjr 77:0b96f6867312 3334 {
mjr 77:0b96f6867312 3335 // It's a modifier key. These are represented in the USB
mjr 77:0b96f6867312 3336 // reports with a bit mask. We arrange the mask bits in
mjr 77:0b96f6867312 3337 // the same order as the scan codes, so we can figure the
mjr 77:0b96f6867312 3338 // appropriate bit with a simple shift.
mjr 77:0b96f6867312 3339 modkeys |= (1 << (val - 0xE0));
mjr 77:0b96f6867312 3340 }
mjr 77:0b96f6867312 3341 else
mjr 77:0b96f6867312 3342 {
mjr 77:0b96f6867312 3343 // It's a regular key. Make sure it's not already in the
mjr 77:0b96f6867312 3344 // list, and that the list isn't full. If neither of these
mjr 77:0b96f6867312 3345 // apply, add the key to the key array.
mjr 77:0b96f6867312 3346 if (nkeys < 7)
mjr 77:0b96f6867312 3347 {
mjr 77:0b96f6867312 3348 bool found = false;
mjr 77:0b96f6867312 3349 for (int i = 0 ; i < nkeys ; ++i)
mjr 77:0b96f6867312 3350 {
mjr 77:0b96f6867312 3351 if (keys[i] == val)
mjr 77:0b96f6867312 3352 {
mjr 77:0b96f6867312 3353 found = true;
mjr 77:0b96f6867312 3354 break;
mjr 77:0b96f6867312 3355 }
mjr 77:0b96f6867312 3356 }
mjr 77:0b96f6867312 3357 if (!found)
mjr 77:0b96f6867312 3358 keys[nkeys++] = val;
mjr 77:0b96f6867312 3359 }
mjr 77:0b96f6867312 3360 }
mjr 77:0b96f6867312 3361 break;
mjr 77:0b96f6867312 3362
mjr 77:0b96f6867312 3363 case BtnTypeMedia:
mjr 77:0b96f6867312 3364 // Media control key. The media keys are mapped in the USB
mjr 77:0b96f6867312 3365 // report to bits, whereas the key codes are specified in the
mjr 77:0b96f6867312 3366 // config with their USB usage numbers. E.g., the config val
mjr 77:0b96f6867312 3367 // for Media Next Track is 0xB5, but we encode this in the USB
mjr 77:0b96f6867312 3368 // report as bit 0x08. The mediaKeyMap[] table translates
mjr 77:0b96f6867312 3369 // from the USB usage number to the mask bit. If the key isn't
mjr 77:0b96f6867312 3370 // among the subset we support, the mapped bit will be zero, so
mjr 77:0b96f6867312 3371 // the "|=" will have no effect and the key will be ignored.
mjr 77:0b96f6867312 3372 mediakeys |= mediaKeyMap[val];
mjr 77:0b96f6867312 3373 break;
mjr 77:0b96f6867312 3374 }
mjr 77:0b96f6867312 3375 }
mjr 77:0b96f6867312 3376 };
mjr 67:c39e66c4e000 3377
mjr 67:c39e66c4e000 3378
mjr 38:091e511ce8a0 3379 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 3380 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 3381 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 3382 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 3383 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 3384 {
mjr 77:0b96f6867312 3385 // key state
mjr 77:0b96f6867312 3386 KeyState ks;
mjr 38:091e511ce8a0 3387
mjr 38:091e511ce8a0 3388 // calculate the time since the last run
mjr 53:9b2611964afc 3389 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 3390 buttonTimer.reset();
mjr 66:2e3583fbd2f4 3391
mjr 66:2e3583fbd2f4 3392 // check the shift button state
mjr 66:2e3583fbd2f4 3393 if (shiftButton.index != -1)
mjr 66:2e3583fbd2f4 3394 {
mjr 78:1e00b3fa11af 3395 // get the shift button's physical state object
mjr 66:2e3583fbd2f4 3396 ButtonState *sbs = &buttonState[shiftButton.index];
mjr 78:1e00b3fa11af 3397
mjr 78:1e00b3fa11af 3398 // figure what to do based on the shift button mode in the config
mjr 78:1e00b3fa11af 3399 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 3400 {
mjr 66:2e3583fbd2f4 3401 case 0:
mjr 78:1e00b3fa11af 3402 default:
mjr 78:1e00b3fa11af 3403 // "Shift OR Key" mode. The shift button doesn't send its key
mjr 78:1e00b3fa11af 3404 // immediately when pressed. Instead, we wait to see what
mjr 78:1e00b3fa11af 3405 // happens while it's down. Check the current cycle state.
mjr 78:1e00b3fa11af 3406 switch (shiftButton.state)
mjr 78:1e00b3fa11af 3407 {
mjr 78:1e00b3fa11af 3408 case 0:
mjr 78:1e00b3fa11af 3409 // Not shifted. Check if the button is now down: if so,
mjr 78:1e00b3fa11af 3410 // switch to state 1 (shift button down, no key pressed yet).
mjr 78:1e00b3fa11af 3411 if (sbs->physState)
mjr 78:1e00b3fa11af 3412 shiftButton.state = 1;
mjr 78:1e00b3fa11af 3413 break;
mjr 78:1e00b3fa11af 3414
mjr 78:1e00b3fa11af 3415 case 1:
mjr 78:1e00b3fa11af 3416 // Shift button down, no key pressed yet. If the button is
mjr 78:1e00b3fa11af 3417 // now up, it counts as an ordinary button press instead of
mjr 78:1e00b3fa11af 3418 // a shift button press, since the shift function was never
mjr 78:1e00b3fa11af 3419 // used. Return to unshifted state and start a timed key
mjr 78:1e00b3fa11af 3420 // pulse event.
mjr 78:1e00b3fa11af 3421 if (!sbs->physState)
mjr 78:1e00b3fa11af 3422 {
mjr 78:1e00b3fa11af 3423 shiftButton.state = 3;
mjr 78:1e00b3fa11af 3424 shiftButton.pulseTime = 50000+dt; // 50 ms left on the key pulse
mjr 78:1e00b3fa11af 3425 }
mjr 78:1e00b3fa11af 3426 break;
mjr 78:1e00b3fa11af 3427
mjr 78:1e00b3fa11af 3428 case 2:
mjr 78:1e00b3fa11af 3429 // Shift button down, other key was pressed. If the button is
mjr 78:1e00b3fa11af 3430 // now up, simply clear the shift state without sending a key
mjr 78:1e00b3fa11af 3431 // press for the shift button itself to the PC. The shift
mjr 78:1e00b3fa11af 3432 // function was used, so its ordinary key press function is
mjr 78:1e00b3fa11af 3433 // suppressed.
mjr 78:1e00b3fa11af 3434 if (!sbs->physState)
mjr 78:1e00b3fa11af 3435 shiftButton.state = 0;
mjr 78:1e00b3fa11af 3436 break;
mjr 78:1e00b3fa11af 3437
mjr 78:1e00b3fa11af 3438 case 3:
mjr 78:1e00b3fa11af 3439 // Sending pulsed keystroke. Deduct the current time interval
mjr 78:1e00b3fa11af 3440 // from the remaining pulse timer. End the pulse if the time
mjr 78:1e00b3fa11af 3441 // has expired.
mjr 78:1e00b3fa11af 3442 if (shiftButton.pulseTime > dt)
mjr 78:1e00b3fa11af 3443 shiftButton.pulseTime -= dt;
mjr 78:1e00b3fa11af 3444 else
mjr 78:1e00b3fa11af 3445 shiftButton.state = 0;
mjr 78:1e00b3fa11af 3446 break;
mjr 78:1e00b3fa11af 3447 }
mjr 66:2e3583fbd2f4 3448 break;
mjr 66:2e3583fbd2f4 3449
mjr 66:2e3583fbd2f4 3450 case 1:
mjr 78:1e00b3fa11af 3451 // "Shift AND Key" mode. In this mode, the shift button acts
mjr 78:1e00b3fa11af 3452 // like any other button and sends its mapped key immediately.
mjr 78:1e00b3fa11af 3453 // The state cycle in this case simply matches the physical
mjr 78:1e00b3fa11af 3454 // state: ON -> cycle state 1, OFF -> cycle state 0.
mjr 78:1e00b3fa11af 3455 shiftButton.state = (sbs->physState ? 1 : 0);
mjr 66:2e3583fbd2f4 3456 break;
mjr 66:2e3583fbd2f4 3457 }
mjr 66:2e3583fbd2f4 3458 }
mjr 38:091e511ce8a0 3459
mjr 11:bd9da7088e6e 3460 // scan the button list
mjr 18:5e890ebd0023 3461 ButtonState *bs = buttonState;
mjr 65:739875521aae 3462 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 11:bd9da7088e6e 3463 {
mjr 77:0b96f6867312 3464 // get the config entry for the button
mjr 77:0b96f6867312 3465 ButtonCfg *bc = &cfg.button[bs->cfgIndex];
mjr 77:0b96f6867312 3466
mjr 66:2e3583fbd2f4 3467 // Check the button type:
mjr 66:2e3583fbd2f4 3468 // - shift button
mjr 66:2e3583fbd2f4 3469 // - pulsed button
mjr 66:2e3583fbd2f4 3470 // - regular button
mjr 66:2e3583fbd2f4 3471 if (shiftButton.index == i)
mjr 66:2e3583fbd2f4 3472 {
mjr 78:1e00b3fa11af 3473 // This is the shift button. The logical state handling
mjr 78:1e00b3fa11af 3474 // depends on the mode.
mjr 78:1e00b3fa11af 3475 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 3476 {
mjr 78:1e00b3fa11af 3477 case 0:
mjr 78:1e00b3fa11af 3478 default:
mjr 78:1e00b3fa11af 3479 // "Shift OR Key" mode. The logical state is ON only
mjr 78:1e00b3fa11af 3480 // during the timed pulse when the key is released, which
mjr 78:1e00b3fa11af 3481 // is signified by shift button state 3.
mjr 78:1e00b3fa11af 3482 bs->logState = (shiftButton.state == 3);
mjr 78:1e00b3fa11af 3483 break;
mjr 78:1e00b3fa11af 3484
mjr 78:1e00b3fa11af 3485 case 1:
mjr 78:1e00b3fa11af 3486 // "Shif AND Key" mode. The shift button acts like any
mjr 78:1e00b3fa11af 3487 // other button, so it's logically on when physically on.
mjr 78:1e00b3fa11af 3488 bs->logState = bs->physState;
mjr 78:1e00b3fa11af 3489 break;
mjr 66:2e3583fbd2f4 3490 }
mjr 66:2e3583fbd2f4 3491 }
mjr 66:2e3583fbd2f4 3492 else if (bs->pulseState != 0)
mjr 18:5e890ebd0023 3493 {
mjr 38:091e511ce8a0 3494 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 3495 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 3496 {
mjr 53:9b2611964afc 3497 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 3498 bs->pulseTime -= dt;
mjr 53:9b2611964afc 3499 }
mjr 53:9b2611964afc 3500 else
mjr 53:9b2611964afc 3501 {
mjr 53:9b2611964afc 3502 // pulse time expired - check for a state change
mjr 53:9b2611964afc 3503 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 3504 switch (bs->pulseState)
mjr 18:5e890ebd0023 3505 {
mjr 38:091e511ce8a0 3506 case 1:
mjr 38:091e511ce8a0 3507 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 3508 if (bs->physState)
mjr 53:9b2611964afc 3509 {
mjr 38:091e511ce8a0 3510 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 3511 bs->pulseState = 2;
mjr 53:9b2611964afc 3512 bs->logState = 1;
mjr 38:091e511ce8a0 3513 }
mjr 38:091e511ce8a0 3514 break;
mjr 18:5e890ebd0023 3515
mjr 38:091e511ce8a0 3516 case 2:
mjr 38:091e511ce8a0 3517 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 3518 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 3519 // change in state in the logical button
mjr 38:091e511ce8a0 3520 bs->pulseState = 3;
mjr 38:091e511ce8a0 3521 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 3522 bs->logState = 0;
mjr 38:091e511ce8a0 3523 break;
mjr 38:091e511ce8a0 3524
mjr 38:091e511ce8a0 3525 case 3:
mjr 38:091e511ce8a0 3526 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 3527 if (!bs->physState)
mjr 53:9b2611964afc 3528 {
mjr 38:091e511ce8a0 3529 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 3530 bs->pulseState = 4;
mjr 53:9b2611964afc 3531 bs->logState = 1;
mjr 38:091e511ce8a0 3532 }
mjr 38:091e511ce8a0 3533 break;
mjr 38:091e511ce8a0 3534
mjr 38:091e511ce8a0 3535 case 4:
mjr 38:091e511ce8a0 3536 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 3537 bs->pulseState = 1;
mjr 38:091e511ce8a0 3538 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 3539 bs->logState = 0;
mjr 38:091e511ce8a0 3540 break;
mjr 18:5e890ebd0023 3541 }
mjr 18:5e890ebd0023 3542 }
mjr 38:091e511ce8a0 3543 }
mjr 38:091e511ce8a0 3544 else
mjr 38:091e511ce8a0 3545 {
mjr 38:091e511ce8a0 3546 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 3547 bs->logState = bs->physState;
mjr 38:091e511ce8a0 3548 }
mjr 77:0b96f6867312 3549
mjr 77:0b96f6867312 3550 // Determine if we're going to use the shifted version of the
mjr 78:1e00b3fa11af 3551 // button. We're using the shifted version if...
mjr 78:1e00b3fa11af 3552 //
mjr 78:1e00b3fa11af 3553 // - the shift button is down, AND
mjr 78:1e00b3fa11af 3554 // - this button isn't itself the shift button, AND
mjr 78:1e00b3fa11af 3555 // - this button has some kind of shifted meaning
mjr 77:0b96f6867312 3556 //
mjr 78:1e00b3fa11af 3557 // A "shifted meaning" means that we have any of the following
mjr 78:1e00b3fa11af 3558 // assigned to the shifted version of the button: a key assignment,
mjr 78:1e00b3fa11af 3559 // (in typ2,key2), an IR command (in IRCommand2), or Night mode.
mjr 78:1e00b3fa11af 3560 //
mjr 78:1e00b3fa11af 3561 // The test for Night Mode is a bit tricky. The shifted version of
mjr 78:1e00b3fa11af 3562 // the button is the Night Mode toggle if the button matches the
mjr 78:1e00b3fa11af 3563 // Night Mode button index, AND its flags are set with "toggle mode
mjr 78:1e00b3fa11af 3564 // ON" (bit 0x02 is on) and "switch mode OFF" (bit 0x01 is off).
mjr 78:1e00b3fa11af 3565 // So (button flags) & 0x03 must equal 0x02.
mjr 77:0b96f6867312 3566 bool useShift =
mjr 77:0b96f6867312 3567 (shiftButton.state != 0
mjr 78:1e00b3fa11af 3568 && shiftButton.index != i
mjr 77:0b96f6867312 3569 && (bc->typ2 != BtnTypeNone
mjr 77:0b96f6867312 3570 || bc->IRCommand2 != 0
mjr 77:0b96f6867312 3571 || (cfg.nightMode.btn == i+1 && (cfg.nightMode.flags & 0x03) == 0x02)));
mjr 77:0b96f6867312 3572
mjr 77:0b96f6867312 3573 // If we're using the shift function, and no other button has used
mjr 77:0b96f6867312 3574 // the shift function yet (shift state 1: "shift button is down but
mjr 77:0b96f6867312 3575 // no one has used the shift function yet"), then we've "consumed"
mjr 77:0b96f6867312 3576 // the shift button press (so go to shift state 2: "shift button has
mjr 77:0b96f6867312 3577 // been used by some other button press that has a shifted meaning").
mjr 78:1e00b3fa11af 3578 if (useShift && shiftButton.state == 1 && bs->logState)
mjr 77:0b96f6867312 3579 shiftButton.state = 2;
mjr 35:e959ffba78fd 3580
mjr 38:091e511ce8a0 3581 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 3582 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 3583 {
mjr 77:0b96f6867312 3584 // check to see if this is the Night Mode button
mjr 53:9b2611964afc 3585 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 3586 {
mjr 77:0b96f6867312 3587 // Check the switch type in the config flags. If flag 0x01 is
mjr 77:0b96f6867312 3588 // set, it's a persistent on/off switch, so the night mode
mjr 77:0b96f6867312 3589 // state simply tracks the current state of the switch.
mjr 77:0b96f6867312 3590 // Otherwise, it's a momentary button, so each button push
mjr 77:0b96f6867312 3591 // (i.e., each transition from logical state OFF to ON) toggles
mjr 77:0b96f6867312 3592 // the night mode state.
mjr 77:0b96f6867312 3593 //
mjr 77:0b96f6867312 3594 // Note that the "shift" flag (0x02) has no effect in switch
mjr 77:0b96f6867312 3595 // mode. Shifting only works for toggle mode.
mjr 82:4f6209cb5c33 3596 if ((cfg.nightMode.flags & 0x01) != 0)
mjr 53:9b2611964afc 3597 {
mjr 77:0b96f6867312 3598 // It's an on/off switch. Night mode simply tracks the
mjr 77:0b96f6867312 3599 // current switch state.
mjr 53:9b2611964afc 3600 setNightMode(bs->logState);
mjr 53:9b2611964afc 3601 }
mjr 82:4f6209cb5c33 3602 else if (bs->logState)
mjr 53:9b2611964afc 3603 {
mjr 77:0b96f6867312 3604 // It's a momentary toggle switch. Toggle the night mode
mjr 77:0b96f6867312 3605 // state on each distinct press of the button: that is,
mjr 77:0b96f6867312 3606 // whenever the button's logical state transitions from
mjr 77:0b96f6867312 3607 // OFF to ON.
mjr 66:2e3583fbd2f4 3608 //
mjr 77:0b96f6867312 3609 // The "shift" flag (0x02) tells us whether night mode is
mjr 77:0b96f6867312 3610 // assigned to the shifted or unshifted version of the
mjr 77:0b96f6867312 3611 // button.
mjr 77:0b96f6867312 3612 bool pressed;
mjr 98:4df3c0f7e707 3613 if (shiftButton.index == i)
mjr 98:4df3c0f7e707 3614 {
mjr 98:4df3c0f7e707 3615 // This button is both the Shift button AND the Night
mjr 98:4df3c0f7e707 3616 // Mode button. This is a special case in that the
mjr 98:4df3c0f7e707 3617 // Shift status is irrelevant, because it's obviously
mjr 98:4df3c0f7e707 3618 // identical to the Night Mode status. So it doesn't
mjr 98:4df3c0f7e707 3619 // matter whether or not the Night Mode button has the
mjr 98:4df3c0f7e707 3620 // shifted flags; the raw button state is all that
mjr 98:4df3c0f7e707 3621 // counts in this case.
mjr 98:4df3c0f7e707 3622 pressed = true;
mjr 98:4df3c0f7e707 3623 }
mjr 98:4df3c0f7e707 3624 else if ((cfg.nightMode.flags & 0x02) != 0)
mjr 66:2e3583fbd2f4 3625 {
mjr 77:0b96f6867312 3626 // Shift bit is set - night mode is assigned to the
mjr 77:0b96f6867312 3627 // shifted version of the button. This is a Night
mjr 77:0b96f6867312 3628 // Mode toggle only if the Shift button is pressed.
mjr 77:0b96f6867312 3629 pressed = (shiftButton.state != 0);
mjr 77:0b96f6867312 3630 }
mjr 77:0b96f6867312 3631 else
mjr 77:0b96f6867312 3632 {
mjr 77:0b96f6867312 3633 // No shift bit - night mode is assigned to the
mjr 77:0b96f6867312 3634 // regular unshifted button. The button press only
mjr 77:0b96f6867312 3635 // applies if the Shift button is NOT pressed.
mjr 77:0b96f6867312 3636 pressed = (shiftButton.state == 0);
mjr 66:2e3583fbd2f4 3637 }
mjr 66:2e3583fbd2f4 3638
mjr 66:2e3583fbd2f4 3639 // if it's pressed (even after considering the shift mode),
mjr 66:2e3583fbd2f4 3640 // toggle night mode
mjr 66:2e3583fbd2f4 3641 if (pressed)
mjr 53:9b2611964afc 3642 toggleNightMode();
mjr 53:9b2611964afc 3643 }
mjr 35:e959ffba78fd 3644 }
mjr 38:091e511ce8a0 3645
mjr 77:0b96f6867312 3646 // press or release IR virtual keys on key state changes
mjr 77:0b96f6867312 3647 uint8_t irc = useShift ? bc->IRCommand2 : bc->IRCommand;
mjr 77:0b96f6867312 3648 if (irc != 0)
mjr 77:0b96f6867312 3649 IR_buttonChange(irc, bs->logState);
mjr 77:0b96f6867312 3650
mjr 38:091e511ce8a0 3651 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 3652 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 3653 }
mjr 38:091e511ce8a0 3654
mjr 53:9b2611964afc 3655 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 3656 // key state list
mjr 53:9b2611964afc 3657 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 3658 {
mjr 70:9f58735a1732 3659 // Get the key type and code. Start by assuming that we're
mjr 70:9f58735a1732 3660 // going to use the normal unshifted meaning.
mjr 77:0b96f6867312 3661 uint8_t typ, val;
mjr 77:0b96f6867312 3662 if (useShift)
mjr 66:2e3583fbd2f4 3663 {
mjr 77:0b96f6867312 3664 typ = bc->typ2;
mjr 77:0b96f6867312 3665 val = bc->val2;
mjr 66:2e3583fbd2f4 3666 }
mjr 77:0b96f6867312 3667 else
mjr 77:0b96f6867312 3668 {
mjr 77:0b96f6867312 3669 typ = bc->typ;
mjr 77:0b96f6867312 3670 val = bc->val;
mjr 77:0b96f6867312 3671 }
mjr 77:0b96f6867312 3672
mjr 70:9f58735a1732 3673 // We've decided on the meaning of the button, so process
mjr 70:9f58735a1732 3674 // the keyboard or joystick event.
mjr 77:0b96f6867312 3675 ks.addKey(typ, val);
mjr 18:5e890ebd0023 3676 }
mjr 11:bd9da7088e6e 3677 }
mjr 77:0b96f6867312 3678
mjr 77:0b96f6867312 3679 // If an IR input command is in effect, add the IR command's
mjr 77:0b96f6867312 3680 // assigned key, if any. If we're in an IR key gap, don't include
mjr 77:0b96f6867312 3681 // the IR key.
mjr 77:0b96f6867312 3682 if (IRCommandIn != 0 && !IRKeyGap)
mjr 77:0b96f6867312 3683 {
mjr 77:0b96f6867312 3684 IRCommandCfg &irc = cfg.IRCommand[IRCommandIn - 1];
mjr 77:0b96f6867312 3685 ks.addKey(irc.keytype, irc.keycode);
mjr 77:0b96f6867312 3686 }
mjr 77:0b96f6867312 3687
mjr 77:0b96f6867312 3688 // We're finished building the new key state. Update the global
mjr 77:0b96f6867312 3689 // key state variables to reflect the new state.
mjr 77:0b96f6867312 3690
mjr 77:0b96f6867312 3691 // set the new joystick buttons (no need to check for changes, as we
mjr 77:0b96f6867312 3692 // report these on every joystick report whether they changed or not)
mjr 77:0b96f6867312 3693 jsButtons = ks.js;
mjr 77:0b96f6867312 3694
mjr 77:0b96f6867312 3695 // check for keyboard key changes (we only send keyboard reports when
mjr 77:0b96f6867312 3696 // something changes)
mjr 77:0b96f6867312 3697 if (kbState.data[0] != ks.modkeys
mjr 77:0b96f6867312 3698 || kbState.nkeys != ks.nkeys
mjr 77:0b96f6867312 3699 || memcmp(ks.keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 3700 {
mjr 35:e959ffba78fd 3701 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3702 kbState.changed = true;
mjr 77:0b96f6867312 3703 kbState.data[0] = ks.modkeys;
mjr 77:0b96f6867312 3704 if (ks.nkeys <= 6) {
mjr 35:e959ffba78fd 3705 // 6 or fewer simultaneous keys - report the key codes
mjr 77:0b96f6867312 3706 kbState.nkeys = ks.nkeys;
mjr 77:0b96f6867312 3707 memcpy(&kbState.data[2], ks.keys, 6);
mjr 35:e959ffba78fd 3708 }
mjr 35:e959ffba78fd 3709 else {
mjr 35:e959ffba78fd 3710 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 3711 kbState.nkeys = 6;
mjr 35:e959ffba78fd 3712 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 3713 }
mjr 35:e959ffba78fd 3714 }
mjr 35:e959ffba78fd 3715
mjr 77:0b96f6867312 3716 // check for media key changes (we only send media key reports when
mjr 77:0b96f6867312 3717 // something changes)
mjr 77:0b96f6867312 3718 if (mediaState.data != ks.mediakeys)
mjr 35:e959ffba78fd 3719 {
mjr 77:0b96f6867312 3720 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3721 mediaState.changed = true;
mjr 77:0b96f6867312 3722 mediaState.data = ks.mediakeys;
mjr 35:e959ffba78fd 3723 }
mjr 11:bd9da7088e6e 3724 }
mjr 11:bd9da7088e6e 3725
mjr 73:4e8ce0b18915 3726 // Send a button status report
mjr 73:4e8ce0b18915 3727 void reportButtonStatus(USBJoystick &js)
mjr 73:4e8ce0b18915 3728 {
mjr 73:4e8ce0b18915 3729 // start with all buttons off
mjr 73:4e8ce0b18915 3730 uint8_t state[(MAX_BUTTONS+7)/8];
mjr 73:4e8ce0b18915 3731 memset(state, 0, sizeof(state));
mjr 73:4e8ce0b18915 3732
mjr 73:4e8ce0b18915 3733 // pack the button states into bytes, one bit per button
mjr 73:4e8ce0b18915 3734 ButtonState *bs = buttonState;
mjr 73:4e8ce0b18915 3735 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 73:4e8ce0b18915 3736 {
mjr 73:4e8ce0b18915 3737 // get the physical state
mjr 73:4e8ce0b18915 3738 int b = bs->physState;
mjr 73:4e8ce0b18915 3739
mjr 73:4e8ce0b18915 3740 // pack it into the appropriate bit
mjr 73:4e8ce0b18915 3741 int idx = bs->cfgIndex;
mjr 73:4e8ce0b18915 3742 int si = idx / 8;
mjr 73:4e8ce0b18915 3743 int shift = idx & 0x07;
mjr 73:4e8ce0b18915 3744 state[si] |= b << shift;
mjr 73:4e8ce0b18915 3745 }
mjr 73:4e8ce0b18915 3746
mjr 73:4e8ce0b18915 3747 // send the report
mjr 73:4e8ce0b18915 3748 js.reportButtonStatus(MAX_BUTTONS, state);
mjr 73:4e8ce0b18915 3749 }
mjr 73:4e8ce0b18915 3750
mjr 5:a70c0bce770d 3751 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3752 //
mjr 5:a70c0bce770d 3753 // Customization joystick subbclass
mjr 5:a70c0bce770d 3754 //
mjr 5:a70c0bce770d 3755
mjr 5:a70c0bce770d 3756 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 3757 {
mjr 5:a70c0bce770d 3758 public:
mjr 35:e959ffba78fd 3759 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 90:aa4e571da8e8 3760 bool waitForConnect, bool enableJoystick, int axisFormat, bool useKB)
mjr 90:aa4e571da8e8 3761 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, axisFormat, useKB)
mjr 5:a70c0bce770d 3762 {
mjr 54:fd77a6b2f76c 3763 sleeping_ = false;
mjr 54:fd77a6b2f76c 3764 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3765 timer_.start();
mjr 54:fd77a6b2f76c 3766 }
mjr 54:fd77a6b2f76c 3767
mjr 54:fd77a6b2f76c 3768 // show diagnostic LED feedback for connect state
mjr 54:fd77a6b2f76c 3769 void diagFlash()
mjr 54:fd77a6b2f76c 3770 {
mjr 54:fd77a6b2f76c 3771 if (!configured() || sleeping_)
mjr 54:fd77a6b2f76c 3772 {
mjr 54:fd77a6b2f76c 3773 // flash once if sleeping or twice if disconnected
mjr 54:fd77a6b2f76c 3774 for (int j = isConnected() ? 1 : 2 ; j > 0 ; --j)
mjr 54:fd77a6b2f76c 3775 {
mjr 54:fd77a6b2f76c 3776 // short red flash
mjr 54:fd77a6b2f76c 3777 diagLED(1, 0, 0);
mjr 54:fd77a6b2f76c 3778 wait_us(50000);
mjr 54:fd77a6b2f76c 3779 diagLED(0, 0, 0);
mjr 54:fd77a6b2f76c 3780 wait_us(50000);
mjr 54:fd77a6b2f76c 3781 }
mjr 54:fd77a6b2f76c 3782 }
mjr 5:a70c0bce770d 3783 }
mjr 5:a70c0bce770d 3784
mjr 5:a70c0bce770d 3785 // are we connected?
mjr 5:a70c0bce770d 3786 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 3787
mjr 54:fd77a6b2f76c 3788 // Are we in sleep mode? If true, this means that the hardware has
mjr 54:fd77a6b2f76c 3789 // detected no activity on the bus for 3ms. This happens when the
mjr 54:fd77a6b2f76c 3790 // cable is physically disconnected, the computer is turned off, or
mjr 54:fd77a6b2f76c 3791 // the connection is otherwise disabled.
mjr 54:fd77a6b2f76c 3792 bool isSleeping() const { return sleeping_; }
mjr 54:fd77a6b2f76c 3793
mjr 54:fd77a6b2f76c 3794 // If necessary, attempt to recover from a broken connection.
mjr 54:fd77a6b2f76c 3795 //
mjr 54:fd77a6b2f76c 3796 // This is a hack, to work around an apparent timing bug in the
mjr 54:fd77a6b2f76c 3797 // KL25Z USB implementation that I haven't been able to solve any
mjr 54:fd77a6b2f76c 3798 // other way.
mjr 54:fd77a6b2f76c 3799 //
mjr 54:fd77a6b2f76c 3800 // The issue: when we have an established connection, and the
mjr 54:fd77a6b2f76c 3801 // connection is broken by physically unplugging the cable or by
mjr 54:fd77a6b2f76c 3802 // rebooting the PC, the KL25Z sometimes fails to reconnect when
mjr 54:fd77a6b2f76c 3803 // the physical connection is re-established. The failure is
mjr 54:fd77a6b2f76c 3804 // sporadic; I'd guess it happens about 25% of the time, but I
mjr 54:fd77a6b2f76c 3805 // haven't collected any real statistics on it.
mjr 54:fd77a6b2f76c 3806 //
mjr 54:fd77a6b2f76c 3807 // The proximate cause of the failure is a deadlock in the SETUP
mjr 54:fd77a6b2f76c 3808 // protocol between the host and device that happens around the
mjr 54:fd77a6b2f76c 3809 // point where the PC is requesting the configuration descriptor.
mjr 54:fd77a6b2f76c 3810 // The exact point in the protocol where this occurs varies slightly;
mjr 54:fd77a6b2f76c 3811 // it can occur a message or two before or after the Get Config
mjr 54:fd77a6b2f76c 3812 // Descriptor packet. No matter where it happens, the nature of
mjr 54:fd77a6b2f76c 3813 // the deadlock is the same: the PC thinks it sees a STALL on EP0
mjr 54:fd77a6b2f76c 3814 // from the device, so it terminates the connection attempt, which
mjr 54:fd77a6b2f76c 3815 // stops further traffic on the cable. The KL25Z USB hardware sees
mjr 54:fd77a6b2f76c 3816 // the lack of traffic and triggers a SLEEP interrupt (a misnomer
mjr 54:fd77a6b2f76c 3817 // for what should have been called a BROKEN CONNECTION interrupt).
mjr 54:fd77a6b2f76c 3818 // Both sides simply stop talking at this point, so the connection
mjr 54:fd77a6b2f76c 3819 // is effectively dead.
mjr 54:fd77a6b2f76c 3820 //
mjr 54:fd77a6b2f76c 3821 // The strange thing is that, as far as I can tell, the KL25Z isn't
mjr 54:fd77a6b2f76c 3822 // doing anything to trigger the STALL on its end. Both the PC
mjr 54:fd77a6b2f76c 3823 // and the KL25Z are happy up until the very point of the failure
mjr 54:fd77a6b2f76c 3824 // and show no signs of anything wrong in the protocol exchange.
mjr 54:fd77a6b2f76c 3825 // In fact, every detail of the protocol exchange up to this point
mjr 54:fd77a6b2f76c 3826 // is identical to every successful exchange that does finish the
mjr 54:fd77a6b2f76c 3827 // whole setup process successfully, on both the KL25Z and Windows
mjr 54:fd77a6b2f76c 3828 // sides of the connection. I can't find any point of difference
mjr 54:fd77a6b2f76c 3829 // between successful and unsuccessful sequences that suggests why
mjr 54:fd77a6b2f76c 3830 // the fateful message fails. This makes me suspect that whatever
mjr 54:fd77a6b2f76c 3831 // is going wrong is inside the KL25Z USB hardware module, which
mjr 54:fd77a6b2f76c 3832 // is a pretty substantial black box - it has a lot of internal
mjr 54:fd77a6b2f76c 3833 // state that's inaccessible to the software. Further bolstering
mjr 54:fd77a6b2f76c 3834 // this theory is a little experiment where I found that I could
mjr 54:fd77a6b2f76c 3835 // reproduce the exact sequence of events of a failed reconnect
mjr 54:fd77a6b2f76c 3836 // attempt in an *initial* connection, which is otherwise 100%
mjr 54:fd77a6b2f76c 3837 // reliable, by inserting a little bit of artifical time padding
mjr 54:fd77a6b2f76c 3838 // (200us per event) into the SETUP interrupt handler. My
mjr 54:fd77a6b2f76c 3839 // hypothesis is that the STALL event happens because the KL25Z
mjr 54:fd77a6b2f76c 3840 // USB hardware is too slow to respond to a message. I'm not
mjr 54:fd77a6b2f76c 3841 // sure why this would only happen after a disconnect and not
mjr 54:fd77a6b2f76c 3842 // during the initial connection; maybe there's some reset work
mjr 54:fd77a6b2f76c 3843 // in the hardware that takes a substantial amount of time after
mjr 54:fd77a6b2f76c 3844 // a disconnect.
mjr 54:fd77a6b2f76c 3845 //
mjr 54:fd77a6b2f76c 3846 // The solution: the problem happens during the SETUP exchange,
mjr 54:fd77a6b2f76c 3847 // after we've been assigned a bus address. It only happens on
mjr 54:fd77a6b2f76c 3848 // some percentage of connection requests, so if we can simply
mjr 54:fd77a6b2f76c 3849 // start over when the failure occurs, we'll eventually succeed
mjr 54:fd77a6b2f76c 3850 // simply because not every attempt fails. The ideal would be
mjr 54:fd77a6b2f76c 3851 // to get the success rate up to 100%, but I can't figure out how
mjr 54:fd77a6b2f76c 3852 // to fix the underlying problem, so this is the next best thing.
mjr 54:fd77a6b2f76c 3853 //
mjr 54:fd77a6b2f76c 3854 // We can detect when the failure occurs by noticing when a SLEEP
mjr 54:fd77a6b2f76c 3855 // interrupt happens while we have an assigned bus address.
mjr 54:fd77a6b2f76c 3856 //
mjr 54:fd77a6b2f76c 3857 // To start a new connection attempt, we have to make the *host*
mjr 54:fd77a6b2f76c 3858 // try again. The logical connection is initiated solely by the
mjr 54:fd77a6b2f76c 3859 // host. Fortunately, it's easy to get the host to initiate the
mjr 54:fd77a6b2f76c 3860 // process: if we disconnect on the device side, it effectively
mjr 54:fd77a6b2f76c 3861 // makes the device look to the PC like it's electrically unplugged.
mjr 54:fd77a6b2f76c 3862 // When we reconnect on the device side, the PC thinks a new device
mjr 54:fd77a6b2f76c 3863 // has been plugged in and initiates the logical connection setup.
mjr 74:822a92bc11d2 3864 // We have to remain disconnected for some minimum interval before
mjr 74:822a92bc11d2 3865 // the host notices; the exact minimum is unclear, but 5ms seems
mjr 74:822a92bc11d2 3866 // reliable in practice.
mjr 54:fd77a6b2f76c 3867 //
mjr 54:fd77a6b2f76c 3868 // Here's the full algorithm:
mjr 54:fd77a6b2f76c 3869 //
mjr 54:fd77a6b2f76c 3870 // 1. In the SLEEP interrupt handler, if we have a bus address,
mjr 54:fd77a6b2f76c 3871 // we disconnect the device. This happens in ISR context, so we
mjr 54:fd77a6b2f76c 3872 // can't wait around for 5ms. Instead, we simply set a flag noting
mjr 54:fd77a6b2f76c 3873 // that the connection has been broken, and we note the time and
mjr 54:fd77a6b2f76c 3874 // return.
mjr 54:fd77a6b2f76c 3875 //
mjr 54:fd77a6b2f76c 3876 // 2. In our main loop, whenever we find that we're disconnected,
mjr 54:fd77a6b2f76c 3877 // we call recoverConnection(). The main loop's job is basically a
mjr 54:fd77a6b2f76c 3878 // bunch of device polling. We're just one more device to poll, so
mjr 54:fd77a6b2f76c 3879 // recoverConnection() will be called soon after a disconnect, and
mjr 54:fd77a6b2f76c 3880 // then will be called in a loop for as long as we're disconnected.
mjr 54:fd77a6b2f76c 3881 //
mjr 54:fd77a6b2f76c 3882 // 3. In recoverConnection(), we check the flag we set in the SLEEP
mjr 54:fd77a6b2f76c 3883 // handler. If set, we wait until 5ms has elapsed from the SLEEP
mjr 54:fd77a6b2f76c 3884 // event time that we noted, then we'll reconnect and clear the flag.
mjr 54:fd77a6b2f76c 3885 // This gives us the required 5ms (or longer) delay between the
mjr 54:fd77a6b2f76c 3886 // disconnect and reconnect, ensuring that the PC will notice and
mjr 54:fd77a6b2f76c 3887 // will start over with the connection protocol.
mjr 54:fd77a6b2f76c 3888 //
mjr 54:fd77a6b2f76c 3889 // 4. The main loop keeps calling recoverConnection() in a loop for
mjr 54:fd77a6b2f76c 3890 // as long as we're disconnected, so if the new connection attempt
mjr 54:fd77a6b2f76c 3891 // triggered in step 3 fails, the SLEEP interrupt will happen again,
mjr 54:fd77a6b2f76c 3892 // we'll disconnect again, the flag will get set again, and
mjr 54:fd77a6b2f76c 3893 // recoverConnection() will reconnect again after another suitable
mjr 54:fd77a6b2f76c 3894 // delay. This will repeat until the connection succeeds or hell
mjr 54:fd77a6b2f76c 3895 // freezes over.
mjr 54:fd77a6b2f76c 3896 //
mjr 54:fd77a6b2f76c 3897 // Each disconnect happens immediately when a reconnect attempt
mjr 54:fd77a6b2f76c 3898 // fails, and an entire successful connection only takes about 25ms,
mjr 54:fd77a6b2f76c 3899 // so our loop can retry at more than 30 attempts per second.
mjr 54:fd77a6b2f76c 3900 // In my testing, lost connections almost always reconnect in
mjr 54:fd77a6b2f76c 3901 // less than second with this code in place.
mjr 54:fd77a6b2f76c 3902 void recoverConnection()
mjr 54:fd77a6b2f76c 3903 {
mjr 54:fd77a6b2f76c 3904 // if a reconnect is pending, reconnect
mjr 54:fd77a6b2f76c 3905 if (reconnectPending_)
mjr 54:fd77a6b2f76c 3906 {
mjr 54:fd77a6b2f76c 3907 // Loop until we reach 5ms after the last sleep event.
mjr 54:fd77a6b2f76c 3908 for (bool done = false ; !done ; )
mjr 54:fd77a6b2f76c 3909 {
mjr 54:fd77a6b2f76c 3910 // If we've reached the target time, reconnect. Do the
mjr 54:fd77a6b2f76c 3911 // time check and flag reset atomically, so that we can't
mjr 54:fd77a6b2f76c 3912 // have another sleep event sneak in after we've verified
mjr 54:fd77a6b2f76c 3913 // the time. If another event occurs, it has to happen
mjr 54:fd77a6b2f76c 3914 // before we check, in which case it'll update the time
mjr 54:fd77a6b2f76c 3915 // before we check it, or after we clear the flag, in
mjr 54:fd77a6b2f76c 3916 // which case it will reset the flag and we'll do another
mjr 54:fd77a6b2f76c 3917 // round the next time we call this routine.
mjr 54:fd77a6b2f76c 3918 __disable_irq();
mjr 54:fd77a6b2f76c 3919 if (uint32_t(timer_.read_us() - lastSleepTime_) > 5000)
mjr 54:fd77a6b2f76c 3920 {
mjr 54:fd77a6b2f76c 3921 connect(false);
mjr 54:fd77a6b2f76c 3922 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3923 done = true;
mjr 54:fd77a6b2f76c 3924 }
mjr 54:fd77a6b2f76c 3925 __enable_irq();
mjr 54:fd77a6b2f76c 3926 }
mjr 54:fd77a6b2f76c 3927 }
mjr 54:fd77a6b2f76c 3928 }
mjr 5:a70c0bce770d 3929
mjr 5:a70c0bce770d 3930 protected:
mjr 54:fd77a6b2f76c 3931 // Handle a USB SLEEP interrupt. This interrupt signifies that the
mjr 54:fd77a6b2f76c 3932 // USB hardware module hasn't seen any token traffic for 3ms, which
mjr 54:fd77a6b2f76c 3933 // means that we're either physically or logically disconnected.
mjr 54:fd77a6b2f76c 3934 //
mjr 54:fd77a6b2f76c 3935 // Important: this runs in ISR context.
mjr 54:fd77a6b2f76c 3936 //
mjr 54:fd77a6b2f76c 3937 // Note that this is a specialized sense of "sleep" that's unrelated
mjr 54:fd77a6b2f76c 3938 // to the similarly named power modes on the PC. This has nothing
mjr 54:fd77a6b2f76c 3939 // to do with suspend/sleep mode on the PC, and it's not a low-power
mjr 54:fd77a6b2f76c 3940 // mode on the KL25Z. They really should have called this interrupt
mjr 54:fd77a6b2f76c 3941 // DISCONNECT or BROKEN CONNECTION.)
mjr 54:fd77a6b2f76c 3942 virtual void sleepStateChanged(unsigned int sleeping)
mjr 54:fd77a6b2f76c 3943 {
mjr 54:fd77a6b2f76c 3944 // note the new state
mjr 54:fd77a6b2f76c 3945 sleeping_ = sleeping;
mjr 54:fd77a6b2f76c 3946
mjr 54:fd77a6b2f76c 3947 // If we have a non-zero bus address, we have at least a partial
mjr 54:fd77a6b2f76c 3948 // connection to the host (we've made it at least as far as the
mjr 54:fd77a6b2f76c 3949 // SETUP stage). Explicitly disconnect, and the pending reconnect
mjr 54:fd77a6b2f76c 3950 // flag, and remember the time of the sleep event.
mjr 54:fd77a6b2f76c 3951 if (USB0->ADDR != 0x00)
mjr 54:fd77a6b2f76c 3952 {
mjr 54:fd77a6b2f76c 3953 disconnect();
mjr 54:fd77a6b2f76c 3954 lastSleepTime_ = timer_.read_us();
mjr 54:fd77a6b2f76c 3955 reconnectPending_ = true;
mjr 54:fd77a6b2f76c 3956 }
mjr 54:fd77a6b2f76c 3957 }
mjr 54:fd77a6b2f76c 3958
mjr 54:fd77a6b2f76c 3959 // is the USB connection asleep?
mjr 54:fd77a6b2f76c 3960 volatile bool sleeping_;
mjr 54:fd77a6b2f76c 3961
mjr 54:fd77a6b2f76c 3962 // flag: reconnect pending after sleep event
mjr 54:fd77a6b2f76c 3963 volatile bool reconnectPending_;
mjr 54:fd77a6b2f76c 3964
mjr 54:fd77a6b2f76c 3965 // time of last sleep event while connected
mjr 54:fd77a6b2f76c 3966 volatile uint32_t lastSleepTime_;
mjr 54:fd77a6b2f76c 3967
mjr 54:fd77a6b2f76c 3968 // timer to keep track of interval since last sleep event
mjr 54:fd77a6b2f76c 3969 Timer timer_;
mjr 5:a70c0bce770d 3970 };
mjr 5:a70c0bce770d 3971
mjr 5:a70c0bce770d 3972 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3973 //
mjr 5:a70c0bce770d 3974 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 3975 //
mjr 5:a70c0bce770d 3976
mjr 5:a70c0bce770d 3977 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 3978 //
mjr 5:a70c0bce770d 3979 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 3980 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 3981 // automatic calibration.
mjr 5:a70c0bce770d 3982 //
mjr 77:0b96f6867312 3983 // We collect data at the device's maximum rate of 800kHz (one sample
mjr 77:0b96f6867312 3984 // every 1.25ms). To keep up with the high data rate, we use the
mjr 77:0b96f6867312 3985 // device's internal FIFO, and drain the FIFO by polling on each
mjr 77:0b96f6867312 3986 // iteration of our main application loop. In the past, we used an
mjr 77:0b96f6867312 3987 // interrupt handler to read the device immediately on the arrival of
mjr 77:0b96f6867312 3988 // each sample, but this created too much latency for the IR remote
mjr 77:0b96f6867312 3989 // receiver, due to the relatively long time it takes to transfer the
mjr 77:0b96f6867312 3990 // accelerometer readings via I2C. The device's on-board FIFO can
mjr 77:0b96f6867312 3991 // store up to 32 samples, which gives us up to about 40ms between
mjr 77:0b96f6867312 3992 // polling iterations before the buffer overflows. Our main loop runs
mjr 77:0b96f6867312 3993 // in under 2ms, so we can easily keep the FIFO far from overflowing.
mjr 77:0b96f6867312 3994 //
mjr 77:0b96f6867312 3995 // The MMA8451Q has three range modes, +/- 2G, 4G, and 8G. The ADC
mjr 77:0b96f6867312 3996 // sample is the same bit width (14 bits) in all modes, so the higher
mjr 77:0b96f6867312 3997 // dynamic range modes trade physical precision for range. For our
mjr 77:0b96f6867312 3998 // purposes, precision is more important than range, so we use the
mjr 77:0b96f6867312 3999 // +/-2G mode. Further, our joystick range is calibrated for only
mjr 77:0b96f6867312 4000 // +/-1G. This was unintentional on my part; I didn't look at the
mjr 77:0b96f6867312 4001 // MMA8451Q library closely enough to realize it was normalizing to
mjr 77:0b96f6867312 4002 // actual "G" units, and assumed that it was normalizing to a -1..+1
mjr 77:0b96f6867312 4003 // scale. In practice, a +/-1G scale seems perfectly adequate for
mjr 77:0b96f6867312 4004 // virtual pinball use, so I'm sticking with that range for now. But
mjr 77:0b96f6867312 4005 // there might be some benefit in renormalizing to a +/-2G range, in
mjr 77:0b96f6867312 4006 // that it would allow for higher dynamic range for very hard nudges.
mjr 77:0b96f6867312 4007 // Everyone would have to tweak their nudge sensitivity in VP if I
mjr 77:0b96f6867312 4008 // made that change, though, so I'm keeping it as is for now; it would
mjr 77:0b96f6867312 4009 // be best to make it a config option ("accelerometer high dynamic range")
mjr 77:0b96f6867312 4010 // rather than change it across the board.
mjr 5:a70c0bce770d 4011 //
mjr 6:cc35eb643e8f 4012 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 4013 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 4014 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 4015 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 4016 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 4017 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 4018 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 4019 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 4020 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 4021 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 4022 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 4023 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 4024 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 4025 // of nudging, say).
mjr 5:a70c0bce770d 4026 //
mjr 5:a70c0bce770d 4027
mjr 17:ab3cec0c8bf4 4028 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 4029 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 4030
mjr 17:ab3cec0c8bf4 4031 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 4032 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 4033 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 4034
mjr 17:ab3cec0c8bf4 4035 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 4036 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 4037 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 4038 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 4039
mjr 17:ab3cec0c8bf4 4040
mjr 6:cc35eb643e8f 4041 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 4042 struct AccHist
mjr 5:a70c0bce770d 4043 {
mjr 77:0b96f6867312 4044 AccHist() { x = y = dsq = 0; xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 4045 void set(int x, int y, AccHist *prv)
mjr 6:cc35eb643e8f 4046 {
mjr 6:cc35eb643e8f 4047 // save the raw position
mjr 6:cc35eb643e8f 4048 this->x = x;
mjr 6:cc35eb643e8f 4049 this->y = y;
mjr 77:0b96f6867312 4050 this->dsq = distanceSquared(prv);
mjr 6:cc35eb643e8f 4051 }
mjr 6:cc35eb643e8f 4052
mjr 6:cc35eb643e8f 4053 // reading for this entry
mjr 77:0b96f6867312 4054 int x, y;
mjr 77:0b96f6867312 4055
mjr 77:0b96f6867312 4056 // (distance from previous entry) squared
mjr 77:0b96f6867312 4057 int dsq;
mjr 5:a70c0bce770d 4058
mjr 6:cc35eb643e8f 4059 // total and count of samples averaged over this period
mjr 77:0b96f6867312 4060 int xtot, ytot;
mjr 6:cc35eb643e8f 4061 int cnt;
mjr 6:cc35eb643e8f 4062
mjr 77:0b96f6867312 4063 void clearAvg() { xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 4064 void addAvg(int x, int y) { xtot += x; ytot += y; ++cnt; }
mjr 77:0b96f6867312 4065 int xAvg() const { return xtot/cnt; }
mjr 77:0b96f6867312 4066 int yAvg() const { return ytot/cnt; }
mjr 77:0b96f6867312 4067
mjr 77:0b96f6867312 4068 int distanceSquared(AccHist *p)
mjr 77:0b96f6867312 4069 { return square(p->x - x) + square(p->y - y); }
mjr 5:a70c0bce770d 4070 };
mjr 5:a70c0bce770d 4071
mjr 5:a70c0bce770d 4072 // accelerometer wrapper class
mjr 3:3514575d4f86 4073 class Accel
mjr 3:3514575d4f86 4074 {
mjr 3:3514575d4f86 4075 public:
mjr 78:1e00b3fa11af 4076 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin,
mjr 78:1e00b3fa11af 4077 int range, int autoCenterMode)
mjr 77:0b96f6867312 4078 : mma_(sda, scl, i2cAddr)
mjr 3:3514575d4f86 4079 {
mjr 5:a70c0bce770d 4080 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 4081 irqPin_ = irqPin;
mjr 77:0b96f6867312 4082
mjr 77:0b96f6867312 4083 // remember the range
mjr 77:0b96f6867312 4084 range_ = range;
mjr 78:1e00b3fa11af 4085
mjr 78:1e00b3fa11af 4086 // set the auto-centering mode
mjr 78:1e00b3fa11af 4087 setAutoCenterMode(autoCenterMode);
mjr 78:1e00b3fa11af 4088
mjr 78:1e00b3fa11af 4089 // no manual centering request has been received
mjr 78:1e00b3fa11af 4090 manualCenterRequest_ = false;
mjr 5:a70c0bce770d 4091
mjr 5:a70c0bce770d 4092 // reset and initialize
mjr 5:a70c0bce770d 4093 reset();
mjr 5:a70c0bce770d 4094 }
mjr 5:a70c0bce770d 4095
mjr 78:1e00b3fa11af 4096 // Request manual centering. This applies the trailing average
mjr 78:1e00b3fa11af 4097 // of recent measurements and applies it as the new center point
mjr 78:1e00b3fa11af 4098 // as soon as we have enough data.
mjr 78:1e00b3fa11af 4099 void manualCenterRequest() { manualCenterRequest_ = true; }
mjr 78:1e00b3fa11af 4100
mjr 78:1e00b3fa11af 4101 // set the auto-centering mode
mjr 78:1e00b3fa11af 4102 void setAutoCenterMode(int mode)
mjr 78:1e00b3fa11af 4103 {
mjr 78:1e00b3fa11af 4104 // remember the mode
mjr 78:1e00b3fa11af 4105 autoCenterMode_ = mode;
mjr 78:1e00b3fa11af 4106
mjr 78:1e00b3fa11af 4107 // Set the time between checks. We check 5 times over the course
mjr 78:1e00b3fa11af 4108 // of the centering time, so the check interval is 1/5 of the total.
mjr 78:1e00b3fa11af 4109 if (mode == 0)
mjr 78:1e00b3fa11af 4110 {
mjr 78:1e00b3fa11af 4111 // mode 0 is the old default of 5 seconds, so check every 1s
mjr 78:1e00b3fa11af 4112 autoCenterCheckTime_ = 1000000;
mjr 78:1e00b3fa11af 4113 }
mjr 78:1e00b3fa11af 4114 else if (mode <= 60)
mjr 78:1e00b3fa11af 4115 {
mjr 78:1e00b3fa11af 4116 // mode 1-60 means reset after 'mode' seconds; the check
mjr 78:1e00b3fa11af 4117 // interval is 1/5 of this
mjr 78:1e00b3fa11af 4118 autoCenterCheckTime_ = mode*200000;
mjr 78:1e00b3fa11af 4119 }
mjr 78:1e00b3fa11af 4120 else
mjr 78:1e00b3fa11af 4121 {
mjr 78:1e00b3fa11af 4122 // Auto-centering is off, but still gather statistics to apply
mjr 78:1e00b3fa11af 4123 // when we get a manual centering request. The check interval
mjr 78:1e00b3fa11af 4124 // in this case is 1/5 of the total time for the trailing average
mjr 78:1e00b3fa11af 4125 // we apply for the manual centering. We want this to be long
mjr 78:1e00b3fa11af 4126 // enough to smooth out the data, but short enough that it only
mjr 78:1e00b3fa11af 4127 // includes recent data.
mjr 78:1e00b3fa11af 4128 autoCenterCheckTime_ = 500000;
mjr 78:1e00b3fa11af 4129 }
mjr 78:1e00b3fa11af 4130 }
mjr 78:1e00b3fa11af 4131
mjr 5:a70c0bce770d 4132 void reset()
mjr 5:a70c0bce770d 4133 {
mjr 6:cc35eb643e8f 4134 // clear the center point
mjr 77:0b96f6867312 4135 cx_ = cy_ = 0;
mjr 6:cc35eb643e8f 4136
mjr 77:0b96f6867312 4137 // start the auto-centering timer
mjr 5:a70c0bce770d 4138 tCenter_.start();
mjr 5:a70c0bce770d 4139 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 4140
mjr 5:a70c0bce770d 4141 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 4142 mma_.init();
mjr 77:0b96f6867312 4143
mjr 77:0b96f6867312 4144 // set the range
mjr 77:0b96f6867312 4145 mma_.setRange(
mjr 77:0b96f6867312 4146 range_ == AccelRange4G ? 4 :
mjr 77:0b96f6867312 4147 range_ == AccelRange8G ? 8 :
mjr 77:0b96f6867312 4148 2);
mjr 6:cc35eb643e8f 4149
mjr 77:0b96f6867312 4150 // set the average accumulators to zero
mjr 77:0b96f6867312 4151 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 4152 nSum_ = 0;
mjr 3:3514575d4f86 4153
mjr 3:3514575d4f86 4154 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 4155 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 4156 }
mjr 3:3514575d4f86 4157
mjr 77:0b96f6867312 4158 void poll()
mjr 76:7f5912b6340e 4159 {
mjr 77:0b96f6867312 4160 // read samples until we clear the FIFO
mjr 77:0b96f6867312 4161 while (mma_.getFIFOCount() != 0)
mjr 77:0b96f6867312 4162 {
mjr 77:0b96f6867312 4163 int x, y, z;
mjr 77:0b96f6867312 4164 mma_.getAccXYZ(x, y, z);
mjr 77:0b96f6867312 4165
mjr 77:0b96f6867312 4166 // add the new reading to the running total for averaging
mjr 77:0b96f6867312 4167 xSum_ += (x - cx_);
mjr 77:0b96f6867312 4168 ySum_ += (y - cy_);
mjr 77:0b96f6867312 4169 ++nSum_;
mjr 77:0b96f6867312 4170
mjr 77:0b96f6867312 4171 // store the updates
mjr 77:0b96f6867312 4172 ax_ = x;
mjr 77:0b96f6867312 4173 ay_ = y;
mjr 77:0b96f6867312 4174 az_ = z;
mjr 77:0b96f6867312 4175 }
mjr 76:7f5912b6340e 4176 }
mjr 77:0b96f6867312 4177
mjr 9:fd65b0a94720 4178 void get(int &x, int &y)
mjr 3:3514575d4f86 4179 {
mjr 77:0b96f6867312 4180 // read the shared data and store locally for calculations
mjr 77:0b96f6867312 4181 int ax = ax_, ay = ay_;
mjr 77:0b96f6867312 4182 int xSum = xSum_, ySum = ySum_;
mjr 77:0b96f6867312 4183 int nSum = nSum_;
mjr 6:cc35eb643e8f 4184
mjr 77:0b96f6867312 4185 // reset the average accumulators for the next run
mjr 77:0b96f6867312 4186 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 4187 nSum_ = 0;
mjr 77:0b96f6867312 4188
mjr 77:0b96f6867312 4189 // add this sample to the current calibration interval's running total
mjr 77:0b96f6867312 4190 AccHist *p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 4191 p->addAvg(ax, ay);
mjr 77:0b96f6867312 4192
mjr 78:1e00b3fa11af 4193 // If we're in auto-centering mode, check for auto-centering
mjr 78:1e00b3fa11af 4194 // at intervals of 1/5 of the overall time. If we're not in
mjr 78:1e00b3fa11af 4195 // auto-centering mode, check anyway at one-second intervals
mjr 78:1e00b3fa11af 4196 // so that we gather averages for manual centering requests.
mjr 78:1e00b3fa11af 4197 if (tCenter_.read_us() > autoCenterCheckTime_)
mjr 77:0b96f6867312 4198 {
mjr 77:0b96f6867312 4199 // add the latest raw sample to the history list
mjr 77:0b96f6867312 4200 AccHist *prv = p;
mjr 77:0b96f6867312 4201 iAccPrv_ = (iAccPrv_ + 1);
mjr 77:0b96f6867312 4202 if (iAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 4203 iAccPrv_ = 0;
mjr 77:0b96f6867312 4204 p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 4205 p->set(ax, ay, prv);
mjr 77:0b96f6867312 4206
mjr 78:1e00b3fa11af 4207 // if we have a full complement, check for auto-centering
mjr 77:0b96f6867312 4208 if (nAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 4209 {
mjr 78:1e00b3fa11af 4210 // Center if:
mjr 78:1e00b3fa11af 4211 //
mjr 78:1e00b3fa11af 4212 // - Auto-centering is on, and we've been stable over the
mjr 78:1e00b3fa11af 4213 // whole sample period at our spot-check points
mjr 78:1e00b3fa11af 4214 //
mjr 78:1e00b3fa11af 4215 // - A manual centering request is pending
mjr 78:1e00b3fa11af 4216 //
mjr 77:0b96f6867312 4217 static const int accTol = 164*164; // 1% of range, squared
mjr 77:0b96f6867312 4218 AccHist *p0 = accPrv_;
mjr 78:1e00b3fa11af 4219 if (manualCenterRequest_
mjr 78:1e00b3fa11af 4220 || (autoCenterMode_ <= 60
mjr 78:1e00b3fa11af 4221 && p0[0].dsq < accTol
mjr 78:1e00b3fa11af 4222 && p0[1].dsq < accTol
mjr 78:1e00b3fa11af 4223 && p0[2].dsq < accTol
mjr 78:1e00b3fa11af 4224 && p0[3].dsq < accTol
mjr 78:1e00b3fa11af 4225 && p0[4].dsq < accTol))
mjr 77:0b96f6867312 4226 {
mjr 77:0b96f6867312 4227 // Figure the new calibration point as the average of
mjr 77:0b96f6867312 4228 // the samples over the rest period
mjr 77:0b96f6867312 4229 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5;
mjr 77:0b96f6867312 4230 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5;
mjr 78:1e00b3fa11af 4231
mjr 78:1e00b3fa11af 4232 // clear any pending manual centering request
mjr 78:1e00b3fa11af 4233 manualCenterRequest_ = false;
mjr 77:0b96f6867312 4234 }
mjr 77:0b96f6867312 4235 }
mjr 77:0b96f6867312 4236 else
mjr 77:0b96f6867312 4237 {
mjr 77:0b96f6867312 4238 // not enough samples yet; just up the count
mjr 77:0b96f6867312 4239 ++nAccPrv_;
mjr 77:0b96f6867312 4240 }
mjr 6:cc35eb643e8f 4241
mjr 77:0b96f6867312 4242 // clear the new item's running totals
mjr 77:0b96f6867312 4243 p->clearAvg();
mjr 5:a70c0bce770d 4244
mjr 77:0b96f6867312 4245 // reset the timer
mjr 77:0b96f6867312 4246 tCenter_.reset();
mjr 77:0b96f6867312 4247 }
mjr 5:a70c0bce770d 4248
mjr 77:0b96f6867312 4249 // report our integrated velocity reading in x,y
mjr 77:0b96f6867312 4250 x = rawToReport(xSum/nSum);
mjr 77:0b96f6867312 4251 y = rawToReport(ySum/nSum);
mjr 5:a70c0bce770d 4252
mjr 6:cc35eb643e8f 4253 #ifdef DEBUG_PRINTF
mjr 77:0b96f6867312 4254 if (x != 0 || y != 0)
mjr 77:0b96f6867312 4255 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 4256 #endif
mjr 77:0b96f6867312 4257 }
mjr 29:582472d0bc57 4258
mjr 3:3514575d4f86 4259 private:
mjr 6:cc35eb643e8f 4260 // adjust a raw acceleration figure to a usb report value
mjr 77:0b96f6867312 4261 int rawToReport(int v)
mjr 5:a70c0bce770d 4262 {
mjr 77:0b96f6867312 4263 // Scale to the joystick report range. The accelerometer
mjr 77:0b96f6867312 4264 // readings use the native 14-bit signed integer representation,
mjr 77:0b96f6867312 4265 // so their scale is 2^13.
mjr 77:0b96f6867312 4266 //
mjr 77:0b96f6867312 4267 // The 1G range is special: it uses the 2G native hardware range,
mjr 77:0b96f6867312 4268 // but rescales the result to a 1G range for the joystick reports.
mjr 77:0b96f6867312 4269 // So for that mode, we divide by 4096 rather than 8192. All of
mjr 77:0b96f6867312 4270 // the other modes map use the hardware scaling directly.
mjr 77:0b96f6867312 4271 int i = v*JOYMAX;
mjr 77:0b96f6867312 4272 i = (range_ == AccelRange1G ? i/4096 : i/8192);
mjr 5:a70c0bce770d 4273
mjr 6:cc35eb643e8f 4274 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 4275 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 4276 static const int filter[] = {
mjr 6:cc35eb643e8f 4277 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 4278 0,
mjr 6:cc35eb643e8f 4279 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 4280 };
mjr 6:cc35eb643e8f 4281 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 4282 }
mjr 5:a70c0bce770d 4283
mjr 3:3514575d4f86 4284 // underlying accelerometer object
mjr 3:3514575d4f86 4285 MMA8451Q mma_;
mjr 3:3514575d4f86 4286
mjr 77:0b96f6867312 4287 // last raw acceleration readings, on the device's signed 14-bit
mjr 77:0b96f6867312 4288 // scale -8192..+8191
mjr 77:0b96f6867312 4289 int ax_, ay_, az_;
mjr 77:0b96f6867312 4290
mjr 77:0b96f6867312 4291 // running sum of readings since last get()
mjr 77:0b96f6867312 4292 int xSum_, ySum_;
mjr 77:0b96f6867312 4293
mjr 77:0b96f6867312 4294 // number of readings since last get()
mjr 77:0b96f6867312 4295 int nSum_;
mjr 6:cc35eb643e8f 4296
mjr 6:cc35eb643e8f 4297 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 4298 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 4299 // at rest.
mjr 77:0b96f6867312 4300 int cx_, cy_;
mjr 77:0b96f6867312 4301
mjr 77:0b96f6867312 4302 // range (AccelRangeXxx value, from config.h)
mjr 77:0b96f6867312 4303 uint8_t range_;
mjr 78:1e00b3fa11af 4304
mjr 78:1e00b3fa11af 4305 // auto-center mode:
mjr 78:1e00b3fa11af 4306 // 0 = default of 5-second auto-centering
mjr 78:1e00b3fa11af 4307 // 1-60 = auto-center after this many seconds
mjr 78:1e00b3fa11af 4308 // 255 = auto-centering off (manual centering only)
mjr 78:1e00b3fa11af 4309 uint8_t autoCenterMode_;
mjr 78:1e00b3fa11af 4310
mjr 78:1e00b3fa11af 4311 // flag: a manual centering request is pending
mjr 78:1e00b3fa11af 4312 bool manualCenterRequest_;
mjr 78:1e00b3fa11af 4313
mjr 78:1e00b3fa11af 4314 // time in us between auto-centering incremental checks
mjr 78:1e00b3fa11af 4315 uint32_t autoCenterCheckTime_;
mjr 78:1e00b3fa11af 4316
mjr 77:0b96f6867312 4317 // atuo-centering timer
mjr 5:a70c0bce770d 4318 Timer tCenter_;
mjr 6:cc35eb643e8f 4319
mjr 6:cc35eb643e8f 4320 // Auto-centering history. This is a separate history list that
mjr 77:0b96f6867312 4321 // records results spaced out sparsely over time, so that we can
mjr 6:cc35eb643e8f 4322 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 4323 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 4324 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 4325 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 4326 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 4327 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 4328 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 4329 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 4330 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 4331 // accelerometer measurement bias (e.g., from temperature changes).
mjr 78:1e00b3fa11af 4332 uint8_t iAccPrv_, nAccPrv_;
mjr 78:1e00b3fa11af 4333 static const uint8_t maxAccPrv = 5;
mjr 6:cc35eb643e8f 4334 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 4335
mjr 5:a70c0bce770d 4336 // interurupt pin name
mjr 5:a70c0bce770d 4337 PinName irqPin_;
mjr 3:3514575d4f86 4338 };
mjr 3:3514575d4f86 4339
mjr 5:a70c0bce770d 4340 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 4341 //
mjr 14:df700b22ca08 4342 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 4343 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 4344 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 4345 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 4346 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 4347 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 4348 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 4349 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 4350 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 4351 //
mjr 14:df700b22ca08 4352 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 4353 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 4354 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 4355 //
mjr 5:a70c0bce770d 4356 void clear_i2c()
mjr 5:a70c0bce770d 4357 {
mjr 38:091e511ce8a0 4358 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 4359 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 4360 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 4361
mjr 5:a70c0bce770d 4362 // clock the SCL 9 times
mjr 5:a70c0bce770d 4363 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 4364 {
mjr 5:a70c0bce770d 4365 scl = 1;
mjr 5:a70c0bce770d 4366 wait_us(20);
mjr 5:a70c0bce770d 4367 scl = 0;
mjr 5:a70c0bce770d 4368 wait_us(20);
mjr 5:a70c0bce770d 4369 }
mjr 5:a70c0bce770d 4370 }
mjr 76:7f5912b6340e 4371
mjr 76:7f5912b6340e 4372
mjr 14:df700b22ca08 4373 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 4374 //
mjr 33:d832bcab089e 4375 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 4376 // for a given interval before allowing an update.
mjr 33:d832bcab089e 4377 //
mjr 33:d832bcab089e 4378 class Debouncer
mjr 33:d832bcab089e 4379 {
mjr 33:d832bcab089e 4380 public:
mjr 33:d832bcab089e 4381 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 4382 {
mjr 33:d832bcab089e 4383 t.start();
mjr 33:d832bcab089e 4384 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 4385 this->tmin = tmin;
mjr 33:d832bcab089e 4386 }
mjr 33:d832bcab089e 4387
mjr 33:d832bcab089e 4388 // Get the current stable value
mjr 33:d832bcab089e 4389 bool val() const { return stable; }
mjr 33:d832bcab089e 4390
mjr 33:d832bcab089e 4391 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 4392 // input device.
mjr 33:d832bcab089e 4393 void sampleIn(bool val)
mjr 33:d832bcab089e 4394 {
mjr 33:d832bcab089e 4395 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 4396 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 4397 // on the sample reader.
mjr 33:d832bcab089e 4398 if (val != prv)
mjr 33:d832bcab089e 4399 {
mjr 33:d832bcab089e 4400 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 4401 t.reset();
mjr 33:d832bcab089e 4402
mjr 33:d832bcab089e 4403 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 4404 prv = val;
mjr 33:d832bcab089e 4405 }
mjr 33:d832bcab089e 4406 else if (val != stable)
mjr 33:d832bcab089e 4407 {
mjr 33:d832bcab089e 4408 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 4409 // and different from the stable value. This means that
mjr 33:d832bcab089e 4410 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 4411 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 4412 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 4413 if (t.read() > tmin)
mjr 33:d832bcab089e 4414 stable = val;
mjr 33:d832bcab089e 4415 }
mjr 33:d832bcab089e 4416 }
mjr 33:d832bcab089e 4417
mjr 33:d832bcab089e 4418 private:
mjr 33:d832bcab089e 4419 // current stable value
mjr 33:d832bcab089e 4420 bool stable;
mjr 33:d832bcab089e 4421
mjr 33:d832bcab089e 4422 // last raw sample value
mjr 33:d832bcab089e 4423 bool prv;
mjr 33:d832bcab089e 4424
mjr 33:d832bcab089e 4425 // elapsed time since last raw input change
mjr 33:d832bcab089e 4426 Timer t;
mjr 33:d832bcab089e 4427
mjr 33:d832bcab089e 4428 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 4429 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 4430 float tmin;
mjr 33:d832bcab089e 4431 };
mjr 33:d832bcab089e 4432
mjr 33:d832bcab089e 4433
mjr 33:d832bcab089e 4434 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 4435 //
mjr 33:d832bcab089e 4436 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 4437 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 4438 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 4439 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 4440 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 4441 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 4442 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 4443 //
mjr 33:d832bcab089e 4444 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 4445 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 4446 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 4447 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 4448 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 4449 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 4450 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 4451 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 4452 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 4453 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 4454 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 4455 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 4456 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 4457 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 4458 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 4459 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 4460 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 4461 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 4462 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 4463 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 4464 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 4465 //
mjr 40:cc0d9814522b 4466 // This scheme might seem a little convoluted, but it handles a number
mjr 40:cc0d9814522b 4467 // of tricky but likely scenarios:
mjr 33:d832bcab089e 4468 //
mjr 33:d832bcab089e 4469 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 40:cc0d9814522b 4470 // so that the PC goes into "Soft Off" mode when the user turns off
mjr 40:cc0d9814522b 4471 // the cabinet by pushing the power button or using the Shut Down
mjr 40:cc0d9814522b 4472 // command from within Windows. In Windows parlance, this "soft off"
mjr 40:cc0d9814522b 4473 // condition is called ACPI State S5. In this state, the main CPU
mjr 40:cc0d9814522b 4474 // power is turned off, but the motherboard still provides power to
mjr 40:cc0d9814522b 4475 // USB devices. This means that the KL25Z keeps running. Without
mjr 40:cc0d9814522b 4476 // the external power sensing circuit, the only hint that we're in
mjr 40:cc0d9814522b 4477 // this state is that the USB connection to the host goes into Suspend
mjr 40:cc0d9814522b 4478 // mode, but that could mean other things as well. The latch circuit
mjr 40:cc0d9814522b 4479 // lets us tell for sure that we're in this state.
mjr 33:d832bcab089e 4480 //
mjr 33:d832bcab089e 4481 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 4482 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 4483 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 4484 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 4485 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 4486 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 4487 // will power up before or after PSU2, so it's not good enough to
mjr 40:cc0d9814522b 4488 // observe the current state of PSU2 when we first check. If PSU2
mjr 40:cc0d9814522b 4489 // were to come on first, checking only the current state would fool
mjr 40:cc0d9814522b 4490 // us into thinking that no action is required, because we'd only see
mjr 40:cc0d9814522b 4491 // that PSU2 is turned on any time we check. The latch handles this
mjr 40:cc0d9814522b 4492 // case by letting us see that PSU2 was indeed off some time before our
mjr 40:cc0d9814522b 4493 // first check.
mjr 33:d832bcab089e 4494 //
mjr 33:d832bcab089e 4495 // - If the KL25Z is rebooted while the main system is running, or the
mjr 40:cc0d9814522b 4496 // KL25Z is unplugged and plugged back in, we'll correctly leave the
mjr 33:d832bcab089e 4497 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 4498 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 4499 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 4500 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 4501 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 4502 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 4503 //
mjr 33:d832bcab089e 4504
mjr 77:0b96f6867312 4505 // Current PSU2 power state:
mjr 33:d832bcab089e 4506 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 4507 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 4508 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 4509 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 4510 // 5 -> TV relay on
mjr 77:0b96f6867312 4511 // 6 -> sending IR signals designed as TV ON signals
mjr 73:4e8ce0b18915 4512 uint8_t psu2_state = 1;
mjr 73:4e8ce0b18915 4513
mjr 73:4e8ce0b18915 4514 // TV relay state. The TV relay can be controlled by the power-on
mjr 73:4e8ce0b18915 4515 // timer and directly from the PC (via USB commands), so keep a
mjr 73:4e8ce0b18915 4516 // separate state for each:
mjr 73:4e8ce0b18915 4517 // 0x01 -> turned on by power-on timer
mjr 73:4e8ce0b18915 4518 // 0x02 -> turned on by USB command
mjr 73:4e8ce0b18915 4519 uint8_t tv_relay_state = 0x00;
mjr 73:4e8ce0b18915 4520 const uint8_t TV_RELAY_POWERON = 0x01;
mjr 73:4e8ce0b18915 4521 const uint8_t TV_RELAY_USB = 0x02;
mjr 73:4e8ce0b18915 4522
mjr 79:682ae3171a08 4523 // pulse timer for manual TV relay pulses
mjr 79:682ae3171a08 4524 Timer tvRelayManualTimer;
mjr 79:682ae3171a08 4525
mjr 77:0b96f6867312 4526 // TV ON IR command state. When the main PSU2 power state reaches
mjr 77:0b96f6867312 4527 // the IR phase, we use this sub-state counter to send the TV ON
mjr 77:0b96f6867312 4528 // IR signals. We initialize to state 0 when the main state counter
mjr 77:0b96f6867312 4529 // reaches the IR step. In state 0, we start transmitting the first
mjr 77:0b96f6867312 4530 // (lowest numbered) IR command slot marked as containing a TV ON
mjr 77:0b96f6867312 4531 // code, and advance to state 1. In state 1, we check to see if
mjr 77:0b96f6867312 4532 // the transmitter is still sending; if so, we do nothing, if so
mjr 77:0b96f6867312 4533 // we start transmitting the second TV ON code and advance to state
mjr 77:0b96f6867312 4534 // 2. Continue until we run out of TV ON IR codes, at which point
mjr 77:0b96f6867312 4535 // we advance to the next main psu2_state step.
mjr 77:0b96f6867312 4536 uint8_t tvon_ir_state = 0;
mjr 77:0b96f6867312 4537
mjr 77:0b96f6867312 4538 // TV ON switch relay control output pin
mjr 73:4e8ce0b18915 4539 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 4540
mjr 35:e959ffba78fd 4541 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 4542 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 4543 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 4544
mjr 73:4e8ce0b18915 4545 // Apply the current TV relay state
mjr 73:4e8ce0b18915 4546 void tvRelayUpdate(uint8_t bit, bool state)
mjr 73:4e8ce0b18915 4547 {
mjr 73:4e8ce0b18915 4548 // update the state
mjr 73:4e8ce0b18915 4549 if (state)
mjr 73:4e8ce0b18915 4550 tv_relay_state |= bit;
mjr 73:4e8ce0b18915 4551 else
mjr 73:4e8ce0b18915 4552 tv_relay_state &= ~bit;
mjr 73:4e8ce0b18915 4553
mjr 73:4e8ce0b18915 4554 // set the relay GPIO to the new state
mjr 73:4e8ce0b18915 4555 if (tv_relay != 0)
mjr 73:4e8ce0b18915 4556 tv_relay->write(tv_relay_state != 0);
mjr 73:4e8ce0b18915 4557 }
mjr 35:e959ffba78fd 4558
mjr 86:e30a1f60f783 4559 // Does the current power status allow a reboot? We shouldn't reboot
mjr 86:e30a1f60f783 4560 // in certain power states, because some states are purely internal:
mjr 86:e30a1f60f783 4561 // we can't get enough information from the external power sensor to
mjr 86:e30a1f60f783 4562 // return to the same state later. Code that performs discretionary
mjr 86:e30a1f60f783 4563 // reboots should always check here first, and delay any reboot until
mjr 86:e30a1f60f783 4564 // we say it's okay.
mjr 86:e30a1f60f783 4565 static inline bool powerStatusAllowsReboot()
mjr 86:e30a1f60f783 4566 {
mjr 86:e30a1f60f783 4567 // The only safe state for rebooting is state 1, idle/default.
mjr 86:e30a1f60f783 4568 // In other states, we can't reboot, because the external sensor
mjr 86:e30a1f60f783 4569 // and latch circuit doesn't give us enough information to return
mjr 86:e30a1f60f783 4570 // to the same state later.
mjr 86:e30a1f60f783 4571 return psu2_state == 1;
mjr 86:e30a1f60f783 4572 }
mjr 86:e30a1f60f783 4573
mjr 77:0b96f6867312 4574 // PSU2 Status update routine. The main loop calls this from time
mjr 77:0b96f6867312 4575 // to time to update the power sensing state and carry out TV ON
mjr 77:0b96f6867312 4576 // functions.
mjr 77:0b96f6867312 4577 Timer powerStatusTimer;
mjr 77:0b96f6867312 4578 uint32_t tv_delay_time_us;
mjr 77:0b96f6867312 4579 void powerStatusUpdate(Config &cfg)
mjr 33:d832bcab089e 4580 {
mjr 79:682ae3171a08 4581 // If the manual relay pulse timer is past the pulse time, end the
mjr 79:682ae3171a08 4582 // manual pulse. The timer only runs when a pulse is active, so
mjr 79:682ae3171a08 4583 // it'll never read as past the time limit if a pulse isn't on.
mjr 79:682ae3171a08 4584 if (tvRelayManualTimer.read_us() > 250000)
mjr 79:682ae3171a08 4585 {
mjr 79:682ae3171a08 4586 // turn off the relay and disable the timer
mjr 79:682ae3171a08 4587 tvRelayUpdate(TV_RELAY_USB, false);
mjr 79:682ae3171a08 4588 tvRelayManualTimer.stop();
mjr 79:682ae3171a08 4589 tvRelayManualTimer.reset();
mjr 79:682ae3171a08 4590 }
mjr 79:682ae3171a08 4591
mjr 77:0b96f6867312 4592 // Only update every 1/4 second or so. Note that if the PSU2
mjr 77:0b96f6867312 4593 // circuit isn't configured, the initialization routine won't
mjr 77:0b96f6867312 4594 // start the timer, so it'll always read zero and we'll always
mjr 77:0b96f6867312 4595 // skip this whole routine.
mjr 77:0b96f6867312 4596 if (powerStatusTimer.read_us() < 250000)
mjr 77:0b96f6867312 4597 return;
mjr 77:0b96f6867312 4598
mjr 77:0b96f6867312 4599 // reset the update timer for next time
mjr 77:0b96f6867312 4600 powerStatusTimer.reset();
mjr 77:0b96f6867312 4601
mjr 77:0b96f6867312 4602 // TV ON timer. We start this timer when we detect a change
mjr 77:0b96f6867312 4603 // in the PSU2 status from OFF to ON. When the timer reaches
mjr 77:0b96f6867312 4604 // the configured TV ON delay time, and the PSU2 power is still
mjr 77:0b96f6867312 4605 // on, we'll trigger the TV ON relay and send the TV ON IR codes.
mjr 35:e959ffba78fd 4606 static Timer tv_timer;
mjr 35:e959ffba78fd 4607
mjr 33:d832bcab089e 4608 // Check our internal state
mjr 33:d832bcab089e 4609 switch (psu2_state)
mjr 33:d832bcab089e 4610 {
mjr 33:d832bcab089e 4611 case 1:
mjr 33:d832bcab089e 4612 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 4613 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 4614 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 4615 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 4616 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 4617 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 4618 {
mjr 33:d832bcab089e 4619 // switch to OFF state
mjr 33:d832bcab089e 4620 psu2_state = 2;
mjr 33:d832bcab089e 4621
mjr 33:d832bcab089e 4622 // try setting the latch
mjr 35:e959ffba78fd 4623 psu2_status_set->write(1);
mjr 33:d832bcab089e 4624 }
mjr 77:0b96f6867312 4625 powerTimerDiagState = 0;
mjr 33:d832bcab089e 4626 break;
mjr 33:d832bcab089e 4627
mjr 33:d832bcab089e 4628 case 2:
mjr 33:d832bcab089e 4629 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 4630 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 4631 psu2_status_set->write(0);
mjr 33:d832bcab089e 4632 psu2_state = 3;
mjr 77:0b96f6867312 4633 powerTimerDiagState = 0;
mjr 33:d832bcab089e 4634 break;
mjr 33:d832bcab089e 4635
mjr 33:d832bcab089e 4636 case 3:
mjr 33:d832bcab089e 4637 // CHECK state: we pulsed SET, and we're now ready to see
mjr 40:cc0d9814522b 4638 // if it stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 4639 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 4640 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 4641 if (psu2_status_sense->read())
mjr 33:d832bcab089e 4642 {
mjr 33:d832bcab089e 4643 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 4644 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 4645 tv_timer.reset();
mjr 33:d832bcab089e 4646 tv_timer.start();
mjr 33:d832bcab089e 4647 psu2_state = 4;
mjr 73:4e8ce0b18915 4648
mjr 73:4e8ce0b18915 4649 // start the power timer diagnostic flashes
mjr 73:4e8ce0b18915 4650 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4651 }
mjr 33:d832bcab089e 4652 else
mjr 33:d832bcab089e 4653 {
mjr 33:d832bcab089e 4654 // The latch didn't stick, so PSU2 was still off at
mjr 87:8d35c74403af 4655 // our last check. Return to idle state.
mjr 87:8d35c74403af 4656 psu2_state = 1;
mjr 33:d832bcab089e 4657 }
mjr 33:d832bcab089e 4658 break;
mjr 33:d832bcab089e 4659
mjr 33:d832bcab089e 4660 case 4:
mjr 77:0b96f6867312 4661 // TV timer countdown in progress. The latch has to stay on during
mjr 77:0b96f6867312 4662 // the countdown; if the latch turns off, PSU2 power must have gone
mjr 77:0b96f6867312 4663 // off again before the countdown finished.
mjr 77:0b96f6867312 4664 if (!psu2_status_sense->read())
mjr 77:0b96f6867312 4665 {
mjr 77:0b96f6867312 4666 // power is off - start a new check cycle
mjr 77:0b96f6867312 4667 psu2_status_set->write(1);
mjr 77:0b96f6867312 4668 psu2_state = 2;
mjr 77:0b96f6867312 4669 break;
mjr 77:0b96f6867312 4670 }
mjr 77:0b96f6867312 4671
mjr 77:0b96f6867312 4672 // Flash the power time diagnostic every two cycles
mjr 77:0b96f6867312 4673 powerTimerDiagState = (powerTimerDiagState + 1) & 0x03;
mjr 77:0b96f6867312 4674
mjr 77:0b96f6867312 4675 // if we've reached the delay time, pulse the relay
mjr 77:0b96f6867312 4676 if (tv_timer.read_us() >= tv_delay_time_us)
mjr 33:d832bcab089e 4677 {
mjr 33:d832bcab089e 4678 // turn on the relay for one timer interval
mjr 73:4e8ce0b18915 4679 tvRelayUpdate(TV_RELAY_POWERON, true);
mjr 33:d832bcab089e 4680 psu2_state = 5;
mjr 77:0b96f6867312 4681
mjr 77:0b96f6867312 4682 // show solid blue on the diagnostic LED while the relay is on
mjr 77:0b96f6867312 4683 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4684 }
mjr 33:d832bcab089e 4685 break;
mjr 33:d832bcab089e 4686
mjr 33:d832bcab089e 4687 case 5:
mjr 33:d832bcab089e 4688 // TV timer relay on. We pulse this for one interval, so
mjr 77:0b96f6867312 4689 // it's now time to turn it off.
mjr 73:4e8ce0b18915 4690 tvRelayUpdate(TV_RELAY_POWERON, false);
mjr 77:0b96f6867312 4691
mjr 77:0b96f6867312 4692 // Proceed to sending any TV ON IR commands
mjr 77:0b96f6867312 4693 psu2_state = 6;
mjr 77:0b96f6867312 4694 tvon_ir_state = 0;
mjr 77:0b96f6867312 4695
mjr 77:0b96f6867312 4696 // diagnostic LEDs off for now
mjr 77:0b96f6867312 4697 powerTimerDiagState = 0;
mjr 77:0b96f6867312 4698 break;
mjr 77:0b96f6867312 4699
mjr 77:0b96f6867312 4700 case 6:
mjr 77:0b96f6867312 4701 // Sending TV ON IR signals. Start with the assumption that
mjr 77:0b96f6867312 4702 // we have no IR work to do, in which case we're done with the
mjr 77:0b96f6867312 4703 // whole TV ON sequence. So by default return to state 1.
mjr 33:d832bcab089e 4704 psu2_state = 1;
mjr 77:0b96f6867312 4705 powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 4706
mjr 77:0b96f6867312 4707 // If we have an IR emitter, check for TV ON IR commands
mjr 77:0b96f6867312 4708 if (ir_tx != 0)
mjr 77:0b96f6867312 4709 {
mjr 77:0b96f6867312 4710 // check to see if the last transmission is still in progress
mjr 77:0b96f6867312 4711 if (ir_tx->isSending())
mjr 77:0b96f6867312 4712 {
mjr 77:0b96f6867312 4713 // We're still sending the last transmission. Stay in
mjr 77:0b96f6867312 4714 // state 6.
mjr 77:0b96f6867312 4715 psu2_state = 6;
mjr 77:0b96f6867312 4716 powerTimerDiagState = 4;
mjr 77:0b96f6867312 4717 break;
mjr 77:0b96f6867312 4718 }
mjr 77:0b96f6867312 4719
mjr 77:0b96f6867312 4720 // The last transmission is done, so check for a new one.
mjr 77:0b96f6867312 4721 // Look for the Nth TV ON IR slot, where N is our state
mjr 77:0b96f6867312 4722 // number.
mjr 77:0b96f6867312 4723 for (int i = 0, n = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 4724 {
mjr 77:0b96f6867312 4725 // is this a TV ON command?
mjr 77:0b96f6867312 4726 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 4727 {
mjr 77:0b96f6867312 4728 // It's a TV ON command - check if it's the one we're
mjr 77:0b96f6867312 4729 // looking for.
mjr 77:0b96f6867312 4730 if (n == tvon_ir_state)
mjr 77:0b96f6867312 4731 {
mjr 77:0b96f6867312 4732 // It's the one. Start transmitting it by
mjr 77:0b96f6867312 4733 // pushing its virtual button.
mjr 77:0b96f6867312 4734 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 4735 ir_tx->pushButton(vb, true);
mjr 77:0b96f6867312 4736
mjr 77:0b96f6867312 4737 // Pushing the button starts transmission, and once
mjr 88:98bce687e6c0 4738 // started, the transmission runs to completion even
mjr 88:98bce687e6c0 4739 // if the button is no longer pushed. So we can
mjr 88:98bce687e6c0 4740 // immediately un-push the button, since we only need
mjr 88:98bce687e6c0 4741 // to send the code once.
mjr 77:0b96f6867312 4742 ir_tx->pushButton(vb, false);
mjr 77:0b96f6867312 4743
mjr 77:0b96f6867312 4744 // Advance to the next TV ON IR state, where we'll
mjr 77:0b96f6867312 4745 // await the end of this transmission and move on to
mjr 77:0b96f6867312 4746 // the next one.
mjr 77:0b96f6867312 4747 psu2_state = 6;
mjr 77:0b96f6867312 4748 tvon_ir_state++;
mjr 77:0b96f6867312 4749 break;
mjr 77:0b96f6867312 4750 }
mjr 77:0b96f6867312 4751
mjr 77:0b96f6867312 4752 // it's not ours - count it and keep looking
mjr 77:0b96f6867312 4753 ++n;
mjr 77:0b96f6867312 4754 }
mjr 77:0b96f6867312 4755 }
mjr 77:0b96f6867312 4756 }
mjr 33:d832bcab089e 4757 break;
mjr 33:d832bcab089e 4758 }
mjr 77:0b96f6867312 4759
mjr 77:0b96f6867312 4760 // update the diagnostic LEDs
mjr 77:0b96f6867312 4761 diagLED();
mjr 33:d832bcab089e 4762 }
mjr 33:d832bcab089e 4763
mjr 77:0b96f6867312 4764 // Start the power status timer. If the status sense circuit is enabled
mjr 77:0b96f6867312 4765 // in the configuration, we'll set up the pin connections and start the
mjr 77:0b96f6867312 4766 // timer for our periodic status checks. Does nothing if any of the pins
mjr 77:0b96f6867312 4767 // are configured as NC.
mjr 77:0b96f6867312 4768 void startPowerStatusTimer(Config &cfg)
mjr 35:e959ffba78fd 4769 {
mjr 55:4db125cd11a0 4770 // only start the timer if the pins are configured and the delay
mjr 55:4db125cd11a0 4771 // time is nonzero
mjr 77:0b96f6867312 4772 powerStatusTimer.reset();
mjr 77:0b96f6867312 4773 if (cfg.TVON.statusPin != 0xFF
mjr 77:0b96f6867312 4774 && cfg.TVON.latchPin != 0xFF)
mjr 35:e959ffba78fd 4775 {
mjr 77:0b96f6867312 4776 // set up the power sensing circuit connections
mjr 53:9b2611964afc 4777 psu2_status_sense = new DigitalIn(wirePinName(cfg.TVON.statusPin));
mjr 53:9b2611964afc 4778 psu2_status_set = new DigitalOut(wirePinName(cfg.TVON.latchPin));
mjr 77:0b96f6867312 4779
mjr 77:0b96f6867312 4780 // if there's a TV ON relay, set up its control pin
mjr 77:0b96f6867312 4781 if (cfg.TVON.relayPin != 0xFF)
mjr 77:0b96f6867312 4782 tv_relay = new DigitalOut(wirePinName(cfg.TVON.relayPin));
mjr 77:0b96f6867312 4783
mjr 77:0b96f6867312 4784 // Set the TV ON delay time. We store the time internally in
mjr 77:0b96f6867312 4785 // microseconds, but the configuration stores it in units of
mjr 77:0b96f6867312 4786 // 1/100 second = 10ms = 10000us.
mjr 77:0b96f6867312 4787 tv_delay_time_us = cfg.TVON.delayTime * 10000;;
mjr 77:0b96f6867312 4788
mjr 77:0b96f6867312 4789 // Start the TV timer
mjr 77:0b96f6867312 4790 powerStatusTimer.start();
mjr 35:e959ffba78fd 4791 }
mjr 35:e959ffba78fd 4792 }
mjr 35:e959ffba78fd 4793
mjr 73:4e8ce0b18915 4794 // Operate the TV ON relay. This allows manual control of the relay
mjr 73:4e8ce0b18915 4795 // from the PC. See protocol message 65 submessage 11.
mjr 73:4e8ce0b18915 4796 //
mjr 73:4e8ce0b18915 4797 // Mode:
mjr 73:4e8ce0b18915 4798 // 0 = turn relay off
mjr 73:4e8ce0b18915 4799 // 1 = turn relay on
mjr 73:4e8ce0b18915 4800 // 2 = pulse relay
mjr 73:4e8ce0b18915 4801 void TVRelay(int mode)
mjr 73:4e8ce0b18915 4802 {
mjr 73:4e8ce0b18915 4803 // if there's no TV relay control pin, ignore this
mjr 73:4e8ce0b18915 4804 if (tv_relay == 0)
mjr 73:4e8ce0b18915 4805 return;
mjr 73:4e8ce0b18915 4806
mjr 73:4e8ce0b18915 4807 switch (mode)
mjr 73:4e8ce0b18915 4808 {
mjr 73:4e8ce0b18915 4809 case 0:
mjr 73:4e8ce0b18915 4810 // relay off
mjr 73:4e8ce0b18915 4811 tvRelayUpdate(TV_RELAY_USB, false);
mjr 73:4e8ce0b18915 4812 break;
mjr 73:4e8ce0b18915 4813
mjr 73:4e8ce0b18915 4814 case 1:
mjr 73:4e8ce0b18915 4815 // relay on
mjr 73:4e8ce0b18915 4816 tvRelayUpdate(TV_RELAY_USB, true);
mjr 73:4e8ce0b18915 4817 break;
mjr 73:4e8ce0b18915 4818
mjr 73:4e8ce0b18915 4819 case 2:
mjr 79:682ae3171a08 4820 // Turn the relay on and reset the manual TV pulse timer
mjr 73:4e8ce0b18915 4821 tvRelayUpdate(TV_RELAY_USB, true);
mjr 79:682ae3171a08 4822 tvRelayManualTimer.reset();
mjr 79:682ae3171a08 4823 tvRelayManualTimer.start();
mjr 73:4e8ce0b18915 4824 break;
mjr 73:4e8ce0b18915 4825 }
mjr 73:4e8ce0b18915 4826 }
mjr 73:4e8ce0b18915 4827
mjr 73:4e8ce0b18915 4828
mjr 35:e959ffba78fd 4829 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4830 //
mjr 35:e959ffba78fd 4831 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 4832 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 4833 //
mjr 35:e959ffba78fd 4834 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 4835 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 4836 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 4837 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 4838 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 4839 // again each time the firmware is updated.
mjr 35:e959ffba78fd 4840 //
mjr 35:e959ffba78fd 4841 NVM nvm;
mjr 35:e959ffba78fd 4842
mjr 86:e30a1f60f783 4843 // Save Config followup time, in seconds. After a successful save,
mjr 86:e30a1f60f783 4844 // we leave the success flag on in the status for this interval. At
mjr 86:e30a1f60f783 4845 // the end of the interval, we reboot the device if requested.
mjr 86:e30a1f60f783 4846 uint8_t saveConfigFollowupTime;
mjr 86:e30a1f60f783 4847
mjr 86:e30a1f60f783 4848 // is a reboot pending at the end of the config save followup interval?
mjr 86:e30a1f60f783 4849 uint8_t saveConfigRebootPending;
mjr 77:0b96f6867312 4850
mjr 79:682ae3171a08 4851 // status flag for successful config save - set to 0x40 on success
mjr 79:682ae3171a08 4852 uint8_t saveConfigSucceededFlag;
mjr 79:682ae3171a08 4853
mjr 86:e30a1f60f783 4854 // Timer for configuration change followup timer
mjr 86:e30a1f60f783 4855 ExtTimer saveConfigFollowupTimer;
mjr 86:e30a1f60f783 4856
mjr 86:e30a1f60f783 4857
mjr 35:e959ffba78fd 4858 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 4859 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 4860
mjr 35:e959ffba78fd 4861 // flash memory controller interface
mjr 35:e959ffba78fd 4862 FreescaleIAP iap;
mjr 35:e959ffba78fd 4863
mjr 79:682ae3171a08 4864 // figure the flash address for the config data
mjr 79:682ae3171a08 4865 const NVM *configFlashAddr()
mjr 76:7f5912b6340e 4866 {
mjr 79:682ae3171a08 4867 // figure the number of sectors we need, rounding up
mjr 79:682ae3171a08 4868 int nSectors = (sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE;
mjr 79:682ae3171a08 4869
mjr 79:682ae3171a08 4870 // figure the total size required from the number of sectors
mjr 79:682ae3171a08 4871 int reservedSize = nSectors * SECTOR_SIZE;
mjr 79:682ae3171a08 4872
mjr 79:682ae3171a08 4873 // locate it at the top of memory
mjr 79:682ae3171a08 4874 uint32_t addr = iap.flashSize() - reservedSize;
mjr 79:682ae3171a08 4875
mjr 79:682ae3171a08 4876 // return it as a read-only NVM pointer
mjr 79:682ae3171a08 4877 return (const NVM *)addr;
mjr 35:e959ffba78fd 4878 }
mjr 35:e959ffba78fd 4879
mjr 76:7f5912b6340e 4880 // Load the config from flash. Returns true if a valid non-default
mjr 76:7f5912b6340e 4881 // configuration was loaded, false if we not. If we return false,
mjr 76:7f5912b6340e 4882 // we load the factory defaults, so the configuration object is valid
mjr 76:7f5912b6340e 4883 // in either case.
mjr 76:7f5912b6340e 4884 bool loadConfigFromFlash()
mjr 35:e959ffba78fd 4885 {
mjr 35:e959ffba78fd 4886 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 4887 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 4888 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 4889 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 4890 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 4891 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 4892 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 4893 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 4894 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 4895 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 4896 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 4897 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 4898 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 4899 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 4900 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 4901 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 4902
mjr 35:e959ffba78fd 4903 // Figure how many sectors we need for our structure
mjr 79:682ae3171a08 4904 const NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 4905
mjr 35:e959ffba78fd 4906 // if the flash is valid, load it; otherwise initialize to defaults
mjr 76:7f5912b6340e 4907 bool nvm_valid = flash->valid();
mjr 76:7f5912b6340e 4908 if (nvm_valid)
mjr 35:e959ffba78fd 4909 {
mjr 35:e959ffba78fd 4910 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 4911 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 4912 }
mjr 35:e959ffba78fd 4913 else
mjr 35:e959ffba78fd 4914 {
mjr 76:7f5912b6340e 4915 // flash is invalid - load factory settings into RAM structure
mjr 35:e959ffba78fd 4916 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 4917 }
mjr 76:7f5912b6340e 4918
mjr 76:7f5912b6340e 4919 // tell the caller what happened
mjr 76:7f5912b6340e 4920 return nvm_valid;
mjr 35:e959ffba78fd 4921 }
mjr 35:e959ffba78fd 4922
mjr 86:e30a1f60f783 4923 // Save the config. Returns true on success, false on failure.
mjr 86:e30a1f60f783 4924 // 'tFollowup' is the follow-up time in seconds. If the write is
mjr 86:e30a1f60f783 4925 // successful, we'll turn on the success flag in the status reports
mjr 86:e30a1f60f783 4926 // and leave it on for this interval. If 'reboot' is true, we'll
mjr 86:e30a1f60f783 4927 // also schedule a reboot at the end of the followup interval.
mjr 86:e30a1f60f783 4928 bool saveConfigToFlash(int tFollowup, bool reboot)
mjr 33:d832bcab089e 4929 {
mjr 76:7f5912b6340e 4930 // make sure the plunger sensor isn't busy
mjr 76:7f5912b6340e 4931 waitPlungerIdle();
mjr 76:7f5912b6340e 4932
mjr 76:7f5912b6340e 4933 // get the config block location in the flash memory
mjr 77:0b96f6867312 4934 uint32_t addr = uint32_t(configFlashAddr());
mjr 79:682ae3171a08 4935
mjr 79:682ae3171a08 4936 // save the data
mjr 86:e30a1f60f783 4937 if (nvm.save(iap, addr))
mjr 86:e30a1f60f783 4938 {
mjr 86:e30a1f60f783 4939 // success - report the successful save in the status flags
mjr 86:e30a1f60f783 4940 saveConfigSucceededFlag = 0x40;
mjr 86:e30a1f60f783 4941
mjr 86:e30a1f60f783 4942 // start the followup timer
mjr 87:8d35c74403af 4943 saveConfigFollowupTime = tFollowup;
mjr 87:8d35c74403af 4944 saveConfigFollowupTimer.reset();
mjr 86:e30a1f60f783 4945 saveConfigFollowupTimer.start();
mjr 86:e30a1f60f783 4946
mjr 86:e30a1f60f783 4947 // if a reboot is pending, flag it
mjr 86:e30a1f60f783 4948 saveConfigRebootPending = reboot;
mjr 86:e30a1f60f783 4949
mjr 86:e30a1f60f783 4950 // return success
mjr 86:e30a1f60f783 4951 return true;
mjr 86:e30a1f60f783 4952 }
mjr 86:e30a1f60f783 4953 else
mjr 86:e30a1f60f783 4954 {
mjr 86:e30a1f60f783 4955 // return failure
mjr 86:e30a1f60f783 4956 return false;
mjr 86:e30a1f60f783 4957 }
mjr 76:7f5912b6340e 4958 }
mjr 76:7f5912b6340e 4959
mjr 76:7f5912b6340e 4960 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 4961 //
mjr 76:7f5912b6340e 4962 // Host-loaded configuration. The Flash NVM block above is designed to be
mjr 76:7f5912b6340e 4963 // stored from within the firmware; in contrast, the host-loaded config is
mjr 76:7f5912b6340e 4964 // stored by the host, by patching the firwmare binary (.bin) file before
mjr 76:7f5912b6340e 4965 // downloading it to the device.
mjr 76:7f5912b6340e 4966 //
mjr 76:7f5912b6340e 4967 // Ideally, we'd use the host-loaded memory for all configuration updates,
mjr 76:7f5912b6340e 4968 // because the KL25Z doesn't seem to be 100% reliable writing flash itself.
mjr 76:7f5912b6340e 4969 // There seems to be a chance of memory bus contention while a write is in
mjr 76:7f5912b6340e 4970 // progress, which can either corrupt the write or cause the CPU to lock up
mjr 76:7f5912b6340e 4971 // before the write is completed. It seems more reliable to program the
mjr 76:7f5912b6340e 4972 // flash externally, via the OpenSDA connection. Unfortunately, none of
mjr 76:7f5912b6340e 4973 // the available OpenSDA versions are capable of programming specific flash
mjr 76:7f5912b6340e 4974 // sectors; they always erase the entire flash memory space. We *could*
mjr 76:7f5912b6340e 4975 // make the Windows config program simply re-download the entire firmware
mjr 76:7f5912b6340e 4976 // for every configuration update, but I'd rather not because of the extra
mjr 76:7f5912b6340e 4977 // wear this would put on the flash. So, as a compromise, we'll use the
mjr 76:7f5912b6340e 4978 // host-loaded config whenever the user explicitly updates the firmware,
mjr 76:7f5912b6340e 4979 // but we'll use the on-board writer when only making a config change.
mjr 76:7f5912b6340e 4980 //
mjr 76:7f5912b6340e 4981 // The memory here is stored using the same format as the USB "Set Config
mjr 76:7f5912b6340e 4982 // Variable" command. These messages are 8 bytes long and start with a
mjr 76:7f5912b6340e 4983 // byte value 66, followed by the variable ID, followed by the variable
mjr 76:7f5912b6340e 4984 // value data in a format defined separately for each variable. To load
mjr 76:7f5912b6340e 4985 // the data, we'll start at the first byte after the signature, and
mjr 76:7f5912b6340e 4986 // interpret each 8-byte block as a type 66 message. If the first byte
mjr 76:7f5912b6340e 4987 // of a block is not 66, we'll take it as the end of the data.
mjr 76:7f5912b6340e 4988 //
mjr 76:7f5912b6340e 4989 // We provide a block of storage here big enough for 1,024 variables.
mjr 76:7f5912b6340e 4990 // The header consists of a 30-byte signature followed by two bytes giving
mjr 76:7f5912b6340e 4991 // the available space in the area, in this case 8192 == 0x0200. The
mjr 76:7f5912b6340e 4992 // length is little-endian. Note that the linker will implicitly zero
mjr 76:7f5912b6340e 4993 // the rest of the block, so if the host doesn't populate it, we'll see
mjr 76:7f5912b6340e 4994 // that it's empty by virtue of not containing the required '66' byte
mjr 76:7f5912b6340e 4995 // prefix for the first 8-byte variable block.
mjr 76:7f5912b6340e 4996 static const uint8_t hostLoadedConfig[8192+32]
mjr 76:7f5912b6340e 4997 __attribute__ ((aligned(SECTOR_SIZE))) =
mjr 76:7f5912b6340e 4998 "///Pinscape.HostLoadedConfig//\0\040"; // 30 byte signature + 2 byte length
mjr 76:7f5912b6340e 4999
mjr 76:7f5912b6340e 5000 // Get a pointer to the first byte of the configuration data
mjr 76:7f5912b6340e 5001 const uint8_t *getHostLoadedConfigData()
mjr 76:7f5912b6340e 5002 {
mjr 76:7f5912b6340e 5003 // the first configuration variable byte immediately follows the
mjr 76:7f5912b6340e 5004 // 32-byte signature header
mjr 76:7f5912b6340e 5005 return hostLoadedConfig + 32;
mjr 76:7f5912b6340e 5006 };
mjr 76:7f5912b6340e 5007
mjr 76:7f5912b6340e 5008 // forward reference to config var store function
mjr 76:7f5912b6340e 5009 void configVarSet(const uint8_t *);
mjr 76:7f5912b6340e 5010
mjr 76:7f5912b6340e 5011 // Load the host-loaded configuration data into the active (RAM)
mjr 76:7f5912b6340e 5012 // configuration object.
mjr 76:7f5912b6340e 5013 void loadHostLoadedConfig()
mjr 76:7f5912b6340e 5014 {
mjr 76:7f5912b6340e 5015 // Start at the first configuration variable. Each variable
mjr 76:7f5912b6340e 5016 // block is in the format of a Set Config Variable command in
mjr 76:7f5912b6340e 5017 // the USB protocol, so each block starts with a byte value of
mjr 76:7f5912b6340e 5018 // 66 and is 8 bytes long. Continue as long as we find valid
mjr 76:7f5912b6340e 5019 // variable blocks, or reach end end of the block.
mjr 76:7f5912b6340e 5020 const uint8_t *start = getHostLoadedConfigData();
mjr 76:7f5912b6340e 5021 const uint8_t *end = hostLoadedConfig + sizeof(hostLoadedConfig);
mjr 76:7f5912b6340e 5022 for (const uint8_t *p = getHostLoadedConfigData() ; start < end && *p == 66 ; p += 8)
mjr 76:7f5912b6340e 5023 {
mjr 76:7f5912b6340e 5024 // load this variable
mjr 76:7f5912b6340e 5025 configVarSet(p);
mjr 76:7f5912b6340e 5026 }
mjr 35:e959ffba78fd 5027 }
mjr 35:e959ffba78fd 5028
mjr 35:e959ffba78fd 5029 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5030 //
mjr 55:4db125cd11a0 5031 // Pixel dump mode - the host requested a dump of image sensor pixels
mjr 55:4db125cd11a0 5032 // (helpful for installing and setting up the sensor and light source)
mjr 55:4db125cd11a0 5033 //
mjr 55:4db125cd11a0 5034 bool reportPlungerStat = false;
mjr 55:4db125cd11a0 5035 uint8_t reportPlungerStatFlags; // plunger pixel report flag bits (see ccdSensor.h)
mjr 55:4db125cd11a0 5036 uint8_t reportPlungerStatTime; // extra exposure time for plunger pixel report
mjr 55:4db125cd11a0 5037
mjr 55:4db125cd11a0 5038
mjr 55:4db125cd11a0 5039
mjr 55:4db125cd11a0 5040 // ---------------------------------------------------------------------------
mjr 55:4db125cd11a0 5041 //
mjr 40:cc0d9814522b 5042 // Night mode setting updates
mjr 40:cc0d9814522b 5043 //
mjr 38:091e511ce8a0 5044
mjr 38:091e511ce8a0 5045 // Turn night mode on or off
mjr 38:091e511ce8a0 5046 static void setNightMode(bool on)
mjr 38:091e511ce8a0 5047 {
mjr 77:0b96f6867312 5048 // Set the new night mode flag in the noisy output class. Note
mjr 77:0b96f6867312 5049 // that we use the status report bit flag value 0x02 when on, so
mjr 77:0b96f6867312 5050 // that we can just '|' this into the overall status bits.
mjr 77:0b96f6867312 5051 nightMode = on ? 0x02 : 0x00;
mjr 55:4db125cd11a0 5052
mjr 40:cc0d9814522b 5053 // update the special output pin that shows the night mode state
mjr 53:9b2611964afc 5054 int port = int(cfg.nightMode.port) - 1;
mjr 53:9b2611964afc 5055 if (port >= 0 && port < numOutputs)
mjr 53:9b2611964afc 5056 lwPin[port]->set(nightMode ? 255 : 0);
mjr 76:7f5912b6340e 5057
mjr 76:7f5912b6340e 5058 // Reset all outputs at their current value, so that the underlying
mjr 76:7f5912b6340e 5059 // physical outputs get turned on or off as appropriate for the night
mjr 76:7f5912b6340e 5060 // mode change.
mjr 76:7f5912b6340e 5061 for (int i = 0 ; i < numOutputs ; ++i)
mjr 76:7f5912b6340e 5062 lwPin[i]->set(outLevel[i]);
mjr 76:7f5912b6340e 5063
mjr 76:7f5912b6340e 5064 // update 74HC595 outputs
mjr 76:7f5912b6340e 5065 if (hc595 != 0)
mjr 76:7f5912b6340e 5066 hc595->update();
mjr 38:091e511ce8a0 5067 }
mjr 38:091e511ce8a0 5068
mjr 38:091e511ce8a0 5069 // Toggle night mode
mjr 38:091e511ce8a0 5070 static void toggleNightMode()
mjr 38:091e511ce8a0 5071 {
mjr 53:9b2611964afc 5072 setNightMode(!nightMode);
mjr 38:091e511ce8a0 5073 }
mjr 38:091e511ce8a0 5074
mjr 38:091e511ce8a0 5075
mjr 38:091e511ce8a0 5076 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 5077 //
mjr 35:e959ffba78fd 5078 // Plunger Sensor
mjr 35:e959ffba78fd 5079 //
mjr 35:e959ffba78fd 5080
mjr 35:e959ffba78fd 5081 // the plunger sensor interface object
mjr 35:e959ffba78fd 5082 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 5083
mjr 87:8d35c74403af 5084 // wait for the plunger sensor to complete any outstanding DMA transfer
mjr 76:7f5912b6340e 5085 static void waitPlungerIdle(void)
mjr 76:7f5912b6340e 5086 {
mjr 87:8d35c74403af 5087 while (plungerSensor->dmaBusy()) { }
mjr 76:7f5912b6340e 5088 }
mjr 76:7f5912b6340e 5089
mjr 35:e959ffba78fd 5090 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 5091 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 5092 void createPlunger()
mjr 35:e959ffba78fd 5093 {
mjr 35:e959ffba78fd 5094 // create the new sensor object according to the type
mjr 35:e959ffba78fd 5095 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 5096 {
mjr 82:4f6209cb5c33 5097 case PlungerType_TSL1410R:
mjr 82:4f6209cb5c33 5098 // TSL1410R, shadow edge detector
mjr 35:e959ffba78fd 5099 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 5100 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 5101 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 5102 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5103 wirePinName(cfg.plunger.sensorPin[2]));
mjr 35:e959ffba78fd 5104 break;
mjr 35:e959ffba78fd 5105
mjr 82:4f6209cb5c33 5106 case PlungerType_TSL1412S:
mjr 82:4f6209cb5c33 5107 // TSL1412S, shadow edge detector
mjr 82:4f6209cb5c33 5108 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 5109 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 5110 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 5111 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5112 wirePinName(cfg.plunger.sensorPin[2]));
mjr 35:e959ffba78fd 5113 break;
mjr 35:e959ffba78fd 5114
mjr 35:e959ffba78fd 5115 case PlungerType_Pot:
mjr 82:4f6209cb5c33 5116 // Potentiometer (or any other sensor with a linear analog voltage
mjr 82:4f6209cb5c33 5117 // reading as the proxy for the position)
mjr 82:4f6209cb5c33 5118 // pins are: AO (analog in)
mjr 53:9b2611964afc 5119 plungerSensor = new PlungerSensorPot(
mjr 53:9b2611964afc 5120 wirePinName(cfg.plunger.sensorPin[0]));
mjr 35:e959ffba78fd 5121 break;
mjr 82:4f6209cb5c33 5122
mjr 82:4f6209cb5c33 5123 case PlungerType_OptQuad:
mjr 82:4f6209cb5c33 5124 // Optical quadrature sensor, AEDR8300-K or similar. The -K is
mjr 82:4f6209cb5c33 5125 // designed for a 75 LPI scale, which translates to 300 pulses/inch.
mjr 82:4f6209cb5c33 5126 // Pins are: CHA, CHB (quadrature pulse inputs).
mjr 82:4f6209cb5c33 5127 plungerSensor = new PlungerSensorQuad(
mjr 82:4f6209cb5c33 5128 300,
mjr 82:4f6209cb5c33 5129 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5130 wirePinName(cfg.plunger.sensorPin[1]));
mjr 82:4f6209cb5c33 5131 break;
mjr 82:4f6209cb5c33 5132
mjr 82:4f6209cb5c33 5133 case PlungerType_TSL1401CL:
mjr 82:4f6209cb5c33 5134 // TSL1401CL, absolute position encoder with bar code scale
mjr 82:4f6209cb5c33 5135 // pins are: SI, CLOCK, AO
mjr 82:4f6209cb5c33 5136 plungerSensor = new PlungerSensorTSL1401CL(
mjr 82:4f6209cb5c33 5137 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5138 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5139 wirePinName(cfg.plunger.sensorPin[2]));
mjr 82:4f6209cb5c33 5140 break;
mjr 82:4f6209cb5c33 5141
mjr 82:4f6209cb5c33 5142 case PlungerType_VL6180X:
mjr 82:4f6209cb5c33 5143 // VL6180X time-of-flight IR distance sensor
mjr 82:4f6209cb5c33 5144 // pins are: SDL, SCL, GPIO0/CE
mjr 82:4f6209cb5c33 5145 plungerSensor = new PlungerSensorVL6180X(
mjr 82:4f6209cb5c33 5146 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5147 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5148 wirePinName(cfg.plunger.sensorPin[2]));
mjr 82:4f6209cb5c33 5149 break;
mjr 82:4f6209cb5c33 5150
mjr 35:e959ffba78fd 5151 case PlungerType_None:
mjr 35:e959ffba78fd 5152 default:
mjr 35:e959ffba78fd 5153 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 5154 break;
mjr 35:e959ffba78fd 5155 }
mjr 86:e30a1f60f783 5156
mjr 87:8d35c74403af 5157 // initialize the config variables affecting the plunger
mjr 87:8d35c74403af 5158 plungerSensor->onConfigChange(19, cfg);
mjr 87:8d35c74403af 5159 plungerSensor->onConfigChange(20, cfg);
mjr 33:d832bcab089e 5160 }
mjr 33:d832bcab089e 5161
mjr 52:8298b2a73eb2 5162 // Global plunger calibration mode flag
mjr 52:8298b2a73eb2 5163 bool plungerCalMode;
mjr 52:8298b2a73eb2 5164
mjr 48:058ace2aed1d 5165 // Plunger reader
mjr 51:57eb311faafa 5166 //
mjr 51:57eb311faafa 5167 // This class encapsulates our plunger data processing. At the simplest
mjr 51:57eb311faafa 5168 // level, we read the position from the sensor, adjust it for the
mjr 51:57eb311faafa 5169 // calibration settings, and report the calibrated position to the host.
mjr 51:57eb311faafa 5170 //
mjr 51:57eb311faafa 5171 // In addition, we constantly monitor the data for "firing" motions.
mjr 51:57eb311faafa 5172 // A firing motion is when the user pulls back the plunger and releases
mjr 51:57eb311faafa 5173 // it, allowing it to shoot forward under the force of the main spring.
mjr 51:57eb311faafa 5174 // When we detect that this is happening, we briefly stop reporting the
mjr 51:57eb311faafa 5175 // real physical position that we're reading from the sensor, and instead
mjr 51:57eb311faafa 5176 // report a synthetic series of positions that depicts an idealized
mjr 51:57eb311faafa 5177 // firing motion.
mjr 51:57eb311faafa 5178 //
mjr 51:57eb311faafa 5179 // The point of the synthetic reports is to correct for distortions
mjr 51:57eb311faafa 5180 // created by the joystick interface conventions used by VP and other
mjr 51:57eb311faafa 5181 // PC pinball emulators. The convention they use is simply to have the
mjr 51:57eb311faafa 5182 // plunger device report the instantaneous position of the real plunger.
mjr 51:57eb311faafa 5183 // The PC software polls this reported position periodically, and moves
mjr 51:57eb311faafa 5184 // the on-screen virtual plunger in sync with the real plunger. This
mjr 51:57eb311faafa 5185 // works fine for human-scale motion when the user is manually moving
mjr 51:57eb311faafa 5186 // the plunger. But it doesn't work for the high speed motion of a
mjr 51:57eb311faafa 5187 // release. The plunger simply moves too fast. VP polls in about 10ms
mjr 51:57eb311faafa 5188 // intervals; the plunger takes about 50ms to travel from fully
mjr 51:57eb311faafa 5189 // retracted to the park position when released. The low sampling
mjr 51:57eb311faafa 5190 // rate relative to the rate of change of the sampled data creates
mjr 51:57eb311faafa 5191 // a classic digital aliasing effect.
mjr 51:57eb311faafa 5192 //
mjr 51:57eb311faafa 5193 // The synthetic reporting scheme compensates for the interface
mjr 51:57eb311faafa 5194 // distortions by essentially changing to a coarse enough timescale
mjr 51:57eb311faafa 5195 // that VP can reliably interpret the readings. Conceptually, there
mjr 51:57eb311faafa 5196 // are three steps involved in doing this. First, we analyze the
mjr 51:57eb311faafa 5197 // actual sensor data to detect and characterize the release motion.
mjr 51:57eb311faafa 5198 // Second, once we think we have a release in progress, we fit the
mjr 51:57eb311faafa 5199 // data to a mathematical model of the release. The model we use is
mjr 51:57eb311faafa 5200 // dead simple: we consider the release to have one parameter, namely
mjr 51:57eb311faafa 5201 // the retraction distance at the moment the user lets go. This is an
mjr 51:57eb311faafa 5202 // excellent proxy in the real physical system for the final speed
mjr 51:57eb311faafa 5203 // when the plunger hits the ball, and it also happens to match how
mjr 51:57eb311faafa 5204 // VP models it internally. Third, we construct synthetic reports
mjr 51:57eb311faafa 5205 // that will make VP's internal state match our model. This is also
mjr 51:57eb311faafa 5206 // pretty simple: we just need to send VP the maximum retraction
mjr 51:57eb311faafa 5207 // distance for long enough to be sure that it polls it at least
mjr 51:57eb311faafa 5208 // once, and then send it the park position for long enough to
mjr 51:57eb311faafa 5209 // ensure that VP will complete the same firing motion. The
mjr 51:57eb311faafa 5210 // immediate jump from the maximum point to the zero point will
mjr 51:57eb311faafa 5211 // cause VP to move its simulation model plunger forward from the
mjr 51:57eb311faafa 5212 // starting point at its natural spring acceleration rate, which
mjr 51:57eb311faafa 5213 // is exactly what the real plunger just did.
mjr 51:57eb311faafa 5214 //
mjr 48:058ace2aed1d 5215 class PlungerReader
mjr 48:058ace2aed1d 5216 {
mjr 48:058ace2aed1d 5217 public:
mjr 48:058ace2aed1d 5218 PlungerReader()
mjr 48:058ace2aed1d 5219 {
mjr 48:058ace2aed1d 5220 // not in a firing event yet
mjr 48:058ace2aed1d 5221 firing = 0;
mjr 48:058ace2aed1d 5222 }
mjr 76:7f5912b6340e 5223
mjr 48:058ace2aed1d 5224 // Collect a reading from the plunger sensor. The main loop calls
mjr 48:058ace2aed1d 5225 // this frequently to read the current raw position data from the
mjr 48:058ace2aed1d 5226 // sensor. We analyze the raw data to produce the calibrated
mjr 48:058ace2aed1d 5227 // position that we report to the PC via the joystick interface.
mjr 48:058ace2aed1d 5228 void read()
mjr 48:058ace2aed1d 5229 {
mjr 76:7f5912b6340e 5230 // if the sensor is busy, skip the reading on this round
mjr 76:7f5912b6340e 5231 if (!plungerSensor->ready())
mjr 76:7f5912b6340e 5232 return;
mjr 76:7f5912b6340e 5233
mjr 48:058ace2aed1d 5234 // Read a sample from the sensor
mjr 48:058ace2aed1d 5235 PlungerReading r;
mjr 48:058ace2aed1d 5236 if (plungerSensor->read(r))
mjr 48:058ace2aed1d 5237 {
mjr 53:9b2611964afc 5238 // check for calibration mode
mjr 53:9b2611964afc 5239 if (plungerCalMode)
mjr 53:9b2611964afc 5240 {
mjr 53:9b2611964afc 5241 // Calibration mode. Adjust the calibration bounds to fit
mjr 53:9b2611964afc 5242 // the value. If this value is beyond the current min or max,
mjr 53:9b2611964afc 5243 // expand the envelope to include this new value.
mjr 53:9b2611964afc 5244 if (r.pos > cfg.plunger.cal.max)
mjr 53:9b2611964afc 5245 cfg.plunger.cal.max = r.pos;
mjr 53:9b2611964afc 5246 if (r.pos < cfg.plunger.cal.min)
mjr 53:9b2611964afc 5247 cfg.plunger.cal.min = r.pos;
mjr 76:7f5912b6340e 5248
mjr 76:7f5912b6340e 5249 // update our cached calibration data
mjr 76:7f5912b6340e 5250 onUpdateCal();
mjr 50:40015764bbe6 5251
mjr 53:9b2611964afc 5252 // If we're in calibration state 0, we're waiting for the
mjr 53:9b2611964afc 5253 // plunger to come to rest at the park position so that we
mjr 53:9b2611964afc 5254 // can take a sample of the park position. Check to see if
mjr 53:9b2611964afc 5255 // we've been at rest for a minimum interval.
mjr 53:9b2611964afc 5256 if (calState == 0)
mjr 53:9b2611964afc 5257 {
mjr 53:9b2611964afc 5258 if (abs(r.pos - calZeroStart.pos) < 65535/3/50)
mjr 53:9b2611964afc 5259 {
mjr 53:9b2611964afc 5260 // we're close enough - make sure we've been here long enough
mjr 53:9b2611964afc 5261 if (uint32_t(r.t - calZeroStart.t) > 100000UL)
mjr 53:9b2611964afc 5262 {
mjr 53:9b2611964afc 5263 // we've been at rest long enough - count it
mjr 53:9b2611964afc 5264 calZeroPosSum += r.pos;
mjr 53:9b2611964afc 5265 calZeroPosN += 1;
mjr 53:9b2611964afc 5266
mjr 53:9b2611964afc 5267 // update the zero position from the new average
mjr 53:9b2611964afc 5268 cfg.plunger.cal.zero = uint16_t(calZeroPosSum / calZeroPosN);
mjr 76:7f5912b6340e 5269 onUpdateCal();
mjr 53:9b2611964afc 5270
mjr 53:9b2611964afc 5271 // switch to calibration state 1 - at rest
mjr 53:9b2611964afc 5272 calState = 1;
mjr 53:9b2611964afc 5273 }
mjr 53:9b2611964afc 5274 }
mjr 53:9b2611964afc 5275 else
mjr 53:9b2611964afc 5276 {
mjr 53:9b2611964afc 5277 // we're not close to the last position - start again here
mjr 53:9b2611964afc 5278 calZeroStart = r;
mjr 53:9b2611964afc 5279 }
mjr 53:9b2611964afc 5280 }
mjr 53:9b2611964afc 5281
mjr 53:9b2611964afc 5282 // Rescale to the joystick range, and adjust for the current
mjr 53:9b2611964afc 5283 // park position, but don't calibrate. We don't know the maximum
mjr 53:9b2611964afc 5284 // point yet, so we can't calibrate the range.
mjr 53:9b2611964afc 5285 r.pos = int(
mjr 53:9b2611964afc 5286 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 5287 / (65535 - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 5288 }
mjr 53:9b2611964afc 5289 else
mjr 53:9b2611964afc 5290 {
mjr 53:9b2611964afc 5291 // Not in calibration mode. Apply the existing calibration and
mjr 53:9b2611964afc 5292 // rescale to the joystick range.
mjr 76:7f5912b6340e 5293 r.pos = applyCal(r.pos);
mjr 53:9b2611964afc 5294
mjr 53:9b2611964afc 5295 // limit the result to the valid joystick range
mjr 53:9b2611964afc 5296 if (r.pos > JOYMAX)
mjr 53:9b2611964afc 5297 r.pos = JOYMAX;
mjr 53:9b2611964afc 5298 else if (r.pos < -JOYMAX)
mjr 53:9b2611964afc 5299 r.pos = -JOYMAX;
mjr 53:9b2611964afc 5300 }
mjr 50:40015764bbe6 5301
mjr 87:8d35c74403af 5302 // Look for a firing event - the user releasing the plunger and
mjr 87:8d35c74403af 5303 // allowing it to shoot forward at full speed. Wait at least 5ms
mjr 87:8d35c74403af 5304 // between samples for this, to help distinguish random motion
mjr 87:8d35c74403af 5305 // from the rapid motion of a firing event.
mjr 50:40015764bbe6 5306 //
mjr 87:8d35c74403af 5307 // There's a trade-off in the choice of minimum sampling interval.
mjr 87:8d35c74403af 5308 // The longer we wait, the more certain we can be of the trend.
mjr 87:8d35c74403af 5309 // But if we wait too long, the user will perceive a delay. We
mjr 87:8d35c74403af 5310 // also want to sample frequently enough to see the release motion
mjr 87:8d35c74403af 5311 // at intermediate steps along the way, so the sampling has to be
mjr 87:8d35c74403af 5312 // considerably faster than the whole travel time, which is about
mjr 87:8d35c74403af 5313 // 25-50ms.
mjr 87:8d35c74403af 5314 if (uint32_t(r.t - prv.t) < 5000UL)
mjr 87:8d35c74403af 5315 return;
mjr 87:8d35c74403af 5316
mjr 87:8d35c74403af 5317 // assume that we'll report this reading as-is
mjr 87:8d35c74403af 5318 z = r.pos;
mjr 87:8d35c74403af 5319
mjr 87:8d35c74403af 5320 // Firing event detection.
mjr 87:8d35c74403af 5321 //
mjr 87:8d35c74403af 5322 // A "firing event" is when the player releases the plunger from
mjr 87:8d35c74403af 5323 // a retracted position, allowing it to shoot forward under the
mjr 87:8d35c74403af 5324 // spring tension.
mjr 50:40015764bbe6 5325 //
mjr 87:8d35c74403af 5326 // We monitor the plunger motion for these events, and when they
mjr 87:8d35c74403af 5327 // occur, we report an "idealized" version of the motion to the
mjr 87:8d35c74403af 5328 // PC. The idealized version consists of a series of readings
mjr 87:8d35c74403af 5329 // frozen at the fully retracted position for the whole duration
mjr 87:8d35c74403af 5330 // of the forward travel, followed by a series of readings at the
mjr 87:8d35c74403af 5331 // fully forward position for long enough for the plunger to come
mjr 87:8d35c74403af 5332 // mostly to rest. The series of frozen readings aren't meant to
mjr 87:8d35c74403af 5333 // be perceptible to the player - we try to keep them short enough
mjr 87:8d35c74403af 5334 // that they're not apparent as delay. Instead, they're for the
mjr 87:8d35c74403af 5335 // PC client software's benefit. PC joystick clients use polling,
mjr 87:8d35c74403af 5336 // so they only see an unpredictable subset of the readings we
mjr 87:8d35c74403af 5337 // send. The only way to be sure that the client sees a particular
mjr 87:8d35c74403af 5338 // reading is to hold it for long enough that the client is sure to
mjr 87:8d35c74403af 5339 // poll within the hold interval. In the case of the plunger
mjr 87:8d35c74403af 5340 // firing motion, it's important that the client sees the *ends*
mjr 87:8d35c74403af 5341 // of the travel - the fully retracted starting position in
mjr 87:8d35c74403af 5342 // particular. If the PC client only polls for a sample while the
mjr 87:8d35c74403af 5343 // plunger is somewhere in the middle of the travel, the PC will
mjr 87:8d35c74403af 5344 // think that the firing motion *started* in that middle position,
mjr 87:8d35c74403af 5345 // so it won't be able to model the right amount of momentum when
mjr 87:8d35c74403af 5346 // the plunger hits the ball. We try to ensure that the PC sees
mjr 87:8d35c74403af 5347 // the right starting point by reporting the starting point for
mjr 87:8d35c74403af 5348 // extra time during the forward motion. By the same token, we
mjr 87:8d35c74403af 5349 // want the PC to know that the plunger has moved all the way
mjr 87:8d35c74403af 5350 // forward, rather than mistakenly thinking that it stopped
mjr 87:8d35c74403af 5351 // somewhere in the middle of the travel, so we freeze at the
mjr 87:8d35c74403af 5352 // forward position for a short time.
mjr 76:7f5912b6340e 5353 //
mjr 87:8d35c74403af 5354 // To detect a firing event, we look for forward motion that's
mjr 87:8d35c74403af 5355 // fast enough to be a firing event. To determine how fast is
mjr 87:8d35c74403af 5356 // fast enough, we use a simple model of the plunger motion where
mjr 87:8d35c74403af 5357 // the acceleration is constant. This is only an approximation,
mjr 87:8d35c74403af 5358 // as the spring force actually varies with spring's compression,
mjr 87:8d35c74403af 5359 // but it's close enough for our purposes here.
mjr 87:8d35c74403af 5360 //
mjr 87:8d35c74403af 5361 // Do calculations in fixed-point 2^48 scale with 64-bit ints.
mjr 87:8d35c74403af 5362 // acc2 = acceleration/2 for 50ms release time, units of unit
mjr 87:8d35c74403af 5363 // distances per microsecond squared, where the unit distance
mjr 87:8d35c74403af 5364 // is the overall travel from the starting retracted position
mjr 87:8d35c74403af 5365 // to the park position.
mjr 87:8d35c74403af 5366 const int32_t acc2 = 112590; // 2^48 scale
mjr 50:40015764bbe6 5367 switch (firing)
mjr 50:40015764bbe6 5368 {
mjr 50:40015764bbe6 5369 case 0:
mjr 87:8d35c74403af 5370 // Not in firing mode. If we're retracted a bit, and the
mjr 87:8d35c74403af 5371 // motion is forward at a fast enough rate to look like a
mjr 87:8d35c74403af 5372 // release, enter firing mode.
mjr 87:8d35c74403af 5373 if (r.pos > JOYMAX/6)
mjr 50:40015764bbe6 5374 {
mjr 87:8d35c74403af 5375 const uint32_t dt = uint32_t(r.t - prv.t);
mjr 87:8d35c74403af 5376 const uint32_t dt2 = dt*dt; // dt^2
mjr 87:8d35c74403af 5377 if (r.pos < prv.pos - int((prv.pos*acc2*uint64_t(dt2)) >> 48))
mjr 87:8d35c74403af 5378 {
mjr 87:8d35c74403af 5379 // Tentatively enter firing mode. Use the prior reading
mjr 87:8d35c74403af 5380 // as the starting point, and freeze reports for now.
mjr 87:8d35c74403af 5381 firingMode(1);
mjr 87:8d35c74403af 5382 f0 = prv;
mjr 87:8d35c74403af 5383 z = f0.pos;
mjr 87:8d35c74403af 5384
mjr 87:8d35c74403af 5385 // if in calibration state 1 (at rest), switch to
mjr 87:8d35c74403af 5386 // state 2 (not at rest)
mjr 87:8d35c74403af 5387 if (calState == 1)
mjr 87:8d35c74403af 5388 calState = 2;
mjr 87:8d35c74403af 5389 }
mjr 50:40015764bbe6 5390 }
mjr 50:40015764bbe6 5391 break;
mjr 50:40015764bbe6 5392
mjr 50:40015764bbe6 5393 case 1:
mjr 87:8d35c74403af 5394 // Tentative firing mode: the plunger was moving forward
mjr 87:8d35c74403af 5395 // at last check. To stay in firing mode, the plunger has
mjr 87:8d35c74403af 5396 // to keep moving forward fast enough to look like it's
mjr 87:8d35c74403af 5397 // moving under spring force. To figure out how fast is
mjr 87:8d35c74403af 5398 // fast enough, we use a simple model where the acceleration
mjr 87:8d35c74403af 5399 // is constant over the whole travel distance and the total
mjr 87:8d35c74403af 5400 // travel time is 50ms. The acceleration actually varies
mjr 87:8d35c74403af 5401 // slightly since it comes from the spring force, which
mjr 87:8d35c74403af 5402 // is linear in the displacement; but the plunger spring is
mjr 87:8d35c74403af 5403 // fairly compressed even when the plunger is all the way
mjr 87:8d35c74403af 5404 // forward, so the difference in tension from one end of
mjr 87:8d35c74403af 5405 // the travel to the other is fairly small, so it's not too
mjr 87:8d35c74403af 5406 // far off to model it as constant. And the real travel
mjr 87:8d35c74403af 5407 // time obviously isn't a constant, but all we need for
mjr 87:8d35c74403af 5408 // that is an upper bound. So: we'll figure the time since
mjr 87:8d35c74403af 5409 // we entered firing mode, and figure the distance we should
mjr 87:8d35c74403af 5410 // have traveled to complete the trip within the maximum
mjr 87:8d35c74403af 5411 // time allowed. If we've moved far enough, we'll stay
mjr 87:8d35c74403af 5412 // in firing mode; if not, we'll exit firing mode. And if
mjr 87:8d35c74403af 5413 // we cross the finish line while still in firing mode,
mjr 87:8d35c74403af 5414 // we'll switch to the next phase of the firing event.
mjr 50:40015764bbe6 5415 if (r.pos <= 0)
mjr 50:40015764bbe6 5416 {
mjr 87:8d35c74403af 5417 // We crossed the park position. Switch to the second
mjr 87:8d35c74403af 5418 // phase of the firing event, where we hold the reported
mjr 87:8d35c74403af 5419 // position at the "bounce" position (where the plunger
mjr 87:8d35c74403af 5420 // is all the way forward, compressing the barrel spring).
mjr 87:8d35c74403af 5421 // We'll stick here long enough to ensure that the PC
mjr 87:8d35c74403af 5422 // client (Visual Pinball or whatever) sees the reading
mjr 87:8d35c74403af 5423 // and processes the release motion via the simulated
mjr 87:8d35c74403af 5424 // physics.
mjr 50:40015764bbe6 5425 firingMode(2);
mjr 53:9b2611964afc 5426
mjr 53:9b2611964afc 5427 // if in calibration mode, and we're in state 2 (moving),
mjr 53:9b2611964afc 5428 // collect firing statistics for calibration purposes
mjr 53:9b2611964afc 5429 if (plungerCalMode && calState == 2)
mjr 53:9b2611964afc 5430 {
mjr 53:9b2611964afc 5431 // collect a new zero point for the average when we
mjr 53:9b2611964afc 5432 // come to rest
mjr 53:9b2611964afc 5433 calState = 0;
mjr 53:9b2611964afc 5434
mjr 87:8d35c74403af 5435 // collect average firing time statistics in millseconds,
mjr 87:8d35c74403af 5436 // if it's in range (20 to 255 ms)
mjr 87:8d35c74403af 5437 const int dt = uint32_t(r.t - f0.t)/1000UL;
mjr 87:8d35c74403af 5438 if (dt >= 15 && dt <= 255)
mjr 53:9b2611964afc 5439 {
mjr 53:9b2611964afc 5440 calRlsTimeSum += dt;
mjr 53:9b2611964afc 5441 calRlsTimeN += 1;
mjr 53:9b2611964afc 5442 cfg.plunger.cal.tRelease = uint8_t(calRlsTimeSum / calRlsTimeN);
mjr 53:9b2611964afc 5443 }
mjr 53:9b2611964afc 5444 }
mjr 87:8d35c74403af 5445
mjr 87:8d35c74403af 5446 // Figure the "bounce" position as forward of the park
mjr 87:8d35c74403af 5447 // position by 1/6 of the starting retraction distance.
mjr 87:8d35c74403af 5448 // This simulates the momentum of the plunger compressing
mjr 87:8d35c74403af 5449 // the barrel spring on the rebound. The barrel spring
mjr 87:8d35c74403af 5450 // can compress by about 1/6 of the maximum retraction
mjr 87:8d35c74403af 5451 // distance, so we'll simply treat its compression as
mjr 87:8d35c74403af 5452 // proportional to the retraction. (It might be more
mjr 87:8d35c74403af 5453 // realistic to use a slightly higher value here, maybe
mjr 87:8d35c74403af 5454 // 1/4 or 1/3 or the retraction distance, capping it at
mjr 87:8d35c74403af 5455 // a maximum of 1/6, because the real plunger probably
mjr 87:8d35c74403af 5456 // compresses the barrel spring by 100% with less than
mjr 87:8d35c74403af 5457 // 100% retraction. But that won't affect the physics
mjr 87:8d35c74403af 5458 // meaningfully, just the animation, and the effect is
mjr 87:8d35c74403af 5459 // small in any case.)
mjr 87:8d35c74403af 5460 z = f0.pos = -f0.pos / 6;
mjr 87:8d35c74403af 5461
mjr 87:8d35c74403af 5462 // reset the starting time for this phase
mjr 87:8d35c74403af 5463 f0.t = r.t;
mjr 50:40015764bbe6 5464 }
mjr 50:40015764bbe6 5465 else
mjr 50:40015764bbe6 5466 {
mjr 87:8d35c74403af 5467 // check for motion since the start of the firing event
mjr 87:8d35c74403af 5468 const uint32_t dt = uint32_t(r.t - f0.t);
mjr 87:8d35c74403af 5469 const uint32_t dt2 = dt*dt; // dt^2
mjr 87:8d35c74403af 5470 if (dt < 50000
mjr 87:8d35c74403af 5471 && r.pos < f0.pos - int((f0.pos*acc2*uint64_t(dt2)) >> 48))
mjr 87:8d35c74403af 5472 {
mjr 87:8d35c74403af 5473 // It's moving fast enough to still be in a release
mjr 87:8d35c74403af 5474 // motion. Continue reporting the start position, and
mjr 87:8d35c74403af 5475 // stay in the first release phase.
mjr 87:8d35c74403af 5476 z = f0.pos;
mjr 87:8d35c74403af 5477 }
mjr 87:8d35c74403af 5478 else
mjr 87:8d35c74403af 5479 {
mjr 87:8d35c74403af 5480 // It's not moving fast enough to be a release
mjr 87:8d35c74403af 5481 // motion. Return to the default state.
mjr 87:8d35c74403af 5482 firingMode(0);
mjr 87:8d35c74403af 5483 calState = 1;
mjr 87:8d35c74403af 5484 }
mjr 50:40015764bbe6 5485 }
mjr 50:40015764bbe6 5486 break;
mjr 50:40015764bbe6 5487
mjr 50:40015764bbe6 5488 case 2:
mjr 87:8d35c74403af 5489 // Firing mode, holding at forward compression position.
mjr 87:8d35c74403af 5490 // Hold here for 25ms.
mjr 87:8d35c74403af 5491 if (uint32_t(r.t - f0.t) < 25000)
mjr 50:40015764bbe6 5492 {
mjr 87:8d35c74403af 5493 // stay here for now
mjr 87:8d35c74403af 5494 z = f0.pos;
mjr 50:40015764bbe6 5495 }
mjr 50:40015764bbe6 5496 else
mjr 50:40015764bbe6 5497 {
mjr 87:8d35c74403af 5498 // advance to the next phase, where we report the park
mjr 87:8d35c74403af 5499 // position until the plunger comes to rest
mjr 50:40015764bbe6 5500 firingMode(3);
mjr 50:40015764bbe6 5501 z = 0;
mjr 87:8d35c74403af 5502
mjr 87:8d35c74403af 5503 // remember when we started
mjr 87:8d35c74403af 5504 f0.t = r.t;
mjr 50:40015764bbe6 5505 }
mjr 50:40015764bbe6 5506 break;
mjr 50:40015764bbe6 5507
mjr 50:40015764bbe6 5508 case 3:
mjr 87:8d35c74403af 5509 // Firing event, holding at park position. Stay here for
mjr 87:8d35c74403af 5510 // a few moments so that the PC client can simulate the
mjr 87:8d35c74403af 5511 // full release motion, then return to real readings.
mjr 87:8d35c74403af 5512 if (uint32_t(r.t - f0.t) < 250000)
mjr 50:40015764bbe6 5513 {
mjr 87:8d35c74403af 5514 // stay here a while longer
mjr 87:8d35c74403af 5515 z = 0;
mjr 50:40015764bbe6 5516 }
mjr 50:40015764bbe6 5517 else
mjr 50:40015764bbe6 5518 {
mjr 87:8d35c74403af 5519 // it's been long enough - return to normal mode
mjr 87:8d35c74403af 5520 firingMode(0);
mjr 50:40015764bbe6 5521 }
mjr 50:40015764bbe6 5522 break;
mjr 50:40015764bbe6 5523 }
mjr 50:40015764bbe6 5524
mjr 82:4f6209cb5c33 5525 // Check for auto-zeroing, if enabled
mjr 82:4f6209cb5c33 5526 if ((cfg.plunger.autoZero.flags & PlungerAutoZeroEnabled) != 0)
mjr 82:4f6209cb5c33 5527 {
mjr 82:4f6209cb5c33 5528 // If we moved since the last reading, reset and restart the
mjr 82:4f6209cb5c33 5529 // auto-zero timer. Otherwise, if the timer has reached the
mjr 82:4f6209cb5c33 5530 // auto-zero timeout, it means we've been motionless for that
mjr 82:4f6209cb5c33 5531 // long, so auto-zero now.
mjr 82:4f6209cb5c33 5532 if (r.pos != prv.pos)
mjr 82:4f6209cb5c33 5533 {
mjr 82:4f6209cb5c33 5534 // movement detected - reset the timer
mjr 82:4f6209cb5c33 5535 autoZeroTimer.reset();
mjr 82:4f6209cb5c33 5536 autoZeroTimer.start();
mjr 82:4f6209cb5c33 5537 }
mjr 82:4f6209cb5c33 5538 else if (autoZeroTimer.read_us() > cfg.plunger.autoZero.t * 1000000UL)
mjr 82:4f6209cb5c33 5539 {
mjr 82:4f6209cb5c33 5540 // auto-zero now
mjr 82:4f6209cb5c33 5541 plungerSensor->autoZero();
mjr 82:4f6209cb5c33 5542
mjr 82:4f6209cb5c33 5543 // stop the timer so that we don't keep repeating this
mjr 82:4f6209cb5c33 5544 // if the plunger stays still for a long time
mjr 82:4f6209cb5c33 5545 autoZeroTimer.stop();
mjr 82:4f6209cb5c33 5546 autoZeroTimer.reset();
mjr 82:4f6209cb5c33 5547 }
mjr 82:4f6209cb5c33 5548 }
mjr 82:4f6209cb5c33 5549
mjr 87:8d35c74403af 5550 // this new reading becomes the previous reading for next time
mjr 87:8d35c74403af 5551 prv = r;
mjr 48:058ace2aed1d 5552 }
mjr 48:058ace2aed1d 5553 }
mjr 48:058ace2aed1d 5554
mjr 48:058ace2aed1d 5555 // Get the current value to report through the joystick interface
mjr 58:523fdcffbe6d 5556 int16_t getPosition()
mjr 58:523fdcffbe6d 5557 {
mjr 86:e30a1f60f783 5558 // return the last reading
mjr 86:e30a1f60f783 5559 return z;
mjr 55:4db125cd11a0 5560 }
mjr 58:523fdcffbe6d 5561
mjr 48:058ace2aed1d 5562 // Set calibration mode on or off
mjr 52:8298b2a73eb2 5563 void setCalMode(bool f)
mjr 48:058ace2aed1d 5564 {
mjr 52:8298b2a73eb2 5565 // check to see if we're entering calibration mode
mjr 52:8298b2a73eb2 5566 if (f && !plungerCalMode)
mjr 52:8298b2a73eb2 5567 {
mjr 52:8298b2a73eb2 5568 // reset the calibration in the configuration
mjr 48:058ace2aed1d 5569 cfg.plunger.cal.begin();
mjr 52:8298b2a73eb2 5570
mjr 52:8298b2a73eb2 5571 // start in state 0 (waiting to settle)
mjr 52:8298b2a73eb2 5572 calState = 0;
mjr 52:8298b2a73eb2 5573 calZeroPosSum = 0;
mjr 52:8298b2a73eb2 5574 calZeroPosN = 0;
mjr 52:8298b2a73eb2 5575 calRlsTimeSum = 0;
mjr 52:8298b2a73eb2 5576 calRlsTimeN = 0;
mjr 52:8298b2a73eb2 5577
mjr 82:4f6209cb5c33 5578 // tell the plunger we're starting calibration
mjr 82:4f6209cb5c33 5579 plungerSensor->beginCalibration();
mjr 82:4f6209cb5c33 5580
mjr 52:8298b2a73eb2 5581 // set the initial zero point to the current position
mjr 52:8298b2a73eb2 5582 PlungerReading r;
mjr 52:8298b2a73eb2 5583 if (plungerSensor->read(r))
mjr 52:8298b2a73eb2 5584 {
mjr 52:8298b2a73eb2 5585 // got a reading - use it as the initial zero point
mjr 52:8298b2a73eb2 5586 cfg.plunger.cal.zero = r.pos;
mjr 76:7f5912b6340e 5587 onUpdateCal();
mjr 52:8298b2a73eb2 5588
mjr 52:8298b2a73eb2 5589 // use it as the starting point for the settling watch
mjr 53:9b2611964afc 5590 calZeroStart = r;
mjr 52:8298b2a73eb2 5591 }
mjr 52:8298b2a73eb2 5592 else
mjr 52:8298b2a73eb2 5593 {
mjr 52:8298b2a73eb2 5594 // no reading available - use the default 1/6 position
mjr 52:8298b2a73eb2 5595 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 5596 onUpdateCal();
mjr 52:8298b2a73eb2 5597
mjr 52:8298b2a73eb2 5598 // we don't have a starting point for the setting watch
mjr 53:9b2611964afc 5599 calZeroStart.pos = -65535;
mjr 53:9b2611964afc 5600 calZeroStart.t = 0;
mjr 53:9b2611964afc 5601 }
mjr 53:9b2611964afc 5602 }
mjr 53:9b2611964afc 5603 else if (!f && plungerCalMode)
mjr 53:9b2611964afc 5604 {
mjr 53:9b2611964afc 5605 // Leaving calibration mode. Make sure the max is past the
mjr 53:9b2611964afc 5606 // zero point - if it's not, we'd have a zero or negative
mjr 53:9b2611964afc 5607 // denominator for the scaling calculation, which would be
mjr 53:9b2611964afc 5608 // physically meaningless.
mjr 53:9b2611964afc 5609 if (cfg.plunger.cal.max <= cfg.plunger.cal.zero)
mjr 53:9b2611964afc 5610 {
mjr 53:9b2611964afc 5611 // bad settings - reset to defaults
mjr 53:9b2611964afc 5612 cfg.plunger.cal.max = 0xffff;
mjr 53:9b2611964afc 5613 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 5614 onUpdateCal();
mjr 52:8298b2a73eb2 5615 }
mjr 52:8298b2a73eb2 5616 }
mjr 52:8298b2a73eb2 5617
mjr 48:058ace2aed1d 5618 // remember the new mode
mjr 52:8298b2a73eb2 5619 plungerCalMode = f;
mjr 48:058ace2aed1d 5620 }
mjr 48:058ace2aed1d 5621
mjr 76:7f5912b6340e 5622 // Cached inverse of the calibration range. This is for calculating
mjr 76:7f5912b6340e 5623 // the calibrated plunger position given a raw sensor reading. The
mjr 76:7f5912b6340e 5624 // cached inverse is calculated as
mjr 76:7f5912b6340e 5625 //
mjr 76:7f5912b6340e 5626 // 64K * JOYMAX / (cfg.plunger.cal.max - cfg.plunger.cal.zero)
mjr 76:7f5912b6340e 5627 //
mjr 76:7f5912b6340e 5628 // To convert a raw sensor reading to a calibrated position, calculate
mjr 76:7f5912b6340e 5629 //
mjr 76:7f5912b6340e 5630 // ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16
mjr 76:7f5912b6340e 5631 //
mjr 76:7f5912b6340e 5632 // That yields the calibration result without performing a division.
mjr 76:7f5912b6340e 5633 int invCalRange;
mjr 76:7f5912b6340e 5634
mjr 76:7f5912b6340e 5635 // apply the calibration range to a reading
mjr 76:7f5912b6340e 5636 inline int applyCal(int reading)
mjr 76:7f5912b6340e 5637 {
mjr 76:7f5912b6340e 5638 return ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16;
mjr 76:7f5912b6340e 5639 }
mjr 76:7f5912b6340e 5640
mjr 76:7f5912b6340e 5641 void onUpdateCal()
mjr 76:7f5912b6340e 5642 {
mjr 76:7f5912b6340e 5643 invCalRange = (JOYMAX << 16)/(cfg.plunger.cal.max - cfg.plunger.cal.zero);
mjr 76:7f5912b6340e 5644 }
mjr 76:7f5912b6340e 5645
mjr 48:058ace2aed1d 5646 // is a firing event in progress?
mjr 53:9b2611964afc 5647 bool isFiring() { return firing == 3; }
mjr 76:7f5912b6340e 5648
mjr 48:058ace2aed1d 5649 private:
mjr 87:8d35c74403af 5650 // current reported joystick reading
mjr 87:8d35c74403af 5651 int z;
mjr 87:8d35c74403af 5652
mjr 87:8d35c74403af 5653 // previous reading
mjr 87:8d35c74403af 5654 PlungerReading prv;
mjr 87:8d35c74403af 5655
mjr 52:8298b2a73eb2 5656 // Calibration state. During calibration mode, we watch for release
mjr 52:8298b2a73eb2 5657 // events, to measure the time it takes to complete the release
mjr 52:8298b2a73eb2 5658 // motion; and we watch for the plunger to come to reset after a
mjr 52:8298b2a73eb2 5659 // release, to gather statistics on the rest position.
mjr 52:8298b2a73eb2 5660 // 0 = waiting to settle
mjr 52:8298b2a73eb2 5661 // 1 = at rest
mjr 52:8298b2a73eb2 5662 // 2 = retracting
mjr 52:8298b2a73eb2 5663 // 3 = possibly releasing
mjr 52:8298b2a73eb2 5664 uint8_t calState;
mjr 52:8298b2a73eb2 5665
mjr 52:8298b2a73eb2 5666 // Calibration zero point statistics.
mjr 52:8298b2a73eb2 5667 // During calibration mode, we collect data on the rest position (the
mjr 52:8298b2a73eb2 5668 // zero point) by watching for the plunger to come to rest after each
mjr 52:8298b2a73eb2 5669 // release. We average these rest positions to get the calibrated
mjr 52:8298b2a73eb2 5670 // zero point. We use the average because the real physical plunger
mjr 52:8298b2a73eb2 5671 // itself doesn't come to rest at exactly the same spot every time,
mjr 52:8298b2a73eb2 5672 // largely due to friction in the mechanism. To calculate the average,
mjr 52:8298b2a73eb2 5673 // we keep a sum of the readings and a count of samples.
mjr 53:9b2611964afc 5674 PlungerReading calZeroStart;
mjr 52:8298b2a73eb2 5675 long calZeroPosSum;
mjr 52:8298b2a73eb2 5676 int calZeroPosN;
mjr 52:8298b2a73eb2 5677
mjr 52:8298b2a73eb2 5678 // Calibration release time statistics.
mjr 52:8298b2a73eb2 5679 // During calibration, we collect an average for the release time.
mjr 52:8298b2a73eb2 5680 long calRlsTimeSum;
mjr 52:8298b2a73eb2 5681 int calRlsTimeN;
mjr 52:8298b2a73eb2 5682
mjr 85:3c28aee81cde 5683 // Auto-zeroing timer
mjr 85:3c28aee81cde 5684 Timer autoZeroTimer;
mjr 85:3c28aee81cde 5685
mjr 48:058ace2aed1d 5686 // set a firing mode
mjr 48:058ace2aed1d 5687 inline void firingMode(int m)
mjr 48:058ace2aed1d 5688 {
mjr 48:058ace2aed1d 5689 firing = m;
mjr 48:058ace2aed1d 5690 }
mjr 48:058ace2aed1d 5691
mjr 48:058ace2aed1d 5692 // Firing event state.
mjr 48:058ace2aed1d 5693 //
mjr 87:8d35c74403af 5694 // 0 - Default state: not in firing event. We report the true
mjr 87:8d35c74403af 5695 // instantaneous plunger position to the joystick interface.
mjr 48:058ace2aed1d 5696 //
mjr 87:8d35c74403af 5697 // 1 - Moving forward at release speed
mjr 48:058ace2aed1d 5698 //
mjr 87:8d35c74403af 5699 // 2 - Firing - reporting the bounce position
mjr 87:8d35c74403af 5700 //
mjr 87:8d35c74403af 5701 // 3 - Firing - reporting the park position
mjr 48:058ace2aed1d 5702 //
mjr 48:058ace2aed1d 5703 int firing;
mjr 48:058ace2aed1d 5704
mjr 87:8d35c74403af 5705 // Starting position for current firing mode phase
mjr 87:8d35c74403af 5706 PlungerReading f0;
mjr 48:058ace2aed1d 5707 };
mjr 48:058ace2aed1d 5708
mjr 48:058ace2aed1d 5709 // plunger reader singleton
mjr 48:058ace2aed1d 5710 PlungerReader plungerReader;
mjr 48:058ace2aed1d 5711
mjr 48:058ace2aed1d 5712 // ---------------------------------------------------------------------------
mjr 48:058ace2aed1d 5713 //
mjr 48:058ace2aed1d 5714 // Handle the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5715 //
mjr 48:058ace2aed1d 5716 // The ZB Launch Ball feature, if enabled, lets the mechanical plunger
mjr 48:058ace2aed1d 5717 // serve as a substitute for a physical Launch Ball button. When a table
mjr 48:058ace2aed1d 5718 // is loaded in VP, and the table has the ZB Launch Ball LedWiz port
mjr 48:058ace2aed1d 5719 // turned on, we'll disable mechanical plunger reports through the
mjr 48:058ace2aed1d 5720 // joystick interface and instead use the plunger only to simulate the
mjr 48:058ace2aed1d 5721 // Launch Ball button. When the mode is active, pulling back and
mjr 48:058ace2aed1d 5722 // releasing the plunger causes a brief simulated press of the Launch
mjr 48:058ace2aed1d 5723 // button, and pushing the plunger forward of the rest position presses
mjr 48:058ace2aed1d 5724 // the Launch button as long as the plunger is pressed forward.
mjr 48:058ace2aed1d 5725 //
mjr 48:058ace2aed1d 5726 // This feature has two configuration components:
mjr 48:058ace2aed1d 5727 //
mjr 48:058ace2aed1d 5728 // - An LedWiz port number. This port is a "virtual" port that doesn't
mjr 48:058ace2aed1d 5729 // have to be attached to any actual output. DOF uses it to signal
mjr 48:058ace2aed1d 5730 // that the current table uses a Launch button instead of a plunger.
mjr 48:058ace2aed1d 5731 // DOF simply turns the port on when such a table is loaded and turns
mjr 48:058ace2aed1d 5732 // it off at all other times. We use it to enable and disable the
mjr 48:058ace2aed1d 5733 // plunger/launch button connection.
mjr 48:058ace2aed1d 5734 //
mjr 48:058ace2aed1d 5735 // - A joystick button ID. We simulate pressing this button when the
mjr 48:058ace2aed1d 5736 // launch feature is activated via the LedWiz port and the plunger is
mjr 48:058ace2aed1d 5737 // either pulled back and releasd, or pushed forward past the rest
mjr 48:058ace2aed1d 5738 // position.
mjr 48:058ace2aed1d 5739 //
mjr 48:058ace2aed1d 5740 class ZBLaunchBall
mjr 48:058ace2aed1d 5741 {
mjr 48:058ace2aed1d 5742 public:
mjr 48:058ace2aed1d 5743 ZBLaunchBall()
mjr 48:058ace2aed1d 5744 {
mjr 48:058ace2aed1d 5745 // start in the default state
mjr 48:058ace2aed1d 5746 lbState = 0;
mjr 53:9b2611964afc 5747 btnState = false;
mjr 48:058ace2aed1d 5748 }
mjr 48:058ace2aed1d 5749
mjr 48:058ace2aed1d 5750 // Update state. This checks the current plunger position and
mjr 48:058ace2aed1d 5751 // the timers to see if the plunger is in a position that simulates
mjr 48:058ace2aed1d 5752 // a Launch Ball button press via the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5753 // Updates the simulated button vector according to the current
mjr 48:058ace2aed1d 5754 // launch ball state. The main loop calls this before each
mjr 48:058ace2aed1d 5755 // joystick update to figure the new simulated button state.
mjr 53:9b2611964afc 5756 void update()
mjr 48:058ace2aed1d 5757 {
mjr 53:9b2611964afc 5758 // If the ZB Launch Ball led wiz output is ON, check for a
mjr 53:9b2611964afc 5759 // plunger firing event
mjr 53:9b2611964afc 5760 if (zbLaunchOn)
mjr 48:058ace2aed1d 5761 {
mjr 53:9b2611964afc 5762 // note the new position
mjr 48:058ace2aed1d 5763 int znew = plungerReader.getPosition();
mjr 53:9b2611964afc 5764
mjr 53:9b2611964afc 5765 // figure the push threshold from the configuration data
mjr 51:57eb311faafa 5766 const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
mjr 53:9b2611964afc 5767
mjr 53:9b2611964afc 5768 // check the state
mjr 48:058ace2aed1d 5769 switch (lbState)
mjr 48:058ace2aed1d 5770 {
mjr 48:058ace2aed1d 5771 case 0:
mjr 53:9b2611964afc 5772 // Default state. If a launch event has been detected on
mjr 53:9b2611964afc 5773 // the plunger, activate a timed pulse and switch to state 1.
mjr 53:9b2611964afc 5774 // If the plunger is pushed forward of the threshold, push
mjr 53:9b2611964afc 5775 // the button.
mjr 53:9b2611964afc 5776 if (plungerReader.isFiring())
mjr 53:9b2611964afc 5777 {
mjr 53:9b2611964afc 5778 // firing event - start a timed Launch button pulse
mjr 53:9b2611964afc 5779 lbTimer.reset();
mjr 53:9b2611964afc 5780 lbTimer.start();
mjr 53:9b2611964afc 5781 setButton(true);
mjr 53:9b2611964afc 5782
mjr 53:9b2611964afc 5783 // switch to state 1
mjr 53:9b2611964afc 5784 lbState = 1;
mjr 53:9b2611964afc 5785 }
mjr 48:058ace2aed1d 5786 else if (znew <= pushThreshold)
mjr 53:9b2611964afc 5787 {
mjr 53:9b2611964afc 5788 // pushed forward without a firing event - hold the
mjr 53:9b2611964afc 5789 // button as long as we're pushed forward
mjr 53:9b2611964afc 5790 setButton(true);
mjr 53:9b2611964afc 5791 }
mjr 53:9b2611964afc 5792 else
mjr 53:9b2611964afc 5793 {
mjr 53:9b2611964afc 5794 // not pushed forward - turn off the Launch button
mjr 53:9b2611964afc 5795 setButton(false);
mjr 53:9b2611964afc 5796 }
mjr 48:058ace2aed1d 5797 break;
mjr 48:058ace2aed1d 5798
mjr 48:058ace2aed1d 5799 case 1:
mjr 53:9b2611964afc 5800 // State 1: Timed Launch button pulse in progress after a
mjr 53:9b2611964afc 5801 // firing event. Wait for the timer to expire.
mjr 53:9b2611964afc 5802 if (lbTimer.read_us() > 200000UL)
mjr 53:9b2611964afc 5803 {
mjr 53:9b2611964afc 5804 // timer expired - turn off the button
mjr 53:9b2611964afc 5805 setButton(false);
mjr 53:9b2611964afc 5806
mjr 53:9b2611964afc 5807 // switch to state 2
mjr 53:9b2611964afc 5808 lbState = 2;
mjr 53:9b2611964afc 5809 }
mjr 48:058ace2aed1d 5810 break;
mjr 48:058ace2aed1d 5811
mjr 48:058ace2aed1d 5812 case 2:
mjr 53:9b2611964afc 5813 // State 2: Timed Launch button pulse done. Wait for the
mjr 53:9b2611964afc 5814 // plunger launch event to end.
mjr 53:9b2611964afc 5815 if (!plungerReader.isFiring())
mjr 53:9b2611964afc 5816 {
mjr 53:9b2611964afc 5817 // firing event done - return to default state
mjr 53:9b2611964afc 5818 lbState = 0;
mjr 53:9b2611964afc 5819 }
mjr 48:058ace2aed1d 5820 break;
mjr 48:058ace2aed1d 5821 }
mjr 53:9b2611964afc 5822 }
mjr 53:9b2611964afc 5823 else
mjr 53:9b2611964afc 5824 {
mjr 53:9b2611964afc 5825 // ZB Launch Ball disabled - turn off the button if it was on
mjr 53:9b2611964afc 5826 setButton(false);
mjr 48:058ace2aed1d 5827
mjr 53:9b2611964afc 5828 // return to the default state
mjr 53:9b2611964afc 5829 lbState = 0;
mjr 48:058ace2aed1d 5830 }
mjr 48:058ace2aed1d 5831 }
mjr 53:9b2611964afc 5832
mjr 53:9b2611964afc 5833 // Set the button state
mjr 53:9b2611964afc 5834 void setButton(bool on)
mjr 53:9b2611964afc 5835 {
mjr 53:9b2611964afc 5836 if (btnState != on)
mjr 53:9b2611964afc 5837 {
mjr 53:9b2611964afc 5838 // remember the new state
mjr 53:9b2611964afc 5839 btnState = on;
mjr 53:9b2611964afc 5840
mjr 53:9b2611964afc 5841 // update the virtual button state
mjr 65:739875521aae 5842 buttonState[zblButtonIndex].virtPress(on);
mjr 53:9b2611964afc 5843 }
mjr 53:9b2611964afc 5844 }
mjr 53:9b2611964afc 5845
mjr 48:058ace2aed1d 5846 private:
mjr 48:058ace2aed1d 5847 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 48:058ace2aed1d 5848 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 48:058ace2aed1d 5849 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 48:058ace2aed1d 5850 // back and releases the plunger, or simply pushes on the plunger from
mjr 48:058ace2aed1d 5851 // the rest position. This allows the plunger to be used in lieu of a
mjr 48:058ace2aed1d 5852 // physical Launch Ball button for tables that don't have plungers.
mjr 48:058ace2aed1d 5853 //
mjr 48:058ace2aed1d 5854 // States:
mjr 48:058ace2aed1d 5855 // 0 = default
mjr 53:9b2611964afc 5856 // 1 = firing (firing event has activated a Launch button pulse)
mjr 53:9b2611964afc 5857 // 2 = firing done (Launch button pulse ended, waiting for plunger
mjr 53:9b2611964afc 5858 // firing event to end)
mjr 53:9b2611964afc 5859 uint8_t lbState;
mjr 48:058ace2aed1d 5860
mjr 53:9b2611964afc 5861 // button state
mjr 53:9b2611964afc 5862 bool btnState;
mjr 48:058ace2aed1d 5863
mjr 48:058ace2aed1d 5864 // Time since last lbState transition. Some of the states are time-
mjr 48:058ace2aed1d 5865 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 48:058ace2aed1d 5866 // we remain in this state for more than a few milliseconds, since
mjr 48:058ace2aed1d 5867 // it indicates that the plunger is being slowly returned to rest
mjr 48:058ace2aed1d 5868 // rather than released. In the "launching" state, we need to release
mjr 48:058ace2aed1d 5869 // the Launch Ball button after a moment, and we need to wait for
mjr 48:058ace2aed1d 5870 // the plunger to come to rest before returning to state 0.
mjr 48:058ace2aed1d 5871 Timer lbTimer;
mjr 48:058ace2aed1d 5872 };
mjr 48:058ace2aed1d 5873
mjr 35:e959ffba78fd 5874 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5875 //
mjr 35:e959ffba78fd 5876 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 5877 //
mjr 54:fd77a6b2f76c 5878 void reboot(USBJoystick &js, bool disconnect = true, long pause_us = 2000000L)
mjr 35:e959ffba78fd 5879 {
mjr 35:e959ffba78fd 5880 // disconnect from USB
mjr 54:fd77a6b2f76c 5881 if (disconnect)
mjr 54:fd77a6b2f76c 5882 js.disconnect();
mjr 35:e959ffba78fd 5883
mjr 35:e959ffba78fd 5884 // wait a few seconds to make sure the host notices the disconnect
mjr 54:fd77a6b2f76c 5885 wait_us(pause_us);
mjr 35:e959ffba78fd 5886
mjr 35:e959ffba78fd 5887 // reset the device
mjr 35:e959ffba78fd 5888 NVIC_SystemReset();
mjr 35:e959ffba78fd 5889 while (true) { }
mjr 35:e959ffba78fd 5890 }
mjr 35:e959ffba78fd 5891
mjr 35:e959ffba78fd 5892 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5893 //
mjr 35:e959ffba78fd 5894 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 5895 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 5896 //
mjr 35:e959ffba78fd 5897 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 5898 {
mjr 35:e959ffba78fd 5899 int tmp;
mjr 78:1e00b3fa11af 5900 switch (cfg.accel.orientation)
mjr 35:e959ffba78fd 5901 {
mjr 35:e959ffba78fd 5902 case OrientationFront:
mjr 35:e959ffba78fd 5903 tmp = x;
mjr 35:e959ffba78fd 5904 x = y;
mjr 35:e959ffba78fd 5905 y = tmp;
mjr 35:e959ffba78fd 5906 break;
mjr 35:e959ffba78fd 5907
mjr 35:e959ffba78fd 5908 case OrientationLeft:
mjr 35:e959ffba78fd 5909 x = -x;
mjr 35:e959ffba78fd 5910 break;
mjr 35:e959ffba78fd 5911
mjr 35:e959ffba78fd 5912 case OrientationRight:
mjr 35:e959ffba78fd 5913 y = -y;
mjr 35:e959ffba78fd 5914 break;
mjr 35:e959ffba78fd 5915
mjr 35:e959ffba78fd 5916 case OrientationRear:
mjr 35:e959ffba78fd 5917 tmp = -x;
mjr 35:e959ffba78fd 5918 x = -y;
mjr 35:e959ffba78fd 5919 y = tmp;
mjr 35:e959ffba78fd 5920 break;
mjr 35:e959ffba78fd 5921 }
mjr 35:e959ffba78fd 5922 }
mjr 35:e959ffba78fd 5923
mjr 35:e959ffba78fd 5924 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5925 //
mjr 35:e959ffba78fd 5926 // Calibration button state:
mjr 35:e959ffba78fd 5927 // 0 = not pushed
mjr 35:e959ffba78fd 5928 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 5929 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 5930 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 5931 int calBtnState = 0;
mjr 35:e959ffba78fd 5932
mjr 35:e959ffba78fd 5933 // calibration button debounce timer
mjr 35:e959ffba78fd 5934 Timer calBtnTimer;
mjr 35:e959ffba78fd 5935
mjr 35:e959ffba78fd 5936 // calibration button light state
mjr 35:e959ffba78fd 5937 int calBtnLit = false;
mjr 35:e959ffba78fd 5938
mjr 35:e959ffba78fd 5939
mjr 35:e959ffba78fd 5940 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5941 //
mjr 40:cc0d9814522b 5942 // Configuration variable get/set message handling
mjr 35:e959ffba78fd 5943 //
mjr 40:cc0d9814522b 5944
mjr 40:cc0d9814522b 5945 // Handle SET messages - write configuration variables from USB message data
mjr 40:cc0d9814522b 5946 #define if_msg_valid(test) if (test)
mjr 53:9b2611964afc 5947 #define v_byte(var, ofs) cfg.var = data[ofs]
mjr 91:ae9be42652bf 5948 #define v_byte_wo(var, ofs) cfg.var = data[ofs]
mjr 53:9b2611964afc 5949 #define v_ui16(var, ofs) cfg.var = wireUI16(data+(ofs))
mjr 77:0b96f6867312 5950 #define v_ui32(var, ofs) cfg.var = wireUI32(data+(ofs))
mjr 53:9b2611964afc 5951 #define v_pin(var, ofs) cfg.var = wirePinName(data[ofs])
mjr 53:9b2611964afc 5952 #define v_byte_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 5953 #define v_ui32_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 5954 #define VAR_MODE_SET 1 // we're in SET mode
mjr 76:7f5912b6340e 5955 #define v_func configVarSet(const uint8_t *data)
mjr 40:cc0d9814522b 5956 #include "cfgVarMsgMap.h"
mjr 35:e959ffba78fd 5957
mjr 40:cc0d9814522b 5958 // redefine everything for the SET messages
mjr 40:cc0d9814522b 5959 #undef if_msg_valid
mjr 40:cc0d9814522b 5960 #undef v_byte
mjr 40:cc0d9814522b 5961 #undef v_ui16
mjr 77:0b96f6867312 5962 #undef v_ui32
mjr 40:cc0d9814522b 5963 #undef v_pin
mjr 53:9b2611964afc 5964 #undef v_byte_ro
mjr 91:ae9be42652bf 5965 #undef v_byte_wo
mjr 74:822a92bc11d2 5966 #undef v_ui32_ro
mjr 74:822a92bc11d2 5967 #undef VAR_MODE_SET
mjr 40:cc0d9814522b 5968 #undef v_func
mjr 38:091e511ce8a0 5969
mjr 91:ae9be42652bf 5970 // Handle GET messages - read variable values and return in USB message data
mjr 40:cc0d9814522b 5971 #define if_msg_valid(test)
mjr 53:9b2611964afc 5972 #define v_byte(var, ofs) data[ofs] = cfg.var
mjr 53:9b2611964afc 5973 #define v_ui16(var, ofs) ui16Wire(data+(ofs), cfg.var)
mjr 77:0b96f6867312 5974 #define v_ui32(var, ofs) ui32Wire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 5975 #define v_pin(var, ofs) pinNameWire(data+(ofs), cfg.var)
mjr 73:4e8ce0b18915 5976 #define v_byte_ro(val, ofs) data[ofs] = (val)
mjr 74:822a92bc11d2 5977 #define v_ui32_ro(val, ofs) ui32Wire(data+(ofs), val);
mjr 74:822a92bc11d2 5978 #define VAR_MODE_SET 0 // we're in GET mode
mjr 91:ae9be42652bf 5979 #define v_byte_wo(var, ofs) // ignore write-only variables in GET mode
mjr 76:7f5912b6340e 5980 #define v_func configVarGet(uint8_t *data)
mjr 40:cc0d9814522b 5981 #include "cfgVarMsgMap.h"
mjr 40:cc0d9814522b 5982
mjr 35:e959ffba78fd 5983
mjr 35:e959ffba78fd 5984 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5985 //
mjr 35:e959ffba78fd 5986 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 5987 // LedWiz protocol.
mjr 33:d832bcab089e 5988 //
mjr 78:1e00b3fa11af 5989 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js, Accel &accel)
mjr 35:e959ffba78fd 5990 {
mjr 38:091e511ce8a0 5991 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 5992 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 5993 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 5994 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 5995 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 5996 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 5997 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 5998 // So our full protocol is as follows:
mjr 38:091e511ce8a0 5999 //
mjr 38:091e511ce8a0 6000 // first byte =
mjr 74:822a92bc11d2 6001 // 0-48 -> PBA
mjr 74:822a92bc11d2 6002 // 64 -> SBA
mjr 38:091e511ce8a0 6003 // 65 -> private control message; second byte specifies subtype
mjr 74:822a92bc11d2 6004 // 129-132 -> PBA
mjr 38:091e511ce8a0 6005 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 6006 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 6007 // other -> reserved for future use
mjr 38:091e511ce8a0 6008 //
mjr 39:b3815a1c3802 6009 uint8_t *data = lwm.data;
mjr 74:822a92bc11d2 6010 if (data[0] == 64)
mjr 35:e959ffba78fd 6011 {
mjr 74:822a92bc11d2 6012 // 64 = SBA (original LedWiz command to set on/off switches for ports 1-32)
mjr 74:822a92bc11d2 6013 //printf("SBA %02x %02x %02x %02x, speed %02x\r\n",
mjr 38:091e511ce8a0 6014 // data[1], data[2], data[3], data[4], data[5]);
mjr 74:822a92bc11d2 6015 sba_sbx(0, data);
mjr 74:822a92bc11d2 6016
mjr 74:822a92bc11d2 6017 // SBA resets the PBA port group counter
mjr 38:091e511ce8a0 6018 pbaIdx = 0;
mjr 38:091e511ce8a0 6019 }
mjr 38:091e511ce8a0 6020 else if (data[0] == 65)
mjr 38:091e511ce8a0 6021 {
mjr 38:091e511ce8a0 6022 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 6023 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 6024 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 6025 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 6026 // message type.
mjr 39:b3815a1c3802 6027 switch (data[1])
mjr 38:091e511ce8a0 6028 {
mjr 39:b3815a1c3802 6029 case 0:
mjr 39:b3815a1c3802 6030 // No Op
mjr 39:b3815a1c3802 6031 break;
mjr 39:b3815a1c3802 6032
mjr 39:b3815a1c3802 6033 case 1:
mjr 38:091e511ce8a0 6034 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 6035 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 6036 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 6037 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 6038 {
mjr 39:b3815a1c3802 6039
mjr 39:b3815a1c3802 6040 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 6041 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 6042 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 6043
mjr 86:e30a1f60f783 6044 // we'll need a reboot if the LedWiz unit number is changing
mjr 86:e30a1f60f783 6045 bool reboot = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 6046
mjr 39:b3815a1c3802 6047 // set the configuration parameters from the message
mjr 39:b3815a1c3802 6048 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 6049 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 6050
mjr 77:0b96f6867312 6051 // set the flag to do the save
mjr 86:e30a1f60f783 6052 saveConfigToFlash(0, reboot);
mjr 39:b3815a1c3802 6053 }
mjr 39:b3815a1c3802 6054 break;
mjr 38:091e511ce8a0 6055
mjr 39:b3815a1c3802 6056 case 2:
mjr 38:091e511ce8a0 6057 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 6058 // (No parameters)
mjr 38:091e511ce8a0 6059
mjr 38:091e511ce8a0 6060 // enter calibration mode
mjr 38:091e511ce8a0 6061 calBtnState = 3;
mjr 52:8298b2a73eb2 6062 plungerReader.setCalMode(true);
mjr 38:091e511ce8a0 6063 calBtnTimer.reset();
mjr 39:b3815a1c3802 6064 break;
mjr 39:b3815a1c3802 6065
mjr 39:b3815a1c3802 6066 case 3:
mjr 52:8298b2a73eb2 6067 // 3 = plunger sensor status report
mjr 48:058ace2aed1d 6068 // data[2] = flag bits
mjr 53:9b2611964afc 6069 // data[3] = extra exposure time, 100us (.1ms) increments
mjr 52:8298b2a73eb2 6070 reportPlungerStat = true;
mjr 53:9b2611964afc 6071 reportPlungerStatFlags = data[2];
mjr 53:9b2611964afc 6072 reportPlungerStatTime = data[3];
mjr 38:091e511ce8a0 6073
mjr 38:091e511ce8a0 6074 // show purple until we finish sending the report
mjr 38:091e511ce8a0 6075 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 6076 break;
mjr 39:b3815a1c3802 6077
mjr 39:b3815a1c3802 6078 case 4:
mjr 38:091e511ce8a0 6079 // 4 = hardware configuration query
mjr 38:091e511ce8a0 6080 // (No parameters)
mjr 38:091e511ce8a0 6081 js.reportConfig(
mjr 38:091e511ce8a0 6082 numOutputs,
mjr 38:091e511ce8a0 6083 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 52:8298b2a73eb2 6084 cfg.plunger.cal.zero, cfg.plunger.cal.max, cfg.plunger.cal.tRelease,
mjr 75:677892300e7a 6085 nvm.valid(), // a config is loaded if the config memory block is valid
mjr 75:677892300e7a 6086 true, // we support sbx/pbx extensions
mjr 78:1e00b3fa11af 6087 true, // we support the new accelerometer settings
mjr 82:4f6209cb5c33 6088 true, // we support the "flash write ok" status bit in joystick reports
mjr 92:f264fbaa1be5 6089 true, // we support the configurable joystick report timing features
mjr 99:8139b0c274f4 6090 true, // chime logic is supported
mjr 79:682ae3171a08 6091 mallocBytesFree()); // remaining memory size
mjr 39:b3815a1c3802 6092 break;
mjr 39:b3815a1c3802 6093
mjr 39:b3815a1c3802 6094 case 5:
mjr 38:091e511ce8a0 6095 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 6096 allOutputsOff();
mjr 39:b3815a1c3802 6097 break;
mjr 39:b3815a1c3802 6098
mjr 39:b3815a1c3802 6099 case 6:
mjr 85:3c28aee81cde 6100 // 6 = Save configuration to flash. Optionally reboot after the
mjr 85:3c28aee81cde 6101 // delay time in seconds given in data[2].
mjr 85:3c28aee81cde 6102 //
mjr 85:3c28aee81cde 6103 // data[2] = delay time in seconds
mjr 85:3c28aee81cde 6104 // data[3] = flags:
mjr 85:3c28aee81cde 6105 // 0x01 -> do not reboot
mjr 86:e30a1f60f783 6106 saveConfigToFlash(data[2], !(data[3] & 0x01));
mjr 39:b3815a1c3802 6107 break;
mjr 40:cc0d9814522b 6108
mjr 40:cc0d9814522b 6109 case 7:
mjr 40:cc0d9814522b 6110 // 7 = Device ID report
mjr 53:9b2611964afc 6111 // data[2] = ID index: 1=CPU ID, 2=OpenSDA TUID
mjr 53:9b2611964afc 6112 js.reportID(data[2]);
mjr 40:cc0d9814522b 6113 break;
mjr 40:cc0d9814522b 6114
mjr 40:cc0d9814522b 6115 case 8:
mjr 40:cc0d9814522b 6116 // 8 = Engage/disengage night mode.
mjr 40:cc0d9814522b 6117 // data[2] = 1 to engage, 0 to disengage
mjr 40:cc0d9814522b 6118 setNightMode(data[2]);
mjr 40:cc0d9814522b 6119 break;
mjr 52:8298b2a73eb2 6120
mjr 52:8298b2a73eb2 6121 case 9:
mjr 52:8298b2a73eb2 6122 // 9 = Config variable query.
mjr 52:8298b2a73eb2 6123 // data[2] = config var ID
mjr 52:8298b2a73eb2 6124 // data[3] = array index (for array vars: button assignments, output ports)
mjr 52:8298b2a73eb2 6125 {
mjr 53:9b2611964afc 6126 // set up the reply buffer with the variable ID data, and zero out
mjr 53:9b2611964afc 6127 // the rest of the buffer
mjr 52:8298b2a73eb2 6128 uint8_t reply[8];
mjr 52:8298b2a73eb2 6129 reply[1] = data[2];
mjr 52:8298b2a73eb2 6130 reply[2] = data[3];
mjr 53:9b2611964afc 6131 memset(reply+3, 0, sizeof(reply)-3);
mjr 52:8298b2a73eb2 6132
mjr 52:8298b2a73eb2 6133 // query the value
mjr 52:8298b2a73eb2 6134 configVarGet(reply);
mjr 52:8298b2a73eb2 6135
mjr 52:8298b2a73eb2 6136 // send the reply
mjr 52:8298b2a73eb2 6137 js.reportConfigVar(reply + 1);
mjr 52:8298b2a73eb2 6138 }
mjr 52:8298b2a73eb2 6139 break;
mjr 53:9b2611964afc 6140
mjr 53:9b2611964afc 6141 case 10:
mjr 53:9b2611964afc 6142 // 10 = Build ID query.
mjr 53:9b2611964afc 6143 js.reportBuildInfo(getBuildID());
mjr 53:9b2611964afc 6144 break;
mjr 73:4e8ce0b18915 6145
mjr 73:4e8ce0b18915 6146 case 11:
mjr 73:4e8ce0b18915 6147 // 11 = TV ON relay control.
mjr 73:4e8ce0b18915 6148 // data[2] = operation:
mjr 73:4e8ce0b18915 6149 // 0 = turn relay off
mjr 73:4e8ce0b18915 6150 // 1 = turn relay on
mjr 73:4e8ce0b18915 6151 // 2 = pulse relay (as though the power-on timer fired)
mjr 73:4e8ce0b18915 6152 TVRelay(data[2]);
mjr 73:4e8ce0b18915 6153 break;
mjr 73:4e8ce0b18915 6154
mjr 73:4e8ce0b18915 6155 case 12:
mjr 77:0b96f6867312 6156 // 12 = Learn IR code. This enters IR learning mode. While
mjr 77:0b96f6867312 6157 // in learning mode, we report raw IR signals and the first IR
mjr 77:0b96f6867312 6158 // command decoded through the special IR report format. IR
mjr 77:0b96f6867312 6159 // learning mode automatically ends after a timeout expires if
mjr 77:0b96f6867312 6160 // no command can be decoded within the time limit.
mjr 77:0b96f6867312 6161
mjr 77:0b96f6867312 6162 // enter IR learning mode
mjr 77:0b96f6867312 6163 IRLearningMode = 1;
mjr 77:0b96f6867312 6164
mjr 77:0b96f6867312 6165 // cancel any regular IR input in progress
mjr 77:0b96f6867312 6166 IRCommandIn = 0;
mjr 77:0b96f6867312 6167
mjr 77:0b96f6867312 6168 // reset and start the learning mode timeout timer
mjr 77:0b96f6867312 6169 IRTimer.reset();
mjr 73:4e8ce0b18915 6170 break;
mjr 73:4e8ce0b18915 6171
mjr 73:4e8ce0b18915 6172 case 13:
mjr 73:4e8ce0b18915 6173 // 13 = Send button status report
mjr 73:4e8ce0b18915 6174 reportButtonStatus(js);
mjr 73:4e8ce0b18915 6175 break;
mjr 78:1e00b3fa11af 6176
mjr 78:1e00b3fa11af 6177 case 14:
mjr 78:1e00b3fa11af 6178 // 14 = manually center the accelerometer
mjr 78:1e00b3fa11af 6179 accel.manualCenterRequest();
mjr 78:1e00b3fa11af 6180 break;
mjr 78:1e00b3fa11af 6181
mjr 78:1e00b3fa11af 6182 case 15:
mjr 78:1e00b3fa11af 6183 // 15 = set up ad hoc IR command, part 1. Mark the command
mjr 78:1e00b3fa11af 6184 // as not ready, and save the partial data from the message.
mjr 78:1e00b3fa11af 6185 IRAdHocCmd.ready = 0;
mjr 78:1e00b3fa11af 6186 IRAdHocCmd.protocol = data[2];
mjr 78:1e00b3fa11af 6187 IRAdHocCmd.dittos = (data[3] & IRFlagDittos) != 0;
mjr 78:1e00b3fa11af 6188 IRAdHocCmd.code = wireUI32(&data[4]);
mjr 78:1e00b3fa11af 6189 break;
mjr 78:1e00b3fa11af 6190
mjr 78:1e00b3fa11af 6191 case 16:
mjr 78:1e00b3fa11af 6192 // 16 = send ad hoc IR command, part 2. Fill in the rest
mjr 78:1e00b3fa11af 6193 // of the data from the message and mark the command as
mjr 78:1e00b3fa11af 6194 // ready. The IR polling routine will send this as soon
mjr 78:1e00b3fa11af 6195 // as the IR transmitter is free.
mjr 78:1e00b3fa11af 6196 IRAdHocCmd.code |= (uint64_t(wireUI32(&data[2])) << 32);
mjr 78:1e00b3fa11af 6197 IRAdHocCmd.ready = 1;
mjr 78:1e00b3fa11af 6198 break;
mjr 88:98bce687e6c0 6199
mjr 88:98bce687e6c0 6200 case 17:
mjr 88:98bce687e6c0 6201 // 17 = send pre-programmed IR command. This works just like
mjr 88:98bce687e6c0 6202 // sending an ad hoc command above, but we get the command data
mjr 88:98bce687e6c0 6203 // from an IR slot in the config rather than from the client.
mjr 88:98bce687e6c0 6204 // First make sure we have a valid slot number.
mjr 88:98bce687e6c0 6205 if (data[2] >= 1 && data[2] <= MAX_IR_CODES)
mjr 88:98bce687e6c0 6206 {
mjr 88:98bce687e6c0 6207 // get the IR command slot in the config
mjr 88:98bce687e6c0 6208 IRCommandCfg &cmd = cfg.IRCommand[data[2] - 1];
mjr 88:98bce687e6c0 6209
mjr 88:98bce687e6c0 6210 // copy the IR command data from the config
mjr 88:98bce687e6c0 6211 IRAdHocCmd.protocol = cmd.protocol;
mjr 88:98bce687e6c0 6212 IRAdHocCmd.dittos = (cmd.flags & IRFlagDittos) != 0;
mjr 88:98bce687e6c0 6213 IRAdHocCmd.code = (uint64_t(cmd.code.hi) << 32) | cmd.code.lo;
mjr 88:98bce687e6c0 6214
mjr 88:98bce687e6c0 6215 // mark the command as ready - this will trigger the polling
mjr 88:98bce687e6c0 6216 // routine to send the command as soon as the transmitter
mjr 88:98bce687e6c0 6217 // is free
mjr 88:98bce687e6c0 6218 IRAdHocCmd.ready = 1;
mjr 88:98bce687e6c0 6219 }
mjr 88:98bce687e6c0 6220 break;
mjr 38:091e511ce8a0 6221 }
mjr 38:091e511ce8a0 6222 }
mjr 38:091e511ce8a0 6223 else if (data[0] == 66)
mjr 38:091e511ce8a0 6224 {
mjr 38:091e511ce8a0 6225 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 6226 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 6227 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 6228 // in a variable-dependent format.
mjr 40:cc0d9814522b 6229 configVarSet(data);
mjr 86:e30a1f60f783 6230
mjr 87:8d35c74403af 6231 // notify the plunger, so that it can update relevant variables
mjr 87:8d35c74403af 6232 // dynamically
mjr 87:8d35c74403af 6233 plungerSensor->onConfigChange(data[1], cfg);
mjr 38:091e511ce8a0 6234 }
mjr 74:822a92bc11d2 6235 else if (data[0] == 67)
mjr 74:822a92bc11d2 6236 {
mjr 74:822a92bc11d2 6237 // SBX - extended SBA message. This is the same as SBA, except
mjr 74:822a92bc11d2 6238 // that the 7th byte selects a group of 32 ports, to allow access
mjr 74:822a92bc11d2 6239 // to ports beyond the first 32.
mjr 74:822a92bc11d2 6240 sba_sbx(data[6], data);
mjr 74:822a92bc11d2 6241 }
mjr 74:822a92bc11d2 6242 else if (data[0] == 68)
mjr 74:822a92bc11d2 6243 {
mjr 74:822a92bc11d2 6244 // PBX - extended PBA message. This is similar to PBA, but
mjr 74:822a92bc11d2 6245 // allows access to more than the first 32 ports by encoding
mjr 74:822a92bc11d2 6246 // a port group byte that selects a block of 8 ports.
mjr 74:822a92bc11d2 6247
mjr 74:822a92bc11d2 6248 // get the port group - the first port is 8*group
mjr 74:822a92bc11d2 6249 int portGroup = data[1];
mjr 74:822a92bc11d2 6250
mjr 74:822a92bc11d2 6251 // unpack the brightness values
mjr 74:822a92bc11d2 6252 uint32_t tmp1 = data[2] | (data[3]<<8) | (data[4]<<16);
mjr 74:822a92bc11d2 6253 uint32_t tmp2 = data[5] | (data[6]<<8) | (data[7]<<16);
mjr 74:822a92bc11d2 6254 uint8_t bri[8] = {
mjr 74:822a92bc11d2 6255 tmp1 & 0x3F, (tmp1>>6) & 0x3F, (tmp1>>12) & 0x3F, (tmp1>>18) & 0x3F,
mjr 74:822a92bc11d2 6256 tmp2 & 0x3F, (tmp2>>6) & 0x3F, (tmp2>>12) & 0x3F, (tmp2>>18) & 0x3F
mjr 74:822a92bc11d2 6257 };
mjr 74:822a92bc11d2 6258
mjr 74:822a92bc11d2 6259 // map the flash levels: 60->129, 61->130, 62->131, 63->132
mjr 74:822a92bc11d2 6260 for (int i = 0 ; i < 8 ; ++i)
mjr 74:822a92bc11d2 6261 {
mjr 74:822a92bc11d2 6262 if (bri[i] >= 60)
mjr 74:822a92bc11d2 6263 bri[i] += 129-60;
mjr 74:822a92bc11d2 6264 }
mjr 74:822a92bc11d2 6265
mjr 74:822a92bc11d2 6266 // Carry out the PBA
mjr 74:822a92bc11d2 6267 pba_pbx(portGroup*8, bri);
mjr 74:822a92bc11d2 6268 }
mjr 38:091e511ce8a0 6269 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 6270 {
mjr 38:091e511ce8a0 6271 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 6272 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 6273 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 6274 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 6275 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 6276 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 6277 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 6278 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 6279 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 6280 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 6281 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 6282 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 6283 //
mjr 38:091e511ce8a0 6284 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 6285 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 6286 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 6287 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 6288 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 6289 // address those ports anyway.
mjr 63:5cd1a5f3a41b 6290
mjr 63:5cd1a5f3a41b 6291 // figure the block of 7 ports covered in the message
mjr 38:091e511ce8a0 6292 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 6293 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 63:5cd1a5f3a41b 6294
mjr 63:5cd1a5f3a41b 6295 // update each port
mjr 38:091e511ce8a0 6296 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 6297 {
mjr 38:091e511ce8a0 6298 // set the brightness level for the output
mjr 40:cc0d9814522b 6299 uint8_t b = data[i-i0+1];
mjr 38:091e511ce8a0 6300 outLevel[i] = b;
mjr 38:091e511ce8a0 6301
mjr 74:822a92bc11d2 6302 // set the port's LedWiz state to the nearest equivalent, so
mjr 74:822a92bc11d2 6303 // that it maintains its current setting if we switch back to
mjr 74:822a92bc11d2 6304 // LedWiz mode on a future update
mjr 76:7f5912b6340e 6305 if (b != 0)
mjr 76:7f5912b6340e 6306 {
mjr 76:7f5912b6340e 6307 // Non-zero brightness - set the SBA switch on, and set the
mjr 76:7f5912b6340e 6308 // PBA brightness to the DOF brightness rescaled to the 1..48
mjr 76:7f5912b6340e 6309 // LedWiz range. If the port is subsequently addressed by an
mjr 76:7f5912b6340e 6310 // LedWiz command, this will carry the current DOF setting
mjr 76:7f5912b6340e 6311 // forward unchanged.
mjr 76:7f5912b6340e 6312 wizOn[i] = 1;
mjr 76:7f5912b6340e 6313 wizVal[i] = dof_to_lw[b];
mjr 76:7f5912b6340e 6314 }
mjr 76:7f5912b6340e 6315 else
mjr 76:7f5912b6340e 6316 {
mjr 76:7f5912b6340e 6317 // Zero brightness. Set the SBA switch off, and leave the
mjr 76:7f5912b6340e 6318 // PBA brightness the same as it was.
mjr 76:7f5912b6340e 6319 wizOn[i] = 0;
mjr 76:7f5912b6340e 6320 }
mjr 74:822a92bc11d2 6321
mjr 38:091e511ce8a0 6322 // set the output
mjr 40:cc0d9814522b 6323 lwPin[i]->set(b);
mjr 38:091e511ce8a0 6324 }
mjr 38:091e511ce8a0 6325
mjr 38:091e511ce8a0 6326 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 6327 if (hc595 != 0)
mjr 38:091e511ce8a0 6328 hc595->update();
mjr 38:091e511ce8a0 6329 }
mjr 38:091e511ce8a0 6330 else
mjr 38:091e511ce8a0 6331 {
mjr 74:822a92bc11d2 6332 // Everything else is an LedWiz PBA message. This is a full
mjr 74:822a92bc11d2 6333 // "profile" dump from the host for one bank of 8 outputs. Each
mjr 74:822a92bc11d2 6334 // byte sets one output in the current bank. The current bank
mjr 74:822a92bc11d2 6335 // is implied; the bank starts at 0 and is reset to 0 by any SBA
mjr 74:822a92bc11d2 6336 // message, and is incremented to the next bank by each PBA. Our
mjr 74:822a92bc11d2 6337 // variable pbaIdx keeps track of the current bank. There's no
mjr 74:822a92bc11d2 6338 // direct way for the host to select the bank; it just has to count
mjr 74:822a92bc11d2 6339 // on us staying in sync. In practice, clients always send the
mjr 74:822a92bc11d2 6340 // full set of 4 PBA messages in a row to set all 32 outputs.
mjr 38:091e511ce8a0 6341 //
mjr 38:091e511ce8a0 6342 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 6343 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 6344 // wizVal[] entry for each output, and that takes precedence
mjr 63:5cd1a5f3a41b 6345 // over the extended protocol settings when we're in LedWiz
mjr 63:5cd1a5f3a41b 6346 // protocol mode.
mjr 38:091e511ce8a0 6347 //
mjr 38:091e511ce8a0 6348 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 6349 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 6350
mjr 74:822a92bc11d2 6351 // carry out the PBA
mjr 74:822a92bc11d2 6352 pba_pbx(pbaIdx, data);
mjr 74:822a92bc11d2 6353
mjr 74:822a92bc11d2 6354 // update the PBX index state for the next message
mjr 74:822a92bc11d2 6355 pbaIdx = (pbaIdx + 8) % 32;
mjr 38:091e511ce8a0 6356 }
mjr 38:091e511ce8a0 6357 }
mjr 35:e959ffba78fd 6358
mjr 38:091e511ce8a0 6359 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 6360 //
mjr 5:a70c0bce770d 6361 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 6362 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 6363 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 6364 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 6365 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 6366 // port outputs.
mjr 5:a70c0bce770d 6367 //
mjr 0:5acbbe3f4cf4 6368 int main(void)
mjr 0:5acbbe3f4cf4 6369 {
mjr 60:f38da020aa13 6370 // say hello to the debug console, in case it's connected
mjr 39:b3815a1c3802 6371 printf("\r\nPinscape Controller starting\r\n");
mjr 94:0476b3e2b996 6372
mjr 98:4df3c0f7e707 6373 // Set the default PWM period to 0.5ms = 2 kHz. This will be used
mjr 98:4df3c0f7e707 6374 // for PWM channels on PWM units whose periods aren't changed
mjr 98:4df3c0f7e707 6375 // explicitly, so it'll apply to LW outputs assigned to GPIO pins.
mjr 98:4df3c0f7e707 6376 // The KL25Z only allows the period to be set at the TPM unit
mjr 94:0476b3e2b996 6377 // level, not per channel, so all channels on a given unit will
mjr 94:0476b3e2b996 6378 // necessarily use the same frequency. We (currently) have two
mjr 94:0476b3e2b996 6379 // subsystems that need specific PWM frequencies: TLC5940NT (which
mjr 94:0476b3e2b996 6380 // uses PWM to generate the grayscale clock signal) and IR remote
mjr 94:0476b3e2b996 6381 // (which uses PWM to generate the IR carrier signal). Since
mjr 94:0476b3e2b996 6382 // those require specific PWM frequencies, it's important to assign
mjr 94:0476b3e2b996 6383 // those to separate TPM units if both are in use simultaneously;
mjr 94:0476b3e2b996 6384 // the Config Tool includes checks to ensure that will happen when
mjr 94:0476b3e2b996 6385 // setting a config interactively. In addition, for the greatest
mjr 94:0476b3e2b996 6386 // flexibility, we take care NOT to assign explicit PWM frequencies
mjr 94:0476b3e2b996 6387 // to pins that don't require special frequences. That way, if a
mjr 94:0476b3e2b996 6388 // pin that doesn't need anything special happens to be sharing a
mjr 94:0476b3e2b996 6389 // TPM unit with a pin that does require a specific frequency, the
mjr 94:0476b3e2b996 6390 // two will co-exist peacefully on the TPM.
mjr 94:0476b3e2b996 6391 //
mjr 94:0476b3e2b996 6392 // We set this default first, before we create any PWM GPIOs, so
mjr 94:0476b3e2b996 6393 // that it will apply to all channels by default but won't override
mjr 94:0476b3e2b996 6394 // any channels that need specific frequences. Currently, the only
mjr 94:0476b3e2b996 6395 // frequency-agnostic PWM user is the LW outputs, so we can choose
mjr 94:0476b3e2b996 6396 // the default to be suitable for those. This is chosen to minimize
mjr 94:0476b3e2b996 6397 // flicker on attached LEDs.
mjr 94:0476b3e2b996 6398 NewPwmUnit::defaultPeriod = 0.0005f;
mjr 82:4f6209cb5c33 6399
mjr 76:7f5912b6340e 6400 // clear the I2C connection
mjr 35:e959ffba78fd 6401 clear_i2c();
mjr 82:4f6209cb5c33 6402
mjr 82:4f6209cb5c33 6403 // Elevate GPIO pin interrupt priorities, so that they can preempt
mjr 82:4f6209cb5c33 6404 // other interrupts. This is important for some external peripherals,
mjr 82:4f6209cb5c33 6405 // particularly the quadrature plunger sensors, which can generate
mjr 82:4f6209cb5c33 6406 // high-speed interrupts that need to be serviced quickly to keep
mjr 82:4f6209cb5c33 6407 // proper count of the quadrature position.
mjr 82:4f6209cb5c33 6408 FastInterruptIn::elevatePriority();
mjr 38:091e511ce8a0 6409
mjr 76:7f5912b6340e 6410 // Load the saved configuration. There are two sources of the
mjr 76:7f5912b6340e 6411 // configuration data:
mjr 76:7f5912b6340e 6412 //
mjr 76:7f5912b6340e 6413 // - Look for an NVM (flash non-volatile memory) configuration.
mjr 76:7f5912b6340e 6414 // If this is valid, we'll load it. The NVM is config data that can
mjr 76:7f5912b6340e 6415 // be updated dynamically by the host via USB commands and then stored
mjr 76:7f5912b6340e 6416 // in the flash by the firmware itself. If this exists, it supersedes
mjr 76:7f5912b6340e 6417 // any of the other settings stores. The Windows config tool uses this
mjr 76:7f5912b6340e 6418 // to store user settings updates.
mjr 76:7f5912b6340e 6419 //
mjr 76:7f5912b6340e 6420 // - If there's no NVM, we'll load the factory defaults, then we'll
mjr 76:7f5912b6340e 6421 // load any settings stored in the host-loaded configuration. The
mjr 76:7f5912b6340e 6422 // host can patch a set of configuration variable settings into the
mjr 76:7f5912b6340e 6423 // .bin file when loading new firmware, in the host-loaded config
mjr 76:7f5912b6340e 6424 // area that we reserve for this purpose. This allows the host to
mjr 76:7f5912b6340e 6425 // restore a configuration at the same time it installs firmware,
mjr 76:7f5912b6340e 6426 // without a separate download of the config data.
mjr 76:7f5912b6340e 6427 //
mjr 76:7f5912b6340e 6428 // The NVM supersedes the host-loaded config, since it can be updated
mjr 76:7f5912b6340e 6429 // between firmware updated and is thus presumably more recent if it's
mjr 76:7f5912b6340e 6430 // present. (Note that the NVM and host-loaded config are both in
mjr 76:7f5912b6340e 6431 // flash, so in principle we could just have a single NVM store that
mjr 76:7f5912b6340e 6432 // the host patches. The only reason we don't is that the NVM store
mjr 76:7f5912b6340e 6433 // is an image of our in-memory config structure, which is a native C
mjr 76:7f5912b6340e 6434 // struct, and we don't want the host to have to know the details of
mjr 76:7f5912b6340e 6435 // its byte layout, for obvious reasons. The host-loaded config, in
mjr 76:7f5912b6340e 6436 // contrast, uses the wire protocol format, which has a well-defined
mjr 76:7f5912b6340e 6437 // byte layout that's independent of the firmware version or the
mjr 76:7f5912b6340e 6438 // details of how the C compiler arranges the struct memory.)
mjr 76:7f5912b6340e 6439 if (!loadConfigFromFlash())
mjr 76:7f5912b6340e 6440 loadHostLoadedConfig();
mjr 35:e959ffba78fd 6441
mjr 38:091e511ce8a0 6442 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 6443 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 6444
mjr 33:d832bcab089e 6445 // we're not connected/awake yet
mjr 33:d832bcab089e 6446 bool connected = false;
mjr 40:cc0d9814522b 6447 Timer connectChangeTimer;
mjr 33:d832bcab089e 6448
mjr 35:e959ffba78fd 6449 // create the plunger sensor interface
mjr 35:e959ffba78fd 6450 createPlunger();
mjr 76:7f5912b6340e 6451
mjr 76:7f5912b6340e 6452 // update the plunger reader's cached calibration data
mjr 76:7f5912b6340e 6453 plungerReader.onUpdateCal();
mjr 33:d832bcab089e 6454
mjr 60:f38da020aa13 6455 // set up the TLC5940 interface, if these chips are present
mjr 35:e959ffba78fd 6456 init_tlc5940(cfg);
mjr 34:6b981a2afab7 6457
mjr 87:8d35c74403af 6458 // initialize the TLC5916 interface, if these chips are present
mjr 87:8d35c74403af 6459 init_tlc59116(cfg);
mjr 87:8d35c74403af 6460
mjr 60:f38da020aa13 6461 // set up 74HC595 interface, if these chips are present
mjr 35:e959ffba78fd 6462 init_hc595(cfg);
mjr 6:cc35eb643e8f 6463
mjr 54:fd77a6b2f76c 6464 // Initialize the LedWiz ports. Note that the ordering here is important:
mjr 54:fd77a6b2f76c 6465 // this has to come after we create the TLC5940 and 74HC595 object instances
mjr 54:fd77a6b2f76c 6466 // (which we just did above), since we need to access those objects to set
mjr 54:fd77a6b2f76c 6467 // up ports assigned to the respective chips.
mjr 35:e959ffba78fd 6468 initLwOut(cfg);
mjr 48:058ace2aed1d 6469
mjr 60:f38da020aa13 6470 // start the TLC5940 refresh cycle clock
mjr 35:e959ffba78fd 6471 if (tlc5940 != 0)
mjr 35:e959ffba78fd 6472 tlc5940->start();
mjr 87:8d35c74403af 6473
mjr 77:0b96f6867312 6474 // Assume that nothing uses keyboard keys. We'll check for keyboard
mjr 77:0b96f6867312 6475 // usage when initializing the various subsystems that can send keys
mjr 77:0b96f6867312 6476 // (buttons, IR). If we find anything that does, we'll create the
mjr 77:0b96f6867312 6477 // USB keyboard interface.
mjr 77:0b96f6867312 6478 bool kbKeys = false;
mjr 77:0b96f6867312 6479
mjr 77:0b96f6867312 6480 // set up the IR remote control emitter & receiver, if present
mjr 77:0b96f6867312 6481 init_IR(cfg, kbKeys);
mjr 77:0b96f6867312 6482
mjr 77:0b96f6867312 6483 // start the power status time, if applicable
mjr 77:0b96f6867312 6484 startPowerStatusTimer(cfg);
mjr 48:058ace2aed1d 6485
mjr 35:e959ffba78fd 6486 // initialize the button input ports
mjr 35:e959ffba78fd 6487 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 6488
mjr 60:f38da020aa13 6489 // Create the joystick USB client. Note that the USB vendor/product ID
mjr 60:f38da020aa13 6490 // information comes from the saved configuration. Also note that we have
mjr 60:f38da020aa13 6491 // to wait until after initializing the input buttons (which we just did
mjr 60:f38da020aa13 6492 // above) to set up the interface, since the button setup will determine
mjr 60:f38da020aa13 6493 // whether or not we need to present a USB keyboard interface in addition
mjr 60:f38da020aa13 6494 // to the joystick interface.
mjr 51:57eb311faafa 6495 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false,
mjr 90:aa4e571da8e8 6496 cfg.joystickEnabled, cfg.joystickAxisFormat, kbKeys);
mjr 51:57eb311faafa 6497
mjr 60:f38da020aa13 6498 // Wait for the USB connection to start up. Show a distinctive diagnostic
mjr 60:f38da020aa13 6499 // flash pattern while waiting.
mjr 70:9f58735a1732 6500 Timer connTimeoutTimer, connFlashTimer;
mjr 70:9f58735a1732 6501 connTimeoutTimer.start();
mjr 70:9f58735a1732 6502 connFlashTimer.start();
mjr 51:57eb311faafa 6503 while (!js.configured())
mjr 51:57eb311faafa 6504 {
mjr 51:57eb311faafa 6505 // show one short yellow flash at 2-second intervals
mjr 70:9f58735a1732 6506 if (connFlashTimer.read_us() > 2000000)
mjr 51:57eb311faafa 6507 {
mjr 51:57eb311faafa 6508 // short yellow flash
mjr 51:57eb311faafa 6509 diagLED(1, 1, 0);
mjr 54:fd77a6b2f76c 6510 wait_us(50000);
mjr 51:57eb311faafa 6511 diagLED(0, 0, 0);
mjr 51:57eb311faafa 6512
mjr 51:57eb311faafa 6513 // reset the flash timer
mjr 70:9f58735a1732 6514 connFlashTimer.reset();
mjr 51:57eb311faafa 6515 }
mjr 70:9f58735a1732 6516
mjr 77:0b96f6867312 6517 // If we've been disconnected for more than the reboot timeout,
mjr 77:0b96f6867312 6518 // reboot. Some PCs won't reconnect if we were left plugged in
mjr 77:0b96f6867312 6519 // during a power cycle on the PC, but fortunately a reboot on
mjr 77:0b96f6867312 6520 // the KL25Z will make the host notice us and trigger a reconnect.
mjr 86:e30a1f60f783 6521 // Don't do this if we're in a non-recoverable PSU2 power state.
mjr 70:9f58735a1732 6522 if (cfg.disconnectRebootTimeout != 0
mjr 86:e30a1f60f783 6523 && connTimeoutTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 6524 && powerStatusAllowsReboot())
mjr 70:9f58735a1732 6525 reboot(js, false, 0);
mjr 77:0b96f6867312 6526
mjr 77:0b96f6867312 6527 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6528 powerStatusUpdate(cfg);
mjr 51:57eb311faafa 6529 }
mjr 60:f38da020aa13 6530
mjr 60:f38da020aa13 6531 // we're now connected to the host
mjr 54:fd77a6b2f76c 6532 connected = true;
mjr 40:cc0d9814522b 6533
mjr 92:f264fbaa1be5 6534 // Set up a timer for keeping track of how long it's been since we
mjr 92:f264fbaa1be5 6535 // sent the last joystick report. We use this to determine when it's
mjr 92:f264fbaa1be5 6536 // time to send the next joystick report.
mjr 92:f264fbaa1be5 6537 //
mjr 92:f264fbaa1be5 6538 // We have to use a timer for two reasons. The first is that our main
mjr 92:f264fbaa1be5 6539 // loop runs too fast (about .25ms to 2.5ms per loop, depending on the
mjr 92:f264fbaa1be5 6540 // type of plunger sensor attached and other factors) for us to send
mjr 92:f264fbaa1be5 6541 // joystick reports on every iteration. We *could*, but the PC couldn't
mjr 92:f264fbaa1be5 6542 // digest them at that pace. So we need to slow down the reports to a
mjr 92:f264fbaa1be5 6543 // reasonable pace. The second is that VP has some complicated timing
mjr 92:f264fbaa1be5 6544 // issues of its own, so we not only need to slow down the reports from
mjr 92:f264fbaa1be5 6545 // our "natural" pace, but also time them to sync up with VP's input
mjr 92:f264fbaa1be5 6546 // sampling rate as best we can.
mjr 38:091e511ce8a0 6547 Timer jsReportTimer;
mjr 38:091e511ce8a0 6548 jsReportTimer.start();
mjr 38:091e511ce8a0 6549
mjr 92:f264fbaa1be5 6550 // Accelerometer sample "stutter" counter. Each time we send a joystick
mjr 92:f264fbaa1be5 6551 // report, we increment this counter, and check to see if it has reached
mjr 92:f264fbaa1be5 6552 // the threshold set in the configuration. If so, we take a new
mjr 92:f264fbaa1be5 6553 // accelerometer sample and send it with the new joystick report. It
mjr 92:f264fbaa1be5 6554 // not, we don't take a new sample, but simply repeat the last sample.
mjr 92:f264fbaa1be5 6555 //
mjr 92:f264fbaa1be5 6556 // This lets us send joystick reports more frequently than accelerometer
mjr 92:f264fbaa1be5 6557 // samples. The point is to let us slow down accelerometer reports to
mjr 92:f264fbaa1be5 6558 // a pace that matches VP's input sampling frequency, while still sending
mjr 92:f264fbaa1be5 6559 // joystick button updates more frequently, so that other programs that
mjr 92:f264fbaa1be5 6560 // can read input faster will see button changes with less latency.
mjr 92:f264fbaa1be5 6561 int jsAccelStutterCounter = 0;
mjr 92:f264fbaa1be5 6562
mjr 92:f264fbaa1be5 6563 // Last accelerometer report, in joystick units. We normally report the
mjr 92:f264fbaa1be5 6564 // acceleromter reading via the joystick X and Y axes, per the VP
mjr 92:f264fbaa1be5 6565 // convention. We can alternatively report in the RX and RY axes; this
mjr 92:f264fbaa1be5 6566 // can be set in the configuration.
mjr 92:f264fbaa1be5 6567 int x = 0, y = 0;
mjr 92:f264fbaa1be5 6568
mjr 60:f38da020aa13 6569 // Time since we successfully sent a USB report. This is a hacky
mjr 60:f38da020aa13 6570 // workaround to deal with any remaining sporadic problems in the USB
mjr 60:f38da020aa13 6571 // stack. I've been trying to bulletproof the USB code over time to
mjr 60:f38da020aa13 6572 // remove all such problems at their source, but it seems unlikely that
mjr 60:f38da020aa13 6573 // we'll ever get them all. Thus this hack. The idea here is that if
mjr 60:f38da020aa13 6574 // we go too long without successfully sending a USB report, we'll
mjr 60:f38da020aa13 6575 // assume that the connection is broken (and the KL25Z USB hardware
mjr 60:f38da020aa13 6576 // hasn't noticed this), and we'll try taking measures to recover.
mjr 38:091e511ce8a0 6577 Timer jsOKTimer;
mjr 38:091e511ce8a0 6578 jsOKTimer.start();
mjr 35:e959ffba78fd 6579
mjr 55:4db125cd11a0 6580 // Initialize the calibration button and lamp, if enabled. To be enabled,
mjr 55:4db125cd11a0 6581 // the pin has to be assigned to something other than NC (0xFF), AND the
mjr 55:4db125cd11a0 6582 // corresponding feature enable flag has to be set.
mjr 55:4db125cd11a0 6583 DigitalIn *calBtn = 0;
mjr 55:4db125cd11a0 6584 DigitalOut *calBtnLed = 0;
mjr 55:4db125cd11a0 6585
mjr 55:4db125cd11a0 6586 // calibration button input - feature flag 0x01
mjr 55:4db125cd11a0 6587 if ((cfg.plunger.cal.features & 0x01) && cfg.plunger.cal.btn != 0xFF)
mjr 55:4db125cd11a0 6588 calBtn = new DigitalIn(wirePinName(cfg.plunger.cal.btn));
mjr 55:4db125cd11a0 6589
mjr 55:4db125cd11a0 6590 // calibration button indicator lamp output - feature flag 0x02
mjr 55:4db125cd11a0 6591 if ((cfg.plunger.cal.features & 0x02) && cfg.plunger.cal.led != 0xFF)
mjr 55:4db125cd11a0 6592 calBtnLed = new DigitalOut(wirePinName(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 6593
mjr 35:e959ffba78fd 6594 // initialize the calibration button
mjr 1:d913e0afb2ac 6595 calBtnTimer.start();
mjr 35:e959ffba78fd 6596 calBtnState = 0;
mjr 1:d913e0afb2ac 6597
mjr 1:d913e0afb2ac 6598 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 6599 Timer hbTimer;
mjr 1:d913e0afb2ac 6600 hbTimer.start();
mjr 1:d913e0afb2ac 6601 int hb = 0;
mjr 5:a70c0bce770d 6602 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 6603
mjr 1:d913e0afb2ac 6604 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 6605 Timer acTimer;
mjr 1:d913e0afb2ac 6606 acTimer.start();
mjr 1:d913e0afb2ac 6607
mjr 0:5acbbe3f4cf4 6608 // create the accelerometer object
mjr 77:0b96f6867312 6609 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS,
mjr 78:1e00b3fa11af 6610 MMA8451_INT_PIN, cfg.accel.range, cfg.accel.autoCenterTime);
mjr 76:7f5912b6340e 6611
mjr 48:058ace2aed1d 6612 // initialize the plunger sensor
mjr 35:e959ffba78fd 6613 plungerSensor->init();
mjr 10:976666ffa4ef 6614
mjr 48:058ace2aed1d 6615 // set up the ZB Launch Ball monitor
mjr 48:058ace2aed1d 6616 ZBLaunchBall zbLaunchBall;
mjr 48:058ace2aed1d 6617
mjr 54:fd77a6b2f76c 6618 // enable the peripheral chips
mjr 54:fd77a6b2f76c 6619 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 6620 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 6621 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6622 hc595->enable(true);
mjr 87:8d35c74403af 6623 if (tlc59116 != 0)
mjr 87:8d35c74403af 6624 tlc59116->enable(true);
mjr 74:822a92bc11d2 6625
mjr 76:7f5912b6340e 6626 // start the LedWiz flash cycle timer
mjr 74:822a92bc11d2 6627 wizCycleTimer.start();
mjr 74:822a92bc11d2 6628
mjr 74:822a92bc11d2 6629 // start the PWM update polling timer
mjr 74:822a92bc11d2 6630 polledPwmTimer.start();
mjr 43:7a6364d82a41 6631
mjr 1:d913e0afb2ac 6632 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 6633 // host requests
mjr 0:5acbbe3f4cf4 6634 for (;;)
mjr 0:5acbbe3f4cf4 6635 {
mjr 74:822a92bc11d2 6636 // start the main loop timer for diagnostic data collection
mjr 76:7f5912b6340e 6637 IF_DIAG(mainLoopTimer.reset(); mainLoopTimer.start();)
mjr 96:68d5621ff49f 6638
mjr 48:058ace2aed1d 6639 // Process incoming reports on the joystick interface. The joystick
mjr 48:058ace2aed1d 6640 // "out" (receive) endpoint is used for LedWiz commands and our
mjr 48:058ace2aed1d 6641 // extended protocol commands. Limit processing time to 5ms to
mjr 48:058ace2aed1d 6642 // ensure we don't starve the input side.
mjr 39:b3815a1c3802 6643 LedWizMsg lwm;
mjr 48:058ace2aed1d 6644 Timer lwt;
mjr 48:058ace2aed1d 6645 lwt.start();
mjr 77:0b96f6867312 6646 IF_DIAG(int msgCount = 0;)
mjr 48:058ace2aed1d 6647 while (js.readLedWizMsg(lwm) && lwt.read_us() < 5000)
mjr 74:822a92bc11d2 6648 {
mjr 78:1e00b3fa11af 6649 handleInputMsg(lwm, js, accel);
mjr 74:822a92bc11d2 6650 IF_DIAG(++msgCount;)
mjr 74:822a92bc11d2 6651 }
mjr 74:822a92bc11d2 6652
mjr 74:822a92bc11d2 6653 // collect performance statistics on the message reader, if desired
mjr 74:822a92bc11d2 6654 IF_DIAG(
mjr 74:822a92bc11d2 6655 if (msgCount != 0)
mjr 74:822a92bc11d2 6656 {
mjr 76:7f5912b6340e 6657 mainLoopMsgTime += lwt.read_us();
mjr 74:822a92bc11d2 6658 mainLoopMsgCount++;
mjr 74:822a92bc11d2 6659 }
mjr 74:822a92bc11d2 6660 )
mjr 74:822a92bc11d2 6661
mjr 77:0b96f6867312 6662 // process IR input
mjr 77:0b96f6867312 6663 process_IR(cfg, js);
mjr 77:0b96f6867312 6664
mjr 77:0b96f6867312 6665 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6666 powerStatusUpdate(cfg);
mjr 77:0b96f6867312 6667
mjr 74:822a92bc11d2 6668 // update flashing LedWiz outputs periodically
mjr 74:822a92bc11d2 6669 wizPulse();
mjr 74:822a92bc11d2 6670
mjr 74:822a92bc11d2 6671 // update PWM outputs
mjr 74:822a92bc11d2 6672 pollPwmUpdates();
mjr 77:0b96f6867312 6673
mjr 99:8139b0c274f4 6674 // update Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 6675 LwFlipperLogicOut::poll();
mjr 99:8139b0c274f4 6676 LwChimeLogicOut::poll();
mjr 89:c43cd923401c 6677
mjr 77:0b96f6867312 6678 // poll the accelerometer
mjr 77:0b96f6867312 6679 accel.poll();
mjr 55:4db125cd11a0 6680
mjr 96:68d5621ff49f 6681 // Note the "effective" plunger enabled status. This has two
mjr 96:68d5621ff49f 6682 // components: the explicit "enabled" bit, and the plunger sensor
mjr 96:68d5621ff49f 6683 // type setting. For most purposes, a plunger type of NONE is
mjr 96:68d5621ff49f 6684 // equivalent to disabled. Set this to explicit 0x01 or 0x00
mjr 96:68d5621ff49f 6685 // so that we can OR the bit into status reports.
mjr 96:68d5621ff49f 6686 uint8_t effectivePlungerEnabled = (cfg.plunger.enabled
mjr 96:68d5621ff49f 6687 && cfg.plunger.sensorType != PlungerType_None) ? 0x01 : 0x00;
mjr 96:68d5621ff49f 6688
mjr 76:7f5912b6340e 6689 // collect diagnostic statistics, checkpoint 0
mjr 76:7f5912b6340e 6690 IF_DIAG(mainLoopIterCheckpt[0] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6691
mjr 55:4db125cd11a0 6692 // send TLC5940 data updates if applicable
mjr 55:4db125cd11a0 6693 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6694 tlc5940->send();
mjr 87:8d35c74403af 6695
mjr 87:8d35c74403af 6696 // send TLC59116 data updates
mjr 87:8d35c74403af 6697 if (tlc59116 != 0)
mjr 87:8d35c74403af 6698 tlc59116->send();
mjr 1:d913e0afb2ac 6699
mjr 76:7f5912b6340e 6700 // collect diagnostic statistics, checkpoint 1
mjr 76:7f5912b6340e 6701 IF_DIAG(mainLoopIterCheckpt[1] += mainLoopTimer.read_us();)
mjr 77:0b96f6867312 6702
mjr 1:d913e0afb2ac 6703 // check for plunger calibration
mjr 17:ab3cec0c8bf4 6704 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 6705 {
mjr 1:d913e0afb2ac 6706 // check the state
mjr 1:d913e0afb2ac 6707 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6708 {
mjr 1:d913e0afb2ac 6709 case 0:
mjr 1:d913e0afb2ac 6710 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 6711 calBtnTimer.reset();
mjr 1:d913e0afb2ac 6712 calBtnState = 1;
mjr 1:d913e0afb2ac 6713 break;
mjr 1:d913e0afb2ac 6714
mjr 1:d913e0afb2ac 6715 case 1:
mjr 1:d913e0afb2ac 6716 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 6717 // passed, start the hold period
mjr 48:058ace2aed1d 6718 if (calBtnTimer.read_us() > 50000)
mjr 1:d913e0afb2ac 6719 calBtnState = 2;
mjr 1:d913e0afb2ac 6720 break;
mjr 1:d913e0afb2ac 6721
mjr 1:d913e0afb2ac 6722 case 2:
mjr 1:d913e0afb2ac 6723 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 6724 // for the entire hold period, move to calibration mode
mjr 48:058ace2aed1d 6725 if (calBtnTimer.read_us() > 2050000)
mjr 1:d913e0afb2ac 6726 {
mjr 1:d913e0afb2ac 6727 // enter calibration mode
mjr 1:d913e0afb2ac 6728 calBtnState = 3;
mjr 9:fd65b0a94720 6729 calBtnTimer.reset();
mjr 35:e959ffba78fd 6730
mjr 44:b5ac89b9cd5d 6731 // begin the plunger calibration limits
mjr 52:8298b2a73eb2 6732 plungerReader.setCalMode(true);
mjr 1:d913e0afb2ac 6733 }
mjr 1:d913e0afb2ac 6734 break;
mjr 2:c174f9ee414a 6735
mjr 2:c174f9ee414a 6736 case 3:
mjr 9:fd65b0a94720 6737 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 6738 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 6739 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 6740 break;
mjr 0:5acbbe3f4cf4 6741 }
mjr 0:5acbbe3f4cf4 6742 }
mjr 1:d913e0afb2ac 6743 else
mjr 1:d913e0afb2ac 6744 {
mjr 2:c174f9ee414a 6745 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 6746 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 6747 // and save the results to flash.
mjr 2:c174f9ee414a 6748 //
mjr 2:c174f9ee414a 6749 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 6750 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 6751 // mode, it simply cancels the attempt.
mjr 48:058ace2aed1d 6752 if (calBtnState == 3 && calBtnTimer.read_us() > 15000000)
mjr 2:c174f9ee414a 6753 {
mjr 2:c174f9ee414a 6754 // exit calibration mode
mjr 1:d913e0afb2ac 6755 calBtnState = 0;
mjr 52:8298b2a73eb2 6756 plungerReader.setCalMode(false);
mjr 2:c174f9ee414a 6757
mjr 6:cc35eb643e8f 6758 // save the updated configuration
mjr 35:e959ffba78fd 6759 cfg.plunger.cal.calibrated = 1;
mjr 86:e30a1f60f783 6760 saveConfigToFlash(0, false);
mjr 2:c174f9ee414a 6761 }
mjr 2:c174f9ee414a 6762 else if (calBtnState != 3)
mjr 2:c174f9ee414a 6763 {
mjr 2:c174f9ee414a 6764 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 6765 calBtnState = 0;
mjr 2:c174f9ee414a 6766 }
mjr 1:d913e0afb2ac 6767 }
mjr 1:d913e0afb2ac 6768
mjr 1:d913e0afb2ac 6769 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 6770 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 6771 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6772 {
mjr 1:d913e0afb2ac 6773 case 2:
mjr 1:d913e0afb2ac 6774 // in the hold period - flash the light
mjr 48:058ace2aed1d 6775 newCalBtnLit = ((calBtnTimer.read_us()/250000) & 1);
mjr 1:d913e0afb2ac 6776 break;
mjr 1:d913e0afb2ac 6777
mjr 1:d913e0afb2ac 6778 case 3:
mjr 1:d913e0afb2ac 6779 // calibration mode - show steady on
mjr 1:d913e0afb2ac 6780 newCalBtnLit = true;
mjr 1:d913e0afb2ac 6781 break;
mjr 1:d913e0afb2ac 6782
mjr 1:d913e0afb2ac 6783 default:
mjr 1:d913e0afb2ac 6784 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 6785 newCalBtnLit = false;
mjr 1:d913e0afb2ac 6786 break;
mjr 1:d913e0afb2ac 6787 }
mjr 3:3514575d4f86 6788
mjr 3:3514575d4f86 6789 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 6790 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 6791 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 6792 {
mjr 1:d913e0afb2ac 6793 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 6794 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 6795 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6796 calBtnLed->write(1);
mjr 38:091e511ce8a0 6797 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 6798 }
mjr 2:c174f9ee414a 6799 else {
mjr 17:ab3cec0c8bf4 6800 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6801 calBtnLed->write(0);
mjr 38:091e511ce8a0 6802 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 6803 }
mjr 1:d913e0afb2ac 6804 }
mjr 35:e959ffba78fd 6805
mjr 76:7f5912b6340e 6806 // collect diagnostic statistics, checkpoint 2
mjr 76:7f5912b6340e 6807 IF_DIAG(mainLoopIterCheckpt[2] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6808
mjr 48:058ace2aed1d 6809 // read the plunger sensor
mjr 48:058ace2aed1d 6810 plungerReader.read();
mjr 48:058ace2aed1d 6811
mjr 76:7f5912b6340e 6812 // collect diagnostic statistics, checkpoint 3
mjr 76:7f5912b6340e 6813 IF_DIAG(mainLoopIterCheckpt[3] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6814
mjr 53:9b2611964afc 6815 // update the ZB Launch Ball status
mjr 53:9b2611964afc 6816 zbLaunchBall.update();
mjr 37:ed52738445fc 6817
mjr 76:7f5912b6340e 6818 // collect diagnostic statistics, checkpoint 4
mjr 76:7f5912b6340e 6819 IF_DIAG(mainLoopIterCheckpt[4] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6820
mjr 53:9b2611964afc 6821 // process button updates
mjr 53:9b2611964afc 6822 processButtons(cfg);
mjr 53:9b2611964afc 6823
mjr 76:7f5912b6340e 6824 // collect diagnostic statistics, checkpoint 5
mjr 76:7f5912b6340e 6825 IF_DIAG(mainLoopIterCheckpt[5] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6826
mjr 38:091e511ce8a0 6827 // send a keyboard report if we have new data
mjr 37:ed52738445fc 6828 if (kbState.changed)
mjr 37:ed52738445fc 6829 {
mjr 38:091e511ce8a0 6830 // send a keyboard report
mjr 37:ed52738445fc 6831 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 6832 kbState.changed = false;
mjr 37:ed52738445fc 6833 }
mjr 38:091e511ce8a0 6834
mjr 38:091e511ce8a0 6835 // likewise for the media controller
mjr 37:ed52738445fc 6836 if (mediaState.changed)
mjr 37:ed52738445fc 6837 {
mjr 38:091e511ce8a0 6838 // send a media report
mjr 37:ed52738445fc 6839 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 6840 mediaState.changed = false;
mjr 37:ed52738445fc 6841 }
mjr 38:091e511ce8a0 6842
mjr 76:7f5912b6340e 6843 // collect diagnostic statistics, checkpoint 6
mjr 76:7f5912b6340e 6844 IF_DIAG(mainLoopIterCheckpt[6] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6845
mjr 38:091e511ce8a0 6846 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 6847 bool jsOK = false;
mjr 55:4db125cd11a0 6848
mjr 55:4db125cd11a0 6849 // figure the current status flags for joystick reports
mjr 77:0b96f6867312 6850 uint16_t statusFlags =
mjr 96:68d5621ff49f 6851 effectivePlungerEnabled // 0x01
mjr 77:0b96f6867312 6852 | nightMode // 0x02
mjr 79:682ae3171a08 6853 | ((psu2_state & 0x07) << 2) // 0x04 0x08 0x10
mjr 79:682ae3171a08 6854 | saveConfigSucceededFlag; // 0x40
mjr 77:0b96f6867312 6855 if (IRLearningMode != 0)
mjr 77:0b96f6867312 6856 statusFlags |= 0x20;
mjr 17:ab3cec0c8bf4 6857
mjr 50:40015764bbe6 6858 // If it's been long enough since our last USB status report, send
mjr 50:40015764bbe6 6859 // the new report. VP only polls for input in 10ms intervals, so
mjr 50:40015764bbe6 6860 // there's no benefit in sending reports more frequently than this.
mjr 50:40015764bbe6 6861 // More frequent reporting would only add USB I/O overhead.
mjr 92:f264fbaa1be5 6862 if (cfg.joystickEnabled && jsReportTimer.read_us() > cfg.jsReportInterval_us)
mjr 17:ab3cec0c8bf4 6863 {
mjr 92:f264fbaa1be5 6864 // Increment the "stutter" counter. If it has reached the
mjr 92:f264fbaa1be5 6865 // stutter threshold, read a new accelerometer sample. If
mjr 92:f264fbaa1be5 6866 // not, repeat the last sample.
mjr 92:f264fbaa1be5 6867 if (++jsAccelStutterCounter >= cfg.accel.stutter)
mjr 92:f264fbaa1be5 6868 {
mjr 92:f264fbaa1be5 6869 // read the accelerometer
mjr 92:f264fbaa1be5 6870 int xa, ya;
mjr 92:f264fbaa1be5 6871 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 6872
mjr 92:f264fbaa1be5 6873 // confine the results to our joystick axis range
mjr 92:f264fbaa1be5 6874 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 92:f264fbaa1be5 6875 if (xa > JOYMAX) xa = JOYMAX;
mjr 92:f264fbaa1be5 6876 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 92:f264fbaa1be5 6877 if (ya > JOYMAX) ya = JOYMAX;
mjr 92:f264fbaa1be5 6878
mjr 92:f264fbaa1be5 6879 // store the updated accelerometer coordinates
mjr 92:f264fbaa1be5 6880 x = xa;
mjr 92:f264fbaa1be5 6881 y = ya;
mjr 92:f264fbaa1be5 6882
mjr 95:8eca8acbb82c 6883 // rotate X and Y according to the device orientation in the cabinet
mjr 95:8eca8acbb82c 6884 accelRotate(x, y);
mjr 95:8eca8acbb82c 6885
mjr 92:f264fbaa1be5 6886 // reset the stutter counter
mjr 92:f264fbaa1be5 6887 jsAccelStutterCounter = 0;
mjr 92:f264fbaa1be5 6888 }
mjr 17:ab3cec0c8bf4 6889
mjr 48:058ace2aed1d 6890 // Report the current plunger position unless the plunger is
mjr 48:058ace2aed1d 6891 // disabled, or the ZB Launch Ball signal is on. In either of
mjr 48:058ace2aed1d 6892 // those cases, just report a constant 0 value. ZB Launch Ball
mjr 48:058ace2aed1d 6893 // temporarily disables mechanical plunger reporting because it
mjr 21:5048e16cc9ef 6894 // tells us that the table has a Launch Ball button instead of
mjr 48:058ace2aed1d 6895 // a traditional plunger, so we don't want to confuse VP with
mjr 48:058ace2aed1d 6896 // regular plunger inputs.
mjr 92:f264fbaa1be5 6897 int zActual = plungerReader.getPosition();
mjr 96:68d5621ff49f 6898 int zReported = (!effectivePlungerEnabled || zbLaunchOn ? 0 : zActual);
mjr 35:e959ffba78fd 6899
mjr 35:e959ffba78fd 6900 // send the joystick report
mjr 92:f264fbaa1be5 6901 jsOK = js.update(x, y, zReported, jsButtons, statusFlags);
mjr 21:5048e16cc9ef 6902
mjr 17:ab3cec0c8bf4 6903 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 6904 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 6905 }
mjr 21:5048e16cc9ef 6906
mjr 52:8298b2a73eb2 6907 // If we're in sensor status mode, report all pixel exposure values
mjr 52:8298b2a73eb2 6908 if (reportPlungerStat)
mjr 10:976666ffa4ef 6909 {
mjr 17:ab3cec0c8bf4 6910 // send the report
mjr 53:9b2611964afc 6911 plungerSensor->sendStatusReport(js, reportPlungerStatFlags, reportPlungerStatTime);
mjr 17:ab3cec0c8bf4 6912
mjr 10:976666ffa4ef 6913 // we have satisfied this request
mjr 52:8298b2a73eb2 6914 reportPlungerStat = false;
mjr 10:976666ffa4ef 6915 }
mjr 10:976666ffa4ef 6916
mjr 35:e959ffba78fd 6917 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 6918 // periodically for the sake of the Windows config tool.
mjr 77:0b96f6867312 6919 if (!cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 21:5048e16cc9ef 6920 {
mjr 55:4db125cd11a0 6921 jsOK = js.updateStatus(statusFlags);
mjr 38:091e511ce8a0 6922 jsReportTimer.reset();
mjr 38:091e511ce8a0 6923 }
mjr 38:091e511ce8a0 6924
mjr 38:091e511ce8a0 6925 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 6926 if (jsOK)
mjr 38:091e511ce8a0 6927 {
mjr 38:091e511ce8a0 6928 jsOKTimer.reset();
mjr 38:091e511ce8a0 6929 jsOKTimer.start();
mjr 21:5048e16cc9ef 6930 }
mjr 21:5048e16cc9ef 6931
mjr 76:7f5912b6340e 6932 // collect diagnostic statistics, checkpoint 7
mjr 76:7f5912b6340e 6933 IF_DIAG(mainLoopIterCheckpt[7] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6934
mjr 6:cc35eb643e8f 6935 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 6936 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 6937 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 6938 #endif
mjr 6:cc35eb643e8f 6939
mjr 33:d832bcab089e 6940 // check for connection status changes
mjr 54:fd77a6b2f76c 6941 bool newConnected = js.isConnected() && !js.isSleeping();
mjr 33:d832bcab089e 6942 if (newConnected != connected)
mjr 33:d832bcab089e 6943 {
mjr 54:fd77a6b2f76c 6944 // give it a moment to stabilize
mjr 40:cc0d9814522b 6945 connectChangeTimer.start();
mjr 55:4db125cd11a0 6946 if (connectChangeTimer.read_us() > 1000000)
mjr 33:d832bcab089e 6947 {
mjr 33:d832bcab089e 6948 // note the new status
mjr 33:d832bcab089e 6949 connected = newConnected;
mjr 40:cc0d9814522b 6950
mjr 40:cc0d9814522b 6951 // done with the change timer for this round - reset it for next time
mjr 40:cc0d9814522b 6952 connectChangeTimer.stop();
mjr 40:cc0d9814522b 6953 connectChangeTimer.reset();
mjr 33:d832bcab089e 6954
mjr 54:fd77a6b2f76c 6955 // if we're newly disconnected, clean up for PC suspend mode or power off
mjr 54:fd77a6b2f76c 6956 if (!connected)
mjr 40:cc0d9814522b 6957 {
mjr 54:fd77a6b2f76c 6958 // turn off all outputs
mjr 33:d832bcab089e 6959 allOutputsOff();
mjr 40:cc0d9814522b 6960
mjr 40:cc0d9814522b 6961 // The KL25Z runs off of USB power, so we might (depending on the PC
mjr 40:cc0d9814522b 6962 // and OS configuration) continue to receive power even when the main
mjr 40:cc0d9814522b 6963 // PC power supply is turned off, such as in soft-off or suspend/sleep
mjr 40:cc0d9814522b 6964 // mode. Any external output controller chips (TLC5940, 74HC595) might
mjr 40:cc0d9814522b 6965 // be powered from the PC power supply directly rather than from our
mjr 40:cc0d9814522b 6966 // USB power, so they might be powered off even when we're still running.
mjr 40:cc0d9814522b 6967 // To ensure cleaner startup when the power comes back on, globally
mjr 40:cc0d9814522b 6968 // disable the outputs. The global disable signals come from GPIO lines
mjr 40:cc0d9814522b 6969 // that remain powered as long as the KL25Z is powered, so these modes
mjr 40:cc0d9814522b 6970 // will apply smoothly across power state transitions in the external
mjr 40:cc0d9814522b 6971 // hardware. That is, when the external chips are powered up, they'll
mjr 40:cc0d9814522b 6972 // see the global disable signals as stable voltage inputs immediately,
mjr 40:cc0d9814522b 6973 // which will cause them to suppress any output triggering. This ensures
mjr 40:cc0d9814522b 6974 // that we don't fire any solenoids or flash any lights spuriously when
mjr 40:cc0d9814522b 6975 // the power first comes on.
mjr 40:cc0d9814522b 6976 if (tlc5940 != 0)
mjr 40:cc0d9814522b 6977 tlc5940->enable(false);
mjr 87:8d35c74403af 6978 if (tlc59116 != 0)
mjr 87:8d35c74403af 6979 tlc59116->enable(false);
mjr 40:cc0d9814522b 6980 if (hc595 != 0)
mjr 40:cc0d9814522b 6981 hc595->enable(false);
mjr 40:cc0d9814522b 6982 }
mjr 33:d832bcab089e 6983 }
mjr 33:d832bcab089e 6984 }
mjr 48:058ace2aed1d 6985
mjr 53:9b2611964afc 6986 // if we have a reboot timer pending, check for completion
mjr 86:e30a1f60f783 6987 if (saveConfigFollowupTimer.isRunning()
mjr 87:8d35c74403af 6988 && saveConfigFollowupTimer.read_us() > saveConfigFollowupTime*1000000UL)
mjr 85:3c28aee81cde 6989 {
mjr 85:3c28aee81cde 6990 // if a reboot is pending, execute it now
mjr 86:e30a1f60f783 6991 if (saveConfigRebootPending)
mjr 82:4f6209cb5c33 6992 {
mjr 86:e30a1f60f783 6993 // Only reboot if the PSU2 power state allows it. If it
mjr 86:e30a1f60f783 6994 // doesn't, suppress the reboot for now, but leave the boot
mjr 86:e30a1f60f783 6995 // flags set so that we keep checking on future rounds.
mjr 86:e30a1f60f783 6996 // That way we should eventually reboot when the power
mjr 86:e30a1f60f783 6997 // status allows it.
mjr 86:e30a1f60f783 6998 if (powerStatusAllowsReboot())
mjr 86:e30a1f60f783 6999 reboot(js);
mjr 82:4f6209cb5c33 7000 }
mjr 85:3c28aee81cde 7001 else
mjr 85:3c28aee81cde 7002 {
mjr 86:e30a1f60f783 7003 // No reboot required. Exit the timed post-save state.
mjr 86:e30a1f60f783 7004
mjr 86:e30a1f60f783 7005 // stop and reset the post-save timer
mjr 86:e30a1f60f783 7006 saveConfigFollowupTimer.stop();
mjr 86:e30a1f60f783 7007 saveConfigFollowupTimer.reset();
mjr 86:e30a1f60f783 7008
mjr 86:e30a1f60f783 7009 // clear the post-save success flag
mjr 86:e30a1f60f783 7010 saveConfigSucceededFlag = 0;
mjr 85:3c28aee81cde 7011 }
mjr 77:0b96f6867312 7012 }
mjr 86:e30a1f60f783 7013
mjr 48:058ace2aed1d 7014 // if we're disconnected, initiate a new connection
mjr 51:57eb311faafa 7015 if (!connected)
mjr 48:058ace2aed1d 7016 {
mjr 54:fd77a6b2f76c 7017 // show USB HAL debug events
mjr 54:fd77a6b2f76c 7018 extern void HAL_DEBUG_PRINTEVENTS(const char *prefix);
mjr 54:fd77a6b2f76c 7019 HAL_DEBUG_PRINTEVENTS(">DISC");
mjr 54:fd77a6b2f76c 7020
mjr 54:fd77a6b2f76c 7021 // show immediate diagnostic feedback
mjr 54:fd77a6b2f76c 7022 js.diagFlash();
mjr 54:fd77a6b2f76c 7023
mjr 54:fd77a6b2f76c 7024 // clear any previous diagnostic LED display
mjr 54:fd77a6b2f76c 7025 diagLED(0, 0, 0);
mjr 51:57eb311faafa 7026
mjr 51:57eb311faafa 7027 // set up a timer to monitor the reboot timeout
mjr 70:9f58735a1732 7028 Timer reconnTimeoutTimer;
mjr 70:9f58735a1732 7029 reconnTimeoutTimer.start();
mjr 48:058ace2aed1d 7030
mjr 54:fd77a6b2f76c 7031 // set up a timer for diagnostic displays
mjr 54:fd77a6b2f76c 7032 Timer diagTimer;
mjr 54:fd77a6b2f76c 7033 diagTimer.reset();
mjr 54:fd77a6b2f76c 7034 diagTimer.start();
mjr 74:822a92bc11d2 7035
mjr 74:822a92bc11d2 7036 // turn off the main loop timer while spinning
mjr 74:822a92bc11d2 7037 IF_DIAG(mainLoopTimer.stop();)
mjr 54:fd77a6b2f76c 7038
mjr 54:fd77a6b2f76c 7039 // loop until we get our connection back
mjr 54:fd77a6b2f76c 7040 while (!js.isConnected() || js.isSleeping())
mjr 51:57eb311faafa 7041 {
mjr 54:fd77a6b2f76c 7042 // try to recover the connection
mjr 54:fd77a6b2f76c 7043 js.recoverConnection();
mjr 54:fd77a6b2f76c 7044
mjr 99:8139b0c274f4 7045 // update Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 7046 LwFlipperLogicOut::poll();
mjr 99:8139b0c274f4 7047 LwChimeLogicOut::poll();
mjr 89:c43cd923401c 7048
mjr 55:4db125cd11a0 7049 // send TLC5940 data if necessary
mjr 55:4db125cd11a0 7050 if (tlc5940 != 0)
mjr 55:4db125cd11a0 7051 tlc5940->send();
mjr 87:8d35c74403af 7052
mjr 87:8d35c74403af 7053 // update TLC59116 outputs
mjr 87:8d35c74403af 7054 if (tlc59116 != 0)
mjr 87:8d35c74403af 7055 tlc59116->send();
mjr 55:4db125cd11a0 7056
mjr 54:fd77a6b2f76c 7057 // show a diagnostic flash every couple of seconds
mjr 54:fd77a6b2f76c 7058 if (diagTimer.read_us() > 2000000)
mjr 51:57eb311faafa 7059 {
mjr 54:fd77a6b2f76c 7060 // flush the USB HAL debug events, if in debug mode
mjr 54:fd77a6b2f76c 7061 HAL_DEBUG_PRINTEVENTS(">NC");
mjr 54:fd77a6b2f76c 7062
mjr 54:fd77a6b2f76c 7063 // show diagnostic feedback
mjr 54:fd77a6b2f76c 7064 js.diagFlash();
mjr 51:57eb311faafa 7065
mjr 51:57eb311faafa 7066 // reset the flash timer
mjr 54:fd77a6b2f76c 7067 diagTimer.reset();
mjr 51:57eb311faafa 7068 }
mjr 51:57eb311faafa 7069
mjr 77:0b96f6867312 7070 // If the disconnect reboot timeout has expired, reboot.
mjr 77:0b96f6867312 7071 // Some PC hosts won't reconnect to a device that's left
mjr 77:0b96f6867312 7072 // plugged in through various events on the PC side, such as
mjr 77:0b96f6867312 7073 // rebooting Windows, cycling power on the PC, or just a lost
mjr 77:0b96f6867312 7074 // USB connection. Rebooting the KL25Z seems to be the most
mjr 77:0b96f6867312 7075 // reliable way to get Windows to notice us again after one
mjr 86:e30a1f60f783 7076 // of these events and make it reconnect. Only reboot if
mjr 86:e30a1f60f783 7077 // the PSU2 power status allows it - if not, skip it on this
mjr 86:e30a1f60f783 7078 // round and keep waiting.
mjr 51:57eb311faafa 7079 if (cfg.disconnectRebootTimeout != 0
mjr 86:e30a1f60f783 7080 && reconnTimeoutTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 7081 && powerStatusAllowsReboot())
mjr 54:fd77a6b2f76c 7082 reboot(js, false, 0);
mjr 77:0b96f6867312 7083
mjr 77:0b96f6867312 7084 // update the PSU2 power sensing status
mjr 77:0b96f6867312 7085 powerStatusUpdate(cfg);
mjr 54:fd77a6b2f76c 7086 }
mjr 54:fd77a6b2f76c 7087
mjr 74:822a92bc11d2 7088 // resume the main loop timer
mjr 74:822a92bc11d2 7089 IF_DIAG(mainLoopTimer.start();)
mjr 74:822a92bc11d2 7090
mjr 54:fd77a6b2f76c 7091 // if we made it out of that loop alive, we're connected again!
mjr 54:fd77a6b2f76c 7092 connected = true;
mjr 54:fd77a6b2f76c 7093 HAL_DEBUG_PRINTEVENTS(">C");
mjr 54:fd77a6b2f76c 7094
mjr 54:fd77a6b2f76c 7095 // Enable peripheral chips and update them with current output data
mjr 54:fd77a6b2f76c 7096 if (tlc5940 != 0)
mjr 55:4db125cd11a0 7097 tlc5940->enable(true);
mjr 87:8d35c74403af 7098 if (tlc59116 != 0)
mjr 87:8d35c74403af 7099 tlc59116->enable(true);
mjr 54:fd77a6b2f76c 7100 if (hc595 != 0)
mjr 54:fd77a6b2f76c 7101 {
mjr 55:4db125cd11a0 7102 hc595->enable(true);
mjr 54:fd77a6b2f76c 7103 hc595->update(true);
mjr 51:57eb311faafa 7104 }
mjr 48:058ace2aed1d 7105 }
mjr 43:7a6364d82a41 7106
mjr 6:cc35eb643e8f 7107 // provide a visual status indication on the on-board LED
mjr 48:058ace2aed1d 7108 if (calBtnState < 2 && hbTimer.read_us() > 1000000)
mjr 1:d913e0afb2ac 7109 {
mjr 54:fd77a6b2f76c 7110 if (jsOKTimer.read_us() > 1000000)
mjr 38:091e511ce8a0 7111 {
mjr 39:b3815a1c3802 7112 // USB freeze - show red/yellow.
mjr 40:cc0d9814522b 7113 //
mjr 54:fd77a6b2f76c 7114 // It's been more than a second since we successfully sent a joystick
mjr 54:fd77a6b2f76c 7115 // update message. This must mean that something's wrong on the USB
mjr 54:fd77a6b2f76c 7116 // connection, even though we haven't detected an outright disconnect.
mjr 54:fd77a6b2f76c 7117 // Show a distinctive diagnostic LED pattern when this occurs.
mjr 38:091e511ce8a0 7118 hb = !hb;
mjr 38:091e511ce8a0 7119 diagLED(1, hb, 0);
mjr 54:fd77a6b2f76c 7120
mjr 54:fd77a6b2f76c 7121 // If the reboot-on-disconnect option is in effect, treat this condition
mjr 54:fd77a6b2f76c 7122 // as equivalent to a disconnect, since something is obviously wrong
mjr 54:fd77a6b2f76c 7123 // with the USB connection.
mjr 54:fd77a6b2f76c 7124 if (cfg.disconnectRebootTimeout != 0)
mjr 54:fd77a6b2f76c 7125 {
mjr 54:fd77a6b2f76c 7126 // The reboot timeout is in effect. If we've been incommunicado for
mjr 54:fd77a6b2f76c 7127 // longer than the timeout, reboot. If we haven't reached the time
mjr 54:fd77a6b2f76c 7128 // limit, keep running for now, and leave the OK timer running so
mjr 86:e30a1f60f783 7129 // that we can continue to monitor this. Only reboot if the PSU2
mjr 86:e30a1f60f783 7130 // power status allows it.
mjr 86:e30a1f60f783 7131 if (jsOKTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 7132 && powerStatusAllowsReboot())
mjr 54:fd77a6b2f76c 7133 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 7134 }
mjr 54:fd77a6b2f76c 7135 else
mjr 54:fd77a6b2f76c 7136 {
mjr 54:fd77a6b2f76c 7137 // There's no reboot timer, so just keep running with the diagnostic
mjr 54:fd77a6b2f76c 7138 // pattern displayed. Since we're not waiting for any other timed
mjr 54:fd77a6b2f76c 7139 // conditions in this state, stop the timer so that it doesn't
mjr 54:fd77a6b2f76c 7140 // overflow if this condition persists for a long time.
mjr 54:fd77a6b2f76c 7141 jsOKTimer.stop();
mjr 54:fd77a6b2f76c 7142 }
mjr 38:091e511ce8a0 7143 }
mjr 73:4e8ce0b18915 7144 else if (psu2_state >= 4)
mjr 73:4e8ce0b18915 7145 {
mjr 73:4e8ce0b18915 7146 // We're in the TV timer countdown. Skip the normal heartbeat
mjr 73:4e8ce0b18915 7147 // flashes and show the TV timer flashes instead.
mjr 73:4e8ce0b18915 7148 diagLED(0, 0, 0);
mjr 73:4e8ce0b18915 7149 }
mjr 96:68d5621ff49f 7150 else if (effectivePlungerEnabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 7151 {
mjr 6:cc35eb643e8f 7152 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 7153 hb = !hb;
mjr 38:091e511ce8a0 7154 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 7155 }
mjr 6:cc35eb643e8f 7156 else
mjr 6:cc35eb643e8f 7157 {
mjr 6:cc35eb643e8f 7158 // connected - flash blue/green
mjr 2:c174f9ee414a 7159 hb = !hb;
mjr 38:091e511ce8a0 7160 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 7161 }
mjr 1:d913e0afb2ac 7162
mjr 1:d913e0afb2ac 7163 // reset the heartbeat timer
mjr 1:d913e0afb2ac 7164 hbTimer.reset();
mjr 5:a70c0bce770d 7165 ++hbcnt;
mjr 1:d913e0afb2ac 7166 }
mjr 74:822a92bc11d2 7167
mjr 74:822a92bc11d2 7168 // collect statistics on the main loop time, if desired
mjr 74:822a92bc11d2 7169 IF_DIAG(
mjr 76:7f5912b6340e 7170 mainLoopIterTime += mainLoopTimer.read_us();
mjr 74:822a92bc11d2 7171 mainLoopIterCount++;
mjr 74:822a92bc11d2 7172 )
mjr 1:d913e0afb2ac 7173 }
mjr 0:5acbbe3f4cf4 7174 }