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 Jan 27 23:47:15 2017 +0000
Revision:
74:822a92bc11d2
Parent:
73:4e8ce0b18915
Child:
75:677892300e7a
SBX/PBX extensions for multiple virtual LedWiz units on client; PWM GPIO update fixes; LedWiz pulse speed settings changed to match real LedWiz

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