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:
Sat Jan 21 19:48:30 2017 +0000
Revision:
73:4e8ce0b18915
Parent:
72:884207c0aab0
Child:
74:822a92bc11d2
Add protocol commands for TV ON and button testers; add free memory status reporting; improve button scan interrupt speed; reduce button memory footprint slightly; further improve TSL1410R "scan mode 2" speed

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