An I/O controller for virtual pinball machines: accelerometer nudge sensing, analog plunger input, button input encoding, LedWiz compatible output controls, and more.

Dependencies:   mbed FastIO FastPWM USBDevice

Fork of Pinscape_Controller by Mike R

/media/uploads/mjr/pinscape_no_background_small_L7Miwr6.jpg

This is Version 2 of the Pinscape Controller, an I/O controller for virtual pinball machines. (You can find the old version 1 software here.) Pinscape is software for the KL25Z that turns the board into a full-featured I/O controller for virtual pinball, with support for accelerometer-based nudging, a mechanical plunger, button inputs, and feedback device control.

In case you haven't heard of the idea before, a "virtual pinball machine" is basically a video pinball simulator that's built into a real pinball machine body. A TV monitor goes in place of the pinball playfield, and a second TV goes in the backbox to show the backglass artwork. Some cabs also include a third monitor to simulate the DMD (Dot Matrix Display) used for scoring on 1990s machines, or even an original plasma DMD. A computer (usually a Windows PC) is hidden inside the cabinet, running pinball emulation software that displays a life-sized playfield on the main TV. The cabinet has all of the usual buttons, too, so it not only looks like the real thing, but plays like it too. That's a picture of my own machine to the right. On the outside, it's built exactly like a real arcade pinball machine, with the same overall dimensions and all of the standard pinball cabinet trim hardware.

It's possible to buy a pre-built virtual pinball machine, but it also makes a great DIY project. If you have some basic wood-working skills and know your way around PCs, you can build one from scratch. The computer part is just an ordinary Windows PC, and all of the pinball emulation can be built out of free, open-source software. In that spirit, the Pinscape Controller is an open-source software/hardware project that offers a no-compromises, all-in-one control center for all of the unique input/output needs of a virtual pinball cabinet. If you've been thinking about building one of these, but you're not sure how to connect a plunger, flipper buttons, lights, nudge sensor, and whatever else you can think of, this project might be just what you're looking for.

You can find much more information about DIY Pin Cab building in general in the Virtual Cabinet Forum on vpforums.org. Also visit my Pinscape Resources page for more about this project and other virtual pinball projects I'm working on.

Downloads

  • Pinscape Release Builds: This page has download links for all of the Pinscape software. To get started, install and run the Pinscape Config Tool on your Windows computer. It will lead you through the steps for installing the Pinscape firmware on the KL25Z.
  • Config Tool Source Code. The complete C# source code for the config tool. You don't need this to run the tool, but it's available if you want to customize anything or see how it works inside.

Documentation

The new Version 2 Build Guide is now complete! This new version aims to be a complete guide to building a virtual pinball machine, including not only the Pinscape elements but all of the basics, from sourcing parts to building all of the hardware.

You can also refer to the original Hardware Build Guide (PDF), but that's out of date now, since it refers to the old version 1 software, which was rather different (especially when it comes to configuration).

System Requirements

The new Config Tool requires a fairly up-to-date Microsoft .NET installation. If you use Windows Update to keep your system current, you should be fine. A modern version of Internet Explorer (IE) is required, even if you don't use it as your main browser, because the Config Tool uses some system components that Microsoft packages into the IE install set. I test with IE11, so that's known to work. IE8 doesn't work. IE9 and 10 are unknown at this point.

The Windows requirements are only for the config tool. The firmware doesn't care about anything on the Windows side, so if you can make do without the config tool, you can use almost any Windows setup.

Main Features

Plunger: The Pinscape Controller started out as a "mechanical plunger" controller: a device for attaching a real pinball plunger to the video game software so that you could launch the ball the natural way. This is still, of course, a central feature of the project. The software supports several types of sensors: a high-resolution optical sensor (which works by essentially taking pictures of the plunger as it moves); a slide potentiometer (which determines the position via the changing electrical resistance in the pot); a quadrature sensor (which counts bars printed on a special guide rail that it moves along); and an IR distance sensor (which determines the position by sending pulses of light at the plunger and measuring the round-trip travel time). The Build Guide explains how to set up each type of sensor.

Nudging: The KL25Z (the little microcontroller that the software runs on) has a built-in accelerometer. The Pinscape software uses it to sense when you nudge the cabinet, and feeds the acceleration data to the pinball software on the PC. This turns physical nudges into virtual English on the ball. The accelerometer is quite sensitive and accurate, so we can measure the difference between little bumps and hard shoves, and everything in between. The result is natural and immersive.

Buttons: You can wire real pinball buttons to the KL25Z, and the software will translate the buttons into PC input. You have the option to map each button to a keyboard key or joystick button. You can wire up your flipper buttons, Magna Save buttons, Start button, coin slots, operator buttons, and whatever else you need.

