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 real plunger, button inputs, and feedback device control.

In case you haven't heard of the concept 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 serve as the "backglass" display. A third smaller monitor can serve as the "DMD" (the Dot Matrix Display used for scoring on newer machines), or you can even install a real pinball plasma DMD. A computer 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 hardware.

A few small companies build and sell complete, finished virtual pinball machines, but I think it's more fun as a 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 potentionmeter (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 VirtuaPin kit uses the same KL25Z microcontroller that Pinscape uses, but the rest of its hardware is different and incompatible. In particular, the Pinscape firmware doesn't include support for the IR proximity sensor used in the VirtuaPin plunger kit, so you won't be able to use your plunger device with the Pinscape firmware. In addition, the VirtuaPin setup uses a different set of GPIO pins for the button inputs from the Pinscape defaults, so if you do install the Pinscape firmware, you'll have to go into the Config Tool and reassign all of the buttons to match the VirtuaPin wiring.

Committer:
mjr
Date:
Fri Feb 03 20:50:02 2017 +0000
Revision:
76:7f5912b6340e
Parent:
75:677892300e7a
Child:
77:0b96f6867312
Rework flash driver to make it truly stable (hopefully to 100% reliability); host-loaded configuration; performance improvements; more performance diagnostics.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 51:57eb311faafa 1 /* Copyright 2014, 2016 M J Roberts, MIT License
mjr 5:a70c0bce770d 2 *
mjr 5:a70c0bce770d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mjr 5:a70c0bce770d 4 * and associated documentation files (the "Software"), to deal in the Software without
mjr 5:a70c0bce770d 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mjr 5:a70c0bce770d 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mjr 5:a70c0bce770d 7 * Software is furnished to do so, subject to the following conditions:
mjr 5:a70c0bce770d 8 *
mjr 5:a70c0bce770d 9 * The above copyright notice and this permission notice shall be included in all copies or
mjr 5:a70c0bce770d 10 * substantial portions of the Software.
mjr 5:a70c0bce770d 11 *
mjr 5:a70c0bce770d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mjr 48:058ace2aed1d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILIT Y, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 5:a70c0bce770d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 5:a70c0bce770d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 5:a70c0bce770d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 5:a70c0bce770d 17 */
mjr 5:a70c0bce770d 18
mjr 5:a70c0bce770d 19 //
mjr 35:e959ffba78fd 20 // The Pinscape Controller
mjr 35:e959ffba78fd 21 // A comprehensive input/output controller for virtual pinball machines
mjr 5:a70c0bce770d 22 //
mjr 48:058ace2aed1d 23 // This project implements an I/O controller for virtual pinball cabinets. The
mjr 48:058ace2aed1d 24 // controller's function is to connect Visual Pinball (and other Windows pinball
mjr 48:058ace2aed1d 25 // emulators) with physical devices in the cabinet: buttons, sensors, and
mjr 48:058ace2aed1d 26 // feedback devices that create visual or mechanical effects during play.
mjr 38:091e511ce8a0 27 //
mjr 48:058ace2aed1d 28 // The controller can perform several different functions, which can be used
mjr 38:091e511ce8a0 29 // individually or in any combination:
mjr 5:a70c0bce770d 30 //
mjr 38:091e511ce8a0 31 // - Nudge sensing. This uses the KL25Z's on-board accelerometer to sense the
mjr 38:091e511ce8a0 32 // motion of the cabinet when you nudge it. Visual Pinball and other pinball
mjr 38:091e511ce8a0 33 // emulators on the PC have native handling for this type of input, so that
mjr 38:091e511ce8a0 34 // physical nudges on the cabinet turn into simulated effects on the virtual
mjr 38:091e511ce8a0 35 // ball. The KL25Z measures accelerations as analog readings and is quite
mjr 38:091e511ce8a0 36 // sensitive, so the effect of a nudge on the simulation is proportional
mjr 38:091e511ce8a0 37 // to the strength of the nudge. Accelerations are reported to the PC via a
mjr 38:091e511ce8a0 38 // simulated joystick (using the X and Y axes); you just have to set some
mjr 38:091e511ce8a0 39 // preferences in your pinball software to tell it that an accelerometer
mjr 38:091e511ce8a0 40 // is attached.
mjr 5:a70c0bce770d 41 //
mjr 74:822a92bc11d2 42 // - Plunger position sensing, with multiple sensor options. To use this feature,
mjr 35:e959ffba78fd 43 // you need to choose a sensor and set it up, connect the sensor electrically to
mjr 35:e959ffba78fd 44 // the KL25Z, and configure the Pinscape software on the KL25Z to let it know how
mjr 35:e959ffba78fd 45 // the sensor is hooked up. The Pinscape software monitors the sensor and sends
mjr 35:e959ffba78fd 46 // readings to Visual Pinball via the joystick Z axis. VP and other PC software
mjr 38:091e511ce8a0 47 // have native support for this type of input; as with the nudge setup, you just
mjr 38:091e511ce8a0 48 // have to set some options in VP to activate the plunger.
mjr 17:ab3cec0c8bf4 49 //
mjr 35:e959ffba78fd 50 // The Pinscape software supports optical sensors (the TAOS TSL1410R and TSL1412R
mjr 35:e959ffba78fd 51 // linear sensor arrays) as well as slide potentiometers. The specific equipment
mjr 35:e959ffba78fd 52 // that's supported, along with physical mounting and wiring details, can be found
mjr 35:e959ffba78fd 53 // in the Build Guide.
mjr 35:e959ffba78fd 54 //
mjr 38:091e511ce8a0 55 // Note VP has built-in support for plunger devices like this one, but some VP
mjr 38:091e511ce8a0 56 // tables can't use it without some additional scripting work. The Build Guide has
mjr 38:091e511ce8a0 57 // advice on adjusting tables to add plunger support when necessary.
mjr 5:a70c0bce770d 58 //
mjr 6:cc35eb643e8f 59 // For best results, the plunger sensor should be calibrated. The calibration
mjr 6:cc35eb643e8f 60 // is stored in non-volatile memory on board the KL25Z, so it's only necessary
mjr 6:cc35eb643e8f 61 // to do the calibration once, when you first install everything. (You might
mjr 6:cc35eb643e8f 62 // also want to re-calibrate if you physically remove and reinstall the CCD
mjr 17:ab3cec0c8bf4 63 // sensor or the mechanical plunger, since their alignment shift change slightly
mjr 17:ab3cec0c8bf4 64 // when you put everything back together.) You can optionally install a
mjr 17:ab3cec0c8bf4 65 // dedicated momentary switch or pushbutton to activate the calibration mode;
mjr 17:ab3cec0c8bf4 66 // this is describe in the project documentation. If you don't want to bother
mjr 17:ab3cec0c8bf4 67 // with the extra button, you can also trigger calibration using the Windows
mjr 17:ab3cec0c8bf4 68 // setup software, which you can find on the Pinscape project page.
mjr 6:cc35eb643e8f 69 //
mjr 17:ab3cec0c8bf4 70 // The calibration procedure is described in the project documentation. Briefly,
mjr 17:ab3cec0c8bf4 71 // when you trigger calibration mode, the software will scan the CCD for about
mjr 17:ab3cec0c8bf4 72 // 15 seconds, during which you should simply pull the physical plunger back
mjr 17:ab3cec0c8bf4 73 // all the way, hold it for a moment, and then slowly return it to the rest
mjr 17:ab3cec0c8bf4 74 // position. (DON'T just release it from the retracted position, since that
mjr 17:ab3cec0c8bf4 75 // let it shoot forward too far. We want to measure the range from the park
mjr 17:ab3cec0c8bf4 76 // position to the fully retracted position only.)
mjr 5:a70c0bce770d 77 //
mjr 13:72dda449c3c0 78 // - Button input wiring. 24 of the KL25Z's GPIO ports are mapped as digital inputs
mjr 38:091e511ce8a0 79 // for buttons and switches. You can wire each input to a physical pinball-style
mjr 38:091e511ce8a0 80 // button or switch, such as flipper buttons, Start buttons, coin chute switches,
mjr 38:091e511ce8a0 81 // tilt bobs, and service buttons. Each button can be configured to be reported
mjr 38:091e511ce8a0 82 // to the PC as a joystick button or as a keyboard key (you can select which key
mjr 38:091e511ce8a0 83 // is used for each button).
mjr 13:72dda449c3c0 84 //
mjr 53:9b2611964afc 85 // - LedWiz emulation. The KL25Z can pretend to be an LedWiz device. This lets
mjr 53:9b2611964afc 86 // you connect feedback devices (lights, solenoids, motors) to GPIO ports on the
mjr 53:9b2611964afc 87 // KL25Z, and lets PC software (such as Visual Pinball) control them during game
mjr 53:9b2611964afc 88 // play to create a more immersive playing experience. The Pinscape software
mjr 53:9b2611964afc 89 // presents itself to the host as an LedWiz device and accepts the full LedWiz
mjr 53:9b2611964afc 90 // command set, so software on the PC designed for real LedWiz'es can control
mjr 53:9b2611964afc 91 // attached devices without any modifications.
mjr 5:a70c0bce770d 92 //
mjr 53:9b2611964afc 93 // Even though the software provides a very thorough LedWiz emulation, the KL25Z
mjr 53:9b2611964afc 94 // GPIO hardware design imposes some serious limitations. The big one is that
mjr 53:9b2611964afc 95 // the KL25Z only has 10 PWM channels, meaning that only 10 ports can have
mjr 53:9b2611964afc 96 // varying-intensity outputs (e.g., for controlling the brightness level of an
mjr 53:9b2611964afc 97 // LED or the speed or a motor). You can control more than 10 output ports, but
mjr 53:9b2611964afc 98 // only 10 can have PWM control; the rest are simple "digital" ports that can only
mjr 53:9b2611964afc 99 // be switched fully on or fully off. The second limitation is that the KL25Z
mjr 53:9b2611964afc 100 // just doesn't have that many GPIO ports overall. There are enough to populate
mjr 53:9b2611964afc 101 // all 32 button inputs OR all 32 LedWiz outputs, but not both. The default is
mjr 53:9b2611964afc 102 // to assign 24 buttons and 22 LedWiz ports; you can change this balance to trade
mjr 53:9b2611964afc 103 // off more outputs for fewer inputs, or vice versa. The third limitation is that
mjr 53:9b2611964afc 104 // the KL25Z GPIO pins have *very* tiny amperage limits - just 4mA, which isn't
mjr 53:9b2611964afc 105 // even enough to control a small LED. So in order to connect any kind of feedback
mjr 53:9b2611964afc 106 // device to an output, you *must* build some external circuitry to boost the
mjr 53:9b2611964afc 107 // current handing. The Build Guide has a reference circuit design for this
mjr 53:9b2611964afc 108 // purpose that's simple and inexpensive to build.
mjr 6:cc35eb643e8f 109 //
mjr 26:cb71c4af2912 110 // - Enhanced LedWiz emulation with TLC5940 PWM controller chips. You can attach
mjr 26:cb71c4af2912 111 // external PWM controller chips for controlling device outputs, instead of using
mjr 53:9b2611964afc 112 // the on-board GPIO ports as described above. The software can control a set of
mjr 53:9b2611964afc 113 // daisy-chained TLC5940 chips. Each chip provides 16 PWM outputs, so you just
mjr 53:9b2611964afc 114 // need two of them to get the full complement of 32 output ports of a real LedWiz.
mjr 53:9b2611964afc 115 // You can hook up even more, though. Four chips gives you 64 ports, which should
mjr 53:9b2611964afc 116 // be plenty for nearly any virtual pinball project. To accommodate the larger
mjr 53:9b2611964afc 117 // supply of ports possible with the PWM chips, the controller software provides
mjr 53:9b2611964afc 118 // a custom, extended version of the LedWiz protocol that can handle up to 128
mjr 53:9b2611964afc 119 // ports. PC software designed only for the real LedWiz obviously won't know
mjr 53:9b2611964afc 120 // about the extended protocol and won't be able to take advantage of its extra
mjr 53:9b2611964afc 121 // capabilities, but the latest version of DOF (DirectOutput Framework) *does*
mjr 53:9b2611964afc 122 // know the new language and can take full advantage. Older software will still
mjr 53:9b2611964afc 123 // work, though - the new extensions are all backward compatible, so old software
mjr 53:9b2611964afc 124 // that only knows about the original LedWiz protocol will still work, with the
mjr 53:9b2611964afc 125 // obvious limitation that it can only access the first 32 ports.
mjr 53:9b2611964afc 126 //
mjr 53:9b2611964afc 127 // The Pinscape Expansion Board project (which appeared in early 2016) provides
mjr 53:9b2611964afc 128 // a reference hardware design, with EAGLE circuit board layouts, that takes full
mjr 53:9b2611964afc 129 // advantage of the TLC5940 capability. It lets you create a customized set of
mjr 53:9b2611964afc 130 // outputs with full PWM control and power handling for high-current devices
mjr 53:9b2611964afc 131 // built in to the boards.
mjr 26:cb71c4af2912 132 //
mjr 38:091e511ce8a0 133 // - Night Mode control for output devices. You can connect a switch or button
mjr 38:091e511ce8a0 134 // to the controller to activate "Night Mode", which disables feedback devices
mjr 38:091e511ce8a0 135 // that you designate as noisy. You can designate outputs individually as being
mjr 38:091e511ce8a0 136 // included in this set or not. This is useful if you want to play a game on
mjr 38:091e511ce8a0 137 // your cabinet late at night without waking the kids and annoying the neighbors.
mjr 38:091e511ce8a0 138 //
mjr 38:091e511ce8a0 139 // - TV ON switch. The controller can pulse a relay to turn on your TVs after
mjr 38:091e511ce8a0 140 // power to the cabinet comes on, with a configurable delay timer. This feature
mjr 38:091e511ce8a0 141 // is for TVs that don't turn themselves on automatically when first plugged in.
mjr 38:091e511ce8a0 142 // To use this feature, you have to build some external circuitry to allow the
mjr 38:091e511ce8a0 143 // software to sense the power supply status, and you have to run wires to your
mjr 38:091e511ce8a0 144 // TV's on/off button, which requires opening the case on your TV. The Build
mjr 38:091e511ce8a0 145 // Guide has details on the necessary circuitry and connections to the TV.
mjr 38:091e511ce8a0 146 //
mjr 35:e959ffba78fd 147 //
mjr 35:e959ffba78fd 148 //
mjr 33:d832bcab089e 149 // STATUS LIGHTS: The on-board LED on the KL25Z flashes to indicate the current
mjr 33:d832bcab089e 150 // device status. The flash patterns are:
mjr 6:cc35eb643e8f 151 //
mjr 48:058ace2aed1d 152 // short yellow flash = waiting to connect
mjr 6:cc35eb643e8f 153 //
mjr 48:058ace2aed1d 154 // short red flash = the connection is suspended (the host is in sleep
mjr 48:058ace2aed1d 155 // or suspend mode, the USB cable is unplugged after a connection
mjr 48:058ace2aed1d 156 // has been established)
mjr 48:058ace2aed1d 157 //
mjr 48:058ace2aed1d 158 // two short red flashes = connection lost (the device should immediately
mjr 48:058ace2aed1d 159 // go back to short-yellow "waiting to reconnect" mode when a connection
mjr 48:058ace2aed1d 160 // is lost, so this display shouldn't normally appear)
mjr 6:cc35eb643e8f 161 //
mjr 38:091e511ce8a0 162 // long red/yellow = USB connection problem. The device still has a USB
mjr 48:058ace2aed1d 163 // connection to the host (or so it appears to the device), but data
mjr 48:058ace2aed1d 164 // transmissions are failing.
mjr 38:091e511ce8a0 165 //
mjr 73:4e8ce0b18915 166 // medium blue flash = TV ON delay timer running. This means that the
mjr 73:4e8ce0b18915 167 // power to the secondary PSU has just been turned on, and the TV ON
mjr 73:4e8ce0b18915 168 // timer is waiting for the configured delay time before pulsing the
mjr 73:4e8ce0b18915 169 // TV power button relay. This is only shown if the TV ON feature is
mjr 73:4e8ce0b18915 170 // enabled.
mjr 73:4e8ce0b18915 171 //
mjr 6:cc35eb643e8f 172 // long yellow/green = everything's working, but the plunger hasn't
mjr 38:091e511ce8a0 173 // been calibrated. Follow the calibration procedure described in
mjr 38:091e511ce8a0 174 // the project documentation. This flash mode won't appear if there's
mjr 38:091e511ce8a0 175 // no plunger sensor configured.
mjr 6:cc35eb643e8f 176 //
mjr 38:091e511ce8a0 177 // alternating blue/green = everything's working normally, and plunger
mjr 38:091e511ce8a0 178 // calibration has been completed (or there's no plunger attached)
mjr 10:976666ffa4ef 179 //
mjr 48:058ace2aed1d 180 // fast red/purple = out of memory. The controller halts and displays
mjr 48:058ace2aed1d 181 // this diagnostic code until you manually reset it. If this happens,
mjr 48:058ace2aed1d 182 // it's probably because the configuration is too complex, in which
mjr 48:058ace2aed1d 183 // case the same error will occur after the reset. If it's stuck
mjr 48:058ace2aed1d 184 // in this cycle, you'll have to restore the default configuration
mjr 48:058ace2aed1d 185 // by re-installing the controller software (the Pinscape .bin file).
mjr 10:976666ffa4ef 186 //
mjr 48:058ace2aed1d 187 //
mjr 48:058ace2aed1d 188 // USB PROTOCOL: Most of our USB messaging is through standard USB HID
mjr 48:058ace2aed1d 189 // classes (joystick, keyboard). We also accept control messages on our
mjr 48:058ace2aed1d 190 // primary HID interface "OUT endpoint" using a custom protocol that's
mjr 48:058ace2aed1d 191 // not defined in any USB standards (we do have to provide a USB HID
mjr 48:058ace2aed1d 192 // Report Descriptor for it, but this just describes the protocol as
mjr 48:058ace2aed1d 193 // opaque vendor-defined bytes). The control protocol incorporates the
mjr 48:058ace2aed1d 194 // LedWiz protocol as a subset, and adds our own private extensions.
mjr 48:058ace2aed1d 195 // For full details, see USBProtocol.h.
mjr 33:d832bcab089e 196
mjr 33:d832bcab089e 197
mjr 0:5acbbe3f4cf4 198 #include "mbed.h"
mjr 6:cc35eb643e8f 199 #include "math.h"
mjr 74:822a92bc11d2 200 #include "diags.h"
mjr 48:058ace2aed1d 201 #include "pinscape.h"
mjr 0:5acbbe3f4cf4 202 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 203 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 204 #include "tsl1410r.h"
mjr 1:d913e0afb2ac 205 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 206 #include "crc32.h"
mjr 26:cb71c4af2912 207 #include "TLC5940.h"
mjr 34:6b981a2afab7 208 #include "74HC595.h"
mjr 35:e959ffba78fd 209 #include "nvm.h"
mjr 35:e959ffba78fd 210 #include "plunger.h"
mjr 35:e959ffba78fd 211 #include "ccdSensor.h"
mjr 35:e959ffba78fd 212 #include "potSensor.h"
mjr 35:e959ffba78fd 213 #include "nullSensor.h"
mjr 48:058ace2aed1d 214 #include "TinyDigitalIn.h"
mjr 74:822a92bc11d2 215
mjr 2:c174f9ee414a 216
mjr 21:5048e16cc9ef 217 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 218 #include "config.h"
mjr 17:ab3cec0c8bf4 219
mjr 76:7f5912b6340e 220 // forward declarations
mjr 76:7f5912b6340e 221 static void waitPlungerIdle(void);
mjr 53:9b2611964afc 222
mjr 53:9b2611964afc 223 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 224 //
mjr 53:9b2611964afc 225 // OpenSDA module identifier. This is for the benefit of the Windows
mjr 53:9b2611964afc 226 // configuration tool. When the config tool installs a .bin file onto
mjr 53:9b2611964afc 227 // the KL25Z, it will first find the sentinel string within the .bin file,
mjr 53:9b2611964afc 228 // and patch the "\0" bytes that follow the sentinel string with the
mjr 53:9b2611964afc 229 // OpenSDA module ID data. This allows us to report the OpenSDA
mjr 53:9b2611964afc 230 // identifiers back to the host system via USB, which in turn allows the
mjr 53:9b2611964afc 231 // config tool to figure out which OpenSDA MSD (mass storage device - a
mjr 53:9b2611964afc 232 // virtual disk drive) correlates to which Pinscape controller USB
mjr 53:9b2611964afc 233 // interface.
mjr 53:9b2611964afc 234 //
mjr 53:9b2611964afc 235 // This is only important if multiple Pinscape devices are attached to
mjr 53:9b2611964afc 236 // the same host. There doesn't seem to be any other way to figure out
mjr 53:9b2611964afc 237 // which OpenSDA MSD corresponds to which KL25Z USB interface; the OpenSDA
mjr 53:9b2611964afc 238 // MSD doesn't report the KL25Z CPU ID anywhere, and the KL25Z doesn't
mjr 53:9b2611964afc 239 // have any way to learn about the OpenSDA module it's connected to. The
mjr 53:9b2611964afc 240 // only way to pass this information to the KL25Z side that I can come up
mjr 53:9b2611964afc 241 // with is to have the Windows host embed it in the .bin file before
mjr 53:9b2611964afc 242 // downloading it to the OpenSDA MSD.
mjr 53:9b2611964afc 243 //
mjr 53:9b2611964afc 244 // We initialize the const data buffer (the part after the sentinel string)
mjr 53:9b2611964afc 245 // with all "\0" bytes, so that's what will be in the executable image that
mjr 53:9b2611964afc 246 // comes out of the mbed compiler. If you manually install the resulting
mjr 53:9b2611964afc 247 // .bin file onto the KL25Z (via the Windows desktop, say), the "\0" bytes
mjr 53:9b2611964afc 248 // will stay this way and read as all 0's at run-time. Since a real TUID
mjr 53:9b2611964afc 249 // would never be all 0's, that tells us that we were never patched and
mjr 53:9b2611964afc 250 // thus don't have any information on the OpenSDA module.
mjr 53:9b2611964afc 251 //
mjr 53:9b2611964afc 252 const char *getOpenSDAID()
mjr 53:9b2611964afc 253 {
mjr 53:9b2611964afc 254 #define OPENSDA_PREFIX "///Pinscape.OpenSDA.TUID///"
mjr 53:9b2611964afc 255 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 256 const size_t OpenSDA_prefix_length = sizeof(OPENSDA_PREFIX) - 1;
mjr 53:9b2611964afc 257
mjr 53:9b2611964afc 258 return OpenSDA + OpenSDA_prefix_length;
mjr 53:9b2611964afc 259 }
mjr 53:9b2611964afc 260
mjr 53:9b2611964afc 261 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 262 //
mjr 53:9b2611964afc 263 // Build ID. We use the date and time of compiling the program as a build
mjr 53:9b2611964afc 264 // identifier. It would be a little nicer to use a simple serial number
mjr 53:9b2611964afc 265 // instead, but the mbed platform doesn't have a way to automate that. The
mjr 53:9b2611964afc 266 // timestamp is a pretty good proxy for a serial number in that it will
mjr 53:9b2611964afc 267 // naturally increase on each new build, which is the primary property we
mjr 53:9b2611964afc 268 // want from this.
mjr 53:9b2611964afc 269 //
mjr 53:9b2611964afc 270 // As with the embedded OpenSDA ID, we store the build timestamp with a
mjr 53:9b2611964afc 271 // sentinel string prefix, to allow automated tools to find the static data
mjr 53:9b2611964afc 272 // in the .bin file by searching for the sentinel string. In contrast to
mjr 53:9b2611964afc 273 // the OpenSDA ID, the value we store here is for tools to extract rather
mjr 53:9b2611964afc 274 // than store, since we automatically populate it via the preprocessor
mjr 53:9b2611964afc 275 // macros.
mjr 53:9b2611964afc 276 //
mjr 53:9b2611964afc 277 const char *getBuildID()
mjr 53:9b2611964afc 278 {
mjr 53:9b2611964afc 279 #define BUILDID_PREFIX "///Pinscape.Build.ID///"
mjr 53:9b2611964afc 280 static const char BuildID[] = BUILDID_PREFIX __DATE__ " " __TIME__ "///";
mjr 53:9b2611964afc 281 const size_t BuildID_prefix_length = sizeof(BUILDID_PREFIX) - 1;
mjr 53:9b2611964afc 282
mjr 53:9b2611964afc 283 return BuildID + BuildID_prefix_length;
mjr 53:9b2611964afc 284 }
mjr 53:9b2611964afc 285
mjr 74:822a92bc11d2 286 // --------------------------------------------------------------------------
mjr 74:822a92bc11d2 287 // Main loop iteration timing statistics. Collected only if
mjr 74:822a92bc11d2 288 // ENABLE_DIAGNOSTICS is set in diags.h.
mjr 76:7f5912b6340e 289 #if ENABLE_DIAGNOSTICS
mjr 76:7f5912b6340e 290 uint64_t mainLoopIterTime, mainLoopIterCheckpt[15], mainLoopIterCount;
mjr 76:7f5912b6340e 291 uint64_t mainLoopMsgTime, mainLoopMsgCount;
mjr 76:7f5912b6340e 292 Timer mainLoopTimer;
mjr 76:7f5912b6340e 293 #endif
mjr 76:7f5912b6340e 294
mjr 53:9b2611964afc 295
mjr 48:058ace2aed1d 296 // --------------------------------------------------------------------------
mjr 48:058ace2aed1d 297 //
mjr 59:94eb9265b6d7 298 // Custom memory allocator. We use our own version of malloc() for more
mjr 59:94eb9265b6d7 299 // efficient memory usage, and to provide diagnostics if we run out of heap.
mjr 48:058ace2aed1d 300 //
mjr 59:94eb9265b6d7 301 // We can implement a more efficient malloc than the library can because we
mjr 59:94eb9265b6d7 302 // can make an assumption that the library can't: allocations are permanent.
mjr 59:94eb9265b6d7 303 // The normal malloc has to assume that allocations can be freed, so it has
mjr 59:94eb9265b6d7 304 // to track blocks individually. For the purposes of this program, though,
mjr 59:94eb9265b6d7 305 // we don't have to do this because virtually all of our allocations are
mjr 59:94eb9265b6d7 306 // de facto permanent. We only allocate dyanmic memory during setup, and
mjr 59:94eb9265b6d7 307 // once we set things up, we never delete anything. This means that we can
mjr 59:94eb9265b6d7 308 // allocate memory in bare blocks without any bookkeeping overhead.
mjr 59:94eb9265b6d7 309 //
mjr 59:94eb9265b6d7 310 // In addition, we can make a much larger overall pool of memory available
mjr 59:94eb9265b6d7 311 // in a custom allocator. The mbed library malloc() seems to have a pool
mjr 59:94eb9265b6d7 312 // of about 3K to work with, even though there's really about 9K of RAM
mjr 59:94eb9265b6d7 313 // left over after counting the static writable data and reserving space
mjr 59:94eb9265b6d7 314 // for a reasonable stack. I haven't looked at the mbed malloc to see why
mjr 59:94eb9265b6d7 315 // they're so stingy, but it appears from empirical testing that we can
mjr 59:94eb9265b6d7 316 // create a static array up to about 9K before things get crashy.
mjr 59:94eb9265b6d7 317
mjr 73:4e8ce0b18915 318 // Dynamic memory pool. We'll reserve space for all dynamic
mjr 73:4e8ce0b18915 319 // allocations by creating a simple C array of bytes. The size
mjr 73:4e8ce0b18915 320 // of this array is the maximum number of bytes we can allocate
mjr 73:4e8ce0b18915 321 // with malloc or operator 'new'.
mjr 73:4e8ce0b18915 322 //
mjr 73:4e8ce0b18915 323 // The maximum safe size for this array is, in essence, the
mjr 73:4e8ce0b18915 324 // amount of physical KL25Z RAM left over after accounting for
mjr 73:4e8ce0b18915 325 // static data throughout the rest of the program, the run-time
mjr 73:4e8ce0b18915 326 // stack, and any other space reserved for compiler or MCU
mjr 73:4e8ce0b18915 327 // overhead. Unfortunately, it's not straightforward to
mjr 73:4e8ce0b18915 328 // determine this analytically. The big complication is that
mjr 73:4e8ce0b18915 329 // the minimum stack size isn't easily predictable, as the stack
mjr 73:4e8ce0b18915 330 // grows according to what the program does. In addition, the
mjr 73:4e8ce0b18915 331 // mbed platform tools don't give us detailed data on the
mjr 73:4e8ce0b18915 332 // compiler/linker memory map. All we get is a generic total
mjr 73:4e8ce0b18915 333 // RAM requirement, which doesn't necessarily account for all
mjr 73:4e8ce0b18915 334 // overhead (e.g., gaps inserted to get proper alignment for
mjr 73:4e8ce0b18915 335 // particular memory blocks).
mjr 73:4e8ce0b18915 336 //
mjr 73:4e8ce0b18915 337 // A very rough estimate: the total RAM size reported by the
mjr 73:4e8ce0b18915 338 // linker is about 3.5K (currently - that can obviously change
mjr 73:4e8ce0b18915 339 // as the project evolves) out of 16K total. Assuming about a
mjr 73:4e8ce0b18915 340 // 3K stack, that leaves in the ballpark of 10K. Empirically,
mjr 73:4e8ce0b18915 341 // that seems pretty close. In testing, we start to see some
mjr 73:4e8ce0b18915 342 // instability at 10K, while 9K seems safe. To be conservative,
mjr 73:4e8ce0b18915 343 // we'll reduce this to 8K.
mjr 73:4e8ce0b18915 344 //
mjr 73:4e8ce0b18915 345 // Our measured total usage in the base configuration (22 GPIO
mjr 73:4e8ce0b18915 346 // output ports, TSL1410R plunger sensor) is about 4000 bytes.
mjr 73:4e8ce0b18915 347 // A pretty fully decked-out configuration (121 output ports,
mjr 73:4e8ce0b18915 348 // with 8 TLC5940 chips and 3 74HC595 chips, plus the TSL1412R
mjr 73:4e8ce0b18915 349 // sensor with the higher pixel count, and all expansion board
mjr 73:4e8ce0b18915 350 // features enabled) comes to about 6700 bytes. That leaves
mjr 73:4e8ce0b18915 351 // us with about 1.5K free out of our 8K, so we still have a
mjr 73:4e8ce0b18915 352 // little more headroom for future expansion.
mjr 73:4e8ce0b18915 353 //
mjr 73:4e8ce0b18915 354 // For comparison, the standard mbed malloc() runs out of
mjr 73:4e8ce0b18915 355 // memory at about 6K. That's what led to this custom malloc:
mjr 73:4e8ce0b18915 356 // we can just fit the base configuration into that 4K, but
mjr 73:4e8ce0b18915 357 // it's not enough space for more complex setups. There's
mjr 73:4e8ce0b18915 358 // still a little room for squeezing out unnecessary space
mjr 73:4e8ce0b18915 359 // from the mbed library code, but at this point I'd prefer
mjr 73:4e8ce0b18915 360 // to treat that as a last resort, since it would mean having
mjr 73:4e8ce0b18915 361 // to fork private copies of the libraries.
mjr 73:4e8ce0b18915 362 static const size_t XMALLOC_POOL_SIZE = 8*1024;
mjr 73:4e8ce0b18915 363 static char xmalloc_pool[XMALLOC_POOL_SIZE];
mjr 73:4e8ce0b18915 364 static char *xmalloc_nxt = xmalloc_pool;
mjr 73:4e8ce0b18915 365 static size_t xmalloc_rem = XMALLOC_POOL_SIZE;
mjr 73:4e8ce0b18915 366
mjr 48:058ace2aed1d 367 void *xmalloc(size_t siz)
mjr 48:058ace2aed1d 368 {
mjr 59:94eb9265b6d7 369 // align to a 4-byte increment
mjr 59:94eb9265b6d7 370 siz = (siz + 3) & ~3;
mjr 59:94eb9265b6d7 371
mjr 59:94eb9265b6d7 372 // If insufficient memory is available, halt and show a fast red/purple
mjr 59:94eb9265b6d7 373 // diagnostic flash. We don't want to return, since we assume throughout
mjr 59:94eb9265b6d7 374 // the program that all memory allocations must succeed. Note that this
mjr 59:94eb9265b6d7 375 // is generally considered bad programming practice in applications on
mjr 59:94eb9265b6d7 376 // "real" computers, but for the purposes of this microcontroller app,
mjr 59:94eb9265b6d7 377 // there's no point in checking for failed allocations individually
mjr 59:94eb9265b6d7 378 // because there's no way to recover from them. It's better in this
mjr 59:94eb9265b6d7 379 // context to handle failed allocations as fatal errors centrally. We
mjr 59:94eb9265b6d7 380 // can't recover from these automatically, so we have to resort to user
mjr 59:94eb9265b6d7 381 // intervention, which we signal with the diagnostic LED flashes.
mjr 73:4e8ce0b18915 382 if (siz > xmalloc_rem)
mjr 59:94eb9265b6d7 383 {
mjr 59:94eb9265b6d7 384 // halt with the diagnostic display (by looping forever)
mjr 59:94eb9265b6d7 385 for (;;)
mjr 59:94eb9265b6d7 386 {
mjr 59:94eb9265b6d7 387 diagLED(1, 0, 0);
mjr 59:94eb9265b6d7 388 wait_us(200000);
mjr 59:94eb9265b6d7 389 diagLED(1, 0, 1);
mjr 59:94eb9265b6d7 390 wait_us(200000);
mjr 59:94eb9265b6d7 391 }
mjr 59:94eb9265b6d7 392 }
mjr 48:058ace2aed1d 393
mjr 59:94eb9265b6d7 394 // get the next free location from the pool to return
mjr 73:4e8ce0b18915 395 char *ret = xmalloc_nxt;
mjr 59:94eb9265b6d7 396
mjr 59:94eb9265b6d7 397 // advance the pool pointer and decrement the remaining size counter
mjr 73:4e8ce0b18915 398 xmalloc_nxt += siz;
mjr 73:4e8ce0b18915 399 xmalloc_rem -= siz;
mjr 59:94eb9265b6d7 400
mjr 59:94eb9265b6d7 401 // return the allocated block
mjr 59:94eb9265b6d7 402 return ret;
mjr 73:4e8ce0b18915 403 };
mjr 73:4e8ce0b18915 404
mjr 73:4e8ce0b18915 405 // our malloc() replacement
mjr 48:058ace2aed1d 406
mjr 59:94eb9265b6d7 407 // Overload operator new to call our custom malloc. This ensures that
mjr 59:94eb9265b6d7 408 // all 'new' allocations throughout the program (including library code)
mjr 59:94eb9265b6d7 409 // go through our private allocator.
mjr 48:058ace2aed1d 410 void *operator new(size_t siz) { return xmalloc(siz); }
mjr 48:058ace2aed1d 411 void *operator new[](size_t siz) { return xmalloc(siz); }
mjr 5:a70c0bce770d 412
mjr 59:94eb9265b6d7 413 // Since we don't do bookkeeping to track released memory, 'delete' does
mjr 59:94eb9265b6d7 414 // nothing. In actual testing, this routine appears to never be called.
mjr 59:94eb9265b6d7 415 // If it *is* ever called, it will simply leave the block in place, which
mjr 59:94eb9265b6d7 416 // will make it unavailable for re-use but will otherwise be harmless.
mjr 59:94eb9265b6d7 417 void operator delete(void *ptr) { }
mjr 59:94eb9265b6d7 418
mjr 59:94eb9265b6d7 419
mjr 5:a70c0bce770d 420 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 421 //
mjr 38:091e511ce8a0 422 // Forward declarations
mjr 38:091e511ce8a0 423 //
mjr 38:091e511ce8a0 424 void setNightMode(bool on);
mjr 38:091e511ce8a0 425 void toggleNightMode();
mjr 38:091e511ce8a0 426
mjr 38:091e511ce8a0 427 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 428 // utilities
mjr 17:ab3cec0c8bf4 429
mjr 26:cb71c4af2912 430 // floating point square of a number
mjr 26:cb71c4af2912 431 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 432
mjr 26:cb71c4af2912 433 // floating point rounding
mjr 26:cb71c4af2912 434 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 435
mjr 17:ab3cec0c8bf4 436
mjr 33:d832bcab089e 437 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 438 //
mjr 40:cc0d9814522b 439 // Extended verison of Timer class. This adds the ability to interrogate
mjr 40:cc0d9814522b 440 // the running state.
mjr 40:cc0d9814522b 441 //
mjr 40:cc0d9814522b 442 class Timer2: public Timer
mjr 40:cc0d9814522b 443 {
mjr 40:cc0d9814522b 444 public:
mjr 40:cc0d9814522b 445 Timer2() : running(false) { }
mjr 40:cc0d9814522b 446
mjr 40:cc0d9814522b 447 void start() { running = true; Timer::start(); }
mjr 40:cc0d9814522b 448 void stop() { running = false; Timer::stop(); }
mjr 40:cc0d9814522b 449
mjr 40:cc0d9814522b 450 bool isRunning() const { return running; }
mjr 40:cc0d9814522b 451
mjr 40:cc0d9814522b 452 private:
mjr 40:cc0d9814522b 453 bool running;
mjr 40:cc0d9814522b 454 };
mjr 40:cc0d9814522b 455
mjr 53:9b2611964afc 456
mjr 53:9b2611964afc 457 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 458 //
mjr 53:9b2611964afc 459 // Reboot timer. When we have a deferred reboot operation pending, we
mjr 53:9b2611964afc 460 // set the target time and start the timer.
mjr 53:9b2611964afc 461 Timer2 rebootTimer;
mjr 53:9b2611964afc 462 long rebootTime_us;
mjr 53:9b2611964afc 463
mjr 40:cc0d9814522b 464 // --------------------------------------------------------------------------
mjr 40:cc0d9814522b 465 //
mjr 33:d832bcab089e 466 // USB product version number
mjr 5:a70c0bce770d 467 //
mjr 47:df7a88cd249c 468 const uint16_t USB_VERSION_NO = 0x000A;
mjr 33:d832bcab089e 469
mjr 33:d832bcab089e 470 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 471 //
mjr 6:cc35eb643e8f 472 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 473 //
mjr 6:cc35eb643e8f 474 #define JOYMAX 4096
mjr 6:cc35eb643e8f 475
mjr 9:fd65b0a94720 476
mjr 17:ab3cec0c8bf4 477 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 478 //
mjr 40:cc0d9814522b 479 // Wire protocol value translations. These translate byte values to and
mjr 40:cc0d9814522b 480 // from the USB protocol to local native format.
mjr 35:e959ffba78fd 481 //
mjr 35:e959ffba78fd 482
mjr 35:e959ffba78fd 483 // unsigned 16-bit integer
mjr 35:e959ffba78fd 484 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 485 {
mjr 35:e959ffba78fd 486 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 487 }
mjr 40:cc0d9814522b 488 inline void ui16Wire(uint8_t *b, uint16_t val)
mjr 40:cc0d9814522b 489 {
mjr 40:cc0d9814522b 490 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 491 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 492 }
mjr 35:e959ffba78fd 493
mjr 35:e959ffba78fd 494 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 495 {
mjr 35:e959ffba78fd 496 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 497 }
mjr 40:cc0d9814522b 498 inline void i16Wire(uint8_t *b, int16_t val)
mjr 40:cc0d9814522b 499 {
mjr 40:cc0d9814522b 500 ui16Wire(b, (uint16_t)val);
mjr 40:cc0d9814522b 501 }
mjr 35:e959ffba78fd 502
mjr 35:e959ffba78fd 503 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 504 {
mjr 35:e959ffba78fd 505 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 506 }
mjr 40:cc0d9814522b 507 inline void ui32Wire(uint8_t *b, uint32_t val)
mjr 40:cc0d9814522b 508 {
mjr 40:cc0d9814522b 509 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 510 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 511 b[2] = (uint8_t)((val >> 16) & 0xff);
mjr 40:cc0d9814522b 512 b[3] = (uint8_t)((val >> 24) & 0xff);
mjr 40:cc0d9814522b 513 }
mjr 35:e959ffba78fd 514
mjr 35:e959ffba78fd 515 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 516 {
mjr 35:e959ffba78fd 517 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 518 }
mjr 35:e959ffba78fd 519
mjr 53:9b2611964afc 520 // Convert "wire" (USB) pin codes to/from PinName values.
mjr 53:9b2611964afc 521 //
mjr 53:9b2611964afc 522 // The internal mbed PinName format is
mjr 53:9b2611964afc 523 //
mjr 53:9b2611964afc 524 // ((port) << PORT_SHIFT) | (pin << 2) // MBED FORMAT
mjr 53:9b2611964afc 525 //
mjr 53:9b2611964afc 526 // where 'port' is 0-4 for Port A to Port E, and 'pin' is
mjr 53:9b2611964afc 527 // 0 to 31. E.g., E31 is (4 << PORT_SHIFT) | (31<<2).
mjr 53:9b2611964afc 528 //
mjr 53:9b2611964afc 529 // We remap this to our more compact wire format where each
mjr 53:9b2611964afc 530 // pin name fits in 8 bits:
mjr 53:9b2611964afc 531 //
mjr 53:9b2611964afc 532 // ((port) << 5) | pin) // WIRE FORMAT
mjr 53:9b2611964afc 533 //
mjr 53:9b2611964afc 534 // E.g., E31 is (4 << 5) | 31.
mjr 53:9b2611964afc 535 //
mjr 53:9b2611964afc 536 // Wire code FF corresponds to PinName NC (not connected).
mjr 53:9b2611964afc 537 //
mjr 53:9b2611964afc 538 inline PinName wirePinName(uint8_t c)
mjr 35:e959ffba78fd 539 {
mjr 53:9b2611964afc 540 if (c == 0xFF)
mjr 53:9b2611964afc 541 return NC; // 0xFF -> NC
mjr 53:9b2611964afc 542 else
mjr 53:9b2611964afc 543 return PinName(
mjr 53:9b2611964afc 544 (int(c & 0xE0) << (PORT_SHIFT - 5)) // top three bits are the port
mjr 53:9b2611964afc 545 | (int(c & 0x1F) << 2)); // bottom five bits are pin
mjr 40:cc0d9814522b 546 }
mjr 40:cc0d9814522b 547 inline void pinNameWire(uint8_t *b, PinName n)
mjr 40:cc0d9814522b 548 {
mjr 53:9b2611964afc 549 *b = PINNAME_TO_WIRE(n);
mjr 35:e959ffba78fd 550 }
mjr 35:e959ffba78fd 551
mjr 35:e959ffba78fd 552
mjr 35:e959ffba78fd 553 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 554 //
mjr 38:091e511ce8a0 555 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 556 //
mjr 38:091e511ce8a0 557 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 558 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 559 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 560 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 561 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 562 // SPI capability.
mjr 38:091e511ce8a0 563 //
mjr 38:091e511ce8a0 564 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 565
mjr 73:4e8ce0b18915 566 // Power on timer state for diagnostics. We flash the blue LED when
mjr 73:4e8ce0b18915 567 // nothing else is going on. State 0-1 = off, 2-3 = on
mjr 73:4e8ce0b18915 568 uint8_t powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 569
mjr 38:091e511ce8a0 570 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 571 // on, and -1 is no change (leaves the current setting intact).
mjr 73:4e8ce0b18915 572 static uint8_t diagLEDState = 0;
mjr 38:091e511ce8a0 573 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 574 {
mjr 73:4e8ce0b18915 575 // remember the new state
mjr 73:4e8ce0b18915 576 diagLEDState = r | (g << 1) | (b << 2);
mjr 73:4e8ce0b18915 577
mjr 73:4e8ce0b18915 578 // if turning everything off, use the power timer state instead,
mjr 73:4e8ce0b18915 579 // applying it to the blue LED
mjr 73:4e8ce0b18915 580 if (diagLEDState == 0)
mjr 73:4e8ce0b18915 581 b = (powerTimerDiagState >= 2);
mjr 73:4e8ce0b18915 582
mjr 73:4e8ce0b18915 583 // set the new state
mjr 38:091e511ce8a0 584 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 585 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 586 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 587 }
mjr 38:091e511ce8a0 588
mjr 73:4e8ce0b18915 589 // update the LEDs with the current state
mjr 73:4e8ce0b18915 590 void diagLED(void)
mjr 73:4e8ce0b18915 591 {
mjr 73:4e8ce0b18915 592 diagLED(
mjr 73:4e8ce0b18915 593 diagLEDState & 0x01,
mjr 73:4e8ce0b18915 594 (diagLEDState >> 1) & 0x01,
mjr 73:4e8ce0b18915 595 (diagLEDState >> 1) & 0x02);
mjr 73:4e8ce0b18915 596 }
mjr 73:4e8ce0b18915 597
mjr 38:091e511ce8a0 598 // check an output port assignment to see if it conflicts with
mjr 38:091e511ce8a0 599 // an on-board LED segment
mjr 38:091e511ce8a0 600 struct LedSeg
mjr 38:091e511ce8a0 601 {
mjr 38:091e511ce8a0 602 bool r, g, b;
mjr 38:091e511ce8a0 603 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 604
mjr 38:091e511ce8a0 605 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 606 {
mjr 38:091e511ce8a0 607 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 608 // our on-board LED segments
mjr 38:091e511ce8a0 609 int t = pc.typ;
mjr 38:091e511ce8a0 610 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 38:091e511ce8a0 611 {
mjr 38:091e511ce8a0 612 // it's a GPIO port - check for a matching pin assignment
mjr 38:091e511ce8a0 613 PinName pin = wirePinName(pc.pin);
mjr 38:091e511ce8a0 614 if (pin == LED1)
mjr 38:091e511ce8a0 615 r = true;
mjr 38:091e511ce8a0 616 else if (pin == LED2)
mjr 38:091e511ce8a0 617 g = true;
mjr 38:091e511ce8a0 618 else if (pin == LED3)
mjr 38:091e511ce8a0 619 b = true;
mjr 38:091e511ce8a0 620 }
mjr 38:091e511ce8a0 621 }
mjr 38:091e511ce8a0 622 };
mjr 38:091e511ce8a0 623
mjr 38:091e511ce8a0 624 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 625 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 626 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 627 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 628 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 629 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 630 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 631 {
mjr 38:091e511ce8a0 632 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 633 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 634 LedSeg l;
mjr 38:091e511ce8a0 635 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 636 l.check(cfg.outPort[i]);
mjr 38:091e511ce8a0 637
mjr 38:091e511ce8a0 638 // We now know which segments are taken for LedWiz use and which
mjr 38:091e511ce8a0 639 // are free. Create diagnostic ports for the ones not claimed for
mjr 38:091e511ce8a0 640 // LedWiz use.
mjr 38:091e511ce8a0 641 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 642 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 643 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 644 }
mjr 38:091e511ce8a0 645
mjr 38:091e511ce8a0 646
mjr 38:091e511ce8a0 647 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 648 //
mjr 76:7f5912b6340e 649 // LedWiz emulation
mjr 76:7f5912b6340e 650 //
mjr 76:7f5912b6340e 651
mjr 76:7f5912b6340e 652 // LedWiz output states.
mjr 76:7f5912b6340e 653 //
mjr 76:7f5912b6340e 654 // The LedWiz protocol has two separate control axes for each output.
mjr 76:7f5912b6340e 655 // One axis is its on/off state; the other is its "profile" state, which
mjr 76:7f5912b6340e 656 // is either a fixed brightness or a blinking pattern for the light.
mjr 76:7f5912b6340e 657 // The two axes are independent.
mjr 76:7f5912b6340e 658 //
mjr 76:7f5912b6340e 659 // Even though the original LedWiz protocol can only access 32 ports, we
mjr 76:7f5912b6340e 660 // maintain LedWiz state for every port, even if we have more than 32. Our
mjr 76:7f5912b6340e 661 // extended protocol allows the client to send LedWiz-style messages that
mjr 76:7f5912b6340e 662 // control any set of ports. A replacement LEDWIZ.DLL can make a single
mjr 76:7f5912b6340e 663 // Pinscape unit look like multiple virtual LedWiz units to legacy clients,
mjr 76:7f5912b6340e 664 // allowing them to control all of our ports. The clients will still be
mjr 76:7f5912b6340e 665 // using LedWiz-style states to control the ports, so we need to support
mjr 76:7f5912b6340e 666 // the LedWiz scheme with separate on/off and brightness control per port.
mjr 76:7f5912b6340e 667
mjr 76:7f5912b6340e 668 // On/off state for each LedWiz output
mjr 76:7f5912b6340e 669 static uint8_t *wizOn;
mjr 76:7f5912b6340e 670
mjr 76:7f5912b6340e 671 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 76:7f5912b6340e 672 // for each LedWiz output. If the output was last updated through an
mjr 76:7f5912b6340e 673 // LedWiz protocol message, it will have one of these values:
mjr 76:7f5912b6340e 674 //
mjr 76:7f5912b6340e 675 // 0-48 = fixed brightness 0% to 100%
mjr 76:7f5912b6340e 676 // 49 = fixed brightness 100% (equivalent to 48)
mjr 76:7f5912b6340e 677 // 129 = ramp up / ramp down
mjr 76:7f5912b6340e 678 // 130 = flash on / off
mjr 76:7f5912b6340e 679 // 131 = on / ramp down
mjr 76:7f5912b6340e 680 // 132 = ramp up / on
mjr 5:a70c0bce770d 681 //
mjr 76:7f5912b6340e 682 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 76:7f5912b6340e 683 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 76:7f5912b6340e 684 // it, so we need to accept it for compatibility.)
mjr 76:7f5912b6340e 685 static uint8_t *wizVal;
mjr 76:7f5912b6340e 686
mjr 76:7f5912b6340e 687 // Current actual brightness for each output. This is a simple linear
mjr 76:7f5912b6340e 688 // value on a 0..255 scale. This is EITHER the linear brightness computed
mjr 76:7f5912b6340e 689 // from the LedWiz setting for the port, OR the 0..255 value set explicitly
mjr 76:7f5912b6340e 690 // by the extended protocol:
mjr 76:7f5912b6340e 691 //
mjr 76:7f5912b6340e 692 // - If the last command that updated the port was an extended protocol
mjr 76:7f5912b6340e 693 // SET BRIGHTNESS command, this is the value set by that command. In
mjr 76:7f5912b6340e 694 // addition, wizOn[port] is set to 0 if the brightness is 0, 1 otherwise;
mjr 76:7f5912b6340e 695 // and wizVal[port] is set to the brightness rescaled to the 0..48 range
mjr 76:7f5912b6340e 696 // if the brightness is non-zero.
mjr 76:7f5912b6340e 697 //
mjr 76:7f5912b6340e 698 // - If the last command that updated the port was an LedWiz command
mjr 76:7f5912b6340e 699 // (SBA/PBA/SBX/PBX), this contains the brightness value computed from
mjr 76:7f5912b6340e 700 // the combination of wizOn[port] and wizVal[port]. If wizOn[port] is
mjr 76:7f5912b6340e 701 // zero, this is simply 0, otherwise it's wizVal[port] rescaled to the
mjr 76:7f5912b6340e 702 // 0..255 range.
mjr 26:cb71c4af2912 703 //
mjr 76:7f5912b6340e 704 // - For a port set to wizOn[port]=1 and wizVal[port] in 129..132, this is
mjr 76:7f5912b6340e 705 // also updated continuously to reflect the current flashing brightness
mjr 76:7f5912b6340e 706 // level.
mjr 26:cb71c4af2912 707 //
mjr 76:7f5912b6340e 708 static uint8_t *outLevel;
mjr 76:7f5912b6340e 709
mjr 76:7f5912b6340e 710
mjr 76:7f5912b6340e 711 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 76:7f5912b6340e 712 // rate for lights in blinking states. The LedWiz API doesn't document
mjr 76:7f5912b6340e 713 // what the numbers mean in real time units, but by observation, the
mjr 76:7f5912b6340e 714 // "speed" setting represents the period of the flash cycle in 0.25s
mjr 76:7f5912b6340e 715 // units, so speed 1 = 0.25 period = 4Hz, speed 7 = 1.75s period = 0.57Hz.
mjr 76:7f5912b6340e 716 // The period is the full cycle time of the flash waveform.
mjr 76:7f5912b6340e 717 //
mjr 76:7f5912b6340e 718 // Each bank of 32 lights has its independent own pulse rate, so we need
mjr 76:7f5912b6340e 719 // one entry per bank. Each bank has 32 outputs, so we need a total of
mjr 76:7f5912b6340e 720 // ceil(number_of_physical_outputs/32) entries. Note that we could allocate
mjr 76:7f5912b6340e 721 // this dynamically once we know the number of actual outputs, but the
mjr 76:7f5912b6340e 722 // upper limit is low enough that it's more efficient to use a fixed array
mjr 76:7f5912b6340e 723 // at the maximum size.
mjr 76:7f5912b6340e 724 static const int MAX_LW_BANKS = (MAX_OUT_PORTS+31)/32;
mjr 76:7f5912b6340e 725 static uint8_t wizSpeed[MAX_LW_BANKS];
mjr 29:582472d0bc57 726
mjr 26:cb71c4af2912 727 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 728 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 729 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 730 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 731 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 732
mjr 76:7f5912b6340e 733
mjr 76:7f5912b6340e 734 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 735 //
mjr 76:7f5912b6340e 736 // Output Ports
mjr 76:7f5912b6340e 737 //
mjr 76:7f5912b6340e 738 // There are two way to connect outputs. First, you can use the on-board
mjr 76:7f5912b6340e 739 // GPIO ports to implement device outputs: each LedWiz software port is
mjr 76:7f5912b6340e 740 // connected to a physical GPIO pin on the KL25Z. This has some pretty
mjr 76:7f5912b6340e 741 // strict limits, though. The KL25Z only has 10 PWM channels, so only 10
mjr 76:7f5912b6340e 742 // GPIO LedWiz ports can be made dimmable; the rest are strictly on/off.
mjr 76:7f5912b6340e 743 // The KL25Z also simply doesn't have enough exposed GPIO ports overall to
mjr 76:7f5912b6340e 744 // support all of the features the software supports. The software allows
mjr 76:7f5912b6340e 745 // for up to 128 outputs, 48 button inputs, plunger input (requiring 1-5
mjr 76:7f5912b6340e 746 // GPIO pins), and various other external devices. The KL25Z only exposes
mjr 76:7f5912b6340e 747 // about 50 GPIO pins. So if you want to do everything with GPIO ports,
mjr 76:7f5912b6340e 748 // you have to ration pins among features.
mjr 76:7f5912b6340e 749 //
mjr 76:7f5912b6340e 750 // To overcome some of these limitations, we also provide two types of
mjr 76:7f5912b6340e 751 // peripheral controllers that allow adding many more outputs, using only
mjr 76:7f5912b6340e 752 // a small number of GPIO pins to interface with the peripherals. First,
mjr 76:7f5912b6340e 753 // we support TLC5940 PWM controller chips. Each TLC5940 provides 16 ports
mjr 76:7f5912b6340e 754 // with full PWM, and multiple TLC5940 chips can be daisy-chained. The
mjr 76:7f5912b6340e 755 // chip only requires 5 GPIO pins for the interface, no matter how many
mjr 76:7f5912b6340e 756 // chips are in the chain, so it effectively converts 5 GPIO pins into
mjr 76:7f5912b6340e 757 // almost any number of PWM outputs. Second, we support 74HC595 chips.
mjr 76:7f5912b6340e 758 // These provide only digital outputs, but like the TLC5940 they can be
mjr 76:7f5912b6340e 759 // daisy-chained to provide almost unlimited outputs with a few GPIO pins
mjr 76:7f5912b6340e 760 // to control the whole chain.
mjr 76:7f5912b6340e 761 //
mjr 76:7f5912b6340e 762 // Direct GPIO output ports and peripheral controllers can be mixed and
mjr 76:7f5912b6340e 763 // matched in one system. The assignment of pins to ports and the
mjr 76:7f5912b6340e 764 // configuration of peripheral controllers is all handled in the software
mjr 76:7f5912b6340e 765 // setup, so a physical system can be expanded and updated at any time.
mjr 76:7f5912b6340e 766 //
mjr 76:7f5912b6340e 767 // To handle the diversity of output port types, we start with an abstract
mjr 76:7f5912b6340e 768 // base class for outputs. Each type of physical output interface has a
mjr 76:7f5912b6340e 769 // concrete subclass. During initialization, we create the appropriate
mjr 76:7f5912b6340e 770 // subclass for each software port, mapping it to the assigned GPIO pin
mjr 76:7f5912b6340e 771 // or peripheral port. Most of the rest of the software only cares about
mjr 76:7f5912b6340e 772 // the abstract interface, so once the subclassed port objects are set up,
mjr 76:7f5912b6340e 773 // the rest of the system can control the ports without knowing which types
mjr 76:7f5912b6340e 774 // of physical devices they're connected to.
mjr 76:7f5912b6340e 775
mjr 76:7f5912b6340e 776
mjr 26:cb71c4af2912 777 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 778 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 779 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 780 class LwOut
mjr 6:cc35eb643e8f 781 {
mjr 6:cc35eb643e8f 782 public:
mjr 40:cc0d9814522b 783 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 784 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 785 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 786 };
mjr 26:cb71c4af2912 787
mjr 35:e959ffba78fd 788 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 789 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 790 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 791 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 792 // numbering.
mjr 35:e959ffba78fd 793 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 794 {
mjr 33:d832bcab089e 795 public:
mjr 35:e959ffba78fd 796 LwVirtualOut() { }
mjr 40:cc0d9814522b 797 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 798 };
mjr 26:cb71c4af2912 799
mjr 34:6b981a2afab7 800 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 801 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 802 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 803 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 804 {
mjr 34:6b981a2afab7 805 public:
mjr 34:6b981a2afab7 806 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 807 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 808
mjr 34:6b981a2afab7 809 private:
mjr 53:9b2611964afc 810 // underlying physical output
mjr 34:6b981a2afab7 811 LwOut *out;
mjr 34:6b981a2afab7 812 };
mjr 34:6b981a2afab7 813
mjr 53:9b2611964afc 814 // Global ZB Launch Ball state
mjr 53:9b2611964afc 815 bool zbLaunchOn = false;
mjr 53:9b2611964afc 816
mjr 53:9b2611964afc 817 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 818 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 819 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 820 {
mjr 53:9b2611964afc 821 public:
mjr 53:9b2611964afc 822 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 823 virtual void set(uint8_t val)
mjr 53:9b2611964afc 824 {
mjr 53:9b2611964afc 825 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 826 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 827
mjr 53:9b2611964afc 828 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 829 out->set(val);
mjr 53:9b2611964afc 830 }
mjr 53:9b2611964afc 831
mjr 53:9b2611964afc 832 private:
mjr 53:9b2611964afc 833 // underlying physical or virtual output
mjr 53:9b2611964afc 834 LwOut *out;
mjr 53:9b2611964afc 835 };
mjr 53:9b2611964afc 836
mjr 53:9b2611964afc 837
mjr 40:cc0d9814522b 838 // Gamma correction table for 8-bit input values
mjr 40:cc0d9814522b 839 static const uint8_t gamma[] = {
mjr 40:cc0d9814522b 840 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 841 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 842 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 843 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 844 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 845 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 846 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 847 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 848 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 849 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 850 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 851 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 852 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 853 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 854 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 855 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 856 };
mjr 40:cc0d9814522b 857
mjr 40:cc0d9814522b 858 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 859 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 860 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 861 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 862 {
mjr 40:cc0d9814522b 863 public:
mjr 40:cc0d9814522b 864 LwGammaOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 865 virtual void set(uint8_t val) { out->set(gamma[val]); }
mjr 40:cc0d9814522b 866
mjr 40:cc0d9814522b 867 private:
mjr 40:cc0d9814522b 868 LwOut *out;
mjr 40:cc0d9814522b 869 };
mjr 40:cc0d9814522b 870
mjr 53:9b2611964afc 871 // global night mode flag
mjr 53:9b2611964afc 872 static bool nightMode = false;
mjr 53:9b2611964afc 873
mjr 40:cc0d9814522b 874 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 875 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 876 // mode is engaged.
mjr 40:cc0d9814522b 877 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 878 {
mjr 40:cc0d9814522b 879 public:
mjr 40:cc0d9814522b 880 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 881 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 882
mjr 53:9b2611964afc 883 private:
mjr 53:9b2611964afc 884 LwOut *out;
mjr 53:9b2611964afc 885 };
mjr 53:9b2611964afc 886
mjr 53:9b2611964afc 887 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 888 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 889 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 890 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 891 {
mjr 53:9b2611964afc 892 public:
mjr 53:9b2611964afc 893 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 894 virtual void set(uint8_t)
mjr 53:9b2611964afc 895 {
mjr 53:9b2611964afc 896 // ignore the host value and simply show the current
mjr 53:9b2611964afc 897 // night mode setting
mjr 53:9b2611964afc 898 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 899 }
mjr 40:cc0d9814522b 900
mjr 40:cc0d9814522b 901 private:
mjr 40:cc0d9814522b 902 LwOut *out;
mjr 40:cc0d9814522b 903 };
mjr 40:cc0d9814522b 904
mjr 26:cb71c4af2912 905
mjr 35:e959ffba78fd 906 //
mjr 35:e959ffba78fd 907 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 908 // assignments set in config.h.
mjr 33:d832bcab089e 909 //
mjr 35:e959ffba78fd 910 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 911 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 912 {
mjr 35:e959ffba78fd 913 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 914 {
mjr 53:9b2611964afc 915 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 916 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 917 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 918 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 919 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 920 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 921 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 922 }
mjr 35:e959ffba78fd 923 }
mjr 26:cb71c4af2912 924
mjr 40:cc0d9814522b 925 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 926 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 927 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 928 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 929 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 930 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 931 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 932 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 933 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 934 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 935 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 936 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 937 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 938 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 939 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 940 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 941 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 942 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 943 };
mjr 40:cc0d9814522b 944
mjr 40:cc0d9814522b 945 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 946 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 947 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 948 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 949 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 950 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 951 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 952 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 953 // are always 8 bits.
mjr 40:cc0d9814522b 954 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 955 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 956 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 957 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 958 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 959 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 960 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 961 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 962 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 963 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 964 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 965 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 966 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 967 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 968 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 969 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 970 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 971 };
mjr 40:cc0d9814522b 972
mjr 26:cb71c4af2912 973 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 974 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 975 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 976 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 977 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 978 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 979 {
mjr 26:cb71c4af2912 980 public:
mjr 60:f38da020aa13 981 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 982 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 983 {
mjr 26:cb71c4af2912 984 if (val != prv)
mjr 40:cc0d9814522b 985 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 986 }
mjr 60:f38da020aa13 987 uint8_t idx;
mjr 40:cc0d9814522b 988 uint8_t prv;
mjr 26:cb71c4af2912 989 };
mjr 26:cb71c4af2912 990
mjr 40:cc0d9814522b 991 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 992 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 993 {
mjr 40:cc0d9814522b 994 public:
mjr 60:f38da020aa13 995 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 996 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 997 {
mjr 40:cc0d9814522b 998 if (val != prv)
mjr 40:cc0d9814522b 999 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 1000 }
mjr 60:f38da020aa13 1001 uint8_t idx;
mjr 40:cc0d9814522b 1002 uint8_t prv;
mjr 40:cc0d9814522b 1003 };
mjr 40:cc0d9814522b 1004
mjr 40:cc0d9814522b 1005
mjr 33:d832bcab089e 1006
mjr 34:6b981a2afab7 1007 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 1008 // config.h.
mjr 35:e959ffba78fd 1009 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 1010
mjr 35:e959ffba78fd 1011 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 1012 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 1013 {
mjr 35:e959ffba78fd 1014 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 1015 {
mjr 53:9b2611964afc 1016 hc595 = new HC595(
mjr 53:9b2611964afc 1017 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 1018 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 1019 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 1020 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 1021 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 1022 hc595->init();
mjr 35:e959ffba78fd 1023 hc595->update();
mjr 35:e959ffba78fd 1024 }
mjr 35:e959ffba78fd 1025 }
mjr 34:6b981a2afab7 1026
mjr 34:6b981a2afab7 1027 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 1028 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 1029 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 1030 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 1031 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 1032 class Lw595Out: public LwOut
mjr 33:d832bcab089e 1033 {
mjr 33:d832bcab089e 1034 public:
mjr 60:f38da020aa13 1035 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1036 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 1037 {
mjr 34:6b981a2afab7 1038 if (val != prv)
mjr 40:cc0d9814522b 1039 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 1040 }
mjr 60:f38da020aa13 1041 uint8_t idx;
mjr 40:cc0d9814522b 1042 uint8_t prv;
mjr 33:d832bcab089e 1043 };
mjr 33:d832bcab089e 1044
mjr 26:cb71c4af2912 1045
mjr 40:cc0d9814522b 1046
mjr 64:ef7ca92dff36 1047 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 1048 // normalized to 0.0 to 1.0 scale.
mjr 74:822a92bc11d2 1049 static const float dof_to_pwm[] = {
mjr 64:ef7ca92dff36 1050 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 1051 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 1052 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 1053 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 1054 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 1055 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 1056 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 1057 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 1058 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 1059 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 1060 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 1061 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 1062 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 1063 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 1064 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 1065 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 1066 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 1067 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 1068 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 1069 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 1070 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 1071 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 1072 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 1073 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 1074 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 1075 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 1076 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 1077 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 1078 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 1079 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 1080 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 1081 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 1082 };
mjr 26:cb71c4af2912 1083
mjr 64:ef7ca92dff36 1084
mjr 64:ef7ca92dff36 1085 // Conversion table for 8-bit DOF level to pulse width in microseconds,
mjr 64:ef7ca92dff36 1086 // with gamma correction. We could use the layered gamma output on top
mjr 64:ef7ca92dff36 1087 // of the regular LwPwmOut class for this, but we get better precision
mjr 64:ef7ca92dff36 1088 // with a dedicated table, because we apply gamma correction to the
mjr 64:ef7ca92dff36 1089 // 32-bit microsecond values rather than the 8-bit DOF levels.
mjr 64:ef7ca92dff36 1090 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 1091 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 1092 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 1093 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 1094 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 1095 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 1096 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 1097 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 1098 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 1099 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 1100 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 1101 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 1102 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 1103 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 1104 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 1105 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 1106 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 1107 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 1108 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 1109 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 1110 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 1111 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 1112 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 1113 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 1114 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 1115 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 1116 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 1117 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 1118 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 1119 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 1120 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 1121 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 1122 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 1123 };
mjr 64:ef7ca92dff36 1124
mjr 74:822a92bc11d2 1125 // MyPwmOut - a slight customization of the base mbed PwmOut class. The
mjr 74:822a92bc11d2 1126 // mbed version of PwmOut.write() resets the PWM cycle counter on every
mjr 74:822a92bc11d2 1127 // update. That's problematic, because the counter reset interrupts the
mjr 74:822a92bc11d2 1128 // cycle in progress, causing a momentary drop in brightness that's visible
mjr 74:822a92bc11d2 1129 // to the eye if the output is connected to an LED or other light source.
mjr 74:822a92bc11d2 1130 // This is especially noticeable when making gradual changes consisting of
mjr 74:822a92bc11d2 1131 // many updates in a short time, such as a slow fade, because the light
mjr 74:822a92bc11d2 1132 // visibly flickers on every step of the transition. This customized
mjr 74:822a92bc11d2 1133 // version removes the cycle reset, which makes for glitch-free updates
mjr 74:822a92bc11d2 1134 // and nice smooth fades.
mjr 74:822a92bc11d2 1135 //
mjr 74:822a92bc11d2 1136 // Initially, I thought the counter reset in the mbed code was simply a
mjr 74:822a92bc11d2 1137 // bug. According to the KL25Z hardware reference, you update the duty
mjr 74:822a92bc11d2 1138 // cycle by writing to the "compare values" (CvN) register. There's no
mjr 74:822a92bc11d2 1139 // hint that you should reset the cycle counter, and indeed, the hardware
mjr 74:822a92bc11d2 1140 // goes out of its way to allow updates mid-cycle (as we'll see shortly).
mjr 74:822a92bc11d2 1141 // They went to lengths specifically so that you *don't* have to reset
mjr 74:822a92bc11d2 1142 // that counter. And there's no comment in the mbed code explaining the
mjr 74:822a92bc11d2 1143 // cycle reset, so it looked to me like something that must have been
mjr 74:822a92bc11d2 1144 // added by someone who didn't read the manual carefully enough and didn't
mjr 74:822a92bc11d2 1145 // test the result thoroughly enough to find the glitch it causes.
mjr 74:822a92bc11d2 1146 //
mjr 74:822a92bc11d2 1147 // After some experimentation, though, I've come to think the code was
mjr 74:822a92bc11d2 1148 // added intentionally, as a workaround for a rather nasty KL25Z hardware
mjr 74:822a92bc11d2 1149 // bug. Whoever wrote the code didn't add any comments explaning why it's
mjr 74:822a92bc11d2 1150 // there, so we can't know for sure, but it does happen to work around the
mjr 74:822a92bc11d2 1151 // bug, so it's a good bet the original programmer found the same hardware
mjr 74:822a92bc11d2 1152 // problem and came up with the counter reset as an imperfect solution.
mjr 74:822a92bc11d2 1153 //
mjr 74:822a92bc11d2 1154 // We'll get to the KL25Z hardware bug shortly, but first we need to look at
mjr 74:822a92bc11d2 1155 // how the hardware is *supposed* to work. The KL25Z is *supposed* to make
mjr 74:822a92bc11d2 1156 // it super easy for software to do glitch-free updates of the duty cycle of
mjr 74:822a92bc11d2 1157 // a PWM channel. With PWM hardware in general, you have to be careful to
mjr 74:822a92bc11d2 1158 // update the duty cycle counter between grayscale cycles, beacuse otherwise
mjr 74:822a92bc11d2 1159 // you might interrupt the cycle in progress and cause a brightness glitch.
mjr 74:822a92bc11d2 1160 // The KL25Z TPM simplifies this with a "staging" register for the duty
mjr 74:822a92bc11d2 1161 // cycle counter. At the end of each cycle, the TPM moves the value from
mjr 74:822a92bc11d2 1162 // the staging register into its internal register that actually controls
mjr 74:822a92bc11d2 1163 // the duty cycle. The idea is that the software can write a new value to
mjr 74:822a92bc11d2 1164 // the staging register at any time, and the hardware will take care of
mjr 74:822a92bc11d2 1165 // synchronizing the actual internal update with the grayscale cycle. In
mjr 74:822a92bc11d2 1166 // principle, this frees the software of any special timing considerations
mjr 74:822a92bc11d2 1167 // for PWM updates.
mjr 74:822a92bc11d2 1168 //
mjr 74:822a92bc11d2 1169 // Now for the bug. The staging register works as advertised, except for
mjr 74:822a92bc11d2 1170 // one little detail: it seems to be implemented as a one-element queue
mjr 74:822a92bc11d2 1171 // that won't accept a new write until the existing value has been read.
mjr 74:822a92bc11d2 1172 // The read only happens at the start of the new cycle. So the effect is
mjr 74:822a92bc11d2 1173 // that we can only write one update per cycle. Any writes after the first
mjr 74:822a92bc11d2 1174 // are simply dropped, lost forever. That causes even worse problems than
mjr 74:822a92bc11d2 1175 // the original glitch. For example, if we're doing a fade-out, the last
mjr 74:822a92bc11d2 1176 // couple of updates in the fade might get lost, leaving the output slightly
mjr 74:822a92bc11d2 1177 // on at the end, when it's supposed to be completely off.
mjr 74:822a92bc11d2 1178 //
mjr 74:822a92bc11d2 1179 // The mbed workaround of resetting the cycle counter fixes the lost-update
mjr 74:822a92bc11d2 1180 // problem, but it causes the constant glitching during fades. So we need
mjr 74:822a92bc11d2 1181 // a third way that works around the hardware problem without causing
mjr 74:822a92bc11d2 1182 // update glitches.
mjr 74:822a92bc11d2 1183 //
mjr 74:822a92bc11d2 1184 // Here's my solution: we basically implement our own staging register,
mjr 74:822a92bc11d2 1185 // using the same principle as the hardware staging register, but hopefully
mjr 74:822a92bc11d2 1186 // with an implementation that actually works! First, when we update a PWM
mjr 74:822a92bc11d2 1187 // output, we won't actually write the value to the hardware register.
mjr 74:822a92bc11d2 1188 // Instead, we'll just stash it internally, effectively in our own staging
mjr 74:822a92bc11d2 1189 // register (but actually just a member variable of this object). Then
mjr 74:822a92bc11d2 1190 // we'll periodically transfer these staged updates to the actual hardware
mjr 74:822a92bc11d2 1191 // registers, being careful to do this no more than once per PWM cycle.
mjr 74:822a92bc11d2 1192 // One way to do this would be to use an interrupt handler that fires at
mjr 74:822a92bc11d2 1193 // the end of the PWM cycle, but that would be fairly complex because we
mjr 74:822a92bc11d2 1194 // have many (up to 10) PWM channels. Instead, we'll just use polling:
mjr 74:822a92bc11d2 1195 // we'll call a routine periodically in our main loop, and we'll transfer
mjr 74:822a92bc11d2 1196 // updates for all of the channels that have been updated since the last
mjr 74:822a92bc11d2 1197 // pass. We can get away with this simple polling approach because the
mjr 74:822a92bc11d2 1198 // hardware design *partially* works: it does manage to free us from the
mjr 74:822a92bc11d2 1199 // need to synchronize updates with the exact end of a PWM cycle. As long
mjr 74:822a92bc11d2 1200 // as we do no more than one write per cycle, we're golden. That's easy
mjr 74:822a92bc11d2 1201 // to accomplish, too: all we need to do is make sure that our polling
mjr 74:822a92bc11d2 1202 // interval is slightly longer than the PWM period. That ensures that
mjr 74:822a92bc11d2 1203 // we can never have two updates during one PWM cycle. It does mean that
mjr 74:822a92bc11d2 1204 // we might have zero updates on some cycles, causing a one-cycle delay
mjr 74:822a92bc11d2 1205 // before an update is actually put into effect, but that shouldn't ever
mjr 74:822a92bc11d2 1206 // be noticeable since the cycles are so short. Specifically, we'll use
mjr 74:822a92bc11d2 1207 // the mbed default 20ms PWM period, and we'll do our update polling
mjr 74:822a92bc11d2 1208 // every 25ms.
mjr 74:822a92bc11d2 1209 class LessGlitchyPwmOut: public PwmOut
mjr 74:822a92bc11d2 1210 {
mjr 74:822a92bc11d2 1211 public:
mjr 74:822a92bc11d2 1212 LessGlitchyPwmOut(PinName pin) : PwmOut(pin) { }
mjr 74:822a92bc11d2 1213
mjr 74:822a92bc11d2 1214 void write(float value)
mjr 74:822a92bc11d2 1215 {
mjr 74:822a92bc11d2 1216 // Update the counter without resetting the counter.
mjr 74:822a92bc11d2 1217 //
mjr 74:822a92bc11d2 1218 // NB: this causes problems if there are multiple writes in one
mjr 74:822a92bc11d2 1219 // PWM cycle: the first write will be applied and later writes
mjr 74:822a92bc11d2 1220 // during the same cycle will be lost. Callers must take care
mjr 74:822a92bc11d2 1221 // to limit writes to one per cycle.
mjr 74:822a92bc11d2 1222 *_pwm.CnV = uint32_t((*_pwm.MOD + 1) * value);
mjr 74:822a92bc11d2 1223 }
mjr 74:822a92bc11d2 1224 };
mjr 74:822a92bc11d2 1225
mjr 74:822a92bc11d2 1226
mjr 74:822a92bc11d2 1227 // Collection of PwmOut objects to update on each polling cycle. The
mjr 74:822a92bc11d2 1228 // KL25Z has 10 physical PWM channels, so we need at most 10 polled outputs.
mjr 74:822a92bc11d2 1229 static int numPolledPwm;
mjr 74:822a92bc11d2 1230 static class LwPwmOut *polledPwm[10];
mjr 74:822a92bc11d2 1231
mjr 74:822a92bc11d2 1232 // LwOut class for a PWM-capable GPIO port.
mjr 6:cc35eb643e8f 1233 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 1234 {
mjr 6:cc35eb643e8f 1235 public:
mjr 43:7a6364d82a41 1236 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1237 {
mjr 74:822a92bc11d2 1238 // set the cycle time to 20ms
mjr 74:822a92bc11d2 1239 p.period_ms(20);
mjr 74:822a92bc11d2 1240
mjr 74:822a92bc11d2 1241 // add myself to the list of polled outputs for periodic updates
mjr 74:822a92bc11d2 1242 if (numPolledPwm < countof(polledPwm))
mjr 74:822a92bc11d2 1243 polledPwm[numPolledPwm++] = this;
mjr 74:822a92bc11d2 1244
mjr 74:822a92bc11d2 1245 // set the initial value, and an explicitly different previous value
mjr 74:822a92bc11d2 1246 prv = ~initVal;
mjr 43:7a6364d82a41 1247 set(initVal);
mjr 43:7a6364d82a41 1248 }
mjr 74:822a92bc11d2 1249
mjr 40:cc0d9814522b 1250 virtual void set(uint8_t val)
mjr 74:822a92bc11d2 1251 {
mjr 74:822a92bc11d2 1252 // on set, just save the value for a later 'commit'
mjr 74:822a92bc11d2 1253 this->val = val;
mjr 13:72dda449c3c0 1254 }
mjr 74:822a92bc11d2 1255
mjr 74:822a92bc11d2 1256 // handle periodic update polling
mjr 74:822a92bc11d2 1257 void poll()
mjr 74:822a92bc11d2 1258 {
mjr 74:822a92bc11d2 1259 // if the value has changed, commit it
mjr 74:822a92bc11d2 1260 if (val != prv)
mjr 74:822a92bc11d2 1261 {
mjr 74:822a92bc11d2 1262 prv = val;
mjr 74:822a92bc11d2 1263 commit(val);
mjr 74:822a92bc11d2 1264 }
mjr 74:822a92bc11d2 1265 }
mjr 74:822a92bc11d2 1266
mjr 74:822a92bc11d2 1267 protected:
mjr 74:822a92bc11d2 1268 virtual void commit(uint8_t v)
mjr 74:822a92bc11d2 1269 {
mjr 74:822a92bc11d2 1270 // write the current value to the PWM controller if it's changed
mjr 74:822a92bc11d2 1271 p.write(dof_to_pwm[v]);
mjr 74:822a92bc11d2 1272 }
mjr 74:822a92bc11d2 1273
mjr 74:822a92bc11d2 1274 LessGlitchyPwmOut p;
mjr 74:822a92bc11d2 1275 uint8_t val, prv;
mjr 6:cc35eb643e8f 1276 };
mjr 26:cb71c4af2912 1277
mjr 74:822a92bc11d2 1278 // Gamma corrected PWM GPIO output. This works exactly like the regular
mjr 74:822a92bc11d2 1279 // PWM output, but translates DOF values through the gamma-corrected
mjr 74:822a92bc11d2 1280 // table instead of the regular linear table.
mjr 64:ef7ca92dff36 1281 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1282 {
mjr 64:ef7ca92dff36 1283 public:
mjr 64:ef7ca92dff36 1284 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1285 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1286 {
mjr 64:ef7ca92dff36 1287 }
mjr 74:822a92bc11d2 1288
mjr 74:822a92bc11d2 1289 protected:
mjr 74:822a92bc11d2 1290 virtual void commit(uint8_t v)
mjr 64:ef7ca92dff36 1291 {
mjr 74:822a92bc11d2 1292 // write the current value to the PWM controller if it's changed
mjr 74:822a92bc11d2 1293 p.write(dof_to_gamma_pwm[v]);
mjr 64:ef7ca92dff36 1294 }
mjr 64:ef7ca92dff36 1295 };
mjr 64:ef7ca92dff36 1296
mjr 74:822a92bc11d2 1297 // poll the PWM outputs
mjr 74:822a92bc11d2 1298 Timer polledPwmTimer;
mjr 76:7f5912b6340e 1299 uint64_t polledPwmTotalTime, polledPwmRunCount;
mjr 74:822a92bc11d2 1300 void pollPwmUpdates()
mjr 74:822a92bc11d2 1301 {
mjr 74:822a92bc11d2 1302 // if it's been at least 25ms since the last update, do another update
mjr 74:822a92bc11d2 1303 if (polledPwmTimer.read_us() >= 25000)
mjr 74:822a92bc11d2 1304 {
mjr 74:822a92bc11d2 1305 // time the run for statistics collection
mjr 74:822a92bc11d2 1306 IF_DIAG(
mjr 74:822a92bc11d2 1307 Timer t;
mjr 74:822a92bc11d2 1308 t.start();
mjr 74:822a92bc11d2 1309 )
mjr 74:822a92bc11d2 1310
mjr 74:822a92bc11d2 1311 // poll each output
mjr 74:822a92bc11d2 1312 for (int i = numPolledPwm ; i > 0 ; )
mjr 74:822a92bc11d2 1313 polledPwm[--i]->poll();
mjr 74:822a92bc11d2 1314
mjr 74:822a92bc11d2 1315 // reset the timer for the next cycle
mjr 74:822a92bc11d2 1316 polledPwmTimer.reset();
mjr 74:822a92bc11d2 1317
mjr 74:822a92bc11d2 1318 // collect statistics
mjr 74:822a92bc11d2 1319 IF_DIAG(
mjr 76:7f5912b6340e 1320 polledPwmTotalTime += t.read_us();
mjr 74:822a92bc11d2 1321 polledPwmRunCount += 1;
mjr 74:822a92bc11d2 1322 )
mjr 74:822a92bc11d2 1323 }
mjr 74:822a92bc11d2 1324 }
mjr 64:ef7ca92dff36 1325
mjr 26:cb71c4af2912 1326 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1327 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1328 {
mjr 6:cc35eb643e8f 1329 public:
mjr 43:7a6364d82a41 1330 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1331 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1332 {
mjr 13:72dda449c3c0 1333 if (val != prv)
mjr 40:cc0d9814522b 1334 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1335 }
mjr 6:cc35eb643e8f 1336 DigitalOut p;
mjr 40:cc0d9814522b 1337 uint8_t prv;
mjr 6:cc35eb643e8f 1338 };
mjr 26:cb71c4af2912 1339
mjr 29:582472d0bc57 1340 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1341 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1342 // port n (0-based).
mjr 35:e959ffba78fd 1343 //
mjr 35:e959ffba78fd 1344 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1345 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1346 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1347 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1348 // 74HC595 ports).
mjr 33:d832bcab089e 1349 static int numOutputs;
mjr 33:d832bcab089e 1350 static LwOut **lwPin;
mjr 33:d832bcab089e 1351
mjr 38:091e511ce8a0 1352 // create a single output pin
mjr 53:9b2611964afc 1353 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1354 {
mjr 38:091e511ce8a0 1355 // get this item's values
mjr 38:091e511ce8a0 1356 int typ = pc.typ;
mjr 38:091e511ce8a0 1357 int pin = pc.pin;
mjr 38:091e511ce8a0 1358 int flags = pc.flags;
mjr 40:cc0d9814522b 1359 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1360 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1361 int gamma = flags & PortFlagGamma;
mjr 38:091e511ce8a0 1362
mjr 38:091e511ce8a0 1363 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1364 LwOut *lwp;
mjr 38:091e511ce8a0 1365 switch (typ)
mjr 38:091e511ce8a0 1366 {
mjr 38:091e511ce8a0 1367 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1368 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1369 if (pin != 0)
mjr 64:ef7ca92dff36 1370 {
mjr 64:ef7ca92dff36 1371 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 1372 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 1373 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 1374 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 1375 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 1376 {
mjr 64:ef7ca92dff36 1377 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 1378 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 1379
mjr 64:ef7ca92dff36 1380 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 1381 gamma = false;
mjr 64:ef7ca92dff36 1382 }
mjr 64:ef7ca92dff36 1383 else
mjr 64:ef7ca92dff36 1384 {
mjr 64:ef7ca92dff36 1385 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 1386 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 1387 }
mjr 64:ef7ca92dff36 1388 }
mjr 48:058ace2aed1d 1389 else
mjr 48:058ace2aed1d 1390 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1391 break;
mjr 38:091e511ce8a0 1392
mjr 38:091e511ce8a0 1393 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1394 // Digital GPIO port
mjr 48:058ace2aed1d 1395 if (pin != 0)
mjr 48:058ace2aed1d 1396 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1397 else
mjr 48:058ace2aed1d 1398 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1399 break;
mjr 38:091e511ce8a0 1400
mjr 38:091e511ce8a0 1401 case PortTypeTLC5940:
mjr 38:091e511ce8a0 1402 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 1403 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 1404 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 1405 {
mjr 40:cc0d9814522b 1406 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 1407 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 1408 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 1409 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 1410 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 1411 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 1412 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 1413 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 1414 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 1415 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 1416 // for this unlikely case.
mjr 40:cc0d9814522b 1417 if (gamma && !activeLow)
mjr 40:cc0d9814522b 1418 {
mjr 40:cc0d9814522b 1419 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 1420 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 1421
mjr 40:cc0d9814522b 1422 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 1423 gamma = false;
mjr 40:cc0d9814522b 1424 }
mjr 40:cc0d9814522b 1425 else
mjr 40:cc0d9814522b 1426 {
mjr 40:cc0d9814522b 1427 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 1428 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 1429 }
mjr 40:cc0d9814522b 1430 }
mjr 38:091e511ce8a0 1431 else
mjr 40:cc0d9814522b 1432 {
mjr 40:cc0d9814522b 1433 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 1434 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 1435 }
mjr 38:091e511ce8a0 1436 break;
mjr 38:091e511ce8a0 1437
mjr 38:091e511ce8a0 1438 case PortType74HC595:
mjr 38:091e511ce8a0 1439 // 74HC595 port (if we don't have an HC595 controller object, or it's not a valid
mjr 38:091e511ce8a0 1440 // output number, create a virtual port)
mjr 38:091e511ce8a0 1441 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 1442 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 1443 else
mjr 38:091e511ce8a0 1444 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1445 break;
mjr 38:091e511ce8a0 1446
mjr 38:091e511ce8a0 1447 case PortTypeVirtual:
mjr 43:7a6364d82a41 1448 case PortTypeDisabled:
mjr 38:091e511ce8a0 1449 default:
mjr 38:091e511ce8a0 1450 // virtual or unknown
mjr 38:091e511ce8a0 1451 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1452 break;
mjr 38:091e511ce8a0 1453 }
mjr 38:091e511ce8a0 1454
mjr 40:cc0d9814522b 1455 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 1456 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 1457 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 1458 if (activeLow)
mjr 38:091e511ce8a0 1459 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 1460
mjr 40:cc0d9814522b 1461 // If it's a noisemaker, layer on a night mode switch. Note that this
mjr 40:cc0d9814522b 1462 // needs to be
mjr 40:cc0d9814522b 1463 if (noisy)
mjr 40:cc0d9814522b 1464 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 1465
mjr 40:cc0d9814522b 1466 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 1467 if (gamma)
mjr 40:cc0d9814522b 1468 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 1469
mjr 53:9b2611964afc 1470 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 1471 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 1472 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 1473 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 1474 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 1475
mjr 53:9b2611964afc 1476 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 1477 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 1478 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 1479
mjr 38:091e511ce8a0 1480 // turn it off initially
mjr 38:091e511ce8a0 1481 lwp->set(0);
mjr 38:091e511ce8a0 1482
mjr 38:091e511ce8a0 1483 // return the pin
mjr 38:091e511ce8a0 1484 return lwp;
mjr 38:091e511ce8a0 1485 }
mjr 38:091e511ce8a0 1486
mjr 6:cc35eb643e8f 1487 // initialize the output pin array
mjr 35:e959ffba78fd 1488 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 1489 {
mjr 35:e959ffba78fd 1490 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 1491 // total number of ports.
mjr 35:e959ffba78fd 1492 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 1493 int i;
mjr 35:e959ffba78fd 1494 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 1495 {
mjr 35:e959ffba78fd 1496 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 1497 {
mjr 35:e959ffba78fd 1498 numOutputs = i;
mjr 34:6b981a2afab7 1499 break;
mjr 34:6b981a2afab7 1500 }
mjr 33:d832bcab089e 1501 }
mjr 33:d832bcab089e 1502
mjr 73:4e8ce0b18915 1503 // allocate the pin array
mjr 73:4e8ce0b18915 1504 lwPin = new LwOut*[numOutputs];
mjr 35:e959ffba78fd 1505
mjr 73:4e8ce0b18915 1506 // Allocate the current brightness array
mjr 73:4e8ce0b18915 1507 outLevel = new uint8_t[numOutputs];
mjr 33:d832bcab089e 1508
mjr 73:4e8ce0b18915 1509 // allocate the LedWiz output state arrays
mjr 73:4e8ce0b18915 1510 wizOn = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 1511 wizVal = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 1512
mjr 73:4e8ce0b18915 1513 // initialize all LedWiz outputs to off and brightness 48
mjr 73:4e8ce0b18915 1514 memset(wizOn, 0, numOutputs);
mjr 73:4e8ce0b18915 1515 memset(wizVal, 48, numOutputs);
mjr 73:4e8ce0b18915 1516
mjr 73:4e8ce0b18915 1517 // set all LedWiz virtual unit flash speeds to 2
mjr 73:4e8ce0b18915 1518 for (i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 1519 wizSpeed[i] = 2;
mjr 33:d832bcab089e 1520
mjr 35:e959ffba78fd 1521 // create the pin interface object for each port
mjr 35:e959ffba78fd 1522 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 1523 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 1524 }
mjr 6:cc35eb643e8f 1525
mjr 76:7f5912b6340e 1526 // Translate an LedWiz brightness level (0..49) to a DOF brightness
mjr 76:7f5912b6340e 1527 // level (0..255). Note that brightness level 49 isn't actually valid,
mjr 76:7f5912b6340e 1528 // according to the LedWiz API documentation, but many clients use it
mjr 76:7f5912b6340e 1529 // anyway, and the real LedWiz accepts it and seems to treat it as
mjr 76:7f5912b6340e 1530 // equivalent to 48.
mjr 40:cc0d9814522b 1531 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 1532 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 1533 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 1534 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 1535 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 1536 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 1537 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 1538 255, 255
mjr 40:cc0d9814522b 1539 };
mjr 40:cc0d9814522b 1540
mjr 76:7f5912b6340e 1541 // Translate a DOF brightness level (0..255) to an LedWiz brightness
mjr 76:7f5912b6340e 1542 // level (1..48)
mjr 76:7f5912b6340e 1543 static const uint8_t dof_to_lw[] = {
mjr 76:7f5912b6340e 1544 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
mjr 76:7f5912b6340e 1545 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
mjr 76:7f5912b6340e 1546 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9,
mjr 76:7f5912b6340e 1547 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12,
mjr 76:7f5912b6340e 1548 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15,
mjr 76:7f5912b6340e 1549 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18,
mjr 76:7f5912b6340e 1550 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21,
mjr 76:7f5912b6340e 1551 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24,
mjr 76:7f5912b6340e 1552 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27,
mjr 76:7f5912b6340e 1553 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30,
mjr 76:7f5912b6340e 1554 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
mjr 76:7f5912b6340e 1555 33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36,
mjr 76:7f5912b6340e 1556 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
mjr 76:7f5912b6340e 1557 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
mjr 76:7f5912b6340e 1558 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45,
mjr 76:7f5912b6340e 1559 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48
mjr 76:7f5912b6340e 1560 };
mjr 76:7f5912b6340e 1561
mjr 74:822a92bc11d2 1562 // LedWiz flash cycle tables. For efficiency, we use a lookup table
mjr 74:822a92bc11d2 1563 // rather than calculating these on the fly. The flash cycles are
mjr 74:822a92bc11d2 1564 // generated by the following formulas, where 'c' is the current
mjr 74:822a92bc11d2 1565 // cycle counter, from 0 to 255:
mjr 74:822a92bc11d2 1566 //
mjr 74:822a92bc11d2 1567 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 1568 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 1569 // mode 131 = on/ramp down = (c < 128 ? 255 : (255-c)*2)
mjr 74:822a92bc11d2 1570 // mode 132 = ramp up/on = (c < 128 ? c*2 : 255)
mjr 74:822a92bc11d2 1571 //
mjr 74:822a92bc11d2 1572 // To look up the current output value for a given mode and a given
mjr 74:822a92bc11d2 1573 // cycle counter 'c', index the table with ((mode-129)*256)+c.
mjr 74:822a92bc11d2 1574 static const uint8_t wizFlashLookup[] = {
mjr 74:822a92bc11d2 1575 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 1576 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
mjr 74:822a92bc11d2 1577 0x21, 0x23, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f,
mjr 74:822a92bc11d2 1578 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f,
mjr 74:822a92bc11d2 1579 0x61, 0x63, 0x65, 0x67, 0x69, 0x6b, 0x6d, 0x6f, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d, 0x7f,
mjr 74:822a92bc11d2 1580 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d, 0x8f, 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9d, 0x9f,
mjr 74:822a92bc11d2 1581 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad, 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, 0xbf,
mjr 74:822a92bc11d2 1582 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
mjr 74:822a92bc11d2 1583 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb, 0xed, 0xef, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0xfd, 0xff,
mjr 74:822a92bc11d2 1584 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 1585 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 1586 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 1587 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 1588 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 1589 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 1590 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 1591 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 1592
mjr 74:822a92bc11d2 1593 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 1594 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1595 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1596 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1597 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1598 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1599 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1600 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1601 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1602 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1603 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1604 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1605 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1606 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1607 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1608 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1609 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 1610
mjr 74:822a92bc11d2 1611 // mode 131 = on/ramp down = c < 128 ? 255 : (255 - c)*2
mjr 74:822a92bc11d2 1612 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1613 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1614 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1615 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1616 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1617 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1618 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1619 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1620 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 1621 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 1622 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 1623 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 1624 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 1625 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 1626 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 1627 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 1628
mjr 74:822a92bc11d2 1629 // mode 132 = ramp up/on = c < 128 ? c*2 : 255
mjr 74:822a92bc11d2 1630 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
mjr 74:822a92bc11d2 1631 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
mjr 74:822a92bc11d2 1632 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
mjr 74:822a92bc11d2 1633 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
mjr 74:822a92bc11d2 1634 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
mjr 74:822a92bc11d2 1635 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
mjr 74:822a92bc11d2 1636 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
mjr 74:822a92bc11d2 1637 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
mjr 74:822a92bc11d2 1638 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1639 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1640 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1641 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1642 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1643 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1644 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 1645 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
mjr 74:822a92bc11d2 1646 };
mjr 74:822a92bc11d2 1647
mjr 74:822a92bc11d2 1648 // LedWiz flash cycle timer. This runs continuously. On each update,
mjr 74:822a92bc11d2 1649 // we use this to figure out where we are on the cycle for each bank.
mjr 74:822a92bc11d2 1650 Timer wizCycleTimer;
mjr 74:822a92bc11d2 1651
mjr 76:7f5912b6340e 1652 // timing statistics for wizPulse()
mjr 76:7f5912b6340e 1653 uint64_t wizPulseTotalTime, wizPulseRunCount;
mjr 76:7f5912b6340e 1654
mjr 76:7f5912b6340e 1655 // LedWiz flash timer pulse. The main loop calls this on each cycle
mjr 76:7f5912b6340e 1656 // to update outputs using LedWiz flash modes. We do one bank of 32
mjr 76:7f5912b6340e 1657 // outputs on each cycle.
mjr 29:582472d0bc57 1658 static void wizPulse()
mjr 29:582472d0bc57 1659 {
mjr 76:7f5912b6340e 1660 // current bank
mjr 76:7f5912b6340e 1661 static int wizPulseBank = 0;
mjr 76:7f5912b6340e 1662
mjr 76:7f5912b6340e 1663 // start a timer for statistics collection
mjr 76:7f5912b6340e 1664 IF_DIAG(
mjr 76:7f5912b6340e 1665 Timer t;
mjr 76:7f5912b6340e 1666 t.start();
mjr 76:7f5912b6340e 1667 )
mjr 76:7f5912b6340e 1668
mjr 76:7f5912b6340e 1669 // Update the current bank's cycle counter: figure the current
mjr 76:7f5912b6340e 1670 // phase of the LedWiz pulse cycle for this bank.
mjr 76:7f5912b6340e 1671 //
mjr 76:7f5912b6340e 1672 // The LedWiz speed setting gives the flash period in 0.25s units
mjr 76:7f5912b6340e 1673 // (speed 1 is a flash period of .25s, speed 7 is a period of 1.75s).
mjr 76:7f5912b6340e 1674 //
mjr 76:7f5912b6340e 1675 // What we're after here is the "phase", which is to say the point
mjr 76:7f5912b6340e 1676 // in the current cycle. If we assume that the cycle has been running
mjr 76:7f5912b6340e 1677 // continuously since some arbitrary time zero in the past, we can
mjr 76:7f5912b6340e 1678 // figure where we are in the current cycle by dividing the time since
mjr 76:7f5912b6340e 1679 // that zero by the cycle period and taking the remainder. E.g., if
mjr 76:7f5912b6340e 1680 // the cycle time is 5 seconds, and the time since t-zero is 17 seconds,
mjr 76:7f5912b6340e 1681 // we divide 17 by 5 to get a remainder of 2. That says we're 2 seconds
mjr 76:7f5912b6340e 1682 // into the current 5-second cycle, or 2/5 of the way through the
mjr 76:7f5912b6340e 1683 // current cycle.
mjr 76:7f5912b6340e 1684 //
mjr 76:7f5912b6340e 1685 // We do this calculation on every iteration of the main loop, so we
mjr 76:7f5912b6340e 1686 // want it to be very fast. To streamline it, we'll use some tricky
mjr 76:7f5912b6340e 1687 // integer arithmetic. The result will be the same as the straightforward
mjr 76:7f5912b6340e 1688 // remainder and fraction calculation we just explained, but we'll get
mjr 76:7f5912b6340e 1689 // there by less-than-obvious means.
mjr 76:7f5912b6340e 1690 //
mjr 76:7f5912b6340e 1691 // Rather than finding the phase as a continuous quantity or floating
mjr 76:7f5912b6340e 1692 // point number, we'll quantize it. We'll divide each cycle into 256
mjr 76:7f5912b6340e 1693 // time units, or quanta. Each quantum is 1/256 of the cycle length,
mjr 76:7f5912b6340e 1694 // so for a 1-second cycle (LedWiz speed 4), each quantum is 1/256 of
mjr 76:7f5912b6340e 1695 // a second, or about 3.9ms. If we express the time since t-zero in
mjr 76:7f5912b6340e 1696 // these units, the time period of one cycle is exactly 256 units, so
mjr 76:7f5912b6340e 1697 // we can calculate our point in the cycle by taking the remainder of
mjr 76:7f5912b6340e 1698 // the time (in our funny units) divided by 256. The special thing
mjr 76:7f5912b6340e 1699 // about making the cycle time equal to 256 units is that "x % 256"
mjr 76:7f5912b6340e 1700 // is exactly the same as "x & 255", which is a much faster operation
mjr 76:7f5912b6340e 1701 // than division on ARM M0+: this CPU has no hardware DIVIDE operation,
mjr 76:7f5912b6340e 1702 // so an integer division takes about 5us. The bit mask operation, in
mjr 76:7f5912b6340e 1703 // contrast, takes only about 60ns - about 100x faster. 5us doesn't
mjr 76:7f5912b6340e 1704 // sound like much, but we do this on every main loop, so every little
mjr 76:7f5912b6340e 1705 // bit counts.
mjr 76:7f5912b6340e 1706 //
mjr 76:7f5912b6340e 1707 // The snag is that our system timer gives us the elapsed time in
mjr 76:7f5912b6340e 1708 // microseconds. We still need to convert this to our special quanta
mjr 76:7f5912b6340e 1709 // of 256 units per cycle. The straightforward way to do that is by
mjr 76:7f5912b6340e 1710 // dividing by (microseconds per quantum). E.g., for LedWiz speed 4,
mjr 76:7f5912b6340e 1711 // we decided that our quantum was 1/256 of a second, or 3906us, so
mjr 76:7f5912b6340e 1712 // dividing the current system time in microseconds by 3906 will give
mjr 76:7f5912b6340e 1713 // us the time in our quantum units. But now we've just substituted
mjr 76:7f5912b6340e 1714 // one division for another!
mjr 76:7f5912b6340e 1715 //
mjr 76:7f5912b6340e 1716 // This is where our really tricky integer math comes in. Dividing
mjr 76:7f5912b6340e 1717 // by X is the same as multiplying by 1/X. In integer math, 1/3906
mjr 76:7f5912b6340e 1718 // is zero, so that won't work. But we can get around that by doing
mjr 76:7f5912b6340e 1719 // the integer math as "fixed point" arithmetic instead. It's still
mjr 76:7f5912b6340e 1720 // actually carried out as integer operations, but we'll scale our
mjr 76:7f5912b6340e 1721 // integers by a scaling factor, then take out the scaling factor
mjr 76:7f5912b6340e 1722 // later to get the final result. The scaling factor we'll use is
mjr 76:7f5912b6340e 1723 // 2^24. So we're going to calculate (time * 2^24/3906), then divide
mjr 76:7f5912b6340e 1724 // the result by 2^24 to get the final answer. I know it seems like
mjr 76:7f5912b6340e 1725 // we're substituting one division for another yet again, but this
mjr 76:7f5912b6340e 1726 // time's the charm, because dividing by 2^24 is a bit shift operation,
mjr 76:7f5912b6340e 1727 // which is another single-cycle operation on M0+. You might also
mjr 76:7f5912b6340e 1728 // wonder how all these tricks don't cause overflows or underflows
mjr 76:7f5912b6340e 1729 // or what not. Well, the multiply by 2^24/3906 will cause an
mjr 76:7f5912b6340e 1730 // overflow, but we don't care, because the overflow will all be in
mjr 76:7f5912b6340e 1731 // the high-order bits that we're going to discard in the final
mjr 76:7f5912b6340e 1732 // remainder calculation anyway.
mjr 76:7f5912b6340e 1733 //
mjr 76:7f5912b6340e 1734 // Each entry in the array below represents 2^24/N for the corresponding
mjr 76:7f5912b6340e 1735 // LedWiz speed, where N is the number of time quanta per cycle at that
mjr 76:7f5912b6340e 1736 // speed. The time quanta are chosen such that 256 quanta add up to
mjr 76:7f5912b6340e 1737 // approximately (LedWiz speed setting * 0.25s).
mjr 76:7f5912b6340e 1738 //
mjr 76:7f5912b6340e 1739 // Note that the calculation has an implicit bit mask (result & 0xFF)
mjr 76:7f5912b6340e 1740 // to get the final result mod 256. But we don't have to actually
mjr 76:7f5912b6340e 1741 // do that work because we're using 32-bit ints and a 2^24 fixed
mjr 76:7f5912b6340e 1742 // point base (X in the narrative above). The final shift right by
mjr 76:7f5912b6340e 1743 // 24 bits to divide out the base will leave us with only 8 bits in
mjr 76:7f5912b6340e 1744 // the result, since we started with 32.
mjr 76:7f5912b6340e 1745 static const uint32_t inv_us_per_quantum[] = { // indexed by LedWiz speed
mjr 76:7f5912b6340e 1746 0, 17172, 8590, 5726, 4295, 3436, 2863, 2454
mjr 76:7f5912b6340e 1747 };
mjr 76:7f5912b6340e 1748 int counter = ((wizCycleTimer.read_us() * inv_us_per_quantum[wizSpeed[wizPulseBank]]) >> 24);
mjr 76:7f5912b6340e 1749
mjr 76:7f5912b6340e 1750 // get the range of 32 output sin this bank
mjr 76:7f5912b6340e 1751 int fromPort = wizPulseBank*32;
mjr 76:7f5912b6340e 1752 int toPort = fromPort+32;
mjr 76:7f5912b6340e 1753 if (toPort > numOutputs)
mjr 76:7f5912b6340e 1754 toPort = numOutputs;
mjr 76:7f5912b6340e 1755
mjr 76:7f5912b6340e 1756 // update all outputs set to flashing values
mjr 76:7f5912b6340e 1757 for (int i = fromPort ; i < toPort ; ++i)
mjr 73:4e8ce0b18915 1758 {
mjr 76:7f5912b6340e 1759 // Update the port only if the LedWiz SBA switch for the port is on
mjr 76:7f5912b6340e 1760 // (wizOn[i]) AND the port is a PBA flash mode in the range 129..132.
mjr 76:7f5912b6340e 1761 // These modes and only these modes have the high bit (0x80) set, so
mjr 76:7f5912b6340e 1762 // we can test for them simply by testing the high bit.
mjr 76:7f5912b6340e 1763 if (wizOn[i])
mjr 29:582472d0bc57 1764 {
mjr 76:7f5912b6340e 1765 uint8_t val = wizVal[i];
mjr 76:7f5912b6340e 1766 if ((val & 0x80) != 0)
mjr 29:582472d0bc57 1767 {
mjr 76:7f5912b6340e 1768 // ook up the value for the mode at the cycle time
mjr 76:7f5912b6340e 1769 lwPin[i]->set(outLevel[i] = wizFlashLookup[((val-129) << 8) + counter]);
mjr 29:582472d0bc57 1770 }
mjr 29:582472d0bc57 1771 }
mjr 76:7f5912b6340e 1772 }
mjr 76:7f5912b6340e 1773
mjr 34:6b981a2afab7 1774 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 1775 if (hc595 != 0)
mjr 35:e959ffba78fd 1776 hc595->update();
mjr 76:7f5912b6340e 1777
mjr 76:7f5912b6340e 1778 // switch to the next bank
mjr 76:7f5912b6340e 1779 if (++wizPulseBank >= MAX_LW_BANKS)
mjr 76:7f5912b6340e 1780 wizPulseBank = 0;
mjr 76:7f5912b6340e 1781
mjr 76:7f5912b6340e 1782 // collect timing statistics
mjr 76:7f5912b6340e 1783 IF_DIAG(
mjr 76:7f5912b6340e 1784 wizPulseTotalTime += t.read_us();
mjr 76:7f5912b6340e 1785 wizPulseRunCount += 1;
mjr 76:7f5912b6340e 1786 )
mjr 1:d913e0afb2ac 1787 }
mjr 38:091e511ce8a0 1788
mjr 76:7f5912b6340e 1789 // Update a port to reflect its new LedWiz SBA+PBA setting.
mjr 76:7f5912b6340e 1790 static void updateLwPort(int port)
mjr 38:091e511ce8a0 1791 {
mjr 76:7f5912b6340e 1792 // check if the SBA switch is on or off
mjr 76:7f5912b6340e 1793 if (wizOn[port])
mjr 76:7f5912b6340e 1794 {
mjr 76:7f5912b6340e 1795 // It's on. If the port is a valid static brightness level,
mjr 76:7f5912b6340e 1796 // set the output port to match. Otherwise leave it as is:
mjr 76:7f5912b6340e 1797 // if it's a flashing mode, the flash mode pulse will update
mjr 76:7f5912b6340e 1798 // it on the next cycle.
mjr 76:7f5912b6340e 1799 int val = wizVal[port];
mjr 76:7f5912b6340e 1800 if (val <= 49)
mjr 76:7f5912b6340e 1801 lwPin[port]->set(outLevel[port] = lw_to_dof[val]);
mjr 76:7f5912b6340e 1802 }
mjr 76:7f5912b6340e 1803 else
mjr 76:7f5912b6340e 1804 {
mjr 76:7f5912b6340e 1805 // the port is off - set absolute brightness zero
mjr 76:7f5912b6340e 1806 lwPin[port]->set(outLevel[port] = 0);
mjr 76:7f5912b6340e 1807 }
mjr 73:4e8ce0b18915 1808 }
mjr 73:4e8ce0b18915 1809
mjr 73:4e8ce0b18915 1810 // Turn off all outputs and restore everything to the default LedWiz
mjr 73:4e8ce0b18915 1811 // state. This sets outputs #1-32 to LedWiz profile value 48 (full
mjr 73:4e8ce0b18915 1812 // brightness) and switch state Off, sets all extended outputs (#33
mjr 73:4e8ce0b18915 1813 // and above) to zero brightness, and sets the LedWiz flash rate to 2.
mjr 73:4e8ce0b18915 1814 // This effectively restores the power-on conditions.
mjr 73:4e8ce0b18915 1815 //
mjr 73:4e8ce0b18915 1816 void allOutputsOff()
mjr 73:4e8ce0b18915 1817 {
mjr 73:4e8ce0b18915 1818 // reset all LedWiz outputs to OFF/48
mjr 73:4e8ce0b18915 1819 for (int i = 0 ; i < numOutputs ; ++i)
mjr 73:4e8ce0b18915 1820 {
mjr 73:4e8ce0b18915 1821 outLevel[i] = 0;
mjr 73:4e8ce0b18915 1822 wizOn[i] = 0;
mjr 73:4e8ce0b18915 1823 wizVal[i] = 48;
mjr 73:4e8ce0b18915 1824 lwPin[i]->set(0);
mjr 73:4e8ce0b18915 1825 }
mjr 73:4e8ce0b18915 1826
mjr 73:4e8ce0b18915 1827 // restore default LedWiz flash rate
mjr 73:4e8ce0b18915 1828 for (int i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 1829 wizSpeed[i] = 2;
mjr 38:091e511ce8a0 1830
mjr 73:4e8ce0b18915 1831 // flush changes to hc595, if applicable
mjr 38:091e511ce8a0 1832 if (hc595 != 0)
mjr 38:091e511ce8a0 1833 hc595->update();
mjr 38:091e511ce8a0 1834 }
mjr 38:091e511ce8a0 1835
mjr 74:822a92bc11d2 1836 // Cary out an SBA or SBX message. portGroup is 0 for ports 1-32,
mjr 74:822a92bc11d2 1837 // 1 for ports 33-64, etc. Original protocol SBA messages always
mjr 74:822a92bc11d2 1838 // address port group 0; our private SBX extension messages can
mjr 74:822a92bc11d2 1839 // address any port group.
mjr 74:822a92bc11d2 1840 void sba_sbx(int portGroup, const uint8_t *data)
mjr 74:822a92bc11d2 1841 {
mjr 76:7f5912b6340e 1842 // update all on/off states in the group
mjr 74:822a92bc11d2 1843 for (int i = 0, bit = 1, imsg = 1, port = portGroup*32 ;
mjr 74:822a92bc11d2 1844 i < 32 && port < numOutputs ;
mjr 74:822a92bc11d2 1845 ++i, bit <<= 1, ++port)
mjr 74:822a92bc11d2 1846 {
mjr 74:822a92bc11d2 1847 // figure the on/off state bit for this output
mjr 74:822a92bc11d2 1848 if (bit == 0x100) {
mjr 74:822a92bc11d2 1849 bit = 1;
mjr 74:822a92bc11d2 1850 ++imsg;
mjr 74:822a92bc11d2 1851 }
mjr 74:822a92bc11d2 1852
mjr 74:822a92bc11d2 1853 // set the on/off state
mjr 76:7f5912b6340e 1854 bool on = wizOn[port] = ((data[imsg] & bit) != 0);
mjr 76:7f5912b6340e 1855
mjr 76:7f5912b6340e 1856 // set the output port brightness to match the new setting
mjr 76:7f5912b6340e 1857 updateLwPort(port);
mjr 74:822a92bc11d2 1858 }
mjr 74:822a92bc11d2 1859
mjr 74:822a92bc11d2 1860 // set the flash speed for the port group
mjr 74:822a92bc11d2 1861 if (portGroup < countof(wizSpeed))
mjr 74:822a92bc11d2 1862 wizSpeed[portGroup] = (data[5] < 1 ? 1 : data[5] > 7 ? 7 : data[5]);
mjr 74:822a92bc11d2 1863
mjr 76:7f5912b6340e 1864 // update 74HC959 outputs
mjr 76:7f5912b6340e 1865 if (hc595 != 0)
mjr 76:7f5912b6340e 1866 hc595->update();
mjr 74:822a92bc11d2 1867 }
mjr 74:822a92bc11d2 1868
mjr 74:822a92bc11d2 1869 // Carry out a PBA or PBX message.
mjr 74:822a92bc11d2 1870 void pba_pbx(int basePort, const uint8_t *data)
mjr 74:822a92bc11d2 1871 {
mjr 74:822a92bc11d2 1872 // update each wizVal entry from the brightness data
mjr 76:7f5912b6340e 1873 for (int i = 0, port = basePort ; i < 8 && port < numOutputs ; ++i, ++port)
mjr 74:822a92bc11d2 1874 {
mjr 74:822a92bc11d2 1875 // get the value
mjr 74:822a92bc11d2 1876 uint8_t v = data[i];
mjr 74:822a92bc11d2 1877
mjr 74:822a92bc11d2 1878 // Validate it. The legal values are 0..49 for brightness
mjr 74:822a92bc11d2 1879 // levels, and 128..132 for flash modes. Set anything invalid
mjr 74:822a92bc11d2 1880 // to full brightness (48) instead. Note that 49 isn't actually
mjr 74:822a92bc11d2 1881 // a valid documented value, but in practice some clients send
mjr 74:822a92bc11d2 1882 // this to mean 100% brightness, and the real LedWiz treats it
mjr 74:822a92bc11d2 1883 // as such.
mjr 74:822a92bc11d2 1884 if ((v > 49 && v < 129) || v > 132)
mjr 74:822a92bc11d2 1885 v = 48;
mjr 74:822a92bc11d2 1886
mjr 74:822a92bc11d2 1887 // store it
mjr 76:7f5912b6340e 1888 wizVal[port] = v;
mjr 76:7f5912b6340e 1889
mjr 76:7f5912b6340e 1890 // update the port
mjr 76:7f5912b6340e 1891 updateLwPort(port);
mjr 74:822a92bc11d2 1892 }
mjr 74:822a92bc11d2 1893
mjr 76:7f5912b6340e 1894 // update 74HC595 outputs
mjr 76:7f5912b6340e 1895 if (hc595 != 0)
mjr 76:7f5912b6340e 1896 hc595->update();
mjr 74:822a92bc11d2 1897 }
mjr 74:822a92bc11d2 1898
mjr 74:822a92bc11d2 1899
mjr 11:bd9da7088e6e 1900 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 1901 //
mjr 11:bd9da7088e6e 1902 // Button input
mjr 11:bd9da7088e6e 1903 //
mjr 11:bd9da7088e6e 1904
mjr 18:5e890ebd0023 1905 // button state
mjr 18:5e890ebd0023 1906 struct ButtonState
mjr 18:5e890ebd0023 1907 {
mjr 38:091e511ce8a0 1908 ButtonState()
mjr 38:091e511ce8a0 1909 {
mjr 53:9b2611964afc 1910 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 1911 virtState = 0;
mjr 53:9b2611964afc 1912 dbState = 0;
mjr 38:091e511ce8a0 1913 pulseState = 0;
mjr 53:9b2611964afc 1914 pulseTime = 0;
mjr 38:091e511ce8a0 1915 }
mjr 35:e959ffba78fd 1916
mjr 53:9b2611964afc 1917 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 1918 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 1919 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 1920 //
mjr 53:9b2611964afc 1921 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 1922 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 1923 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 1924 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 1925 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 1926 void virtPress(bool on)
mjr 53:9b2611964afc 1927 {
mjr 53:9b2611964afc 1928 // Increment or decrement the current state
mjr 53:9b2611964afc 1929 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 1930 }
mjr 53:9b2611964afc 1931
mjr 53:9b2611964afc 1932 // DigitalIn for the button, if connected to a physical input
mjr 73:4e8ce0b18915 1933 TinyDigitalIn di;
mjr 38:091e511ce8a0 1934
mjr 65:739875521aae 1935 // Time of last pulse state transition.
mjr 65:739875521aae 1936 //
mjr 65:739875521aae 1937 // Each state change sticks for a minimum period; when the timer expires,
mjr 65:739875521aae 1938 // if the underlying physical switch is in a different state, we switch
mjr 65:739875521aae 1939 // to the next state and restart the timer. pulseTime is the time remaining
mjr 65:739875521aae 1940 // remaining before we can make another state transition, in microseconds.
mjr 65:739875521aae 1941 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 65:739875521aae 1942 // this guarantees that the parity of the pulse count always matches the
mjr 65:739875521aae 1943 // current physical switch state when the latter is stable, which makes
mjr 65:739875521aae 1944 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 65:739875521aae 1945 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 65:739875521aae 1946 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 65:739875521aae 1947 // This software system can't be fooled that way.)
mjr 65:739875521aae 1948 uint32_t pulseTime;
mjr 18:5e890ebd0023 1949
mjr 65:739875521aae 1950 // Config key index. This points to the ButtonCfg structure in the
mjr 65:739875521aae 1951 // configuration that contains the PC key mapping for the button.
mjr 65:739875521aae 1952 uint8_t cfgIndex;
mjr 53:9b2611964afc 1953
mjr 53:9b2611964afc 1954 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 1955 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 1956 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 1957 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 1958 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 1959 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 1960 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 1961 // and physical source states.
mjr 53:9b2611964afc 1962 uint8_t virtState;
mjr 38:091e511ce8a0 1963
mjr 38:091e511ce8a0 1964 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 1965 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 1966 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 1967 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 1968 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 1969 uint8_t dbState;
mjr 38:091e511ce8a0 1970
mjr 65:739875521aae 1971 // current PHYSICAL on/off state, after debouncing
mjr 65:739875521aae 1972 uint8_t physState : 1;
mjr 65:739875521aae 1973
mjr 65:739875521aae 1974 // current LOGICAL on/off state as reported to the host.
mjr 65:739875521aae 1975 uint8_t logState : 1;
mjr 65:739875521aae 1976
mjr 65:739875521aae 1977 // previous logical on/off state, when keys were last processed for USB
mjr 65:739875521aae 1978 // reports and local effects
mjr 65:739875521aae 1979 uint8_t prevLogState : 1;
mjr 65:739875521aae 1980
mjr 65:739875521aae 1981 // Pulse state
mjr 65:739875521aae 1982 //
mjr 65:739875521aae 1983 // A button in pulse mode (selected via the config flags for the button)
mjr 65:739875521aae 1984 // transmits a brief logical button press and release each time the attached
mjr 65:739875521aae 1985 // physical switch changes state. This is useful for cases where the host
mjr 65:739875521aae 1986 // expects a key press for each change in the state of the physical switch.
mjr 65:739875521aae 1987 // The canonical example is the Coin Door switch in VPinMAME, which requires
mjr 65:739875521aae 1988 // pressing the END key to toggle the open/closed state. This software design
mjr 65:739875521aae 1989 // isn't easily implemented in a physical coin door, though; the simplest
mjr 65:739875521aae 1990 // physical sensor for the coin door state is a switch that's on when the
mjr 65:739875521aae 1991 // door is open and off when the door is closed (or vice versa, but in either
mjr 65:739875521aae 1992 // case, the switch state corresponds to the current state of the door at any
mjr 65:739875521aae 1993 // given time, rather than pulsing on state changes). The "pulse mode"
mjr 65:739875521aae 1994 // option brdiges this gap by generating a toggle key event each time
mjr 65:739875521aae 1995 // there's a change to the physical switch's state.
mjr 38:091e511ce8a0 1996 //
mjr 38:091e511ce8a0 1997 // Pulse state:
mjr 38:091e511ce8a0 1998 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 1999 // 1 -> off
mjr 38:091e511ce8a0 2000 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 2001 // 3 -> on
mjr 38:091e511ce8a0 2002 // 4 -> transitioning on-off
mjr 65:739875521aae 2003 uint8_t pulseState : 3; // 5 states -> we need 3 bits
mjr 65:739875521aae 2004
mjr 65:739875521aae 2005 } __attribute__((packed));
mjr 65:739875521aae 2006
mjr 65:739875521aae 2007 ButtonState *buttonState; // live button slots, allocated on startup
mjr 65:739875521aae 2008 int8_t nButtons; // number of live button slots allocated
mjr 65:739875521aae 2009 int8_t zblButtonIndex = -1; // index of ZB Launch button slot; -1 if unused
mjr 18:5e890ebd0023 2010
mjr 66:2e3583fbd2f4 2011 // Shift button state
mjr 66:2e3583fbd2f4 2012 struct
mjr 66:2e3583fbd2f4 2013 {
mjr 66:2e3583fbd2f4 2014 int8_t index; // buttonState[] index of shift button; -1 if none
mjr 66:2e3583fbd2f4 2015 uint8_t state : 2; // current shift state:
mjr 66:2e3583fbd2f4 2016 // 0 = not shifted
mjr 66:2e3583fbd2f4 2017 // 1 = shift button down, no key pressed yet
mjr 66:2e3583fbd2f4 2018 // 2 = shift button down, key pressed
mjr 66:2e3583fbd2f4 2019 uint8_t pulse : 1; // sending pulsed keystroke on release
mjr 66:2e3583fbd2f4 2020 uint32_t pulseTime; // time of start of pulsed keystroke
mjr 66:2e3583fbd2f4 2021 }
mjr 66:2e3583fbd2f4 2022 __attribute__((packed)) shiftButton;
mjr 38:091e511ce8a0 2023
mjr 38:091e511ce8a0 2024 // Button data
mjr 38:091e511ce8a0 2025 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 2026
mjr 38:091e511ce8a0 2027 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 2028 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 2029 // modifier keys.
mjr 38:091e511ce8a0 2030 struct
mjr 38:091e511ce8a0 2031 {
mjr 38:091e511ce8a0 2032 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 2033 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 2034 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 2035 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 2036 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 2037
mjr 38:091e511ce8a0 2038 // Media key state
mjr 38:091e511ce8a0 2039 struct
mjr 38:091e511ce8a0 2040 {
mjr 38:091e511ce8a0 2041 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 2042 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 2043 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 2044
mjr 38:091e511ce8a0 2045 // button scan interrupt ticker
mjr 38:091e511ce8a0 2046 Ticker buttonTicker;
mjr 38:091e511ce8a0 2047
mjr 38:091e511ce8a0 2048 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 2049 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 2050 void scanButtons()
mjr 38:091e511ce8a0 2051 {
mjr 38:091e511ce8a0 2052 // scan all button input pins
mjr 73:4e8ce0b18915 2053 ButtonState *bs = buttonState, *last = bs + nButtons;
mjr 73:4e8ce0b18915 2054 for ( ; bs < last ; ++bs)
mjr 38:091e511ce8a0 2055 {
mjr 73:4e8ce0b18915 2056 // Shift the new state into the debounce history
mjr 73:4e8ce0b18915 2057 uint8_t db = (bs->dbState << 1) | bs->di.read();
mjr 73:4e8ce0b18915 2058 bs->dbState = db;
mjr 73:4e8ce0b18915 2059
mjr 73:4e8ce0b18915 2060 // If we have all 0's or 1's in the history for the required
mjr 73:4e8ce0b18915 2061 // debounce period, the key state is stable, so apply the new
mjr 73:4e8ce0b18915 2062 // physical state. Note that the pins are active low, so the
mjr 73:4e8ce0b18915 2063 // new button on/off state is the inverse of the GPIO state.
mjr 73:4e8ce0b18915 2064 const uint8_t stable = 0x1F; // 00011111b -> low 5 bits = last 5 readings
mjr 73:4e8ce0b18915 2065 db &= stable;
mjr 73:4e8ce0b18915 2066 if (db == 0 || db == stable)
mjr 73:4e8ce0b18915 2067 bs->physState = !db;
mjr 38:091e511ce8a0 2068 }
mjr 38:091e511ce8a0 2069 }
mjr 38:091e511ce8a0 2070
mjr 38:091e511ce8a0 2071 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 2072 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 2073 // in the physical button state.
mjr 38:091e511ce8a0 2074 Timer buttonTimer;
mjr 12:669df364a565 2075
mjr 65:739875521aae 2076 // Count a button during the initial setup scan
mjr 72:884207c0aab0 2077 void countButton(uint8_t typ, uint8_t shiftTyp, bool &kbKeys)
mjr 65:739875521aae 2078 {
mjr 65:739875521aae 2079 // count it
mjr 65:739875521aae 2080 ++nButtons;
mjr 65:739875521aae 2081
mjr 67:c39e66c4e000 2082 // if it's a keyboard key or media key, note that we need a USB
mjr 67:c39e66c4e000 2083 // keyboard interface
mjr 72:884207c0aab0 2084 if (typ == BtnTypeKey || typ == BtnTypeMedia
mjr 72:884207c0aab0 2085 || shiftTyp == BtnTypeKey || shiftTyp == BtnTypeMedia)
mjr 65:739875521aae 2086 kbKeys = true;
mjr 65:739875521aae 2087 }
mjr 65:739875521aae 2088
mjr 11:bd9da7088e6e 2089 // initialize the button inputs
mjr 35:e959ffba78fd 2090 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 2091 {
mjr 35:e959ffba78fd 2092 // presume we'll find no keyboard keys
mjr 35:e959ffba78fd 2093 kbKeys = false;
mjr 35:e959ffba78fd 2094
mjr 66:2e3583fbd2f4 2095 // presume no shift key
mjr 66:2e3583fbd2f4 2096 shiftButton.index = -1;
mjr 66:2e3583fbd2f4 2097
mjr 65:739875521aae 2098 // Count up how many button slots we'll need to allocate. Start
mjr 65:739875521aae 2099 // with assigned buttons from the configuration, noting that we
mjr 65:739875521aae 2100 // only need to create slots for buttons that are actually wired.
mjr 65:739875521aae 2101 nButtons = 0;
mjr 65:739875521aae 2102 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 2103 {
mjr 65:739875521aae 2104 // it's valid if it's wired to a real input pin
mjr 65:739875521aae 2105 if (wirePinName(cfg.button[i].pin) != NC)
mjr 72:884207c0aab0 2106 countButton(cfg.button[i].typ, cfg.button[i].typ2, kbKeys);
mjr 65:739875521aae 2107 }
mjr 65:739875521aae 2108
mjr 65:739875521aae 2109 // Count virtual buttons
mjr 65:739875521aae 2110
mjr 65:739875521aae 2111 // ZB Launch
mjr 65:739875521aae 2112 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 65:739875521aae 2113 {
mjr 65:739875521aae 2114 // valid - remember the live button index
mjr 65:739875521aae 2115 zblButtonIndex = nButtons;
mjr 65:739875521aae 2116
mjr 65:739875521aae 2117 // count it
mjr 72:884207c0aab0 2118 countButton(cfg.plunger.zbLaunchBall.keytype, BtnTypeNone, kbKeys);
mjr 65:739875521aae 2119 }
mjr 65:739875521aae 2120
mjr 65:739875521aae 2121 // Allocate the live button slots
mjr 65:739875521aae 2122 ButtonState *bs = buttonState = new ButtonState[nButtons];
mjr 65:739875521aae 2123
mjr 65:739875521aae 2124 // Configure the physical inputs
mjr 65:739875521aae 2125 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 2126 {
mjr 65:739875521aae 2127 PinName pin = wirePinName(cfg.button[i].pin);
mjr 65:739875521aae 2128 if (pin != NC)
mjr 65:739875521aae 2129 {
mjr 65:739875521aae 2130 // point back to the config slot for the keyboard data
mjr 65:739875521aae 2131 bs->cfgIndex = i;
mjr 65:739875521aae 2132
mjr 65:739875521aae 2133 // set up the GPIO input pin for this button
mjr 73:4e8ce0b18915 2134 bs->di.assignPin(pin);
mjr 65:739875521aae 2135
mjr 65:739875521aae 2136 // if it's a pulse mode button, set the initial pulse state to Off
mjr 65:739875521aae 2137 if (cfg.button[i].flags & BtnFlagPulse)
mjr 65:739875521aae 2138 bs->pulseState = 1;
mjr 65:739875521aae 2139
mjr 66:2e3583fbd2f4 2140 // If this is the shift button, note its buttonState[] index.
mjr 66:2e3583fbd2f4 2141 // We have to figure the buttonState[] index separately from
mjr 66:2e3583fbd2f4 2142 // the config index, because the indices can differ if some
mjr 66:2e3583fbd2f4 2143 // config slots are left unused.
mjr 66:2e3583fbd2f4 2144 if (cfg.shiftButton == i+1)
mjr 66:2e3583fbd2f4 2145 shiftButton.index = bs - buttonState;
mjr 66:2e3583fbd2f4 2146
mjr 65:739875521aae 2147 // advance to the next button
mjr 65:739875521aae 2148 ++bs;
mjr 65:739875521aae 2149 }
mjr 65:739875521aae 2150 }
mjr 65:739875521aae 2151
mjr 53:9b2611964afc 2152 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 2153 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 2154 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 2155 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 2156
mjr 53:9b2611964afc 2157 // ZB Launch Ball button
mjr 65:739875521aae 2158 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 11:bd9da7088e6e 2159 {
mjr 65:739875521aae 2160 // Point back to the config slot for the keyboard data.
mjr 66:2e3583fbd2f4 2161 // We use a special extra slot for virtual buttons,
mjr 66:2e3583fbd2f4 2162 // so we also need to set up the slot data by copying
mjr 66:2e3583fbd2f4 2163 // the ZBL config data to our virtual button slot.
mjr 65:739875521aae 2164 bs->cfgIndex = ZBL_BUTTON_CFG;
mjr 65:739875521aae 2165 cfg.button[ZBL_BUTTON_CFG].pin = PINNAME_TO_WIRE(NC);
mjr 65:739875521aae 2166 cfg.button[ZBL_BUTTON_CFG].typ = cfg.plunger.zbLaunchBall.keytype;
mjr 65:739875521aae 2167 cfg.button[ZBL_BUTTON_CFG].val = cfg.plunger.zbLaunchBall.keycode;
mjr 65:739875521aae 2168
mjr 66:2e3583fbd2f4 2169 // advance to the next button
mjr 65:739875521aae 2170 ++bs;
mjr 11:bd9da7088e6e 2171 }
mjr 12:669df364a565 2172
mjr 38:091e511ce8a0 2173 // start the button scan thread
mjr 38:091e511ce8a0 2174 buttonTicker.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 2175
mjr 38:091e511ce8a0 2176 // start the button state transition timer
mjr 12:669df364a565 2177 buttonTimer.start();
mjr 11:bd9da7088e6e 2178 }
mjr 11:bd9da7088e6e 2179
mjr 67:c39e66c4e000 2180 // Media key mapping. This maps from an 8-bit USB media key
mjr 67:c39e66c4e000 2181 // code to the corresponding bit in our USB report descriptor.
mjr 67:c39e66c4e000 2182 // The USB key code is the index, and the value at the index
mjr 67:c39e66c4e000 2183 // is the report descriptor bit. See joystick.cpp for the
mjr 67:c39e66c4e000 2184 // media descriptor details. Our currently mapped keys are:
mjr 67:c39e66c4e000 2185 //
mjr 67:c39e66c4e000 2186 // 0xE2 -> Mute -> 0x01
mjr 67:c39e66c4e000 2187 // 0xE9 -> Volume Up -> 0x02
mjr 67:c39e66c4e000 2188 // 0xEA -> Volume Down -> 0x04
mjr 67:c39e66c4e000 2189 // 0xB5 -> Next Track -> 0x08
mjr 67:c39e66c4e000 2190 // 0xB6 -> Previous Track -> 0x10
mjr 67:c39e66c4e000 2191 // 0xB7 -> Stop -> 0x20
mjr 67:c39e66c4e000 2192 // 0xCD -> Play / Pause -> 0x40
mjr 67:c39e66c4e000 2193 //
mjr 67:c39e66c4e000 2194 static const uint8_t mediaKeyMap[] = {
mjr 67:c39e66c4e000 2195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-0F
mjr 67:c39e66c4e000 2196 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-1F
mjr 67:c39e66c4e000 2197 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-2F
mjr 67:c39e66c4e000 2198 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-3F
mjr 67:c39e66c4e000 2199 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-4F
mjr 67:c39e66c4e000 2200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-5F
mjr 67:c39e66c4e000 2201 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-6F
mjr 67:c39e66c4e000 2202 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-7F
mjr 67:c39e66c4e000 2203 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
mjr 67:c39e66c4e000 2204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-9F
mjr 67:c39e66c4e000 2205 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0-AF
mjr 67:c39e66c4e000 2206 0, 0, 0, 0, 0, 8, 16, 32, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
mjr 67:c39e66c4e000 2207 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, // C0-CF
mjr 67:c39e66c4e000 2208 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D0-DF
mjr 67:c39e66c4e000 2209 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, // E0-EF
mjr 67:c39e66c4e000 2210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-FF
mjr 67:c39e66c4e000 2211 };
mjr 67:c39e66c4e000 2212
mjr 67:c39e66c4e000 2213
mjr 38:091e511ce8a0 2214 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 2215 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 2216 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 2217 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 2218 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 2219 {
mjr 35:e959ffba78fd 2220 // start with an empty list of USB key codes
mjr 35:e959ffba78fd 2221 uint8_t modkeys = 0;
mjr 35:e959ffba78fd 2222 uint8_t keys[7] = { 0, 0, 0, 0, 0, 0, 0 };
mjr 35:e959ffba78fd 2223 int nkeys = 0;
mjr 11:bd9da7088e6e 2224
mjr 35:e959ffba78fd 2225 // clear the joystick buttons
mjr 36:b9747461331e 2226 uint32_t newjs = 0;
mjr 35:e959ffba78fd 2227
mjr 35:e959ffba78fd 2228 // start with no media keys pressed
mjr 35:e959ffba78fd 2229 uint8_t mediakeys = 0;
mjr 38:091e511ce8a0 2230
mjr 38:091e511ce8a0 2231 // calculate the time since the last run
mjr 53:9b2611964afc 2232 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 2233 buttonTimer.reset();
mjr 66:2e3583fbd2f4 2234
mjr 66:2e3583fbd2f4 2235 // check the shift button state
mjr 66:2e3583fbd2f4 2236 if (shiftButton.index != -1)
mjr 66:2e3583fbd2f4 2237 {
mjr 66:2e3583fbd2f4 2238 ButtonState *sbs = &buttonState[shiftButton.index];
mjr 66:2e3583fbd2f4 2239 switch (shiftButton.state)
mjr 66:2e3583fbd2f4 2240 {
mjr 66:2e3583fbd2f4 2241 case 0:
mjr 66:2e3583fbd2f4 2242 // Not shifted. Check if the button is now down: if so,
mjr 66:2e3583fbd2f4 2243 // switch to state 1 (shift button down, no key pressed yet).
mjr 66:2e3583fbd2f4 2244 if (sbs->physState)
mjr 66:2e3583fbd2f4 2245 shiftButton.state = 1;
mjr 66:2e3583fbd2f4 2246 break;
mjr 66:2e3583fbd2f4 2247
mjr 66:2e3583fbd2f4 2248 case 1:
mjr 66:2e3583fbd2f4 2249 // Shift button down, no key pressed yet. If the button is
mjr 66:2e3583fbd2f4 2250 // now up, it counts as an ordinary button press instead of
mjr 66:2e3583fbd2f4 2251 // a shift button press, since the shift function was never
mjr 66:2e3583fbd2f4 2252 // used. Return to unshifted state and start a timed key
mjr 66:2e3583fbd2f4 2253 // pulse event.
mjr 66:2e3583fbd2f4 2254 if (!sbs->physState)
mjr 66:2e3583fbd2f4 2255 {
mjr 66:2e3583fbd2f4 2256 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 2257 shiftButton.pulse = 1;
mjr 66:2e3583fbd2f4 2258 shiftButton.pulseTime = 50000+dt; // 50 ms left on the key pulse
mjr 66:2e3583fbd2f4 2259 }
mjr 66:2e3583fbd2f4 2260 break;
mjr 66:2e3583fbd2f4 2261
mjr 66:2e3583fbd2f4 2262 case 2:
mjr 66:2e3583fbd2f4 2263 // Shift button down, other key was pressed. If the button is
mjr 66:2e3583fbd2f4 2264 // now up, simply clear the shift state without sending a key
mjr 66:2e3583fbd2f4 2265 // press for the shift button itself to the PC. The shift
mjr 66:2e3583fbd2f4 2266 // function was used, so its ordinary key press function is
mjr 66:2e3583fbd2f4 2267 // suppressed.
mjr 66:2e3583fbd2f4 2268 if (!sbs->physState)
mjr 66:2e3583fbd2f4 2269 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 2270 break;
mjr 66:2e3583fbd2f4 2271 }
mjr 66:2e3583fbd2f4 2272 }
mjr 38:091e511ce8a0 2273
mjr 11:bd9da7088e6e 2274 // scan the button list
mjr 18:5e890ebd0023 2275 ButtonState *bs = buttonState;
mjr 65:739875521aae 2276 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 11:bd9da7088e6e 2277 {
mjr 66:2e3583fbd2f4 2278 // Check the button type:
mjr 66:2e3583fbd2f4 2279 // - shift button
mjr 66:2e3583fbd2f4 2280 // - pulsed button
mjr 66:2e3583fbd2f4 2281 // - regular button
mjr 66:2e3583fbd2f4 2282 if (shiftButton.index == i)
mjr 66:2e3583fbd2f4 2283 {
mjr 66:2e3583fbd2f4 2284 // This is the shift button. Its logical state for key
mjr 66:2e3583fbd2f4 2285 // reporting purposes is controlled by the shift buttton
mjr 66:2e3583fbd2f4 2286 // pulse timer. If we're in a pulse, its logical state
mjr 66:2e3583fbd2f4 2287 // is pressed.
mjr 66:2e3583fbd2f4 2288 if (shiftButton.pulse)
mjr 66:2e3583fbd2f4 2289 {
mjr 66:2e3583fbd2f4 2290 // deduct the current interval from the pulse time, ending
mjr 66:2e3583fbd2f4 2291 // the pulse if the time has expired
mjr 66:2e3583fbd2f4 2292 if (shiftButton.pulseTime > dt)
mjr 66:2e3583fbd2f4 2293 shiftButton.pulseTime -= dt;
mjr 66:2e3583fbd2f4 2294 else
mjr 66:2e3583fbd2f4 2295 shiftButton.pulse = 0;
mjr 66:2e3583fbd2f4 2296 }
mjr 66:2e3583fbd2f4 2297
mjr 66:2e3583fbd2f4 2298 // the button is logically pressed if we're in a pulse
mjr 66:2e3583fbd2f4 2299 bs->logState = shiftButton.pulse;
mjr 66:2e3583fbd2f4 2300 }
mjr 66:2e3583fbd2f4 2301 else if (bs->pulseState != 0)
mjr 18:5e890ebd0023 2302 {
mjr 38:091e511ce8a0 2303 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 2304 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 2305 {
mjr 53:9b2611964afc 2306 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 2307 bs->pulseTime -= dt;
mjr 53:9b2611964afc 2308 }
mjr 53:9b2611964afc 2309 else
mjr 53:9b2611964afc 2310 {
mjr 53:9b2611964afc 2311 // pulse time expired - check for a state change
mjr 53:9b2611964afc 2312 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 2313 switch (bs->pulseState)
mjr 18:5e890ebd0023 2314 {
mjr 38:091e511ce8a0 2315 case 1:
mjr 38:091e511ce8a0 2316 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 2317 if (bs->physState)
mjr 53:9b2611964afc 2318 {
mjr 38:091e511ce8a0 2319 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 2320 bs->pulseState = 2;
mjr 53:9b2611964afc 2321 bs->logState = 1;
mjr 38:091e511ce8a0 2322 }
mjr 38:091e511ce8a0 2323 break;
mjr 18:5e890ebd0023 2324
mjr 38:091e511ce8a0 2325 case 2:
mjr 38:091e511ce8a0 2326 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 2327 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 2328 // change in state in the logical button
mjr 38:091e511ce8a0 2329 bs->pulseState = 3;
mjr 38:091e511ce8a0 2330 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 2331 bs->logState = 0;
mjr 38:091e511ce8a0 2332 break;
mjr 38:091e511ce8a0 2333
mjr 38:091e511ce8a0 2334 case 3:
mjr 38:091e511ce8a0 2335 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 2336 if (!bs->physState)
mjr 53:9b2611964afc 2337 {
mjr 38:091e511ce8a0 2338 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 2339 bs->pulseState = 4;
mjr 53:9b2611964afc 2340 bs->logState = 1;
mjr 38:091e511ce8a0 2341 }
mjr 38:091e511ce8a0 2342 break;
mjr 38:091e511ce8a0 2343
mjr 38:091e511ce8a0 2344 case 4:
mjr 38:091e511ce8a0 2345 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 2346 bs->pulseState = 1;
mjr 38:091e511ce8a0 2347 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 2348 bs->logState = 0;
mjr 38:091e511ce8a0 2349 break;
mjr 18:5e890ebd0023 2350 }
mjr 18:5e890ebd0023 2351 }
mjr 38:091e511ce8a0 2352 }
mjr 38:091e511ce8a0 2353 else
mjr 38:091e511ce8a0 2354 {
mjr 38:091e511ce8a0 2355 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 2356 bs->logState = bs->physState;
mjr 38:091e511ce8a0 2357 }
mjr 35:e959ffba78fd 2358
mjr 38:091e511ce8a0 2359 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 2360 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 2361 {
mjr 38:091e511ce8a0 2362 // check for special key transitions
mjr 53:9b2611964afc 2363 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 2364 {
mjr 53:9b2611964afc 2365 // Check the switch type in the config flags. If flag 0x01 is set,
mjr 53:9b2611964afc 2366 // it's a persistent on/off switch, so the night mode state simply
mjr 53:9b2611964afc 2367 // follows the current state of the switch. Otherwise, it's a
mjr 53:9b2611964afc 2368 // momentary button, so each button push (i.e., each transition from
mjr 53:9b2611964afc 2369 // logical state OFF to ON) toggles the current night mode state.
mjr 53:9b2611964afc 2370 if (cfg.nightMode.flags & 0x01)
mjr 53:9b2611964afc 2371 {
mjr 69:cc5039284fac 2372 // on/off switch - when the button changes state, change
mjr 53:9b2611964afc 2373 // night mode to match the new state
mjr 53:9b2611964afc 2374 setNightMode(bs->logState);
mjr 53:9b2611964afc 2375 }
mjr 53:9b2611964afc 2376 else
mjr 53:9b2611964afc 2377 {
mjr 66:2e3583fbd2f4 2378 // Momentary switch - toggle the night mode state when the
mjr 53:9b2611964afc 2379 // physical button is pushed (i.e., when its logical state
mjr 66:2e3583fbd2f4 2380 // transitions from OFF to ON).
mjr 66:2e3583fbd2f4 2381 //
mjr 66:2e3583fbd2f4 2382 // In momentary mode, night mode flag 0x02 makes it the
mjr 66:2e3583fbd2f4 2383 // shifted version of the button. In this case, only
mjr 66:2e3583fbd2f4 2384 // proceed if the shift button is pressed.
mjr 66:2e3583fbd2f4 2385 bool pressed = bs->logState;
mjr 66:2e3583fbd2f4 2386 if ((cfg.nightMode.flags & 0x02) != 0)
mjr 66:2e3583fbd2f4 2387 {
mjr 66:2e3583fbd2f4 2388 // if the shift button is pressed but hasn't been used
mjr 66:2e3583fbd2f4 2389 // as a shift yet, mark it as used, so that it doesn't
mjr 66:2e3583fbd2f4 2390 // also generate its own key code on release
mjr 66:2e3583fbd2f4 2391 if (shiftButton.state == 1)
mjr 66:2e3583fbd2f4 2392 shiftButton.state = 2;
mjr 66:2e3583fbd2f4 2393
mjr 66:2e3583fbd2f4 2394 // if the shift button isn't even pressed
mjr 66:2e3583fbd2f4 2395 if (shiftButton.state == 0)
mjr 66:2e3583fbd2f4 2396 pressed = false;
mjr 66:2e3583fbd2f4 2397 }
mjr 66:2e3583fbd2f4 2398
mjr 66:2e3583fbd2f4 2399 // if it's pressed (even after considering the shift mode),
mjr 66:2e3583fbd2f4 2400 // toggle night mode
mjr 66:2e3583fbd2f4 2401 if (pressed)
mjr 53:9b2611964afc 2402 toggleNightMode();
mjr 53:9b2611964afc 2403 }
mjr 35:e959ffba78fd 2404 }
mjr 38:091e511ce8a0 2405
mjr 38:091e511ce8a0 2406 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 2407 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 2408 }
mjr 38:091e511ce8a0 2409
mjr 53:9b2611964afc 2410 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 2411 // key state list
mjr 53:9b2611964afc 2412 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 2413 {
mjr 70:9f58735a1732 2414 // Get the key type and code. Start by assuming that we're
mjr 70:9f58735a1732 2415 // going to use the normal unshifted meaning.
mjr 65:739875521aae 2416 ButtonCfg *bc = &cfg.button[bs->cfgIndex];
mjr 70:9f58735a1732 2417 uint8_t typ = bc->typ;
mjr 70:9f58735a1732 2418 uint8_t val = bc->val;
mjr 70:9f58735a1732 2419
mjr 70:9f58735a1732 2420 // If the shift button is down, check for a shifted meaning.
mjr 70:9f58735a1732 2421 if (shiftButton.state)
mjr 66:2e3583fbd2f4 2422 {
mjr 70:9f58735a1732 2423 // assume there's no shifted meaning
mjr 70:9f58735a1732 2424 bool useShift = false;
mjr 66:2e3583fbd2f4 2425
mjr 70:9f58735a1732 2426 // If the button has a shifted meaning, use that. The
mjr 70:9f58735a1732 2427 // meaning might be a keyboard key or joystick button,
mjr 70:9f58735a1732 2428 // but it could also be as the Night Mode toggle.
mjr 70:9f58735a1732 2429 //
mjr 70:9f58735a1732 2430 // The condition to check if it's the Night Mode toggle
mjr 70:9f58735a1732 2431 // is a little complicated. First, the easy part: our
mjr 70:9f58735a1732 2432 // button index has to match the Night Mode button index.
mjr 70:9f58735a1732 2433 // Now the hard part: the Night Mode button flags have
mjr 70:9f58735a1732 2434 // to be set to 0x01 OFF and 0x02 ON: toggle mode (not
mjr 70:9f58735a1732 2435 // switch mode, 0x01), and shift mode, 0x02. So AND the
mjr 70:9f58735a1732 2436 // flags with 0x03 to get these two bits, and check that
mjr 70:9f58735a1732 2437 // the result is 0x02, meaning that only shift mode is on.
mjr 70:9f58735a1732 2438 if (bc->typ2 != BtnTypeNone)
mjr 70:9f58735a1732 2439 {
mjr 70:9f58735a1732 2440 // there's a shifted key assignment - use it
mjr 70:9f58735a1732 2441 typ = bc->typ2;
mjr 70:9f58735a1732 2442 val = bc->val2;
mjr 70:9f58735a1732 2443 useShift = true;
mjr 70:9f58735a1732 2444 }
mjr 70:9f58735a1732 2445 else if (cfg.nightMode.btn == i+1
mjr 70:9f58735a1732 2446 && (cfg.nightMode.flags & 0x03) == 0x02)
mjr 70:9f58735a1732 2447 {
mjr 70:9f58735a1732 2448 // shift+button = night mode toggle
mjr 70:9f58735a1732 2449 typ = BtnTypeNone;
mjr 70:9f58735a1732 2450 val = 0;
mjr 70:9f58735a1732 2451 useShift = true;
mjr 70:9f58735a1732 2452 }
mjr 70:9f58735a1732 2453
mjr 70:9f58735a1732 2454 // If there's a shifted meaning, advance the shift
mjr 70:9f58735a1732 2455 // button state from 1 to 2 if applicable. This signals
mjr 70:9f58735a1732 2456 // that we've "consumed" the shift button press as the
mjr 70:9f58735a1732 2457 // shift button, so it shouldn't generate its own key
mjr 70:9f58735a1732 2458 // code event when released.
mjr 70:9f58735a1732 2459 if (useShift && shiftButton.state == 1)
mjr 66:2e3583fbd2f4 2460 shiftButton.state = 2;
mjr 66:2e3583fbd2f4 2461 }
mjr 66:2e3583fbd2f4 2462
mjr 70:9f58735a1732 2463 // We've decided on the meaning of the button, so process
mjr 70:9f58735a1732 2464 // the keyboard or joystick event.
mjr 66:2e3583fbd2f4 2465 switch (typ)
mjr 53:9b2611964afc 2466 {
mjr 53:9b2611964afc 2467 case BtnTypeJoystick:
mjr 53:9b2611964afc 2468 // joystick button
mjr 53:9b2611964afc 2469 newjs |= (1 << (val - 1));
mjr 53:9b2611964afc 2470 break;
mjr 53:9b2611964afc 2471
mjr 53:9b2611964afc 2472 case BtnTypeKey:
mjr