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:
Tue Jun 14 20:24:34 2016 +0000
Revision:
63:5cd1a5f3a41b
Parent:
60:f38da020aa13
Child:
64:ef7ca92dff36
Changed LedWiz/extended protocol mode sensing from per-output to global

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