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:
Sun Nov 27 21:42:42 2016 +0000
Revision:
67:c39e66c4e000
Parent:
66:2e3583fbd2f4
Child:
69:cc5039284fac
Send USB reports for Keyboard Volume Up, Keyboard Volume Down, and Keyboard Mute as ordinary keyboard keys (they were previously mapped to the corresponding Media Control keys)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 51:57eb311faafa 1 /* Copyright 2014, 2016 M J Roberts, MIT License
mjr 5:a70c0bce770d 2 *
mjr 5:a70c0bce770d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mjr 5:a70c0bce770d 4 * and associated documentation files (the "Software"), to deal in the Software without
mjr 5:a70c0bce770d 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mjr 5:a70c0bce770d 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mjr 5:a70c0bce770d 7 * Software is furnished to do so, subject to the following conditions:
mjr 5:a70c0bce770d 8 *
mjr 5:a70c0bce770d 9 * The above copyright notice and this permission notice shall be included in all copies or
mjr 5:a70c0bce770d 10 * substantial portions of the Software.
mjr 5:a70c0bce770d 11 *
mjr 5:a70c0bce770d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mjr 48:058ace2aed1d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILIT Y, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 5:a70c0bce770d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 5:a70c0bce770d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 5:a70c0bce770d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 5:a70c0bce770d 17 */
mjr 5:a70c0bce770d 18
mjr 5:a70c0bce770d 19 //
mjr 35:e959ffba78fd 20 // The Pinscape Controller
mjr 35:e959ffba78fd 21 // A comprehensive input/output controller for virtual pinball machines
mjr 5:a70c0bce770d 22 //
mjr 48:058ace2aed1d 23 // This project implements an I/O controller for virtual pinball cabinets. The
mjr 48:058ace2aed1d 24 // controller's function is to connect Visual Pinball (and other Windows pinball
mjr 48:058ace2aed1d 25 // emulators) with physical devices in the cabinet: buttons, sensors, and
mjr 48:058ace2aed1d 26 // feedback devices that create visual or mechanical effects during play.
mjr 38:091e511ce8a0 27 //
mjr 48:058ace2aed1d 28 // The controller can perform several different functions, which can be used
mjr 38:091e511ce8a0 29 // individually or in any combination:
mjr 5:a70c0bce770d 30 //
mjr 38:091e511ce8a0 31 // - Nudge sensing. This uses the KL25Z's on-board accelerometer to sense the
mjr 38:091e511ce8a0 32 // motion of the cabinet when you nudge it. Visual Pinball and other pinball
mjr 38:091e511ce8a0 33 // emulators on the PC have native handling for this type of input, so that
mjr 38:091e511ce8a0 34 // physical nudges on the cabinet turn into simulated effects on the virtual
mjr 38:091e511ce8a0 35 // ball. The KL25Z measures accelerations as analog readings and is quite
mjr 38:091e511ce8a0 36 // sensitive, so the effect of a nudge on the simulation is proportional
mjr 38:091e511ce8a0 37 // to the strength of the nudge. Accelerations are reported to the PC via a
mjr 38:091e511ce8a0 38 // simulated joystick (using the X and Y axes); you just have to set some
mjr 38:091e511ce8a0 39 // preferences in your pinball software to tell it that an accelerometer
mjr 38:091e511ce8a0 40 // is attached.
mjr 5:a70c0bce770d 41 //
mjr 38:091e511ce8a0 42 // - Plunger position sensing, with mulitple sensor options. To use this feature,
mjr 35:e959ffba78fd 43 // you need to choose a sensor and set it up, connect the sensor electrically to
mjr 35:e959ffba78fd 44 // the KL25Z, and configure the Pinscape software on the KL25Z to let it know how
mjr 35:e959ffba78fd 45 // the sensor is hooked up. The Pinscape software monitors the sensor and sends
mjr 35:e959ffba78fd 46 // readings to Visual Pinball via the joystick Z axis. VP and other PC software
mjr 38:091e511ce8a0 47 // have native support for this type of input; as with the nudge setup, you just
mjr 38:091e511ce8a0 48 // have to set some options in VP to activate the plunger.
mjr 17:ab3cec0c8bf4 49 //
mjr 35:e959ffba78fd 50 // The Pinscape software supports optical sensors (the TAOS TSL1410R and TSL1412R
mjr 35:e959ffba78fd 51 // linear sensor arrays) as well as slide potentiometers. The specific equipment
mjr 35:e959ffba78fd 52 // that's supported, along with physical mounting and wiring details, can be found
mjr 35:e959ffba78fd 53 // in the Build Guide.
mjr 35:e959ffba78fd 54 //
mjr 38:091e511ce8a0 55 // Note VP has built-in support for plunger devices like this one, but some VP
mjr 38:091e511ce8a0 56 // tables can't use it without some additional scripting work. The Build Guide has
mjr 38:091e511ce8a0 57 // advice on adjusting tables to add plunger support when necessary.
mjr 5:a70c0bce770d 58 //
mjr 6:cc35eb643e8f 59 // For best results, the plunger sensor should be calibrated. The calibration
mjr 6:cc35eb643e8f 60 // is stored in non-volatile memory on board the KL25Z, so it's only necessary
mjr 6:cc35eb643e8f 61 // to do the calibration once, when you first install everything. (You might
mjr 6:cc35eb643e8f 62 // also want to re-calibrate if you physically remove and reinstall the CCD
mjr 17:ab3cec0c8bf4 63 // sensor or the mechanical plunger, since their alignment shift change slightly
mjr 17:ab3cec0c8bf4 64 // when you put everything back together.) You can optionally install a
mjr 17:ab3cec0c8bf4 65 // dedicated momentary switch or pushbutton to activate the calibration mode;
mjr 17:ab3cec0c8bf4 66 // this is describe in the project documentation. If you don't want to bother
mjr 17:ab3cec0c8bf4 67 // with the extra button, you can also trigger calibration using the Windows
mjr 17:ab3cec0c8bf4 68 // setup software, which you can find on the Pinscape project page.
mjr 6:cc35eb643e8f 69 //
mjr 17:ab3cec0c8bf4 70 // The calibration procedure is described in the project documentation. Briefly,
mjr 17:ab3cec0c8bf4 71 // when you trigger calibration mode, the software will scan the CCD for about
mjr 17:ab3cec0c8bf4 72 // 15 seconds, during which you should simply pull the physical plunger back
mjr 17:ab3cec0c8bf4 73 // all the way, hold it for a moment, and then slowly return it to the rest
mjr 17:ab3cec0c8bf4 74 // position. (DON'T just release it from the retracted position, since that
mjr 17:ab3cec0c8bf4 75 // let it shoot forward too far. We want to measure the range from the park
mjr 17:ab3cec0c8bf4 76 // position to the fully retracted position only.)
mjr 5:a70c0bce770d 77 //
mjr 13:72dda449c3c0 78 // - Button input wiring. 24 of the KL25Z's GPIO ports are mapped as digital inputs
mjr 38:091e511ce8a0 79 // for buttons and switches. You can wire each input to a physical pinball-style
mjr 38:091e511ce8a0 80 // button or switch, such as flipper buttons, Start buttons, coin chute switches,
mjr 38:091e511ce8a0 81 // tilt bobs, and service buttons. Each button can be configured to be reported
mjr 38:091e511ce8a0 82 // to the PC as a joystick button or as a keyboard key (you can select which key
mjr 38:091e511ce8a0 83 // is used for each button).
mjr 13:72dda449c3c0 84 //
mjr 53:9b2611964afc 85 // - LedWiz emulation. The KL25Z can pretend to be an LedWiz device. This lets
mjr 53:9b2611964afc 86 // you connect feedback devices (lights, solenoids, motors) to GPIO ports on the
mjr 53:9b2611964afc 87 // KL25Z, and lets PC software (such as Visual Pinball) control them during game
mjr 53:9b2611964afc 88 // play to create a more immersive playing experience. The Pinscape software
mjr 53:9b2611964afc 89 // presents itself to the host as an LedWiz device and accepts the full LedWiz
mjr 53:9b2611964afc 90 // command set, so software on the PC designed for real LedWiz'es can control
mjr 53:9b2611964afc 91 // attached devices without any modifications.
mjr 5:a70c0bce770d 92 //
mjr 53:9b2611964afc 93 // Even though the software provides a very thorough LedWiz emulation, the KL25Z
mjr 53:9b2611964afc 94 // GPIO hardware design imposes some serious limitations. The big one is that
mjr 53:9b2611964afc 95 // the KL25Z only has 10 PWM channels, meaning that only 10 ports can have
mjr 53:9b2611964afc 96 // varying-intensity outputs (e.g., for controlling the brightness level of an
mjr 53:9b2611964afc 97 // LED or the speed or a motor). You can control more than 10 output ports, but
mjr 53:9b2611964afc 98 // only 10 can have PWM control; the rest are simple "digital" ports that can only
mjr 53:9b2611964afc 99 // be switched fully on or fully off. The second limitation is that the KL25Z
mjr 53:9b2611964afc 100 // just doesn't have that many GPIO ports overall. There are enough to populate
mjr 53:9b2611964afc 101 // all 32 button inputs OR all 32 LedWiz outputs, but not both. The default is
mjr 53:9b2611964afc 102 // to assign 24 buttons and 22 LedWiz ports; you can change this balance to trade
mjr 53:9b2611964afc 103 // off more outputs for fewer inputs, or vice versa. The third limitation is that
mjr 53:9b2611964afc 104 // the KL25Z GPIO pins have *very* tiny amperage limits - just 4mA, which isn't
mjr 53:9b2611964afc 105 // even enough to control a small LED. So in order to connect any kind of feedback
mjr 53:9b2611964afc 106 // device to an output, you *must* build some external circuitry to boost the
mjr 53:9b2611964afc 107 // current handing. The Build Guide has a reference circuit design for this
mjr 53:9b2611964afc 108 // purpose that's simple and inexpensive to build.
mjr 6:cc35eb643e8f 109 //
mjr 26:cb71c4af2912 110 // - Enhanced LedWiz emulation with TLC5940 PWM controller chips. You can attach
mjr 26:cb71c4af2912 111 // external PWM controller chips for controlling device outputs, instead of using
mjr 53:9b2611964afc 112 // the on-board GPIO ports as described above. The software can control a set of
mjr 53:9b2611964afc 113 // daisy-chained TLC5940 chips. Each chip provides 16 PWM outputs, so you just
mjr 53:9b2611964afc 114 // need two of them to get the full complement of 32 output ports of a real LedWiz.
mjr 53:9b2611964afc 115 // You can hook up even more, though. Four chips gives you 64 ports, which should
mjr 53:9b2611964afc 116 // be plenty for nearly any virtual pinball project. To accommodate the larger
mjr 53:9b2611964afc 117 // supply of ports possible with the PWM chips, the controller software provides
mjr 53:9b2611964afc 118 // a custom, extended version of the LedWiz protocol that can handle up to 128
mjr 53:9b2611964afc 119 // ports. PC software designed only for the real LedWiz obviously won't know
mjr 53:9b2611964afc 120 // about the extended protocol and won't be able to take advantage of its extra
mjr 53:9b2611964afc 121 // capabilities, but the latest version of DOF (DirectOutput Framework) *does*
mjr 53:9b2611964afc 122 // know the new language and can take full advantage. Older software will still
mjr 53:9b2611964afc 123 // work, though - the new extensions are all backward compatible, so old software
mjr 53:9b2611964afc 124 // that only knows about the original LedWiz protocol will still work, with the
mjr 53:9b2611964afc 125 // obvious limitation that it can only access the first 32 ports.
mjr 53:9b2611964afc 126 //
mjr 53:9b2611964afc 127 // The Pinscape Expansion Board project (which appeared in early 2016) provides
mjr 53:9b2611964afc 128 // a reference hardware design, with EAGLE circuit board layouts, that takes full
mjr 53:9b2611964afc 129 // advantage of the TLC5940 capability. It lets you create a customized set of
mjr 53:9b2611964afc 130 // outputs with full PWM control and power handling for high-current devices
mjr 53:9b2611964afc 131 // built in to the boards.
mjr 26:cb71c4af2912 132 //
mjr 38:091e511ce8a0 133 // - Night Mode control for output devices. You can connect a switch or button
mjr 38:091e511ce8a0 134 // to the controller to activate "Night Mode", which disables feedback devices
mjr 38:091e511ce8a0 135 // that you designate as noisy. You can designate outputs individually as being
mjr 38:091e511ce8a0 136 // included in this set or not. This is useful if you want to play a game on
mjr 38:091e511ce8a0 137 // your cabinet late at night without waking the kids and annoying the neighbors.
mjr 38:091e511ce8a0 138 //
mjr 38:091e511ce8a0 139 // - TV ON switch. The controller can pulse a relay to turn on your TVs after
mjr 38:091e511ce8a0 140 // power to the cabinet comes on, with a configurable delay timer. This feature
mjr 38:091e511ce8a0 141 // is for TVs that don't turn themselves on automatically when first plugged in.
mjr 38:091e511ce8a0 142 // To use this feature, you have to build some external circuitry to allow the
mjr 38:091e511ce8a0 143 // software to sense the power supply status, and you have to run wires to your
mjr 38:091e511ce8a0 144 // TV's on/off button, which requires opening the case on your TV. The Build
mjr 38:091e511ce8a0 145 // Guide has details on the necessary circuitry and connections to the TV.
mjr 38:091e511ce8a0 146 //
mjr 35:e959ffba78fd 147 //
mjr 35:e959ffba78fd 148 //
mjr 33:d832bcab089e 149 // STATUS LIGHTS: The on-board LED on the KL25Z flashes to indicate the current
mjr 33:d832bcab089e 150 // device status. The flash patterns are:
mjr 6:cc35eb643e8f 151 //
mjr 48:058ace2aed1d 152 // short yellow flash = waiting to connect
mjr 6:cc35eb643e8f 153 //
mjr 48:058ace2aed1d 154 // short red flash = the connection is suspended (the host is in sleep
mjr 48:058ace2aed1d 155 // or suspend mode, the USB cable is unplugged after a connection
mjr 48:058ace2aed1d 156 // has been established)
mjr 48:058ace2aed1d 157 //
mjr 48:058ace2aed1d 158 // two short red flashes = connection lost (the device should immediately
mjr 48:058ace2aed1d 159 // go back to short-yellow "waiting to reconnect" mode when a connection
mjr 48:058ace2aed1d 160 // is lost, so this display shouldn't normally appear)
mjr 6:cc35eb643e8f 161 //
mjr 38:091e511ce8a0 162 // long red/yellow = USB connection problem. The device still has a USB
mjr 48:058ace2aed1d 163 // connection to the host (or so it appears to the device), but data
mjr 48:058ace2aed1d 164 // transmissions are failing.
mjr 38:091e511ce8a0 165 //
mjr 6:cc35eb643e8f 166 // long yellow/green = everything's working, but the plunger hasn't
mjr 38:091e511ce8a0 167 // been calibrated. Follow the calibration procedure described in
mjr 38:091e511ce8a0 168 // the project documentation. This flash mode won't appear if there's
mjr 38:091e511ce8a0 169 // no plunger sensor configured.
mjr 6:cc35eb643e8f 170 //
mjr 38:091e511ce8a0 171 // alternating blue/green = everything's working normally, and plunger
mjr 38:091e511ce8a0 172 // calibration has been completed (or there's no plunger attached)
mjr 10:976666ffa4ef 173 //
mjr 48:058ace2aed1d 174 // fast red/purple = out of memory. The controller halts and displays
mjr 48:058ace2aed1d 175 // this diagnostic code until you manually reset it. If this happens,
mjr 48:058ace2aed1d 176 // it's probably because the configuration is too complex, in which
mjr 48:058ace2aed1d 177 // case the same error will occur after the reset. If it's stuck
mjr 48:058ace2aed1d 178 // in this cycle, you'll have to restore the default configuration
mjr 48:058ace2aed1d 179 // by re-installing the controller software (the Pinscape .bin file).
mjr 10:976666ffa4ef 180 //
mjr 48:058ace2aed1d 181 //
mjr 48:058ace2aed1d 182 // USB PROTOCOL: Most of our USB messaging is through standard USB HID
mjr 48:058ace2aed1d 183 // classes (joystick, keyboard). We also accept control messages on our
mjr 48:058ace2aed1d 184 // primary HID interface "OUT endpoint" using a custom protocol that's
mjr 48:058ace2aed1d 185 // not defined in any USB standards (we do have to provide a USB HID
mjr 48:058ace2aed1d 186 // Report Descriptor for it, but this just describes the protocol as
mjr 48:058ace2aed1d 187 // opaque vendor-defined bytes). The control protocol incorporates the
mjr 48:058ace2aed1d 188 // LedWiz protocol as a subset, and adds our own private extensions.
mjr 48:058ace2aed1d 189 // For full details, see USBProtocol.h.
mjr 33:d832bcab089e 190
mjr 33:d832bcab089e 191
mjr 0:5acbbe3f4cf4 192 #include "mbed.h"
mjr 6:cc35eb643e8f 193 #include "math.h"
mjr 48:058ace2aed1d 194 #include "pinscape.h"
mjr 0:5acbbe3f4cf4 195 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 196 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 197 #include "tsl1410r.h"
mjr 1:d913e0afb2ac 198 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 199 #include "crc32.h"
mjr 26:cb71c4af2912 200 #include "TLC5940.h"
mjr 34:6b981a2afab7 201 #include "74HC595.h"
mjr 35:e959ffba78fd 202 #include "nvm.h"
mjr 35:e959ffba78fd 203 #include "plunger.h"
mjr 35:e959ffba78fd 204 #include "ccdSensor.h"
mjr 35:e959ffba78fd 205 #include "potSensor.h"
mjr 35:e959ffba78fd 206 #include "nullSensor.h"
mjr 48:058ace2aed1d 207 #include "TinyDigitalIn.h"
mjr 64:ef7ca92dff36 208 #include "FastPWM.h"
mjr 2:c174f9ee414a 209
mjr 21:5048e16cc9ef 210 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 211 #include "config.h"
mjr 17:ab3cec0c8bf4 212
mjr 53:9b2611964afc 213
mjr 53:9b2611964afc 214 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 215 //
mjr 53:9b2611964afc 216 // OpenSDA module identifier. This is for the benefit of the Windows
mjr 53:9b2611964afc 217 // configuration tool. When the config tool installs a .bin file onto
mjr 53:9b2611964afc 218 // the KL25Z, it will first find the sentinel string within the .bin file,
mjr 53:9b2611964afc 219 // and patch the "\0" bytes that follow the sentinel string with the
mjr 53:9b2611964afc 220 // OpenSDA module ID data. This allows us to report the OpenSDA
mjr 53:9b2611964afc 221 // identifiers back to the host system via USB, which in turn allows the
mjr 53:9b2611964afc 222 // config tool to figure out which OpenSDA MSD (mass storage device - a
mjr 53:9b2611964afc 223 // virtual disk drive) correlates to which Pinscape controller USB
mjr 53:9b2611964afc 224 // interface.
mjr 53:9b2611964afc 225 //
mjr 53:9b2611964afc 226 // This is only important if multiple Pinscape devices are attached to
mjr 53:9b2611964afc 227 // the same host. There doesn't seem to be any other way to figure out
mjr 53:9b2611964afc 228 // which OpenSDA MSD corresponds to which KL25Z USB interface; the OpenSDA
mjr 53:9b2611964afc 229 // MSD doesn't report the KL25Z CPU ID anywhere, and the KL25Z doesn't
mjr 53:9b2611964afc 230 // have any way to learn about the OpenSDA module it's connected to. The
mjr 53:9b2611964afc 231 // only way to pass this information to the KL25Z side that I can come up
mjr 53:9b2611964afc 232 // with is to have the Windows host embed it in the .bin file before
mjr 53:9b2611964afc 233 // downloading it to the OpenSDA MSD.
mjr 53:9b2611964afc 234 //
mjr 53:9b2611964afc 235 // We initialize the const data buffer (the part after the sentinel string)
mjr 53:9b2611964afc 236 // with all "\0" bytes, so that's what will be in the executable image that
mjr 53:9b2611964afc 237 // comes out of the mbed compiler. If you manually install the resulting
mjr 53:9b2611964afc 238 // .bin file onto the KL25Z (via the Windows desktop, say), the "\0" bytes
mjr 53:9b2611964afc 239 // will stay this way and read as all 0's at run-time. Since a real TUID
mjr 53:9b2611964afc 240 // would never be all 0's, that tells us that we were never patched and
mjr 53:9b2611964afc 241 // thus don't have any information on the OpenSDA module.
mjr 53:9b2611964afc 242 //
mjr 53:9b2611964afc 243 const char *getOpenSDAID()
mjr 53:9b2611964afc 244 {
mjr 53:9b2611964afc 245 #define OPENSDA_PREFIX "///Pinscape.OpenSDA.TUID///"
mjr 53:9b2611964afc 246 static const char OpenSDA[] = OPENSDA_PREFIX "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0///";
mjr 53:9b2611964afc 247 const size_t OpenSDA_prefix_length = sizeof(OPENSDA_PREFIX) - 1;
mjr 53:9b2611964afc 248
mjr 53:9b2611964afc 249 return OpenSDA + OpenSDA_prefix_length;
mjr 53:9b2611964afc 250 }
mjr 53:9b2611964afc 251
mjr 53:9b2611964afc 252 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 253 //
mjr 53:9b2611964afc 254 // Build ID. We use the date and time of compiling the program as a build
mjr 53:9b2611964afc 255 // identifier. It would be a little nicer to use a simple serial number
mjr 53:9b2611964afc 256 // instead, but the mbed platform doesn't have a way to automate that. The
mjr 53:9b2611964afc 257 // timestamp is a pretty good proxy for a serial number in that it will
mjr 53:9b2611964afc 258 // naturally increase on each new build, which is the primary property we
mjr 53:9b2611964afc 259 // want from this.
mjr 53:9b2611964afc 260 //
mjr 53:9b2611964afc 261 // As with the embedded OpenSDA ID, we store the build timestamp with a
mjr 53:9b2611964afc 262 // sentinel string prefix, to allow automated tools to find the static data
mjr 53:9b2611964afc 263 // in the .bin file by searching for the sentinel string. In contrast to
mjr 53:9b2611964afc 264 // the OpenSDA ID, the value we store here is for tools to extract rather
mjr 53:9b2611964afc 265 // than store, since we automatically populate it via the preprocessor
mjr 53:9b2611964afc 266 // macros.
mjr 53:9b2611964afc 267 //
mjr 53:9b2611964afc 268 const char *getBuildID()
mjr 53:9b2611964afc 269 {
mjr 53:9b2611964afc 270 #define BUILDID_PREFIX "///Pinscape.Build.ID///"
mjr 53:9b2611964afc 271 static const char BuildID[] = BUILDID_PREFIX __DATE__ " " __TIME__ "///";
mjr 53:9b2611964afc 272 const size_t BuildID_prefix_length = sizeof(BUILDID_PREFIX) - 1;
mjr 53:9b2611964afc 273
mjr 53:9b2611964afc 274 return BuildID + BuildID_prefix_length;
mjr 53:9b2611964afc 275 }
mjr 53:9b2611964afc 276
mjr 53:9b2611964afc 277
mjr 48:058ace2aed1d 278 // --------------------------------------------------------------------------
mjr 48:058ace2aed1d 279 //
mjr 59:94eb9265b6d7 280 // Custom memory allocator. We use our own version of malloc() for more
mjr 59:94eb9265b6d7 281 // efficient memory usage, and to provide diagnostics if we run out of heap.
mjr 48:058ace2aed1d 282 //
mjr 59:94eb9265b6d7 283 // We can implement a more efficient malloc than the library can because we
mjr 59:94eb9265b6d7 284 // can make an assumption that the library can't: allocations are permanent.
mjr 59:94eb9265b6d7 285 // The normal malloc has to assume that allocations can be freed, so it has
mjr 59:94eb9265b6d7 286 // to track blocks individually. For the purposes of this program, though,
mjr 59:94eb9265b6d7 287 // we don't have to do this because virtually all of our allocations are
mjr 59:94eb9265b6d7 288 // de facto permanent. We only allocate dyanmic memory during setup, and
mjr 59:94eb9265b6d7 289 // once we set things up, we never delete anything. This means that we can
mjr 59:94eb9265b6d7 290 // allocate memory in bare blocks without any bookkeeping overhead.
mjr 59:94eb9265b6d7 291 //
mjr 59:94eb9265b6d7 292 // In addition, we can make a much larger overall pool of memory available
mjr 59:94eb9265b6d7 293 // in a custom allocator. The mbed library malloc() seems to have a pool
mjr 59:94eb9265b6d7 294 // of about 3K to work with, even though there's really about 9K of RAM
mjr 59:94eb9265b6d7 295 // left over after counting the static writable data and reserving space
mjr 59:94eb9265b6d7 296 // for a reasonable stack. I haven't looked at the mbed malloc to see why
mjr 59:94eb9265b6d7 297 // they're so stingy, but it appears from empirical testing that we can
mjr 59:94eb9265b6d7 298 // create a static array up to about 9K before things get crashy.
mjr 59:94eb9265b6d7 299
mjr 48:058ace2aed1d 300 void *xmalloc(size_t siz)
mjr 48:058ace2aed1d 301 {
mjr 59:94eb9265b6d7 302 // Dynamic memory pool. We'll reserve space for all dynamic
mjr 59:94eb9265b6d7 303 // allocations by creating a simple C array of bytes. The size
mjr 59:94eb9265b6d7 304 // of this array is the maximum number of bytes we can allocate
mjr 59:94eb9265b6d7 305 // with malloc or operator 'new'.
mjr 59:94eb9265b6d7 306 //
mjr 59:94eb9265b6d7 307 // The maximum safe size for this array is, in essence, the
mjr 59:94eb9265b6d7 308 // amount of physical KL25Z RAM left over after accounting for
mjr 59:94eb9265b6d7 309 // static data throughout the rest of the program, the run-time
mjr 59:94eb9265b6d7 310 // stack, and any other space reserved for compiler or MCU
mjr 59:94eb9265b6d7 311 // overhead. Unfortunately, it's not straightforward to
mjr 59:94eb9265b6d7 312 // determine this analytically. The big complication is that
mjr 59:94eb9265b6d7 313 // the minimum stack size isn't easily predictable, as the stack
mjr 59:94eb9265b6d7 314 // grows according to what the program does. In addition, the
mjr 59:94eb9265b6d7 315 // mbed platform tools don't give us detailed data on the
mjr 59:94eb9265b6d7 316 // compiler/linker memory map. All we get is a generic total
mjr 59:94eb9265b6d7 317 // RAM requirement, which doesn't necessarily account for all
mjr 59:94eb9265b6d7 318 // overhead (e.g., gaps inserted to get proper alignment for
mjr 59:94eb9265b6d7 319 // particular memory blocks).
mjr 59:94eb9265b6d7 320 //
mjr 59:94eb9265b6d7 321 // A very rough estimate: the total RAM size reported by the
mjr 59:94eb9265b6d7 322 // linker is about 3.5K (currently - that can obviously change
mjr 59:94eb9265b6d7 323 // as the project evolves) out of 16K total. Assuming about a
mjr 59:94eb9265b6d7 324 // 3K stack, that leaves in the ballpark of 10K. Empirically,
mjr 59:94eb9265b6d7 325 // that seems pretty close. In testing, we start to see some
mjr 59:94eb9265b6d7 326 // instability at 10K, while 9K seems safe. To be conservative,
mjr 59:94eb9265b6d7 327 // we'll reduce this to 8K.
mjr 59:94eb9265b6d7 328 //
mjr 59:94eb9265b6d7 329 // Our measured total usage in the base configuration (22 GPIO
mjr 59:94eb9265b6d7 330 // output ports, TSL1410R plunger sensor) is about 4000 bytes.
mjr 59:94eb9265b6d7 331 // A pretty fully decked-out configuration (121 output ports,
mjr 59:94eb9265b6d7 332 // with 8 TLC5940 chips and 3 74HC595 chips, plus the TSL1412R
mjr 59:94eb9265b6d7 333 // sensor with the higher pixel count, and all expansion board
mjr 59:94eb9265b6d7 334 // features enabled) comes to about 6700 bytes. That leaves
mjr 59:94eb9265b6d7 335 // us with about 1.5K free out of our 8K, so we still have a
mjr 59:94eb9265b6d7 336 // little more headroom for future expansion.
mjr 59:94eb9265b6d7 337 //
mjr 59:94eb9265b6d7 338 // For comparison, the standard mbed malloc() runs out of
mjr 59:94eb9265b6d7 339 // memory at about 6K. That's what led to this custom malloc:
mjr 59:94eb9265b6d7 340 // we can just fit the base configuration into that 4K, but
mjr 59:94eb9265b6d7 341 // it's not enough space for more complex setups. There's
mjr 59:94eb9265b6d7 342 // still a little room for squeezing out unnecessary space
mjr 59:94eb9265b6d7 343 // from the mbed library code, but at this point I'd prefer
mjr 59:94eb9265b6d7 344 // to treat that as a last resort, since it would mean having
mjr 59:94eb9265b6d7 345 // to fork private copies of the libraries.
mjr 59:94eb9265b6d7 346 static char pool[8*1024];
mjr 59:94eb9265b6d7 347 static char *nxt = pool;
mjr 59:94eb9265b6d7 348 static size_t rem = sizeof(pool);
mjr 59:94eb9265b6d7 349
mjr 59:94eb9265b6d7 350 // align to a 4-byte increment
mjr 59:94eb9265b6d7 351 siz = (siz + 3) & ~3;
mjr 59:94eb9265b6d7 352
mjr 59:94eb9265b6d7 353 // If insufficient memory is available, halt and show a fast red/purple
mjr 59:94eb9265b6d7 354 // diagnostic flash. We don't want to return, since we assume throughout
mjr 59:94eb9265b6d7 355 // the program that all memory allocations must succeed. Note that this
mjr 59:94eb9265b6d7 356 // is generally considered bad programming practice in applications on
mjr 59:94eb9265b6d7 357 // "real" computers, but for the purposes of this microcontroller app,
mjr 59:94eb9265b6d7 358 // there's no point in checking for failed allocations individually
mjr 59:94eb9265b6d7 359 // because there's no way to recover from them. It's better in this
mjr 59:94eb9265b6d7 360 // context to handle failed allocations as fatal errors centrally. We
mjr 59:94eb9265b6d7 361 // can't recover from these automatically, so we have to resort to user
mjr 59:94eb9265b6d7 362 // intervention, which we signal with the diagnostic LED flashes.
mjr 59:94eb9265b6d7 363 if (siz > rem)
mjr 59:94eb9265b6d7 364 {
mjr 59:94eb9265b6d7 365 // halt with the diagnostic display (by looping forever)
mjr 59:94eb9265b6d7 366 for (;;)
mjr 59:94eb9265b6d7 367 {
mjr 59:94eb9265b6d7 368 diagLED(1, 0, 0);
mjr 59:94eb9265b6d7 369 wait_us(200000);
mjr 59:94eb9265b6d7 370 diagLED(1, 0, 1);
mjr 59:94eb9265b6d7 371 wait_us(200000);
mjr 59:94eb9265b6d7 372 }
mjr 59:94eb9265b6d7 373 }
mjr 48:058ace2aed1d 374
mjr 59:94eb9265b6d7 375 // get the next free location from the pool to return
mjr 59:94eb9265b6d7 376 char *ret = nxt;
mjr 59:94eb9265b6d7 377
mjr 59:94eb9265b6d7 378 // advance the pool pointer and decrement the remaining size counter
mjr 59:94eb9265b6d7 379 nxt += siz;
mjr 59:94eb9265b6d7 380 rem -= siz;
mjr 59:94eb9265b6d7 381
mjr 59:94eb9265b6d7 382 // return the allocated block
mjr 59:94eb9265b6d7 383 return ret;
mjr 48:058ace2aed1d 384 }
mjr 48:058ace2aed1d 385
mjr 59:94eb9265b6d7 386 // Overload operator new to call our custom malloc. This ensures that
mjr 59:94eb9265b6d7 387 // all 'new' allocations throughout the program (including library code)
mjr 59:94eb9265b6d7 388 // go through our private allocator.
mjr 48:058ace2aed1d 389 void *operator new(size_t siz) { return xmalloc(siz); }
mjr 48:058ace2aed1d 390 void *operator new[](size_t siz) { return xmalloc(siz); }
mjr 5:a70c0bce770d 391
mjr 59:94eb9265b6d7 392 // Since we don't do bookkeeping to track released memory, 'delete' does
mjr 59:94eb9265b6d7 393 // nothing. In actual testing, this routine appears to never be called.
mjr 59:94eb9265b6d7 394 // If it *is* ever called, it will simply leave the block in place, which
mjr 59:94eb9265b6d7 395 // will make it unavailable for re-use but will otherwise be harmless.
mjr 59:94eb9265b6d7 396 void operator delete(void *ptr) { }
mjr 59:94eb9265b6d7 397
mjr 59:94eb9265b6d7 398
mjr 5:a70c0bce770d 399 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 400 //
mjr 38:091e511ce8a0 401 // Forward declarations
mjr 38:091e511ce8a0 402 //
mjr 38:091e511ce8a0 403 void setNightMode(bool on);
mjr 38:091e511ce8a0 404 void toggleNightMode();
mjr 38:091e511ce8a0 405
mjr 38:091e511ce8a0 406 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 407 // utilities
mjr 17:ab3cec0c8bf4 408
mjr 26:cb71c4af2912 409 // floating point square of a number
mjr 26:cb71c4af2912 410 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 411
mjr 26:cb71c4af2912 412 // floating point rounding
mjr 26:cb71c4af2912 413 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 414
mjr 17:ab3cec0c8bf4 415
mjr 33:d832bcab089e 416 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 417 //
mjr 40:cc0d9814522b 418 // Extended verison of Timer class. This adds the ability to interrogate
mjr 40:cc0d9814522b 419 // the running state.
mjr 40:cc0d9814522b 420 //
mjr 40:cc0d9814522b 421 class Timer2: public Timer
mjr 40:cc0d9814522b 422 {
mjr 40:cc0d9814522b 423 public:
mjr 40:cc0d9814522b 424 Timer2() : running(false) { }
mjr 40:cc0d9814522b 425
mjr 40:cc0d9814522b 426 void start() { running = true; Timer::start(); }
mjr 40:cc0d9814522b 427 void stop() { running = false; Timer::stop(); }
mjr 40:cc0d9814522b 428
mjr 40:cc0d9814522b 429 bool isRunning() const { return running; }
mjr 40:cc0d9814522b 430
mjr 40:cc0d9814522b 431 private:
mjr 40:cc0d9814522b 432 bool running;
mjr 40:cc0d9814522b 433 };
mjr 40:cc0d9814522b 434
mjr 53:9b2611964afc 435
mjr 53:9b2611964afc 436 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 437 //
mjr 53:9b2611964afc 438 // Reboot timer. When we have a deferred reboot operation pending, we
mjr 53:9b2611964afc 439 // set the target time and start the timer.
mjr 53:9b2611964afc 440 Timer2 rebootTimer;
mjr 53:9b2611964afc 441 long rebootTime_us;
mjr 53:9b2611964afc 442
mjr 40:cc0d9814522b 443 // --------------------------------------------------------------------------
mjr 40:cc0d9814522b 444 //
mjr 33:d832bcab089e 445 // USB product version number
mjr 5:a70c0bce770d 446 //
mjr 47:df7a88cd249c 447 const uint16_t USB_VERSION_NO = 0x000A;
mjr 33:d832bcab089e 448
mjr 33:d832bcab089e 449 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 450 //
mjr 6:cc35eb643e8f 451 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 452 //
mjr 6:cc35eb643e8f 453 #define JOYMAX 4096
mjr 6:cc35eb643e8f 454
mjr 9:fd65b0a94720 455
mjr 17:ab3cec0c8bf4 456 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 457 //
mjr 40:cc0d9814522b 458 // Wire protocol value translations. These translate byte values to and
mjr 40:cc0d9814522b 459 // from the USB protocol to local native format.
mjr 35:e959ffba78fd 460 //
mjr 35:e959ffba78fd 461
mjr 35:e959ffba78fd 462 // unsigned 16-bit integer
mjr 35:e959ffba78fd 463 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 464 {
mjr 35:e959ffba78fd 465 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 466 }
mjr 40:cc0d9814522b 467 inline void ui16Wire(uint8_t *b, uint16_t val)
mjr 40:cc0d9814522b 468 {
mjr 40:cc0d9814522b 469 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 470 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 471 }
mjr 35:e959ffba78fd 472
mjr 35:e959ffba78fd 473 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 474 {
mjr 35:e959ffba78fd 475 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 476 }
mjr 40:cc0d9814522b 477 inline void i16Wire(uint8_t *b, int16_t val)
mjr 40:cc0d9814522b 478 {
mjr 40:cc0d9814522b 479 ui16Wire(b, (uint16_t)val);
mjr 40:cc0d9814522b 480 }
mjr 35:e959ffba78fd 481
mjr 35:e959ffba78fd 482 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 483 {
mjr 35:e959ffba78fd 484 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 485 }
mjr 40:cc0d9814522b 486 inline void ui32Wire(uint8_t *b, uint32_t val)
mjr 40:cc0d9814522b 487 {
mjr 40:cc0d9814522b 488 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 489 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 490 b[2] = (uint8_t)((val >> 16) & 0xff);
mjr 40:cc0d9814522b 491 b[3] = (uint8_t)((val >> 24) & 0xff);
mjr 40:cc0d9814522b 492 }
mjr 35:e959ffba78fd 493
mjr 35:e959ffba78fd 494 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 495 {
mjr 35:e959ffba78fd 496 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 497 }
mjr 35:e959ffba78fd 498
mjr 53:9b2611964afc 499 // Convert "wire" (USB) pin codes to/from PinName values.
mjr 53:9b2611964afc 500 //
mjr 53:9b2611964afc 501 // The internal mbed PinName format is
mjr 53:9b2611964afc 502 //
mjr 53:9b2611964afc 503 // ((port) << PORT_SHIFT) | (pin << 2) // MBED FORMAT
mjr 53:9b2611964afc 504 //
mjr 53:9b2611964afc 505 // where 'port' is 0-4 for Port A to Port E, and 'pin' is
mjr 53:9b2611964afc 506 // 0 to 31. E.g., E31 is (4 << PORT_SHIFT) | (31<<2).
mjr 53:9b2611964afc 507 //
mjr 53:9b2611964afc 508 // We remap this to our more compact wire format where each
mjr 53:9b2611964afc 509 // pin name fits in 8 bits:
mjr 53:9b2611964afc 510 //
mjr 53:9b2611964afc 511 // ((port) << 5) | pin) // WIRE FORMAT
mjr 53:9b2611964afc 512 //
mjr 53:9b2611964afc 513 // E.g., E31 is (4 << 5) | 31.
mjr 53:9b2611964afc 514 //
mjr 53:9b2611964afc 515 // Wire code FF corresponds to PinName NC (not connected).
mjr 53:9b2611964afc 516 //
mjr 53:9b2611964afc 517 inline PinName wirePinName(uint8_t c)
mjr 35:e959ffba78fd 518 {
mjr 53:9b2611964afc 519 if (c == 0xFF)
mjr 53:9b2611964afc 520 return NC; // 0xFF -> NC
mjr 53:9b2611964afc 521 else
mjr 53:9b2611964afc 522 return PinName(
mjr 53:9b2611964afc 523 (int(c & 0xE0) << (PORT_SHIFT - 5)) // top three bits are the port
mjr 53:9b2611964afc 524 | (int(c & 0x1F) << 2)); // bottom five bits are pin
mjr 40:cc0d9814522b 525 }
mjr 40:cc0d9814522b 526 inline void pinNameWire(uint8_t *b, PinName n)
mjr 40:cc0d9814522b 527 {
mjr 53:9b2611964afc 528 *b = PINNAME_TO_WIRE(n);
mjr 35:e959ffba78fd 529 }
mjr 35:e959ffba78fd 530
mjr 35:e959ffba78fd 531
mjr 35:e959ffba78fd 532 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 533 //
mjr 38:091e511ce8a0 534 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 535 //
mjr 38:091e511ce8a0 536 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 537 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 538 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 539 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 540 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 541 // SPI capability.
mjr 38:091e511ce8a0 542 //
mjr 38:091e511ce8a0 543 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 544
mjr 38:091e511ce8a0 545 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 546 // on, and -1 is no change (leaves the current setting intact).
mjr 38:091e511ce8a0 547 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 548 {
mjr 38:091e511ce8a0 549 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 550 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 551 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 552 }
mjr 38:091e511ce8a0 553
mjr 38:091e511ce8a0 554 // check an output port assignment to see if it conflicts with
mjr 38:091e511ce8a0 555 // an on-board LED segment
mjr 38:091e511ce8a0 556 struct LedSeg
mjr 38:091e511ce8a0 557 {
mjr 38:091e511ce8a0 558 bool r, g, b;
mjr 38:091e511ce8a0 559 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 560
mjr 38:091e511ce8a0 561 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 562 {
mjr 38:091e511ce8a0 563 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 564 // our on-board LED segments
mjr 38:091e511ce8a0 565 int t = pc.typ;
mjr 38:091e511ce8a0 566 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 38:091e511ce8a0 567 {
mjr 38:091e511ce8a0 568 // it's a GPIO port - check for a matching pin assignment
mjr 38:091e511ce8a0 569 PinName pin = wirePinName(pc.pin);
mjr 38:091e511ce8a0 570 if (pin == LED1)
mjr 38:091e511ce8a0 571 r = true;
mjr 38:091e511ce8a0 572 else if (pin == LED2)
mjr 38:091e511ce8a0 573 g = true;
mjr 38:091e511ce8a0 574 else if (pin == LED3)
mjr 38:091e511ce8a0 575 b = true;
mjr 38:091e511ce8a0 576 }
mjr 38:091e511ce8a0 577 }
mjr 38:091e511ce8a0 578 };
mjr 38:091e511ce8a0 579
mjr 38:091e511ce8a0 580 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 581 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 582 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 583 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 584 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 585 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 586 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 587 {
mjr 38:091e511ce8a0 588 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 589 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 590 LedSeg l;
mjr 38:091e511ce8a0 591 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 592 l.check(cfg.outPort[i]);
mjr 38:091e511ce8a0 593
mjr 38:091e511ce8a0 594 // We now know which segments are taken for LedWiz use and which
mjr 38:091e511ce8a0 595 // are free. Create diagnostic ports for the ones not claimed for
mjr 38:091e511ce8a0 596 // LedWiz use.
mjr 38:091e511ce8a0 597 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 598 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 599 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 600 }
mjr 38:091e511ce8a0 601
mjr 38:091e511ce8a0 602
mjr 38:091e511ce8a0 603 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 604 //
mjr 29:582472d0bc57 605 // LedWiz emulation, and enhanced TLC5940 output controller
mjr 5:a70c0bce770d 606 //
mjr 26:cb71c4af2912 607 // There are two modes for this feature. The default mode uses the on-board
mjr 26:cb71c4af2912 608 // GPIO ports to implement device outputs - each LedWiz software port is
mjr 26:cb71c4af2912 609 // connected to a physical GPIO pin on the KL25Z. The KL25Z only has 10
mjr 26:cb71c4af2912 610 // PWM channels, so in this mode only 10 LedWiz ports will be dimmable; the
mjr 26:cb71c4af2912 611 // rest are strictly on/off. The KL25Z also has a limited number of GPIO
mjr 26:cb71c4af2912 612 // ports overall - not enough for the full complement of 32 LedWiz ports
mjr 26:cb71c4af2912 613 // and 24 VP joystick inputs, so it's necessary to trade one against the
mjr 26:cb71c4af2912 614 // other if both features are to be used.
mjr 26:cb71c4af2912 615 //
mjr 26:cb71c4af2912 616 // The alternative, enhanced mode uses external TLC5940 PWM controller
mjr 26:cb71c4af2912 617 // chips to control device outputs. In this mode, each LedWiz software
mjr 26:cb71c4af2912 618 // port is mapped to an output on one of the external TLC5940 chips.
mjr 26:cb71c4af2912 619 // Two 5940s is enough for the full set of 32 LedWiz ports, and we can
mjr 26:cb71c4af2912 620 // support even more chips for even more outputs (although doing so requires
mjr 26:cb71c4af2912 621 // breaking LedWiz compatibility, since the LedWiz USB protocol is hardwired
mjr 26:cb71c4af2912 622 // for 32 outputs). Every port in this mode has full PWM support.
mjr 26:cb71c4af2912 623 //
mjr 5:a70c0bce770d 624
mjr 29:582472d0bc57 625
mjr 26:cb71c4af2912 626 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 627 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 628 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 629 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 630 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 631
mjr 26:cb71c4af2912 632 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 633 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 634 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 635 class LwOut
mjr 6:cc35eb643e8f 636 {
mjr 6:cc35eb643e8f 637 public:
mjr 40:cc0d9814522b 638 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 639 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 640 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 641 };
mjr 26:cb71c4af2912 642
mjr 35:e959ffba78fd 643 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 644 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 645 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 646 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 647 // numbering.
mjr 35:e959ffba78fd 648 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 649 {
mjr 33:d832bcab089e 650 public:
mjr 35:e959ffba78fd 651 LwVirtualOut() { }
mjr 40:cc0d9814522b 652 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 653 };
mjr 26:cb71c4af2912 654
mjr 34:6b981a2afab7 655 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 656 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 657 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 658 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 659 {
mjr 34:6b981a2afab7 660 public:
mjr 34:6b981a2afab7 661 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 662 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 663
mjr 34:6b981a2afab7 664 private:
mjr 53:9b2611964afc 665 // underlying physical output
mjr 34:6b981a2afab7 666 LwOut *out;
mjr 34:6b981a2afab7 667 };
mjr 34:6b981a2afab7 668
mjr 53:9b2611964afc 669 // Global ZB Launch Ball state
mjr 53:9b2611964afc 670 bool zbLaunchOn = false;
mjr 53:9b2611964afc 671
mjr 53:9b2611964afc 672 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 673 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 674 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 675 {
mjr 53:9b2611964afc 676 public:
mjr 53:9b2611964afc 677 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 678 virtual void set(uint8_t val)
mjr 53:9b2611964afc 679 {
mjr 53:9b2611964afc 680 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 681 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 682
mjr 53:9b2611964afc 683 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 684 out->set(val);
mjr 53:9b2611964afc 685 }
mjr 53:9b2611964afc 686
mjr 53:9b2611964afc 687 private:
mjr 53:9b2611964afc 688 // underlying physical or virtual output
mjr 53:9b2611964afc 689 LwOut *out;
mjr 53:9b2611964afc 690 };
mjr 53:9b2611964afc 691
mjr 53:9b2611964afc 692
mjr 40:cc0d9814522b 693 // Gamma correction table for 8-bit input values
mjr 40:cc0d9814522b 694 static const uint8_t gamma[] = {
mjr 40:cc0d9814522b 695 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 696 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 697 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 698 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 699 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 700 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 701 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 702 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 703 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 704 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 705 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 706 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 707 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 708 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 709 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 710 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 711 };
mjr 40:cc0d9814522b 712
mjr 40:cc0d9814522b 713 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 714 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 715 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 716 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 717 {
mjr 40:cc0d9814522b 718 public:
mjr 40:cc0d9814522b 719 LwGammaOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 720 virtual void set(uint8_t val) { out->set(gamma[val]); }
mjr 40:cc0d9814522b 721
mjr 40:cc0d9814522b 722 private:
mjr 40:cc0d9814522b 723 LwOut *out;
mjr 40:cc0d9814522b 724 };
mjr 40:cc0d9814522b 725
mjr 53:9b2611964afc 726 // global night mode flag
mjr 53:9b2611964afc 727 static bool nightMode = false;
mjr 53:9b2611964afc 728
mjr 40:cc0d9814522b 729 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 730 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 731 // mode is engaged.
mjr 40:cc0d9814522b 732 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 733 {
mjr 40:cc0d9814522b 734 public:
mjr 40:cc0d9814522b 735 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 736 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 737
mjr 53:9b2611964afc 738 private:
mjr 53:9b2611964afc 739 LwOut *out;
mjr 53:9b2611964afc 740 };
mjr 53:9b2611964afc 741
mjr 53:9b2611964afc 742 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 743 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 744 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 745 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 746 {
mjr 53:9b2611964afc 747 public:
mjr 53:9b2611964afc 748 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 749 virtual void set(uint8_t)
mjr 53:9b2611964afc 750 {
mjr 53:9b2611964afc 751 // ignore the host value and simply show the current
mjr 53:9b2611964afc 752 // night mode setting
mjr 53:9b2611964afc 753 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 754 }
mjr 40:cc0d9814522b 755
mjr 40:cc0d9814522b 756 private:
mjr 40:cc0d9814522b 757 LwOut *out;
mjr 40:cc0d9814522b 758 };
mjr 40:cc0d9814522b 759
mjr 26:cb71c4af2912 760
mjr 35:e959ffba78fd 761 //
mjr 35:e959ffba78fd 762 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 763 // assignments set in config.h.
mjr 33:d832bcab089e 764 //
mjr 35:e959ffba78fd 765 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 766 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 767 {
mjr 35:e959ffba78fd 768 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 769 {
mjr 53:9b2611964afc 770 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 771 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 772 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 773 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 774 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 775 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 776 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 777 }
mjr 35:e959ffba78fd 778 }
mjr 26:cb71c4af2912 779
mjr 40:cc0d9814522b 780 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 781 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 782 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 783 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 784 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 785 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 786 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 787 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 788 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 789 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 790 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 791 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 792 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 793 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 794 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 795 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 796 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 797 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 798 };
mjr 40:cc0d9814522b 799
mjr 40:cc0d9814522b 800 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 801 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 802 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 803 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 804 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 805 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 806 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 807 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 808 // are always 8 bits.
mjr 40:cc0d9814522b 809 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 810 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 811 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 812 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 813 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 814 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 815 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 816 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 817 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 818 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 819 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 820 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 821 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 822 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 823 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 824 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 825 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 826 };
mjr 40:cc0d9814522b 827
mjr 26:cb71c4af2912 828 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 829 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 830 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 831 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 832 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 833 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 834 {
mjr 26:cb71c4af2912 835 public:
mjr 60:f38da020aa13 836 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 837 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 838 {
mjr 26:cb71c4af2912 839 if (val != prv)
mjr 40:cc0d9814522b 840 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 841 }
mjr 60:f38da020aa13 842 uint8_t idx;
mjr 40:cc0d9814522b 843 uint8_t prv;
mjr 26:cb71c4af2912 844 };
mjr 26:cb71c4af2912 845
mjr 40:cc0d9814522b 846 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 847 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 848 {
mjr 40:cc0d9814522b 849 public:
mjr 60:f38da020aa13 850 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 851 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 852 {
mjr 40:cc0d9814522b 853 if (val != prv)
mjr 40:cc0d9814522b 854 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 855 }
mjr 60:f38da020aa13 856 uint8_t idx;
mjr 40:cc0d9814522b 857 uint8_t prv;
mjr 40:cc0d9814522b 858 };
mjr 40:cc0d9814522b 859
mjr 40:cc0d9814522b 860
mjr 33:d832bcab089e 861
mjr 34:6b981a2afab7 862 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 863 // config.h.
mjr 35:e959ffba78fd 864 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 865
mjr 35:e959ffba78fd 866 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 867 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 868 {
mjr 35:e959ffba78fd 869 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 870 {
mjr 53:9b2611964afc 871 hc595 = new HC595(
mjr 53:9b2611964afc 872 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 873 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 874 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 875 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 876 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 877 hc595->init();
mjr 35:e959ffba78fd 878 hc595->update();
mjr 35:e959ffba78fd 879 }
mjr 35:e959ffba78fd 880 }
mjr 34:6b981a2afab7 881
mjr 34:6b981a2afab7 882 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 883 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 884 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 885 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 886 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 887 class Lw595Out: public LwOut
mjr 33:d832bcab089e 888 {
mjr 33:d832bcab089e 889 public:
mjr 60:f38da020aa13 890 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 891 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 892 {
mjr 34:6b981a2afab7 893 if (val != prv)
mjr 40:cc0d9814522b 894 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 895 }
mjr 60:f38da020aa13 896 uint8_t idx;
mjr 40:cc0d9814522b 897 uint8_t prv;
mjr 33:d832bcab089e 898 };
mjr 33:d832bcab089e 899
mjr 26:cb71c4af2912 900
mjr 40:cc0d9814522b 901
mjr 64:ef7ca92dff36 902 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 903 // normalized to 0.0 to 1.0 scale.
mjr 40:cc0d9814522b 904 static const float pwm_level[] = {
mjr 64:ef7ca92dff36 905 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 906 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 907 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 908 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 909 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 910 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 911 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 912 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 913 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 914 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 915 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 916 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 917 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 918 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 919 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 920 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 921 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 922 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 923 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 924 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 925 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 926 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 927 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 928 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 929 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 930 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 931 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 932 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 933 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 934 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 935 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 936 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 937 };
mjr 26:cb71c4af2912 938
mjr 64:ef7ca92dff36 939
mjr 64:ef7ca92dff36 940 // Conversion table for 8-bit DOF level to pulse width in microseconds,
mjr 64:ef7ca92dff36 941 // with gamma correction. We could use the layered gamma output on top
mjr 64:ef7ca92dff36 942 // of the regular LwPwmOut class for this, but we get better precision
mjr 64:ef7ca92dff36 943 // with a dedicated table, because we apply gamma correction to the
mjr 64:ef7ca92dff36 944 // 32-bit microsecond values rather than the 8-bit DOF levels.
mjr 64:ef7ca92dff36 945 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 946 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 947 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 948 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 949 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 950 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 951 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 952 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 953 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 954 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 955 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 956 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 957 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 958 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 959 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 960 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 961 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 962 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 963 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 964 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 965 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 966 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 967 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 968 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 969 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 970 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 971 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 972 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 973 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 974 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 975 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 976 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 977 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 978 };
mjr 64:ef7ca92dff36 979
mjr 64:ef7ca92dff36 980 // LwOut class for a PWM-capable GPIO port. Note that we use FastPWM for
mjr 64:ef7ca92dff36 981 // the underlying port interface. This isn't because we need the "fast"
mjr 64:ef7ca92dff36 982 // part; it's because FastPWM fixes a bug in the base mbed PwmOut class
mjr 64:ef7ca92dff36 983 // that makes it look ugly for fades. The base PwmOut class resets
mjr 64:ef7ca92dff36 984 // the cycle counter when changing the duty cycle, which makes the output
mjr 64:ef7ca92dff36 985 // reset immediately on every change. For an output connected to a lamp
mjr 64:ef7ca92dff36 986 // or LED, this causes obvious flickering when performing a rapid series
mjr 64:ef7ca92dff36 987 // of writes, such as during a fade. The KL25Z TPM hardware is specifically
mjr 64:ef7ca92dff36 988 // designed to make it easy for software to avoid this kind of flickering
mjr 64:ef7ca92dff36 989 // when used correctly: it has an internal staging register for the duty
mjr 64:ef7ca92dff36 990 // cycle register that gets latched at the start of the next cycle, ensuring
mjr 64:ef7ca92dff36 991 // that the duty cycle setting never changes mid-cycle. The mbed PwmOut
mjr 64:ef7ca92dff36 992 // defeats this by resetting the cycle counter on every write, which aborts
mjr 64:ef7ca92dff36 993 // the current cycle at the moment of the write, causing an effectively random
mjr 64:ef7ca92dff36 994 // drop in brightness on each write (by artificially shortening a cycle).
mjr 64:ef7ca92dff36 995 // Fortunately, we can fix this by switching to the API-compatible FastPWM
mjr 64:ef7ca92dff36 996 // class, which does the write right (heh).
mjr 6:cc35eb643e8f 997 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 998 {
mjr 6:cc35eb643e8f 999 public:
mjr 43:7a6364d82a41 1000 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1001 {
mjr 43:7a6364d82a41 1002 prv = initVal ^ 0xFF;
mjr 43:7a6364d82a41 1003 set(initVal);
mjr 43:7a6364d82a41 1004 }
mjr 40:cc0d9814522b 1005 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1006 {
mjr 13:72dda449c3c0 1007 if (val != prv)
mjr 40:cc0d9814522b 1008 p.write(pwm_level[prv = val]);
mjr 13:72dda449c3c0 1009 }
mjr 64:ef7ca92dff36 1010 FastPWM p;
mjr 40:cc0d9814522b 1011 uint8_t prv;
mjr 6:cc35eb643e8f 1012 };
mjr 26:cb71c4af2912 1013
mjr 64:ef7ca92dff36 1014 // Gamma corrected PWM GPIO output
mjr 64:ef7ca92dff36 1015 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1016 {
mjr 64:ef7ca92dff36 1017 public:
mjr 64:ef7ca92dff36 1018 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1019 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1020 {
mjr 64:ef7ca92dff36 1021 }
mjr 64:ef7ca92dff36 1022 virtual void set(uint8_t val)
mjr 64:ef7ca92dff36 1023 {
mjr 64:ef7ca92dff36 1024 if (val != prv)
mjr 64:ef7ca92dff36 1025 p.write(dof_to_gamma_pwm[prv = val]);
mjr 64:ef7ca92dff36 1026 }
mjr 64:ef7ca92dff36 1027 };
mjr 64:ef7ca92dff36 1028
mjr 64:ef7ca92dff36 1029
mjr 26:cb71c4af2912 1030 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1031 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1032 {
mjr 6:cc35eb643e8f 1033 public:
mjr 43:7a6364d82a41 1034 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1035 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1036 {
mjr 13:72dda449c3c0 1037 if (val != prv)
mjr 40:cc0d9814522b 1038 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1039 }
mjr 6:cc35eb643e8f 1040 DigitalOut p;
mjr 40:cc0d9814522b 1041 uint8_t prv;
mjr 6:cc35eb643e8f 1042 };
mjr 26:cb71c4af2912 1043
mjr 29:582472d0bc57 1044 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1045 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1046 // port n (0-based).
mjr 35:e959ffba78fd 1047 //
mjr 35:e959ffba78fd 1048 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1049 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1050 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1051 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1052 // 74HC595 ports).
mjr 33:d832bcab089e 1053 static int numOutputs;
mjr 33:d832bcab089e 1054 static LwOut **lwPin;
mjr 33:d832bcab089e 1055
mjr 38:091e511ce8a0 1056
mjr 35:e959ffba78fd 1057 // Number of LedWiz emulation outputs. This is the number of ports
mjr 35:e959ffba78fd 1058 // accessible through the standard (non-extended) LedWiz protocol
mjr 35:e959ffba78fd 1059 // messages. The protocol has a fixed set of 32 outputs, but we
mjr 35:e959ffba78fd 1060 // might have fewer actual outputs. This is therefore set to the
mjr 35:e959ffba78fd 1061 // lower of 32 or the actual number of outputs.
mjr 35:e959ffba78fd 1062 static int numLwOutputs;
mjr 35:e959ffba78fd 1063
mjr 63:5cd1a5f3a41b 1064 // Current absolute brightness levels for all outputs. These are
mjr 63:5cd1a5f3a41b 1065 // DOF brightness level value, from 0 for fully off to 255 for fully
mjr 63:5cd1a5f3a41b 1066 // on. These are always used for extended ports (33 and above), and
mjr 63:5cd1a5f3a41b 1067 // are used for LedWiz ports (1-32) when we're in extended protocol
mjr 63:5cd1a5f3a41b 1068 // mode (i.e., ledWizMode == false).
mjr 40:cc0d9814522b 1069 static uint8_t *outLevel;
mjr 38:091e511ce8a0 1070
mjr 38:091e511ce8a0 1071 // create a single output pin
mjr 53:9b2611964afc 1072 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1073 {
mjr 38:091e511ce8a0 1074 // get this item's values
mjr 38:091e511ce8a0 1075 int typ = pc.typ;
mjr 38:091e511ce8a0 1076 int pin = pc.pin;
mjr 38:091e511ce8a0 1077 int flags = pc.flags;
mjr 40:cc0d9814522b 1078 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1079 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1080 int gamma = flags & PortFlagGamma;
mjr 38:091e511ce8a0 1081
mjr 38:091e511ce8a0 1082 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1083 LwOut *lwp;
mjr 38:091e511ce8a0 1084 switch (typ)
mjr 38:091e511ce8a0 1085 {
mjr 38:091e511ce8a0 1086 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1087 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1088 if (pin != 0)
mjr 64:ef7ca92dff36 1089 {
mjr 64:ef7ca92dff36 1090 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 1091 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 1092 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 1093 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 1094 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 1095 {
mjr 64:ef7ca92dff36 1096 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 1097 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 1098
mjr 64:ef7ca92dff36 1099 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 1100 gamma = false;
mjr 64:ef7ca92dff36 1101 }
mjr 64:ef7ca92dff36 1102 else
mjr 64:ef7ca92dff36 1103 {
mjr 64:ef7ca92dff36 1104 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 1105 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 1106 }
mjr 64:ef7ca92dff36 1107 }
mjr 48:058ace2aed1d 1108 else
mjr 48:058ace2aed1d 1109 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1110 break;
mjr 38:091e511ce8a0 1111
mjr 38:091e511ce8a0 1112 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1113 // Digital GPIO port
mjr 48:058ace2aed1d 1114 if (pin != 0)
mjr 48:058ace2aed1d 1115 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1116 else
mjr 48:058ace2aed1d 1117 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1118 break;
mjr 38:091e511ce8a0 1119
mjr 38:091e511ce8a0 1120 case PortTypeTLC5940:
mjr 38:091e511ce8a0 1121 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 1122 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 1123 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 1124 {
mjr 40:cc0d9814522b 1125 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 1126 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 1127 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 1128 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 1129 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 1130 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 1131 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 1132 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 1133 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 1134 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 1135 // for this unlikely case.
mjr 40:cc0d9814522b 1136 if (gamma && !activeLow)
mjr 40:cc0d9814522b 1137 {
mjr 40:cc0d9814522b 1138 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 1139 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 1140
mjr 40:cc0d9814522b 1141 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 1142 gamma = false;
mjr 40:cc0d9814522b 1143 }
mjr 40:cc0d9814522b 1144 else
mjr 40:cc0d9814522b 1145 {
mjr 40:cc0d9814522b 1146 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 1147 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 1148 }
mjr 40:cc0d9814522b 1149 }
mjr 38:091e511ce8a0 1150 else
mjr 40:cc0d9814522b 1151 {
mjr 40:cc0d9814522b 1152 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 1153 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 1154 }
mjr 38:091e511ce8a0 1155 break;
mjr 38:091e511ce8a0 1156
mjr 38:091e511ce8a0 1157 case PortType74HC595:
mjr 38:091e511ce8a0 1158 // 74HC595 port (if we don't have an HC595 controller object, or it's not a valid
mjr 38:091e511ce8a0 1159 // output number, create a virtual port)
mjr 38:091e511ce8a0 1160 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 1161 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 1162 else
mjr 38:091e511ce8a0 1163 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1164 break;
mjr 38:091e511ce8a0 1165
mjr 38:091e511ce8a0 1166 case PortTypeVirtual:
mjr 43:7a6364d82a41 1167 case PortTypeDisabled:
mjr 38:091e511ce8a0 1168 default:
mjr 38:091e511ce8a0 1169 // virtual or unknown
mjr 38:091e511ce8a0 1170 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1171 break;
mjr 38:091e511ce8a0 1172 }
mjr 38:091e511ce8a0 1173
mjr 40:cc0d9814522b 1174 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 1175 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 1176 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 1177 if (activeLow)
mjr 38:091e511ce8a0 1178 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 1179
mjr 40:cc0d9814522b 1180 // If it's a noisemaker, layer on a night mode switch. Note that this
mjr 40:cc0d9814522b 1181 // needs to be
mjr 40:cc0d9814522b 1182 if (noisy)
mjr 40:cc0d9814522b 1183 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 1184
mjr 40:cc0d9814522b 1185 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 1186 if (gamma)
mjr 40:cc0d9814522b 1187 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 1188
mjr 53:9b2611964afc 1189 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 1190 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 1191 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 1192 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 1193 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 1194
mjr 53:9b2611964afc 1195 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 1196 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 1197 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 1198
mjr 38:091e511ce8a0 1199 // turn it off initially
mjr 38:091e511ce8a0 1200 lwp->set(0);
mjr 38:091e511ce8a0 1201
mjr 38:091e511ce8a0 1202 // return the pin
mjr 38:091e511ce8a0 1203 return lwp;
mjr 38:091e511ce8a0 1204 }
mjr 38:091e511ce8a0 1205
mjr 6:cc35eb643e8f 1206 // initialize the output pin array
mjr 35:e959ffba78fd 1207 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 1208 {
mjr 35:e959ffba78fd 1209 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 1210 // total number of ports.
mjr 35:e959ffba78fd 1211 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 1212 int i;
mjr 35:e959ffba78fd 1213 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 1214 {
mjr 35:e959ffba78fd 1215 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 1216 {
mjr 35:e959ffba78fd 1217 numOutputs = i;
mjr 34:6b981a2afab7 1218 break;
mjr 34:6b981a2afab7 1219 }
mjr 33:d832bcab089e 1220 }
mjr 33:d832bcab089e 1221
mjr 35:e959ffba78fd 1222 // the real LedWiz protocol can access at most 32 ports, or the
mjr 35:e959ffba78fd 1223 // actual number of outputs, whichever is lower
mjr 35:e959ffba78fd 1224 numLwOutputs = (numOutputs < 32 ? numOutputs : 32);
mjr 35:e959ffba78fd 1225
mjr 33:d832bcab089e 1226 // allocate the pin array
mjr 33:d832bcab089e 1227 lwPin = new LwOut*[numOutputs];
mjr 33:d832bcab089e 1228
mjr 38:091e511ce8a0 1229 // Allocate the current brightness array. For these, allocate at
mjr 38:091e511ce8a0 1230 // least 32, so that we have enough for all LedWiz messages, but
mjr 38:091e511ce8a0 1231 // allocate the full set of actual ports if we have more than the
mjr 38:091e511ce8a0 1232 // LedWiz complement.
mjr 38:091e511ce8a0 1233 int minOuts = numOutputs < 32 ? 32 : numOutputs;
mjr 40:cc0d9814522b 1234 outLevel = new uint8_t[minOuts];
mjr 33:d832bcab089e 1235
mjr 35:e959ffba78fd 1236 // create the pin interface object for each port
mjr 35:e959ffba78fd 1237 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 1238 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 1239 }
mjr 6:cc35eb643e8f 1240
mjr 63:5cd1a5f3a41b 1241 // LedWiz/Extended protocol mode.
mjr 63:5cd1a5f3a41b 1242 //
mjr 63:5cd1a5f3a41b 1243 // We implement output port control using both the legacy LedWiz
mjr 63:5cd1a5f3a41b 1244 // protocol and a private extended protocol (which is 100% backwards
mjr 63:5cd1a5f3a41b 1245 // compatible with the LedWiz protocol: we recognize all valid legacy
mjr 63:5cd1a5f3a41b 1246 // protocol commands and handle them the same way a real LedWiz does).
mjr 63:5cd1a5f3a41b 1247 // The legacy protocol can access the first 32 ports; the extended
mjr 63:5cd1a5f3a41b 1248 // protocol can access all ports, including the first 32 as well as
mjr 63:5cd1a5f3a41b 1249 // the higher numbered ports. This means that the first 32 ports
mjr 63:5cd1a5f3a41b 1250 // can be addressed with either protocol, which muddies the waters
mjr 63:5cd1a5f3a41b 1251 // a bit because of the different approaches the two protocols take.
mjr 63:5cd1a5f3a41b 1252 // The legacy protocol separates the brightness/flash state of an
mjr 63:5cd1a5f3a41b 1253 // output (which it calls the "profile" state) from the on/off state.
mjr 63:5cd1a5f3a41b 1254 // The extended protocol doesn't; "off" is simply represented as
mjr 63:5cd1a5f3a41b 1255 // brightness 0.
mjr 63:5cd1a5f3a41b 1256 //
mjr 63:5cd1a5f3a41b 1257 // To deal with the different approaches, we use this flag to keep
mjr 63:5cd1a5f3a41b 1258 // track of the global protocol state. Each time we get an output
mjr 63:5cd1a5f3a41b 1259 // port command, we switch the protocol state to the protocol that
mjr 63:5cd1a5f3a41b 1260 // was used in the command. On a legacy SBA or PBA, we switch to
mjr 63:5cd1a5f3a41b 1261 // LedWiz mode; on an extended output set message, we switch to
mjr 63:5cd1a5f3a41b 1262 // extended mode. We remember the LedWiz and extended output state
mjr 63:5cd1a5f3a41b 1263 // for each LW ports (1-32) separately. Any time the mode changes,
mjr 63:5cd1a5f3a41b 1264 // we set ports 1-32 back to the state for the new mode.
mjr 63:5cd1a5f3a41b 1265 //
mjr 63:5cd1a5f3a41b 1266 // The reasoning here is that any given client (on the PC) will use
mjr 63:5cd1a5f3a41b 1267 // one mode or the other, and won't mix the two. An older program
mjr 63:5cd1a5f3a41b 1268 // that only knows about the LedWiz protocol will use the legacy
mjr 63:5cd1a5f3a41b 1269 // protocol only, and never send us an extended command. A DOF-based
mjr 63:5cd1a5f3a41b 1270 // program might use one or the other, according to how the user has
mjr 63:5cd1a5f3a41b 1271 // configured DOF. We have to be able to switch seamlessly between
mjr 63:5cd1a5f3a41b 1272 // the protocols to accommodate switching from one type of program
mjr 63:5cd1a5f3a41b 1273 // on the PC to the other, but we shouldn't have to worry about one
mjr 63:5cd1a5f3a41b 1274 // program switching back and forth.
mjr 63:5cd1a5f3a41b 1275 static uint8_t ledWizMode = true;
mjr 63:5cd1a5f3a41b 1276
mjr 29:582472d0bc57 1277 // LedWiz output states.
mjr 29:582472d0bc57 1278 //
mjr 29:582472d0bc57 1279 // The LedWiz protocol has two separate control axes for each output.
mjr 29:582472d0bc57 1280 // One axis is its on/off state; the other is its "profile" state, which
mjr 29:582472d0bc57 1281 // is either a fixed brightness or a blinking pattern for the light.
mjr 29:582472d0bc57 1282 // The two axes are independent.
mjr 29:582472d0bc57 1283 //
mjr 29:582472d0bc57 1284 // Note that the LedWiz protocol can only address 32 outputs, so the
mjr 29:582472d0bc57 1285 // wizOn and wizVal arrays have fixed sizes of 32 elements no matter
mjr 29:582472d0bc57 1286 // how many physical outputs we're using.
mjr 29:582472d0bc57 1287
mjr 0:5acbbe3f4cf4 1288 // on/off state for each LedWiz output
mjr 1:d913e0afb2ac 1289 static uint8_t wizOn[32];
mjr 0:5acbbe3f4cf4 1290
mjr 40:cc0d9814522b 1291 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 40:cc0d9814522b 1292 // for each LedWiz output. If the output was last updated through an
mjr 40:cc0d9814522b 1293 // LedWiz protocol message, it will have one of these values:
mjr 29:582472d0bc57 1294 //
mjr 29:582472d0bc57 1295 // 0-48 = fixed brightness 0% to 100%
mjr 40:cc0d9814522b 1296 // 49 = fixed brightness 100% (equivalent to 48)
mjr 29:582472d0bc57 1297 // 129 = ramp up / ramp down
mjr 29:582472d0bc57 1298 // 130 = flash on / off
mjr 29:582472d0bc57 1299 // 131 = on / ramp down
mjr 29:582472d0bc57 1300 // 132 = ramp up / on
mjr 29:582472d0bc57 1301 //
mjr 40:cc0d9814522b 1302 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 40:cc0d9814522b 1303 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 40:cc0d9814522b 1304 // it, so we need to accept it for compatibility.)
mjr 1:d913e0afb2ac 1305 static uint8_t wizVal[32] = {
mjr 13:72dda449c3c0 1306 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 1307 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 1308 48, 48, 48, 48, 48, 48, 48, 48,
mjr 13:72dda449c3c0 1309 48, 48, 48, 48, 48, 48, 48, 48
mjr 0:5acbbe3f4cf4 1310 };
mjr 0:5acbbe3f4cf4 1311
mjr 29:582472d0bc57 1312 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 29:582472d0bc57 1313 // rate for lights in blinking states.
mjr 29:582472d0bc57 1314 static uint8_t wizSpeed = 2;
mjr 29:582472d0bc57 1315
mjr 40:cc0d9814522b 1316 // Current LedWiz flash cycle counter. This runs from 0 to 255
mjr 40:cc0d9814522b 1317 // during each cycle.
mjr 29:582472d0bc57 1318 static uint8_t wizFlashCounter = 0;
mjr 29:582472d0bc57 1319
mjr 40:cc0d9814522b 1320 // translate an LedWiz brightness level (0-49) to a DOF brightness
mjr 40:cc0d9814522b 1321 // level (0-255)
mjr 40:cc0d9814522b 1322 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 1323 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 1324 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 1325 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 1326 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 1327 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 1328 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 1329 255, 255
mjr 40:cc0d9814522b 1330 };
mjr 40:cc0d9814522b 1331
mjr 40:cc0d9814522b 1332 // Translate an LedWiz output (ports 1-32) to a DOF brightness level.
mjr 40:cc0d9814522b 1333 static uint8_t wizState(int idx)
mjr 0:5acbbe3f4cf4 1334 {
mjr 63:5cd1a5f3a41b 1335 // If we're in extended protocol mode, ignore the LedWiz setting
mjr 63:5cd1a5f3a41b 1336 // for the port and use the new protocol setting instead.
mjr 63:5cd1a5f3a41b 1337 if (!ledWizMode)
mjr 29:582472d0bc57 1338 return outLevel[idx];
mjr 29:582472d0bc57 1339
mjr 29:582472d0bc57 1340 // if it's off, show at zero intensity
mjr 29:582472d0bc57 1341 if (!wizOn[idx])
mjr 29:582472d0bc57 1342 return 0;
mjr 29:582472d0bc57 1343
mjr 29:582472d0bc57 1344 // check the state
mjr 29:582472d0bc57 1345 uint8_t val = wizVal[idx];
mjr 40:cc0d9814522b 1346 if (val <= 49)
mjr 29:582472d0bc57 1347 {
mjr 29:582472d0bc57 1348 // PWM brightness/intensity level. Rescale from the LedWiz
mjr 29:582472d0bc57 1349 // 0..48 integer range to our internal PwmOut 0..1 float range.
mjr 29:582472d0bc57 1350 // Note that on the actual LedWiz, level 48 is actually about
mjr 29:582472d0bc57 1351 // 98% on - contrary to the LedWiz documentation, level 49 is
mjr 29:582472d0bc57 1352 // the true 100% level. (In the documentation, level 49 is
mjr 29:582472d0bc57 1353 // simply not a valid setting.) Even so, we treat level 48 as
mjr 29:582472d0bc57 1354 // 100% on to match the documentation. This won't be perfectly
mjr 29:582472d0bc57 1355 // ocmpatible with the actual LedWiz, but it makes for such a
mjr 29:582472d0bc57 1356 // small difference in brightness (if the output device is an
mjr 29:582472d0bc57 1357 // LED, say) that no one should notice. It seems better to
mjr 29:582472d0bc57 1358 // err in this direction, because while the difference in
mjr 29:582472d0bc57 1359 // brightness when attached to an LED won't be noticeable, the
mjr 29:582472d0bc57 1360 // difference in duty cycle when attached to something like a
mjr 29:582472d0bc57 1361 // contactor *can* be noticeable - anything less than 100%
mjr 29:582472d0bc57 1362 // can cause a contactor or relay to chatter. There's almost
mjr 29:582472d0bc57 1363 // never a situation where you'd want values other than 0% and
mjr 29:582472d0bc57 1364 // 100% for a contactor or relay, so treating level 48 as 100%
mjr 29:582472d0bc57 1365 // makes us work properly with software that's expecting the
mjr 29:582472d0bc57 1366 // documented LedWiz behavior and therefore uses level 48 to
mjr 29:582472d0bc57 1367 // turn a contactor or relay fully on.
mjr 40:cc0d9814522b 1368 //
mjr 40:cc0d9814522b 1369 // Note that value 49 is undefined in the LedWiz documentation,
mjr 40:cc0d9814522b 1370 // but real LedWiz units treat it as 100%, equivalent to 48.
mjr 40:cc0d9814522b 1371 // Some software on the PC side uses this, so we need to treat
mjr 40:cc0d9814522b 1372 // it the same way for compatibility.
mjr 40:cc0d9814522b 1373 return lw_to_dof[val];
mjr 29:582472d0bc57 1374 }
mjr 29:582472d0bc57 1375 else if (val == 129)
mjr 29:582472d0bc57 1376 {
mjr 40:cc0d9814522b 1377 // 129 = ramp up / ramp down
mjr 30:6e9902f06f48 1378 return wizFlashCounter < 128
mjr 40:cc0d9814522b 1379 ? wizFlashCounter*2 + 1
mjr 40:cc0d9814522b 1380 : (255 - wizFlashCounter)*2;
mjr 29:582472d0bc57 1381 }
mjr 29:582472d0bc57 1382 else if (val == 130)
mjr 29:582472d0bc57 1383 {
mjr 40:cc0d9814522b 1384 // 130 = flash on / off
mjr 40:cc0d9814522b 1385 return wizFlashCounter < 128 ? 255 : 0;
mjr 29:582472d0bc57 1386 }
mjr 29:582472d0bc57 1387 else if (val == 131)
mjr 29:582472d0bc57 1388 {
mjr 40:cc0d9814522b 1389 // 131 = on / ramp down
mjr 40:cc0d9814522b 1390 return wizFlashCounter < 128 ? 255 : (255 - wizFlashCounter)*2;
mjr 0:5acbbe3f4cf4 1391 }
mjr 29:582472d0bc57 1392 else if (val == 132)
mjr 29:582472d0bc57 1393 {
mjr 40:cc0d9814522b 1394 // 132 = ramp up / on
mjr 40:cc0d9814522b 1395 return wizFlashCounter < 128 ? wizFlashCounter*2 : 255;
mjr 29:582472d0bc57 1396 }
mjr 29:582472d0bc57 1397 else
mjr 13:72dda449c3c0 1398 {
mjr 29:582472d0bc57 1399 // Other values are undefined in the LedWiz documentation. Hosts
mjr 29:582472d0bc57 1400 // *should* never send undefined values, since whatever behavior an
mjr 29:582472d0bc57 1401 // LedWiz unit exhibits in response is accidental and could change
mjr 29:582472d0bc57 1402 // in a future version. We'll treat all undefined values as equivalent
mjr 29:582472d0bc57 1403 // to 48 (fully on).
mjr 40:cc0d9814522b 1404 return 255;
mjr 0:5acbbe3f4cf4 1405 }
mjr 0:5acbbe3f4cf4 1406 }
mjr 0:5acbbe3f4cf4 1407
mjr 29:582472d0bc57 1408 // LedWiz flash timer pulse. This fires periodically to update
mjr 29:582472d0bc57 1409 // LedWiz flashing outputs. At the slowest pulse speed set via
mjr 29:582472d0bc57 1410 // the SBA command, each waveform cycle has 256 steps, so we
mjr 29:582472d0bc57 1411 // choose the pulse time base so that the slowest cycle completes
mjr 29:582472d0bc57 1412 // in 2 seconds. This seems to roughly match the real LedWiz
mjr 29:582472d0bc57 1413 // behavior. We run the pulse timer at the same rate regardless
mjr 29:582472d0bc57 1414 // of the pulse speed; at higher pulse speeds, we simply use
mjr 29:582472d0bc57 1415 // larger steps through the cycle on each interrupt. Running
mjr 29:582472d0bc57 1416 // every 1/127 of a second = 8ms seems to be a pretty light load.
mjr 29:582472d0bc57 1417 Timeout wizPulseTimer;
mjr 38:091e511ce8a0 1418 #define WIZ_PULSE_TIME_BASE (1.0f/127.0f)
mjr 29:582472d0bc57 1419 static void wizPulse()
mjr 29:582472d0bc57 1420 {
mjr 29:582472d0bc57 1421 // increase the counter by the speed increment, and wrap at 256
mjr 29:582472d0bc57 1422 wizFlashCounter += wizSpeed;
mjr 29:582472d0bc57 1423 wizFlashCounter &= 0xff;
mjr 29:582472d0bc57 1424
mjr 29:582472d0bc57 1425 // if we have any flashing lights, update them
mjr 29:582472d0bc57 1426 int ena = false;
mjr 35:e959ffba78fd 1427 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 29:582472d0bc57 1428 {
mjr 29:582472d0bc57 1429 if (wizOn[i])
mjr 29:582472d0bc57 1430 {
mjr 29:582472d0bc57 1431 uint8_t s = wizVal[i];
mjr 29:582472d0bc57 1432 if (s >= 129 && s <= 132)
mjr 29:582472d0bc57 1433 {
mjr 40:cc0d9814522b 1434 lwPin[i]->set(wizState(i));
mjr 29:582472d0bc57 1435 ena = true;
mjr 29:582472d0bc57 1436 }
mjr 29:582472d0bc57 1437 }
mjr 29:582472d0bc57 1438 }
mjr 29:582472d0bc57 1439
mjr 29:582472d0bc57 1440 // Set up the next timer pulse only if we found anything flashing.
mjr 29:582472d0bc57 1441 // To minimize overhead from this feature, we only enable the interrupt
mjr 29:582472d0bc57 1442 // when we need it. This eliminates any performance penalty to other
mjr 29:582472d0bc57 1443 // features when the host software doesn't care about the flashing
mjr 29:582472d0bc57 1444 // modes. For example, DOF never uses these modes, so there's no
mjr 29:582472d0bc57 1445 // need for them when running Visual Pinball.
mjr 29:582472d0bc57 1446 if (ena)
mjr 29:582472d0bc57 1447 wizPulseTimer.attach(wizPulse, WIZ_PULSE_TIME_BASE);
mjr 29:582472d0bc57 1448 }
mjr 29:582472d0bc57 1449
mjr 29:582472d0bc57 1450 // Update the physical outputs connected to the LedWiz ports. This is
mjr 29:582472d0bc57 1451 // called after any update from an LedWiz protocol message.
mjr 1:d913e0afb2ac 1452 static void updateWizOuts()
mjr 1:d913e0afb2ac 1453 {
mjr 29:582472d0bc57 1454 // update each output
mjr 29:582472d0bc57 1455 int pulse = false;
mjr 35:e959ffba78fd 1456 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 29:582472d0bc57 1457 {
mjr 29:582472d0bc57 1458 pulse |= (wizVal[i] >= 129 && wizVal[i] <= 132);
mjr 40:cc0d9814522b 1459 lwPin[i]->set(wizState(i));
mjr 29:582472d0bc57 1460 }
mjr 29:582472d0bc57 1461
mjr 29:582472d0bc57 1462 // if any outputs are set to flashing mode, and the pulse timer
mjr 29:582472d0bc57 1463 // isn't running, turn it on
mjr 29:582472d0bc57 1464 if (pulse)
mjr 29:582472d0bc57 1465 wizPulseTimer.attach(wizPulse, WIZ_PULSE_TIME_BASE);
mjr 34:6b981a2afab7 1466
mjr 34:6b981a2afab7 1467 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 1468 if (hc595 != 0)
mjr 35:e959ffba78fd 1469 hc595->update();
mjr 1:d913e0afb2ac 1470 }
mjr 38:091e511ce8a0 1471
mjr 38:091e511ce8a0 1472 // Update all physical outputs. This is called after a change to a global
mjr 38:091e511ce8a0 1473 // setting that affects all outputs, such as engaging or canceling Night Mode.
mjr 38:091e511ce8a0 1474 static void updateAllOuts()
mjr 38:091e511ce8a0 1475 {
mjr 38:091e511ce8a0 1476 // uddate each LedWiz output
mjr 38:091e511ce8a0 1477 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 40:cc0d9814522b 1478 lwPin[i]->set(wizState(i));
mjr 34:6b981a2afab7 1479
mjr 38:091e511ce8a0 1480 // update each extended output
mjr 63:5cd1a5f3a41b 1481 for (int i = numLwOutputs ; i < numOutputs ; ++i)
mjr 40:cc0d9814522b 1482 lwPin[i]->set(outLevel[i]);
mjr 38:091e511ce8a0 1483
mjr 38:091e511ce8a0 1484 // flush 74HC595 changes, if necessary
mjr 38:091e511ce8a0 1485 if (hc595 != 0)
mjr 38:091e511ce8a0 1486 hc595->update();
mjr 38:091e511ce8a0 1487 }
mjr 38:091e511ce8a0 1488
mjr 11:bd9da7088e6e 1489 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 1490 //
mjr 11:bd9da7088e6e 1491 // Button input
mjr 11:bd9da7088e6e 1492 //
mjr 11:bd9da7088e6e 1493
mjr 18:5e890ebd0023 1494 // button state
mjr 18:5e890ebd0023 1495 struct ButtonState
mjr 18:5e890ebd0023 1496 {
mjr 38:091e511ce8a0 1497 ButtonState()
mjr 38:091e511ce8a0 1498 {
mjr 38:091e511ce8a0 1499 di = NULL;
mjr 53:9b2611964afc 1500 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 1501 virtState = 0;
mjr 53:9b2611964afc 1502 dbState = 0;
mjr 38:091e511ce8a0 1503 pulseState = 0;
mjr 53:9b2611964afc 1504 pulseTime = 0;
mjr 38:091e511ce8a0 1505 }
mjr 35:e959ffba78fd 1506
mjr 53:9b2611964afc 1507 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 1508 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 1509 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 1510 //
mjr 53:9b2611964afc 1511 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 1512 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 1513 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 1514 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 1515 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 1516 void virtPress(bool on)
mjr 53:9b2611964afc 1517 {
mjr 53:9b2611964afc 1518 // Increment or decrement the current state
mjr 53:9b2611964afc 1519 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 1520 }
mjr 53:9b2611964afc 1521
mjr 53:9b2611964afc 1522 // DigitalIn for the button, if connected to a physical input
mjr 48:058ace2aed1d 1523 TinyDigitalIn *di;
mjr 38:091e511ce8a0 1524
mjr 65:739875521aae 1525 // Time of last pulse state transition.
mjr 65:739875521aae 1526 //
mjr 65:739875521aae 1527 // Each state change sticks for a minimum period; when the timer expires,
mjr 65:739875521aae 1528 // if the underlying physical switch is in a different state, we switch
mjr 65:739875521aae 1529 // to the next state and restart the timer. pulseTime is the time remaining
mjr 65:739875521aae 1530 // remaining before we can make another state transition, in microseconds.
mjr 65:739875521aae 1531 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 65:739875521aae 1532 // this guarantees that the parity of the pulse count always matches the
mjr 65:739875521aae 1533 // current physical switch state when the latter is stable, which makes
mjr 65:739875521aae 1534 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 65:739875521aae 1535 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 65:739875521aae 1536 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 65:739875521aae 1537 // This software system can't be fooled that way.)
mjr 65:739875521aae 1538 uint32_t pulseTime;
mjr 18:5e890ebd0023 1539
mjr 65:739875521aae 1540 // Config key index. This points to the ButtonCfg structure in the
mjr 65:739875521aae 1541 // configuration that contains the PC key mapping for the button.
mjr 65:739875521aae 1542 uint8_t cfgIndex;
mjr 53:9b2611964afc 1543
mjr 53:9b2611964afc 1544 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 1545 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 1546 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 1547 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 1548 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 1549 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 1550 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 1551 // and physical source states.
mjr 53:9b2611964afc 1552 uint8_t virtState;
mjr 38:091e511ce8a0 1553
mjr 38:091e511ce8a0 1554 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 1555 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 1556 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 1557 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 1558 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 1559 uint8_t dbState;
mjr 38:091e511ce8a0 1560
mjr 65:739875521aae 1561 // current PHYSICAL on/off state, after debouncing
mjr 65:739875521aae 1562 uint8_t physState : 1;
mjr 65:739875521aae 1563
mjr 65:739875521aae 1564 // current LOGICAL on/off state as reported to the host.
mjr 65:739875521aae 1565 uint8_t logState : 1;
mjr 65:739875521aae 1566
mjr 65:739875521aae 1567 // previous logical on/off state, when keys were last processed for USB
mjr 65:739875521aae 1568 // reports and local effects
mjr 65:739875521aae 1569 uint8_t prevLogState : 1;
mjr 65:739875521aae 1570
mjr 65:739875521aae 1571 // Pulse state
mjr 65:739875521aae 1572 //
mjr 65:739875521aae 1573 // A button in pulse mode (selected via the config flags for the button)
mjr 65:739875521aae 1574 // transmits a brief logical button press and release each time the attached
mjr 65:739875521aae 1575 // physical switch changes state. This is useful for cases where the host
mjr 65:739875521aae 1576 // expects a key press for each change in the state of the physical switch.
mjr 65:739875521aae 1577 // The canonical example is the Coin Door switch in VPinMAME, which requires
mjr 65:739875521aae 1578 // pressing the END key to toggle the open/closed state. This software design
mjr 65:739875521aae 1579 // isn't easily implemented in a physical coin door, though; the simplest
mjr 65:739875521aae 1580 // physical sensor for the coin door state is a switch that's on when the
mjr 65:739875521aae 1581 // door is open and off when the door is closed (or vice versa, but in either
mjr 65:739875521aae 1582 // case, the switch state corresponds to the current state of the door at any
mjr 65:739875521aae 1583 // given time, rather than pulsing on state changes). The "pulse mode"
mjr 65:739875521aae 1584 // option brdiges this gap by generating a toggle key event each time
mjr 65:739875521aae 1585 // there's a change to the physical switch's state.
mjr 38:091e511ce8a0 1586 //
mjr 38:091e511ce8a0 1587 // Pulse state:
mjr 38:091e511ce8a0 1588 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 1589 // 1 -> off
mjr 38:091e511ce8a0 1590 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 1591 // 3 -> on
mjr 38:091e511ce8a0 1592 // 4 -> transitioning on-off
mjr 65:739875521aae 1593 uint8_t pulseState : 3; // 5 states -> we need 3 bits
mjr 65:739875521aae 1594
mjr 65:739875521aae 1595 } __attribute__((packed));
mjr 65:739875521aae 1596
mjr 65:739875521aae 1597 ButtonState *buttonState; // live button slots, allocated on startup
mjr 65:739875521aae 1598 int8_t nButtons; // number of live button slots allocated
mjr 65:739875521aae 1599 int8_t zblButtonIndex = -1; // index of ZB Launch button slot; -1 if unused
mjr 18:5e890ebd0023 1600
mjr 66:2e3583fbd2f4 1601 // Shift button state
mjr 66:2e3583fbd2f4 1602 struct
mjr 66:2e3583fbd2f4 1603 {
mjr 66:2e3583fbd2f4 1604 int8_t index; // buttonState[] index of shift button; -1 if none
mjr 66:2e3583fbd2f4 1605 uint8_t state : 2; // current shift state:
mjr 66:2e3583fbd2f4 1606 // 0 = not shifted
mjr 66:2e3583fbd2f4 1607 // 1 = shift button down, no key pressed yet
mjr 66:2e3583fbd2f4 1608 // 2 = shift button down, key pressed
mjr 66:2e3583fbd2f4 1609 uint8_t pulse : 1; // sending pulsed keystroke on release
mjr 66:2e3583fbd2f4 1610 uint32_t pulseTime; // time of start of pulsed keystroke
mjr 66:2e3583fbd2f4 1611 }
mjr 66:2e3583fbd2f4 1612 __attribute__((packed)) shiftButton;
mjr 38:091e511ce8a0 1613
mjr 38:091e511ce8a0 1614 // Button data
mjr 38:091e511ce8a0 1615 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 1616
mjr 38:091e511ce8a0 1617 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 1618 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 1619 // modifier keys.
mjr 38:091e511ce8a0 1620 struct
mjr 38:091e511ce8a0 1621 {
mjr 38:091e511ce8a0 1622 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 1623 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 1624 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 1625 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 1626 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 1627
mjr 38:091e511ce8a0 1628 // Media key state
mjr 38:091e511ce8a0 1629 struct
mjr 38:091e511ce8a0 1630 {
mjr 38:091e511ce8a0 1631 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 1632 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 1633 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 1634
mjr 38:091e511ce8a0 1635 // button scan interrupt ticker
mjr 38:091e511ce8a0 1636 Ticker buttonTicker;
mjr 38:091e511ce8a0 1637
mjr 38:091e511ce8a0 1638 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 1639 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 1640 void scanButtons()
mjr 38:091e511ce8a0 1641 {
mjr 38:091e511ce8a0 1642 // scan all button input pins
mjr 38:091e511ce8a0 1643 ButtonState *bs = buttonState;
mjr 65:739875521aae 1644 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 38:091e511ce8a0 1645 {
mjr 53:9b2611964afc 1646 // if this logical button is connected to a physical input, check
mjr 53:9b2611964afc 1647 // the GPIO pin state
mjr 38:091e511ce8a0 1648 if (bs->di != NULL)
mjr 38:091e511ce8a0 1649 {
mjr 38:091e511ce8a0 1650 // Shift the new state into the debounce history. Note that
mjr 38:091e511ce8a0 1651 // the physical pin inputs are active low (0V/GND = ON), so invert
mjr 38:091e511ce8a0 1652 // the reading by XOR'ing the low bit with 1. And of course we
mjr 38:091e511ce8a0 1653 // only want the low bit (since the history is effectively a bit
mjr 38:091e511ce8a0 1654 // vector), so mask the whole thing with 0x01 as well.
mjr 53:9b2611964afc 1655 uint8_t db = bs->dbState;
mjr 38:091e511ce8a0 1656 db <<= 1;
mjr 38:091e511ce8a0 1657 db |= (bs->di->read() & 0x01) ^ 0x01;
mjr 53:9b2611964afc 1658 bs->dbState = db;
mjr 38:091e511ce8a0 1659
mjr 38:091e511ce8a0 1660 // if we have all 0's or 1's in the history for the required
mjr 38:091e511ce8a0 1661 // debounce period, the key state is stable - check for a change
mjr 38:091e511ce8a0 1662 // to the last stable state
mjr 38:091e511ce8a0 1663 const uint8_t stable = 0x1F; // 00011111b -> 5 stable readings
mjr 38:091e511ce8a0 1664 db &= stable;
mjr 38:091e511ce8a0 1665 if (db == 0 || db == stable)
mjr 53:9b2611964afc 1666 bs->physState = db & 1;
mjr 38:091e511ce8a0 1667 }
mjr 38:091e511ce8a0 1668 }
mjr 38:091e511ce8a0 1669 }
mjr 38:091e511ce8a0 1670
mjr 38:091e511ce8a0 1671 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 1672 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 1673 // in the physical button state.
mjr 38:091e511ce8a0 1674 Timer buttonTimer;
mjr 12:669df364a565 1675
mjr 65:739875521aae 1676 // Count a button during the initial setup scan
mjr 65:739875521aae 1677 void countButton(uint8_t typ, bool &kbKeys)
mjr 65:739875521aae 1678 {
mjr 65:739875521aae 1679 // count it
mjr 65:739875521aae 1680 ++nButtons;
mjr 65:739875521aae 1681
mjr 67:c39e66c4e000 1682 // if it's a keyboard key or media key, note that we need a USB
mjr 67:c39e66c4e000 1683 // keyboard interface
mjr 67:c39e66c4e000 1684 if (typ == BtnTypeKey || typ == BtnTypeMedia)
mjr 65:739875521aae 1685 kbKeys = true;
mjr 65:739875521aae 1686 }
mjr 65:739875521aae 1687
mjr 11:bd9da7088e6e 1688 // initialize the button inputs
mjr 35:e959ffba78fd 1689 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 1690 {
mjr 35:e959ffba78fd 1691 // presume we'll find no keyboard keys
mjr 35:e959ffba78fd 1692 kbKeys = false;
mjr 35:e959ffba78fd 1693
mjr 66:2e3583fbd2f4 1694 // presume no shift key
mjr 66:2e3583fbd2f4 1695 shiftButton.index = -1;
mjr 66:2e3583fbd2f4 1696
mjr 65:739875521aae 1697 // Count up how many button slots we'll need to allocate. Start
mjr 65:739875521aae 1698 // with assigned buttons from the configuration, noting that we
mjr 65:739875521aae 1699 // only need to create slots for buttons that are actually wired.
mjr 65:739875521aae 1700 nButtons = 0;
mjr 65:739875521aae 1701 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 1702 {
mjr 65:739875521aae 1703 // it's valid if it's wired to a real input pin
mjr 65:739875521aae 1704 if (wirePinName(cfg.button[i].pin) != NC)
mjr 65:739875521aae 1705 countButton(cfg.button[i].typ, kbKeys);
mjr 65:739875521aae 1706 }
mjr 65:739875521aae 1707
mjr 65:739875521aae 1708 // Count virtual buttons
mjr 65:739875521aae 1709
mjr 65:739875521aae 1710 // ZB Launch
mjr 65:739875521aae 1711 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 65:739875521aae 1712 {
mjr 65:739875521aae 1713 // valid - remember the live button index
mjr 65:739875521aae 1714 zblButtonIndex = nButtons;
mjr 65:739875521aae 1715
mjr 65:739875521aae 1716 // count it
mjr 65:739875521aae 1717 countButton(cfg.plunger.zbLaunchBall.keytype, kbKeys);
mjr 65:739875521aae 1718 }
mjr 65:739875521aae 1719
mjr 65:739875521aae 1720 // Allocate the live button slots
mjr 65:739875521aae 1721 ButtonState *bs = buttonState = new ButtonState[nButtons];
mjr 65:739875521aae 1722
mjr 65:739875521aae 1723 // Configure the physical inputs
mjr 65:739875521aae 1724 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 1725 {
mjr 65:739875521aae 1726 PinName pin = wirePinName(cfg.button[i].pin);
mjr 65:739875521aae 1727 if (pin != NC)
mjr 65:739875521aae 1728 {
mjr 65:739875521aae 1729 // point back to the config slot for the keyboard data
mjr 65:739875521aae 1730 bs->cfgIndex = i;
mjr 65:739875521aae 1731
mjr 65:739875521aae 1732 // set up the GPIO input pin for this button
mjr 65:739875521aae 1733 bs->di = new TinyDigitalIn(pin);
mjr 65:739875521aae 1734
mjr 65:739875521aae 1735 // if it's a pulse mode button, set the initial pulse state to Off
mjr 65:739875521aae 1736 if (cfg.button[i].flags & BtnFlagPulse)
mjr 65:739875521aae 1737 bs->pulseState = 1;
mjr 65:739875521aae 1738
mjr 66:2e3583fbd2f4 1739 // If this is the shift button, note its buttonState[] index.
mjr 66:2e3583fbd2f4 1740 // We have to figure the buttonState[] index separately from
mjr 66:2e3583fbd2f4 1741 // the config index, because the indices can differ if some
mjr 66:2e3583fbd2f4 1742 // config slots are left unused.
mjr 66:2e3583fbd2f4 1743 if (cfg.shiftButton == i+1)
mjr 66:2e3583fbd2f4 1744 shiftButton.index = bs - buttonState;
mjr 66:2e3583fbd2f4 1745
mjr 65:739875521aae 1746 // advance to the next button
mjr 65:739875521aae 1747 ++bs;
mjr 65:739875521aae 1748 }
mjr 65:739875521aae 1749 }
mjr 65:739875521aae 1750
mjr 53:9b2611964afc 1751 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 1752 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 1753 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 1754 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 1755
mjr 53:9b2611964afc 1756 // ZB Launch Ball button
mjr 65:739875521aae 1757 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 11:bd9da7088e6e 1758 {
mjr 65:739875521aae 1759 // Point back to the config slot for the keyboard data.
mjr 66:2e3583fbd2f4 1760 // We use a special extra slot for virtual buttons,
mjr 66:2e3583fbd2f4 1761 // so we also need to set up the slot data by copying
mjr 66:2e3583fbd2f4 1762 // the ZBL config data to our virtual button slot.
mjr 65:739875521aae 1763 bs->cfgIndex = ZBL_BUTTON_CFG;
mjr 65:739875521aae 1764 cfg.button[ZBL_BUTTON_CFG].pin = PINNAME_TO_WIRE(NC);
mjr 65:739875521aae 1765 cfg.button[ZBL_BUTTON_CFG].typ = cfg.plunger.zbLaunchBall.keytype;
mjr 65:739875521aae 1766 cfg.button[ZBL_BUTTON_CFG].val = cfg.plunger.zbLaunchBall.keycode;
mjr 65:739875521aae 1767
mjr 66:2e3583fbd2f4 1768 // advance to the next button
mjr 65:739875521aae 1769 ++bs;
mjr 11:bd9da7088e6e 1770 }
mjr 12:669df364a565 1771
mjr 38:091e511ce8a0 1772 // start the button scan thread
mjr 38:091e511ce8a0 1773 buttonTicker.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 1774
mjr 38:091e511ce8a0 1775 // start the button state transition timer
mjr 12:669df364a565 1776 buttonTimer.start();
mjr 11:bd9da7088e6e 1777 }
mjr 11:bd9da7088e6e 1778
mjr 67:c39e66c4e000 1779 // Media key mapping. This maps from an 8-bit USB media key
mjr 67:c39e66c4e000 1780 // code to the corresponding bit in our USB report descriptor.
mjr 67:c39e66c4e000 1781 // The USB key code is the index, and the value at the index
mjr 67:c39e66c4e000 1782 // is the report descriptor bit. See joystick.cpp for the
mjr 67:c39e66c4e000 1783 // media descriptor details. Our currently mapped keys are:
mjr 67:c39e66c4e000 1784 //
mjr 67:c39e66c4e000 1785 // 0xE2 -> Mute -> 0x01
mjr 67:c39e66c4e000 1786 // 0xE9 -> Volume Up -> 0x02
mjr 67:c39e66c4e000 1787 // 0xEA -> Volume Down -> 0x04
mjr 67:c39e66c4e000 1788 // 0xB5 -> Next Track -> 0x08
mjr 67:c39e66c4e000 1789 // 0xB6 -> Previous Track -> 0x10
mjr 67:c39e66c4e000 1790 // 0xB7 -> Stop -> 0x20
mjr 67:c39e66c4e000 1791 // 0xCD -> Play / Pause -> 0x40
mjr 67:c39e66c4e000 1792 //
mjr 67:c39e66c4e000 1793 static const uint8_t mediaKeyMap[] = {
mjr 67:c39e66c4e000 1794 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-0F
mjr 67:c39e66c4e000 1795 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-1F
mjr 67:c39e66c4e000 1796 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-2F
mjr 67:c39e66c4e000 1797 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-3F
mjr 67:c39e66c4e000 1798 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-4F
mjr 67:c39e66c4e000 1799 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-5F
mjr 67:c39e66c4e000 1800 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-6F
mjr 67:c39e66c4e000 1801 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-7F
mjr 67:c39e66c4e000 1802 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
mjr 67:c39e66c4e000 1803 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-9F
mjr 67:c39e66c4e000 1804 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0-AF
mjr 67:c39e66c4e000 1805 0, 0, 0, 0, 0, 8, 16, 32, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
mjr 67:c39e66c4e000 1806 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, // C0-CF
mjr 67:c39e66c4e000 1807 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D0-DF
mjr 67:c39e66c4e000 1808 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, // E0-EF
mjr 67:c39e66c4e000 1809 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-FF
mjr 67:c39e66c4e000 1810 };
mjr 67:c39e66c4e000 1811
mjr 67:c39e66c4e000 1812
mjr 38:091e511ce8a0 1813 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 1814 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 1815 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 1816 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 1817 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 1818 {
mjr 35:e959ffba78fd 1819 // start with an empty list of USB key codes
mjr 35:e959ffba78fd 1820 uint8_t modkeys = 0;
mjr 35:e959ffba78fd 1821 uint8_t keys[7] = { 0, 0, 0, 0, 0, 0, 0 };
mjr 35:e959ffba78fd 1822 int nkeys = 0;
mjr 11:bd9da7088e6e 1823
mjr 35:e959ffba78fd 1824 // clear the joystick buttons
mjr 36:b9747461331e 1825 uint32_t newjs = 0;
mjr 35:e959ffba78fd 1826
mjr 35:e959ffba78fd 1827 // start with no media keys pressed
mjr 35:e959ffba78fd 1828 uint8_t mediakeys = 0;
mjr 38:091e511ce8a0 1829
mjr 38:091e511ce8a0 1830 // calculate the time since the last run
mjr 53:9b2611964afc 1831 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 1832 buttonTimer.reset();
mjr 66:2e3583fbd2f4 1833
mjr 66:2e3583fbd2f4 1834 // check the shift button state
mjr 66:2e3583fbd2f4 1835 if (shiftButton.index != -1)
mjr 66:2e3583fbd2f4 1836 {
mjr 66:2e3583fbd2f4 1837 ButtonState *sbs = &buttonState[shiftButton.index];
mjr 66:2e3583fbd2f4 1838 switch (shiftButton.state)
mjr 66:2e3583fbd2f4 1839 {
mjr 66:2e3583fbd2f4 1840 case 0:
mjr 66:2e3583fbd2f4 1841 // Not shifted. Check if the button is now down: if so,
mjr 66:2e3583fbd2f4 1842 // switch to state 1 (shift button down, no key pressed yet).
mjr 66:2e3583fbd2f4 1843 if (sbs->physState)
mjr 66:2e3583fbd2f4 1844 shiftButton.state = 1;
mjr 66:2e3583fbd2f4 1845 break;
mjr 66:2e3583fbd2f4 1846
mjr 66:2e3583fbd2f4 1847 case 1:
mjr 66:2e3583fbd2f4 1848 // Shift button down, no key pressed yet. If the button is
mjr 66:2e3583fbd2f4 1849 // now up, it counts as an ordinary button press instead of
mjr 66:2e3583fbd2f4 1850 // a shift button press, since the shift function was never
mjr 66:2e3583fbd2f4 1851 // used. Return to unshifted state and start a timed key
mjr 66:2e3583fbd2f4 1852 // pulse event.
mjr 66:2e3583fbd2f4 1853 if (!sbs->physState)
mjr 66:2e3583fbd2f4 1854 {
mjr 66:2e3583fbd2f4 1855 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 1856 shiftButton.pulse = 1;
mjr 66:2e3583fbd2f4 1857 shiftButton.pulseTime = 50000+dt; // 50 ms left on the key pulse
mjr 66:2e3583fbd2f4 1858 }
mjr 66:2e3583fbd2f4 1859 break;
mjr 66:2e3583fbd2f4 1860
mjr 66:2e3583fbd2f4 1861 case 2:
mjr 66:2e3583fbd2f4 1862 // Shift button down, other key was pressed. If the button is
mjr 66:2e3583fbd2f4 1863 // now up, simply clear the shift state without sending a key
mjr 66:2e3583fbd2f4 1864 // press for the shift button itself to the PC. The shift
mjr 66:2e3583fbd2f4 1865 // function was used, so its ordinary key press function is
mjr 66:2e3583fbd2f4 1866 // suppressed.
mjr 66:2e3583fbd2f4 1867 if (!sbs->physState)
mjr 66:2e3583fbd2f4 1868 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 1869 break;
mjr 66:2e3583fbd2f4 1870 }
mjr 66:2e3583fbd2f4 1871 }
mjr 38:091e511ce8a0 1872
mjr 11:bd9da7088e6e 1873 // scan the button list
mjr 18:5e890ebd0023 1874 ButtonState *bs = buttonState;
mjr 65:739875521aae 1875 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 11:bd9da7088e6e 1876 {
mjr 66:2e3583fbd2f4 1877 // Check the button type:
mjr 66:2e3583fbd2f4 1878 // - shift button
mjr 66:2e3583fbd2f4 1879 // - pulsed button
mjr 66:2e3583fbd2f4 1880 // - regular button
mjr 66:2e3583fbd2f4 1881 if (shiftButton.index == i)
mjr 66:2e3583fbd2f4 1882 {
mjr 66:2e3583fbd2f4 1883 // This is the shift button. Its logical state for key
mjr 66:2e3583fbd2f4 1884 // reporting purposes is controlled by the shift buttton
mjr 66:2e3583fbd2f4 1885 // pulse timer. If we're in a pulse, its logical state
mjr 66:2e3583fbd2f4 1886 // is pressed.
mjr 66:2e3583fbd2f4 1887 if (shiftButton.pulse)
mjr 66:2e3583fbd2f4 1888 {
mjr 66:2e3583fbd2f4 1889 // deduct the current interval from the pulse time, ending
mjr 66:2e3583fbd2f4 1890 // the pulse if the time has expired
mjr 66:2e3583fbd2f4 1891 if (shiftButton.pulseTime > dt)
mjr 66:2e3583fbd2f4 1892 shiftButton.pulseTime -= dt;
mjr 66:2e3583fbd2f4 1893 else
mjr 66:2e3583fbd2f4 1894 shiftButton.pulse = 0;
mjr 66:2e3583fbd2f4 1895 }
mjr 66:2e3583fbd2f4 1896
mjr 66:2e3583fbd2f4 1897 // the button is logically pressed if we're in a pulse
mjr 66:2e3583fbd2f4 1898 bs->logState = shiftButton.pulse;
mjr 66:2e3583fbd2f4 1899 }
mjr 66:2e3583fbd2f4 1900 else if (bs->pulseState != 0)
mjr 18:5e890ebd0023 1901 {
mjr 38:091e511ce8a0 1902 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 1903 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 1904 {
mjr 53:9b2611964afc 1905 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 1906 bs->pulseTime -= dt;
mjr 53:9b2611964afc 1907 }
mjr 53:9b2611964afc 1908 else
mjr 53:9b2611964afc 1909 {
mjr 53:9b2611964afc 1910 // pulse time expired - check for a state change
mjr 53:9b2611964afc 1911 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 1912 switch (bs->pulseState)
mjr 18:5e890ebd0023 1913 {
mjr 38:091e511ce8a0 1914 case 1:
mjr 38:091e511ce8a0 1915 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 1916 if (bs->physState)
mjr 53:9b2611964afc 1917 {
mjr 38:091e511ce8a0 1918 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1919 bs->pulseState = 2;
mjr 53:9b2611964afc 1920 bs->logState = 1;
mjr 38:091e511ce8a0 1921 }
mjr 38:091e511ce8a0 1922 break;
mjr 18:5e890ebd0023 1923
mjr 38:091e511ce8a0 1924 case 2:
mjr 38:091e511ce8a0 1925 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 1926 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 1927 // change in state in the logical button
mjr 38:091e511ce8a0 1928 bs->pulseState = 3;
mjr 38:091e511ce8a0 1929 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 1930 bs->logState = 0;
mjr 38:091e511ce8a0 1931 break;
mjr 38:091e511ce8a0 1932
mjr 38:091e511ce8a0 1933 case 3:
mjr 38:091e511ce8a0 1934 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 1935 if (!bs->physState)
mjr 53:9b2611964afc 1936 {
mjr 38:091e511ce8a0 1937 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 1938 bs->pulseState = 4;
mjr 53:9b2611964afc 1939 bs->logState = 1;
mjr 38:091e511ce8a0 1940 }
mjr 38:091e511ce8a0 1941 break;
mjr 38:091e511ce8a0 1942
mjr 38:091e511ce8a0 1943 case 4:
mjr 38:091e511ce8a0 1944 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 1945 bs->pulseState = 1;
mjr 38:091e511ce8a0 1946 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 1947 bs->logState = 0;
mjr 38:091e511ce8a0 1948 break;
mjr 18:5e890ebd0023 1949 }
mjr 18:5e890ebd0023 1950 }
mjr 38:091e511ce8a0 1951 }
mjr 38:091e511ce8a0 1952 else
mjr 38:091e511ce8a0 1953 {
mjr 38:091e511ce8a0 1954 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 1955 bs->logState = bs->physState;
mjr 38:091e511ce8a0 1956 }
mjr 35:e959ffba78fd 1957
mjr 38:091e511ce8a0 1958 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 1959 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 1960 {
mjr 38:091e511ce8a0 1961 // check for special key transitions
mjr 53:9b2611964afc 1962 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 1963 {
mjr 53:9b2611964afc 1964 // Check the switch type in the config flags. If flag 0x01 is set,
mjr 53:9b2611964afc 1965 // it's a persistent on/off switch, so the night mode state simply
mjr 53:9b2611964afc 1966 // follows the current state of the switch. Otherwise, it's a
mjr 53:9b2611964afc 1967 // momentary button, so each button push (i.e., each transition from
mjr 53:9b2611964afc 1968 // logical state OFF to ON) toggles the current night mode state.
mjr 53:9b2611964afc 1969 if (cfg.nightMode.flags & 0x01)
mjr 53:9b2611964afc 1970 {
mjr 53:9b2611964afc 1971 // toggle switch - when the button changes state, change
mjr 53:9b2611964afc 1972 // night mode to match the new state
mjr 53:9b2611964afc 1973 setNightMode(bs->logState);
mjr 53:9b2611964afc 1974 }
mjr 53:9b2611964afc 1975 else
mjr 53:9b2611964afc 1976 {
mjr 66:2e3583fbd2f4 1977 // Momentary switch - toggle the night mode state when the
mjr 53:9b2611964afc 1978 // physical button is pushed (i.e., when its logical state
mjr 66:2e3583fbd2f4 1979 // transitions from OFF to ON).
mjr 66:2e3583fbd2f4 1980 //
mjr 66:2e3583fbd2f4 1981 // In momentary mode, night mode flag 0x02 makes it the
mjr 66:2e3583fbd2f4 1982 // shifted version of the button. In this case, only
mjr 66:2e3583fbd2f4 1983 // proceed if the shift button is pressed.
mjr 66:2e3583fbd2f4 1984 bool pressed = bs->logState;
mjr 66:2e3583fbd2f4 1985 if ((cfg.nightMode.flags & 0x02) != 0)
mjr 66:2e3583fbd2f4 1986 {
mjr 66:2e3583fbd2f4 1987 // if the shift button is pressed but hasn't been used
mjr 66:2e3583fbd2f4 1988 // as a shift yet, mark it as used, so that it doesn't
mjr 66:2e3583fbd2f4 1989 // also generate its own key code on release
mjr 66:2e3583fbd2f4 1990 if (shiftButton.state == 1)
mjr 66:2e3583fbd2f4 1991 shiftButton.state = 2;
mjr 66:2e3583fbd2f4 1992
mjr 66:2e3583fbd2f4 1993 // if the shift button isn't even pressed
mjr 66:2e3583fbd2f4 1994 if (shiftButton.state == 0)
mjr 66:2e3583fbd2f4 1995 pressed = false;
mjr 66:2e3583fbd2f4 1996 }
mjr 66:2e3583fbd2f4 1997
mjr 66:2e3583fbd2f4 1998 // if it's pressed (even after considering the shift mode),
mjr 66:2e3583fbd2f4 1999 // toggle night mode
mjr 66:2e3583fbd2f4 2000 if (pressed)
mjr 53:9b2611964afc 2001 toggleNightMode();
mjr 53:9b2611964afc 2002 }
mjr 35:e959ffba78fd 2003 }
mjr 38:091e511ce8a0 2004
mjr 38:091e511ce8a0 2005 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 2006 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 2007 }
mjr 38:091e511ce8a0 2008
mjr 53:9b2611964afc 2009 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 2010 // key state list
mjr 53:9b2611964afc 2011 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 2012 {
mjr 66:2e3583fbd2f4 2013 // Get the key type and code. If the shift button is down AND
mjr 66:2e3583fbd2f4 2014 // this button has a shifted key meaning, use the shifted key
mjr 66:2e3583fbd2f4 2015 // meaning. Otherwise use the primary key meaning.
mjr 65:739875521aae 2016 ButtonCfg *bc = &cfg.button[bs->cfgIndex];
mjr 66:2e3583fbd2f4 2017 uint8_t typ, val;
mjr 66:2e3583fbd2f4 2018 if (shiftButton.state && bc->typ2 != BtnTypeNone)
mjr 66:2e3583fbd2f4 2019 {
mjr 66:2e3583fbd2f4 2020 // shifted - use the secondary key code
mjr 66:2e3583fbd2f4 2021 typ = bc->typ2, val = bc->val2;
mjr 66:2e3583fbd2f4 2022
mjr 66:2e3583fbd2f4 2023 // If the shift button hasn't been used a a shift yet
mjr 66:2e3583fbd2f4 2024 // (state 1), mark it as used (state 2). This prevents
mjr 66:2e3583fbd2f4 2025 // it from generating its own keystroke when released,
mjr 66:2e3583fbd2f4 2026 // which it does if no other keys are pressed while it's
mjr 66:2e3583fbd2f4 2027 // being held.
mjr 66:2e3583fbd2f4 2028 if (shiftButton.state == 1)
mjr 66:2e3583fbd2f4 2029 shiftButton.state = 2;
mjr 66:2e3583fbd2f4 2030 }
mjr 66:2e3583fbd2f4 2031 else
mjr 66:2e3583fbd2f4 2032 {
mjr 66:2e3583fbd2f4 2033 // not shifted - use the primary key code
mjr 66:2e3583fbd2f4 2034 typ = bc->typ, val = bc->val;
mjr 66:2e3583fbd2f4 2035 }
mjr 66:2e3583fbd2f4 2036
mjr 66:2e3583fbd2f4 2037 switch (typ)
mjr 53:9b2611964afc 2038 {
mjr 53:9b2611964afc 2039 case BtnTypeJoystick:
mjr 53:9b2611964afc 2040 // joystick button
mjr 53:9b2611964afc 2041 newjs |= (1 << (val - 1));
mjr 53:9b2611964afc 2042 break;
mjr 53:9b2611964afc 2043
mjr 53:9b2611964afc 2044 case BtnTypeKey:
mjr 67:c39e66c4e000 2045 // Keyboard key. The USB keyboard report encodes regular
mjr 67:c39e66c4e000 2046 // keys and modifier keys separately, so we need to check
mjr 67:c39e66c4e000 2047 // which type we have. Note that past versions mapped the
mjr 67:c39e66c4e000 2048 // Keyboard Volume Up, Keyboard Volume Down, and Keyboard
mjr 67:c39e66c4e000 2049 // Mute keys to the corresponding Media keys. We no longer
mjr 67:c39e66c4e000 2050 // do this; instead, we have the separate BtnTypeMedia for
mjr 67:c39e66c4e000 2051 // explicitly using media keys if desired.
mjr 67:c39e66c4e000 2052 if (val >= 0xE0 && val <= 0xE7)
mjr 53:9b2611964afc 2053 {
mjr 67:c39e66c4e000 2054 // It's a modifier key. These are represented in the USB
mjr 67:c39e66c4e000 2055 // reports with a bit mask. We arrange the mask bits in
mjr 67:c39e66c4e000 2056 // the same order as the scan codes, so we can figure the
mjr 67:c39e66c4e000 2057 // appropriate bit with a simple shift.
mjr 53:9b2611964afc 2058 modkeys |= (1 << (val - 0xE0));
mjr 53:9b2611964afc 2059 }
mjr 53:9b2611964afc 2060 else
mjr 53:9b2611964afc 2061 {
mjr 67:c39e66c4e000 2062 // It's a regular key. Make sure it's not already in the
mjr 67:c39e66c4e000 2063 // list, and that the list isn't full. If neither of these
mjr 67:c39e66c4e000 2064 // apply, add the key to the key array.
mjr 53:9b2611964afc 2065 if (nkeys < 7)
mjr 53:9b2611964afc 2066 {
mjr 57:cc03231f676b 2067 bool found = false;
mjr 53:9b2611964afc 2068 for (int j = 0 ; j < nkeys ; ++j)
mjr 53:9b2611964afc 2069 {
mjr 53:9b2611964afc 2070 if (keys[j] == val)
mjr 53:9b2611964afc 2071 {
mjr 53:9b2611964afc 2072 found = true;
mjr 53:9b2611964afc 2073 break;
mjr 53:9b2611964afc 2074 }
mjr 53:9b2611964afc 2075 }
mjr 53:9b2611964afc 2076 if (!found)
mjr 53:9b2611964afc 2077 keys[nkeys++] = val;
mjr 53:9b2611964afc 2078 }
mjr 53:9b2611964afc 2079 }
mjr 53:9b2611964afc 2080 break;
mjr 67:c39e66c4e000 2081
mjr 67:c39e66c4e000 2082 case BtnTypeMedia:
mjr 67:c39e66c4e000 2083 // Media control key. The media keys are mapped in the USB
mjr 67:c39e66c4e000 2084 // report to bits, whereas the key codes are specified in the
mjr 67:c39e66c4e000 2085 // config with their USB usage numbers. E.g., the config val
mjr 67:c39e66c4e000 2086 // for Media Next Track is 0xB5, but we encode this in the USB
mjr 67:c39e66c4e000 2087 // report as bit 0x08. The mediaKeyMap[] table translates
mjr 67:c39e66c4e000 2088 // from the USB usage number to the mask bit. If the key isn't
mjr 67:c39e66c4e000 2089 // among the subset we support, the mapped bit will be zero, so
mjr 67:c39e66c4e000 2090 // the "|=" will have no effect and the key will be ignored.
mjr 67:c39e66c4e000 2091 mediakeys |= mediaKeyMap[val];
mjr 67:c39e66c4e000 2092 break;
mjr 53:9b2611964afc 2093 }
mjr 18:5e890ebd0023 2094 }
mjr 11:bd9da7088e6e 2095 }
mjr 36:b9747461331e 2096
mjr 36:b9747461331e 2097 // check for joystick button changes
mjr 36:b9747461331e 2098 if (jsButtons != newjs)
mjr 36:b9747461331e 2099 jsButtons = newjs;
mjr 11:bd9da7088e6e 2100
mjr 35:e959ffba78fd 2101 // Check for changes to the keyboard keys
mjr 35:e959ffba78fd 2102 if (kbState.data[0] != modkeys
mjr 35:e959ffba78fd 2103 || kbState.nkeys != nkeys
mjr 35:e959ffba78fd 2104 || memcmp(keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 2105 {
mjr 35:e959ffba78fd 2106 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 2107 kbState.changed = true;
mjr 35:e959ffba78fd 2108 kbState.data[0] = modkeys;
mjr 35:e959ffba78fd 2109 if (nkeys <= 6) {
mjr 35:e959ffba78fd 2110 // 6 or fewer simultaneous keys - report the key codes
mjr 35:e959ffba78fd 2111 kbState.nkeys = nkeys;
mjr 35:e959ffba78fd 2112 memcpy(&kbState.data[2], keys, 6);
mjr 35:e959ffba78fd 2113 }
mjr 35:e959ffba78fd 2114 else {
mjr 35:e959ffba78fd 2115 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 2116 kbState.nkeys = 6;
mjr 35:e959ffba78fd 2117 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 2118 }
mjr 35:e959ffba78fd 2119 }
mjr 35:e959ffba78fd 2120
mjr 35:e959ffba78fd 2121 // Check for changes to media keys
mjr 35:e959ffba78fd 2122 if (mediaState.data != mediakeys)
mjr 35:e959ffba78fd 2123 {
mjr 35:e959ffba78fd 2124 mediaState.changed = true;
mjr 35:e959ffba78fd 2125 mediaState.data = mediakeys;
mjr 35:e959ffba78fd 2126 }
mjr 11:bd9da7088e6e 2127 }
mjr 11:bd9da7088e6e 2128
mjr 5:a70c0bce770d 2129 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 2130 //
mjr 5:a70c0bce770d 2131 // Customization joystick subbclass
mjr 5:a70c0bce770d 2132 //
mjr 5:a70c0bce770d 2133
mjr 5:a70c0bce770d 2134 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 2135 {
mjr 5:a70c0bce770d 2136 public:
mjr 35:e959ffba78fd 2137 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 35:e959ffba78fd 2138 bool waitForConnect, bool enableJoystick, bool useKB)
mjr 35:e959ffba78fd 2139 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, useKB)
mjr 5:a70c0bce770d 2140 {
mjr 54:fd77a6b2f76c 2141 sleeping_ = false;
mjr 54:fd77a6b2f76c 2142 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 2143 timer_.start();
mjr 54:fd77a6b2f76c 2144 }
mjr 54:fd77a6b2f76c 2145
mjr 54:fd77a6b2f76c 2146 // show diagnostic LED feedback for connect state
mjr 54:fd77a6b2f76c 2147 void diagFlash()
mjr 54:fd77a6b2f76c 2148 {
mjr 54:fd77a6b2f76c 2149 if (!configured() || sleeping_)
mjr 54:fd77a6b2f76c 2150 {
mjr 54:fd77a6b2f76c 2151 // flash once if sleeping or twice if disconnected
mjr 54:fd77a6b2f76c 2152 for (int j = isConnected() ? 1 : 2 ; j > 0 ; --j)
mjr 54:fd77a6b2f76c 2153 {
mjr 54:fd77a6b2f76c 2154 // short red flash
mjr 54:fd77a6b2f76c 2155 diagLED(1, 0, 0);
mjr 54:fd77a6b2f76c 2156 wait_us(50000);
mjr 54:fd77a6b2f76c 2157 diagLED(0, 0, 0);
mjr 54:fd77a6b2f76c 2158 wait_us(50000);
mjr 54:fd77a6b2f76c 2159 }
mjr 54:fd77a6b2f76c 2160 }
mjr 5:a70c0bce770d 2161 }
mjr 5:a70c0bce770d 2162
mjr 5:a70c0bce770d 2163 // are we connected?
mjr 5:a70c0bce770d 2164 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 2165
mjr 54:fd77a6b2f76c 2166 // Are we in sleep mode? If true, this means that the hardware has
mjr 54:fd77a6b2f76c 2167 // detected no activity on the bus for 3ms. This happens when the
mjr 54:fd77a6b2f76c 2168 // cable is physically disconnected, the computer is turned off, or
mjr 54:fd77a6b2f76c 2169 // the connection is otherwise disabled.
mjr 54:fd77a6b2f76c 2170 bool isSleeping() const { return sleeping_; }
mjr 54:fd77a6b2f76c 2171
mjr 54:fd77a6b2f76c 2172 // If necessary, attempt to recover from a broken connection.
mjr 54:fd77a6b2f76c 2173 //
mjr 54:fd77a6b2f76c 2174 // This is a hack, to work around an apparent timing bug in the
mjr 54:fd77a6b2f76c 2175 // KL25Z USB implementation that I haven't been able to solve any
mjr 54:fd77a6b2f76c 2176 // other way.
mjr 54:fd77a6b2f76c 2177 //
mjr 54:fd77a6b2f76c 2178 // The issue: when we have an established connection, and the
mjr 54:fd77a6b2f76c 2179 // connection is broken by physically unplugging the cable or by
mjr 54:fd77a6b2f76c 2180 // rebooting the PC, the KL25Z sometimes fails to reconnect when
mjr 54:fd77a6b2f76c 2181 // the physical connection is re-established. The failure is
mjr 54:fd77a6b2f76c 2182 // sporadic; I'd guess it happens about 25% of the time, but I
mjr 54:fd77a6b2f76c 2183 // haven't collected any real statistics on it.
mjr 54:fd77a6b2f76c 2184 //
mjr 54:fd77a6b2f76c 2185 // The proximate cause of the failure is a deadlock in the SETUP
mjr 54:fd77a6b2f76c 2186 // protocol between the host and device that happens around the
mjr 54:fd77a6b2f76c 2187 // point where the PC is requesting the configuration descriptor.
mjr 54:fd77a6b2f76c 2188 // The exact point in the protocol where this occurs varies slightly;
mjr 54:fd77a6b2f76c 2189 // it can occur a message or two before or after the Get Config
mjr 54:fd77a6b2f76c 2190 // Descriptor packet. No matter where it happens, the nature of
mjr 54:fd77a6b2f76c 2191 // the deadlock is the same: the PC thinks it sees a STALL on EP0
mjr 54:fd77a6b2f76c 2192 // from the device, so it terminates the connection attempt, which
mjr 54:fd77a6b2f76c 2193 // stops further traffic on the cable. The KL25Z USB hardware sees
mjr 54:fd77a6b2f76c 2194 // the lack of traffic and triggers a SLEEP interrupt (a misnomer
mjr 54:fd77a6b2f76c 2195 // for what should have been called a BROKEN CONNECTION interrupt).
mjr 54:fd77a6b2f76c 2196 // Both sides simply stop talking at this point, so the connection
mjr 54:fd77a6b2f76c 2197 // is effectively dead.
mjr 54:fd77a6b2f76c 2198 //
mjr 54:fd77a6b2f76c 2199 // The strange thing is that, as far as I can tell, the KL25Z isn't
mjr 54:fd77a6b2f76c 2200 // doing anything to trigger the STALL on its end. Both the PC
mjr 54:fd77a6b2f76c 2201 // and the KL25Z are happy up until the very point of the failure
mjr 54:fd77a6b2f76c 2202 // and show no signs of anything wrong in the protocol exchange.
mjr 54:fd77a6b2f76c 2203 // In fact, every detail of the protocol exchange up to this point
mjr 54:fd77a6b2f76c 2204 // is identical to every successful exchange that does finish the
mjr 54:fd77a6b2f76c 2205 // whole setup process successfully, on both the KL25Z and Windows
mjr 54:fd77a6b2f76c 2206 // sides of the connection. I can't find any point of difference
mjr 54:fd77a6b2f76c 2207 // between successful and unsuccessful sequences that suggests why
mjr 54:fd77a6b2f76c 2208 // the fateful message fails. This makes me suspect that whatever
mjr 54:fd77a6b2f76c 2209 // is going wrong is inside the KL25Z USB hardware module, which
mjr 54:fd77a6b2f76c 2210 // is a pretty substantial black box - it has a lot of internal
mjr 54:fd77a6b2f76c 2211 // state that's inaccessible to the software. Further bolstering
mjr 54:fd77a6b2f76c 2212 // this theory is a little experiment where I found that I could
mjr 54:fd77a6b2f76c 2213 // reproduce the exact sequence of events of a failed reconnect
mjr 54:fd77a6b2f76c 2214 // attempt in an *initial* connection, which is otherwise 100%
mjr 54:fd77a6b2f76c 2215 // reliable, by inserting a little bit of artifical time padding
mjr 54:fd77a6b2f76c 2216 // (200us per event) into the SETUP interrupt handler. My
mjr 54:fd77a6b2f76c 2217 // hypothesis is that the STALL event happens because the KL25Z
mjr 54:fd77a6b2f76c 2218 // USB hardware is too slow to respond to a message. I'm not
mjr 54:fd77a6b2f76c 2219 // sure why this would only happen after a disconnect and not
mjr 54:fd77a6b2f76c 2220 // during the initial connection; maybe there's some reset work
mjr 54:fd77a6b2f76c 2221 // in the hardware that takes a substantial amount of time after
mjr 54:fd77a6b2f76c 2222 // a disconnect.
mjr 54:fd77a6b2f76c 2223 //
mjr 54:fd77a6b2f76c 2224 // The solution: the problem happens during the SETUP exchange,
mjr 54:fd77a6b2f76c 2225 // after we've been assigned a bus address. It only happens on
mjr 54:fd77a6b2f76c 2226 // some percentage of connection requests, so if we can simply
mjr 54:fd77a6b2f76c 2227 // start over when the failure occurs, we'll eventually succeed
mjr 54:fd77a6b2f76c 2228 // simply because not every attempt fails. The ideal would be
mjr 54:fd77a6b2f76c 2229 // to get the success rate up to 100%, but I can't figure out how
mjr 54:fd77a6b2f76c 2230 // to fix the underlying problem, so this is the next best thing.
mjr 54:fd77a6b2f76c 2231 //
mjr 54:fd77a6b2f76c 2232 // We can detect when the failure occurs by noticing when a SLEEP
mjr 54:fd77a6b2f76c 2233 // interrupt happens while we have an assigned bus address.
mjr 54:fd77a6b2f76c 2234 //
mjr 54:fd77a6b2f76c 2235 // To start a new connection attempt, we have to make the *host*
mjr 54:fd77a6b2f76c 2236 // try again. The logical connection is initiated solely by the
mjr 54:fd77a6b2f76c 2237 // host. Fortunately, it's easy to get the host to initiate the
mjr 54:fd77a6b2f76c 2238 // process: if we disconnect on the device side, it effectively
mjr 54:fd77a6b2f76c 2239 // makes the device look to the PC like it's electrically unplugged.
mjr 54:fd77a6b2f76c 2240 // When we reconnect on the device side, the PC thinks a new device
mjr 54:fd77a6b2f76c 2241 // has been plugged in and initiates the logical connection setup.
mjr 54:fd77a6b2f76c 2242 // We have to remain disconnected for a macroscopic interval for
mjr 54:fd77a6b2f76c 2243 // this to happen - 5ms seems to do the trick.
mjr 54:fd77a6b2f76c 2244 //
mjr 54:fd77a6b2f76c 2245 // Here's the full algorithm:
mjr 54:fd77a6b2f76c 2246 //
mjr 54:fd77a6b2f76c 2247 // 1. In the SLEEP interrupt handler, if we have a bus address,
mjr 54:fd77a6b2f76c 2248 // we disconnect the device. This happens in ISR context, so we
mjr 54:fd77a6b2f76c 2249 // can't wait around for 5ms. Instead, we simply set a flag noting
mjr 54:fd77a6b2f76c 2250 // that the connection has been broken, and we note the time and
mjr 54:fd77a6b2f76c 2251 // return.
mjr 54:fd77a6b2f76c 2252 //
mjr 54:fd77a6b2f76c 2253 // 2. In our main loop, whenever we find that we're disconnected,
mjr 54:fd77a6b2f76c 2254 // we call recoverConnection(). The main loop's job is basically a
mjr 54:fd77a6b2f76c 2255 // bunch of device polling. We're just one more device to poll, so
mjr 54:fd77a6b2f76c 2256 // recoverConnection() will be called soon after a disconnect, and
mjr 54:fd77a6b2f76c 2257 // then will be called in a loop for as long as we're disconnected.
mjr 54:fd77a6b2f76c 2258 //
mjr 54:fd77a6b2f76c 2259 // 3. In recoverConnection(), we check the flag we set in the SLEEP
mjr 54:fd77a6b2f76c 2260 // handler. If set, we wait until 5ms has elapsed from the SLEEP
mjr 54:fd77a6b2f76c 2261 // event time that we noted, then we'll reconnect and clear the flag.
mjr 54:fd77a6b2f76c 2262 // This gives us the required 5ms (or longer) delay between the
mjr 54:fd77a6b2f76c 2263 // disconnect and reconnect, ensuring that the PC will notice and
mjr 54:fd77a6b2f76c 2264 // will start over with the connection protocol.
mjr 54:fd77a6b2f76c 2265 //
mjr 54:fd77a6b2f76c 2266 // 4. The main loop keeps calling recoverConnection() in a loop for
mjr 54:fd77a6b2f76c 2267 // as long as we're disconnected, so if the new connection attempt
mjr 54:fd77a6b2f76c 2268 // triggered in step 3 fails, the SLEEP interrupt will happen again,
mjr 54:fd77a6b2f76c 2269 // we'll disconnect again, the flag will get set again, and
mjr 54:fd77a6b2f76c 2270 // recoverConnection() will reconnect again after another suitable
mjr 54:fd77a6b2f76c 2271 // delay. This will repeat until the connection succeeds or hell
mjr 54:fd77a6b2f76c 2272 // freezes over.
mjr 54:fd77a6b2f76c 2273 //
mjr 54:fd77a6b2f76c 2274 // Each disconnect happens immediately when a reconnect attempt
mjr 54:fd77a6b2f76c 2275 // fails, and an entire successful connection only takes about 25ms,
mjr 54:fd77a6b2f76c 2276 // so our loop can retry at more than 30 attempts per second.
mjr 54:fd77a6b2f76c 2277 // In my testing, lost connections almost always reconnect in
mjr 54:fd77a6b2f76c 2278 // less than second with this code in place.
mjr 54:fd77a6b2f76c 2279 void recoverConnection()
mjr 54:fd77a6b2f76c 2280 {
mjr 54:fd77a6b2f76c 2281 // if a reconnect is pending, reconnect
mjr 54:fd77a6b2f76c 2282 if (reconnectPending_)
mjr 54:fd77a6b2f76c 2283 {
mjr 54:fd77a6b2f76c 2284 // Loop until we reach 5ms after the last sleep event.
mjr 54:fd77a6b2f76c 2285 for (bool done = false ; !done ; )
mjr 54:fd77a6b2f76c 2286 {
mjr 54:fd77a6b2f76c 2287 // If we've reached the target time, reconnect. Do the
mjr 54:fd77a6b2f76c 2288 // time check and flag reset atomically, so that we can't
mjr 54:fd77a6b2f76c 2289 // have another sleep event sneak in after we've verified
mjr 54:fd77a6b2f76c 2290 // the time. If another event occurs, it has to happen
mjr 54:fd77a6b2f76c 2291 // before we check, in which case it'll update the time
mjr 54:fd77a6b2f76c 2292 // before we check it, or after we clear the flag, in
mjr 54:fd77a6b2f76c 2293 // which case it will reset the flag and we'll do another
mjr 54:fd77a6b2f76c 2294 // round the next time we call this routine.
mjr 54:fd77a6b2f76c 2295 __disable_irq();
mjr 54:fd77a6b2f76c 2296 if (uint32_t(timer_.read_us() - lastSleepTime_) > 5000)
mjr 54:fd77a6b2f76c 2297 {
mjr 54:fd77a6b2f76c 2298 connect(false);
mjr 54:fd77a6b2f76c 2299 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 2300 done = true;
mjr 54:fd77a6b2f76c 2301 }
mjr 54:fd77a6b2f76c 2302 __enable_irq();
mjr 54:fd77a6b2f76c 2303 }
mjr 54:fd77a6b2f76c 2304 }
mjr 54:fd77a6b2f76c 2305 }
mjr 5:a70c0bce770d 2306
mjr 5:a70c0bce770d 2307 protected:
mjr 54:fd77a6b2f76c 2308 // Handle a USB SLEEP interrupt. This interrupt signifies that the
mjr 54:fd77a6b2f76c 2309 // USB hardware module hasn't seen any token traffic for 3ms, which
mjr 54:fd77a6b2f76c 2310 // means that we're either physically or logically disconnected.
mjr 54:fd77a6b2f76c 2311 //
mjr 54:fd77a6b2f76c 2312 // Important: this runs in ISR context.
mjr 54:fd77a6b2f76c 2313 //
mjr 54:fd77a6b2f76c 2314 // Note that this is a specialized sense of "sleep" that's unrelated
mjr 54:fd77a6b2f76c 2315 // to the similarly named power modes on the PC. This has nothing
mjr 54:fd77a6b2f76c 2316 // to do with suspend/sleep mode on the PC, and it's not a low-power
mjr 54:fd77a6b2f76c 2317 // mode on the KL25Z. They really should have called this interrupt
mjr 54:fd77a6b2f76c 2318 // DISCONNECT or BROKEN CONNECTION.)
mjr 54:fd77a6b2f76c 2319 virtual void sleepStateChanged(unsigned int sleeping)
mjr 54:fd77a6b2f76c 2320 {
mjr 54:fd77a6b2f76c 2321 // note the new state
mjr 54:fd77a6b2f76c 2322 sleeping_ = sleeping;
mjr 54:fd77a6b2f76c 2323
mjr 54:fd77a6b2f76c 2324 // If we have a non-zero bus address, we have at least a partial
mjr 54:fd77a6b2f76c 2325 // connection to the host (we've made it at least as far as the
mjr 54:fd77a6b2f76c 2326 // SETUP stage). Explicitly disconnect, and the pending reconnect
mjr 54:fd77a6b2f76c 2327 // flag, and remember the time of the sleep event.
mjr 54:fd77a6b2f76c 2328 if (USB0->ADDR != 0x00)
mjr 54:fd77a6b2f76c 2329 {
mjr 54:fd77a6b2f76c 2330 disconnect();
mjr 54:fd77a6b2f76c 2331 lastSleepTime_ = timer_.read_us();
mjr 54:fd77a6b2f76c 2332 reconnectPending_ = true;
mjr 54:fd77a6b2f76c 2333 }
mjr 54:fd77a6b2f76c 2334 }
mjr 54:fd77a6b2f76c 2335
mjr 54:fd77a6b2f76c 2336 // is the USB connection asleep?
mjr 54:fd77a6b2f76c 2337 volatile bool sleeping_;
mjr 54:fd77a6b2f76c 2338
mjr 54:fd77a6b2f76c 2339 // flag: reconnect pending after sleep event
mjr 54:fd77a6b2f76c 2340 volatile bool reconnectPending_;
mjr 54:fd77a6b2f76c 2341
mjr 54:fd77a6b2f76c 2342 // time of last sleep event while connected
mjr 54:fd77a6b2f76c 2343 volatile uint32_t lastSleepTime_;
mjr 54:fd77a6b2f76c 2344
mjr 54:fd77a6b2f76c 2345 // timer to keep track of interval since last sleep event
mjr 54:fd77a6b2f76c 2346 Timer timer_;
mjr 5:a70c0bce770d 2347 };
mjr 5:a70c0bce770d 2348
mjr 5:a70c0bce770d 2349 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 2350 //
mjr 5:a70c0bce770d 2351 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 2352 //
mjr 5:a70c0bce770d 2353
mjr 5:a70c0bce770d 2354 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 2355 //
mjr 5:a70c0bce770d 2356 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 2357 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 2358 // automatic calibration.
mjr 5:a70c0bce770d 2359 //
mjr 5:a70c0bce770d 2360 // We install an interrupt handler on the accelerometer "data ready"
mjr 6:cc35eb643e8f 2361 // interrupt to ensure that we fetch each sample immediately when it
mjr 6:cc35eb643e8f 2362 // becomes available. The accelerometer data rate is fiarly high
mjr 6:cc35eb643e8f 2363 // (800 Hz), so it's not practical to keep up with it by polling.
mjr 6:cc35eb643e8f 2364 // Using an interrupt handler lets us respond quickly and read
mjr 6:cc35eb643e8f 2365 // every sample.
mjr 5:a70c0bce770d 2366 //
mjr 6:cc35eb643e8f 2367 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 2368 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 2369 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 2370 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 2371 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 2372 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 2373 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 2374 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 2375 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 2376 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 2377 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 2378 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 2379 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 2380 // of nudging, say).
mjr 5:a70c0bce770d 2381 //
mjr 5:a70c0bce770d 2382
mjr 17:ab3cec0c8bf4 2383 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 2384 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 2385
mjr 17:ab3cec0c8bf4 2386 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 2387 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 2388 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 2389
mjr 17:ab3cec0c8bf4 2390 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 2391 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 2392 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 2393 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 2394
mjr 17:ab3cec0c8bf4 2395
mjr 6:cc35eb643e8f 2396 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 2397 struct AccHist
mjr 5:a70c0bce770d 2398 {
mjr 6:cc35eb643e8f 2399 AccHist() { x = y = d = 0.0; xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 2400 void set(float x, float y, AccHist *prv)
mjr 6:cc35eb643e8f 2401 {
mjr 6:cc35eb643e8f 2402 // save the raw position
mjr 6:cc35eb643e8f 2403 this->x = x;
mjr 6:cc35eb643e8f 2404 this->y = y;
mjr 6:cc35eb643e8f 2405 this->d = distance(prv);
mjr 6:cc35eb643e8f 2406 }
mjr 6:cc35eb643e8f 2407
mjr 6:cc35eb643e8f 2408 // reading for this entry
mjr 5:a70c0bce770d 2409 float x, y;
mjr 5:a70c0bce770d 2410
mjr 6:cc35eb643e8f 2411 // distance from previous entry
mjr 6:cc35eb643e8f 2412 float d;
mjr 5:a70c0bce770d 2413
mjr 6:cc35eb643e8f 2414 // total and count of samples averaged over this period
mjr 6:cc35eb643e8f 2415 float xtot, ytot;
mjr 6:cc35eb643e8f 2416 int cnt;
mjr 6:cc35eb643e8f 2417
mjr 6:cc35eb643e8f 2418 void clearAvg() { xtot = ytot = 0.0; cnt = 0; }
mjr 6:cc35eb643e8f 2419 void addAvg(float x, float y) { xtot += x; ytot += y; ++cnt; }
mjr 6:cc35eb643e8f 2420 float xAvg() const { return xtot/cnt; }
mjr 6:cc35eb643e8f 2421 float yAvg() const { return ytot/cnt; }
mjr 5:a70c0bce770d 2422
mjr 6:cc35eb643e8f 2423 float distance(AccHist *p)
mjr 6:cc35eb643e8f 2424 { return sqrt(square(p->x - x) + square(p->y - y)); }
mjr 5:a70c0bce770d 2425 };
mjr 5:a70c0bce770d 2426
mjr 5:a70c0bce770d 2427 // accelerometer wrapper class
mjr 3:3514575d4f86 2428 class Accel
mjr 3:3514575d4f86 2429 {
mjr 3:3514575d4f86 2430 public:
mjr 3:3514575d4f86 2431 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin)
mjr 3:3514575d4f86 2432 : mma_(sda, scl, i2cAddr), intIn_(irqPin)
mjr 3:3514575d4f86 2433 {
mjr 5:a70c0bce770d 2434 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 2435 irqPin_ = irqPin;
mjr 5:a70c0bce770d 2436
mjr 5:a70c0bce770d 2437 // reset and initialize
mjr 5:a70c0bce770d 2438 reset();
mjr 5:a70c0bce770d 2439 }
mjr 5:a70c0bce770d 2440
mjr 5:a70c0bce770d 2441 void reset()
mjr 5:a70c0bce770d 2442 {
mjr 6:cc35eb643e8f 2443 // clear the center point
mjr 6:cc35eb643e8f 2444 cx_ = cy_ = 0.0;
mjr 6:cc35eb643e8f 2445
mjr 6:cc35eb643e8f 2446 // start the calibration timer
mjr 5:a70c0bce770d 2447 tCenter_.start();
mjr 5:a70c0bce770d 2448 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 2449
mjr 5:a70c0bce770d 2450 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 2451 mma_.init();
mjr 6:cc35eb643e8f 2452
mjr 6:cc35eb643e8f 2453 // set the initial integrated velocity reading to zero
mjr 6:cc35eb643e8f 2454 vx_ = vy_ = 0;
mjr 3:3514575d4f86 2455
mjr 6:cc35eb643e8f 2456 // set up our accelerometer interrupt handling
mjr 6:cc35eb643e8f 2457 intIn_.rise(this, &Accel::isr);
mjr 5:a70c0bce770d 2458 mma_.setInterruptMode(irqPin_ == PTA14 ? 1 : 2);
mjr 3:3514575d4f86 2459
mjr 3:3514575d4f86 2460 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 2461 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 2462
mjr 3:3514575d4f86 2463 // start our timers
mjr 3:3514575d4f86 2464 tGet_.start();
mjr 3:3514575d4f86 2465 tInt_.start();
mjr 3:3514575d4f86 2466 }
mjr 3:3514575d4f86 2467
mjr 9:fd65b0a94720 2468 void get(int &x, int &y)
mjr 3:3514575d4f86 2469 {
mjr 3:3514575d4f86 2470 // disable interrupts while manipulating the shared data
mjr 3:3514575d4f86 2471 __disable_irq();
mjr 3:3514575d4f86 2472
mjr 3:3514575d4f86 2473 // read the shared data and store locally for calculations
mjr 6:cc35eb643e8f 2474 float ax = ax_, ay = ay_;
mjr 6:cc35eb643e8f 2475 float vx = vx_, vy = vy_;
mjr 5:a70c0bce770d 2476
mjr 6:cc35eb643e8f 2477 // reset the velocity sum for the next run
mjr 6:cc35eb643e8f 2478 vx_ = vy_ = 0;
mjr 3:3514575d4f86 2479
mjr 3:3514575d4f86 2480 // get the time since the last get() sample
mjr 38:091e511ce8a0 2481 float dt = tGet_.read_us()/1.0e6f;
mjr 3:3514575d4f86 2482 tGet_.reset();
mjr 3:3514575d4f86 2483
mjr 3:3514575d4f86 2484 // done manipulating the shared data
mjr 3:3514575d4f86 2485 __enable_irq();
mjr 3:3514575d4f86 2486
mjr 6:cc35eb643e8f 2487 // adjust the readings for the integration time
mjr 6:cc35eb643e8f 2488 vx /= dt;
mjr 6:cc35eb643e8f 2489 vy /= dt;
mjr 6:cc35eb643e8f 2490
mjr 6:cc35eb643e8f 2491 // add this sample to the current calibration interval's running total
mjr 6:cc35eb643e8f 2492 AccHist *p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 2493 p->addAvg(ax, ay);
mjr 6:cc35eb643e8f 2494
mjr 5:a70c0bce770d 2495 // check for auto-centering every so often
mjr 48:058ace2aed1d 2496 if (tCenter_.read_us() > 1000000)
mjr 5:a70c0bce770d 2497 {
mjr 5:a70c0bce770d 2498 // add the latest raw sample to the history list
mjr 6:cc35eb643e8f 2499 AccHist *prv = p;
mjr 5:a70c0bce770d 2500 iAccPrv_ = (iAccPrv_ + 1) % maxAccPrv;
mjr 6:cc35eb643e8f 2501 p = accPrv_ + iAccPrv_;
mjr 6:cc35eb643e8f 2502 p->set(ax, ay, prv);
mjr 5:a70c0bce770d 2503
mjr 5:a70c0bce770d 2504 // if we have a full complement, check for stability
mjr 5:a70c0bce770d 2505 if (nAccPrv_ >= maxAccPrv)
mjr 5:a70c0bce770d 2506 {
mjr 5:a70c0bce770d 2507 // check if we've been stable for all recent samples
mjr 6:cc35eb643e8f 2508 static const float accTol = .01;
mjr 6:cc35eb643e8f 2509 AccHist *p0 = accPrv_;
mjr 6:cc35eb643e8f 2510 if (p0[0].d < accTol
mjr 6:cc35eb643e8f 2511 && p0[1].d < accTol
mjr 6:cc35eb643e8f 2512 && p0[2].d < accTol
mjr 6:cc35eb643e8f 2513 && p0[3].d < accTol
mjr 6:cc35eb643e8f 2514 && p0[4].d < accTol)
mjr 5:a70c0bce770d 2515 {
mjr 6:cc35eb643e8f 2516 // Figure the new calibration point as the average of
mjr 6:cc35eb643e8f 2517 // the samples over the rest period
mjr 6:cc35eb643e8f 2518 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5.0;
mjr 6:cc35eb643e8f 2519 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5.0;
mjr 5:a70c0bce770d 2520 }
mjr 5:a70c0bce770d 2521 }
mjr 5:a70c0bce770d 2522 else
mjr 5:a70c0bce770d 2523 {
mjr 5:a70c0bce770d 2524 // not enough samples yet; just up the count
mjr 5:a70c0bce770d 2525 ++nAccPrv_;
mjr 5:a70c0bce770d 2526 }
mjr 6:cc35eb643e8f 2527
mjr 6:cc35eb643e8f 2528 // clear the new item's running totals
mjr 6:cc35eb643e8f 2529 p->clearAvg();
mjr 5:a70c0bce770d 2530
mjr 5:a70c0bce770d 2531 // reset the timer
mjr 5:a70c0bce770d 2532 tCenter_.reset();
mjr 39:b3815a1c3802 2533
mjr 39:b3815a1c3802 2534 // If we haven't seen an interrupt in a while, do an explicit read to
mjr 39:b3815a1c3802 2535 // "unstick" the device. The device can become stuck - which is to say,
mjr 39:b3815a1c3802 2536 // it will stop delivering data-ready interrupts - if we fail to service
mjr 39:b3815a1c3802 2537 // one data-ready interrupt before the next one occurs. Reading a sample
mjr 39:b3815a1c3802 2538 // will clear up this overrun condition and allow normal interrupt
mjr 39:b3815a1c3802 2539 // generation to continue.
mjr 39:b3815a1c3802 2540 //
mjr 39:b3815a1c3802 2541 // Note that this stuck condition *shouldn't* ever occur - if it does,
mjr 39:b3815a1c3802 2542 // it means that we're spending a long period with interrupts disabled
mjr 39:b3815a1c3802 2543 // (either in a critical section or in another interrupt handler), which
mjr 39:b3815a1c3802 2544 // will likely cause other worse problems beyond the sticky accelerometer.
mjr 39:b3815a1c3802 2545 // Even so, it's easy to detect and correct, so we'll do so for the sake
mjr 39:b3815a1c3802 2546 // of making the system more fault-tolerant.
mjr 39:b3815a1c3802 2547 if (tInt_.read() > 1.0f)
mjr 39:b3815a1c3802 2548 {
mjr 39:b3815a1c3802 2549 float x, y, z;
mjr 39:b3815a1c3802 2550 mma_.getAccXYZ(x, y, z);
mjr 39:b3815a1c3802 2551 }
mjr 5:a70c0bce770d 2552 }
mjr 5:a70c0bce770d 2553
mjr 6:cc35eb643e8f 2554 // report our integrated velocity reading in x,y
mjr 6:cc35eb643e8f 2555 x = rawToReport(vx);
mjr 6:cc35eb643e8f 2556 y = rawToReport(vy);
mjr 5:a70c0bce770d 2557
mjr 6:cc35eb643e8f 2558 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 2559 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 2560 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 2561 #endif
mjr 3:3514575d4f86 2562 }
mjr 29:582472d0bc57 2563
mjr 3:3514575d4f86 2564 private:
mjr 6:cc35eb643e8f 2565 // adjust a raw acceleration figure to a usb report value
mjr 6:cc35eb643e8f 2566 int rawToReport(float v)
mjr 5:a70c0bce770d 2567 {
mjr 6:cc35eb643e8f 2568 // scale to the joystick report range and round to integer
mjr 6:cc35eb643e8f 2569 int i = int(round(v*JOYMAX));
mjr 5:a70c0bce770d 2570
mjr 6:cc35eb643e8f 2571 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 2572 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 2573 static const int filter[] = {
mjr 6:cc35eb643e8f 2574 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 2575 0,
mjr 6:cc35eb643e8f 2576 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 2577 };
mjr 6:cc35eb643e8f 2578 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 2579 }
mjr 5:a70c0bce770d 2580
mjr 3:3514575d4f86 2581 // interrupt handler
mjr 3:3514575d4f86 2582 void isr()
mjr 3:3514575d4f86 2583 {
mjr 3:3514575d4f86 2584 // Read the axes. Note that we have to read all three axes
mjr 3:3514575d4f86 2585 // (even though we only really use x and y) in order to clear
mjr 3:3514575d4f86 2586 // the "data ready" status bit in the accelerometer. The
mjr 3:3514575d4f86 2587 // interrupt only occurs when the "ready" bit transitions from
mjr 3:3514575d4f86 2588 // off to on, so we have to make sure it's off.
mjr 5:a70c0bce770d 2589 float x, y, z;
mjr 5:a70c0bce770d 2590 mma_.getAccXYZ(x, y, z);
mjr 3:3514575d4f86 2591
mjr 3:3514575d4f86 2592 // calculate the time since the last interrupt
mjr 39:b3815a1c3802 2593 float dt = tInt_.read();
mjr 3:3514575d4f86 2594 tInt_.reset();
mjr 6:cc35eb643e8f 2595
mjr 6:cc35eb643e8f 2596 // integrate the time slice from the previous reading to this reading
mjr 6:cc35eb643e8f 2597 vx_ += (x + ax_ - 2*cx_)*dt/2;
mjr 6:cc35eb643e8f 2598 vy_ += (y + ay_ - 2*cy_)*dt/2;
mjr 3:3514575d4f86 2599
mjr 6:cc35eb643e8f 2600 // store the updates
mjr 6:cc35eb643e8f 2601 ax_ = x;
mjr 6:cc35eb643e8f 2602 ay_ = y;
mjr 6:cc35eb643e8f 2603 az_ = z;
mjr 3:3514575d4f86 2604 }
mjr 3:3514575d4f86 2605
mjr 3:3514575d4f86 2606 // underlying accelerometer object
mjr 3:3514575d4f86 2607 MMA8451Q mma_;
mjr 3:3514575d4f86 2608
mjr 5:a70c0bce770d 2609 // last raw acceleration readings
mjr 6:cc35eb643e8f 2610 float ax_, ay_, az_;
mjr 5:a70c0bce770d 2611
mjr 6:cc35eb643e8f 2612 // integrated velocity reading since last get()
mjr 6:cc35eb643e8f 2613 float vx_, vy_;
mjr 6:cc35eb643e8f 2614
mjr 3:3514575d4f86 2615 // timer for measuring time between get() samples
mjr 3:3514575d4f86 2616 Timer tGet_;
mjr 3:3514575d4f86 2617
mjr 3:3514575d4f86 2618 // timer for measuring time between interrupts
mjr 3:3514575d4f86 2619 Timer tInt_;
mjr 5:a70c0bce770d 2620
mjr 6:cc35eb643e8f 2621 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 2622 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 2623 // at rest.
mjr 6:cc35eb643e8f 2624 float cx_, cy_;
mjr 5:a70c0bce770d 2625
mjr 5:a70c0bce770d 2626 // timer for atuo-centering
mjr 5:a70c0bce770d 2627 Timer tCenter_;
mjr 6:cc35eb643e8f 2628
mjr 6:cc35eb643e8f 2629 // Auto-centering history. This is a separate history list that
mjr 6:cc35eb643e8f 2630 // records results spaced out sparesely over time, so that we can
mjr 6:cc35eb643e8f 2631 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 2632 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 2633 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 2634 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 2635 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 2636 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 2637 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 2638 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 2639 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 2640 // accelerometer measurement bias (e.g., from temperature changes).
mjr 5:a70c0bce770d 2641 int iAccPrv_, nAccPrv_;
mjr 5:a70c0bce770d 2642 static const int maxAccPrv = 5;
mjr 6:cc35eb643e8f 2643 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 2644
mjr 5:a70c0bce770d 2645 // interurupt pin name
mjr 5:a70c0bce770d 2646 PinName irqPin_;
mjr 5:a70c0bce770d 2647
mjr 5:a70c0bce770d 2648 // interrupt router
mjr 5:a70c0bce770d 2649 InterruptIn intIn_;
mjr 3:3514575d4f86 2650 };
mjr 3:3514575d4f86 2651
mjr 5:a70c0bce770d 2652
mjr 5:a70c0bce770d 2653 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 2654 //
mjr 14:df700b22ca08 2655 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 2656 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 2657 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 2658 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 2659 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 2660 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 2661 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 2662 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 2663 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 2664 //
mjr 14:df700b22ca08 2665 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 2666 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 2667 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 2668 //
mjr 5:a70c0bce770d 2669 void clear_i2c()
mjr 5:a70c0bce770d 2670 {
mjr 38:091e511ce8a0 2671 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 2672 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 2673 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 2674
mjr 5:a70c0bce770d 2675 // clock the SCL 9 times
mjr 5:a70c0bce770d 2676 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 2677 {
mjr 5:a70c0bce770d 2678 scl = 1;
mjr 5:a70c0bce770d 2679 wait_us(20);
mjr 5:a70c0bce770d 2680 scl = 0;
mjr 5:a70c0bce770d 2681 wait_us(20);
mjr 5:a70c0bce770d 2682 }
mjr 5:a70c0bce770d 2683 }
mjr 14:df700b22ca08 2684
mjr 14:df700b22ca08 2685 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 2686 //
mjr 33:d832bcab089e 2687 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 2688 // for a given interval before allowing an update.
mjr 33:d832bcab089e 2689 //
mjr 33:d832bcab089e 2690 class Debouncer
mjr 33:d832bcab089e 2691 {
mjr 33:d832bcab089e 2692 public:
mjr 33:d832bcab089e 2693 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 2694 {
mjr 33:d832bcab089e 2695 t.start();
mjr 33:d832bcab089e 2696 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 2697 this->tmin = tmin;
mjr 33:d832bcab089e 2698 }
mjr 33:d832bcab089e 2699
mjr 33:d832bcab089e 2700 // Get the current stable value
mjr 33:d832bcab089e 2701 bool val() const { return stable; }
mjr 33:d832bcab089e 2702
mjr 33:d832bcab089e 2703 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 2704 // input device.
mjr 33:d832bcab089e 2705 void sampleIn(bool val)
mjr 33:d832bcab089e 2706 {
mjr 33:d832bcab089e 2707 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 2708 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 2709 // on the sample reader.
mjr 33:d832bcab089e 2710 if (val != prv)
mjr 33:d832bcab089e 2711 {
mjr 33:d832bcab089e 2712 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 2713 t.reset();
mjr 33:d832bcab089e 2714
mjr 33:d832bcab089e 2715 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 2716 prv = val;
mjr 33:d832bcab089e 2717 }
mjr 33:d832bcab089e 2718 else if (val != stable)
mjr 33:d832bcab089e 2719 {
mjr 33:d832bcab089e 2720 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 2721 // and different from the stable value. This means that
mjr 33:d832bcab089e 2722 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 2723 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 2724 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 2725 if (t.read() > tmin)
mjr 33:d832bcab089e 2726 stable = val;
mjr 33:d832bcab089e 2727 }
mjr 33:d832bcab089e 2728 }
mjr 33:d832bcab089e 2729
mjr 33:d832bcab089e 2730 private:
mjr 33:d832bcab089e 2731 // current stable value
mjr 33:d832bcab089e 2732 bool stable;
mjr 33:d832bcab089e 2733
mjr 33:d832bcab089e 2734 // last raw sample value
mjr 33:d832bcab089e 2735 bool prv;
mjr 33:d832bcab089e 2736
mjr 33:d832bcab089e 2737 // elapsed time since last raw input change
mjr 33:d832bcab089e 2738 Timer t;
mjr 33:d832bcab089e 2739
mjr 33:d832bcab089e 2740 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 2741 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 2742 float tmin;
mjr 33:d832bcab089e 2743 };
mjr 33:d832bcab089e 2744
mjr 33:d832bcab089e 2745
mjr 33:d832bcab089e 2746 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 2747 //
mjr 33:d832bcab089e 2748 // Turn off all outputs and restore everything to the default LedWiz
mjr 33:d832bcab089e 2749 // state. This sets outputs #1-32 to LedWiz profile value 48 (full
mjr 33:d832bcab089e 2750 // brightness) and switch state Off, sets all extended outputs (#33
mjr 33:d832bcab089e 2751 // and above) to zero brightness, and sets the LedWiz flash rate to 2.
mjr 33:d832bcab089e 2752 // This effectively restores the power-on conditions.
mjr 33:d832bcab089e 2753 //
mjr 33:d832bcab089e 2754 void allOutputsOff()
mjr 33:d832bcab089e 2755 {
mjr 33:d832bcab089e 2756 // reset all LedWiz outputs to OFF/48
mjr 35:e959ffba78fd 2757 for (int i = 0 ; i < numLwOutputs ; ++i)
mjr 33:d832bcab089e 2758 {
mjr 33:d832bcab089e 2759 outLevel[i] = 0;
mjr 33:d832bcab089e 2760 wizOn[i] = 0;
mjr 33:d832bcab089e 2761 wizVal[i] = 48;
mjr 33:d832bcab089e 2762 lwPin[i]->set(0);
mjr 33:d832bcab089e 2763 }
mjr 33:d832bcab089e 2764
mjr 33:d832bcab089e 2765 // reset all extended outputs (ports >32) to full off (brightness 0)
mjr 40:cc0d9814522b 2766 for (int i = numLwOutputs ; i < numOutputs ; ++i)
mjr 33:d832bcab089e 2767 {
mjr 33:d832bcab089e 2768 outLevel[i] = 0;
mjr 33:d832bcab089e 2769 lwPin[i]->set(0);
mjr 33:d832bcab089e 2770 }
mjr 33:d832bcab089e 2771
mjr 33:d832bcab089e 2772 // restore default LedWiz flash rate
mjr 33:d832bcab089e 2773 wizSpeed = 2;
mjr 34:6b981a2afab7 2774
mjr 34:6b981a2afab7 2775 // flush changes to hc595, if applicable
mjr 35:e959ffba78fd 2776 if (hc595 != 0)
mjr 35:e959ffba78fd 2777 hc595->update();
mjr 33:d832bcab089e 2778 }
mjr 33:d832bcab089e 2779
mjr 33:d832bcab089e 2780 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 2781 //
mjr 33:d832bcab089e 2782 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 2783 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 2784 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 2785 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 2786 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 2787 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 2788 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 2789 //
mjr 33:d832bcab089e 2790 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 2791 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 2792 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 2793 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 2794 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 2795 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 2796 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 2797 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 2798 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 2799 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 2800 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 2801 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 2802 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 2803 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 2804 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 2805 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 2806 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 2807 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 2808 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 2809 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 2810 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 2811 //
mjr 40:cc0d9814522b 2812 // This scheme might seem a little convoluted, but it handles a number
mjr 40:cc0d9814522b 2813 // of tricky but likely scenarios:
mjr 33:d832bcab089e 2814 //
mjr 33:d832bcab089e 2815 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 40:cc0d9814522b 2816 // so that the PC goes into "Soft Off" mode when the user turns off
mjr 40:cc0d9814522b 2817 // the cabinet by pushing the power button or using the Shut Down
mjr 40:cc0d9814522b 2818 // command from within Windows. In Windows parlance, this "soft off"
mjr 40:cc0d9814522b 2819 // condition is called ACPI State S5. In this state, the main CPU
mjr 40:cc0d9814522b 2820 // power is turned off, but the motherboard still provides power to
mjr 40:cc0d9814522b 2821 // USB devices. This means that the KL25Z keeps running. Without
mjr 40:cc0d9814522b 2822 // the external power sensing circuit, the only hint that we're in
mjr 40:cc0d9814522b 2823 // this state is that the USB connection to the host goes into Suspend
mjr 40:cc0d9814522b 2824 // mode, but that could mean other things as well. The latch circuit
mjr 40:cc0d9814522b 2825 // lets us tell for sure that we're in this state.
mjr 33:d832bcab089e 2826 //
mjr 33:d832bcab089e 2827 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 2828 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 2829 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 2830 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 2831 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 2832 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 2833 // will power up before or after PSU2, so it's not good enough to
mjr 40:cc0d9814522b 2834 // observe the current state of PSU2 when we first check. If PSU2
mjr 40:cc0d9814522b 2835 // were to come on first, checking only the current state would fool
mjr 40:cc0d9814522b 2836 // us into thinking that no action is required, because we'd only see
mjr 40:cc0d9814522b 2837 // that PSU2 is turned on any time we check. The latch handles this
mjr 40:cc0d9814522b 2838 // case by letting us see that PSU2 was indeed off some time before our
mjr 40:cc0d9814522b 2839 // first check.
mjr 33:d832bcab089e 2840 //
mjr 33:d832bcab089e 2841 // - If the KL25Z is rebooted while the main system is running, or the
mjr 40:cc0d9814522b 2842 // KL25Z is unplugged and plugged back in, we'll correctly leave the
mjr 33:d832bcab089e 2843 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 2844 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 2845 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 2846 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 2847 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 2848 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 2849 //
mjr 33:d832bcab089e 2850
mjr 33:d832bcab089e 2851 // Current PSU2 state:
mjr 33:d832bcab089e 2852 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 2853 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 2854 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 2855 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 2856 // 5 -> TV relay on
mjr 33:d832bcab089e 2857 int psu2_state = 1;
mjr 35:e959ffba78fd 2858
mjr 35:e959ffba78fd 2859 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 2860 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 2861 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 2862
mjr 35:e959ffba78fd 2863 // TV ON switch relay control
mjr 35:e959ffba78fd 2864 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 2865
mjr 35:e959ffba78fd 2866 // Timer interrupt
mjr 35:e959ffba78fd 2867 Ticker tv_ticker;
mjr 35:e959ffba78fd 2868 float tv_delay_time;
mjr 33:d832bcab089e 2869 void TVTimerInt()
mjr 33:d832bcab089e 2870 {
mjr 35:e959ffba78fd 2871 // time since last state change
mjr 35:e959ffba78fd 2872 static Timer tv_timer;
mjr 35:e959ffba78fd 2873
mjr 33:d832bcab089e 2874 // Check our internal state
mjr 33:d832bcab089e 2875 switch (psu2_state)
mjr 33:d832bcab089e 2876 {
mjr 33:d832bcab089e 2877 case 1:
mjr 33:d832bcab089e 2878 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 2879 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 2880 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 2881 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 2882 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 2883 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 2884 {
mjr 33:d832bcab089e 2885 // switch to OFF state
mjr 33:d832bcab089e 2886 psu2_state = 2;
mjr 33:d832bcab089e 2887
mjr 33:d832bcab089e 2888 // try setting the latch
mjr 35:e959ffba78fd 2889 psu2_status_set->write(1);
mjr 33:d832bcab089e 2890 }
mjr 33:d832bcab089e 2891 break;
mjr 33:d832bcab089e 2892
mjr 33:d832bcab089e 2893 case 2:
mjr 33:d832bcab089e 2894 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 2895 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 2896 psu2_status_set->write(0);
mjr 33:d832bcab089e 2897 psu2_state = 3;
mjr 33:d832bcab089e 2898 break;
mjr 33:d832bcab089e 2899
mjr 33:d832bcab089e 2900 case 3:
mjr 33:d832bcab089e 2901 // CHECK state: we pulsed SET, and we're now ready to see
mjr 40:cc0d9814522b 2902 // if it stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 2903 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 2904 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 2905 if (psu2_status_sense->read())
mjr 33:d832bcab089e 2906 {
mjr 33:d832bcab089e 2907 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 2908 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 2909 tv_timer.reset();
mjr 33:d832bcab089e 2910 tv_timer.start();
mjr 33:d832bcab089e 2911 psu2_state = 4;
mjr 33:d832bcab089e 2912 }
mjr 33:d832bcab089e 2913 else
mjr 33:d832bcab089e 2914 {
mjr 33:d832bcab089e 2915 // The latch didn't stick, so PSU2 was still off at
mjr 33:d832bcab089e 2916 // our last check. Try pulsing it again in case PSU2
mjr 33:d832bcab089e 2917 // was turned on since the last check.
mjr 35:e959ffba78fd 2918 psu2_status_set->write(1);
mjr 33:d832bcab089e 2919 psu2_state = 2;
mjr 33:d832bcab089e 2920 }
mjr 33:d832bcab089e 2921 break;
mjr 33:d832bcab089e 2922
mjr 33:d832bcab089e 2923 case 4:
mjr 33:d832bcab089e 2924 // TV timer countdown in progress. If we've reached the
mjr 33:d832bcab089e 2925 // delay time, pulse the relay.
mjr 35:e959ffba78fd 2926 if (tv_timer.read() >= tv_delay_time)
mjr 33:d832bcab089e 2927 {
mjr 33:d832bcab089e 2928 // turn on the relay for one timer interval
mjr 35:e959ffba78fd 2929 tv_relay->write(1);
mjr 33:d832bcab089e 2930 psu2_state = 5;
mjr 33:d832bcab089e 2931 }
mjr 33:d832bcab089e 2932 break;
mjr 33:d832bcab089e 2933
mjr 33:d832bcab089e 2934 case 5:
mjr 33:d832bcab089e 2935 // TV timer relay on. We pulse this for one interval, so
mjr 33:d832bcab089e 2936 // it's now time to turn it off and return to the default state.
mjr 35:e959ffba78fd 2937 tv_relay->write(0);
mjr 33:d832bcab089e 2938 psu2_state = 1;
mjr 33:d832bcab089e 2939 break;
mjr 33:d832bcab089e 2940 }
mjr 33:d832bcab089e 2941 }
mjr 33:d832bcab089e 2942
mjr 35:e959ffba78fd 2943 // Start the TV ON checker. If the status sense circuit is enabled in
mjr 35:e959ffba78fd 2944 // the configuration, we'll set up the pin connections and start the
mjr 35:e959ffba78fd 2945 // interrupt handler that periodically checks the status. Does nothing
mjr 35:e959ffba78fd 2946 // if any of the pins are configured as NC.
mjr 35:e959ffba78fd 2947 void startTVTimer(Config &cfg)
mjr 35:e959ffba78fd 2948 {
mjr 55:4db125cd11a0 2949 // only start the timer if the pins are configured and the delay
mjr 55:4db125cd11a0 2950 // time is nonzero
mjr 55:4db125cd11a0 2951 if (cfg.TVON.delayTime != 0
mjr 55:4db125cd11a0 2952 && cfg.TVON.statusPin != 0xFF
mjr 53:9b2611964afc 2953 && cfg.TVON.latchPin != 0xFF
mjr 53:9b2611964afc 2954 && cfg.TVON.relayPin != 0xFF)
mjr 35:e959ffba78fd 2955 {
mjr 53:9b2611964afc 2956 psu2_status_sense = new DigitalIn(wirePinName(cfg.TVON.statusPin));
mjr 53:9b2611964afc 2957 psu2_status_set = new DigitalOut(wirePinName(cfg.TVON.latchPin));
mjr 53:9b2611964afc 2958 tv_relay = new DigitalOut(wirePinName(cfg.TVON.relayPin));
mjr 40:cc0d9814522b 2959 tv_delay_time = cfg.TVON.delayTime/100.0;
mjr 35:e959ffba78fd 2960
mjr 35:e959ffba78fd 2961 // Set up our time routine to run every 1/4 second.
mjr 35:e959ffba78fd 2962 tv_ticker.attach(&TVTimerInt, 0.25);
mjr 35:e959ffba78fd 2963 }
mjr 35:e959ffba78fd 2964 }
mjr 35:e959ffba78fd 2965
mjr 35:e959ffba78fd 2966 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 2967 //
mjr 35:e959ffba78fd 2968 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 2969 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 2970 //
mjr 35:e959ffba78fd 2971 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 2972 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 2973 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 2974 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 2975 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 2976 // again each time the firmware is updated.
mjr 35:e959ffba78fd 2977 //
mjr 35:e959ffba78fd 2978 NVM nvm;
mjr 35:e959ffba78fd 2979
mjr 35:e959ffba78fd 2980 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 2981 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 2982
mjr 35:e959ffba78fd 2983 // flash memory controller interface
mjr 35:e959ffba78fd 2984 FreescaleIAP iap;
mjr 35:e959ffba78fd 2985
mjr 35:e959ffba78fd 2986 // figure the flash address as a pointer along with the number of sectors
mjr 35:e959ffba78fd 2987 // required to store the structure
mjr 35:e959ffba78fd 2988 NVM *configFlashAddr(int &addr, int &numSectors)
mjr 35:e959ffba78fd 2989 {
mjr 35:e959ffba78fd 2990 // figure how many flash sectors we span, rounding up to whole sectors
mjr 35:e959ffba78fd 2991 numSectors = (sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE;
mjr 35:e959ffba78fd 2992
mjr 35:e959ffba78fd 2993 // figure the address - this is the highest flash address where the
mjr 35:e959ffba78fd 2994 // structure will fit with the start aligned on a sector boundary
mjr 35:e959ffba78fd 2995 addr = iap.flash_size() - (numSectors * SECTOR_SIZE);
mjr 35:e959ffba78fd 2996
mjr 35:e959ffba78fd 2997 // return the address as a pointer
mjr 35:e959ffba78fd 2998 return (NVM *)addr;
mjr 35:e959ffba78fd 2999 }
mjr 35:e959ffba78fd 3000
mjr 35:e959ffba78fd 3001 // figure the flash address as a pointer
mjr 35:e959ffba78fd 3002 NVM *configFlashAddr()
mjr 35:e959ffba78fd 3003 {
mjr 35:e959ffba78fd 3004 int addr, numSectors;
mjr 35:e959ffba78fd 3005 return configFlashAddr(addr, numSectors);
mjr 35:e959ffba78fd 3006 }
mjr 35:e959ffba78fd 3007
mjr 35:e959ffba78fd 3008 // Load the config from flash
mjr 35:e959ffba78fd 3009 void loadConfigFromFlash()
mjr 35:e959ffba78fd 3010 {
mjr 35:e959ffba78fd 3011 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 3012 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 3013 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 3014 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 3015 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 3016 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 3017 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 3018 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 3019 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 3020 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 3021 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 3022 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 3023 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 3024 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 3025 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 3026 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 3027
mjr 35:e959ffba78fd 3028 // Figure how many sectors we need for our structure
mjr 35:e959ffba78fd 3029 NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 3030
mjr 35:e959ffba78fd 3031 // if the flash is valid, load it; otherwise initialize to defaults
mjr 35:e959ffba78fd 3032 if (flash->valid())
mjr 35:e959ffba78fd 3033 {
mjr 35:e959ffba78fd 3034 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 3035 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 3036 }
mjr 35:e959ffba78fd 3037 else
mjr 35:e959ffba78fd 3038 {
mjr 35:e959ffba78fd 3039 // flash is invalid - load factory settings nito RAM structure
mjr 35:e959ffba78fd 3040 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 3041 }
mjr 35:e959ffba78fd 3042 }
mjr 35:e959ffba78fd 3043
mjr 35:e959ffba78fd 3044 void saveConfigToFlash()
mjr 33:d832bcab089e 3045 {
mjr 35:e959ffba78fd 3046 int addr, sectors;
mjr 35:e959ffba78fd 3047 configFlashAddr(addr, sectors);
mjr 35:e959ffba78fd 3048 nvm.save(iap, addr);
mjr 35:e959ffba78fd 3049 }
mjr 35:e959ffba78fd 3050
mjr 35:e959ffba78fd 3051 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 3052 //
mjr 55:4db125cd11a0 3053 // Pixel dump mode - the host requested a dump of image sensor pixels
mjr 55:4db125cd11a0 3054 // (helpful for installing and setting up the sensor and light source)
mjr 55:4db125cd11a0 3055 //
mjr 55:4db125cd11a0 3056 bool reportPlungerStat = false;
mjr 55:4db125cd11a0 3057 uint8_t reportPlungerStatFlags; // plunger pixel report flag bits (see ccdSensor.h)
mjr 55:4db125cd11a0 3058 uint8_t reportPlungerStatTime; // extra exposure time for plunger pixel report
mjr 55:4db125cd11a0 3059
mjr 55:4db125cd11a0 3060
mjr 55:4db125cd11a0 3061
mjr 55:4db125cd11a0 3062 // ---------------------------------------------------------------------------
mjr 55:4db125cd11a0 3063 //
mjr 40:cc0d9814522b 3064 // Night mode setting updates
mjr 40:cc0d9814522b 3065 //
mjr 38:091e511ce8a0 3066
mjr 38:091e511ce8a0 3067 // Turn night mode on or off
mjr 38:091e511ce8a0 3068 static void setNightMode(bool on)
mjr 38:091e511ce8a0 3069 {
mjr 40:cc0d9814522b 3070 // set the new night mode flag in the noisy output class
mjr 53:9b2611964afc 3071 nightMode = on;
mjr 55:4db125cd11a0 3072
mjr 40:cc0d9814522b 3073 // update the special output pin that shows the night mode state
mjr 53:9b2611964afc 3074 int port = int(cfg.nightMode.port) - 1;
mjr 53:9b2611964afc 3075 if (port >= 0 && port < numOutputs)
mjr 53:9b2611964afc 3076 lwPin[port]->set(nightMode ? 255 : 0);
mjr 40:cc0d9814522b 3077
mjr 40:cc0d9814522b 3078 // update all outputs for the mode change
mjr 40:cc0d9814522b 3079 updateAllOuts();
mjr 38:091e511ce8a0 3080 }
mjr 38:091e511ce8a0 3081
mjr 38:091e511ce8a0 3082 // Toggle night mode
mjr 38:091e511ce8a0 3083 static void toggleNightMode()
mjr 38:091e511ce8a0 3084 {
mjr 53:9b2611964afc 3085 setNightMode(!nightMode);
mjr 38:091e511ce8a0 3086 }
mjr 38:091e511ce8a0 3087
mjr 38:091e511ce8a0 3088
mjr 38:091e511ce8a0 3089 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 3090 //
mjr 35:e959ffba78fd 3091 // Plunger Sensor
mjr 35:e959ffba78fd 3092 //
mjr 35:e959ffba78fd 3093
mjr 35:e959ffba78fd 3094 // the plunger sensor interface object
mjr 35:e959ffba78fd 3095 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 3096
mjr 35:e959ffba78fd 3097 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 3098 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 3099 void createPlunger()
mjr 35:e959ffba78fd 3100 {
mjr 35:e959ffba78fd 3101 // create the new sensor object according to the type
mjr 35:e959ffba78fd 3102 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 3103 {
mjr 35:e959ffba78fd 3104 case PlungerType_TSL1410RS:
mjr 35:e959ffba78fd 3105 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 3106 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 3107 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 3108 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 3109 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 3110 NC);
mjr 35:e959ffba78fd 3111 break;
mjr 35:e959ffba78fd 3112
mjr 35:e959ffba78fd 3113 case PlungerType_TSL1410RP:
mjr 35:e959ffba78fd 3114 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 3115 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 3116 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 3117 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 3118 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 3119 wirePinName(cfg.plunger.sensorPin[3]));
mjr 35:e959ffba78fd 3120 break;
mjr 35:e959ffba78fd 3121
mjr 35:e959ffba78fd 3122 case PlungerType_TSL1412RS:
mjr 35:e959ffba78fd 3123 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 3124 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 3125 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 3126 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 3127 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 3128 NC);
mjr 35:e959ffba78fd 3129 break;
mjr 35:e959ffba78fd 3130
mjr 35:e959ffba78fd 3131 case PlungerType_TSL1412RP:
mjr 35:e959ffba78fd 3132 // pins are: SI, CLOCK, AO1, AO2
mjr 53:9b2611964afc 3133 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 3134 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 3135 wirePinName(cfg.plunger.sensorPin[1]),
mjr 53:9b2611964afc 3136 wirePinName(cfg.plunger.sensorPin[2]),
mjr 53:9b2611964afc 3137 wirePinName(cfg.plunger.sensorPin[3]));
mjr 35:e959ffba78fd 3138 break;
mjr 35:e959ffba78fd 3139
mjr 35:e959ffba78fd 3140 case PlungerType_Pot:
mjr 35:e959ffba78fd 3141 // pins are: AO
mjr 53:9b2611964afc 3142 plungerSensor = new PlungerSensorPot(
mjr 53:9b2611964afc 3143 wirePinName(cfg.plunger.sensorPin[0]));
mjr 35:e959ffba78fd 3144 break;
mjr 35:e959ffba78fd 3145
mjr 35:e959ffba78fd 3146 case PlungerType_None:
mjr 35:e959ffba78fd 3147 default:
mjr 35:e959ffba78fd 3148 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 3149 break;
mjr 35:e959ffba78fd 3150 }
mjr 33:d832bcab089e 3151 }
mjr 33:d832bcab089e 3152
mjr 52:8298b2a73eb2 3153 // Global plunger calibration mode flag
mjr 52:8298b2a73eb2 3154 bool plungerCalMode;
mjr 52:8298b2a73eb2 3155
mjr 48:058ace2aed1d 3156 // Plunger reader
mjr 51:57eb311faafa 3157 //
mjr 51:57eb311faafa 3158 // This class encapsulates our plunger data processing. At the simplest
mjr 51:57eb311faafa 3159 // level, we read the position from the sensor, adjust it for the
mjr 51:57eb311faafa 3160 // calibration settings, and report the calibrated position to the host.
mjr 51:57eb311faafa 3161 //
mjr 51:57eb311faafa 3162 // In addition, we constantly monitor the data for "firing" motions.
mjr 51:57eb311faafa 3163 // A firing motion is when the user pulls back the plunger and releases
mjr 51:57eb311faafa 3164 // it, allowing it to shoot forward under the force of the main spring.
mjr 51:57eb311faafa 3165 // When we detect that this is happening, we briefly stop reporting the
mjr 51:57eb311faafa 3166 // real physical position that we're reading from the sensor, and instead
mjr 51:57eb311faafa 3167 // report a synthetic series of positions that depicts an idealized
mjr 51:57eb311faafa 3168 // firing motion.
mjr 51:57eb311faafa 3169 //
mjr 51:57eb311faafa 3170 // The point of the synthetic reports is to correct for distortions
mjr 51:57eb311faafa 3171 // created by the joystick interface conventions used by VP and other
mjr 51:57eb311faafa 3172 // PC pinball emulators. The convention they use is simply to have the
mjr 51:57eb311faafa 3173 // plunger device report the instantaneous position of the real plunger.
mjr 51:57eb311faafa 3174 // The PC software polls this reported position periodically, and moves
mjr 51:57eb311faafa 3175 // the on-screen virtual plunger in sync with the real plunger. This
mjr 51:57eb311faafa 3176 // works fine for human-scale motion when the user is manually moving
mjr 51:57eb311faafa 3177 // the plunger. But it doesn't work for the high speed motion of a
mjr 51:57eb311faafa 3178 // release. The plunger simply moves too fast. VP polls in about 10ms
mjr 51:57eb311faafa 3179 // intervals; the plunger takes about 50ms to travel from fully
mjr 51:57eb311faafa 3180 // retracted to the park position when released. The low sampling
mjr 51:57eb311faafa 3181 // rate relative to the rate of change of the sampled data creates
mjr 51:57eb311faafa 3182 // a classic digital aliasing effect.
mjr 51:57eb311faafa 3183 //
mjr 51:57eb311faafa 3184 // The synthetic reporting scheme compensates for the interface
mjr 51:57eb311faafa 3185 // distortions by essentially changing to a coarse enough timescale
mjr 51:57eb311faafa 3186 // that VP can reliably interpret the readings. Conceptually, there
mjr 51:57eb311faafa 3187 // are three steps involved in doing this. First, we analyze the
mjr 51:57eb311faafa 3188 // actual sensor data to detect and characterize the release motion.
mjr 51:57eb311faafa 3189 // Second, once we think we have a release in progress, we fit the
mjr 51:57eb311faafa 3190 // data to a mathematical model of the release. The model we use is
mjr 51:57eb311faafa 3191 // dead simple: we consider the release to have one parameter, namely
mjr 51:57eb311faafa 3192 // the retraction distance at the moment the user lets go. This is an
mjr 51:57eb311faafa 3193 // excellent proxy in the real physical system for the final speed
mjr 51:57eb311faafa 3194 // when the plunger hits the ball, and it also happens to match how
mjr 51:57eb311faafa 3195 // VP models it internally. Third, we construct synthetic reports
mjr 51:57eb311faafa 3196 // that will make VP's internal state match our model. This is also
mjr 51:57eb311faafa 3197 // pretty simple: we just need to send VP the maximum retraction
mjr 51:57eb311faafa 3198 // distance for long enough to be sure that it polls it at least
mjr 51:57eb311faafa 3199 // once, and then send it the park position for long enough to
mjr 51:57eb311faafa 3200 // ensure that VP will complete the same firing motion. The
mjr 51:57eb311faafa 3201 // immediate jump from the maximum point to the zero point will
mjr 51:57eb311faafa 3202 // cause VP to move its simulation model plunger forward from the
mjr 51:57eb311faafa 3203 // starting point at its natural spring acceleration rate, which
mjr 51:57eb311faafa 3204 // is exactly what the real plunger just did.
mjr 51:57eb311faafa 3205 //
mjr 48:058ace2aed1d 3206 class PlungerReader
mjr 48:058ace2aed1d 3207 {
mjr 48:058ace2aed1d 3208 public:
mjr 48:058ace2aed1d 3209 PlungerReader()
mjr 48:058ace2aed1d 3210 {
mjr 48:058ace2aed1d 3211 // not in a firing event yet
mjr 48:058ace2aed1d 3212 firing = 0;
mjr 48:058ace2aed1d 3213
mjr 48:058ace2aed1d 3214 // no history yet
mjr 48:058ace2aed1d 3215 histIdx = 0;
mjr 55:4db125cd11a0 3216
mjr 55:4db125cd11a0 3217 // initialize the filter
mjr 55:4db125cd11a0 3218 initFilter();
mjr 48:058ace2aed1d 3219 }
mjr 48:058ace2aed1d 3220
mjr 48:058ace2aed1d 3221 // Collect a reading from the plunger sensor. The main loop calls
mjr 48:058ace2aed1d 3222 // this frequently to read the current raw position data from the
mjr 48:058ace2aed1d 3223 // sensor. We analyze the raw data to produce the calibrated
mjr 48:058ace2aed1d 3224 // position that we report to the PC via the joystick interface.
mjr 48:058ace2aed1d 3225 void read()
mjr 48:058ace2aed1d 3226 {
mjr 48:058ace2aed1d 3227 // Read a sample from the sensor
mjr 48:058ace2aed1d 3228 PlungerReading r;
mjr 48:058ace2aed1d 3229 if (plungerSensor->read(r))
mjr 48:058ace2aed1d 3230 {
mjr 51:57eb311faafa 3231 // Pull the previous reading from the history
mjr 50:40015764bbe6 3232 const PlungerReading &prv = nthHist(0);
mjr 48:058ace2aed1d 3233
mjr 48:058ace2aed1d 3234 // If the new reading is within 2ms of the previous reading,
mjr 48:058ace2aed1d 3235 // ignore it. We require a minimum time between samples to
mjr 48:058ace2aed1d 3236 // ensure that we have a usable amount of precision in the
mjr 48:058ace2aed1d 3237 // denominator (the time interval) for calculating the plunger
mjr 48:058ace2aed1d 3238 // velocity. (The CCD sensor can't take readings faster than
mjr 48:058ace2aed1d 3239 // this anyway, but other sensor types, such as potentiometers,
mjr 48:058ace2aed1d 3240 // can, so we have to throttle the rate artifically in case
mjr 48:058ace2aed1d 3241 // we're using a fast sensor like that.)
mjr 48:058ace2aed1d 3242 if (uint32_t(r.t - prv.t) < 2000UL)
mjr 48:058ace2aed1d 3243 return;
mjr 53:9b2611964afc 3244
mjr 53:9b2611964afc 3245 // check for calibration mode
mjr 53:9b2611964afc 3246 if (plungerCalMode)
mjr 53:9b2611964afc 3247 {
mjr 53:9b2611964afc 3248 // Calibration mode. Adjust the calibration bounds to fit
mjr 53:9b2611964afc 3249 // the value. If this value is beyond the current min or max,
mjr 53:9b2611964afc 3250 // expand the envelope to include this new value.
mjr 53:9b2611964afc 3251 if (r.pos > cfg.plunger.cal.max)
mjr 53:9b2611964afc 3252 cfg.plunger.cal.max = r.pos;
mjr 53:9b2611964afc 3253 if (r.pos < cfg.plunger.cal.min)
mjr 53:9b2611964afc 3254 cfg.plunger.cal.min = r.pos;
mjr 50:40015764bbe6 3255
mjr 53:9b2611964afc 3256 // If we're in calibration state 0, we're waiting for the
mjr 53:9b2611964afc 3257 // plunger to come to rest at the park position so that we
mjr 53:9b2611964afc 3258 // can take a sample of the park position. Check to see if
mjr 53:9b2611964afc 3259 // we've been at rest for a minimum interval.
mjr 53:9b2611964afc 3260 if (calState == 0)
mjr 53:9b2611964afc 3261 {
mjr 53:9b2611964afc 3262 if (abs(r.pos - calZeroStart.pos) < 65535/3/50)
mjr 53:9b2611964afc 3263 {
mjr 53:9b2611964afc 3264 // we're close enough - make sure we've been here long enough
mjr 53:9b2611964afc 3265 if (uint32_t(r.t - calZeroStart.t) > 100000UL)
mjr 53:9b2611964afc 3266 {
mjr 53:9b2611964afc 3267 // we've been at rest long enough - count it
mjr 53:9b2611964afc 3268 calZeroPosSum += r.pos;
mjr 53:9b2611964afc 3269 calZeroPosN += 1;
mjr 53:9b2611964afc 3270
mjr 53:9b2611964afc 3271 // update the zero position from the new average
mjr 53:9b2611964afc 3272 cfg.plunger.cal.zero = uint16_t(calZeroPosSum / calZeroPosN);
mjr 53:9b2611964afc 3273
mjr 53:9b2611964afc 3274 // switch to calibration state 1 - at rest
mjr 53:9b2611964afc 3275 calState = 1;
mjr 53:9b2611964afc 3276 }
mjr 53:9b2611964afc 3277 }
mjr 53:9b2611964afc 3278 else
mjr 53:9b2611964afc 3279 {
mjr 53:9b2611964afc 3280 // we're not close to the last position - start again here
mjr 53:9b2611964afc 3281 calZeroStart = r;
mjr 53:9b2611964afc 3282 }
mjr 53:9b2611964afc 3283 }
mjr 53:9b2611964afc 3284
mjr 53:9b2611964afc 3285 // Rescale to the joystick range, and adjust for the current
mjr 53:9b2611964afc 3286 // park position, but don't calibrate. We don't know the maximum
mjr 53:9b2611964afc 3287 // point yet, so we can't calibrate the range.
mjr 53:9b2611964afc 3288 r.pos = int(
mjr 53:9b2611964afc 3289 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 3290 / (65535 - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 3291 }
mjr 53:9b2611964afc 3292 else
mjr 53:9b2611964afc 3293 {
mjr 53:9b2611964afc 3294 // Not in calibration mode. Apply the existing calibration and
mjr 53:9b2611964afc 3295 // rescale to the joystick range.
mjr 53:9b2611964afc 3296 r.pos = int(
mjr 53:9b2611964afc 3297 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 3298 / (cfg.plunger.cal.max - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 3299
mjr 53:9b2611964afc 3300 // limit the result to the valid joystick range
mjr 53:9b2611964afc 3301 if (r.pos > JOYMAX)
mjr 53:9b2611964afc 3302 r.pos = JOYMAX;
mjr 53:9b2611964afc 3303 else if (r.pos < -JOYMAX)
mjr 53:9b2611964afc 3304 r.pos = -JOYMAX;
mjr 53:9b2611964afc 3305 }
mjr 50:40015764bbe6 3306
mjr 50:40015764bbe6 3307 // Calculate the velocity from the second-to-last reading
mjr 50:40015764bbe6 3308 // to here, in joystick distance units per microsecond.
mjr 50:40015764bbe6 3309 // Note that we use the second-to-last reading rather than
mjr 50:40015764bbe6 3310 // the very last reading to give ourselves a little longer
mjr 50:40015764bbe6 3311 // time base. The time base is so short between consecutive
mjr 50:40015764bbe6 3312 // readings that the error bars in the position would be too
mjr 50:40015764bbe6 3313 // large.
mjr 50:40015764bbe6 3314 //
mjr 50:40015764bbe6 3315 // For reference, the physical plunger velocity ranges up
mjr 50:40015764bbe6 3316 // to about 100,000 joystick distance units/sec. This is
mjr 50:40015764bbe6 3317 // based on empirical measurements. The typical time for
mjr 50:40015764bbe6 3318 // a real plunger to travel the full distance when released
mjr 50:40015764bbe6 3319 // from full retraction is about 85ms, so the average velocity
mjr 50:40015764bbe6 3320 // covering this distance is about 56,000 units/sec. The
mjr 50:40015764bbe6 3321 // peak is probably about twice that. In real-world units,
mjr 50:40015764bbe6 3322 // this translates to an average speed of about .75 m/s and
mjr 50:40015764bbe6 3323 // a peak of about 1.5 m/s.
mjr 50:40015764bbe6 3324 //
mjr 50:40015764bbe6 3325 // Note that we actually calculate the value here in units
mjr 50:40015764bbe6 3326 // per *microsecond* - the discussion above is in terms of
mjr 50:40015764bbe6 3327 // units/sec because that's more on a human scale. Our
mjr 50:40015764bbe6 3328 // choice of internal units here really isn't important,
mjr 50:40015764bbe6 3329 // since we only use the velocity for comparison purposes,
mjr 50:40015764bbe6 3330 // to detect acceleration trends. We therefore save ourselves
mjr 50:40015764bbe6 3331 // a little CPU time by using the natural units of our inputs.
mjr 51:57eb311faafa 3332 const PlungerReading &prv2 = nthHist(1);
mjr 50:40015764bbe6 3333 float v = float(r.pos - prv2.pos)/float(r.t - prv2.t);
mjr 50:40015764bbe6 3334
mjr 50:40015764bbe6 3335 // presume we'll report the latest instantaneous reading
mjr 50:40015764bbe6 3336 z = r.pos;
mjr 50:40015764bbe6 3337 vz = v;
mjr 48:058ace2aed1d 3338
mjr 50:40015764bbe6 3339 // Check firing events
mjr 50:40015764bbe6 3340 switch (firing)
mjr 50:40015764bbe6 3341 {
mjr 50:40015764bbe6 3342 case 0:
mjr 50:40015764bbe6 3343 // Default state - not in a firing event.
mjr 50:40015764bbe6 3344
mjr 50:40015764bbe6 3345 // If we have forward motion from a position that's retracted
mjr 50:40015764bbe6 3346 // beyond a threshold, enter phase 1. If we're not pulled back
mjr 50:40015764bbe6 3347 // far enough, don't bother with this, as a release wouldn't
mjr 50:40015764bbe6 3348 // be strong enough to require the synthetic firing treatment.
mjr 50:40015764bbe6 3349 if (v < 0 && r.pos > JOYMAX/6)
mjr 50:40015764bbe6 3350 {
mjr 53:9b2611964afc 3351 // enter firing phase 1
mjr 50:40015764bbe6 3352 firingMode(1);
mjr 50:40015764bbe6 3353
mjr 53:9b2611964afc 3354 // if in calibration state 1 (at rest), switch to state 2 (not
mjr 53:9b2611964afc 3355 // at rest)
mjr 53:9b2611964afc 3356 if (calState == 1)
mjr 53:9b2611964afc 3357 calState = 2;
mjr 53:9b2611964afc 3358
mjr 50:40015764bbe6 3359 // we don't have a freeze position yet, but note the start time
mjr 50:40015764bbe6 3360 f1.pos = 0;
mjr 50:40015764bbe6 3361 f1.t = r.t;
mjr 50:40015764bbe6 3362
mjr 50:40015764bbe6 3363 // Figure the barrel spring "bounce" position in case we complete
mjr 50:40015764bbe6 3364 // the firing event. This is the amount that the forward momentum
mjr 50:40015764bbe6 3365 // of the plunger will compress the barrel spring at the peak of
mjr 50:40015764bbe6 3366 // the forward travel during the release. Assume that this is
mjr 50:40015764bbe6 3367 // linearly proportional to the starting retraction distance.
mjr 50:40015764bbe6 3368 // The barrel spring is about 1/6 the length of the main spring,
mjr 50:40015764bbe6 3369 // so figure it compresses by 1/6 the distance. (This is overly
mjr 53:9b2611964afc 3370 // simplistic and not very accurate, but it seems to give good
mjr 50:40015764bbe6 3371 // visual results, and that's all it's for.)
mjr 50:40015764bbe6 3372 f2.pos = -r.pos/6;
mjr 50:40015764bbe6 3373 }
mjr 50:40015764bbe6 3374 break;
mjr 50:40015764bbe6 3375
mjr 50:40015764bbe6 3376 case 1:
mjr 50:40015764bbe6 3377 // Phase 1 - acceleration. If we cross the zero point, trigger
mjr 50:40015764bbe6 3378 // the firing event. Otherwise, continue monitoring as long as we
mjr 50:40015764bbe6 3379 // see acceleration in the forward direction.
mjr 50:40015764bbe6 3380 if (r.pos <= 0)
mjr 50:40015764bbe6 3381 {
mjr 50:40015764bbe6 3382 // switch to the synthetic firing mode
mjr 50:40015764bbe6 3383 firingMode(2);
mjr 50:40015764bbe6 3384 z = f2.pos;
mjr 50:40015764bbe6 3385
mjr 50:40015764bbe6 3386 // note the start time for the firing phase
mjr 50:40015764bbe6 3387 f2.t = r.t;
mjr 53:9b2611964afc 3388
mjr 53:9b2611964afc 3389 // if in calibration mode, and we're in state 2 (moving),
mjr 53:9b2611964afc 3390 // collect firing statistics for calibration purposes
mjr 53:9b2611964afc 3391 if (plungerCalMode && calState == 2)
mjr 53:9b2611964afc 3392 {
mjr 53:9b2611964afc 3393 // collect a new zero point for the average when we
mjr 53:9b2611964afc 3394 // come to rest
mjr 53:9b2611964afc 3395 calState = 0;
mjr 53:9b2611964afc 3396
mjr 53:9b2611964afc 3397 // collect average firing time statistics in millseconds, if
mjr 53:9b2611964afc 3398 // it's in range (20 to 255 ms)
mjr 53:9b2611964afc 3399 int dt = uint32_t(r.t - f1.t)/1000UL;
mjr 53:9b2611964afc 3400 if (dt >= 20 && dt <= 255)
mjr 53:9b2611964afc 3401 {
mjr 53:9b2611964afc 3402 calRlsTimeSum += dt;
mjr 53:9b2611964afc 3403 calRlsTimeN += 1;
mjr 53:9b2611964afc 3404 cfg.plunger.cal.tRelease = uint8_t(calRlsTimeSum / calRlsTimeN);
mjr 53:9b2611964afc 3405 }
mjr 53:9b2611964afc 3406 }
mjr 50:40015764bbe6 3407 }
mjr 50:40015764bbe6 3408 else if (v < vprv2)
mjr 50:40015764bbe6 3409 {
mjr 50:40015764bbe6 3410 // We're still accelerating, and we haven't crossed the zero
mjr 50:40015764bbe6 3411 // point yet - stay in phase 1. (Note that forward motion is
mjr 50:40015764bbe6 3412 // negative velocity, so accelerating means that the new
mjr 50:40015764bbe6 3413 // velocity is more negative than the previous one, which
mjr 50:40015764bbe6 3414 // is to say numerically less than - that's why the test
mjr 50:40015764bbe6 3415 // for acceleration is the seemingly backwards 'v < vprv'.)
mjr 50:40015764bbe6 3416
mjr 50:40015764bbe6 3417 // If we've been accelerating for at least 20ms, we're probably
mjr 50:40015764bbe6 3418 // really doing a release. Jump back to the recent local
mjr 50:40015764bbe6 3419 // maximum where the release *really* started. This is always
mjr 50:40015764bbe6 3420 // a bit before we started seeing sustained accleration, because
mjr 50:40015764bbe6 3421 // the plunger motion for the first few milliseconds is too slow
mjr 50:40015764bbe6 3422 // for our sensor precision to reliably detect acceleration.
mjr 50:40015764bbe6 3423 if (f1.pos != 0)
mjr 50:40015764bbe6 3424 {
mjr 50:40015764bbe6 3425 // we have a reset point - freeze there
mjr 50:40015764bbe6 3426 z = f1.pos;
mjr 50:40015764bbe6 3427 }
mjr 50:40015764bbe6 3428 else if (uint32_t(r.t - f1.t) >= 20000UL)
mjr 50:40015764bbe6 3429 {
mjr 50:40015764bbe6 3430 // it's been long enough - set a reset point.
mjr 50:40015764bbe6 3431 f1.pos = z = histLocalMax(r.t, 50000UL);
mjr 50:40015764bbe6 3432 }
mjr 50:40015764bbe6 3433 }
mjr 50:40015764bbe6 3434 else
mjr 50:40015764bbe6 3435 {
mjr 50:40015764bbe6 3436 // We're not accelerating. Cancel the firing event.
mjr 50:40015764bbe6 3437 firingMode(0);
mjr 53:9b2611964afc 3438 calState = 1;
mjr 50:40015764bbe6 3439 }
mjr 50:40015764bbe6 3440 break;
mjr 50:40015764bbe6 3441
mjr 50:40015764bbe6 3442 case 2:
mjr 50:40015764bbe6 3443 // Phase 2 - start of synthetic firing event. Report the fake
mjr 50:40015764bbe6 3444 // bounce for 25ms. VP polls the joystick about every 10ms, so
mjr 50:40015764bbe6 3445 // this should be enough time to guarantee that VP sees this
mjr 50:40015764bbe6 3446 // report at least once.
mjr 50:40015764bbe6 3447 if (uint32_t(r.t - f2.t) < 25000UL)
mjr 50:40015764bbe6 3448 {
mjr 50:40015764bbe6 3449 // report the bounce position
mjr 50:40015764bbe6 3450 z = f2.pos;
mjr 50:40015764bbe6 3451 }
mjr 50:40015764bbe6 3452 else
mjr 50:40015764bbe6 3453 {
mjr 50:40015764bbe6 3454 // it's been long enough - switch to phase 3, where we
mjr 50:40015764bbe6 3455 // report the park position until the real plunger comes
mjr 50:40015764bbe6 3456 // to rest
mjr 50:40015764bbe6 3457 firingMode(3);
mjr 50:40015764bbe6 3458 z = 0;
mjr 50:40015764bbe6 3459
mjr 50:40015764bbe6 3460 // set the start of the "stability window" to the rest position
mjr 50:40015764bbe6 3461 f3s.t = r.t;
mjr 50:40015764bbe6 3462 f3s.pos = 0;
mjr 50:40015764bbe6 3463
mjr 50:40015764bbe6 3464 // set the start of the "retraction window" to the actual position
mjr 50:40015764bbe6 3465 f3r = r;
mjr 50:40015764bbe6 3466 }
mjr 50:40015764bbe6 3467 break;
mjr 50:40015764bbe6 3468
mjr 50:40015764bbe6 3469 case 3:
mjr 50:40015764bbe6 3470 // Phase 3 - in synthetic firing event. Report the park position
mjr 50:40015764bbe6 3471 // until the plunger position stabilizes. Left to its own devices,
mjr 50:40015764bbe6 3472 // the plunger will usualy bounce off the barrel spring several
mjr 50:40015764bbe6 3473 // times before coming to rest, so we'll see oscillating motion
mjr 50:40015764bbe6 3474 // for a second or two. In the simplest case, we can aimply wait
mjr 50:40015764bbe6 3475 // for the plunger to stop moving for a short time. However, the
mjr 50:40015764bbe6 3476 // player might intervene by pulling the plunger back again, so
mjr 50:40015764bbe6 3477 // watch for that motion as well. If we're just bouncing freely,
mjr 50:40015764bbe6 3478 // we'll see the direction change frequently. If the player is
mjr 50:40015764bbe6 3479 // moving the plunger manually, the direction will be constant
mjr 50:40015764bbe6 3480 // for longer.
mjr 50:40015764bbe6 3481 if (v >= 0)
mjr 50:40015764bbe6 3482 {
mjr 50:40015764bbe6 3483 // We're moving back (or standing still). If this has been
mjr 50:40015764bbe6 3484 // going on for a while, the user must have taken control.
mjr 50:40015764bbe6 3485 if (uint32_t(r.t - f3r.t) > 65000UL)
mjr 50:40015764bbe6 3486 {
mjr 50:40015764bbe6 3487 // user has taken control - cancel firing mode
mjr 50:40015764bbe6 3488 firingMode(0);
mjr 50:40015764bbe6 3489 break;
mjr 50:40015764bbe6 3490 }
mjr 50:40015764bbe6 3491 }
mjr 50:40015764bbe6 3492 else
mjr 50:40015764bbe6 3493 {
mjr 50:40015764bbe6 3494 // forward motion - reset retraction window
mjr 50:40015764bbe6 3495 f3r.t = r.t;
mjr 50:40015764bbe6 3496 }
mjr 50:40015764bbe6 3497
mjr 53:9b2611964afc 3498 // Check if we're close to the last starting point. The joystick
mjr 53:9b2611964afc 3499 // positive axis range (0..4096) covers the retraction distance of
mjr 53:9b2611964afc 3500 // about 2.5", so 1" is about 1638 joystick units, hence 1/16" is
mjr 53:9b2611964afc 3501 // about 100 units.
mjr 53:9b2611964afc 3502 if (abs(r.pos - f3s.pos) < 100)
mjr 50:40015764bbe6 3503 {
mjr 53:9b2611964afc 3504 // It's at roughly the same position as the starting point.
mjr 53:9b2611964afc 3505 // Consider it stable if this has been true for 300ms.
mjr 50:40015764bbe6 3506 if (uint32_t(r.t - f3s.t) > 30000UL)
mjr 50:40015764bbe6 3507 {
mjr 50:40015764bbe6 3508 // we're done with the firing event
mjr 50:40015764bbe6 3509 firingMode(0);
mjr 50:40015764bbe6 3510 }
mjr 50:40015764bbe6 3511 else
mjr 50:40015764bbe6 3512 {
mjr 50:40015764bbe6 3513 // it's close to the last position but hasn't been
mjr 50:40015764bbe6 3514 // here long enough; stay in firing mode and continue
mjr 50:40015764bbe6 3515 // to report the park position
mjr 50:40015764bbe6 3516 z = 0;
mjr 50:40015764bbe6 3517 }
mjr 50:40015764bbe6 3518 }
mjr 50:40015764bbe6 3519 else
mjr 50:40015764bbe6 3520 {
mjr 50:40015764bbe6 3521 // It's not close enough to the last starting point, so use
mjr 50:40015764bbe6 3522 // this as a new starting point, and stay in firing mode.
mjr 50:40015764bbe6 3523 f3s = r;
mjr 50:40015764bbe6 3524 z = 0;
mjr 50:40015764bbe6 3525 }
mjr 50:40015764bbe6 3526 break;
mjr 50:40015764bbe6 3527 }
mjr 50:40015764bbe6 3528
mjr 50:40015764bbe6 3529 // save the velocity reading for next time
mjr 50:40015764bbe6 3530 vprv2 = vprv;
mjr 50:40015764bbe6 3531 vprv = v;
mjr 50:40015764bbe6 3532
mjr 50:40015764bbe6 3533 // add the new reading to the history
mjr 50:40015764bbe6 3534 hist[histIdx++] = r;
mjr 50:40015764bbe6 3535 histIdx %= countof(hist);
mjr 58:523fdcffbe6d 3536
mjr 58:523fdcffbe6d 3537 // figure the filtered value
mjr 58:523fdcffbe6d 3538 zf = applyFilter();
mjr 48:058ace2aed1d 3539 }
mjr 48:058ace2aed1d 3540 }
mjr 48:058ace2aed1d 3541
mjr 48:058ace2aed1d 3542 // Get the current value to report through the joystick interface
mjr 58:523fdcffbe6d 3543 int16_t getPosition()
mjr 58:523fdcffbe6d 3544 {
mjr 58:523fdcffbe6d 3545 // return the last filtered reading
mjr 58:523fdcffbe6d 3546 return zf;
mjr 55:4db125cd11a0 3547 }
mjr 58:523fdcffbe6d 3548
mjr 48:058ace2aed1d 3549 // Get the current velocity (joystick distance units per microsecond)
mjr 48:058ace2aed1d 3550 float getVelocity() const { return vz; }
mjr 48:058ace2aed1d 3551
mjr 48:058ace2aed1d 3552 // get the timestamp of the current joystick report (microseconds)
mjr 50:40015764bbe6 3553 uint32_t getTimestamp() const { return nthHist(0).t; }
mjr 48:058ace2aed1d 3554
mjr 48:058ace2aed1d 3555 // Set calibration mode on or off
mjr 52:8298b2a73eb2 3556 void setCalMode(bool f)
mjr 48:058ace2aed1d 3557 {
mjr 52:8298b2a73eb2 3558 // check to see if we're entering calibration mode
mjr 52:8298b2a73eb2 3559 if (f && !plungerCalMode)
mjr 52:8298b2a73eb2 3560 {
mjr 52:8298b2a73eb2 3561 // reset the calibration in the configuration
mjr 48:058ace2aed1d 3562 cfg.plunger.cal.begin();
mjr 52:8298b2a73eb2 3563
mjr 52:8298b2a73eb2 3564 // start in state 0 (waiting to settle)
mjr 52:8298b2a73eb2 3565 calState = 0;
mjr 52:8298b2a73eb2 3566 calZeroPosSum = 0;
mjr 52:8298b2a73eb2 3567 calZeroPosN = 0;
mjr 52:8298b2a73eb2 3568 calRlsTimeSum = 0;
mjr 52:8298b2a73eb2 3569 calRlsTimeN = 0;
mjr 52:8298b2a73eb2 3570
mjr 52:8298b2a73eb2 3571 // set the initial zero point to the current position
mjr 52:8298b2a73eb2 3572 PlungerReading r;
mjr 52:8298b2a73eb2 3573 if (plungerSensor->read(r))
mjr 52:8298b2a73eb2 3574 {
mjr 52:8298b2a73eb2 3575 // got a reading - use it as the initial zero point
mjr 52:8298b2a73eb2 3576 cfg.plunger.cal.zero = r.pos;
mjr 52:8298b2a73eb2 3577
mjr 52:8298b2a73eb2 3578 // use it as the starting point for the settling watch
mjr 53:9b2611964afc 3579 calZeroStart = r;
mjr 52:8298b2a73eb2 3580 }
mjr 52:8298b2a73eb2 3581 else
mjr 52:8298b2a73eb2 3582 {
mjr 52:8298b2a73eb2 3583 // no reading available - use the default 1/6 position
mjr 52:8298b2a73eb2 3584 cfg.plunger.cal.zero = 0xffff/6;
mjr 52:8298b2a73eb2 3585
mjr 52:8298b2a73eb2 3586 // we don't have a starting point for the setting watch
mjr 53:9b2611964afc 3587 calZeroStart.pos = -65535;
mjr 53:9b2611964afc 3588 calZeroStart.t = 0;
mjr 53:9b2611964afc 3589 }
mjr 53:9b2611964afc 3590 }
mjr 53:9b2611964afc 3591 else if (!f && plungerCalMode)
mjr 53:9b2611964afc 3592 {
mjr 53:9b2611964afc 3593 // Leaving calibration mode. Make sure the max is past the
mjr 53:9b2611964afc 3594 // zero point - if it's not, we'd have a zero or negative
mjr 53:9b2611964afc 3595 // denominator for the scaling calculation, which would be
mjr 53:9b2611964afc 3596 // physically meaningless.
mjr 53:9b2611964afc 3597 if (cfg.plunger.cal.max <= cfg.plunger.cal.zero)
mjr 53:9b2611964afc 3598 {
mjr 53:9b2611964afc 3599 // bad settings - reset to defaults
mjr 53:9b2611964afc 3600 cfg.plunger.cal.max = 0xffff;
mjr 53:9b2611964afc 3601 cfg.plunger.cal.zero = 0xffff/6;
mjr 52:8298b2a73eb2 3602 }
mjr 52:8298b2a73eb2 3603 }
mjr 52:8298b2a73eb2 3604
mjr 48:058ace2aed1d 3605 // remember the new mode
mjr 52:8298b2a73eb2 3606 plungerCalMode = f;
mjr 48:058ace2aed1d 3607 }
mjr 48:058ace2aed1d 3608
mjr 48:058ace2aed1d 3609 // is a firing event in progress?
mjr 53:9b2611964afc 3610 bool isFiring() { return firing == 3; }
mjr 48:058ace2aed1d 3611
mjr 48:058ace2aed1d 3612 private:
mjr 52:8298b2a73eb2 3613
mjr 58:523fdcffbe6d 3614 // Figure the next filtered value. This applies the hysteresis
mjr 58:523fdcffbe6d 3615 // filter to the last raw z value and returns the filtered result.
mjr 58:523fdcffbe6d 3616 int applyFilter()
mjr 58:523fdcffbe6d 3617 {
mjr 58:523fdcffbe6d 3618 if (firing <= 1)
mjr 58:523fdcffbe6d 3619 {
mjr 58:523fdcffbe6d 3620 // Filter limit - 5 samples. Once we've been moving
mjr 58:523fdcffbe6d 3621 // in the same direction for this many samples, we'll
mjr 58:523fdcffbe6d 3622 // clear the history and start over.
mjr 58:523fdcffbe6d 3623 const int filterMask = 0x1f;
mjr 58:523fdcffbe6d 3624
mjr 58:523fdcffbe6d 3625 // figure the last average
mjr 58:523fdcffbe6d 3626 int lastAvg = int(filterSum / filterN);
mjr 58:523fdcffbe6d 3627
mjr 58:523fdcffbe6d 3628 // figure the direction of this sample relative to the average,
mjr 58:523fdcffbe6d 3629 // and shift it in to our bit mask of recent direction data
mjr 58:523fdcffbe6d 3630 if (z != lastAvg)
mjr 58:523fdcffbe6d 3631 {
mjr 58:523fdcffbe6d 3632 // shift the new direction bit into the vector
mjr 58:523fdcffbe6d 3633 filterDir <<= 1;
mjr 58:523fdcffbe6d 3634 if (z > lastAvg) filterDir |= 1;
mjr 58:523fdcffbe6d 3635 }
mjr 58:523fdcffbe6d 3636
mjr 58:523fdcffbe6d 3637 // keep only the last N readings, up to the filter limit
mjr 58:523fdcffbe6d 3638 filterDir &= filterMask;
mjr 58:523fdcffbe6d 3639
mjr 58:523fdcffbe6d 3640 // if we've been moving consistently in one direction (all 1's
mjr 58:523fdcffbe6d 3641 // or all 0's in the direction history vector), reset the average
mjr 58:523fdcffbe6d 3642 if (filterDir == 0x00 || filterDir == filterMask)
mjr 58:523fdcffbe6d 3643 {
mjr 58:523fdcffbe6d 3644 // motion away from the average - reset the average
mjr 58:523fdcffbe6d 3645 filterDir = 0x5555;
mjr 58:523fdcffbe6d 3646 filterN = 1;
mjr 58:523fdcffbe6d 3647 filterSum = (lastAvg + z)/2;
mjr 58:523fdcffbe6d 3648 return int16_t(filterSum);
mjr 58:523fdcffbe6d 3649 }
mjr 58:523fdcffbe6d 3650 else
mjr 58:523fdcffbe6d 3651 {
mjr 58:523fdcffbe6d 3652 // we're diretionless - return the new average, with the
mjr 58:523fdcffbe6d 3653 // new sample included
mjr 58:523fdcffbe6d 3654 filterSum += z;
mjr 58:523fdcffbe6d 3655 ++filterN;
mjr 58:523fdcffbe6d 3656 return int16_t(filterSum / filterN);
mjr 58:523fdcffbe6d 3657 }
mjr 58:523fdcffbe6d 3658 }
mjr 58:523fdcffbe6d 3659 else
mjr 58:523fdcffbe6d 3660 {
mjr 58:523fdcffbe6d 3661 // firing mode - skip the filter
mjr 58:523fdcffbe6d 3662 filterN = 1;
mjr 58:523fdcffbe6d 3663 filterSum = z;
mjr 58:523fdcffbe6d 3664 filterDir = 0x5555;
mjr 58:523fdcffbe6d 3665 return z;
mjr 58:523fdcffbe6d 3666 }
mjr 58:523fdcffbe6d 3667 }
mjr 58:523fdcffbe6d 3668
mjr 58:523fdcffbe6d 3669 void initFilter()
mjr 58:523fdcffbe6d 3670 {
mjr 58:523fdcffbe6d 3671 filterSum = 0;
mjr 58:523fdcffbe6d 3672 filterN = 1;
mjr 58:523fdcffbe6d 3673 filterDir = 0x5555;
mjr 58:523fdcffbe6d 3674 }
mjr 58:523fdcffbe6d 3675 int64_t filterSum;
mjr 58:523fdcffbe6d 3676 int64_t filterN;
mjr 58:523fdcffbe6d 3677 uint16_t filterDir;
mjr 58:523fdcffbe6d 3678
mjr 58:523fdcffbe6d 3679
mjr 52:8298b2a73eb2 3680 // Calibration state. During calibration mode, we watch for release
mjr 52:8298b2a73eb2 3681 // events, to measure the time it takes to complete the release
mjr 52:8298b2a73eb2 3682 // motion; and we watch for the plunger to come to reset after a
mjr 52:8298b2a73eb2 3683 // release, to gather statistics on the rest position.
mjr 52:8298b2a73eb2 3684 // 0 = waiting to settle
mjr 52:8298b2a73eb2 3685 // 1 = at rest
mjr 52:8298b2a73eb2 3686 // 2 = retracting
mjr 52:8298b2a73eb2 3687 // 3 = possibly releasing
mjr 52:8298b2a73eb2 3688 uint8_t calState;
mjr 52:8298b2a73eb2 3689
mjr 52:8298b2a73eb2 3690 // Calibration zero point statistics.
mjr 52:8298b2a73eb2 3691 // During calibration mode, we collect data on the rest position (the
mjr 52:8298b2a73eb2 3692 // zero point) by watching for the plunger to come to rest after each
mjr 52:8298b2a73eb2 3693 // release. We average these rest positions to get the calibrated
mjr 52:8298b2a73eb2 3694 // zero point. We use the average because the real physical plunger
mjr 52:8298b2a73eb2 3695 // itself doesn't come to rest at exactly the same spot every time,
mjr 52:8298b2a73eb2 3696 // largely due to friction in the mechanism. To calculate the average,
mjr 52:8298b2a73eb2 3697 // we keep a sum of the readings and a count of samples.
mjr 53:9b2611964afc 3698 PlungerReading calZeroStart;
mjr 52:8298b2a73eb2 3699 long calZeroPosSum;
mjr 52:8298b2a73eb2 3700 int calZeroPosN;
mjr 52:8298b2a73eb2 3701
mjr 52:8298b2a73eb2 3702 // Calibration release time statistics.
mjr 52:8298b2a73eb2 3703 // During calibration, we collect an average for the release time.
mjr 52:8298b2a73eb2 3704 long calRlsTimeSum;
mjr 52:8298b2a73eb2 3705 int calRlsTimeN;
mjr 52:8298b2a73eb2 3706
mjr 48:058ace2aed1d 3707 // set a firing mode
mjr 48:058ace2aed1d 3708 inline void firingMode(int m)
mjr 48:058ace2aed1d 3709 {
mjr 48:058ace2aed1d 3710 firing = m;
mjr 48:058ace2aed1d 3711 }
mjr 48:058ace2aed1d 3712
mjr 48:058ace2aed1d 3713 // Find the most recent local maximum in the history data, up to
mjr 48:058ace2aed1d 3714 // the given time limit.
mjr 48:058ace2aed1d 3715 int histLocalMax(uint32_t tcur, uint32_t dt)
mjr 48:058ace2aed1d 3716 {
mjr 48:058ace2aed1d 3717 // start with the prior entry
mjr 48:058ace2aed1d 3718 int idx = (histIdx == 0 ? countof(hist) : histIdx) - 1;
mjr 48:058ace2aed1d 3719 int hi = hist[idx].pos;
mjr 48:058ace2aed1d 3720
mjr 48:058ace2aed1d 3721 // scan backwards for a local maximum
mjr 48:058ace2aed1d 3722 for (int n = countof(hist) - 1 ; n > 0 ; idx = (idx == 0 ? countof(hist) : idx) - 1)
mjr 48:058ace2aed1d 3723 {
mjr 48:058ace2aed1d 3724 // if this isn't within the time window, stop
mjr 48:058ace2aed1d 3725 if (uint32_t(tcur - hist[idx].t) > dt)
mjr 48:058ace2aed1d 3726 break;
mjr 48:058ace2aed1d 3727
mjr 48:058ace2aed1d 3728 // if this isn't above the current hith, stop
mjr 48:058ace2aed1d 3729 if (hist[idx].pos < hi)
mjr 48:058ace2aed1d 3730 break;
mjr 48:058ace2aed1d 3731
mjr 48:058ace2aed1d 3732 // this is the new high
mjr 48:058ace2aed1d 3733 hi = hist[idx].pos;
mjr 48:058ace2aed1d 3734 }
mjr 48:058ace2aed1d 3735
mjr 48:058ace2aed1d 3736 // return the local maximum
mjr 48:058ace2aed1d 3737 return hi;
mjr 48:058ace2aed1d 3738 }
mjr 48:058ace2aed1d 3739
mjr 50:40015764bbe6 3740 // velocity at previous reading, and the one before that
mjr 50:40015764bbe6 3741 float vprv, vprv2;
mjr 48:058ace2aed1d 3742
mjr 48:058ace2aed1d 3743 // Circular buffer of recent readings. We keep a short history
mjr 48:058ace2aed1d 3744 // of readings to analyze during firing events. We can only identify
mjr 48:058ace2aed1d 3745 // a firing event once it's somewhat under way, so we need a little
mjr 48:058ace2aed1d 3746 // retrospective information to accurately determine after the fact
mjr 48:058ace2aed1d 3747 // exactly when it started. We throttle our readings to no more
mjr 48:058ace2aed1d 3748 // than one every 2ms, so we have at least N*2ms of history in this
mjr 48:058ace2aed1d 3749 // array.
mjr 50:40015764bbe6 3750 PlungerReading hist[25];
mjr 48:058ace2aed1d 3751 int histIdx;
mjr 49:37bd97eb7688 3752
mjr 50:40015764bbe6 3753 // get the nth history item (0=last, 1=2nd to last, etc)
mjr 50:40015764bbe6 3754 const PlungerReading &nthHist(int n) const
mjr 50:40015764bbe6 3755 {
mjr 50:40015764bbe6 3756 // histIdx-1 is the last written; go from there
mjr 50:40015764bbe6 3757 n = histIdx - 1 - n;
mjr 50:40015764bbe6 3758
mjr 50:40015764bbe6 3759 // adjust for wrapping
mjr 50:40015764bbe6 3760 if (n < 0)
mjr 50:40015764bbe6 3761 n += countof(hist);
mjr 50:40015764bbe6 3762
mjr 50:40015764bbe6 3763 // return the item
mjr 50:40015764bbe6 3764 return hist[n];
mjr 50:40015764bbe6 3765 }
mjr 48:058ace2aed1d 3766
mjr 48:058ace2aed1d 3767 // Firing event state.
mjr 48:058ace2aed1d 3768 //
mjr 48:058ace2aed1d 3769 // 0 - Default state. We report the real instantaneous plunger
mjr 48:058ace2aed1d 3770 // position to the joystick interface.
mjr 48:058ace2aed1d 3771 //
mjr 53:9b2611964afc 3772 // 1 - Moving forward
mjr 48:058ace2aed1d 3773 //
mjr 53:9b2611964afc 3774 // 2 - Accelerating
mjr 48:058ace2aed1d 3775 //
mjr 53:9b2611964afc 3776 // 3 - Firing. We report the rest position for a minimum interval,
mjr 53:9b2611964afc 3777 // or until the real plunger comes to rest somewhere.
mjr 48:058ace2aed1d 3778 //
mjr 48:058ace2aed1d 3779 int firing;
mjr 48:058ace2aed1d 3780
mjr 51:57eb311faafa 3781 // Position/timestamp at start of firing phase 1. When we see a
mjr 51:57eb311faafa 3782 // sustained forward acceleration, we freeze joystick reports at
mjr 51:57eb311faafa 3783 // the recent local maximum, on the assumption that this was the
mjr 51:57eb311faafa 3784 // start of the release. If this is zero, it means that we're
mjr 51:57eb311faafa 3785 // monitoring accelerating motion but haven't seen it for long
mjr 51:57eb311faafa 3786 // enough yet to be confident that a release is in progress.
mjr 48:058ace2aed1d 3787 PlungerReading f1;
mjr 48:058ace2aed1d 3788
mjr 48:058ace2aed1d 3789 // Position/timestamp at start of firing phase 2. The position is
mjr 48:058ace2aed1d 3790 // the fake "bounce" position we report during this phase, and the
mjr 48:058ace2aed1d 3791 // timestamp tells us when the phase began so that we can end it
mjr 48:058ace2aed1d 3792 // after enough time elapses.
mjr 48:058ace2aed1d 3793 PlungerReading f2;
mjr 48:058ace2aed1d 3794
mjr 48:058ace2aed1d 3795 // Position/timestamp of start of stability window during phase 3.
mjr 48:058ace2aed1d 3796 // We use this to determine when the plunger comes to rest. We set
mjr 51:57eb311faafa 3797 // this at the beginning of phase 3, and then reset it when the
mjr 48:058ace2aed1d 3798 // plunger moves too far from the last position.
mjr 48:058ace2aed1d 3799 PlungerReading f3s;
mjr 48:058ace2aed1d 3800
mjr 48:058ace2aed1d 3801 // Position/timestamp of start of retraction window during phase 3.
mjr 48:058ace2aed1d 3802 // We use this to determine if the user is drawing the plunger back.
mjr 48:058ace2aed1d 3803 // If we see retraction motion for more than about 65ms, we assume
mjr 48:058ace2aed1d 3804 // that the user has taken over, because we should see forward
mjr 48:058ace2aed1d 3805 // motion within this timeframe if the plunger is just bouncing
mjr 48:058ace2aed1d 3806 // freely.
mjr 48:058ace2aed1d 3807 PlungerReading f3r;
mjr 48:058ace2aed1d 3808
mjr 58:523fdcffbe6d 3809 // next raw (unfiltered) Z value to report to the joystick interface
mjr 58:523fdcffbe6d 3810 // (in joystick distance units)
mjr 48:058ace2aed1d 3811 int z;
mjr 48:058ace2aed1d 3812
mjr 48:058ace2aed1d 3813 // velocity of this reading (joystick distance units per microsecond)
mjr 48:058ace2aed1d 3814 float vz;
mjr 58:523fdcffbe6d 3815
mjr 58:523fdcffbe6d 3816 // next filtered Z value to report to the joystick interface
mjr 58:523fdcffbe6d 3817 int zf;
mjr 48:058ace2aed1d 3818 };
mjr 48:058ace2aed1d 3819
mjr 48:058ace2aed1d 3820 // plunger reader singleton
mjr 48:058ace2aed1d 3821 PlungerReader plungerReader;
mjr 48:058ace2aed1d 3822
mjr 48:058ace2aed1d 3823 // ---------------------------------------------------------------------------
mjr 48:058ace2aed1d 3824 //
mjr 48:058ace2aed1d 3825 // Handle the ZB Launch Ball feature.
mjr 48:058ace2aed1d 3826 //
mjr 48:058ace2aed1d 3827 // The ZB Launch Ball feature, if enabled, lets the mechanical plunger
mjr 48:058ace2aed1d 3828 // serve as a substitute for a physical Launch Ball button. When a table
mjr 48:058ace2aed1d 3829 // is loaded in VP, and the table has the ZB Launch Ball LedWiz port
mjr 48:058ace2aed1d 3830 // turned on, we'll disable mechanical plunger reports through the
mjr 48:058ace2aed1d 3831 // joystick interface and instead use the plunger only to simulate the
mjr 48:058ace2aed1d 3832 // Launch Ball button. When the mode is active, pulling back and
mjr 48:058ace2aed1d 3833 // releasing the plunger causes a brief simulated press of the Launch
mjr 48:058ace2aed1d 3834 // button, and pushing the plunger forward of the rest position presses
mjr 48:058ace2aed1d 3835 // the Launch button as long as the plunger is pressed forward.
mjr 48:058ace2aed1d 3836 //
mjr 48:058ace2aed1d 3837 // This feature has two configuration components:
mjr 48:058ace2aed1d 3838 //
mjr 48:058ace2aed1d 3839 // - An LedWiz port number. This port is a "virtual" port that doesn't
mjr 48:058ace2aed1d 3840 // have to be attached to any actual output. DOF uses it to signal
mjr 48:058ace2aed1d 3841 // that the current table uses a Launch button instead of a plunger.
mjr 48:058ace2aed1d 3842 // DOF simply turns the port on when such a table is loaded and turns
mjr 48:058ace2aed1d 3843 // it off at all other times. We use it to enable and disable the
mjr 48:058ace2aed1d 3844 // plunger/launch button connection.
mjr 48:058ace2aed1d 3845 //
mjr 48:058ace2aed1d 3846 // - A joystick button ID. We simulate pressing this button when the
mjr 48:058ace2aed1d 3847 // launch feature is activated via the LedWiz port and the plunger is
mjr 48:058ace2aed1d 3848 // either pulled back and releasd, or pushed forward past the rest
mjr 48:058ace2aed1d 3849 // position.
mjr 48:058ace2aed1d 3850 //
mjr 48:058ace2aed1d 3851 class ZBLaunchBall
mjr 48:058ace2aed1d 3852 {
mjr 48:058ace2aed1d 3853 public:
mjr 48:058ace2aed1d 3854 ZBLaunchBall()
mjr 48:058ace2aed1d 3855 {
mjr 48:058ace2aed1d 3856 // start in the default state
mjr 48:058ace2aed1d 3857 lbState = 0;
mjr 53:9b2611964afc 3858 btnState = false;
mjr 48:058ace2aed1d 3859 }
mjr 48:058ace2aed1d 3860
mjr 48:058ace2aed1d 3861 // Update state. This checks the current plunger position and
mjr 48:058ace2aed1d 3862 // the timers to see if the plunger is in a position that simulates
mjr 48:058ace2aed1d 3863 // a Launch Ball button press via the ZB Launch Ball feature.
mjr 48:058ace2aed1d 3864 // Updates the simulated button vector according to the current
mjr 48:058ace2aed1d 3865 // launch ball state. The main loop calls this before each
mjr 48:058ace2aed1d 3866 // joystick update to figure the new simulated button state.
mjr 53:9b2611964afc 3867 void update()
mjr 48:058ace2aed1d 3868 {
mjr 53:9b2611964afc 3869 // If the ZB Launch Ball led wiz output is ON, check for a
mjr 53:9b2611964afc 3870 // plunger firing event
mjr 53:9b2611964afc 3871 if (zbLaunchOn)
mjr 48:058ace2aed1d 3872 {
mjr 53:9b2611964afc 3873 // note the new position
mjr 48:058ace2aed1d 3874 int znew = plungerReader.getPosition();
mjr 53:9b2611964afc 3875
mjr 53:9b2611964afc 3876 // figure the push threshold from the configuration data
mjr 51:57eb311faafa 3877 const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
mjr 53:9b2611964afc 3878
mjr 53:9b2611964afc 3879 // check the state
mjr 48:058ace2aed1d 3880 switch (lbState)
mjr 48:058ace2aed1d 3881 {
mjr 48:058ace2aed1d 3882 case 0:
mjr 53:9b2611964afc 3883 // Default state. If a launch event has been detected on
mjr 53:9b2611964afc 3884 // the plunger, activate a timed pulse and switch to state 1.
mjr 53:9b2611964afc 3885 // If the plunger is pushed forward of the threshold, push
mjr 53:9b2611964afc 3886 // the button.
mjr 53:9b2611964afc 3887 if (plungerReader.isFiring())
mjr 53:9b2611964afc 3888 {
mjr 53:9b2611964afc 3889 // firing event - start a timed Launch button pulse
mjr 53:9b2611964afc 3890 lbTimer.reset();
mjr 53:9b2611964afc 3891 lbTimer.start();
mjr 53:9b2611964afc 3892 setButton(true);
mjr 53:9b2611964afc 3893
mjr 53:9b2611964afc 3894 // switch to state 1
mjr 53:9b2611964afc 3895 lbState = 1;
mjr 53:9b2611964afc 3896 }
mjr 48:058ace2aed1d 3897 else if (znew <= pushThreshold)
mjr 53:9b2611964afc 3898 {
mjr 53:9b2611964afc 3899 // pushed forward without a firing event - hold the
mjr 53:9b2611964afc 3900 // button as long as we're pushed forward
mjr 53:9b2611964afc 3901 setButton(true);
mjr 53:9b2611964afc 3902 }
mjr 53:9b2611964afc 3903 else
mjr 53:9b2611964afc 3904 {
mjr 53:9b2611964afc 3905 // not pushed forward - turn off the Launch button
mjr 53:9b2611964afc 3906 setButton(false);
mjr 53:9b2611964afc 3907 }
mjr 48:058ace2aed1d 3908 break;
mjr 48:058ace2aed1d 3909
mjr 48:058ace2aed1d 3910 case 1:
mjr 53:9b2611964afc 3911 // State 1: Timed Launch button pulse in progress after a
mjr 53:9b2611964afc 3912 // firing event. Wait for the timer to expire.
mjr 53:9b2611964afc 3913 if (lbTimer.read_us() > 200000UL)
mjr 53:9b2611964afc 3914 {
mjr 53:9b2611964afc 3915 // timer expired - turn off the button
mjr 53:9b2611964afc 3916 setButton(false);
mjr 53:9b2611964afc 3917
mjr 53:9b2611964afc 3918 // switch to state 2
mjr 53:9b2611964afc 3919 lbState = 2;
mjr 53:9b2611964afc 3920 }
mjr 48:058ace2aed1d 3921 break;
mjr 48:058ace2aed1d 3922
mjr 48:058ace2aed1d 3923 case 2:
mjr 53:9b2611964afc 3924 // State 2: Timed Launch button pulse done. Wait for the
mjr 53:9b2611964afc 3925 // plunger launch event to end.
mjr 53:9b2611964afc 3926 if (!plungerReader.isFiring())
mjr 53:9b2611964afc 3927 {
mjr 53:9b2611964afc 3928 // firing event done - return to default state
mjr 53:9b2611964afc 3929 lbState = 0;
mjr 53:9b2611964afc 3930 }
mjr 48:058ace2aed1d 3931 break;
mjr 48:058ace2aed1d 3932 }
mjr 53:9b2611964afc 3933 }
mjr 53:9b2611964afc 3934 else
mjr 53:9b2611964afc 3935 {
mjr 53:9b2611964afc 3936 // ZB Launch Ball disabled - turn off the button if it was on
mjr 53:9b2611964afc 3937 setButton(false);
mjr 48:058ace2aed1d 3938
mjr 53:9b2611964afc 3939 // return to the default state
mjr 53:9b2611964afc 3940 lbState = 0;
mjr 48:058ace2aed1d 3941 }
mjr 48:058ace2aed1d 3942 }
mjr 53:9b2611964afc 3943
mjr 53:9b2611964afc 3944 // Set the button state
mjr 53:9b2611964afc 3945 void setButton(bool on)
mjr 53:9b2611964afc 3946 {
mjr 53:9b2611964afc 3947 if (btnState != on)
mjr 53:9b2611964afc 3948 {
mjr 53:9b2611964afc 3949 // remember the new state
mjr 53:9b2611964afc 3950 btnState = on;
mjr 53:9b2611964afc 3951
mjr 53:9b2611964afc 3952 // update the virtual button state
mjr 65:739875521aae 3953 buttonState[zblButtonIndex].virtPress(on);
mjr 53:9b2611964afc 3954 }
mjr 53:9b2611964afc 3955 }
mjr 53:9b2611964afc 3956
mjr 48:058ace2aed1d 3957 private:
mjr 48:058ace2aed1d 3958 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 48:058ace2aed1d 3959 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 48:058ace2aed1d 3960 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 48:058ace2aed1d 3961 // back and releases the plunger, or simply pushes on the plunger from
mjr 48:058ace2aed1d 3962 // the rest position. This allows the plunger to be used in lieu of a
mjr 48:058ace2aed1d 3963 // physical Launch Ball button for tables that don't have plungers.
mjr 48:058ace2aed1d 3964 //
mjr 48:058ace2aed1d 3965 // States:
mjr 48:058ace2aed1d 3966 // 0 = default
mjr 53:9b2611964afc 3967 // 1 = firing (firing event has activated a Launch button pulse)
mjr 53:9b2611964afc 3968 // 2 = firing done (Launch button pulse ended, waiting for plunger
mjr 53:9b2611964afc 3969 // firing event to end)
mjr 53:9b2611964afc 3970 uint8_t lbState;
mjr 48:058ace2aed1d 3971
mjr 53:9b2611964afc 3972 // button state
mjr 53:9b2611964afc 3973 bool btnState;
mjr 48:058ace2aed1d 3974
mjr 48:058ace2aed1d 3975 // Time since last lbState transition. Some of the states are time-
mjr 48:058ace2aed1d 3976 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 48:058ace2aed1d 3977 // we remain in this state for more than a few milliseconds, since
mjr 48:058ace2aed1d 3978 // it indicates that the plunger is being slowly returned to rest
mjr 48:058ace2aed1d 3979 // rather than released. In the "launching" state, we need to release
mjr 48:058ace2aed1d 3980 // the Launch Ball button after a moment, and we need to wait for
mjr 48:058ace2aed1d 3981 // the plunger to come to rest before returning to state 0.
mjr 48:058ace2aed1d 3982 Timer lbTimer;
mjr 48:058ace2aed1d 3983 };
mjr 48:058ace2aed1d 3984
mjr 35:e959ffba78fd 3985 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 3986 //
mjr 35:e959ffba78fd 3987 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 3988 //
mjr 54:fd77a6b2f76c 3989 void reboot(USBJoystick &js, bool disconnect = true, long pause_us = 2000000L)
mjr 35:e959ffba78fd 3990 {
mjr 35:e959ffba78fd 3991 // disconnect from USB
mjr 54:fd77a6b2f76c 3992 if (disconnect)
mjr 54:fd77a6b2f76c 3993 js.disconnect();
mjr 35:e959ffba78fd 3994
mjr 35:e959ffba78fd 3995 // wait a few seconds to make sure the host notices the disconnect
mjr 54:fd77a6b2f76c 3996 wait_us(pause_us);
mjr 35:e959ffba78fd 3997
mjr 35:e959ffba78fd 3998 // reset the device
mjr 35:e959ffba78fd 3999 NVIC_SystemReset();
mjr 35:e959ffba78fd 4000 while (true) { }
mjr 35:e959ffba78fd 4001 }
mjr 35:e959ffba78fd 4002
mjr 35:e959ffba78fd 4003 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4004 //
mjr 35:e959ffba78fd 4005 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 4006 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 4007 //
mjr 35:e959ffba78fd 4008 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 4009 {
mjr 35:e959ffba78fd 4010 int tmp;
mjr 35:e959ffba78fd 4011 switch (cfg.orientation)
mjr 35:e959ffba78fd 4012 {
mjr 35:e959ffba78fd 4013 case OrientationFront:
mjr 35:e959ffba78fd 4014 tmp = x;
mjr 35:e959ffba78fd 4015 x = y;
mjr 35:e959ffba78fd 4016 y = tmp;
mjr 35:e959ffba78fd 4017 break;
mjr 35:e959ffba78fd 4018
mjr 35:e959ffba78fd 4019 case OrientationLeft:
mjr 35:e959ffba78fd 4020 x = -x;
mjr 35:e959ffba78fd 4021 break;
mjr 35:e959ffba78fd 4022
mjr 35:e959ffba78fd 4023 case OrientationRight:
mjr 35:e959ffba78fd 4024 y = -y;
mjr 35:e959ffba78fd 4025 break;
mjr 35:e959ffba78fd 4026
mjr 35:e959ffba78fd 4027 case OrientationRear:
mjr 35:e959ffba78fd 4028 tmp = -x;
mjr 35:e959ffba78fd 4029 x = -y;
mjr 35:e959ffba78fd 4030 y = tmp;
mjr 35:e959ffba78fd 4031 break;
mjr 35:e959ffba78fd 4032 }
mjr 35:e959ffba78fd 4033 }
mjr 35:e959ffba78fd 4034
mjr 35:e959ffba78fd 4035 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4036 //
mjr 35:e959ffba78fd 4037 // Calibration button state:
mjr 35:e959ffba78fd 4038 // 0 = not pushed
mjr 35:e959ffba78fd 4039 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 4040 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 4041 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 4042 int calBtnState = 0;
mjr 35:e959ffba78fd 4043
mjr 35:e959ffba78fd 4044 // calibration button debounce timer
mjr 35:e959ffba78fd 4045 Timer calBtnTimer;
mjr 35:e959ffba78fd 4046
mjr 35:e959ffba78fd 4047 // calibration button light state
mjr 35:e959ffba78fd 4048 int calBtnLit = false;
mjr 35:e959ffba78fd 4049
mjr 35:e959ffba78fd 4050
mjr 35:e959ffba78fd 4051 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4052 //
mjr 40:cc0d9814522b 4053 // Configuration variable get/set message handling
mjr 35:e959ffba78fd 4054 //
mjr 40:cc0d9814522b 4055
mjr 40:cc0d9814522b 4056 // Handle SET messages - write configuration variables from USB message data
mjr 40:cc0d9814522b 4057 #define if_msg_valid(test) if (test)
mjr 53:9b2611964afc 4058 #define v_byte(var, ofs) cfg.var = data[ofs]
mjr 53:9b2611964afc 4059 #define v_ui16(var, ofs) cfg.var = wireUI16(data+(ofs))
mjr 53:9b2611964afc 4060 #define v_pin(var, ofs) cfg.var = wirePinName(data[ofs])
mjr 53:9b2611964afc 4061 #define v_byte_ro(val, ofs) // ignore read-only variables on SET
mjr 40:cc0d9814522b 4062 #define v_func configVarSet
mjr 40:cc0d9814522b 4063 #include "cfgVarMsgMap.h"
mjr 35:e959ffba78fd 4064
mjr 40:cc0d9814522b 4065 // redefine everything for the SET messages
mjr 40:cc0d9814522b 4066 #undef if_msg_valid
mjr 40:cc0d9814522b 4067 #undef v_byte
mjr 40:cc0d9814522b 4068 #undef v_ui16
mjr 40:cc0d9814522b 4069 #undef v_pin
mjr 53:9b2611964afc 4070 #undef v_byte_ro
mjr 40:cc0d9814522b 4071 #undef v_func
mjr 38:091e511ce8a0 4072
mjr 40:cc0d9814522b 4073 // Handle GET messages - read variable values and return in USB message daa
mjr 40:cc0d9814522b 4074 #define if_msg_valid(test)
mjr 53:9b2611964afc 4075 #define v_byte(var, ofs) data[ofs] = cfg.var
mjr 53:9b2611964afc 4076 #define v_ui16(var, ofs) ui16Wire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 4077 #define v_pin(var, ofs) pinNameWire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 4078 #define v_byte_ro(val, ofs) data[ofs] = val
mjr 40:cc0d9814522b 4079 #define v_func configVarGet
mjr 40:cc0d9814522b 4080 #include "cfgVarMsgMap.h"
mjr 40:cc0d9814522b 4081
mjr 35:e959ffba78fd 4082
mjr 35:e959ffba78fd 4083 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4084 //
mjr 35:e959ffba78fd 4085 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 4086 // LedWiz protocol.
mjr 33:d832bcab089e 4087 //
mjr 48:058ace2aed1d 4088 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js)
mjr 35:e959ffba78fd 4089 {
mjr 38:091e511ce8a0 4090 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 4091 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 4092 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 4093 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 4094 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 4095 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 4096 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 4097 // So our full protocol is as follows:
mjr 38:091e511ce8a0 4098 //
mjr 38:091e511ce8a0 4099 // first byte =
mjr 38:091e511ce8a0 4100 // 0-48 -> LWZ-PBA
mjr 38:091e511ce8a0 4101 // 64 -> LWZ SBA
mjr 38:091e511ce8a0 4102 // 65 -> private control message; second byte specifies subtype
mjr 38:091e511ce8a0 4103 // 129-132 -> LWZ-PBA
mjr 38:091e511ce8a0 4104 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 4105 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 4106 // other -> reserved for future use
mjr 38:091e511ce8a0 4107 //
mjr 39:b3815a1c3802 4108 uint8_t *data = lwm.data;
mjr 38:091e511ce8a0 4109 if (data[0] == 64)
mjr 35:e959ffba78fd 4110 {
mjr 38:091e511ce8a0 4111 // LWZ-SBA - first four bytes are bit-packed on/off flags
mjr 38:091e511ce8a0 4112 // for the outputs; 5th byte is the pulse speed (1-7)
mjr 38:091e511ce8a0 4113 //printf("LWZ-SBA %02x %02x %02x %02x ; %02x\r\n",
mjr 38:091e511ce8a0 4114 // data[1], data[2], data[3], data[4], data[5]);
mjr 38:091e511ce8a0 4115
mjr 63:5cd1a5f3a41b 4116 // switch to LedWiz protocol mode
mjr 63:5cd1a5f3a41b 4117 ledWizMode = true;
mjr 63:5cd1a5f3a41b 4118
mjr 38:091e511ce8a0 4119 // update all on/off states
mjr 38:091e511ce8a0 4120 for (int i = 0, bit = 1, ri = 1 ; i < numLwOutputs ; ++i, bit <<= 1)
mjr 35:e959ffba78fd 4121 {
mjr 38:091e511ce8a0 4122 // figure the on/off state bit for this output
mjr 38:091e511ce8a0 4123 if (bit == 0x100) {
mjr 38:091e511ce8a0 4124 bit = 1;
mjr 38:091e511ce8a0 4125 ++ri;
mjr 35:e959ffba78fd 4126 }
mjr 35:e959ffba78fd 4127
mjr 38:091e511ce8a0 4128 // set the on/off state
mjr 38:091e511ce8a0 4129 wizOn[i] = ((data[ri] & bit) != 0);
mjr 38:091e511ce8a0 4130 }
mjr 38:091e511ce8a0 4131
mjr 38:091e511ce8a0 4132 // set the flash speed - enforce the value range 1-7
mjr 38:091e511ce8a0 4133 wizSpeed = data[5];
mjr 38:091e511ce8a0 4134 if (wizSpeed < 1)
mjr 38:091e511ce8a0 4135 wizSpeed = 1;
mjr 38:091e511ce8a0 4136 else if (wizSpeed > 7)
mjr 38:091e511ce8a0 4137 wizSpeed = 7;
mjr 38:091e511ce8a0 4138
mjr 38:091e511ce8a0 4139 // update the physical outputs
mjr 38:091e511ce8a0 4140 updateWizOuts();
mjr 38:091e511ce8a0 4141 if (hc595 != 0)
mjr 38:091e511ce8a0 4142 hc595->update();
mjr 38:091e511ce8a0 4143
mjr 38:091e511ce8a0 4144 // reset the PBA counter
mjr 38:091e511ce8a0 4145 pbaIdx = 0;
mjr 38:091e511ce8a0 4146 }
mjr 38:091e511ce8a0 4147 else if (data[0] == 65)
mjr 38:091e511ce8a0 4148 {
mjr 38:091e511ce8a0 4149 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 4150 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 4151 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 4152 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 4153 // message type.
mjr 39:b3815a1c3802 4154 switch (data[1])
mjr 38:091e511ce8a0 4155 {
mjr 39:b3815a1c3802 4156 case 0:
mjr 39:b3815a1c3802 4157 // No Op
mjr 39:b3815a1c3802 4158 break;
mjr 39:b3815a1c3802 4159
mjr 39:b3815a1c3802 4160 case 1:
mjr 38:091e511ce8a0 4161 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 4162 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 4163 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 4164 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 4165 {
mjr 39:b3815a1c3802 4166
mjr 39:b3815a1c3802 4167 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 4168 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 4169 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 4170
mjr 39:b3815a1c3802 4171 // we'll need a reset if the LedWiz unit number is changing
mjr 39:b3815a1c3802 4172 bool needReset = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 4173
mjr 39:b3815a1c3802 4174 // set the configuration parameters from the message
mjr 39:b3815a1c3802 4175 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 4176 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 4177
mjr 39:b3815a1c3802 4178 // save the configuration
mjr 39:b3815a1c3802 4179 saveConfigToFlash();
mjr 39:b3815a1c3802 4180
mjr 39:b3815a1c3802 4181 // reboot if necessary
mjr 39:b3815a1c3802 4182 if (needReset)
mjr 39:b3815a1c3802 4183 reboot(js);
mjr 39:b3815a1c3802 4184 }
mjr 39:b3815a1c3802 4185 break;
mjr 38:091e511ce8a0 4186
mjr 39:b3815a1c3802 4187 case 2:
mjr 38:091e511ce8a0 4188 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 4189 // (No parameters)
mjr 38:091e511ce8a0 4190
mjr 38:091e511ce8a0 4191 // enter calibration mode
mjr 38:091e511ce8a0 4192 calBtnState = 3;
mjr 52:8298b2a73eb2 4193 plungerReader.setCalMode(true);
mjr 38:091e511ce8a0 4194 calBtnTimer.reset();
mjr 39:b3815a1c3802 4195 break;
mjr 39:b3815a1c3802 4196
mjr 39:b3815a1c3802 4197 case 3:
mjr 52:8298b2a73eb2 4198 // 3 = plunger sensor status report
mjr 48:058ace2aed1d 4199 // data[2] = flag bits
mjr 53:9b2611964afc 4200 // data[3] = extra exposure time, 100us (.1ms) increments
mjr 52:8298b2a73eb2 4201 reportPlungerStat = true;
mjr 53:9b2611964afc 4202 reportPlungerStatFlags = data[2];
mjr 53:9b2611964afc 4203 reportPlungerStatTime = data[3];
mjr 38:091e511ce8a0 4204
mjr 38:091e511ce8a0 4205 // show purple until we finish sending the report
mjr 38:091e511ce8a0 4206 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 4207 break;
mjr 39:b3815a1c3802 4208
mjr 39:b3815a1c3802 4209 case 4:
mjr 38:091e511ce8a0 4210 // 4 = hardware configuration query
mjr 38:091e511ce8a0 4211 // (No parameters)
mjr 38:091e511ce8a0 4212 js.reportConfig(
mjr 38:091e511ce8a0 4213 numOutputs,
mjr 38:091e511ce8a0 4214 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 52:8298b2a73eb2 4215 cfg.plunger.cal.zero, cfg.plunger.cal.max, cfg.plunger.cal.tRelease,
mjr 40:cc0d9814522b 4216 nvm.valid());
mjr 39:b3815a1c3802 4217 break;
mjr 39:b3815a1c3802 4218
mjr 39:b3815a1c3802 4219 case 5:
mjr 38:091e511ce8a0 4220 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 4221 allOutputsOff();
mjr 39:b3815a1c3802 4222 break;
mjr 39:b3815a1c3802 4223
mjr 39:b3815a1c3802 4224 case 6:
mjr 38:091e511ce8a0 4225 // 6 = Save configuration to flash.
mjr 38:091e511ce8a0 4226 saveConfigToFlash();
mjr 38:091e511ce8a0 4227
mjr 53:9b2611964afc 4228 // before disconnecting, pause for the delay time specified in
mjr 53:9b2611964afc 4229 // the parameter byte (in seconds)
mjr 53:9b2611964afc 4230 rebootTime_us = data[2] * 1000000L;
mjr 53:9b2611964afc 4231 rebootTimer.start();
mjr 39:b3815a1c3802 4232 break;
mjr 40:cc0d9814522b 4233
mjr 40:cc0d9814522b 4234 case 7:
mjr 40:cc0d9814522b 4235 // 7 = Device ID report
mjr 53:9b2611964afc 4236 // data[2] = ID index: 1=CPU ID, 2=OpenSDA TUID
mjr 53:9b2611964afc 4237 js.reportID(data[2]);
mjr 40:cc0d9814522b 4238 break;
mjr 40:cc0d9814522b 4239
mjr 40:cc0d9814522b 4240 case 8:
mjr 40:cc0d9814522b 4241 // 8 = Engage/disengage night mode.
mjr 40:cc0d9814522b 4242 // data[2] = 1 to engage, 0 to disengage
mjr 40:cc0d9814522b 4243 setNightMode(data[2]);
mjr 40:cc0d9814522b 4244 break;
mjr 52:8298b2a73eb2 4245
mjr 52:8298b2a73eb2 4246 case 9:
mjr 52:8298b2a73eb2 4247 // 9 = Config variable query.
mjr 52:8298b2a73eb2 4248 // data[2] = config var ID
mjr 52:8298b2a73eb2 4249 // data[3] = array index (for array vars: button assignments, output ports)
mjr 52:8298b2a73eb2 4250 {
mjr 53:9b2611964afc 4251 // set up the reply buffer with the variable ID data, and zero out
mjr 53:9b2611964afc 4252 // the rest of the buffer
mjr 52:8298b2a73eb2 4253 uint8_t reply[8];
mjr 52:8298b2a73eb2 4254 reply[1] = data[2];
mjr 52:8298b2a73eb2 4255 reply[2] = data[3];
mjr 53:9b2611964afc 4256 memset(reply+3, 0, sizeof(reply)-3);
mjr 52:8298b2a73eb2 4257
mjr 52:8298b2a73eb2 4258 // query the value
mjr 52:8298b2a73eb2 4259 configVarGet(reply);
mjr 52:8298b2a73eb2 4260
mjr 52:8298b2a73eb2 4261 // send the reply
mjr 52:8298b2a73eb2 4262 js.reportConfigVar(reply + 1);
mjr 52:8298b2a73eb2 4263 }
mjr 52:8298b2a73eb2 4264 break;
mjr 53:9b2611964afc 4265
mjr 53:9b2611964afc 4266 case 10:
mjr 53:9b2611964afc 4267 // 10 = Build ID query.
mjr 53:9b2611964afc 4268 js.reportBuildInfo(getBuildID());
mjr 53:9b2611964afc 4269 break;
mjr 38:091e511ce8a0 4270 }
mjr 38:091e511ce8a0 4271 }
mjr 38:091e511ce8a0 4272 else if (data[0] == 66)
mjr 38:091e511ce8a0 4273 {
mjr 38:091e511ce8a0 4274 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 4275 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 4276 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 4277 // in a variable-dependent format.
mjr 40:cc0d9814522b 4278 configVarSet(data);
mjr 38:091e511ce8a0 4279 }
mjr 38:091e511ce8a0 4280 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 4281 {
mjr 38:091e511ce8a0 4282 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 4283 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 4284 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 4285 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 4286 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 4287 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 4288 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 4289 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 4290 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 4291 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 4292 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 4293 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 4294 //
mjr 38:091e511ce8a0 4295 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 4296 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 4297 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 4298 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 4299 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 4300 // address those ports anyway.
mjr 63:5cd1a5f3a41b 4301
mjr 63:5cd1a5f3a41b 4302 // flag that we're in extended protocol mode
mjr 63:5cd1a5f3a41b 4303 ledWizMode = false;
mjr 63:5cd1a5f3a41b 4304
mjr 63:5cd1a5f3a41b 4305 // figure the block of 7 ports covered in the message
mjr 38:091e511ce8a0 4306 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 4307 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 63:5cd1a5f3a41b 4308
mjr 63:5cd1a5f3a41b 4309 // update each port
mjr 38:091e511ce8a0 4310 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 4311 {
mjr 38:091e511ce8a0 4312 // set the brightness level for the output
mjr 40:cc0d9814522b 4313 uint8_t b = data[i-i0+1];
mjr 38:091e511ce8a0 4314 outLevel[i] = b;
mjr 38:091e511ce8a0 4315
mjr 38:091e511ce8a0 4316 // set the output
mjr 40:cc0d9814522b 4317 lwPin[i]->set(b);
mjr 38:091e511ce8a0 4318 }
mjr 38:091e511ce8a0 4319
mjr 38:091e511ce8a0 4320 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 4321 if (hc595 != 0)
mjr 38:091e511ce8a0 4322 hc595->update();
mjr 38:091e511ce8a0 4323 }
mjr 38:091e511ce8a0 4324 else
mjr 38:091e511ce8a0 4325 {
mjr 38:091e511ce8a0 4326 // Everything else is LWZ-PBA. This is a full "profile"
mjr 38:091e511ce8a0 4327 // dump from the host for one bank of 8 outputs. Each
mjr 38:091e511ce8a0 4328 // byte sets one output in the current bank. The current
mjr 38:091e511ce8a0 4329 // bank is implied; the bank starts at 0 and is reset to 0
mjr 38:091e511ce8a0 4330 // by any LWZ-SBA message, and is incremented to the next
mjr 38:091e511ce8a0 4331 // bank by each LWZ-PBA message. Our variable pbaIdx keeps
mjr 38:091e511ce8a0 4332 // track of our notion of the current bank. There's no direct
mjr 38:091e511ce8a0 4333 // way for the host to select the bank; it just has to count
mjr 38:091e511ce8a0 4334 // on us staying in sync. In practice, the host will always
mjr 38:091e511ce8a0 4335 // send a full set of 4 PBA messages in a row to set all 32
mjr 38:091e511ce8a0 4336 // outputs.
mjr 38:091e511ce8a0 4337 //
mjr 38:091e511ce8a0 4338 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 4339 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 4340 // wizVal[] entry for each output, and that takes precedence
mjr 63:5cd1a5f3a41b 4341 // over the extended protocol settings when we're in LedWiz
mjr 63:5cd1a5f3a41b 4342 // protocol mode.
mjr 38:091e511ce8a0 4343 //
mjr 38:091e511ce8a0 4344 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 4345 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 4346
mjr 63:5cd1a5f3a41b 4347 // flag that we received an LedWiz message
mjr 63:5cd1a5f3a41b 4348 ledWizMode = true;
mjr 63:5cd1a5f3a41b 4349
mjr 38:091e511ce8a0 4350 // Update all output profile settings
mjr 38:091e511ce8a0 4351 for (int i = 0 ; i < 8 ; ++i)
mjr 38:091e511ce8a0 4352 wizVal[pbaIdx + i] = data[i];
mjr 38:091e511ce8a0 4353
mjr 38:091e511ce8a0 4354 // Update the physical LED state if this is the last bank.
mjr 38:091e511ce8a0 4355 // Note that hosts always send a full set of four PBA
mjr 38:091e511ce8a0 4356 // messages, so there's no need to do a physical update
mjr 38:091e511ce8a0 4357 // until we've received the last bank's PBA message.
mjr 38:091e511ce8a0 4358 if (pbaIdx == 24)
mjr 38:091e511ce8a0 4359 {
mjr 35:e959ffba78fd 4360 updateWizOuts();
mjr 35:e959ffba78fd 4361 if (hc595 != 0)
mjr 35:e959ffba78fd 4362 hc595->update();
mjr 35:e959ffba78fd 4363 pbaIdx = 0;
mjr 35:e959ffba78fd 4364 }
mjr 38:091e511ce8a0 4365 else
mjr 38:091e511ce8a0 4366 pbaIdx += 8;
mjr 38:091e511ce8a0 4367 }
mjr 38:091e511ce8a0 4368 }
mjr 35:e959ffba78fd 4369
mjr 33:d832bcab089e 4370
mjr 38:091e511ce8a0 4371 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 4372 //
mjr 5:a70c0bce770d 4373 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 4374 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 4375 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 4376 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 4377 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 4378 // port outputs.
mjr 5:a70c0bce770d 4379 //
mjr 0:5acbbe3f4cf4 4380 int main(void)
mjr 0:5acbbe3f4cf4 4381 {
mjr 60:f38da020aa13 4382 // say hello to the debug console, in case it's connected
mjr 39:b3815a1c3802 4383 printf("\r\nPinscape Controller starting\r\n");
mjr 60:f38da020aa13 4384
mjr 60:f38da020aa13 4385 // debugging: print memory config info
mjr 59:94eb9265b6d7 4386 // -> no longer very useful, since we use our own custom malloc/new allocator (see xmalloc() above)
mjr 60:f38da020aa13 4387 // {int *a = new int; printf("Stack=%lx, heap=%lx, free=%ld\r\n", (long)&a, (long)a, (long)&a - (long)a);}
mjr 1:d913e0afb2ac 4388
mjr 39:b3815a1c3802 4389 // clear the I2C bus (for the accelerometer)
mjr 35:e959ffba78fd 4390 clear_i2c();
mjr 38:091e511ce8a0 4391
mjr 43:7a6364d82a41 4392 // load the saved configuration (or set factory defaults if no flash
mjr 43:7a6364d82a41 4393 // configuration has ever been saved)
mjr 35:e959ffba78fd 4394 loadConfigFromFlash();
mjr 35:e959ffba78fd 4395
mjr 38:091e511ce8a0 4396 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 4397 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 4398
mjr 33:d832bcab089e 4399 // we're not connected/awake yet
mjr 33:d832bcab089e 4400 bool connected = false;
mjr 40:cc0d9814522b 4401 Timer connectChangeTimer;
mjr 33:d832bcab089e 4402
mjr 35:e959ffba78fd 4403 // create the plunger sensor interface
mjr 35:e959ffba78fd 4404 createPlunger();
mjr 33:d832bcab089e 4405
mjr 60:f38da020aa13 4406 // set up the TLC5940 interface, if these chips are present
mjr 35:e959ffba78fd 4407 init_tlc5940(cfg);
mjr 34:6b981a2afab7 4408
mjr 60:f38da020aa13 4409 // set up 74HC595 interface, if these chips are present
mjr 35:e959ffba78fd 4410 init_hc595(cfg);
mjr 6:cc35eb643e8f 4411
mjr 54:fd77a6b2f76c 4412 // Initialize the LedWiz ports. Note that the ordering here is important:
mjr 54:fd77a6b2f76c 4413 // this has to come after we create the TLC5940 and 74HC595 object instances
mjr 54:fd77a6b2f76c 4414 // (which we just did above), since we need to access those objects to set
mjr 54:fd77a6b2f76c 4415 // up ports assigned to the respective chips.
mjr 35:e959ffba78fd 4416 initLwOut(cfg);
mjr 48:058ace2aed1d 4417
mjr 60:f38da020aa13 4418 // start the TLC5940 refresh cycle clock
mjr 35:e959ffba78fd 4419 if (tlc5940 != 0)
mjr 35:e959ffba78fd 4420 tlc5940->start();
mjr 35:e959ffba78fd 4421
mjr 40:cc0d9814522b 4422 // start the TV timer, if applicable
mjr 40:cc0d9814522b 4423 startTVTimer(cfg);
mjr 48:058ace2aed1d 4424
mjr 35:e959ffba78fd 4425 // initialize the button input ports
mjr 35:e959ffba78fd 4426 bool kbKeys = false;
mjr 35:e959ffba78fd 4427 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 4428
mjr 60:f38da020aa13 4429 // Create the joystick USB client. Note that the USB vendor/product ID
mjr 60:f38da020aa13 4430 // information comes from the saved configuration. Also note that we have
mjr 60:f38da020aa13 4431 // to wait until after initializing the input buttons (which we just did
mjr 60:f38da020aa13 4432 // above) to set up the interface, since the button setup will determine
mjr 60:f38da020aa13 4433 // whether or not we need to present a USB keyboard interface in addition
mjr 60:f38da020aa13 4434 // to the joystick interface.
mjr 51:57eb311faafa 4435 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false,
mjr 51:57eb311faafa 4436 cfg.joystickEnabled, kbKeys);
mjr 51:57eb311faafa 4437
mjr 60:f38da020aa13 4438 // Wait for the USB connection to start up. Show a distinctive diagnostic
mjr 60:f38da020aa13 4439 // flash pattern while waiting.
mjr 51:57eb311faafa 4440 Timer connectTimer;
mjr 51:57eb311faafa 4441 connectTimer.start();
mjr 51:57eb311faafa 4442 while (!js.configured())
mjr 51:57eb311faafa 4443 {
mjr 51:57eb311faafa 4444 // show one short yellow flash at 2-second intervals
mjr 51:57eb311faafa 4445 if (connectTimer.read_us() > 2000000)
mjr 51:57eb311faafa 4446 {
mjr 51:57eb311faafa 4447 // short yellow flash
mjr 51:57eb311faafa 4448 diagLED(1, 1, 0);
mjr 54:fd77a6b2f76c 4449 wait_us(50000);
mjr 51:57eb311faafa 4450 diagLED(0, 0, 0);
mjr 51:57eb311faafa 4451
mjr 51:57eb311faafa 4452 // reset the flash timer
mjr 51:57eb311faafa 4453 connectTimer.reset();
mjr 51:57eb311faafa 4454 }
mjr 51:57eb311faafa 4455 }
mjr 60:f38da020aa13 4456
mjr 60:f38da020aa13 4457 // we're now connected to the host
mjr 54:fd77a6b2f76c 4458 connected = true;
mjr 40:cc0d9814522b 4459
mjr 60:f38da020aa13 4460 // Last report timer for the joytick interface. We use this timer to
mjr 60:f38da020aa13 4461 // throttle the report rate to a pace that's suitable for VP. Without
mjr 60:f38da020aa13 4462 // any artificial delays, we could generate data to send on the joystick
mjr 60:f38da020aa13 4463 // interface on every loop iteration. The loop iteration time depends
mjr 60:f38da020aa13 4464 // on which devices are attached, since most of the work in our main
mjr 60:f38da020aa13 4465 // loop is simply polling our devices. For typical setups, the loop
mjr 60:f38da020aa13 4466 // time ranges from about 0.25ms to 2.5ms; the biggest factor is the
mjr 60:f38da020aa13 4467 // plunger sensor. But VP polls for input about every 10ms, so there's
mjr 60:f38da020aa13 4468 // no benefit in sending data faster than that, and there's some harm,
mjr 60:f38da020aa13 4469 // in that it creates USB overhead (both on the wire and on the host
mjr 60:f38da020aa13 4470 // CPU). We therefore use this timer to pace our reports to roughly
mjr 60:f38da020aa13 4471 // the VP input polling rate. Note that there's no way to actually
mjr 60:f38da020aa13 4472 // synchronize with VP's polling, but there's also no need to, as the
mjr 60:f38da020aa13 4473 // input model is designed to reflect the overall current state at any
mjr 60:f38da020aa13 4474 // given time rather than events or deltas. If VP polls twice between
mjr 60:f38da020aa13 4475 // two updates, it simply sees no state change; if we send two updates
mjr 60:f38da020aa13 4476 // between VP polls, VP simply sees the latest state when it does get
mjr 60:f38da020aa13 4477 // around to polling.
mjr 38:091e511ce8a0 4478 Timer jsReportTimer;
mjr 38:091e511ce8a0 4479 jsReportTimer.start();
mjr 38:091e511ce8a0 4480
mjr 60:f38da020aa13 4481 // Time since we successfully sent a USB report. This is a hacky
mjr 60:f38da020aa13 4482 // workaround to deal with any remaining sporadic problems in the USB
mjr 60:f38da020aa13 4483 // stack. I've been trying to bulletproof the USB code over time to
mjr 60:f38da020aa13 4484 // remove all such problems at their source, but it seems unlikely that
mjr 60:f38da020aa13 4485 // we'll ever get them all. Thus this hack. The idea here is that if
mjr 60:f38da020aa13 4486 // we go too long without successfully sending a USB report, we'll
mjr 60:f38da020aa13 4487 // assume that the connection is broken (and the KL25Z USB hardware
mjr 60:f38da020aa13 4488 // hasn't noticed this), and we'll try taking measures to recover.
mjr 38:091e511ce8a0 4489 Timer jsOKTimer;
mjr 38:091e511ce8a0 4490 jsOKTimer.start();
mjr 35:e959ffba78fd 4491
mjr 55:4db125cd11a0 4492 // Initialize the calibration button and lamp, if enabled. To be enabled,
mjr 55:4db125cd11a0 4493 // the pin has to be assigned to something other than NC (0xFF), AND the
mjr 55:4db125cd11a0 4494 // corresponding feature enable flag has to be set.
mjr 55:4db125cd11a0 4495 DigitalIn *calBtn = 0;
mjr 55:4db125cd11a0 4496 DigitalOut *calBtnLed = 0;
mjr 55:4db125cd11a0 4497
mjr 55:4db125cd11a0 4498 // calibration button input - feature flag 0x01
mjr 55:4db125cd11a0 4499 if ((cfg.plunger.cal.features & 0x01) && cfg.plunger.cal.btn != 0xFF)
mjr 55:4db125cd11a0 4500 calBtn = new DigitalIn(wirePinName(cfg.plunger.cal.btn));
mjr 55:4db125cd11a0 4501
mjr 55:4db125cd11a0 4502 // calibration button indicator lamp output - feature flag 0x02
mjr 55:4db125cd11a0 4503 if ((cfg.plunger.cal.features & 0x02) && cfg.plunger.cal.led != 0xFF)
mjr 55:4db125cd11a0 4504 calBtnLed = new DigitalOut(wirePinName(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 4505
mjr 35:e959ffba78fd 4506 // initialize the calibration button
mjr 1:d913e0afb2ac 4507 calBtnTimer.start();
mjr 35:e959ffba78fd 4508 calBtnState = 0;
mjr 1:d913e0afb2ac 4509
mjr 1:d913e0afb2ac 4510 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 4511 Timer hbTimer;
mjr 1:d913e0afb2ac 4512 hbTimer.start();
mjr 1:d913e0afb2ac 4513 int hb = 0;
mjr 5:a70c0bce770d 4514 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 4515
mjr 1:d913e0afb2ac 4516 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 4517 Timer acTimer;
mjr 1:d913e0afb2ac 4518 acTimer.start();
mjr 1:d913e0afb2ac 4519
mjr 0:5acbbe3f4cf4 4520 // create the accelerometer object
mjr 5:a70c0bce770d 4521 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS, MMA8451_INT_PIN);
mjr 48:058ace2aed1d 4522
mjr 17:ab3cec0c8bf4 4523 // last accelerometer report, in joystick units (we report the nudge
mjr 17:ab3cec0c8bf4 4524 // acceleration via the joystick x & y axes, per the VP convention)
mjr 17:ab3cec0c8bf4 4525 int x = 0, y = 0;
mjr 17:ab3cec0c8bf4 4526
mjr 48:058ace2aed1d 4527 // initialize the plunger sensor
mjr 35:e959ffba78fd 4528 plungerSensor->init();
mjr 10:976666ffa4ef 4529
mjr 48:058ace2aed1d 4530 // set up the ZB Launch Ball monitor
mjr 48:058ace2aed1d 4531 ZBLaunchBall zbLaunchBall;
mjr 48:058ace2aed1d 4532
mjr 54:fd77a6b2f76c 4533 // enable the peripheral chips
mjr 54:fd77a6b2f76c 4534 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 4535 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 4536 if (hc595 != 0)
mjr 54:fd77a6b2f76c 4537 hc595->enable(true);
mjr 43:7a6364d82a41 4538
mjr 1:d913e0afb2ac 4539 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 4540 // host requests
mjr 0:5acbbe3f4cf4 4541 for (;;)
mjr 0:5acbbe3f4cf4 4542 {
mjr 48:058ace2aed1d 4543 // Process incoming reports on the joystick interface. The joystick
mjr 48:058ace2aed1d 4544 // "out" (receive) endpoint is used for LedWiz commands and our
mjr 48:058ace2aed1d 4545 // extended protocol commands. Limit processing time to 5ms to
mjr 48:058ace2aed1d 4546 // ensure we don't starve the input side.
mjr 39:b3815a1c3802 4547 LedWizMsg lwm;
mjr 48:058ace2aed1d 4548 Timer lwt;
mjr 48:058ace2aed1d 4549 lwt.start();
mjr 48:058ace2aed1d 4550 while (js.readLedWizMsg(lwm) && lwt.read_us() < 5000)
mjr 48:058ace2aed1d 4551 handleInputMsg(lwm, js);
mjr 55:4db125cd11a0 4552
mjr 55:4db125cd11a0 4553 // send TLC5940 data updates if applicable
mjr 55:4db125cd11a0 4554 if (tlc5940 != 0)
mjr 55:4db125cd11a0 4555 tlc5940->send();
mjr 1:d913e0afb2ac 4556
mjr 1:d913e0afb2ac 4557 // check for plunger calibration
mjr 17:ab3cec0c8bf4 4558 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 4559 {
mjr 1:d913e0afb2ac 4560 // check the state
mjr 1:d913e0afb2ac 4561 switch (calBtnState)
mjr 0:5acbbe3f4cf4 4562 {
mjr 1:d913e0afb2ac 4563 case 0:
mjr 1:d913e0afb2ac 4564 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 4565 calBtnTimer.reset();
mjr 1:d913e0afb2ac 4566 calBtnState = 1;
mjr 1:d913e0afb2ac 4567 break;
mjr 1:d913e0afb2ac 4568
mjr 1:d913e0afb2ac 4569 case 1:
mjr 1:d913e0afb2ac 4570 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 4571 // passed, start the hold period
mjr 48:058ace2aed1d 4572 if (calBtnTimer.read_us() > 50000)
mjr 1:d913e0afb2ac 4573 calBtnState = 2;
mjr 1:d913e0afb2ac 4574 break;
mjr 1:d913e0afb2ac 4575
mjr 1:d913e0afb2ac 4576 case 2:
mjr 1:d913e0afb2ac 4577 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 4578 // for the entire hold period, move to calibration mode
mjr 48:058ace2aed1d 4579 if (calBtnTimer.read_us() > 2050000)
mjr 1:d913e0afb2ac 4580 {
mjr 1:d913e0afb2ac 4581 // enter calibration mode
mjr 1:d913e0afb2ac 4582 calBtnState = 3;
mjr 9:fd65b0a94720 4583 calBtnTimer.reset();
mjr 35:e959ffba78fd 4584
mjr 44:b5ac89b9cd5d 4585 // begin the plunger calibration limits
mjr 52:8298b2a73eb2 4586 plungerReader.setCalMode(true);
mjr 1:d913e0afb2ac 4587 }
mjr 1:d913e0afb2ac 4588 break;
mjr 2:c174f9ee414a 4589
mjr 2:c174f9ee414a 4590 case 3:
mjr 9:fd65b0a94720 4591 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 4592 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 4593 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 4594 break;
mjr 0:5acbbe3f4cf4 4595 }
mjr 0:5acbbe3f4cf4 4596 }
mjr 1:d913e0afb2ac 4597 else
mjr 1:d913e0afb2ac 4598 {
mjr 2:c174f9ee414a 4599 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 4600 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 4601 // and save the results to flash.
mjr 2:c174f9ee414a 4602 //
mjr 2:c174f9ee414a 4603 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 4604 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 4605 // mode, it simply cancels the attempt.
mjr 48:058ace2aed1d 4606 if (calBtnState == 3 && calBtnTimer.read_us() > 15000000)
mjr 2:c174f9ee414a 4607 {
mjr 2:c174f9ee414a 4608 // exit calibration mode
mjr 1:d913e0afb2ac 4609 calBtnState = 0;
mjr 52:8298b2a73eb2 4610 plungerReader.setCalMode(false);
mjr 2:c174f9ee414a 4611
mjr 6:cc35eb643e8f 4612 // save the updated configuration
mjr 35:e959ffba78fd 4613 cfg.plunger.cal.calibrated = 1;
mjr 35:e959ffba78fd 4614 saveConfigToFlash();
mjr 2:c174f9ee414a 4615 }
mjr 2:c174f9ee414a 4616 else if (calBtnState != 3)
mjr 2:c174f9ee414a 4617 {
mjr 2:c174f9ee414a 4618 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 4619 calBtnState = 0;
mjr 2:c174f9ee414a 4620 }
mjr 1:d913e0afb2ac 4621 }
mjr 1:d913e0afb2ac 4622
mjr 1:d913e0afb2ac 4623 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 4624 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 4625 switch (calBtnState)
mjr 0:5acbbe3f4cf4 4626 {
mjr 1:d913e0afb2ac 4627 case 2:
mjr 1:d913e0afb2ac 4628 // in the hold period - flash the light
mjr 48:058ace2aed1d 4629 newCalBtnLit = ((calBtnTimer.read_us()/250000) & 1);
mjr 1:d913e0afb2ac 4630 break;
mjr 1:d913e0afb2ac 4631
mjr 1:d913e0afb2ac 4632 case 3:
mjr 1:d913e0afb2ac 4633 // calibration mode - show steady on
mjr 1:d913e0afb2ac 4634 newCalBtnLit = true;
mjr 1:d913e0afb2ac 4635 break;
mjr 1:d913e0afb2ac 4636
mjr 1:d913e0afb2ac 4637 default:
mjr 1:d913e0afb2ac 4638 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 4639 newCalBtnLit = false;
mjr 1:d913e0afb2ac 4640 break;
mjr 1:d913e0afb2ac 4641 }
mjr 3:3514575d4f86 4642
mjr 3:3514575d4f86 4643 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 4644 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 4645 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 4646 {
mjr 1:d913e0afb2ac 4647 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 4648 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 4649 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 4650 calBtnLed->write(1);
mjr 38:091e511ce8a0 4651 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 4652 }
mjr 2:c174f9ee414a 4653 else {
mjr 17:ab3cec0c8bf4 4654 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 4655 calBtnLed->write(0);
mjr 38:091e511ce8a0 4656 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 4657 }
mjr 1:d913e0afb2ac 4658 }
mjr 35:e959ffba78fd 4659
mjr 48:058ace2aed1d 4660 // read the plunger sensor
mjr 48:058ace2aed1d 4661 plungerReader.read();
mjr 48:058ace2aed1d 4662
mjr 53:9b2611964afc 4663 // update the ZB Launch Ball status
mjr 53:9b2611964afc 4664 zbLaunchBall.update();
mjr 37:ed52738445fc 4665
mjr 53:9b2611964afc 4666 // process button updates
mjr 53:9b2611964afc 4667 processButtons(cfg);
mjr 53:9b2611964afc 4668
mjr 38:091e511ce8a0 4669 // send a keyboard report if we have new data
mjr 37:ed52738445fc 4670 if (kbState.changed)
mjr 37:ed52738445fc 4671 {
mjr 38:091e511ce8a0 4672 // send a keyboard report
mjr 37:ed52738445fc 4673 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 4674 kbState.changed = false;
mjr 37:ed52738445fc 4675 }
mjr 38:091e511ce8a0 4676
mjr 38:091e511ce8a0 4677 // likewise for the media controller
mjr 37:ed52738445fc 4678 if (mediaState.changed)
mjr 37:ed52738445fc 4679 {
mjr 38:091e511ce8a0 4680 // send a media report
mjr 37:ed52738445fc 4681 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 4682 mediaState.changed = false;
mjr 37:ed52738445fc 4683 }
mjr 38:091e511ce8a0 4684
mjr 38:091e511ce8a0 4685 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 4686 bool jsOK = false;
mjr 55:4db125cd11a0 4687
mjr 55:4db125cd11a0 4688 // figure the current status flags for joystick reports
mjr 55:4db125cd11a0 4689 uint16_t statusFlags =
mjr 55:4db125cd11a0 4690 (cfg.plunger.enabled ? 0x01 : 0x00)
mjr 55:4db125cd11a0 4691 | (nightMode ? 0x02 : 0x00);
mjr 17:ab3cec0c8bf4 4692
mjr 50:40015764bbe6 4693 // If it's been long enough since our last USB status report, send
mjr 50:40015764bbe6 4694 // the new report. VP only polls for input in 10ms intervals, so
mjr 50:40015764bbe6 4695 // there's no benefit in sending reports more frequently than this.
mjr 50:40015764bbe6 4696 // More frequent reporting would only add USB I/O overhead.
mjr 50:40015764bbe6 4697 if (cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 17:ab3cec0c8bf4 4698 {
mjr 17:ab3cec0c8bf4 4699 // read the accelerometer
mjr 17:ab3cec0c8bf4 4700 int xa, ya;
mjr 17:ab3cec0c8bf4 4701 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 4702
mjr 17:ab3cec0c8bf4 4703 // confine the results to our joystick axis range
mjr 17:ab3cec0c8bf4 4704 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 17:ab3cec0c8bf4 4705 if (xa > JOYMAX) xa = JOYMAX;
mjr 17:ab3cec0c8bf4 4706 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 17:ab3cec0c8bf4 4707 if (ya > JOYMAX) ya = JOYMAX;
mjr 17:ab3cec0c8bf4 4708
mjr 17:ab3cec0c8bf4 4709 // store the updated accelerometer coordinates
mjr 17:ab3cec0c8bf4 4710 x = xa;
mjr 17:ab3cec0c8bf4 4711 y = ya;
mjr 17:ab3cec0c8bf4 4712
mjr 48:058ace2aed1d 4713 // Report the current plunger position unless the plunger is
mjr 48:058ace2aed1d 4714 // disabled, or the ZB Launch Ball signal is on. In either of
mjr 48:058ace2aed1d 4715 // those cases, just report a constant 0 value. ZB Launch Ball
mjr 48:058ace2aed1d 4716 // temporarily disables mechanical plunger reporting because it
mjr 21:5048e16cc9ef 4717 // tells us that the table has a Launch Ball button instead of
mjr 48:058ace2aed1d 4718 // a traditional plunger, so we don't want to confuse VP with
mjr 48:058ace2aed1d 4719 // regular plunger inputs.
mjr 48:058ace2aed1d 4720 int z = plungerReader.getPosition();
mjr 53:9b2611964afc 4721 int zrep = (!cfg.plunger.enabled || zbLaunchOn ? 0 : z);
mjr 35:e959ffba78fd 4722
mjr 35:e959ffba78fd 4723 // rotate X and Y according to the device orientation in the cabinet
mjr 35:e959ffba78fd 4724 accelRotate(x, y);
mjr 35:e959ffba78fd 4725
mjr 35:e959ffba78fd 4726 // send the joystick report
mjr 53:9b2611964afc 4727 jsOK = js.update(x, y, zrep, jsButtons, statusFlags);
mjr 21:5048e16cc9ef 4728
mjr 17:ab3cec0c8bf4 4729 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 4730 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 4731 }
mjr 21:5048e16cc9ef 4732
mjr 52:8298b2a73eb2 4733 // If we're in sensor status mode, report all pixel exposure values
mjr 52:8298b2a73eb2 4734 if (reportPlungerStat)
mjr 10:976666ffa4ef 4735 {
mjr 17:ab3cec0c8bf4 4736 // send the report
mjr 53:9b2611964afc 4737 plungerSensor->sendStatusReport(js, reportPlungerStatFlags, reportPlungerStatTime);
mjr 17:ab3cec0c8bf4 4738
mjr 10:976666ffa4ef 4739 // we have satisfied this request
mjr 52:8298b2a73eb2 4740 reportPlungerStat = false;
mjr 10:976666ffa4ef 4741 }
mjr 10:976666ffa4ef 4742
mjr 35:e959ffba78fd 4743 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 4744 // periodically for the sake of the Windows config tool.
mjr 55:4db125cd11a0 4745 if (!cfg.joystickEnabled && jsReportTimer.read_us() > 5000)
mjr 21:5048e16cc9ef 4746 {
mjr 55:4db125cd11a0 4747 jsOK = js.updateStatus(statusFlags);
mjr 38:091e511ce8a0 4748 jsReportTimer.reset();
mjr 38:091e511ce8a0 4749 }
mjr 38:091e511ce8a0 4750
mjr 38:091e511ce8a0 4751 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 4752 if (jsOK)
mjr 38:091e511ce8a0 4753 {
mjr 38:091e511ce8a0 4754 jsOKTimer.reset();
mjr 38:091e511ce8a0 4755 jsOKTimer.start();
mjr 21:5048e16cc9ef 4756 }
mjr 21:5048e16cc9ef 4757
mjr 6:cc35eb643e8f 4758 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 4759 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 4760 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 4761 #endif
mjr 6:cc35eb643e8f 4762
mjr 33:d832bcab089e 4763 // check for connection status changes
mjr 54:fd77a6b2f76c 4764 bool newConnected = js.isConnected() && !js.isSleeping();
mjr 33:d832bcab089e 4765 if (newConnected != connected)
mjr 33:d832bcab089e 4766 {
mjr 54:fd77a6b2f76c 4767 // give it a moment to stabilize
mjr 40:cc0d9814522b 4768 connectChangeTimer.start();
mjr 55:4db125cd11a0 4769 if (connectChangeTimer.read_us() > 1000000)
mjr 33:d832bcab089e 4770 {
mjr 33:d832bcab089e 4771 // note the new status
mjr 33:d832bcab089e 4772 connected = newConnected;
mjr 40:cc0d9814522b 4773
mjr 40:cc0d9814522b 4774 // done with the change timer for this round - reset it for next time
mjr 40:cc0d9814522b 4775 connectChangeTimer.stop();
mjr 40:cc0d9814522b 4776 connectChangeTimer.reset();
mjr 33:d832bcab089e 4777
mjr 54:fd77a6b2f76c 4778 // if we're newly disconnected, clean up for PC suspend mode or power off
mjr 54:fd77a6b2f76c 4779 if (!connected)
mjr 40:cc0d9814522b 4780 {
mjr 54:fd77a6b2f76c 4781 // turn off all outputs
mjr 33:d832bcab089e 4782 allOutputsOff();
mjr 40:cc0d9814522b 4783
mjr 40:cc0d9814522b 4784 // The KL25Z runs off of USB power, so we might (depending on the PC
mjr 40:cc0d9814522b 4785 // and OS configuration) continue to receive power even when the main
mjr 40:cc0d9814522b 4786 // PC power supply is turned off, such as in soft-off or suspend/sleep
mjr 40:cc0d9814522b 4787 // mode. Any external output controller chips (TLC5940, 74HC595) might
mjr 40:cc0d9814522b 4788 // be powered from the PC power supply directly rather than from our
mjr 40:cc0d9814522b 4789 // USB power, so they might be powered off even when we're still running.
mjr 40:cc0d9814522b 4790 // To ensure cleaner startup when the power comes back on, globally
mjr 40:cc0d9814522b 4791 // disable the outputs. The global disable signals come from GPIO lines
mjr 40:cc0d9814522b 4792 // that remain powered as long as the KL25Z is powered, so these modes
mjr 40:cc0d9814522b 4793 // will apply smoothly across power state transitions in the external
mjr 40:cc0d9814522b 4794 // hardware. That is, when the external chips are powered up, they'll
mjr 40:cc0d9814522b 4795 // see the global disable signals as stable voltage inputs immediately,
mjr 40:cc0d9814522b 4796 // which will cause them to suppress any output triggering. This ensures
mjr 40:cc0d9814522b 4797 // that we don't fire any solenoids or flash any lights spuriously when
mjr 40:cc0d9814522b 4798 // the power first comes on.
mjr 40:cc0d9814522b 4799 if (tlc5940 != 0)
mjr 40:cc0d9814522b 4800 tlc5940->enable(false);
mjr 40:cc0d9814522b 4801 if (hc595 != 0)
mjr 40:cc0d9814522b 4802 hc595->enable(false);
mjr 40:cc0d9814522b 4803 }
mjr 33:d832bcab089e 4804 }
mjr 33:d832bcab089e 4805 }
mjr 48:058ace2aed1d 4806
mjr 53:9b2611964afc 4807 // if we have a reboot timer pending, check for completion
mjr 53:9b2611964afc 4808 if (rebootTimer.isRunning() && rebootTimer.read_us() > rebootTime_us)
mjr 53:9b2611964afc 4809 reboot(js);
mjr 53:9b2611964afc 4810
mjr 48:058ace2aed1d 4811 // if we're disconnected, initiate a new connection
mjr 51:57eb311faafa 4812 if (!connected)
mjr 48:058ace2aed1d 4813 {
mjr 54:fd77a6b2f76c 4814 // show USB HAL debug events
mjr 54:fd77a6b2f76c 4815 extern void HAL_DEBUG_PRINTEVENTS(const char *prefix);
mjr 54:fd77a6b2f76c 4816 HAL_DEBUG_PRINTEVENTS(">DISC");
mjr 54:fd77a6b2f76c 4817
mjr 54:fd77a6b2f76c 4818 // show immediate diagnostic feedback
mjr 54:fd77a6b2f76c 4819 js.diagFlash();
mjr 54:fd77a6b2f76c 4820
mjr 54:fd77a6b2f76c 4821 // clear any previous diagnostic LED display
mjr 54:fd77a6b2f76c 4822 diagLED(0, 0, 0);
mjr 51:57eb311faafa 4823
mjr 51:57eb311faafa 4824 // set up a timer to monitor the reboot timeout
mjr 51:57eb311faafa 4825 Timer rebootTimer;
mjr 51:57eb311faafa 4826 rebootTimer.start();
mjr 48:058ace2aed1d 4827
mjr 54:fd77a6b2f76c 4828 // set up a timer for diagnostic displays
mjr 54:fd77a6b2f76c 4829 Timer diagTimer;
mjr 54:fd77a6b2f76c 4830 diagTimer.reset();
mjr 54:fd77a6b2f76c 4831 diagTimer.start();
mjr 54:fd77a6b2f76c 4832
mjr 54:fd77a6b2f76c 4833 // loop until we get our connection back
mjr 54:fd77a6b2f76c 4834 while (!js.isConnected() || js.isSleeping())
mjr 51:57eb311faafa 4835 {
mjr 54:fd77a6b2f76c 4836 // try to recover the connection
mjr 54:fd77a6b2f76c 4837 js.recoverConnection();
mjr 54:fd77a6b2f76c 4838
mjr 55:4db125cd11a0 4839 // send TLC5940 data if necessary
mjr 55:4db125cd11a0 4840 if (tlc5940 != 0)
mjr 55:4db125cd11a0 4841 tlc5940->send();
mjr 55:4db125cd11a0 4842
mjr 54:fd77a6b2f76c 4843 // show a diagnostic flash every couple of seconds
mjr 54:fd77a6b2f76c 4844 if (diagTimer.read_us() > 2000000)
mjr 51:57eb311faafa 4845 {
mjr 54:fd77a6b2f76c 4846 // flush the USB HAL debug events, if in debug mode
mjr 54:fd77a6b2f76c 4847 HAL_DEBUG_PRINTEVENTS(">NC");
mjr 54:fd77a6b2f76c 4848
mjr 54:fd77a6b2f76c 4849 // show diagnostic feedback
mjr 54:fd77a6b2f76c 4850 js.diagFlash();
mjr 51:57eb311faafa 4851
mjr 51:57eb311faafa 4852 // reset the flash timer
mjr 54:fd77a6b2f76c 4853 diagTimer.reset();
mjr 51:57eb311faafa 4854 }
mjr 51:57eb311faafa 4855
mjr 51:57eb311faafa 4856 // if the disconnect reboot timeout has expired, reboot
mjr 51:57eb311faafa 4857 if (cfg.disconnectRebootTimeout != 0
mjr 51:57eb311faafa 4858 && rebootTimer.read() > cfg.disconnectRebootTimeout)
mjr 54:fd77a6b2f76c 4859 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 4860 }
mjr 54:fd77a6b2f76c 4861
mjr 54:fd77a6b2f76c 4862 // if we made it out of that loop alive, we're connected again!
mjr 54:fd77a6b2f76c 4863 connected = true;
mjr 54:fd77a6b2f76c 4864 HAL_DEBUG_PRINTEVENTS(">C");
mjr 54:fd77a6b2f76c 4865
mjr 54:fd77a6b2f76c 4866 // Enable peripheral chips and update them with current output data
mjr 54:fd77a6b2f76c 4867 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 4868 {
mjr 55:4db125cd11a0 4869 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 4870 tlc5940->update(true);
mjr 54:fd77a6b2f76c 4871 }
mjr 54:fd77a6b2f76c 4872 if (hc595 != 0)
mjr 54:fd77a6b2f76c 4873 {
mjr 55:4db125cd11a0 4874 hc595->enable(true);
mjr 54:fd77a6b2f76c 4875 hc595->update(true);
mjr 51:57eb311faafa 4876 }
mjr 48:058ace2aed1d 4877 }
mjr 43:7a6364d82a41 4878
mjr 6:cc35eb643e8f 4879 // provide a visual status indication on the on-board LED
mjr 48:058ace2aed1d 4880 if (calBtnState < 2 && hbTimer.read_us() > 1000000)
mjr 1:d913e0afb2ac 4881 {
mjr 54:fd77a6b2f76c 4882 if (jsOKTimer.read_us() > 1000000)
mjr 38:091e511ce8a0 4883 {
mjr 39:b3815a1c3802 4884 // USB freeze - show red/yellow.
mjr 40:cc0d9814522b 4885 //
mjr 54:fd77a6b2f76c 4886 // It's been more than a second since we successfully sent a joystick
mjr 54:fd77a6b2f76c 4887 // update message. This must mean that something's wrong on the USB
mjr 54:fd77a6b2f76c 4888 // connection, even though we haven't detected an outright disconnect.
mjr 54:fd77a6b2f76c 4889 // Show a distinctive diagnostic LED pattern when this occurs.
mjr 38:091e511ce8a0 4890 hb = !hb;
mjr 38:091e511ce8a0 4891 diagLED(1, hb, 0);
mjr 54:fd77a6b2f76c 4892
mjr 54:fd77a6b2f76c 4893 // If the reboot-on-disconnect option is in effect, treat this condition
mjr 54:fd77a6b2f76c 4894 // as equivalent to a disconnect, since something is obviously wrong
mjr 54:fd77a6b2f76c 4895 // with the USB connection.
mjr 54:fd77a6b2f76c 4896 if (cfg.disconnectRebootTimeout != 0)
mjr 54:fd77a6b2f76c 4897 {
mjr 54:fd77a6b2f76c 4898 // The reboot timeout is in effect. If we've been incommunicado for
mjr 54:fd77a6b2f76c 4899 // longer than the timeout, reboot. If we haven't reached the time
mjr 54:fd77a6b2f76c 4900 // limit, keep running for now, and leave the OK timer running so
mjr 54:fd77a6b2f76c 4901 // that we can continue to monitor this.
mjr 54:fd77a6b2f76c 4902 if (jsOKTimer.read() > cfg.disconnectRebootTimeout)
mjr 54:fd77a6b2f76c 4903 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 4904 }
mjr 54:fd77a6b2f76c 4905 else
mjr 54:fd77a6b2f76c 4906 {
mjr 54:fd77a6b2f76c 4907 // There's no reboot timer, so just keep running with the diagnostic
mjr 54:fd77a6b2f76c 4908 // pattern displayed. Since we're not waiting for any other timed
mjr 54:fd77a6b2f76c 4909 // conditions in this state, stop the timer so that it doesn't
mjr 54:fd77a6b2f76c 4910 // overflow if this condition persists for a long time.
mjr 54:fd77a6b2f76c 4911 jsOKTimer.stop();
mjr 54:fd77a6b2f76c 4912 }
mjr 38:091e511ce8a0 4913 }
mjr 35:e959ffba78fd 4914 else if (cfg.plunger.enabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 4915 {
mjr 6:cc35eb643e8f 4916 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 4917 hb = !hb;
mjr 38:091e511ce8a0 4918 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 4919 }
mjr 6:cc35eb643e8f 4920 else
mjr 6:cc35eb643e8f 4921 {
mjr 6:cc35eb643e8f 4922 // connected - flash blue/green
mjr 2:c174f9ee414a 4923 hb = !hb;
mjr 38:091e511ce8a0 4924 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 4925 }
mjr 1:d913e0afb2ac 4926
mjr 1:d913e0afb2ac 4927 // reset the heartbeat timer
mjr 1:d913e0afb2ac 4928 hbTimer.reset();
mjr 5:a70c0bce770d 4929 ++hbcnt;
mjr 1:d913e0afb2ac 4930 }
mjr 1:d913e0afb2ac 4931 }
mjr 0:5acbbe3f4cf4 4932 }