Feedback devices: You can also attach "feedback devices" to the KL25Z. Feedback devices are things that create tactile, sound, and lighting effects in sync with the game action. The most popular PC pinball emulators know how to address a wide variety of these devices, and know how to match them to on-screen action in each virtual table. You just need an I/O controller that translates commands from the PC into electrical signals that turn the devices on and off. The Pinscape Controller can do that for you.

Expansion Boards

There are two main ways to run the Pinscape Controller: standalone, or using the "expansion boards".

In the basic standalone setup, you just need the KL25Z, plus whatever buttons, sensors, and feedback devices you want to attach to it. This mode lets you take advantage of everything the software can do, but for some features, you'll have to build some ad hoc external circuitry to interface external devices with the KL25Z. The Build Guide has detailed plans for exactly what you need to build.

The other option is the Pinscape Expansion Boards. The expansion boards are a companion project, which is also totally free and open-source, that provides Printed Circuit Board (PCB) layouts that are designed specifically to work with the Pinscape software. The PCB designs are in the widely used EAGLE format, which many PCB manufacturers can turn directly into physical boards for you. The expansion boards organize all of the external connections more neatly than on the standalone KL25Z, and they add all of the interface circuitry needed for all of the advanced software functions. The big thing they bring to the table is lots of high-power outputs. The boards provide a modular system that lets you add boards to add more outputs. If you opt for the basic core setup, you'll have enough outputs for all of the toys in a really well-equipped cabinet. If your ambitions go beyond merely well-equipped and run to the ridiculously extravagant, just add an extra board or two. The modular design also means that you can add to the system over time.

Expansion Board project page

Update notes

If you have a Pinscape V1 setup already installed, you should be able to switch to the new version pretty seamlessly. There are just a couple of things to be aware of.

First, the "configuration" procedure is completely different in the new version. Way better and way easier, but it's not what you're used to from V1. In V1, you had to edit the project source code and compile your own custom version of the program. No more! With V2, you simply install the standard, pre-compiled .bin file, and select options using the Pinscape Config Tool on Windows.

Second, if you're using the TSL1410R optical sensor for your plunger, there's a chance you'll need to boost your light source's brightness a little bit. The "shutter speed" is faster in this version, which means that it doesn't spend as much time collecting light per frame as before. The software actually does "auto exposure" adaptation on every frame, so the increased shutter speed really shouldn't bother it, but it does require a certain minimum level of contrast, which requires a certain minimal level of lighting. Check the plunger viewer in the setup tool if you have any problems; if the image looks totally dark, try increasing the light level to see if that helps.

New Features

V2 has numerous new features. Here are some of the highlights...

Dynamic configuration: as explained above, configuration is now handled through the Config Tool on Windows. It's no longer necessary to edit the source code or compile your own modified binary.

Improved plunger sensing: the software now reads the TSL1410R optical sensor about 15x faster than it did before. This allows reading the sensor at full resolution (400dpi), about 400 times per second. The faster frame rate makes a big difference in how accurately we can read the plunger position during the fast motion of a release, which allows for more precise position sensing and faster response. The differences aren't dramatic, since the sensing was already pretty good even with the slower V1 scan rate, but you might notice a little better precision in tricky skill shots.

Keyboard keys: button inputs can now be mapped to keyboard keys. The joystick button option is still available as well, of course. Keyboard keys have the advantage of being closer to universal for PC pinball software: some pinball software can be set up to take joystick input, but nearly all PC pinball emulators can take keyboard input, and nearly all of them use the same key mappings.

Local shift button: one physical button can be designed as the local shift button. This works like a Shift button on a keyboard, but with cabinet buttons. It allows each physical button on the cabinet to have two PC keys assigned, one normal and one shifted. Hold down the local shift button, then press another key, and the other key's shifted key mapping is sent to the PC. The shift button can have a regular key mapping of its own as well, so it can do double duty. The shift feature lets you access more functions without cluttering your cabinet with extra buttons. It's especially nice for less frequently used functions like adjusting the volume or activating night mode.

Night mode: the output controller has a new "night mode" option, which lets you turn off all of your noisy devices with a single button, switch, or PC command. You can designate individual ports as noisy or not. Night mode only disables the noisemakers, so you still get the benefit of your flashers, button lights, and other quiet devices. This lets you play late into the night without disturbing your housemates or neighbors.

Gamma correction: you can designate individual output ports for gamma correction. This adjusts the intensity level of an output to make it match the way the human eye perceives brightness, so that fades and color mixes look more natural in lighting devices. You can apply this to individual ports, so that it only affects ports that actually have lights of some kind attached.

IR Remote Control: the controller software can transmit and/or receive IR remote control commands if you attach appropriate parts (an IR LED to send, an IR sensor chip to receive). This can be used to turn on your TV(s) when the system powers on, if they don't turn on automatically, and for any other functions you can think of requiring IR send/receive capabilities. You can assign IR commands to cabinet buttons, so that pressing a button on your cabinet sends a remote control command from the attached IR LED, and you can have the controller generate virtual key presses on your PC in response to received IR commands. If you have the IR sensor attached, the system can use it to learn commands from your existing remotes.

