An I/O controller for virtual pinball machines: accelerometer nudge sensing, analog plunger input, button input encoding, LedWiz compatible output controls, and more.

Dependencies:   mbed FastIO FastPWM USBDevice

Fork of Pinscape_Controller by Mike R

/media/uploads/mjr/pinscape_no_background_small_L7Miwr6.jpg

This is Version 2 of the Pinscape Controller, an I/O controller for virtual pinball machines. (You can find the old version 1 software here.) Pinscape is software for the KL25Z that turns the board into a full-featured I/O controller for virtual pinball, with support for accelerometer-based nudging, a real plunger, button inputs, and feedback device control.

In case you haven't heard of the concept before, a "virtual pinball machine" is basically a video pinball simulator that's built into a real pinball machine body. A TV monitor goes in place of the pinball playfield, and a second TV goes in the backbox to serve as the "backglass" display. A third smaller monitor can serve as the "DMD" (the Dot Matrix Display used for scoring on newer machines), or you can even install a real pinball plasma DMD. A computer is hidden inside the cabinet, running pinball emulation software that displays a life-sized playfield on the main TV. The cabinet has all of the usual buttons, too, so it not only looks like the real thing, but plays like it too. That's a picture of my own machine to the right. On the outside, it's built exactly like a real arcade pinball machine, with the same overall dimensions and all of the standard pinball cabinet hardware.

A few small companies build and sell complete, finished virtual pinball machines, but I think it's more fun as a DIY project. If you have some basic wood-working skills and know your way around PCs, you can build one from scratch. The computer part is just an ordinary Windows PC, and all of the pinball emulation can be built out of free, open-source software. In that spirit, the Pinscape Controller is an open-source software/hardware project that offers a no-compromises, all-in-one control center for all of the unique input/output needs of a virtual pinball cabinet. If you've been thinking about building one of these, but you're not sure how to connect a plunger, flipper buttons, lights, nudge sensor, and whatever else you can think of, this project might be just what you're looking for.

You can find much more information about DIY Pin Cab building in general in the Virtual Cabinet Forum on vpforums.org. Also visit my Pinscape Resources page for more about this project and other virtual pinball projects I'm working on.

Downloads

  • Pinscape Release Builds: This page has download links for all of the Pinscape software. To get started, install and run the Pinscape Config Tool on your Windows computer. It will lead you through the steps for installing the Pinscape firmware on the KL25Z.
  • Config Tool Source Code. The complete C# source code for the config tool. You don't need this to run the tool, but it's available if you want to customize anything or see how it works inside.

Documentation

The new Version 2 Build Guide is now complete! This new version aims to be a complete guide to building a virtual pinball machine, including not only the Pinscape elements but all of the basics, from sourcing parts to building all of the hardware.

You can also refer to the original Hardware Build Guide (PDF), but that's out of date now, since it refers to the old version 1 software, which was rather different (especially when it comes to configuration).

System Requirements

The new config tool requires a fairly up-to-date Microsoft .NET installation. If you use Windows Update to keep your system current, you should be fine. A modern version of Internet Explorer (IE) is required, even if you don't use it as your main browser, because the config tool uses some system components that Microsoft packages into the IE install set. I test with IE11, so that's known to work. IE8 doesn't work. IE9 and 10 are unknown at this point.

The Windows requirements are only for the config tool. The firmware doesn't care about anything on the Windows side, so if you can make do without the config tool, you can use almost any Windows setup.

Main Features

Plunger: The Pinscape Controller started out as a "mechanical plunger" controller: a device for attaching a real pinball plunger to the video game software so that you could launch the ball the natural way. This is still, of course, a central feature of the project. The software supports several types of sensors: a high-resolution optical sensor (which works by essentially taking pictures of the plunger as it moves); a slide potentionmeter (which determines the position via the changing electrical resistance in the pot); a quadrature sensor (which counts bars printed on a special guide rail that it moves along); and an IR distance sensor (which determines the position by sending pulses of light at the plunger and measuring the round-trip travel time). The Build Guide explains how to set up each type of sensor.

Nudging: The KL25Z (the little microcontroller that the software runs on) has a built-in accelerometer. The Pinscape software uses it to sense when you nudge the cabinet, and feeds the acceleration data to the pinball software on the PC. This turns physical nudges into virtual English on the ball. The accelerometer is quite sensitive and accurate, so we can measure the difference between little bumps and hard shoves, and everything in between. The result is natural and immersive.

