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:
Sat Jan 21 19:48:30 2017 +0000
Revision:
73:4e8ce0b18915
Parent:
72:884207c0aab0
Child:
74:822a92bc11d2
Add protocol commands for TV ON and button testers; add free memory status reporting; improve button scan interrupt speed; reduce button memory footprint slightly; further improve TSL1410R "scan mode 2" speed

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