Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Mon Feb 22 06:57:59 2021 +0000
Revision:
111:42dc75fbe623
Parent:
109:310ac82cbbee
Child:
112:8ed709f455c0
Add initial support for VCNL4010 IR distance sensor -  experimental and untested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 111:42dc75fbe623 1 /* Copyright 2014, 2021 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 111:42dc75fbe623 69 // - TCD1103 optical linear imaging array. This is a CCD-based optical
mjr 111:42dc75fbe623 70 // imaging sensor, essentially an optical camera sensor, with a linear
mjr 111:42dc75fbe623 71 // (single-row) pixel file. This is similar to the venerable TSL1410R,
mjr 111:42dc75fbe623 72 // the original Pinscape plunger sensor. By arranging the sensor's
mjr 111:42dc75fbe623 73 // linear pixel array parallel to the plunger's axis of travel, we can
mjr 111:42dc75fbe623 74 // use the sensor to take pictures of the plunger, and then analyze the
mjr 111:42dc75fbe623 75 // images in software to determine the position by looking for the edge
mjr 111:42dc75fbe623 76 // between the tip of the plunger and the background. The TCD1103 is
mjr 111:42dc75fbe623 77 // produces low-noise images with 1500 pixels of resolution, and with
mjr 111:42dc75fbe623 78 // a small focusing lens, the software can reliably determine the
mjr 111:42dc75fbe623 79 // plunger position to a single pixel, which translates to about
mjr 111:42dc75fbe623 80 // 1/400" precision. The sensor can take these images (and we can
mjr 111:42dc75fbe623 81 // analyze them) at about 400 frames per second. Between the high
mjr 111:42dc75fbe623 82 // spatial resolution and fast update rate, this is the best sensor
mjr 111:42dc75fbe623 83 // I've found for this job.
mjr 111:42dc75fbe623 84 //
mjr 111:42dc75fbe623 85 // - VCNL4010 IR proximity sensor. This is an optical distance sensor that
mjr 111:42dc75fbe623 86 // estimates the distance to a target by measuring the intensity of a
mjr 111:42dc75fbe623 87 // reflected IR light signal that the sensor bounces off the target.
mjr 111:42dc75fbe623 88 // This is the sensor that's used in the commercial VirtuaPin "v3"
mjr 111:42dc75fbe623 89 // plunger kit. Since the VirtuaPin kit also uses a KL25Z as its
mjr 111:42dc75fbe623 90 // microcontroller, some users of that product have asked for support
mjr 111:42dc75fbe623 91 // for this sensor in the Pinscape code, so that they have the option
mjr 111:42dc75fbe623 92 // to use their hardware from that kit with the Pinscape software.
mjr 111:42dc75fbe623 93 // IR proximity sensors aren't very accurate or precise, so I don't
mjr 111:42dc75fbe623 94 // recommend it to people setting up a new system from scratch - it's
mjr 111:42dc75fbe623 95 // mostly for people who already have the VirtuaPin kit and don't want
mjr 111:42dc75fbe623 96 // to change their hardware to migrate to Pinscape. However, Adafruit
mjr 111:42dc75fbe623 97 // makes a breakout board for the sensor that you can use to set up a
mjr 111:42dc75fbe623 98 // new system if you want to try it - it only requires a few wires to
mjr 111:42dc75fbe623 99 // connect to the KL25Z. (In fact, it appears that VirtuaPin buys the
mjr 111:42dc75fbe623 100 // Adafruit breakout board and repackages it for their kit, so you'll
mjr 111:42dc75fbe623 101 // be using the same thing that VirtuaPin customers have.)
mjr 111:42dc75fbe623 102 //
mjr 87:8d35c74403af 103 // - VL6108X time-of-flight distance sensor. This is an optical distance
mjr 87:8d35c74403af 104 // sensor that measures the distance to a nearby object (within about 10cm)
mjr 87:8d35c74403af 105 // by measuring the travel time for reflected pulses of light. It's fairly
mjr 87:8d35c74403af 106 // cheap and easy to set up, but I don't recommend it because it has very
mjr 87:8d35c74403af 107 // low precision.
mjr 6:cc35eb643e8f 108 //
mjr 87:8d35c74403af 109 // - TSL1410R/TSL1412R linear array optical sensors. These are large optical
mjr 87:8d35c74403af 110 // sensors with the pixels arranged in a single row. The pixel arrays are
mjr 87:8d35c74403af 111 // large enough on these to cover the travel distance of the plunger, so we
mjr 87:8d35c74403af 112 // can set up the sensor near the plunger in such a way that the plunger
mjr 87:8d35c74403af 113 // casts a shadow on the sensor. We detect the plunger position by finding
mjr 87:8d35c74403af 114 // the edge of the sahdow in the image. The optics for this setup are very
mjr 87:8d35c74403af 115 // simple since we don't need any lenses. This was the first sensor we
mjr 87:8d35c74403af 116 // supported, and works very well, but unfortunately the sensor is difficult
mjr 111:42dc75fbe623 117 // to find now since it's been discontinued by the manufacturer. Happily,
mjr 111:42dc75fbe623 118 // a good alternative is available: the Toshiba TCD1103, which is another
mjr 111:42dc75fbe623 119 // linear imaging sensor that works on a similar principle, but produces
mjr 111:42dc75fbe623 120 // even better results.
mjr 87:8d35c74403af 121 //
mjr 87:8d35c74403af 122 // The v2 Build Guide has details on how to build and configure all of the
mjr 87:8d35c74403af 123 // sensor options.
mjr 87:8d35c74403af 124 //
mjr 87:8d35c74403af 125 // Visual Pinball has built-in support for plunger devices like this one, but
mjr 87:8d35c74403af 126 // some older VP tables (particularly for VP 9) can't use it without some
mjr 87:8d35c74403af 127 // modifications to their scripting. The Build Guide has advice on how to
mjr 87:8d35c74403af 128 // fix up VP tables to add plunger support when necessary.
mjr 5:a70c0bce770d 129 //
mjr 77:0b96f6867312 130 // - Button input wiring. You can assign GPIO ports as inputs for physical
mjr 77:0b96f6867312 131 // pinball-style buttons, such as flipper buttons, a Start button, coin
mjr 77:0b96f6867312 132 // chute switches, tilt bobs, and service panel buttons. You can configure
mjr 77:0b96f6867312 133 // each button input to report a keyboard key or joystick button press to
mjr 77:0b96f6867312 134 // the PC when the physical button is pushed.
mjr 13:72dda449c3c0 135 //
mjr 53:9b2611964afc 136 // - LedWiz emulation. The KL25Z can pretend to be an LedWiz device. This lets
mjr 53:9b2611964afc 137 // you connect feedback devices (lights, solenoids, motors) to GPIO ports on the
mjr 53:9b2611964afc 138 // KL25Z, and lets PC software (such as Visual Pinball) control them during game
mjr 53:9b2611964afc 139 // play to create a more immersive playing experience. The Pinscape software
mjr 53:9b2611964afc 140 // presents itself to the host as an LedWiz device and accepts the full LedWiz
mjr 53:9b2611964afc 141 // command set, so software on the PC designed for real LedWiz'es can control
mjr 53:9b2611964afc 142 // attached devices without any modifications.
mjr 5:a70c0bce770d 143 //
mjr 53:9b2611964afc 144 // Even though the software provides a very thorough LedWiz emulation, the KL25Z
mjr 53:9b2611964afc 145 // GPIO hardware design imposes some serious limitations. The big one is that
mjr 53:9b2611964afc 146 // the KL25Z only has 10 PWM channels, meaning that only 10 ports can have
mjr 53:9b2611964afc 147 // varying-intensity outputs (e.g., for controlling the brightness level of an
mjr 53:9b2611964afc 148 // LED or the speed or a motor). You can control more than 10 output ports, but
mjr 53:9b2611964afc 149 // only 10 can have PWM control; the rest are simple "digital" ports that can only
mjr 53:9b2611964afc 150 // be switched fully on or fully off. The second limitation is that the KL25Z
mjr 53:9b2611964afc 151 // just doesn't have that many GPIO ports overall. There are enough to populate
mjr 53:9b2611964afc 152 // all 32 button inputs OR all 32 LedWiz outputs, but not both. The default is
mjr 53:9b2611964afc 153 // to assign 24 buttons and 22 LedWiz ports; you can change this balance to trade
mjr 53:9b2611964afc 154 // off more outputs for fewer inputs, or vice versa. The third limitation is that
mjr 53:9b2611964afc 155 // the KL25Z GPIO pins have *very* tiny amperage limits - just 4mA, which isn't
mjr 53:9b2611964afc 156 // even enough to control a small LED. So in order to connect any kind of feedback
mjr 53:9b2611964afc 157 // device to an output, you *must* build some external circuitry to boost the
mjr 53:9b2611964afc 158 // current handing. The Build Guide has a reference circuit design for this
mjr 53:9b2611964afc 159 // purpose that's simple and inexpensive to build.
mjr 6:cc35eb643e8f 160 //
mjr 87:8d35c74403af 161 // - Enhanced LedWiz emulation with TLC5940 and/or TLC59116 PWM controller chips.
mjr 87:8d35c74403af 162 // You can attach external PWM chips for controlling device outputs, instead of
mjr 87:8d35c74403af 163 // using (or in addition to) the on-board GPIO ports as described above. The
mjr 87:8d35c74403af 164 // software can control a set of daisy-chained TLC5940 or TLC59116 chips. Each
mjr 87:8d35c74403af 165 // chip provides 16 PWM outputs, so you just need two of them to get the full
mjr 87:8d35c74403af 166 // complement of 32 output ports of a real LedWiz. You can hook up even more,
mjr 87:8d35c74403af 167 // though. Four chips gives you 64 ports, which should be plenty for nearly any
mjr 87:8d35c74403af 168 // virtual pinball project.
mjr 53:9b2611964afc 169 //
mjr 53:9b2611964afc 170 // The Pinscape Expansion Board project (which appeared in early 2016) provides
mjr 53:9b2611964afc 171 // a reference hardware design, with EAGLE circuit board layouts, that takes full
mjr 53:9b2611964afc 172 // advantage of the TLC5940 capability. It lets you create a customized set of
mjr 53:9b2611964afc 173 // outputs with full PWM control and power handling for high-current devices
mjr 87:8d35c74403af 174 // built in to the boards.
mjr 87:8d35c74403af 175 //
mjr 87:8d35c74403af 176 // To accommodate the larger supply of ports possible with the external chips,
mjr 87:8d35c74403af 177 // the controller software provides a custom, extended version of the LedWiz
mjr 87:8d35c74403af 178 // protocol that can handle up to 128 ports. Legacy PC software designed only
mjr 87:8d35c74403af 179 // for the original LedWiz obviously can't use the extended protocol, and thus
mjr 87:8d35c74403af 180 // can't take advantage of its extra capabilities, but the latest version of
mjr 87:8d35c74403af 181 // DOF (DirectOutput Framework) *does* know the new language and can take full
mjr 87:8d35c74403af 182 // advantage. Older software will still work, though - the new extensions are
mjr 87:8d35c74403af 183 // all backwards compatible, so old software that only knows about the original
mjr 87:8d35c74403af 184 // LedWiz protocol will still work, with the limitation that it can only access
mjr 87:8d35c74403af 185 // the first 32 ports. In addition, we provide a replacement LEDWIZ.DLL that
mjr 87:8d35c74403af 186 // creates virtual LedWiz units representing additional ports beyond the first
mjr 87:8d35c74403af 187 // 32. This allows legacy LedWiz client software to address all ports by
mjr 87:8d35c74403af 188 // making them think that you have several physical LedWiz units installed.
mjr 26:cb71c4af2912 189 //
mjr 38:091e511ce8a0 190 // - Night Mode control for output devices. You can connect a switch or button
mjr 38:091e511ce8a0 191 // to the controller to activate "Night Mode", which disables feedback devices
mjr 38:091e511ce8a0 192 // that you designate as noisy. You can designate outputs individually as being
mjr 38:091e511ce8a0 193 // included in this set or not. This is useful if you want to play a game on
mjr 38:091e511ce8a0 194 // your cabinet late at night without waking the kids and annoying the neighbors.
mjr 38:091e511ce8a0 195 //
mjr 38:091e511ce8a0 196 // - TV ON switch. The controller can pulse a relay to turn on your TVs after
mjr 38:091e511ce8a0 197 // power to the cabinet comes on, with a configurable delay timer. This feature
mjr 38:091e511ce8a0 198 // is for TVs that don't turn themselves on automatically when first plugged in.
mjr 38:091e511ce8a0 199 // To use this feature, you have to build some external circuitry to allow the
mjr 77:0b96f6867312 200 // software to sense the power supply status. The Build Guide has details
mjr 77:0b96f6867312 201 // on the necessary circuitry. You can use this to switch your TV on via a
mjr 77:0b96f6867312 202 // hardwired connection to the TV's "on" button, which requires taking the
mjr 77:0b96f6867312 203 // TV apart to gain access to its internal wiring, or optionally via the IR
mjr 77:0b96f6867312 204 // remote control transmitter feature below.
mjr 77:0b96f6867312 205 //
mjr 77:0b96f6867312 206 // - Infrared (IR) remote control receiver and transmitter. You can attach an
mjr 77:0b96f6867312 207 // IR LED and/or an IR sensor (we recommend the TSOP384xx series) to make the
mjr 77:0b96f6867312 208 // KL25Z capable of sending and/or receiving IR remote control signals. This
mjr 77:0b96f6867312 209 // can be used with the TV ON feature above to turn your TV(s) on when the
mjr 77:0b96f6867312 210 // system power comes on by sending the "on" command to them via IR, as though
mjr 77:0b96f6867312 211 // you pressed the "on" button on the remote control. The sensor lets the
mjr 77:0b96f6867312 212 // Pinscape software learn the IR codes from your existing remotes, in the
mjr 77:0b96f6867312 213 // same manner as a handheld universal remote control, and the IR LED lets
mjr 77:0b96f6867312 214 // it transmit learned codes. The sensor can also be used to receive codes
mjr 77:0b96f6867312 215 // during normal operation and turn them into PC keystrokes; this lets you
mjr 77:0b96f6867312 216 // access extra commands on the PC without adding more buttons to your
mjr 77:0b96f6867312 217 // cabinet. The IR LED can also be used to transmit other codes when you
mjr 77:0b96f6867312 218 // press selected cabinet buttons, allowing you to assign cabinet buttons
mjr 77:0b96f6867312 219 // to send IR commands to your cabinet TV or other devices.
mjr 38:091e511ce8a0 220 //
mjr 35:e959ffba78fd 221 //
mjr 35:e959ffba78fd 222 //
mjr 33:d832bcab089e 223 // STATUS LIGHTS: The on-board LED on the KL25Z flashes to indicate the current
mjr 33:d832bcab089e 224 // device status. The flash patterns are:
mjr 6:cc35eb643e8f 225 //
mjr 48:058ace2aed1d 226 // short yellow flash = waiting to connect
mjr 6:cc35eb643e8f 227 //
mjr 48:058ace2aed1d 228 // short red flash = the connection is suspended (the host is in sleep
mjr 48:058ace2aed1d 229 // or suspend mode, the USB cable is unplugged after a connection
mjr 48:058ace2aed1d 230 // has been established)
mjr 48:058ace2aed1d 231 //
mjr 48:058ace2aed1d 232 // two short red flashes = connection lost (the device should immediately
mjr 48:058ace2aed1d 233 // go back to short-yellow "waiting to reconnect" mode when a connection
mjr 48:058ace2aed1d 234 // is lost, so this display shouldn't normally appear)
mjr 6:cc35eb643e8f 235 //
mjr 38:091e511ce8a0 236 // long red/yellow = USB connection problem. The device still has a USB
mjr 48:058ace2aed1d 237 // connection to the host (or so it appears to the device), but data
mjr 48:058ace2aed1d 238 // transmissions are failing.
mjr 38:091e511ce8a0 239 //
mjr 73:4e8ce0b18915 240 // medium blue flash = TV ON delay timer running. This means that the
mjr 73:4e8ce0b18915 241 // power to the secondary PSU has just been turned on, and the TV ON
mjr 73:4e8ce0b18915 242 // timer is waiting for the configured delay time before pulsing the
mjr 73:4e8ce0b18915 243 // TV power button relay. This is only shown if the TV ON feature is
mjr 73:4e8ce0b18915 244 // enabled.
mjr 73:4e8ce0b18915 245 //
mjr 6:cc35eb643e8f 246 // long yellow/green = everything's working, but the plunger hasn't
mjr 38:091e511ce8a0 247 // been calibrated. Follow the calibration procedure described in
mjr 38:091e511ce8a0 248 // the project documentation. This flash mode won't appear if there's
mjr 38:091e511ce8a0 249 // no plunger sensor configured.
mjr 6:cc35eb643e8f 250 //
mjr 38:091e511ce8a0 251 // alternating blue/green = everything's working normally, and plunger
mjr 38:091e511ce8a0 252 // calibration has been completed (or there's no plunger attached)
mjr 10:976666ffa4ef 253 //
mjr 48:058ace2aed1d 254 // fast red/purple = out of memory. The controller halts and displays
mjr 48:058ace2aed1d 255 // this diagnostic code until you manually reset it. If this happens,
mjr 48:058ace2aed1d 256 // it's probably because the configuration is too complex, in which
mjr 48:058ace2aed1d 257 // case the same error will occur after the reset. If it's stuck
mjr 48:058ace2aed1d 258 // in this cycle, you'll have to restore the default configuration
mjr 48:058ace2aed1d 259 // by re-installing the controller software (the Pinscape .bin file).
mjr 10:976666ffa4ef 260 //
mjr 48:058ace2aed1d 261 //
mjr 48:058ace2aed1d 262 // USB PROTOCOL: Most of our USB messaging is through standard USB HID
mjr 48:058ace2aed1d 263 // classes (joystick, keyboard). We also accept control messages on our
mjr 48:058ace2aed1d 264 // primary HID interface "OUT endpoint" using a custom protocol that's
mjr 48:058ace2aed1d 265 // not defined in any USB standards (we do have to provide a USB HID
mjr 48:058ace2aed1d 266 // Report Descriptor for it, but this just describes the protocol as
mjr 48:058ace2aed1d 267 // opaque vendor-defined bytes). The control protocol incorporates the
mjr 48:058ace2aed1d 268 // LedWiz protocol as a subset, and adds our own private extensions.
mjr 48:058ace2aed1d 269 // For full details, see USBProtocol.h.
mjr 33:d832bcab089e 270
mjr 33:d832bcab089e 271
mjr 0:5acbbe3f4cf4 272 #include "mbed.h"
mjr 6:cc35eb643e8f 273 #include "math.h"
mjr 74:822a92bc11d2 274 #include "diags.h"
mjr 48:058ace2aed1d 275 #include "pinscape.h"
mjr 79:682ae3171a08 276 #include "NewMalloc.h"
mjr 0:5acbbe3f4cf4 277 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 278 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 279 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 280 #include "crc32.h"
mjr 26:cb71c4af2912 281 #include "TLC5940.h"
mjr 87:8d35c74403af 282 #include "TLC59116.h"
mjr 34:6b981a2afab7 283 #include "74HC595.h"
mjr 35:e959ffba78fd 284 #include "nvm.h"
mjr 48:058ace2aed1d 285 #include "TinyDigitalIn.h"
mjr 77:0b96f6867312 286 #include "IRReceiver.h"
mjr 77:0b96f6867312 287 #include "IRTransmitter.h"
mjr 77:0b96f6867312 288 #include "NewPwm.h"
mjr 74:822a92bc11d2 289
mjr 82:4f6209cb5c33 290 // plunger sensors
mjr 82:4f6209cb5c33 291 #include "plunger.h"
mjr 82:4f6209cb5c33 292 #include "edgeSensor.h"
mjr 82:4f6209cb5c33 293 #include "potSensor.h"
mjr 82:4f6209cb5c33 294 #include "quadSensor.h"
mjr 82:4f6209cb5c33 295 #include "nullSensor.h"
mjr 82:4f6209cb5c33 296 #include "barCodeSensor.h"
mjr 82:4f6209cb5c33 297 #include "distanceSensor.h"
mjr 87:8d35c74403af 298 #include "tsl14xxSensor.h"
mjr 100:1ff35c07217c 299 #include "rotarySensor.h"
mjr 100:1ff35c07217c 300 #include "tcd1103Sensor.h"
mjr 82:4f6209cb5c33 301
mjr 2:c174f9ee414a 302
mjr 21:5048e16cc9ef 303 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 304 #include "config.h"
mjr 17:ab3cec0c8bf4 305
mjr 53:9b2611964afc 306
mjr 53:9b2611964afc 307 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 308 //
mjr 53:9b2611964afc 309 // OpenSDA module identifier. This is for the benefit of the Windows
mjr 53:9b2611964afc 310 // configuration tool. When the config tool installs a .bin file onto
mjr 53:9b2611964afc 311 // the KL25Z, it will first find the sentinel string within the .bin file,
mjr 53:9b2611964afc 312 // and patch the "\0" bytes that follow the sentinel string with the
mjr 53:9b2611964afc 313 // OpenSDA module ID data. This allows us to report the OpenSDA
mjr 53:9b2611964afc 314 // identifiers back to the host system via USB, which in turn allows the
mjr 53:9b2611964afc 315 // config tool to figure out which OpenSDA MSD (mass storage device - a
mjr 53:9b2611964afc 316 // virtual disk drive) correlates to which Pinscape controller USB
mjr 53:9b2611964afc 317 // interface.
mjr 53:9b2611964afc 318 //
mjr 53:9b2611964afc 319 // This is only important if multiple Pinscape devices are attached to
mjr 53:9b2611964afc 320 // the same host. There doesn't seem to be any other way to figure out
mjr 53:9b2611964afc 321 // which OpenSDA MSD corresponds to which KL25Z USB interface; the OpenSDA
mjr 53:9b2611964afc 322 // MSD doesn't report the KL25Z CPU ID anywhere, and the KL25Z doesn't
mjr 53:9b2611964afc 323 // have any way to learn about the OpenSDA module it's connected to. The
mjr 53:9b2611964afc 324 // only way to pass this information to the KL25Z side that I can come up
mjr 53:9b2611964afc 325 // with is to have the Windows host embed it in the .bin file before
mjr 53:9b2611964afc 326 // downloading it to the OpenSDA MSD.
mjr 53:9b2611964afc 327 //
mjr 53:9b2611964afc 328 // We initialize the const data buffer (the part after the sentinel string)
mjr 53:9b2611964afc 329 // with all "\0" bytes, so that's what will be in the executable image that
mjr 53:9b2611964afc 330 // comes out of the mbed compiler. If you manually install the resulting
mjr 53:9b2611964afc 331 // .bin file onto the KL25Z (via the Windows desktop, say), the "\0" bytes
mjr 53:9b2611964afc 332 // will stay this way and read as all 0's at run-time. Since a real TUID
mjr 53:9b2611964afc 333 // would never be all 0's, that tells us that we were never patched and
mjr 53:9b2611964afc 334 // thus don't have any information on the OpenSDA module.
mjr 53:9b2611964afc 335 //
mjr 53:9b2611964afc 336 const char *getOpenSDAID()
mjr 53:9b2611964afc 337 {
mjr 53:9b2611964afc 338 #define OPENSDA_PREFIX "///Pinscape.OpenSDA.TUID///"
mjr 53:9b2611964afc 339 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 340 const size_t OpenSDA_prefix_length = sizeof(OPENSDA_PREFIX) - 1;
mjr 53:9b2611964afc 341
mjr 53:9b2611964afc 342 return OpenSDA + OpenSDA_prefix_length;
mjr 53:9b2611964afc 343 }
mjr 53:9b2611964afc 344
mjr 53:9b2611964afc 345 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 346 //
mjr 53:9b2611964afc 347 // Build ID. We use the date and time of compiling the program as a build
mjr 53:9b2611964afc 348 // identifier. It would be a little nicer to use a simple serial number
mjr 53:9b2611964afc 349 // instead, but the mbed platform doesn't have a way to automate that. The
mjr 53:9b2611964afc 350 // timestamp is a pretty good proxy for a serial number in that it will
mjr 53:9b2611964afc 351 // naturally increase on each new build, which is the primary property we
mjr 53:9b2611964afc 352 // want from this.
mjr 53:9b2611964afc 353 //
mjr 53:9b2611964afc 354 // As with the embedded OpenSDA ID, we store the build timestamp with a
mjr 53:9b2611964afc 355 // sentinel string prefix, to allow automated tools to find the static data
mjr 53:9b2611964afc 356 // in the .bin file by searching for the sentinel string. In contrast to
mjr 53:9b2611964afc 357 // the OpenSDA ID, the value we store here is for tools to extract rather
mjr 53:9b2611964afc 358 // than store, since we automatically populate it via the preprocessor
mjr 53:9b2611964afc 359 // macros.
mjr 53:9b2611964afc 360 //
mjr 53:9b2611964afc 361 const char *getBuildID()
mjr 53:9b2611964afc 362 {
mjr 53:9b2611964afc 363 #define BUILDID_PREFIX "///Pinscape.Build.ID///"
mjr 53:9b2611964afc 364 static const char BuildID[] = BUILDID_PREFIX __DATE__ " " __TIME__ "///";
mjr 53:9b2611964afc 365 const size_t BuildID_prefix_length = sizeof(BUILDID_PREFIX) - 1;
mjr 53:9b2611964afc 366
mjr 53:9b2611964afc 367 return BuildID + BuildID_prefix_length;
mjr 53:9b2611964afc 368 }
mjr 53:9b2611964afc 369
mjr 74:822a92bc11d2 370 // --------------------------------------------------------------------------
mjr 74:822a92bc11d2 371 // Main loop iteration timing statistics. Collected only if
mjr 74:822a92bc11d2 372 // ENABLE_DIAGNOSTICS is set in diags.h.
mjr 76:7f5912b6340e 373 #if ENABLE_DIAGNOSTICS
mjr 76:7f5912b6340e 374 uint64_t mainLoopIterTime, mainLoopIterCheckpt[15], mainLoopIterCount;
mjr 76:7f5912b6340e 375 uint64_t mainLoopMsgTime, mainLoopMsgCount;
mjr 76:7f5912b6340e 376 Timer mainLoopTimer;
mjr 76:7f5912b6340e 377 #endif
mjr 76:7f5912b6340e 378
mjr 53:9b2611964afc 379
mjr 5:a70c0bce770d 380 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 381 //
mjr 38:091e511ce8a0 382 // Forward declarations
mjr 38:091e511ce8a0 383 //
mjr 38:091e511ce8a0 384 void setNightMode(bool on);
mjr 38:091e511ce8a0 385 void toggleNightMode();
mjr 38:091e511ce8a0 386
mjr 38:091e511ce8a0 387 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 388 // utilities
mjr 17:ab3cec0c8bf4 389
mjr 77:0b96f6867312 390 // int/float point square of a number
mjr 77:0b96f6867312 391 inline int square(int x) { return x*x; }
mjr 26:cb71c4af2912 392 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 393
mjr 26:cb71c4af2912 394 // floating point rounding
mjr 26:cb71c4af2912 395 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 396
mjr 17:ab3cec0c8bf4 397
mjr 33:d832bcab089e 398 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 399 //
mjr 40:cc0d9814522b 400 // Extended verison of Timer class. This adds the ability to interrogate
mjr 40:cc0d9814522b 401 // the running state.
mjr 40:cc0d9814522b 402 //
mjr 77:0b96f6867312 403 class ExtTimer: public Timer
mjr 40:cc0d9814522b 404 {
mjr 40:cc0d9814522b 405 public:
mjr 77:0b96f6867312 406 ExtTimer() : running(false) { }
mjr 40:cc0d9814522b 407
mjr 40:cc0d9814522b 408 void start() { running = true; Timer::start(); }
mjr 40:cc0d9814522b 409 void stop() { running = false; Timer::stop(); }
mjr 40:cc0d9814522b 410
mjr 40:cc0d9814522b 411 bool isRunning() const { return running; }
mjr 40:cc0d9814522b 412
mjr 40:cc0d9814522b 413 private:
mjr 40:cc0d9814522b 414 bool running;
mjr 40:cc0d9814522b 415 };
mjr 40:cc0d9814522b 416
mjr 53:9b2611964afc 417
mjr 53:9b2611964afc 418 // --------------------------------------------------------------------------
mjr 40:cc0d9814522b 419 //
mjr 33:d832bcab089e 420 // USB product version number
mjr 5:a70c0bce770d 421 //
mjr 47:df7a88cd249c 422 const uint16_t USB_VERSION_NO = 0x000A;
mjr 33:d832bcab089e 423
mjr 33:d832bcab089e 424 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 425 //
mjr 6:cc35eb643e8f 426 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 427 //
mjr 6:cc35eb643e8f 428 #define JOYMAX 4096
mjr 6:cc35eb643e8f 429
mjr 9:fd65b0a94720 430
mjr 17:ab3cec0c8bf4 431 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 432 //
mjr 40:cc0d9814522b 433 // Wire protocol value translations. These translate byte values to and
mjr 40:cc0d9814522b 434 // from the USB protocol to local native format.
mjr 35:e959ffba78fd 435 //
mjr 35:e959ffba78fd 436
mjr 35:e959ffba78fd 437 // unsigned 16-bit integer
mjr 35:e959ffba78fd 438 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 439 {
mjr 35:e959ffba78fd 440 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 441 }
mjr 40:cc0d9814522b 442 inline void ui16Wire(uint8_t *b, uint16_t val)
mjr 40:cc0d9814522b 443 {
mjr 40:cc0d9814522b 444 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 445 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 446 }
mjr 35:e959ffba78fd 447
mjr 35:e959ffba78fd 448 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 449 {
mjr 35:e959ffba78fd 450 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 451 }
mjr 40:cc0d9814522b 452 inline void i16Wire(uint8_t *b, int16_t val)
mjr 40:cc0d9814522b 453 {
mjr 40:cc0d9814522b 454 ui16Wire(b, (uint16_t)val);
mjr 40:cc0d9814522b 455 }
mjr 35:e959ffba78fd 456
mjr 35:e959ffba78fd 457 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 458 {
mjr 35:e959ffba78fd 459 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 460 }
mjr 40:cc0d9814522b 461 inline void ui32Wire(uint8_t *b, uint32_t val)
mjr 40:cc0d9814522b 462 {
mjr 40:cc0d9814522b 463 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 464 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 465 b[2] = (uint8_t)((val >> 16) & 0xff);
mjr 40:cc0d9814522b 466 b[3] = (uint8_t)((val >> 24) & 0xff);
mjr 40:cc0d9814522b 467 }
mjr 35:e959ffba78fd 468
mjr 35:e959ffba78fd 469 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 470 {
mjr 35:e959ffba78fd 471 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 472 }
mjr 35:e959ffba78fd 473
mjr 53:9b2611964afc 474 // Convert "wire" (USB) pin codes to/from PinName values.
mjr 53:9b2611964afc 475 //
mjr 53:9b2611964afc 476 // The internal mbed PinName format is
mjr 53:9b2611964afc 477 //
mjr 53:9b2611964afc 478 // ((port) << PORT_SHIFT) | (pin << 2) // MBED FORMAT
mjr 53:9b2611964afc 479 //
mjr 53:9b2611964afc 480 // where 'port' is 0-4 for Port A to Port E, and 'pin' is
mjr 53:9b2611964afc 481 // 0 to 31. E.g., E31 is (4 << PORT_SHIFT) | (31<<2).
mjr 53:9b2611964afc 482 //
mjr 53:9b2611964afc 483 // We remap this to our more compact wire format where each
mjr 53:9b2611964afc 484 // pin name fits in 8 bits:
mjr 53:9b2611964afc 485 //
mjr 53:9b2611964afc 486 // ((port) << 5) | pin) // WIRE FORMAT
mjr 53:9b2611964afc 487 //
mjr 53:9b2611964afc 488 // E.g., E31 is (4 << 5) | 31.
mjr 53:9b2611964afc 489 //
mjr 53:9b2611964afc 490 // Wire code FF corresponds to PinName NC (not connected).
mjr 53:9b2611964afc 491 //
mjr 53:9b2611964afc 492 inline PinName wirePinName(uint8_t c)
mjr 35:e959ffba78fd 493 {
mjr 53:9b2611964afc 494 if (c == 0xFF)
mjr 53:9b2611964afc 495 return NC; // 0xFF -> NC
mjr 53:9b2611964afc 496 else
mjr 53:9b2611964afc 497 return PinName(
mjr 53:9b2611964afc 498 (int(c & 0xE0) << (PORT_SHIFT - 5)) // top three bits are the port
mjr 53:9b2611964afc 499 | (int(c & 0x1F) << 2)); // bottom five bits are pin
mjr 40:cc0d9814522b 500 }
mjr 40:cc0d9814522b 501 inline void pinNameWire(uint8_t *b, PinName n)
mjr 40:cc0d9814522b 502 {
mjr 53:9b2611964afc 503 *b = PINNAME_TO_WIRE(n);
mjr 35:e959ffba78fd 504 }
mjr 35:e959ffba78fd 505
mjr 35:e959ffba78fd 506
mjr 35:e959ffba78fd 507 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 508 //
mjr 38:091e511ce8a0 509 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 510 //
mjr 38:091e511ce8a0 511 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 512 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 513 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 514 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 515 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 516 // SPI capability.
mjr 38:091e511ce8a0 517 //
mjr 38:091e511ce8a0 518 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 519
mjr 73:4e8ce0b18915 520 // Power on timer state for diagnostics. We flash the blue LED when
mjr 77:0b96f6867312 521 // nothing else is going on. State 0-1 = off, 2-3 = on blue. Also
mjr 77:0b96f6867312 522 // show red when transmitting an LED signal, indicated by state 4.
mjr 73:4e8ce0b18915 523 uint8_t powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 524
mjr 38:091e511ce8a0 525 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 526 // on, and -1 is no change (leaves the current setting intact).
mjr 73:4e8ce0b18915 527 static uint8_t diagLEDState = 0;
mjr 38:091e511ce8a0 528 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 529 {
mjr 73:4e8ce0b18915 530 // remember the new state
mjr 73:4e8ce0b18915 531 diagLEDState = r | (g << 1) | (b << 2);
mjr 73:4e8ce0b18915 532
mjr 73:4e8ce0b18915 533 // if turning everything off, use the power timer state instead,
mjr 73:4e8ce0b18915 534 // applying it to the blue LED
mjr 73:4e8ce0b18915 535 if (diagLEDState == 0)
mjr 77:0b96f6867312 536 {
mjr 77:0b96f6867312 537 b = (powerTimerDiagState == 2 || powerTimerDiagState == 3);
mjr 77:0b96f6867312 538 r = (powerTimerDiagState == 4);
mjr 77:0b96f6867312 539 }
mjr 73:4e8ce0b18915 540
mjr 73:4e8ce0b18915 541 // set the new state
mjr 38:091e511ce8a0 542 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 543 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 544 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 545 }
mjr 38:091e511ce8a0 546
mjr 73:4e8ce0b18915 547 // update the LEDs with the current state
mjr 73:4e8ce0b18915 548 void diagLED(void)
mjr 73:4e8ce0b18915 549 {
mjr 73:4e8ce0b18915 550 diagLED(
mjr 73:4e8ce0b18915 551 diagLEDState & 0x01,
mjr 73:4e8ce0b18915 552 (diagLEDState >> 1) & 0x01,
mjr 77:0b96f6867312 553 (diagLEDState >> 2) & 0x01);
mjr 73:4e8ce0b18915 554 }
mjr 73:4e8ce0b18915 555
mjr 106:e9e3b46132c1 556 // check an output port or pin assignment to see if it conflicts with
mjr 38:091e511ce8a0 557 // an on-board LED segment
mjr 38:091e511ce8a0 558 struct LedSeg
mjr 38:091e511ce8a0 559 {
mjr 38:091e511ce8a0 560 bool r, g, b;
mjr 38:091e511ce8a0 561 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 562
mjr 106:e9e3b46132c1 563 // check an output port to see if it conflicts with one of the LED ports
mjr 38:091e511ce8a0 564 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 565 {
mjr 38:091e511ce8a0 566 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 567 // our on-board LED segments
mjr 38:091e511ce8a0 568 int t = pc.typ;
mjr 38:091e511ce8a0 569 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 106:e9e3b46132c1 570 check(pc.pin);
mjr 106:e9e3b46132c1 571 }
mjr 106:e9e3b46132c1 572
mjr 106:e9e3b46132c1 573 // check a pin to see if it conflicts with one of the diagnostic LED ports
mjr 106:e9e3b46132c1 574 void check(uint8_t pinId)
mjr 106:e9e3b46132c1 575 {
mjr 106:e9e3b46132c1 576 PinName pin = wirePinName(pinId);
mjr 106:e9e3b46132c1 577 if (pin == LED1)
mjr 106:e9e3b46132c1 578 r = true;
mjr 106:e9e3b46132c1 579 else if (pin == LED2)
mjr 106:e9e3b46132c1 580 g = true;
mjr 106:e9e3b46132c1 581 else if (pin == LED3)
mjr 106:e9e3b46132c1 582 b = true;
mjr 38:091e511ce8a0 583 }
mjr 38:091e511ce8a0 584 };
mjr 38:091e511ce8a0 585
mjr 38:091e511ce8a0 586 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 587 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 588 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 589 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 590 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 591 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 592 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 593 {
mjr 38:091e511ce8a0 594 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 595 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 596 LedSeg l;
mjr 38:091e511ce8a0 597 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 598 l.check(cfg.outPort[i]);
mjr 106:e9e3b46132c1 599
mjr 106:e9e3b46132c1 600 // check the button inputs
mjr 106:e9e3b46132c1 601 for (int i = 0 ; i < countof(cfg.button) ; ++i)
mjr 106:e9e3b46132c1 602 l.check(cfg.button[i].pin);
mjr 106:e9e3b46132c1 603
mjr 106:e9e3b46132c1 604 // check plunger inputs
mjr 106:e9e3b46132c1 605 if (cfg.plunger.enabled && cfg.plunger.sensorType != PlungerType_None)
mjr 106:e9e3b46132c1 606 {
mjr 106:e9e3b46132c1 607 for (int i = 0 ; i < countof(cfg.plunger.sensorPin) ; ++i)
mjr 106:e9e3b46132c1 608 l.check(cfg.plunger.sensorPin[i]);
mjr 107:8f3c7aeae7e0 609
mjr 107:8f3c7aeae7e0 610 l.check(cfg.plunger.cal.btn);
mjr 107:8f3c7aeae7e0 611 l.check(cfg.plunger.cal.led);
mjr 106:e9e3b46132c1 612 }
mjr 106:e9e3b46132c1 613
mjr 106:e9e3b46132c1 614 // check the TV ON pin assignments
mjr 106:e9e3b46132c1 615 l.check(cfg.TVON.statusPin);
mjr 106:e9e3b46132c1 616 l.check(cfg.TVON.latchPin);
mjr 106:e9e3b46132c1 617 l.check(cfg.TVON.relayPin);
mjr 106:e9e3b46132c1 618
mjr 106:e9e3b46132c1 619 // check the TLC5940 pins
mjr 106:e9e3b46132c1 620 if (cfg.tlc5940.nchips != 0)
mjr 106:e9e3b46132c1 621 {
mjr 106:e9e3b46132c1 622 l.check(cfg.tlc5940.sin);
mjr 106:e9e3b46132c1 623 l.check(cfg.tlc5940.sclk);
mjr 106:e9e3b46132c1 624 l.check(cfg.tlc5940.xlat);
mjr 106:e9e3b46132c1 625 l.check(cfg.tlc5940.blank);
mjr 106:e9e3b46132c1 626 l.check(cfg.tlc5940.gsclk);
mjr 106:e9e3b46132c1 627 }
mjr 106:e9e3b46132c1 628
mjr 106:e9e3b46132c1 629 // check 74HC595 pin assignments
mjr 106:e9e3b46132c1 630 if (cfg.hc595.nchips != 0)
mjr 106:e9e3b46132c1 631 {
mjr 106:e9e3b46132c1 632 l.check(cfg.hc595.sin);
mjr 106:e9e3b46132c1 633 l.check(cfg.hc595.sclk);
mjr 106:e9e3b46132c1 634 l.check(cfg.hc595.latch);
mjr 106:e9e3b46132c1 635 l.check(cfg.hc595.ena);
mjr 106:e9e3b46132c1 636 }
mjr 106:e9e3b46132c1 637
mjr 106:e9e3b46132c1 638 // check TLC59116 pin assignments
mjr 106:e9e3b46132c1 639 if (cfg.tlc59116.chipMask != 0)
mjr 106:e9e3b46132c1 640 {
mjr 106:e9e3b46132c1 641 l.check(cfg.tlc59116.sda);
mjr 106:e9e3b46132c1 642 l.check(cfg.tlc59116.scl);
mjr 106:e9e3b46132c1 643 l.check(cfg.tlc59116.reset);
mjr 106:e9e3b46132c1 644 }
mjr 106:e9e3b46132c1 645
mjr 106:e9e3b46132c1 646 // check the IR remove control hardware
mjr 106:e9e3b46132c1 647 l.check(cfg.IR.sensor);
mjr 106:e9e3b46132c1 648 l.check(cfg.IR.emitter);
mjr 106:e9e3b46132c1 649
mjr 106:e9e3b46132c1 650 // We now know which segments are taken for other uses and which
mjr 38:091e511ce8a0 651 // are free. Create diagnostic ports for the ones not claimed for
mjr 106:e9e3b46132c1 652 // other purposes.
mjr 38:091e511ce8a0 653 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 654 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 655 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 656 }
mjr 38:091e511ce8a0 657
mjr 38:091e511ce8a0 658
mjr 38:091e511ce8a0 659 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 660 //
mjr 76:7f5912b6340e 661 // LedWiz emulation
mjr 76:7f5912b6340e 662 //
mjr 76:7f5912b6340e 663
mjr 76:7f5912b6340e 664 // LedWiz output states.
mjr 76:7f5912b6340e 665 //
mjr 76:7f5912b6340e 666 // The LedWiz protocol has two separate control axes for each output.
mjr 76:7f5912b6340e 667 // One axis is its on/off state; the other is its "profile" state, which
mjr 76:7f5912b6340e 668 // is either a fixed brightness or a blinking pattern for the light.
mjr 76:7f5912b6340e 669 // The two axes are independent.
mjr 76:7f5912b6340e 670 //
mjr 76:7f5912b6340e 671 // Even though the original LedWiz protocol can only access 32 ports, we
mjr 76:7f5912b6340e 672 // maintain LedWiz state for every port, even if we have more than 32. Our
mjr 76:7f5912b6340e 673 // extended protocol allows the client to send LedWiz-style messages that
mjr 76:7f5912b6340e 674 // control any set of ports. A replacement LEDWIZ.DLL can make a single
mjr 76:7f5912b6340e 675 // Pinscape unit look like multiple virtual LedWiz units to legacy clients,
mjr 76:7f5912b6340e 676 // allowing them to control all of our ports. The clients will still be
mjr 76:7f5912b6340e 677 // using LedWiz-style states to control the ports, so we need to support
mjr 76:7f5912b6340e 678 // the LedWiz scheme with separate on/off and brightness control per port.
mjr 76:7f5912b6340e 679
mjr 76:7f5912b6340e 680 // On/off state for each LedWiz output
mjr 76:7f5912b6340e 681 static uint8_t *wizOn;
mjr 76:7f5912b6340e 682
mjr 76:7f5912b6340e 683 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 76:7f5912b6340e 684 // for each LedWiz output. If the output was last updated through an
mjr 76:7f5912b6340e 685 // LedWiz protocol message, it will have one of these values:
mjr 76:7f5912b6340e 686 //
mjr 76:7f5912b6340e 687 // 0-48 = fixed brightness 0% to 100%
mjr 76:7f5912b6340e 688 // 49 = fixed brightness 100% (equivalent to 48)
mjr 76:7f5912b6340e 689 // 129 = ramp up / ramp down
mjr 76:7f5912b6340e 690 // 130 = flash on / off
mjr 76:7f5912b6340e 691 // 131 = on / ramp down
mjr 76:7f5912b6340e 692 // 132 = ramp up / on
mjr 5:a70c0bce770d 693 //
mjr 76:7f5912b6340e 694 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 76:7f5912b6340e 695 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 76:7f5912b6340e 696 // it, so we need to accept it for compatibility.)
mjr 76:7f5912b6340e 697 static uint8_t *wizVal;
mjr 76:7f5912b6340e 698
mjr 76:7f5912b6340e 699 // Current actual brightness for each output. This is a simple linear
mjr 76:7f5912b6340e 700 // value on a 0..255 scale. This is EITHER the linear brightness computed
mjr 76:7f5912b6340e 701 // from the LedWiz setting for the port, OR the 0..255 value set explicitly
mjr 76:7f5912b6340e 702 // by the extended protocol:
mjr 76:7f5912b6340e 703 //
mjr 76:7f5912b6340e 704 // - If the last command that updated the port was an extended protocol
mjr 76:7f5912b6340e 705 // SET BRIGHTNESS command, this is the value set by that command. In
mjr 76:7f5912b6340e 706 // addition, wizOn[port] is set to 0 if the brightness is 0, 1 otherwise;
mjr 76:7f5912b6340e 707 // and wizVal[port] is set to the brightness rescaled to the 0..48 range
mjr 76:7f5912b6340e 708 // if the brightness is non-zero.
mjr 76:7f5912b6340e 709 //
mjr 76:7f5912b6340e 710 // - If the last command that updated the port was an LedWiz command
mjr 76:7f5912b6340e 711 // (SBA/PBA/SBX/PBX), this contains the brightness value computed from
mjr 76:7f5912b6340e 712 // the combination of wizOn[port] and wizVal[port]. If wizOn[port] is
mjr 76:7f5912b6340e 713 // zero, this is simply 0, otherwise it's wizVal[port] rescaled to the
mjr 76:7f5912b6340e 714 // 0..255 range.
mjr 26:cb71c4af2912 715 //
mjr 76:7f5912b6340e 716 // - For a port set to wizOn[port]=1 and wizVal[port] in 129..132, this is
mjr 76:7f5912b6340e 717 // also updated continuously to reflect the current flashing brightness
mjr 76:7f5912b6340e 718 // level.
mjr 26:cb71c4af2912 719 //
mjr 76:7f5912b6340e 720 static uint8_t *outLevel;
mjr 76:7f5912b6340e 721
mjr 76:7f5912b6340e 722
mjr 76:7f5912b6340e 723 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 76:7f5912b6340e 724 // rate for lights in blinking states. The LedWiz API doesn't document
mjr 76:7f5912b6340e 725 // what the numbers mean in real time units, but by observation, the
mjr 76:7f5912b6340e 726 // "speed" setting represents the period of the flash cycle in 0.25s
mjr 76:7f5912b6340e 727 // units, so speed 1 = 0.25 period = 4Hz, speed 7 = 1.75s period = 0.57Hz.
mjr 76:7f5912b6340e 728 // The period is the full cycle time of the flash waveform.
mjr 76:7f5912b6340e 729 //
mjr 76:7f5912b6340e 730 // Each bank of 32 lights has its independent own pulse rate, so we need
mjr 76:7f5912b6340e 731 // one entry per bank. Each bank has 32 outputs, so we need a total of
mjr 76:7f5912b6340e 732 // ceil(number_of_physical_outputs/32) entries. Note that we could allocate
mjr 76:7f5912b6340e 733 // this dynamically once we know the number of actual outputs, but the
mjr 76:7f5912b6340e 734 // upper limit is low enough that it's more efficient to use a fixed array
mjr 76:7f5912b6340e 735 // at the maximum size.
mjr 76:7f5912b6340e 736 static const int MAX_LW_BANKS = (MAX_OUT_PORTS+31)/32;
mjr 76:7f5912b6340e 737 static uint8_t wizSpeed[MAX_LW_BANKS];
mjr 29:582472d0bc57 738
mjr 26:cb71c4af2912 739 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 740 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 741 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 742 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 743 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 744
mjr 76:7f5912b6340e 745
mjr 76:7f5912b6340e 746 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 747 //
mjr 76:7f5912b6340e 748 // Output Ports
mjr 76:7f5912b6340e 749 //
mjr 76:7f5912b6340e 750 // There are two way to connect outputs. First, you can use the on-board
mjr 76:7f5912b6340e 751 // GPIO ports to implement device outputs: each LedWiz software port is
mjr 76:7f5912b6340e 752 // connected to a physical GPIO pin on the KL25Z. This has some pretty
mjr 76:7f5912b6340e 753 // strict limits, though. The KL25Z only has 10 PWM channels, so only 10
mjr 76:7f5912b6340e 754 // GPIO LedWiz ports can be made dimmable; the rest are strictly on/off.
mjr 76:7f5912b6340e 755 // The KL25Z also simply doesn't have enough exposed GPIO ports overall to
mjr 76:7f5912b6340e 756 // support all of the features the software supports. The software allows
mjr 76:7f5912b6340e 757 // for up to 128 outputs, 48 button inputs, plunger input (requiring 1-5
mjr 76:7f5912b6340e 758 // GPIO pins), and various other external devices. The KL25Z only exposes
mjr 76:7f5912b6340e 759 // about 50 GPIO pins. So if you want to do everything with GPIO ports,
mjr 76:7f5912b6340e 760 // you have to ration pins among features.
mjr 76:7f5912b6340e 761 //
mjr 87:8d35c74403af 762 // To overcome some of these limitations, we also support several external
mjr 76:7f5912b6340e 763 // peripheral controllers that allow adding many more outputs, using only
mjr 87:8d35c74403af 764 // a small number of GPIO pins to interface with the peripherals:
mjr 87:8d35c74403af 765 //
mjr 87:8d35c74403af 766 // - TLC5940 PWM controller chips. Each TLC5940 provides 16 ports with
mjr 87:8d35c74403af 767 // 12-bit PWM, and multiple TLC5940 chips can be daisy-chained. The
mjr 87:8d35c74403af 768 // chips connect via 5 GPIO pins, and since they're daisy-chainable,
mjr 87:8d35c74403af 769 // one set of 5 pins can control any number of the chips. So this chip
mjr 87:8d35c74403af 770 // effectively converts 5 GPIO pins into almost any number of PWM outputs.
mjr 87:8d35c74403af 771 //
mjr 87:8d35c74403af 772 // - TLC59116 PWM controller chips. These are similar to the TLC5940 but
mjr 87:8d35c74403af 773 // a newer generation with an improved design. These use an I2C bus,
mjr 87:8d35c74403af 774 // allowing up to 14 chips to be connected via 3 GPIO pins.
mjr 87:8d35c74403af 775 //
mjr 87:8d35c74403af 776 // - 74HC595 shift register chips. These provide 8 digital (on/off only)
mjr 87:8d35c74403af 777 // outputs per chip. These need 4 GPIO pins, and like the other can be
mjr 87:8d35c74403af 778 // daisy chained to add more outputs without using more GPIO pins. These
mjr 87:8d35c74403af 779 // are advantageous for outputs that don't require PWM, since the data
mjr 87:8d35c74403af 780 // transfer sizes are so much smaller. The expansion boards use these
mjr 87:8d35c74403af 781 // for the chime board outputs.
mjr 76:7f5912b6340e 782 //
mjr 76:7f5912b6340e 783 // Direct GPIO output ports and peripheral controllers can be mixed and
mjr 76:7f5912b6340e 784 // matched in one system. The assignment of pins to ports and the
mjr 76:7f5912b6340e 785 // configuration of peripheral controllers is all handled in the software
mjr 76:7f5912b6340e 786 // setup, so a physical system can be expanded and updated at any time.
mjr 76:7f5912b6340e 787 //
mjr 76:7f5912b6340e 788 // To handle the diversity of output port types, we start with an abstract
mjr 76:7f5912b6340e 789 // base class for outputs. Each type of physical output interface has a
mjr 76:7f5912b6340e 790 // concrete subclass. During initialization, we create the appropriate
mjr 76:7f5912b6340e 791 // subclass for each software port, mapping it to the assigned GPIO pin
mjr 76:7f5912b6340e 792 // or peripheral port. Most of the rest of the software only cares about
mjr 76:7f5912b6340e 793 // the abstract interface, so once the subclassed port objects are set up,
mjr 76:7f5912b6340e 794 // the rest of the system can control the ports without knowing which types
mjr 76:7f5912b6340e 795 // of physical devices they're connected to.
mjr 76:7f5912b6340e 796
mjr 76:7f5912b6340e 797
mjr 26:cb71c4af2912 798 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 799 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 800 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 801 class LwOut
mjr 6:cc35eb643e8f 802 {
mjr 6:cc35eb643e8f 803 public:
mjr 40:cc0d9814522b 804 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 805 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 806 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 807 };
mjr 26:cb71c4af2912 808
mjr 35:e959ffba78fd 809 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 810 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 811 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 812 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 813 // numbering.
mjr 35:e959ffba78fd 814 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 815 {
mjr 33:d832bcab089e 816 public:
mjr 35:e959ffba78fd 817 LwVirtualOut() { }
mjr 40:cc0d9814522b 818 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 819 };
mjr 26:cb71c4af2912 820
mjr 34:6b981a2afab7 821 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 822 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 823 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 824 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 825 {
mjr 34:6b981a2afab7 826 public:
mjr 34:6b981a2afab7 827 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 828 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 829
mjr 34:6b981a2afab7 830 private:
mjr 53:9b2611964afc 831 // underlying physical output
mjr 34:6b981a2afab7 832 LwOut *out;
mjr 34:6b981a2afab7 833 };
mjr 34:6b981a2afab7 834
mjr 53:9b2611964afc 835 // Global ZB Launch Ball state
mjr 53:9b2611964afc 836 bool zbLaunchOn = false;
mjr 53:9b2611964afc 837
mjr 53:9b2611964afc 838 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 839 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 840 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 841 {
mjr 53:9b2611964afc 842 public:
mjr 53:9b2611964afc 843 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 844 virtual void set(uint8_t val)
mjr 53:9b2611964afc 845 {
mjr 53:9b2611964afc 846 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 847 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 848
mjr 53:9b2611964afc 849 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 850 out->set(val);
mjr 53:9b2611964afc 851 }
mjr 53:9b2611964afc 852
mjr 53:9b2611964afc 853 private:
mjr 53:9b2611964afc 854 // underlying physical or virtual output
mjr 53:9b2611964afc 855 LwOut *out;
mjr 53:9b2611964afc 856 };
mjr 53:9b2611964afc 857
mjr 53:9b2611964afc 858
mjr 40:cc0d9814522b 859 // Gamma correction table for 8-bit input values
mjr 87:8d35c74403af 860 static const uint8_t dof_to_gamma_8bit[] = {
mjr 40:cc0d9814522b 861 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 862 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 863 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 864 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 865 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 866 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 867 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 868 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 869 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 870 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 871 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 872 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 873 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 874 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 875 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 876 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 877 };
mjr 40:cc0d9814522b 878
mjr 40:cc0d9814522b 879 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 880 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 881 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 882 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 883 {
mjr 40:cc0d9814522b 884 public:
mjr 40:cc0d9814522b 885 LwGammaOut(LwOut *o) : out(o) { }
mjr 87:8d35c74403af 886 virtual void set(uint8_t val) { out->set(dof_to_gamma_8bit[val]); }
mjr 40:cc0d9814522b 887
mjr 40:cc0d9814522b 888 private:
mjr 40:cc0d9814522b 889 LwOut *out;
mjr 40:cc0d9814522b 890 };
mjr 40:cc0d9814522b 891
mjr 77:0b96f6867312 892 // Global night mode flag. To minimize overhead when reporting
mjr 77:0b96f6867312 893 // the status, we set this to the status report flag bit for
mjr 77:0b96f6867312 894 // night mode, 0x02, when engaged.
mjr 77:0b96f6867312 895 static uint8_t nightMode = 0x00;
mjr 53:9b2611964afc 896
mjr 40:cc0d9814522b 897 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 898 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 899 // mode is engaged.
mjr 40:cc0d9814522b 900 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 901 {
mjr 40:cc0d9814522b 902 public:
mjr 40:cc0d9814522b 903 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 904 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 905
mjr 53:9b2611964afc 906 private:
mjr 53:9b2611964afc 907 LwOut *out;
mjr 53:9b2611964afc 908 };
mjr 53:9b2611964afc 909
mjr 53:9b2611964afc 910 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 911 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 912 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 913 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 914 {
mjr 53:9b2611964afc 915 public:
mjr 53:9b2611964afc 916 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 89:c43cd923401c 917 virtual void set(uint8_t)
mjr 53:9b2611964afc 918 {
mjr 53:9b2611964afc 919 // ignore the host value and simply show the current
mjr 53:9b2611964afc 920 // night mode setting
mjr 53:9b2611964afc 921 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 922 }
mjr 40:cc0d9814522b 923
mjr 40:cc0d9814522b 924 private:
mjr 40:cc0d9814522b 925 LwOut *out;
mjr 40:cc0d9814522b 926 };
mjr 40:cc0d9814522b 927
mjr 26:cb71c4af2912 928
mjr 89:c43cd923401c 929 // Flipper Logic output. This is a filter object that we layer on
mjr 89:c43cd923401c 930 // top of a physical pin output.
mjr 89:c43cd923401c 931 //
mjr 89:c43cd923401c 932 // A Flipper Logic output is effectively a digital output from the
mjr 89:c43cd923401c 933 // client's perspective, in that it ignores the intensity level and
mjr 89:c43cd923401c 934 // only pays attention to the ON/OFF state. 0 is OFF and any other
mjr 89:c43cd923401c 935 // level is ON.
mjr 89:c43cd923401c 936 //
mjr 89:c43cd923401c 937 // In terms of the physical output, though, we do use varying power.
mjr 89:c43cd923401c 938 // It's just that the varying power isn't under the client's control;
mjr 89:c43cd923401c 939 // we control it according to our flipperLogic settings:
mjr 89:c43cd923401c 940 //
mjr 89:c43cd923401c 941 // - When the software port transitions from OFF (0 brightness) to ON
mjr 89:c43cd923401c 942 // (any non-zero brightness level), we set the physical port to 100%
mjr 89:c43cd923401c 943 // power and start a timer.
mjr 89:c43cd923401c 944 //
mjr 89:c43cd923401c 945 // - When the full power time in our flipperLogic settings elapses,
mjr 89:c43cd923401c 946 // if the software port is still ON, we reduce the physical port to
mjr 89:c43cd923401c 947 // the PWM level in our flipperLogic setting.
mjr 89:c43cd923401c 948 //
mjr 89:c43cd923401c 949 class LwFlipperLogicOut: public LwOut
mjr 89:c43cd923401c 950 {
mjr 89:c43cd923401c 951 public:
mjr 89:c43cd923401c 952 // Set up the output. 'params' is the flipperLogic value from
mjr 89:c43cd923401c 953 // the configuration.
mjr 89:c43cd923401c 954 LwFlipperLogicOut(LwOut *o, uint8_t params)
mjr 89:c43cd923401c 955 : out(o), params(params)
mjr 89:c43cd923401c 956 {
mjr 89:c43cd923401c 957 // initially OFF
mjr 89:c43cd923401c 958 state = 0;
mjr 89:c43cd923401c 959 }
mjr 89:c43cd923401c 960
mjr 89:c43cd923401c 961 virtual void set(uint8_t level)
mjr 89:c43cd923401c 962 {
mjr 98:4df3c0f7e707 963 // remember the new nominal level set by the client
mjr 89:c43cd923401c 964 val = level;
mjr 89:c43cd923401c 965
mjr 89:c43cd923401c 966 // update the physical output according to our current timing state
mjr 89:c43cd923401c 967 switch (state)
mjr 89:c43cd923401c 968 {
mjr 89:c43cd923401c 969 case 0:
mjr 89:c43cd923401c 970 // We're currently off. If the new level is non-zero, switch
mjr 89:c43cd923401c 971 // to state 1 (initial full-power interval) and set the requested
mjr 89:c43cd923401c 972 // level. If the new level is zero, we're switching from off to
mjr 89:c43cd923401c 973 // off, so there's no change.
mjr 89:c43cd923401c 974 if (level != 0)
mjr 89:c43cd923401c 975 {
mjr 89:c43cd923401c 976 // switch to state 1 (initial full-power interval)
mjr 89:c43cd923401c 977 state = 1;
mjr 89:c43cd923401c 978
mjr 89:c43cd923401c 979 // set the requested output level - there's no limit during
mjr 89:c43cd923401c 980 // the initial full-power interval, so set the exact level
mjr 89:c43cd923401c 981 // requested
mjr 89:c43cd923401c 982 out->set(level);
mjr 89:c43cd923401c 983
mjr 89:c43cd923401c 984 // add myself to the pending timer list
mjr 89:c43cd923401c 985 pending[nPending++] = this;
mjr 89:c43cd923401c 986
mjr 89:c43cd923401c 987 // note the starting time
mjr 89:c43cd923401c 988 t0 = timer.read_us();
mjr 89:c43cd923401c 989 }
mjr 89:c43cd923401c 990 break;
mjr 89:c43cd923401c 991
mjr 89:c43cd923401c 992 case 1:
mjr 89:c43cd923401c 993 // Initial full-power interval. If the new level is non-zero,
mjr 89:c43cd923401c 994 // simply apply the new level as requested, since there's no
mjr 89:c43cd923401c 995 // limit during this period. If the new level is zero, shut
mjr 89:c43cd923401c 996 // off the output and cancel the pending timer.
mjr 89:c43cd923401c 997 out->set(level);
mjr 89:c43cd923401c 998 if (level == 0)
mjr 89:c43cd923401c 999 {
mjr 89:c43cd923401c 1000 // We're switching off. In state 1, we have a pending timer,
mjr 89:c43cd923401c 1001 // so we need to remove it from the list.
mjr 89:c43cd923401c 1002 for (int i = 0 ; i < nPending ; ++i)
mjr 89:c43cd923401c 1003 {
mjr 89:c43cd923401c 1004 // is this us?
mjr 89:c43cd923401c 1005 if (pending[i] == this)
mjr 89:c43cd923401c 1006 {
mjr 89:c43cd923401c 1007 // remove myself by replacing the slot with the
mjr 89:c43cd923401c 1008 // last list entry
mjr 89:c43cd923401c 1009 pending[i] = pending[--nPending];
mjr 89:c43cd923401c 1010
mjr 89:c43cd923401c 1011 // no need to look any further
mjr 89:c43cd923401c 1012 break;
mjr 89:c43cd923401c 1013 }
mjr 89:c43cd923401c 1014 }
mjr 89:c43cd923401c 1015
mjr 89:c43cd923401c 1016 // switch to state 0 (off)
mjr 89:c43cd923401c 1017 state = 0;
mjr 89:c43cd923401c 1018 }
mjr 89:c43cd923401c 1019 break;
mjr 89:c43cd923401c 1020
mjr 89:c43cd923401c 1021 case 2:
mjr 89:c43cd923401c 1022 // Hold interval. If the new level is zero, switch to state
mjr 89:c43cd923401c 1023 // 0 (off). If the new level is non-zero, stay in the hold
mjr 89:c43cd923401c 1024 // state, and set the new level, applying the hold power setting
mjr 89:c43cd923401c 1025 // as the upper bound.
mjr 89:c43cd923401c 1026 if (level == 0)
mjr 89:c43cd923401c 1027 {
mjr 89:c43cd923401c 1028 // switching off - turn off the physical output
mjr 89:c43cd923401c 1029 out->set(0);
mjr 89:c43cd923401c 1030
mjr 89:c43cd923401c 1031 // go to state 0 (off)
mjr 89:c43cd923401c 1032 state = 0;
mjr 89:c43cd923401c 1033 }
mjr 89:c43cd923401c 1034 else
mjr 89:c43cd923401c 1035 {
mjr 89:c43cd923401c 1036 // staying on - set the new physical output power to the
mjr 89:c43cd923401c 1037 // lower of the requested power and the hold power
mjr 89:c43cd923401c 1038 uint8_t hold = holdPower();
mjr 89:c43cd923401c 1039 out->set(level < hold ? level : hold);
mjr 89:c43cd923401c 1040 }
mjr 89:c43cd923401c 1041 break;
mjr 89:c43cd923401c 1042 }
mjr 89:c43cd923401c 1043 }
mjr 89:c43cd923401c 1044
mjr 89:c43cd923401c 1045 // Class initialization
mjr 89:c43cd923401c 1046 static void classInit(Config &cfg)
mjr 89:c43cd923401c 1047 {
mjr 89:c43cd923401c 1048 // Count the Flipper Logic outputs in the configuration. We
mjr 89:c43cd923401c 1049 // need to allocate enough pending timer list space to accommodate
mjr 89:c43cd923401c 1050 // all of these outputs.
mjr 89:c43cd923401c 1051 int n = 0;
mjr 89:c43cd923401c 1052 for (int i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 89:c43cd923401c 1053 {
mjr 89:c43cd923401c 1054 // if this port is active and marked as Flipper Logic, count it
mjr 89:c43cd923401c 1055 if (cfg.outPort[i].typ != PortTypeDisabled
mjr 89:c43cd923401c 1056 && (cfg.outPort[i].flags & PortFlagFlipperLogic) != 0)
mjr 89:c43cd923401c 1057 ++n;
mjr 89:c43cd923401c 1058 }
mjr 89:c43cd923401c 1059
mjr 89:c43cd923401c 1060 // allocate space for the pending timer list
mjr 89:c43cd923401c 1061 pending = new LwFlipperLogicOut*[n];
mjr 89:c43cd923401c 1062
mjr 89:c43cd923401c 1063 // there's nothing in the pending list yet
mjr 89:c43cd923401c 1064 nPending = 0;
mjr 89:c43cd923401c 1065
mjr 89:c43cd923401c 1066 // Start our shared timer. The epoch is arbitrary, since we only
mjr 89:c43cd923401c 1067 // use it to figure elapsed times.
mjr 89:c43cd923401c 1068 timer.start();
mjr 89:c43cd923401c 1069 }
mjr 89:c43cd923401c 1070
mjr 89:c43cd923401c 1071 // Check for ports with pending timers. The main routine should
mjr 89:c43cd923401c 1072 // call this on each iteration to process our state transitions.
mjr 89:c43cd923401c 1073 static void poll()
mjr 89:c43cd923401c 1074 {
mjr 89:c43cd923401c 1075 // note the current time
mjr 89:c43cd923401c 1076 uint32_t t = timer.read_us();
mjr 89:c43cd923401c 1077
mjr 89:c43cd923401c 1078 // go through the timer list
mjr 89:c43cd923401c 1079 for (int i = 0 ; i < nPending ; )
mjr 89:c43cd923401c 1080 {
mjr 89:c43cd923401c 1081 // get the port
mjr 89:c43cd923401c 1082 LwFlipperLogicOut *port = pending[i];
mjr 89:c43cd923401c 1083
mjr 89:c43cd923401c 1084 // assume we'll keep it
mjr 89:c43cd923401c 1085 bool remove = false;
mjr 89:c43cd923401c 1086
mjr 89:c43cd923401c 1087 // check if the port is still on
mjr 89:c43cd923401c 1088 if (port->state != 0)
mjr 89:c43cd923401c 1089 {
mjr 89:c43cd923401c 1090 // it's still on - check if the initial full power time has elapsed
mjr 89:c43cd923401c 1091 if (uint32_t(t - port->t0) > port->fullPowerTime_us())
mjr 89:c43cd923401c 1092 {
mjr 89:c43cd923401c 1093 // done with the full power interval - switch to hold state
mjr 89:c43cd923401c 1094 port->state = 2;
mjr 89:c43cd923401c 1095
mjr 89:c43cd923401c 1096 // set the physical port to the hold power setting or the
mjr 89:c43cd923401c 1097 // client brightness setting, whichever is lower
mjr 89:c43cd923401c 1098 uint8_t hold = port->holdPower();
mjr 89:c43cd923401c 1099 uint8_t val = port->val;
mjr 89:c43cd923401c 1100 port->out->set(val < hold ? val : hold);
mjr 89:c43cd923401c 1101
mjr 89:c43cd923401c 1102 // we're done with the timer
mjr 89:c43cd923401c 1103 remove = true;
mjr 89:c43cd923401c 1104 }
mjr 89:c43cd923401c 1105 }
mjr 89:c43cd923401c 1106 else
mjr 89:c43cd923401c 1107 {
mjr 89:c43cd923401c 1108 // the port was turned off before the timer expired - remove
mjr 89:c43cd923401c 1109 // it from the timer list
mjr 89:c43cd923401c 1110 remove = true;
mjr 89:c43cd923401c 1111 }
mjr 89:c43cd923401c 1112
mjr 89:c43cd923401c 1113 // if desired, remove the port from the timer list
mjr 89:c43cd923401c 1114 if (remove)
mjr 89:c43cd923401c 1115 {
mjr 89:c43cd923401c 1116 // Remove the list entry by overwriting the slot with
mjr 89:c43cd923401c 1117 // the last entry in the list.
mjr 89:c43cd923401c 1118 pending[i] = pending[--nPending];
mjr 89:c43cd923401c 1119
mjr 89:c43cd923401c 1120 // Note that we don't increment the loop counter, since
mjr 89:c43cd923401c 1121 // we now need to revisit this same slot.
mjr 89:c43cd923401c 1122 }
mjr 89:c43cd923401c 1123 else
mjr 89:c43cd923401c 1124 {
mjr 89:c43cd923401c 1125 // we're keeping this item; move on to the next one
mjr 89:c43cd923401c 1126 ++i;
mjr 89:c43cd923401c 1127 }
mjr 89:c43cd923401c 1128 }
mjr 89:c43cd923401c 1129 }
mjr 89:c43cd923401c 1130
mjr 89:c43cd923401c 1131 protected:
mjr 89:c43cd923401c 1132 // underlying physical output
mjr 89:c43cd923401c 1133 LwOut *out;
mjr 89:c43cd923401c 1134
mjr 89:c43cd923401c 1135 // Timestamp on 'timer' of start of full-power interval. We set this
mjr 89:c43cd923401c 1136 // to the current 'timer' timestamp when entering state 1.
mjr 89:c43cd923401c 1137 uint32_t t0;
mjr 89:c43cd923401c 1138
mjr 89:c43cd923401c 1139 // Nominal output level (brightness) last set by the client. During
mjr 89:c43cd923401c 1140 // the initial full-power interval, we replicate the requested level
mjr 89:c43cd923401c 1141 // exactly on the physical output. During the hold interval, we limit
mjr 89:c43cd923401c 1142 // the physical output to the hold power, but use the caller's value
mjr 89:c43cd923401c 1143 // if it's lower.
mjr 89:c43cd923401c 1144 uint8_t val;
mjr 89:c43cd923401c 1145
mjr 89:c43cd923401c 1146 // Current port state:
mjr 89:c43cd923401c 1147 //
mjr 89:c43cd923401c 1148 // 0 = off
mjr 89:c43cd923401c 1149 // 1 = on at initial full power
mjr 89:c43cd923401c 1150 // 2 = on at hold power
mjr 89:c43cd923401c 1151 uint8_t state;
mjr 89:c43cd923401c 1152
mjr 89:c43cd923401c 1153 // Configuration parameters. The high 4 bits encode the initial full-
mjr 89:c43cd923401c 1154 // power time in 50ms units, starting at 0=50ms. The low 4 bits encode
mjr 89:c43cd923401c 1155 // the hold power (applied after the initial time expires if the output
mjr 89:c43cd923401c 1156 // is still on) in units of 6.66%. The resulting percentage is used
mjr 89:c43cd923401c 1157 // for the PWM duty cycle of the physical output.
mjr 89:c43cd923401c 1158 uint8_t params;
mjr 89:c43cd923401c 1159
mjr 99:8139b0c274f4 1160 // Figure the initial full-power time in microseconds: 50ms * (1+N),
mjr 99:8139b0c274f4 1161 // where N is the high 4 bits of the parameter byte.
mjr 99:8139b0c274f4 1162 inline uint32_t fullPowerTime_us() const { return 50000*(1 + ((params >> 4) & 0x0F)); }
mjr 89:c43cd923401c 1163
mjr 89:c43cd923401c 1164 // Figure the hold power PWM level (0-255)
mjr 89:c43cd923401c 1165 inline uint8_t holdPower() const { return (params & 0x0F) * 17; }
mjr 89:c43cd923401c 1166
mjr 89:c43cd923401c 1167 // Timer. This is a shared timer for all of the FL ports. When we
mjr 89:c43cd923401c 1168 // transition from OFF to ON, we note the current time on this timer
mjr 89:c43cd923401c 1169 // (which runs continuously).
mjr 89:c43cd923401c 1170 static Timer timer;
mjr 89:c43cd923401c 1171
mjr 89:c43cd923401c 1172 // Flipper logic pending timer list. Whenever a flipper logic output
mjr 98:4df3c0f7e707 1173 // transitions from OFF to ON, we add it to this list. We scan the
mjr 98:4df3c0f7e707 1174 // list in our polling routine to find ports that have reached the
mjr 98:4df3c0f7e707 1175 // expiration of their initial full-power intervals.
mjr 89:c43cd923401c 1176 static LwFlipperLogicOut **pending;
mjr 89:c43cd923401c 1177 static uint8_t nPending;
mjr 89:c43cd923401c 1178 };
mjr 89:c43cd923401c 1179
mjr 89:c43cd923401c 1180 // Flipper Logic statics
mjr 89:c43cd923401c 1181 Timer LwFlipperLogicOut::timer;
mjr 89:c43cd923401c 1182 LwFlipperLogicOut **LwFlipperLogicOut::pending;
mjr 89:c43cd923401c 1183 uint8_t LwFlipperLogicOut::nPending;
mjr 99:8139b0c274f4 1184
mjr 99:8139b0c274f4 1185 // Chime Logic. This is a filter output that we layer on a physical
mjr 99:8139b0c274f4 1186 // output to set a minimum and maximum ON time for the output.
mjr 99:8139b0c274f4 1187 class LwChimeLogicOut: public LwOut
mjr 98:4df3c0f7e707 1188 {
mjr 98:4df3c0f7e707 1189 public:
mjr 99:8139b0c274f4 1190 // Set up the output. 'params' encodes the minimum and maximum time.
mjr 99:8139b0c274f4 1191 LwChimeLogicOut(LwOut *o, uint8_t params)
mjr 99:8139b0c274f4 1192 : out(o), params(params)
mjr 98:4df3c0f7e707 1193 {
mjr 98:4df3c0f7e707 1194 // initially OFF
mjr 98:4df3c0f7e707 1195 state = 0;
mjr 98:4df3c0f7e707 1196 }
mjr 98:4df3c0f7e707 1197
mjr 98:4df3c0f7e707 1198 virtual void set(uint8_t level)
mjr 98:4df3c0f7e707 1199 {
mjr 98:4df3c0f7e707 1200 // update the physical output according to our current timing state
mjr 98:4df3c0f7e707 1201 switch (state)
mjr 98:4df3c0f7e707 1202 {
mjr 98:4df3c0f7e707 1203 case 0:
mjr 98:4df3c0f7e707 1204 // We're currently off. If the new level is non-zero, switch
mjr 98:4df3c0f7e707 1205 // to state 1 (initial minimum interval) and set the requested
mjr 98:4df3c0f7e707 1206 // level. If the new level is zero, we're switching from off to
mjr 98:4df3c0f7e707 1207 // off, so there's no change.
mjr 98:4df3c0f7e707 1208 if (level != 0)
mjr 98:4df3c0f7e707 1209 {
mjr 98:4df3c0f7e707 1210 // switch to state 1 (initial minimum interval, port is
mjr 98:4df3c0f7e707 1211 // logically on)
mjr 98:4df3c0f7e707 1212 state = 1;
mjr 98:4df3c0f7e707 1213
mjr 98:4df3c0f7e707 1214 // set the requested output level
mjr 98:4df3c0f7e707 1215 out->set(level);
mjr 98:4df3c0f7e707 1216
mjr 98:4df3c0f7e707 1217 // add myself to the pending timer list
mjr 98:4df3c0f7e707 1218 pending[nPending++] = this;
mjr 98:4df3c0f7e707 1219
mjr 98:4df3c0f7e707 1220 // note the starting time
mjr 98:4df3c0f7e707 1221 t0 = timer.read_us();
mjr 98:4df3c0f7e707 1222 }
mjr 98:4df3c0f7e707 1223 break;
mjr 98:4df3c0f7e707 1224
mjr 98:4df3c0f7e707 1225 case 1: // min ON interval, port on
mjr 98:4df3c0f7e707 1226 case 2: // min ON interval, port off
mjr 98:4df3c0f7e707 1227 // We're in the initial minimum ON interval. If the new power
mjr 98:4df3c0f7e707 1228 // level is non-zero, pass it through to the physical port, since
mjr 98:4df3c0f7e707 1229 // the client is allowed to change the power level during the
mjr 98:4df3c0f7e707 1230 // initial ON interval - they just can't turn it off entirely.
mjr 98:4df3c0f7e707 1231 // Set the state to 1 to indicate that the logical port is on.
mjr 98:4df3c0f7e707 1232 //
mjr 98:4df3c0f7e707 1233 // If the new level is zero, leave the underlying port at its
mjr 98:4df3c0f7e707 1234 // current power level, since we're not allowed to turn it off
mjr 98:4df3c0f7e707 1235 // during this period. Set the state to 2 to indicate that the
mjr 98:4df3c0f7e707 1236 // logical port is off even though the physical port has to stay
mjr 98:4df3c0f7e707 1237 // on for the remainder of the interval.
mjr 98:4df3c0f7e707 1238 if (level != 0)
mjr 98:4df3c0f7e707 1239 {
mjr 98:4df3c0f7e707 1240 // client is leaving the port on - pass through the new
mjr 98:4df3c0f7e707 1241 // power level and set state 1 (logically on)
mjr 98:4df3c0f7e707 1242 out->set(level);
mjr 98:4df3c0f7e707 1243 state = 1;
mjr 98:4df3c0f7e707 1244 }
mjr 98:4df3c0f7e707 1245 else
mjr 98:4df3c0f7e707 1246 {
mjr 98:4df3c0f7e707 1247 // Client is turning off the port - leave the underlying port
mjr 98:4df3c0f7e707 1248 // on at its current level and set state 2 (logically off).
mjr 98:4df3c0f7e707 1249 // When the minimum ON time expires, the polling routine will
mjr 98:4df3c0f7e707 1250 // see that we're logically off and will pass that through to
mjr 98:4df3c0f7e707 1251 // the underlying physical port. Until then, though, we have
mjr 98:4df3c0f7e707 1252 // to leave the physical port on to satisfy the minimum ON
mjr 98:4df3c0f7e707 1253 // time requirement.
mjr 98:4df3c0f7e707 1254 state = 2;
mjr 98:4df3c0f7e707 1255 }
mjr 98:4df3c0f7e707 1256 break;
mjr 98:4df3c0f7e707 1257
mjr 98:4df3c0f7e707 1258 case 3:
mjr 99:8139b0c274f4 1259 // We're after the minimum ON interval and before the maximum
mjr 99:8139b0c274f4 1260 // ON time limit. We can set any new level, including fully off.
mjr 99:8139b0c274f4 1261 // Pass the new power level through to the port.
mjr 98:4df3c0f7e707 1262 out->set(level);
mjr 98:4df3c0f7e707 1263
mjr 98:4df3c0f7e707 1264 // if the port is now off, return to state 0 (OFF)
mjr 98:4df3c0f7e707 1265 if (level == 0)
mjr 99:8139b0c274f4 1266 {
mjr 99:8139b0c274f4 1267 // return to the OFF state
mjr 99:8139b0c274f4 1268 state = 0;
mjr 99:8139b0c274f4 1269
mjr 99:8139b0c274f4 1270 // If we have a timer pending, remove it. A timer will be
mjr 99:8139b0c274f4 1271 // pending if we have a non-infinite maximum on time for the
mjr 99:8139b0c274f4 1272 // port.
mjr 99:8139b0c274f4 1273 for (int i = 0 ; i < nPending ; ++i)
mjr 99:8139b0c274f4 1274 {
mjr 99:8139b0c274f4 1275 // is this us?
mjr 99:8139b0c274f4 1276 if (pending[i] == this)
mjr 99:8139b0c274f4 1277 {
mjr 99:8139b0c274f4 1278 // remove myself by replacing the slot with the
mjr 99:8139b0c274f4 1279 // last list entry
mjr 99:8139b0c274f4 1280 pending[i] = pending[--nPending];
mjr 99:8139b0c274f4 1281
mjr 99:8139b0c274f4 1282 // no need to look any further
mjr 99:8139b0c274f4 1283 break;
mjr 99:8139b0c274f4 1284 }
mjr 99:8139b0c274f4 1285 }
mjr 99:8139b0c274f4 1286 }
mjr 99:8139b0c274f4 1287 break;
mjr 99:8139b0c274f4 1288
mjr 99:8139b0c274f4 1289 case 4:
mjr 99:8139b0c274f4 1290 // We're after the maximum ON time. The physical port stays off
mjr 99:8139b0c274f4 1291 // during this interval, so we don't pass any changes through to
mjr 99:8139b0c274f4 1292 // the physical port. When the client sets the level to 0, we
mjr 99:8139b0c274f4 1293 // turn off the logical port and reset to state 0.
mjr 99:8139b0c274f4 1294 if (level == 0)
mjr 98:4df3c0f7e707 1295 state = 0;
mjr 98:4df3c0f7e707 1296 break;
mjr 98:4df3c0f7e707 1297 }
mjr 98:4df3c0f7e707 1298 }
mjr 98:4df3c0f7e707 1299
mjr 98:4df3c0f7e707 1300 // Class initialization
mjr 98:4df3c0f7e707 1301 static void classInit(Config &cfg)
mjr 98:4df3c0f7e707 1302 {
mjr 98:4df3c0f7e707 1303 // Count the Minimum On Time outputs in the configuration. We
mjr 98:4df3c0f7e707 1304 // need to allocate enough pending timer list space to accommodate
mjr 98:4df3c0f7e707 1305 // all of these outputs.
mjr 98:4df3c0f7e707 1306 int n = 0;
mjr 98:4df3c0f7e707 1307 for (int i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 98:4df3c0f7e707 1308 {
mjr 98:4df3c0f7e707 1309 // if this port is active and marked as Flipper Logic, count it
mjr 98:4df3c0f7e707 1310 if (cfg.outPort[i].typ != PortTypeDisabled
mjr 99:8139b0c274f4 1311 && (cfg.outPort[i].flags & PortFlagChimeLogic) != 0)
mjr 98:4df3c0f7e707 1312 ++n;
mjr 98:4df3c0f7e707 1313 }
mjr 98:4df3c0f7e707 1314
mjr 98:4df3c0f7e707 1315 // allocate space for the pending timer list
mjr 99:8139b0c274f4 1316 pending = new LwChimeLogicOut*[n];
mjr 98:4df3c0f7e707 1317
mjr 98:4df3c0f7e707 1318 // there's nothing in the pending list yet
mjr 98:4df3c0f7e707 1319 nPending = 0;
mjr 98:4df3c0f7e707 1320
mjr 98:4df3c0f7e707 1321 // Start our shared timer. The epoch is arbitrary, since we only
mjr 98:4df3c0f7e707 1322 // use it to figure elapsed times.
mjr 98:4df3c0f7e707 1323 timer.start();
mjr 98:4df3c0f7e707 1324 }
mjr 98:4df3c0f7e707 1325
mjr 98:4df3c0f7e707 1326 // Check for ports with pending timers. The main routine should
mjr 98:4df3c0f7e707 1327 // call this on each iteration to process our state transitions.
mjr 98:4df3c0f7e707 1328 static void poll()
mjr 98:4df3c0f7e707 1329 {
mjr 98:4df3c0f7e707 1330 // note the current time
mjr 98:4df3c0f7e707 1331 uint32_t t = timer.read_us();
mjr 98:4df3c0f7e707 1332
mjr 98:4df3c0f7e707 1333 // go through the timer list
mjr 98:4df3c0f7e707 1334 for (int i = 0 ; i < nPending ; )
mjr 98:4df3c0f7e707 1335 {
mjr 98:4df3c0f7e707 1336 // get the port
mjr 99:8139b0c274f4 1337 LwChimeLogicOut *port = pending[i];
mjr 98:4df3c0f7e707 1338
mjr 98:4df3c0f7e707 1339 // assume we'll keep it
mjr 98:4df3c0f7e707 1340 bool remove = false;
mjr 98:4df3c0f7e707 1341
mjr 99:8139b0c274f4 1342 // check our state
mjr 99:8139b0c274f4 1343 switch (port->state)
mjr 98:4df3c0f7e707 1344 {
mjr 99:8139b0c274f4 1345 case 1: // initial minimum ON time, port logically on
mjr 99:8139b0c274f4 1346 case 2: // initial minimum ON time, port logically off
mjr 99:8139b0c274f4 1347 // check if the minimum ON time has elapsed
mjr 98:4df3c0f7e707 1348 if (uint32_t(t - port->t0) > port->minOnTime_us())
mjr 98:4df3c0f7e707 1349 {
mjr 98:4df3c0f7e707 1350 // This port has completed its initial ON interval, so
mjr 98:4df3c0f7e707 1351 // it advances to the next state.
mjr 98:4df3c0f7e707 1352 if (port->state == 1)
mjr 98:4df3c0f7e707 1353 {
mjr 99:8139b0c274f4 1354 // The port is logically on, so advance to state 3.
mjr 99:8139b0c274f4 1355 // The underlying port is already at its proper level,
mjr 99:8139b0c274f4 1356 // since we pass through non-zero power settings to the
mjr 99:8139b0c274f4 1357 // underlying port throughout the initial minimum time.
mjr 99:8139b0c274f4 1358 // The timer stays active into state 3.
mjr 98:4df3c0f7e707 1359 port->state = 3;
mjr 99:8139b0c274f4 1360
mjr 99:8139b0c274f4 1361 // Special case: maximum on time 0 means "infinite".
mjr 99:8139b0c274f4 1362 // There's no need for a timer in this case; we'll
mjr 99:8139b0c274f4 1363 // just stay in state 3 until the client turns the
mjr 99:8139b0c274f4 1364 // port off.
mjr 99:8139b0c274f4 1365 if (port->maxOnTime_us() == 0)
mjr 99:8139b0c274f4 1366 remove = true;
mjr 98:4df3c0f7e707 1367 }
mjr 98:4df3c0f7e707 1368 else
mjr 98:4df3c0f7e707 1369 {
mjr 98:4df3c0f7e707 1370 // The port was switched off by the client during the
mjr 98:4df3c0f7e707 1371 // minimum ON period. We haven't passed the OFF state
mjr 98:4df3c0f7e707 1372 // to the underlying port yet, because the port has to
mjr 98:4df3c0f7e707 1373 // stay on throughout the minimum ON period. So turn
mjr 98:4df3c0f7e707 1374 // the port off now.
mjr 98:4df3c0f7e707 1375 port->out->set(0);
mjr 98:4df3c0f7e707 1376
mjr 98:4df3c0f7e707 1377 // return to state 0 (OFF)
mjr 98:4df3c0f7e707 1378 port->state = 0;
mjr 99:8139b0c274f4 1379
mjr 99:8139b0c274f4 1380 // we're done with the timer
mjr 99:8139b0c274f4 1381 remove = true;
mjr 98:4df3c0f7e707 1382 }
mjr 99:8139b0c274f4 1383 }
mjr 99:8139b0c274f4 1384 break;
mjr 99:8139b0c274f4 1385
mjr 99:8139b0c274f4 1386 case 3: // between minimum ON time and maximum ON time
mjr 99:8139b0c274f4 1387 // check if the maximum ON time has expired
mjr 99:8139b0c274f4 1388 if (uint32_t(t - port->t0) > port->maxOnTime_us())
mjr 99:8139b0c274f4 1389 {
mjr 99:8139b0c274f4 1390 // The maximum ON time has expired. Turn off the physical
mjr 99:8139b0c274f4 1391 // port.
mjr 99:8139b0c274f4 1392 port->out->set(0);
mjr 98:4df3c0f7e707 1393
mjr 99:8139b0c274f4 1394 // Switch to state 4 (logically ON past maximum time)
mjr 99:8139b0c274f4 1395 port->state = 4;
mjr 99:8139b0c274f4 1396
mjr 99:8139b0c274f4 1397 // Remove the timer on this port. This port simply stays
mjr 99:8139b0c274f4 1398 // in state 4 until the client turns off the port.
mjr 98:4df3c0f7e707 1399 remove = true;
mjr 98:4df3c0f7e707 1400 }
mjr 99:8139b0c274f4 1401 break;
mjr 98:4df3c0f7e707 1402 }
mjr 98:4df3c0f7e707 1403
mjr 98:4df3c0f7e707 1404 // if desired, remove the port from the timer list
mjr 98:4df3c0f7e707 1405 if (remove)
mjr 98:4df3c0f7e707 1406 {
mjr 98:4df3c0f7e707 1407 // Remove the list entry by overwriting the slot with
mjr 98:4df3c0f7e707 1408 // the last entry in the list.
mjr 98:4df3c0f7e707 1409 pending[i] = pending[--nPending];
mjr 98:4df3c0f7e707 1410
mjr 98:4df3c0f7e707 1411 // Note that we don't increment the loop counter, since
mjr 98:4df3c0f7e707 1412 // we now need to revisit this same slot.
mjr 98:4df3c0f7e707 1413 }
mjr 98:4df3c0f7e707 1414 else
mjr 98:4df3c0f7e707 1415 {
mjr 98:4df3c0f7e707 1416 // we're keeping this item; move on to the next one
mjr 98:4df3c0f7e707 1417 ++i;
mjr 98:4df3c0f7e707 1418 }
mjr 98:4df3c0f7e707 1419 }
mjr 98:4df3c0f7e707 1420 }
mjr 98:4df3c0f7e707 1421
mjr 98:4df3c0f7e707 1422 protected:
mjr 98:4df3c0f7e707 1423 // underlying physical output
mjr 98:4df3c0f7e707 1424 LwOut *out;
mjr 98:4df3c0f7e707 1425
mjr 98:4df3c0f7e707 1426 // Timestamp on 'timer' of start of full-power interval. We set this
mjr 98:4df3c0f7e707 1427 // to the current 'timer' timestamp when entering state 1.
mjr 98:4df3c0f7e707 1428 uint32_t t0;
mjr 98:4df3c0f7e707 1429
mjr 98:4df3c0f7e707 1430 // Current port state:
mjr 98:4df3c0f7e707 1431 //
mjr 98:4df3c0f7e707 1432 // 0 = off
mjr 99:8139b0c274f4 1433 // 1 = in initial minimum ON interval, logical port is on
mjr 99:8139b0c274f4 1434 // 2 = in initial minimum ON interval, logical port is off
mjr 99:8139b0c274f4 1435 // 3 = in interval between minimum and maximum ON times
mjr 99:8139b0c274f4 1436 // 4 = after the maximum ON interval
mjr 99:8139b0c274f4 1437 //
mjr 99:8139b0c274f4 1438 // The "logical" on/off state of the port is the state set by the
mjr 99:8139b0c274f4 1439 // client. The "physical" state is the state of the underlying port.
mjr 99:8139b0c274f4 1440 // The relationships between logical and physical port state, and the
mjr 99:8139b0c274f4 1441 // effects of updates by the client, are as follows:
mjr 99:8139b0c274f4 1442 //
mjr 99:8139b0c274f4 1443 // State | Logical | Physical | Client set on | Client set off
mjr 99:8139b0c274f4 1444 // -----------------------------------------------------------
mjr 99:8139b0c274f4 1445 // 0 | Off | Off | phys on, -> 1 | no effect
mjr 99:8139b0c274f4 1446 // 1 | On | On | no effect | -> 2
mjr 99:8139b0c274f4 1447 // 2 | Off | On | -> 1 | no effect
mjr 99:8139b0c274f4 1448 // 3 | On | On | no effect | phys off, -> 0
mjr 99:8139b0c274f4 1449 // 4 | On | On | no effect | phys off, -> 0
mjr 99:8139b0c274f4 1450 //
mjr 99:8139b0c274f4 1451 // The polling routine makes the following transitions when the current
mjr 99:8139b0c274f4 1452 // time limit expires:
mjr 99:8139b0c274f4 1453 //
mjr 99:8139b0c274f4 1454 // 1: at end of minimum ON, -> 3 (or 4 if max == infinity)
mjr 99:8139b0c274f4 1455 // 2: at end of minimum ON, port off, -> 0
mjr 99:8139b0c274f4 1456 // 3: at end of maximum ON, port off, -> 4
mjr 98:4df3c0f7e707 1457 //
mjr 98:4df3c0f7e707 1458 uint8_t state;
mjr 98:4df3c0f7e707 1459
mjr 99:8139b0c274f4 1460 // Configuration parameters byte. This encodes the minimum and maximum
mjr 99:8139b0c274f4 1461 // ON times.
mjr 99:8139b0c274f4 1462 uint8_t params;
mjr 98:4df3c0f7e707 1463
mjr 98:4df3c0f7e707 1464 // Timer. This is a shared timer for all of the minimum ON time ports.
mjr 98:4df3c0f7e707 1465 // When we transition from OFF to ON, we note the current time on this
mjr 98:4df3c0f7e707 1466 // timer to establish the start of our minimum ON period.
mjr 98:4df3c0f7e707 1467 static Timer timer;
mjr 98:4df3c0f7e707 1468
mjr 98:4df3c0f7e707 1469 // translaton table from timing parameter in config to minimum ON time
mjr 98:4df3c0f7e707 1470 static const uint32_t paramToTime_us[];
mjr 98:4df3c0f7e707 1471
mjr 99:8139b0c274f4 1472 // Figure the minimum ON time. The minimum ON time is given by the
mjr 99:8139b0c274f4 1473 // low-order 4 bits of the parameters byte, which serves as an index
mjr 99:8139b0c274f4 1474 // into our time table.
mjr 99:8139b0c274f4 1475 inline uint32_t minOnTime_us() const { return paramToTime_us[params & 0x0F]; }
mjr 99:8139b0c274f4 1476
mjr 99:8139b0c274f4 1477 // Figure the maximum ON time. The maximum time is the high 4 bits
mjr 99:8139b0c274f4 1478 // of the parameters byte. This is an index into our time table, but
mjr 99:8139b0c274f4 1479 // 0 has the special meaning "infinite".
mjr 99:8139b0c274f4 1480 inline uint32_t maxOnTime_us() const { return paramToTime_us[((params >> 4) & 0x0F)]; }
mjr 98:4df3c0f7e707 1481
mjr 98:4df3c0f7e707 1482 // Pending timer list. Whenever one of our ports transitions from OFF
mjr 98:4df3c0f7e707 1483 // to ON, we add it to this list. We scan this list in our polling
mjr 98:4df3c0f7e707 1484 // routine to find ports that have reached the ends of their initial
mjr 98:4df3c0f7e707 1485 // ON intervals.
mjr 99:8139b0c274f4 1486 static LwChimeLogicOut **pending;
mjr 98:4df3c0f7e707 1487 static uint8_t nPending;
mjr 98:4df3c0f7e707 1488 };
mjr 98:4df3c0f7e707 1489
mjr 98:4df3c0f7e707 1490 // Min Time Out statics
mjr 99:8139b0c274f4 1491 Timer LwChimeLogicOut::timer;
mjr 99:8139b0c274f4 1492 LwChimeLogicOut **LwChimeLogicOut::pending;
mjr 99:8139b0c274f4 1493 uint8_t LwChimeLogicOut::nPending;
mjr 99:8139b0c274f4 1494 const uint32_t LwChimeLogicOut::paramToTime_us[] = {
mjr 99:8139b0c274f4 1495 0, // for the max time, this means "infinite"
mjr 98:4df3c0f7e707 1496 1000,
mjr 98:4df3c0f7e707 1497 2000,
mjr 98:4df3c0f7e707 1498 5000,
mjr 98:4df3c0f7e707 1499 10000,
mjr 98:4df3c0f7e707 1500 20000,
mjr 98:4df3c0f7e707 1501 40000,
mjr 98:4df3c0f7e707 1502 80000,
mjr 98:4df3c0f7e707 1503 100000,
mjr 98:4df3c0f7e707 1504 200000,
mjr 98:4df3c0f7e707 1505 300000,
mjr 98:4df3c0f7e707 1506 400000,
mjr 98:4df3c0f7e707 1507 500000,
mjr 98:4df3c0f7e707 1508 600000,
mjr 98:4df3c0f7e707 1509 700000,
mjr 98:4df3c0f7e707 1510 800000
mjr 98:4df3c0f7e707 1511 };
mjr 89:c43cd923401c 1512
mjr 35:e959ffba78fd 1513 //
mjr 35:e959ffba78fd 1514 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 1515 // assignments set in config.h.
mjr 33:d832bcab089e 1516 //
mjr 35:e959ffba78fd 1517 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 1518 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 1519 {
mjr 35:e959ffba78fd 1520 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 1521 {
mjr 53:9b2611964afc 1522 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 1523 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 1524 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 1525 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 1526 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 1527 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 1528 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 1529 }
mjr 35:e959ffba78fd 1530 }
mjr 26:cb71c4af2912 1531
mjr 40:cc0d9814522b 1532 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 1533 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 1534 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 1535 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 1536 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 1537 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 1538 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 1539 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 1540 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 1541 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 1542 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 1543 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 1544 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 1545 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 1546 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 1547 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 1548 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 1549 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 1550 };
mjr 40:cc0d9814522b 1551
mjr 40:cc0d9814522b 1552 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 1553 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 1554 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 1555 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 1556 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 1557 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 1558 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 1559 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 1560 // are always 8 bits.
mjr 40:cc0d9814522b 1561 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 1562 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 1563 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 1564 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 1565 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 1566 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 1567 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 1568 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 1569 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 1570 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 1571 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 1572 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 1573 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 1574 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 1575 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 1576 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 1577 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 1578 };
mjr 40:cc0d9814522b 1579
mjr 26:cb71c4af2912 1580 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 1581 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 1582 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 1583 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 1584 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 1585 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 1586 {
mjr 26:cb71c4af2912 1587 public:
mjr 60:f38da020aa13 1588 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1589 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 1590 {
mjr 26:cb71c4af2912 1591 if (val != prv)
mjr 40:cc0d9814522b 1592 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 1593 }
mjr 60:f38da020aa13 1594 uint8_t idx;
mjr 40:cc0d9814522b 1595 uint8_t prv;
mjr 26:cb71c4af2912 1596 };
mjr 26:cb71c4af2912 1597
mjr 40:cc0d9814522b 1598 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 1599 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 1600 {
mjr 40:cc0d9814522b 1601 public:
mjr 60:f38da020aa13 1602 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1603 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 1604 {
mjr 40:cc0d9814522b 1605 if (val != prv)
mjr 40:cc0d9814522b 1606 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 1607 }
mjr 60:f38da020aa13 1608 uint8_t idx;
mjr 40:cc0d9814522b 1609 uint8_t prv;
mjr 40:cc0d9814522b 1610 };
mjr 40:cc0d9814522b 1611
mjr 87:8d35c74403af 1612 //
mjr 87:8d35c74403af 1613 // TLC59116 interface object
mjr 87:8d35c74403af 1614 //
mjr 87:8d35c74403af 1615 TLC59116 *tlc59116 = 0;
mjr 87:8d35c74403af 1616 void init_tlc59116(Config &cfg)
mjr 87:8d35c74403af 1617 {
mjr 87:8d35c74403af 1618 // Create the interface if any chips are enabled
mjr 87:8d35c74403af 1619 if (cfg.tlc59116.chipMask != 0)
mjr 87:8d35c74403af 1620 {
mjr 87:8d35c74403af 1621 // set up the interface
mjr 87:8d35c74403af 1622 tlc59116 = new TLC59116(
mjr 87:8d35c74403af 1623 wirePinName(cfg.tlc59116.sda),
mjr 87:8d35c74403af 1624 wirePinName(cfg.tlc59116.scl),
mjr 87:8d35c74403af 1625 wirePinName(cfg.tlc59116.reset));
mjr 87:8d35c74403af 1626
mjr 87:8d35c74403af 1627 // initialize the chips
mjr 87:8d35c74403af 1628 tlc59116->init();
mjr 87:8d35c74403af 1629 }
mjr 87:8d35c74403af 1630 }
mjr 87:8d35c74403af 1631
mjr 87:8d35c74403af 1632 // LwOut class for TLC59116 outputs. The 'addr' value in the constructor
mjr 87:8d35c74403af 1633 // is low 4 bits of the chip's I2C address; this is the part of the address
mjr 87:8d35c74403af 1634 // that's configurable per chip. 'port' is the output number on the chip
mjr 87:8d35c74403af 1635 // (0-15).
mjr 87:8d35c74403af 1636 //
mjr 87:8d35c74403af 1637 // Note that we don't need a separate gamma-corrected subclass for this
mjr 87:8d35c74403af 1638 // output type, since there's no loss of precision with the standard layered
mjr 87:8d35c74403af 1639 // gamma (it emits 8-bit values, and we take 8-bit inputs).
mjr 87:8d35c74403af 1640 class Lw59116Out: public LwOut
mjr 87:8d35c74403af 1641 {
mjr 87:8d35c74403af 1642 public:
mjr 87:8d35c74403af 1643 Lw59116Out(uint8_t addr, uint8_t port) : addr(addr), port(port) { prv = 0; }
mjr 87:8d35c74403af 1644 virtual void set(uint8_t val)
mjr 87:8d35c74403af 1645 {
mjr 87:8d35c74403af 1646 if (val != prv)
mjr 87:8d35c74403af 1647 tlc59116->set(addr, port, prv = val);
mjr 87:8d35c74403af 1648 }
mjr 87:8d35c74403af 1649
mjr 87:8d35c74403af 1650 protected:
mjr 87:8d35c74403af 1651 uint8_t addr;
mjr 87:8d35c74403af 1652 uint8_t port;
mjr 87:8d35c74403af 1653 uint8_t prv;
mjr 87:8d35c74403af 1654 };
mjr 87:8d35c74403af 1655
mjr 87:8d35c74403af 1656
mjr 87:8d35c74403af 1657 //
mjr 34:6b981a2afab7 1658 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 1659 // config.h.
mjr 87:8d35c74403af 1660 //
mjr 35:e959ffba78fd 1661 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 1662
mjr 35:e959ffba78fd 1663 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 1664 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 1665 {
mjr 35:e959ffba78fd 1666 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 1667 {
mjr 53:9b2611964afc 1668 hc595 = new HC595(
mjr 53:9b2611964afc 1669 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 1670 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 1671 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 1672 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 1673 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 1674 hc595->init();
mjr 35:e959ffba78fd 1675 hc595->update();
mjr 35:e959ffba78fd 1676 }
mjr 35:e959ffba78fd 1677 }
mjr 34:6b981a2afab7 1678
mjr 34:6b981a2afab7 1679 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 1680 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 1681 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 1682 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 1683 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 1684 class Lw595Out: public LwOut
mjr 33:d832bcab089e 1685 {
mjr 33:d832bcab089e 1686 public:
mjr 60:f38da020aa13 1687 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1688 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 1689 {
mjr 34:6b981a2afab7 1690 if (val != prv)
mjr 40:cc0d9814522b 1691 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 1692 }
mjr 60:f38da020aa13 1693 uint8_t idx;
mjr 40:cc0d9814522b 1694 uint8_t prv;
mjr 33:d832bcab089e 1695 };
mjr 33:d832bcab089e 1696
mjr 26:cb71c4af2912 1697
mjr 40:cc0d9814522b 1698
mjr 64:ef7ca92dff36 1699 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 1700 // normalized to 0.0 to 1.0 scale.
mjr 74:822a92bc11d2 1701 static const float dof_to_pwm[] = {
mjr 64:ef7ca92dff36 1702 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 1703 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 1704 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 1705 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 1706 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 1707 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 1708 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 1709 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 1710 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 1711 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 1712 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 1713 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 1714 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 1715 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 1716 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 1717 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 1718 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 1719 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 1720 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 1721 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 1722 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 1723 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 1724 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 1725 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 1726 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 1727 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 1728 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 1729 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 1730 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 1731 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 1732 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 1733 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 1734 };
mjr 26:cb71c4af2912 1735
mjr 64:ef7ca92dff36 1736
mjr 92:f264fbaa1be5 1737 // Conversion table for 8-bit DOF level to pulse width, with gamma correction
mjr 92:f264fbaa1be5 1738 // pre-calculated. The values are normalized duty cycles from 0.0 to 1.0.
mjr 92:f264fbaa1be5 1739 // Note that we could use the layered gamma output on top of the regular
mjr 92:f264fbaa1be5 1740 // LwPwmOut class for this instead of a separate table, but we get much better
mjr 92:f264fbaa1be5 1741 // precision with a dedicated table, because we apply gamma correction to the
mjr 92:f264fbaa1be5 1742 // actual duty cycle values (as 'float') rather than the 8-bit DOF values.
mjr 64:ef7ca92dff36 1743 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 1744 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 1745 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 1746 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 1747 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 1748 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 1749 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 1750 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 1751 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 1752 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 1753 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 1754 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 1755 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 1756 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 1757 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 1758 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 1759 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 1760 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 1761 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 1762 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 1763 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 1764 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 1765 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 1766 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 1767 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 1768 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 1769 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 1770 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 1771 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 1772 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 1773 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 1774 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 1775 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 1776 };
mjr 64:ef7ca92dff36 1777
mjr 77:0b96f6867312 1778 // Polled-update PWM output list
mjr 74:822a92bc11d2 1779 //
mjr 77:0b96f6867312 1780 // This is a workaround for a KL25Z hardware bug/limitation. The bug (more
mjr 77:0b96f6867312 1781 // about this below) is that we can't write to a PWM output "value" register
mjr 77:0b96f6867312 1782 // more than once per PWM cycle; if we do, outputs after the first are lost.
mjr 77:0b96f6867312 1783 // The value register controls the duty cycle, so it's what you have to write
mjr 77:0b96f6867312 1784 // if you want to update the brightness of an output.
mjr 74:822a92bc11d2 1785 //
mjr 92:f264fbaa1be5 1786 // The symptom of the problem, if it's not worked around somehow, is that
mjr 92:f264fbaa1be5 1787 // an output will get "stuck" due to a missed write. This is especially
mjr 92:f264fbaa1be5 1788 // noticeable during a series of updates such as a fade. If the last
mjr 92:f264fbaa1be5 1789 // couple of updates in a fade are lost, the output will get stuck at some
mjr 92:f264fbaa1be5 1790 // value above or below the desired final value. The stuck setting will
mjr 92:f264fbaa1be5 1791 // persist until the output is deliberately changed again later.
mjr 92:f264fbaa1be5 1792 //
mjr 92:f264fbaa1be5 1793 // Our solution: Simply repeat all PWM updates periodically. This way, any
mjr 92:f264fbaa1be5 1794 // lost write will *eventually* take hold on one of the repeats. Repeats of
mjr 92:f264fbaa1be5 1795 // the same value won't change anything and thus won't be noticeable. We do
mjr 92:f264fbaa1be5 1796 // these periodic updates during the main loop, which makes them very low
mjr 92:f264fbaa1be5 1797 // overhead (there's no interrupt overhead; we just do them when convenient
mjr 92:f264fbaa1be5 1798 // in the main loop), and also makes them very frequent. The frequency
mjr 92:f264fbaa1be5 1799 // is crucial because it ensures that updates will never be lost for long
mjr 92:f264fbaa1be5 1800 // enough to become noticeable.
mjr 92:f264fbaa1be5 1801 //
mjr 92:f264fbaa1be5 1802 // The mbed library has its own, different solution to this bug, but the
mjr 92:f264fbaa1be5 1803 // mbed solution isn't really a solution at all because it creates a separate
mjr 100:1ff35c07217c 1804 // problem of its own. The mbed approach is to reset the TPM "count" register
mjr 92:f264fbaa1be5 1805 // on every value register write. The count reset truncates the current
mjr 92:f264fbaa1be5 1806 // PWM cycle, which bypasses the hardware problem. Remember, the hardware
mjr 92:f264fbaa1be5 1807 // problem is that you can only write once per cycle; the mbed "solution" gets
mjr 92:f264fbaa1be5 1808 // around that by making sure the cycle ends immediately after the write.
mjr 92:f264fbaa1be5 1809 // The problem with this approach is that the truncated cycle causes visible
mjr 92:f264fbaa1be5 1810 // flicker if the output is connected to an LED. This is particularly
mjr 92:f264fbaa1be5 1811 // noticeable during fades, when we're updating the value register repeatedly
mjr 92:f264fbaa1be5 1812 // and rapidly: an attempt to fade from fully on to fully off causes rapid
mjr 92:f264fbaa1be5 1813 // fluttering and flashing rather than a smooth brightness fade. That's why
mjr 92:f264fbaa1be5 1814 // I had to come up with something different - the mbed solution just trades
mjr 92:f264fbaa1be5 1815 // one annoying bug for another that's just as bad.
mjr 92:f264fbaa1be5 1816 //
mjr 92:f264fbaa1be5 1817 // The hardware bug, by the way, is a case of good intentions gone bad.
mjr 92:f264fbaa1be5 1818 // The whole point of the staging register is to make things easier for
mjr 92:f264fbaa1be5 1819 // us software writers. In most PWM hardware, software has to coordinate
mjr 92:f264fbaa1be5 1820 // with the PWM duty cycle when updating registers to avoid a glitch that
mjr 92:f264fbaa1be5 1821 // you'd get by scribbling to the duty cycle register mid-cycle. The
mjr 92:f264fbaa1be5 1822 // staging register solves this by letting the software write an update at
mjr 92:f264fbaa1be5 1823 // any time, knowing that the hardware will apply the update at exactly the
mjr 92:f264fbaa1be5 1824 // end of the cycle, ensuring glitch-free updates. It's a great design,
mjr 92:f264fbaa1be5 1825 // except that it doesn't quite work. The problem is that they implemented
mjr 92:f264fbaa1be5 1826 // this clever staging register as a one-element FIFO that refuses any more
mjr 92:f264fbaa1be5 1827 // writes when full. That is, writing a value to the FIFO fills it; once
mjr 92:f264fbaa1be5 1828 // full, it ignores writes until it gets emptied out. How's it emptied out?
mjr 92:f264fbaa1be5 1829 // By the hardware moving the staged value to the real register. Sadly, they
mjr 92:f264fbaa1be5 1830 // didn't provide any way for the software to clear the register, and no way
mjr 92:f264fbaa1be5 1831 // to even tell that it's full. So we don't have glitches on write, but we're
mjr 92:f264fbaa1be5 1832 // back to the original problem that the software has to be aware of the PWM
mjr 92:f264fbaa1be5 1833 // cycle timing, because the only way for the software to know that a write
mjr 92:f264fbaa1be5 1834 // actually worked is to know that it's been at least one PWM cycle since the
mjr 92:f264fbaa1be5 1835 // last write. That largely defeats the whole purpose of the staging register,
mjr 92:f264fbaa1be5 1836 // since the whole point was to free software writers of these timing
mjr 92:f264fbaa1be5 1837 // considerations. It's still an improvement over no staging register at
mjr 92:f264fbaa1be5 1838 // all, since we at least don't have to worry about glitches, but it leaves
mjr 92:f264fbaa1be5 1839 // us with this somewhat similar hassle.
mjr 74:822a92bc11d2 1840 //
mjr 77:0b96f6867312 1841 // So here we have our list of PWM outputs that need to be polled for updates.
mjr 77:0b96f6867312 1842 // The KL25Z hardware only has 10 PWM channels, so we only need a fixed set
mjr 77:0b96f6867312 1843 // of polled items.
mjr 74:822a92bc11d2 1844 static int numPolledPwm;
mjr 74:822a92bc11d2 1845 static class LwPwmOut *polledPwm[10];
mjr 74:822a92bc11d2 1846
mjr 74:822a92bc11d2 1847 // LwOut class for a PWM-capable GPIO port.
mjr 6:cc35eb643e8f 1848 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 1849 {
mjr 6:cc35eb643e8f 1850 public:
mjr 43:7a6364d82a41 1851 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1852 {
mjr 77:0b96f6867312 1853 // add myself to the list of polled outputs for periodic updates
mjr 77:0b96f6867312 1854 if (numPolledPwm < countof(polledPwm))
mjr 74:822a92bc11d2 1855 polledPwm[numPolledPwm++] = this;
mjr 93:177832c29041 1856
mjr 94:0476b3e2b996 1857 // IMPORTANT: Do not set the PWM period (frequency) here explicitly.
mjr 94:0476b3e2b996 1858 // We instead want to accept the current setting for the TPM unit
mjr 94:0476b3e2b996 1859 // we're assigned to. The KL25Z hardware can only set the period at
mjr 94:0476b3e2b996 1860 // the TPM unit level, not per channel, so if we changed the frequency
mjr 94:0476b3e2b996 1861 // here, we'd change it for everything attached to our TPM unit. LW
mjr 94:0476b3e2b996 1862 // outputs don't care about frequency other than that it's fast enough
mjr 94:0476b3e2b996 1863 // that attached LEDs won't flicker. Some other PWM users (IR remote,
mjr 94:0476b3e2b996 1864 // TLC5940) DO care about exact frequencies, because they use the PWM
mjr 94:0476b3e2b996 1865 // as a signal generator rather than merely for brightness control.
mjr 94:0476b3e2b996 1866 // If we changed the frequency here, we could clobber one of those
mjr 94:0476b3e2b996 1867 // carefully chosen frequencies and break the other subsystem. So
mjr 94:0476b3e2b996 1868 // we need to be the "free variable" here and accept whatever setting
mjr 94:0476b3e2b996 1869 // is currently on our assigned unit. To minimize flicker, the main()
mjr 94:0476b3e2b996 1870 // entrypoint sets a default PWM rate of 1kHz on all channels. All
mjr 94:0476b3e2b996 1871 // of the other subsystems that might set specific frequencies will
mjr 94:0476b3e2b996 1872 // set much high frequencies, so that should only be good for us.
mjr 94:0476b3e2b996 1873
mjr 94:0476b3e2b996 1874 // set the initial brightness value
mjr 77:0b96f6867312 1875 set(initVal);
mjr 43:7a6364d82a41 1876 }
mjr 74:822a92bc11d2 1877
mjr 40:cc0d9814522b 1878 virtual void set(uint8_t val)
mjr 74:822a92bc11d2 1879 {
mjr 77:0b96f6867312 1880 // save the new value
mjr 74:822a92bc11d2 1881 this->val = val;
mjr 77:0b96f6867312 1882
mjr 77:0b96f6867312 1883 // commit it to the hardware
mjr 77:0b96f6867312 1884 commit();
mjr 13:72dda449c3c0 1885 }
mjr 74:822a92bc11d2 1886
mjr 74:822a92bc11d2 1887 // handle periodic update polling
mjr 74:822a92bc11d2 1888 void poll()
mjr 74:822a92bc11d2 1889 {
mjr 77:0b96f6867312 1890 commit();
mjr 74:822a92bc11d2 1891 }
mjr 74:822a92bc11d2 1892
mjr 74:822a92bc11d2 1893 protected:
mjr 77:0b96f6867312 1894 virtual void commit()
mjr 74:822a92bc11d2 1895 {
mjr 74:822a92bc11d2 1896 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1897 p.glitchFreeWrite(dof_to_pwm[val]);
mjr 74:822a92bc11d2 1898 }
mjr 74:822a92bc11d2 1899
mjr 77:0b96f6867312 1900 NewPwmOut p;
mjr 77:0b96f6867312 1901 uint8_t val;
mjr 6:cc35eb643e8f 1902 };
mjr 26:cb71c4af2912 1903
mjr 74:822a92bc11d2 1904 // Gamma corrected PWM GPIO output. This works exactly like the regular
mjr 74:822a92bc11d2 1905 // PWM output, but translates DOF values through the gamma-corrected
mjr 74:822a92bc11d2 1906 // table instead of the regular linear table.
mjr 64:ef7ca92dff36 1907 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1908 {
mjr 64:ef7ca92dff36 1909 public:
mjr 64:ef7ca92dff36 1910 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1911 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1912 {
mjr 64:ef7ca92dff36 1913 }
mjr 74:822a92bc11d2 1914
mjr 74:822a92bc11d2 1915 protected:
mjr 77:0b96f6867312 1916 virtual void commit()
mjr 64:ef7ca92dff36 1917 {
mjr 74:822a92bc11d2 1918 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1919 p.glitchFreeWrite(dof_to_gamma_pwm[val]);
mjr 64:ef7ca92dff36 1920 }
mjr 64:ef7ca92dff36 1921 };
mjr 64:ef7ca92dff36 1922
mjr 74:822a92bc11d2 1923 // poll the PWM outputs
mjr 74:822a92bc11d2 1924 Timer polledPwmTimer;
mjr 76:7f5912b6340e 1925 uint64_t polledPwmTotalTime, polledPwmRunCount;
mjr 74:822a92bc11d2 1926 void pollPwmUpdates()
mjr 74:822a92bc11d2 1927 {
mjr 94:0476b3e2b996 1928 // If it's been long enough since the last update, do another update.
mjr 94:0476b3e2b996 1929 // Note that the time limit is fairly arbitrary: it has to be at least
mjr 94:0476b3e2b996 1930 // 1.5X the PWM period, so that we can be sure that at least one PWM
mjr 94:0476b3e2b996 1931 // period has elapsed since the last update, but there's no hard upper
mjr 94:0476b3e2b996 1932 // bound. Instead, it only has to be short enough that fades don't
mjr 94:0476b3e2b996 1933 // become noticeably chunky. The competing interest is that we don't
mjr 94:0476b3e2b996 1934 // want to do this more often than necessary to provide incremental
mjr 94:0476b3e2b996 1935 // benefit, because the polling adds overhead to the main loop and
mjr 94:0476b3e2b996 1936 // takes time away from other tasks we could be performing. The
mjr 94:0476b3e2b996 1937 // shortest time with practical benefit is probably around 50-60Hz,
mjr 94:0476b3e2b996 1938 // since that gives us "video rate" granularity in fades. Anything
mjr 94:0476b3e2b996 1939 // faster wouldn't probably make fades look any smoother to a human
mjr 94:0476b3e2b996 1940 // viewer.
mjr 94:0476b3e2b996 1941 if (polledPwmTimer.read_us() >= 15000)
mjr 74:822a92bc11d2 1942 {
mjr 74:822a92bc11d2 1943 // time the run for statistics collection
mjr 74:822a92bc11d2 1944 IF_DIAG(
mjr 74:822a92bc11d2 1945 Timer t;
mjr 74:822a92bc11d2 1946 t.start();
mjr 74:822a92bc11d2 1947 )
mjr 74:822a92bc11d2 1948
mjr 74:822a92bc11d2 1949 // poll each output
mjr 74:822a92bc11d2 1950 for (int i = numPolledPwm ; i > 0 ; )
mjr 74:822a92bc11d2 1951 polledPwm[--i]->poll();
mjr 74:822a92bc11d2 1952
mjr 74:822a92bc11d2 1953 // reset the timer for the next cycle
mjr 74:822a92bc11d2 1954 polledPwmTimer.reset();
mjr 74:822a92bc11d2 1955
mjr 74:822a92bc11d2 1956 // collect statistics
mjr 74:822a92bc11d2 1957 IF_DIAG(
mjr 76:7f5912b6340e 1958 polledPwmTotalTime += t.read_us();
mjr 74:822a92bc11d2 1959 polledPwmRunCount += 1;
mjr 74:822a92bc11d2 1960 )
mjr 74:822a92bc11d2 1961 }
mjr 74:822a92bc11d2 1962 }
mjr 64:ef7ca92dff36 1963
mjr 26:cb71c4af2912 1964 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1965 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1966 {
mjr 6:cc35eb643e8f 1967 public:
mjr 43:7a6364d82a41 1968 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1969 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1970 {
mjr 13:72dda449c3c0 1971 if (val != prv)
mjr 40:cc0d9814522b 1972 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1973 }
mjr 6:cc35eb643e8f 1974 DigitalOut p;
mjr 40:cc0d9814522b 1975 uint8_t prv;
mjr 6:cc35eb643e8f 1976 };
mjr 26:cb71c4af2912 1977
mjr 29:582472d0bc57 1978 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1979 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1980 // port n (0-based).
mjr 35:e959ffba78fd 1981 //
mjr 35:e959ffba78fd 1982 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1983 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1984 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1985 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1986 // 74HC595 ports).
mjr 33:d832bcab089e 1987 static int numOutputs;
mjr 33:d832bcab089e 1988 static LwOut **lwPin;
mjr 33:d832bcab089e 1989
mjr 38:091e511ce8a0 1990 // create a single output pin
mjr 53:9b2611964afc 1991 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1992 {
mjr 38:091e511ce8a0 1993 // get this item's values
mjr 38:091e511ce8a0 1994 int typ = pc.typ;
mjr 38:091e511ce8a0 1995 int pin = pc.pin;
mjr 38:091e511ce8a0 1996 int flags = pc.flags;
mjr 40:cc0d9814522b 1997 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1998 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1999 int gamma = flags & PortFlagGamma;
mjr 89:c43cd923401c 2000 int flipperLogic = flags & PortFlagFlipperLogic;
mjr 99:8139b0c274f4 2001 int chimeLogic = flags & PortFlagChimeLogic;
mjr 89:c43cd923401c 2002
mjr 89:c43cd923401c 2003 // cancel gamma on flipper logic ports
mjr 89:c43cd923401c 2004 if (flipperLogic)
mjr 89:c43cd923401c 2005 gamma = false;
mjr 38:091e511ce8a0 2006
mjr 38:091e511ce8a0 2007 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 2008 LwOut *lwp;
mjr 38:091e511ce8a0 2009 switch (typ)
mjr 38:091e511ce8a0 2010 {
mjr 38:091e511ce8a0 2011 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 2012 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 2013 if (pin != 0)
mjr 64:ef7ca92dff36 2014 {
mjr 64:ef7ca92dff36 2015 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 2016 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 2017 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 2018 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 2019 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 2020 {
mjr 64:ef7ca92dff36 2021 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 2022 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 2023
mjr 64:ef7ca92dff36 2024 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 2025 gamma = false;
mjr 64:ef7ca92dff36 2026 }
mjr 64:ef7ca92dff36 2027 else
mjr 64:ef7ca92dff36 2028 {
mjr 64:ef7ca92dff36 2029 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 2030 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 2031 }
mjr 64:ef7ca92dff36 2032 }
mjr 48:058ace2aed1d 2033 else
mjr 48:058ace2aed1d 2034 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2035 break;
mjr 38:091e511ce8a0 2036
mjr 38:091e511ce8a0 2037 case PortTypeGPIODig:
mjr 38:091e511ce8a0 2038 // Digital GPIO port
mjr 48:058ace2aed1d 2039 if (pin != 0)
mjr 48:058ace2aed1d 2040 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 2041 else
mjr 48:058ace2aed1d 2042 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2043 break;
mjr 38:091e511ce8a0 2044
mjr 38:091e511ce8a0 2045 case PortTypeTLC5940:
mjr 38:091e511ce8a0 2046 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 2047 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 2048 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 2049 {
mjr 40:cc0d9814522b 2050 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 2051 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 2052 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 2053 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 2054 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 2055 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 2056 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 2057 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 2058 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 2059 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 2060 // for this unlikely case.
mjr 40:cc0d9814522b 2061 if (gamma && !activeLow)
mjr 40:cc0d9814522b 2062 {
mjr 40:cc0d9814522b 2063 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 2064 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 2065
mjr 40:cc0d9814522b 2066 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 2067 gamma = false;
mjr 40:cc0d9814522b 2068 }
mjr 40:cc0d9814522b 2069 else
mjr 40:cc0d9814522b 2070 {
mjr 40:cc0d9814522b 2071 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 2072 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 2073 }
mjr 40:cc0d9814522b 2074 }
mjr 38:091e511ce8a0 2075 else
mjr 40:cc0d9814522b 2076 {
mjr 40:cc0d9814522b 2077 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 2078 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 2079 }
mjr 38:091e511ce8a0 2080 break;
mjr 38:091e511ce8a0 2081
mjr 38:091e511ce8a0 2082 case PortType74HC595:
mjr 87:8d35c74403af 2083 // 74HC595 port (if we don't have an HC595 controller object, or it's not
mjr 87:8d35c74403af 2084 // a valid output number, create a virtual port)
mjr 38:091e511ce8a0 2085 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 2086 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 2087 else
mjr 38:091e511ce8a0 2088 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2089 break;
mjr 87:8d35c74403af 2090
mjr 87:8d35c74403af 2091 case PortTypeTLC59116:
mjr 87:8d35c74403af 2092 // TLC59116 port. The pin number in the config encodes the chip address
mjr 87:8d35c74403af 2093 // in the high 4 bits and the output number on the chip in the low 4 bits.
mjr 87:8d35c74403af 2094 // There's no gamma-corrected version of this output handler, so we don't
mjr 87:8d35c74403af 2095 // need to worry about that here; just use the layered gamma as needed.
mjr 87:8d35c74403af 2096 if (tlc59116 != 0)
mjr 87:8d35c74403af 2097 lwp = new Lw59116Out((pin >> 4) & 0x0F, pin & 0x0F);
mjr 87:8d35c74403af 2098 break;
mjr 38:091e511ce8a0 2099
mjr 38:091e511ce8a0 2100 case PortTypeVirtual:
mjr 43:7a6364d82a41 2101 case PortTypeDisabled:
mjr 38:091e511ce8a0 2102 default:
mjr 38:091e511ce8a0 2103 // virtual or unknown
mjr 38:091e511ce8a0 2104 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2105 break;
mjr 38:091e511ce8a0 2106 }
mjr 38:091e511ce8a0 2107
mjr 40:cc0d9814522b 2108 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 2109 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 2110 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 2111 if (activeLow)
mjr 38:091e511ce8a0 2112 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 2113
mjr 89:c43cd923401c 2114 // Layer on Flipper Logic if desired
mjr 89:c43cd923401c 2115 if (flipperLogic)
mjr 89:c43cd923401c 2116 lwp = new LwFlipperLogicOut(lwp, pc.flipperLogic);
mjr 89:c43cd923401c 2117
mjr 99:8139b0c274f4 2118 // Layer on Chime Logic if desired. Note that Chime Logic and
mjr 99:8139b0c274f4 2119 // Flipper Logic are mutually exclusive, and Flipper Logic takes
mjr 99:8139b0c274f4 2120 // precedence, so ignore the Chime Logic bit if both are set.
mjr 99:8139b0c274f4 2121 if (chimeLogic && !flipperLogic)
mjr 99:8139b0c274f4 2122 lwp = new LwChimeLogicOut(lwp, pc.flipperLogic);
mjr 98:4df3c0f7e707 2123
mjr 89:c43cd923401c 2124 // If it's a noisemaker, layer on a night mode switch
mjr 40:cc0d9814522b 2125 if (noisy)
mjr 40:cc0d9814522b 2126 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 2127
mjr 40:cc0d9814522b 2128 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 2129 if (gamma)
mjr 40:cc0d9814522b 2130 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 2131
mjr 53:9b2611964afc 2132 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 2133 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 2134 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 2135 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 2136 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 2137
mjr 53:9b2611964afc 2138 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 2139 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 2140 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 2141
mjr 38:091e511ce8a0 2142 // turn it off initially
mjr 38:091e511ce8a0 2143 lwp->set(0);
mjr 38:091e511ce8a0 2144
mjr 38:091e511ce8a0 2145 // return the pin
mjr 38:091e511ce8a0 2146 return lwp;
mjr 38:091e511ce8a0 2147 }
mjr 38:091e511ce8a0 2148
mjr 6:cc35eb643e8f 2149 // initialize the output pin array
mjr 35:e959ffba78fd 2150 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 2151 {
mjr 99:8139b0c274f4 2152 // Initialize the Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 2153 LwFlipperLogicOut::classInit(cfg);
mjr 99:8139b0c274f4 2154 LwChimeLogicOut::classInit(cfg);
mjr 89:c43cd923401c 2155
mjr 35:e959ffba78fd 2156 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 2157 // total number of ports.
mjr 35:e959ffba78fd 2158 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 2159 int i;
mjr 35:e959ffba78fd 2160 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 2161 {
mjr 35:e959ffba78fd 2162 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 2163 {
mjr 35:e959ffba78fd 2164 numOutputs = i;
mjr 34:6b981a2afab7 2165 break;
mjr 34:6b981a2afab7 2166 }
mjr 33:d832bcab089e 2167 }
mjr 33:d832bcab089e 2168
mjr 73:4e8ce0b18915 2169 // allocate the pin array
mjr 73:4e8ce0b18915 2170 lwPin = new LwOut*[numOutputs];
mjr 35:e959ffba78fd 2171
mjr 73:4e8ce0b18915 2172 // Allocate the current brightness array
mjr 73:4e8ce0b18915 2173 outLevel = new uint8_t[numOutputs];
mjr 33:d832bcab089e 2174
mjr 73:4e8ce0b18915 2175 // allocate the LedWiz output state arrays
mjr 73:4e8ce0b18915 2176 wizOn = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 2177 wizVal = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 2178
mjr 73:4e8ce0b18915 2179 // initialize all LedWiz outputs to off and brightness 48
mjr 73:4e8ce0b18915 2180 memset(wizOn, 0, numOutputs);
mjr 73:4e8ce0b18915 2181 memset(wizVal, 48, numOutputs);
mjr 73:4e8ce0b18915 2182
mjr 73:4e8ce0b18915 2183 // set all LedWiz virtual unit flash speeds to 2
mjr 73:4e8ce0b18915 2184 for (i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2185 wizSpeed[i] = 2;
mjr 33:d832bcab089e 2186
mjr 35:e959ffba78fd 2187 // create the pin interface object for each port
mjr 35:e959ffba78fd 2188 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 2189 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 2190 }
mjr 6:cc35eb643e8f 2191
mjr 76:7f5912b6340e 2192 // Translate an LedWiz brightness level (0..49) to a DOF brightness
mjr 76:7f5912b6340e 2193 // level (0..255). Note that brightness level 49 isn't actually valid,
mjr 76:7f5912b6340e 2194 // according to the LedWiz API documentation, but many clients use it
mjr 76:7f5912b6340e 2195 // anyway, and the real LedWiz accepts it and seems to treat it as
mjr 76:7f5912b6340e 2196 // equivalent to 48.
mjr 40:cc0d9814522b 2197 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 2198 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 2199 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 2200 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 2201 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 2202 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 2203 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 2204 255, 255
mjr 40:cc0d9814522b 2205 };
mjr 40:cc0d9814522b 2206
mjr 76:7f5912b6340e 2207 // Translate a DOF brightness level (0..255) to an LedWiz brightness
mjr 76:7f5912b6340e 2208 // level (1..48)
mjr 76:7f5912b6340e 2209 static const uint8_t dof_to_lw[] = {
mjr 76:7f5912b6340e 2210 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
mjr 76:7f5912b6340e 2211 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
mjr 76:7f5912b6340e 2212 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9,
mjr 76:7f5912b6340e 2213 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12,
mjr 76:7f5912b6340e 2214 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15,
mjr 76:7f5912b6340e 2215 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18,
mjr 76:7f5912b6340e 2216 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21,
mjr 76:7f5912b6340e 2217 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24,
mjr 76:7f5912b6340e 2218 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27,
mjr 76:7f5912b6340e 2219 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30,
mjr 76:7f5912b6340e 2220 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
mjr 76:7f5912b6340e 2221 33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36,
mjr 76:7f5912b6340e 2222 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
mjr 76:7f5912b6340e 2223 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
mjr 76:7f5912b6340e 2224 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45,
mjr 76:7f5912b6340e 2225 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48
mjr 76:7f5912b6340e 2226 };
mjr 76:7f5912b6340e 2227
mjr 74:822a92bc11d2 2228 // LedWiz flash cycle tables. For efficiency, we use a lookup table
mjr 74:822a92bc11d2 2229 // rather than calculating these on the fly. The flash cycles are
mjr 74:822a92bc11d2 2230 // generated by the following formulas, where 'c' is the current
mjr 74:822a92bc11d2 2231 // cycle counter, from 0 to 255:
mjr 74:822a92bc11d2 2232 //
mjr 74:822a92bc11d2 2233 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 2234 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 2235 // mode 131 = on/ramp down = (c < 128 ? 255 : (255-c)*2)
mjr 74:822a92bc11d2 2236 // mode 132 = ramp up/on = (c < 128 ? c*2 : 255)
mjr 74:822a92bc11d2 2237 //
mjr 74:822a92bc11d2 2238 // To look up the current output value for a given mode and a given
mjr 74:822a92bc11d2 2239 // cycle counter 'c', index the table with ((mode-129)*256)+c.
mjr 74:822a92bc11d2 2240 static const uint8_t wizFlashLookup[] = {
mjr 74:822a92bc11d2 2241 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 2242 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
mjr 74:822a92bc11d2 2243 0x21, 0x23, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f,
mjr 74:822a92bc11d2 2244 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f,
mjr 74:822a92bc11d2 2245 0x61, 0x63, 0x65, 0x67, 0x69, 0x6b, 0x6d, 0x6f, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d, 0x7f,
mjr 74:822a92bc11d2 2246 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d, 0x8f, 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9d, 0x9f,
mjr 74:822a92bc11d2 2247 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad, 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, 0xbf,
mjr 74:822a92bc11d2 2248 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
mjr 74:822a92bc11d2 2249 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb, 0xed, 0xef, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0xfd, 0xff,
mjr 74:822a92bc11d2 2250 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 2251 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 2252 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 2253 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 2254 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 2255 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 2256 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 2257 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 2258
mjr 74:822a92bc11d2 2259 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 2260 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2261 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2262 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2263 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2264 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2265 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2266 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2267 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2268 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2269 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2270 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2271 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2272 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2273 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2274 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2275 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2276
mjr 74:822a92bc11d2 2277 // mode 131 = on/ramp down = c < 128 ? 255 : (255 - c)*2
mjr 74:822a92bc11d2 2278 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2279 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2280 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2281 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2282 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2283 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2284 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2285 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2286 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 2287 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 2288 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 2289 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 2290 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 2291 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 2292 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 2293 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 2294
mjr 74:822a92bc11d2 2295 // mode 132 = ramp up/on = c < 128 ? c*2 : 255
mjr 74:822a92bc11d2 2296 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
mjr 74:822a92bc11d2 2297 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
mjr 74:822a92bc11d2 2298 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
mjr 74:822a92bc11d2 2299 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
mjr 74:822a92bc11d2 2300 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
mjr 74:822a92bc11d2 2301 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
mjr 74:822a92bc11d2 2302 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
mjr 74:822a92bc11d2 2303 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
mjr 74:822a92bc11d2 2304 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2305 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2306 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2307 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2308 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2309 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2310 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2311 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
mjr 74:822a92bc11d2 2312 };
mjr 74:822a92bc11d2 2313
mjr 74:822a92bc11d2 2314 // LedWiz flash cycle timer. This runs continuously. On each update,
mjr 74:822a92bc11d2 2315 // we use this to figure out where we are on the cycle for each bank.
mjr 74:822a92bc11d2 2316 Timer wizCycleTimer;
mjr 74:822a92bc11d2 2317
mjr 76:7f5912b6340e 2318 // timing statistics for wizPulse()
mjr 76:7f5912b6340e 2319 uint64_t wizPulseTotalTime, wizPulseRunCount;
mjr 76:7f5912b6340e 2320
mjr 76:7f5912b6340e 2321 // LedWiz flash timer pulse. The main loop calls this on each cycle
mjr 76:7f5912b6340e 2322 // to update outputs using LedWiz flash modes. We do one bank of 32
mjr 76:7f5912b6340e 2323 // outputs on each cycle.
mjr 29:582472d0bc57 2324 static void wizPulse()
mjr 29:582472d0bc57 2325 {
mjr 76:7f5912b6340e 2326 // current bank
mjr 76:7f5912b6340e 2327 static int wizPulseBank = 0;
mjr 76:7f5912b6340e 2328
mjr 76:7f5912b6340e 2329 // start a timer for statistics collection
mjr 76:7f5912b6340e 2330 IF_DIAG(
mjr 76:7f5912b6340e 2331 Timer t;
mjr 76:7f5912b6340e 2332 t.start();
mjr 76:7f5912b6340e 2333 )
mjr 76:7f5912b6340e 2334
mjr 76:7f5912b6340e 2335 // Update the current bank's cycle counter: figure the current
mjr 76:7f5912b6340e 2336 // phase of the LedWiz pulse cycle for this bank.
mjr 76:7f5912b6340e 2337 //
mjr 76:7f5912b6340e 2338 // The LedWiz speed setting gives the flash period in 0.25s units
mjr 76:7f5912b6340e 2339 // (speed 1 is a flash period of .25s, speed 7 is a period of 1.75s).
mjr 76:7f5912b6340e 2340 //
mjr 76:7f5912b6340e 2341 // What we're after here is the "phase", which is to say the point
mjr 76:7f5912b6340e 2342 // in the current cycle. If we assume that the cycle has been running
mjr 76:7f5912b6340e 2343 // continuously since some arbitrary time zero in the past, we can
mjr 76:7f5912b6340e 2344 // figure where we are in the current cycle by dividing the time since
mjr 76:7f5912b6340e 2345 // that zero by the cycle period and taking the remainder. E.g., if
mjr 76:7f5912b6340e 2346 // the cycle time is 5 seconds, and the time since t-zero is 17 seconds,
mjr 76:7f5912b6340e 2347 // we divide 17 by 5 to get a remainder of 2. That says we're 2 seconds
mjr 76:7f5912b6340e 2348 // into the current 5-second cycle, or 2/5 of the way through the
mjr 76:7f5912b6340e 2349 // current cycle.
mjr 76:7f5912b6340e 2350 //
mjr 76:7f5912b6340e 2351 // We do this calculation on every iteration of the main loop, so we
mjr 76:7f5912b6340e 2352 // want it to be very fast. To streamline it, we'll use some tricky
mjr 76:7f5912b6340e 2353 // integer arithmetic. The result will be the same as the straightforward
mjr 76:7f5912b6340e 2354 // remainder and fraction calculation we just explained, but we'll get
mjr 76:7f5912b6340e 2355 // there by less-than-obvious means.
mjr 76:7f5912b6340e 2356 //
mjr 76:7f5912b6340e 2357 // Rather than finding the phase as a continuous quantity or floating
mjr 76:7f5912b6340e 2358 // point number, we'll quantize it. We'll divide each cycle into 256
mjr 76:7f5912b6340e 2359 // time units, or quanta. Each quantum is 1/256 of the cycle length,
mjr 76:7f5912b6340e 2360 // so for a 1-second cycle (LedWiz speed 4), each quantum is 1/256 of
mjr 76:7f5912b6340e 2361 // a second, or about 3.9ms. If we express the time since t-zero in
mjr 76:7f5912b6340e 2362 // these units, the time period of one cycle is exactly 256 units, so
mjr 76:7f5912b6340e 2363 // we can calculate our point in the cycle by taking the remainder of
mjr 76:7f5912b6340e 2364 // the time (in our funny units) divided by 256. The special thing
mjr 76:7f5912b6340e 2365 // about making the cycle time equal to 256 units is that "x % 256"
mjr 76:7f5912b6340e 2366 // is exactly the same as "x & 255", which is a much faster operation
mjr 76:7f5912b6340e 2367 // than division on ARM M0+: this CPU has no hardware DIVIDE operation,
mjr 76:7f5912b6340e 2368 // so an integer division takes about 5us. The bit mask operation, in
mjr 76:7f5912b6340e 2369 // contrast, takes only about 60ns - about 100x faster. 5us doesn't
mjr 76:7f5912b6340e 2370 // sound like much, but we do this on every main loop, so every little
mjr 76:7f5912b6340e 2371 // bit counts.
mjr 76:7f5912b6340e 2372 //
mjr 76:7f5912b6340e 2373 // The snag is that our system timer gives us the elapsed time in
mjr 76:7f5912b6340e 2374 // microseconds. We still need to convert this to our special quanta
mjr 76:7f5912b6340e 2375 // of 256 units per cycle. The straightforward way to do that is by
mjr 76:7f5912b6340e 2376 // dividing by (microseconds per quantum). E.g., for LedWiz speed 4,
mjr 76:7f5912b6340e 2377 // we decided that our quantum was 1/256 of a second, or 3906us, so
mjr 76:7f5912b6340e 2378 // dividing the current system time in microseconds by 3906 will give
mjr 76:7f5912b6340e 2379 // us the time in our quantum units. But now we've just substituted
mjr 76:7f5912b6340e 2380 // one division for another!
mjr 76:7f5912b6340e 2381 //
mjr 76:7f5912b6340e 2382 // This is where our really tricky integer math comes in. Dividing
mjr 76:7f5912b6340e 2383 // by X is the same as multiplying by 1/X. In integer math, 1/3906
mjr 76:7f5912b6340e 2384 // is zero, so that won't work. But we can get around that by doing
mjr 76:7f5912b6340e 2385 // the integer math as "fixed point" arithmetic instead. It's still
mjr 76:7f5912b6340e 2386 // actually carried out as integer operations, but we'll scale our
mjr 76:7f5912b6340e 2387 // integers by a scaling factor, then take out the scaling factor
mjr 76:7f5912b6340e 2388 // later to get the final result. The scaling factor we'll use is
mjr 76:7f5912b6340e 2389 // 2^24. So we're going to calculate (time * 2^24/3906), then divide
mjr 76:7f5912b6340e 2390 // the result by 2^24 to get the final answer. I know it seems like
mjr 76:7f5912b6340e 2391 // we're substituting one division for another yet again, but this
mjr 76:7f5912b6340e 2392 // time's the charm, because dividing by 2^24 is a bit shift operation,
mjr 76:7f5912b6340e 2393 // which is another single-cycle operation on M0+. You might also
mjr 76:7f5912b6340e 2394 // wonder how all these tricks don't cause overflows or underflows
mjr 76:7f5912b6340e 2395 // or what not. Well, the multiply by 2^24/3906 will cause an
mjr 76:7f5912b6340e 2396 // overflow, but we don't care, because the overflow will all be in
mjr 76:7f5912b6340e 2397 // the high-order bits that we're going to discard in the final
mjr 76:7f5912b6340e 2398 // remainder calculation anyway.
mjr 76:7f5912b6340e 2399 //
mjr 76:7f5912b6340e 2400 // Each entry in the array below represents 2^24/N for the corresponding
mjr 76:7f5912b6340e 2401 // LedWiz speed, where N is the number of time quanta per cycle at that
mjr 76:7f5912b6340e 2402 // speed. The time quanta are chosen such that 256 quanta add up to
mjr 76:7f5912b6340e 2403 // approximately (LedWiz speed setting * 0.25s).
mjr 76:7f5912b6340e 2404 //
mjr 76:7f5912b6340e 2405 // Note that the calculation has an implicit bit mask (result & 0xFF)
mjr 76:7f5912b6340e 2406 // to get the final result mod 256. But we don't have to actually
mjr 76:7f5912b6340e 2407 // do that work because we're using 32-bit ints and a 2^24 fixed
mjr 76:7f5912b6340e 2408 // point base (X in the narrative above). The final shift right by
mjr 76:7f5912b6340e 2409 // 24 bits to divide out the base will leave us with only 8 bits in
mjr 76:7f5912b6340e 2410 // the result, since we started with 32.
mjr 76:7f5912b6340e 2411 static const uint32_t inv_us_per_quantum[] = { // indexed by LedWiz speed
mjr 76:7f5912b6340e 2412 0, 17172, 8590, 5726, 4295, 3436, 2863, 2454
mjr 76:7f5912b6340e 2413 };
mjr 76:7f5912b6340e 2414 int counter = ((wizCycleTimer.read_us() * inv_us_per_quantum[wizSpeed[wizPulseBank]]) >> 24);
mjr 76:7f5912b6340e 2415
mjr 76:7f5912b6340e 2416 // get the range of 32 output sin this bank
mjr 76:7f5912b6340e 2417 int fromPort = wizPulseBank*32;
mjr 76:7f5912b6340e 2418 int toPort = fromPort+32;
mjr 76:7f5912b6340e 2419 if (toPort > numOutputs)
mjr 76:7f5912b6340e 2420 toPort = numOutputs;
mjr 76:7f5912b6340e 2421
mjr 76:7f5912b6340e 2422 // update all outputs set to flashing values
mjr 76:7f5912b6340e 2423 for (int i = fromPort ; i < toPort ; ++i)
mjr 73:4e8ce0b18915 2424 {
mjr 76:7f5912b6340e 2425 // Update the port only if the LedWiz SBA switch for the port is on
mjr 76:7f5912b6340e 2426 // (wizOn[i]) AND the port is a PBA flash mode in the range 129..132.
mjr 76:7f5912b6340e 2427 // These modes and only these modes have the high bit (0x80) set, so
mjr 76:7f5912b6340e 2428 // we can test for them simply by testing the high bit.
mjr 76:7f5912b6340e 2429 if (wizOn[i])
mjr 29:582472d0bc57 2430 {
mjr 76:7f5912b6340e 2431 uint8_t val = wizVal[i];
mjr 76:7f5912b6340e 2432 if ((val & 0x80) != 0)
mjr 29:582472d0bc57 2433 {
mjr 76:7f5912b6340e 2434 // ook up the value for the mode at the cycle time
mjr 76:7f5912b6340e 2435 lwPin[i]->set(outLevel[i] = wizFlashLookup[((val-129) << 8) + counter]);
mjr 29:582472d0bc57 2436 }
mjr 29:582472d0bc57 2437 }
mjr 76:7f5912b6340e 2438 }
mjr 76:7f5912b6340e 2439
mjr 34:6b981a2afab7 2440 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 2441 if (hc595 != 0)
mjr 35:e959ffba78fd 2442 hc595->update();
mjr 76:7f5912b6340e 2443
mjr 76:7f5912b6340e 2444 // switch to the next bank
mjr 76:7f5912b6340e 2445 if (++wizPulseBank >= MAX_LW_BANKS)
mjr 76:7f5912b6340e 2446 wizPulseBank = 0;
mjr 76:7f5912b6340e 2447
mjr 76:7f5912b6340e 2448 // collect timing statistics
mjr 76:7f5912b6340e 2449 IF_DIAG(
mjr 76:7f5912b6340e 2450 wizPulseTotalTime += t.read_us();
mjr 76:7f5912b6340e 2451 wizPulseRunCount += 1;
mjr 76:7f5912b6340e 2452 )
mjr 1:d913e0afb2ac 2453 }
mjr 38:091e511ce8a0 2454
mjr 76:7f5912b6340e 2455 // Update a port to reflect its new LedWiz SBA+PBA setting.
mjr 76:7f5912b6340e 2456 static void updateLwPort(int port)
mjr 38:091e511ce8a0 2457 {
mjr 76:7f5912b6340e 2458 // check if the SBA switch is on or off
mjr 76:7f5912b6340e 2459 if (wizOn[port])
mjr 76:7f5912b6340e 2460 {
mjr 76:7f5912b6340e 2461 // It's on. If the port is a valid static brightness level,
mjr 76:7f5912b6340e 2462 // set the output port to match. Otherwise leave it as is:
mjr 76:7f5912b6340e 2463 // if it's a flashing mode, the flash mode pulse will update
mjr 76:7f5912b6340e 2464 // it on the next cycle.
mjr 76:7f5912b6340e 2465 int val = wizVal[port];
mjr 76:7f5912b6340e 2466 if (val <= 49)
mjr 76:7f5912b6340e 2467 lwPin[port]->set(outLevel[port] = lw_to_dof[val]);
mjr 76:7f5912b6340e 2468 }
mjr 76:7f5912b6340e 2469 else
mjr 76:7f5912b6340e 2470 {
mjr 76:7f5912b6340e 2471 // the port is off - set absolute brightness zero
mjr 76:7f5912b6340e 2472 lwPin[port]->set(outLevel[port] = 0);
mjr 76:7f5912b6340e 2473 }
mjr 73:4e8ce0b18915 2474 }
mjr 73:4e8ce0b18915 2475
mjr 73:4e8ce0b18915 2476 // Turn off all outputs and restore everything to the default LedWiz
mjr 92:f264fbaa1be5 2477 // state. This sets all outputs to LedWiz profile value 48 (full
mjr 92:f264fbaa1be5 2478 // brightness) and switch state Off, and sets the LedWiz flash rate
mjr 92:f264fbaa1be5 2479 // to 2. This effectively restores the power-on conditions.
mjr 73:4e8ce0b18915 2480 //
mjr 73:4e8ce0b18915 2481 void allOutputsOff()
mjr 73:4e8ce0b18915 2482 {
mjr 92:f264fbaa1be5 2483 // reset all outputs to OFF/48
mjr 73:4e8ce0b18915 2484 for (int i = 0 ; i < numOutputs ; ++i)
mjr 73:4e8ce0b18915 2485 {
mjr 73:4e8ce0b18915 2486 outLevel[i] = 0;
mjr 73:4e8ce0b18915 2487 wizOn[i] = 0;
mjr 73:4e8ce0b18915 2488 wizVal[i] = 48;
mjr 73:4e8ce0b18915 2489 lwPin[i]->set(0);
mjr 73:4e8ce0b18915 2490 }
mjr 73:4e8ce0b18915 2491
mjr 73:4e8ce0b18915 2492 // restore default LedWiz flash rate
mjr 73:4e8ce0b18915 2493 for (int i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2494 wizSpeed[i] = 2;
mjr 38:091e511ce8a0 2495
mjr 73:4e8ce0b18915 2496 // flush changes to hc595, if applicable
mjr 38:091e511ce8a0 2497 if (hc595 != 0)
mjr 38:091e511ce8a0 2498 hc595->update();
mjr 38:091e511ce8a0 2499 }
mjr 38:091e511ce8a0 2500
mjr 74:822a92bc11d2 2501 // Cary out an SBA or SBX message. portGroup is 0 for ports 1-32,
mjr 74:822a92bc11d2 2502 // 1 for ports 33-64, etc. Original protocol SBA messages always
mjr 74:822a92bc11d2 2503 // address port group 0; our private SBX extension messages can
mjr 74:822a92bc11d2 2504 // address any port group.
mjr 74:822a92bc11d2 2505 void sba_sbx(int portGroup, const uint8_t *data)
mjr 74:822a92bc11d2 2506 {
mjr 76:7f5912b6340e 2507 // update all on/off states in the group
mjr 74:822a92bc11d2 2508 for (int i = 0, bit = 1, imsg = 1, port = portGroup*32 ;
mjr 74:822a92bc11d2 2509 i < 32 && port < numOutputs ;
mjr 74:822a92bc11d2 2510 ++i, bit <<= 1, ++port)
mjr 74:822a92bc11d2 2511 {
mjr 74:822a92bc11d2 2512 // figure the on/off state bit for this output
mjr 74:822a92bc11d2 2513 if (bit == 0x100) {
mjr 74:822a92bc11d2 2514 bit = 1;
mjr 74:822a92bc11d2 2515 ++imsg;
mjr 74:822a92bc11d2 2516 }
mjr 74:822a92bc11d2 2517
mjr 74:822a92bc11d2 2518 // set the on/off state
mjr 76:7f5912b6340e 2519 bool on = wizOn[port] = ((data[imsg] & bit) != 0);
mjr 76:7f5912b6340e 2520
mjr 76:7f5912b6340e 2521 // set the output port brightness to match the new setting
mjr 76:7f5912b6340e 2522 updateLwPort(port);
mjr 74:822a92bc11d2 2523 }
mjr 74:822a92bc11d2 2524
mjr 74:822a92bc11d2 2525 // set the flash speed for the port group
mjr 74:822a92bc11d2 2526 if (portGroup < countof(wizSpeed))
mjr 74:822a92bc11d2 2527 wizSpeed[portGroup] = (data[5] < 1 ? 1 : data[5] > 7 ? 7 : data[5]);
mjr 74:822a92bc11d2 2528
mjr 76:7f5912b6340e 2529 // update 74HC959 outputs
mjr 76:7f5912b6340e 2530 if (hc595 != 0)
mjr 76:7f5912b6340e 2531 hc595->update();
mjr 74:822a92bc11d2 2532 }
mjr 74:822a92bc11d2 2533
mjr 74:822a92bc11d2 2534 // Carry out a PBA or PBX message.
mjr 74:822a92bc11d2 2535 void pba_pbx(int basePort, const uint8_t *data)
mjr 74:822a92bc11d2 2536 {
mjr 74:822a92bc11d2 2537 // update each wizVal entry from the brightness data
mjr 76:7f5912b6340e 2538 for (int i = 0, port = basePort ; i < 8 && port < numOutputs ; ++i, ++port)
mjr 74:822a92bc11d2 2539 {
mjr 74:822a92bc11d2 2540 // get the value
mjr 74:822a92bc11d2 2541 uint8_t v = data[i];
mjr 74:822a92bc11d2 2542
mjr 74:822a92bc11d2 2543 // Validate it. The legal values are 0..49 for brightness
mjr 74:822a92bc11d2 2544 // levels, and 128..132 for flash modes. Set anything invalid
mjr 74:822a92bc11d2 2545 // to full brightness (48) instead. Note that 49 isn't actually
mjr 74:822a92bc11d2 2546 // a valid documented value, but in practice some clients send
mjr 74:822a92bc11d2 2547 // this to mean 100% brightness, and the real LedWiz treats it
mjr 74:822a92bc11d2 2548 // as such.
mjr 74:822a92bc11d2 2549 if ((v > 49 && v < 129) || v > 132)
mjr 74:822a92bc11d2 2550 v = 48;
mjr 74:822a92bc11d2 2551
mjr 74:822a92bc11d2 2552 // store it
mjr 76:7f5912b6340e 2553 wizVal[port] = v;
mjr 76:7f5912b6340e 2554
mjr 76:7f5912b6340e 2555 // update the port
mjr 76:7f5912b6340e 2556 updateLwPort(port);
mjr 74:822a92bc11d2 2557 }
mjr 74:822a92bc11d2 2558
mjr 76:7f5912b6340e 2559 // update 74HC595 outputs
mjr 76:7f5912b6340e 2560 if (hc595 != 0)
mjr 76:7f5912b6340e 2561 hc595->update();
mjr 74:822a92bc11d2 2562 }
mjr 74:822a92bc11d2 2563
mjr 77:0b96f6867312 2564 // ---------------------------------------------------------------------------
mjr 77:0b96f6867312 2565 //
mjr 77:0b96f6867312 2566 // IR Remote Control transmitter & receiver
mjr 77:0b96f6867312 2567 //
mjr 77:0b96f6867312 2568
mjr 77:0b96f6867312 2569 // receiver
mjr 77:0b96f6867312 2570 IRReceiver *ir_rx;
mjr 77:0b96f6867312 2571
mjr 77:0b96f6867312 2572 // transmitter
mjr 77:0b96f6867312 2573 IRTransmitter *ir_tx;
mjr 77:0b96f6867312 2574
mjr 77:0b96f6867312 2575 // Mapping from IR commands slots in the configuration to "virtual button"
mjr 77:0b96f6867312 2576 // numbers on the IRTransmitter's "virtual remote". To minimize RAM usage,
mjr 77:0b96f6867312 2577 // we only create virtual buttons on the transmitter object for code slots
mjr 77:0b96f6867312 2578 // that are configured for transmission, which includes slots used for TV
mjr 77:0b96f6867312 2579 // ON commands and slots that can be triggered by button presses. This
mjr 77:0b96f6867312 2580 // means that virtual button numbers won't necessarily match the config
mjr 77:0b96f6867312 2581 // slot numbers. This table provides the mapping:
mjr 77:0b96f6867312 2582 // IRConfigSlotToVirtualButton[n] = ir_tx virtual button number for
mjr 77:0b96f6867312 2583 // configuration slot n
mjr 77:0b96f6867312 2584 uint8_t IRConfigSlotToVirtualButton[MAX_IR_CODES];
mjr 78:1e00b3fa11af 2585
mjr 78:1e00b3fa11af 2586 // IR transmitter virtual button number for ad hoc IR command. We allocate
mjr 78:1e00b3fa11af 2587 // one virtual button for sending ad hoc IR codes, such as through the USB
mjr 78:1e00b3fa11af 2588 // protocol.
mjr 78:1e00b3fa11af 2589 uint8_t IRAdHocBtn;
mjr 78:1e00b3fa11af 2590
mjr 78:1e00b3fa11af 2591 // Staging area for ad hoc IR commands. It takes multiple messages
mjr 78:1e00b3fa11af 2592 // to fill out an IR command, so we store the partial command here
mjr 78:1e00b3fa11af 2593 // while waiting for the rest.
mjr 78:1e00b3fa11af 2594 static struct
mjr 78:1e00b3fa11af 2595 {
mjr 78:1e00b3fa11af 2596 uint8_t protocol; // protocol ID
mjr 78:1e00b3fa11af 2597 uint64_t code; // code
mjr 78:1e00b3fa11af 2598 uint8_t dittos : 1; // using dittos?
mjr 78:1e00b3fa11af 2599 uint8_t ready : 1; // do we have a code ready to transmit?
mjr 78:1e00b3fa11af 2600 } IRAdHocCmd;
mjr 88:98bce687e6c0 2601
mjr 77:0b96f6867312 2602
mjr 77:0b96f6867312 2603 // IR mode timer. In normal mode, this is the time since the last
mjr 77:0b96f6867312 2604 // command received; we use this to handle commands with timed effects,
mjr 77:0b96f6867312 2605 // such as sending a key to the PC. In learning mode, this is the time
mjr 77:0b96f6867312 2606 // since we activated learning mode, which we use to automatically end
mjr 77:0b96f6867312 2607 // learning mode if a decodable command isn't received within a reasonable
mjr 77:0b96f6867312 2608 // amount of time.
mjr 77:0b96f6867312 2609 Timer IRTimer;
mjr 77:0b96f6867312 2610
mjr 77:0b96f6867312 2611 // IR Learning Mode. The PC enters learning mode via special function 65 12.
mjr 77:0b96f6867312 2612 // The states are:
mjr 77:0b96f6867312 2613 //
mjr 77:0b96f6867312 2614 // 0 -> normal operation (not in learning mode)
mjr 77:0b96f6867312 2615 // 1 -> learning mode; reading raw codes, no command read yet
mjr 77:0b96f6867312 2616 // 2 -> learning mode; command received, awaiting auto-repeat
mjr 77:0b96f6867312 2617 // 3 -> learning mode; done, command and repeat mode decoded
mjr 77:0b96f6867312 2618 //
mjr 77:0b96f6867312 2619 // When we enter learning mode, we reset IRTimer to keep track of how long
mjr 77:0b96f6867312 2620 // we've been in the mode. This allows the mode to time out if no code is
mjr 77:0b96f6867312 2621 // received within a reasonable time.
mjr 77:0b96f6867312 2622 uint8_t IRLearningMode = 0;
mjr 77:0b96f6867312 2623
mjr 77:0b96f6867312 2624 // Learning mode command received. This stores the first decoded command
mjr 77:0b96f6867312 2625 // when in learning mode. For some protocols, we can't just report the
mjr 77:0b96f6867312 2626 // first command we receive, because we need to wait for an auto-repeat to
mjr 77:0b96f6867312 2627 // determine what format the remote uses for repeats. This stores the first
mjr 77:0b96f6867312 2628 // command while we await a repeat. This is necessary for protocols that
mjr 77:0b96f6867312 2629 // have "dittos", since some remotes for such protocols use the dittos and
mjr 77:0b96f6867312 2630 // some don't; the only way to find out is to read a repeat code and see if
mjr 77:0b96f6867312 2631 // it's a ditto or just a repeat of the full code.
mjr 77:0b96f6867312 2632 IRCommand learnedIRCode;
mjr 77:0b96f6867312 2633
mjr 78:1e00b3fa11af 2634 // IR command received, as a config slot index, 1..MAX_IR_CODES.
mjr 77:0b96f6867312 2635 // When we receive a command that matches one of our programmed commands,
mjr 77:0b96f6867312 2636 // we note the slot here. We also reset the IR timer so that we know how
mjr 77:0b96f6867312 2637 // long it's been since the command came in. This lets us handle commands
mjr 77:0b96f6867312 2638 // with timed effects, such as PC key input. Note that this is a 1-based
mjr 77:0b96f6867312 2639 // index; 0 represents no command.
mjr 77:0b96f6867312 2640 uint8_t IRCommandIn = 0;
mjr 77:0b96f6867312 2641
mjr 77:0b96f6867312 2642 // "Toggle bit" of last command. Some IR protocols have a toggle bit
mjr 77:0b96f6867312 2643 // that distinguishes an auto-repeating key from a key being pressed
mjr 77:0b96f6867312 2644 // several times in a row. This records the toggle bit of the last
mjr 77:0b96f6867312 2645 // command we received.
mjr 77:0b96f6867312 2646 uint8_t lastIRToggle = 0;
mjr 77:0b96f6867312 2647
mjr 77:0b96f6867312 2648 // Are we in a gap between successive key presses? When we detect that a
mjr 77:0b96f6867312 2649 // key is being pressed multiple times rather than auto-repeated (which we
mjr 77:0b96f6867312 2650 // can detect via a toggle bit in some protocols), we'll briefly stop sending
mjr 77:0b96f6867312 2651 // the associated key to the PC, so that the PC likewise recognizes the
mjr 77:0b96f6867312 2652 // distinct key press.
mjr 77:0b96f6867312 2653 uint8_t IRKeyGap = false;
mjr 77:0b96f6867312 2654
mjr 78:1e00b3fa11af 2655
mjr 77:0b96f6867312 2656 // initialize
mjr 77:0b96f6867312 2657 void init_IR(Config &cfg, bool &kbKeys)
mjr 77:0b96f6867312 2658 {
mjr 77:0b96f6867312 2659 PinName pin;
mjr 77:0b96f6867312 2660
mjr 77:0b96f6867312 2661 // start the IR timer
mjr 77:0b96f6867312 2662 IRTimer.start();
mjr 77:0b96f6867312 2663
mjr 77:0b96f6867312 2664 // if there's a transmitter, set it up
mjr 77:0b96f6867312 2665 if ((pin = wirePinName(cfg.IR.emitter)) != NC)
mjr 77:0b96f6867312 2666 {
mjr 77:0b96f6867312 2667 // no virtual buttons yet
mjr 77:0b96f6867312 2668 int nVirtualButtons = 0;
mjr 77:0b96f6867312 2669 memset(IRConfigSlotToVirtualButton, 0xFF, sizeof(IRConfigSlotToVirtualButton));
mjr 77:0b96f6867312 2670
mjr 77:0b96f6867312 2671 // assign virtual buttons slots for TV ON codes
mjr 77:0b96f6867312 2672 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2673 {
mjr 77:0b96f6867312 2674 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 2675 IRConfigSlotToVirtualButton[i] = nVirtualButtons++;
mjr 77:0b96f6867312 2676 }
mjr 77:0b96f6867312 2677
mjr 77:0b96f6867312 2678 // assign virtual buttons for codes that can be triggered by
mjr 77:0b96f6867312 2679 // real button inputs
mjr 77:0b96f6867312 2680 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 77:0b96f6867312 2681 {
mjr 77:0b96f6867312 2682 // get the button
mjr 77:0b96f6867312 2683 ButtonCfg &b = cfg.button[i];
mjr 77:0b96f6867312 2684
mjr 77:0b96f6867312 2685 // check the unshifted button
mjr 77:0b96f6867312 2686 int c = b.IRCommand - 1;
mjr 77:0b96f6867312 2687 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2688 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2689 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2690
mjr 77:0b96f6867312 2691 // check the shifted button
mjr 77:0b96f6867312 2692 c = b.IRCommand2 - 1;
mjr 77:0b96f6867312 2693 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2694 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2695 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2696 }
mjr 77:0b96f6867312 2697
mjr 77:0b96f6867312 2698 // allocate an additional virtual button for transmitting ad hoc
mjr 77:0b96f6867312 2699 // codes, such as for the "send code" USB API function
mjr 78:1e00b3fa11af 2700 IRAdHocBtn = nVirtualButtons++;
mjr 77:0b96f6867312 2701
mjr 77:0b96f6867312 2702 // create the transmitter
mjr 77:0b96f6867312 2703 ir_tx = new IRTransmitter(pin, nVirtualButtons);
mjr 77:0b96f6867312 2704
mjr 77:0b96f6867312 2705 // program the commands into the virtual button slots
mjr 77:0b96f6867312 2706 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2707 {
mjr 77:0b96f6867312 2708 // if this slot is assigned to a virtual button, program it
mjr 77:0b96f6867312 2709 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 2710 if (vb != 0xFF)
mjr 77:0b96f6867312 2711 {
mjr 77:0b96f6867312 2712 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2713 uint64_t code = cb.code.lo | (uint64_t(cb.code.hi) << 32);
mjr 77:0b96f6867312 2714 bool dittos = (cb.flags & IRFlagDittos) != 0;
mjr 77:0b96f6867312 2715 ir_tx->programButton(vb, cb.protocol, dittos, code);
mjr 77:0b96f6867312 2716 }
mjr 77:0b96f6867312 2717 }
mjr 77:0b96f6867312 2718 }
mjr 77:0b96f6867312 2719
mjr 77:0b96f6867312 2720 // if there's a receiver, set it up
mjr 77:0b96f6867312 2721 if ((pin = wirePinName(cfg.IR.sensor)) != NC)
mjr 77:0b96f6867312 2722 {
mjr 77:0b96f6867312 2723 // create the receiver
mjr 77:0b96f6867312 2724 ir_rx = new IRReceiver(pin, 32);
mjr 77:0b96f6867312 2725
mjr 77:0b96f6867312 2726 // connect the transmitter (if any) to the receiver, so that
mjr 77:0b96f6867312 2727 // the receiver can suppress reception of our own transmissions
mjr 77:0b96f6867312 2728 ir_rx->setTransmitter(ir_tx);
mjr 77:0b96f6867312 2729
mjr 77:0b96f6867312 2730 // enable it
mjr 77:0b96f6867312 2731 ir_rx->enable();
mjr 77:0b96f6867312 2732
mjr 77:0b96f6867312 2733 // Check the IR command slots to see if any slots are configured
mjr 77:0b96f6867312 2734 // to send a keyboard key on receiving an IR command. If any are,
mjr 77:0b96f6867312 2735 // tell the caller that we need a USB keyboard interface.
mjr 77:0b96f6867312 2736 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2737 {
mjr 77:0b96f6867312 2738 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2739 if (cb.protocol != 0
mjr 77:0b96f6867312 2740 && (cb.keytype == BtnTypeKey || cb.keytype == BtnTypeMedia))
mjr 77:0b96f6867312 2741 {
mjr 77:0b96f6867312 2742 kbKeys = true;
mjr 77:0b96f6867312 2743 break;
mjr 77:0b96f6867312 2744 }
mjr 77:0b96f6867312 2745 }
mjr 77:0b96f6867312 2746 }
mjr 77:0b96f6867312 2747 }
mjr 77:0b96f6867312 2748
mjr 77:0b96f6867312 2749 // Press or release a button with an assigned IR function. 'cmd'
mjr 77:0b96f6867312 2750 // is the command slot number (1..MAX_IR_CODES) assigned to the button.
mjr 77:0b96f6867312 2751 void IR_buttonChange(uint8_t cmd, bool pressed)
mjr 77:0b96f6867312 2752 {
mjr 77:0b96f6867312 2753 // only proceed if there's an IR transmitter attached
mjr 77:0b96f6867312 2754 if (ir_tx != 0)
mjr 77:0b96f6867312 2755 {
mjr 77:0b96f6867312 2756 // adjust the command slot to a zero-based index
mjr 77:0b96f6867312 2757 int slot = cmd - 1;
mjr 77:0b96f6867312 2758
mjr 77:0b96f6867312 2759 // press or release the virtual button
mjr 77:0b96f6867312 2760 ir_tx->pushButton(IRConfigSlotToVirtualButton[slot], pressed);
mjr 77:0b96f6867312 2761 }
mjr 77:0b96f6867312 2762 }
mjr 77:0b96f6867312 2763
mjr 78:1e00b3fa11af 2764 // Process IR input and output
mjr 77:0b96f6867312 2765 void process_IR(Config &cfg, USBJoystick &js)
mjr 77:0b96f6867312 2766 {
mjr 78:1e00b3fa11af 2767 // check for transmitter tasks, if there's a transmitter
mjr 78:1e00b3fa11af 2768 if (ir_tx != 0)
mjr 77:0b96f6867312 2769 {
mjr 78:1e00b3fa11af 2770 // If we're not currently sending, and an ad hoc IR command
mjr 78:1e00b3fa11af 2771 // is ready to send, send it.
mjr 78:1e00b3fa11af 2772 if (!ir_tx->isSending() && IRAdHocCmd.ready)
mjr 78:1e00b3fa11af 2773 {
mjr 78:1e00b3fa11af 2774 // program the command into the transmitter virtual button
mjr 78:1e00b3fa11af 2775 // that we reserved for ad hoc commands
mjr 78:1e00b3fa11af 2776 ir_tx->programButton(IRAdHocBtn, IRAdHocCmd.protocol,
mjr 78:1e00b3fa11af 2777 IRAdHocCmd.dittos, IRAdHocCmd.code);
mjr 78:1e00b3fa11af 2778
mjr 78:1e00b3fa11af 2779 // send the command - just pulse the button to send it once
mjr 78:1e00b3fa11af 2780 ir_tx->pushButton(IRAdHocBtn, true);
mjr 78:1e00b3fa11af 2781 ir_tx->pushButton(IRAdHocBtn, false);
mjr 78:1e00b3fa11af 2782
mjr 78:1e00b3fa11af 2783 // we've sent the command, so clear the 'ready' flag
mjr 78:1e00b3fa11af 2784 IRAdHocCmd.ready = false;
mjr 78:1e00b3fa11af 2785 }
mjr 77:0b96f6867312 2786 }
mjr 78:1e00b3fa11af 2787
mjr 78:1e00b3fa11af 2788 // check for receiver tasks, if there's a receiver
mjr 78:1e00b3fa11af 2789 if (ir_rx != 0)
mjr 77:0b96f6867312 2790 {
mjr 78:1e00b3fa11af 2791 // Time out any received command
mjr 78:1e00b3fa11af 2792 if (IRCommandIn != 0)
mjr 78:1e00b3fa11af 2793 {
mjr 80:94dc2946871b 2794 // Time out commands after 200ms without a repeat signal.
mjr 80:94dc2946871b 2795 // Time out the inter-key gap after 50ms.
mjr 78:1e00b3fa11af 2796 uint32_t t = IRTimer.read_us();
mjr 80:94dc2946871b 2797 if (t > 200000)
mjr 78:1e00b3fa11af 2798 IRCommandIn = 0;
mjr 80:94dc2946871b 2799 else if (t > 50000)
mjr 78:1e00b3fa11af 2800 IRKeyGap = false;
mjr 78:1e00b3fa11af 2801 }
mjr 78:1e00b3fa11af 2802
mjr 78:1e00b3fa11af 2803 // Check if we're in learning mode
mjr 78:1e00b3fa11af 2804 if (IRLearningMode != 0)
mjr 78:1e00b3fa11af 2805 {
mjr 78:1e00b3fa11af 2806 // Learning mode. Read raw inputs from the IR sensor and
mjr 78:1e00b3fa11af 2807 // forward them to the PC via USB reports, up to the report
mjr 78:1e00b3fa11af 2808 // limit.
mjr 78:1e00b3fa11af 2809 const int nmax = USBJoystick::maxRawIR;
mjr 78:1e00b3fa11af 2810 uint16_t raw[nmax];
mjr 78:1e00b3fa11af 2811 int n;
mjr 78:1e00b3fa11af 2812 for (n = 0 ; n < nmax && ir_rx->processOne(raw[n]) ; ++n) ;
mjr 77:0b96f6867312 2813
mjr 78:1e00b3fa11af 2814 // if we read any raw samples, report them
mjr 78:1e00b3fa11af 2815 if (n != 0)
mjr 78:1e00b3fa11af 2816 js.reportRawIR(n, raw);
mjr 77:0b96f6867312 2817
mjr 78:1e00b3fa11af 2818 // check for a command
mjr 78:1e00b3fa11af 2819 IRCommand c;
mjr 78:1e00b3fa11af 2820 if (ir_rx->readCommand(c))
mjr 78:1e00b3fa11af 2821 {
mjr 78:1e00b3fa11af 2822 // check the current learning state
mjr 78:1e00b3fa11af 2823 switch (IRLearningMode)
mjr 78:1e00b3fa11af 2824 {
mjr 78:1e00b3fa11af 2825 case 1:
mjr 78:1e00b3fa11af 2826 // Initial state, waiting for the first decoded command.
mjr 78:1e00b3fa11af 2827 // This is it.
mjr 78:1e00b3fa11af 2828 learnedIRCode = c;
mjr 78:1e00b3fa11af 2829
mjr 78:1e00b3fa11af 2830 // Check if we need additional information. If the
mjr 78:1e00b3fa11af 2831 // protocol supports dittos, we have to wait for a repeat
mjr 78:1e00b3fa11af 2832 // to see if the remote actually uses the dittos, since
mjr 78:1e00b3fa11af 2833 // some implementations of such protocols use the dittos
mjr 78:1e00b3fa11af 2834 // while others just send repeated full codes. Otherwise,
mjr 78:1e00b3fa11af 2835 // all we need is the initial code, so we're done.
mjr 78:1e00b3fa11af 2836 IRLearningMode = (c.hasDittos ? 2 : 3);
mjr 78:1e00b3fa11af 2837 break;
mjr 78:1e00b3fa11af 2838
mjr 78:1e00b3fa11af 2839 case 2:
mjr 78:1e00b3fa11af 2840 // Code received, awaiting auto-repeat information. If
mjr 78:1e00b3fa11af 2841 // the protocol has dittos, check to see if we got a ditto:
mjr 78:1e00b3fa11af 2842 //
mjr 78:1e00b3fa11af 2843 // - If we received a ditto in the same protocol as the
mjr 78:1e00b3fa11af 2844 // prior command, the remote uses dittos.
mjr 78:1e00b3fa11af 2845 //
mjr 78:1e00b3fa11af 2846 // - If we received a repeat of the prior command (not a
mjr 78:1e00b3fa11af 2847 // ditto, but a repeat of the full code), the remote
mjr 78:1e00b3fa11af 2848 // doesn't use dittos even though the protocol supports
mjr 78:1e00b3fa11af 2849 // them.
mjr 78:1e00b3fa11af 2850 //
mjr 78:1e00b3fa11af 2851 // - Otherwise, it's not an auto-repeat at all, so we
mjr 78:1e00b3fa11af 2852 // can't decide one way or the other on dittos: start
mjr 78:1e00b3fa11af 2853 // over.
mjr 78:1e00b3fa11af 2854 if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2855 && c.hasDittos
mjr 78:1e00b3fa11af 2856 && c.ditto)
mjr 78:1e00b3fa11af 2857 {
mjr 78:1e00b3fa11af 2858 // success - the remote uses dittos
mjr 78:1e00b3fa11af 2859 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2860 }
mjr 78:1e00b3fa11af 2861 else if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2862 && c.hasDittos
mjr 78:1e00b3fa11af 2863 && !c.ditto
mjr 78:1e00b3fa11af 2864 && c.code == learnedIRCode.code)
mjr 78:1e00b3fa11af 2865 {
mjr 78:1e00b3fa11af 2866 // success - it's a repeat of the last code, so
mjr 78:1e00b3fa11af 2867 // the remote doesn't use dittos even though the
mjr 78:1e00b3fa11af 2868 // protocol supports them
mjr 78:1e00b3fa11af 2869 learnedIRCode.hasDittos = false;
mjr 78:1e00b3fa11af 2870 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2871 }
mjr 78:1e00b3fa11af 2872 else
mjr 78:1e00b3fa11af 2873 {
mjr 78:1e00b3fa11af 2874 // It's not a ditto and not a full repeat of the
mjr 78:1e00b3fa11af 2875 // last code, so it's either a new key, or some kind
mjr 78:1e00b3fa11af 2876 // of multi-code key encoding that we don't recognize.
mjr 78:1e00b3fa11af 2877 // We can't use this code, so start over.
mjr 78:1e00b3fa11af 2878 IRLearningMode = 1;
mjr 78:1e00b3fa11af 2879 }
mjr 78:1e00b3fa11af 2880 break;
mjr 78:1e00b3fa11af 2881 }
mjr 77:0b96f6867312 2882
mjr 78:1e00b3fa11af 2883 // If we ended in state 3, we've successfully decoded
mjr 78:1e00b3fa11af 2884 // the transmission. Report the decoded data and terminate
mjr 78:1e00b3fa11af 2885 // learning mode.
mjr 78:1e00b3fa11af 2886 if (IRLearningMode == 3)
mjr 77:0b96f6867312 2887 {
mjr 78:1e00b3fa11af 2888 // figure the flags:
mjr 78:1e00b3fa11af 2889 // 0x02 -> dittos
mjr 78:1e00b3fa11af 2890 uint8_t flags = 0;
mjr 78:1e00b3fa11af 2891 if (learnedIRCode.hasDittos)
mjr 78:1e00b3fa11af 2892 flags |= 0x02;
mjr 78:1e00b3fa11af 2893
mjr 78:1e00b3fa11af 2894 // report the code
mjr 78:1e00b3fa11af 2895 js.reportIRCode(learnedIRCode.proId, flags, learnedIRCode.code);
mjr 78:1e00b3fa11af 2896
mjr 78:1e00b3fa11af 2897 // exit learning mode
mjr 78:1e00b3fa11af 2898 IRLearningMode = 0;
mjr 77:0b96f6867312 2899 }
mjr 77:0b96f6867312 2900 }
mjr 77:0b96f6867312 2901
mjr 78:1e00b3fa11af 2902 // time out of IR learning mode if it's been too long
mjr 78:1e00b3fa11af 2903 if (IRLearningMode != 0 && IRTimer.read_us() > 10000000L)
mjr 77:0b96f6867312 2904 {
mjr 78:1e00b3fa11af 2905 // report the termination by sending a raw IR report with
mjr 78:1e00b3fa11af 2906 // zero data elements
mjr 78:1e00b3fa11af 2907 js.reportRawIR(0, 0);
mjr 78:1e00b3fa11af 2908
mjr 78:1e00b3fa11af 2909
mjr 78:1e00b3fa11af 2910 // cancel learning mode
mjr 77:0b96f6867312 2911 IRLearningMode = 0;
mjr 77:0b96f6867312 2912 }
mjr 77:0b96f6867312 2913 }
mjr 78:1e00b3fa11af 2914 else
mjr 77:0b96f6867312 2915 {
mjr 78:1e00b3fa11af 2916 // Not in learning mode. We don't care about the raw signals;
mjr 78:1e00b3fa11af 2917 // just run them through the protocol decoders.
mjr 78:1e00b3fa11af 2918 ir_rx->process();
mjr 78:1e00b3fa11af 2919
mjr 78:1e00b3fa11af 2920 // Check for decoded commands. Keep going until all commands
mjr 78:1e00b3fa11af 2921 // have been read.
mjr 78:1e00b3fa11af 2922 IRCommand c;
mjr 78:1e00b3fa11af 2923 while (ir_rx->readCommand(c))
mjr 77:0b96f6867312 2924 {
mjr 78:1e00b3fa11af 2925 // We received a decoded command. Determine if it's a repeat,
mjr 78:1e00b3fa11af 2926 // and if so, try to determine whether it's an auto-repeat (due
mjr 78:1e00b3fa11af 2927 // to the remote key being held down) or a distinct new press
mjr 78:1e00b3fa11af 2928 // on the same key as last time. The distinction is significant
mjr 78:1e00b3fa11af 2929 // because it affects the auto-repeat behavior of the PC key
mjr 78:1e00b3fa11af 2930 // input. An auto-repeat represents a key being held down on
mjr 78:1e00b3fa11af 2931 // the remote, which we want to translate to a (virtual) key
mjr 78:1e00b3fa11af 2932 // being held down on the PC keyboard; a distinct key press on
mjr 78:1e00b3fa11af 2933 // the remote translates to a distinct key press on the PC.
mjr 78:1e00b3fa11af 2934 //
mjr 78:1e00b3fa11af 2935 // It can only be a repeat if there's a prior command that
mjr 78:1e00b3fa11af 2936 // hasn't timed out yet, so start by checking for a previous
mjr 78:1e00b3fa11af 2937 // command.
mjr 78:1e00b3fa11af 2938 bool repeat = false, autoRepeat = false;
mjr 78:1e00b3fa11af 2939 if (IRCommandIn != 0)
mjr 77:0b96f6867312 2940 {
mjr 78:1e00b3fa11af 2941 // We have a command in progress. Check to see if the
mjr 78:1e00b3fa11af 2942 // new command is a repeat of the previous command. Check
mjr 78:1e00b3fa11af 2943 // first to see if it's a "ditto", which explicitly represents
mjr 78:1e00b3fa11af 2944 // an auto-repeat of the last command.
mjr 78:1e00b3fa11af 2945 IRCommandCfg &cmdcfg = cfg.IRCommand[IRCommandIn - 1];
mjr 78:1e00b3fa11af 2946 if (c.ditto)
mjr 78:1e00b3fa11af 2947 {
mjr 78:1e00b3fa11af 2948 // We received a ditto. Dittos are always auto-
mjr 78:1e00b3fa11af 2949 // repeats, so it's an auto-repeat as long as the
mjr 78:1e00b3fa11af 2950 // ditto is in the same protocol as the last command.
mjr 78:1e00b3fa11af 2951 // If the ditto is in a new protocol, the ditto can't
mjr 78:1e00b3fa11af 2952 // be for the last command we saw, because a ditto
mjr 78:1e00b3fa11af 2953 // never changes protocols from its antecedent. In
mjr 78:1e00b3fa11af 2954 // such a case, we must have missed the antecedent
mjr 78:1e00b3fa11af 2955 // command and thus don't know what's being repeated.
mjr 78:1e00b3fa11af 2956 repeat = autoRepeat = (c.proId == cmdcfg.protocol);
mjr 78:1e00b3fa11af 2957 }
mjr 78:1e00b3fa11af 2958 else
mjr 78:1e00b3fa11af 2959 {
mjr 78:1e00b3fa11af 2960 // It's not a ditto. The new command is a repeat if
mjr 78:1e00b3fa11af 2961 // it matches the protocol and command code of the
mjr 78:1e00b3fa11af 2962 // prior command.
mjr 78:1e00b3fa11af 2963 repeat = (c.proId == cmdcfg.protocol
mjr 78:1e00b3fa11af 2964 && uint32_t(c.code) == cmdcfg.code.lo
mjr 78:1e00b3fa11af 2965 && uint32_t(c.code >> 32) == cmdcfg.code.hi);
mjr 78:1e00b3fa11af 2966
mjr 78:1e00b3fa11af 2967 // If the command is a repeat, try to determine whether
mjr 78:1e00b3fa11af 2968 // it's an auto-repeat or a new press on the same key.
mjr 78:1e00b3fa11af 2969 // If the protocol uses dittos, it's definitely a new
mjr 78:1e00b3fa11af 2970 // key press, because an auto-repeat would have used a
mjr 78:1e00b3fa11af 2971 // ditto. For a protocol that doesn't use dittos, both
mjr 78:1e00b3fa11af 2972 // an auto-repeat and a new key press just send the key
mjr 78:1e00b3fa11af 2973 // code again, so we can't tell the difference based on
mjr 78:1e00b3fa11af 2974 // that alone. But if the protocol has a toggle bit, we
mjr 78:1e00b3fa11af 2975 // can tell by the toggle bit value: a new key press has
mjr 78:1e00b3fa11af 2976 // the opposite toggle value as the last key press, while
mjr 78:1e00b3fa11af 2977 // an auto-repeat has the same toggle. Note that if the
mjr 78:1e00b3fa11af 2978 // protocol doesn't use toggle bits, the toggle value
mjr 78:1e00b3fa11af 2979 // will always be the same, so we'll simply always treat
mjr 78:1e00b3fa11af 2980 // any repeat as an auto-repeat. Many protocols simply
mjr 78:1e00b3fa11af 2981 // provide no way to distinguish the two, so in such
mjr 78:1e00b3fa11af 2982 // cases it's consistent with the native implementations
mjr 78:1e00b3fa11af 2983 // to treat any repeat as an auto-repeat.
mjr 78:1e00b3fa11af 2984 autoRepeat =
mjr 78:1e00b3fa11af 2985 repeat
mjr 78:1e00b3fa11af 2986 && !(cmdcfg.flags & IRFlagDittos)
mjr 78:1e00b3fa11af 2987 && c.toggle == lastIRToggle;
mjr 78:1e00b3fa11af 2988 }
mjr 78:1e00b3fa11af 2989 }
mjr 78:1e00b3fa11af 2990
mjr 78:1e00b3fa11af 2991 // Check to see if it's a repeat of any kind
mjr 78:1e00b3fa11af 2992 if (repeat)
mjr 78:1e00b3fa11af 2993 {
mjr 78:1e00b3fa11af 2994 // It's a repeat. If it's not an auto-repeat, it's a
mjr 78:1e00b3fa11af 2995 // new distinct key press, so we need to send the PC a
mjr 78:1e00b3fa11af 2996 // momentary gap where we're not sending the same key,
mjr 78:1e00b3fa11af 2997 // so that the PC also recognizes this as a distinct
mjr 78:1e00b3fa11af 2998 // key press event.
mjr 78:1e00b3fa11af 2999 if (!autoRepeat)
mjr 78:1e00b3fa11af 3000 IRKeyGap = true;
mjr 78:1e00b3fa11af 3001
mjr 78:1e00b3fa11af 3002 // restart the key-up timer
mjr 78:1e00b3fa11af 3003 IRTimer.reset();
mjr 78:1e00b3fa11af 3004 }
mjr 78:1e00b3fa11af 3005 else if (c.ditto)
mjr 78:1e00b3fa11af 3006 {
mjr 78:1e00b3fa11af 3007 // It's a ditto, but not a repeat of the last command.
mjr 78:1e00b3fa11af 3008 // But a ditto doesn't contain any information of its own
mjr 78:1e00b3fa11af 3009 // on the command being repeated, so given that it's not
mjr 78:1e00b3fa11af 3010 // our last command, we can't infer what command the ditto
mjr 78:1e00b3fa11af 3011 // is for and thus can't make sense of it. We have to
mjr 78:1e00b3fa11af 3012 // simply ignore it and wait for the sender to start with
mjr 78:1e00b3fa11af 3013 // a full command for a new key press.
mjr 78:1e00b3fa11af 3014 IRCommandIn = 0;
mjr 77:0b96f6867312 3015 }
mjr 77:0b96f6867312 3016 else
mjr 77:0b96f6867312 3017 {
mjr 78:1e00b3fa11af 3018 // It's not a repeat, so the last command is no longer
mjr 78:1e00b3fa11af 3019 // in effect (regardless of whether we find a match for
mjr 78:1e00b3fa11af 3020 // the new command).
mjr 78:1e00b3fa11af 3021 IRCommandIn = 0;
mjr 77:0b96f6867312 3022
mjr 78:1e00b3fa11af 3023 // Check to see if we recognize the new command, by
mjr 78:1e00b3fa11af 3024 // searching for a match in our learned code list.
mjr 78:1e00b3fa11af 3025 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 3026 {
mjr 78:1e00b3fa11af 3027 // if the protocol and command code from the code
mjr 78:1e00b3fa11af 3028 // list both match the input, it's a match
mjr 78:1e00b3fa11af 3029 IRCommandCfg &cmdcfg = cfg.IRCommand[i];
mjr 78:1e00b3fa11af 3030 if (cmdcfg.protocol == c.proId
mjr 78:1e00b3fa11af 3031 && cmdcfg.code.lo == uint32_t(c.code)
mjr 78:1e00b3fa11af 3032 && cmdcfg.code.hi == uint32_t(c.code >> 32))
mjr 78:1e00b3fa11af 3033 {
mjr 78:1e00b3fa11af 3034 // Found it! Make this the last command, and
mjr 78:1e00b3fa11af 3035 // remember the starting time.
mjr 78:1e00b3fa11af 3036 IRCommandIn = i + 1;
mjr 78:1e00b3fa11af 3037 lastIRToggle = c.toggle;
mjr 78:1e00b3fa11af 3038 IRTimer.reset();
mjr 78:1e00b3fa11af 3039
mjr 78:1e00b3fa11af 3040 // no need to keep searching
mjr 78:1e00b3fa11af 3041 break;
mjr 78:1e00b3fa11af 3042 }
mjr 77:0b96f6867312 3043 }
mjr 77:0b96f6867312 3044 }
mjr 77:0b96f6867312 3045 }
mjr 77:0b96f6867312 3046 }
mjr 77:0b96f6867312 3047 }
mjr 77:0b96f6867312 3048 }
mjr 77:0b96f6867312 3049
mjr 74:822a92bc11d2 3050
mjr 11:bd9da7088e6e 3051 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 3052 //
mjr 11:bd9da7088e6e 3053 // Button input
mjr 11:bd9da7088e6e 3054 //
mjr 11:bd9da7088e6e 3055
mjr 18:5e890ebd0023 3056 // button state
mjr 18:5e890ebd0023 3057 struct ButtonState
mjr 18:5e890ebd0023 3058 {
mjr 38:091e511ce8a0 3059 ButtonState()
mjr 38:091e511ce8a0 3060 {
mjr 53:9b2611964afc 3061 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 3062 virtState = 0;
mjr 53:9b2611964afc 3063 dbState = 0;
mjr 38:091e511ce8a0 3064 pulseState = 0;
mjr 53:9b2611964afc 3065 pulseTime = 0;
mjr 38:091e511ce8a0 3066 }
mjr 35:e959ffba78fd 3067
mjr 53:9b2611964afc 3068 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 3069 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 3070 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 3071 //
mjr 53:9b2611964afc 3072 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 3073 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 3074 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 3075 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 3076 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 3077 void virtPress(bool on)
mjr 53:9b2611964afc 3078 {
mjr 53:9b2611964afc 3079 // Increment or decrement the current state
mjr 53:9b2611964afc 3080 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 3081 }
mjr 53:9b2611964afc 3082
mjr 53:9b2611964afc 3083 // DigitalIn for the button, if connected to a physical input
mjr 73:4e8ce0b18915 3084 TinyDigitalIn di;
mjr 38:091e511ce8a0 3085
mjr 65:739875521aae 3086 // Time of last pulse state transition.
mjr 65:739875521aae 3087 //
mjr 65:739875521aae 3088 // Each state change sticks for a minimum period; when the timer expires,
mjr 65:739875521aae 3089 // if the underlying physical switch is in a different state, we switch
mjr 65:739875521aae 3090 // to the next state and restart the timer. pulseTime is the time remaining
mjr 65:739875521aae 3091 // remaining before we can make another state transition, in microseconds.
mjr 65:739875521aae 3092 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 65:739875521aae 3093 // this guarantees that the parity of the pulse count always matches the
mjr 65:739875521aae 3094 // current physical switch state when the latter is stable, which makes
mjr 65:739875521aae 3095 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 65:739875521aae 3096 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 65:739875521aae 3097 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 65:739875521aae 3098 // This software system can't be fooled that way.)
mjr 65:739875521aae 3099 uint32_t pulseTime;
mjr 18:5e890ebd0023 3100
mjr 65:739875521aae 3101 // Config key index. This points to the ButtonCfg structure in the
mjr 65:739875521aae 3102 // configuration that contains the PC key mapping for the button.
mjr 65:739875521aae 3103 uint8_t cfgIndex;
mjr 53:9b2611964afc 3104
mjr 53:9b2611964afc 3105 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 3106 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 3107 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 3108 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 3109 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 3110 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 3111 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 3112 // and physical source states.
mjr 53:9b2611964afc 3113 uint8_t virtState;
mjr 38:091e511ce8a0 3114
mjr 38:091e511ce8a0 3115 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 3116 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 3117 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 3118 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 3119 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 3120 uint8_t dbState;
mjr 38:091e511ce8a0 3121
mjr 65:739875521aae 3122 // current PHYSICAL on/off state, after debouncing
mjr 65:739875521aae 3123 uint8_t physState : 1;
mjr 65:739875521aae 3124
mjr 65:739875521aae 3125 // current LOGICAL on/off state as reported to the host.
mjr 65:739875521aae 3126 uint8_t logState : 1;
mjr 65:739875521aae 3127
mjr 79:682ae3171a08 3128 // Previous logical on/off state, when keys were last processed for USB
mjr 79:682ae3171a08 3129 // reports and local effects. This lets us detect edges (transitions)
mjr 79:682ae3171a08 3130 // in the logical state, for effects that are triggered when the state
mjr 79:682ae3171a08 3131 // changes rather than merely by the button being on or off.
mjr 65:739875521aae 3132 uint8_t prevLogState : 1;
mjr 65:739875521aae 3133
mjr 65:739875521aae 3134 // Pulse state
mjr 65:739875521aae 3135 //
mjr 65:739875521aae 3136 // A button in pulse mode (selected via the config flags for the button)
mjr 65:739875521aae 3137 // transmits a brief logical button press and release each time the attached
mjr 65:739875521aae 3138 // physical switch changes state. This is useful for cases where the host
mjr 65:739875521aae 3139 // expects a key press for each change in the state of the physical switch.
mjr 65:739875521aae 3140 // The canonical example is the Coin Door switch in VPinMAME, which requires
mjr 65:739875521aae 3141 // pressing the END key to toggle the open/closed state. This software design
mjr 65:739875521aae 3142 // isn't easily implemented in a physical coin door, though; the simplest
mjr 65:739875521aae 3143 // physical sensor for the coin door state is a switch that's on when the
mjr 65:739875521aae 3144 // door is open and off when the door is closed (or vice versa, but in either
mjr 65:739875521aae 3145 // case, the switch state corresponds to the current state of the door at any
mjr 65:739875521aae 3146 // given time, rather than pulsing on state changes). The "pulse mode"
mjr 79:682ae3171a08 3147 // option bridges this gap by generating a toggle key event each time
mjr 65:739875521aae 3148 // there's a change to the physical switch's state.
mjr 38:091e511ce8a0 3149 //
mjr 38:091e511ce8a0 3150 // Pulse state:
mjr 38:091e511ce8a0 3151 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 3152 // 1 -> off
mjr 38:091e511ce8a0 3153 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 3154 // 3 -> on
mjr 38:091e511ce8a0 3155 // 4 -> transitioning on-off
mjr 65:739875521aae 3156 uint8_t pulseState : 3; // 5 states -> we need 3 bits
mjr 65:739875521aae 3157
mjr 65:739875521aae 3158 } __attribute__((packed));
mjr 65:739875521aae 3159
mjr 65:739875521aae 3160 ButtonState *buttonState; // live button slots, allocated on startup
mjr 65:739875521aae 3161 int8_t nButtons; // number of live button slots allocated
mjr 65:739875521aae 3162 int8_t zblButtonIndex = -1; // index of ZB Launch button slot; -1 if unused
mjr 18:5e890ebd0023 3163
mjr 66:2e3583fbd2f4 3164 // Shift button state
mjr 66:2e3583fbd2f4 3165 struct
mjr 66:2e3583fbd2f4 3166 {
mjr 66:2e3583fbd2f4 3167 int8_t index; // buttonState[] index of shift button; -1 if none
mjr 78:1e00b3fa11af 3168 uint8_t state; // current state, for "Key OR Shift" mode:
mjr 66:2e3583fbd2f4 3169 // 0 = not shifted
mjr 66:2e3583fbd2f4 3170 // 1 = shift button down, no key pressed yet
mjr 66:2e3583fbd2f4 3171 // 2 = shift button down, key pressed
mjr 78:1e00b3fa11af 3172 // 3 = released, sending pulsed keystroke
mjr 78:1e00b3fa11af 3173 uint32_t pulseTime; // time remaining in pulsed keystroke (state 3)
mjr 66:2e3583fbd2f4 3174 }
mjr 66:2e3583fbd2f4 3175 __attribute__((packed)) shiftButton;
mjr 38:091e511ce8a0 3176
mjr 38:091e511ce8a0 3177 // Button data
mjr 38:091e511ce8a0 3178 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 3179
mjr 38:091e511ce8a0 3180 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 3181 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 3182 // modifier keys.
mjr 38:091e511ce8a0 3183 struct
mjr 38:091e511ce8a0 3184 {
mjr 38:091e511ce8a0 3185 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 3186 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 3187 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 3188 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 3189 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 3190
mjr 38:091e511ce8a0 3191 // Media key state
mjr 38:091e511ce8a0 3192 struct
mjr 38:091e511ce8a0 3193 {
mjr 38:091e511ce8a0 3194 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 3195 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 3196 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 3197
mjr 79:682ae3171a08 3198 // button scan interrupt timer
mjr 79:682ae3171a08 3199 Timeout scanButtonsTimeout;
mjr 38:091e511ce8a0 3200
mjr 38:091e511ce8a0 3201 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 3202 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 3203 void scanButtons()
mjr 38:091e511ce8a0 3204 {
mjr 79:682ae3171a08 3205 // schedule the next interrupt
mjr 79:682ae3171a08 3206 scanButtonsTimeout.attach_us(&scanButtons, 1000);
mjr 79:682ae3171a08 3207
mjr 38:091e511ce8a0 3208 // scan all button input pins
mjr 73:4e8ce0b18915 3209 ButtonState *bs = buttonState, *last = bs + nButtons;
mjr 73:4e8ce0b18915 3210 for ( ; bs < last ; ++bs)
mjr 38:091e511ce8a0 3211 {
mjr 73:4e8ce0b18915 3212 // Shift the new state into the debounce history
mjr 73:4e8ce0b18915 3213 uint8_t db = (bs->dbState << 1) | bs->di.read();
mjr 73:4e8ce0b18915 3214 bs->dbState = db;
mjr 73:4e8ce0b18915 3215
mjr 73:4e8ce0b18915 3216 // If we have all 0's or 1's in the history for the required
mjr 73:4e8ce0b18915 3217 // debounce period, the key state is stable, so apply the new
mjr 73:4e8ce0b18915 3218 // physical state. Note that the pins are active low, so the
mjr 73:4e8ce0b18915 3219 // new button on/off state is the inverse of the GPIO state.
mjr 73:4e8ce0b18915 3220 const uint8_t stable = 0x1F; // 00011111b -> low 5 bits = last 5 readings
mjr 73:4e8ce0b18915 3221 db &= stable;
mjr 73:4e8ce0b18915 3222 if (db == 0 || db == stable)
mjr 73:4e8ce0b18915 3223 bs->physState = !db;
mjr 38:091e511ce8a0 3224 }
mjr 38:091e511ce8a0 3225 }
mjr 38:091e511ce8a0 3226
mjr 38:091e511ce8a0 3227 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 3228 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 3229 // in the physical button state.
mjr 38:091e511ce8a0 3230 Timer buttonTimer;
mjr 12:669df364a565 3231
mjr 65:739875521aae 3232 // Count a button during the initial setup scan
mjr 72:884207c0aab0 3233 void countButton(uint8_t typ, uint8_t shiftTyp, bool &kbKeys)
mjr 65:739875521aae 3234 {
mjr 65:739875521aae 3235 // count it
mjr 65:739875521aae 3236 ++nButtons;
mjr 65:739875521aae 3237
mjr 67:c39e66c4e000 3238 // if it's a keyboard key or media key, note that we need a USB
mjr 67:c39e66c4e000 3239 // keyboard interface
mjr 72:884207c0aab0 3240 if (typ == BtnTypeKey || typ == BtnTypeMedia
mjr 72:884207c0aab0 3241 || shiftTyp == BtnTypeKey || shiftTyp == BtnTypeMedia)
mjr 65:739875521aae 3242 kbKeys = true;
mjr 65:739875521aae 3243 }
mjr 65:739875521aae 3244
mjr 11:bd9da7088e6e 3245 // initialize the button inputs
mjr 35:e959ffba78fd 3246 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 3247 {
mjr 66:2e3583fbd2f4 3248 // presume no shift key
mjr 66:2e3583fbd2f4 3249 shiftButton.index = -1;
mjr 82:4f6209cb5c33 3250 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 3251
mjr 65:739875521aae 3252 // Count up how many button slots we'll need to allocate. Start
mjr 65:739875521aae 3253 // with assigned buttons from the configuration, noting that we
mjr 65:739875521aae 3254 // only need to create slots for buttons that are actually wired.
mjr 65:739875521aae 3255 nButtons = 0;
mjr 65:739875521aae 3256 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 3257 {
mjr 65:739875521aae 3258 // it's valid if it's wired to a real input pin
mjr 65:739875521aae 3259 if (wirePinName(cfg.button[i].pin) != NC)
mjr 72:884207c0aab0 3260 countButton(cfg.button[i].typ, cfg.button[i].typ2, kbKeys);
mjr 65:739875521aae 3261 }
mjr 65:739875521aae 3262
mjr 65:739875521aae 3263 // Count virtual buttons
mjr 65:739875521aae 3264
mjr 65:739875521aae 3265 // ZB Launch
mjr 65:739875521aae 3266 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 65:739875521aae 3267 {
mjr 65:739875521aae 3268 // valid - remember the live button index
mjr 65:739875521aae 3269 zblButtonIndex = nButtons;
mjr 65:739875521aae 3270
mjr 65:739875521aae 3271 // count it
mjr 72:884207c0aab0 3272 countButton(cfg.plunger.zbLaunchBall.keytype, BtnTypeNone, kbKeys);
mjr 65:739875521aae 3273 }
mjr 65:739875521aae 3274
mjr 65:739875521aae 3275 // Allocate the live button slots
mjr 65:739875521aae 3276 ButtonState *bs = buttonState = new ButtonState[nButtons];
mjr 65:739875521aae 3277
mjr 65:739875521aae 3278 // Configure the physical inputs
mjr 65:739875521aae 3279 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 3280 {
mjr 65:739875521aae 3281 PinName pin = wirePinName(cfg.button[i].pin);
mjr 65:739875521aae 3282 if (pin != NC)
mjr 65:739875521aae 3283 {
mjr 65:739875521aae 3284 // point back to the config slot for the keyboard data
mjr 65:739875521aae 3285 bs->cfgIndex = i;
mjr 65:739875521aae 3286
mjr 65:739875521aae 3287 // set up the GPIO input pin for this button
mjr 73:4e8ce0b18915 3288 bs->di.assignPin(pin);
mjr 65:739875521aae 3289
mjr 65:739875521aae 3290 // if it's a pulse mode button, set the initial pulse state to Off
mjr 65:739875521aae 3291 if (cfg.button[i].flags & BtnFlagPulse)
mjr 65:739875521aae 3292 bs->pulseState = 1;
mjr 65:739875521aae 3293
mjr 66:2e3583fbd2f4 3294 // If this is the shift button, note its buttonState[] index.
mjr 66:2e3583fbd2f4 3295 // We have to figure the buttonState[] index separately from
mjr 66:2e3583fbd2f4 3296 // the config index, because the indices can differ if some
mjr 66:2e3583fbd2f4 3297 // config slots are left unused.
mjr 78:1e00b3fa11af 3298 if (cfg.shiftButton.idx == i+1)
mjr 66:2e3583fbd2f4 3299 shiftButton.index = bs - buttonState;
mjr 66:2e3583fbd2f4 3300
mjr 65:739875521aae 3301 // advance to the next button
mjr 65:739875521aae 3302 ++bs;
mjr 65:739875521aae 3303 }
mjr 65:739875521aae 3304 }
mjr 65:739875521aae 3305
mjr 53:9b2611964afc 3306 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 3307 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 3308 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 3309 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 3310
mjr 53:9b2611964afc 3311 // ZB Launch Ball button
mjr 65:739875521aae 3312 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 11:bd9da7088e6e 3313 {
mjr 65:739875521aae 3314 // Point back to the config slot for the keyboard data.
mjr 66:2e3583fbd2f4 3315 // We use a special extra slot for virtual buttons,
mjr 66:2e3583fbd2f4 3316 // so we also need to set up the slot data by copying
mjr 66:2e3583fbd2f4 3317 // the ZBL config data to our virtual button slot.
mjr 65:739875521aae 3318 bs->cfgIndex = ZBL_BUTTON_CFG;
mjr 65:739875521aae 3319 cfg.button[ZBL_BUTTON_CFG].pin = PINNAME_TO_WIRE(NC);
mjr 65:739875521aae 3320 cfg.button[ZBL_BUTTON_CFG].typ = cfg.plunger.zbLaunchBall.keytype;
mjr 65:739875521aae 3321 cfg.button[ZBL_BUTTON_CFG].val = cfg.plunger.zbLaunchBall.keycode;
mjr 65:739875521aae 3322
mjr 66:2e3583fbd2f4 3323 // advance to the next button
mjr 65:739875521aae 3324 ++bs;
mjr 11:bd9da7088e6e 3325 }
mjr 12:669df364a565 3326
mjr 38:091e511ce8a0 3327 // start the button scan thread
mjr 79:682ae3171a08 3328 scanButtonsTimeout.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 3329
mjr 38:091e511ce8a0 3330 // start the button state transition timer
mjr 12:669df364a565 3331 buttonTimer.start();
mjr 11:bd9da7088e6e 3332 }
mjr 11:bd9da7088e6e 3333
mjr 67:c39e66c4e000 3334 // Media key mapping. This maps from an 8-bit USB media key
mjr 67:c39e66c4e000 3335 // code to the corresponding bit in our USB report descriptor.
mjr 67:c39e66c4e000 3336 // The USB key code is the index, and the value at the index
mjr 67:c39e66c4e000 3337 // is the report descriptor bit. See joystick.cpp for the
mjr 67:c39e66c4e000 3338 // media descriptor details. Our currently mapped keys are:
mjr 67:c39e66c4e000 3339 //
mjr 67:c39e66c4e000 3340 // 0xE2 -> Mute -> 0x01
mjr 67:c39e66c4e000 3341 // 0xE9 -> Volume Up -> 0x02
mjr 67:c39e66c4e000 3342 // 0xEA -> Volume Down -> 0x04
mjr 67:c39e66c4e000 3343 // 0xB5 -> Next Track -> 0x08
mjr 67:c39e66c4e000 3344 // 0xB6 -> Previous Track -> 0x10
mjr 67:c39e66c4e000 3345 // 0xB7 -> Stop -> 0x20
mjr 67:c39e66c4e000 3346 // 0xCD -> Play / Pause -> 0x40
mjr 67:c39e66c4e000 3347 //
mjr 67:c39e66c4e000 3348 static const uint8_t mediaKeyMap[] = {
mjr 67:c39e66c4e000 3349 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-0F
mjr 67:c39e66c4e000 3350 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-1F
mjr 67:c39e66c4e000 3351 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-2F
mjr 67:c39e66c4e000 3352 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-3F
mjr 67:c39e66c4e000 3353 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-4F
mjr 67:c39e66c4e000 3354 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-5F
mjr 67:c39e66c4e000 3355 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-6F
mjr 67:c39e66c4e000 3356 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-7F
mjr 67:c39e66c4e000 3357 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
mjr 67:c39e66c4e000 3358 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-9F
mjr 67:c39e66c4e000 3359 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0-AF
mjr 67:c39e66c4e000 3360 0, 0, 0, 0, 0, 8, 16, 32, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
mjr 67:c39e66c4e000 3361 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, // C0-CF
mjr 67:c39e66c4e000 3362 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D0-DF
mjr 67:c39e66c4e000 3363 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, // E0-EF
mjr 67:c39e66c4e000 3364 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-FF
mjr 77:0b96f6867312 3365 };
mjr 77:0b96f6867312 3366
mjr 77:0b96f6867312 3367 // Keyboard key/joystick button state. processButtons() uses this to
mjr 77:0b96f6867312 3368 // build the set of key presses to report to the PC based on the logical
mjr 77:0b96f6867312 3369 // states of the button iputs.
mjr 77:0b96f6867312 3370 struct KeyState
mjr 77:0b96f6867312 3371 {
mjr 77:0b96f6867312 3372 KeyState()
mjr 77:0b96f6867312 3373 {
mjr 77:0b96f6867312 3374 // zero all members
mjr 77:0b96f6867312 3375 memset(this, 0, sizeof(*this));
mjr 77:0b96f6867312 3376 }
mjr 77:0b96f6867312 3377
mjr 77:0b96f6867312 3378 // Keyboard media keys currently pressed. This is a bit vector in
mjr 77:0b96f6867312 3379 // the format used in our USB keyboard reports (see USBJoystick.cpp).
mjr 77:0b96f6867312 3380 uint8_t mediakeys;
mjr 77:0b96f6867312 3381
mjr 77:0b96f6867312 3382 // Keyboard modifier (shift) keys currently pressed. This is a bit
mjr 77:0b96f6867312 3383 // vector in the format used in our USB keyboard reports (see
mjr 77:0b96f6867312 3384 // USBJoystick.cpp).
mjr 77:0b96f6867312 3385 uint8_t modkeys;
mjr 77:0b96f6867312 3386
mjr 77:0b96f6867312 3387 // Regular keyboard keys currently pressed. Each element is a USB
mjr 77:0b96f6867312 3388 // key code, or 0 for empty slots. Note that the USB report format
mjr 77:0b96f6867312 3389 // theoretically allows a flexible size limit, but the Windows KB
mjr 77:0b96f6867312 3390 // drivers have a fixed limit of 6 simultaneous keys (and won't
mjr 77:0b96f6867312 3391 // accept reports with more), so there's no point in making this
mjr 77:0b96f6867312 3392 // flexible; we'll just use the fixed size dictated by Windows.
mjr 77:0b96f6867312 3393 uint8_t keys[7];
mjr 77:0b96f6867312 3394
mjr 77:0b96f6867312 3395 // number of valid entries in keys[] array
mjr 77:0b96f6867312 3396 int nkeys;
mjr 77:0b96f6867312 3397
mjr 77:0b96f6867312 3398 // Joystick buttons pressed, as a bit vector. Bit n (1 << n)
mjr 77:0b96f6867312 3399 // represents joystick button n, n in 0..31, with 0 meaning
mjr 77:0b96f6867312 3400 // unpressed and 1 meaning pressed.
mjr 77:0b96f6867312 3401 uint32_t js;
mjr 77:0b96f6867312 3402
mjr 77:0b96f6867312 3403
mjr 77:0b96f6867312 3404 // Add a key press. 'typ' is the button type code (ButtonTypeXxx),
mjr 77:0b96f6867312 3405 // and 'val' is the value (the meaning of which varies by type code).
mjr 77:0b96f6867312 3406 void addKey(uint8_t typ, uint8_t val)
mjr 77:0b96f6867312 3407 {
mjr 77:0b96f6867312 3408 // add the key according to the type
mjr 77:0b96f6867312 3409 switch (typ)
mjr 77:0b96f6867312 3410 {
mjr 77:0b96f6867312 3411 case BtnTypeJoystick:
mjr 77:0b96f6867312 3412 // joystick button
mjr 77:0b96f6867312 3413 js |= (1 << (val - 1));
mjr 77:0b96f6867312 3414 break;
mjr 77:0b96f6867312 3415
mjr 77:0b96f6867312 3416 case BtnTypeKey:
mjr 77:0b96f6867312 3417 // Keyboard key. The USB keyboard report encodes regular
mjr 77:0b96f6867312 3418 // keys and modifier keys separately, so we need to check
mjr 77:0b96f6867312 3419 // which type we have. Note that past versions mapped the
mjr 77:0b96f6867312 3420 // Keyboard Volume Up, Keyboard Volume Down, and Keyboard
mjr 77:0b96f6867312 3421 // Mute keys to the corresponding Media keys. We no longer
mjr 77:0b96f6867312 3422 // do this; instead, we have the separate BtnTypeMedia for
mjr 77:0b96f6867312 3423 // explicitly using media keys if desired.
mjr 77:0b96f6867312 3424 if (val >= 0xE0 && val <= 0xE7)
mjr 77:0b96f6867312 3425 {
mjr 77:0b96f6867312 3426 // It's a modifier key. These are represented in the USB
mjr 77:0b96f6867312 3427 // reports with a bit mask. We arrange the mask bits in
mjr 77:0b96f6867312 3428 // the same order as the scan codes, so we can figure the
mjr 77:0b96f6867312 3429 // appropriate bit with a simple shift.
mjr 77:0b96f6867312 3430 modkeys |= (1 << (val - 0xE0));
mjr 77:0b96f6867312 3431 }
mjr 77:0b96f6867312 3432 else
mjr 77:0b96f6867312 3433 {
mjr 77:0b96f6867312 3434 // It's a regular key. Make sure it's not already in the
mjr 77:0b96f6867312 3435 // list, and that the list isn't full. If neither of these
mjr 77:0b96f6867312 3436 // apply, add the key to the key array.
mjr 77:0b96f6867312 3437 if (nkeys < 7)
mjr 77:0b96f6867312 3438 {
mjr 77:0b96f6867312 3439 bool found = false;
mjr 77:0b96f6867312 3440 for (int i = 0 ; i < nkeys ; ++i)
mjr 77:0b96f6867312 3441 {
mjr 77:0b96f6867312 3442 if (keys[i] == val)
mjr 77:0b96f6867312 3443 {
mjr 77:0b96f6867312 3444 found = true;
mjr 77:0b96f6867312 3445 break;
mjr 77:0b96f6867312 3446 }
mjr 77:0b96f6867312 3447 }
mjr 77:0b96f6867312 3448 if (!found)
mjr 77:0b96f6867312 3449 keys[nkeys++] = val;
mjr 77:0b96f6867312 3450 }
mjr 77:0b96f6867312 3451 }
mjr 77:0b96f6867312 3452 break;
mjr 77:0b96f6867312 3453
mjr 77:0b96f6867312 3454 case BtnTypeMedia:
mjr 77:0b96f6867312 3455 // Media control key. The media keys are mapped in the USB
mjr 77:0b96f6867312 3456 // report to bits, whereas the key codes are specified in the
mjr 77:0b96f6867312 3457 // config with their USB usage numbers. E.g., the config val
mjr 77:0b96f6867312 3458 // for Media Next Track is 0xB5, but we encode this in the USB
mjr 77:0b96f6867312 3459 // report as bit 0x08. The mediaKeyMap[] table translates
mjr 77:0b96f6867312 3460 // from the USB usage number to the mask bit. If the key isn't
mjr 77:0b96f6867312 3461 // among the subset we support, the mapped bit will be zero, so
mjr 77:0b96f6867312 3462 // the "|=" will have no effect and the key will be ignored.
mjr 77:0b96f6867312 3463 mediakeys |= mediaKeyMap[val];
mjr 77:0b96f6867312 3464 break;
mjr 77:0b96f6867312 3465 }
mjr 77:0b96f6867312 3466 }
mjr 77:0b96f6867312 3467 };
mjr 67:c39e66c4e000 3468
mjr 67:c39e66c4e000 3469
mjr 38:091e511ce8a0 3470 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 3471 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 3472 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 3473 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 3474 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 3475 {
mjr 77:0b96f6867312 3476 // key state
mjr 77:0b96f6867312 3477 KeyState ks;
mjr 38:091e511ce8a0 3478
mjr 38:091e511ce8a0 3479 // calculate the time since the last run
mjr 53:9b2611964afc 3480 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 3481 buttonTimer.reset();
mjr 66:2e3583fbd2f4 3482
mjr 66:2e3583fbd2f4 3483 // check the shift button state
mjr 66:2e3583fbd2f4 3484 if (shiftButton.index != -1)
mjr 66:2e3583fbd2f4 3485 {
mjr 78:1e00b3fa11af 3486 // get the shift button's physical state object
mjr 66:2e3583fbd2f4 3487 ButtonState *sbs = &buttonState[shiftButton.index];
mjr 78:1e00b3fa11af 3488
mjr 78:1e00b3fa11af 3489 // figure what to do based on the shift button mode in the config
mjr 78:1e00b3fa11af 3490 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 3491 {
mjr 66:2e3583fbd2f4 3492 case 0:
mjr 78:1e00b3fa11af 3493 default:
mjr 78:1e00b3fa11af 3494 // "Shift OR Key" mode. The shift button doesn't send its key
mjr 78:1e00b3fa11af 3495 // immediately when pressed. Instead, we wait to see what
mjr 78:1e00b3fa11af 3496 // happens while it's down. Check the current cycle state.
mjr 78:1e00b3fa11af 3497 switch (shiftButton.state)
mjr 78:1e00b3fa11af 3498 {
mjr 78:1e00b3fa11af 3499 case 0:
mjr 78:1e00b3fa11af 3500 // Not shifted. Check if the button is now down: if so,
mjr 78:1e00b3fa11af 3501 // switch to state 1 (shift button down, no key pressed yet).
mjr 78:1e00b3fa11af 3502 if (sbs->physState)
mjr 78:1e00b3fa11af 3503 shiftButton.state = 1;
mjr 78:1e00b3fa11af 3504 break;
mjr 78:1e00b3fa11af 3505
mjr 78:1e00b3fa11af 3506 case 1:
mjr 78:1e00b3fa11af 3507 // Shift button down, no key pressed yet. If the button is
mjr 78:1e00b3fa11af 3508 // now up, it counts as an ordinary button press instead of
mjr 78:1e00b3fa11af 3509 // a shift button press, since the shift function was never
mjr 78:1e00b3fa11af 3510 // used. Return to unshifted state and start a timed key
mjr 78:1e00b3fa11af 3511 // pulse event.
mjr 78:1e00b3fa11af 3512 if (!sbs->physState)
mjr 78:1e00b3fa11af 3513 {
mjr 78:1e00b3fa11af 3514 shiftButton.state = 3;
mjr 78:1e00b3fa11af 3515 shiftButton.pulseTime = 50000+dt; // 50 ms left on the key pulse
mjr 78:1e00b3fa11af 3516 }
mjr 78:1e00b3fa11af 3517 break;
mjr 78:1e00b3fa11af 3518
mjr 78:1e00b3fa11af 3519 case 2:
mjr 78:1e00b3fa11af 3520 // Shift button down, other key was pressed. If the button is
mjr 78:1e00b3fa11af 3521 // now up, simply clear the shift state without sending a key
mjr 78:1e00b3fa11af 3522 // press for the shift button itself to the PC. The shift
mjr 78:1e00b3fa11af 3523 // function was used, so its ordinary key press function is
mjr 78:1e00b3fa11af 3524 // suppressed.
mjr 78:1e00b3fa11af 3525 if (!sbs->physState)
mjr 78:1e00b3fa11af 3526 shiftButton.state = 0;
mjr 78:1e00b3fa11af 3527 break;
mjr 78:1e00b3fa11af 3528
mjr 78:1e00b3fa11af 3529 case 3:
mjr 78:1e00b3fa11af 3530 // Sending pulsed keystroke. Deduct the current time interval
mjr 78:1e00b3fa11af 3531 // from the remaining pulse timer. End the pulse if the time
mjr 78:1e00b3fa11af 3532 // has expired.
mjr 78:1e00b3fa11af 3533 if (shiftButton.pulseTime > dt)
mjr 78:1e00b3fa11af 3534 shiftButton.pulseTime -= dt;
mjr 78:1e00b3fa11af 3535 else
mjr 78:1e00b3fa11af 3536 shiftButton.state = 0;
mjr 78:1e00b3fa11af 3537 break;
mjr 78:1e00b3fa11af 3538 }
mjr 66:2e3583fbd2f4 3539 break;
mjr 66:2e3583fbd2f4 3540
mjr 66:2e3583fbd2f4 3541 case 1:
mjr 78:1e00b3fa11af 3542 // "Shift AND Key" mode. In this mode, the shift button acts
mjr 78:1e00b3fa11af 3543 // like any other button and sends its mapped key immediately.
mjr 78:1e00b3fa11af 3544 // The state cycle in this case simply matches the physical
mjr 78:1e00b3fa11af 3545 // state: ON -> cycle state 1, OFF -> cycle state 0.
mjr 78:1e00b3fa11af 3546 shiftButton.state = (sbs->physState ? 1 : 0);
mjr 66:2e3583fbd2f4 3547 break;
mjr 66:2e3583fbd2f4 3548 }
mjr 66:2e3583fbd2f4 3549 }
mjr 38:091e511ce8a0 3550
mjr 11:bd9da7088e6e 3551 // scan the button list
mjr 18:5e890ebd0023 3552 ButtonState *bs = buttonState;
mjr 65:739875521aae 3553 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 11:bd9da7088e6e 3554 {
mjr 77:0b96f6867312 3555 // get the config entry for the button
mjr 77:0b96f6867312 3556 ButtonCfg *bc = &cfg.button[bs->cfgIndex];
mjr 77:0b96f6867312 3557
mjr 66:2e3583fbd2f4 3558 // Check the button type:
mjr 66:2e3583fbd2f4 3559 // - shift button
mjr 66:2e3583fbd2f4 3560 // - pulsed button
mjr 66:2e3583fbd2f4 3561 // - regular button
mjr 66:2e3583fbd2f4 3562 if (shiftButton.index == i)
mjr 66:2e3583fbd2f4 3563 {
mjr 78:1e00b3fa11af 3564 // This is the shift button. The logical state handling
mjr 78:1e00b3fa11af 3565 // depends on the mode.
mjr 78:1e00b3fa11af 3566 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 3567 {
mjr 78:1e00b3fa11af 3568 case 0:
mjr 78:1e00b3fa11af 3569 default:
mjr 78:1e00b3fa11af 3570 // "Shift OR Key" mode. The logical state is ON only
mjr 78:1e00b3fa11af 3571 // during the timed pulse when the key is released, which
mjr 78:1e00b3fa11af 3572 // is signified by shift button state 3.
mjr 78:1e00b3fa11af 3573 bs->logState = (shiftButton.state == 3);
mjr 78:1e00b3fa11af 3574 break;
mjr 78:1e00b3fa11af 3575
mjr 78:1e00b3fa11af 3576 case 1:
mjr 78:1e00b3fa11af 3577 // "Shif AND Key" mode. The shift button acts like any
mjr 78:1e00b3fa11af 3578 // other button, so it's logically on when physically on.
mjr 78:1e00b3fa11af 3579 bs->logState = bs->physState;
mjr 78:1e00b3fa11af 3580 break;
mjr 66:2e3583fbd2f4 3581 }
mjr 66:2e3583fbd2f4 3582 }
mjr 66:2e3583fbd2f4 3583 else if (bs->pulseState != 0)
mjr 18:5e890ebd0023 3584 {
mjr 38:091e511ce8a0 3585 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 3586 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 3587 {
mjr 53:9b2611964afc 3588 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 3589 bs->pulseTime -= dt;
mjr 53:9b2611964afc 3590 }
mjr 53:9b2611964afc 3591 else
mjr 53:9b2611964afc 3592 {
mjr 53:9b2611964afc 3593 // pulse time expired - check for a state change
mjr 53:9b2611964afc 3594 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 3595 switch (bs->pulseState)
mjr 18:5e890ebd0023 3596 {
mjr 38:091e511ce8a0 3597 case 1:
mjr 38:091e511ce8a0 3598 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 3599 if (bs->physState)
mjr 53:9b2611964afc 3600 {
mjr 38:091e511ce8a0 3601 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 3602 bs->pulseState = 2;
mjr 53:9b2611964afc 3603 bs->logState = 1;
mjr 38:091e511ce8a0 3604 }
mjr 38:091e511ce8a0 3605 break;
mjr 18:5e890ebd0023 3606
mjr 38:091e511ce8a0 3607 case 2:
mjr 38:091e511ce8a0 3608 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 3609 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 3610 // change in state in the logical button
mjr 38:091e511ce8a0 3611 bs->pulseState = 3;
mjr 38:091e511ce8a0 3612 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 3613 bs->logState = 0;
mjr 38:091e511ce8a0 3614 break;
mjr 38:091e511ce8a0 3615
mjr 38:091e511ce8a0 3616 case 3:
mjr 38:091e511ce8a0 3617 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 3618 if (!bs->physState)
mjr 53:9b2611964afc 3619 {
mjr 38:091e511ce8a0 3620 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 3621 bs->pulseState = 4;
mjr 53:9b2611964afc 3622 bs->logState = 1;
mjr 38:091e511ce8a0 3623 }
mjr 38:091e511ce8a0 3624 break;
mjr 38:091e511ce8a0 3625
mjr 38:091e511ce8a0 3626 case 4:
mjr 38:091e511ce8a0 3627 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 3628 bs->pulseState = 1;
mjr 38:091e511ce8a0 3629 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 3630 bs->logState = 0;
mjr 38:091e511ce8a0 3631 break;
mjr 18:5e890ebd0023 3632 }
mjr 18:5e890ebd0023 3633 }
mjr 38:091e511ce8a0 3634 }
mjr 38:091e511ce8a0 3635 else
mjr 38:091e511ce8a0 3636 {
mjr 38:091e511ce8a0 3637 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 3638 bs->logState = bs->physState;
mjr 38:091e511ce8a0 3639 }
mjr 77:0b96f6867312 3640
mjr 77:0b96f6867312 3641 // Determine if we're going to use the shifted version of the
mjr 78:1e00b3fa11af 3642 // button. We're using the shifted version if...
mjr 78:1e00b3fa11af 3643 //
mjr 78:1e00b3fa11af 3644 // - the shift button is down, AND
mjr 78:1e00b3fa11af 3645 // - this button isn't itself the shift button, AND
mjr 78:1e00b3fa11af 3646 // - this button has some kind of shifted meaning
mjr 77:0b96f6867312 3647 //
mjr 78:1e00b3fa11af 3648 // A "shifted meaning" means that we have any of the following
mjr 78:1e00b3fa11af 3649 // assigned to the shifted version of the button: a key assignment,
mjr 78:1e00b3fa11af 3650 // (in typ2,key2), an IR command (in IRCommand2), or Night mode.
mjr 78:1e00b3fa11af 3651 //
mjr 78:1e00b3fa11af 3652 // The test for Night Mode is a bit tricky. The shifted version of
mjr 78:1e00b3fa11af 3653 // the button is the Night Mode toggle if the button matches the
mjr 78:1e00b3fa11af 3654 // Night Mode button index, AND its flags are set with "toggle mode
mjr 78:1e00b3fa11af 3655 // ON" (bit 0x02 is on) and "switch mode OFF" (bit 0x01 is off).
mjr 78:1e00b3fa11af 3656 // So (button flags) & 0x03 must equal 0x02.
mjr 77:0b96f6867312 3657 bool useShift =
mjr 77:0b96f6867312 3658 (shiftButton.state != 0
mjr 78:1e00b3fa11af 3659 && shiftButton.index != i
mjr 77:0b96f6867312 3660 && (bc->typ2 != BtnTypeNone
mjr 77:0b96f6867312 3661 || bc->IRCommand2 != 0
mjr 77:0b96f6867312 3662 || (cfg.nightMode.btn == i+1 && (cfg.nightMode.flags & 0x03) == 0x02)));
mjr 77:0b96f6867312 3663
mjr 77:0b96f6867312 3664 // If we're using the shift function, and no other button has used
mjr 77:0b96f6867312 3665 // the shift function yet (shift state 1: "shift button is down but
mjr 77:0b96f6867312 3666 // no one has used the shift function yet"), then we've "consumed"
mjr 77:0b96f6867312 3667 // the shift button press (so go to shift state 2: "shift button has
mjr 77:0b96f6867312 3668 // been used by some other button press that has a shifted meaning").
mjr 78:1e00b3fa11af 3669 if (useShift && shiftButton.state == 1 && bs->logState)
mjr 77:0b96f6867312 3670 shiftButton.state = 2;
mjr 35:e959ffba78fd 3671
mjr 38:091e511ce8a0 3672 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 3673 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 3674 {
mjr 77:0b96f6867312 3675 // check to see if this is the Night Mode button
mjr 53:9b2611964afc 3676 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 3677 {
mjr 77:0b96f6867312 3678 // Check the switch type in the config flags. If flag 0x01 is
mjr 77:0b96f6867312 3679 // set, it's a persistent on/off switch, so the night mode
mjr 77:0b96f6867312 3680 // state simply tracks the current state of the switch.
mjr 77:0b96f6867312 3681 // Otherwise, it's a momentary button, so each button push
mjr 77:0b96f6867312 3682 // (i.e., each transition from logical state OFF to ON) toggles
mjr 77:0b96f6867312 3683 // the night mode state.
mjr 77:0b96f6867312 3684 //
mjr 77:0b96f6867312 3685 // Note that the "shift" flag (0x02) has no effect in switch
mjr 77:0b96f6867312 3686 // mode. Shifting only works for toggle mode.
mjr 82:4f6209cb5c33 3687 if ((cfg.nightMode.flags & 0x01) != 0)
mjr 53:9b2611964afc 3688 {
mjr 77:0b96f6867312 3689 // It's an on/off switch. Night mode simply tracks the
mjr 77:0b96f6867312 3690 // current switch state.
mjr 53:9b2611964afc 3691 setNightMode(bs->logState);
mjr 53:9b2611964afc 3692 }
mjr 82:4f6209cb5c33 3693 else if (bs->logState)
mjr 53:9b2611964afc 3694 {
mjr 77:0b96f6867312 3695 // It's a momentary toggle switch. Toggle the night mode
mjr 77:0b96f6867312 3696 // state on each distinct press of the button: that is,
mjr 77:0b96f6867312 3697 // whenever the button's logical state transitions from
mjr 77:0b96f6867312 3698 // OFF to ON.
mjr 66:2e3583fbd2f4 3699 //
mjr 77:0b96f6867312 3700 // The "shift" flag (0x02) tells us whether night mode is
mjr 77:0b96f6867312 3701 // assigned to the shifted or unshifted version of the
mjr 77:0b96f6867312 3702 // button.
mjr 77:0b96f6867312 3703 bool pressed;
mjr 98:4df3c0f7e707 3704 if (shiftButton.index == i)
mjr 98:4df3c0f7e707 3705 {
mjr 98:4df3c0f7e707 3706 // This button is both the Shift button AND the Night
mjr 98:4df3c0f7e707 3707 // Mode button. This is a special case in that the
mjr 98:4df3c0f7e707 3708 // Shift status is irrelevant, because it's obviously
mjr 98:4df3c0f7e707 3709 // identical to the Night Mode status. So it doesn't
mjr 98:4df3c0f7e707 3710 // matter whether or not the Night Mode button has the
mjr 98:4df3c0f7e707 3711 // shifted flags; the raw button state is all that
mjr 98:4df3c0f7e707 3712 // counts in this case.
mjr 98:4df3c0f7e707 3713 pressed = true;
mjr 98:4df3c0f7e707 3714 }
mjr 98:4df3c0f7e707 3715 else if ((cfg.nightMode.flags & 0x02) != 0)
mjr 66:2e3583fbd2f4 3716 {
mjr 77:0b96f6867312 3717 // Shift bit is set - night mode is assigned to the
mjr 77:0b96f6867312 3718 // shifted version of the button. This is a Night
mjr 77:0b96f6867312 3719 // Mode toggle only if the Shift button is pressed.
mjr 77:0b96f6867312 3720 pressed = (shiftButton.state != 0);
mjr 77:0b96f6867312 3721 }
mjr 77:0b96f6867312 3722 else
mjr 77:0b96f6867312 3723 {
mjr 77:0b96f6867312 3724 // No shift bit - night mode is assigned to the
mjr 77:0b96f6867312 3725 // regular unshifted button. The button press only
mjr 77:0b96f6867312 3726 // applies if the Shift button is NOT pressed.
mjr 77:0b96f6867312 3727 pressed = (shiftButton.state == 0);
mjr 66:2e3583fbd2f4 3728 }
mjr 66:2e3583fbd2f4 3729
mjr 66:2e3583fbd2f4 3730 // if it's pressed (even after considering the shift mode),
mjr 66:2e3583fbd2f4 3731 // toggle night mode
mjr 66:2e3583fbd2f4 3732 if (pressed)
mjr 53:9b2611964afc 3733 toggleNightMode();
mjr 53:9b2611964afc 3734 }
mjr 35:e959ffba78fd 3735 }
mjr 38:091e511ce8a0 3736
mjr 77:0b96f6867312 3737 // press or release IR virtual keys on key state changes
mjr 77:0b96f6867312 3738 uint8_t irc = useShift ? bc->IRCommand2 : bc->IRCommand;
mjr 77:0b96f6867312 3739 if (irc != 0)
mjr 77:0b96f6867312 3740 IR_buttonChange(irc, bs->logState);
mjr 77:0b96f6867312 3741
mjr 38:091e511ce8a0 3742 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 3743 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 3744 }
mjr 38:091e511ce8a0 3745
mjr 53:9b2611964afc 3746 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 3747 // key state list
mjr 53:9b2611964afc 3748 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 3749 {
mjr 70:9f58735a1732 3750 // Get the key type and code. Start by assuming that we're
mjr 70:9f58735a1732 3751 // going to use the normal unshifted meaning.
mjr 77:0b96f6867312 3752 uint8_t typ, val;
mjr 77:0b96f6867312 3753 if (useShift)
mjr 66:2e3583fbd2f4 3754 {
mjr 77:0b96f6867312 3755 typ = bc->typ2;
mjr 77:0b96f6867312 3756 val = bc->val2;
mjr 66:2e3583fbd2f4 3757 }
mjr 77:0b96f6867312 3758 else
mjr 77:0b96f6867312 3759 {
mjr 77:0b96f6867312 3760 typ = bc->typ;
mjr 77:0b96f6867312 3761 val = bc->val;
mjr 77:0b96f6867312 3762 }
mjr 77:0b96f6867312 3763
mjr 70:9f58735a1732 3764 // We've decided on the meaning of the button, so process
mjr 70:9f58735a1732 3765 // the keyboard or joystick event.
mjr 77:0b96f6867312 3766 ks.addKey(typ, val);
mjr 18:5e890ebd0023 3767 }
mjr 11:bd9da7088e6e 3768 }
mjr 77:0b96f6867312 3769
mjr 77:0b96f6867312 3770 // If an IR input command is in effect, add the IR command's
mjr 77:0b96f6867312 3771 // assigned key, if any. If we're in an IR key gap, don't include
mjr 77:0b96f6867312 3772 // the IR key.
mjr 77:0b96f6867312 3773 if (IRCommandIn != 0 && !IRKeyGap)
mjr 77:0b96f6867312 3774 {
mjr 77:0b96f6867312 3775 IRCommandCfg &irc = cfg.IRCommand[IRCommandIn - 1];
mjr 77:0b96f6867312 3776 ks.addKey(irc.keytype, irc.keycode);
mjr 77:0b96f6867312 3777 }
mjr 77:0b96f6867312 3778
mjr 77:0b96f6867312 3779 // We're finished building the new key state. Update the global
mjr 77:0b96f6867312 3780 // key state variables to reflect the new state.
mjr 77:0b96f6867312 3781
mjr 77:0b96f6867312 3782 // set the new joystick buttons (no need to check for changes, as we
mjr 77:0b96f6867312 3783 // report these on every joystick report whether they changed or not)
mjr 77:0b96f6867312 3784 jsButtons = ks.js;
mjr 77:0b96f6867312 3785
mjr 77:0b96f6867312 3786 // check for keyboard key changes (we only send keyboard reports when
mjr 77:0b96f6867312 3787 // something changes)
mjr 77:0b96f6867312 3788 if (kbState.data[0] != ks.modkeys
mjr 77:0b96f6867312 3789 || kbState.nkeys != ks.nkeys
mjr 77:0b96f6867312 3790 || memcmp(ks.keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 3791 {
mjr 35:e959ffba78fd 3792 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3793 kbState.changed = true;
mjr 77:0b96f6867312 3794 kbState.data[0] = ks.modkeys;
mjr 77:0b96f6867312 3795 if (ks.nkeys <= 6) {
mjr 35:e959ffba78fd 3796 // 6 or fewer simultaneous keys - report the key codes
mjr 77:0b96f6867312 3797 kbState.nkeys = ks.nkeys;
mjr 77:0b96f6867312 3798 memcpy(&kbState.data[2], ks.keys, 6);
mjr 35:e959ffba78fd 3799 }
mjr 35:e959ffba78fd 3800 else {
mjr 35:e959ffba78fd 3801 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 3802 kbState.nkeys = 6;
mjr 35:e959ffba78fd 3803 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 3804 }
mjr 35:e959ffba78fd 3805 }
mjr 35:e959ffba78fd 3806
mjr 77:0b96f6867312 3807 // check for media key changes (we only send media key reports when
mjr 77:0b96f6867312 3808 // something changes)
mjr 77:0b96f6867312 3809 if (mediaState.data != ks.mediakeys)
mjr 35:e959ffba78fd 3810 {
mjr 77:0b96f6867312 3811 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3812 mediaState.changed = true;
mjr 77:0b96f6867312 3813 mediaState.data = ks.mediakeys;
mjr 35:e959ffba78fd 3814 }
mjr 11:bd9da7088e6e 3815 }
mjr 11:bd9da7088e6e 3816
mjr 73:4e8ce0b18915 3817 // Send a button status report
mjr 73:4e8ce0b18915 3818 void reportButtonStatus(USBJoystick &js)
mjr 73:4e8ce0b18915 3819 {
mjr 73:4e8ce0b18915 3820 // start with all buttons off
mjr 73:4e8ce0b18915 3821 uint8_t state[(MAX_BUTTONS+7)/8];
mjr 73:4e8ce0b18915 3822 memset(state, 0, sizeof(state));
mjr 73:4e8ce0b18915 3823
mjr 73:4e8ce0b18915 3824 // pack the button states into bytes, one bit per button
mjr 73:4e8ce0b18915 3825 ButtonState *bs = buttonState;
mjr 73:4e8ce0b18915 3826 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 73:4e8ce0b18915 3827 {
mjr 73:4e8ce0b18915 3828 // get the physical state
mjr 73:4e8ce0b18915 3829 int b = bs->physState;
mjr 73:4e8ce0b18915 3830
mjr 73:4e8ce0b18915 3831 // pack it into the appropriate bit
mjr 73:4e8ce0b18915 3832 int idx = bs->cfgIndex;
mjr 73:4e8ce0b18915 3833 int si = idx / 8;
mjr 73:4e8ce0b18915 3834 int shift = idx & 0x07;
mjr 73:4e8ce0b18915 3835 state[si] |= b << shift;
mjr 73:4e8ce0b18915 3836 }
mjr 73:4e8ce0b18915 3837
mjr 73:4e8ce0b18915 3838 // send the report
mjr 73:4e8ce0b18915 3839 js.reportButtonStatus(MAX_BUTTONS, state);
mjr 73:4e8ce0b18915 3840 }
mjr 73:4e8ce0b18915 3841
mjr 5:a70c0bce770d 3842 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3843 //
mjr 5:a70c0bce770d 3844 // Customization joystick subbclass
mjr 5:a70c0bce770d 3845 //
mjr 5:a70c0bce770d 3846
mjr 5:a70c0bce770d 3847 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 3848 {
mjr 5:a70c0bce770d 3849 public:
mjr 35:e959ffba78fd 3850 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 90:aa4e571da8e8 3851 bool waitForConnect, bool enableJoystick, int axisFormat, bool useKB)
mjr 90:aa4e571da8e8 3852 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, axisFormat, useKB)
mjr 5:a70c0bce770d 3853 {
mjr 54:fd77a6b2f76c 3854 sleeping_ = false;
mjr 54:fd77a6b2f76c 3855 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3856 timer_.start();
mjr 54:fd77a6b2f76c 3857 }
mjr 54:fd77a6b2f76c 3858
mjr 54:fd77a6b2f76c 3859 // show diagnostic LED feedback for connect state
mjr 54:fd77a6b2f76c 3860 void diagFlash()
mjr 54:fd77a6b2f76c 3861 {
mjr 54:fd77a6b2f76c 3862 if (!configured() || sleeping_)
mjr 54:fd77a6b2f76c 3863 {
mjr 54:fd77a6b2f76c 3864 // flash once if sleeping or twice if disconnected
mjr 54:fd77a6b2f76c 3865 for (int j = isConnected() ? 1 : 2 ; j > 0 ; --j)
mjr 54:fd77a6b2f76c 3866 {
mjr 54:fd77a6b2f76c 3867 // short red flash
mjr 54:fd77a6b2f76c 3868 diagLED(1, 0, 0);
mjr 54:fd77a6b2f76c 3869 wait_us(50000);
mjr 54:fd77a6b2f76c 3870 diagLED(0, 0, 0);
mjr 54:fd77a6b2f76c 3871 wait_us(50000);
mjr 54:fd77a6b2f76c 3872 }
mjr 54:fd77a6b2f76c 3873 }
mjr 5:a70c0bce770d 3874 }
mjr 5:a70c0bce770d 3875
mjr 5:a70c0bce770d 3876 // are we connected?
mjr 5:a70c0bce770d 3877 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 3878
mjr 54:fd77a6b2f76c 3879 // Are we in sleep mode? If true, this means that the hardware has
mjr 54:fd77a6b2f76c 3880 // detected no activity on the bus for 3ms. This happens when the
mjr 54:fd77a6b2f76c 3881 // cable is physically disconnected, the computer is turned off, or
mjr 54:fd77a6b2f76c 3882 // the connection is otherwise disabled.
mjr 54:fd77a6b2f76c 3883 bool isSleeping() const { return sleeping_; }
mjr 54:fd77a6b2f76c 3884
mjr 54:fd77a6b2f76c 3885 // If necessary, attempt to recover from a broken connection.
mjr 54:fd77a6b2f76c 3886 //
mjr 54:fd77a6b2f76c 3887 // This is a hack, to work around an apparent timing bug in the
mjr 54:fd77a6b2f76c 3888 // KL25Z USB implementation that I haven't been able to solve any
mjr 54:fd77a6b2f76c 3889 // other way.
mjr 54:fd77a6b2f76c 3890 //
mjr 54:fd77a6b2f76c 3891 // The issue: when we have an established connection, and the
mjr 54:fd77a6b2f76c 3892 // connection is broken by physically unplugging the cable or by
mjr 54:fd77a6b2f76c 3893 // rebooting the PC, the KL25Z sometimes fails to reconnect when
mjr 54:fd77a6b2f76c 3894 // the physical connection is re-established. The failure is
mjr 54:fd77a6b2f76c 3895 // sporadic; I'd guess it happens about 25% of the time, but I
mjr 54:fd77a6b2f76c 3896 // haven't collected any real statistics on it.
mjr 54:fd77a6b2f76c 3897 //
mjr 54:fd77a6b2f76c 3898 // The proximate cause of the failure is a deadlock in the SETUP
mjr 54:fd77a6b2f76c 3899 // protocol between the host and device that happens around the
mjr 54:fd77a6b2f76c 3900 // point where the PC is requesting the configuration descriptor.
mjr 54:fd77a6b2f76c 3901 // The exact point in the protocol where this occurs varies slightly;
mjr 54:fd77a6b2f76c 3902 // it can occur a message or two before or after the Get Config
mjr 54:fd77a6b2f76c 3903 // Descriptor packet. No matter where it happens, the nature of
mjr 54:fd77a6b2f76c 3904 // the deadlock is the same: the PC thinks it sees a STALL on EP0
mjr 54:fd77a6b2f76c 3905 // from the device, so it terminates the connection attempt, which
mjr 54:fd77a6b2f76c 3906 // stops further traffic on the cable. The KL25Z USB hardware sees
mjr 54:fd77a6b2f76c 3907 // the lack of traffic and triggers a SLEEP interrupt (a misnomer
mjr 54:fd77a6b2f76c 3908 // for what should have been called a BROKEN CONNECTION interrupt).
mjr 54:fd77a6b2f76c 3909 // Both sides simply stop talking at this point, so the connection
mjr 54:fd77a6b2f76c 3910 // is effectively dead.
mjr 54:fd77a6b2f76c 3911 //
mjr 54:fd77a6b2f76c 3912 // The strange thing is that, as far as I can tell, the KL25Z isn't
mjr 54:fd77a6b2f76c 3913 // doing anything to trigger the STALL on its end. Both the PC
mjr 54:fd77a6b2f76c 3914 // and the KL25Z are happy up until the very point of the failure
mjr 54:fd77a6b2f76c 3915 // and show no signs of anything wrong in the protocol exchange.
mjr 54:fd77a6b2f76c 3916 // In fact, every detail of the protocol exchange up to this point
mjr 54:fd77a6b2f76c 3917 // is identical to every successful exchange that does finish the
mjr 54:fd77a6b2f76c 3918 // whole setup process successfully, on both the KL25Z and Windows
mjr 54:fd77a6b2f76c 3919 // sides of the connection. I can't find any point of difference
mjr 54:fd77a6b2f76c 3920 // between successful and unsuccessful sequences that suggests why
mjr 54:fd77a6b2f76c 3921 // the fateful message fails. This makes me suspect that whatever
mjr 54:fd77a6b2f76c 3922 // is going wrong is inside the KL25Z USB hardware module, which
mjr 54:fd77a6b2f76c 3923 // is a pretty substantial black box - it has a lot of internal
mjr 54:fd77a6b2f76c 3924 // state that's inaccessible to the software. Further bolstering
mjr 54:fd77a6b2f76c 3925 // this theory is a little experiment where I found that I could
mjr 54:fd77a6b2f76c 3926 // reproduce the exact sequence of events of a failed reconnect
mjr 54:fd77a6b2f76c 3927 // attempt in an *initial* connection, which is otherwise 100%
mjr 54:fd77a6b2f76c 3928 // reliable, by inserting a little bit of artifical time padding
mjr 54:fd77a6b2f76c 3929 // (200us per event) into the SETUP interrupt handler. My
mjr 54:fd77a6b2f76c 3930 // hypothesis is that the STALL event happens because the KL25Z
mjr 54:fd77a6b2f76c 3931 // USB hardware is too slow to respond to a message. I'm not
mjr 54:fd77a6b2f76c 3932 // sure why this would only happen after a disconnect and not
mjr 54:fd77a6b2f76c 3933 // during the initial connection; maybe there's some reset work
mjr 54:fd77a6b2f76c 3934 // in the hardware that takes a substantial amount of time after
mjr 54:fd77a6b2f76c 3935 // a disconnect.
mjr 54:fd77a6b2f76c 3936 //
mjr 54:fd77a6b2f76c 3937 // The solution: the problem happens during the SETUP exchange,
mjr 54:fd77a6b2f76c 3938 // after we've been assigned a bus address. It only happens on
mjr 54:fd77a6b2f76c 3939 // some percentage of connection requests, so if we can simply
mjr 54:fd77a6b2f76c 3940 // start over when the failure occurs, we'll eventually succeed
mjr 54:fd77a6b2f76c 3941 // simply because not every attempt fails. The ideal would be
mjr 54:fd77a6b2f76c 3942 // to get the success rate up to 100%, but I can't figure out how
mjr 54:fd77a6b2f76c 3943 // to fix the underlying problem, so this is the next best thing.
mjr 54:fd77a6b2f76c 3944 //
mjr 54:fd77a6b2f76c 3945 // We can detect when the failure occurs by noticing when a SLEEP
mjr 54:fd77a6b2f76c 3946 // interrupt happens while we have an assigned bus address.
mjr 54:fd77a6b2f76c 3947 //
mjr 54:fd77a6b2f76c 3948 // To start a new connection attempt, we have to make the *host*
mjr 54:fd77a6b2f76c 3949 // try again. The logical connection is initiated solely by the
mjr 54:fd77a6b2f76c 3950 // host. Fortunately, it's easy to get the host to initiate the
mjr 54:fd77a6b2f76c 3951 // process: if we disconnect on the device side, it effectively
mjr 54:fd77a6b2f76c 3952 // makes the device look to the PC like it's electrically unplugged.
mjr 54:fd77a6b2f76c 3953 // When we reconnect on the device side, the PC thinks a new device
mjr 54:fd77a6b2f76c 3954 // has been plugged in and initiates the logical connection setup.
mjr 74:822a92bc11d2 3955 // We have to remain disconnected for some minimum interval before
mjr 74:822a92bc11d2 3956 // the host notices; the exact minimum is unclear, but 5ms seems
mjr 74:822a92bc11d2 3957 // reliable in practice.
mjr 54:fd77a6b2f76c 3958 //
mjr 54:fd77a6b2f76c 3959 // Here's the full algorithm:
mjr 54:fd77a6b2f76c 3960 //
mjr 54:fd77a6b2f76c 3961 // 1. In the SLEEP interrupt handler, if we have a bus address,
mjr 54:fd77a6b2f76c 3962 // we disconnect the device. This happens in ISR context, so we
mjr 54:fd77a6b2f76c 3963 // can't wait around for 5ms. Instead, we simply set a flag noting
mjr 54:fd77a6b2f76c 3964 // that the connection has been broken, and we note the time and
mjr 54:fd77a6b2f76c 3965 // return.
mjr 54:fd77a6b2f76c 3966 //
mjr 54:fd77a6b2f76c 3967 // 2. In our main loop, whenever we find that we're disconnected,
mjr 54:fd77a6b2f76c 3968 // we call recoverConnection(). The main loop's job is basically a
mjr 54:fd77a6b2f76c 3969 // bunch of device polling. We're just one more device to poll, so
mjr 54:fd77a6b2f76c 3970 // recoverConnection() will be called soon after a disconnect, and
mjr 54:fd77a6b2f76c 3971 // then will be called in a loop for as long as we're disconnected.
mjr 54:fd77a6b2f76c 3972 //
mjr 54:fd77a6b2f76c 3973 // 3. In recoverConnection(), we check the flag we set in the SLEEP
mjr 54:fd77a6b2f76c 3974 // handler. If set, we wait until 5ms has elapsed from the SLEEP
mjr 54:fd77a6b2f76c 3975 // event time that we noted, then we'll reconnect and clear the flag.
mjr 54:fd77a6b2f76c 3976 // This gives us the required 5ms (or longer) delay between the
mjr 54:fd77a6b2f76c 3977 // disconnect and reconnect, ensuring that the PC will notice and
mjr 54:fd77a6b2f76c 3978 // will start over with the connection protocol.
mjr 54:fd77a6b2f76c 3979 //
mjr 54:fd77a6b2f76c 3980 // 4. The main loop keeps calling recoverConnection() in a loop for
mjr 54:fd77a6b2f76c 3981 // as long as we're disconnected, so if the new connection attempt
mjr 54:fd77a6b2f76c 3982 // triggered in step 3 fails, the SLEEP interrupt will happen again,
mjr 54:fd77a6b2f76c 3983 // we'll disconnect again, the flag will get set again, and
mjr 54:fd77a6b2f76c 3984 // recoverConnection() will reconnect again after another suitable
mjr 54:fd77a6b2f76c 3985 // delay. This will repeat until the connection succeeds or hell
mjr 54:fd77a6b2f76c 3986 // freezes over.
mjr 54:fd77a6b2f76c 3987 //
mjr 54:fd77a6b2f76c 3988 // Each disconnect happens immediately when a reconnect attempt
mjr 54:fd77a6b2f76c 3989 // fails, and an entire successful connection only takes about 25ms,
mjr 54:fd77a6b2f76c 3990 // so our loop can retry at more than 30 attempts per second.
mjr 54:fd77a6b2f76c 3991 // In my testing, lost connections almost always reconnect in
mjr 54:fd77a6b2f76c 3992 // less than second with this code in place.
mjr 54:fd77a6b2f76c 3993 void recoverConnection()
mjr 54:fd77a6b2f76c 3994 {
mjr 54:fd77a6b2f76c 3995 // if a reconnect is pending, reconnect
mjr 54:fd77a6b2f76c 3996 if (reconnectPending_)
mjr 54:fd77a6b2f76c 3997 {
mjr 54:fd77a6b2f76c 3998 // Loop until we reach 5ms after the last sleep event.
mjr 54:fd77a6b2f76c 3999 for (bool done = false ; !done ; )
mjr 54:fd77a6b2f76c 4000 {
mjr 54:fd77a6b2f76c 4001 // If we've reached the target time, reconnect. Do the
mjr 54:fd77a6b2f76c 4002 // time check and flag reset atomically, so that we can't
mjr 54:fd77a6b2f76c 4003 // have another sleep event sneak in after we've verified
mjr 54:fd77a6b2f76c 4004 // the time. If another event occurs, it has to happen
mjr 54:fd77a6b2f76c 4005 // before we check, in which case it'll update the time
mjr 54:fd77a6b2f76c 4006 // before we check it, or after we clear the flag, in
mjr 54:fd77a6b2f76c 4007 // which case it will reset the flag and we'll do another
mjr 54:fd77a6b2f76c 4008 // round the next time we call this routine.
mjr 54:fd77a6b2f76c 4009 __disable_irq();
mjr 54:fd77a6b2f76c 4010 if (uint32_t(timer_.read_us() - lastSleepTime_) > 5000)
mjr 54:fd77a6b2f76c 4011 {
mjr 54:fd77a6b2f76c 4012 connect(false);
mjr 54:fd77a6b2f76c 4013 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 4014 done = true;
mjr 54:fd77a6b2f76c 4015 }
mjr 54:fd77a6b2f76c 4016 __enable_irq();
mjr 54:fd77a6b2f76c 4017 }
mjr 54:fd77a6b2f76c 4018 }
mjr 54:fd77a6b2f76c 4019 }
mjr 5:a70c0bce770d 4020
mjr 5:a70c0bce770d 4021 protected:
mjr 54:fd77a6b2f76c 4022 // Handle a USB SLEEP interrupt. This interrupt signifies that the
mjr 54:fd77a6b2f76c 4023 // USB hardware module hasn't seen any token traffic for 3ms, which
mjr 54:fd77a6b2f76c 4024 // means that we're either physically or logically disconnected.
mjr 54:fd77a6b2f76c 4025 //
mjr 54:fd77a6b2f76c 4026 // Important: this runs in ISR context.
mjr 54:fd77a6b2f76c 4027 //
mjr 54:fd77a6b2f76c 4028 // Note that this is a specialized sense of "sleep" that's unrelated
mjr 54:fd77a6b2f76c 4029 // to the similarly named power modes on the PC. This has nothing
mjr 54:fd77a6b2f76c 4030 // to do with suspend/sleep mode on the PC, and it's not a low-power
mjr 54:fd77a6b2f76c 4031 // mode on the KL25Z. They really should have called this interrupt
mjr 54:fd77a6b2f76c 4032 // DISCONNECT or BROKEN CONNECTION.)
mjr 54:fd77a6b2f76c 4033 virtual void sleepStateChanged(unsigned int sleeping)
mjr 54:fd77a6b2f76c 4034 {
mjr 54:fd77a6b2f76c 4035 // note the new state
mjr 54:fd77a6b2f76c 4036 sleeping_ = sleeping;
mjr 54:fd77a6b2f76c 4037
mjr 54:fd77a6b2f76c 4038 // If we have a non-zero bus address, we have at least a partial
mjr 54:fd77a6b2f76c 4039 // connection to the host (we've made it at least as far as the
mjr 54:fd77a6b2f76c 4040 // SETUP stage). Explicitly disconnect, and the pending reconnect
mjr 54:fd77a6b2f76c 4041 // flag, and remember the time of the sleep event.
mjr 54:fd77a6b2f76c 4042 if (USB0->ADDR != 0x00)
mjr 54:fd77a6b2f76c 4043 {
mjr 54:fd77a6b2f76c 4044 disconnect();
mjr 54:fd77a6b2f76c 4045 lastSleepTime_ = timer_.read_us();
mjr 54:fd77a6b2f76c 4046 reconnectPending_ = true;
mjr 54:fd77a6b2f76c 4047 }
mjr 54:fd77a6b2f76c 4048 }
mjr 54:fd77a6b2f76c 4049
mjr 54:fd77a6b2f76c 4050 // is the USB connection asleep?
mjr 54:fd77a6b2f76c 4051 volatile bool sleeping_;
mjr 54:fd77a6b2f76c 4052
mjr 54:fd77a6b2f76c 4053 // flag: reconnect pending after sleep event
mjr 54:fd77a6b2f76c 4054 volatile bool reconnectPending_;
mjr 54:fd77a6b2f76c 4055
mjr 54:fd77a6b2f76c 4056 // time of last sleep event while connected
mjr 54:fd77a6b2f76c 4057 volatile uint32_t lastSleepTime_;
mjr 54:fd77a6b2f76c 4058
mjr 54:fd77a6b2f76c 4059 // timer to keep track of interval since last sleep event
mjr 54:fd77a6b2f76c 4060 Timer timer_;
mjr 5:a70c0bce770d 4061 };
mjr 5:a70c0bce770d 4062
mjr 5:a70c0bce770d 4063 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 4064 //
mjr 5:a70c0bce770d 4065 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 4066 //
mjr 5:a70c0bce770d 4067
mjr 5:a70c0bce770d 4068 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 4069 //
mjr 5:a70c0bce770d 4070 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 4071 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 4072 // automatic calibration.
mjr 5:a70c0bce770d 4073 //
mjr 77:0b96f6867312 4074 // We collect data at the device's maximum rate of 800kHz (one sample
mjr 77:0b96f6867312 4075 // every 1.25ms). To keep up with the high data rate, we use the
mjr 77:0b96f6867312 4076 // device's internal FIFO, and drain the FIFO by polling on each
mjr 77:0b96f6867312 4077 // iteration of our main application loop. In the past, we used an
mjr 77:0b96f6867312 4078 // interrupt handler to read the device immediately on the arrival of
mjr 77:0b96f6867312 4079 // each sample, but this created too much latency for the IR remote
mjr 77:0b96f6867312 4080 // receiver, due to the relatively long time it takes to transfer the
mjr 77:0b96f6867312 4081 // accelerometer readings via I2C. The device's on-board FIFO can
mjr 77:0b96f6867312 4082 // store up to 32 samples, which gives us up to about 40ms between
mjr 77:0b96f6867312 4083 // polling iterations before the buffer overflows. Our main loop runs
mjr 77:0b96f6867312 4084 // in under 2ms, so we can easily keep the FIFO far from overflowing.
mjr 77:0b96f6867312 4085 //
mjr 77:0b96f6867312 4086 // The MMA8451Q has three range modes, +/- 2G, 4G, and 8G. The ADC
mjr 77:0b96f6867312 4087 // sample is the same bit width (14 bits) in all modes, so the higher
mjr 77:0b96f6867312 4088 // dynamic range modes trade physical precision for range. For our
mjr 77:0b96f6867312 4089 // purposes, precision is more important than range, so we use the
mjr 77:0b96f6867312 4090 // +/-2G mode. Further, our joystick range is calibrated for only
mjr 77:0b96f6867312 4091 // +/-1G. This was unintentional on my part; I didn't look at the
mjr 77:0b96f6867312 4092 // MMA8451Q library closely enough to realize it was normalizing to
mjr 77:0b96f6867312 4093 // actual "G" units, and assumed that it was normalizing to a -1..+1
mjr 77:0b96f6867312 4094 // scale. In practice, a +/-1G scale seems perfectly adequate for
mjr 77:0b96f6867312 4095 // virtual pinball use, so I'm sticking with that range for now. But
mjr 77:0b96f6867312 4096 // there might be some benefit in renormalizing to a +/-2G range, in
mjr 77:0b96f6867312 4097 // that it would allow for higher dynamic range for very hard nudges.
mjr 77:0b96f6867312 4098 // Everyone would have to tweak their nudge sensitivity in VP if I
mjr 77:0b96f6867312 4099 // made that change, though, so I'm keeping it as is for now; it would
mjr 77:0b96f6867312 4100 // be best to make it a config option ("accelerometer high dynamic range")
mjr 77:0b96f6867312 4101 // rather than change it across the board.
mjr 5:a70c0bce770d 4102 //
mjr 6:cc35eb643e8f 4103 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 4104 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 4105 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 4106 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 4107 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 4108 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 4109 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 4110 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 4111 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 4112 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 4113 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 4114 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 4115 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 4116 // of nudging, say).
mjr 5:a70c0bce770d 4117 //
mjr 5:a70c0bce770d 4118
mjr 17:ab3cec0c8bf4 4119 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 4120 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 4121
mjr 17:ab3cec0c8bf4 4122 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 4123 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 4124 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 4125
mjr 17:ab3cec0c8bf4 4126 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 4127 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 4128 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 4129 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 4130
mjr 17:ab3cec0c8bf4 4131
mjr 6:cc35eb643e8f 4132 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 4133 struct AccHist
mjr 5:a70c0bce770d 4134 {
mjr 77:0b96f6867312 4135 AccHist() { x = y = dsq = 0; xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 4136 void set(int x, int y, AccHist *prv)
mjr 6:cc35eb643e8f 4137 {
mjr 6:cc35eb643e8f 4138 // save the raw position
mjr 6:cc35eb643e8f 4139 this->x = x;
mjr 6:cc35eb643e8f 4140 this->y = y;
mjr 77:0b96f6867312 4141 this->dsq = distanceSquared(prv);
mjr 6:cc35eb643e8f 4142 }
mjr 6:cc35eb643e8f 4143
mjr 6:cc35eb643e8f 4144 // reading for this entry
mjr 77:0b96f6867312 4145 int x, y;
mjr 77:0b96f6867312 4146
mjr 77:0b96f6867312 4147 // (distance from previous entry) squared
mjr 77:0b96f6867312 4148 int dsq;
mjr 5:a70c0bce770d 4149
mjr 6:cc35eb643e8f 4150 // total and count of samples averaged over this period
mjr 77:0b96f6867312 4151 int xtot, ytot;
mjr 6:cc35eb643e8f 4152 int cnt;
mjr 6:cc35eb643e8f 4153
mjr 77:0b96f6867312 4154 void clearAvg() { xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 4155 void addAvg(int x, int y) { xtot += x; ytot += y; ++cnt; }
mjr 77:0b96f6867312 4156 int xAvg() const { return xtot/cnt; }
mjr 77:0b96f6867312 4157 int yAvg() const { return ytot/cnt; }
mjr 77:0b96f6867312 4158
mjr 77:0b96f6867312 4159 int distanceSquared(AccHist *p)
mjr 77:0b96f6867312 4160 { return square(p->x - x) + square(p->y - y); }
mjr 5:a70c0bce770d 4161 };
mjr 5:a70c0bce770d 4162
mjr 5:a70c0bce770d 4163 // accelerometer wrapper class
mjr 3:3514575d4f86 4164 class Accel
mjr 3:3514575d4f86 4165 {
mjr 3:3514575d4f86 4166 public:
mjr 78:1e00b3fa11af 4167 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin,
mjr 78:1e00b3fa11af 4168 int range, int autoCenterMode)
mjr 77:0b96f6867312 4169 : mma_(sda, scl, i2cAddr)
mjr 3:3514575d4f86 4170 {
mjr 5:a70c0bce770d 4171 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 4172 irqPin_ = irqPin;
mjr 77:0b96f6867312 4173
mjr 77:0b96f6867312 4174 // remember the range
mjr 77:0b96f6867312 4175 range_ = range;
mjr 78:1e00b3fa11af 4176
mjr 78:1e00b3fa11af 4177 // set the auto-centering mode
mjr 78:1e00b3fa11af 4178 setAutoCenterMode(autoCenterMode);
mjr 78:1e00b3fa11af 4179
mjr 78:1e00b3fa11af 4180 // no manual centering request has been received
mjr 78:1e00b3fa11af 4181 manualCenterRequest_ = false;
mjr 5:a70c0bce770d 4182
mjr 5:a70c0bce770d 4183 // reset and initialize
mjr 5:a70c0bce770d 4184 reset();
mjr 5:a70c0bce770d 4185 }
mjr 5:a70c0bce770d 4186
mjr 78:1e00b3fa11af 4187 // Request manual centering. This applies the trailing average
mjr 78:1e00b3fa11af 4188 // of recent measurements and applies it as the new center point
mjr 78:1e00b3fa11af 4189 // as soon as we have enough data.
mjr 78:1e00b3fa11af 4190 void manualCenterRequest() { manualCenterRequest_ = true; }
mjr 78:1e00b3fa11af 4191
mjr 78:1e00b3fa11af 4192 // set the auto-centering mode
mjr 78:1e00b3fa11af 4193 void setAutoCenterMode(int mode)
mjr 78:1e00b3fa11af 4194 {
mjr 78:1e00b3fa11af 4195 // remember the mode
mjr 78:1e00b3fa11af 4196 autoCenterMode_ = mode;
mjr 78:1e00b3fa11af 4197
mjr 78:1e00b3fa11af 4198 // Set the time between checks. We check 5 times over the course
mjr 78:1e00b3fa11af 4199 // of the centering time, so the check interval is 1/5 of the total.
mjr 78:1e00b3fa11af 4200 if (mode == 0)
mjr 78:1e00b3fa11af 4201 {
mjr 78:1e00b3fa11af 4202 // mode 0 is the old default of 5 seconds, so check every 1s
mjr 78:1e00b3fa11af 4203 autoCenterCheckTime_ = 1000000;
mjr 78:1e00b3fa11af 4204 }
mjr 78:1e00b3fa11af 4205 else if (mode <= 60)
mjr 78:1e00b3fa11af 4206 {
mjr 78:1e00b3fa11af 4207 // mode 1-60 means reset after 'mode' seconds; the check
mjr 78:1e00b3fa11af 4208 // interval is 1/5 of this
mjr 78:1e00b3fa11af 4209 autoCenterCheckTime_ = mode*200000;
mjr 78:1e00b3fa11af 4210 }
mjr 78:1e00b3fa11af 4211 else
mjr 78:1e00b3fa11af 4212 {
mjr 78:1e00b3fa11af 4213 // Auto-centering is off, but still gather statistics to apply
mjr 78:1e00b3fa11af 4214 // when we get a manual centering request. The check interval
mjr 78:1e00b3fa11af 4215 // in this case is 1/5 of the total time for the trailing average
mjr 78:1e00b3fa11af 4216 // we apply for the manual centering. We want this to be long
mjr 78:1e00b3fa11af 4217 // enough to smooth out the data, but short enough that it only
mjr 78:1e00b3fa11af 4218 // includes recent data.
mjr 78:1e00b3fa11af 4219 autoCenterCheckTime_ = 500000;
mjr 78:1e00b3fa11af 4220 }
mjr 78:1e00b3fa11af 4221 }
mjr 78:1e00b3fa11af 4222
mjr 5:a70c0bce770d 4223 void reset()
mjr 5:a70c0bce770d 4224 {
mjr 6:cc35eb643e8f 4225 // clear the center point
mjr 77:0b96f6867312 4226 cx_ = cy_ = 0;
mjr 6:cc35eb643e8f 4227
mjr 77:0b96f6867312 4228 // start the auto-centering timer
mjr 5:a70c0bce770d 4229 tCenter_.start();
mjr 5:a70c0bce770d 4230 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 4231
mjr 5:a70c0bce770d 4232 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 4233 mma_.init();
mjr 77:0b96f6867312 4234
mjr 77:0b96f6867312 4235 // set the range
mjr 77:0b96f6867312 4236 mma_.setRange(
mjr 77:0b96f6867312 4237 range_ == AccelRange4G ? 4 :
mjr 77:0b96f6867312 4238 range_ == AccelRange8G ? 8 :
mjr 77:0b96f6867312 4239 2);
mjr 6:cc35eb643e8f 4240
mjr 77:0b96f6867312 4241 // set the average accumulators to zero
mjr 77:0b96f6867312 4242 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 4243 nSum_ = 0;
mjr 3:3514575d4f86 4244
mjr 3:3514575d4f86 4245 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 4246 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 4247 }
mjr 3:3514575d4f86 4248
mjr 77:0b96f6867312 4249 void poll()
mjr 76:7f5912b6340e 4250 {
mjr 77:0b96f6867312 4251 // read samples until we clear the FIFO
mjr 77:0b96f6867312 4252 while (mma_.getFIFOCount() != 0)
mjr 77:0b96f6867312 4253 {
mjr 77:0b96f6867312 4254 int x, y, z;
mjr 77:0b96f6867312 4255 mma_.getAccXYZ(x, y, z);
mjr 77:0b96f6867312 4256
mjr 77:0b96f6867312 4257 // add the new reading to the running total for averaging
mjr 77:0b96f6867312 4258 xSum_ += (x - cx_);
mjr 77:0b96f6867312 4259 ySum_ += (y - cy_);
mjr 77:0b96f6867312 4260 ++nSum_;
mjr 77:0b96f6867312 4261
mjr 77:0b96f6867312 4262 // store the updates
mjr 77:0b96f6867312 4263 ax_ = x;
mjr 77:0b96f6867312 4264 ay_ = y;
mjr 77:0b96f6867312 4265 az_ = z;
mjr 77:0b96f6867312 4266 }
mjr 76:7f5912b6340e 4267 }
mjr 77:0b96f6867312 4268
mjr 9:fd65b0a94720 4269 void get(int &x, int &y)
mjr 3:3514575d4f86 4270 {
mjr 77:0b96f6867312 4271 // read the shared data and store locally for calculations
mjr 77:0b96f6867312 4272 int ax = ax_, ay = ay_;
mjr 77:0b96f6867312 4273 int xSum = xSum_, ySum = ySum_;
mjr 77:0b96f6867312 4274 int nSum = nSum_;
mjr 6:cc35eb643e8f 4275
mjr 77:0b96f6867312 4276 // reset the average accumulators for the next run
mjr 77:0b96f6867312 4277 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 4278 nSum_ = 0;
mjr 77:0b96f6867312 4279
mjr 77:0b96f6867312 4280 // add this sample to the current calibration interval's running total
mjr 77:0b96f6867312 4281 AccHist *p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 4282 p->addAvg(ax, ay);
mjr 77:0b96f6867312 4283
mjr 78:1e00b3fa11af 4284 // If we're in auto-centering mode, check for auto-centering
mjr 78:1e00b3fa11af 4285 // at intervals of 1/5 of the overall time. If we're not in
mjr 78:1e00b3fa11af 4286 // auto-centering mode, check anyway at one-second intervals
mjr 78:1e00b3fa11af 4287 // so that we gather averages for manual centering requests.
mjr 78:1e00b3fa11af 4288 if (tCenter_.read_us() > autoCenterCheckTime_)
mjr 77:0b96f6867312 4289 {
mjr 77:0b96f6867312 4290 // add the latest raw sample to the history list
mjr 77:0b96f6867312 4291 AccHist *prv = p;
mjr 77:0b96f6867312 4292 iAccPrv_ = (iAccPrv_ + 1);
mjr 77:0b96f6867312 4293 if (iAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 4294 iAccPrv_ = 0;
mjr 77:0b96f6867312 4295 p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 4296 p->set(ax, ay, prv);
mjr 77:0b96f6867312 4297
mjr 78:1e00b3fa11af 4298 // if we have a full complement, check for auto-centering
mjr 77:0b96f6867312 4299 if (nAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 4300 {
mjr 78:1e00b3fa11af 4301 // Center if:
mjr 78:1e00b3fa11af 4302 //
mjr 78:1e00b3fa11af 4303 // - Auto-centering is on, and we've been stable over the
mjr 78:1e00b3fa11af 4304 // whole sample period at our spot-check points
mjr 78:1e00b3fa11af 4305 //
mjr 78:1e00b3fa11af 4306 // - A manual centering request is pending
mjr 78:1e00b3fa11af 4307 //
mjr 77:0b96f6867312 4308 static const int accTol = 164*164; // 1% of range, squared
mjr 77:0b96f6867312 4309 AccHist *p0 = accPrv_;
mjr 78:1e00b3fa11af 4310 if (manualCenterRequest_
mjr 78:1e00b3fa11af 4311 || (autoCenterMode_ <= 60
mjr 78:1e00b3fa11af 4312 && p0[0].dsq < accTol
mjr 78:1e00b3fa11af 4313 && p0[1].dsq < accTol
mjr 78:1e00b3fa11af 4314 && p0[2].dsq < accTol
mjr 78:1e00b3fa11af 4315 && p0[3].dsq < accTol
mjr 78:1e00b3fa11af 4316 && p0[4].dsq < accTol))
mjr 77:0b96f6867312 4317 {
mjr 77:0b96f6867312 4318 // Figure the new calibration point as the average of
mjr 77:0b96f6867312 4319 // the samples over the rest period
mjr 77:0b96f6867312 4320 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5;
mjr 77:0b96f6867312 4321 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5;
mjr 78:1e00b3fa11af 4322
mjr 78:1e00b3fa11af 4323 // clear any pending manual centering request
mjr 78:1e00b3fa11af 4324 manualCenterRequest_ = false;
mjr 77:0b96f6867312 4325 }
mjr 77:0b96f6867312 4326 }
mjr 77:0b96f6867312 4327 else
mjr 77:0b96f6867312 4328 {
mjr 77:0b96f6867312 4329 // not enough samples yet; just up the count
mjr 77:0b96f6867312 4330 ++nAccPrv_;
mjr 77:0b96f6867312 4331 }
mjr 6:cc35eb643e8f 4332
mjr 77:0b96f6867312 4333 // clear the new item's running totals
mjr 77:0b96f6867312 4334 p->clearAvg();
mjr 5:a70c0bce770d 4335
mjr 77:0b96f6867312 4336 // reset the timer
mjr 77:0b96f6867312 4337 tCenter_.reset();
mjr 77:0b96f6867312 4338 }
mjr 5:a70c0bce770d 4339
mjr 77:0b96f6867312 4340 // report our integrated velocity reading in x,y
mjr 77:0b96f6867312 4341 x = rawToReport(xSum/nSum);
mjr 77:0b96f6867312 4342 y = rawToReport(ySum/nSum);
mjr 5:a70c0bce770d 4343
mjr 6:cc35eb643e8f 4344 #ifdef DEBUG_PRINTF
mjr 77:0b96f6867312 4345 if (x != 0 || y != 0)
mjr 77:0b96f6867312 4346 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 4347 #endif
mjr 77:0b96f6867312 4348 }
mjr 29:582472d0bc57 4349
mjr 3:3514575d4f86 4350 private:
mjr 6:cc35eb643e8f 4351 // adjust a raw acceleration figure to a usb report value
mjr 77:0b96f6867312 4352 int rawToReport(int v)
mjr 5:a70c0bce770d 4353 {
mjr 77:0b96f6867312 4354 // Scale to the joystick report range. The accelerometer
mjr 77:0b96f6867312 4355 // readings use the native 14-bit signed integer representation,
mjr 77:0b96f6867312 4356 // so their scale is 2^13.
mjr 77:0b96f6867312 4357 //
mjr 77:0b96f6867312 4358 // The 1G range is special: it uses the 2G native hardware range,
mjr 77:0b96f6867312 4359 // but rescales the result to a 1G range for the joystick reports.
mjr 77:0b96f6867312 4360 // So for that mode, we divide by 4096 rather than 8192. All of
mjr 77:0b96f6867312 4361 // the other modes map use the hardware scaling directly.
mjr 77:0b96f6867312 4362 int i = v*JOYMAX;
mjr 77:0b96f6867312 4363 i = (range_ == AccelRange1G ? i/4096 : i/8192);
mjr 5:a70c0bce770d 4364
mjr 6:cc35eb643e8f 4365 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 4366 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 4367 static const int filter[] = {
mjr 6:cc35eb643e8f 4368 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 4369 0,
mjr 6:cc35eb643e8f 4370 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 4371 };
mjr 6:cc35eb643e8f 4372 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 4373 }
mjr 5:a70c0bce770d 4374
mjr 3:3514575d4f86 4375 // underlying accelerometer object
mjr 3:3514575d4f86 4376 MMA8451Q mma_;
mjr 3:3514575d4f86 4377
mjr 77:0b96f6867312 4378 // last raw acceleration readings, on the device's signed 14-bit
mjr 77:0b96f6867312 4379 // scale -8192..+8191
mjr 77:0b96f6867312 4380 int ax_, ay_, az_;
mjr 77:0b96f6867312 4381
mjr 77:0b96f6867312 4382 // running sum of readings since last get()
mjr 77:0b96f6867312 4383 int xSum_, ySum_;
mjr 77:0b96f6867312 4384
mjr 77:0b96f6867312 4385 // number of readings since last get()
mjr 77:0b96f6867312 4386 int nSum_;
mjr 6:cc35eb643e8f 4387
mjr 6:cc35eb643e8f 4388 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 4389 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 4390 // at rest.
mjr 77:0b96f6867312 4391 int cx_, cy_;
mjr 77:0b96f6867312 4392
mjr 77:0b96f6867312 4393 // range (AccelRangeXxx value, from config.h)
mjr 77:0b96f6867312 4394 uint8_t range_;
mjr 78:1e00b3fa11af 4395
mjr 78:1e00b3fa11af 4396 // auto-center mode:
mjr 78:1e00b3fa11af 4397 // 0 = default of 5-second auto-centering
mjr 78:1e00b3fa11af 4398 // 1-60 = auto-center after this many seconds
mjr 78:1e00b3fa11af 4399 // 255 = auto-centering off (manual centering only)
mjr 78:1e00b3fa11af 4400 uint8_t autoCenterMode_;
mjr 78:1e00b3fa11af 4401
mjr 78:1e00b3fa11af 4402 // flag: a manual centering request is pending
mjr 78:1e00b3fa11af 4403 bool manualCenterRequest_;
mjr 78:1e00b3fa11af 4404
mjr 78:1e00b3fa11af 4405 // time in us between auto-centering incremental checks
mjr 78:1e00b3fa11af 4406 uint32_t autoCenterCheckTime_;
mjr 78:1e00b3fa11af 4407
mjr 77:0b96f6867312 4408 // atuo-centering timer
mjr 5:a70c0bce770d 4409 Timer tCenter_;
mjr 6:cc35eb643e8f 4410
mjr 6:cc35eb643e8f 4411 // Auto-centering history. This is a separate history list that
mjr 77:0b96f6867312 4412 // records results spaced out sparsely over time, so that we can
mjr 6:cc35eb643e8f 4413 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 4414 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 4415 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 4416 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 4417 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 4418 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 4419 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 4420 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 4421 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 4422 // accelerometer measurement bias (e.g., from temperature changes).
mjr 78:1e00b3fa11af 4423 uint8_t iAccPrv_, nAccPrv_;
mjr 78:1e00b3fa11af 4424 static const uint8_t maxAccPrv = 5;
mjr 6:cc35eb643e8f 4425 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 4426
mjr 5:a70c0bce770d 4427 // interurupt pin name
mjr 5:a70c0bce770d 4428 PinName irqPin_;
mjr 3:3514575d4f86 4429 };
mjr 3:3514575d4f86 4430
mjr 5:a70c0bce770d 4431 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 4432 //
mjr 14:df700b22ca08 4433 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 4434 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 4435 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 4436 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 4437 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 4438 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 4439 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 4440 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 4441 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 4442 //
mjr 14:df700b22ca08 4443 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 4444 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 4445 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 4446 //
mjr 5:a70c0bce770d 4447 void clear_i2c()
mjr 5:a70c0bce770d 4448 {
mjr 38:091e511ce8a0 4449 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 4450 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 4451 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 4452
mjr 5:a70c0bce770d 4453 // clock the SCL 9 times
mjr 5:a70c0bce770d 4454 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 4455 {
mjr 5:a70c0bce770d 4456 scl = 1;
mjr 5:a70c0bce770d 4457 wait_us(20);
mjr 5:a70c0bce770d 4458 scl = 0;
mjr 5:a70c0bce770d 4459 wait_us(20);
mjr 5:a70c0bce770d 4460 }
mjr 5:a70c0bce770d 4461 }
mjr 76:7f5912b6340e 4462
mjr 76:7f5912b6340e 4463
mjr 14:df700b22ca08 4464 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 4465 //
mjr 33:d832bcab089e 4466 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 4467 // for a given interval before allowing an update.
mjr 33:d832bcab089e 4468 //
mjr 33:d832bcab089e 4469 class Debouncer
mjr 33:d832bcab089e 4470 {
mjr 33:d832bcab089e 4471 public:
mjr 33:d832bcab089e 4472 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 4473 {
mjr 33:d832bcab089e 4474 t.start();
mjr 33:d832bcab089e 4475 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 4476 this->tmin = tmin;
mjr 33:d832bcab089e 4477 }
mjr 33:d832bcab089e 4478
mjr 33:d832bcab089e 4479 // Get the current stable value
mjr 33:d832bcab089e 4480 bool val() const { return stable; }
mjr 33:d832bcab089e 4481
mjr 33:d832bcab089e 4482 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 4483 // input device.
mjr 33:d832bcab089e 4484 void sampleIn(bool val)
mjr 33:d832bcab089e 4485 {
mjr 33:d832bcab089e 4486 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 4487 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 4488 // on the sample reader.
mjr 33:d832bcab089e 4489 if (val != prv)
mjr 33:d832bcab089e 4490 {
mjr 33:d832bcab089e 4491 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 4492 t.reset();
mjr 33:d832bcab089e 4493
mjr 33:d832bcab089e 4494 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 4495 prv = val;
mjr 33:d832bcab089e 4496 }
mjr 33:d832bcab089e 4497 else if (val != stable)
mjr 33:d832bcab089e 4498 {
mjr 33:d832bcab089e 4499 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 4500 // and different from the stable value. This means that
mjr 33:d832bcab089e 4501 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 4502 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 4503 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 4504 if (t.read() > tmin)
mjr 33:d832bcab089e 4505 stable = val;
mjr 33:d832bcab089e 4506 }
mjr 33:d832bcab089e 4507 }
mjr 33:d832bcab089e 4508
mjr 33:d832bcab089e 4509 private:
mjr 33:d832bcab089e 4510 // current stable value
mjr 33:d832bcab089e 4511 bool stable;
mjr 33:d832bcab089e 4512
mjr 33:d832bcab089e 4513 // last raw sample value
mjr 33:d832bcab089e 4514 bool prv;
mjr 33:d832bcab089e 4515
mjr 33:d832bcab089e 4516 // elapsed time since last raw input change
mjr 33:d832bcab089e 4517 Timer t;
mjr 33:d832bcab089e 4518
mjr 33:d832bcab089e 4519 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 4520 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 4521 float tmin;
mjr 33:d832bcab089e 4522 };
mjr 33:d832bcab089e 4523
mjr 33:d832bcab089e 4524
mjr 33:d832bcab089e 4525 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 4526 //
mjr 33:d832bcab089e 4527 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 4528 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 4529 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 4530 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 4531 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 4532 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 4533 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 4534 //
mjr 33:d832bcab089e 4535 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 4536 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 4537 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 4538 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 4539 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 4540 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 4541 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 4542 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 4543 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 4544 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 4545 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 4546 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 4547 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 4548 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 4549 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 4550 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 4551 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 4552 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 4553 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 4554 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 4555 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 4556 //
mjr 40:cc0d9814522b 4557 // This scheme might seem a little convoluted, but it handles a number
mjr 40:cc0d9814522b 4558 // of tricky but likely scenarios:
mjr 33:d832bcab089e 4559 //
mjr 33:d832bcab089e 4560 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 40:cc0d9814522b 4561 // so that the PC goes into "Soft Off" mode when the user turns off
mjr 40:cc0d9814522b 4562 // the cabinet by pushing the power button or using the Shut Down
mjr 40:cc0d9814522b 4563 // command from within Windows. In Windows parlance, this "soft off"
mjr 40:cc0d9814522b 4564 // condition is called ACPI State S5. In this state, the main CPU
mjr 40:cc0d9814522b 4565 // power is turned off, but the motherboard still provides power to
mjr 40:cc0d9814522b 4566 // USB devices. This means that the KL25Z keeps running. Without
mjr 40:cc0d9814522b 4567 // the external power sensing circuit, the only hint that we're in
mjr 40:cc0d9814522b 4568 // this state is that the USB connection to the host goes into Suspend
mjr 40:cc0d9814522b 4569 // mode, but that could mean other things as well. The latch circuit
mjr 40:cc0d9814522b 4570 // lets us tell for sure that we're in this state.
mjr 33:d832bcab089e 4571 //
mjr 33:d832bcab089e 4572 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 4573 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 4574 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 4575 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 4576 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 4577 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 4578 // will power up before or after PSU2, so it's not good enough to
mjr 40:cc0d9814522b 4579 // observe the current state of PSU2 when we first check. If PSU2
mjr 40:cc0d9814522b 4580 // were to come on first, checking only the current state would fool
mjr 40:cc0d9814522b 4581 // us into thinking that no action is required, because we'd only see
mjr 40:cc0d9814522b 4582 // that PSU2 is turned on any time we check. The latch handles this
mjr 40:cc0d9814522b 4583 // case by letting us see that PSU2 was indeed off some time before our
mjr 40:cc0d9814522b 4584 // first check.
mjr 33:d832bcab089e 4585 //
mjr 33:d832bcab089e 4586 // - If the KL25Z is rebooted while the main system is running, or the
mjr 40:cc0d9814522b 4587 // KL25Z is unplugged and plugged back in, we'll correctly leave the
mjr 33:d832bcab089e 4588 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 4589 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 4590 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 4591 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 4592 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 4593 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 4594 //
mjr 33:d832bcab089e 4595
mjr 77:0b96f6867312 4596 // Current PSU2 power state:
mjr 33:d832bcab089e 4597 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 4598 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 4599 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 4600 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 4601 // 5 -> TV relay on
mjr 77:0b96f6867312 4602 // 6 -> sending IR signals designed as TV ON signals
mjr 73:4e8ce0b18915 4603 uint8_t psu2_state = 1;
mjr 73:4e8ce0b18915 4604
mjr 73:4e8ce0b18915 4605 // TV relay state. The TV relay can be controlled by the power-on
mjr 73:4e8ce0b18915 4606 // timer and directly from the PC (via USB commands), so keep a
mjr 73:4e8ce0b18915 4607 // separate state for each:
mjr 73:4e8ce0b18915 4608 // 0x01 -> turned on by power-on timer
mjr 73:4e8ce0b18915 4609 // 0x02 -> turned on by USB command
mjr 73:4e8ce0b18915 4610 uint8_t tv_relay_state = 0x00;
mjr 73:4e8ce0b18915 4611 const uint8_t TV_RELAY_POWERON = 0x01;
mjr 73:4e8ce0b18915 4612 const uint8_t TV_RELAY_USB = 0x02;
mjr 73:4e8ce0b18915 4613
mjr 79:682ae3171a08 4614 // pulse timer for manual TV relay pulses
mjr 79:682ae3171a08 4615 Timer tvRelayManualTimer;
mjr 79:682ae3171a08 4616
mjr 77:0b96f6867312 4617 // TV ON IR command state. When the main PSU2 power state reaches
mjr 77:0b96f6867312 4618 // the IR phase, we use this sub-state counter to send the TV ON
mjr 77:0b96f6867312 4619 // IR signals. We initialize to state 0 when the main state counter
mjr 77:0b96f6867312 4620 // reaches the IR step. In state 0, we start transmitting the first
mjr 77:0b96f6867312 4621 // (lowest numbered) IR command slot marked as containing a TV ON
mjr 77:0b96f6867312 4622 // code, and advance to state 1. In state 1, we check to see if
mjr 77:0b96f6867312 4623 // the transmitter is still sending; if so, we do nothing, if so
mjr 77:0b96f6867312 4624 // we start transmitting the second TV ON code and advance to state
mjr 77:0b96f6867312 4625 // 2. Continue until we run out of TV ON IR codes, at which point
mjr 77:0b96f6867312 4626 // we advance to the next main psu2_state step.
mjr 77:0b96f6867312 4627 uint8_t tvon_ir_state = 0;
mjr 77:0b96f6867312 4628
mjr 77:0b96f6867312 4629 // TV ON switch relay control output pin
mjr 73:4e8ce0b18915 4630 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 4631
mjr 35:e959ffba78fd 4632 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 4633 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 4634 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 4635
mjr 73:4e8ce0b18915 4636 // Apply the current TV relay state
mjr 73:4e8ce0b18915 4637 void tvRelayUpdate(uint8_t bit, bool state)
mjr 73:4e8ce0b18915 4638 {
mjr 73:4e8ce0b18915 4639 // update the state
mjr 73:4e8ce0b18915 4640 if (state)
mjr 73:4e8ce0b18915 4641 tv_relay_state |= bit;
mjr 73:4e8ce0b18915 4642 else
mjr 73:4e8ce0b18915 4643 tv_relay_state &= ~bit;
mjr 73:4e8ce0b18915 4644
mjr 73:4e8ce0b18915 4645 // set the relay GPIO to the new state
mjr 73:4e8ce0b18915 4646 if (tv_relay != 0)
mjr 73:4e8ce0b18915 4647 tv_relay->write(tv_relay_state != 0);
mjr 73:4e8ce0b18915 4648 }
mjr 35:e959ffba78fd 4649
mjr 86:e30a1f60f783 4650 // Does the current power status allow a reboot? We shouldn't reboot
mjr 86:e30a1f60f783 4651 // in certain power states, because some states are purely internal:
mjr 86:e30a1f60f783 4652 // we can't get enough information from the external power sensor to
mjr 86:e30a1f60f783 4653 // return to the same state later. Code that performs discretionary
mjr 86:e30a1f60f783 4654 // reboots should always check here first, and delay any reboot until
mjr 86:e30a1f60f783 4655 // we say it's okay.
mjr 86:e30a1f60f783 4656 static inline bool powerStatusAllowsReboot()
mjr 86:e30a1f60f783 4657 {
mjr 86:e30a1f60f783 4658 // The only safe state for rebooting is state 1, idle/default.
mjr 86:e30a1f60f783 4659 // In other states, we can't reboot, because the external sensor
mjr 86:e30a1f60f783 4660 // and latch circuit doesn't give us enough information to return
mjr 86:e30a1f60f783 4661 // to the same state later.
mjr 86:e30a1f60f783 4662 return psu2_state == 1;
mjr 86:e30a1f60f783 4663 }
mjr 86:e30a1f60f783 4664
mjr 77:0b96f6867312 4665 // PSU2 Status update routine. The main loop calls this from time
mjr 77:0b96f6867312 4666 // to time to update the power sensing state and carry out TV ON
mjr 77:0b96f6867312 4667 // functions.
mjr 77:0b96f6867312 4668 Timer powerStatusTimer;
mjr 77:0b96f6867312 4669 uint32_t tv_delay_time_us;
mjr 77:0b96f6867312 4670 void powerStatusUpdate(Config &cfg)
mjr 33:d832bcab089e 4671 {
mjr 79:682ae3171a08 4672 // If the manual relay pulse timer is past the pulse time, end the
mjr 79:682ae3171a08 4673 // manual pulse. The timer only runs when a pulse is active, so
mjr 79:682ae3171a08 4674 // it'll never read as past the time limit if a pulse isn't on.
mjr 79:682ae3171a08 4675 if (tvRelayManualTimer.read_us() > 250000)
mjr 79:682ae3171a08 4676 {
mjr 79:682ae3171a08 4677 // turn off the relay and disable the timer
mjr 79:682ae3171a08 4678 tvRelayUpdate(TV_RELAY_USB, false);
mjr 79:682ae3171a08 4679 tvRelayManualTimer.stop();
mjr 79:682ae3171a08 4680 tvRelayManualTimer.reset();
mjr 79:682ae3171a08 4681 }
mjr 79:682ae3171a08 4682
mjr 77:0b96f6867312 4683 // Only update every 1/4 second or so. Note that if the PSU2
mjr 77:0b96f6867312 4684 // circuit isn't configured, the initialization routine won't
mjr 77:0b96f6867312 4685 // start the timer, so it'll always read zero and we'll always
mjr 77:0b96f6867312 4686 // skip this whole routine.
mjr 77:0b96f6867312 4687 if (powerStatusTimer.read_us() < 250000)
mjr 77:0b96f6867312 4688 return;
mjr 77:0b96f6867312 4689
mjr 77:0b96f6867312 4690 // reset the update timer for next time
mjr 77:0b96f6867312 4691 powerStatusTimer.reset();
mjr 77:0b96f6867312 4692
mjr 77:0b96f6867312 4693 // TV ON timer. We start this timer when we detect a change
mjr 77:0b96f6867312 4694 // in the PSU2 status from OFF to ON. When the timer reaches
mjr 77:0b96f6867312 4695 // the configured TV ON delay time, and the PSU2 power is still
mjr 77:0b96f6867312 4696 // on, we'll trigger the TV ON relay and send the TV ON IR codes.
mjr 35:e959ffba78fd 4697 static Timer tv_timer;
mjr 35:e959ffba78fd 4698
mjr 33:d832bcab089e 4699 // Check our internal state
mjr 33:d832bcab089e 4700 switch (psu2_state)
mjr 33:d832bcab089e 4701 {
mjr 33:d832bcab089e 4702 case 1:
mjr 33:d832bcab089e 4703 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 4704 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 4705 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 4706 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 4707 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 4708 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 4709 {
mjr 33:d832bcab089e 4710 // switch to OFF state
mjr 33:d832bcab089e 4711 psu2_state = 2;
mjr 33:d832bcab089e 4712
mjr 33:d832bcab089e 4713 // try setting the latch
mjr 35:e959ffba78fd 4714 psu2_status_set->write(1);
mjr 33:d832bcab089e 4715 }
mjr 77:0b96f6867312 4716 powerTimerDiagState = 0;
mjr 33:d832bcab089e 4717 break;
mjr 33:d832bcab089e 4718
mjr 33:d832bcab089e 4719 case 2:
mjr 33:d832bcab089e 4720 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 4721 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 4722 psu2_status_set->write(0);
mjr 33:d832bcab089e 4723 psu2_state = 3;
mjr 77:0b96f6867312 4724 powerTimerDiagState = 0;
mjr 33:d832bcab089e 4725 break;
mjr 33:d832bcab089e 4726
mjr 33:d832bcab089e 4727 case 3:
mjr 33:d832bcab089e 4728 // CHECK state: we pulsed SET, and we're now ready to see
mjr 40:cc0d9814522b 4729 // if it stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 4730 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 4731 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 4732 if (psu2_status_sense->read())
mjr 33:d832bcab089e 4733 {
mjr 33:d832bcab089e 4734 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 4735 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 4736 tv_timer.reset();
mjr 33:d832bcab089e 4737 tv_timer.start();
mjr 33:d832bcab089e 4738 psu2_state = 4;
mjr 73:4e8ce0b18915 4739
mjr 73:4e8ce0b18915 4740 // start the power timer diagnostic flashes
mjr 73:4e8ce0b18915 4741 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4742 }
mjr 33:d832bcab089e 4743 else
mjr 33:d832bcab089e 4744 {
mjr 33:d832bcab089e 4745 // The latch didn't stick, so PSU2 was still off at
mjr 87:8d35c74403af 4746 // our last check. Return to idle state.
mjr 87:8d35c74403af 4747 psu2_state = 1;
mjr 33:d832bcab089e 4748 }
mjr 33:d832bcab089e 4749 break;
mjr 33:d832bcab089e 4750
mjr 33:d832bcab089e 4751 case 4:
mjr 77:0b96f6867312 4752 // TV timer countdown in progress. The latch has to stay on during
mjr 77:0b96f6867312 4753 // the countdown; if the latch turns off, PSU2 power must have gone
mjr 77:0b96f6867312 4754 // off again before the countdown finished.
mjr 77:0b96f6867312 4755 if (!psu2_status_sense->read())
mjr 77:0b96f6867312 4756 {
mjr 77:0b96f6867312 4757 // power is off - start a new check cycle
mjr 77:0b96f6867312 4758 psu2_status_set->write(1);
mjr 77:0b96f6867312 4759 psu2_state = 2;
mjr 77:0b96f6867312 4760 break;
mjr 77:0b96f6867312 4761 }
mjr 77:0b96f6867312 4762
mjr 77:0b96f6867312 4763 // Flash the power time diagnostic every two cycles
mjr 77:0b96f6867312 4764 powerTimerDiagState = (powerTimerDiagState + 1) & 0x03;
mjr 77:0b96f6867312 4765
mjr 77:0b96f6867312 4766 // if we've reached the delay time, pulse the relay
mjr 77:0b96f6867312 4767 if (tv_timer.read_us() >= tv_delay_time_us)
mjr 33:d832bcab089e 4768 {
mjr 33:d832bcab089e 4769 // turn on the relay for one timer interval
mjr 73:4e8ce0b18915 4770 tvRelayUpdate(TV_RELAY_POWERON, true);
mjr 33:d832bcab089e 4771 psu2_state = 5;
mjr 77:0b96f6867312 4772
mjr 77:0b96f6867312 4773 // show solid blue on the diagnostic LED while the relay is on
mjr 77:0b96f6867312 4774 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4775 }
mjr 33:d832bcab089e 4776 break;
mjr 33:d832bcab089e 4777
mjr 33:d832bcab089e 4778 case 5:
mjr 33:d832bcab089e 4779 // TV timer relay on. We pulse this for one interval, so
mjr 77:0b96f6867312 4780 // it's now time to turn it off.
mjr 73:4e8ce0b18915 4781 tvRelayUpdate(TV_RELAY_POWERON, false);
mjr 77:0b96f6867312 4782
mjr 77:0b96f6867312 4783 // Proceed to sending any TV ON IR commands
mjr 77:0b96f6867312 4784 psu2_state = 6;
mjr 77:0b96f6867312 4785 tvon_ir_state = 0;
mjr 77:0b96f6867312 4786
mjr 77:0b96f6867312 4787 // diagnostic LEDs off for now
mjr 77:0b96f6867312 4788 powerTimerDiagState = 0;
mjr 77:0b96f6867312 4789 break;
mjr 77:0b96f6867312 4790
mjr 77:0b96f6867312 4791 case 6:
mjr 77:0b96f6867312 4792 // Sending TV ON IR signals. Start with the assumption that
mjr 77:0b96f6867312 4793 // we have no IR work to do, in which case we're done with the
mjr 77:0b96f6867312 4794 // whole TV ON sequence. So by default return to state 1.
mjr 33:d832bcab089e 4795 psu2_state = 1;
mjr 77:0b96f6867312 4796 powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 4797
mjr 77:0b96f6867312 4798 // If we have an IR emitter, check for TV ON IR commands
mjr 77:0b96f6867312 4799 if (ir_tx != 0)
mjr 77:0b96f6867312 4800 {
mjr 77:0b96f6867312 4801 // check to see if the last transmission is still in progress
mjr 77:0b96f6867312 4802 if (ir_tx->isSending())
mjr 77:0b96f6867312 4803 {
mjr 77:0b96f6867312 4804 // We're still sending the last transmission. Stay in
mjr 77:0b96f6867312 4805 // state 6.
mjr 77:0b96f6867312 4806 psu2_state = 6;
mjr 77:0b96f6867312 4807 powerTimerDiagState = 4;
mjr 77:0b96f6867312 4808 break;
mjr 77:0b96f6867312 4809 }
mjr 77:0b96f6867312 4810
mjr 77:0b96f6867312 4811 // The last transmission is done, so check for a new one.
mjr 77:0b96f6867312 4812 // Look for the Nth TV ON IR slot, where N is our state
mjr 77:0b96f6867312 4813 // number.
mjr 77:0b96f6867312 4814 for (int i = 0, n = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 4815 {
mjr 77:0b96f6867312 4816 // is this a TV ON command?
mjr 77:0b96f6867312 4817 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 4818 {
mjr 77:0b96f6867312 4819 // It's a TV ON command - check if it's the one we're
mjr 109:310ac82cbbee 4820 // looking for. We can match any code starting at the
mjr 109:310ac82cbbee 4821 // current state. (We ignore codes BEFORE the current
mjr 109:310ac82cbbee 4822 // state, because we've already processed them on past
mjr 109:310ac82cbbee 4823 // iterations.)
mjr 109:310ac82cbbee 4824 if (n >= tvon_ir_state)
mjr 77:0b96f6867312 4825 {
mjr 77:0b96f6867312 4826 // It's the one. Start transmitting it by
mjr 77:0b96f6867312 4827 // pushing its virtual button.
mjr 77:0b96f6867312 4828 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 4829 ir_tx->pushButton(vb, true);
mjr 77:0b96f6867312 4830
mjr 77:0b96f6867312 4831 // Pushing the button starts transmission, and once
mjr 88:98bce687e6c0 4832 // started, the transmission runs to completion even
mjr 88:98bce687e6c0 4833 // if the button is no longer pushed. So we can
mjr 88:98bce687e6c0 4834 // immediately un-push the button, since we only need
mjr 88:98bce687e6c0 4835 // to send the code once.
mjr 77:0b96f6867312 4836 ir_tx->pushButton(vb, false);
mjr 77:0b96f6867312 4837
mjr 77:0b96f6867312 4838 // Advance to the next TV ON IR state, where we'll
mjr 77:0b96f6867312 4839 // await the end of this transmission and move on to
mjr 77:0b96f6867312 4840 // the next one.
mjr 77:0b96f6867312 4841 psu2_state = 6;
mjr 77:0b96f6867312 4842 tvon_ir_state++;
mjr 77:0b96f6867312 4843 break;
mjr 77:0b96f6867312 4844 }
mjr 77:0b96f6867312 4845
mjr 77:0b96f6867312 4846 // it's not ours - count it and keep looking
mjr 77:0b96f6867312 4847 ++n;
mjr 77:0b96f6867312 4848 }
mjr 77:0b96f6867312 4849 }
mjr 77:0b96f6867312 4850 }
mjr 33:d832bcab089e 4851 break;
mjr 33:d832bcab089e 4852 }
mjr 77:0b96f6867312 4853
mjr 77:0b96f6867312 4854 // update the diagnostic LEDs
mjr 77:0b96f6867312 4855 diagLED();
mjr 33:d832bcab089e 4856 }
mjr 33:d832bcab089e 4857
mjr 77:0b96f6867312 4858 // Start the power status timer. If the status sense circuit is enabled
mjr 77:0b96f6867312 4859 // in the configuration, we'll set up the pin connections and start the
mjr 77:0b96f6867312 4860 // timer for our periodic status checks. Does nothing if any of the pins
mjr 77:0b96f6867312 4861 // are configured as NC.
mjr 77:0b96f6867312 4862 void startPowerStatusTimer(Config &cfg)
mjr 35:e959ffba78fd 4863 {
mjr 55:4db125cd11a0 4864 // only start the timer if the pins are configured and the delay
mjr 55:4db125cd11a0 4865 // time is nonzero
mjr 77:0b96f6867312 4866 powerStatusTimer.reset();
mjr 77:0b96f6867312 4867 if (cfg.TVON.statusPin != 0xFF
mjr 77:0b96f6867312 4868 && cfg.TVON.latchPin != 0xFF)
mjr 35:e959ffba78fd 4869 {
mjr 77:0b96f6867312 4870 // set up the power sensing circuit connections
mjr 53:9b2611964afc 4871 psu2_status_sense = new DigitalIn(wirePinName(cfg.TVON.statusPin));
mjr 53:9b2611964afc 4872 psu2_status_set = new DigitalOut(wirePinName(cfg.TVON.latchPin));
mjr 77:0b96f6867312 4873
mjr 77:0b96f6867312 4874 // if there's a TV ON relay, set up its control pin
mjr 77:0b96f6867312 4875 if (cfg.TVON.relayPin != 0xFF)
mjr 77:0b96f6867312 4876 tv_relay = new DigitalOut(wirePinName(cfg.TVON.relayPin));
mjr 77:0b96f6867312 4877
mjr 77:0b96f6867312 4878 // Set the TV ON delay time. We store the time internally in
mjr 77:0b96f6867312 4879 // microseconds, but the configuration stores it in units of
mjr 77:0b96f6867312 4880 // 1/100 second = 10ms = 10000us.
mjr 77:0b96f6867312 4881 tv_delay_time_us = cfg.TVON.delayTime * 10000;;
mjr 77:0b96f6867312 4882
mjr 77:0b96f6867312 4883 // Start the TV timer
mjr 77:0b96f6867312 4884 powerStatusTimer.start();
mjr 35:e959ffba78fd 4885 }
mjr 35:e959ffba78fd 4886 }
mjr 35:e959ffba78fd 4887
mjr 73:4e8ce0b18915 4888 // Operate the TV ON relay. This allows manual control of the relay
mjr 73:4e8ce0b18915 4889 // from the PC. See protocol message 65 submessage 11.
mjr 73:4e8ce0b18915 4890 //
mjr 73:4e8ce0b18915 4891 // Mode:
mjr 73:4e8ce0b18915 4892 // 0 = turn relay off
mjr 73:4e8ce0b18915 4893 // 1 = turn relay on
mjr 73:4e8ce0b18915 4894 // 2 = pulse relay
mjr 73:4e8ce0b18915 4895 void TVRelay(int mode)
mjr 73:4e8ce0b18915 4896 {
mjr 73:4e8ce0b18915 4897 // if there's no TV relay control pin, ignore this
mjr 73:4e8ce0b18915 4898 if (tv_relay == 0)
mjr 73:4e8ce0b18915 4899 return;
mjr 73:4e8ce0b18915 4900
mjr 73:4e8ce0b18915 4901 switch (mode)
mjr 73:4e8ce0b18915 4902 {
mjr 73:4e8ce0b18915 4903 case 0:
mjr 73:4e8ce0b18915 4904 // relay off
mjr 73:4e8ce0b18915 4905 tvRelayUpdate(TV_RELAY_USB, false);
mjr 73:4e8ce0b18915 4906 break;
mjr 73:4e8ce0b18915 4907
mjr 73:4e8ce0b18915 4908 case 1:
mjr 73:4e8ce0b18915 4909 // relay on
mjr 73:4e8ce0b18915 4910 tvRelayUpdate(TV_RELAY_USB, true);
mjr 73:4e8ce0b18915 4911 break;
mjr 73:4e8ce0b18915 4912
mjr 73:4e8ce0b18915 4913 case 2:
mjr 79:682ae3171a08 4914 // Turn the relay on and reset the manual TV pulse timer
mjr 73:4e8ce0b18915 4915 tvRelayUpdate(TV_RELAY_USB, true);
mjr 79:682ae3171a08 4916 tvRelayManualTimer.reset();
mjr 79:682ae3171a08 4917 tvRelayManualTimer.start();
mjr 73:4e8ce0b18915 4918 break;
mjr 73:4e8ce0b18915 4919 }
mjr 73:4e8ce0b18915 4920 }
mjr 73:4e8ce0b18915 4921
mjr 73:4e8ce0b18915 4922
mjr 35:e959ffba78fd 4923 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4924 //
mjr 35:e959ffba78fd 4925 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 4926 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 4927 //
mjr 35:e959ffba78fd 4928 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 4929 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 4930 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 4931 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 4932 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 4933 // again each time the firmware is updated.
mjr 35:e959ffba78fd 4934 //
mjr 35:e959ffba78fd 4935 NVM nvm;
mjr 35:e959ffba78fd 4936
mjr 86:e30a1f60f783 4937 // Save Config followup time, in seconds. After a successful save,
mjr 86:e30a1f60f783 4938 // we leave the success flag on in the status for this interval. At
mjr 86:e30a1f60f783 4939 // the end of the interval, we reboot the device if requested.
mjr 86:e30a1f60f783 4940 uint8_t saveConfigFollowupTime;
mjr 86:e30a1f60f783 4941
mjr 86:e30a1f60f783 4942 // is a reboot pending at the end of the config save followup interval?
mjr 86:e30a1f60f783 4943 uint8_t saveConfigRebootPending;
mjr 77:0b96f6867312 4944
mjr 79:682ae3171a08 4945 // status flag for successful config save - set to 0x40 on success
mjr 79:682ae3171a08 4946 uint8_t saveConfigSucceededFlag;
mjr 79:682ae3171a08 4947
mjr 86:e30a1f60f783 4948 // Timer for configuration change followup timer
mjr 86:e30a1f60f783 4949 ExtTimer saveConfigFollowupTimer;
mjr 86:e30a1f60f783 4950
mjr 86:e30a1f60f783 4951
mjr 35:e959ffba78fd 4952 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 4953 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 4954
mjr 35:e959ffba78fd 4955 // flash memory controller interface
mjr 35:e959ffba78fd 4956 FreescaleIAP iap;
mjr 35:e959ffba78fd 4957
mjr 79:682ae3171a08 4958 // figure the flash address for the config data
mjr 79:682ae3171a08 4959 const NVM *configFlashAddr()
mjr 76:7f5912b6340e 4960 {
mjr 79:682ae3171a08 4961 // figure the number of sectors we need, rounding up
mjr 79:682ae3171a08 4962 int nSectors = (sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE;
mjr 79:682ae3171a08 4963
mjr 79:682ae3171a08 4964 // figure the total size required from the number of sectors
mjr 79:682ae3171a08 4965 int reservedSize = nSectors * SECTOR_SIZE;
mjr 79:682ae3171a08 4966
mjr 79:682ae3171a08 4967 // locate it at the top of memory
mjr 79:682ae3171a08 4968 uint32_t addr = iap.flashSize() - reservedSize;
mjr 79:682ae3171a08 4969
mjr 79:682ae3171a08 4970 // return it as a read-only NVM pointer
mjr 79:682ae3171a08 4971 return (const NVM *)addr;
mjr 35:e959ffba78fd 4972 }
mjr 35:e959ffba78fd 4973
mjr 76:7f5912b6340e 4974 // Load the config from flash. Returns true if a valid non-default
mjr 76:7f5912b6340e 4975 // configuration was loaded, false if we not. If we return false,
mjr 76:7f5912b6340e 4976 // we load the factory defaults, so the configuration object is valid
mjr 76:7f5912b6340e 4977 // in either case.
mjr 76:7f5912b6340e 4978 bool loadConfigFromFlash()
mjr 35:e959ffba78fd 4979 {
mjr 35:e959ffba78fd 4980 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 4981 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 4982 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 4983 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 4984 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 4985 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 4986 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 4987 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 4988 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 4989 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 4990 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 4991 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 4992 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 4993 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 4994 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 4995 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 4996
mjr 35:e959ffba78fd 4997 // Figure how many sectors we need for our structure
mjr 79:682ae3171a08 4998 const NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 4999
mjr 35:e959ffba78fd 5000 // if the flash is valid, load it; otherwise initialize to defaults
mjr 76:7f5912b6340e 5001 bool nvm_valid = flash->valid();
mjr 76:7f5912b6340e 5002 if (nvm_valid)
mjr 35:e959ffba78fd 5003 {
mjr 35:e959ffba78fd 5004 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 5005 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 5006 }
mjr 35:e959ffba78fd 5007 else
mjr 35:e959ffba78fd 5008 {
mjr 76:7f5912b6340e 5009 // flash is invalid - load factory settings into RAM structure
mjr 35:e959ffba78fd 5010 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 5011 }
mjr 76:7f5912b6340e 5012
mjr 76:7f5912b6340e 5013 // tell the caller what happened
mjr 76:7f5912b6340e 5014 return nvm_valid;
mjr 35:e959ffba78fd 5015 }
mjr 35:e959ffba78fd 5016
mjr 86:e30a1f60f783 5017 // Save the config. Returns true on success, false on failure.
mjr 86:e30a1f60f783 5018 // 'tFollowup' is the follow-up time in seconds. If the write is
mjr 86:e30a1f60f783 5019 // successful, we'll turn on the success flag in the status reports
mjr 86:e30a1f60f783 5020 // and leave it on for this interval. If 'reboot' is true, we'll
mjr 86:e30a1f60f783 5021 // also schedule a reboot at the end of the followup interval.
mjr 86:e30a1f60f783 5022 bool saveConfigToFlash(int tFollowup, bool reboot)
mjr 33:d832bcab089e 5023 {
mjr 76:7f5912b6340e 5024 // get the config block location in the flash memory
mjr 77:0b96f6867312 5025 uint32_t addr = uint32_t(configFlashAddr());
mjr 79:682ae3171a08 5026
mjr 101:755f44622abc 5027 // save the data
mjr 101:755f44622abc 5028 bool ok = nvm.save(iap, addr);
mjr 101:755f44622abc 5029
mjr 101:755f44622abc 5030 // if the save succeeded, do post-save work
mjr 101:755f44622abc 5031 if (ok)
mjr 86:e30a1f60f783 5032 {
mjr 86:e30a1f60f783 5033 // success - report the successful save in the status flags
mjr 86:e30a1f60f783 5034 saveConfigSucceededFlag = 0x40;
mjr 86:e30a1f60f783 5035
mjr 86:e30a1f60f783 5036 // start the followup timer
mjr 87:8d35c74403af 5037 saveConfigFollowupTime = tFollowup;
mjr 87:8d35c74403af 5038 saveConfigFollowupTimer.reset();
mjr 86:e30a1f60f783 5039 saveConfigFollowupTimer.start();
mjr 86:e30a1f60f783 5040
mjr 86:e30a1f60f783 5041 // if a reboot is pending, flag it
mjr 86:e30a1f60f783 5042 saveConfigRebootPending = reboot;
mjr 86:e30a1f60f783 5043 }
mjr 101:755f44622abc 5044
mjr 101:755f44622abc 5045 // return the success indication
mjr 101:755f44622abc 5046 return ok;
mjr 76:7f5912b6340e 5047 }
mjr 76:7f5912b6340e 5048
mjr 76:7f5912b6340e 5049 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 5050 //
mjr 76:7f5912b6340e 5051 // Host-loaded configuration. The Flash NVM block above is designed to be
mjr 76:7f5912b6340e 5052 // stored from within the firmware; in contrast, the host-loaded config is
mjr 76:7f5912b6340e 5053 // stored by the host, by patching the firwmare binary (.bin) file before
mjr 76:7f5912b6340e 5054 // downloading it to the device.
mjr 76:7f5912b6340e 5055 //
mjr 100:1ff35c07217c 5056 // Ideally, we'd use the host-loaded memory for all configuration updates
mjr 100:1ff35c07217c 5057 // from the host - that is, any time the host wants to update config settings,
mjr 100:1ff35c07217c 5058 // such as via user input in the config tool. In the past, I wanted to do
mjr 100:1ff35c07217c 5059 // it this way because it seemed to be unreliable to write flash memory via
mjr 100:1ff35c07217c 5060 // the device. But that turned out to be due to a bug in the mbed Ticker
mjr 100:1ff35c07217c 5061 // code (of all things!), which we've fixed - since then, flash writing on
mjr 100:1ff35c07217c 5062 // the device has been bulletproof. Even so, doing host-to-device flash
mjr 100:1ff35c07217c 5063 // writing for config updates would be nice just for the sake of speed, as
mjr 100:1ff35c07217c 5064 // the alternative is that we send the variables one at a time by USB, which
mjr 100:1ff35c07217c 5065 // takes noticeable time when reprogramming the whole config set. But
mjr 100:1ff35c07217c 5066 // there's no way to accomplish a single-sector flash write via OpenSDA; you
mjr 100:1ff35c07217c 5067 // can only rewrite the entire flash memory as a unit.
mjr 100:1ff35c07217c 5068 //
mjr 100:1ff35c07217c 5069 // We can at least use this approach to do a fast configuration restore
mjr 100:1ff35c07217c 5070 // when downloading new firmware. In that case, we're rewriting all of
mjr 100:1ff35c07217c 5071 // flash memory anyway, so we might as well include the config data.
mjr 76:7f5912b6340e 5072 //
mjr 76:7f5912b6340e 5073 // The memory here is stored using the same format as the USB "Set Config
mjr 76:7f5912b6340e 5074 // Variable" command. These messages are 8 bytes long and start with a
mjr 76:7f5912b6340e 5075 // byte value 66, followed by the variable ID, followed by the variable
mjr 76:7f5912b6340e 5076 // value data in a format defined separately for each variable. To load
mjr 76:7f5912b6340e 5077 // the data, we'll start at the first byte after the signature, and
mjr 76:7f5912b6340e 5078 // interpret each 8-byte block as a type 66 message. If the first byte
mjr 76:7f5912b6340e 5079 // of a block is not 66, we'll take it as the end of the data.
mjr 76:7f5912b6340e 5080 //
mjr 76:7f5912b6340e 5081 // We provide a block of storage here big enough for 1,024 variables.
mjr 76:7f5912b6340e 5082 // The header consists of a 30-byte signature followed by two bytes giving
mjr 76:7f5912b6340e 5083 // the available space in the area, in this case 8192 == 0x0200. The
mjr 76:7f5912b6340e 5084 // length is little-endian. Note that the linker will implicitly zero
mjr 76:7f5912b6340e 5085 // the rest of the block, so if the host doesn't populate it, we'll see
mjr 76:7f5912b6340e 5086 // that it's empty by virtue of not containing the required '66' byte
mjr 76:7f5912b6340e 5087 // prefix for the first 8-byte variable block.
mjr 76:7f5912b6340e 5088 static const uint8_t hostLoadedConfig[8192+32]
mjr 76:7f5912b6340e 5089 __attribute__ ((aligned(SECTOR_SIZE))) =
mjr 76:7f5912b6340e 5090 "///Pinscape.HostLoadedConfig//\0\040"; // 30 byte signature + 2 byte length
mjr 76:7f5912b6340e 5091
mjr 76:7f5912b6340e 5092 // Get a pointer to the first byte of the configuration data
mjr 76:7f5912b6340e 5093 const uint8_t *getHostLoadedConfigData()
mjr 76:7f5912b6340e 5094 {
mjr 76:7f5912b6340e 5095 // the first configuration variable byte immediately follows the
mjr 76:7f5912b6340e 5096 // 32-byte signature header
mjr 76:7f5912b6340e 5097 return hostLoadedConfig + 32;
mjr 76:7f5912b6340e 5098 };
mjr 76:7f5912b6340e 5099
mjr 76:7f5912b6340e 5100 // forward reference to config var store function
mjr 76:7f5912b6340e 5101 void configVarSet(const uint8_t *);
mjr 76:7f5912b6340e 5102
mjr 76:7f5912b6340e 5103 // Load the host-loaded configuration data into the active (RAM)
mjr 76:7f5912b6340e 5104 // configuration object.
mjr 76:7f5912b6340e 5105 void loadHostLoadedConfig()
mjr 76:7f5912b6340e 5106 {
mjr 76:7f5912b6340e 5107 // Start at the first configuration variable. Each variable
mjr 76:7f5912b6340e 5108 // block is in the format of a Set Config Variable command in
mjr 76:7f5912b6340e 5109 // the USB protocol, so each block starts with a byte value of
mjr 76:7f5912b6340e 5110 // 66 and is 8 bytes long. Continue as long as we find valid
mjr 76:7f5912b6340e 5111 // variable blocks, or reach end end of the block.
mjr 76:7f5912b6340e 5112 const uint8_t *start = getHostLoadedConfigData();
mjr 76:7f5912b6340e 5113 const uint8_t *end = hostLoadedConfig + sizeof(hostLoadedConfig);
mjr 76:7f5912b6340e 5114 for (const uint8_t *p = getHostLoadedConfigData() ; start < end && *p == 66 ; p += 8)
mjr 76:7f5912b6340e 5115 {
mjr 76:7f5912b6340e 5116 // load this variable
mjr 76:7f5912b6340e 5117 configVarSet(p);
mjr 76:7f5912b6340e 5118 }
mjr 35:e959ffba78fd 5119 }
mjr 35:e959ffba78fd 5120
mjr 35:e959ffba78fd 5121 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5122 //
mjr 55:4db125cd11a0 5123 // Pixel dump mode - the host requested a dump of image sensor pixels
mjr 55:4db125cd11a0 5124 // (helpful for installing and setting up the sensor and light source)
mjr 55:4db125cd11a0 5125 //
mjr 55:4db125cd11a0 5126 bool reportPlungerStat = false;
mjr 55:4db125cd11a0 5127 uint8_t reportPlungerStatFlags; // plunger pixel report flag bits (see ccdSensor.h)
mjr 55:4db125cd11a0 5128 uint8_t reportPlungerStatTime; // extra exposure time for plunger pixel report
mjr 101:755f44622abc 5129 uint8_t tReportPlungerStat; // timestamp of most recent plunger status request
mjr 55:4db125cd11a0 5130
mjr 55:4db125cd11a0 5131
mjr 55:4db125cd11a0 5132 // ---------------------------------------------------------------------------
mjr 55:4db125cd11a0 5133 //
mjr 40:cc0d9814522b 5134 // Night mode setting updates
mjr 40:cc0d9814522b 5135 //
mjr 38:091e511ce8a0 5136
mjr 38:091e511ce8a0 5137 // Turn night mode on or off
mjr 38:091e511ce8a0 5138 static void setNightMode(bool on)
mjr 38:091e511ce8a0 5139 {
mjr 77:0b96f6867312 5140 // Set the new night mode flag in the noisy output class. Note
mjr 77:0b96f6867312 5141 // that we use the status report bit flag value 0x02 when on, so
mjr 77:0b96f6867312 5142 // that we can just '|' this into the overall status bits.
mjr 77:0b96f6867312 5143 nightMode = on ? 0x02 : 0x00;
mjr 55:4db125cd11a0 5144
mjr 40:cc0d9814522b 5145 // update the special output pin that shows the night mode state
mjr 53:9b2611964afc 5146 int port = int(cfg.nightMode.port) - 1;
mjr 53:9b2611964afc 5147 if (port >= 0 && port < numOutputs)
mjr 53:9b2611964afc 5148 lwPin[port]->set(nightMode ? 255 : 0);
mjr 76:7f5912b6340e 5149
mjr 76:7f5912b6340e 5150 // Reset all outputs at their current value, so that the underlying
mjr 76:7f5912b6340e 5151 // physical outputs get turned on or off as appropriate for the night
mjr 76:7f5912b6340e 5152 // mode change.
mjr 76:7f5912b6340e 5153 for (int i = 0 ; i < numOutputs ; ++i)
mjr 76:7f5912b6340e 5154 lwPin[i]->set(outLevel[i]);
mjr 76:7f5912b6340e 5155
mjr 76:7f5912b6340e 5156 // update 74HC595 outputs
mjr 76:7f5912b6340e 5157 if (hc595 != 0)
mjr 76:7f5912b6340e 5158 hc595->update();
mjr 38:091e511ce8a0 5159 }
mjr 38:091e511ce8a0 5160
mjr 38:091e511ce8a0 5161 // Toggle night mode
mjr 38:091e511ce8a0 5162 static void toggleNightMode()
mjr 38:091e511ce8a0 5163 {
mjr 53:9b2611964afc 5164 setNightMode(!nightMode);
mjr 38:091e511ce8a0 5165 }
mjr 38:091e511ce8a0 5166
mjr 38:091e511ce8a0 5167
mjr 38:091e511ce8a0 5168 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 5169 //
mjr 35:e959ffba78fd 5170 // Plunger Sensor
mjr 35:e959ffba78fd 5171 //
mjr 35:e959ffba78fd 5172
mjr 35:e959ffba78fd 5173 // the plunger sensor interface object
mjr 35:e959ffba78fd 5174 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 5175
mjr 76:7f5912b6340e 5176
mjr 35:e959ffba78fd 5177 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 5178 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 5179 void createPlunger()
mjr 35:e959ffba78fd 5180 {
mjr 35:e959ffba78fd 5181 // create the new sensor object according to the type
mjr 35:e959ffba78fd 5182 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 5183 {
mjr 82:4f6209cb5c33 5184 case PlungerType_TSL1410R:
mjr 82:4f6209cb5c33 5185 // TSL1410R, shadow edge detector
mjr 35:e959ffba78fd 5186 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 5187 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 5188 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 5189 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5190 wirePinName(cfg.plunger.sensorPin[2]));
mjr 35:e959ffba78fd 5191 break;
mjr 35:e959ffba78fd 5192
mjr 82:4f6209cb5c33 5193 case PlungerType_TSL1412S:
mjr 82:4f6209cb5c33 5194 // TSL1412S, shadow edge detector
mjr 82:4f6209cb5c33 5195 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 5196 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 5197 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 5198 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5199 wirePinName(cfg.plunger.sensorPin[2]));
mjr 35:e959ffba78fd 5200 break;
mjr 35:e959ffba78fd 5201
mjr 35:e959ffba78fd 5202 case PlungerType_Pot:
mjr 82:4f6209cb5c33 5203 // Potentiometer (or any other sensor with a linear analog voltage
mjr 82:4f6209cb5c33 5204 // reading as the proxy for the position)
mjr 82:4f6209cb5c33 5205 // pins are: AO (analog in)
mjr 53:9b2611964afc 5206 plungerSensor = new PlungerSensorPot(
mjr 53:9b2611964afc 5207 wirePinName(cfg.plunger.sensorPin[0]));
mjr 35:e959ffba78fd 5208 break;
mjr 82:4f6209cb5c33 5209
mjr 82:4f6209cb5c33 5210 case PlungerType_OptQuad:
mjr 82:4f6209cb5c33 5211 // Optical quadrature sensor, AEDR8300-K or similar. The -K is
mjr 82:4f6209cb5c33 5212 // designed for a 75 LPI scale, which translates to 300 pulses/inch.
mjr 82:4f6209cb5c33 5213 // Pins are: CHA, CHB (quadrature pulse inputs).
mjr 82:4f6209cb5c33 5214 plungerSensor = new PlungerSensorQuad(
mjr 82:4f6209cb5c33 5215 300,
mjr 82:4f6209cb5c33 5216 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5217 wirePinName(cfg.plunger.sensorPin[1]));
mjr 82:4f6209cb5c33 5218 break;
mjr 82:4f6209cb5c33 5219
mjr 82:4f6209cb5c33 5220 case PlungerType_TSL1401CL:
mjr 82:4f6209cb5c33 5221 // TSL1401CL, absolute position encoder with bar code scale
mjr 82:4f6209cb5c33 5222 // pins are: SI, CLOCK, AO
mjr 82:4f6209cb5c33 5223 plungerSensor = new PlungerSensorTSL1401CL(
mjr 82:4f6209cb5c33 5224 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5225 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5226 wirePinName(cfg.plunger.sensorPin[2]));
mjr 82:4f6209cb5c33 5227 break;
mjr 82:4f6209cb5c33 5228
mjr 82:4f6209cb5c33 5229 case PlungerType_VL6180X:
mjr 82:4f6209cb5c33 5230 // VL6180X time-of-flight IR distance sensor
mjr 111:42dc75fbe623 5231 // pins are: SDA, SCL, GPIO0/CE
mjr 82:4f6209cb5c33 5232 plungerSensor = new PlungerSensorVL6180X(
mjr 82:4f6209cb5c33 5233 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5234 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5235 wirePinName(cfg.plunger.sensorPin[2]));
mjr 82:4f6209cb5c33 5236 break;
mjr 82:4f6209cb5c33 5237
mjr 100:1ff35c07217c 5238 case PlungerType_AEAT6012:
mjr 100:1ff35c07217c 5239 // Broadcom AEAT-6012-A06 magnetic rotary encoder
mjr 100:1ff35c07217c 5240 // pins are: CS (chip select, dig out), CLK (dig out), DO (data, dig in)
mjr 100:1ff35c07217c 5241 plungerSensor = new PlungerSensorAEAT601X<12>(
mjr 100:1ff35c07217c 5242 wirePinName(cfg.plunger.sensorPin[0]),
mjr 100:1ff35c07217c 5243 wirePinName(cfg.plunger.sensorPin[1]),
mjr 100:1ff35c07217c 5244 wirePinName(cfg.plunger.sensorPin[2]));
mjr 100:1ff35c07217c 5245 break;
mjr 100:1ff35c07217c 5246
mjr 100:1ff35c07217c 5247 case PlungerType_TCD1103:
mjr 100:1ff35c07217c 5248 // Toshiba TCD1103GFG linear CCD, optical edge detection, with
mjr 100:1ff35c07217c 5249 // inverted logic gates.
mjr 100:1ff35c07217c 5250 //
mjr 100:1ff35c07217c 5251 // Pins are: fM (master clock, PWM), OS (sample data, analog in),
mjr 100:1ff35c07217c 5252 // ICG (integration clear gate, dig out), SH (shift gate, dig out)
mjr 100:1ff35c07217c 5253 plungerSensor = new PlungerSensorTCD1103<true>(
mjr 100:1ff35c07217c 5254 wirePinName(cfg.plunger.sensorPin[0]),
mjr 100:1ff35c07217c 5255 wirePinName(cfg.plunger.sensorPin[1]),
mjr 100:1ff35c07217c 5256 wirePinName(cfg.plunger.sensorPin[2]),
mjr 100:1ff35c07217c 5257 wirePinName(cfg.plunger.sensorPin[3]));
mjr 100:1ff35c07217c 5258 break;
mjr 100:1ff35c07217c 5259
mjr 111:42dc75fbe623 5260 case PlungerType_VCNL4010:
mjr 111:42dc75fbe623 5261 // VCNL4010 IR proximity sensor pins are: SDA, SCL
mjr 111:42dc75fbe623 5262 plungerSensor = new PlungerSensorVCNL4010(
mjr 111:42dc75fbe623 5263 wirePinName(cfg.plunger.sensorPin[0]),
mjr 111:42dc75fbe623 5264 wirePinName(cfg.plunger.sensorPin[1]));
mjr 111:42dc75fbe623 5265 break;
mjr 111:42dc75fbe623 5266
mjr 35:e959ffba78fd 5267 case PlungerType_None:
mjr 35:e959ffba78fd 5268 default:
mjr 35:e959ffba78fd 5269 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 5270 break;
mjr 35:e959ffba78fd 5271 }
mjr 100:1ff35c07217c 5272
mjr 100:1ff35c07217c 5273 // initialize the plunger from the saved configuration
mjr 100:1ff35c07217c 5274 plungerSensor->restoreCalibration(cfg);
mjr 86:e30a1f60f783 5275
mjr 87:8d35c74403af 5276 // initialize the config variables affecting the plunger
mjr 87:8d35c74403af 5277 plungerSensor->onConfigChange(19, cfg);
mjr 87:8d35c74403af 5278 plungerSensor->onConfigChange(20, cfg);
mjr 33:d832bcab089e 5279 }
mjr 33:d832bcab089e 5280
mjr 52:8298b2a73eb2 5281 // Global plunger calibration mode flag
mjr 52:8298b2a73eb2 5282 bool plungerCalMode;
mjr 52:8298b2a73eb2 5283
mjr 48:058ace2aed1d 5284 // Plunger reader
mjr 51:57eb311faafa 5285 //
mjr 51:57eb311faafa 5286 // This class encapsulates our plunger data processing. At the simplest
mjr 51:57eb311faafa 5287 // level, we read the position from the sensor, adjust it for the
mjr 51:57eb311faafa 5288 // calibration settings, and report the calibrated position to the host.
mjr 51:57eb311faafa 5289 //
mjr 51:57eb311faafa 5290 // In addition, we constantly monitor the data for "firing" motions.
mjr 51:57eb311faafa 5291 // A firing motion is when the user pulls back the plunger and releases
mjr 51:57eb311faafa 5292 // it, allowing it to shoot forward under the force of the main spring.
mjr 51:57eb311faafa 5293 // When we detect that this is happening, we briefly stop reporting the
mjr 51:57eb311faafa 5294 // real physical position that we're reading from the sensor, and instead
mjr 51:57eb311faafa 5295 // report a synthetic series of positions that depicts an idealized
mjr 51:57eb311faafa 5296 // firing motion.
mjr 51:57eb311faafa 5297 //
mjr 51:57eb311faafa 5298 // The point of the synthetic reports is to correct for distortions
mjr 51:57eb311faafa 5299 // created by the joystick interface conventions used by VP and other
mjr 51:57eb311faafa 5300 // PC pinball emulators. The convention they use is simply to have the
mjr 51:57eb311faafa 5301 // plunger device report the instantaneous position of the real plunger.
mjr 51:57eb311faafa 5302 // The PC software polls this reported position periodically, and moves
mjr 51:57eb311faafa 5303 // the on-screen virtual plunger in sync with the real plunger. This
mjr 51:57eb311faafa 5304 // works fine for human-scale motion when the user is manually moving
mjr 51:57eb311faafa 5305 // the plunger. But it doesn't work for the high speed motion of a
mjr 51:57eb311faafa 5306 // release. The plunger simply moves too fast. VP polls in about 10ms
mjr 51:57eb311faafa 5307 // intervals; the plunger takes about 50ms to travel from fully
mjr 51:57eb311faafa 5308 // retracted to the park position when released. The low sampling
mjr 51:57eb311faafa 5309 // rate relative to the rate of change of the sampled data creates
mjr 51:57eb311faafa 5310 // a classic digital aliasing effect.
mjr 51:57eb311faafa 5311 //
mjr 51:57eb311faafa 5312 // The synthetic reporting scheme compensates for the interface
mjr 51:57eb311faafa 5313 // distortions by essentially changing to a coarse enough timescale
mjr 51:57eb311faafa 5314 // that VP can reliably interpret the readings. Conceptually, there
mjr 51:57eb311faafa 5315 // are three steps involved in doing this. First, we analyze the
mjr 51:57eb311faafa 5316 // actual sensor data to detect and characterize the release motion.
mjr 51:57eb311faafa 5317 // Second, once we think we have a release in progress, we fit the
mjr 51:57eb311faafa 5318 // data to a mathematical model of the release. The model we use is
mjr 51:57eb311faafa 5319 // dead simple: we consider the release to have one parameter, namely
mjr 51:57eb311faafa 5320 // the retraction distance at the moment the user lets go. This is an
mjr 51:57eb311faafa 5321 // excellent proxy in the real physical system for the final speed
mjr 51:57eb311faafa 5322 // when the plunger hits the ball, and it also happens to match how
mjr 51:57eb311faafa 5323 // VP models it internally. Third, we construct synthetic reports
mjr 51:57eb311faafa 5324 // that will make VP's internal state match our model. This is also
mjr 51:57eb311faafa 5325 // pretty simple: we just need to send VP the maximum retraction
mjr 51:57eb311faafa 5326 // distance for long enough to be sure that it polls it at least
mjr 51:57eb311faafa 5327 // once, and then send it the park position for long enough to
mjr 51:57eb311faafa 5328 // ensure that VP will complete the same firing motion. The
mjr 51:57eb311faafa 5329 // immediate jump from the maximum point to the zero point will
mjr 51:57eb311faafa 5330 // cause VP to move its simulation model plunger forward from the
mjr 51:57eb311faafa 5331 // starting point at its natural spring acceleration rate, which
mjr 51:57eb311faafa 5332 // is exactly what the real plunger just did.
mjr 51:57eb311faafa 5333 //
mjr 48:058ace2aed1d 5334 class PlungerReader
mjr 48:058ace2aed1d 5335 {
mjr 48:058ace2aed1d 5336 public:
mjr 48:058ace2aed1d 5337 PlungerReader()
mjr 48:058ace2aed1d 5338 {
mjr 48:058ace2aed1d 5339 // not in a firing event yet
mjr 48:058ace2aed1d 5340 firing = 0;
mjr 48:058ace2aed1d 5341 }
mjr 76:7f5912b6340e 5342
mjr 48:058ace2aed1d 5343 // Collect a reading from the plunger sensor. The main loop calls
mjr 48:058ace2aed1d 5344 // this frequently to read the current raw position data from the
mjr 48:058ace2aed1d 5345 // sensor. We analyze the raw data to produce the calibrated
mjr 48:058ace2aed1d 5346 // position that we report to the PC via the joystick interface.
mjr 48:058ace2aed1d 5347 void read()
mjr 48:058ace2aed1d 5348 {
mjr 76:7f5912b6340e 5349 // if the sensor is busy, skip the reading on this round
mjr 76:7f5912b6340e 5350 if (!plungerSensor->ready())
mjr 76:7f5912b6340e 5351 return;
mjr 76:7f5912b6340e 5352
mjr 48:058ace2aed1d 5353 // Read a sample from the sensor
mjr 48:058ace2aed1d 5354 PlungerReading r;
mjr 48:058ace2aed1d 5355 if (plungerSensor->read(r))
mjr 48:058ace2aed1d 5356 {
mjr 53:9b2611964afc 5357 // check for calibration mode
mjr 53:9b2611964afc 5358 if (plungerCalMode)
mjr 53:9b2611964afc 5359 {
mjr 53:9b2611964afc 5360 // Calibration mode. Adjust the calibration bounds to fit
mjr 53:9b2611964afc 5361 // the value. If this value is beyond the current min or max,
mjr 53:9b2611964afc 5362 // expand the envelope to include this new value.
mjr 53:9b2611964afc 5363 if (r.pos > cfg.plunger.cal.max)
mjr 53:9b2611964afc 5364 cfg.plunger.cal.max = r.pos;
mjr 53:9b2611964afc 5365 if (r.pos < cfg.plunger.cal.min)
mjr 53:9b2611964afc 5366 cfg.plunger.cal.min = r.pos;
mjr 76:7f5912b6340e 5367
mjr 76:7f5912b6340e 5368 // update our cached calibration data
mjr 76:7f5912b6340e 5369 onUpdateCal();
mjr 50:40015764bbe6 5370
mjr 53:9b2611964afc 5371 // If we're in calibration state 0, we're waiting for the
mjr 53:9b2611964afc 5372 // plunger to come to rest at the park position so that we
mjr 53:9b2611964afc 5373 // can take a sample of the park position. Check to see if
mjr 53:9b2611964afc 5374 // we've been at rest for a minimum interval.
mjr 53:9b2611964afc 5375 if (calState == 0)
mjr 53:9b2611964afc 5376 {
mjr 53:9b2611964afc 5377 if (abs(r.pos - calZeroStart.pos) < 65535/3/50)
mjr 53:9b2611964afc 5378 {
mjr 53:9b2611964afc 5379 // we're close enough - make sure we've been here long enough
mjr 53:9b2611964afc 5380 if (uint32_t(r.t - calZeroStart.t) > 100000UL)
mjr 53:9b2611964afc 5381 {
mjr 53:9b2611964afc 5382 // we've been at rest long enough - count it
mjr 53:9b2611964afc 5383 calZeroPosSum += r.pos;
mjr 53:9b2611964afc 5384 calZeroPosN += 1;
mjr 53:9b2611964afc 5385
mjr 53:9b2611964afc 5386 // update the zero position from the new average
mjr 53:9b2611964afc 5387 cfg.plunger.cal.zero = uint16_t(calZeroPosSum / calZeroPosN);
mjr 76:7f5912b6340e 5388 onUpdateCal();
mjr 53:9b2611964afc 5389
mjr 53:9b2611964afc 5390 // switch to calibration state 1 - at rest
mjr 53:9b2611964afc 5391 calState = 1;
mjr 53:9b2611964afc 5392 }
mjr 53:9b2611964afc 5393 }
mjr 53:9b2611964afc 5394 else
mjr 53:9b2611964afc 5395 {
mjr 53:9b2611964afc 5396 // we're not close to the last position - start again here
mjr 53:9b2611964afc 5397 calZeroStart = r;
mjr 53:9b2611964afc 5398 }
mjr 53:9b2611964afc 5399 }
mjr 53:9b2611964afc 5400
mjr 53:9b2611964afc 5401 // Rescale to the joystick range, and adjust for the current
mjr 53:9b2611964afc 5402 // park position, but don't calibrate. We don't know the maximum
mjr 53:9b2611964afc 5403 // point yet, so we can't calibrate the range.
mjr 53:9b2611964afc 5404 r.pos = int(
mjr 53:9b2611964afc 5405 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 5406 / (65535 - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 5407 }
mjr 53:9b2611964afc 5408 else
mjr 53:9b2611964afc 5409 {
mjr 53:9b2611964afc 5410 // Not in calibration mode. Apply the existing calibration and
mjr 53:9b2611964afc 5411 // rescale to the joystick range.
mjr 76:7f5912b6340e 5412 r.pos = applyCal(r.pos);
mjr 53:9b2611964afc 5413
mjr 53:9b2611964afc 5414 // limit the result to the valid joystick range
mjr 53:9b2611964afc 5415 if (r.pos > JOYMAX)
mjr 53:9b2611964afc 5416 r.pos = JOYMAX;
mjr 53:9b2611964afc 5417 else if (r.pos < -JOYMAX)
mjr 53:9b2611964afc 5418 r.pos = -JOYMAX;
mjr 53:9b2611964afc 5419 }
mjr 50:40015764bbe6 5420
mjr 87:8d35c74403af 5421 // Look for a firing event - the user releasing the plunger and
mjr 87:8d35c74403af 5422 // allowing it to shoot forward at full speed. Wait at least 5ms
mjr 87:8d35c74403af 5423 // between samples for this, to help distinguish random motion
mjr 87:8d35c74403af 5424 // from the rapid motion of a firing event.
mjr 50:40015764bbe6 5425 //
mjr 87:8d35c74403af 5426 // There's a trade-off in the choice of minimum sampling interval.
mjr 87:8d35c74403af 5427 // The longer we wait, the more certain we can be of the trend.
mjr 87:8d35c74403af 5428 // But if we wait too long, the user will perceive a delay. We
mjr 87:8d35c74403af 5429 // also want to sample frequently enough to see the release motion
mjr 87:8d35c74403af 5430 // at intermediate steps along the way, so the sampling has to be
mjr 87:8d35c74403af 5431 // considerably faster than the whole travel time, which is about
mjr 87:8d35c74403af 5432 // 25-50ms.
mjr 87:8d35c74403af 5433 if (uint32_t(r.t - prv.t) < 5000UL)
mjr 87:8d35c74403af 5434 return;
mjr 87:8d35c74403af 5435
mjr 87:8d35c74403af 5436 // assume that we'll report this reading as-is
mjr 87:8d35c74403af 5437 z = r.pos;
mjr 87:8d35c74403af 5438
mjr 87:8d35c74403af 5439 // Firing event detection.
mjr 87:8d35c74403af 5440 //
mjr 87:8d35c74403af 5441 // A "firing event" is when the player releases the plunger from
mjr 87:8d35c74403af 5442 // a retracted position, allowing it to shoot forward under the
mjr 87:8d35c74403af 5443 // spring tension.
mjr 50:40015764bbe6 5444 //
mjr 87:8d35c74403af 5445 // We monitor the plunger motion for these events, and when they
mjr 87:8d35c74403af 5446 // occur, we report an "idealized" version of the motion to the
mjr 87:8d35c74403af 5447 // PC. The idealized version consists of a series of readings
mjr 87:8d35c74403af 5448 // frozen at the fully retracted position for the whole duration
mjr 87:8d35c74403af 5449 // of the forward travel, followed by a series of readings at the
mjr 87:8d35c74403af 5450 // fully forward position for long enough for the plunger to come
mjr 87:8d35c74403af 5451 // mostly to rest. The series of frozen readings aren't meant to
mjr 87:8d35c74403af 5452 // be perceptible to the player - we try to keep them short enough
mjr 87:8d35c74403af 5453 // that they're not apparent as delay. Instead, they're for the
mjr 87:8d35c74403af 5454 // PC client software's benefit. PC joystick clients use polling,
mjr 87:8d35c74403af 5455 // so they only see an unpredictable subset of the readings we
mjr 87:8d35c74403af 5456 // send. The only way to be sure that the client sees a particular
mjr 87:8d35c74403af 5457 // reading is to hold it for long enough that the client is sure to
mjr 87:8d35c74403af 5458 // poll within the hold interval. In the case of the plunger
mjr 87:8d35c74403af 5459 // firing motion, it's important that the client sees the *ends*
mjr 87:8d35c74403af 5460 // of the travel - the fully retracted starting position in
mjr 87:8d35c74403af 5461 // particular. If the PC client only polls for a sample while the
mjr 87:8d35c74403af 5462 // plunger is somewhere in the middle of the travel, the PC will
mjr 87:8d35c74403af 5463 // think that the firing motion *started* in that middle position,
mjr 87:8d35c74403af 5464 // so it won't be able to model the right amount of momentum when
mjr 87:8d35c74403af 5465 // the plunger hits the ball. We try to ensure that the PC sees
mjr 87:8d35c74403af 5466 // the right starting point by reporting the starting point for
mjr 87:8d35c74403af 5467 // extra time during the forward motion. By the same token, we
mjr 87:8d35c74403af 5468 // want the PC to know that the plunger has moved all the way
mjr 87:8d35c74403af 5469 // forward, rather than mistakenly thinking that it stopped
mjr 87:8d35c74403af 5470 // somewhere in the middle of the travel, so we freeze at the
mjr 87:8d35c74403af 5471 // forward position for a short time.
mjr 76:7f5912b6340e 5472 //
mjr 87:8d35c74403af 5473 // To detect a firing event, we look for forward motion that's
mjr 87:8d35c74403af 5474 // fast enough to be a firing event. To determine how fast is
mjr 87:8d35c74403af 5475 // fast enough, we use a simple model of the plunger motion where
mjr 87:8d35c74403af 5476 // the acceleration is constant. This is only an approximation,
mjr 87:8d35c74403af 5477 // as the spring force actually varies with spring's compression,
mjr 87:8d35c74403af 5478 // but it's close enough for our purposes here.
mjr 87:8d35c74403af 5479 //
mjr 87:8d35c74403af 5480 // Do calculations in fixed-point 2^48 scale with 64-bit ints.
mjr 87:8d35c74403af 5481 // acc2 = acceleration/2 for 50ms release time, units of unit
mjr 87:8d35c74403af 5482 // distances per microsecond squared, where the unit distance
mjr 87:8d35c74403af 5483 // is the overall travel from the starting retracted position
mjr 87:8d35c74403af 5484 // to the park position.
mjr 87:8d35c74403af 5485 const int32_t acc2 = 112590; // 2^48 scale
mjr 50:40015764bbe6 5486 switch (firing)
mjr 50:40015764bbe6 5487 {
mjr 50:40015764bbe6 5488 case 0:
mjr 87:8d35c74403af 5489 // Not in firing mode. If we're retracted a bit, and the
mjr 87:8d35c74403af 5490 // motion is forward at a fast enough rate to look like a
mjr 87:8d35c74403af 5491 // release, enter firing mode.
mjr 87:8d35c74403af 5492 if (r.pos > JOYMAX/6)
mjr 50:40015764bbe6 5493 {
mjr 87:8d35c74403af 5494 const uint32_t dt = uint32_t(r.t - prv.t);
mjr 87:8d35c74403af 5495 const uint32_t dt2 = dt*dt; // dt^2
mjr 87:8d35c74403af 5496 if (r.pos < prv.pos - int((prv.pos*acc2*uint64_t(dt2)) >> 48))
mjr 87:8d35c74403af 5497 {
mjr 87:8d35c74403af 5498 // Tentatively enter firing mode. Use the prior reading
mjr 87:8d35c74403af 5499 // as the starting point, and freeze reports for now.
mjr 87:8d35c74403af 5500 firingMode(1);
mjr 87:8d35c74403af 5501 f0 = prv;
mjr 87:8d35c74403af 5502 z = f0.pos;
mjr 87:8d35c74403af 5503
mjr 87:8d35c74403af 5504 // if in calibration state 1 (at rest), switch to
mjr 87:8d35c74403af 5505 // state 2 (not at rest)
mjr 87:8d35c74403af 5506 if (calState == 1)
mjr 87:8d35c74403af 5507 calState = 2;
mjr 87:8d35c74403af 5508 }
mjr 50:40015764bbe6 5509 }
mjr 50:40015764bbe6 5510 break;
mjr 50:40015764bbe6 5511
mjr 50:40015764bbe6 5512 case 1:
mjr 87:8d35c74403af 5513 // Tentative firing mode: the plunger was moving forward
mjr 87:8d35c74403af 5514 // at last check. To stay in firing mode, the plunger has
mjr 87:8d35c74403af 5515 // to keep moving forward fast enough to look like it's
mjr 87:8d35c74403af 5516 // moving under spring force. To figure out how fast is
mjr 87:8d35c74403af 5517 // fast enough, we use a simple model where the acceleration
mjr 87:8d35c74403af 5518 // is constant over the whole travel distance and the total
mjr 87:8d35c74403af 5519 // travel time is 50ms. The acceleration actually varies
mjr 87:8d35c74403af 5520 // slightly since it comes from the spring force, which
mjr 87:8d35c74403af 5521 // is linear in the displacement; but the plunger spring is
mjr 87:8d35c74403af 5522 // fairly compressed even when the plunger is all the way
mjr 87:8d35c74403af 5523 // forward, so the difference in tension from one end of
mjr 87:8d35c74403af 5524 // the travel to the other is fairly small, so it's not too
mjr 87:8d35c74403af 5525 // far off to model it as constant. And the real travel
mjr 87:8d35c74403af 5526 // time obviously isn't a constant, but all we need for
mjr 87:8d35c74403af 5527 // that is an upper bound. So: we'll figure the time since
mjr 87:8d35c74403af 5528 // we entered firing mode, and figure the distance we should
mjr 87:8d35c74403af 5529 // have traveled to complete the trip within the maximum
mjr 87:8d35c74403af 5530 // time allowed. If we've moved far enough, we'll stay
mjr 87:8d35c74403af 5531 // in firing mode; if not, we'll exit firing mode. And if
mjr 87:8d35c74403af 5532 // we cross the finish line while still in firing mode,
mjr 87:8d35c74403af 5533 // we'll switch to the next phase of the firing event.
mjr 50:40015764bbe6 5534 if (r.pos <= 0)
mjr 50:40015764bbe6 5535 {
mjr 87:8d35c74403af 5536 // We crossed the park position. Switch to the second
mjr 87:8d35c74403af 5537 // phase of the firing event, where we hold the reported
mjr 87:8d35c74403af 5538 // position at the "bounce" position (where the plunger
mjr 87:8d35c74403af 5539 // is all the way forward, compressing the barrel spring).
mjr 87:8d35c74403af 5540 // We'll stick here long enough to ensure that the PC
mjr 87:8d35c74403af 5541 // client (Visual Pinball or whatever) sees the reading
mjr 87:8d35c74403af 5542 // and processes the release motion via the simulated
mjr 87:8d35c74403af 5543 // physics.
mjr 50:40015764bbe6 5544 firingMode(2);
mjr 53:9b2611964afc 5545
mjr 53:9b2611964afc 5546 // if in calibration mode, and we're in state 2 (moving),
mjr 53:9b2611964afc 5547 // collect firing statistics for calibration purposes
mjr 53:9b2611964afc 5548 if (plungerCalMode && calState == 2)
mjr 53:9b2611964afc 5549 {
mjr 53:9b2611964afc 5550 // collect a new zero point for the average when we
mjr 53:9b2611964afc 5551 // come to rest
mjr 53:9b2611964afc 5552 calState = 0;
mjr 53:9b2611964afc 5553
mjr 87:8d35c74403af 5554 // collect average firing time statistics in millseconds,
mjr 87:8d35c74403af 5555 // if it's in range (20 to 255 ms)
mjr 87:8d35c74403af 5556 const int dt = uint32_t(r.t - f0.t)/1000UL;
mjr 87:8d35c74403af 5557 if (dt >= 15 && dt <= 255)
mjr 53:9b2611964afc 5558 {
mjr 53:9b2611964afc 5559 calRlsTimeSum += dt;
mjr 53:9b2611964afc 5560 calRlsTimeN += 1;
mjr 53:9b2611964afc 5561 cfg.plunger.cal.tRelease = uint8_t(calRlsTimeSum / calRlsTimeN);
mjr 53:9b2611964afc 5562 }
mjr 53:9b2611964afc 5563 }
mjr 87:8d35c74403af 5564
mjr 87:8d35c74403af 5565 // Figure the "bounce" position as forward of the park
mjr 87:8d35c74403af 5566 // position by 1/6 of the starting retraction distance.
mjr 87:8d35c74403af 5567 // This simulates the momentum of the plunger compressing
mjr 87:8d35c74403af 5568 // the barrel spring on the rebound. The barrel spring
mjr 87:8d35c74403af 5569 // can compress by about 1/6 of the maximum retraction
mjr 87:8d35c74403af 5570 // distance, so we'll simply treat its compression as
mjr 87:8d35c74403af 5571 // proportional to the retraction. (It might be more
mjr 87:8d35c74403af 5572 // realistic to use a slightly higher value here, maybe
mjr 87:8d35c74403af 5573 // 1/4 or 1/3 or the retraction distance, capping it at
mjr 87:8d35c74403af 5574 // a maximum of 1/6, because the real plunger probably
mjr 87:8d35c74403af 5575 // compresses the barrel spring by 100% with less than
mjr 87:8d35c74403af 5576 // 100% retraction. But that won't affect the physics
mjr 87:8d35c74403af 5577 // meaningfully, just the animation, and the effect is
mjr 87:8d35c74403af 5578 // small in any case.)
mjr 87:8d35c74403af 5579 z = f0.pos = -f0.pos / 6;
mjr 87:8d35c74403af 5580
mjr 87:8d35c74403af 5581 // reset the starting time for this phase
mjr 87:8d35c74403af 5582 f0.t = r.t;
mjr 50:40015764bbe6 5583 }
mjr 50:40015764bbe6 5584 else
mjr 50:40015764bbe6 5585 {
mjr 87:8d35c74403af 5586 // check for motion since the start of the firing event
mjr 87:8d35c74403af 5587 const uint32_t dt = uint32_t(r.t - f0.t);
mjr 87:8d35c74403af 5588 const uint32_t dt2 = dt*dt; // dt^2
mjr 87:8d35c74403af 5589 if (dt < 50000
mjr 87:8d35c74403af 5590 && r.pos < f0.pos - int((f0.pos*acc2*uint64_t(dt2)) >> 48))
mjr 87:8d35c74403af 5591 {
mjr 87:8d35c74403af 5592 // It's moving fast enough to still be in a release
mjr 87:8d35c74403af 5593 // motion. Continue reporting the start position, and
mjr 87:8d35c74403af 5594 // stay in the first release phase.
mjr 87:8d35c74403af 5595 z = f0.pos;
mjr 87:8d35c74403af 5596 }
mjr 87:8d35c74403af 5597 else
mjr 87:8d35c74403af 5598 {
mjr 87:8d35c74403af 5599 // It's not moving fast enough to be a release
mjr 87:8d35c74403af 5600 // motion. Return to the default state.
mjr 87:8d35c74403af 5601 firingMode(0);
mjr 87:8d35c74403af 5602 calState = 1;
mjr 87:8d35c74403af 5603 }
mjr 50:40015764bbe6 5604 }
mjr 50:40015764bbe6 5605 break;
mjr 50:40015764bbe6 5606
mjr 50:40015764bbe6 5607 case 2:
mjr 87:8d35c74403af 5608 // Firing mode, holding at forward compression position.
mjr 87:8d35c74403af 5609 // Hold here for 25ms.
mjr 87:8d35c74403af 5610 if (uint32_t(r.t - f0.t) < 25000)
mjr 50:40015764bbe6 5611 {
mjr 87:8d35c74403af 5612 // stay here for now
mjr 87:8d35c74403af 5613 z = f0.pos;
mjr 50:40015764bbe6 5614 }
mjr 50:40015764bbe6 5615 else
mjr 50:40015764bbe6 5616 {
mjr 87:8d35c74403af 5617 // advance to the next phase, where we report the park
mjr 87:8d35c74403af 5618 // position until the plunger comes to rest
mjr 50:40015764bbe6 5619 firingMode(3);
mjr 50:40015764bbe6 5620 z = 0;
mjr 87:8d35c74403af 5621
mjr 87:8d35c74403af 5622 // remember when we started
mjr 87:8d35c74403af 5623 f0.t = r.t;
mjr 50:40015764bbe6 5624 }
mjr 50:40015764bbe6 5625 break;
mjr 50:40015764bbe6 5626
mjr 50:40015764bbe6 5627 case 3:
mjr 87:8d35c74403af 5628 // Firing event, holding at park position. Stay here for
mjr 87:8d35c74403af 5629 // a few moments so that the PC client can simulate the
mjr 87:8d35c74403af 5630 // full release motion, then return to real readings.
mjr 87:8d35c74403af 5631 if (uint32_t(r.t - f0.t) < 250000)
mjr 50:40015764bbe6 5632 {
mjr 87:8d35c74403af 5633 // stay here a while longer
mjr 87:8d35c74403af 5634 z = 0;
mjr 50:40015764bbe6 5635 }
mjr 50:40015764bbe6 5636 else
mjr 50:40015764bbe6 5637 {
mjr 87:8d35c74403af 5638 // it's been long enough - return to normal mode
mjr 87:8d35c74403af 5639 firingMode(0);
mjr 50:40015764bbe6 5640 }
mjr 50:40015764bbe6 5641 break;
mjr 50:40015764bbe6 5642 }
mjr 50:40015764bbe6 5643
mjr 82:4f6209cb5c33 5644 // Check for auto-zeroing, if enabled
mjr 82:4f6209cb5c33 5645 if ((cfg.plunger.autoZero.flags & PlungerAutoZeroEnabled) != 0)
mjr 82:4f6209cb5c33 5646 {
mjr 82:4f6209cb5c33 5647 // If we moved since the last reading, reset and restart the
mjr 82:4f6209cb5c33 5648 // auto-zero timer. Otherwise, if the timer has reached the
mjr 82:4f6209cb5c33 5649 // auto-zero timeout, it means we've been motionless for that
mjr 82:4f6209cb5c33 5650 // long, so auto-zero now.
mjr 82:4f6209cb5c33 5651 if (r.pos != prv.pos)
mjr 82:4f6209cb5c33 5652 {
mjr 82:4f6209cb5c33 5653 // movement detected - reset the timer
mjr 82:4f6209cb5c33 5654 autoZeroTimer.reset();
mjr 82:4f6209cb5c33 5655 autoZeroTimer.start();
mjr 82:4f6209cb5c33 5656 }
mjr 82:4f6209cb5c33 5657 else if (autoZeroTimer.read_us() > cfg.plunger.autoZero.t * 1000000UL)
mjr 82:4f6209cb5c33 5658 {
mjr 82:4f6209cb5c33 5659 // auto-zero now
mjr 82:4f6209cb5c33 5660 plungerSensor->autoZero();
mjr 82:4f6209cb5c33 5661
mjr 82:4f6209cb5c33 5662 // stop the timer so that we don't keep repeating this
mjr 82:4f6209cb5c33 5663 // if the plunger stays still for a long time
mjr 82:4f6209cb5c33 5664 autoZeroTimer.stop();
mjr 82:4f6209cb5c33 5665 autoZeroTimer.reset();
mjr 82:4f6209cb5c33 5666 }
mjr 82:4f6209cb5c33 5667 }
mjr 82:4f6209cb5c33 5668
mjr 87:8d35c74403af 5669 // this new reading becomes the previous reading for next time
mjr 87:8d35c74403af 5670 prv = r;
mjr 48:058ace2aed1d 5671 }
mjr 48:058ace2aed1d 5672 }
mjr 48:058ace2aed1d 5673
mjr 48:058ace2aed1d 5674 // Get the current value to report through the joystick interface
mjr 58:523fdcffbe6d 5675 int16_t getPosition()
mjr 58:523fdcffbe6d 5676 {
mjr 86:e30a1f60f783 5677 // return the last reading
mjr 86:e30a1f60f783 5678 return z;
mjr 55:4db125cd11a0 5679 }
mjr 58:523fdcffbe6d 5680
mjr 48:058ace2aed1d 5681 // Set calibration mode on or off
mjr 52:8298b2a73eb2 5682 void setCalMode(bool f)
mjr 48:058ace2aed1d 5683 {
mjr 52:8298b2a73eb2 5684 // check to see if we're entering calibration mode
mjr 52:8298b2a73eb2 5685 if (f && !plungerCalMode)
mjr 52:8298b2a73eb2 5686 {
mjr 52:8298b2a73eb2 5687 // reset the calibration in the configuration
mjr 48:058ace2aed1d 5688 cfg.plunger.cal.begin();
mjr 52:8298b2a73eb2 5689
mjr 52:8298b2a73eb2 5690 // start in state 0 (waiting to settle)
mjr 52:8298b2a73eb2 5691 calState = 0;
mjr 52:8298b2a73eb2 5692 calZeroPosSum = 0;
mjr 52:8298b2a73eb2 5693 calZeroPosN = 0;
mjr 52:8298b2a73eb2 5694 calRlsTimeSum = 0;
mjr 52:8298b2a73eb2 5695 calRlsTimeN = 0;
mjr 52:8298b2a73eb2 5696
mjr 82:4f6209cb5c33 5697 // tell the plunger we're starting calibration
mjr 100:1ff35c07217c 5698 plungerSensor->beginCalibration(cfg);
mjr 82:4f6209cb5c33 5699
mjr 52:8298b2a73eb2 5700 // set the initial zero point to the current position
mjr 52:8298b2a73eb2 5701 PlungerReading r;
mjr 52:8298b2a73eb2 5702 if (plungerSensor->read(r))
mjr 52:8298b2a73eb2 5703 {
mjr 52:8298b2a73eb2 5704 // got a reading - use it as the initial zero point
mjr 52:8298b2a73eb2 5705 cfg.plunger.cal.zero = r.pos;
mjr 76:7f5912b6340e 5706 onUpdateCal();
mjr 52:8298b2a73eb2 5707
mjr 52:8298b2a73eb2 5708 // use it as the starting point for the settling watch
mjr 53:9b2611964afc 5709 calZeroStart = r;
mjr 52:8298b2a73eb2 5710 }
mjr 52:8298b2a73eb2 5711 else
mjr 52:8298b2a73eb2 5712 {
mjr 52:8298b2a73eb2 5713 // no reading available - use the default 1/6 position
mjr 52:8298b2a73eb2 5714 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 5715 onUpdateCal();
mjr 52:8298b2a73eb2 5716
mjr 52:8298b2a73eb2 5717 // we don't have a starting point for the setting watch
mjr 53:9b2611964afc 5718 calZeroStart.pos = -65535;
mjr 53:9b2611964afc 5719 calZeroStart.t = 0;
mjr 53:9b2611964afc 5720 }
mjr 53:9b2611964afc 5721 }
mjr 53:9b2611964afc 5722 else if (!f && plungerCalMode)
mjr 53:9b2611964afc 5723 {
mjr 53:9b2611964afc 5724 // Leaving calibration mode. Make sure the max is past the
mjr 53:9b2611964afc 5725 // zero point - if it's not, we'd have a zero or negative
mjr 53:9b2611964afc 5726 // denominator for the scaling calculation, which would be
mjr 53:9b2611964afc 5727 // physically meaningless.
mjr 53:9b2611964afc 5728 if (cfg.plunger.cal.max <= cfg.plunger.cal.zero)
mjr 53:9b2611964afc 5729 {
mjr 53:9b2611964afc 5730 // bad settings - reset to defaults
mjr 53:9b2611964afc 5731 cfg.plunger.cal.max = 0xffff;
mjr 53:9b2611964afc 5732 cfg.plunger.cal.zero = 0xffff/6;
mjr 52:8298b2a73eb2 5733 }
mjr 100:1ff35c07217c 5734
mjr 100:1ff35c07217c 5735 // finalize the configuration in the plunger object
mjr 100:1ff35c07217c 5736 plungerSensor->endCalibration(cfg);
mjr 100:1ff35c07217c 5737
mjr 100:1ff35c07217c 5738 // update our internal cached information for the new calibration
mjr 100:1ff35c07217c 5739 onUpdateCal();
mjr 52:8298b2a73eb2 5740 }
mjr 52:8298b2a73eb2 5741
mjr 48:058ace2aed1d 5742 // remember the new mode
mjr 52:8298b2a73eb2 5743 plungerCalMode = f;
mjr 48:058ace2aed1d 5744 }
mjr 48:058ace2aed1d 5745
mjr 76:7f5912b6340e 5746 // Cached inverse of the calibration range. This is for calculating
mjr 76:7f5912b6340e 5747 // the calibrated plunger position given a raw sensor reading. The
mjr 76:7f5912b6340e 5748 // cached inverse is calculated as
mjr 76:7f5912b6340e 5749 //
mjr 76:7f5912b6340e 5750 // 64K * JOYMAX / (cfg.plunger.cal.max - cfg.plunger.cal.zero)
mjr 76:7f5912b6340e 5751 //
mjr 76:7f5912b6340e 5752 // To convert a raw sensor reading to a calibrated position, calculate
mjr 76:7f5912b6340e 5753 //
mjr 76:7f5912b6340e 5754 // ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16
mjr 76:7f5912b6340e 5755 //
mjr 76:7f5912b6340e 5756 // That yields the calibration result without performing a division.
mjr 76:7f5912b6340e 5757 int invCalRange;
mjr 76:7f5912b6340e 5758
mjr 76:7f5912b6340e 5759 // apply the calibration range to a reading
mjr 76:7f5912b6340e 5760 inline int applyCal(int reading)
mjr 76:7f5912b6340e 5761 {
mjr 76:7f5912b6340e 5762 return ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16;
mjr 76:7f5912b6340e 5763 }
mjr 76:7f5912b6340e 5764
mjr 76:7f5912b6340e 5765 void onUpdateCal()
mjr 76:7f5912b6340e 5766 {
mjr 76:7f5912b6340e 5767 invCalRange = (JOYMAX << 16)/(cfg.plunger.cal.max - cfg.plunger.cal.zero);
mjr 76:7f5912b6340e 5768 }
mjr 76:7f5912b6340e 5769
mjr 48:058ace2aed1d 5770 // is a firing event in progress?
mjr 53:9b2611964afc 5771 bool isFiring() { return firing == 3; }
mjr 76:7f5912b6340e 5772
mjr 48:058ace2aed1d 5773 private:
mjr 87:8d35c74403af 5774 // current reported joystick reading
mjr 87:8d35c74403af 5775 int z;
mjr 87:8d35c74403af 5776
mjr 87:8d35c74403af 5777 // previous reading
mjr 87:8d35c74403af 5778 PlungerReading prv;
mjr 87:8d35c74403af 5779
mjr 52:8298b2a73eb2 5780 // Calibration state. During calibration mode, we watch for release
mjr 52:8298b2a73eb2 5781 // events, to measure the time it takes to complete the release
mjr 52:8298b2a73eb2 5782 // motion; and we watch for the plunger to come to reset after a
mjr 52:8298b2a73eb2 5783 // release, to gather statistics on the rest position.
mjr 52:8298b2a73eb2 5784 // 0 = waiting to settle
mjr 52:8298b2a73eb2 5785 // 1 = at rest
mjr 52:8298b2a73eb2 5786 // 2 = retracting
mjr 52:8298b2a73eb2 5787 // 3 = possibly releasing
mjr 52:8298b2a73eb2 5788 uint8_t calState;
mjr 52:8298b2a73eb2 5789
mjr 52:8298b2a73eb2 5790 // Calibration zero point statistics.
mjr 52:8298b2a73eb2 5791 // During calibration mode, we collect data on the rest position (the
mjr 52:8298b2a73eb2 5792 // zero point) by watching for the plunger to come to rest after each
mjr 52:8298b2a73eb2 5793 // release. We average these rest positions to get the calibrated
mjr 52:8298b2a73eb2 5794 // zero point. We use the average because the real physical plunger
mjr 52:8298b2a73eb2 5795 // itself doesn't come to rest at exactly the same spot every time,
mjr 52:8298b2a73eb2 5796 // largely due to friction in the mechanism. To calculate the average,
mjr 52:8298b2a73eb2 5797 // we keep a sum of the readings and a count of samples.
mjr 53:9b2611964afc 5798 PlungerReading calZeroStart;
mjr 52:8298b2a73eb2 5799 long calZeroPosSum;
mjr 52:8298b2a73eb2 5800 int calZeroPosN;
mjr 52:8298b2a73eb2 5801
mjr 52:8298b2a73eb2 5802 // Calibration release time statistics.
mjr 52:8298b2a73eb2 5803 // During calibration, we collect an average for the release time.
mjr 52:8298b2a73eb2 5804 long calRlsTimeSum;
mjr 52:8298b2a73eb2 5805 int calRlsTimeN;
mjr 52:8298b2a73eb2 5806
mjr 85:3c28aee81cde 5807 // Auto-zeroing timer
mjr 85:3c28aee81cde 5808 Timer autoZeroTimer;
mjr 85:3c28aee81cde 5809
mjr 48:058ace2aed1d 5810 // set a firing mode
mjr 48:058ace2aed1d 5811 inline void firingMode(int m)
mjr 48:058ace2aed1d 5812 {
mjr 48:058ace2aed1d 5813 firing = m;
mjr 48:058ace2aed1d 5814 }
mjr 48:058ace2aed1d 5815
mjr 48:058ace2aed1d 5816 // Firing event state.
mjr 48:058ace2aed1d 5817 //
mjr 87:8d35c74403af 5818 // 0 - Default state: not in firing event. We report the true
mjr 87:8d35c74403af 5819 // instantaneous plunger position to the joystick interface.
mjr 48:058ace2aed1d 5820 //
mjr 87:8d35c74403af 5821 // 1 - Moving forward at release speed
mjr 48:058ace2aed1d 5822 //
mjr 87:8d35c74403af 5823 // 2 - Firing - reporting the bounce position
mjr 87:8d35c74403af 5824 //
mjr 87:8d35c74403af 5825 // 3 - Firing - reporting the park position
mjr 48:058ace2aed1d 5826 //
mjr 48:058ace2aed1d 5827 int firing;
mjr 48:058ace2aed1d 5828
mjr 87:8d35c74403af 5829 // Starting position for current firing mode phase
mjr 87:8d35c74403af 5830 PlungerReading f0;
mjr 48:058ace2aed1d 5831 };
mjr 48:058ace2aed1d 5832
mjr 48:058ace2aed1d 5833 // plunger reader singleton
mjr 48:058ace2aed1d 5834 PlungerReader plungerReader;
mjr 48:058ace2aed1d 5835
mjr 48:058ace2aed1d 5836 // ---------------------------------------------------------------------------
mjr 48:058ace2aed1d 5837 //
mjr 48:058ace2aed1d 5838 // Handle the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5839 //
mjr 48:058ace2aed1d 5840 // The ZB Launch Ball feature, if enabled, lets the mechanical plunger
mjr 48:058ace2aed1d 5841 // serve as a substitute for a physical Launch Ball button. When a table
mjr 48:058ace2aed1d 5842 // is loaded in VP, and the table has the ZB Launch Ball LedWiz port
mjr 48:058ace2aed1d 5843 // turned on, we'll disable mechanical plunger reports through the
mjr 48:058ace2aed1d 5844 // joystick interface and instead use the plunger only to simulate the
mjr 48:058ace2aed1d 5845 // Launch Ball button. When the mode is active, pulling back and
mjr 48:058ace2aed1d 5846 // releasing the plunger causes a brief simulated press of the Launch
mjr 48:058ace2aed1d 5847 // button, and pushing the plunger forward of the rest position presses
mjr 48:058ace2aed1d 5848 // the Launch button as long as the plunger is pressed forward.
mjr 48:058ace2aed1d 5849 //
mjr 48:058ace2aed1d 5850 // This feature has two configuration components:
mjr 48:058ace2aed1d 5851 //
mjr 48:058ace2aed1d 5852 // - An LedWiz port number. This port is a "virtual" port that doesn't
mjr 48:058ace2aed1d 5853 // have to be attached to any actual output. DOF uses it to signal
mjr 48:058ace2aed1d 5854 // that the current table uses a Launch button instead of a plunger.
mjr 48:058ace2aed1d 5855 // DOF simply turns the port on when such a table is loaded and turns
mjr 48:058ace2aed1d 5856 // it off at all other times. We use it to enable and disable the
mjr 48:058ace2aed1d 5857 // plunger/launch button connection.
mjr 48:058ace2aed1d 5858 //
mjr 48:058ace2aed1d 5859 // - A joystick button ID. We simulate pressing this button when the
mjr 48:058ace2aed1d 5860 // launch feature is activated via the LedWiz port and the plunger is
mjr 48:058ace2aed1d 5861 // either pulled back and releasd, or pushed forward past the rest
mjr 48:058ace2aed1d 5862 // position.
mjr 48:058ace2aed1d 5863 //
mjr 48:058ace2aed1d 5864 class ZBLaunchBall
mjr 48:058ace2aed1d 5865 {
mjr 48:058ace2aed1d 5866 public:
mjr 48:058ace2aed1d 5867 ZBLaunchBall()
mjr 48:058ace2aed1d 5868 {
mjr 48:058ace2aed1d 5869 // start in the default state
mjr 48:058ace2aed1d 5870 lbState = 0;
mjr 53:9b2611964afc 5871 btnState = false;
mjr 48:058ace2aed1d 5872 }
mjr 48:058ace2aed1d 5873
mjr 48:058ace2aed1d 5874 // Update state. This checks the current plunger position and
mjr 48:058ace2aed1d 5875 // the timers to see if the plunger is in a position that simulates
mjr 48:058ace2aed1d 5876 // a Launch Ball button press via the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5877 // Updates the simulated button vector according to the current
mjr 48:058ace2aed1d 5878 // launch ball state. The main loop calls this before each
mjr 48:058ace2aed1d 5879 // joystick update to figure the new simulated button state.
mjr 53:9b2611964afc 5880 void update()
mjr 48:058ace2aed1d 5881 {
mjr 53:9b2611964afc 5882 // If the ZB Launch Ball led wiz output is ON, check for a
mjr 53:9b2611964afc 5883 // plunger firing event
mjr 53:9b2611964afc 5884 if (zbLaunchOn)
mjr 48:058ace2aed1d 5885 {
mjr 53:9b2611964afc 5886 // note the new position
mjr 48:058ace2aed1d 5887 int znew = plungerReader.getPosition();
mjr 53:9b2611964afc 5888
mjr 53:9b2611964afc 5889 // figure the push threshold from the configuration data
mjr 51:57eb311faafa 5890 const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
mjr 53:9b2611964afc 5891
mjr 53:9b2611964afc 5892 // check the state
mjr 48:058ace2aed1d 5893 switch (lbState)
mjr 48:058ace2aed1d 5894 {
mjr 48:058ace2aed1d 5895 case 0:
mjr 53:9b2611964afc 5896 // Default state. If a launch event has been detected on
mjr 53:9b2611964afc 5897 // the plunger, activate a timed pulse and switch to state 1.
mjr 53:9b2611964afc 5898 // If the plunger is pushed forward of the threshold, push
mjr 53:9b2611964afc 5899 // the button.
mjr 53:9b2611964afc 5900 if (plungerReader.isFiring())
mjr 53:9b2611964afc 5901 {
mjr 53:9b2611964afc 5902 // firing event - start a timed Launch button pulse
mjr 53:9b2611964afc 5903 lbTimer.reset();
mjr 53:9b2611964afc 5904 lbTimer.start();
mjr 53:9b2611964afc 5905 setButton(true);
mjr 53:9b2611964afc 5906
mjr 53:9b2611964afc 5907 // switch to state 1
mjr 53:9b2611964afc 5908 lbState = 1;
mjr 53:9b2611964afc 5909 }
mjr 48:058ace2aed1d 5910 else if (znew <= pushThreshold)
mjr 53:9b2611964afc 5911 {
mjr 53:9b2611964afc 5912 // pushed forward without a firing event - hold the
mjr 53:9b2611964afc 5913 // button as long as we're pushed forward
mjr 53:9b2611964afc 5914 setButton(true);
mjr 53:9b2611964afc 5915 }
mjr 53:9b2611964afc 5916 else
mjr 53:9b2611964afc 5917 {
mjr 53:9b2611964afc 5918 // not pushed forward - turn off the Launch button
mjr 53:9b2611964afc 5919 setButton(false);
mjr 53:9b2611964afc 5920 }
mjr 48:058ace2aed1d 5921 break;
mjr 48:058ace2aed1d 5922
mjr 48:058ace2aed1d 5923 case 1:
mjr 53:9b2611964afc 5924 // State 1: Timed Launch button pulse in progress after a
mjr 53:9b2611964afc 5925 // firing event. Wait for the timer to expire.
mjr 53:9b2611964afc 5926 if (lbTimer.read_us() > 200000UL)
mjr 53:9b2611964afc 5927 {
mjr 53:9b2611964afc 5928 // timer expired - turn off the button
mjr 53:9b2611964afc 5929 setButton(false);
mjr 53:9b2611964afc 5930
mjr 53:9b2611964afc 5931 // switch to state 2
mjr 53:9b2611964afc 5932 lbState = 2;
mjr 53:9b2611964afc 5933 }
mjr 48:058ace2aed1d 5934 break;
mjr 48:058ace2aed1d 5935
mjr 48:058ace2aed1d 5936 case 2:
mjr 53:9b2611964afc 5937 // State 2: Timed Launch button pulse done. Wait for the
mjr 53:9b2611964afc 5938 // plunger launch event to end.
mjr 53:9b2611964afc 5939 if (!plungerReader.isFiring())
mjr 53:9b2611964afc 5940 {
mjr 53:9b2611964afc 5941 // firing event done - return to default state
mjr 53:9b2611964afc 5942 lbState = 0;
mjr 53:9b2611964afc 5943 }
mjr 48:058ace2aed1d 5944 break;
mjr 48:058ace2aed1d 5945 }
mjr 53:9b2611964afc 5946 }
mjr 53:9b2611964afc 5947 else
mjr 53:9b2611964afc 5948 {
mjr 53:9b2611964afc 5949 // ZB Launch Ball disabled - turn off the button if it was on
mjr 53:9b2611964afc 5950 setButton(false);
mjr 48:058ace2aed1d 5951
mjr 53:9b2611964afc 5952 // return to the default state
mjr 53:9b2611964afc 5953 lbState = 0;
mjr 48:058ace2aed1d 5954 }
mjr 48:058ace2aed1d 5955 }
mjr 53:9b2611964afc 5956
mjr 53:9b2611964afc 5957 // Set the button state
mjr 53:9b2611964afc 5958 void setButton(bool on)
mjr 53:9b2611964afc 5959 {
mjr 53:9b2611964afc 5960 if (btnState != on)
mjr 53:9b2611964afc 5961 {
mjr 53:9b2611964afc 5962 // remember the new state
mjr 53:9b2611964afc 5963 btnState = on;
mjr 53:9b2611964afc 5964
mjr 53:9b2611964afc 5965 // update the virtual button state
mjr 65:739875521aae 5966 buttonState[zblButtonIndex].virtPress(on);
mjr 53:9b2611964afc 5967 }
mjr 53:9b2611964afc 5968 }
mjr 53:9b2611964afc 5969
mjr 48:058ace2aed1d 5970 private:
mjr 48:058ace2aed1d 5971 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 48:058ace2aed1d 5972 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 48:058ace2aed1d 5973 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 48:058ace2aed1d 5974 // back and releases the plunger, or simply pushes on the plunger from
mjr 48:058ace2aed1d 5975 // the rest position. This allows the plunger to be used in lieu of a
mjr 48:058ace2aed1d 5976 // physical Launch Ball button for tables that don't have plungers.
mjr 48:058ace2aed1d 5977 //
mjr 48:058ace2aed1d 5978 // States:
mjr 48:058ace2aed1d 5979 // 0 = default
mjr 53:9b2611964afc 5980 // 1 = firing (firing event has activated a Launch button pulse)
mjr 53:9b2611964afc 5981 // 2 = firing done (Launch button pulse ended, waiting for plunger
mjr 53:9b2611964afc 5982 // firing event to end)
mjr 53:9b2611964afc 5983 uint8_t lbState;
mjr 48:058ace2aed1d 5984
mjr 53:9b2611964afc 5985 // button state
mjr 53:9b2611964afc 5986 bool btnState;
mjr 48:058ace2aed1d 5987
mjr 48:058ace2aed1d 5988 // Time since last lbState transition. Some of the states are time-
mjr 48:058ace2aed1d 5989 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 48:058ace2aed1d 5990 // we remain in this state for more than a few milliseconds, since
mjr 48:058ace2aed1d 5991 // it indicates that the plunger is being slowly returned to rest
mjr 48:058ace2aed1d 5992 // rather than released. In the "launching" state, we need to release
mjr 48:058ace2aed1d 5993 // the Launch Ball button after a moment, and we need to wait for
mjr 48:058ace2aed1d 5994 // the plunger to come to rest before returning to state 0.
mjr 48:058ace2aed1d 5995 Timer lbTimer;
mjr 48:058ace2aed1d 5996 };
mjr 48:058ace2aed1d 5997
mjr 35:e959ffba78fd 5998 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5999 //
mjr 35:e959ffba78fd 6000 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 6001 //
mjr 54:fd77a6b2f76c 6002 void reboot(USBJoystick &js, bool disconnect = true, long pause_us = 2000000L)
mjr 35:e959ffba78fd 6003 {
mjr 35:e959ffba78fd 6004 // disconnect from USB
mjr 54:fd77a6b2f76c 6005 if (disconnect)
mjr 54:fd77a6b2f76c 6006 js.disconnect();
mjr 35:e959ffba78fd 6007
mjr 35:e959ffba78fd 6008 // wait a few seconds to make sure the host notices the disconnect
mjr 54:fd77a6b2f76c 6009 wait_us(pause_us);
mjr 35:e959ffba78fd 6010
mjr 35:e959ffba78fd 6011 // reset the device
mjr 35:e959ffba78fd 6012 NVIC_SystemReset();
mjr 35:e959ffba78fd 6013 while (true) { }
mjr 35:e959ffba78fd 6014 }
mjr 35:e959ffba78fd 6015
mjr 35:e959ffba78fd 6016 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 6017 //
mjr 35:e959ffba78fd 6018 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 6019 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 6020 //
mjr 35:e959ffba78fd 6021 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 6022 {
mjr 35:e959ffba78fd 6023 int tmp;
mjr 78:1e00b3fa11af 6024 switch (cfg.accel.orientation)
mjr 35:e959ffba78fd 6025 {
mjr 35:e959ffba78fd 6026 case OrientationFront:
mjr 35:e959ffba78fd 6027 tmp = x;
mjr 35:e959ffba78fd 6028 x = y;
mjr 35:e959ffba78fd 6029 y = tmp;
mjr 35:e959ffba78fd 6030 break;
mjr 35:e959ffba78fd 6031
mjr 35:e959ffba78fd 6032 case OrientationLeft:
mjr 35:e959ffba78fd 6033 x = -x;
mjr 35:e959ffba78fd 6034 break;
mjr 35:e959ffba78fd 6035
mjr 35:e959ffba78fd 6036 case OrientationRight:
mjr 35:e959ffba78fd 6037 y = -y;
mjr 35:e959ffba78fd 6038 break;
mjr 35:e959ffba78fd 6039
mjr 35:e959ffba78fd 6040 case OrientationRear:
mjr 35:e959ffba78fd 6041 tmp = -x;
mjr 35:e959ffba78fd 6042 x = -y;
mjr 35:e959ffba78fd 6043 y = tmp;
mjr 35:e959ffba78fd 6044 break;
mjr 35:e959ffba78fd 6045 }
mjr 35:e959ffba78fd 6046 }
mjr 35:e959ffba78fd 6047
mjr 35:e959ffba78fd 6048 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 6049 //
mjr 35:e959ffba78fd 6050 // Calibration button state:
mjr 35:e959ffba78fd 6051 // 0 = not pushed
mjr 35:e959ffba78fd 6052 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 6053 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 6054 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 6055 int calBtnState = 0;
mjr 35:e959ffba78fd 6056
mjr 35:e959ffba78fd 6057 // calibration button debounce timer
mjr 35:e959ffba78fd 6058 Timer calBtnTimer;
mjr 35:e959ffba78fd 6059
mjr 35:e959ffba78fd 6060 // calibration button light state
mjr 35:e959ffba78fd 6061 int calBtnLit = false;
mjr 35:e959ffba78fd 6062
mjr 35:e959ffba78fd 6063
mjr 35:e959ffba78fd 6064 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 6065 //
mjr 40:cc0d9814522b 6066 // Configuration variable get/set message handling
mjr 35:e959ffba78fd 6067 //
mjr 40:cc0d9814522b 6068
mjr 40:cc0d9814522b 6069 // Handle SET messages - write configuration variables from USB message data
mjr 40:cc0d9814522b 6070 #define if_msg_valid(test) if (test)
mjr 53:9b2611964afc 6071 #define v_byte(var, ofs) cfg.var = data[ofs]
mjr 91:ae9be42652bf 6072 #define v_byte_wo(var, ofs) cfg.var = data[ofs]
mjr 53:9b2611964afc 6073 #define v_ui16(var, ofs) cfg.var = wireUI16(data+(ofs))
mjr 77:0b96f6867312 6074 #define v_ui32(var, ofs) cfg.var = wireUI32(data+(ofs))
mjr 53:9b2611964afc 6075 #define v_pin(var, ofs) cfg.var = wirePinName(data[ofs])
mjr 53:9b2611964afc 6076 #define v_byte_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 6077 #define v_ui32_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 6078 #define VAR_MODE_SET 1 // we're in SET mode
mjr 76:7f5912b6340e 6079 #define v_func configVarSet(const uint8_t *data)
mjr 40:cc0d9814522b 6080 #include "cfgVarMsgMap.h"
mjr 35:e959ffba78fd 6081
mjr 40:cc0d9814522b 6082 // redefine everything for the SET messages
mjr 40:cc0d9814522b 6083 #undef if_msg_valid
mjr 40:cc0d9814522b 6084 #undef v_byte
mjr 40:cc0d9814522b 6085 #undef v_ui16
mjr 77:0b96f6867312 6086 #undef v_ui32
mjr 40:cc0d9814522b 6087 #undef v_pin
mjr 53:9b2611964afc 6088 #undef v_byte_ro
mjr 91:ae9be42652bf 6089 #undef v_byte_wo
mjr 74:822a92bc11d2 6090 #undef v_ui32_ro
mjr 74:822a92bc11d2 6091 #undef VAR_MODE_SET
mjr 40:cc0d9814522b 6092 #undef v_func
mjr 38:091e511ce8a0 6093
mjr 91:ae9be42652bf 6094 // Handle GET messages - read variable values and return in USB message data
mjr 40:cc0d9814522b 6095 #define if_msg_valid(test)
mjr 53:9b2611964afc 6096 #define v_byte(var, ofs) data[ofs] = cfg.var
mjr 53:9b2611964afc 6097 #define v_ui16(var, ofs) ui16Wire(data+(ofs), cfg.var)
mjr 77:0b96f6867312 6098 #define v_ui32(var, ofs) ui32Wire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 6099 #define v_pin(var, ofs) pinNameWire(data+(ofs), cfg.var)
mjr 73:4e8ce0b18915 6100 #define v_byte_ro(val, ofs) data[ofs] = (val)
mjr 74:822a92bc11d2 6101 #define v_ui32_ro(val, ofs) ui32Wire(data+(ofs), val);
mjr 74:822a92bc11d2 6102 #define VAR_MODE_SET 0 // we're in GET mode
mjr 91:ae9be42652bf 6103 #define v_byte_wo(var, ofs) // ignore write-only variables in GET mode
mjr 76:7f5912b6340e 6104 #define v_func configVarGet(uint8_t *data)
mjr 40:cc0d9814522b 6105 #include "cfgVarMsgMap.h"
mjr 40:cc0d9814522b 6106
mjr 35:e959ffba78fd 6107
mjr 35:e959ffba78fd 6108 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 6109 //
mjr 101:755f44622abc 6110 // Timer for timestamping input requests
mjr 101:755f44622abc 6111 //
mjr 101:755f44622abc 6112 Timer requestTimestamper;
mjr 101:755f44622abc 6113
mjr 101:755f44622abc 6114 // ---------------------------------------------------------------------------
mjr 101:755f44622abc 6115 //
mjr 35:e959ffba78fd 6116 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 6117 // LedWiz protocol.
mjr 33:d832bcab089e 6118 //
mjr 78:1e00b3fa11af 6119 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js, Accel &accel)
mjr 35:e959ffba78fd 6120 {
mjr 38:091e511ce8a0 6121 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 6122 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 6123 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 6124 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 6125 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 6126 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 6127 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 6128 // So our full protocol is as follows:
mjr 38:091e511ce8a0 6129 //
mjr 38:091e511ce8a0 6130 // first byte =
mjr 74:822a92bc11d2 6131 // 0-48 -> PBA
mjr 74:822a92bc11d2 6132 // 64 -> SBA
mjr 38:091e511ce8a0 6133 // 65 -> private control message; second byte specifies subtype
mjr 74:822a92bc11d2 6134 // 129-132 -> PBA
mjr 38:091e511ce8a0 6135 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 6136 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 6137 // other -> reserved for future use
mjr 38:091e511ce8a0 6138 //
mjr 39:b3815a1c3802 6139 uint8_t *data = lwm.data;
mjr 74:822a92bc11d2 6140 if (data[0] == 64)
mjr 35:e959ffba78fd 6141 {
mjr 74:822a92bc11d2 6142 // 64 = SBA (original LedWiz command to set on/off switches for ports 1-32)
mjr 74:822a92bc11d2 6143 //printf("SBA %02x %02x %02x %02x, speed %02x\r\n",
mjr 38:091e511ce8a0 6144 // data[1], data[2], data[3], data[4], data[5]);
mjr 74:822a92bc11d2 6145 sba_sbx(0, data);
mjr 74:822a92bc11d2 6146
mjr 74:822a92bc11d2 6147 // SBA resets the PBA port group counter
mjr 38:091e511ce8a0 6148 pbaIdx = 0;
mjr 38:091e511ce8a0 6149 }
mjr 38:091e511ce8a0 6150 else if (data[0] == 65)
mjr 38:091e511ce8a0 6151 {
mjr 38:091e511ce8a0 6152 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 6153 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 6154 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 6155 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 6156 // message type.
mjr 39:b3815a1c3802 6157 switch (data[1])
mjr 38:091e511ce8a0 6158 {
mjr 39:b3815a1c3802 6159 case 0:
mjr 39:b3815a1c3802 6160 // No Op
mjr 39:b3815a1c3802 6161 break;
mjr 39:b3815a1c3802 6162
mjr 39:b3815a1c3802 6163 case 1:
mjr 38:091e511ce8a0 6164 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 6165 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 6166 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 6167 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 6168 {
mjr 39:b3815a1c3802 6169
mjr 39:b3815a1c3802 6170 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 6171 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 6172 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 6173
mjr 86:e30a1f60f783 6174 // we'll need a reboot if the LedWiz unit number is changing
mjr 86:e30a1f60f783 6175 bool reboot = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 6176
mjr 39:b3815a1c3802 6177 // set the configuration parameters from the message
mjr 39:b3815a1c3802 6178 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 6179 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 6180
mjr 77:0b96f6867312 6181 // set the flag to do the save
mjr 86:e30a1f60f783 6182 saveConfigToFlash(0, reboot);
mjr 39:b3815a1c3802 6183 }
mjr 39:b3815a1c3802 6184 break;
mjr 38:091e511ce8a0 6185
mjr 39:b3815a1c3802 6186 case 2:
mjr 38:091e511ce8a0 6187 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 6188 // (No parameters)
mjr 38:091e511ce8a0 6189
mjr 38:091e511ce8a0 6190 // enter calibration mode
mjr 38:091e511ce8a0 6191 calBtnState = 3;
mjr 52:8298b2a73eb2 6192 plungerReader.setCalMode(true);
mjr 38:091e511ce8a0 6193 calBtnTimer.reset();
mjr 39:b3815a1c3802 6194 break;
mjr 39:b3815a1c3802 6195
mjr 39:b3815a1c3802 6196 case 3:
mjr 52:8298b2a73eb2 6197 // 3 = plunger sensor status report
mjr 48:058ace2aed1d 6198 // data[2] = flag bits
mjr 53:9b2611964afc 6199 // data[3] = extra exposure time, 100us (.1ms) increments
mjr 52:8298b2a73eb2 6200 reportPlungerStat = true;
mjr 53:9b2611964afc 6201 reportPlungerStatFlags = data[2];
mjr 53:9b2611964afc 6202 reportPlungerStatTime = data[3];
mjr 38:091e511ce8a0 6203
mjr 101:755f44622abc 6204 // set the extra integration time in the sensor
mjr 101:755f44622abc 6205 plungerSensor->setExtraIntegrationTime(reportPlungerStatTime * 100);
mjr 101:755f44622abc 6206
mjr 101:755f44622abc 6207 // make a note of the request timestamp
mjr 101:755f44622abc 6208 tReportPlungerStat = requestTimestamper.read_us();
mjr 101:755f44622abc 6209
mjr 38:091e511ce8a0 6210 // show purple until we finish sending the report
mjr 38:091e511ce8a0 6211 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 6212 break;
mjr 39:b3815a1c3802 6213
mjr 39:b3815a1c3802 6214 case 4:
mjr 38:091e511ce8a0 6215 // 4 = hardware configuration query
mjr 38:091e511ce8a0 6216 // (No parameters)
mjr 38:091e511ce8a0 6217 js.reportConfig(
mjr 38:091e511ce8a0 6218 numOutputs,
mjr 38:091e511ce8a0 6219 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 52:8298b2a73eb2 6220 cfg.plunger.cal.zero, cfg.plunger.cal.max, cfg.plunger.cal.tRelease,
mjr 75:677892300e7a 6221 nvm.valid(), // a config is loaded if the config memory block is valid
mjr 75:677892300e7a 6222 true, // we support sbx/pbx extensions
mjr 78:1e00b3fa11af 6223 true, // we support the new accelerometer settings
mjr 82:4f6209cb5c33 6224 true, // we support the "flash write ok" status bit in joystick reports
mjr 92:f264fbaa1be5 6225 true, // we support the configurable joystick report timing features
mjr 99:8139b0c274f4 6226 true, // chime logic is supported
mjr 79:682ae3171a08 6227 mallocBytesFree()); // remaining memory size
mjr 39:b3815a1c3802 6228 break;
mjr 39:b3815a1c3802 6229
mjr 39:b3815a1c3802 6230 case 5:
mjr 38:091e511ce8a0 6231 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 6232 allOutputsOff();
mjr 39:b3815a1c3802 6233 break;
mjr 39:b3815a1c3802 6234
mjr 39:b3815a1c3802 6235 case 6:
mjr 85:3c28aee81cde 6236 // 6 = Save configuration to flash. Optionally reboot after the
mjr 85:3c28aee81cde 6237 // delay time in seconds given in data[2].
mjr 85:3c28aee81cde 6238 //
mjr 85:3c28aee81cde 6239 // data[2] = delay time in seconds
mjr 85:3c28aee81cde 6240 // data[3] = flags:
mjr 85:3c28aee81cde 6241 // 0x01 -> do not reboot
mjr 86:e30a1f60f783 6242 saveConfigToFlash(data[2], !(data[3] & 0x01));
mjr 39:b3815a1c3802 6243 break;
mjr 40:cc0d9814522b 6244
mjr 40:cc0d9814522b 6245 case 7:
mjr 40:cc0d9814522b 6246 // 7 = Device ID report
mjr 53:9b2611964afc 6247 // data[2] = ID index: 1=CPU ID, 2=OpenSDA TUID
mjr 53:9b2611964afc 6248 js.reportID(data[2]);
mjr 40:cc0d9814522b 6249 break;
mjr 40:cc0d9814522b 6250
mjr 40:cc0d9814522b 6251 case 8:
mjr 40:cc0d9814522b 6252 // 8 = Engage/disengage night mode.
mjr 40:cc0d9814522b 6253 // data[2] = 1 to engage, 0 to disengage
mjr 40:cc0d9814522b 6254 setNightMode(data[2]);
mjr 40:cc0d9814522b 6255 break;
mjr 52:8298b2a73eb2 6256
mjr 52:8298b2a73eb2 6257 case 9:
mjr 52:8298b2a73eb2 6258 // 9 = Config variable query.
mjr 52:8298b2a73eb2 6259 // data[2] = config var ID
mjr 52:8298b2a73eb2 6260 // data[3] = array index (for array vars: button assignments, output ports)
mjr 52:8298b2a73eb2 6261 {
mjr 53:9b2611964afc 6262 // set up the reply buffer with the variable ID data, and zero out
mjr 53:9b2611964afc 6263 // the rest of the buffer
mjr 52:8298b2a73eb2 6264 uint8_t reply[8];
mjr 52:8298b2a73eb2 6265 reply[1] = data[2];
mjr 52:8298b2a73eb2 6266 reply[2] = data[3];
mjr 53:9b2611964afc 6267 memset(reply+3, 0, sizeof(reply)-3);
mjr 52:8298b2a73eb2 6268
mjr 52:8298b2a73eb2 6269 // query the value
mjr 52:8298b2a73eb2 6270 configVarGet(reply);
mjr 52:8298b2a73eb2 6271
mjr 52:8298b2a73eb2 6272 // send the reply
mjr 52:8298b2a73eb2 6273 js.reportConfigVar(reply + 1);
mjr 52:8298b2a73eb2 6274 }
mjr 52:8298b2a73eb2 6275 break;
mjr 53:9b2611964afc 6276
mjr 53:9b2611964afc 6277 case 10:
mjr 53:9b2611964afc 6278 // 10 = Build ID query.
mjr 53:9b2611964afc 6279 js.reportBuildInfo(getBuildID());
mjr 53:9b2611964afc 6280 break;
mjr 73:4e8ce0b18915 6281
mjr 73:4e8ce0b18915 6282 case 11:
mjr 73:4e8ce0b18915 6283 // 11 = TV ON relay control.
mjr 73:4e8ce0b18915 6284 // data[2] = operation:
mjr 73:4e8ce0b18915 6285 // 0 = turn relay off
mjr 73:4e8ce0b18915 6286 // 1 = turn relay on
mjr 73:4e8ce0b18915 6287 // 2 = pulse relay (as though the power-on timer fired)
mjr 73:4e8ce0b18915 6288 TVRelay(data[2]);
mjr 73:4e8ce0b18915 6289 break;
mjr 73:4e8ce0b18915 6290
mjr 73:4e8ce0b18915 6291 case 12:
mjr 77:0b96f6867312 6292 // 12 = Learn IR code. This enters IR learning mode. While
mjr 77:0b96f6867312 6293 // in learning mode, we report raw IR signals and the first IR
mjr 77:0b96f6867312 6294 // command decoded through the special IR report format. IR
mjr 77:0b96f6867312 6295 // learning mode automatically ends after a timeout expires if
mjr 77:0b96f6867312 6296 // no command can be decoded within the time limit.
mjr 77:0b96f6867312 6297
mjr 77:0b96f6867312 6298 // enter IR learning mode
mjr 77:0b96f6867312 6299 IRLearningMode = 1;
mjr 77:0b96f6867312 6300
mjr 77:0b96f6867312 6301 // cancel any regular IR input in progress
mjr 77:0b96f6867312 6302 IRCommandIn = 0;
mjr 77:0b96f6867312 6303
mjr 77:0b96f6867312 6304 // reset and start the learning mode timeout timer
mjr 77:0b96f6867312 6305 IRTimer.reset();
mjr 73:4e8ce0b18915 6306 break;
mjr 73:4e8ce0b18915 6307
mjr 73:4e8ce0b18915 6308 case 13:
mjr 73:4e8ce0b18915 6309 // 13 = Send button status report
mjr 73:4e8ce0b18915 6310 reportButtonStatus(js);
mjr 73:4e8ce0b18915 6311 break;
mjr 78:1e00b3fa11af 6312
mjr 78:1e00b3fa11af 6313 case 14:
mjr 78:1e00b3fa11af 6314 // 14 = manually center the accelerometer
mjr 78:1e00b3fa11af 6315 accel.manualCenterRequest();
mjr 78:1e00b3fa11af 6316 break;
mjr 78:1e00b3fa11af 6317
mjr 78:1e00b3fa11af 6318 case 15:
mjr 78:1e00b3fa11af 6319 // 15 = set up ad hoc IR command, part 1. Mark the command
mjr 78:1e00b3fa11af 6320 // as not ready, and save the partial data from the message.
mjr 78:1e00b3fa11af 6321 IRAdHocCmd.ready = 0;
mjr 78:1e00b3fa11af 6322 IRAdHocCmd.protocol = data[2];
mjr 78:1e00b3fa11af 6323 IRAdHocCmd.dittos = (data[3] & IRFlagDittos) != 0;
mjr 78:1e00b3fa11af 6324 IRAdHocCmd.code = wireUI32(&data[4]);
mjr 78:1e00b3fa11af 6325 break;
mjr 78:1e00b3fa11af 6326
mjr 78:1e00b3fa11af 6327 case 16:
mjr 78:1e00b3fa11af 6328 // 16 = send ad hoc IR command, part 2. Fill in the rest
mjr 78:1e00b3fa11af 6329 // of the data from the message and mark the command as
mjr 78:1e00b3fa11af 6330 // ready. The IR polling routine will send this as soon
mjr 78:1e00b3fa11af 6331 // as the IR transmitter is free.
mjr 78:1e00b3fa11af 6332 IRAdHocCmd.code |= (uint64_t(wireUI32(&data[2])) << 32);
mjr 78:1e00b3fa11af 6333 IRAdHocCmd.ready = 1;
mjr 78:1e00b3fa11af 6334 break;
mjr 88:98bce687e6c0 6335
mjr 88:98bce687e6c0 6336 case 17:
mjr 88:98bce687e6c0 6337 // 17 = send pre-programmed IR command. This works just like
mjr 88:98bce687e6c0 6338 // sending an ad hoc command above, but we get the command data
mjr 88:98bce687e6c0 6339 // from an IR slot in the config rather than from the client.
mjr 88:98bce687e6c0 6340 // First make sure we have a valid slot number.
mjr 88:98bce687e6c0 6341 if (data[2] >= 1 && data[2] <= MAX_IR_CODES)
mjr 88:98bce687e6c0 6342 {
mjr 88:98bce687e6c0 6343 // get the IR command slot in the config
mjr 88:98bce687e6c0 6344 IRCommandCfg &cmd = cfg.IRCommand[data[2] - 1];
mjr 88:98bce687e6c0 6345
mjr 88:98bce687e6c0 6346 // copy the IR command data from the config
mjr 88:98bce687e6c0 6347 IRAdHocCmd.protocol = cmd.protocol;
mjr 88:98bce687e6c0 6348 IRAdHocCmd.dittos = (cmd.flags & IRFlagDittos) != 0;
mjr 88:98bce687e6c0 6349 IRAdHocCmd.code = (uint64_t(cmd.code.hi) << 32) | cmd.code.lo;
mjr 88:98bce687e6c0 6350
mjr 88:98bce687e6c0 6351 // mark the command as ready - this will trigger the polling
mjr 88:98bce687e6c0 6352 // routine to send the command as soon as the transmitter
mjr 88:98bce687e6c0 6353 // is free
mjr 88:98bce687e6c0 6354 IRAdHocCmd.ready = 1;
mjr 88:98bce687e6c0 6355 }
mjr 88:98bce687e6c0 6356 break;
mjr 38:091e511ce8a0 6357 }
mjr 38:091e511ce8a0 6358 }
mjr 38:091e511ce8a0 6359 else if (data[0] == 66)
mjr 38:091e511ce8a0 6360 {
mjr 38:091e511ce8a0 6361 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 6362 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 6363 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 6364 // in a variable-dependent format.
mjr 40:cc0d9814522b 6365 configVarSet(data);
mjr 86:e30a1f60f783 6366
mjr 87:8d35c74403af 6367 // notify the plunger, so that it can update relevant variables
mjr 87:8d35c74403af 6368 // dynamically
mjr 87:8d35c74403af 6369 plungerSensor->onConfigChange(data[1], cfg);
mjr 38:091e511ce8a0 6370 }
mjr 74:822a92bc11d2 6371 else if (data[0] == 67)
mjr 74:822a92bc11d2 6372 {
mjr 74:822a92bc11d2 6373 // SBX - extended SBA message. This is the same as SBA, except
mjr 74:822a92bc11d2 6374 // that the 7th byte selects a group of 32 ports, to allow access
mjr 74:822a92bc11d2 6375 // to ports beyond the first 32.
mjr 74:822a92bc11d2 6376 sba_sbx(data[6], data);
mjr 74:822a92bc11d2 6377 }
mjr 74:822a92bc11d2 6378 else if (data[0] == 68)
mjr 74:822a92bc11d2 6379 {
mjr 74:822a92bc11d2 6380 // PBX - extended PBA message. This is similar to PBA, but
mjr 74:822a92bc11d2 6381 // allows access to more than the first 32 ports by encoding
mjr 74:822a92bc11d2 6382 // a port group byte that selects a block of 8 ports.
mjr 74:822a92bc11d2 6383
mjr 74:822a92bc11d2 6384 // get the port group - the first port is 8*group
mjr 74:822a92bc11d2 6385 int portGroup = data[1];
mjr 74:822a92bc11d2 6386
mjr 74:822a92bc11d2 6387 // unpack the brightness values
mjr 74:822a92bc11d2 6388 uint32_t tmp1 = data[2] | (data[3]<<8) | (data[4]<<16);
mjr 74:822a92bc11d2 6389 uint32_t tmp2 = data[5] | (data[6]<<8) | (data[7]<<16);
mjr 74:822a92bc11d2 6390 uint8_t bri[8] = {
mjr 74:822a92bc11d2 6391 tmp1 & 0x3F, (tmp1>>6) & 0x3F, (tmp1>>12) & 0x3F, (tmp1>>18) & 0x3F,
mjr 74:822a92bc11d2 6392 tmp2 & 0x3F, (tmp2>>6) & 0x3F, (tmp2>>12) & 0x3F, (tmp2>>18) & 0x3F
mjr 74:822a92bc11d2 6393 };
mjr 74:822a92bc11d2 6394
mjr 74:822a92bc11d2 6395 // map the flash levels: 60->129, 61->130, 62->131, 63->132
mjr 74:822a92bc11d2 6396 for (int i = 0 ; i < 8 ; ++i)
mjr 74:822a92bc11d2 6397 {
mjr 74:822a92bc11d2 6398 if (bri[i] >= 60)
mjr 74:822a92bc11d2 6399 bri[i] += 129-60;
mjr 74:822a92bc11d2 6400 }
mjr 74:822a92bc11d2 6401
mjr 74:822a92bc11d2 6402 // Carry out the PBA
mjr 74:822a92bc11d2 6403 pba_pbx(portGroup*8, bri);
mjr 74:822a92bc11d2 6404 }
mjr 38:091e511ce8a0 6405 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 6406 {
mjr 38:091e511ce8a0 6407 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 6408 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 6409 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 6410 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 6411 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 6412 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 6413 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 6414 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 6415 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 6416 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 6417 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 6418 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 6419 //
mjr 38:091e511ce8a0 6420 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 6421 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 6422 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 6423 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 6424 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 6425 // address those ports anyway.
mjr 63:5cd1a5f3a41b 6426
mjr 63:5cd1a5f3a41b 6427 // figure the block of 7 ports covered in the message
mjr 38:091e511ce8a0 6428 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 6429 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 63:5cd1a5f3a41b 6430
mjr 63:5cd1a5f3a41b 6431 // update each port
mjr 38:091e511ce8a0 6432 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 6433 {
mjr 38:091e511ce8a0 6434 // set the brightness level for the output
mjr 40:cc0d9814522b 6435 uint8_t b = data[i-i0+1];
mjr 38:091e511ce8a0 6436 outLevel[i] = b;
mjr 38:091e511ce8a0 6437
mjr 74:822a92bc11d2 6438 // set the port's LedWiz state to the nearest equivalent, so
mjr 74:822a92bc11d2 6439 // that it maintains its current setting if we switch back to
mjr 74:822a92bc11d2 6440 // LedWiz mode on a future update
mjr 76:7f5912b6340e 6441 if (b != 0)
mjr 76:7f5912b6340e 6442 {
mjr 76:7f5912b6340e 6443 // Non-zero brightness - set the SBA switch on, and set the
mjr 76:7f5912b6340e 6444 // PBA brightness to the DOF brightness rescaled to the 1..48
mjr 76:7f5912b6340e 6445 // LedWiz range. If the port is subsequently addressed by an
mjr 76:7f5912b6340e 6446 // LedWiz command, this will carry the current DOF setting
mjr 76:7f5912b6340e 6447 // forward unchanged.
mjr 76:7f5912b6340e 6448 wizOn[i] = 1;
mjr 76:7f5912b6340e 6449 wizVal[i] = dof_to_lw[b];
mjr 76:7f5912b6340e 6450 }
mjr 76:7f5912b6340e 6451 else
mjr 76:7f5912b6340e 6452 {
mjr 76:7f5912b6340e 6453 // Zero brightness. Set the SBA switch off, and leave the
mjr 76:7f5912b6340e 6454 // PBA brightness the same as it was.
mjr 76:7f5912b6340e 6455 wizOn[i] = 0;
mjr 76:7f5912b6340e 6456 }
mjr 74:822a92bc11d2 6457
mjr 38:091e511ce8a0 6458 // set the output
mjr 40:cc0d9814522b 6459 lwPin[i]->set(b);
mjr 38:091e511ce8a0 6460 }
mjr 38:091e511ce8a0 6461
mjr 38:091e511ce8a0 6462 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 6463 if (hc595 != 0)
mjr 38:091e511ce8a0 6464 hc595->update();
mjr 38:091e511ce8a0 6465 }
mjr 38:091e511ce8a0 6466 else
mjr 38:091e511ce8a0 6467 {
mjr 74:822a92bc11d2 6468 // Everything else is an LedWiz PBA message. This is a full
mjr 74:822a92bc11d2 6469 // "profile" dump from the host for one bank of 8 outputs. Each
mjr 74:822a92bc11d2 6470 // byte sets one output in the current bank. The current bank
mjr 74:822a92bc11d2 6471 // is implied; the bank starts at 0 and is reset to 0 by any SBA
mjr 74:822a92bc11d2 6472 // message, and is incremented to the next bank by each PBA. Our
mjr 74:822a92bc11d2 6473 // variable pbaIdx keeps track of the current bank. There's no
mjr 74:822a92bc11d2 6474 // direct way for the host to select the bank; it just has to count
mjr 74:822a92bc11d2 6475 // on us staying in sync. In practice, clients always send the
mjr 74:822a92bc11d2 6476 // full set of 4 PBA messages in a row to set all 32 outputs.
mjr 38:091e511ce8a0 6477 //
mjr 38:091e511ce8a0 6478 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 6479 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 6480 // wizVal[] entry for each output, and that takes precedence
mjr 63:5cd1a5f3a41b 6481 // over the extended protocol settings when we're in LedWiz
mjr 63:5cd1a5f3a41b 6482 // protocol mode.
mjr 38:091e511ce8a0 6483 //
mjr 38:091e511ce8a0 6484 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 6485 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 6486
mjr 74:822a92bc11d2 6487 // carry out the PBA
mjr 74:822a92bc11d2 6488 pba_pbx(pbaIdx, data);
mjr 74:822a92bc11d2 6489
mjr 74:822a92bc11d2 6490 // update the PBX index state for the next message
mjr 74:822a92bc11d2 6491 pbaIdx = (pbaIdx + 8) % 32;
mjr 38:091e511ce8a0 6492 }
mjr 38:091e511ce8a0 6493 }
mjr 35:e959ffba78fd 6494
mjr 38:091e511ce8a0 6495 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 6496 //
mjr 5:a70c0bce770d 6497 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 6498 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 6499 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 6500 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 6501 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 6502 // port outputs.
mjr 5:a70c0bce770d 6503 //
mjr 0:5acbbe3f4cf4 6504 int main(void)
mjr 0:5acbbe3f4cf4 6505 {
mjr 60:f38da020aa13 6506 // say hello to the debug console, in case it's connected
mjr 39:b3815a1c3802 6507 printf("\r\nPinscape Controller starting\r\n");
mjr 94:0476b3e2b996 6508
mjr 98:4df3c0f7e707 6509 // Set the default PWM period to 0.5ms = 2 kHz. This will be used
mjr 98:4df3c0f7e707 6510 // for PWM channels on PWM units whose periods aren't changed
mjr 98:4df3c0f7e707 6511 // explicitly, so it'll apply to LW outputs assigned to GPIO pins.
mjr 98:4df3c0f7e707 6512 // The KL25Z only allows the period to be set at the TPM unit
mjr 94:0476b3e2b996 6513 // level, not per channel, so all channels on a given unit will
mjr 94:0476b3e2b996 6514 // necessarily use the same frequency. We (currently) have two
mjr 94:0476b3e2b996 6515 // subsystems that need specific PWM frequencies: TLC5940NT (which
mjr 94:0476b3e2b996 6516 // uses PWM to generate the grayscale clock signal) and IR remote
mjr 94:0476b3e2b996 6517 // (which uses PWM to generate the IR carrier signal). Since
mjr 94:0476b3e2b996 6518 // those require specific PWM frequencies, it's important to assign
mjr 94:0476b3e2b996 6519 // those to separate TPM units if both are in use simultaneously;
mjr 94:0476b3e2b996 6520 // the Config Tool includes checks to ensure that will happen when
mjr 94:0476b3e2b996 6521 // setting a config interactively. In addition, for the greatest
mjr 94:0476b3e2b996 6522 // flexibility, we take care NOT to assign explicit PWM frequencies
mjr 94:0476b3e2b996 6523 // to pins that don't require special frequences. That way, if a
mjr 94:0476b3e2b996 6524 // pin that doesn't need anything special happens to be sharing a
mjr 94:0476b3e2b996 6525 // TPM unit with a pin that does require a specific frequency, the
mjr 94:0476b3e2b996 6526 // two will co-exist peacefully on the TPM.
mjr 94:0476b3e2b996 6527 //
mjr 94:0476b3e2b996 6528 // We set this default first, before we create any PWM GPIOs, so
mjr 94:0476b3e2b996 6529 // that it will apply to all channels by default but won't override
mjr 94:0476b3e2b996 6530 // any channels that need specific frequences. Currently, the only
mjr 94:0476b3e2b996 6531 // frequency-agnostic PWM user is the LW outputs, so we can choose
mjr 94:0476b3e2b996 6532 // the default to be suitable for those. This is chosen to minimize
mjr 94:0476b3e2b996 6533 // flicker on attached LEDs.
mjr 94:0476b3e2b996 6534 NewPwmUnit::defaultPeriod = 0.0005f;
mjr 82:4f6209cb5c33 6535
mjr 76:7f5912b6340e 6536 // clear the I2C connection
mjr 35:e959ffba78fd 6537 clear_i2c();
mjr 82:4f6209cb5c33 6538
mjr 82:4f6209cb5c33 6539 // Elevate GPIO pin interrupt priorities, so that they can preempt
mjr 82:4f6209cb5c33 6540 // other interrupts. This is important for some external peripherals,
mjr 82:4f6209cb5c33 6541 // particularly the quadrature plunger sensors, which can generate
mjr 82:4f6209cb5c33 6542 // high-speed interrupts that need to be serviced quickly to keep
mjr 82:4f6209cb5c33 6543 // proper count of the quadrature position.
mjr 82:4f6209cb5c33 6544 FastInterruptIn::elevatePriority();
mjr 38:091e511ce8a0 6545
mjr 76:7f5912b6340e 6546 // Load the saved configuration. There are two sources of the
mjr 76:7f5912b6340e 6547 // configuration data:
mjr 76:7f5912b6340e 6548 //
mjr 76:7f5912b6340e 6549 // - Look for an NVM (flash non-volatile memory) configuration.
mjr 76:7f5912b6340e 6550 // If this is valid, we'll load it. The NVM is config data that can
mjr 76:7f5912b6340e 6551 // be updated dynamically by the host via USB commands and then stored
mjr 76:7f5912b6340e 6552 // in the flash by the firmware itself. If this exists, it supersedes
mjr 76:7f5912b6340e 6553 // any of the other settings stores. The Windows config tool uses this
mjr 76:7f5912b6340e 6554 // to store user settings updates.
mjr 76:7f5912b6340e 6555 //
mjr 76:7f5912b6340e 6556 // - If there's no NVM, we'll load the factory defaults, then we'll
mjr 76:7f5912b6340e 6557 // load any settings stored in the host-loaded configuration. The
mjr 76:7f5912b6340e 6558 // host can patch a set of configuration variable settings into the
mjr 76:7f5912b6340e 6559 // .bin file when loading new firmware, in the host-loaded config
mjr 76:7f5912b6340e 6560 // area that we reserve for this purpose. This allows the host to
mjr 76:7f5912b6340e 6561 // restore a configuration at the same time it installs firmware,
mjr 76:7f5912b6340e 6562 // without a separate download of the config data.
mjr 76:7f5912b6340e 6563 //
mjr 76:7f5912b6340e 6564 // The NVM supersedes the host-loaded config, since it can be updated
mjr 76:7f5912b6340e 6565 // between firmware updated and is thus presumably more recent if it's
mjr 76:7f5912b6340e 6566 // present. (Note that the NVM and host-loaded config are both in
mjr 76:7f5912b6340e 6567 // flash, so in principle we could just have a single NVM store that
mjr 76:7f5912b6340e 6568 // the host patches. The only reason we don't is that the NVM store
mjr 76:7f5912b6340e 6569 // is an image of our in-memory config structure, which is a native C
mjr 76:7f5912b6340e 6570 // struct, and we don't want the host to have to know the details of
mjr 76:7f5912b6340e 6571 // its byte layout, for obvious reasons. The host-loaded config, in
mjr 76:7f5912b6340e 6572 // contrast, uses the wire protocol format, which has a well-defined
mjr 76:7f5912b6340e 6573 // byte layout that's independent of the firmware version or the
mjr 76:7f5912b6340e 6574 // details of how the C compiler arranges the struct memory.)
mjr 76:7f5912b6340e 6575 if (!loadConfigFromFlash())
mjr 76:7f5912b6340e 6576 loadHostLoadedConfig();
mjr 35:e959ffba78fd 6577
mjr 38:091e511ce8a0 6578 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 6579 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 6580
mjr 33:d832bcab089e 6581 // we're not connected/awake yet
mjr 33:d832bcab089e 6582 bool connected = false;
mjr 40:cc0d9814522b 6583 Timer connectChangeTimer;
mjr 33:d832bcab089e 6584
mjr 35:e959ffba78fd 6585 // create the plunger sensor interface
mjr 35:e959ffba78fd 6586 createPlunger();
mjr 76:7f5912b6340e 6587
mjr 76:7f5912b6340e 6588 // update the plunger reader's cached calibration data
mjr 76:7f5912b6340e 6589 plungerReader.onUpdateCal();
mjr 33:d832bcab089e 6590
mjr 60:f38da020aa13 6591 // set up the TLC5940 interface, if these chips are present
mjr 35:e959ffba78fd 6592 init_tlc5940(cfg);
mjr 34:6b981a2afab7 6593
mjr 87:8d35c74403af 6594 // initialize the TLC5916 interface, if these chips are present
mjr 87:8d35c74403af 6595 init_tlc59116(cfg);
mjr 87:8d35c74403af 6596
mjr 60:f38da020aa13 6597 // set up 74HC595 interface, if these chips are present
mjr 35:e959ffba78fd 6598 init_hc595(cfg);
mjr 6:cc35eb643e8f 6599
mjr 54:fd77a6b2f76c 6600 // Initialize the LedWiz ports. Note that the ordering here is important:
mjr 54:fd77a6b2f76c 6601 // this has to come after we create the TLC5940 and 74HC595 object instances
mjr 54:fd77a6b2f76c 6602 // (which we just did above), since we need to access those objects to set
mjr 54:fd77a6b2f76c 6603 // up ports assigned to the respective chips.
mjr 35:e959ffba78fd 6604 initLwOut(cfg);
mjr 48:058ace2aed1d 6605
mjr 60:f38da020aa13 6606 // start the TLC5940 refresh cycle clock
mjr 35:e959ffba78fd 6607 if (tlc5940 != 0)
mjr 35:e959ffba78fd 6608 tlc5940->start();
mjr 87:8d35c74403af 6609
mjr 77:0b96f6867312 6610 // Assume that nothing uses keyboard keys. We'll check for keyboard
mjr 77:0b96f6867312 6611 // usage when initializing the various subsystems that can send keys
mjr 77:0b96f6867312 6612 // (buttons, IR). If we find anything that does, we'll create the
mjr 77:0b96f6867312 6613 // USB keyboard interface.
mjr 77:0b96f6867312 6614 bool kbKeys = false;
mjr 77:0b96f6867312 6615
mjr 77:0b96f6867312 6616 // set up the IR remote control emitter & receiver, if present
mjr 77:0b96f6867312 6617 init_IR(cfg, kbKeys);
mjr 77:0b96f6867312 6618
mjr 77:0b96f6867312 6619 // start the power status time, if applicable
mjr 77:0b96f6867312 6620 startPowerStatusTimer(cfg);
mjr 48:058ace2aed1d 6621
mjr 35:e959ffba78fd 6622 // initialize the button input ports
mjr 35:e959ffba78fd 6623 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 6624
mjr 60:f38da020aa13 6625 // Create the joystick USB client. Note that the USB vendor/product ID
mjr 60:f38da020aa13 6626 // information comes from the saved configuration. Also note that we have
mjr 60:f38da020aa13 6627 // to wait until after initializing the input buttons (which we just did
mjr 60:f38da020aa13 6628 // above) to set up the interface, since the button setup will determine
mjr 60:f38da020aa13 6629 // whether or not we need to present a USB keyboard interface in addition
mjr 60:f38da020aa13 6630 // to the joystick interface.
mjr 51:57eb311faafa 6631 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false,
mjr 90:aa4e571da8e8 6632 cfg.joystickEnabled, cfg.joystickAxisFormat, kbKeys);
mjr 51:57eb311faafa 6633
mjr 101:755f44622abc 6634 // start the request timestamp timer
mjr 101:755f44622abc 6635 requestTimestamper.start();
mjr 101:755f44622abc 6636
mjr 60:f38da020aa13 6637 // Wait for the USB connection to start up. Show a distinctive diagnostic
mjr 60:f38da020aa13 6638 // flash pattern while waiting.
mjr 70:9f58735a1732 6639 Timer connTimeoutTimer, connFlashTimer;
mjr 70:9f58735a1732 6640 connTimeoutTimer.start();
mjr 70:9f58735a1732 6641 connFlashTimer.start();
mjr 51:57eb311faafa 6642 while (!js.configured())
mjr 51:57eb311faafa 6643 {
mjr 51:57eb311faafa 6644 // show one short yellow flash at 2-second intervals
mjr 70:9f58735a1732 6645 if (connFlashTimer.read_us() > 2000000)
mjr 51:57eb311faafa 6646 {
mjr 51:57eb311faafa 6647 // short yellow flash
mjr 51:57eb311faafa 6648 diagLED(1, 1, 0);
mjr 54:fd77a6b2f76c 6649 wait_us(50000);
mjr 51:57eb311faafa 6650 diagLED(0, 0, 0);
mjr 51:57eb311faafa 6651
mjr 51:57eb311faafa 6652 // reset the flash timer
mjr 70:9f58735a1732 6653 connFlashTimer.reset();
mjr 51:57eb311faafa 6654 }
mjr 70:9f58735a1732 6655
mjr 77:0b96f6867312 6656 // If we've been disconnected for more than the reboot timeout,
mjr 77:0b96f6867312 6657 // reboot. Some PCs won't reconnect if we were left plugged in
mjr 77:0b96f6867312 6658 // during a power cycle on the PC, but fortunately a reboot on
mjr 77:0b96f6867312 6659 // the KL25Z will make the host notice us and trigger a reconnect.
mjr 86:e30a1f60f783 6660 // Don't do this if we're in a non-recoverable PSU2 power state.
mjr 70:9f58735a1732 6661 if (cfg.disconnectRebootTimeout != 0
mjr 86:e30a1f60f783 6662 && connTimeoutTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 6663 && powerStatusAllowsReboot())
mjr 70:9f58735a1732 6664 reboot(js, false, 0);
mjr 77:0b96f6867312 6665
mjr 77:0b96f6867312 6666 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6667 powerStatusUpdate(cfg);
mjr 51:57eb311faafa 6668 }
mjr 60:f38da020aa13 6669
mjr 60:f38da020aa13 6670 // we're now connected to the host
mjr 54:fd77a6b2f76c 6671 connected = true;
mjr 40:cc0d9814522b 6672
mjr 92:f264fbaa1be5 6673 // Set up a timer for keeping track of how long it's been since we
mjr 92:f264fbaa1be5 6674 // sent the last joystick report. We use this to determine when it's
mjr 92:f264fbaa1be5 6675 // time to send the next joystick report.
mjr 92:f264fbaa1be5 6676 //
mjr 92:f264fbaa1be5 6677 // We have to use a timer for two reasons. The first is that our main
mjr 92:f264fbaa1be5 6678 // loop runs too fast (about .25ms to 2.5ms per loop, depending on the
mjr 92:f264fbaa1be5 6679 // type of plunger sensor attached and other factors) for us to send
mjr 92:f264fbaa1be5 6680 // joystick reports on every iteration. We *could*, but the PC couldn't
mjr 92:f264fbaa1be5 6681 // digest them at that pace. So we need to slow down the reports to a
mjr 92:f264fbaa1be5 6682 // reasonable pace. The second is that VP has some complicated timing
mjr 92:f264fbaa1be5 6683 // issues of its own, so we not only need to slow down the reports from
mjr 92:f264fbaa1be5 6684 // our "natural" pace, but also time them to sync up with VP's input
mjr 92:f264fbaa1be5 6685 // sampling rate as best we can.
mjr 38:091e511ce8a0 6686 Timer jsReportTimer;
mjr 38:091e511ce8a0 6687 jsReportTimer.start();
mjr 38:091e511ce8a0 6688
mjr 92:f264fbaa1be5 6689 // Accelerometer sample "stutter" counter. Each time we send a joystick
mjr 92:f264fbaa1be5 6690 // report, we increment this counter, and check to see if it has reached
mjr 92:f264fbaa1be5 6691 // the threshold set in the configuration. If so, we take a new
mjr 92:f264fbaa1be5 6692 // accelerometer sample and send it with the new joystick report. It
mjr 92:f264fbaa1be5 6693 // not, we don't take a new sample, but simply repeat the last sample.
mjr 92:f264fbaa1be5 6694 //
mjr 92:f264fbaa1be5 6695 // This lets us send joystick reports more frequently than accelerometer
mjr 92:f264fbaa1be5 6696 // samples. The point is to let us slow down accelerometer reports to
mjr 92:f264fbaa1be5 6697 // a pace that matches VP's input sampling frequency, while still sending
mjr 92:f264fbaa1be5 6698 // joystick button updates more frequently, so that other programs that
mjr 92:f264fbaa1be5 6699 // can read input faster will see button changes with less latency.
mjr 92:f264fbaa1be5 6700 int jsAccelStutterCounter = 0;
mjr 92:f264fbaa1be5 6701
mjr 92:f264fbaa1be5 6702 // Last accelerometer report, in joystick units. We normally report the
mjr 92:f264fbaa1be5 6703 // acceleromter reading via the joystick X and Y axes, per the VP
mjr 92:f264fbaa1be5 6704 // convention. We can alternatively report in the RX and RY axes; this
mjr 92:f264fbaa1be5 6705 // can be set in the configuration.
mjr 92:f264fbaa1be5 6706 int x = 0, y = 0;
mjr 92:f264fbaa1be5 6707
mjr 60:f38da020aa13 6708 // Time since we successfully sent a USB report. This is a hacky
mjr 60:f38da020aa13 6709 // workaround to deal with any remaining sporadic problems in the USB
mjr 60:f38da020aa13 6710 // stack. I've been trying to bulletproof the USB code over time to
mjr 60:f38da020aa13 6711 // remove all such problems at their source, but it seems unlikely that
mjr 60:f38da020aa13 6712 // we'll ever get them all. Thus this hack. The idea here is that if
mjr 60:f38da020aa13 6713 // we go too long without successfully sending a USB report, we'll
mjr 60:f38da020aa13 6714 // assume that the connection is broken (and the KL25Z USB hardware
mjr 60:f38da020aa13 6715 // hasn't noticed this), and we'll try taking measures to recover.
mjr 38:091e511ce8a0 6716 Timer jsOKTimer;
mjr 38:091e511ce8a0 6717 jsOKTimer.start();
mjr 35:e959ffba78fd 6718
mjr 55:4db125cd11a0 6719 // Initialize the calibration button and lamp, if enabled. To be enabled,
mjr 55:4db125cd11a0 6720 // the pin has to be assigned to something other than NC (0xFF), AND the
mjr 55:4db125cd11a0 6721 // corresponding feature enable flag has to be set.
mjr 55:4db125cd11a0 6722 DigitalIn *calBtn = 0;
mjr 55:4db125cd11a0 6723 DigitalOut *calBtnLed = 0;
mjr 55:4db125cd11a0 6724
mjr 55:4db125cd11a0 6725 // calibration button input - feature flag 0x01
mjr 55:4db125cd11a0 6726 if ((cfg.plunger.cal.features & 0x01) && cfg.plunger.cal.btn != 0xFF)
mjr 55:4db125cd11a0 6727 calBtn = new DigitalIn(wirePinName(cfg.plunger.cal.btn));
mjr 55:4db125cd11a0 6728
mjr 55:4db125cd11a0 6729 // calibration button indicator lamp output - feature flag 0x02
mjr 55:4db125cd11a0 6730 if ((cfg.plunger.cal.features & 0x02) && cfg.plunger.cal.led != 0xFF)
mjr 55:4db125cd11a0 6731 calBtnLed = new DigitalOut(wirePinName(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 6732
mjr 35:e959ffba78fd 6733 // initialize the calibration button
mjr 1:d913e0afb2ac 6734 calBtnTimer.start();
mjr 35:e959ffba78fd 6735 calBtnState = 0;
mjr 1:d913e0afb2ac 6736
mjr 1:d913e0afb2ac 6737 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 6738 Timer hbTimer;
mjr 1:d913e0afb2ac 6739 hbTimer.start();
mjr 1:d913e0afb2ac 6740 int hb = 0;
mjr 5:a70c0bce770d 6741 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 6742
mjr 1:d913e0afb2ac 6743 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 6744 Timer acTimer;
mjr 1:d913e0afb2ac 6745 acTimer.start();
mjr 1:d913e0afb2ac 6746
mjr 0:5acbbe3f4cf4 6747 // create the accelerometer object
mjr 77:0b96f6867312 6748 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS,
mjr 78:1e00b3fa11af 6749 MMA8451_INT_PIN, cfg.accel.range, cfg.accel.autoCenterTime);
mjr 76:7f5912b6340e 6750
mjr 48:058ace2aed1d 6751 // initialize the plunger sensor
mjr 35:e959ffba78fd 6752 plungerSensor->init();
mjr 10:976666ffa4ef 6753
mjr 48:058ace2aed1d 6754 // set up the ZB Launch Ball monitor
mjr 48:058ace2aed1d 6755 ZBLaunchBall zbLaunchBall;
mjr 48:058ace2aed1d 6756
mjr 54:fd77a6b2f76c 6757 // enable the peripheral chips
mjr 54:fd77a6b2f76c 6758 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 6759 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 6760 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6761 hc595->enable(true);
mjr 87:8d35c74403af 6762 if (tlc59116 != 0)
mjr 87:8d35c74403af 6763 tlc59116->enable(true);
mjr 74:822a92bc11d2 6764
mjr 76:7f5912b6340e 6765 // start the LedWiz flash cycle timer
mjr 74:822a92bc11d2 6766 wizCycleTimer.start();
mjr 74:822a92bc11d2 6767
mjr 74:822a92bc11d2 6768 // start the PWM update polling timer
mjr 74:822a92bc11d2 6769 polledPwmTimer.start();
mjr 43:7a6364d82a41 6770
mjr 1:d913e0afb2ac 6771 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 6772 // host requests
mjr 0:5acbbe3f4cf4 6773 for (;;)
mjr 0:5acbbe3f4cf4 6774 {
mjr 74:822a92bc11d2 6775 // start the main loop timer for diagnostic data collection
mjr 76:7f5912b6340e 6776 IF_DIAG(mainLoopTimer.reset(); mainLoopTimer.start();)
mjr 96:68d5621ff49f 6777
mjr 48:058ace2aed1d 6778 // Process incoming reports on the joystick interface. The joystick
mjr 48:058ace2aed1d 6779 // "out" (receive) endpoint is used for LedWiz commands and our
mjr 48:058ace2aed1d 6780 // extended protocol commands. Limit processing time to 5ms to
mjr 48:058ace2aed1d 6781 // ensure we don't starve the input side.
mjr 39:b3815a1c3802 6782 LedWizMsg lwm;
mjr 48:058ace2aed1d 6783 Timer lwt;
mjr 48:058ace2aed1d 6784 lwt.start();
mjr 77:0b96f6867312 6785 IF_DIAG(int msgCount = 0;)
mjr 48:058ace2aed1d 6786 while (js.readLedWizMsg(lwm) && lwt.read_us() < 5000)
mjr 74:822a92bc11d2 6787 {
mjr 78:1e00b3fa11af 6788 handleInputMsg(lwm, js, accel);
mjr 74:822a92bc11d2 6789 IF_DIAG(++msgCount;)
mjr 74:822a92bc11d2 6790 }
mjr 74:822a92bc11d2 6791
mjr 74:822a92bc11d2 6792 // collect performance statistics on the message reader, if desired
mjr 74:822a92bc11d2 6793 IF_DIAG(
mjr 74:822a92bc11d2 6794 if (msgCount != 0)
mjr 74:822a92bc11d2 6795 {
mjr 76:7f5912b6340e 6796 mainLoopMsgTime += lwt.read_us();
mjr 74:822a92bc11d2 6797 mainLoopMsgCount++;
mjr 74:822a92bc11d2 6798 }
mjr 74:822a92bc11d2 6799 )
mjr 74:822a92bc11d2 6800
mjr 77:0b96f6867312 6801 // process IR input
mjr 77:0b96f6867312 6802 process_IR(cfg, js);
mjr 77:0b96f6867312 6803
mjr 77:0b96f6867312 6804 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6805 powerStatusUpdate(cfg);
mjr 77:0b96f6867312 6806
mjr 74:822a92bc11d2 6807 // update flashing LedWiz outputs periodically
mjr 74:822a92bc11d2 6808 wizPulse();
mjr 74:822a92bc11d2 6809
mjr 74:822a92bc11d2 6810 // update PWM outputs
mjr 74:822a92bc11d2 6811 pollPwmUpdates();
mjr 77:0b96f6867312 6812
mjr 99:8139b0c274f4 6813 // update Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 6814 LwFlipperLogicOut::poll();
mjr 99:8139b0c274f4 6815 LwChimeLogicOut::poll();
mjr 89:c43cd923401c 6816
mjr 77:0b96f6867312 6817 // poll the accelerometer
mjr 77:0b96f6867312 6818 accel.poll();
mjr 55:4db125cd11a0 6819
mjr 96:68d5621ff49f 6820 // Note the "effective" plunger enabled status. This has two
mjr 96:68d5621ff49f 6821 // components: the explicit "enabled" bit, and the plunger sensor
mjr 96:68d5621ff49f 6822 // type setting. For most purposes, a plunger type of NONE is
mjr 96:68d5621ff49f 6823 // equivalent to disabled. Set this to explicit 0x01 or 0x00
mjr 96:68d5621ff49f 6824 // so that we can OR the bit into status reports.
mjr 96:68d5621ff49f 6825 uint8_t effectivePlungerEnabled = (cfg.plunger.enabled
mjr 96:68d5621ff49f 6826 && cfg.plunger.sensorType != PlungerType_None) ? 0x01 : 0x00;
mjr 96:68d5621ff49f 6827
mjr 76:7f5912b6340e 6828 // collect diagnostic statistics, checkpoint 0
mjr 76:7f5912b6340e 6829 IF_DIAG(mainLoopIterCheckpt[0] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6830
mjr 55:4db125cd11a0 6831 // send TLC5940 data updates if applicable
mjr 55:4db125cd11a0 6832 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6833 tlc5940->send();
mjr 87:8d35c74403af 6834
mjr 87:8d35c74403af 6835 // send TLC59116 data updates
mjr 87:8d35c74403af 6836 if (tlc59116 != 0)
mjr 87:8d35c74403af 6837 tlc59116->send();
mjr 1:d913e0afb2ac 6838
mjr 76:7f5912b6340e 6839 // collect diagnostic statistics, checkpoint 1
mjr 76:7f5912b6340e 6840 IF_DIAG(mainLoopIterCheckpt[1] += mainLoopTimer.read_us();)
mjr 77:0b96f6867312 6841
mjr 1:d913e0afb2ac 6842 // check for plunger calibration
mjr 17:ab3cec0c8bf4 6843 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 6844 {
mjr 1:d913e0afb2ac 6845 // check the state
mjr 1:d913e0afb2ac 6846 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6847 {
mjr 1:d913e0afb2ac 6848 case 0:
mjr 1:d913e0afb2ac 6849 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 6850 calBtnTimer.reset();
mjr 1:d913e0afb2ac 6851 calBtnState = 1;
mjr 1:d913e0afb2ac 6852 break;
mjr 1:d913e0afb2ac 6853
mjr 1:d913e0afb2ac 6854 case 1:
mjr 1:d913e0afb2ac 6855 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 6856 // passed, start the hold period
mjr 48:058ace2aed1d 6857 if (calBtnTimer.read_us() > 50000)
mjr 1:d913e0afb2ac 6858 calBtnState = 2;
mjr 1:d913e0afb2ac 6859 break;
mjr 1:d913e0afb2ac 6860
mjr 1:d913e0afb2ac 6861 case 2:
mjr 1:d913e0afb2ac 6862 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 6863 // for the entire hold period, move to calibration mode
mjr 48:058ace2aed1d 6864 if (calBtnTimer.read_us() > 2050000)
mjr 1:d913e0afb2ac 6865 {
mjr 1:d913e0afb2ac 6866 // enter calibration mode
mjr 1:d913e0afb2ac 6867 calBtnState = 3;
mjr 9:fd65b0a94720 6868 calBtnTimer.reset();
mjr 35:e959ffba78fd 6869
mjr 44:b5ac89b9cd5d 6870 // begin the plunger calibration limits
mjr 52:8298b2a73eb2 6871 plungerReader.setCalMode(true);
mjr 1:d913e0afb2ac 6872 }
mjr 1:d913e0afb2ac 6873 break;
mjr 2:c174f9ee414a 6874
mjr 2:c174f9ee414a 6875 case 3:
mjr 9:fd65b0a94720 6876 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 6877 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 6878 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 6879 break;
mjr 0:5acbbe3f4cf4 6880 }
mjr 0:5acbbe3f4cf4 6881 }
mjr 1:d913e0afb2ac 6882 else
mjr 1:d913e0afb2ac 6883 {
mjr 2:c174f9ee414a 6884 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 6885 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 6886 // and save the results to flash.
mjr 2:c174f9ee414a 6887 //
mjr 2:c174f9ee414a 6888 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 6889 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 6890 // mode, it simply cancels the attempt.
mjr 48:058ace2aed1d 6891 if (calBtnState == 3 && calBtnTimer.read_us() > 15000000)
mjr 2:c174f9ee414a 6892 {
mjr 2:c174f9ee414a 6893 // exit calibration mode
mjr 1:d913e0afb2ac 6894 calBtnState = 0;
mjr 52:8298b2a73eb2 6895 plungerReader.setCalMode(false);
mjr 2:c174f9ee414a 6896
mjr 6:cc35eb643e8f 6897 // save the updated configuration
mjr 35:e959ffba78fd 6898 cfg.plunger.cal.calibrated = 1;
mjr 86:e30a1f60f783 6899 saveConfigToFlash(0, false);
mjr 2:c174f9ee414a 6900 }
mjr 2:c174f9ee414a 6901 else if (calBtnState != 3)
mjr 2:c174f9ee414a 6902 {
mjr 2:c174f9ee414a 6903 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 6904 calBtnState = 0;
mjr 2:c174f9ee414a 6905 }
mjr 1:d913e0afb2ac 6906 }
mjr 1:d913e0afb2ac 6907
mjr 1:d913e0afb2ac 6908 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 6909 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 6910 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6911 {
mjr 1:d913e0afb2ac 6912 case 2:
mjr 1:d913e0afb2ac 6913 // in the hold period - flash the light
mjr 48:058ace2aed1d 6914 newCalBtnLit = ((calBtnTimer.read_us()/250000) & 1);
mjr 1:d913e0afb2ac 6915 break;
mjr 1:d913e0afb2ac 6916
mjr 1:d913e0afb2ac 6917 case 3:
mjr 1:d913e0afb2ac 6918 // calibration mode - show steady on
mjr 1:d913e0afb2ac 6919 newCalBtnLit = true;
mjr 1:d913e0afb2ac 6920 break;
mjr 1:d913e0afb2ac 6921
mjr 1:d913e0afb2ac 6922 default:
mjr 1:d913e0afb2ac 6923 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 6924 newCalBtnLit = false;
mjr 1:d913e0afb2ac 6925 break;
mjr 1:d913e0afb2ac 6926 }
mjr 3:3514575d4f86 6927
mjr 3:3514575d4f86 6928 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 6929 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 6930 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 6931 {
mjr 1:d913e0afb2ac 6932 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 6933 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 6934 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6935 calBtnLed->write(1);
mjr 38:091e511ce8a0 6936 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 6937 }
mjr 2:c174f9ee414a 6938 else {
mjr 17:ab3cec0c8bf4 6939 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6940 calBtnLed->write(0);
mjr 38:091e511ce8a0 6941 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 6942 }
mjr 1:d913e0afb2ac 6943 }
mjr 35:e959ffba78fd 6944
mjr 76:7f5912b6340e 6945 // collect diagnostic statistics, checkpoint 2
mjr 76:7f5912b6340e 6946 IF_DIAG(mainLoopIterCheckpt[2] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6947
mjr 48:058ace2aed1d 6948 // read the plunger sensor
mjr 48:058ace2aed1d 6949 plungerReader.read();
mjr 48:058ace2aed1d 6950
mjr 76:7f5912b6340e 6951 // collect diagnostic statistics, checkpoint 3
mjr 76:7f5912b6340e 6952 IF_DIAG(mainLoopIterCheckpt[3] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6953
mjr 53:9b2611964afc 6954 // update the ZB Launch Ball status
mjr 53:9b2611964afc 6955 zbLaunchBall.update();
mjr 37:ed52738445fc 6956
mjr 76:7f5912b6340e 6957 // collect diagnostic statistics, checkpoint 4
mjr 76:7f5912b6340e 6958 IF_DIAG(mainLoopIterCheckpt[4] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6959
mjr 53:9b2611964afc 6960 // process button updates
mjr 53:9b2611964afc 6961 processButtons(cfg);
mjr 53:9b2611964afc 6962
mjr 76:7f5912b6340e 6963 // collect diagnostic statistics, checkpoint 5
mjr 76:7f5912b6340e 6964 IF_DIAG(mainLoopIterCheckpt[5] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6965
mjr 38:091e511ce8a0 6966 // send a keyboard report if we have new data
mjr 37:ed52738445fc 6967 if (kbState.changed)
mjr 37:ed52738445fc 6968 {
mjr 38:091e511ce8a0 6969 // send a keyboard report
mjr 37:ed52738445fc 6970 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 6971 kbState.changed = false;
mjr 37:ed52738445fc 6972 }
mjr 38:091e511ce8a0 6973
mjr 38:091e511ce8a0 6974 // likewise for the media controller
mjr 37:ed52738445fc 6975 if (mediaState.changed)
mjr 37:ed52738445fc 6976 {
mjr 38:091e511ce8a0 6977 // send a media report
mjr 37:ed52738445fc 6978 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 6979 mediaState.changed = false;
mjr 37:ed52738445fc 6980 }
mjr 38:091e511ce8a0 6981
mjr 76:7f5912b6340e 6982 // collect diagnostic statistics, checkpoint 6
mjr 76:7f5912b6340e 6983 IF_DIAG(mainLoopIterCheckpt[6] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6984
mjr 38:091e511ce8a0 6985 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 6986 bool jsOK = false;
mjr 55:4db125cd11a0 6987
mjr 55:4db125cd11a0 6988 // figure the current status flags for joystick reports
mjr 77:0b96f6867312 6989 uint16_t statusFlags =
mjr 96:68d5621ff49f 6990 effectivePlungerEnabled // 0x01
mjr 77:0b96f6867312 6991 | nightMode // 0x02
mjr 79:682ae3171a08 6992 | ((psu2_state & 0x07) << 2) // 0x04 0x08 0x10
mjr 79:682ae3171a08 6993 | saveConfigSucceededFlag; // 0x40
mjr 77:0b96f6867312 6994 if (IRLearningMode != 0)
mjr 77:0b96f6867312 6995 statusFlags |= 0x20;
mjr 17:ab3cec0c8bf4 6996
mjr 50:40015764bbe6 6997 // If it's been long enough since our last USB status report, send
mjr 50:40015764bbe6 6998 // the new report. VP only polls for input in 10ms intervals, so
mjr 50:40015764bbe6 6999 // there's no benefit in sending reports more frequently than this.
mjr 50:40015764bbe6 7000 // More frequent reporting would only add USB I/O overhead.
mjr 92:f264fbaa1be5 7001 if (cfg.joystickEnabled && jsReportTimer.read_us() > cfg.jsReportInterval_us)
mjr 17:ab3cec0c8bf4 7002 {
mjr 92:f264fbaa1be5 7003 // Increment the "stutter" counter. If it has reached the
mjr 92:f264fbaa1be5 7004 // stutter threshold, read a new accelerometer sample. If
mjr 92:f264fbaa1be5 7005 // not, repeat the last sample.
mjr 92:f264fbaa1be5 7006 if (++jsAccelStutterCounter >= cfg.accel.stutter)
mjr 92:f264fbaa1be5 7007 {
mjr 92:f264fbaa1be5 7008 // read the accelerometer
mjr 92:f264fbaa1be5 7009 int xa, ya;
mjr 92:f264fbaa1be5 7010 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 7011
mjr 92:f264fbaa1be5 7012 // confine the results to our joystick axis range
mjr 92:f264fbaa1be5 7013 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 92:f264fbaa1be5 7014 if (xa > JOYMAX) xa = JOYMAX;
mjr 92:f264fbaa1be5 7015 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 92:f264fbaa1be5 7016 if (ya > JOYMAX) ya = JOYMAX;
mjr 92:f264fbaa1be5 7017
mjr 92:f264fbaa1be5 7018 // store the updated accelerometer coordinates
mjr 92:f264fbaa1be5 7019 x = xa;
mjr 92:f264fbaa1be5 7020 y = ya;
mjr 92:f264fbaa1be5 7021
mjr 95:8eca8acbb82c 7022 // rotate X and Y according to the device orientation in the cabinet
mjr 95:8eca8acbb82c 7023 accelRotate(x, y);
mjr 95:8eca8acbb82c 7024
mjr 92:f264fbaa1be5 7025 // reset the stutter counter
mjr 92:f264fbaa1be5 7026 jsAccelStutterCounter = 0;
mjr 92:f264fbaa1be5 7027 }
mjr 17:ab3cec0c8bf4 7028
mjr 48:058ace2aed1d 7029 // Report the current plunger position unless the plunger is
mjr 48:058ace2aed1d 7030 // disabled, or the ZB Launch Ball signal is on. In either of
mjr 48:058ace2aed1d 7031 // those cases, just report a constant 0 value. ZB Launch Ball
mjr 48:058ace2aed1d 7032 // temporarily disables mechanical plunger reporting because it
mjr 21:5048e16cc9ef 7033 // tells us that the table has a Launch Ball button instead of
mjr 48:058ace2aed1d 7034 // a traditional plunger, so we don't want to confuse VP with
mjr 48:058ace2aed1d 7035 // regular plunger inputs.
mjr 92:f264fbaa1be5 7036 int zActual = plungerReader.getPosition();
mjr 96:68d5621ff49f 7037 int zReported = (!effectivePlungerEnabled || zbLaunchOn ? 0 : zActual);
mjr 35:e959ffba78fd 7038
mjr 35:e959ffba78fd 7039 // send the joystick report
mjr 92:f264fbaa1be5 7040 jsOK = js.update(x, y, zReported, jsButtons, statusFlags);
mjr 21:5048e16cc9ef 7041
mjr 17:ab3cec0c8bf4 7042 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 7043 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 7044 }
mjr 21:5048e16cc9ef 7045
mjr 52:8298b2a73eb2 7046 // If we're in sensor status mode, report all pixel exposure values
mjr 101:755f44622abc 7047 if (reportPlungerStat && plungerSensor->ready())
mjr 10:976666ffa4ef 7048 {
mjr 17:ab3cec0c8bf4 7049 // send the report
mjr 101:755f44622abc 7050 plungerSensor->sendStatusReport(js, reportPlungerStatFlags);
mjr 17:ab3cec0c8bf4 7051
mjr 10:976666ffa4ef 7052 // we have satisfied this request
mjr 52:8298b2a73eb2 7053 reportPlungerStat = false;
mjr 10:976666ffa4ef 7054 }
mjr 10:976666ffa4ef 7055
mjr 101:755f44622abc 7056 // Reset the plunger status report extra timer after enough time has
mjr 101:755f44622abc 7057 // elapsed to satisfy the request. We don't just do this immediately
mjr 101:755f44622abc 7058 // because of the complexities of the pixel frame buffer pipelines in
mjr 101:755f44622abc 7059 // most of the image sensors. The pipelines delay the effect of the
mjr 101:755f44622abc 7060 // exposure time request by a couple of frames, so we can't be sure
mjr 101:755f44622abc 7061 // exactly when they're applied - meaning we can't consider the
mjr 101:755f44622abc 7062 // delay time to be consumed after a fixed number of frames. Instead,
mjr 101:755f44622abc 7063 // we'll consider it consumed after a long enough time to be sure
mjr 101:755f44622abc 7064 // we've sent a few frames. The extra time value is meant to be an
mjr 101:755f44622abc 7065 // interactive tool for debugging, so it's not important to reset it
mjr 101:755f44622abc 7066 // immediately - the user will probably want to see the effect over
mjr 101:755f44622abc 7067 // many frames, so they're likely to keep sending requests with the
mjr 101:755f44622abc 7068 // time value over and over. They'll eventually shut down the frame
mjr 101:755f44622abc 7069 // viewer and return to normal operation, at which point the requests
mjr 101:755f44622abc 7070 // will stop. So we just have to clear things out after we haven't
mjr 101:755f44622abc 7071 // seen a request with extra time for a little while.
mjr 101:755f44622abc 7072 if (reportPlungerStatTime != 0
mjr 101:755f44622abc 7073 && static_cast<uint32_t>(requestTimestamper.read_us() - tReportPlungerStat) > 1000000)
mjr 101:755f44622abc 7074 {
mjr 101:755f44622abc 7075 reportPlungerStatTime = 0;
mjr 101:755f44622abc 7076 plungerSensor->setExtraIntegrationTime(0);
mjr 101:755f44622abc 7077 }
mjr 101:755f44622abc 7078
mjr 35:e959ffba78fd 7079 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 7080 // periodically for the sake of the Windows config tool.
mjr 77:0b96f6867312 7081 if (!cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 21:5048e16cc9ef 7082 {
mjr 55:4db125cd11a0 7083 jsOK = js.updateStatus(statusFlags);
mjr 38:091e511ce8a0 7084 jsReportTimer.reset();
mjr 38:091e511ce8a0 7085 }
mjr 38:091e511ce8a0 7086
mjr 38:091e511ce8a0 7087 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 7088 if (jsOK)
mjr 38:091e511ce8a0 7089 {
mjr 38:091e511ce8a0 7090 jsOKTimer.reset();
mjr 38:091e511ce8a0 7091 jsOKTimer.start();
mjr 21:5048e16cc9ef 7092 }
mjr 21:5048e16cc9ef 7093
mjr 76:7f5912b6340e 7094 // collect diagnostic statistics, checkpoint 7
mjr 76:7f5912b6340e 7095 IF_DIAG(mainLoopIterCheckpt[7] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 7096
mjr 6:cc35eb643e8f 7097 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 7098 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 7099 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 7100 #endif
mjr 6:cc35eb643e8f 7101
mjr 33:d832bcab089e 7102 // check for connection status changes
mjr 54:fd77a6b2f76c 7103 bool newConnected = js.isConnected() && !js.isSleeping();
mjr 33:d832bcab089e 7104 if (newConnected != connected)
mjr 33:d832bcab089e 7105 {
mjr 54:fd77a6b2f76c 7106 // give it a moment to stabilize
mjr 40:cc0d9814522b 7107 connectChangeTimer.start();
mjr 55:4db125cd11a0 7108 if (connectChangeTimer.read_us() > 1000000)
mjr 33:d832bcab089e 7109 {
mjr 33:d832bcab089e 7110 // note the new status
mjr 33:d832bcab089e 7111 connected = newConnected;
mjr 40:cc0d9814522b 7112
mjr 40:cc0d9814522b 7113 // done with the change timer for this round - reset it for next time
mjr 40:cc0d9814522b 7114 connectChangeTimer.stop();
mjr 40:cc0d9814522b 7115 connectChangeTimer.reset();
mjr 33:d832bcab089e 7116
mjr 54:fd77a6b2f76c 7117 // if we're newly disconnected, clean up for PC suspend mode or power off
mjr 54:fd77a6b2f76c 7118 if (!connected)
mjr 40:cc0d9814522b 7119 {
mjr 54:fd77a6b2f76c 7120 // turn off all outputs
mjr 33:d832bcab089e 7121 allOutputsOff();
mjr 40:cc0d9814522b 7122
mjr 40:cc0d9814522b 7123 // The KL25Z runs off of USB power, so we might (depending on the PC
mjr 40:cc0d9814522b 7124 // and OS configuration) continue to receive power even when the main
mjr 40:cc0d9814522b 7125 // PC power supply is turned off, such as in soft-off or suspend/sleep
mjr 40:cc0d9814522b 7126 // mode. Any external output controller chips (TLC5940, 74HC595) might
mjr 40:cc0d9814522b 7127 // be powered from the PC power supply directly rather than from our
mjr 40:cc0d9814522b 7128 // USB power, so they might be powered off even when we're still running.
mjr 40:cc0d9814522b 7129 // To ensure cleaner startup when the power comes back on, globally
mjr 40:cc0d9814522b 7130 // disable the outputs. The global disable signals come from GPIO lines
mjr 40:cc0d9814522b 7131 // that remain powered as long as the KL25Z is powered, so these modes
mjr 40:cc0d9814522b 7132 // will apply smoothly across power state transitions in the external
mjr 40:cc0d9814522b 7133 // hardware. That is, when the external chips are powered up, they'll
mjr 40:cc0d9814522b 7134 // see the global disable signals as stable voltage inputs immediately,
mjr 40:cc0d9814522b 7135 // which will cause them to suppress any output triggering. This ensures
mjr 40:cc0d9814522b 7136 // that we don't fire any solenoids or flash any lights spuriously when
mjr 40:cc0d9814522b 7137 // the power first comes on.
mjr 40:cc0d9814522b 7138 if (tlc5940 != 0)
mjr 40:cc0d9814522b 7139 tlc5940->enable(false);
mjr 87:8d35c74403af 7140 if (tlc59116 != 0)
mjr 87:8d35c74403af 7141 tlc59116->enable(false);
mjr 40:cc0d9814522b 7142 if (hc595 != 0)
mjr 40:cc0d9814522b 7143 hc595->enable(false);
mjr 40:cc0d9814522b 7144 }
mjr 33:d832bcab089e 7145 }
mjr 33:d832bcab089e 7146 }
mjr 48:058ace2aed1d 7147
mjr 53:9b2611964afc 7148 // if we have a reboot timer pending, check for completion
mjr 86:e30a1f60f783 7149 if (saveConfigFollowupTimer.isRunning()
mjr 87:8d35c74403af 7150 && saveConfigFollowupTimer.read_us() > saveConfigFollowupTime*1000000UL)
mjr 85:3c28aee81cde 7151 {
mjr 85:3c28aee81cde 7152 // if a reboot is pending, execute it now
mjr 86:e30a1f60f783 7153 if (saveConfigRebootPending)
mjr 82:4f6209cb5c33 7154 {
mjr 86:e30a1f60f783 7155 // Only reboot if the PSU2 power state allows it. If it
mjr 86:e30a1f60f783 7156 // doesn't, suppress the reboot for now, but leave the boot
mjr 86:e30a1f60f783 7157 // flags set so that we keep checking on future rounds.
mjr 86:e30a1f60f783 7158 // That way we should eventually reboot when the power
mjr 86:e30a1f60f783 7159 // status allows it.
mjr 86:e30a1f60f783 7160 if (powerStatusAllowsReboot())
mjr 86:e30a1f60f783 7161 reboot(js);
mjr 82:4f6209cb5c33 7162 }
mjr 85:3c28aee81cde 7163 else
mjr 85:3c28aee81cde 7164 {
mjr 86:e30a1f60f783 7165 // No reboot required. Exit the timed post-save state.
mjr 86:e30a1f60f783 7166
mjr 86:e30a1f60f783 7167 // stop and reset the post-save timer
mjr 86:e30a1f60f783 7168 saveConfigFollowupTimer.stop();
mjr 86:e30a1f60f783 7169 saveConfigFollowupTimer.reset();
mjr 86:e30a1f60f783 7170
mjr 86:e30a1f60f783 7171 // clear the post-save success flag
mjr 86:e30a1f60f783 7172 saveConfigSucceededFlag = 0;
mjr 85:3c28aee81cde 7173 }
mjr 77:0b96f6867312 7174 }
mjr 86:e30a1f60f783 7175
mjr 48:058ace2aed1d 7176 // if we're disconnected, initiate a new connection
mjr 51:57eb311faafa 7177 if (!connected)
mjr 48:058ace2aed1d 7178 {
mjr 54:fd77a6b2f76c 7179 // show USB HAL debug events
mjr 54:fd77a6b2f76c 7180 extern void HAL_DEBUG_PRINTEVENTS(const char *prefix);
mjr 54:fd77a6b2f76c 7181 HAL_DEBUG_PRINTEVENTS(">DISC");
mjr 54:fd77a6b2f76c 7182
mjr 54:fd77a6b2f76c 7183 // show immediate diagnostic feedback
mjr 54:fd77a6b2f76c 7184 js.diagFlash();
mjr 54:fd77a6b2f76c 7185
mjr 54:fd77a6b2f76c 7186 // clear any previous diagnostic LED display
mjr 54:fd77a6b2f76c 7187 diagLED(0, 0, 0);
mjr 51:57eb311faafa 7188
mjr 51:57eb311faafa 7189 // set up a timer to monitor the reboot timeout
mjr 70:9f58735a1732 7190 Timer reconnTimeoutTimer;
mjr 70:9f58735a1732 7191 reconnTimeoutTimer.start();
mjr 48:058ace2aed1d 7192
mjr 54:fd77a6b2f76c 7193 // set up a timer for diagnostic displays
mjr 54:fd77a6b2f76c 7194 Timer diagTimer;
mjr 54:fd77a6b2f76c 7195 diagTimer.reset();
mjr 54:fd77a6b2f76c 7196 diagTimer.start();
mjr 74:822a92bc11d2 7197
mjr 74:822a92bc11d2 7198 // turn off the main loop timer while spinning
mjr 74:822a92bc11d2 7199 IF_DIAG(mainLoopTimer.stop();)
mjr 54:fd77a6b2f76c 7200
mjr 54:fd77a6b2f76c 7201 // loop until we get our connection back
mjr 54:fd77a6b2f76c 7202 while (!js.isConnected() || js.isSleeping())
mjr 51:57eb311faafa 7203 {
mjr 54:fd77a6b2f76c 7204 // try to recover the connection
mjr 54:fd77a6b2f76c 7205 js.recoverConnection();
mjr 54:fd77a6b2f76c 7206
mjr 99:8139b0c274f4 7207 // update Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 7208 LwFlipperLogicOut::poll();
mjr 99:8139b0c274f4 7209 LwChimeLogicOut::poll();
mjr 89:c43cd923401c 7210
mjr 55:4db125cd11a0 7211 // send TLC5940 data if necessary
mjr 55:4db125cd11a0 7212 if (tlc5940 != 0)
mjr 55:4db125cd11a0 7213 tlc5940->send();
mjr 87:8d35c74403af 7214
mjr 87:8d35c74403af 7215 // update TLC59116 outputs
mjr 87:8d35c74403af 7216 if (tlc59116 != 0)
mjr 87:8d35c74403af 7217 tlc59116->send();
mjr 55:4db125cd11a0 7218
mjr 54:fd77a6b2f76c 7219 // show a diagnostic flash every couple of seconds
mjr 54:fd77a6b2f76c 7220 if (diagTimer.read_us() > 2000000)
mjr 51:57eb311faafa 7221 {
mjr 54:fd77a6b2f76c 7222 // flush the USB HAL debug events, if in debug mode
mjr 54:fd77a6b2f76c 7223 HAL_DEBUG_PRINTEVENTS(">NC");
mjr 54:fd77a6b2f76c 7224
mjr 54:fd77a6b2f76c 7225 // show diagnostic feedback
mjr 54:fd77a6b2f76c 7226 js.diagFlash();
mjr 51:57eb311faafa 7227
mjr 51:57eb311faafa 7228 // reset the flash timer
mjr 54:fd77a6b2f76c 7229 diagTimer.reset();
mjr 51:57eb311faafa 7230 }
mjr 51:57eb311faafa 7231
mjr 77:0b96f6867312 7232 // If the disconnect reboot timeout has expired, reboot.
mjr 77:0b96f6867312 7233 // Some PC hosts won't reconnect to a device that's left
mjr 77:0b96f6867312 7234 // plugged in through various events on the PC side, such as
mjr 77:0b96f6867312 7235 // rebooting Windows, cycling power on the PC, or just a lost
mjr 77:0b96f6867312 7236 // USB connection. Rebooting the KL25Z seems to be the most
mjr 77:0b96f6867312 7237 // reliable way to get Windows to notice us again after one
mjr 86:e30a1f60f783 7238 // of these events and make it reconnect. Only reboot if
mjr 86:e30a1f60f783 7239 // the PSU2 power status allows it - if not, skip it on this
mjr 86:e30a1f60f783 7240 // round and keep waiting.
mjr 51:57eb311faafa 7241 if (cfg.disconnectRebootTimeout != 0
mjr 86:e30a1f60f783 7242 && reconnTimeoutTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 7243 && powerStatusAllowsReboot())
mjr 54:fd77a6b2f76c 7244 reboot(js, false, 0);
mjr 77:0b96f6867312 7245
mjr 77:0b96f6867312 7246 // update the PSU2 power sensing status
mjr 77:0b96f6867312 7247 powerStatusUpdate(cfg);
mjr 54:fd77a6b2f76c 7248 }
mjr 54:fd77a6b2f76c 7249
mjr 74:822a92bc11d2 7250 // resume the main loop timer
mjr 74:822a92bc11d2 7251 IF_DIAG(mainLoopTimer.start();)
mjr 74:822a92bc11d2 7252
mjr 54:fd77a6b2f76c 7253 // if we made it out of that loop alive, we're connected again!
mjr 54:fd77a6b2f76c 7254 connected = true;
mjr 54:fd77a6b2f76c 7255 HAL_DEBUG_PRINTEVENTS(">C");
mjr 54:fd77a6b2f76c 7256
mjr 54:fd77a6b2f76c 7257 // Enable peripheral chips and update them with current output data
mjr 54:fd77a6b2f76c 7258 if (tlc5940 != 0)
mjr 55:4db125cd11a0 7259 tlc5940->enable(true);
mjr 87:8d35c74403af 7260 if (tlc59116 != 0)
mjr 87:8d35c74403af 7261 tlc59116->enable(true);
mjr 54:fd77a6b2f76c 7262 if (hc595 != 0)
mjr 54:fd77a6b2f76c 7263 {
mjr 55:4db125cd11a0 7264 hc595->enable(true);
mjr 54:fd77a6b2f76c 7265 hc595->update(true);
mjr 51:57eb311faafa 7266 }
mjr 48:058ace2aed1d 7267 }
mjr 43:7a6364d82a41 7268
mjr 6:cc35eb643e8f 7269 // provide a visual status indication on the on-board LED
mjr 48:058ace2aed1d 7270 if (calBtnState < 2 && hbTimer.read_us() > 1000000)
mjr 1:d913e0afb2ac 7271 {
mjr 54:fd77a6b2f76c 7272 if (jsOKTimer.read_us() > 1000000)
mjr 38:091e511ce8a0 7273 {
mjr 39:b3815a1c3802 7274 // USB freeze - show red/yellow.
mjr 40:cc0d9814522b 7275 //
mjr 54:fd77a6b2f76c 7276 // It's been more than a second since we successfully sent a joystick
mjr 54:fd77a6b2f76c 7277 // update message. This must mean that something's wrong on the USB
mjr 54:fd77a6b2f76c 7278 // connection, even though we haven't detected an outright disconnect.
mjr 54:fd77a6b2f76c 7279 // Show a distinctive diagnostic LED pattern when this occurs.
mjr 38:091e511ce8a0 7280 hb = !hb;
mjr 38:091e511ce8a0 7281 diagLED(1, hb, 0);
mjr 54:fd77a6b2f76c 7282
mjr 54:fd77a6b2f76c 7283 // If the reboot-on-disconnect option is in effect, treat this condition
mjr 54:fd77a6b2f76c 7284 // as equivalent to a disconnect, since something is obviously wrong
mjr 54:fd77a6b2f76c 7285 // with the USB connection.
mjr 54:fd77a6b2f76c 7286 if (cfg.disconnectRebootTimeout != 0)
mjr 54:fd77a6b2f76c 7287 {
mjr 54:fd77a6b2f76c 7288 // The reboot timeout is in effect. If we've been incommunicado for
mjr 54:fd77a6b2f76c 7289 // longer than the timeout, reboot. If we haven't reached the time
mjr 54:fd77a6b2f76c 7290 // limit, keep running for now, and leave the OK timer running so
mjr 86:e30a1f60f783 7291 // that we can continue to monitor this. Only reboot if the PSU2
mjr 86:e30a1f60f783 7292 // power status allows it.
mjr 86:e30a1f60f783 7293 if (jsOKTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 7294 && powerStatusAllowsReboot())
mjr 54:fd77a6b2f76c 7295 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 7296 }
mjr 54:fd77a6b2f76c 7297 else
mjr 54:fd77a6b2f76c 7298 {
mjr 54:fd77a6b2f76c 7299 // There's no reboot timer, so just keep running with the diagnostic
mjr 54:fd77a6b2f76c 7300 // pattern displayed. Since we're not waiting for any other timed
mjr 54:fd77a6b2f76c 7301 // conditions in this state, stop the timer so that it doesn't
mjr 54:fd77a6b2f76c 7302 // overflow if this condition persists for a long time.
mjr 54:fd77a6b2f76c 7303 jsOKTimer.stop();
mjr 54:fd77a6b2f76c 7304 }
mjr 38:091e511ce8a0 7305 }
mjr 73:4e8ce0b18915 7306 else if (psu2_state >= 4)
mjr 73:4e8ce0b18915 7307 {
mjr 73:4e8ce0b18915 7308 // We're in the TV timer countdown. Skip the normal heartbeat
mjr 73:4e8ce0b18915 7309 // flashes and show the TV timer flashes instead.
mjr 73:4e8ce0b18915 7310 diagLED(0, 0, 0);
mjr 73:4e8ce0b18915 7311 }
mjr 96:68d5621ff49f 7312 else if (effectivePlungerEnabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 7313 {
mjr 6:cc35eb643e8f 7314 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 7315 hb = !hb;
mjr 38:091e511ce8a0 7316 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 7317 }
mjr 6:cc35eb643e8f 7318 else
mjr 6:cc35eb643e8f 7319 {
mjr 6:cc35eb643e8f 7320 // connected - flash blue/green
mjr 2:c174f9ee414a 7321 hb = !hb;
mjr 38:091e511ce8a0 7322 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 7323 }
mjr 1:d913e0afb2ac 7324
mjr 1:d913e0afb2ac 7325 // reset the heartbeat timer
mjr 1:d913e0afb2ac 7326 hbTimer.reset();
mjr 5:a70c0bce770d 7327 ++hbcnt;
mjr 1:d913e0afb2ac 7328 }
mjr 74:822a92bc11d2 7329
mjr 74:822a92bc11d2 7330 // collect statistics on the main loop time, if desired
mjr 74:822a92bc11d2 7331 IF_DIAG(
mjr 76:7f5912b6340e 7332 mainLoopIterTime += mainLoopTimer.read_us();
mjr 74:822a92bc11d2 7333 mainLoopIterCount++;
mjr 74:822a92bc11d2 7334 )
mjr 1:d913e0afb2ac 7335 }
mjr 0:5acbbe3f4cf4 7336 }