Yet more USB fixes: I've been gradually finding and fixing USB bugs in the mbed library for months now. This version has all of the fixes of the last couple of releases, of course, plus some new ones. It also has a new "last resort" feature, since there always seems to be "just one more" USB bug. The last resort is that you can tell the device to automatically reboot itself if it loses the USB connection and can't restore it within a given time limit.

More Downloads

  • Custom VP builds: I created modified versions of Visual Pinball 9.9 and Physmod5 that you might want to use in combination with this controller. The modified versions have special handling for plunger calibration specific to the Pinscape Controller, as well as some enhancements to the nudge physics. If you're not using the plunger, you might still want it for the nudge improvements. The modified version also works with any other input controller, so you can get the enhanced nudging effects even if you're using a different plunger/nudge kit. The big change in the modified versions is a "filter" for accelerometer input that's designed to make the response to cabinet nudges more realistic. It also makes the response more subdued than in the standard VP, so it's not to everyone's taste. The downloads include both the updated executables and the source code changes, in case you want to merge the changes into your own custom version(s).

    Note! These features are now standard in the official VP releases, so you don't need my custom builds if you're using 9.9.1 or later and/or VP 10. I don't think there's any reason to use my versions instead of the latest official ones, and in fact I'd encourage you to use the official releases since they're more up to date, but I'm leaving my builds available just in case. In the official versions, look for the checkbox "Enable Nudge Filter" in the Keys preferences dialog. My custom versions don't include that checkbox; they just enable the filter unconditionally.
  • Output circuit shopping list: This is a saved shopping cart at mouser.com with the parts needed to build one copy of the high-power output circuit for the LedWiz emulator feature, for use with the standalone KL25Z (that is, without the expansion boards). The quantities in the cart are for one output channel, so if you want N outputs, simply multiply the quantities by the N, with one exception: you only need one ULN2803 transistor array chip for each eight output circuits. If you're using the expansion boards, you won't need any of this, since the boards provide their own high-power outputs.
  • Cary Owens' optical sensor housing: A 3D-printable design for a housing/mounting bracket for the optical plunger sensor, designed by Cary Owens. This makes it easy to mount the sensor.
  • Lemming77's potentiometer mounting bracket and shooter rod connecter: Sketchup designs for 3D-printable parts for mounting a slide potentiometer as the plunger sensor. These were designed for a particular slide potentiometer that used to be available from an Aliexpress.com seller but is no longer listed. You can probably use this design as a starting point for other similar devices; just check the dimensions before committing the design to plastic.

Copyright and License

The Pinscape firmware is copyright 2014, 2021 by Michael J Roberts. It's released under an MIT open-source license. See License.

Warning to VirtuaPin Kit Owners

This software isn't designed as a replacement for the VirtuaPin plunger kit's firmware. If you bought the VirtuaPin kit, I recommend that you don't install this software. The KL25Z can only run one firmware program at a time, so if you install the Pinscape firmware on your KL25Z, it will replace and erase your existing VirtuaPin proprietary firmware. If you do this, the only way to restore your VirtuaPin firmware is to physically ship the KL25Z back to VirtuaPin and ask them to re-flash it. They don't allow you to do this at home, and they don't even allow you to back up your firmware, since they want to protect their proprietary software from copying. For all of these reasons, if you want to run the Pinscape software, I strongly recommend that you buy a "blank" retail KL25Z to use with Pinscape. They only cost about $15 and are available at several online retailers, including Amazon, Mouser, and eBay. The blank retail boards don't come with any proprietary firmware pre-installed, so installing Pinscape won't delete anything that you paid extra for.

With those warnings in mind, if you're absolutely sure that you don't mind permanently erasing your VirtuaPin firmware, it is at least possible to use Pinscape as a replacement for the VirtuaPin firmware. Pinscape uses the same button wiring conventions as the VirtuaPin setup, so you can keep your buttons (although you'll have to update the GPIO pin mappings in the Config Tool to match your physical wiring). As of the June, 2021 firmware, the Vishay VCNL4010 plunger sensor that comes with the VirtuaPin v3 plunger kit is supported, so you can also keep your plunger, if you have that chip. (You should check to be sure that's the sensor chip you have before committing to this route, if keeping the plunger sensor is important to you. The older VirtuaPin plunger kits came with different IR sensors that the Pinscape software doesn't handle.)

Committer:
mjr
Date:
Wed Sep 23 05:06:39 2015 +0000
Revision:
26:cb71c4af2912
Parent:
25:e22b88bd783a
Child:
29:582472d0bc57
Initial TLC5940 PWM controller chip support.

Who changed what in which revision?

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