Buttons: You can wire real pinball buttons to the KL25Z, and the software will translate the buttons into PC input. You have the option to map each button to a keyboard key or joystick button. You can wire up your flipper buttons, Magna Save buttons, Start button, coin slots, operator buttons, and whatever else you need.

Feedback devices: You can also attach "feedback devices" to the KL25Z. Feedback devices are things that create tactile, sound, and lighting effects in sync with the game action. The most popular PC pinball emulators know how to address a wide variety of these devices, and know how to match them to on-screen action in each virtual table. You just need an I/O controller that translates commands from the PC into electrical signals that turn the devices on and off. The Pinscape Controller can do that for you.

Expansion Boards

There are two main ways to run the Pinscape Controller: standalone, or using the "expansion boards".

In the basic standalone setup, you just need the KL25Z, plus whatever buttons, sensors, and feedback devices you want to attach to it. This mode lets you take advantage of everything the software can do, but for some features, you'll have to build some ad hoc external circuitry to interface external devices with the KL25Z. The Build Guide has detailed plans for exactly what you need to build.

The other option is the Pinscape Expansion Boards. The expansion boards are a companion project, which is also totally free and open-source, that provides Printed Circuit Board (PCB) layouts that are designed specifically to work with the Pinscape software. The PCB designs are in the widely used EAGLE format, which many PCB manufacturers can turn directly into physical boards for you. The expansion boards organize all of the external connections more neatly than on the standalone KL25Z, and they add all of the interface circuitry needed for all of the advanced software functions. The big thing they bring to the table is lots of high-power outputs. The boards provide a modular system that lets you add boards to add more outputs. If you opt for the basic core setup, you'll have enough outputs for all of the toys in a really well-equipped cabinet. If your ambitions go beyond merely well-equipped and run to the ridiculously extravagant, just add an extra board or two. The modular design also means that you can add to the system over time.

Expansion Board project page

Update notes

If you have a Pinscape V1 setup already installed, you should be able to switch to the new version pretty seamlessly. There are just a couple of things to be aware of.

First, the "configuration" procedure is completely different in the new version. Way better and way easier, but it's not what you're used to from V1. In V1, you had to edit the project source code and compile your own custom version of the program. No more! With V2, you simply install the standard, pre-compiled .bin file, and select options using the Pinscape Config Tool on Windows.

Second, if you're using the TSL1410R optical sensor for your plunger, there's a chance you'll need to boost your light source's brightness a little bit. The "shutter speed" is faster in this version, which means that it doesn't spend as much time collecting light per frame as before. The software actually does "auto exposure" adaptation on every frame, so the increased shutter speed really shouldn't bother it, but it does require a certain minimum level of contrast, which requires a certain minimal level of lighting. Check the plunger viewer in the setup tool if you have any problems; if the image looks totally dark, try increasing the light level to see if that helps.

New Features

V2 has numerous new features. Here are some of the highlights...

Dynamic configuration: as explained above, configuration is now handled through the Config Tool on Windows. It's no longer necessary to edit the source code or compile your own modified binary.

Improved plunger sensing: the software now reads the TSL1410R optical sensor about 15x faster than it did before. This allows reading the sensor at full resolution (400dpi), about 400 times per second. The faster frame rate makes a big difference in how accurately we can read the plunger position during the fast motion of a release, which allows for more precise position sensing and faster response. The differences aren't dramatic, since the sensing was already pretty good even with the slower V1 scan rate, but you might notice a little better precision in tricky skill shots.

Keyboard keys: button inputs can now be mapped to keyboard keys. The joystick button option is still available as well, of course. Keyboard keys have the advantage of being closer to universal for PC pinball software: some pinball software can be set up to take joystick input, but nearly all PC pinball emulators can take keyboard input, and nearly all of them use the same key mappings.

Local shift button: one physical button can be designed as the local shift button. This works like a Shift button on a keyboard, but with cabinet buttons. It allows each physical button on the cabinet to have two PC keys assigned, one normal and one shifted. Hold down the local shift button, then press another key, and the other key's shifted key mapping is sent to the PC. The shift button can have a regular key mapping of its own as well, so it can do double duty. The shift feature lets you access more functions without cluttering your cabinet with extra buttons. It's especially nice for less frequently used functions like adjusting the volume or activating night mode.

Night mode: the output controller has a new "night mode" option, which lets you turn off all of your noisy devices with a single button, switch, or PC command. You can designate individual ports as noisy or not. Night mode only disables the noisemakers, so you still get the benefit of your flashers, button lights, and other quiet devices. This lets you play late into the night without disturbing your housemates or neighbors.

