work in progress

Dependencies:   FastAnalogIn FastIO USBDevice mbed FastPWM SimpleDMA

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Wed Aug 27 00:43:20 2014 +0000
Revision:
12:669df364a565
Parent:
11:bd9da7088e6e
Child:
13:72dda449c3c0
Fixed the indicator light (broken by bug in button reader)

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 5:a70c0bce770d 22 // "Pinscape" is the name of my custom-built virtual pinball cabinet. I wrote this
mjr 5:a70c0bce770d 23 // software to perform a number of tasks that I needed for my cabinet. It runs on a
mjr 5:a70c0bce770d 24 // Freescale KL25Z microcontroller, which is a small and inexpensive device that
mjr 5:a70c0bce770d 25 // attaches to the host PC via USB and can interface with numerous types of external
mjr 5:a70c0bce770d 26 // hardware.
mjr 5:a70c0bce770d 27 //
mjr 5:a70c0bce770d 28 // I designed the software and hardware in this project especially for Pinscape, but
mjr 5:a70c0bce770d 29 // it uses standard interfaces in Windows and Visual Pinball, so it should be
mjr 5:a70c0bce770d 30 // readily usable in anyone else's VP-based cabinet. I've tried to document the
mjr 5:a70c0bce770d 31 // hardware in enough detail for anyone else to duplicate the entire project, and
mjr 5:a70c0bce770d 32 // the full software is open source.
mjr 5:a70c0bce770d 33 //
mjr 6:cc35eb643e8f 34 // The device appears to the host computer as a USB joystick. This works with the
mjr 6:cc35eb643e8f 35 // standard Windows joystick device drivers, so there's no need to install any
mjr 6:cc35eb643e8f 36 // software on the PC - Windows should recognize it as a joystick when you plug
mjr 6:cc35eb643e8f 37 // it in and shouldn't ask you to install anything. If you bring up the control
mjr 6:cc35eb643e8f 38 // panel for USB Game Controllers, this device will appear as "Pinscape Controller".
mjr 6:cc35eb643e8f 39 // *Don't* do any calibration with the Windows control panel or third-part
mjr 6:cc35eb643e8f 40 // calibration tools. The device calibrates itself automatically for the
mjr 6:cc35eb643e8f 41 // accelerometer data, and has its own special calibration procedure for the
mjr 6:cc35eb643e8f 42 // plunger (see below).
mjr 6:cc35eb643e8f 43 //
mjr 5:a70c0bce770d 44 // The controller provides the following functions. It should be possible to use
mjr 5:a70c0bce770d 45 // any subet of the features without using all of them. External hardware for any
mjr 5:a70c0bce770d 46 // particular function can simply be omitted if that feature isn't needed.
mjr 5:a70c0bce770d 47 //
mjr 5:a70c0bce770d 48 // - Nudge sensing via the KL25Z's on-board accelerometer. Nudge accelerations are
mjr 5:a70c0bce770d 49 // processed into a physics model of a rolling ball, and changes to the ball's
mjr 5:a70c0bce770d 50 // motion are sent to the host computer via the joystick interface. This is designed
mjr 5:a70c0bce770d 51 // especially to work with Visuall Pinball's nudge handling to produce realistic
mjr 5:a70c0bce770d 52 // on-screen results in VP. By doing some physics modeling right on the device,
mjr 5:a70c0bce770d 53 // rather than sending raw accelerometer data to VP, we can produce better results
mjr 5:a70c0bce770d 54 // using our awareness of the real physical parameters of a pinball cabinet.
mjr 5:a70c0bce770d 55 // VP's nudge handling has to be more generic, so it can't make the same sorts
mjr 5:a70c0bce770d 56 // of assumptions that we can about the dynamics of a real cabinet.
mjr 5:a70c0bce770d 57 //
mjr 5:a70c0bce770d 58 // The nudge data reports are compatible with the built-in Windows USB joystick
mjr 5:a70c0bce770d 59 // drivers and with VP's own joystick input scheme, so the nudge sensing is almost
mjr 5:a70c0bce770d 60 // plug-and-play. There are no Windiows drivers to install, and the only VP work
mjr 5:a70c0bce770d 61 // needed is to customize a few global preference settings.
mjr 5:a70c0bce770d 62 //
mjr 5:a70c0bce770d 63 // - Plunger position sensing via an attached TAOS TSL 1410R CCD linear array sensor.
mjr 5:a70c0bce770d 64 // The sensor must be wired to a particular set of I/O ports on the KL25Z, and must
mjr 5:a70c0bce770d 65 // be positioned adjacent to the plunger with proper lighting. The physical and
mjr 5:a70c0bce770d 66 // electronic installation details are desribed in the project documentation. We read
mjr 5:a70c0bce770d 67 // the CCD to determine how far back the plunger is pulled, and report this to Visual
mjr 5:a70c0bce770d 68 // Pinball via the joystick interface. As with the nudge data, this is all nearly
mjr 5:a70c0bce770d 69 // plug-and-play, in that it works with the default Windows USB drivers and works
mjr 5:a70c0bce770d 70 // with the existing VP handling for analog plunger input. A few VP settings are
mjr 5:a70c0bce770d 71 // needed to tell VP to allow the plunger.
mjr 5:a70c0bce770d 72 //
mjr 6:cc35eb643e8f 73 // For best results, the plunger sensor should be calibrated. The calibration
mjr 6:cc35eb643e8f 74 // is stored in non-volatile memory on board the KL25Z, so it's only necessary
mjr 6:cc35eb643e8f 75 // to do the calibration once, when you first install everything. (You might
mjr 6:cc35eb643e8f 76 // also want to re-calibrate if you physically remove and reinstall the CCD
mjr 6:cc35eb643e8f 77 // sensor or the mechanical plunger, since their alignment might change slightly
mjr 6:cc35eb643e8f 78 // when you put everything back together.) To calibrate, you have to attach a
mjr 6:cc35eb643e8f 79 // momentary switch (e.g., a push-button switch) between one of the KL25Z ground
mjr 6:cc35eb643e8f 80 // pins (e.g., jumper J9 pin 12) and PTE29 (J10 pin 9). Press and hold the
mjr 6:cc35eb643e8f 81 // button for about two seconds - the LED on the KL25Z wlil flash blue while
mjr 6:cc35eb643e8f 82 // you hold the button, and will turn solid blue when you've held it down long
mjr 6:cc35eb643e8f 83 // enough to enter calibration mode. This mode will last about 15 seconds.
mjr 6:cc35eb643e8f 84 // Simply pull the plunger all the way back, hold it for a few moments, and
mjr 6:cc35eb643e8f 85 // gradually return it to the starting position. *Don't* release it - we want
mjr 6:cc35eb643e8f 86 // to measure the maximum retracted position and the rest position, but NOT
mjr 6:cc35eb643e8f 87 // the maximum forward position when the outer barrel spring is compressed.
mjr 6:cc35eb643e8f 88 // After about 15 seconds, the device will save the new calibration settings
mjr 6:cc35eb643e8f 89 // to its flash memory, and the LED will return to the regular "heartbeat"
mjr 6:cc35eb643e8f 90 // flashes. If this is the first time you calibrated, you should observe the
mjr 6:cc35eb643e8f 91 // color of the flashes change from yellow/green to blue/green to indicate
mjr 6:cc35eb643e8f 92 // that the plunger has been calibrated.
mjr 6:cc35eb643e8f 93 //
mjr 6:cc35eb643e8f 94 // Note that while Visual Pinball itself has good native support for analog
mjr 6:cc35eb643e8f 95 // plungers, most of the VP tables in circulation don't implement the necessary
mjr 6:cc35eb643e8f 96 // scripting features to make this work properly. Therefore, you'll have to do
mjr 6:cc35eb643e8f 97 // a little scripting work for each table you download to add the required code
mjr 6:cc35eb643e8f 98 // to that individual table. The work has to be customized for each table, so
mjr 6:cc35eb643e8f 99 // I haven't been able to automate this process, but I have tried to reduce it
mjr 6:cc35eb643e8f 100 // to a relatively simple recipe that I've documented separately.
mjr 5:a70c0bce770d 101 //
mjr 5:a70c0bce770d 102 // - In addition to the CCD sensor, a button should be attached (also described in
mjr 5:a70c0bce770d 103 // the project documentation) to activate calibration mode for the plunger. When
mjr 5:a70c0bce770d 104 // calibration mode is activated, the software reads the plunger position for about
mjr 5:a70c0bce770d 105 // 10 seconds when to note the limits of travel, and uses these limits to ensure
mjr 5:a70c0bce770d 106 // accurate reports to VP that properly report the actual position of the physical
mjr 5:a70c0bce770d 107 // plunger. The calibration is stored in non-volatile memory on the KL25Z, so it's
mjr 5:a70c0bce770d 108 // only necessary to calibrate once - the calibration will survive power cycling
mjr 5:a70c0bce770d 109 // and reboots of the PC. It's only necessary to recalibrate if the CCD sensor or
mjr 5:a70c0bce770d 110 // the plunger are removed and reinstalled, since the relative alignment of the
mjr 5:a70c0bce770d 111 // parts could cahnge slightly when reinstalling.
mjr 5:a70c0bce770d 112 //
mjr 5:a70c0bce770d 113 // - LedWiz emulation. The KL25Z can appear to the PC as an LedWiz device, and will
mjr 5:a70c0bce770d 114 // accept and process LedWiz commands from the host. The software can turn digital
mjr 5:a70c0bce770d 115 // output ports on and off, and can set varying PWM intensitiy levels on a subset
mjr 5:a70c0bce770d 116 // of ports. (The KL25Z can only provide 6 PWM ports. Intensity level settings on
mjr 5:a70c0bce770d 117 // other ports is ignored, so non-PWM ports can only be used for simple on/off
mjr 5:a70c0bce770d 118 // devices such as contactors and solenoids.) The KL25Z can only supply 4mA on its
mjr 5:a70c0bce770d 119 // output ports, so external hardware is required to take advantage of the LedWiz
mjr 5:a70c0bce770d 120 // emulation. Many different hardware designs are possible, but there's a simple
mjr 5:a70c0bce770d 121 // reference design in the documentation that uses a Darlington array IC to
mjr 5:a70c0bce770d 122 // increase the output from each port to 500mA (the same level as the LedWiz),
mjr 5:a70c0bce770d 123 // plus an extended design that adds an optocoupler and MOSFET to provide very
mjr 5:a70c0bce770d 124 // high power handling, up to about 45A or 150W, with voltages up to 100V.
mjr 5:a70c0bce770d 125 // That will handle just about any DC device directly (wtihout relays or other
mjr 5:a70c0bce770d 126 // amplifiers), and switches fast enough to support PWM devices.
mjr 5:a70c0bce770d 127 //
mjr 5:a70c0bce770d 128 // The device can report any desired LedWiz unit number to the host, which makes
mjr 5:a70c0bce770d 129 // it possible to use the LedWiz emulation on a machine that also has one or more
mjr 5:a70c0bce770d 130 // actual LedWiz devices intalled. The LedWiz design allows for up to 16 units
mjr 5:a70c0bce770d 131 // to be installed in one machine - each one is invidually addressable by its
mjr 5:a70c0bce770d 132 // distinct unit number.
mjr 5:a70c0bce770d 133 //
mjr 5:a70c0bce770d 134 // The LedWiz emulation features are of course optional. There's no need to
mjr 5:a70c0bce770d 135 // build any of the external port hardware (or attach anything to the output
mjr 5:a70c0bce770d 136 // ports at all) if the LedWiz features aren't needed. Most people won't have
mjr 5:a70c0bce770d 137 // any use for the LedWiz features. I built them mostly as a learning exercise,
mjr 5:a70c0bce770d 138 // but with a slight practical need for a handful of extra ports (I'm using the
mjr 5:a70c0bce770d 139 // cutting-edge 10-contactor setup, so my real LedWiz is full!).
mjr 6:cc35eb643e8f 140 //
mjr 6:cc35eb643e8f 141 // The on-board LED on the KL25Z flashes to indicate the current device status:
mjr 6:cc35eb643e8f 142 //
mjr 6:cc35eb643e8f 143 // two short red flashes = the device is powered but hasn't successfully
mjr 6:cc35eb643e8f 144 // connected to the host via USB (either it's not physically connected
mjr 6:cc35eb643e8f 145 // to the USB port, or there was a problem with the software handshake
mjr 6:cc35eb643e8f 146 // with the USB device driver on the computer)
mjr 6:cc35eb643e8f 147 //
mjr 6:cc35eb643e8f 148 // short red flash = the host computer is in sleep/suspend mode
mjr 6:cc35eb643e8f 149 //
mjr 6:cc35eb643e8f 150 // long red/green = the LedWiz unti number has been changed, so a reset
mjr 6:cc35eb643e8f 151 // is needed. You can simply unplug the device and plug it back in,
mjr 6:cc35eb643e8f 152 // or presss and hold the reset button on the device for a few seconds.
mjr 6:cc35eb643e8f 153 //
mjr 6:cc35eb643e8f 154 // long yellow/green = everything's working, but the plunger hasn't
mjr 6:cc35eb643e8f 155 // been calibrated; follow the calibration procedure described above.
mjr 6:cc35eb643e8f 156 // This flash mode won't appear if the CCD has been disabled. Note
mjr 6:cc35eb643e8f 157 // that the device can't tell whether a CCD is physically attached,
mjr 6:cc35eb643e8f 158 // so you should use the config command to disable the CCD software
mjr 6:cc35eb643e8f 159 // features if you won't be attaching a CCD.
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 5:a70c0bce770d 218
mjr 5:a70c0bce770d 219 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 220 //
mjr 5:a70c0bce770d 221 // Configuration details
mjr 5:a70c0bce770d 222 //
mjr 2:c174f9ee414a 223
mjr 5:a70c0bce770d 224 // Our USB device vendor ID, product ID, and version.
mjr 5:a70c0bce770d 225 // We use the vendor ID for the LedWiz, so that the PC-side software can
mjr 5:a70c0bce770d 226 // identify us as capable of performing LedWiz commands. The LedWiz uses
mjr 5:a70c0bce770d 227 // a product ID value from 0xF0 to 0xFF; the last four bits identify the
mjr 5:a70c0bce770d 228 // unit number (e.g., product ID 0xF7 means unit #7). This allows multiple
mjr 5:a70c0bce770d 229 // LedWiz units to be installed in a single PC; the software on the PC side
mjr 5:a70c0bce770d 230 // uses the unit number to route commands to the devices attached to each
mjr 5:a70c0bce770d 231 // unit. On the real LedWiz, the unit number must be set in the firmware
mjr 5:a70c0bce770d 232 // at the factory; it's not configurable by the end user. Most LedWiz's
mjr 5:a70c0bce770d 233 // ship with the unit number set to 0, but the vendor will set different
mjr 5:a70c0bce770d 234 // unit numbers if requested at the time of purchase. So if you have a
mjr 5:a70c0bce770d 235 // single LedWiz already installed in your cabinet, and you didn't ask for
mjr 5:a70c0bce770d 236 // a non-default unit number, your existing LedWiz will be unit 0.
mjr 5:a70c0bce770d 237 //
mjr 5:a70c0bce770d 238 // We use unit #7 by default. There doesn't seem to be a requirement that
mjr 5:a70c0bce770d 239 // unit numbers be contiguous (DirectOutput Framework and other software
mjr 5:a70c0bce770d 240 // seem happy to have units 0 and 7 installed, without 1-6 existing).
mjr 5:a70c0bce770d 241 // Marking this unit as #7 should work for almost everybody out of the box;
mjr 5:a70c0bce770d 242 // the most common case seems to be to have a single LedWiz installed, and
mjr 5:a70c0bce770d 243 // it's probably extremely rare to more than two.
mjr 6:cc35eb643e8f 244 //
mjr 6:cc35eb643e8f 245 // Note that the USB_PRODUCT_ID value set here omits the unit number. We
mjr 6:cc35eb643e8f 246 // take the unit number from the saved configuration. We provide a
mjr 6:cc35eb643e8f 247 // configuration command that can be sent via the USB connection to change
mjr 6:cc35eb643e8f 248 // the unit number, so that users can select the unit number without having
mjr 6:cc35eb643e8f 249 // to install a different version of the software. We'll combine the base
mjr 6:cc35eb643e8f 250 // product ID here with the unit number to get the actual product ID that
mjr 6:cc35eb643e8f 251 // we send to the USB controller.
mjr 5:a70c0bce770d 252 const uint16_t USB_VENDOR_ID = 0xFAFA;
mjr 6:cc35eb643e8f 253 const uint16_t USB_PRODUCT_ID = 0x00F0;
mjr 6:cc35eb643e8f 254 const uint16_t USB_VERSION_NO = 0x0006;
mjr 6:cc35eb643e8f 255 const uint8_t DEFAULT_LEDWIZ_UNIT_NUMBER = 0x07;
mjr 0:5acbbe3f4cf4 256
mjr 9:fd65b0a94720 257 // Number of pixels we read from the sensor on each frame. This can be
mjr 9:fd65b0a94720 258 // less than the physical pixel count if desired; we'll read every nth
mjr 9:fd65b0a94720 259 // piexl if so. E.g., with a 1280-pixel physical sensor, if npix is 320,
mjr 9:fd65b0a94720 260 // we'll read every 4th pixel. It takes time to read each pixel, so the
mjr 9:fd65b0a94720 261 // fewer pixels we read, the higher the refresh rate we can achieve.
mjr 9:fd65b0a94720 262 // It's therefore better not to read more pixels than we have to.
mjr 9:fd65b0a94720 263 //
mjr 9:fd65b0a94720 264 // VP seems to have an internal resolution in the 8-bit range, so there's
mjr 9:fd65b0a94720 265 // no apparent benefit to reading more than 128-256 pixels when using VP.
mjr 9:fd65b0a94720 266 // Empirically, 160 pixels seems about right. The overall travel of a
mjr 9:fd65b0a94720 267 // standard pinball plunger is about 3", so 160 pixels gives us resolution
mjr 9:fd65b0a94720 268 // of about 1/50". This seems to take full advantage of VP's modeling
mjr 9:fd65b0a94720 269 // ability, and is probably also more precise than a human player's
mjr 9:fd65b0a94720 270 // perception of the plunger position.
mjr 9:fd65b0a94720 271 const int npix = 160;
mjr 9:fd65b0a94720 272
mjr 4:02c7cd7b2183 273 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 4:02c7cd7b2183 274 DigitalOut ledR(LED1), ledG(LED2), ledB(LED3);
mjr 0:5acbbe3f4cf4 275
mjr 1:d913e0afb2ac 276 // calibration button - switch input and LED output
mjr 1:d913e0afb2ac 277 DigitalIn calBtn(PTE29);
mjr 1:d913e0afb2ac 278 DigitalOut calBtnLed(PTE23);
mjr 0:5acbbe3f4cf4 279
mjr 11:bd9da7088e6e 280 // Joystick button input pin assignments. You can wire up to
mjr 11:bd9da7088e6e 281 // 32 GPIO ports to buttons (equipped with momentary switches).
mjr 11:bd9da7088e6e 282 // Connect each switch between the desired GPIO port and ground
mjr 11:bd9da7088e6e 283 // (J9 pin 12 or 14). When the button is pressed, we'll tell the
mjr 11:bd9da7088e6e 284 // host PC that the corresponding joystick button as pressed. We
mjr 11:bd9da7088e6e 285 // debounce the keystrokes in software, so you can simply wire
mjr 11:bd9da7088e6e 286 // directly to pushbuttons with no additional external hardware.
mjr 11:bd9da7088e6e 287 //
mjr 11:bd9da7088e6e 288 // Note that we assign 24 buttons by default, even though the USB
mjr 11:bd9da7088e6e 289 // joystick interface can handle up to 32 buttons. VP itself only
mjr 11:bd9da7088e6e 290 // allows mapping of up to 24 buttons in the preferences dialog
mjr 11:bd9da7088e6e 291 // (although it can recognize 32 buttons internally). If you want
mjr 11:bd9da7088e6e 292 // more buttons, you can reassign pins that are assigned by default
mjr 11:bd9da7088e6e 293 // as LedWiz outputs. To reassign a pin, find the pin you wish to
mjr 11:bd9da7088e6e 294 // reassign in the LedWizPortMap array below, and change the pin name
mjr 11:bd9da7088e6e 295 // there to NC (for Not Connected). You can then change one of the
mjr 11:bd9da7088e6e 296 // "NC" entries below to the reallocated pin name. The limit is 32
mjr 11:bd9da7088e6e 297 // buttons total.
mjr 11:bd9da7088e6e 298 //
mjr 11:bd9da7088e6e 299 // Note: PTD1 (pin J2-12) should NOT be assigned as a button input,
mjr 11:bd9da7088e6e 300 // as this pin is physically connected on the KL25Z to the on-board
mjr 11:bd9da7088e6e 301 // indicator LED's blue segment. This precludes any other use of
mjr 11:bd9da7088e6e 302 // the pin.
mjr 11:bd9da7088e6e 303 PinName buttonMap[] = {
mjr 11:bd9da7088e6e 304 PTC2, // J10 pin 10, joystick button 1
mjr 11:bd9da7088e6e 305 PTB3, // J10 pin 8, joystick button 2
mjr 11:bd9da7088e6e 306 PTB2, // J10 pin 6, joystick button 3
mjr 11:bd9da7088e6e 307 PTB1, // J10 pin 4, joystick button 4
mjr 11:bd9da7088e6e 308
mjr 11:bd9da7088e6e 309 PTE30, // J10 pin 11, joystick button 5
mjr 11:bd9da7088e6e 310 PTE22, // J10 pin 5, joystick button 6
mjr 11:bd9da7088e6e 311
mjr 11:bd9da7088e6e 312 PTE5, // J9 pin 15, joystick button 7
mjr 11:bd9da7088e6e 313 PTE4, // J9 pin 13, joystick button 8
mjr 11:bd9da7088e6e 314 PTE3, // J9 pin 11, joystick button 9
mjr 11:bd9da7088e6e 315 PTE2, // J9 pin 9, joystick button 10
mjr 11:bd9da7088e6e 316 PTB11, // J9 pin 7, joystick button 11
mjr 11:bd9da7088e6e 317 PTB10, // J9 pin 5, joystick button 12
mjr 11:bd9da7088e6e 318 PTB9, // J9 pin 3, joystick button 13
mjr 11:bd9da7088e6e 319 PTB8, // J9 pin 1, joystick button 14
mjr 11:bd9da7088e6e 320
mjr 11:bd9da7088e6e 321 PTC12, // J2 pin 1, joystick button 15
mjr 11:bd9da7088e6e 322 PTC13, // J2 pin 3, joystick button 16
mjr 11:bd9da7088e6e 323 PTC16, // J2 pin 5, joystick button 17
mjr 11:bd9da7088e6e 324 PTC17, // J2 pin 7, joystick button 18
mjr 11:bd9da7088e6e 325 PTA16, // J2 pin 9, joystick button 19
mjr 11:bd9da7088e6e 326 PTA17, // J2 pin 11, joystick button 20
mjr 11:bd9da7088e6e 327 PTE31, // J2 pin 13, joystick button 21
mjr 11:bd9da7088e6e 328 PTD6, // J2 pin 17, joystick button 22
mjr 11:bd9da7088e6e 329 PTD7, // J2 pin 19, joystick button 23
mjr 11:bd9da7088e6e 330
mjr 11:bd9da7088e6e 331 PTE1, // J2 pin 20, joystick button 24
mjr 11:bd9da7088e6e 332
mjr 11:bd9da7088e6e 333 NC, // not used, joystick button 25
mjr 11:bd9da7088e6e 334 NC, // not used, joystick button 26
mjr 11:bd9da7088e6e 335 NC, // not used, joystick button 27
mjr 11:bd9da7088e6e 336 NC, // not used, joystick button 28
mjr 11:bd9da7088e6e 337 NC, // not used, joystick button 29
mjr 11:bd9da7088e6e 338 NC, // not used, joystick button 30
mjr 11:bd9da7088e6e 339 NC, // not used, joystick button 31
mjr 11:bd9da7088e6e 340 NC // not used, joystick button 32
mjr 11:bd9da7088e6e 341 };
mjr 11:bd9da7088e6e 342
mjr 11:bd9da7088e6e 343 // LED-Wiz emulation output pin assignments.
mjr 6:cc35eb643e8f 344 //
mjr 6:cc35eb643e8f 345 // The LED-Wiz protocol allows setting individual intensity levels
mjr 6:cc35eb643e8f 346 // on all outputs, with 48 levels of intensity. This can be used
mjr 6:cc35eb643e8f 347 // to control lamp brightness and motor speeds, among other things.
mjr 6:cc35eb643e8f 348 // Unfortunately, the KL25Z only has 10 PWM channels, so while we
mjr 6:cc35eb643e8f 349 // can support the full complement of 32 outputs, we can only provide
mjr 6:cc35eb643e8f 350 // PWM dimming/speed control on 10 of them. The remaining outputs
mjr 6:cc35eb643e8f 351 // can only be switched fully on and fully off - we can't support
mjr 6:cc35eb643e8f 352 // dimming on these, so they'll ignore any intensity level setting
mjr 6:cc35eb643e8f 353 // requested by the host. Use these for devices that don't have any
mjr 6:cc35eb643e8f 354 // use for intensity settings anyway, such as contactors and knockers.
mjr 6:cc35eb643e8f 355 //
mjr 11:bd9da7088e6e 356 // Ports with pins assigned as "NC" are not connected. That is,
mjr 11:bd9da7088e6e 357 // there's no physical pin for that LedWiz port number. You can
mjr 11:bd9da7088e6e 358 // send LedWiz commands to turn NC ports on and off, but doing so
mjr 11:bd9da7088e6e 359 // will have no effect. The reason we leave some ports unassigned
mjr 11:bd9da7088e6e 360 // is that we don't have enough physical GPIO pins to fill out the
mjr 11:bd9da7088e6e 361 // full LedWiz complement of 32 ports. Many pins are already taken
mjr 11:bd9da7088e6e 362 // for other purposes, such as button inputs or the plunger CCD
mjr 11:bd9da7088e6e 363 // interface.
mjr 11:bd9da7088e6e 364 //
mjr 6:cc35eb643e8f 365 // The mapping between physical output pins on the KL25Z and the
mjr 6:cc35eb643e8f 366 // assigned LED-Wiz port numbers is essentially arbitrary - you can
mjr 6:cc35eb643e8f 367 // customize this by changing the entries in the array below if you
mjr 6:cc35eb643e8f 368 // wish to rearrange the pins for any reason. Be aware that some
mjr 6:cc35eb643e8f 369 // of the physical outputs are already used for other purposes
mjr 6:cc35eb643e8f 370 // (e.g., some of the GPIO pins on header J10 are used for the
mjr 6:cc35eb643e8f 371 // CCD sensor - but you can of course reassign those as well by
mjr 6:cc35eb643e8f 372 // changing the corresponding declarations elsewhere in this module).
mjr 6:cc35eb643e8f 373 // The assignments we make here have two main objectives: first,
mjr 6:cc35eb643e8f 374 // to group the outputs on headers J1 and J2 (to facilitate neater
mjr 6:cc35eb643e8f 375 // wiring by keeping the output pins together physically), and
mjr 6:cc35eb643e8f 376 // second, to make the physical pin layout match the LED-Wiz port
mjr 6:cc35eb643e8f 377 // numbering order to the extent possible. There's one big wrench
mjr 6:cc35eb643e8f 378 // in the works, though, which is the limited number and discontiguous
mjr 6:cc35eb643e8f 379 // placement of the KL25Z PWM-capable output pins. This prevents
mjr 6:cc35eb643e8f 380 // us from doing the most obvious sequential ordering of the pins,
mjr 6:cc35eb643e8f 381 // so we end up with the outputs arranged into several blocks.
mjr 6:cc35eb643e8f 382 // Hopefully this isn't too confusing; for more detailed rationale,
mjr 6:cc35eb643e8f 383 // read on...
mjr 6:cc35eb643e8f 384 //
mjr 6:cc35eb643e8f 385 // With the LED-Wiz, the host software configuration usually
mjr 6:cc35eb643e8f 386 // assumes that each RGB LED is hooked up to three consecutive ports
mjr 6:cc35eb643e8f 387 // (for the red, green, and blue components, which need to be
mjr 6:cc35eb643e8f 388 // physically wired to separate outputs to allow each color to be
mjr 6:cc35eb643e8f 389 // controlled independently). To facilitate this, we arrange the
mjr 6:cc35eb643e8f 390 // PWM-enabled outputs so that they're grouped together in the
mjr 6:cc35eb643e8f 391 // port numbering scheme. Unfortunately, these outputs aren't
mjr 6:cc35eb643e8f 392 // together in a single group in the physical pin layout, so to
mjr 6:cc35eb643e8f 393 // group them logically in the LED-Wiz port numbering scheme, we
mjr 6:cc35eb643e8f 394 // have to break up the overall numbering scheme into several blocks.
mjr 6:cc35eb643e8f 395 // So our port numbering goes sequentially down each column of
mjr 6:cc35eb643e8f 396 // header pins, but there are several break points where we have
mjr 6:cc35eb643e8f 397 // to interrupt the obvious sequence to keep the PWM pins grouped
mjr 6:cc35eb643e8f 398 // logically.
mjr 6:cc35eb643e8f 399 //
mjr 6:cc35eb643e8f 400 // In the list below, "pin J1-2" refers to pin 2 on header J1 on
mjr 6:cc35eb643e8f 401 // the KL25Z, using the standard pin numbering in the KL25Z
mjr 6:cc35eb643e8f 402 // documentation - this is the physical pin that the port controls.
mjr 6:cc35eb643e8f 403 // "LW port 1" means LED-Wiz port 1 - this is the LED-Wiz port
mjr 6:cc35eb643e8f 404 // number that you use on the PC side (in the DirectOutput config
mjr 6:cc35eb643e8f 405 // file, for example) to address the port. PWM-capable ports are
mjr 6:cc35eb643e8f 406 // marked as such - we group the PWM-capable ports into the first
mjr 6:cc35eb643e8f 407 // 10 LED-Wiz port numbers.
mjr 11:bd9da7088e6e 408 //
mjr 11:bd9da7088e6e 409 // If you wish to reallocate a pin in the array below to some other
mjr 11:bd9da7088e6e 410 // use, such as a button input port, simply change the pin name in
mjr 11:bd9da7088e6e 411 // the entry to NC (for Not Connected). This will disable the given
mjr 11:bd9da7088e6e 412 // logical LedWiz port number and free up the physical pin.
mjr 11:bd9da7088e6e 413 //
mjr 11:bd9da7088e6e 414 // If you wish to reallocate a pin currently assigned to the button
mjr 11:bd9da7088e6e 415 // input array, simply change the entry for the pin in the buttonMap[]
mjr 11:bd9da7088e6e 416 // array above to NC (for "not connected"), and plug the pin name into
mjr 11:bd9da7088e6e 417 // a slot of your choice in the array below.
mjr 11:bd9da7088e6e 418 //
mjr 11:bd9da7088e6e 419 // Note: PTD1 (pin J2-12) should NOT be assigned as an LedWiz output,
mjr 11:bd9da7088e6e 420 // as this pin is physically connected on the KL25Z to the on-board
mjr 11:bd9da7088e6e 421 // indicator LED's blue segment. This precludes any other use of
mjr 11:bd9da7088e6e 422 // the pin.
mjr 6:cc35eb643e8f 423 //
mjr 6:cc35eb643e8f 424 struct {
mjr 6:cc35eb643e8f 425 PinName pin;
mjr 6:cc35eb643e8f 426 bool isPWM;
mjr 6:cc35eb643e8f 427 } ledWizPortMap[32] = {
mjr 6:cc35eb643e8f 428 { PTA1, true }, // pin J1-2, LW port 1 (PWM capable - TPM 2.0 = channel 9)
mjr 6:cc35eb643e8f 429 { PTA2, true }, // pin J1-4, LW port 2 (PWM capable - TPM 2.1 = channel 10)
mjr 6:cc35eb643e8f 430 { PTD4, true }, // pin J1-6, LW port 3 (PWM capable - TPM 0.4 = channel 5)
mjr 6:cc35eb643e8f 431 { PTA12, true }, // pin J1-8, LW port 4 (PWM capable - TPM 1.0 = channel 7)
mjr 6:cc35eb643e8f 432 { PTA4, true }, // pin J1-10, LW port 5 (PWM capable - TPM 0.1 = channel 2)
mjr 6:cc35eb643e8f 433 { PTA5, true }, // pin J1-12, LW port 6 (PWM capable - TPM 0.2 = channel 3)
mjr 6:cc35eb643e8f 434 { PTA13, true }, // pin J2-2, LW port 7 (PWM capable - TPM 1.1 = channel 13)
mjr 6:cc35eb643e8f 435 { PTD5, true }, // pin J2-4, LW port 8 (PWM capable - TPM 0.5 = channel 6)
mjr 6:cc35eb643e8f 436 { PTD0, true }, // pin J2-6, LW port 9 (PWM capable - TPM 0.0 = channel 1)
mjr 6:cc35eb643e8f 437 { PTD3, true }, // pin J2-10, LW port 10 (PWM capable - TPM 0.3 = channel 4)
mjr 9:fd65b0a94720 438 { PTD2, false }, // pin J2-8, LW port 11
mjr 9:fd65b0a94720 439 { PTC8, false }, // pin J1-14, LW port 12
mjr 9:fd65b0a94720 440 { PTC9, false }, // pin J1-16, LW port 13
mjr 9:fd65b0a94720 441 { PTC7, false }, // pin J1-1, LW port 14
mjr 9:fd65b0a94720 442 { PTC0, false }, // pin J1-3, LW port 15
mjr 9:fd65b0a94720 443 { PTC3, false }, // pin J1-5, LW port 16
mjr 9:fd65b0a94720 444 { PTC4, false }, // pin J1-7, LW port 17
mjr 9:fd65b0a94720 445 { PTC5, false }, // pin J1-9, LW port 18
mjr 9:fd65b0a94720 446 { PTC6, false }, // pin J1-11, LW port 19
mjr 9:fd65b0a94720 447 { PTC10, false }, // pin J1-13, LW port 20
mjr 9:fd65b0a94720 448 { PTC11, false }, // pin J1-15, LW port 21
mjr 11:bd9da7088e6e 449 { PTE0, false }, // pin J2-18, LW port 22
mjr 11:bd9da7088e6e 450 { NC, false }, // Not used, LW port 23
mjr 11:bd9da7088e6e 451 { NC, false }, // Not used, LW port 24
mjr 11:bd9da7088e6e 452 { NC, false }, // Not used, LW port 25
mjr 11:bd9da7088e6e 453 { NC, false }, // Not used, LW port 26
mjr 11:bd9da7088e6e 454 { NC, false }, // Not used, LW port 27
mjr 11:bd9da7088e6e 455 { NC, false }, // Not used, LW port 28
mjr 11:bd9da7088e6e 456 { NC, false }, // Not used, LW port 29
mjr 11:bd9da7088e6e 457 { NC, false }, // Not used, LW port 30
mjr 11:bd9da7088e6e 458 { NC, false }, // Not used, LW port 31
mjr 11:bd9da7088e6e 459 { NC, false } // Not used, LW port 32
mjr 6:cc35eb643e8f 460 };
mjr 6:cc35eb643e8f 461
mjr 6:cc35eb643e8f 462
mjr 5:a70c0bce770d 463 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 5:a70c0bce770d 464 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 5:a70c0bce770d 465
mjr 5:a70c0bce770d 466 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 5:a70c0bce770d 467 #define MMA8451_SCL_PIN PTE25
mjr 5:a70c0bce770d 468 #define MMA8451_SDA_PIN PTE24
mjr 5:a70c0bce770d 469
mjr 5:a70c0bce770d 470 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 5:a70c0bce770d 471 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 5:a70c0bce770d 472 // wired on this board to the MMA8451 interrupt controller.
mjr 5:a70c0bce770d 473 #define MMA8451_INT_PIN PTA15
mjr 5:a70c0bce770d 474
mjr 6:cc35eb643e8f 475 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 6:cc35eb643e8f 476 #define JOYMAX 4096
mjr 6:cc35eb643e8f 477
mjr 5:a70c0bce770d 478
mjr 5:a70c0bce770d 479 // ---------------------------------------------------------------------------
mjr 9:fd65b0a94720 480 // utilities
mjr 9:fd65b0a94720 481
mjr 9:fd65b0a94720 482 // number of elements in an array
mjr 9:fd65b0a94720 483 #define countof(x) (sizeof(x)/sizeof((x)[0]))
mjr 9:fd65b0a94720 484
mjr 9:fd65b0a94720 485 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 486 //
mjr 5:a70c0bce770d 487 // LedWiz emulation
mjr 5:a70c0bce770d 488 //
mjr 5:a70c0bce770d 489
mjr 0:5acbbe3f4cf4 490 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 491
mjr 6:cc35eb643e8f 492 // LedWiz output pin interface. We create a cover class to virtualize
mjr 6:cc35eb643e8f 493 // digital vs PWM outputs and give them a common interface. The KL25Z
mjr 6:cc35eb643e8f 494 // unfortunately doesn't have enough hardware PWM channels to support
mjr 6:cc35eb643e8f 495 // PWM on all 32 LedWiz outputs, so we provide as many PWM channels as
mjr 6:cc35eb643e8f 496 // we can (10), and fill out the rest of the outputs with plain digital
mjr 6:cc35eb643e8f 497 // outs.
mjr 6:cc35eb643e8f 498 class LwOut
mjr 6:cc35eb643e8f 499 {
mjr 6:cc35eb643e8f 500 public:
mjr 6:cc35eb643e8f 501 virtual void set(float val) = 0;
mjr 6:cc35eb643e8f 502 };
mjr 6:cc35eb643e8f 503 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 504 {
mjr 6:cc35eb643e8f 505 public:
mjr 6:cc35eb643e8f 506 LwPwmOut(PinName pin) : p(pin) { }
mjr 6:cc35eb643e8f 507 virtual void set(float val) { p = val; }
mjr 6:cc35eb643e8f 508 PwmOut p;
mjr 6:cc35eb643e8f 509 };
mjr 6:cc35eb643e8f 510 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 511 {
mjr 6:cc35eb643e8f 512 public:
mjr 6:cc35eb643e8f 513 LwDigOut(PinName pin) : p(pin) { }
mjr 6:cc35eb643e8f 514 virtual void set(float val) { p = val; }
mjr 6:cc35eb643e8f 515 DigitalOut p;
mjr 6:cc35eb643e8f 516 };
mjr 11:bd9da7088e6e 517 class LwUnusedOut: public LwOut
mjr 11:bd9da7088e6e 518 {
mjr 11:bd9da7088e6e 519 public:
mjr 11:bd9da7088e6e 520 LwUnusedOut() { }
mjr 11:bd9da7088e6e 521 virtual void set(float val) { }
mjr 11:bd9da7088e6e 522 };
mjr 6:cc35eb643e8f 523
mjr 6:cc35eb643e8f 524 // output pin array
mjr 6:cc35eb643e8f 525 static LwOut *lwPin[32];
mjr 6:cc35eb643e8f 526
mjr 6:cc35eb643e8f 527 // initialize the output pin array
mjr 6:cc35eb643e8f 528 void initLwOut()
mjr 6:cc35eb643e8f 529 {
mjr 9:fd65b0a94720 530 for (int i = 0 ; i < countof(lwPin) ; ++i)
mjr 6:cc35eb643e8f 531 {
mjr 11:bd9da7088e6e 532 PinName p = (i < countof(ledWizPortMap) ? ledWizPortMap[i].pin : NC);
mjr 11:bd9da7088e6e 533 if (p == NC)
mjr 11:bd9da7088e6e 534 lwPin[i] = new LwUnusedOut();
mjr 11:bd9da7088e6e 535 else if (ledWizPortMap[i].isPWM)
mjr 11:bd9da7088e6e 536 lwPin[i] = new LwPwmOut(p);
mjr 11:bd9da7088e6e 537 else
mjr 11:bd9da7088e6e 538 lwPin[i] = new LwDigOut(p);
mjr 6:cc35eb643e8f 539 }
mjr 6:cc35eb643e8f 540 }
mjr 6:cc35eb643e8f 541
mjr 0:5acbbe3f4cf4 542 // on/off state for each LedWiz output
mjr 1:d913e0afb2ac 543 static uint8_t wizOn[32];
mjr 0:5acbbe3f4cf4 544
mjr 0:5acbbe3f4cf4 545 // profile (brightness/blink) state for each LedWiz output
mjr 1:d913e0afb2ac 546 static uint8_t wizVal[32] = {
mjr 0:5acbbe3f4cf4 547 0, 0, 0, 0, 0, 0, 0, 0,
mjr 0:5acbbe3f4cf4 548 0, 0, 0, 0, 0, 0, 0, 0,
mjr 0:5acbbe3f4cf4 549 0, 0, 0, 0, 0, 0, 0, 0,
mjr 0:5acbbe3f4cf4 550 0, 0, 0, 0, 0, 0, 0, 0
mjr 0:5acbbe3f4cf4 551 };
mjr 0:5acbbe3f4cf4 552
mjr 1:d913e0afb2ac 553 static float wizState(int idx)
mjr 0:5acbbe3f4cf4 554 {
mjr 1:d913e0afb2ac 555 if (wizOn[idx]) {
mjr 0:5acbbe3f4cf4 556 // on - map profile brightness state to PWM level
mjr 1:d913e0afb2ac 557 uint8_t val = wizVal[idx];
mjr 0:5acbbe3f4cf4 558 if (val >= 1 && val <= 48)
mjr 0:5acbbe3f4cf4 559 return 1.0 - val/48.0;
mjr 0:5acbbe3f4cf4 560 else if (val >= 129 && val <= 132)
mjr 0:5acbbe3f4cf4 561 return 0.0;
mjr 0:5acbbe3f4cf4 562 else
mjr 0:5acbbe3f4cf4 563 return 1.0;
mjr 0:5acbbe3f4cf4 564 }
mjr 0:5acbbe3f4cf4 565 else {
mjr 0:5acbbe3f4cf4 566 // off
mjr 0:5acbbe3f4cf4 567 return 1.0;
mjr 0:5acbbe3f4cf4 568 }
mjr 0:5acbbe3f4cf4 569 }
mjr 0:5acbbe3f4cf4 570
mjr 1:d913e0afb2ac 571 static void updateWizOuts()
mjr 1:d913e0afb2ac 572 {
mjr 6:cc35eb643e8f 573 for (int i = 0 ; i < 32 ; ++i)
mjr 6:cc35eb643e8f 574 lwPin[i]->set(wizState(i));
mjr 1:d913e0afb2ac 575 }
mjr 1:d913e0afb2ac 576
mjr 11:bd9da7088e6e 577
mjr 11:bd9da7088e6e 578 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 579 //
mjr 11:bd9da7088e6e 580 // Button input
mjr 11:bd9da7088e6e 581 //
mjr 11:bd9da7088e6e 582
mjr 11:bd9da7088e6e 583 // button input map array
mjr 11:bd9da7088e6e 584 DigitalIn *buttonDigIn[32];
mjr 11:bd9da7088e6e 585
mjr 12:669df364a565 586 // timer for button reports
mjr 12:669df364a565 587 static Timer buttonTimer;
mjr 12:669df364a565 588
mjr 11:bd9da7088e6e 589 // initialize the button inputs
mjr 11:bd9da7088e6e 590 void initButtons()
mjr 11:bd9da7088e6e 591 {
mjr 11:bd9da7088e6e 592 // create the digital inputs
mjr 11:bd9da7088e6e 593 for (int i = 0 ; i < countof(buttonDigIn) ; ++i)
mjr 11:bd9da7088e6e 594 {
mjr 11:bd9da7088e6e 595 if (i < countof(buttonMap) && buttonMap[i] != NC)
mjr 11:bd9da7088e6e 596 buttonDigIn[i] = new DigitalIn(buttonMap[i]);
mjr 11:bd9da7088e6e 597 else
mjr 11:bd9da7088e6e 598 buttonDigIn[i] = 0;
mjr 11:bd9da7088e6e 599 }
mjr 12:669df364a565 600
mjr 12:669df364a565 601 // start the button timer
mjr 12:669df364a565 602 buttonTimer.start();
mjr 11:bd9da7088e6e 603 }
mjr 11:bd9da7088e6e 604
mjr 11:bd9da7088e6e 605
mjr 11:bd9da7088e6e 606 // read the raw button input state
mjr 11:bd9da7088e6e 607 uint32_t readButtonsRaw()
mjr 11:bd9da7088e6e 608 {
mjr 11:bd9da7088e6e 609 // start with all buttons off
mjr 11:bd9da7088e6e 610 uint32_t buttons = 0;
mjr 11:bd9da7088e6e 611
mjr 11:bd9da7088e6e 612 // scan the button list
mjr 11:bd9da7088e6e 613 uint32_t bit = 1;
mjr 11:bd9da7088e6e 614 for (int i = 0 ; i < countof(buttonDigIn) ; ++i, bit <<= 1)
mjr 11:bd9da7088e6e 615 {
mjr 11:bd9da7088e6e 616 if (buttonDigIn[i] != 0 && !buttonDigIn[i]->read())
mjr 11:bd9da7088e6e 617 buttons |= bit;
mjr 11:bd9da7088e6e 618 }
mjr 11:bd9da7088e6e 619
mjr 11:bd9da7088e6e 620 // return the button list
mjr 11:bd9da7088e6e 621 return buttons;
mjr 11:bd9da7088e6e 622 }
mjr 11:bd9da7088e6e 623
mjr 11:bd9da7088e6e 624 // Read buttons with debouncing. We keep a circular buffer
mjr 11:bd9da7088e6e 625 // of recent input readings. We'll AND together the status of
mjr 11:bd9da7088e6e 626 // each button over the past 50ms. A button that has been on
mjr 11:bd9da7088e6e 627 // continuously for 50ms will be reported as ON. All others
mjr 11:bd9da7088e6e 628 // will be reported as OFF.
mjr 11:bd9da7088e6e 629 uint32_t readButtonsDebounced()
mjr 11:bd9da7088e6e 630 {
mjr 11:bd9da7088e6e 631 struct reading {
mjr 11:bd9da7088e6e 632 int dt; // time since previous reading
mjr 11:bd9da7088e6e 633 uint32_t b; // button state at this reading
mjr 11:bd9da7088e6e 634 };
mjr 11:bd9da7088e6e 635 static reading readings[8]; // circular buffer of readings
mjr 11:bd9da7088e6e 636 static int ri = 0; // reading buffer index (next write position)
mjr 11:bd9da7088e6e 637
mjr 11:bd9da7088e6e 638 // get the write pointer
mjr 11:bd9da7088e6e 639 reading *r = &readings[ri];
mjr 11:bd9da7088e6e 640
mjr 11:bd9da7088e6e 641 // figure the time since the last reading, and read the raw button state
mjr 12:669df364a565 642 r->dt = buttonTimer.read_ms();
mjr 11:bd9da7088e6e 643 uint32_t b = r->b = readButtonsRaw();
mjr 11:bd9da7088e6e 644
mjr 11:bd9da7088e6e 645 // start timing the next interval
mjr 12:669df364a565 646 buttonTimer.reset();
mjr 11:bd9da7088e6e 647
mjr 11:bd9da7088e6e 648 // AND together readings over 50ms
mjr 11:bd9da7088e6e 649 int ms = 0;
mjr 12:669df364a565 650 for (int i = 1 ; i < countof(readings) && ms < 50 ; ++i)
mjr 11:bd9da7088e6e 651 {
mjr 11:bd9da7088e6e 652 // find the next prior reading, wrapping in the circular buffer
mjr 11:bd9da7088e6e 653 int j = ri - i;
mjr 11:bd9da7088e6e 654 if (j < 0)
mjr 11:bd9da7088e6e 655 j = countof(readings) - 1;
mjr 11:bd9da7088e6e 656
mjr 11:bd9da7088e6e 657 reading *rj = &readings[j];
mjr 11:bd9da7088e6e 658
mjr 11:bd9da7088e6e 659 // AND the buttons for this reading
mjr 11:bd9da7088e6e 660 b &= rj->b;
mjr 11:bd9da7088e6e 661
mjr 11:bd9da7088e6e 662 // count the time
mjr 11:bd9da7088e6e 663 ms += rj->dt;
mjr 11:bd9da7088e6e 664 }
mjr 11:bd9da7088e6e 665
mjr 11:bd9da7088e6e 666 // advance the write position for next time
mjr 11:bd9da7088e6e 667 ri += 1;
mjr 12:669df364a565 668 if (ri >= countof(readings))
mjr 11:bd9da7088e6e 669 ri = 0;
mjr 11:bd9da7088e6e 670
mjr 11:bd9da7088e6e 671 // return the debounced result
mjr 11:bd9da7088e6e 672 return b;
mjr 11:bd9da7088e6e 673 }
mjr 11:bd9da7088e6e 674
mjr 5:a70c0bce770d 675 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 676 //
mjr 5:a70c0bce770d 677 // Non-volatile memory (NVM)
mjr 5:a70c0bce770d 678 //
mjr 0:5acbbe3f4cf4 679
mjr 5:a70c0bce770d 680 // Structure defining our NVM storage layout. We store a small
mjr 2:c174f9ee414a 681 // amount of persistent data in flash memory to retain calibration
mjr 5:a70c0bce770d 682 // data when powered off.
mjr 2:c174f9ee414a 683 struct NVM
mjr 2:c174f9ee414a 684 {
mjr 2:c174f9ee414a 685 // checksum - we use this to determine if the flash record
mjr 6:cc35eb643e8f 686 // has been properly initialized
mjr 2:c174f9ee414a 687 uint32_t checksum;
mjr 2:c174f9ee414a 688
mjr 2:c174f9ee414a 689 // signature value
mjr 2:c174f9ee414a 690 static const uint32_t SIGNATURE = 0x4D4A522A;
mjr 6:cc35eb643e8f 691 static const uint16_t VERSION = 0x0003;
mjr 6:cc35eb643e8f 692
mjr 6:cc35eb643e8f 693 // Is the data structure valid? We test the signature and
mjr 6:cc35eb643e8f 694 // checksum to determine if we've been properly stored.
mjr 6:cc35eb643e8f 695 int valid() const
mjr 6:cc35eb643e8f 696 {
mjr 6:cc35eb643e8f 697 return (d.sig == SIGNATURE
mjr 6:cc35eb643e8f 698 && d.vsn == VERSION
mjr 6:cc35eb643e8f 699 && d.sz == sizeof(NVM)
mjr 6:cc35eb643e8f 700 && checksum == CRC32(&d, sizeof(d)));
mjr 6:cc35eb643e8f 701 }
mjr 6:cc35eb643e8f 702
mjr 6:cc35eb643e8f 703 // save to non-volatile memory
mjr 6:cc35eb643e8f 704 void save(FreescaleIAP &iap, int addr)
mjr 6:cc35eb643e8f 705 {
mjr 6:cc35eb643e8f 706 // update the checksum and structure size
mjr 6:cc35eb643e8f 707 checksum = CRC32(&d, sizeof(d));
mjr 6:cc35eb643e8f 708 d.sz = sizeof(NVM);
mjr 6:cc35eb643e8f 709
mjr 6:cc35eb643e8f 710 // erase the sector
mjr 6:cc35eb643e8f 711 iap.erase_sector(addr);
mjr 6:cc35eb643e8f 712
mjr 6:cc35eb643e8f 713 // save the data
mjr 6:cc35eb643e8f 714 iap.program_flash(addr, this, sizeof(*this));
mjr 6:cc35eb643e8f 715 }
mjr 2:c174f9ee414a 716
mjr 9:fd65b0a94720 717 // reset calibration data for calibration mode
mjr 9:fd65b0a94720 718 void resetPlunger()
mjr 9:fd65b0a94720 719 {
mjr 9:fd65b0a94720 720 // set extremes for the calibration data
mjr 9:fd65b0a94720 721 d.plungerMax = 0;
mjr 9:fd65b0a94720 722 d.plungerZero = npix;
mjr 9:fd65b0a94720 723 d.plungerMin = npix;
mjr 9:fd65b0a94720 724 }
mjr 9:fd65b0a94720 725
mjr 2:c174f9ee414a 726 // stored data (excluding the checksum)
mjr 2:c174f9ee414a 727 struct
mjr 2:c174f9ee414a 728 {
mjr 6:cc35eb643e8f 729 // Signature, structure version, and structure size - further verification
mjr 6:cc35eb643e8f 730 // that we have valid initialized data. The size is a simple proxy for a
mjr 6:cc35eb643e8f 731 // structure version, as the most common type of change to the structure as
mjr 6:cc35eb643e8f 732 // the software evolves will be the addition of new elements. We also
mjr 6:cc35eb643e8f 733 // provide an explicit version number that we can update manually if we
mjr 6:cc35eb643e8f 734 // make any changes that don't affect the structure size but would affect
mjr 6:cc35eb643e8f 735 // compatibility with a saved record (e.g., swapping two existing elements).
mjr 2:c174f9ee414a 736 uint32_t sig;
mjr 2:c174f9ee414a 737 uint16_t vsn;
mjr 6:cc35eb643e8f 738 int sz;
mjr 2:c174f9ee414a 739
mjr 6:cc35eb643e8f 740 // has the plunger been manually calibrated?
mjr 6:cc35eb643e8f 741 int plungerCal;
mjr 6:cc35eb643e8f 742
mjr 2:c174f9ee414a 743 // plunger calibration min and max
mjr 2:c174f9ee414a 744 int plungerMin;
mjr 6:cc35eb643e8f 745 int plungerZero;
mjr 2:c174f9ee414a 746 int plungerMax;
mjr 6:cc35eb643e8f 747
mjr 6:cc35eb643e8f 748 // is the CCD enabled?
mjr 6:cc35eb643e8f 749 int ccdEnabled;
mjr 6:cc35eb643e8f 750
mjr 6:cc35eb643e8f 751 // LedWiz unit number
mjr 6:cc35eb643e8f 752 uint8_t ledWizUnitNo;
mjr 2:c174f9ee414a 753 } d;
mjr 2:c174f9ee414a 754 };
mjr 2:c174f9ee414a 755
mjr 5:a70c0bce770d 756
mjr 5:a70c0bce770d 757 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 758 //
mjr 5:a70c0bce770d 759 // Customization joystick subbclass
mjr 5:a70c0bce770d 760 //
mjr 5:a70c0bce770d 761
mjr 5:a70c0bce770d 762 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 763 {
mjr 5:a70c0bce770d 764 public:
mjr 5:a70c0bce770d 765 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
mjr 5:a70c0bce770d 766 : USBJoystick(vendor_id, product_id, product_release, true)
mjr 5:a70c0bce770d 767 {
mjr 5:a70c0bce770d 768 suspended_ = false;
mjr 5:a70c0bce770d 769 }
mjr 5:a70c0bce770d 770
mjr 5:a70c0bce770d 771 // are we connected?
mjr 5:a70c0bce770d 772 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 773
mjr 5:a70c0bce770d 774 // Are we in suspend mode?
mjr 5:a70c0bce770d 775 int isSuspended() const { return suspended_; }
mjr 5:a70c0bce770d 776
mjr 5:a70c0bce770d 777 protected:
mjr 5:a70c0bce770d 778 virtual void suspendStateChanged(unsigned int suspended)
mjr 5:a70c0bce770d 779 { suspended_ = suspended; }
mjr 5:a70c0bce770d 780
mjr 5:a70c0bce770d 781 // are we suspended?
mjr 5:a70c0bce770d 782 int suspended_;
mjr 5:a70c0bce770d 783 };
mjr 5:a70c0bce770d 784
mjr 5:a70c0bce770d 785 // ---------------------------------------------------------------------------
mjr 6:cc35eb643e8f 786 //
mjr 6:cc35eb643e8f 787 // Some simple math service routines
mjr 6:cc35eb643e8f 788 //
mjr 6:cc35eb643e8f 789
mjr 6:cc35eb643e8f 790 inline float square(float x) { return x*x; }
mjr 6:cc35eb643e8f 791 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 6:cc35eb643e8f 792
mjr 6:cc35eb643e8f 793 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 794 //
mjr 5:a70c0bce770d 795 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 796 //
mjr 5:a70c0bce770d 797
mjr 5:a70c0bce770d 798 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 799 //
mjr 5:a70c0bce770d 800 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 801 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 802 // automatic calibration.
mjr 5:a70c0bce770d 803 //
mjr 5:a70c0bce770d 804 // We install an interrupt handler on the accelerometer "data ready"
mjr 6:cc35eb643e8f 805 // interrupt to ensure that we fetch each sample immediately when it
mjr 6:cc35eb643e8f 806 // becomes available. The accelerometer data rate is fiarly high
mjr 6:cc35eb643e8f 807 // (800 Hz), so it's not practical to keep up with it by polling.
mjr 6:cc35eb643e8f 808 // Using an interrupt handler lets us respond quickly and read
mjr 6:cc35eb643e8f 809 // every sample.
mjr 5:a70c0bce770d 810 //
mjr 6:cc35eb643e8f 811 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 812 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 813 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 814 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 815 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 816 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 817 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 818 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 819 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 820 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 821 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 822 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 823 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 824 // of nudging, say).
mjr 5:a70c0bce770d 825 //
mjr 5:a70c0bce770d 826
mjr 6:cc35eb643e8f 827 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 828 struct AccHist
mjr 5:a70c0bce770d 829 {
mjr 6:cc35eb643e8f 830 AccHist() { x = y = d = 0.0; xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 831 void set(float x, float y, AccHist *prv)
mjr 6:cc35eb643e8f 832 {
mjr 6:cc35eb643e8f 833 // save the raw position
mjr 6:cc35eb643e8f 834 this->x = x;
mjr 6:cc35eb643e8f 835 this->y = y;
mjr 6:cc35eb643e8f 836 this->d = distance(prv);
mjr 6:cc35eb643e8f 837 }
mjr 6:cc35eb643e8f 838
mjr 6:cc35eb643e8f 839 // reading for this entry
mjr 5:a70c0bce770d 840 float x, y;
mjr 5:a70c0bce770d 841
mjr 6:cc35eb643e8f 842 // distance from previous entry
mjr 6:cc35eb643e8f 843 float d;
mjr 5:a70c0bce770d 844
mjr 6:cc35eb643e8f 845 // total and count of samples averaged over this period
mjr 6:cc35eb643e8f 846 float xtot, ytot;
mjr 6:cc35eb643e8f 847 int cnt;
mjr 6:cc35eb643e8f 848
mjr 6:cc35eb643e8f 849 void clearAvg() { xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 850 void addAvg(float x, float y) { xtot += x; ytot += y; ++cnt; }
mjr 6:cc35eb643e8f 851 float xAvg() const { return xtot/cnt; }
mjr 6:cc35eb643e8f 852 float yAvg() const { return ytot/cnt; }
mjr 5:a70c0bce770d 853
mjr 6:cc35eb643e8f 854 float distance(AccHist *p)
mjr 6:cc35eb643e8f 855 { return sqrt(square(p->x - x) + square(p->y - y)); }
mjr 5:a70c0bce770d 856 };
mjr 5:a70c0bce770d 857
mjr 5:a70c0bce770d 858 // accelerometer wrapper class
mjr 3:3514575d4f86 859 class Accel
mjr 3:3514575d4f86 860 {
mjr 3:3514575d4f86 861 public:
mjr 3:3514575d4f86 862 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin)
mjr 3:3514575d4f86 863 : mma_(sda, scl, i2cAddr), intIn_(irqPin)
mjr 3:3514575d4f86 864 {
mjr 5:a70c0bce770d 865 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 866 irqPin_ = irqPin;
mjr 5:a70c0bce770d 867
mjr 5:a70c0bce770d 868 // reset and initialize
mjr 5:a70c0bce770d 869 reset();
mjr 5:a70c0bce770d 870 }
mjr 5:a70c0bce770d 871
mjr 5:a70c0bce770d 872 void reset()
mjr 5:a70c0bce770d 873 {
mjr 6:cc35eb643e8f 874 // clear the center point
mjr 6:cc35eb643e8f 875 cx_ = cy_ = 0.0;
mjr 6:cc35eb643e8f 876
mjr 6:cc35eb643e8f 877 // start the calibration timer
mjr 5:a70c0bce770d 878 tCenter_.start();
mjr 5:a70c0bce770d 879 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 880
mjr 5:a70c0bce770d 881 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 882 mma_.init();
mjr 6:cc35eb643e8f 883
mjr 6:cc35eb643e8f 884 // set the initial integrated velocity reading to zero
mjr 6:cc35eb643e8f 885 vx_ = vy_ = 0;
mjr 3:3514575d4f86 886
mjr 6:cc35eb643e8f 887 // set up our accelerometer interrupt handling
mjr 6:cc35eb643e8f 888 intIn_.rise(this, &Accel::isr);
mjr 5:a70c0bce770d 889 mma_.setInterruptMode(irqPin_ == PTA14 ? 1 : 2);
mjr 3:3514575d4f86 890
mjr 3:3514575d4f86 891 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 892 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 893
mjr 3:3514575d4f86 894 // start our timers
mjr 3:3514575d4f86 895 tGet_.start();
mjr 3:3514575d4f86 896 tInt_.start();
mjr 3:3514575d4f86 897 }
mjr 3:3514575d4f86 898
mjr 9:fd65b0a94720 899 void get(int &x, int &y)
mjr 3:3514575d4f86 900 {
mjr 3:3514575d4f86 901 // disable interrupts while manipulating the shared data
mjr 3:3514575d4f86 902 __disable_irq();
mjr 3:3514575d4f86 903
mjr 3:3514575d4f86 904 // read the shared data and store locally for calculations
mjr 6:cc35eb643e8f 905 float ax = ax_, ay = ay_;
mjr 6:cc35eb643e8f 906 float vx = vx_, vy = vy_;
mjr 5:a70c0bce770d 907
mjr 6:cc35eb643e8f 908 // reset the velocity sum for the next run
mjr 6:cc35eb643e8f 909 vx_ = vy_ = 0;
mjr 3:3514575d4f86 910
mjr 3:3514575d4f86 911 // get the time since the last get() sample
mjr 3:3514575d4f86 912 float dt = tGet_.read_us()/1.0e6;
mjr 3:3514575d4f86 913 tGet_.reset();
mjr 3:3514575d4f86 914
mjr 3:3514575d4f86 915 // done manipulating the shared data
mjr 3:3514575d4f86 916 __enable_irq();
mjr 3:3514575d4f86 917
mjr 6:cc35eb643e8f 918 // adjust the readings for the integration time
mjr 6:cc35eb643e8f 919 vx /= dt;
mjr 6:cc35eb643e8f 920 vy /= dt;
mjr 6:cc35eb643e8f 921
mjr 6:cc35eb643e8f 922 // add this sample to the current calibration interval's running total
mjr 6:cc35eb643e8f 923 AccHist *p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 924 p->addAvg(ax, ay);
mjr 6:cc35eb643e8f 925
mjr 5:a70c0bce770d 926 // check for auto-centering every so often
mjr 5:a70c0bce770d 927 if (tCenter_.read_ms() > 1000)
mjr 5:a70c0bce770d 928 {
mjr 5:a70c0bce770d 929 // add the latest raw sample to the history list
mjr 6:cc35eb643e8f 930 AccHist *prv = p;
mjr 5:a70c0bce770d 931 iAccPrv_ = (iAccPrv_ + 1) % maxAccPrv;
mjr 6:cc35eb643e8f 932 p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 933 p->set(ax, ay, prv);
mjr 5:a70c0bce770d 934
mjr 5:a70c0bce770d 935 // if we have a full complement, check for stability
mjr 5:a70c0bce770d 936 if (nAccPrv_ >= maxAccPrv)
mjr 5:a70c0bce770d 937 {
mjr 5:a70c0bce770d 938 // check if we've been stable for all recent samples
mjr 6:cc35eb643e8f 939 static const float accTol = .01;
mjr 6:cc35eb643e8f 940 AccHist *p0 = accPrv_;
mjr 6:cc35eb643e8f 941 if (p0[0].d < accTol
mjr 6:cc35eb643e8f 942 && p0[1].d < accTol
mjr 6:cc35eb643e8f 943 && p0[2].d < accTol
mjr 6:cc35eb643e8f 944 && p0[3].d < accTol
mjr 6:cc35eb643e8f 945 && p0[4].d < accTol)
mjr 5:a70c0bce770d 946 {
mjr 6:cc35eb643e8f 947 // Figure the new calibration point as the average of
mjr 6:cc35eb643e8f 948 // the samples over the rest period
mjr 6:cc35eb643e8f 949 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5.0;
mjr 6:cc35eb643e8f 950 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5.0;
mjr 5:a70c0bce770d 951 }
mjr 5:a70c0bce770d 952 }
mjr 5:a70c0bce770d 953 else
mjr 5:a70c0bce770d 954 {
mjr 5:a70c0bce770d 955 // not enough samples yet; just up the count
mjr 5:a70c0bce770d 956 ++nAccPrv_;
mjr 5:a70c0bce770d 957 }
mjr 6:cc35eb643e8f 958
mjr 6:cc35eb643e8f 959 // clear the new item's running totals
mjr 6:cc35eb643e8f 960 p->clearAvg();
mjr 5:a70c0bce770d 961
mjr 5:a70c0bce770d 962 // reset the timer
mjr 5:a70c0bce770d 963 tCenter_.reset();
mjr 5:a70c0bce770d 964 }
mjr 5:a70c0bce770d 965
mjr 6:cc35eb643e8f 966 // report our integrated velocity reading in x,y
mjr 6:cc35eb643e8f 967 x = rawToReport(vx);
mjr 6:cc35eb643e8f 968 y = rawToReport(vy);
mjr 5:a70c0bce770d 969
mjr 6:cc35eb643e8f 970 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 971 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 972 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 973 #endif
mjr 3:3514575d4f86 974 }
mjr 3:3514575d4f86 975
mjr 3:3514575d4f86 976 private:
mjr 6:cc35eb643e8f 977 // adjust a raw acceleration figure to a usb report value
mjr 6:cc35eb643e8f 978 int rawToReport(float v)
mjr 5:a70c0bce770d 979 {
mjr 6:cc35eb643e8f 980 // scale to the joystick report range and round to integer
mjr 6:cc35eb643e8f 981 int i = int(round(v*JOYMAX));
mjr 5:a70c0bce770d 982
mjr 6:cc35eb643e8f 983 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 984 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 985 static const int filter[] = {
mjr 6:cc35eb643e8f 986 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 987 0,
mjr 6:cc35eb643e8f 988 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 989 };
mjr 6:cc35eb643e8f 990 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 991 }
mjr 5:a70c0bce770d 992
mjr 3:3514575d4f86 993 // interrupt handler
mjr 3:3514575d4f86 994 void isr()
mjr 3:3514575d4f86 995 {
mjr 3:3514575d4f86 996 // Read the axes. Note that we have to read all three axes
mjr 3:3514575d4f86 997 // (even though we only really use x and y) in order to clear
mjr 3:3514575d4f86 998 // the "data ready" status bit in the accelerometer. The
mjr 3:3514575d4f86 999 // interrupt only occurs when the "ready" bit transitions from
mjr 3:3514575d4f86 1000 // off to on, so we have to make sure it's off.
mjr 5:a70c0bce770d 1001 float x, y, z;
mjr 5:a70c0bce770d 1002 mma_.getAccXYZ(x, y, z);
mjr 3:3514575d4f86 1003
mjr 3:3514575d4f86 1004 // calculate the time since the last interrupt
mjr 3:3514575d4f86 1005 float dt = tInt_.read_us()/1.0e6;
mjr 3:3514575d4f86 1006 tInt_.reset();
mjr 6:cc35eb643e8f 1007
mjr 6:cc35eb643e8f 1008 // integrate the time slice from the previous reading to this reading
mjr 6:cc35eb643e8f 1009 vx_ += (x + ax_ - 2*cx_)*dt/2;
mjr 6:cc35eb643e8f 1010 vy_ += (y + ay_ - 2*cy_)*dt/2;
mjr 3:3514575d4f86 1011
mjr 6:cc35eb643e8f 1012 // store the updates
mjr 6:cc35eb643e8f 1013 ax_ = x;
mjr 6:cc35eb643e8f 1014 ay_ = y;
mjr 6:cc35eb643e8f 1015 az_ = z;
mjr 3:3514575d4f86 1016 }
mjr 3:3514575d4f86 1017
mjr 3:3514575d4f86 1018 // underlying accelerometer object
mjr 3:3514575d4f86 1019 MMA8451Q mma_;
mjr 3:3514575d4f86 1020
mjr 5:a70c0bce770d 1021 // last raw acceleration readings
mjr 6:cc35eb643e8f 1022 float ax_, ay_, az_;
mjr 5:a70c0bce770d 1023
mjr 6:cc35eb643e8f 1024 // integrated velocity reading since last get()
mjr 6:cc35eb643e8f 1025 float vx_, vy_;
mjr 6:cc35eb643e8f 1026
mjr 3:3514575d4f86 1027 // timer for measuring time between get() samples
mjr 3:3514575d4f86 1028 Timer tGet_;
mjr 3:3514575d4f86 1029
mjr 3:3514575d4f86 1030 // timer for measuring time between interrupts
mjr 3:3514575d4f86 1031 Timer tInt_;
mjr 5:a70c0bce770d 1032
mjr 6:cc35eb643e8f 1033 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 1034 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 1035 // at rest.
mjr 6:cc35eb643e8f 1036 float cx_, cy_;
mjr 5:a70c0bce770d 1037
mjr 5:a70c0bce770d 1038 // timer for atuo-centering
mjr 5:a70c0bce770d 1039 Timer tCenter_;
mjr 6:cc35eb643e8f 1040
mjr 6:cc35eb643e8f 1041 // Auto-centering history. This is a separate history list that
mjr 6:cc35eb643e8f 1042 // records results spaced out sparesely over time, so that we can
mjr 6:cc35eb643e8f 1043 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 1044 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 1045 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 1046 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 1047 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 1048 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 1049 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 1050 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 1051 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 1052 // accelerometer measurement bias (e.g., from temperature changes).
mjr 5:a70c0bce770d 1053 int iAccPrv_, nAccPrv_;
mjr 5:a70c0bce770d 1054 static const int maxAccPrv = 5;
mjr 6:cc35eb643e8f 1055 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 1056
mjr 5:a70c0bce770d 1057 // interurupt pin name
mjr 5:a70c0bce770d 1058 PinName irqPin_;
mjr 5:a70c0bce770d 1059
mjr 5:a70c0bce770d 1060 // interrupt router
mjr 5:a70c0bce770d 1061 InterruptIn intIn_;
mjr 3:3514575d4f86 1062 };
mjr 3:3514575d4f86 1063
mjr 5:a70c0bce770d 1064
mjr 5:a70c0bce770d 1065 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1066 //
mjr 5:a70c0bce770d 1067 // Clear the I2C bus for the MMA8451!. This seems necessary some of the time
mjr 5:a70c0bce770d 1068 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 1069 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 1070 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 5:a70c0bce770d 1071 // the SCL line is supposed to clear this conidtion.
mjr 5:a70c0bce770d 1072 //
mjr 5:a70c0bce770d 1073 void clear_i2c()
mjr 5:a70c0bce770d 1074 {
mjr 5:a70c0bce770d 1075 // assume a general-purpose output pin to the I2C clock
mjr 5:a70c0bce770d 1076 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 1077 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 1078
mjr 5:a70c0bce770d 1079 // clock the SCL 9 times
mjr 5:a70c0bce770d 1080 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 1081 {
mjr 5:a70c0bce770d 1082 scl = 1;
mjr 5:a70c0bce770d 1083 wait_us(20);
mjr 5:a70c0bce770d 1084 scl = 0;
mjr 5:a70c0bce770d 1085 wait_us(20);
mjr 5:a70c0bce770d 1086 }
mjr 5:a70c0bce770d 1087 }
mjr 5:a70c0bce770d 1088
mjr 5:a70c0bce770d 1089 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 1090 //
mjr 5:a70c0bce770d 1091 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 1092 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 1093 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 1094 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 1095 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 1096 // port outputs.
mjr 5:a70c0bce770d 1097 //
mjr 0:5acbbe3f4cf4 1098 int main(void)
mjr 0:5acbbe3f4cf4 1099 {
mjr 1:d913e0afb2ac 1100 // turn off our on-board indicator LED
mjr 4:02c7cd7b2183 1101 ledR = 1;
mjr 4:02c7cd7b2183 1102 ledG = 1;
mjr 4:02c7cd7b2183 1103 ledB = 1;
mjr 1:d913e0afb2ac 1104
mjr 6:cc35eb643e8f 1105 // initialize the LedWiz ports
mjr 6:cc35eb643e8f 1106 initLwOut();
mjr 6:cc35eb643e8f 1107
mjr 11:bd9da7088e6e 1108 // initialize the button input ports
mjr 11:bd9da7088e6e 1109 initButtons();
mjr 11:bd9da7088e6e 1110
mjr 6:cc35eb643e8f 1111 // we don't need a reset yet
mjr 6:cc35eb643e8f 1112 bool needReset = false;
mjr 6:cc35eb643e8f 1113
mjr 5:a70c0bce770d 1114 // clear the I2C bus for the accelerometer
mjr 5:a70c0bce770d 1115 clear_i2c();
mjr 5:a70c0bce770d 1116
mjr 2:c174f9ee414a 1117 // set up a flash memory controller
mjr 2:c174f9ee414a 1118 FreescaleIAP iap;
mjr 2:c174f9ee414a 1119
mjr 2:c174f9ee414a 1120 // use the last sector of flash for our non-volatile memory structure
mjr 2:c174f9ee414a 1121 int flash_addr = (iap.flash_size() - SECTOR_SIZE);
mjr 2:c174f9ee414a 1122 NVM *flash = (NVM *)flash_addr;
mjr 2:c174f9ee414a 1123 NVM cfg;
mjr 2:c174f9ee414a 1124
mjr 2:c174f9ee414a 1125 // check for valid flash
mjr 6:cc35eb643e8f 1126 bool flash_valid = flash->valid();
mjr 2:c174f9ee414a 1127
mjr 2:c174f9ee414a 1128 // if the flash is valid, load it; otherwise initialize to defaults
mjr 2:c174f9ee414a 1129 if (flash_valid) {
mjr 2:c174f9ee414a 1130 memcpy(&cfg, flash, sizeof(cfg));
mjr 6:cc35eb643e8f 1131 printf("Flash restored: plunger cal=%d, min=%d, zero=%d, max=%d\r\n",
mjr 6:cc35eb643e8f 1132 cfg.d.plungerCal, cfg.d.plungerMin, cfg.d.plungerZero, cfg.d.plungerMax);
mjr 2:c174f9ee414a 1133 }
mjr 2:c174f9ee414a 1134 else {
mjr 2:c174f9ee414a 1135 printf("Factory reset\r\n");
mjr 2:c174f9ee414a 1136 cfg.d.sig = cfg.SIGNATURE;
mjr 2:c174f9ee414a 1137 cfg.d.vsn = cfg.VERSION;
mjr 6:cc35eb643e8f 1138 cfg.d.plungerCal = 0;
mjr 6:cc35eb643e8f 1139 cfg.d.plungerZero = 0;
mjr 2:c174f9ee414a 1140 cfg.d.plungerMin = 0;
mjr 2:c174f9ee414a 1141 cfg.d.plungerMax = npix;
mjr 6:cc35eb643e8f 1142 cfg.d.ledWizUnitNo = DEFAULT_LEDWIZ_UNIT_NUMBER;
mjr 6:cc35eb643e8f 1143 cfg.d.ccdEnabled = true;
mjr 2:c174f9ee414a 1144 }
mjr 1:d913e0afb2ac 1145
mjr 6:cc35eb643e8f 1146 // Create the joystick USB client. Note that we use the LedWiz unit
mjr 6:cc35eb643e8f 1147 // number from the saved configuration.
mjr 6:cc35eb643e8f 1148 MyUSBJoystick js(
mjr 6:cc35eb643e8f 1149 USB_VENDOR_ID,
mjr 6:cc35eb643e8f 1150 USB_PRODUCT_ID | cfg.d.ledWizUnitNo,
mjr 6:cc35eb643e8f 1151 USB_VERSION_NO);
mjr 6:cc35eb643e8f 1152
mjr 1:d913e0afb2ac 1153 // plunger calibration button debounce timer
mjr 1:d913e0afb2ac 1154 Timer calBtnTimer;
mjr 1:d913e0afb2ac 1155 calBtnTimer.start();
mjr 1:d913e0afb2ac 1156 int calBtnLit = false;
mjr 1:d913e0afb2ac 1157
mjr 1:d913e0afb2ac 1158 // Calibration button state:
mjr 1:d913e0afb2ac 1159 // 0 = not pushed
mjr 1:d913e0afb2ac 1160 // 1 = pushed, not yet debounced
mjr 1:d913e0afb2ac 1161 // 2 = pushed, debounced, waiting for hold time
mjr 1:d913e0afb2ac 1162 // 3 = pushed, hold time completed - in calibration mode
mjr 1:d913e0afb2ac 1163 int calBtnState = 0;
mjr 1:d913e0afb2ac 1164
mjr 1:d913e0afb2ac 1165 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 1166 Timer hbTimer;
mjr 1:d913e0afb2ac 1167 hbTimer.start();
mjr 1:d913e0afb2ac 1168 int hb = 0;
mjr 5:a70c0bce770d 1169 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 1170
mjr 1:d913e0afb2ac 1171 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 1172 Timer acTimer;
mjr 1:d913e0afb2ac 1173 acTimer.start();
mjr 1:d913e0afb2ac 1174
mjr 0:5acbbe3f4cf4 1175 // create the accelerometer object
mjr 5:a70c0bce770d 1176 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS, MMA8451_INT_PIN);
mjr 0:5acbbe3f4cf4 1177
mjr 0:5acbbe3f4cf4 1178 // create the CCD array object
mjr 1:d913e0afb2ac 1179 TSL1410R ccd(PTE20, PTE21, PTB0);
mjr 2:c174f9ee414a 1180
mjr 1:d913e0afb2ac 1181 // last accelerometer report, in mouse coordinates
mjr 6:cc35eb643e8f 1182 int x = 0, y = 0, z = 0;
mjr 6:cc35eb643e8f 1183
mjr 6:cc35eb643e8f 1184 // previous two plunger readings, for "debouncing" the results (z0 is
mjr 6:cc35eb643e8f 1185 // the most recent, z1 is the one before that)
mjr 6:cc35eb643e8f 1186 int z0 = 0, z1 = 0, z2 = 0;
mjr 6:cc35eb643e8f 1187
mjr 6:cc35eb643e8f 1188 // Firing in progress: we set this when we detect the start of rapid
mjr 6:cc35eb643e8f 1189 // plunger movement from a retracted position towards the rest position.
mjr 6:cc35eb643e8f 1190 // The actual plunger spring return speed seems to be too slow for VP,
mjr 6:cc35eb643e8f 1191 // so when we detect the start of this motion, we immediately tell VP
mjr 6:cc35eb643e8f 1192 // to return the plunger to rest, then we monitor the real plunger
mjr 6:cc35eb643e8f 1193 // until it atcually stops.
mjr 9:fd65b0a94720 1194 int firing = 0;
mjr 2:c174f9ee414a 1195
mjr 2:c174f9ee414a 1196 // start the first CCD integration cycle
mjr 2:c174f9ee414a 1197 ccd.clear();
mjr 9:fd65b0a94720 1198
mjr 9:fd65b0a94720 1199 // Device status. We report this on each update so that the host config
mjr 9:fd65b0a94720 1200 // tool can detect our current settings. This is a bit mask consisting
mjr 9:fd65b0a94720 1201 // of these bits:
mjr 9:fd65b0a94720 1202 // 0x01 -> plunger sensor enabled
mjr 9:fd65b0a94720 1203 uint16_t statusFlags = (cfg.d.ccdEnabled ? 0x01 : 0x00);
mjr 10:976666ffa4ef 1204
mjr 10:976666ffa4ef 1205 // flag: send a pixel dump after the next read
mjr 10:976666ffa4ef 1206 bool reportPix = false;
mjr 1:d913e0afb2ac 1207
mjr 1:d913e0afb2ac 1208 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 1209 // host requests
mjr 0:5acbbe3f4cf4 1210 for (;;)
mjr 0:5acbbe3f4cf4 1211 {
mjr 0:5acbbe3f4cf4 1212 // Look for an incoming report. Continue processing input as
mjr 0:5acbbe3f4cf4 1213 // long as there's anything pending - this ensures that we
mjr 0:5acbbe3f4cf4 1214 // handle input in as timely a fashion as possible by deferring
mjr 0:5acbbe3f4cf4 1215 // output tasks as long as there's input to process.
mjr 0:5acbbe3f4cf4 1216 HID_REPORT report;
mjr 6:cc35eb643e8f 1217 while (js.readNB(&report))
mjr 0:5acbbe3f4cf4 1218 {
mjr 6:cc35eb643e8f 1219 // all Led-Wiz reports are 8 bytes exactly
mjr 6:cc35eb643e8f 1220 if (report.length == 8)
mjr 1:d913e0afb2ac 1221 {
mjr 6:cc35eb643e8f 1222 uint8_t *data = report.data;
mjr 6:cc35eb643e8f 1223 if (data[0] == 64)
mjr 0:5acbbe3f4cf4 1224 {
mjr 6:cc35eb643e8f 1225 // LWZ-SBA - first four bytes are bit-packed on/off flags
mjr 6:cc35eb643e8f 1226 // for the outputs; 5th byte is the pulse speed (0-7)
mjr 6:cc35eb643e8f 1227 //printf("LWZ-SBA %02x %02x %02x %02x ; %02x\r\n",
mjr 6:cc35eb643e8f 1228 // data[1], data[2], data[3], data[4], data[5]);
mjr 0:5acbbe3f4cf4 1229
mjr 6:cc35eb643e8f 1230 // update all on/off states
mjr 6:cc35eb643e8f 1231 for (int i = 0, bit = 1, ri = 1 ; i < 32 ; ++i, bit <<= 1)
mjr 6:cc35eb643e8f 1232 {
mjr 6:cc35eb643e8f 1233 if (bit == 0x100) {
mjr 6:cc35eb643e8f 1234 bit = 1;
mjr 6:cc35eb643e8f 1235 ++ri;
mjr 6:cc35eb643e8f 1236 }
mjr 6:cc35eb643e8f 1237 wizOn[i] = ((data[ri] & bit) != 0);
mjr 6:cc35eb643e8f 1238 }
mjr 6:cc35eb643e8f 1239
mjr 6:cc35eb643e8f 1240 // update the physical outputs
mjr 1:d913e0afb2ac 1241 updateWizOuts();
mjr 6:cc35eb643e8f 1242
mjr 6:cc35eb643e8f 1243 // reset the PBA counter
mjr 6:cc35eb643e8f 1244 pbaIdx = 0;
mjr 6:cc35eb643e8f 1245 }
mjr 6:cc35eb643e8f 1246 else if (data[0] == 65)
mjr 6:cc35eb643e8f 1247 {
mjr 6:cc35eb643e8f 1248 // Private control message. This isn't an LedWiz message - it's
mjr 6:cc35eb643e8f 1249 // an extension for this device. 65 is an invalid PBA setting,
mjr 6:cc35eb643e8f 1250 // and isn't used for any other LedWiz message, so we appropriate
mjr 6:cc35eb643e8f 1251 // it for our own private use. The first byte specifies the
mjr 6:cc35eb643e8f 1252 // message type.
mjr 6:cc35eb643e8f 1253 if (data[1] == 1)
mjr 6:cc35eb643e8f 1254 {
mjr 9:fd65b0a94720 1255 // 1 = Set Configuration:
mjr 6:cc35eb643e8f 1256 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 6:cc35eb643e8f 1257 // data[3] = feature enable bit mask:
mjr 6:cc35eb643e8f 1258 // 0x01 = enable CCD
mjr 6:cc35eb643e8f 1259
mjr 6:cc35eb643e8f 1260 // we'll need a reset if the LedWiz unit number is changing
mjr 6:cc35eb643e8f 1261 uint8_t newUnitNo = data[2] & 0x0f;
mjr 6:cc35eb643e8f 1262 needReset |= (newUnitNo != cfg.d.ledWizUnitNo);
mjr 6:cc35eb643e8f 1263
mjr 6:cc35eb643e8f 1264 // set the configuration parameters from the message
mjr 6:cc35eb643e8f 1265 cfg.d.ledWizUnitNo = newUnitNo;
mjr 6:cc35eb643e8f 1266 cfg.d.ccdEnabled = data[3] & 0x01;
mjr 6:cc35eb643e8f 1267
mjr 9:fd65b0a94720 1268 // update the status flags
mjr 9:fd65b0a94720 1269 statusFlags = (statusFlags & ~0x01) | (data[3] & 0x01);
mjr 9:fd65b0a94720 1270
mjr 9:fd65b0a94720 1271 // if the ccd is no longer enabled, use 0 for z reports
mjr 9:fd65b0a94720 1272 if (!cfg.d.ccdEnabled)
mjr 9:fd65b0a94720 1273 z = 0;
mjr 9:fd65b0a94720 1274
mjr 6:cc35eb643e8f 1275 // save the configuration
mjr 6:cc35eb643e8f 1276 cfg.save(iap, flash_addr);
mjr 6:cc35eb643e8f 1277 }
mjr 9:fd65b0a94720 1278 else if (data[1] == 2)
mjr 9:fd65b0a94720 1279 {
mjr 9:fd65b0a94720 1280 // 2 = Calibrate plunger
mjr 9:fd65b0a94720 1281 // (No parameters)
mjr 9:fd65b0a94720 1282
mjr 9:fd65b0a94720 1283 // enter calibration mode
mjr 9:fd65b0a94720 1284 calBtnState = 3;
mjr 9:fd65b0a94720 1285 calBtnTimer.reset();
mjr 9:fd65b0a94720 1286 cfg.resetPlunger();
mjr 9:fd65b0a94720 1287 }
mjr 10:976666ffa4ef 1288 else if (data[1] == 3)
mjr 10:976666ffa4ef 1289 {
mjr 10:976666ffa4ef 1290 // 3 = pixel dump
mjr 10:976666ffa4ef 1291 // (No parameters)
mjr 10:976666ffa4ef 1292 reportPix = true;
mjr 10:976666ffa4ef 1293
mjr 10:976666ffa4ef 1294 // show purple until we finish sending the report
mjr 10:976666ffa4ef 1295 ledR = 0;
mjr 10:976666ffa4ef 1296 ledB = 0;
mjr 10:976666ffa4ef 1297 ledG = 1;
mjr 10:976666ffa4ef 1298 }
mjr 6:cc35eb643e8f 1299 }
mjr 6:cc35eb643e8f 1300 else
mjr 6:cc35eb643e8f 1301 {
mjr 6:cc35eb643e8f 1302 // LWZ-PBA - full state dump; each byte is one output
mjr 6:cc35eb643e8f 1303 // in the current bank. pbaIdx keeps track of the bank;
mjr 6:cc35eb643e8f 1304 // this is incremented implicitly by each PBA message.
mjr 6:cc35eb643e8f 1305 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 6:cc35eb643e8f 1306 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 6:cc35eb643e8f 1307
mjr 6:cc35eb643e8f 1308 // update all output profile settings
mjr 6:cc35eb643e8f 1309 for (int i = 0 ; i < 8 ; ++i)
mjr 6:cc35eb643e8f 1310 wizVal[pbaIdx + i] = data[i];
mjr 6:cc35eb643e8f 1311
mjr 6:cc35eb643e8f 1312 // update the physical LED state if this is the last bank
mjr 6:cc35eb643e8f 1313 if (pbaIdx == 24)
mjr 6:cc35eb643e8f 1314 updateWizOuts();
mjr 6:cc35eb643e8f 1315
mjr 6:cc35eb643e8f 1316 // advance to the next bank
mjr 6:cc35eb643e8f 1317 pbaIdx = (pbaIdx + 8) & 31;
mjr 6:cc35eb643e8f 1318 }
mjr 0:5acbbe3f4cf4 1319 }
mjr 0:5acbbe3f4cf4 1320 }
mjr 1:d913e0afb2ac 1321
mjr 1:d913e0afb2ac 1322 // check for plunger calibration
mjr 1:d913e0afb2ac 1323 if (!calBtn)
mjr 0:5acbbe3f4cf4 1324 {
mjr 1:d913e0afb2ac 1325 // check the state
mjr 1:d913e0afb2ac 1326 switch (calBtnState)
mjr 0:5acbbe3f4cf4 1327 {
mjr 1:d913e0afb2ac 1328 case 0:
mjr 1:d913e0afb2ac 1329 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 1330 calBtnTimer.reset();
mjr 1:d913e0afb2ac 1331 calBtnState = 1;
mjr 1:d913e0afb2ac 1332 break;
mjr 1:d913e0afb2ac 1333
mjr 1:d913e0afb2ac 1334 case 1:
mjr 1:d913e0afb2ac 1335 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 1336 // passed, start the hold period
mjr 9:fd65b0a94720 1337 if (calBtnTimer.read_ms() > 50)
mjr 1:d913e0afb2ac 1338 calBtnState = 2;
mjr 1:d913e0afb2ac 1339 break;
mjr 1:d913e0afb2ac 1340
mjr 1:d913e0afb2ac 1341 case 2:
mjr 1:d913e0afb2ac 1342 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 1343 // for the entire hold period, move to calibration mode
mjr 9:fd65b0a94720 1344 if (calBtnTimer.read_ms() > 2050)
mjr 1:d913e0afb2ac 1345 {
mjr 1:d913e0afb2ac 1346 // enter calibration mode
mjr 1:d913e0afb2ac 1347 calBtnState = 3;
mjr 9:fd65b0a94720 1348 calBtnTimer.reset();
mjr 9:fd65b0a94720 1349 cfg.resetPlunger();
mjr 1:d913e0afb2ac 1350 }
mjr 1:d913e0afb2ac 1351 break;
mjr 2:c174f9ee414a 1352
mjr 2:c174f9ee414a 1353 case 3:
mjr 9:fd65b0a94720 1354 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 1355 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 1356 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 1357 break;
mjr 0:5acbbe3f4cf4 1358 }
mjr 0:5acbbe3f4cf4 1359 }
mjr 1:d913e0afb2ac 1360 else
mjr 1:d913e0afb2ac 1361 {
mjr 2:c174f9ee414a 1362 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 1363 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 1364 // and save the results to flash.
mjr 2:c174f9ee414a 1365 //
mjr 2:c174f9ee414a 1366 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 1367 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 1368 // mode, it simply cancels the attempt.
mjr 9:fd65b0a94720 1369 if (calBtnState == 3 && calBtnTimer.read_ms() > 15000)
mjr 2:c174f9ee414a 1370 {
mjr 2:c174f9ee414a 1371 // exit calibration mode
mjr 1:d913e0afb2ac 1372 calBtnState = 0;
mjr 2:c174f9ee414a 1373
mjr 6:cc35eb643e8f 1374 // save the updated configuration
mjr 6:cc35eb643e8f 1375 cfg.d.plungerCal = 1;
mjr 6:cc35eb643e8f 1376 cfg.save(iap, flash_addr);
mjr 2:c174f9ee414a 1377
mjr 2:c174f9ee414a 1378 // the flash state is now valid
mjr 2:c174f9ee414a 1379 flash_valid = true;
mjr 2:c174f9ee414a 1380 }
mjr 2:c174f9ee414a 1381 else if (calBtnState != 3)
mjr 2:c174f9ee414a 1382 {
mjr 2:c174f9ee414a 1383 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 1384 calBtnState = 0;
mjr 2:c174f9ee414a 1385 }
mjr 1:d913e0afb2ac 1386 }
mjr 1:d913e0afb2ac 1387
mjr 1:d913e0afb2ac 1388 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 1389 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 1390 switch (calBtnState)
mjr 0:5acbbe3f4cf4 1391 {
mjr 1:d913e0afb2ac 1392 case 2:
mjr 1:d913e0afb2ac 1393 // in the hold period - flash the light
mjr 9:fd65b0a94720 1394 newCalBtnLit = ((calBtnTimer.read_ms()/250) & 1);
mjr 1:d913e0afb2ac 1395 break;
mjr 1:d913e0afb2ac 1396
mjr 1:d913e0afb2ac 1397 case 3:
mjr 1:d913e0afb2ac 1398 // calibration mode - show steady on
mjr 1:d913e0afb2ac 1399 newCalBtnLit = true;
mjr 1:d913e0afb2ac 1400 break;
mjr 1:d913e0afb2ac 1401
mjr 1:d913e0afb2ac 1402 default:
mjr 1:d913e0afb2ac 1403 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 1404 newCalBtnLit = false;
mjr 1:d913e0afb2ac 1405 break;
mjr 1:d913e0afb2ac 1406 }
mjr 3:3514575d4f86 1407
mjr 3:3514575d4f86 1408 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 1409 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 1410 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 1411 {
mjr 1:d913e0afb2ac 1412 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 1413 if (calBtnLit) {
mjr 2:c174f9ee414a 1414 calBtnLed = 1;
mjr 4:02c7cd7b2183 1415 ledR = 1;
mjr 4:02c7cd7b2183 1416 ledG = 1;
mjr 9:fd65b0a94720 1417 ledB = 0;
mjr 2:c174f9ee414a 1418 }
mjr 2:c174f9ee414a 1419 else {
mjr 2:c174f9ee414a 1420 calBtnLed = 0;
mjr 4:02c7cd7b2183 1421 ledR = 1;
mjr 4:02c7cd7b2183 1422 ledG = 1;
mjr 9:fd65b0a94720 1423 ledB = 1;
mjr 2:c174f9ee414a 1424 }
mjr 1:d913e0afb2ac 1425 }
mjr 1:d913e0afb2ac 1426
mjr 6:cc35eb643e8f 1427 // read the plunger sensor, if it's enabled
mjr 10:976666ffa4ef 1428 uint16_t pix[npix];
mjr 6:cc35eb643e8f 1429 if (cfg.d.ccdEnabled)
mjr 6:cc35eb643e8f 1430 {
mjr 6:cc35eb643e8f 1431 // start with the previous reading, in case we don't have a
mjr 6:cc35eb643e8f 1432 // clear result on this frame
mjr 6:cc35eb643e8f 1433 int znew = z;
mjr 2:c174f9ee414a 1434
mjr 6:cc35eb643e8f 1435 // read the array
mjr 6:cc35eb643e8f 1436 ccd.read(pix, npix);
mjr 6:cc35eb643e8f 1437
mjr 6:cc35eb643e8f 1438 // get the average brightness at each end of the sensor
mjr 6:cc35eb643e8f 1439 long avg1 = (long(pix[0]) + long(pix[1]) + long(pix[2]) + long(pix[3]) + long(pix[4]))/5;
mjr 6:cc35eb643e8f 1440 long avg2 = (long(pix[npix-1]) + long(pix[npix-2]) + long(pix[npix-3]) + long(pix[npix-4]) + long(pix[npix-5]))/5;
mjr 6:cc35eb643e8f 1441
mjr 6:cc35eb643e8f 1442 // figure the midpoint in the brightness; multiply by 3 so that we can
mjr 6:cc35eb643e8f 1443 // compare sums of three pixels at a time to smooth out noise
mjr 6:cc35eb643e8f 1444 long midpt = (avg1 + avg2)/2 * 3;
mjr 6:cc35eb643e8f 1445
mjr 6:cc35eb643e8f 1446 // Work from the bright end to the dark end. VP interprets the
mjr 6:cc35eb643e8f 1447 // Z axis value as the amount the plunger is pulled: zero is the
mjr 6:cc35eb643e8f 1448 // rest position, and the axis maximum is fully pulled. So we
mjr 6:cc35eb643e8f 1449 // essentially want to report how much of the sensor is lit,
mjr 6:cc35eb643e8f 1450 // since this increases as the plunger is pulled back.
mjr 6:cc35eb643e8f 1451 int si = 1, di = 1;
mjr 6:cc35eb643e8f 1452 if (avg1 < avg2)
mjr 6:cc35eb643e8f 1453 si = npix - 2, di = -1;
mjr 6:cc35eb643e8f 1454
mjr 6:cc35eb643e8f 1455 // If the bright end and dark end don't differ by enough, skip this
mjr 6:cc35eb643e8f 1456 // reading entirely - we must have an overexposed or underexposed frame.
mjr 6:cc35eb643e8f 1457 // Otherwise proceed with the scan.
mjr 6:cc35eb643e8f 1458 if (labs(avg1 - avg2) > 0x1000)
mjr 6:cc35eb643e8f 1459 {
mjr 6:cc35eb643e8f 1460 uint16_t *pixp = pix + si;
mjr 6:cc35eb643e8f 1461 for (int n = 1 ; n < npix - 1 ; ++n, pixp += di)
mjr 6:cc35eb643e8f 1462 {
mjr 6:cc35eb643e8f 1463 // if we've crossed the midpoint, report this position
mjr 6:cc35eb643e8f 1464 if (long(pixp[-1]) + long(pixp[0]) + long(pixp[1]) < midpt)
mjr 6:cc35eb643e8f 1465 {
mjr 6:cc35eb643e8f 1466 // note the new position
mjr 6:cc35eb643e8f 1467 int pos = n;
mjr 6:cc35eb643e8f 1468
mjr 6:cc35eb643e8f 1469 // Calibrate, or apply calibration, depending on the mode.
mjr 6:cc35eb643e8f 1470 // In either case, normalize to our range. VP appears to
mjr 6:cc35eb643e8f 1471 // ignore negative Z axis values.
mjr 6:cc35eb643e8f 1472 if (calBtnState == 3)
mjr 6:cc35eb643e8f 1473 {
mjr 6:cc35eb643e8f 1474 // calibrating - note if we're expanding the calibration envelope
mjr 6:cc35eb643e8f 1475 if (pos < cfg.d.plungerMin)
mjr 6:cc35eb643e8f 1476 cfg.d.plungerMin = pos;
mjr 6:cc35eb643e8f 1477 if (pos < cfg.d.plungerZero)
mjr 6:cc35eb643e8f 1478 cfg.d.plungerZero = pos;
mjr 6:cc35eb643e8f 1479 if (pos > cfg.d.plungerMax)
mjr 6:cc35eb643e8f 1480 cfg.d.plungerMax = pos;
mjr 6:cc35eb643e8f 1481
mjr 6:cc35eb643e8f 1482 // normalize to the full physical range while calibrating
mjr 6:cc35eb643e8f 1483 znew = int(round(float(pos)/npix * JOYMAX));
mjr 6:cc35eb643e8f 1484 }
mjr 6:cc35eb643e8f 1485 else
mjr 6:cc35eb643e8f 1486 {
mjr 6:cc35eb643e8f 1487 // Running normally - normalize to the calibration range. Note
mjr 6:cc35eb643e8f 1488 // that values below the zero point are allowed - the zero point
mjr 6:cc35eb643e8f 1489 // represents the park position, where the plunger sits when at
mjr 6:cc35eb643e8f 1490 // rest, but a mechanical plunger has a smmall amount of travel
mjr 6:cc35eb643e8f 1491 // in the "push" direction. We represent forward travel with
mjr 6:cc35eb643e8f 1492 // negative z values.
mjr 6:cc35eb643e8f 1493 if (pos > cfg.d.plungerMax)
mjr 6:cc35eb643e8f 1494 pos = cfg.d.plungerMax;
mjr 6:cc35eb643e8f 1495 znew = int(round(float(pos - cfg.d.plungerZero)
mjr 6:cc35eb643e8f 1496 / (cfg.d.plungerMax - cfg.d.plungerZero + 1) * JOYMAX));
mjr 6:cc35eb643e8f 1497 }
mjr 6:cc35eb643e8f 1498
mjr 6:cc35eb643e8f 1499 // done
mjr 6:cc35eb643e8f 1500 break;
mjr 6:cc35eb643e8f 1501 }
mjr 6:cc35eb643e8f 1502 }
mjr 6:cc35eb643e8f 1503 }
mjr 7:100a25f8bf56 1504
mjr 7:100a25f8bf56 1505 // Determine if the plunger is being fired - i.e., if the player
mjr 7:100a25f8bf56 1506 // has just released the plunger from a retracted position.
mjr 6:cc35eb643e8f 1507 //
mjr 7:100a25f8bf56 1508 // We treat firing as an event. That is, we tell VP when the
mjr 7:100a25f8bf56 1509 // plunger is fired, and then stop sending data until the firing
mjr 7:100a25f8bf56 1510 // is complete, allowing VP to carry out the firing motion using
mjr 7:100a25f8bf56 1511 // its internal model plunger rather than trying to track the
mjr 7:100a25f8bf56 1512 // intermediate positions of the mechanical plunger throughout
mjr 9:fd65b0a94720 1513 // the firing motion. This is essential because the firing
mjr 9:fd65b0a94720 1514 // motion is too fast for us to track - in the time it takes us
mjr 9:fd65b0a94720 1515 // to read one frame, the plunger can make it all the way to the
mjr 9:fd65b0a94720 1516 // zero position and bounce back halfway. Fortunately, the range
mjr 9:fd65b0a94720 1517 // of motions for the plunger is limited, so if we see any rapid
mjr 9:fd65b0a94720 1518 // change of position toward the rest position, it's reasonably
mjr 9:fd65b0a94720 1519 // safe to interpret it as a firing event.
mjr 9:fd65b0a94720 1520 //
mjr 9:fd65b0a94720 1521 // This isn't foolproof. The user can trick us by doing a
mjr 9:fd65b0a94720 1522 // controlled rapid forward push but stopping short of the rest
mjr 9:fd65b0a94720 1523 // position. We'll misinterpret that as a firing event. But
mjr 9:fd65b0a94720 1524 // that's not a natural motion that a user would make with a
mjr 9:fd65b0a94720 1525 // plunger, so it's probably an acceptable false positive.
mjr 9:fd65b0a94720 1526 //
mjr 9:fd65b0a94720 1527 // Possible future enhancement: we could add a second physical
mjr 9:fd65b0a94720 1528 // sensor that detects when the plunger reaches the zero position
mjr 9:fd65b0a94720 1529 // and asserts an interrupt. In the interrupt handler, set a
mjr 9:fd65b0a94720 1530 // flag indicating the zero position signal. On each scan of
mjr 9:fd65b0a94720 1531 // the CCD, also check that flag; if it's set, enter firing
mjr 9:fd65b0a94720 1532 // event mode just as we do now. The key here is that the
mjr 9:fd65b0a94720 1533 // secondary sensor would have to be something much faster
mjr 9:fd65b0a94720 1534 // than our CCD scan - it would have to react on, say, the
mjr 9:fd65b0a94720 1535 // millisecond time scale. A simple mechanical switch or a
mjr 9:fd65b0a94720 1536 // proximity sensor could work. This would let us detect
mjr 9:fd65b0a94720 1537 // with certainty when the plunger physically fires, eliminating
mjr 9:fd65b0a94720 1538 // the case where the use can fool us with motion that's fast
mjr 9:fd65b0a94720 1539 // enough to look like a release but doesn't actually reach the
mjr 9:fd65b0a94720 1540 // starting position.
mjr 6:cc35eb643e8f 1541 //
mjr 7:100a25f8bf56 1542 // To detremine when a firing even occurs, we watch for rapid
mjr 7:100a25f8bf56 1543 // motion from a retracted position towards the rest position -
mjr 7:100a25f8bf56 1544 // that is, large position changes in the negative direction over
mjr 7:100a25f8bf56 1545 // a couple of consecutive readings. When we see a rapid move
mjr 7:100a25f8bf56 1546 // toward zero, we set our internal 'firing' flag, immediately
mjr 7:100a25f8bf56 1547 // report to VP that the plunger has returned to the zero
mjr 7:100a25f8bf56 1548 // position, and then suspend reports until the mechanical
mjr 7:100a25f8bf56 1549 // readings indicate that the plunger has come to rest (indicated
mjr 7:100a25f8bf56 1550 // by several readings in a row at roughly the same position).
mjr 9:fd65b0a94720 1551 //
mjr 9:fd65b0a94720 1552 // Tolerance for firing is 1/3 of the current pull distance, or
mjr 9:fd65b0a94720 1553 // about 1/2", whichever is greater. Making this value too small
mjr 9:fd65b0a94720 1554 // makes for too many false positives. Empirically, 1/4" is too
mjr 9:fd65b0a94720 1555 // twitchy, so set a floor at about 1/2". But we can be less
mjr 9:fd65b0a94720 1556 // sensitive the further back the plunger is pulled, since even
mjr 9:fd65b0a94720 1557 // a long pull will snap back quickly. Note that JOYMAX always
mjr 9:fd65b0a94720 1558 // corresponds to about 3", no matter how many pixels we're
mjr 9:fd65b0a94720 1559 // reading, since the physical sensor is about 3" long; so we
mjr 9:fd65b0a94720 1560 // factor out the pixel count calculate (approximate) physical
mjr 9:fd65b0a94720 1561 // distances based on the normalized axis range.
mjr 9:fd65b0a94720 1562 //
mjr 9:fd65b0a94720 1563 // Firing pattern: when firing, don't simply report a solid 0,
mjr 9:fd65b0a94720 1564 // but instead report a series of pseudo-bouces. This looks
mjr 9:fd65b0a94720 1565 // more realistic, beacause the real plunger is also bouncing
mjr 9:fd65b0a94720 1566 // around during this time. To get maximum firing power in
mjr 9:fd65b0a94720 1567 // the simulation, though, our pseudo-bounces are tiny cmopared
mjr 9:fd65b0a94720 1568 // to the real thing.
mjr 9:fd65b0a94720 1569 const int restTol = JOYMAX/24;
mjr 9:fd65b0a94720 1570 int fireTol = z/3 > JOYMAX/6 ? z/3 : JOYMAX/6;
mjr 9:fd65b0a94720 1571 static const int firePattern[] = {
mjr 9:fd65b0a94720 1572 -JOYMAX/12, -JOYMAX/12, -JOYMAX/12,
mjr 9:fd65b0a94720 1573 };
mjr 9:fd65b0a94720 1574 if (firing != 0)
mjr 6:cc35eb643e8f 1575 {
mjr 6:cc35eb643e8f 1576 // Firing in progress - we've already told VP to send its
mjr 6:cc35eb643e8f 1577 // model plunger all the way back to the rest position, so
mjr 6:cc35eb643e8f 1578 // send no further reports until the mechanical plunger
mjr 6:cc35eb643e8f 1579 // actually comes to rest somewhere.
mjr 6:cc35eb643e8f 1580 if (abs(z0 - z2) < restTol && abs(znew - z2) < restTol)
mjr 6:cc35eb643e8f 1581 {
mjr 6:cc35eb643e8f 1582 // the plunger is back at rest - firing is done
mjr 9:fd65b0a94720 1583 firing = 0;
mjr 6:cc35eb643e8f 1584
mjr 6:cc35eb643e8f 1585 // resume normal reporting
mjr 6:cc35eb643e8f 1586 z = z2;
mjr 6:cc35eb643e8f 1587 }
mjr 9:fd65b0a94720 1588 else if (firing < countof(firePattern))
mjr 9:fd65b0a94720 1589 {
mjr 9:fd65b0a94720 1590 // firing - report the next position in the pseudo-bounce
mjr 9:fd65b0a94720 1591 // pattern
mjr 9:fd65b0a94720 1592 z = firePattern[firing++];
mjr 9:fd65b0a94720 1593 }
mjr 9:fd65b0a94720 1594 else
mjr 9:fd65b0a94720 1595 {
mjr 9:fd65b0a94720 1596 // firing, out of pseudo-bounce items - just report the
mjr 9:fd65b0a94720 1597 // rest position
mjr 9:fd65b0a94720 1598 z = 0;
mjr 9:fd65b0a94720 1599 }
mjr 6:cc35eb643e8f 1600 }
mjr 6:cc35eb643e8f 1601 else if (z0 < z2 && z1 < z2 && znew < z2
mjr 6:cc35eb643e8f 1602 && (z0 < z2 - fireTol
mjr 6:cc35eb643e8f 1603 || z1 < z2 - fireTol
mjr 6:cc35eb643e8f 1604 || znew < z2 - fireTol))
mjr 6:cc35eb643e8f 1605 {
mjr 6:cc35eb643e8f 1606 // Big jumps toward rest position in last two readings -
mjr 6:cc35eb643e8f 1607 // firing has begun. Report an immediate return to the
mjr 6:cc35eb643e8f 1608 // rest position, and send no further reports until the
mjr 6:cc35eb643e8f 1609 // physical plunger has come to rest. This effectively
mjr 6:cc35eb643e8f 1610 // detaches VP's model plunger from the real world for
mjr 6:cc35eb643e8f 1611 // the duration of the spring return, letting VP evolve
mjr 6:cc35eb643e8f 1612 // its model without trying to synchronize with the
mjr 6:cc35eb643e8f 1613 // mechanical version. The release motion is too fast
mjr 6:cc35eb643e8f 1614 // for that to work well; we can't take samples quickly
mjr 6:cc35eb643e8f 1615 // enough to get prcise velocity or acceleration
mjr 6:cc35eb643e8f 1616 // readings. It's better to let VP figure the speed
mjr 6:cc35eb643e8f 1617 // and acceleration through modeling. Plus, that lets
mjr 6:cc35eb643e8f 1618 // each virtual table set the desired parameters for its
mjr 6:cc35eb643e8f 1619 // virtual plunger, rather than imposing the actual
mjr 6:cc35eb643e8f 1620 // mechanical charateristics of the physical plunger on
mjr 6:cc35eb643e8f 1621 // every table.
mjr 9:fd65b0a94720 1622 firing = 1;
mjr 9:fd65b0a94720 1623
mjr 9:fd65b0a94720 1624 // report the first firing pattern position
mjr 9:fd65b0a94720 1625 z = firePattern[0];
mjr 6:cc35eb643e8f 1626 }
mjr 6:cc35eb643e8f 1627 else
mjr 6:cc35eb643e8f 1628 {
mjr 6:cc35eb643e8f 1629 // everything normal; report the 3rd recent position on
mjr 6:cc35eb643e8f 1630 // tape delay
mjr 6:cc35eb643e8f 1631 z = z2;
mjr 6:cc35eb643e8f 1632 }
mjr 6:cc35eb643e8f 1633
mjr 6:cc35eb643e8f 1634 // shift in the new reading
mjr 6:cc35eb643e8f 1635 z2 = z1;
mjr 6:cc35eb643e8f 1636 z1 = z0;
mjr 6:cc35eb643e8f 1637 z0 = znew;
mjr 2:c174f9ee414a 1638 }
mjr 9:fd65b0a94720 1639 else
mjr 9:fd65b0a94720 1640 {
mjr 9:fd65b0a94720 1641 // plunger disabled - pause 10ms to throttle updates to a
mjr 9:fd65b0a94720 1642 // reasonable pace
mjr 9:fd65b0a94720 1643 wait_ms(10);
mjr 9:fd65b0a94720 1644 }
mjr 6:cc35eb643e8f 1645
mjr 1:d913e0afb2ac 1646 // read the accelerometer
mjr 9:fd65b0a94720 1647 int xa, ya;
mjr 9:fd65b0a94720 1648 accel.get(xa, ya);
mjr 1:d913e0afb2ac 1649
mjr 6:cc35eb643e8f 1650 // confine the results to our joystick axis range
mjr 6:cc35eb643e8f 1651 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 6:cc35eb643e8f 1652 if (xa > JOYMAX) xa = JOYMAX;
mjr 6:cc35eb643e8f 1653 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 6:cc35eb643e8f 1654 if (ya > JOYMAX) ya = JOYMAX;
mjr 1:d913e0afb2ac 1655
mjr 6:cc35eb643e8f 1656 // store the updated accelerometer coordinates
mjr 6:cc35eb643e8f 1657 x = xa;
mjr 6:cc35eb643e8f 1658 y = ya;
mjr 6:cc35eb643e8f 1659
mjr 11:bd9da7088e6e 1660 // update the buttons
mjr 11:bd9da7088e6e 1661 uint32_t buttons = readButtonsDebounced();
mjr 11:bd9da7088e6e 1662
mjr 8:c732e279ee29 1663 // Send the status report. Note that the nominal x and y axes
mjr 8:c732e279ee29 1664 // are reversed - this makes it more intuitive to set up in VP.
mjr 8:c732e279ee29 1665 // If we mount the Freesale card flat on the floor of the cabinet
mjr 8:c732e279ee29 1666 // with the USB connectors facing the front of the cabinet, this
mjr 8:c732e279ee29 1667 // arrangement of our nominal axes aligns with VP's standard
mjr 8:c732e279ee29 1668 // setting, so that we can configure VP with X Axis = X on the
mjr 8:c732e279ee29 1669 // joystick and Y Axis = Y on the joystick.
mjr 11:bd9da7088e6e 1670 js.update(y, x, z, buttons, statusFlags);
mjr 1:d913e0afb2ac 1671
mjr 10:976666ffa4ef 1672 // If we're in pixel dump mode, report all pixel exposure values
mjr 10:976666ffa4ef 1673 if (reportPix)
mjr 10:976666ffa4ef 1674 {
mjr 10:976666ffa4ef 1675 // we have satisfied this request
mjr 10:976666ffa4ef 1676 reportPix = false;
mjr 10:976666ffa4ef 1677
mjr 10:976666ffa4ef 1678 // send reports for all pixels
mjr 10:976666ffa4ef 1679 int idx = 0;
mjr 10:976666ffa4ef 1680 while (idx < npix)
mjr 10:976666ffa4ef 1681 js.updateExposure(idx, npix, pix);
mjr 10:976666ffa4ef 1682
mjr 10:976666ffa4ef 1683 // The pixel dump requires many USB reports, since each report
mjr 10:976666ffa4ef 1684 // can only send a few pixel values. An integration cycle has
mjr 10:976666ffa4ef 1685 // been running all this time, since each read starts a new
mjr 10:976666ffa4ef 1686 // cycle. Our timing is longer than usual on this round, so
mjr 10:976666ffa4ef 1687 // the integration won't be comparable to a normal cycle. Throw
mjr 10:976666ffa4ef 1688 // this one away by doing a read now, and throwing it away - that
mjr 10:976666ffa4ef 1689 // will get the timing of the *next* cycle roughly back to normal.
mjr 10:976666ffa4ef 1690 ccd.read(pix, npix);
mjr 10:976666ffa4ef 1691 }
mjr 10:976666ffa4ef 1692
mjr 6:cc35eb643e8f 1693 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 1694 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 1695 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 1696 #endif
mjr 6:cc35eb643e8f 1697
mjr 6:cc35eb643e8f 1698 // provide a visual status indication on the on-board LED
mjr 5:a70c0bce770d 1699 if (calBtnState < 2 && hbTimer.read_ms() > 1000)
mjr 1:d913e0afb2ac 1700 {
mjr 5:a70c0bce770d 1701 if (js.isSuspended() || !js.isConnected())
mjr 2:c174f9ee414a 1702 {
mjr 5:a70c0bce770d 1703 // suspended - turn off the LED
mjr 4:02c7cd7b2183 1704 ledR = 1;
mjr 4:02c7cd7b2183 1705 ledG = 1;
mjr 4:02c7cd7b2183 1706 ledB = 1;
mjr 5:a70c0bce770d 1707
mjr 5:a70c0bce770d 1708 // show a status flash every so often
mjr 5:a70c0bce770d 1709 if (hbcnt % 3 == 0)
mjr 5:a70c0bce770d 1710 {
mjr 6:cc35eb643e8f 1711 // disconnected = red/red flash; suspended = red
mjr 5:a70c0bce770d 1712 for (int n = js.isConnected() ? 1 : 2 ; n > 0 ; --n)
mjr 5:a70c0bce770d 1713 {
mjr 5:a70c0bce770d 1714 ledR = 0;
mjr 5:a70c0bce770d 1715 wait(0.05);
mjr 5:a70c0bce770d 1716 ledR = 1;
mjr 5:a70c0bce770d 1717 wait(0.25);
mjr 5:a70c0bce770d 1718 }
mjr 5:a70c0bce770d 1719 }
mjr 2:c174f9ee414a 1720 }
mjr 6:cc35eb643e8f 1721 else if (needReset)
mjr 2:c174f9ee414a 1722 {
mjr 6:cc35eb643e8f 1723 // connected, need to reset due to changes in config parameters -
mjr 6:cc35eb643e8f 1724 // flash red/green
mjr 6:cc35eb643e8f 1725 hb = !hb;
mjr 6:cc35eb643e8f 1726 ledR = (hb ? 0 : 1);
mjr 6:cc35eb643e8f 1727 ledG = (hb ? 1 : 0);
mjr 6:cc35eb643e8f 1728 ledB = 0;
mjr 6:cc35eb643e8f 1729 }
mjr 6:cc35eb643e8f 1730 else if (cfg.d.ccdEnabled && !cfg.d.plungerCal)
mjr 6:cc35eb643e8f 1731 {
mjr 6:cc35eb643e8f 1732 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 1733 hb = !hb;
mjr 6:cc35eb643e8f 1734 ledR = (hb ? 0 : 1);
mjr 6:cc35eb643e8f 1735 ledG = 0;
mjr 6:cc35eb643e8f 1736 ledB = 1;
mjr 6:cc35eb643e8f 1737 }
mjr 6:cc35eb643e8f 1738 else
mjr 6:cc35eb643e8f 1739 {
mjr 6:cc35eb643e8f 1740 // connected - flash blue/green
mjr 2:c174f9ee414a 1741 hb = !hb;
mjr 4:02c7cd7b2183 1742 ledR = 1;
mjr 4:02c7cd7b2183 1743 ledG = (hb ? 0 : 1);
mjr 4:02c7cd7b2183 1744 ledB = (hb ? 1 : 0);
mjr 2:c174f9ee414a 1745 }
mjr 1:d913e0afb2ac 1746
mjr 1:d913e0afb2ac 1747 // reset the heartbeat timer
mjr 1:d913e0afb2ac 1748 hbTimer.reset();
mjr 5:a70c0bce770d 1749 ++hbcnt;
mjr 1:d913e0afb2ac 1750 }
mjr 1:d913e0afb2ac 1751 }
mjr 0:5acbbe3f4cf4 1752 }