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:
Thu Apr 13 23:22:12 2017 +0000
Revision:
83:ea44e193fd55
Parent:
80:94dc2946871b
Child:
84:31e926f4f3bc
Fixed night mode button toggle problem

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