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