Pinscape Controller version 1 fork. This is a fork to allow for ongoing bug fixes to the original controller version, from before the major changes for the expansion board project.

Dependencies:   FastIO FastPWM SimpleDMA mbed

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Tue Sep 01 04:27:15 2015 +0000
Revision:
25:e22b88bd783a
Parent:
23:14f8c5004cd0
Child:
26:cb71c4af2912
Centralized the CCD pixel count setting to a single config.h option; added an option to config.h to select the board's mounting orientation for the accelerometer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 5:a70c0bce770d 1 /* Copyright 2014 M J Roberts, MIT License
mjr 5:a70c0bce770d 2 *
mjr 5:a70c0bce770d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mjr 5:a70c0bce770d 4 * and associated documentation files (the "Software"), to deal in the Software without
mjr 5:a70c0bce770d 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mjr 5:a70c0bce770d 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mjr 5:a70c0bce770d 7 * Software is furnished to do so, subject to the following conditions:
mjr 5:a70c0bce770d 8 *
mjr 5:a70c0bce770d 9 * The above copyright notice and this permission notice shall be included in all copies or
mjr 5:a70c0bce770d 10 * substantial portions of the Software.
mjr 5:a70c0bce770d 11 *
mjr 5:a70c0bce770d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mjr 5:a70c0bce770d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 5:a70c0bce770d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 5:a70c0bce770d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 5:a70c0bce770d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 5:a70c0bce770d 17 */
mjr 5:a70c0bce770d 18
mjr 5:a70c0bce770d 19 //
mjr 5:a70c0bce770d 20 // Pinscape Controller
mjr 5:a70c0bce770d 21 //
mjr 17:ab3cec0c8bf4 22 // "Pinscape" is the name of my custom-built virtual pinball cabinet, so I call this
mjr 17:ab3cec0c8bf4 23 // software the Pinscape Controller. I wrote it to handle several tasks that I needed
mjr 17:ab3cec0c8bf4 24 // for my cabinet. It runs on a Freescale KL25Z microcontroller, which is a small and
mjr 17:ab3cec0c8bf4 25 // inexpensive device that attaches to the cabinet PC via a USB cable, and can attach
mjr 17:ab3cec0c8bf4 26 // via custom wiring to sensors, buttons, and other devices in the cabinet.
mjr 5:a70c0bce770d 27 //
mjr 17:ab3cec0c8bf4 28 // I designed the software and hardware in this project especially for my own
mjr 17:ab3cec0c8bf4 29 // cabinet, but it uses standard interfaces in Windows and Visual Pinball, so it should
mjr 17:ab3cec0c8bf4 30 // work in any VP-based cabinet, as long as you're using the usual VP software suite.
mjr 17:ab3cec0c8bf4 31 // I've tried to document the hardware in enough detail for anyone else to duplicate
mjr 17:ab3cec0c8bf4 32 // the entire project, and the full software is open source.
mjr 5:a70c0bce770d 33 //
mjr 17:ab3cec0c8bf4 34 // The Freescale board appears to the host PC as a standard USB joystick. This works
mjr 17:ab3cec0c8bf4 35 // with the built-in Windows joystick device drivers, so there's no need to install any
mjr 17:ab3cec0c8bf4 36 // new drivers or other software on the PC. Windows should recognize the Freescale
mjr 17:ab3cec0c8bf4 37 // as a joystick when you plug it into the USB port, and Windows shouldn't ask you to
mjr 17:ab3cec0c8bf4 38 // install any drivers. If you bring up the Windows control panel for USB Game
mjr 17:ab3cec0c8bf4 39 // Controllers, this device will appear as "Pinscape Controller". *Don't* do any
mjr 17:ab3cec0c8bf4 40 // calibration with the Windows control panel or third-part calibration tools. The
mjr 17:ab3cec0c8bf4 41 // software calibrates the accelerometer portion automatically, and has its own special
mjr 17:ab3cec0c8bf4 42 // calibration procedure for the plunger sensor, if you're using that (see below).
mjr 5:a70c0bce770d 43 //
mjr 17:ab3cec0c8bf4 44 // This software provides a whole bunch of separate features. You can use any of these
mjr 17:ab3cec0c8bf4 45 // features individually or all together. If you're not using a particular feature, you
mjr 17:ab3cec0c8bf4 46 // can simply omit the extra wiring and/or hardware for that feature. You can use
mjr 17:ab3cec0c8bf4 47 // the nudging feature by itself without any extra hardware attached, since the
mjr 17:ab3cec0c8bf4 48 // accelerometer is built in to the KL25Z board.
mjr 5:a70c0bce770d 49 //
mjr 17:ab3cec0c8bf4 50 // - Nudge sensing via the KL25Z's on-board accelerometer. Nudging the cabinet
mjr 17:ab3cec0c8bf4 51 // causes small accelerations that the accelerometer can detect; these are sent to
mjr 17:ab3cec0c8bf4 52 // Visual Pinball via the joystick interface so that VP can simulate the effect
mjr 17:ab3cec0c8bf4 53 // of the real physical nudges on its simulated ball. VP has native handling for
mjr 17:ab3cec0c8bf4 54 // this type of input, so all you have to do is set some preferences in VP to tell
mjr 17:ab3cec0c8bf4 55 // it that an accelerometer is attached.
mjr 5:a70c0bce770d 56 //
mjr 5:a70c0bce770d 57 // - Plunger position sensing via an attached TAOS TSL 1410R CCD linear array sensor.
mjr 17:ab3cec0c8bf4 58 // To use this feature, you need to buy the TAOS device (it's not built in to the
mjr 17:ab3cec0c8bf4 59 // KL25Z, obviously), wire it to the KL25Z (5 wire connections between the two
mjr 17:ab3cec0c8bf4 60 // devices are required), and mount the TAOS sensor in your cabinet so that it's
mjr 17:ab3cec0c8bf4 61 // positioned properly to capture images of the physical plunger shooter rod.
mjr 17:ab3cec0c8bf4 62 //
mjr 17:ab3cec0c8bf4 63 // The physical mounting and wiring details are desribed in the project
mjr 17:ab3cec0c8bf4 64 // documentation.
mjr 17:ab3cec0c8bf4 65 //
mjr 17:ab3cec0c8bf4 66 // If the CCD is attached, the software constantly captures images from the CCD
mjr 17:ab3cec0c8bf4 67 // and analyzes them to determine how far back the plunger is pulled. It reports
mjr 17:ab3cec0c8bf4 68 // this to Visual Pinball via the joystick interface. This allows VP to make the
mjr 17:ab3cec0c8bf4 69 // simulated on-screen plunger track the motion of the physical plunger in real
mjr 17:ab3cec0c8bf4 70 // time. As with the nudge data, VP has native handling for the plunger input,
mjr 17:ab3cec0c8bf4 71 // so you just need to set the VP preferences to tell it that an analog plunger
mjr 17:ab3cec0c8bf4 72 // device is attached. One caveat, though: although VP itself has built-in
mjr 17:ab3cec0c8bf4 73 // support for an analog plunger, not all existing tables take advantage of it.
mjr 17:ab3cec0c8bf4 74 // Many existing tables have their own custom plunger scripting that doesn't
mjr 17:ab3cec0c8bf4 75 // cooperate with the VP plunger input. All tables *can* be made to work with
mjr 17:ab3cec0c8bf4 76 // the plunger, and in most cases it only requires some simple script editing,
mjr 17:ab3cec0c8bf4 77 // but in some cases it requires some more extensive surgery.
mjr 5:a70c0bce770d 78 //
mjr 6:cc35eb643e8f 79 // For best results, the plunger sensor should be calibrated. The calibration
mjr 6:cc35eb643e8f 80 // is stored in non-volatile memory on board the KL25Z, so it's only necessary
mjr 6:cc35eb643e8f 81 // to do the calibration once, when you first install everything. (You might
mjr 6:cc35eb643e8f 82 // also want to re-calibrate if you physically remove and reinstall the CCD
mjr 17:ab3cec0c8bf4 83 // sensor or the mechanical plunger, since their alignment shift change slightly
mjr 17:ab3cec0c8bf4 84 // when you put everything back together.) You can optionally install a
mjr 17:ab3cec0c8bf4 85 // dedicated momentary switch or pushbutton to activate the calibration mode;
mjr 17:ab3cec0c8bf4 86 // this is describe in the project documentation. If you don't want to bother
mjr 17:ab3cec0c8bf4 87 // with the extra button, you can also trigger calibration using the Windows
mjr 17:ab3cec0c8bf4 88 // setup software, which you can find on the Pinscape project page.
mjr 6:cc35eb643e8f 89 //
mjr 17:ab3cec0c8bf4 90 // The calibration procedure is described in the project documentation. Briefly,
mjr 17:ab3cec0c8bf4 91 // when you trigger calibration mode, the software will scan the CCD for about
mjr 17:ab3cec0c8bf4 92 // 15 seconds, during which you should simply pull the physical plunger back
mjr 17:ab3cec0c8bf4 93 // all the way, hold it for a moment, and then slowly return it to the rest
mjr 17:ab3cec0c8bf4 94 // position. (DON'T just release it from the retracted position, since that
mjr 17:ab3cec0c8bf4 95 // let it shoot forward too far. We want to measure the range from the park
mjr 17:ab3cec0c8bf4 96 // position to the fully retracted position only.)
mjr 5:a70c0bce770d 97 //
mjr 13:72dda449c3c0 98 // - Button input wiring. 24 of the KL25Z's GPIO ports are mapped as digital inputs
mjr 13:72dda449c3c0 99 // for buttons and switches. The software reports these as joystick buttons when
mjr 13:72dda449c3c0 100 // it sends reports to the PC. These can be used to wire physical pinball-style
mjr 13:72dda449c3c0 101 // buttons in the cabinet (e.g., flipper buttons, the Start button) and miscellaneous
mjr 13:72dda449c3c0 102 // switches (such as a tilt bob) to the PC. Visual Pinball can use joystick buttons
mjr 13:72dda449c3c0 103 // for input - you just have to assign a VP function to each button using VP's
mjr 13:72dda449c3c0 104 // keyboard options dialog. To wire a button physically, connect one terminal of
mjr 13:72dda449c3c0 105 // the button switch to the KL25Z ground, and connect the other terminal to the
mjr 13:72dda449c3c0 106 // the GPIO port you wish to assign to the button. See the buttonMap[] array
mjr 13:72dda449c3c0 107 // below for the available GPIO ports and their assigned joystick button numbers.
mjr 13:72dda449c3c0 108 // If you're not using a GPIO port, you can just leave it unconnected - the digital
mjr 13:72dda449c3c0 109 // inputs have built-in pull-up resistors, so an unconnected port is the same as
mjr 13:72dda449c3c0 110 // an open switch (an "off" state for the button).
mjr 13:72dda449c3c0 111 //
mjr 5:a70c0bce770d 112 // - LedWiz emulation. The KL25Z can appear to the PC as an LedWiz device, and will
mjr 5:a70c0bce770d 113 // accept and process LedWiz commands from the host. The software can turn digital
mjr 5:a70c0bce770d 114 // output ports on and off, and can set varying PWM intensitiy levels on a subset
mjr 5:a70c0bce770d 115 // of ports. (The KL25Z can only provide 6 PWM ports. Intensity level settings on
mjr 5:a70c0bce770d 116 // other ports is ignored, so non-PWM ports can only be used for simple on/off
mjr 5:a70c0bce770d 117 // devices such as contactors and solenoids.) The KL25Z can only supply 4mA on its
mjr 5:a70c0bce770d 118 // output ports, so external hardware is required to take advantage of the LedWiz
mjr 5:a70c0bce770d 119 // emulation. Many different hardware designs are possible, but there's a simple
mjr 5:a70c0bce770d 120 // reference design in the documentation that uses a Darlington array IC to
mjr 5:a70c0bce770d 121 // increase the output from each port to 500mA (the same level as the LedWiz),
mjr 5:a70c0bce770d 122 // plus an extended design that adds an optocoupler and MOSFET to provide very
mjr 5:a70c0bce770d 123 // high power handling, up to about 45A or 150W, with voltages up to 100V.
mjr 5:a70c0bce770d 124 // That will handle just about any DC device directly (wtihout relays or other
mjr 5:a70c0bce770d 125 // amplifiers), and switches fast enough to support PWM devices.
mjr 5:a70c0bce770d 126 //
mjr 5:a70c0bce770d 127 // The device can report any desired LedWiz unit number to the host, which makes
mjr 5:a70c0bce770d 128 // it possible to use the LedWiz emulation on a machine that also has one or more
mjr 5:a70c0bce770d 129 // actual LedWiz devices intalled. The LedWiz design allows for up to 16 units
mjr 5:a70c0bce770d 130 // to be installed in one machine - each one is invidually addressable by its
mjr 5:a70c0bce770d 131 // distinct unit number.
mjr 5:a70c0bce770d 132 //
mjr 5:a70c0bce770d 133 // The LedWiz emulation features are of course optional. There's no need to
mjr 5:a70c0bce770d 134 // build any of the external port hardware (or attach anything to the output
mjr 5:a70c0bce770d 135 // ports at all) if the LedWiz features aren't needed. Most people won't have
mjr 5:a70c0bce770d 136 // any use for the LedWiz features. I built them mostly as a learning exercise,
mjr 5:a70c0bce770d 137 // but with a slight practical need for a handful of extra ports (I'm using the
mjr 5:a70c0bce770d 138 // cutting-edge 10-contactor setup, so my real LedWiz is full!).
mjr 6:cc35eb643e8f 139 //
mjr 6:cc35eb643e8f 140 // The on-board LED on the KL25Z flashes to indicate the current device status:
mjr 6:cc35eb643e8f 141 //
mjr 6:cc35eb643e8f 142 // two short red flashes = the device is powered but hasn't successfully
mjr 6:cc35eb643e8f 143 // connected to the host via USB (either it's not physically connected
mjr 6:cc35eb643e8f 144 // to the USB port, or there was a problem with the software handshake
mjr 6:cc35eb643e8f 145 // with the USB device driver on the computer)
mjr 6:cc35eb643e8f 146 //
mjr 6:cc35eb643e8f 147 // short red flash = the host computer is in sleep/suspend mode
mjr 6:cc35eb643e8f 148 //
mjr 6:cc35eb643e8f 149 // long red/green = the LedWiz unti number has been changed, so a reset
mjr 6:cc35eb643e8f 150 // is needed. You can simply unplug the device and plug it back in,
mjr 6:cc35eb643e8f 151 // or presss and hold the reset button on the device for a few seconds.
mjr 6:cc35eb643e8f 152 //
mjr 6:cc35eb643e8f 153 // long yellow/green = everything's working, but the plunger hasn't
mjr 6:cc35eb643e8f 154 // been calibrated; follow the calibration procedure described above.
mjr 6:cc35eb643e8f 155 // This flash mode won't appear if the CCD has been disabled. Note
mjr 18:5e890ebd0023 156 // that the device can't tell whether a CCD is physically attached;
mjr 18:5e890ebd0023 157 // if you don't have a CCD attached, you can set the appropriate option
mjr 18:5e890ebd0023 158 // in config.h or use the Windows config tool to disable the CCD
mjr 18:5e890ebd0023 159 // software features.
mjr 6:cc35eb643e8f 160 //
mjr 6:cc35eb643e8f 161 // alternating blue/green = everything's working
mjr 6:cc35eb643e8f 162 //
mjr 6:cc35eb643e8f 163 // Software configuration: you can change option settings by sending special
mjr 6:cc35eb643e8f 164 // USB commands from the PC. I've provided a Windows program for this purpose;
mjr 6:cc35eb643e8f 165 // refer to the documentation for details. For reference, here's the format
mjr 6:cc35eb643e8f 166 // of the USB command for option changes:
mjr 6:cc35eb643e8f 167 //
mjr 6:cc35eb643e8f 168 // length of report = 8 bytes
mjr 6:cc35eb643e8f 169 // byte 0 = 65 (0x41)
mjr 6:cc35eb643e8f 170 // byte 1 = 1 (0x01)
mjr 6:cc35eb643e8f 171 // byte 2 = new LedWiz unit number, 0x01 to 0x0f
mjr 6:cc35eb643e8f 172 // byte 3 = feature enable bit mask:
mjr 6:cc35eb643e8f 173 // 0x01 = enable CCD (default = on)
mjr 9:fd65b0a94720 174 //
mjr 9:fd65b0a94720 175 // Plunger calibration mode: the host can activate plunger calibration mode
mjr 9:fd65b0a94720 176 // by sending this packet. This has the same effect as pressing and holding
mjr 9:fd65b0a94720 177 // the plunger calibration button for two seconds, to allow activating this
mjr 9:fd65b0a94720 178 // mode without attaching a physical button.
mjr 9:fd65b0a94720 179 //
mjr 9:fd65b0a94720 180 // length = 8 bytes
mjr 9:fd65b0a94720 181 // byte 0 = 65 (0x41)
mjr 9:fd65b0a94720 182 // byte 1 = 2 (0x02)
mjr 9:fd65b0a94720 183 //
mjr 10:976666ffa4ef 184 // Exposure reports: the host can request a report of the full set of pixel
mjr 10:976666ffa4ef 185 // values for the next frame by sending this special packet:
mjr 10:976666ffa4ef 186 //
mjr 10:976666ffa4ef 187 // length = 8 bytes
mjr 10:976666ffa4ef 188 // byte 0 = 65 (0x41)
mjr 10:976666ffa4ef 189 // byte 1 = 3 (0x03)
mjr 10:976666ffa4ef 190 //
mjr 10:976666ffa4ef 191 // We'll respond with a series of special reports giving the exposure status.
mjr 10:976666ffa4ef 192 // Each report has the following structure:
mjr 10:976666ffa4ef 193 //
mjr 10:976666ffa4ef 194 // bytes 0:1 = 11-bit index, with high 5 bits set to 10000. For
mjr 10:976666ffa4ef 195 // example, 0x04 0x80 indicates index 4. This is the
mjr 10:976666ffa4ef 196 // starting pixel number in the report. The first report
mjr 10:976666ffa4ef 197 // will be 0x00 0x80 to indicate pixel #0.
mjr 10:976666ffa4ef 198 // bytes 2:3 = 16-bit unsigned int brightness level of pixel at index
mjr 10:976666ffa4ef 199 // bytes 4:5 = brightness of pixel at index+1
mjr 10:976666ffa4ef 200 // etc for the rest of the packet
mjr 10:976666ffa4ef 201 //
mjr 10:976666ffa4ef 202 // This still has the form of a joystick packet at the USB level, but
mjr 10:976666ffa4ef 203 // can be differentiated by the host via the status bits. It would have
mjr 10:976666ffa4ef 204 // been cleaner to use a different Report ID at the USB level, but this
mjr 10:976666ffa4ef 205 // would have necessitated a different container structure in the report
mjr 10:976666ffa4ef 206 // descriptor, which would have broken LedWiz compatibility. Given that
mjr 10:976666ffa4ef 207 // constraint, we have to re-use the joystick report type, making for
mjr 10:976666ffa4ef 208 // this somewhat kludgey approach.
mjr 6:cc35eb643e8f 209
mjr 0:5acbbe3f4cf4 210 #include "mbed.h"
mjr 6:cc35eb643e8f 211 #include "math.h"
mjr 0:5acbbe3f4cf4 212 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 213 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 214 #include "tsl1410r.h"
mjr 1:d913e0afb2ac 215 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 216 #include "crc32.h"
mjr 2:c174f9ee414a 217
mjr 17:ab3cec0c8bf4 218 // our local configuration file
mjr 21:5048e16cc9ef 219 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 220 #include "config.h"
mjr 17:ab3cec0c8bf4 221
mjr 5:a70c0bce770d 222
mjr 5:a70c0bce770d 223 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 224 // utilities
mjr 17:ab3cec0c8bf4 225
mjr 17:ab3cec0c8bf4 226 // number of elements in an array
mjr 17:ab3cec0c8bf4 227 #define countof(x) (sizeof(x)/sizeof((x)[0]))
mjr 17:ab3cec0c8bf4 228
mjr 17:ab3cec0c8bf4 229
mjr 17:ab3cec0c8bf4 230 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 231 // USB device vendor ID, product ID, and version.
mjr 5:a70c0bce770d 232 //
mjr 5:a70c0bce770d 233 // We use the vendor ID for the LedWiz, so that the PC-side software can
mjr 5:a70c0bce770d 234 // identify us as capable of performing LedWiz commands. The LedWiz uses
mjr 5:a70c0bce770d 235 // a product ID value from 0xF0 to 0xFF; the last four bits identify the
mjr 5:a70c0bce770d 236 // unit number (e.g., product ID 0xF7 means unit #7). This allows multiple
mjr 5:a70c0bce770d 237 // LedWiz units to be installed in a single PC; the software on the PC side
mjr 5:a70c0bce770d 238 // uses the unit number to route commands to the devices attached to each
mjr 5:a70c0bce770d 239 // unit. On the real LedWiz, the unit number must be set in the firmware
mjr 5:a70c0bce770d 240 // at the factory; it's not configurable by the end user. Most LedWiz's
mjr 5:a70c0bce770d 241 // ship with the unit number set to 0, but the vendor will set different
mjr 5:a70c0bce770d 242 // unit numbers if requested at the time of purchase. So if you have a
mjr 5:a70c0bce770d 243 // single LedWiz already installed in your cabinet, and you didn't ask for
mjr 5:a70c0bce770d 244 // a non-default unit number, your existing LedWiz will be unit 0.
mjr 5:a70c0bce770d 245 //
mjr 6:cc35eb643e8f 246 // Note that the USB_PRODUCT_ID value set here omits the unit number. We
mjr 6:cc35eb643e8f 247 // take the unit number from the saved configuration. We provide a
mjr 6:cc35eb643e8f 248 // configuration command that can be sent via the USB connection to change
mjr 6:cc35eb643e8f 249 // the unit number, so that users can select the unit number without having
mjr 6:cc35eb643e8f 250 // to install a different version of the software. We'll combine the base
mjr 6:cc35eb643e8f 251 // product ID here with the unit number to get the actual product ID that
mjr 6:cc35eb643e8f 252 // we send to the USB controller.
mjr 5:a70c0bce770d 253 const uint16_t USB_VENDOR_ID = 0xFAFA;
mjr 6:cc35eb643e8f 254 const uint16_t USB_PRODUCT_ID = 0x00F0;
mjr 6:cc35eb643e8f 255 const uint16_t USB_VERSION_NO = 0x0006;
mjr 0:5acbbe3f4cf4 256
mjr 5:a70c0bce770d 257
mjr 6:cc35eb643e8f 258 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 6:cc35eb643e8f 259 #define JOYMAX 4096
mjr 6:cc35eb643e8f 260
mjr 25:e22b88bd783a 261 // --------------------------------------------------------------------------
mjr 25:e22b88bd783a 262 //
mjr 25:e22b88bd783a 263 // Set up mappings for the joystick X and Y reports based on the mounting
mjr 25:e22b88bd783a 264 // orientation of the KL25Z in the cabinet. Visual Pinball and other
mjr 25:e22b88bd783a 265 // pinball software effectively use video coordinates to define the axes:
mjr 25:e22b88bd783a 266 // positive X is to the right of the table, negative Y to the left, positive
mjr 25:e22b88bd783a 267 // Y toward the front of the table, negative Y toward the back. The KL25Z
mjr 25:e22b88bd783a 268 // accelerometer is mounted on the board with positive Y toward the USB
mjr 25:e22b88bd783a 269 // ports and positive X toward the right side of the board with the USB
mjr 25:e22b88bd783a 270 // ports pointing up. It's a simple matter to remap the KL25Z coordinate
mjr 25:e22b88bd783a 271 // system to match VP's coordinate system for mounting orientations at
mjr 25:e22b88bd783a 272 // 90-degree increments...
mjr 25:e22b88bd783a 273 //
mjr 25:e22b88bd783a 274 #if defined(ORIENTATION_PORTS_AT_FRONT)
mjr 25:e22b88bd783a 275 # define JOY_X(x, y) (y)
mjr 25:e22b88bd783a 276 # define JOY_Y(x, y) (x)
mjr 25:e22b88bd783a 277 #elif defined(ORIENTATION_PORTS_AT_LEFT)
mjr 25:e22b88bd783a 278 # define JOY_X(x, y) (-(x))
mjr 25:e22b88bd783a 279 # define JOY_Y(x, y) (y)
mjr 25:e22b88bd783a 280 #elif defined(ORIENTATION_PORTS_AT_RIGHT)
mjr 25:e22b88bd783a 281 # define JOY_X(x, y) (x)
mjr 25:e22b88bd783a 282 # define JOY_Y(x, y) (-(y))
mjr 25:e22b88bd783a 283 #elif defined(ORIENTATION_PORTS_AT_REAR)
mjr 25:e22b88bd783a 284 # define JOY_X(x, y) (-(y))
mjr 25:e22b88bd783a 285 # define JOY_Y(x, y) (-(x))
mjr 25:e22b88bd783a 286 #else
mjr 25:e22b88bd783a 287 # error Please define one of the ORIENTATION_PORTS_AT_xxx macros to establish the accelerometer orientation in your cabinet
mjr 25:e22b88bd783a 288 #endif
mjr 25:e22b88bd783a 289
mjr 25:e22b88bd783a 290
mjr 5:a70c0bce770d 291
mjr 17:ab3cec0c8bf4 292 // --------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 293 //
mjr 21:5048e16cc9ef 294 // Define a symbol to tell us whether any sort of plunger sensor code
mjr 21:5048e16cc9ef 295 // is enabled in this build. Note that this doesn't tell us that a
mjr 21:5048e16cc9ef 296 // plunger device is actually attached or *currently* enabled; it just
mjr 21:5048e16cc9ef 297 // tells us whether or not the code for plunger sensing is enabled in
mjr 21:5048e16cc9ef 298 // the software build. This lets us leave out some unnecessary code
mjr 21:5048e16cc9ef 299 // on installations where no physical plunger is attached.
mjr 17:ab3cec0c8bf4 300 //
mjr 21:5048e16cc9ef 301 const int PLUNGER_CODE_ENABLED =
mjr 21:5048e16cc9ef 302 #if defined(ENABLE_CCD_SENSOR) || defined(ENABLE_POT_SENSOR)
mjr 21:5048e16cc9ef 303 1;
mjr 17:ab3cec0c8bf4 304 #else
mjr 21:5048e16cc9ef 305 0;
mjr 17:ab3cec0c8bf4 306 #endif
mjr 9:fd65b0a94720 307
mjr 17:ab3cec0c8bf4 308 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 309 //
mjr 17:ab3cec0c8bf4 310 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 17:ab3cec0c8bf4 311 //
mjr 17:ab3cec0c8bf4 312 DigitalOut ledR(LED1), ledG(LED2), ledB(LED3);
mjr 17:ab3cec0c8bf4 313
mjr 9:fd65b0a94720 314
mjr 9:fd65b0a94720 315 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 316 //
mjr 5:a70c0bce770d 317 // LedWiz emulation
mjr 5:a70c0bce770d 318 //
mjr 5:a70c0bce770d 319
mjr 0:5acbbe3f4cf4 320 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 321
mjr 6:cc35eb643e8f 322 // LedWiz output pin interface. We create a cover class to virtualize
mjr 6:cc35eb643e8f 323 // digital vs PWM outputs and give them a common interface. The KL25Z
mjr 6:cc35eb643e8f 324 // unfortunately doesn't have enough hardware PWM channels to support
mjr 6:cc35eb643e8f 325 // PWM on all 32 LedWiz outputs, so we provide as many PWM channels as
mjr 6:cc35eb643e8f 326 // we can (10), and fill out the rest of the outputs with plain digital
mjr 6:cc35eb643e8f 327 // outs.
mjr 6:cc35eb643e8f 328 class LwOut
mjr 6:cc35eb643e8f 329 {
mjr 6:cc35eb643e8f 330 public:
mjr 6:cc35eb643e8f 331 virtual void set(float val) = 0;
mjr 6:cc35eb643e8f 332 };
mjr 6:cc35eb643e8f 333 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 334 {
mjr 6:cc35eb643e8f 335 public:
mjr 13:72dda449c3c0 336 LwPwmOut(PinName pin) : p(pin) { prv = -1; }
mjr 13:72dda449c3c0 337 virtual void set(float val)
mjr 13:72dda449c3c0 338 {
mjr 13:72dda449c3c0 339 if (val != prv)
mjr 13:72dda449c3c0 340 p.write(prv = val);
mjr 13:72dda449c3c0 341 }
mjr 6:cc35eb643e8f 342 PwmOut p;
mjr 13:72dda449c3c0 343 float prv;
mjr 6:cc35eb643e8f 344 };
mjr 6:cc35eb643e8f 345 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 346 {
mjr 6:cc35eb643e8f 347 public:
mjr 13:72dda449c3c0 348 LwDigOut(PinName pin) : p(pin) { prv = -1; }
mjr 13:72dda449c3c0 349 virtual void set(float val)
mjr 13:72dda449c3c0 350 {
mjr 13:72dda449c3c0 351 if (val != prv)
mjr 13:72dda449c3c0 352 p.write((prv = val) == 0.0 ? 0 : 1);
mjr 13:72dda449c3c0 353 }
mjr 6:cc35eb643e8f 354 DigitalOut p;
mjr 13:72dda449c3c0 355 float prv;
mjr 6:cc35eb643e8f 356 };
mjr 11:bd9da7088e6e 357 class LwUnusedOut: public LwOut
mjr 11:bd9da7088e6e 358 {
mjr 11:bd9da7088e6e 359 public:
mjr 11:bd9da7088e6e 360 LwUnusedOut() { }
mjr 11:bd9da7088e6e 361 virtual void set(float val) { }
mjr 11:bd9da7088e6e 362 };
mjr 6:cc35eb643e8f 363
mjr 6:cc35eb643e8f 364 // output pin array
mjr 6:cc35eb643e8f 365 static LwOut *lwPin[32];
mjr 6:cc35eb643e8f 366
mjr 6:cc35eb643e8f 367 // initialize the output pin array
mjr 6:cc35eb643e8f 368 void initLwOut()
mjr 6:cc35eb643e8f 369 {
mjr 9:fd65b0a94720 370 for (int i = 0 ; i < countof(lwPin) ; ++i)
mjr 6:cc35eb643e8f 371 {
mjr 11:bd9da7088e6e 372 PinName p = (i < countof(ledWizPortMap) ? ledWizPortMap[i].pin : NC);
mjr 11:bd9da7088e6e 373 if (p == NC)
mjr 11:bd9da7088e6e 374 lwPin[i] = new LwUnusedOut();
mjr 11:bd9da7088e6e 375 else if (ledWizPortMap[i].isPWM)
mjr 11:bd9da7088e6e 376 lwPin[i] = new LwPwmOut(p);
mjr 11:bd9da7088e6e 377 else
mjr 11:bd9da7088e6e 378 lwPin[i] = new LwDigOut(p);
mjr 6:cc35eb643e8f 379 }
mjr 6:cc35eb643e8f 380 }
mjr 6:cc35eb643e8f 381
mjr 0:5acbbe3f4cf4 382 // on/off state for each LedWiz output
mjr 1:d913e0afb2ac 383 static uint8_t wizOn[32];
mjr 0:5acbbe3f4cf4 384
mjr 0:5acbbe3f4cf4 385 // profile (brightness/blink) state for each LedWiz output
mjr 1:d913e0afb2ac 386 static uint8_t wizVal[32] = {
mjr 13:72dda449c3c0 387 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 388 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 389 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 390 48, 48, 48, 48, 48, 48, 48, 48
mjr 0:5acbbe3f4cf4 391 };
mjr 0:5acbbe3f4cf4 392
mjr 1:d913e0afb2ac 393 static float wizState(int idx)
mjr 0:5acbbe3f4cf4 394 {
mjr 13:72dda449c3c0 395 if (wizOn[idx])
mjr 13:72dda449c3c0 396 {
mjr 0:5acbbe3f4cf4 397 // on - map profile brightness state to PWM level
mjr 1:d913e0afb2ac 398 uint8_t val = wizVal[idx];
mjr 13:72dda449c3c0 399 if (val <= 48)
mjr 13:72dda449c3c0 400 {
mjr 15:944bbc29c4dd 401 // PWM brightness/intensity level. Rescale from the LedWiz
mjr 15:944bbc29c4dd 402 // 0..48 integer range to our internal PwmOut 0..1 float range.
mjr 15:944bbc29c4dd 403 // Note that on the actual LedWiz, level 48 is actually about
mjr 15:944bbc29c4dd 404 // 98% on - contrary to the LedWiz documentation, level 49 is
mjr 15:944bbc29c4dd 405 // the true 100% level. (In the documentation, level 49 is
mjr 15:944bbc29c4dd 406 // simply not a valid setting.) Even so, we treat level 48 as
mjr 15:944bbc29c4dd 407 // 100% on to match the documentation. This won't be perfectly
mjr 15:944bbc29c4dd 408 // ocmpatible with the actual LedWiz, but it makes for such a
mjr 15:944bbc29c4dd 409 // small difference in brightness (if the output device is an
mjr 15:944bbc29c4dd 410 // LED, say) that no one should notice. It seems better to
mjr 15:944bbc29c4dd 411 // err in this direction, because while the difference in
mjr 15:944bbc29c4dd 412 // brightness when attached to an LED won't be noticeable, the
mjr 15:944bbc29c4dd 413 // difference in duty cycle when attached to something like a
mjr 15:944bbc29c4dd 414 // contactor *can* be noticeable - anything less than 100%
mjr 15:944bbc29c4dd 415 // can cause a contactor or relay to chatter. There's almost
mjr 15:944bbc29c4dd 416 // never a situation where you'd want values other than 0% and
mjr 15:944bbc29c4dd 417 // 100% for a contactor or relay, so treating level 48 as 100%
mjr 15:944bbc29c4dd 418 // makes us work properly with software that's expecting the
mjr 15:944bbc29c4dd 419 // documented LedWiz behavior and therefore uses level 48 to
mjr 15:944bbc29c4dd 420 // turn a contactor or relay fully on.
mjr 13:72dda449c3c0 421 return val/48.0;
mjr 13:72dda449c3c0 422 }
mjr 13:72dda449c3c0 423 else if (val == 49)
mjr 13:72dda449c3c0 424 {
mjr 15:944bbc29c4dd 425 // 49 is undefined in the LedWiz documentation, but actually
mjr 15:944bbc29c4dd 426 // means 100% on. The documentation says that levels 1-48 are
mjr 15:944bbc29c4dd 427 // the full PWM range, but empirically it appears that the real
mjr 15:944bbc29c4dd 428 // range implemented in the firmware is 1-49. Some software on
mjr 15:944bbc29c4dd 429 // the PC side (notably DOF) is aware of this and uses level 49
mjr 15:944bbc29c4dd 430 // to mean "100% on". To ensure compatibility with existing
mjr 15:944bbc29c4dd 431 // PC-side software, we need to recognize level 49.
mjr 13:72dda449c3c0 432 return 1.0;
mjr 13:72dda449c3c0 433 }
mjr 0:5acbbe3f4cf4 434 else if (val >= 129 && val <= 132)
mjr 13:72dda449c3c0 435 {
mjr 13:72dda449c3c0 436 // Values of 129-132 select different flashing modes. We don't
mjr 13:72dda449c3c0 437 // support any of these. Instead, simply treat them as fully on.
mjr 13:72dda449c3c0 438 // Note that DOF doesn't ever use modes 129-132, as it implements
mjr 13:72dda449c3c0 439 // all flashing modes itself on the host side, so this limitation
mjr 13:72dda449c3c0 440 // won't have any effect on DOF users. You can observe it using
mjr 13:72dda449c3c0 441 // LedBlinky, though.
mjr 13:72dda449c3c0 442 return 1.0;
mjr 13:72dda449c3c0 443 }
mjr 0:5acbbe3f4cf4 444 else
mjr 13:72dda449c3c0 445 {
mjr 13:72dda449c3c0 446 // Other values are undefined in the LedWiz documentation. Hosts
mjr 13:72dda449c3c0 447 // *should* never send undefined values, since whatever behavior an
mjr 13:72dda449c3c0 448 // LedWiz unit exhibits in response is accidental and could change
mjr 13:72dda449c3c0 449 // in a future version. We'll treat all undefined values as equivalent
mjr 13:72dda449c3c0 450 // to 48 (fully on).
mjr 13:72dda449c3c0 451 //
mjr 13:72dda449c3c0 452 // NB: the 49 and 129-132 cases are broken out above for the sake
mjr 13:72dda449c3c0 453 // of documentation. We end up using 1.0 as the return value for
mjr 13:72dda449c3c0 454 // everything outside of the defined 0-48 range, so we could collapse
mjr 13:72dda449c3c0 455 // this whole thing to a single 'else' branch, but I wanted to call
mjr 13:72dda449c3c0 456 // out the specific reasons for handling the settings above as we do.
mjr 0:5acbbe3f4cf4 457 return 1.0;
mjr 13:72dda449c3c0 458 }
mjr 0:5acbbe3f4cf4 459 }
mjr 13:72dda449c3c0 460 else
mjr 13:72dda449c3c0 461 {
mjr 13:72dda449c3c0 462 // off - show at 0 intensity
mjr 13:72dda449c3c0 463 return 0.0;
mjr 0:5acbbe3f4cf4 464 }
mjr 0:5acbbe3f4cf4 465 }
mjr 0:5acbbe3f4cf4 466
mjr 1:d913e0afb2ac 467 static void updateWizOuts()
mjr 1:d913e0afb2ac 468 {
mjr 6:cc35eb643e8f 469 for (int i = 0 ; i < 32 ; ++i)
mjr 6:cc35eb643e8f 470 lwPin[i]->set(wizState(i));
mjr 1:d913e0afb2ac 471 }
mjr 1:d913e0afb2ac 472
mjr 11:bd9da7088e6e 473
mjr 11:bd9da7088e6e 474 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 475 //
mjr 11:bd9da7088e6e 476 // Button input
mjr 11:bd9da7088e6e 477 //
mjr 11:bd9da7088e6e 478
mjr 11:bd9da7088e6e 479 // button input map array
mjr 11:bd9da7088e6e 480 DigitalIn *buttonDigIn[32];
mjr 11:bd9da7088e6e 481
mjr 18:5e890ebd0023 482 // button state
mjr 18:5e890ebd0023 483 struct ButtonState
mjr 18:5e890ebd0023 484 {
mjr 18:5e890ebd0023 485 // current on/off state
mjr 18:5e890ebd0023 486 int pressed;
mjr 18:5e890ebd0023 487
mjr 18:5e890ebd0023 488 // Sticky time remaining for current state. When a
mjr 18:5e890ebd0023 489 // state transition occurs, we set this to a debounce
mjr 18:5e890ebd0023 490 // period. Future state transitions will be ignored
mjr 18:5e890ebd0023 491 // until the debounce time elapses.
mjr 18:5e890ebd0023 492 int t;
mjr 18:5e890ebd0023 493 } buttonState[32];
mjr 18:5e890ebd0023 494
mjr 12:669df364a565 495 // timer for button reports
mjr 12:669df364a565 496 static Timer buttonTimer;
mjr 12:669df364a565 497
mjr 11:bd9da7088e6e 498 // initialize the button inputs
mjr 11:bd9da7088e6e 499 void initButtons()
mjr 11:bd9da7088e6e 500 {
mjr 11:bd9da7088e6e 501 // create the digital inputs
mjr 11:bd9da7088e6e 502 for (int i = 0 ; i < countof(buttonDigIn) ; ++i)
mjr 11:bd9da7088e6e 503 {
mjr 11:bd9da7088e6e 504 if (i < countof(buttonMap) && buttonMap[i] != NC)
mjr 11:bd9da7088e6e 505 buttonDigIn[i] = new DigitalIn(buttonMap[i]);
mjr 11:bd9da7088e6e 506 else
mjr 11:bd9da7088e6e 507 buttonDigIn[i] = 0;
mjr 11:bd9da7088e6e 508 }
mjr 12:669df364a565 509
mjr 12:669df364a565 510 // start the button timer
mjr 12:669df364a565 511 buttonTimer.start();
mjr 11:bd9da7088e6e 512 }
mjr 11:bd9da7088e6e 513
mjr 11:bd9da7088e6e 514
mjr 18:5e890ebd0023 515 // read the button input state
mjr 18:5e890ebd0023 516 uint32_t readButtons()
mjr 11:bd9da7088e6e 517 {
mjr 11:bd9da7088e6e 518 // start with all buttons off
mjr 11:bd9da7088e6e 519 uint32_t buttons = 0;
mjr 11:bd9da7088e6e 520
mjr 18:5e890ebd0023 521 // figure the time elapsed since the last scan
mjr 18:5e890ebd0023 522 int dt = buttonTimer.read_ms();
mjr 18:5e890ebd0023 523
mjr 18:5e890ebd0023 524 // reset the timef for the next scan
mjr 18:5e890ebd0023 525 buttonTimer.reset();
mjr 18:5e890ebd0023 526
mjr 11:bd9da7088e6e 527 // scan the button list
mjr 11:bd9da7088e6e 528 uint32_t bit = 1;
mjr 18:5e890ebd0023 529 DigitalIn **di = buttonDigIn;
mjr 18:5e890ebd0023 530 ButtonState *bs = buttonState;
mjr 18:5e890ebd0023 531 for (int i = 0 ; i < countof(buttonDigIn) ; ++i, ++di, ++bs, bit <<= 1)
mjr 11:bd9da7088e6e 532 {
mjr 18:5e890ebd0023 533 // read this button
mjr 18:5e890ebd0023 534 if (*di != 0)
mjr 18:5e890ebd0023 535 {
mjr 18:5e890ebd0023 536 // deduct the elapsed time since the last update
mjr 18:5e890ebd0023 537 // from the button's remaining sticky time
mjr 18:5e890ebd0023 538 bs->t -= dt;
mjr 18:5e890ebd0023 539 if (bs->t < 0)
mjr 18:5e890ebd0023 540 bs->t = 0;
mjr 18:5e890ebd0023 541
mjr 18:5e890ebd0023 542 // If the sticky time has elapsed, note the new physical
mjr 18:5e890ebd0023 543 // state of the button. If we still have sticky time
mjr 18:5e890ebd0023 544 // remaining, ignore the physical state; the last state
mjr 18:5e890ebd0023 545 // change persists until the sticky time elapses so that
mjr 18:5e890ebd0023 546 // we smooth out any "bounce" (electrical transients that
mjr 18:5e890ebd0023 547 // occur when the switch contact is opened or closed).
mjr 18:5e890ebd0023 548 if (bs->t == 0)
mjr 18:5e890ebd0023 549 {
mjr 18:5e890ebd0023 550 // get the new physical state
mjr 18:5e890ebd0023 551 int pressed = !(*di)->read();
mjr 18:5e890ebd0023 552
mjr 18:5e890ebd0023 553 // update the button's logical state if this is a change
mjr 18:5e890ebd0023 554 if (pressed != bs->pressed)
mjr 18:5e890ebd0023 555 {
mjr 18:5e890ebd0023 556 // store the new state
mjr 18:5e890ebd0023 557 bs->pressed = pressed;
mjr 18:5e890ebd0023 558
mjr 18:5e890ebd0023 559 // start a new sticky period for debouncing this
mjr 18:5e890ebd0023 560 // state change
mjr 19:054f8af32fce 561 bs->t = 25;
mjr 18:5e890ebd0023 562 }
mjr 18:5e890ebd0023 563 }
mjr 18:5e890ebd0023 564
mjr 18:5e890ebd0023 565 // if it's pressed, OR its bit into the state
mjr 18:5e890ebd0023 566 if (bs->pressed)
mjr 18:5e890ebd0023 567 buttons |= bit;
mjr 18:5e890ebd0023 568 }
mjr 11:bd9da7088e6e 569 }
mjr 11:bd9da7088e6e 570
mjr 18:5e890ebd0023 571 // return the new button list
mjr 11:bd9da7088e6e 572 return buttons;
mjr 11:bd9da7088e6e 573 }
mjr 11:bd9da7088e6e 574
mjr 5:a70c0bce770d 575 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 576 //
mjr 5:a70c0bce770d 577 // Customization joystick subbclass
mjr 5:a70c0bce770d 578 //
mjr 5:a70c0bce770d 579
mjr 5:a70c0bce770d 580 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 581 {
mjr 5:a70c0bce770d 582 public:
mjr 5:a70c0bce770d 583 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
mjr 5:a70c0bce770d 584 : USBJoystick(vendor_id, product_id, product_release, true)
mjr 5:a70c0bce770d 585 {
mjr 5:a70c0bce770d 586 suspended_ = false;
mjr 5:a70c0bce770d 587 }
mjr 5:a70c0bce770d 588
mjr 5:a70c0bce770d 589 // are we connected?
mjr 5:a70c0bce770d 590 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 591
mjr 5:a70c0bce770d 592 // Are we in suspend mode?
mjr 5:a70c0bce770d 593 int isSuspended() const { return suspended_; }
mjr 5:a70c0bce770d 594
mjr 5:a70c0bce770d 595 protected:
mjr 5:a70c0bce770d 596 virtual void suspendStateChanged(unsigned int suspended)
mjr 5:a70c0bce770d 597 { suspended_ = suspended; }
mjr 5:a70c0bce770d 598
mjr 5:a70c0bce770d 599 // are we suspended?
mjr 5:a70c0bce770d 600 int suspended_;
mjr 5:a70c0bce770d 601 };
mjr 5:a70c0bce770d 602
mjr 5:a70c0bce770d 603 // ---------------------------------------------------------------------------
mjr 6:cc35eb643e8f 604 //
mjr 6:cc35eb643e8f 605 // Some simple math service routines
mjr 6:cc35eb643e8f 606 //
mjr 6:cc35eb643e8f 607
mjr 6:cc35eb643e8f 608 inline float square(float x) { return x*x; }
mjr 6:cc35eb643e8f 609 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 6:cc35eb643e8f 610
mjr 6:cc35eb643e8f 611 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 612 //
mjr 5:a70c0bce770d 613 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 614 //
mjr 5:a70c0bce770d 615
mjr 5:a70c0bce770d 616 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 617 //
mjr 5:a70c0bce770d 618 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 619 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 620 // automatic calibration.
mjr 5:a70c0bce770d 621 //
mjr 5:a70c0bce770d 622 // We install an interrupt handler on the accelerometer "data ready"
mjr 6:cc35eb643e8f 623 // interrupt to ensure that we fetch each sample immediately when it
mjr 6:cc35eb643e8f 624 // becomes available. The accelerometer data rate is fiarly high
mjr 6:cc35eb643e8f 625 // (800 Hz), so it's not practical to keep up with it by polling.
mjr 6:cc35eb643e8f 626 // Using an interrupt handler lets us respond quickly and read
mjr 6:cc35eb643e8f 627 // every sample.
mjr 5:a70c0bce770d 628 //
mjr 6:cc35eb643e8f 629 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 630 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 631 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 632 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 633 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 634 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 635 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 636 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 637 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 638 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 639 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 640 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 641 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 642 // of nudging, say).
mjr 5:a70c0bce770d 643 //
mjr 5:a70c0bce770d 644
mjr 17:ab3cec0c8bf4 645 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 646 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 647
mjr 17:ab3cec0c8bf4 648 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 649 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 650 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 651
mjr 17:ab3cec0c8bf4 652 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 653 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 654 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 655 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 656
mjr 17:ab3cec0c8bf4 657
mjr 6:cc35eb643e8f 658 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 659 struct AccHist
mjr 5:a70c0bce770d 660 {
mjr 6:cc35eb643e8f 661 AccHist() { x = y = d = 0.0; xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 662 void set(float x, float y, AccHist *prv)
mjr 6:cc35eb643e8f 663 {
mjr 6:cc35eb643e8f 664 // save the raw position
mjr 6:cc35eb643e8f 665 this->x = x;
mjr 6:cc35eb643e8f 666 this->y = y;
mjr 6:cc35eb643e8f 667 this->d = distance(prv);
mjr 6:cc35eb643e8f 668 }
mjr 6:cc35eb643e8f 669
mjr 6:cc35eb643e8f 670 // reading for this entry
mjr 5:a70c0bce770d 671 float x, y;
mjr 5:a70c0bce770d 672
mjr 6:cc35eb643e8f 673 // distance from previous entry
mjr 6:cc35eb643e8f 674 float d;
mjr 5:a70c0bce770d 675
mjr 6:cc35eb643e8f 676 // total and count of samples averaged over this period
mjr 6:cc35eb643e8f 677 float xtot, ytot;
mjr 6:cc35eb643e8f 678 int cnt;
mjr 6:cc35eb643e8f 679
mjr 6:cc35eb643e8f 680 void clearAvg() { xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 681 void addAvg(float x, float y) { xtot += x; ytot += y; ++cnt; }
mjr 6:cc35eb643e8f 682 float xAvg() const { return xtot/cnt; }
mjr 6:cc35eb643e8f 683 float yAvg() const { return ytot/cnt; }
mjr 5:a70c0bce770d 684
mjr 6:cc35eb643e8f 685 float distance(AccHist *p)
mjr 6:cc35eb643e8f 686 { return sqrt(square(p->x - x) + square(p->y - y)); }
mjr 5:a70c0bce770d 687 };
mjr 5:a70c0bce770d 688
mjr 5:a70c0bce770d 689 // accelerometer wrapper class
mjr 3:3514575d4f86 690 class Accel
mjr 3:3514575d4f86 691 {
mjr 3:3514575d4f86 692 public:
mjr 3:3514575d4f86 693 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin)
mjr 3:3514575d4f86 694 : mma_(sda, scl, i2cAddr), intIn_(irqPin)
mjr 3:3514575d4f86 695 {
mjr 5:a70c0bce770d 696 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 697 irqPin_ = irqPin;
mjr 5:a70c0bce770d 698
mjr 5:a70c0bce770d 699 // reset and initialize
mjr 5:a70c0bce770d 700 reset();
mjr 5:a70c0bce770d 701 }
mjr 5:a70c0bce770d 702
mjr 5:a70c0bce770d 703 void reset()
mjr 5:a70c0bce770d 704 {
mjr 6:cc35eb643e8f 705 // clear the center point
mjr 6:cc35eb643e8f 706 cx_ = cy_ = 0.0;
mjr 6:cc35eb643e8f 707
mjr 6:cc35eb643e8f 708 // start the calibration timer
mjr 5:a70c0bce770d 709 tCenter_.start();
mjr 5:a70c0bce770d 710 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 711
mjr 5:a70c0bce770d 712 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 713 mma_.init();
mjr 6:cc35eb643e8f 714
mjr 6:cc35eb643e8f 715 // set the initial integrated velocity reading to zero
mjr 6:cc35eb643e8f 716 vx_ = vy_ = 0;
mjr 3:3514575d4f86 717
mjr 6:cc35eb643e8f 718 // set up our accelerometer interrupt handling
mjr 6:cc35eb643e8f 719 intIn_.rise(this, &Accel::isr);
mjr 5:a70c0bce770d 720 mma_.setInterruptMode(irqPin_ == PTA14 ? 1 : 2);
mjr 3:3514575d4f86 721
mjr 3:3514575d4f86 722 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 723 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 724
mjr 3:3514575d4f86 725 // start our timers
mjr 3:3514575d4f86 726 tGet_.start();
mjr 3:3514575d4f86 727 tInt_.start();
mjr 3:3514575d4f86 728 }
mjr 3:3514575d4f86 729
mjr 9:fd65b0a94720 730 void get(int &x, int &y)
mjr 3:3514575d4f86 731 {
mjr 3:3514575d4f86 732 // disable interrupts while manipulating the shared data
mjr 3:3514575d4f86 733 __disable_irq();
mjr 3:3514575d4f86 734
mjr 3:3514575d4f86 735 // read the shared data and store locally for calculations
mjr 6:cc35eb643e8f 736 float ax = ax_, ay = ay_;
mjr 6:cc35eb643e8f 737 float vx = vx_, vy = vy_;
mjr 5:a70c0bce770d 738
mjr 6:cc35eb643e8f 739 // reset the velocity sum for the next run
mjr 6:cc35eb643e8f 740 vx_ = vy_ = 0;
mjr 3:3514575d4f86 741
mjr 3:3514575d4f86 742 // get the time since the last get() sample
mjr 3:3514575d4f86 743 float dt = tGet_.read_us()/1.0e6;
mjr 3:3514575d4f86 744 tGet_.reset();
mjr 3:3514575d4f86 745
mjr 3:3514575d4f86 746 // done manipulating the shared data
mjr 3:3514575d4f86 747 __enable_irq();
mjr 3:3514575d4f86 748
mjr 6:cc35eb643e8f 749 // adjust the readings for the integration time
mjr 6:cc35eb643e8f 750 vx /= dt;
mjr 6:cc35eb643e8f 751 vy /= dt;
mjr 6:cc35eb643e8f 752
mjr 6:cc35eb643e8f 753 // add this sample to the current calibration interval's running total
mjr 6:cc35eb643e8f 754 AccHist *p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 755 p->addAvg(ax, ay);
mjr 6:cc35eb643e8f 756
mjr 5:a70c0bce770d 757 // check for auto-centering every so often
mjr 5:a70c0bce770d 758 if (tCenter_.read_ms() > 1000)
mjr 5:a70c0bce770d 759 {
mjr 5:a70c0bce770d 760 // add the latest raw sample to the history list
mjr 6:cc35eb643e8f 761 AccHist *prv = p;
mjr 5:a70c0bce770d 762 iAccPrv_ = (iAccPrv_ + 1) % maxAccPrv;
mjr 6:cc35eb643e8f 763 p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 764 p->set(ax, ay, prv);
mjr 5:a70c0bce770d 765
mjr 5:a70c0bce770d 766 // if we have a full complement, check for stability
mjr 5:a70c0bce770d 767 if (nAccPrv_ >= maxAccPrv)
mjr 5:a70c0bce770d 768 {
mjr 5:a70c0bce770d 769 // check if we've been stable for all recent samples
mjr 6:cc35eb643e8f 770 static const float accTol = .01;
mjr 6:cc35eb643e8f 771 AccHist *p0 = accPrv_;
mjr 6:cc35eb643e8f 772 if (p0[0].d < accTol
mjr 6:cc35eb643e8f 773 && p0[1].d < accTol
mjr 6:cc35eb643e8f 774 && p0[2].d < accTol
mjr 6:cc35eb643e8f 775 && p0[3].d < accTol
mjr 6:cc35eb643e8f 776 && p0[4].d < accTol)
mjr 5:a70c0bce770d 777 {
mjr 6:cc35eb643e8f 778 // Figure the new calibration point as the average of
mjr 6:cc35eb643e8f 779 // the samples over the rest period
mjr 6:cc35eb643e8f 780 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5.0;
mjr 6:cc35eb643e8f 781 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5.0;
mjr 5:a70c0bce770d 782 }
mjr 5:a70c0bce770d 783 }
mjr 5:a70c0bce770d 784 else
mjr 5:a70c0bce770d 785 {
mjr 5:a70c0bce770d 786 // not enough samples yet; just up the count
mjr 5:a70c0bce770d 787 ++nAccPrv_;
mjr 5:a70c0bce770d 788 }
mjr 6:cc35eb643e8f 789
mjr 6:cc35eb643e8f 790 // clear the new item's running totals
mjr 6:cc35eb643e8f 791 p->clearAvg();
mjr 5:a70c0bce770d 792
mjr 5:a70c0bce770d 793 // reset the timer
mjr 5:a70c0bce770d 794 tCenter_.reset();
mjr 5:a70c0bce770d 795 }
mjr 5:a70c0bce770d 796
mjr 6:cc35eb643e8f 797 // report our integrated velocity reading in x,y
mjr 6:cc35eb643e8f 798 x = rawToReport(vx);
mjr 6:cc35eb643e8f 799 y = rawToReport(vy);
mjr 5:a70c0bce770d 800
mjr 6:cc35eb643e8f 801 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 802 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 803 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 804 #endif
mjr 3:3514575d4f86 805 }
mjr 3:3514575d4f86 806
mjr 3:3514575d4f86 807 private:
mjr 6:cc35eb643e8f 808 // adjust a raw acceleration figure to a usb report value
mjr 6:cc35eb643e8f 809 int rawToReport(float v)
mjr 5:a70c0bce770d 810 {
mjr 6:cc35eb643e8f 811 // scale to the joystick report range and round to integer
mjr 6:cc35eb643e8f 812 int i = int(round(v*JOYMAX));
mjr 5:a70c0bce770d 813
mjr 6:cc35eb643e8f 814 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 815 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 816 static const int filter[] = {
mjr 6:cc35eb643e8f 817 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 818 0,
mjr 6:cc35eb643e8f 819 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 820 };
mjr 6:cc35eb643e8f 821 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 822 }
mjr 5:a70c0bce770d 823
mjr 3:3514575d4f86 824 // interrupt handler
mjr 3:3514575d4f86 825 void isr()
mjr 3:3514575d4f86 826 {
mjr 3:3514575d4f86 827 // Read the axes. Note that we have to read all three axes
mjr 3:3514575d4f86 828 // (even though we only really use x and y) in order to clear
mjr 3:3514575d4f86 829 // the "data ready" status bit in the accelerometer. The
mjr 3:3514575d4f86 830 // interrupt only occurs when the "ready" bit transitions from
mjr 3:3514575d4f86 831 // off to on, so we have to make sure it's off.
mjr 5:a70c0bce770d 832 float x, y, z;
mjr 5:a70c0bce770d 833 mma_.getAccXYZ(x, y, z);
mjr 3:3514575d4f86 834
mjr 3:3514575d4f86 835 // calculate the time since the last interrupt
mjr 3:3514575d4f86 836 float dt = tInt_.read_us()/1.0e6;
mjr 3:3514575d4f86 837 tInt_.reset();
mjr 6:cc35eb643e8f 838
mjr 6:cc35eb643e8f 839 // integrate the time slice from the previous reading to this reading
mjr 6:cc35eb643e8f 840 vx_ += (x + ax_ - 2*cx_)*dt/2;
mjr 6:cc35eb643e8f 841 vy_ += (y + ay_ - 2*cy_)*dt/2;
mjr 3:3514575d4f86 842
mjr 6:cc35eb643e8f 843 // store the updates
mjr 6:cc35eb643e8f 844 ax_ = x;
mjr 6:cc35eb643e8f 845 ay_ = y;
mjr 6:cc35eb643e8f 846 az_ = z;
mjr 3:3514575d4f86 847 }
mjr 3:3514575d4f86 848
mjr 3:3514575d4f86 849 // underlying accelerometer object
mjr 3:3514575d4f86 850 MMA8451Q mma_;
mjr 3:3514575d4f86 851
mjr 5:a70c0bce770d 852 // last raw acceleration readings
mjr 6:cc35eb643e8f 853 float ax_, ay_, az_;
mjr 5:a70c0bce770d 854
mjr 6:cc35eb643e8f 855 // integrated velocity reading since last get()
mjr 6:cc35eb643e8f 856 float vx_, vy_;
mjr 6:cc35eb643e8f 857
mjr 3:3514575d4f86 858 // timer for measuring time between get() samples
mjr 3:3514575d4f86 859 Timer tGet_;
mjr 3:3514575d4f86 860
mjr 3:3514575d4f86 861 // timer for measuring time between interrupts
mjr 3:3514575d4f86 862 Timer tInt_;
mjr 5:a70c0bce770d 863
mjr 6:cc35eb643e8f 864 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 865 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 866 // at rest.
mjr 6:cc35eb643e8f 867 float cx_, cy_;
mjr 5:a70c0bce770d 868
mjr 5:a70c0bce770d 869 // timer for atuo-centering
mjr 5:a70c0bce770d 870 Timer tCenter_;
mjr 6:cc35eb643e8f 871
mjr 6:cc35eb643e8f 872 // Auto-centering history. This is a separate history list that
mjr 6:cc35eb643e8f 873 // records results spaced out sparesely over time, so that we can
mjr 6:cc35eb643e8f 874 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 875 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 876 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 877 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 878 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 879 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 880 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 881 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 882 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 883 // accelerometer measurement bias (e.g., from temperature changes).
mjr 5:a70c0bce770d 884 int iAccPrv_, nAccPrv_;
mjr 5:a70c0bce770d 885 static const int maxAccPrv = 5;
mjr 6:cc35eb643e8f 886 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 887
mjr 5:a70c0bce770d 888 // interurupt pin name
mjr 5:a70c0bce770d 889 PinName irqPin_;
mjr 5:a70c0bce770d 890
mjr 5:a70c0bce770d 891 // interrupt router
mjr 5:a70c0bce770d 892 InterruptIn intIn_;
mjr 3:3514575d4f86 893 };
mjr 3:3514575d4f86 894
mjr 5:a70c0bce770d 895
mjr 5:a70c0bce770d 896 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 897 //
mjr 14:df700b22ca08 898 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 899 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 900 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 901 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 902 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 903 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 904 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 905 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 906 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 907 //
mjr 14:df700b22ca08 908 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 909 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 910 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 911 //
mjr 5:a70c0bce770d 912 void clear_i2c()
mjr 5:a70c0bce770d 913 {
mjr 5:a70c0bce770d 914 // assume a general-purpose output pin to the I2C clock
mjr 5:a70c0bce770d 915 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 916 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 917
mjr 5:a70c0bce770d 918 // clock the SCL 9 times
mjr 5:a70c0bce770d 919 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 920 {
mjr 5:a70c0bce770d 921 scl = 1;
mjr 5:a70c0bce770d 922 wait_us(20);
mjr 5:a70c0bce770d 923 scl = 0;
mjr 5:a70c0bce770d 924 wait_us(20);
mjr 5:a70c0bce770d 925 }
mjr 5:a70c0bce770d 926 }
mjr 14:df700b22ca08 927
mjr 14:df700b22ca08 928 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 929 //
mjr 17:ab3cec0c8bf4 930 // Include the appropriate plunger sensor definition. This will define a
mjr 17:ab3cec0c8bf4 931 // class called PlungerSensor, with a standard interface that we use in
mjr 17:ab3cec0c8bf4 932 // the main loop below. This is *kind of* like a virtual class interface,
mjr 17:ab3cec0c8bf4 933 // but it actually defines the methods statically, which is a little more
mjr 17:ab3cec0c8bf4 934 // efficient at run-time. There's no need for a true virtual interface
mjr 17:ab3cec0c8bf4 935 // because we don't need to be able to change sensor types on the fly.
mjr 17:ab3cec0c8bf4 936 //
mjr 17:ab3cec0c8bf4 937
mjr 22:71422c359f2a 938 #if defined(ENABLE_CCD_SENSOR)
mjr 17:ab3cec0c8bf4 939 #include "ccdSensor.h"
mjr 22:71422c359f2a 940 #elif defined(ENABLE_POT_SENSOR)
mjr 17:ab3cec0c8bf4 941 #include "potSensor.h"
mjr 17:ab3cec0c8bf4 942 #else
mjr 17:ab3cec0c8bf4 943 #include "nullSensor.h"
mjr 17:ab3cec0c8bf4 944 #endif
mjr 17:ab3cec0c8bf4 945
mjr 17:ab3cec0c8bf4 946
mjr 17:ab3cec0c8bf4 947 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 948 //
mjr 17:ab3cec0c8bf4 949 // Non-volatile memory (NVM)
mjr 17:ab3cec0c8bf4 950 //
mjr 17:ab3cec0c8bf4 951
mjr 17:ab3cec0c8bf4 952 // Structure defining our NVM storage layout. We store a small
mjr 17:ab3cec0c8bf4 953 // amount of persistent data in flash memory to retain calibration
mjr 17:ab3cec0c8bf4 954 // data when powered off.
mjr 17:ab3cec0c8bf4 955 struct NVM
mjr 17:ab3cec0c8bf4 956 {
mjr 17:ab3cec0c8bf4 957 // checksum - we use this to determine if the flash record
mjr 17:ab3cec0c8bf4 958 // has been properly initialized
mjr 17:ab3cec0c8bf4 959 uint32_t checksum;
mjr 17:ab3cec0c8bf4 960
mjr 17:ab3cec0c8bf4 961 // signature value
mjr 17:ab3cec0c8bf4 962 static const uint32_t SIGNATURE = 0x4D4A522A;
mjr 17:ab3cec0c8bf4 963 static const uint16_t VERSION = 0x0003;
mjr 17:ab3cec0c8bf4 964
mjr 17:ab3cec0c8bf4 965 // Is the data structure valid? We test the signature and
mjr 17:ab3cec0c8bf4 966 // checksum to determine if we've been properly stored.
mjr 17:ab3cec0c8bf4 967 int valid() const
mjr 17:ab3cec0c8bf4 968 {
mjr 17:ab3cec0c8bf4 969 return (d.sig == SIGNATURE
mjr 17:ab3cec0c8bf4 970 && d.vsn == VERSION
mjr 17:ab3cec0c8bf4 971 && d.sz == sizeof(NVM)
mjr 17:ab3cec0c8bf4 972 && checksum == CRC32(&d, sizeof(d)));
mjr 17:ab3cec0c8bf4 973 }
mjr 17:ab3cec0c8bf4 974
mjr 17:ab3cec0c8bf4 975 // save to non-volatile memory
mjr 17:ab3cec0c8bf4 976 void save(FreescaleIAP &iap, int addr)
mjr 17:ab3cec0c8bf4 977 {
mjr 17:ab3cec0c8bf4 978 // update the checksum and structure size
mjr 17:ab3cec0c8bf4 979 checksum = CRC32(&d, sizeof(d));
mjr 17:ab3cec0c8bf4 980 d.sz = sizeof(NVM);
mjr 17:ab3cec0c8bf4 981
mjr 17:ab3cec0c8bf4 982 // erase the sector
mjr 17:ab3cec0c8bf4 983 iap.erase_sector(addr);
mjr 17:ab3cec0c8bf4 984
mjr 17:ab3cec0c8bf4 985 // save the data
mjr 17:ab3cec0c8bf4 986 iap.program_flash(addr, this, sizeof(*this));
mjr 17:ab3cec0c8bf4 987 }
mjr 17:ab3cec0c8bf4 988
mjr 17:ab3cec0c8bf4 989 // reset calibration data for calibration mode
mjr 17:ab3cec0c8bf4 990 void resetPlunger()
mjr 17:ab3cec0c8bf4 991 {
mjr 17:ab3cec0c8bf4 992 // set extremes for the calibration data
mjr 17:ab3cec0c8bf4 993 d.plungerMax = 0;
mjr 17:ab3cec0c8bf4 994 d.plungerZero = npix;
mjr 17:ab3cec0c8bf4 995 d.plungerMin = npix;
mjr 17:ab3cec0c8bf4 996 }
mjr 17:ab3cec0c8bf4 997
mjr 17:ab3cec0c8bf4 998 // stored data (excluding the checksum)
mjr 17:ab3cec0c8bf4 999 struct
mjr 17:ab3cec0c8bf4 1000 {
mjr 17:ab3cec0c8bf4 1001 // Signature, structure version, and structure size - further verification
mjr 17:ab3cec0c8bf4 1002 // that we have valid initialized data. The size is a simple proxy for a
mjr 17:ab3cec0c8bf4 1003 // structure version, as the most common type of change to the structure as
mjr 17:ab3cec0c8bf4 1004 // the software evolves will be the addition of new elements. We also
mjr 17:ab3cec0c8bf4 1005 // provide an explicit version number that we can update manually if we
mjr 17:ab3cec0c8bf4 1006 // make any changes that don't affect the structure size but would affect
mjr 17:ab3cec0c8bf4 1007 // compatibility with a saved record (e.g., swapping two existing elements).
mjr 17:ab3cec0c8bf4 1008 uint32_t sig;
mjr 17:ab3cec0c8bf4 1009 uint16_t vsn;
mjr 17:ab3cec0c8bf4 1010 int sz;
mjr 17:ab3cec0c8bf4 1011
mjr 17:ab3cec0c8bf4 1012 // has the plunger been manually calibrated?
mjr 17:ab3cec0c8bf4 1013 int plungerCal;
mjr 17:ab3cec0c8bf4 1014
mjr 17:ab3cec0c8bf4 1015 // Plunger calibration min, zero, and max. The zero point is the
mjr 17:ab3cec0c8bf4 1016 // rest position (aka park position), where it's in equilibrium between
mjr 17:ab3cec0c8bf4 1017 // the main spring and the barrel spring. It can travel a small distance
mjr 17:ab3cec0c8bf4 1018 // forward of the rest position, because the barrel spring can be
mjr 17:ab3cec0c8bf4 1019 // compressed by the user pushing on the plunger or by the momentum
mjr 17:ab3cec0c8bf4 1020 // of a release motion. The minimum is the maximum forward point where
mjr 17:ab3cec0c8bf4 1021 // the barrel spring can't be compressed any further.
mjr 17:ab3cec0c8bf4 1022 int plungerMin;
mjr 17:ab3cec0c8bf4 1023 int plungerZero;
mjr 17:ab3cec0c8bf4 1024 int plungerMax;
mjr 17:ab3cec0c8bf4 1025
mjr 17:ab3cec0c8bf4 1026 // is the plunger sensor enabled?
mjr 17:ab3cec0c8bf4 1027 int plungerEnabled;
mjr 17:ab3cec0c8bf4 1028
mjr 17:ab3cec0c8bf4 1029 // LedWiz unit number
mjr 17:ab3cec0c8bf4 1030 uint8_t ledWizUnitNo;
mjr 17:ab3cec0c8bf4 1031 } d;
mjr 17:ab3cec0c8bf4 1032 };
mjr 17:ab3cec0c8bf4 1033
mjr 17:ab3cec0c8bf4 1034
mjr 17:ab3cec0c8bf4 1035 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 1036 //
mjr 5:a70c0bce770d 1037 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 1038 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 1039 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 1040 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 1041 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 1042 // port outputs.
mjr 5:a70c0bce770d 1043 //
mjr 0:5acbbe3f4cf4 1044 int main(void)
mjr 0:5acbbe3f4cf4 1045 {
mjr 1:d913e0afb2ac 1046 // turn off our on-board indicator LED
mjr 4:02c7cd7b2183 1047 ledR = 1;
mjr 4:02c7cd7b2183 1048 ledG = 1;
mjr 4:02c7cd7b2183 1049 ledB = 1;
mjr 1:d913e0afb2ac 1050
mjr 6:cc35eb643e8f 1051 // initialize the LedWiz ports
mjr 6:cc35eb643e8f 1052 initLwOut();
mjr 6:cc35eb643e8f 1053
mjr 11:bd9da7088e6e 1054 // initialize the button input ports
mjr 11:bd9da7088e6e 1055 initButtons();
mjr 11:bd9da7088e6e 1056
mjr 6:cc35eb643e8f 1057 // we don't need a reset yet
mjr 6:cc35eb643e8f 1058 bool needReset = false;
mjr 6:cc35eb643e8f 1059
mjr 5:a70c0bce770d 1060 // clear the I2C bus for the accelerometer
mjr 5:a70c0bce770d 1061 clear_i2c();
mjr 5:a70c0bce770d 1062
mjr 2:c174f9ee414a 1063 // set up a flash memory controller
mjr 2:c174f9ee414a 1064 FreescaleIAP iap;
mjr 2:c174f9ee414a 1065
mjr 2:c174f9ee414a 1066 // use the last sector of flash for our non-volatile memory structure
mjr 2:c174f9ee414a 1067 int flash_addr = (iap.flash_size() - SECTOR_SIZE);
mjr 2:c174f9ee414a 1068 NVM *flash = (NVM *)flash_addr;
mjr 2:c174f9ee414a 1069 NVM cfg;
mjr 2:c174f9ee414a 1070
mjr 2:c174f9ee414a 1071 // check for valid flash
mjr 6:cc35eb643e8f 1072 bool flash_valid = flash->valid();
mjr 2:c174f9ee414a 1073
mjr 2:c174f9ee414a 1074 // if the flash is valid, load it; otherwise initialize to defaults
mjr 2:c174f9ee414a 1075 if (flash_valid) {
mjr 2:c174f9ee414a 1076 memcpy(&cfg, flash, sizeof(cfg));
mjr 6:cc35eb643e8f 1077 printf("Flash restored: plunger cal=%d, min=%d, zero=%d, max=%d\r\n",
mjr 6:cc35eb643e8f 1078 cfg.d.plungerCal, cfg.d.plungerMin, cfg.d.plungerZero, cfg.d.plungerMax);
mjr 2:c174f9ee414a 1079 }
mjr 2:c174f9ee414a 1080 else {
mjr 2:c174f9ee414a 1081 printf("Factory reset\r\n");
mjr 2:c174f9ee414a 1082 cfg.d.sig = cfg.SIGNATURE;
mjr 2:c174f9ee414a 1083 cfg.d.vsn = cfg.VERSION;
mjr 6:cc35eb643e8f 1084 cfg.d.plungerCal = 0;
mjr 17:ab3cec0c8bf4 1085 cfg.d.plungerMin = 0; // assume we can go all the way forward...
mjr 17:ab3cec0c8bf4 1086 cfg.d.plungerMax = npix; // ...and all the way back
mjr 17:ab3cec0c8bf4 1087 cfg.d.plungerZero = npix/6; // the rest position is usually around 1/2" back
mjr 21:5048e16cc9ef 1088 cfg.d.ledWizUnitNo = DEFAULT_LEDWIZ_UNIT_NUMBER - 1; // unit numbering starts from 0 internally
mjr 21:5048e16cc9ef 1089 cfg.d.plungerEnabled = PLUNGER_CODE_ENABLED;
mjr 2:c174f9ee414a 1090 }
mjr 1:d913e0afb2ac 1091
mjr 6:cc35eb643e8f 1092 // Create the joystick USB client. Note that we use the LedWiz unit
mjr 6:cc35eb643e8f 1093 // number from the saved configuration.
mjr 6:cc35eb643e8f 1094 MyUSBJoystick js(
mjr 6:cc35eb643e8f 1095 USB_VENDOR_ID,
mjr 6:cc35eb643e8f 1096 USB_PRODUCT_ID | cfg.d.ledWizUnitNo,
mjr 6:cc35eb643e8f 1097 USB_VERSION_NO);
mjr 17:ab3cec0c8bf4 1098
mjr 17:ab3cec0c8bf4 1099 // last report timer - we use this to throttle reports, since VP
mjr 17:ab3cec0c8bf4 1100 // doesn't want to hear from us more than about every 10ms
mjr 17:ab3cec0c8bf4 1101 Timer reportTimer;
mjr 17:ab3cec0c8bf4 1102 reportTimer.start();
mjr 17:ab3cec0c8bf4 1103
mjr 17:ab3cec0c8bf4 1104 // initialize the calibration buttons, if present
mjr 17:ab3cec0c8bf4 1105 DigitalIn *calBtn = (CAL_BUTTON_PIN == NC ? 0 : new DigitalIn(CAL_BUTTON_PIN));
mjr 17:ab3cec0c8bf4 1106 DigitalOut *calBtnLed = (CAL_BUTTON_LED == NC ? 0 : new DigitalOut(CAL_BUTTON_LED));
mjr 6:cc35eb643e8f 1107
mjr 1:d913e0afb2ac 1108 // plunger calibration button debounce timer
mjr 1:d913e0afb2ac 1109 Timer calBtnTimer;
mjr 1:d913e0afb2ac 1110 calBtnTimer.start();
mjr 1:d913e0afb2ac 1111 int calBtnLit = false;
mjr 1:d913e0afb2ac 1112
mjr 1:d913e0afb2ac 1113 // Calibration button state:
mjr 1:d913e0afb2ac 1114 // 0 = not pushed
mjr 1:d913e0afb2ac 1115 // 1 = pushed, not yet debounced
mjr 1:d913e0afb2ac 1116 // 2 = pushed, debounced, waiting for hold time
mjr 1:d913e0afb2ac 1117 // 3 = pushed, hold time completed - in calibration mode
mjr 1:d913e0afb2ac 1118 int calBtnState = 0;
mjr 1:d913e0afb2ac 1119
mjr 1:d913e0afb2ac 1120 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 1121 Timer hbTimer;
mjr 1:d913e0afb2ac 1122 hbTimer.start();
mjr 1:d913e0afb2ac 1123 int hb = 0;
mjr 5:a70c0bce770d 1124 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 1125
mjr 1:d913e0afb2ac 1126 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 1127 Timer acTimer;
mjr 1:d913e0afb2ac 1128 acTimer.start();
mjr 1:d913e0afb2ac 1129
mjr 0:5acbbe3f4cf4 1130 // create the accelerometer object
mjr 5:a70c0bce770d 1131 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS, MMA8451_INT_PIN);
mjr 0:5acbbe3f4cf4 1132
mjr 21:5048e16cc9ef 1133 #ifdef ENABLE_JOYSTICK
mjr 17:ab3cec0c8bf4 1134 // last accelerometer report, in joystick units (we report the nudge
mjr 17:ab3cec0c8bf4 1135 // acceleration via the joystick x & y axes, per the VP convention)
mjr 17:ab3cec0c8bf4 1136 int x = 0, y = 0;
mjr 17:ab3cec0c8bf4 1137
mjr 21:5048e16cc9ef 1138 // flag: send a pixel dump after the next read
mjr 21:5048e16cc9ef 1139 bool reportPix = false;
mjr 21:5048e16cc9ef 1140 #endif
mjr 21:5048e16cc9ef 1141
mjr 17:ab3cec0c8bf4 1142 // create our plunger sensor object
mjr 17:ab3cec0c8bf4 1143 PlungerSensor plungerSensor;
mjr 17:ab3cec0c8bf4 1144
mjr 17:ab3cec0c8bf4 1145 // last plunger report position, in 'npix' normalized pixel units
mjr 17:ab3cec0c8bf4 1146 int pos = 0;
mjr 17:ab3cec0c8bf4 1147
mjr 17:ab3cec0c8bf4 1148 // last plunger report, in joystick units (we report the plunger as the
mjr 17:ab3cec0c8bf4 1149 // "z" axis of the joystick, per the VP convention)
mjr 17:ab3cec0c8bf4 1150 int z = 0;
mjr 17:ab3cec0c8bf4 1151
mjr 17:ab3cec0c8bf4 1152 // most recent prior plunger readings, for tracking release events(z0 is
mjr 17:ab3cec0c8bf4 1153 // reading just before the last one we reported, z1 is the one before that,
mjr 17:ab3cec0c8bf4 1154 // z2 the next before that)
mjr 17:ab3cec0c8bf4 1155 int z0 = 0, z1 = 0, z2 = 0;
mjr 17:ab3cec0c8bf4 1156
mjr 17:ab3cec0c8bf4 1157 // Simulated "bounce" position when firing. We model the bounce off of
mjr 17:ab3cec0c8bf4 1158 // the barrel spring when the plunger is released as proportional to the
mjr 17:ab3cec0c8bf4 1159 // distance it was retracted just before being released.
mjr 17:ab3cec0c8bf4 1160 int zBounce = 0;
mjr 2:c174f9ee414a 1161
mjr 17:ab3cec0c8bf4 1162 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 17:ab3cec0c8bf4 1163 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 17:ab3cec0c8bf4 1164 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 17:ab3cec0c8bf4 1165 // back and releases the plunger, or simply pushes on the plunger from
mjr 17:ab3cec0c8bf4 1166 // the rest position. This allows the plunger to be used in lieu of a
mjr 17:ab3cec0c8bf4 1167 // physical Launch Ball button for tables that don't have plungers.
mjr 17:ab3cec0c8bf4 1168 //
mjr 17:ab3cec0c8bf4 1169 // States:
mjr 17:ab3cec0c8bf4 1170 // 0 = default
mjr 17:ab3cec0c8bf4 1171 // 1 = cocked (plunger has been pulled back about 1" from state 0)
mjr 17:ab3cec0c8bf4 1172 // 2 = uncocked (plunger is pulled back less than 1" from state 1)
mjr 21:5048e16cc9ef 1173 // 3 = launching, plunger is forward beyond park position
mjr 21:5048e16cc9ef 1174 // 4 = launching, plunger is behind park position
mjr 21:5048e16cc9ef 1175 // 5 = pressed and holding (plunger has been pressed forward beyond
mjr 21:5048e16cc9ef 1176 // the park position from state 0)
mjr 17:ab3cec0c8bf4 1177 int lbState = 0;
mjr 6:cc35eb643e8f 1178
mjr 17:ab3cec0c8bf4 1179 // Time since last lbState transition. Some of the states are time-
mjr 17:ab3cec0c8bf4 1180 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 17:ab3cec0c8bf4 1181 // we remain in this state for more than a few milliseconds, since
mjr 17:ab3cec0c8bf4 1182 // it indicates that the plunger is being slowly returned to rest
mjr 17:ab3cec0c8bf4 1183 // rather than released. In the "launching" state, we need to release
mjr 17:ab3cec0c8bf4 1184 // the Launch Ball button after a moment, and we need to wait for
mjr 17:ab3cec0c8bf4 1185 // the plunger to come to rest before returning to state 0.
mjr 17:ab3cec0c8bf4 1186 Timer lbTimer;
mjr 17:ab3cec0c8bf4 1187 lbTimer.start();
mjr 17:ab3cec0c8bf4 1188
mjr 18:5e890ebd0023 1189 // Launch Ball simulated push timer. We start this when we simulate
mjr 18:5e890ebd0023 1190 // the button push, and turn off the simulated button when enough time
mjr 18:5e890ebd0023 1191 // has elapsed.
mjr 18:5e890ebd0023 1192 Timer lbBtnTimer;
mjr 18:5e890ebd0023 1193
mjr 17:ab3cec0c8bf4 1194 // Simulated button states. This is a vector of button states
mjr 17:ab3cec0c8bf4 1195 // for the simulated buttons. We combine this with the physical
mjr 17:ab3cec0c8bf4 1196 // button states on each USB joystick report, so we will report
mjr 17:ab3cec0c8bf4 1197 // a button as pressed if either the physical button is being pressed
mjr 17:ab3cec0c8bf4 1198 // or we're simulating a press on the button. This is used for the
mjr 17:ab3cec0c8bf4 1199 // simulated Launch Ball button.
mjr 17:ab3cec0c8bf4 1200 uint32_t simButtons = 0;
mjr 6:cc35eb643e8f 1201
mjr 6:cc35eb643e8f 1202 // Firing in progress: we set this when we detect the start of rapid
mjr 6:cc35eb643e8f 1203 // plunger movement from a retracted position towards the rest position.
mjr 17:ab3cec0c8bf4 1204 //
mjr 17:ab3cec0c8bf4 1205 // When we detect a firing event, we send VP a series of synthetic
mjr 17:ab3cec0c8bf4 1206 // reports simulating the idealized plunger motion. The actual physical
mjr 17:ab3cec0c8bf4 1207 // motion is much too fast to report to VP; in the time between two USB
mjr 17:ab3cec0c8bf4 1208 // reports, the plunger can shoot all the way forward, rebound off of
mjr 17:ab3cec0c8bf4 1209 // the barrel spring, bounce back part way, and bounce forward again,
mjr 17:ab3cec0c8bf4 1210 // or even do all of this more than once. This means that sampling the
mjr 17:ab3cec0c8bf4 1211 // physical motion at the USB report rate would create a misleading
mjr 17:ab3cec0c8bf4 1212 // picture of the plunger motion, since our samples would catch the
mjr 17:ab3cec0c8bf4 1213 // plunger at random points in this oscillating motion. From the
mjr 17:ab3cec0c8bf4 1214 // user's perspective, the physical action that occurred is simply that
mjr 17:ab3cec0c8bf4 1215 // the plunger was released from a particular distance, so it's this
mjr 17:ab3cec0c8bf4 1216 // high-level event that we want to convey to VP. To do this, we
mjr 17:ab3cec0c8bf4 1217 // synthesize a series of reports to convey an idealized version of
mjr 17:ab3cec0c8bf4 1218 // the release motion that's perfectly synchronized to the VP reports.
mjr 17:ab3cec0c8bf4 1219 // Essentially we pretend that our USB position samples are exactly
mjr 17:ab3cec0c8bf4 1220 // aligned in time with (1) the point of retraction just before the
mjr 17:ab3cec0c8bf4 1221 // user released the plunger, (2) the point of maximum forward motion
mjr 17:ab3cec0c8bf4 1222 // just after the user released the plunger (the point of maximum
mjr 17:ab3cec0c8bf4 1223 // compression as the plunger bounces off of the barrel spring), and
mjr 17:ab3cec0c8bf4 1224 // (3) the plunger coming to rest at the park position. This series
mjr 17:ab3cec0c8bf4 1225 // of reports is synthetic in the sense that it's not what we actually
mjr 17:ab3cec0c8bf4 1226 // see on the CCD at the times of these reports - the true plunger
mjr 17:ab3cec0c8bf4 1227 // position is oscillating at high speed during this period. But at
mjr 17:ab3cec0c8bf4 1228 // the same time it conveys a more faithful picture of the true physical
mjr 17:ab3cec0c8bf4 1229 // motion to VP, and allows VP to reproduce the true physical motion
mjr 17:ab3cec0c8bf4 1230 // more faithfully in its simulation model, by correcting for the
mjr 17:ab3cec0c8bf4 1231 // relatively low sampling rate in the communication path between the
mjr 17:ab3cec0c8bf4 1232 // real plunger and VP's model plunger.
mjr 17:ab3cec0c8bf4 1233 //
mjr 17:ab3cec0c8bf4 1234 // If 'firing' is non-zero, it's the index of our current report in
mjr 17:ab3cec0c8bf4 1235 // the synthetic firing report series.
mjr 9:fd65b0a94720 1236 int firing = 0;
mjr 2:c174f9ee414a 1237
mjr 2:c174f9ee414a 1238 // start the first CCD integration cycle
mjr 17:ab3cec0c8bf4 1239 plungerSensor.init();
mjr 9:fd65b0a94720 1240
mjr 9:fd65b0a94720 1241 // Device status. We report this on each update so that the host config
mjr 9:fd65b0a94720 1242 // tool can detect our current settings. This is a bit mask consisting
mjr 9:fd65b0a94720 1243 // of these bits:
mjr 9:fd65b0a94720 1244 // 0x01 -> plunger sensor enabled
mjr 17:ab3cec0c8bf4 1245 uint16_t statusFlags = (cfg.d.plungerEnabled ? 0x01 : 0x00);
mjr 10:976666ffa4ef 1246
mjr 1:d913e0afb2ac 1247 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 1248 // host requests
mjr 0:5acbbe3f4cf4 1249 for (;;)
mjr 0:5acbbe3f4cf4 1250 {
mjr 18:5e890ebd0023 1251 // Look for an incoming report. Process a few input reports in
mjr 18:5e890ebd0023 1252 // a row, but stop after a few so that a barrage of inputs won't
mjr 20:4c43877327ab 1253 // starve our output event processing. Also, pause briefly between
mjr 20:4c43877327ab 1254 // reads; allowing reads to occur back-to-back seems to occasionally
mjr 20:4c43877327ab 1255 // stall the USB pipeline (for reasons unknown; I'd fix the underlying
mjr 20:4c43877327ab 1256 // problem if I knew what it was).
mjr 0:5acbbe3f4cf4 1257 HID_REPORT report;
mjr 20:4c43877327ab 1258 for (int rr = 0 ; rr < 4 && js.readNB(&report) ; ++rr, wait_ms(1))
mjr 0:5acbbe3f4cf4 1259 {
mjr 6:cc35eb643e8f 1260 // all Led-Wiz reports are 8 bytes exactly
mjr 6:cc35eb643e8f 1261 if (report.length == 8)
mjr 1:d913e0afb2ac 1262 {
mjr 6:cc35eb643e8f 1263 uint8_t *data = report.data;
mjr 6:cc35eb643e8f 1264 if (data[0] == 64)
mjr 0:5acbbe3f4cf4 1265 {
mjr 6:cc35eb643e8f 1266 // LWZ-SBA - first four bytes are bit-packed on/off flags
mjr 6:cc35eb643e8f 1267 // for the outputs; 5th byte is the pulse speed (0-7)
mjr 6:cc35eb643e8f 1268 //printf("LWZ-SBA %02x %02x %02x %02x ; %02x\r\n",
mjr 6:cc35eb643e8f 1269 // data[1], data[2], data[3], data[4], data[5]);
mjr 0:5acbbe3f4cf4 1270
mjr 6:cc35eb643e8f 1271 // update all on/off states
mjr 6:cc35eb643e8f 1272 for (int i = 0, bit = 1, ri = 1 ; i < 32 ; ++i, bit <<= 1)
mjr 6:cc35eb643e8f 1273 {
mjr 6:cc35eb643e8f 1274 if (bit == 0x100) {
mjr 6:cc35eb643e8f 1275 bit = 1;
mjr 6:cc35eb643e8f 1276 ++ri;
mjr 6:cc35eb643e8f 1277 }
mjr 6:cc35eb643e8f 1278 wizOn[i] = ((data[ri] & bit) != 0);
mjr 6:cc35eb643e8f 1279 }
mjr 6:cc35eb643e8f 1280
mjr 6:cc35eb643e8f 1281 // update the physical outputs
mjr 1:d913e0afb2ac 1282 updateWizOuts();
mjr 6:cc35eb643e8f 1283
mjr 6:cc35eb643e8f 1284 // reset the PBA counter
mjr 6:cc35eb643e8f 1285 pbaIdx = 0;
mjr 6:cc35eb643e8f 1286 }
mjr 6:cc35eb643e8f 1287 else if (data[0] == 65)
mjr 6:cc35eb643e8f 1288 {
mjr 6:cc35eb643e8f 1289 // Private control message. This isn't an LedWiz message - it's
mjr 6:cc35eb643e8f 1290 // an extension for this device. 65 is an invalid PBA setting,
mjr 6:cc35eb643e8f 1291 // and isn't used for any other LedWiz message, so we appropriate
mjr 6:cc35eb643e8f 1292 // it for our own private use. The first byte specifies the
mjr 6:cc35eb643e8f 1293 // message type.
mjr 6:cc35eb643e8f 1294 if (data[1] == 1)
mjr 6:cc35eb643e8f 1295 {
mjr 9:fd65b0a94720 1296 // 1 = Set Configuration:
mjr 6:cc35eb643e8f 1297 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 6:cc35eb643e8f 1298 // data[3] = feature enable bit mask:
mjr 21:5048e16cc9ef 1299 // 0x01 = enable plunger sensor
mjr 6:cc35eb643e8f 1300
mjr 6:cc35eb643e8f 1301 // we'll need a reset if the LedWiz unit number is changing
mjr 6:cc35eb643e8f 1302 uint8_t newUnitNo = data[2] & 0x0f;
mjr 6:cc35eb643e8f 1303 needReset |= (newUnitNo != cfg.d.ledWizUnitNo);
mjr 6:cc35eb643e8f 1304
mjr 6:cc35eb643e8f 1305 // set the configuration parameters from the message
mjr 6:cc35eb643e8f 1306 cfg.d.ledWizUnitNo = newUnitNo;
mjr 17:ab3cec0c8bf4 1307 cfg.d.plungerEnabled = data[3] & 0x01;
mjr 6:cc35eb643e8f 1308
mjr 9:fd65b0a94720 1309 // update the status flags
mjr 9:fd65b0a94720 1310 statusFlags = (statusFlags & ~0x01) | (data[3] & 0x01);
mjr 9:fd65b0a94720 1311
mjr 9:fd65b0a94720 1312 // if the ccd is no longer enabled, use 0 for z reports
mjr 17:ab3cec0c8bf4 1313 if (!cfg.d.plungerEnabled)
mjr 9:fd65b0a94720 1314 z = 0;
mjr 9:fd65b0a94720 1315
mjr 6:cc35eb643e8f 1316 // save the configuration
mjr 6:cc35eb643e8f 1317 cfg.save(iap, flash_addr);
mjr 6:cc35eb643e8f 1318 }
mjr 21:5048e16cc9ef 1319 #ifdef ENABLE_JOYSTICK
mjr 9:fd65b0a94720 1320 else if (data[1] == 2)
mjr 9:fd65b0a94720 1321 {
mjr 9:fd65b0a94720 1322 // 2 = Calibrate plunger
mjr 9:fd65b0a94720 1323 // (No parameters)
mjr 9:fd65b0a94720 1324
mjr 9:fd65b0a94720 1325 // enter calibration mode
mjr 9:fd65b0a94720 1326 calBtnState = 3;
mjr 9:fd65b0a94720 1327 calBtnTimer.reset();
mjr 9:fd65b0a94720 1328 cfg.resetPlunger();
mjr 9:fd65b0a94720 1329 }
mjr 10:976666ffa4ef 1330 else if (data[1] == 3)
mjr 10:976666ffa4ef 1331 {
mjr 10:976666ffa4ef 1332 // 3 = pixel dump
mjr 10:976666ffa4ef 1333 // (No parameters)
mjr 10:976666ffa4ef 1334 reportPix = true;
mjr 10:976666ffa4ef 1335
mjr 10:976666ffa4ef 1336 // show purple until we finish sending the report
mjr 10:976666ffa4ef 1337 ledR = 0;
mjr 10:976666ffa4ef 1338 ledB = 0;
mjr 10:976666ffa4ef 1339 ledG = 1;
mjr 10:976666ffa4ef 1340 }
mjr 21:5048e16cc9ef 1341 #endif // ENABLE_JOYSTICK
mjr 6:cc35eb643e8f 1342 }
mjr 6:cc35eb643e8f 1343 else
mjr 6:cc35eb643e8f 1344 {
mjr 6:cc35eb643e8f 1345 // LWZ-PBA - full state dump; each byte is one output
mjr 6:cc35eb643e8f 1346 // in the current bank. pbaIdx keeps track of the bank;
mjr 6:cc35eb643e8f 1347 // this is incremented implicitly by each PBA message.
mjr 6:cc35eb643e8f 1348 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 6:cc35eb643e8f 1349 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 6:cc35eb643e8f 1350
mjr 6:cc35eb643e8f 1351 // update all output profile settings
mjr 6:cc35eb643e8f 1352 for (int i = 0 ; i < 8 ; ++i)
mjr 6:cc35eb643e8f 1353 wizVal[pbaIdx + i] = data[i];
mjr 6:cc35eb643e8f 1354
mjr 6:cc35eb643e8f 1355 // update the physical LED state if this is the last bank
mjr 6:cc35eb643e8f 1356 if (pbaIdx == 24)
mjr 13:72dda449c3c0 1357 {
mjr 6:cc35eb643e8f 1358 updateWizOuts();
mjr 13:72dda449c3c0 1359 pbaIdx = 0;
mjr 13:72dda449c3c0 1360 }
mjr 13:72dda449c3c0 1361 else
mjr 13:72dda449c3c0 1362 pbaIdx += 8;
mjr 6:cc35eb643e8f 1363 }
mjr 0:5acbbe3f4cf4 1364 }
mjr 0:5acbbe3f4cf4 1365 }
mjr 1:d913e0afb2ac 1366
mjr 1:d913e0afb2ac 1367 // check for plunger calibration
mjr 17:ab3cec0c8bf4 1368 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 1369 {
mjr 1:d913e0afb2ac 1370 // check the state
mjr 1:d913e0afb2ac 1371 switch (calBtnState)
mjr 0:5acbbe3f4cf4 1372 {
mjr 1:d913e0afb2ac 1373 case 0:
mjr 1:d913e0afb2ac 1374 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 1375 calBtnTimer.reset();
mjr 1:d913e0afb2ac 1376 calBtnState = 1;
mjr 1:d913e0afb2ac 1377 break;
mjr 1:d913e0afb2ac 1378
mjr 1:d913e0afb2ac 1379 case 1:
mjr 1:d913e0afb2ac 1380 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 1381 // passed, start the hold period
mjr 9:fd65b0a94720 1382 if (calBtnTimer.read_ms() > 50)
mjr 1:d913e0afb2ac 1383 calBtnState = 2;
mjr 1:d913e0afb2ac 1384 break;
mjr 1:d913e0afb2ac 1385
mjr 1:d913e0afb2ac 1386 case 2:
mjr 1:d913e0afb2ac 1387 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 1388 // for the entire hold period, move to calibration mode
mjr 9:fd65b0a94720 1389 if (calBtnTimer.read_ms() > 2050)
mjr 1:d913e0afb2ac 1390 {
mjr 1:d913e0afb2ac 1391 // enter calibration mode
mjr 1:d913e0afb2ac 1392 calBtnState = 3;
mjr 9:fd65b0a94720 1393 calBtnTimer.reset();
mjr 9:fd65b0a94720 1394 cfg.resetPlunger();
mjr 1:d913e0afb2ac 1395 }
mjr 1:d913e0afb2ac 1396 break;
mjr 2:c174f9ee414a 1397
mjr 2:c174f9ee414a 1398 case 3:
mjr 9:fd65b0a94720 1399 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 1400 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 1401 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 1402 break;
mjr 0:5acbbe3f4cf4 1403 }
mjr 0:5acbbe3f4cf4 1404 }
mjr 1:d913e0afb2ac 1405 else
mjr 1:d913e0afb2ac 1406 {
mjr 2:c174f9ee414a 1407 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 1408 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 1409 // and save the results to flash.
mjr 2:c174f9ee414a 1410 //
mjr 2:c174f9ee414a 1411 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 1412 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 1413 // mode, it simply cancels the attempt.
mjr 9:fd65b0a94720 1414 if (calBtnState == 3 && calBtnTimer.read_ms() > 15000)
mjr 2:c174f9ee414a 1415 {
mjr 2:c174f9ee414a 1416 // exit calibration mode
mjr 1:d913e0afb2ac 1417 calBtnState = 0;
mjr 2:c174f9ee414a 1418
mjr 6:cc35eb643e8f 1419 // save the updated configuration
mjr 6:cc35eb643e8f 1420 cfg.d.plungerCal = 1;
mjr 6:cc35eb643e8f 1421 cfg.save(iap, flash_addr);
mjr 2:c174f9ee414a 1422
mjr 2:c174f9ee414a 1423 // the flash state is now valid
mjr 2:c174f9ee414a 1424 flash_valid = true;
mjr 2:c174f9ee414a 1425 }
mjr 2:c174f9ee414a 1426 else if (calBtnState != 3)
mjr 2:c174f9ee414a 1427 {
mjr 2:c174f9ee414a 1428 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 1429 calBtnState = 0;
mjr 2:c174f9ee414a 1430 }
mjr 1:d913e0afb2ac 1431 }
mjr 1:d913e0afb2ac 1432
mjr 1:d913e0afb2ac 1433 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 1434 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 1435 switch (calBtnState)
mjr 0:5acbbe3f4cf4 1436 {
mjr 1:d913e0afb2ac 1437 case 2:
mjr 1:d913e0afb2ac 1438 // in the hold period - flash the light
mjr 9:fd65b0a94720 1439 newCalBtnLit = ((calBtnTimer.read_ms()/250) & 1);
mjr 1:d913e0afb2ac 1440 break;
mjr 1:d913e0afb2ac 1441
mjr 1:d913e0afb2ac 1442 case 3:
mjr 1:d913e0afb2ac 1443 // calibration mode - show steady on
mjr 1:d913e0afb2ac 1444 newCalBtnLit = true;
mjr 1:d913e0afb2ac 1445 break;
mjr 1:d913e0afb2ac 1446
mjr 1:d913e0afb2ac 1447 default:
mjr 1:d913e0afb2ac 1448 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 1449 newCalBtnLit = false;
mjr 1:d913e0afb2ac 1450 break;
mjr 1:d913e0afb2ac 1451 }
mjr 3:3514575d4f86 1452
mjr 3:3514575d4f86 1453 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 1454 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 1455 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 1456 {
mjr 1:d913e0afb2ac 1457 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 1458 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 1459 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 1460 calBtnLed->write(1);
mjr 4:02c7cd7b2183 1461 ledR = 1;
mjr 4:02c7cd7b2183 1462 ledG = 1;
mjr 9:fd65b0a94720 1463 ledB = 0;
mjr 2:c174f9ee414a 1464 }
mjr 2:c174f9ee414a 1465 else {
mjr 17:ab3cec0c8bf4 1466 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 1467 calBtnLed->write(0);
mjr 4:02c7cd7b2183 1468 ledR = 1;
mjr 4:02c7cd7b2183 1469 ledG = 1;
mjr 9:fd65b0a94720 1470 ledB = 1;
mjr 2:c174f9ee414a 1471 }
mjr 1:d913e0afb2ac 1472 }
mjr 1:d913e0afb2ac 1473
mjr 17:ab3cec0c8bf4 1474 // If the plunger is enabled, and we're not already in a firing event,
mjr 17:ab3cec0c8bf4 1475 // and the last plunger reading had the plunger pulled back at least
mjr 17:ab3cec0c8bf4 1476 // a bit, watch for plunger release events until it's time for our next
mjr 17:ab3cec0c8bf4 1477 // USB report.
mjr 17:ab3cec0c8bf4 1478 if (!firing && cfg.d.plungerEnabled && z >= JOYMAX/6)
mjr 17:ab3cec0c8bf4 1479 {
mjr 17:ab3cec0c8bf4 1480 // monitor the plunger until it's time for our next report
mjr 17:ab3cec0c8bf4 1481 while (reportTimer.read_ms() < 15)
mjr 17:ab3cec0c8bf4 1482 {
mjr 17:ab3cec0c8bf4 1483 // do a fast low-res scan; if it's at or past the zero point,
mjr 17:ab3cec0c8bf4 1484 // start a firing event
mjr 17:ab3cec0c8bf4 1485 if (plungerSensor.lowResScan() <= cfg.d.plungerZero)
mjr 17:ab3cec0c8bf4 1486 firing = 1;
mjr 17:ab3cec0c8bf4 1487 }
mjr 17:ab3cec0c8bf4 1488 }
mjr 17:ab3cec0c8bf4 1489
mjr 6:cc35eb643e8f 1490 // read the plunger sensor, if it's enabled
mjr 17:ab3cec0c8bf4 1491 if (cfg.d.plungerEnabled)
mjr 6:cc35eb643e8f 1492 {
mjr 6:cc35eb643e8f 1493 // start with the previous reading, in case we don't have a
mjr 6:cc35eb643e8f 1494 // clear result on this frame
mjr 6:cc35eb643e8f 1495 int znew = z;
mjr 17:ab3cec0c8bf4 1496 if (plungerSensor.highResScan(pos))
mjr 6:cc35eb643e8f 1497 {
mjr 17:ab3cec0c8bf4 1498 // We got a new reading. If we're in calibration mode, use it
mjr 17:ab3cec0c8bf4 1499 // to figure the new calibration, otherwise adjust the new reading
mjr 17:ab3cec0c8bf4 1500 // for the established calibration.
mjr 17:ab3cec0c8bf4 1501 if (calBtnState == 3)
mjr 6:cc35eb643e8f 1502 {
mjr 17:ab3cec0c8bf4 1503 // Calibration mode. If this reading is outside of the current
mjr 17:ab3cec0c8bf4 1504 // calibration bounds, expand the bounds.
mjr 17:ab3cec0c8bf4 1505 if (pos < cfg.d.plungerMin)
mjr 17:ab3cec0c8bf4 1506 cfg.d.plungerMin = pos;
mjr 17:ab3cec0c8bf4 1507 if (pos < cfg.d.plungerZero)
mjr 17:ab3cec0c8bf4 1508 cfg.d.plungerZero = pos;
mjr 17:ab3cec0c8bf4 1509 if (pos > cfg.d.plungerMax)
mjr 17:ab3cec0c8bf4 1510 cfg.d.plungerMax = pos;
mjr 6:cc35eb643e8f 1511
mjr 17:ab3cec0c8bf4 1512 // normalize to the full physical range while calibrating
mjr 17:ab3cec0c8bf4 1513 znew = int(round(float(pos)/npix * JOYMAX));
mjr 17:ab3cec0c8bf4 1514 }
mjr 17:ab3cec0c8bf4 1515 else
mjr 17:ab3cec0c8bf4 1516 {
mjr 17:ab3cec0c8bf4 1517 // Not in calibration mode, so normalize the new reading to the
mjr 17:ab3cec0c8bf4 1518 // established calibration range.
mjr 17:ab3cec0c8bf4 1519 //
mjr 17:ab3cec0c8bf4 1520 // Note that negative values are allowed. Zero represents the
mjr 17:ab3cec0c8bf4 1521 // "park" position, where the plunger sits when at rest. A mechanical
mjr 23:14f8c5004cd0 1522 // plunger has a small amount of travel in the "push" direction,
mjr 17:ab3cec0c8bf4 1523 // since the barrel spring can be compressed slightly. Negative
mjr 17:ab3cec0c8bf4 1524 // values represent travel in the push direction.
mjr 17:ab3cec0c8bf4 1525 if (pos > cfg.d.plungerMax)
mjr 17:ab3cec0c8bf4 1526 pos = cfg.d.plungerMax;
mjr 17:ab3cec0c8bf4 1527 znew = int(round(float(pos - cfg.d.plungerZero)
mjr 17:ab3cec0c8bf4 1528 / (cfg.d.plungerMax - cfg.d.plungerZero + 1) * JOYMAX));
mjr 6:cc35eb643e8f 1529 }
mjr 6:cc35eb643e8f 1530 }
mjr 7:100a25f8bf56 1531
mjr 17:ab3cec0c8bf4 1532 // If we're not already in a firing event, check to see if the
mjr 17:ab3cec0c8bf4 1533 // new position is forward of the last report. If it is, a firing
mjr 17:ab3cec0c8bf4 1534 // event might have started during the high-res scan. This might
mjr 17:ab3cec0c8bf4 1535 // seem unlikely given that the scan only takes about 5ms, but that
mjr 17:ab3cec0c8bf4 1536 // 5ms represents about 25-30% of our total time between reports,
mjr 17:ab3cec0c8bf4 1537 // there's about a 1 in 4 chance that a release starts during a
mjr 17:ab3cec0c8bf4 1538 // scan.
mjr 17:ab3cec0c8bf4 1539 if (!firing && z0 > 0 && znew < z0)
mjr 17:ab3cec0c8bf4 1540 {
mjr 17:ab3cec0c8bf4 1541 // The plunger has moved forward since the previous report.
mjr 17:ab3cec0c8bf4 1542 // Watch it for a few more ms to see if we can get a stable
mjr 17:ab3cec0c8bf4 1543 // new position.
mjr 23:14f8c5004cd0 1544 int pos0 = plungerSensor.lowResScan();
mjr 23:14f8c5004cd0 1545 int pos1 = pos0;
mjr 17:ab3cec0c8bf4 1546 Timer tw;
mjr 17:ab3cec0c8bf4 1547 tw.start();
mjr 17:ab3cec0c8bf4 1548 while (tw.read_ms() < 6)
mjr 17:ab3cec0c8bf4 1549 {
mjr 23:14f8c5004cd0 1550 // read the new position
mjr 23:14f8c5004cd0 1551 int pos2 = plungerSensor.lowResScan();
mjr 23:14f8c5004cd0 1552
mjr 23:14f8c5004cd0 1553 // If it's stable over consecutive readings, stop looping.
mjr 23:14f8c5004cd0 1554 // (Count it as stable if the position is within about 1/8".
mjr 23:14f8c5004cd0 1555 // pos1 and pos2 are reported in pixels, so they range from
mjr 23:14f8c5004cd0 1556 // 0 to npix. The overall travel of a standard plunger is
mjr 23:14f8c5004cd0 1557 // about 3.2", so we have (npix/3.2) pixels per inch, hence
mjr 23:14f8c5004cd0 1558 // 1/8" is (npix/3.2)*(1/8) pixels.)
mjr 23:14f8c5004cd0 1559 if (abs(pos2 - pos1) < int(npix/(3.2*8)))
mjr 23:14f8c5004cd0 1560 break;
mjr 23:14f8c5004cd0 1561
mjr 23:14f8c5004cd0 1562 // If we've crossed the rest position, and we've moved by
mjr 23:14f8c5004cd0 1563 // a minimum distance from where we starting this loop, begin
mjr 23:14f8c5004cd0 1564 // a firing event. (We require a minimum distance to prevent
mjr 23:14f8c5004cd0 1565 // spurious firing from random analog noise in the readings
mjr 23:14f8c5004cd0 1566 // when the plunger is actually just sitting still at the
mjr 23:14f8c5004cd0 1567 // rest position. If it's at rest, it's normal to see small
mjr 23:14f8c5004cd0 1568 // random fluctuations in the analog reading +/- 1% or so
mjr 23:14f8c5004cd0 1569 // from the 0 point, especially with a sensor like a
mjr 23:14f8c5004cd0 1570 // potentionemeter that reports the position as a single
mjr 23:14f8c5004cd0 1571 // analog voltage.) Note that we compare the latest reading
mjr 23:14f8c5004cd0 1572 // to the first reading of the loop - we don't require the
mjr 23:14f8c5004cd0 1573 // threshold motion over consecutive readings, but any time
mjr 23:14f8c5004cd0 1574 // over the stability wait loop.
mjr 23:14f8c5004cd0 1575 if (pos1 < cfg.d.plungerZero
mjr 23:14f8c5004cd0 1576 && abs(pos2 - pos0) > int(npix/(3.2*8)))
mjr 17:ab3cec0c8bf4 1577 {
mjr 17:ab3cec0c8bf4 1578 firing = 1;
mjr 17:ab3cec0c8bf4 1579 break;
mjr 17:ab3cec0c8bf4 1580 }
mjr 23:14f8c5004cd0 1581
mjr 17:ab3cec0c8bf4 1582 // the new reading is now the prior reading
mjr 17:ab3cec0c8bf4 1583 pos1 = pos2;
mjr 17:ab3cec0c8bf4 1584 }
mjr 17:ab3cec0c8bf4 1585 }
mjr 17:ab3cec0c8bf4 1586
mjr 17:ab3cec0c8bf4 1587 // Check for a simulated Launch Ball button press, if enabled
mjr 18:5e890ebd0023 1588 if (ZBLaunchBallPort != 0)
mjr 17:ab3cec0c8bf4 1589 {
mjr 18:5e890ebd0023 1590 const int cockThreshold = JOYMAX/3;
mjr 18:5e890ebd0023 1591 const int pushThreshold = int(-JOYMAX/3 * LaunchBallPushDistance);
mjr 17:ab3cec0c8bf4 1592 int newState = lbState;
mjr 17:ab3cec0c8bf4 1593 switch (lbState)
mjr 17:ab3cec0c8bf4 1594 {
mjr 17:ab3cec0c8bf4 1595 case 0:
mjr 17:ab3cec0c8bf4 1596 // Base state. If the plunger is pulled back by an inch
mjr 17:ab3cec0c8bf4 1597 // or more, go to "cocked" state. If the plunger is pushed
mjr 21:5048e16cc9ef 1598 // forward by 1/4" or more, go to "pressed" state.
mjr 18:5e890ebd0023 1599 if (znew >= cockThreshold)
mjr 17:ab3cec0c8bf4 1600 newState = 1;
mjr 18:5e890ebd0023 1601 else if (znew <= pushThreshold)
mjr 21:5048e16cc9ef 1602 newState = 5;
mjr 17:ab3cec0c8bf4 1603 break;
mjr 17:ab3cec0c8bf4 1604
mjr 17:ab3cec0c8bf4 1605 case 1:
mjr 17:ab3cec0c8bf4 1606 // Cocked state. If a firing event is now in progress,
mjr 17:ab3cec0c8bf4 1607 // go to "launch" state. Otherwise, if the plunger is less
mjr 17:ab3cec0c8bf4 1608 // than 1" retracted, go to "uncocked" state - the player
mjr 17:ab3cec0c8bf4 1609 // might be slowly returning the plunger to rest so as not
mjr 17:ab3cec0c8bf4 1610 // to trigger a launch.
mjr 17:ab3cec0c8bf4 1611 if (firing || znew <= 0)
mjr 17:ab3cec0c8bf4 1612 newState = 3;
mjr 18:5e890ebd0023 1613 else if (znew < cockThreshold)
mjr 17:ab3cec0c8bf4 1614 newState = 2;
mjr 17:ab3cec0c8bf4 1615 break;
mjr 17:ab3cec0c8bf4 1616
mjr 17:ab3cec0c8bf4 1617 case 2:
mjr 17:ab3cec0c8bf4 1618 // Uncocked state. If the plunger is more than an inch
mjr 17:ab3cec0c8bf4 1619 // retracted, return to cocked state. If we've been in
mjr 17:ab3cec0c8bf4 1620 // the uncocked state for more than half a second, return
mjr 18:5e890ebd0023 1621 // to the base state. This allows the user to return the
mjr 18:5e890ebd0023 1622 // plunger to rest without triggering a launch, by moving
mjr 18:5e890ebd0023 1623 // it at manual speed to the rest position rather than
mjr 18:5e890ebd0023 1624 // releasing it.
mjr 18:5e890ebd0023 1625 if (znew >= cockThreshold)
mjr 17:ab3cec0c8bf4 1626 newState = 1;
mjr 17:ab3cec0c8bf4 1627 else if (lbTimer.read_ms() > 500)
mjr 17:ab3cec0c8bf4 1628 newState = 0;
mjr 17:ab3cec0c8bf4 1629 break;
mjr 17:ab3cec0c8bf4 1630
mjr 17:ab3cec0c8bf4 1631 case 3:
mjr 17:ab3cec0c8bf4 1632 // Launch state. If the plunger is no longer pushed
mjr 17:ab3cec0c8bf4 1633 // forward, switch to launch rest state.
mjr 18:5e890ebd0023 1634 if (znew >= 0)
mjr 17:ab3cec0c8bf4 1635 newState = 4;
mjr 17:ab3cec0c8bf4 1636 break;
mjr 17:ab3cec0c8bf4 1637
mjr 17:ab3cec0c8bf4 1638 case 4:
mjr 17:ab3cec0c8bf4 1639 // Launch rest state. If the plunger is pushed forward
mjr 17:ab3cec0c8bf4 1640 // again, switch back to launch state. If not, and we've
mjr 17:ab3cec0c8bf4 1641 // been in this state for at least 200ms, return to the
mjr 17:ab3cec0c8bf4 1642 // default state.
mjr 18:5e890ebd0023 1643 if (znew <= pushThreshold)
mjr 17:ab3cec0c8bf4 1644 newState = 3;
mjr 17:ab3cec0c8bf4 1645 else if (lbTimer.read_ms() > 200)
mjr 17:ab3cec0c8bf4 1646 newState = 0;
mjr 17:ab3cec0c8bf4 1647 break;
mjr 21:5048e16cc9ef 1648
mjr 21:5048e16cc9ef 1649 case 5:
mjr 21:5048e16cc9ef 1650 // Press-and-Hold state. If the plunger is no longer pushed
mjr 21:5048e16cc9ef 1651 // forward, AND it's been at least 50ms since we generated
mjr 21:5048e16cc9ef 1652 // the simulated Launch Ball button press, return to the base
mjr 21:5048e16cc9ef 1653 // state. The minimum time is to ensure that VP has a chance
mjr 21:5048e16cc9ef 1654 // to see the button press and to avoid transient key bounce
mjr 21:5048e16cc9ef 1655 // effects when the plunger position is right on the threshold.
mjr 21:5048e16cc9ef 1656 if (znew > pushThreshold && lbTimer.read_ms() > 50)
mjr 21:5048e16cc9ef 1657 newState = 0;
mjr 21:5048e16cc9ef 1658 break;
mjr 17:ab3cec0c8bf4 1659 }
mjr 17:ab3cec0c8bf4 1660
mjr 17:ab3cec0c8bf4 1661 // change states if desired
mjr 18:5e890ebd0023 1662 const uint32_t lbButtonBit = (1 << (LaunchBallButton - 1));
mjr 17:ab3cec0c8bf4 1663 if (newState != lbState)
mjr 17:ab3cec0c8bf4 1664 {
mjr 21:5048e16cc9ef 1665 // If we're entering Launch state OR we're entering the
mjr 21:5048e16cc9ef 1666 // Press-and-Hold state, AND the ZB Launch Ball LedWiz signal
mjr 21:5048e16cc9ef 1667 // is turned on, simulate a Launch Ball button press.
mjr 21:5048e16cc9ef 1668 if (((newState == 3 && lbState != 4) || newState == 5)
mjr 21:5048e16cc9ef 1669 && wizOn[ZBLaunchBallPort-1])
mjr 18:5e890ebd0023 1670 {
mjr 18:5e890ebd0023 1671 lbBtnTimer.reset();
mjr 18:5e890ebd0023 1672 lbBtnTimer.start();
mjr 18:5e890ebd0023 1673 simButtons |= lbButtonBit;
mjr 18:5e890ebd0023 1674 }
mjr 21:5048e16cc9ef 1675
mjr 17:ab3cec0c8bf4 1676 // if we're switching to state 0, release the button
mjr 17:ab3cec0c8bf4 1677 if (newState == 0)
mjr 17:ab3cec0c8bf4 1678 simButtons &= ~(1 << (LaunchBallButton - 1));
mjr 17:ab3cec0c8bf4 1679
mjr 17:ab3cec0c8bf4 1680 // switch to the new state
mjr 17:ab3cec0c8bf4 1681 lbState = newState;
mjr 17:ab3cec0c8bf4 1682
mjr 17:ab3cec0c8bf4 1683 // start timing in the new state
mjr 17:ab3cec0c8bf4 1684 lbTimer.reset();
mjr 17:ab3cec0c8bf4 1685 }
mjr 21:5048e16cc9ef 1686
mjr 21:5048e16cc9ef 1687 // If the Launch Ball button press is in effect, but the
mjr 21:5048e16cc9ef 1688 // ZB Launch Ball LedWiz signal is no longer turned on, turn
mjr 21:5048e16cc9ef 1689 // off the button.
mjr 21:5048e16cc9ef 1690 //
mjr 21:5048e16cc9ef 1691 // If we're in one of the Launch states (state #3 or #4),
mjr 21:5048e16cc9ef 1692 // and the button has been on for long enough, turn it off.
mjr 21:5048e16cc9ef 1693 // The Launch mode is triggered by a pull-and-release gesture.
mjr 21:5048e16cc9ef 1694 // From the user's perspective, this is just a single gesture
mjr 21:5048e16cc9ef 1695 // that should trigger just one momentary press on the Launch
mjr 21:5048e16cc9ef 1696 // Ball button. Physically, though, the plunger usually
mjr 21:5048e16cc9ef 1697 // bounces back and forth for 500ms or so before coming to
mjr 21:5048e16cc9ef 1698 // rest after this gesture. That's what the whole state
mjr 21:5048e16cc9ef 1699 // #3-#4 business is all about - we stay in this pair of
mjr 21:5048e16cc9ef 1700 // states until the plunger comes to rest. As long as we're
mjr 21:5048e16cc9ef 1701 // in these states, we won't send duplicate button presses.
mjr 21:5048e16cc9ef 1702 // But we also don't want the one button press to continue
mjr 21:5048e16cc9ef 1703 // the whole time, so we'll time it out now.
mjr 21:5048e16cc9ef 1704 //
mjr 21:5048e16cc9ef 1705 // (This could be written as one big 'if' condition, but
mjr 21:5048e16cc9ef 1706 // I'm breaking it out verbosely like this to make it easier
mjr 21:5048e16cc9ef 1707 // for human readers such as myself to comprehend the logic.)
mjr 21:5048e16cc9ef 1708 if ((simButtons & lbButtonBit) != 0)
mjr 18:5e890ebd0023 1709 {
mjr 21:5048e16cc9ef 1710 int turnOff = false;
mjr 21:5048e16cc9ef 1711
mjr 21:5048e16cc9ef 1712 // turn it off if the ZB Launch Ball signal is off
mjr 21:5048e16cc9ef 1713 if (!wizOn[ZBLaunchBallPort-1])
mjr 21:5048e16cc9ef 1714 turnOff = true;
mjr 21:5048e16cc9ef 1715
mjr 21:5048e16cc9ef 1716 // also turn it off if we're in state 3 or 4 ("Launch"),
mjr 21:5048e16cc9ef 1717 // and the button has been on long enough
mjr 21:5048e16cc9ef 1718 if ((lbState == 3 || lbState == 4) && lbBtnTimer.read_ms() > 250)
mjr 21:5048e16cc9ef 1719 turnOff = true;
mjr 21:5048e16cc9ef 1720
mjr 21:5048e16cc9ef 1721 // if we decided to turn off the button, do so
mjr 21:5048e16cc9ef 1722 if (turnOff)
mjr 21:5048e16cc9ef 1723 {
mjr 21:5048e16cc9ef 1724 lbBtnTimer.stop();
mjr 21:5048e16cc9ef 1725 simButtons &= ~lbButtonBit;
mjr 21:5048e16cc9ef 1726 }
mjr 18:5e890ebd0023 1727 }
mjr 17:ab3cec0c8bf4 1728 }
mjr 17:ab3cec0c8bf4 1729
mjr 17:ab3cec0c8bf4 1730 // If a firing event is in progress, generate synthetic reports to
mjr 17:ab3cec0c8bf4 1731 // describe an idealized version of the plunger motion to VP rather
mjr 17:ab3cec0c8bf4 1732 // than reporting the actual physical plunger position.
mjr 6:cc35eb643e8f 1733 //
mjr 17:ab3cec0c8bf4 1734 // We use the synthetic reports during a release event because the
mjr 17:ab3cec0c8bf4 1735 // physical plunger motion when released is too fast for VP to track.
mjr 17:ab3cec0c8bf4 1736 // VP only syncs its internal physics model with the outside world
mjr 17:ab3cec0c8bf4 1737 // about every 10ms. In that amount of time, the plunger moves
mjr 17:ab3cec0c8bf4 1738 // fast enough when released that it can shoot all the way forward,
mjr 17:ab3cec0c8bf4 1739 // bounce off of the barrel spring, and rebound part of the way
mjr 17:ab3cec0c8bf4 1740 // back. The result is the classic analog-to-digital problem of
mjr 17:ab3cec0c8bf4 1741 // sample aliasing. If we happen to time our sample during the
mjr 17:ab3cec0c8bf4 1742 // release motion so that we catch the plunger at the peak of a
mjr 17:ab3cec0c8bf4 1743 // bounce, the digital signal incorrectly looks like the plunger
mjr 17:ab3cec0c8bf4 1744 // is moving slowly forward - VP thinks we went from fully
mjr 17:ab3cec0c8bf4 1745 // retracted to half retracted in the sample interval, whereas
mjr 17:ab3cec0c8bf4 1746 // we actually traveled all the way forward and half way back,
mjr 17:ab3cec0c8bf4 1747 // so the speed VP infers is about 1/3 of the actual speed.
mjr 9:fd65b0a94720 1748 //
mjr 17:ab3cec0c8bf4 1749 // To correct this, we take advantage of our ability to sample
mjr 17:ab3cec0c8bf4 1750 // the CCD image several times in the course of a VP report. If
mjr 17:ab3cec0c8bf4 1751 // we catch the plunger near the origin after we've seen it
mjr 17:ab3cec0c8bf4 1752 // retracted, we go into Release Event mode. During this mode,
mjr 17:ab3cec0c8bf4 1753 // we stop reporting the true physical plunger position, and
mjr 17:ab3cec0c8bf4 1754 // instead report an idealized pattern: we report the plunger
mjr 17:ab3cec0c8bf4 1755 // immediately shooting forward to a position in front of the
mjr 17:ab3cec0c8bf4 1756 // park position that's in proportion to how far back the plunger
mjr 17:ab3cec0c8bf4 1757 // was just before the release, and we then report it stationary
mjr 17:ab3cec0c8bf4 1758 // at the park position. We continue to report the stationary
mjr 17:ab3cec0c8bf4 1759 // park position until the actual physical plunger motion has
mjr 17:ab3cec0c8bf4 1760 // stabilized on a new position. We then exit Release Event
mjr 17:ab3cec0c8bf4 1761 // mode and return to reporting the true physical position.
mjr 17:ab3cec0c8bf4 1762 if (firing)
mjr 6:cc35eb643e8f 1763 {
mjr 17:ab3cec0c8bf4 1764 // Firing in progress. Keep reporting the park position
mjr 17:ab3cec0c8bf4 1765 // until the physical plunger position comes to rest.
mjr 17:ab3cec0c8bf4 1766 const int restTol = JOYMAX/24;
mjr 17:ab3cec0c8bf4 1767 if (firing == 1)
mjr 6:cc35eb643e8f 1768 {
mjr 17:ab3cec0c8bf4 1769 // For the first couple of frames, show the plunger shooting
mjr 17:ab3cec0c8bf4 1770 // forward past the zero point, to simulate the momentum carrying
mjr 17:ab3cec0c8bf4 1771 // it forward to bounce off of the barrel spring. Show the
mjr 17:ab3cec0c8bf4 1772 // bounce as proportional to the distance it was retracted
mjr 17:ab3cec0c8bf4 1773 // in the prior report.
mjr 17:ab3cec0c8bf4 1774 z = zBounce = -z0/6;
mjr 17:ab3cec0c8bf4 1775 ++firing;
mjr 6:cc35eb643e8f 1776 }
mjr 17:ab3cec0c8bf4 1777 else if (firing == 2)
mjr 9:fd65b0a94720 1778 {
mjr 17:ab3cec0c8bf4 1779 // second frame - keep the bounce a little longer
mjr 17:ab3cec0c8bf4 1780 z = zBounce;
mjr 17:ab3cec0c8bf4 1781 ++firing;
mjr 17:ab3cec0c8bf4 1782 }
mjr 17:ab3cec0c8bf4 1783 else if (firing > 4
mjr 17:ab3cec0c8bf4 1784 && abs(znew - z0) < restTol
mjr 17:ab3cec0c8bf4 1785 && abs(znew - z1) < restTol
mjr 17:ab3cec0c8bf4 1786 && abs(znew - z2) < restTol)
mjr 17:ab3cec0c8bf4 1787 {
mjr 17:ab3cec0c8bf4 1788 // The physical plunger has come to rest. Exit firing
mjr 17:ab3cec0c8bf4 1789 // mode and resume reporting the actual position.
mjr 17:ab3cec0c8bf4 1790 firing = false;
mjr 17:ab3cec0c8bf4 1791 z = znew;
mjr 9:fd65b0a94720 1792 }
mjr 9:fd65b0a94720 1793 else
mjr 9:fd65b0a94720 1794 {
mjr 17:ab3cec0c8bf4 1795 // until the physical plunger comes to rest, simply
mjr 17:ab3cec0c8bf4 1796 // report the park position
mjr 9:fd65b0a94720 1797 z = 0;
mjr 17:ab3cec0c8bf4 1798 ++firing;
mjr 9:fd65b0a94720 1799 }
mjr 6:cc35eb643e8f 1800 }
mjr 6:cc35eb643e8f 1801 else
mjr 6:cc35eb643e8f 1802 {
mjr 17:ab3cec0c8bf4 1803 // not in firing mode - report the true physical position
mjr 17:ab3cec0c8bf4 1804 z = znew;
mjr 6:cc35eb643e8f 1805 }
mjr 17:ab3cec0c8bf4 1806
mjr 17:ab3cec0c8bf4 1807 // shift the new reading into the recent history buffer
mjr 6:cc35eb643e8f 1808 z2 = z1;
mjr 6:cc35eb643e8f 1809 z1 = z0;
mjr 6:cc35eb643e8f 1810 z0 = znew;
mjr 2:c174f9ee414a 1811 }
mjr 6:cc35eb643e8f 1812
mjr 11:bd9da7088e6e 1813 // update the buttons
mjr 18:5e890ebd0023 1814 uint32_t buttons = readButtons();
mjr 17:ab3cec0c8bf4 1815
mjr 21:5048e16cc9ef 1816 #ifdef ENABLE_JOYSTICK
mjr 17:ab3cec0c8bf4 1817 // If it's been long enough since our last USB status report,
mjr 17:ab3cec0c8bf4 1818 // send the new report. We throttle the report rate because
mjr 17:ab3cec0c8bf4 1819 // it can overwhelm the PC side if we report too frequently.
mjr 17:ab3cec0c8bf4 1820 // VP only wants to sync with the real world in 10ms intervals,
mjr 17:ab3cec0c8bf4 1821 // so reporting more frequently only creates i/o overhead
mjr 17:ab3cec0c8bf4 1822 // without doing anything to improve the simulation.
mjr 17:ab3cec0c8bf4 1823 if (reportTimer.read_ms() > 15)
mjr 17:ab3cec0c8bf4 1824 {
mjr 17:ab3cec0c8bf4 1825 // read the accelerometer
mjr 17:ab3cec0c8bf4 1826 int xa, ya;
mjr 17:ab3cec0c8bf4 1827 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 1828
mjr 17:ab3cec0c8bf4 1829 // confine the results to our joystick axis range
mjr 17:ab3cec0c8bf4 1830 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 17:ab3cec0c8bf4 1831 if (xa > JOYMAX) xa = JOYMAX;
mjr 17:ab3cec0c8bf4 1832 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 17:ab3cec0c8bf4 1833 if (ya > JOYMAX) ya = JOYMAX;
mjr 17:ab3cec0c8bf4 1834
mjr 17:ab3cec0c8bf4 1835 // store the updated accelerometer coordinates
mjr 17:ab3cec0c8bf4 1836 x = xa;
mjr 17:ab3cec0c8bf4 1837 y = ya;
mjr 17:ab3cec0c8bf4 1838
mjr 21:5048e16cc9ef 1839 // Report the current plunger position UNLESS the ZB Launch Ball
mjr 21:5048e16cc9ef 1840 // signal is on, in which case just report a constant 0 value.
mjr 21:5048e16cc9ef 1841 // ZB Launch Ball turns off the plunger position because it
mjr 21:5048e16cc9ef 1842 // tells us that the table has a Launch Ball button instead of
mjr 21:5048e16cc9ef 1843 // a traditional plunger.
mjr 21:5048e16cc9ef 1844 int zrep = (ZBLaunchBallPort != 0 && wizOn[ZBLaunchBallPort-1] ? 0 : z);
mjr 21:5048e16cc9ef 1845
mjr 25:e22b88bd783a 1846 // Send the status report. Note that we have to map the X and Y
mjr 25:e22b88bd783a 1847 // axes from the accelerometer to match the Windows joystick axes.
mjr 25:e22b88bd783a 1848 // The mapping is determined according to the mounting direction
mjr 25:e22b88bd783a 1849 // set in config.h via the ORIENTATION_xxx macros.
mjr 25:e22b88bd783a 1850 js.update(JOY_X(x,y), JOY_Y(x,y), zrep, buttons | simButtons, statusFlags);
mjr 17:ab3cec0c8bf4 1851
mjr 17:ab3cec0c8bf4 1852 // we've just started a new report interval, so reset the timer
mjr 17:ab3cec0c8bf4 1853 reportTimer.reset();
mjr 17:ab3cec0c8bf4 1854 }
mjr 21:5048e16cc9ef 1855
mjr 10:976666ffa4ef 1856 // If we're in pixel dump mode, report all pixel exposure values
mjr 10:976666ffa4ef 1857 if (reportPix)
mjr 10:976666ffa4ef 1858 {
mjr 17:ab3cec0c8bf4 1859 // send the report
mjr 17:ab3cec0c8bf4 1860 plungerSensor.sendExposureReport(js);
mjr 17:ab3cec0c8bf4 1861
mjr 10:976666ffa4ef 1862 // we have satisfied this request
mjr 10:976666ffa4ef 1863 reportPix = false;
mjr 10:976666ffa4ef 1864 }
mjr 10:976666ffa4ef 1865
mjr 21:5048e16cc9ef 1866 #else // ENABLE_JOYSTICK
mjr 21:5048e16cc9ef 1867 // We're a secondary controller, with no joystick reporting. Send
mjr 21:5048e16cc9ef 1868 // a generic status report to the host periodically for the sake of
mjr 21:5048e16cc9ef 1869 // the Windows config tool.
mjr 21:5048e16cc9ef 1870 if (reportTimer.read_ms() > 200)
mjr 21:5048e16cc9ef 1871 {
mjr 21:5048e16cc9ef 1872 js.updateStatus(0);
mjr 21:5048e16cc9ef 1873 }
mjr 21:5048e16cc9ef 1874
mjr 21:5048e16cc9ef 1875 #endif // ENABLE_JOYSTICK
mjr 21:5048e16cc9ef 1876
mjr 6:cc35eb643e8f 1877 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 1878 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 1879 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 1880 #endif
mjr 6:cc35eb643e8f 1881
mjr 6:cc35eb643e8f 1882 // provide a visual status indication on the on-board LED
mjr 5:a70c0bce770d 1883 if (calBtnState < 2 && hbTimer.read_ms() > 1000)
mjr 1:d913e0afb2ac 1884 {
mjr 5:a70c0bce770d 1885 if (js.isSuspended() || !js.isConnected())
mjr 2:c174f9ee414a 1886 {
mjr 5:a70c0bce770d 1887 // suspended - turn off the LED
mjr 4:02c7cd7b2183 1888 ledR = 1;
mjr 4:02c7cd7b2183 1889 ledG = 1;
mjr 4:02c7cd7b2183 1890 ledB = 1;
mjr 5:a70c0bce770d 1891
mjr 5:a70c0bce770d 1892 // show a status flash every so often
mjr 5:a70c0bce770d 1893 if (hbcnt % 3 == 0)
mjr 5:a70c0bce770d 1894 {
mjr 6:cc35eb643e8f 1895 // disconnected = red/red flash; suspended = red
mjr 5:a70c0bce770d 1896 for (int n = js.isConnected() ? 1 : 2 ; n > 0 ; --n)
mjr 5:a70c0bce770d 1897 {
mjr 5:a70c0bce770d 1898 ledR = 0;
mjr 5:a70c0bce770d 1899 wait(0.05);
mjr 5:a70c0bce770d 1900 ledR = 1;
mjr 5:a70c0bce770d 1901 wait(0.25);
mjr 5:a70c0bce770d 1902 }
mjr 5:a70c0bce770d 1903 }
mjr 2:c174f9ee414a 1904 }
mjr 6:cc35eb643e8f 1905 else if (needReset)
mjr 2:c174f9ee414a 1906 {
mjr 6:cc35eb643e8f 1907 // connected, need to reset due to changes in config parameters -
mjr 6:cc35eb643e8f 1908 // flash red/green
mjr 6:cc35eb643e8f 1909 hb = !hb;
mjr 6:cc35eb643e8f 1910 ledR = (hb ? 0 : 1);
mjr 6:cc35eb643e8f 1911 ledG = (hb ? 1 : 0);
mjr 6:cc35eb643e8f 1912 ledB = 0;
mjr 6:cc35eb643e8f 1913 }
mjr 17:ab3cec0c8bf4 1914 else if (cfg.d.plungerEnabled && !cfg.d.plungerCal)
mjr 6:cc35eb643e8f 1915 {
mjr 6:cc35eb643e8f 1916 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 1917 hb = !hb;
mjr 6:cc35eb643e8f 1918 ledR = (hb ? 0 : 1);
mjr 6:cc35eb643e8f 1919 ledG = 0;
mjr 6:cc35eb643e8f 1920 ledB = 1;
mjr 6:cc35eb643e8f 1921 }
mjr 6:cc35eb643e8f 1922 else
mjr 6:cc35eb643e8f 1923 {
mjr 6:cc35eb643e8f 1924 // connected - flash blue/green
mjr 2:c174f9ee414a 1925 hb = !hb;
mjr 4:02c7cd7b2183 1926 ledR = 1;
mjr 4:02c7cd7b2183 1927 ledG = (hb ? 0 : 1);
mjr 4:02c7cd7b2183 1928 ledB = (hb ? 1 : 0);
mjr 2:c174f9ee414a 1929 }
mjr 1:d913e0afb2ac 1930
mjr 1:d913e0afb2ac 1931 // reset the heartbeat timer
mjr 1:d913e0afb2ac 1932 hbTimer.reset();
mjr 5:a70c0bce770d 1933 ++hbcnt;
mjr 1:d913e0afb2ac 1934 }
mjr 1:d913e0afb2ac 1935 }
mjr 0:5acbbe3f4cf4 1936 }