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:
Fri Apr 22 17:58:35 2016 +0000
Revision:
53:9b2611964afc
Parent:
52:8298b2a73eb2
Child:
54:fd77a6b2f76c
Save some debugging instrumentation to be removed for release

Who changed what in which revision?

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