Gamma correction: you can designate individual output ports for gamma correction. This adjusts the intensity level of an output to make it match the way the human eye perceives brightness, so that fades and color mixes look more natural in lighting devices. You can apply this to individual ports, so that it only affects ports that actually have lights of some kind attached.

IR Remote Control: the controller software can transmit and/or receive IR remote control commands if you attach appropriate parts (an IR LED to send, an IR sensor chip to receive). This can be used to turn on your TV(s) when the system powers on, if they don't turn on automatically, and for any other functions you can think of requiring IR send/receive capabilities. You can assign IR commands to cabinet buttons, so that pressing a button on your cabinet sends a remote control command from the attached IR LED, and you can have the controller generate virtual key presses on your PC in response to received IR commands. If you have the IR sensor attached, the system can use it to learn commands from your existing remotes.

Yet more USB fixes: I've been gradually finding and fixing USB bugs in the mbed library for months now. This version has all of the fixes of the last couple of releases, of course, plus some new ones. It also has a new "last resort" feature, since there always seems to be "just one more" USB bug. The last resort is that you can tell the device to automatically reboot itself if it loses the USB connection and can't restore it within a given time limit.

More Downloads

  • Custom VP builds: I created modified versions of Visual Pinball 9.9 and Physmod5 that you might want to use in combination with this controller. The modified versions have special handling for plunger calibration specific to the Pinscape Controller, as well as some enhancements to the nudge physics. If you're not using the plunger, you might still want it for the nudge improvements. The modified version also works with any other input controller, so you can get the enhanced nudging effects even if you're using a different plunger/nudge kit. The big change in the modified versions is a "filter" for accelerometer input that's designed to make the response to cabinet nudges more realistic. It also makes the response more subdued than in the standard VP, so it's not to everyone's taste. The downloads include both the updated executables and the source code changes, in case you want to merge the changes into your own custom version(s).

    Note! These features are now standard in the official VP releases, so you don't need my custom builds if you're using 9.9.1 or later and/or VP 10. I don't think there's any reason to use my versions instead of the latest official ones, and in fact I'd encourage you to use the official releases since they're more up to date, but I'm leaving my builds available just in case. In the official versions, look for the checkbox "Enable Nudge Filter" in the Keys preferences dialog. My custom versions don't include that checkbox; they just enable the filter unconditionally.
  • Output circuit shopping list: This is a saved shopping cart at mouser.com with the parts needed to build one copy of the high-power output circuit for the LedWiz emulator feature, for use with the standalone KL25Z (that is, without the expansion boards). The quantities in the cart are for one output channel, so if you want N outputs, simply multiply the quantities by the N, with one exception: you only need one ULN2803 transistor array chip for each eight output circuits. If you're using the expansion boards, you won't need any of this, since the boards provide their own high-power outputs.
  • Cary Owens' optical sensor housing: A 3D-printable design for a housing/mounting bracket for the optical plunger sensor, designed by Cary Owens. This makes it easy to mount the sensor.
  • Lemming77's potentiometer mounting bracket and shooter rod connecter: Sketchup designs for 3D-printable parts for mounting a slide potentiometer as the plunger sensor. These were designed for a particular slide potentiometer that used to be available from an Aliexpress.com seller but is no longer listed. You can probably use this design as a starting point for other similar devices; just check the dimensions before committing the design to plastic.

Copyright and License

The Pinscape firmware is copyright 2014, 2021 by Michael J Roberts. It's released under an MIT open-source license. See License.

Warning to VirtuaPin Kit Owners

This software isn't designed as a replacement for the VirtuaPin plunger kit's firmware. If you bought the VirtuaPin kit, I recommend that you don't install this software. The VirtuaPin kit uses the same KL25Z microcontroller that Pinscape uses, but the rest of its hardware is different and incompatible. In particular, the Pinscape firmware doesn't include support for the IR proximity sensor used in the VirtuaPin plunger kit, so you won't be able to use your plunger device with the Pinscape firmware. In addition, the VirtuaPin setup uses a different set of GPIO pins for the button inputs from the Pinscape defaults, so if you do install the Pinscape firmware, you'll have to go into the Config Tool and reassign all of the buttons to match the VirtuaPin wiring.

Committer:
mjr
Date:
Fri Mar 17 22:02:08 2017 +0000
Revision:
77:0b96f6867312
Parent:
76:7f5912b6340e
Child:
78:1e00b3fa11af
New memory pool management; keeping old ones as #ifdefs for now for reference.

Who changed what in which revision?

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