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

In case you haven't heard of the idea 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 show the backglass artwork. Some cabs also include a third monitor to simulate the DMD (Dot Matrix Display) used for scoring on 1990s machines, or even an original plasma DMD. A computer (usually a Windows PC) 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 trim hardware.

It's possible to buy a pre-built virtual pinball machine, but it also makes a great 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 potentiometer (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 KL25Z can only run one firmware program at a time, so if you install the Pinscape firmware on your KL25Z, it will replace and erase your existing VirtuaPin proprietary firmware. If you do this, the only way to restore your VirtuaPin firmware is to physically ship the KL25Z back to VirtuaPin and ask them to re-flash it. They don't allow you to do this at home, and they don't even allow you to back up your firmware, since they want to protect their proprietary software from copying. For all of these reasons, if you want to run the Pinscape software, I strongly recommend that you buy a "blank" retail KL25Z to use with Pinscape. They only cost about $15 and are available at several online retailers, including Amazon, Mouser, and eBay. The blank retail boards don't come with any proprietary firmware pre-installed, so installing Pinscape won't delete anything that you paid extra for.

With those warnings in mind, if you're absolutely sure that you don't mind permanently erasing your VirtuaPin firmware, it is at least possible to use Pinscape as a replacement for the VirtuaPin firmware. Pinscape uses the same button wiring conventions as the VirtuaPin setup, so you can keep your buttons (although you'll have to update the GPIO pin mappings in the Config Tool to match your physical wiring). As of the June, 2021 firmware, the Vishay VCNL4010 plunger sensor that comes with the VirtuaPin v3 plunger kit is supported, so you can also keep your plunger, if you have that chip. (You should check to be sure that's the sensor chip you have before committing to this route, if keeping the plunger sensor is important to you. The older VirtuaPin plunger kits came with different IR sensors that the Pinscape software doesn't handle.)

Committer:
mjr
Date:
Mon Feb 03 21:27:55 2020 +0000
Revision:
106:e9e3b46132c1
Parent:
101:755f44622abc
Child:
107:8f3c7aeae7e0
Check diagnostic LEDs against all configured pins (not just output ports)

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 99:8139b0c274f4 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 5:a70c0bce770d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 5:a70c0bce770d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 5:a70c0bce770d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 5:a70c0bce770d 17 */
mjr 5:a70c0bce770d 18
mjr 5:a70c0bce770d 19 //
mjr 35:e959ffba78fd 20 // The Pinscape Controller
mjr 35:e959ffba78fd 21 // A comprehensive input/output controller for virtual pinball machines
mjr 5:a70c0bce770d 22 //
mjr 48:058ace2aed1d 23 // This project implements an I/O controller for virtual pinball cabinets. The
mjr 48:058ace2aed1d 24 // controller's function is to connect Visual Pinball (and other Windows pinball
mjr 48:058ace2aed1d 25 // emulators) with physical devices in the cabinet: buttons, sensors, and
mjr 48:058ace2aed1d 26 // feedback devices that create visual or mechanical effects during play.
mjr 38:091e511ce8a0 27 //
mjr 48:058ace2aed1d 28 // The controller can perform several different functions, which can be used
mjr 38:091e511ce8a0 29 // individually or in any combination:
mjr 5:a70c0bce770d 30 //
mjr 38:091e511ce8a0 31 // - Nudge sensing. This uses the KL25Z's on-board accelerometer to sense the
mjr 38:091e511ce8a0 32 // motion of the cabinet when you nudge it. Visual Pinball and other pinball
mjr 38:091e511ce8a0 33 // emulators on the PC have native handling for this type of input, so that
mjr 38:091e511ce8a0 34 // physical nudges on the cabinet turn into simulated effects on the virtual
mjr 38:091e511ce8a0 35 // ball. The KL25Z measures accelerations as analog readings and is quite
mjr 38:091e511ce8a0 36 // sensitive, so the effect of a nudge on the simulation is proportional
mjr 38:091e511ce8a0 37 // to the strength of the nudge. Accelerations are reported to the PC via a
mjr 38:091e511ce8a0 38 // simulated joystick (using the X and Y axes); you just have to set some
mjr 38:091e511ce8a0 39 // preferences in your pinball software to tell it that an accelerometer
mjr 38:091e511ce8a0 40 // is attached.
mjr 5:a70c0bce770d 41 //
mjr 74:822a92bc11d2 42 // - Plunger position sensing, with multiple sensor options. To use this feature,
mjr 35:e959ffba78fd 43 // you need to choose a sensor and set it up, connect the sensor electrically to
mjr 35:e959ffba78fd 44 // the KL25Z, and configure the Pinscape software on the KL25Z to let it know how
mjr 35:e959ffba78fd 45 // the sensor is hooked up. The Pinscape software monitors the sensor and sends
mjr 35:e959ffba78fd 46 // readings to Visual Pinball via the joystick Z axis. VP and other PC software
mjr 38:091e511ce8a0 47 // have native support for this type of input; as with the nudge setup, you just
mjr 38:091e511ce8a0 48 // have to set some options in VP to activate the plunger.
mjr 17:ab3cec0c8bf4 49 //
mjr 87:8d35c74403af 50 // We support several sensor types:
mjr 35:e959ffba78fd 51 //
mjr 87:8d35c74403af 52 // - AEDR-8300-1K2 optical encoders. These are quadrature encoders with
mjr 87:8d35c74403af 53 // reflective optical sensing and built-in lighting and optics. The sensor
mjr 87:8d35c74403af 54 // is attached to the plunger so that it moves with the plunger, and slides
mjr 87:8d35c74403af 55 // along a guide rail with a reflective pattern of regularly spaces bars
mjr 87:8d35c74403af 56 // for the encoder to read. We read the plunger position by counting the
mjr 87:8d35c74403af 57 // bars the sensor passes as it moves across the rail. This is the newest
mjr 87:8d35c74403af 58 // option, and it's my current favorite because it's highly accurate,
mjr 87:8d35c74403af 59 // precise, and fast, plus it's relatively inexpensive.
mjr 87:8d35c74403af 60 //
mjr 87:8d35c74403af 61 // - Slide potentiometers. There are slide potentioneters available with a
mjr 87:8d35c74403af 62 // long enough travel distance (at least 85mm) to cover the plunger travel.
mjr 87:8d35c74403af 63 // Attach the plunger to the potentiometer knob so that the moving the
mjr 87:8d35c74403af 64 // plunger moves the pot knob. We sense the position by simply reading
mjr 87:8d35c74403af 65 // the analog voltage on the pot brush. A pot with a "linear taper" (that
mjr 87:8d35c74403af 66 // is, the resistance varies linearly with the position) is required.
mjr 87:8d35c74403af 67 // This option is cheap, easy to set up, and works well.
mjr 5:a70c0bce770d 68 //
mjr 87:8d35c74403af 69 // - VL6108X time-of-flight distance sensor. This is an optical distance
mjr 87:8d35c74403af 70 // sensor that measures the distance to a nearby object (within about 10cm)
mjr 87:8d35c74403af 71 // by measuring the travel time for reflected pulses of light. It's fairly
mjr 87:8d35c74403af 72 // cheap and easy to set up, but I don't recommend it because it has very
mjr 87:8d35c74403af 73 // low precision.
mjr 6:cc35eb643e8f 74 //
mjr 87:8d35c74403af 75 // - TSL1410R/TSL1412R linear array optical sensors. These are large optical
mjr 87:8d35c74403af 76 // sensors with the pixels arranged in a single row. The pixel arrays are
mjr 87:8d35c74403af 77 // large enough on these to cover the travel distance of the plunger, so we
mjr 87:8d35c74403af 78 // can set up the sensor near the plunger in such a way that the plunger
mjr 87:8d35c74403af 79 // casts a shadow on the sensor. We detect the plunger position by finding
mjr 87:8d35c74403af 80 // the edge of the sahdow in the image. The optics for this setup are very
mjr 87:8d35c74403af 81 // simple since we don't need any lenses. This was the first sensor we
mjr 87:8d35c74403af 82 // supported, and works very well, but unfortunately the sensor is difficult
mjr 87:8d35c74403af 83 // to find now since it's been discontinued by the manufacturer.
mjr 87:8d35c74403af 84 //
mjr 87:8d35c74403af 85 // The v2 Build Guide has details on how to build and configure all of the
mjr 87:8d35c74403af 86 // sensor options.
mjr 87:8d35c74403af 87 //
mjr 87:8d35c74403af 88 // Visual Pinball has built-in support for plunger devices like this one, but
mjr 87:8d35c74403af 89 // some older VP tables (particularly for VP 9) can't use it without some
mjr 87:8d35c74403af 90 // modifications to their scripting. The Build Guide has advice on how to
mjr 87:8d35c74403af 91 // fix up VP tables to add plunger support when necessary.
mjr 5:a70c0bce770d 92 //
mjr 77:0b96f6867312 93 // - Button input wiring. You can assign GPIO ports as inputs for physical
mjr 77:0b96f6867312 94 // pinball-style buttons, such as flipper buttons, a Start button, coin
mjr 77:0b96f6867312 95 // chute switches, tilt bobs, and service panel buttons. You can configure
mjr 77:0b96f6867312 96 // each button input to report a keyboard key or joystick button press to
mjr 77:0b96f6867312 97 // the PC when the physical button is pushed.
mjr 13:72dda449c3c0 98 //
mjr 53:9b2611964afc 99 // - LedWiz emulation. The KL25Z can pretend to be an LedWiz device. This lets
mjr 53:9b2611964afc 100 // you connect feedback devices (lights, solenoids, motors) to GPIO ports on the
mjr 53:9b2611964afc 101 // KL25Z, and lets PC software (such as Visual Pinball) control them during game
mjr 53:9b2611964afc 102 // play to create a more immersive playing experience. The Pinscape software
mjr 53:9b2611964afc 103 // presents itself to the host as an LedWiz device and accepts the full LedWiz
mjr 53:9b2611964afc 104 // command set, so software on the PC designed for real LedWiz'es can control
mjr 53:9b2611964afc 105 // attached devices without any modifications.
mjr 5:a70c0bce770d 106 //
mjr 53:9b2611964afc 107 // Even though the software provides a very thorough LedWiz emulation, the KL25Z
mjr 53:9b2611964afc 108 // GPIO hardware design imposes some serious limitations. The big one is that
mjr 53:9b2611964afc 109 // the KL25Z only has 10 PWM channels, meaning that only 10 ports can have
mjr 53:9b2611964afc 110 // varying-intensity outputs (e.g., for controlling the brightness level of an
mjr 53:9b2611964afc 111 // LED or the speed or a motor). You can control more than 10 output ports, but
mjr 53:9b2611964afc 112 // only 10 can have PWM control; the rest are simple "digital" ports that can only
mjr 53:9b2611964afc 113 // be switched fully on or fully off. The second limitation is that the KL25Z
mjr 53:9b2611964afc 114 // just doesn't have that many GPIO ports overall. There are enough to populate
mjr 53:9b2611964afc 115 // all 32 button inputs OR all 32 LedWiz outputs, but not both. The default is
mjr 53:9b2611964afc 116 // to assign 24 buttons and 22 LedWiz ports; you can change this balance to trade
mjr 53:9b2611964afc 117 // off more outputs for fewer inputs, or vice versa. The third limitation is that
mjr 53:9b2611964afc 118 // the KL25Z GPIO pins have *very* tiny amperage limits - just 4mA, which isn't
mjr 53:9b2611964afc 119 // even enough to control a small LED. So in order to connect any kind of feedback
mjr 53:9b2611964afc 120 // device to an output, you *must* build some external circuitry to boost the
mjr 53:9b2611964afc 121 // current handing. The Build Guide has a reference circuit design for this
mjr 53:9b2611964afc 122 // purpose that's simple and inexpensive to build.
mjr 6:cc35eb643e8f 123 //
mjr 87:8d35c74403af 124 // - Enhanced LedWiz emulation with TLC5940 and/or TLC59116 PWM controller chips.
mjr 87:8d35c74403af 125 // You can attach external PWM chips for controlling device outputs, instead of
mjr 87:8d35c74403af 126 // using (or in addition to) the on-board GPIO ports as described above. The
mjr 87:8d35c74403af 127 // software can control a set of daisy-chained TLC5940 or TLC59116 chips. Each
mjr 87:8d35c74403af 128 // chip provides 16 PWM outputs, so you just need two of them to get the full
mjr 87:8d35c74403af 129 // complement of 32 output ports of a real LedWiz. You can hook up even more,
mjr 87:8d35c74403af 130 // though. Four chips gives you 64 ports, which should be plenty for nearly any
mjr 87:8d35c74403af 131 // virtual pinball project.
mjr 53:9b2611964afc 132 //
mjr 53:9b2611964afc 133 // The Pinscape Expansion Board project (which appeared in early 2016) provides
mjr 53:9b2611964afc 134 // a reference hardware design, with EAGLE circuit board layouts, that takes full
mjr 53:9b2611964afc 135 // advantage of the TLC5940 capability. It lets you create a customized set of
mjr 53:9b2611964afc 136 // outputs with full PWM control and power handling for high-current devices
mjr 87:8d35c74403af 137 // built in to the boards.
mjr 87:8d35c74403af 138 //
mjr 87:8d35c74403af 139 // To accommodate the larger supply of ports possible with the external chips,
mjr 87:8d35c74403af 140 // the controller software provides a custom, extended version of the LedWiz
mjr 87:8d35c74403af 141 // protocol that can handle up to 128 ports. Legacy PC software designed only
mjr 87:8d35c74403af 142 // for the original LedWiz obviously can't use the extended protocol, and thus
mjr 87:8d35c74403af 143 // can't take advantage of its extra capabilities, but the latest version of
mjr 87:8d35c74403af 144 // DOF (DirectOutput Framework) *does* know the new language and can take full
mjr 87:8d35c74403af 145 // advantage. Older software will still work, though - the new extensions are
mjr 87:8d35c74403af 146 // all backwards compatible, so old software that only knows about the original
mjr 87:8d35c74403af 147 // LedWiz protocol will still work, with the limitation that it can only access
mjr 87:8d35c74403af 148 // the first 32 ports. In addition, we provide a replacement LEDWIZ.DLL that
mjr 87:8d35c74403af 149 // creates virtual LedWiz units representing additional ports beyond the first
mjr 87:8d35c74403af 150 // 32. This allows legacy LedWiz client software to address all ports by
mjr 87:8d35c74403af 151 // making them think that you have several physical LedWiz units installed.
mjr 26:cb71c4af2912 152 //
mjr 38:091e511ce8a0 153 // - Night Mode control for output devices. You can connect a switch or button
mjr 38:091e511ce8a0 154 // to the controller to activate "Night Mode", which disables feedback devices
mjr 38:091e511ce8a0 155 // that you designate as noisy. You can designate outputs individually as being
mjr 38:091e511ce8a0 156 // included in this set or not. This is useful if you want to play a game on
mjr 38:091e511ce8a0 157 // your cabinet late at night without waking the kids and annoying the neighbors.
mjr 38:091e511ce8a0 158 //
mjr 38:091e511ce8a0 159 // - TV ON switch. The controller can pulse a relay to turn on your TVs after
mjr 38:091e511ce8a0 160 // power to the cabinet comes on, with a configurable delay timer. This feature
mjr 38:091e511ce8a0 161 // is for TVs that don't turn themselves on automatically when first plugged in.
mjr 38:091e511ce8a0 162 // To use this feature, you have to build some external circuitry to allow the
mjr 77:0b96f6867312 163 // software to sense the power supply status. The Build Guide has details
mjr 77:0b96f6867312 164 // on the necessary circuitry. You can use this to switch your TV on via a
mjr 77:0b96f6867312 165 // hardwired connection to the TV's "on" button, which requires taking the
mjr 77:0b96f6867312 166 // TV apart to gain access to its internal wiring, or optionally via the IR
mjr 77:0b96f6867312 167 // remote control transmitter feature below.
mjr 77:0b96f6867312 168 //
mjr 77:0b96f6867312 169 // - Infrared (IR) remote control receiver and transmitter. You can attach an
mjr 77:0b96f6867312 170 // IR LED and/or an IR sensor (we recommend the TSOP384xx series) to make the
mjr 77:0b96f6867312 171 // KL25Z capable of sending and/or receiving IR remote control signals. This
mjr 77:0b96f6867312 172 // can be used with the TV ON feature above to turn your TV(s) on when the
mjr 77:0b96f6867312 173 // system power comes on by sending the "on" command to them via IR, as though
mjr 77:0b96f6867312 174 // you pressed the "on" button on the remote control. The sensor lets the
mjr 77:0b96f6867312 175 // Pinscape software learn the IR codes from your existing remotes, in the
mjr 77:0b96f6867312 176 // same manner as a handheld universal remote control, and the IR LED lets
mjr 77:0b96f6867312 177 // it transmit learned codes. The sensor can also be used to receive codes
mjr 77:0b96f6867312 178 // during normal operation and turn them into PC keystrokes; this lets you
mjr 77:0b96f6867312 179 // access extra commands on the PC without adding more buttons to your
mjr 77:0b96f6867312 180 // cabinet. The IR LED can also be used to transmit other codes when you
mjr 77:0b96f6867312 181 // press selected cabinet buttons, allowing you to assign cabinet buttons
mjr 77:0b96f6867312 182 // to send IR commands to your cabinet TV or other devices.
mjr 38:091e511ce8a0 183 //
mjr 35:e959ffba78fd 184 //
mjr 35:e959ffba78fd 185 //
mjr 33:d832bcab089e 186 // STATUS LIGHTS: The on-board LED on the KL25Z flashes to indicate the current
mjr 33:d832bcab089e 187 // device status. The flash patterns are:
mjr 6:cc35eb643e8f 188 //
mjr 48:058ace2aed1d 189 // short yellow flash = waiting to connect
mjr 6:cc35eb643e8f 190 //
mjr 48:058ace2aed1d 191 // short red flash = the connection is suspended (the host is in sleep
mjr 48:058ace2aed1d 192 // or suspend mode, the USB cable is unplugged after a connection
mjr 48:058ace2aed1d 193 // has been established)
mjr 48:058ace2aed1d 194 //
mjr 48:058ace2aed1d 195 // two short red flashes = connection lost (the device should immediately
mjr 48:058ace2aed1d 196 // go back to short-yellow "waiting to reconnect" mode when a connection
mjr 48:058ace2aed1d 197 // is lost, so this display shouldn't normally appear)
mjr 6:cc35eb643e8f 198 //
mjr 38:091e511ce8a0 199 // long red/yellow = USB connection problem. The device still has a USB
mjr 48:058ace2aed1d 200 // connection to the host (or so it appears to the device), but data
mjr 48:058ace2aed1d 201 // transmissions are failing.
mjr 38:091e511ce8a0 202 //
mjr 73:4e8ce0b18915 203 // medium blue flash = TV ON delay timer running. This means that the
mjr 73:4e8ce0b18915 204 // power to the secondary PSU has just been turned on, and the TV ON
mjr 73:4e8ce0b18915 205 // timer is waiting for the configured delay time before pulsing the
mjr 73:4e8ce0b18915 206 // TV power button relay. This is only shown if the TV ON feature is
mjr 73:4e8ce0b18915 207 // enabled.
mjr 73:4e8ce0b18915 208 //
mjr 6:cc35eb643e8f 209 // long yellow/green = everything's working, but the plunger hasn't
mjr 38:091e511ce8a0 210 // been calibrated. Follow the calibration procedure described in
mjr 38:091e511ce8a0 211 // the project documentation. This flash mode won't appear if there's
mjr 38:091e511ce8a0 212 // no plunger sensor configured.
mjr 6:cc35eb643e8f 213 //
mjr 38:091e511ce8a0 214 // alternating blue/green = everything's working normally, and plunger
mjr 38:091e511ce8a0 215 // calibration has been completed (or there's no plunger attached)
mjr 10:976666ffa4ef 216 //
mjr 48:058ace2aed1d 217 // fast red/purple = out of memory. The controller halts and displays
mjr 48:058ace2aed1d 218 // this diagnostic code until you manually reset it. If this happens,
mjr 48:058ace2aed1d 219 // it's probably because the configuration is too complex, in which
mjr 48:058ace2aed1d 220 // case the same error will occur after the reset. If it's stuck
mjr 48:058ace2aed1d 221 // in this cycle, you'll have to restore the default configuration
mjr 48:058ace2aed1d 222 // by re-installing the controller software (the Pinscape .bin file).
mjr 10:976666ffa4ef 223 //
mjr 48:058ace2aed1d 224 //
mjr 48:058ace2aed1d 225 // USB PROTOCOL: Most of our USB messaging is through standard USB HID
mjr 48:058ace2aed1d 226 // classes (joystick, keyboard). We also accept control messages on our
mjr 48:058ace2aed1d 227 // primary HID interface "OUT endpoint" using a custom protocol that's
mjr 48:058ace2aed1d 228 // not defined in any USB standards (we do have to provide a USB HID
mjr 48:058ace2aed1d 229 // Report Descriptor for it, but this just describes the protocol as
mjr 48:058ace2aed1d 230 // opaque vendor-defined bytes). The control protocol incorporates the
mjr 48:058ace2aed1d 231 // LedWiz protocol as a subset, and adds our own private extensions.
mjr 48:058ace2aed1d 232 // For full details, see USBProtocol.h.
mjr 33:d832bcab089e 233
mjr 33:d832bcab089e 234
mjr 0:5acbbe3f4cf4 235 #include "mbed.h"
mjr 6:cc35eb643e8f 236 #include "math.h"
mjr 74:822a92bc11d2 237 #include "diags.h"
mjr 48:058ace2aed1d 238 #include "pinscape.h"
mjr 79:682ae3171a08 239 #include "NewMalloc.h"
mjr 0:5acbbe3f4cf4 240 #include "USBJoystick.h"
mjr 0:5acbbe3f4cf4 241 #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 242 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 243 #include "crc32.h"
mjr 26:cb71c4af2912 244 #include "TLC5940.h"
mjr 87:8d35c74403af 245 #include "TLC59116.h"
mjr 34:6b981a2afab7 246 #include "74HC595.h"
mjr 35:e959ffba78fd 247 #include "nvm.h"
mjr 48:058ace2aed1d 248 #include "TinyDigitalIn.h"
mjr 77:0b96f6867312 249 #include "IRReceiver.h"
mjr 77:0b96f6867312 250 #include "IRTransmitter.h"
mjr 77:0b96f6867312 251 #include "NewPwm.h"
mjr 74:822a92bc11d2 252
mjr 82:4f6209cb5c33 253 // plunger sensors
mjr 82:4f6209cb5c33 254 #include "plunger.h"
mjr 82:4f6209cb5c33 255 #include "edgeSensor.h"
mjr 82:4f6209cb5c33 256 #include "potSensor.h"
mjr 82:4f6209cb5c33 257 #include "quadSensor.h"
mjr 82:4f6209cb5c33 258 #include "nullSensor.h"
mjr 82:4f6209cb5c33 259 #include "barCodeSensor.h"
mjr 82:4f6209cb5c33 260 #include "distanceSensor.h"
mjr 87:8d35c74403af 261 #include "tsl14xxSensor.h"
mjr 100:1ff35c07217c 262 #include "rotarySensor.h"
mjr 100:1ff35c07217c 263 #include "tcd1103Sensor.h"
mjr 82:4f6209cb5c33 264
mjr 2:c174f9ee414a 265
mjr 21:5048e16cc9ef 266 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 267 #include "config.h"
mjr 17:ab3cec0c8bf4 268
mjr 53:9b2611964afc 269
mjr 53:9b2611964afc 270 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 271 //
mjr 53:9b2611964afc 272 // OpenSDA module identifier. This is for the benefit of the Windows
mjr 53:9b2611964afc 273 // configuration tool. When the config tool installs a .bin file onto
mjr 53:9b2611964afc 274 // the KL25Z, it will first find the sentinel string within the .bin file,
mjr 53:9b2611964afc 275 // and patch the "\0" bytes that follow the sentinel string with the
mjr 53:9b2611964afc 276 // OpenSDA module ID data. This allows us to report the OpenSDA
mjr 53:9b2611964afc 277 // identifiers back to the host system via USB, which in turn allows the
mjr 53:9b2611964afc 278 // config tool to figure out which OpenSDA MSD (mass storage device - a
mjr 53:9b2611964afc 279 // virtual disk drive) correlates to which Pinscape controller USB
mjr 53:9b2611964afc 280 // interface.
mjr 53:9b2611964afc 281 //
mjr 53:9b2611964afc 282 // This is only important if multiple Pinscape devices are attached to
mjr 53:9b2611964afc 283 // the same host. There doesn't seem to be any other way to figure out
mjr 53:9b2611964afc 284 // which OpenSDA MSD corresponds to which KL25Z USB interface; the OpenSDA
mjr 53:9b2611964afc 285 // MSD doesn't report the KL25Z CPU ID anywhere, and the KL25Z doesn't
mjr 53:9b2611964afc 286 // have any way to learn about the OpenSDA module it's connected to. The
mjr 53:9b2611964afc 287 // only way to pass this information to the KL25Z side that I can come up
mjr 53:9b2611964afc 288 // with is to have the Windows host embed it in the .bin file before
mjr 53:9b2611964afc 289 // downloading it to the OpenSDA MSD.
mjr 53:9b2611964afc 290 //
mjr 53:9b2611964afc 291 // We initialize the const data buffer (the part after the sentinel string)
mjr 53:9b2611964afc 292 // with all "\0" bytes, so that's what will be in the executable image that
mjr 53:9b2611964afc 293 // comes out of the mbed compiler. If you manually install the resulting
mjr 53:9b2611964afc 294 // .bin file onto the KL25Z (via the Windows desktop, say), the "\0" bytes
mjr 53:9b2611964afc 295 // will stay this way and read as all 0's at run-time. Since a real TUID
mjr 53:9b2611964afc 296 // would never be all 0's, that tells us that we were never patched and
mjr 53:9b2611964afc 297 // thus don't have any information on the OpenSDA module.
mjr 53:9b2611964afc 298 //
mjr 53:9b2611964afc 299 const char *getOpenSDAID()
mjr 53:9b2611964afc 300 {
mjr 53:9b2611964afc 301 #define OPENSDA_PREFIX "///Pinscape.OpenSDA.TUID///"
mjr 53:9b2611964afc 302 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 303 const size_t OpenSDA_prefix_length = sizeof(OPENSDA_PREFIX) - 1;
mjr 53:9b2611964afc 304
mjr 53:9b2611964afc 305 return OpenSDA + OpenSDA_prefix_length;
mjr 53:9b2611964afc 306 }
mjr 53:9b2611964afc 307
mjr 53:9b2611964afc 308 // --------------------------------------------------------------------------
mjr 53:9b2611964afc 309 //
mjr 53:9b2611964afc 310 // Build ID. We use the date and time of compiling the program as a build
mjr 53:9b2611964afc 311 // identifier. It would be a little nicer to use a simple serial number
mjr 53:9b2611964afc 312 // instead, but the mbed platform doesn't have a way to automate that. The
mjr 53:9b2611964afc 313 // timestamp is a pretty good proxy for a serial number in that it will
mjr 53:9b2611964afc 314 // naturally increase on each new build, which is the primary property we
mjr 53:9b2611964afc 315 // want from this.
mjr 53:9b2611964afc 316 //
mjr 53:9b2611964afc 317 // As with the embedded OpenSDA ID, we store the build timestamp with a
mjr 53:9b2611964afc 318 // sentinel string prefix, to allow automated tools to find the static data
mjr 53:9b2611964afc 319 // in the .bin file by searching for the sentinel string. In contrast to
mjr 53:9b2611964afc 320 // the OpenSDA ID, the value we store here is for tools to extract rather
mjr 53:9b2611964afc 321 // than store, since we automatically populate it via the preprocessor
mjr 53:9b2611964afc 322 // macros.
mjr 53:9b2611964afc 323 //
mjr 53:9b2611964afc 324 const char *getBuildID()
mjr 53:9b2611964afc 325 {
mjr 53:9b2611964afc 326 #define BUILDID_PREFIX "///Pinscape.Build.ID///"
mjr 53:9b2611964afc 327 static const char BuildID[] = BUILDID_PREFIX __DATE__ " " __TIME__ "///";
mjr 53:9b2611964afc 328 const size_t BuildID_prefix_length = sizeof(BUILDID_PREFIX) - 1;
mjr 53:9b2611964afc 329
mjr 53:9b2611964afc 330 return BuildID + BuildID_prefix_length;
mjr 53:9b2611964afc 331 }
mjr 53:9b2611964afc 332
mjr 74:822a92bc11d2 333 // --------------------------------------------------------------------------
mjr 74:822a92bc11d2 334 // Main loop iteration timing statistics. Collected only if
mjr 74:822a92bc11d2 335 // ENABLE_DIAGNOSTICS is set in diags.h.
mjr 76:7f5912b6340e 336 #if ENABLE_DIAGNOSTICS
mjr 76:7f5912b6340e 337 uint64_t mainLoopIterTime, mainLoopIterCheckpt[15], mainLoopIterCount;
mjr 76:7f5912b6340e 338 uint64_t mainLoopMsgTime, mainLoopMsgCount;
mjr 76:7f5912b6340e 339 Timer mainLoopTimer;
mjr 76:7f5912b6340e 340 #endif
mjr 76:7f5912b6340e 341
mjr 53:9b2611964afc 342
mjr 5:a70c0bce770d 343 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 344 //
mjr 38:091e511ce8a0 345 // Forward declarations
mjr 38:091e511ce8a0 346 //
mjr 38:091e511ce8a0 347 void setNightMode(bool on);
mjr 38:091e511ce8a0 348 void toggleNightMode();
mjr 38:091e511ce8a0 349
mjr 38:091e511ce8a0 350 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 351 // utilities
mjr 17:ab3cec0c8bf4 352
mjr 77:0b96f6867312 353 // int/float point square of a number
mjr 77:0b96f6867312 354 inline int square(int x) { return x*x; }
mjr 26:cb71c4af2912 355 inline float square(float x) { return x*x; }
mjr 26:cb71c4af2912 356
mjr 26:cb71c4af2912 357 // floating point rounding
mjr 26:cb71c4af2912 358 inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
mjr 26:cb71c4af2912 359
mjr 17:ab3cec0c8bf4 360
mjr 33:d832bcab089e 361 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 362 //
mjr 40:cc0d9814522b 363 // Extended verison of Timer class. This adds the ability to interrogate
mjr 40:cc0d9814522b 364 // the running state.
mjr 40:cc0d9814522b 365 //
mjr 77:0b96f6867312 366 class ExtTimer: public Timer
mjr 40:cc0d9814522b 367 {
mjr 40:cc0d9814522b 368 public:
mjr 77:0b96f6867312 369 ExtTimer() : running(false) { }
mjr 40:cc0d9814522b 370
mjr 40:cc0d9814522b 371 void start() { running = true; Timer::start(); }
mjr 40:cc0d9814522b 372 void stop() { running = false; Timer::stop(); }
mjr 40:cc0d9814522b 373
mjr 40:cc0d9814522b 374 bool isRunning() const { return running; }
mjr 40:cc0d9814522b 375
mjr 40:cc0d9814522b 376 private:
mjr 40:cc0d9814522b 377 bool running;
mjr 40:cc0d9814522b 378 };
mjr 40:cc0d9814522b 379
mjr 53:9b2611964afc 380
mjr 53:9b2611964afc 381 // --------------------------------------------------------------------------
mjr 40:cc0d9814522b 382 //
mjr 33:d832bcab089e 383 // USB product version number
mjr 5:a70c0bce770d 384 //
mjr 47:df7a88cd249c 385 const uint16_t USB_VERSION_NO = 0x000A;
mjr 33:d832bcab089e 386
mjr 33:d832bcab089e 387 // --------------------------------------------------------------------------
mjr 33:d832bcab089e 388 //
mjr 6:cc35eb643e8f 389 // Joystick axis report range - we report from -JOYMAX to +JOYMAX
mjr 33:d832bcab089e 390 //
mjr 6:cc35eb643e8f 391 #define JOYMAX 4096
mjr 6:cc35eb643e8f 392
mjr 9:fd65b0a94720 393
mjr 17:ab3cec0c8bf4 394 // ---------------------------------------------------------------------------
mjr 17:ab3cec0c8bf4 395 //
mjr 40:cc0d9814522b 396 // Wire protocol value translations. These translate byte values to and
mjr 40:cc0d9814522b 397 // from the USB protocol to local native format.
mjr 35:e959ffba78fd 398 //
mjr 35:e959ffba78fd 399
mjr 35:e959ffba78fd 400 // unsigned 16-bit integer
mjr 35:e959ffba78fd 401 inline uint16_t wireUI16(const uint8_t *b)
mjr 35:e959ffba78fd 402 {
mjr 35:e959ffba78fd 403 return b[0] | ((uint16_t)b[1] << 8);
mjr 35:e959ffba78fd 404 }
mjr 40:cc0d9814522b 405 inline void ui16Wire(uint8_t *b, uint16_t val)
mjr 40:cc0d9814522b 406 {
mjr 40:cc0d9814522b 407 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 408 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 409 }
mjr 35:e959ffba78fd 410
mjr 35:e959ffba78fd 411 inline int16_t wireI16(const uint8_t *b)
mjr 35:e959ffba78fd 412 {
mjr 35:e959ffba78fd 413 return (int16_t)wireUI16(b);
mjr 35:e959ffba78fd 414 }
mjr 40:cc0d9814522b 415 inline void i16Wire(uint8_t *b, int16_t val)
mjr 40:cc0d9814522b 416 {
mjr 40:cc0d9814522b 417 ui16Wire(b, (uint16_t)val);
mjr 40:cc0d9814522b 418 }
mjr 35:e959ffba78fd 419
mjr 35:e959ffba78fd 420 inline uint32_t wireUI32(const uint8_t *b)
mjr 35:e959ffba78fd 421 {
mjr 35:e959ffba78fd 422 return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
mjr 35:e959ffba78fd 423 }
mjr 40:cc0d9814522b 424 inline void ui32Wire(uint8_t *b, uint32_t val)
mjr 40:cc0d9814522b 425 {
mjr 40:cc0d9814522b 426 b[0] = (uint8_t)(val & 0xff);
mjr 40:cc0d9814522b 427 b[1] = (uint8_t)((val >> 8) & 0xff);
mjr 40:cc0d9814522b 428 b[2] = (uint8_t)((val >> 16) & 0xff);
mjr 40:cc0d9814522b 429 b[3] = (uint8_t)((val >> 24) & 0xff);
mjr 40:cc0d9814522b 430 }
mjr 35:e959ffba78fd 431
mjr 35:e959ffba78fd 432 inline int32_t wireI32(const uint8_t *b)
mjr 35:e959ffba78fd 433 {
mjr 35:e959ffba78fd 434 return (int32_t)wireUI32(b);
mjr 35:e959ffba78fd 435 }
mjr 35:e959ffba78fd 436
mjr 53:9b2611964afc 437 // Convert "wire" (USB) pin codes to/from PinName values.
mjr 53:9b2611964afc 438 //
mjr 53:9b2611964afc 439 // The internal mbed PinName format is
mjr 53:9b2611964afc 440 //
mjr 53:9b2611964afc 441 // ((port) << PORT_SHIFT) | (pin << 2) // MBED FORMAT
mjr 53:9b2611964afc 442 //
mjr 53:9b2611964afc 443 // where 'port' is 0-4 for Port A to Port E, and 'pin' is
mjr 53:9b2611964afc 444 // 0 to 31. E.g., E31 is (4 << PORT_SHIFT) | (31<<2).
mjr 53:9b2611964afc 445 //
mjr 53:9b2611964afc 446 // We remap this to our more compact wire format where each
mjr 53:9b2611964afc 447 // pin name fits in 8 bits:
mjr 53:9b2611964afc 448 //
mjr 53:9b2611964afc 449 // ((port) << 5) | pin) // WIRE FORMAT
mjr 53:9b2611964afc 450 //
mjr 53:9b2611964afc 451 // E.g., E31 is (4 << 5) | 31.
mjr 53:9b2611964afc 452 //
mjr 53:9b2611964afc 453 // Wire code FF corresponds to PinName NC (not connected).
mjr 53:9b2611964afc 454 //
mjr 53:9b2611964afc 455 inline PinName wirePinName(uint8_t c)
mjr 35:e959ffba78fd 456 {
mjr 53:9b2611964afc 457 if (c == 0xFF)
mjr 53:9b2611964afc 458 return NC; // 0xFF -> NC
mjr 53:9b2611964afc 459 else
mjr 53:9b2611964afc 460 return PinName(
mjr 53:9b2611964afc 461 (int(c & 0xE0) << (PORT_SHIFT - 5)) // top three bits are the port
mjr 53:9b2611964afc 462 | (int(c & 0x1F) << 2)); // bottom five bits are pin
mjr 40:cc0d9814522b 463 }
mjr 40:cc0d9814522b 464 inline void pinNameWire(uint8_t *b, PinName n)
mjr 40:cc0d9814522b 465 {
mjr 53:9b2611964afc 466 *b = PINNAME_TO_WIRE(n);
mjr 35:e959ffba78fd 467 }
mjr 35:e959ffba78fd 468
mjr 35:e959ffba78fd 469
mjr 35:e959ffba78fd 470 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 471 //
mjr 38:091e511ce8a0 472 // On-board RGB LED elements - we use these for diagnostic displays.
mjr 38:091e511ce8a0 473 //
mjr 38:091e511ce8a0 474 // Note that LED3 (the blue segment) is hard-wired on the KL25Z to PTD1,
mjr 38:091e511ce8a0 475 // so PTD1 shouldn't be used for any other purpose (e.g., as a keyboard
mjr 38:091e511ce8a0 476 // input or a device output). This is kind of unfortunate in that it's
mjr 38:091e511ce8a0 477 // one of only two ports exposed on the jumper pins that can be muxed to
mjr 38:091e511ce8a0 478 // SPI0 SCLK. This effectively limits us to PTC5 if we want to use the
mjr 38:091e511ce8a0 479 // SPI capability.
mjr 38:091e511ce8a0 480 //
mjr 38:091e511ce8a0 481 DigitalOut *ledR, *ledG, *ledB;
mjr 38:091e511ce8a0 482
mjr 73:4e8ce0b18915 483 // Power on timer state for diagnostics. We flash the blue LED when
mjr 77:0b96f6867312 484 // nothing else is going on. State 0-1 = off, 2-3 = on blue. Also
mjr 77:0b96f6867312 485 // show red when transmitting an LED signal, indicated by state 4.
mjr 73:4e8ce0b18915 486 uint8_t powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 487
mjr 38:091e511ce8a0 488 // Show the indicated pattern on the diagnostic LEDs. 0 is off, 1 is
mjr 38:091e511ce8a0 489 // on, and -1 is no change (leaves the current setting intact).
mjr 73:4e8ce0b18915 490 static uint8_t diagLEDState = 0;
mjr 38:091e511ce8a0 491 void diagLED(int r, int g, int b)
mjr 38:091e511ce8a0 492 {
mjr 73:4e8ce0b18915 493 // remember the new state
mjr 73:4e8ce0b18915 494 diagLEDState = r | (g << 1) | (b << 2);
mjr 73:4e8ce0b18915 495
mjr 73:4e8ce0b18915 496 // if turning everything off, use the power timer state instead,
mjr 73:4e8ce0b18915 497 // applying it to the blue LED
mjr 73:4e8ce0b18915 498 if (diagLEDState == 0)
mjr 77:0b96f6867312 499 {
mjr 77:0b96f6867312 500 b = (powerTimerDiagState == 2 || powerTimerDiagState == 3);
mjr 77:0b96f6867312 501 r = (powerTimerDiagState == 4);
mjr 77:0b96f6867312 502 }
mjr 73:4e8ce0b18915 503
mjr 73:4e8ce0b18915 504 // set the new state
mjr 38:091e511ce8a0 505 if (ledR != 0 && r != -1) ledR->write(!r);
mjr 38:091e511ce8a0 506 if (ledG != 0 && g != -1) ledG->write(!g);
mjr 38:091e511ce8a0 507 if (ledB != 0 && b != -1) ledB->write(!b);
mjr 38:091e511ce8a0 508 }
mjr 38:091e511ce8a0 509
mjr 73:4e8ce0b18915 510 // update the LEDs with the current state
mjr 73:4e8ce0b18915 511 void diagLED(void)
mjr 73:4e8ce0b18915 512 {
mjr 73:4e8ce0b18915 513 diagLED(
mjr 73:4e8ce0b18915 514 diagLEDState & 0x01,
mjr 73:4e8ce0b18915 515 (diagLEDState >> 1) & 0x01,
mjr 77:0b96f6867312 516 (diagLEDState >> 2) & 0x01);
mjr 73:4e8ce0b18915 517 }
mjr 73:4e8ce0b18915 518
mjr 106:e9e3b46132c1 519 // check an output port or pin assignment to see if it conflicts with
mjr 38:091e511ce8a0 520 // an on-board LED segment
mjr 38:091e511ce8a0 521 struct LedSeg
mjr 38:091e511ce8a0 522 {
mjr 38:091e511ce8a0 523 bool r, g, b;
mjr 38:091e511ce8a0 524 LedSeg() { r = g = b = false; }
mjr 38:091e511ce8a0 525
mjr 106:e9e3b46132c1 526 // check an output port to see if it conflicts with one of the LED ports
mjr 38:091e511ce8a0 527 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 528 {
mjr 38:091e511ce8a0 529 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 530 // our on-board LED segments
mjr 38:091e511ce8a0 531 int t = pc.typ;
mjr 38:091e511ce8a0 532 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 106:e9e3b46132c1 533 check(pc.pin);
mjr 106:e9e3b46132c1 534 }
mjr 106:e9e3b46132c1 535
mjr 106:e9e3b46132c1 536 // check a pin to see if it conflicts with one of the diagnostic LED ports
mjr 106:e9e3b46132c1 537 void check(uint8_t pinId)
mjr 106:e9e3b46132c1 538 {
mjr 106:e9e3b46132c1 539 PinName pin = wirePinName(pinId);
mjr 106:e9e3b46132c1 540 if (pin == LED1)
mjr 106:e9e3b46132c1 541 r = true;
mjr 106:e9e3b46132c1 542 else if (pin == LED2)
mjr 106:e9e3b46132c1 543 g = true;
mjr 106:e9e3b46132c1 544 else if (pin == LED3)
mjr 106:e9e3b46132c1 545 b = true;
mjr 38:091e511ce8a0 546 }
mjr 38:091e511ce8a0 547 };
mjr 38:091e511ce8a0 548
mjr 38:091e511ce8a0 549 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 550 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 551 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 552 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 553 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 554 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 555 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 556 {
mjr 38:091e511ce8a0 557 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 558 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 559 LedSeg l;
mjr 38:091e511ce8a0 560 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 561 l.check(cfg.outPort[i]);
mjr 106:e9e3b46132c1 562
mjr 106:e9e3b46132c1 563 // check the button inputs
mjr 106:e9e3b46132c1 564 for (int i = 0 ; i < countof(cfg.button) ; ++i)
mjr 106:e9e3b46132c1 565 l.check(cfg.button[i].pin);
mjr 106:e9e3b46132c1 566
mjr 106:e9e3b46132c1 567 // check plunger inputs
mjr 106:e9e3b46132c1 568 if (cfg.plunger.enabled && cfg.plunger.sensorType != PlungerType_None)
mjr 106:e9e3b46132c1 569 {
mjr 106:e9e3b46132c1 570 for (int i = 0 ; i < countof(cfg.plunger.sensorPin) ; ++i)
mjr 106:e9e3b46132c1 571 l.check(cfg.plunger.sensorPin[i]);
mjr 106:e9e3b46132c1 572 }
mjr 106:e9e3b46132c1 573
mjr 106:e9e3b46132c1 574 // check the TV ON pin assignments
mjr 106:e9e3b46132c1 575 l.check(cfg.TVON.statusPin);
mjr 106:e9e3b46132c1 576 l.check(cfg.TVON.latchPin);
mjr 106:e9e3b46132c1 577 l.check(cfg.TVON.relayPin);
mjr 106:e9e3b46132c1 578
mjr 106:e9e3b46132c1 579 // check the TLC5940 pins
mjr 106:e9e3b46132c1 580 if (cfg.tlc5940.nchips != 0)
mjr 106:e9e3b46132c1 581 {
mjr 106:e9e3b46132c1 582 l.check(cfg.tlc5940.sin);
mjr 106:e9e3b46132c1 583 l.check(cfg.tlc5940.sclk);
mjr 106:e9e3b46132c1 584 l.check(cfg.tlc5940.xlat);
mjr 106:e9e3b46132c1 585 l.check(cfg.tlc5940.blank);
mjr 106:e9e3b46132c1 586 l.check(cfg.tlc5940.gsclk);
mjr 106:e9e3b46132c1 587 }
mjr 106:e9e3b46132c1 588
mjr 106:e9e3b46132c1 589 // check 74HC595 pin assignments
mjr 106:e9e3b46132c1 590 if (cfg.hc595.nchips != 0)
mjr 106:e9e3b46132c1 591 {
mjr 106:e9e3b46132c1 592 l.check(cfg.hc595.sin);
mjr 106:e9e3b46132c1 593 l.check(cfg.hc595.sclk);
mjr 106:e9e3b46132c1 594 l.check(cfg.hc595.latch);
mjr 106:e9e3b46132c1 595 l.check(cfg.hc595.ena);
mjr 106:e9e3b46132c1 596 }
mjr 106:e9e3b46132c1 597
mjr 106:e9e3b46132c1 598 // check TLC59116 pin assignments
mjr 106:e9e3b46132c1 599 if (cfg.tlc59116.chipMask != 0)
mjr 106:e9e3b46132c1 600 {
mjr 106:e9e3b46132c1 601 l.check(cfg.tlc59116.sda);
mjr 106:e9e3b46132c1 602 l.check(cfg.tlc59116.scl);
mjr 106:e9e3b46132c1 603 l.check(cfg.tlc59116.reset);
mjr 106:e9e3b46132c1 604 }
mjr 106:e9e3b46132c1 605
mjr 106:e9e3b46132c1 606 // check the IR remove control hardware
mjr 106:e9e3b46132c1 607 l.check(cfg.IR.sensor);
mjr 106:e9e3b46132c1 608 l.check(cfg.IR.emitter);
mjr 106:e9e3b46132c1 609
mjr 106:e9e3b46132c1 610 // We now know which segments are taken for other uses and which
mjr 38:091e511ce8a0 611 // are free. Create diagnostic ports for the ones not claimed for
mjr 106:e9e3b46132c1 612 // other purposes.
mjr 38:091e511ce8a0 613 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 614 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 615 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 616 }
mjr 38:091e511ce8a0 617
mjr 38:091e511ce8a0 618
mjr 38:091e511ce8a0 619 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 620 //
mjr 76:7f5912b6340e 621 // LedWiz emulation
mjr 76:7f5912b6340e 622 //
mjr 76:7f5912b6340e 623
mjr 76:7f5912b6340e 624 // LedWiz output states.
mjr 76:7f5912b6340e 625 //
mjr 76:7f5912b6340e 626 // The LedWiz protocol has two separate control axes for each output.
mjr 76:7f5912b6340e 627 // One axis is its on/off state; the other is its "profile" state, which
mjr 76:7f5912b6340e 628 // is either a fixed brightness or a blinking pattern for the light.
mjr 76:7f5912b6340e 629 // The two axes are independent.
mjr 76:7f5912b6340e 630 //
mjr 76:7f5912b6340e 631 // Even though the original LedWiz protocol can only access 32 ports, we
mjr 76:7f5912b6340e 632 // maintain LedWiz state for every port, even if we have more than 32. Our
mjr 76:7f5912b6340e 633 // extended protocol allows the client to send LedWiz-style messages that
mjr 76:7f5912b6340e 634 // control any set of ports. A replacement LEDWIZ.DLL can make a single
mjr 76:7f5912b6340e 635 // Pinscape unit look like multiple virtual LedWiz units to legacy clients,
mjr 76:7f5912b6340e 636 // allowing them to control all of our ports. The clients will still be
mjr 76:7f5912b6340e 637 // using LedWiz-style states to control the ports, so we need to support
mjr 76:7f5912b6340e 638 // the LedWiz scheme with separate on/off and brightness control per port.
mjr 76:7f5912b6340e 639
mjr 76:7f5912b6340e 640 // On/off state for each LedWiz output
mjr 76:7f5912b6340e 641 static uint8_t *wizOn;
mjr 76:7f5912b6340e 642
mjr 76:7f5912b6340e 643 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 76:7f5912b6340e 644 // for each LedWiz output. If the output was last updated through an
mjr 76:7f5912b6340e 645 // LedWiz protocol message, it will have one of these values:
mjr 76:7f5912b6340e 646 //
mjr 76:7f5912b6340e 647 // 0-48 = fixed brightness 0% to 100%
mjr 76:7f5912b6340e 648 // 49 = fixed brightness 100% (equivalent to 48)
mjr 76:7f5912b6340e 649 // 129 = ramp up / ramp down
mjr 76:7f5912b6340e 650 // 130 = flash on / off
mjr 76:7f5912b6340e 651 // 131 = on / ramp down
mjr 76:7f5912b6340e 652 // 132 = ramp up / on
mjr 5:a70c0bce770d 653 //
mjr 76:7f5912b6340e 654 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 76:7f5912b6340e 655 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 76:7f5912b6340e 656 // it, so we need to accept it for compatibility.)
mjr 76:7f5912b6340e 657 static uint8_t *wizVal;
mjr 76:7f5912b6340e 658
mjr 76:7f5912b6340e 659 // Current actual brightness for each output. This is a simple linear
mjr 76:7f5912b6340e 660 // value on a 0..255 scale. This is EITHER the linear brightness computed
mjr 76:7f5912b6340e 661 // from the LedWiz setting for the port, OR the 0..255 value set explicitly
mjr 76:7f5912b6340e 662 // by the extended protocol:
mjr 76:7f5912b6340e 663 //
mjr 76:7f5912b6340e 664 // - If the last command that updated the port was an extended protocol
mjr 76:7f5912b6340e 665 // SET BRIGHTNESS command, this is the value set by that command. In
mjr 76:7f5912b6340e 666 // addition, wizOn[port] is set to 0 if the brightness is 0, 1 otherwise;
mjr 76:7f5912b6340e 667 // and wizVal[port] is set to the brightness rescaled to the 0..48 range
mjr 76:7f5912b6340e 668 // if the brightness is non-zero.
mjr 76:7f5912b6340e 669 //
mjr 76:7f5912b6340e 670 // - If the last command that updated the port was an LedWiz command
mjr 76:7f5912b6340e 671 // (SBA/PBA/SBX/PBX), this contains the brightness value computed from
mjr 76:7f5912b6340e 672 // the combination of wizOn[port] and wizVal[port]. If wizOn[port] is
mjr 76:7f5912b6340e 673 // zero, this is simply 0, otherwise it's wizVal[port] rescaled to the
mjr 76:7f5912b6340e 674 // 0..255 range.
mjr 26:cb71c4af2912 675 //
mjr 76:7f5912b6340e 676 // - For a port set to wizOn[port]=1 and wizVal[port] in 129..132, this is
mjr 76:7f5912b6340e 677 // also updated continuously to reflect the current flashing brightness
mjr 76:7f5912b6340e 678 // level.
mjr 26:cb71c4af2912 679 //
mjr 76:7f5912b6340e 680 static uint8_t *outLevel;
mjr 76:7f5912b6340e 681
mjr 76:7f5912b6340e 682
mjr 76:7f5912b6340e 683 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 76:7f5912b6340e 684 // rate for lights in blinking states. The LedWiz API doesn't document
mjr 76:7f5912b6340e 685 // what the numbers mean in real time units, but by observation, the
mjr 76:7f5912b6340e 686 // "speed" setting represents the period of the flash cycle in 0.25s
mjr 76:7f5912b6340e 687 // units, so speed 1 = 0.25 period = 4Hz, speed 7 = 1.75s period = 0.57Hz.
mjr 76:7f5912b6340e 688 // The period is the full cycle time of the flash waveform.
mjr 76:7f5912b6340e 689 //
mjr 76:7f5912b6340e 690 // Each bank of 32 lights has its independent own pulse rate, so we need
mjr 76:7f5912b6340e 691 // one entry per bank. Each bank has 32 outputs, so we need a total of
mjr 76:7f5912b6340e 692 // ceil(number_of_physical_outputs/32) entries. Note that we could allocate
mjr 76:7f5912b6340e 693 // this dynamically once we know the number of actual outputs, but the
mjr 76:7f5912b6340e 694 // upper limit is low enough that it's more efficient to use a fixed array
mjr 76:7f5912b6340e 695 // at the maximum size.
mjr 76:7f5912b6340e 696 static const int MAX_LW_BANKS = (MAX_OUT_PORTS+31)/32;
mjr 76:7f5912b6340e 697 static uint8_t wizSpeed[MAX_LW_BANKS];
mjr 29:582472d0bc57 698
mjr 26:cb71c4af2912 699 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 700 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 701 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 702 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 703 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 704
mjr 76:7f5912b6340e 705
mjr 76:7f5912b6340e 706 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 707 //
mjr 76:7f5912b6340e 708 // Output Ports
mjr 76:7f5912b6340e 709 //
mjr 76:7f5912b6340e 710 // There are two way to connect outputs. First, you can use the on-board
mjr 76:7f5912b6340e 711 // GPIO ports to implement device outputs: each LedWiz software port is
mjr 76:7f5912b6340e 712 // connected to a physical GPIO pin on the KL25Z. This has some pretty
mjr 76:7f5912b6340e 713 // strict limits, though. The KL25Z only has 10 PWM channels, so only 10
mjr 76:7f5912b6340e 714 // GPIO LedWiz ports can be made dimmable; the rest are strictly on/off.
mjr 76:7f5912b6340e 715 // The KL25Z also simply doesn't have enough exposed GPIO ports overall to
mjr 76:7f5912b6340e 716 // support all of the features the software supports. The software allows
mjr 76:7f5912b6340e 717 // for up to 128 outputs, 48 button inputs, plunger input (requiring 1-5
mjr 76:7f5912b6340e 718 // GPIO pins), and various other external devices. The KL25Z only exposes
mjr 76:7f5912b6340e 719 // about 50 GPIO pins. So if you want to do everything with GPIO ports,
mjr 76:7f5912b6340e 720 // you have to ration pins among features.
mjr 76:7f5912b6340e 721 //
mjr 87:8d35c74403af 722 // To overcome some of these limitations, we also support several external
mjr 76:7f5912b6340e 723 // peripheral controllers that allow adding many more outputs, using only
mjr 87:8d35c74403af 724 // a small number of GPIO pins to interface with the peripherals:
mjr 87:8d35c74403af 725 //
mjr 87:8d35c74403af 726 // - TLC5940 PWM controller chips. Each TLC5940 provides 16 ports with
mjr 87:8d35c74403af 727 // 12-bit PWM, and multiple TLC5940 chips can be daisy-chained. The
mjr 87:8d35c74403af 728 // chips connect via 5 GPIO pins, and since they're daisy-chainable,
mjr 87:8d35c74403af 729 // one set of 5 pins can control any number of the chips. So this chip
mjr 87:8d35c74403af 730 // effectively converts 5 GPIO pins into almost any number of PWM outputs.
mjr 87:8d35c74403af 731 //
mjr 87:8d35c74403af 732 // - TLC59116 PWM controller chips. These are similar to the TLC5940 but
mjr 87:8d35c74403af 733 // a newer generation with an improved design. These use an I2C bus,
mjr 87:8d35c74403af 734 // allowing up to 14 chips to be connected via 3 GPIO pins.
mjr 87:8d35c74403af 735 //
mjr 87:8d35c74403af 736 // - 74HC595 shift register chips. These provide 8 digital (on/off only)
mjr 87:8d35c74403af 737 // outputs per chip. These need 4 GPIO pins, and like the other can be
mjr 87:8d35c74403af 738 // daisy chained to add more outputs without using more GPIO pins. These
mjr 87:8d35c74403af 739 // are advantageous for outputs that don't require PWM, since the data
mjr 87:8d35c74403af 740 // transfer sizes are so much smaller. The expansion boards use these
mjr 87:8d35c74403af 741 // for the chime board outputs.
mjr 76:7f5912b6340e 742 //
mjr 76:7f5912b6340e 743 // Direct GPIO output ports and peripheral controllers can be mixed and
mjr 76:7f5912b6340e 744 // matched in one system. The assignment of pins to ports and the
mjr 76:7f5912b6340e 745 // configuration of peripheral controllers is all handled in the software
mjr 76:7f5912b6340e 746 // setup, so a physical system can be expanded and updated at any time.
mjr 76:7f5912b6340e 747 //
mjr 76:7f5912b6340e 748 // To handle the diversity of output port types, we start with an abstract
mjr 76:7f5912b6340e 749 // base class for outputs. Each type of physical output interface has a
mjr 76:7f5912b6340e 750 // concrete subclass. During initialization, we create the appropriate
mjr 76:7f5912b6340e 751 // subclass for each software port, mapping it to the assigned GPIO pin
mjr 76:7f5912b6340e 752 // or peripheral port. Most of the rest of the software only cares about
mjr 76:7f5912b6340e 753 // the abstract interface, so once the subclassed port objects are set up,
mjr 76:7f5912b6340e 754 // the rest of the system can control the ports without knowing which types
mjr 76:7f5912b6340e 755 // of physical devices they're connected to.
mjr 76:7f5912b6340e 756
mjr 76:7f5912b6340e 757
mjr 26:cb71c4af2912 758 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 759 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 760 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 761 class LwOut
mjr 6:cc35eb643e8f 762 {
mjr 6:cc35eb643e8f 763 public:
mjr 40:cc0d9814522b 764 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 765 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 766 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 767 };
mjr 26:cb71c4af2912 768
mjr 35:e959ffba78fd 769 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 770 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 771 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 772 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 773 // numbering.
mjr 35:e959ffba78fd 774 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 775 {
mjr 33:d832bcab089e 776 public:
mjr 35:e959ffba78fd 777 LwVirtualOut() { }
mjr 40:cc0d9814522b 778 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 779 };
mjr 26:cb71c4af2912 780
mjr 34:6b981a2afab7 781 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 782 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 783 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 784 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 785 {
mjr 34:6b981a2afab7 786 public:
mjr 34:6b981a2afab7 787 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 788 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 789
mjr 34:6b981a2afab7 790 private:
mjr 53:9b2611964afc 791 // underlying physical output
mjr 34:6b981a2afab7 792 LwOut *out;
mjr 34:6b981a2afab7 793 };
mjr 34:6b981a2afab7 794
mjr 53:9b2611964afc 795 // Global ZB Launch Ball state
mjr 53:9b2611964afc 796 bool zbLaunchOn = false;
mjr 53:9b2611964afc 797
mjr 53:9b2611964afc 798 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 799 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 800 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 801 {
mjr 53:9b2611964afc 802 public:
mjr 53:9b2611964afc 803 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 804 virtual void set(uint8_t val)
mjr 53:9b2611964afc 805 {
mjr 53:9b2611964afc 806 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 807 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 808
mjr 53:9b2611964afc 809 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 810 out->set(val);
mjr 53:9b2611964afc 811 }
mjr 53:9b2611964afc 812
mjr 53:9b2611964afc 813 private:
mjr 53:9b2611964afc 814 // underlying physical or virtual output
mjr 53:9b2611964afc 815 LwOut *out;
mjr 53:9b2611964afc 816 };
mjr 53:9b2611964afc 817
mjr 53:9b2611964afc 818
mjr 40:cc0d9814522b 819 // Gamma correction table for 8-bit input values
mjr 87:8d35c74403af 820 static const uint8_t dof_to_gamma_8bit[] = {
mjr 40:cc0d9814522b 821 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 822 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 823 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 824 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 825 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 826 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 827 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 828 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 829 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 830 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 831 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 832 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 833 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 834 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 835 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 836 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 837 };
mjr 40:cc0d9814522b 838
mjr 40:cc0d9814522b 839 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 840 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 841 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 842 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 843 {
mjr 40:cc0d9814522b 844 public:
mjr 40:cc0d9814522b 845 LwGammaOut(LwOut *o) : out(o) { }
mjr 87:8d35c74403af 846 virtual void set(uint8_t val) { out->set(dof_to_gamma_8bit[val]); }
mjr 40:cc0d9814522b 847
mjr 40:cc0d9814522b 848 private:
mjr 40:cc0d9814522b 849 LwOut *out;
mjr 40:cc0d9814522b 850 };
mjr 40:cc0d9814522b 851
mjr 77:0b96f6867312 852 // Global night mode flag. To minimize overhead when reporting
mjr 77:0b96f6867312 853 // the status, we set this to the status report flag bit for
mjr 77:0b96f6867312 854 // night mode, 0x02, when engaged.
mjr 77:0b96f6867312 855 static uint8_t nightMode = 0x00;
mjr 53:9b2611964afc 856
mjr 40:cc0d9814522b 857 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 858 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 859 // mode is engaged.
mjr 40:cc0d9814522b 860 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 861 {
mjr 40:cc0d9814522b 862 public:
mjr 40:cc0d9814522b 863 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 864 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 865
mjr 53:9b2611964afc 866 private:
mjr 53:9b2611964afc 867 LwOut *out;
mjr 53:9b2611964afc 868 };
mjr 53:9b2611964afc 869
mjr 53:9b2611964afc 870 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 871 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 872 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 873 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 874 {
mjr 53:9b2611964afc 875 public:
mjr 53:9b2611964afc 876 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 89:c43cd923401c 877 virtual void set(uint8_t)
mjr 53:9b2611964afc 878 {
mjr 53:9b2611964afc 879 // ignore the host value and simply show the current
mjr 53:9b2611964afc 880 // night mode setting
mjr 53:9b2611964afc 881 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 882 }
mjr 40:cc0d9814522b 883
mjr 40:cc0d9814522b 884 private:
mjr 40:cc0d9814522b 885 LwOut *out;
mjr 40:cc0d9814522b 886 };
mjr 40:cc0d9814522b 887
mjr 26:cb71c4af2912 888
mjr 89:c43cd923401c 889 // Flipper Logic output. This is a filter object that we layer on
mjr 89:c43cd923401c 890 // top of a physical pin output.
mjr 89:c43cd923401c 891 //
mjr 89:c43cd923401c 892 // A Flipper Logic output is effectively a digital output from the
mjr 89:c43cd923401c 893 // client's perspective, in that it ignores the intensity level and
mjr 89:c43cd923401c 894 // only pays attention to the ON/OFF state. 0 is OFF and any other
mjr 89:c43cd923401c 895 // level is ON.
mjr 89:c43cd923401c 896 //
mjr 89:c43cd923401c 897 // In terms of the physical output, though, we do use varying power.
mjr 89:c43cd923401c 898 // It's just that the varying power isn't under the client's control;
mjr 89:c43cd923401c 899 // we control it according to our flipperLogic settings:
mjr 89:c43cd923401c 900 //
mjr 89:c43cd923401c 901 // - When the software port transitions from OFF (0 brightness) to ON
mjr 89:c43cd923401c 902 // (any non-zero brightness level), we set the physical port to 100%
mjr 89:c43cd923401c 903 // power and start a timer.
mjr 89:c43cd923401c 904 //
mjr 89:c43cd923401c 905 // - When the full power time in our flipperLogic settings elapses,
mjr 89:c43cd923401c 906 // if the software port is still ON, we reduce the physical port to
mjr 89:c43cd923401c 907 // the PWM level in our flipperLogic setting.
mjr 89:c43cd923401c 908 //
mjr 89:c43cd923401c 909 class LwFlipperLogicOut: public LwOut
mjr 89:c43cd923401c 910 {
mjr 89:c43cd923401c 911 public:
mjr 89:c43cd923401c 912 // Set up the output. 'params' is the flipperLogic value from
mjr 89:c43cd923401c 913 // the configuration.
mjr 89:c43cd923401c 914 LwFlipperLogicOut(LwOut *o, uint8_t params)
mjr 89:c43cd923401c 915 : out(o), params(params)
mjr 89:c43cd923401c 916 {
mjr 89:c43cd923401c 917 // initially OFF
mjr 89:c43cd923401c 918 state = 0;
mjr 89:c43cd923401c 919 }
mjr 89:c43cd923401c 920
mjr 89:c43cd923401c 921 virtual void set(uint8_t level)
mjr 89:c43cd923401c 922 {
mjr 98:4df3c0f7e707 923 // remember the new nominal level set by the client
mjr 89:c43cd923401c 924 val = level;
mjr 89:c43cd923401c 925
mjr 89:c43cd923401c 926 // update the physical output according to our current timing state
mjr 89:c43cd923401c 927 switch (state)
mjr 89:c43cd923401c 928 {
mjr 89:c43cd923401c 929 case 0:
mjr 89:c43cd923401c 930 // We're currently off. If the new level is non-zero, switch
mjr 89:c43cd923401c 931 // to state 1 (initial full-power interval) and set the requested
mjr 89:c43cd923401c 932 // level. If the new level is zero, we're switching from off to
mjr 89:c43cd923401c 933 // off, so there's no change.
mjr 89:c43cd923401c 934 if (level != 0)
mjr 89:c43cd923401c 935 {
mjr 89:c43cd923401c 936 // switch to state 1 (initial full-power interval)
mjr 89:c43cd923401c 937 state = 1;
mjr 89:c43cd923401c 938
mjr 89:c43cd923401c 939 // set the requested output level - there's no limit during
mjr 89:c43cd923401c 940 // the initial full-power interval, so set the exact level
mjr 89:c43cd923401c 941 // requested
mjr 89:c43cd923401c 942 out->set(level);
mjr 89:c43cd923401c 943
mjr 89:c43cd923401c 944 // add myself to the pending timer list
mjr 89:c43cd923401c 945 pending[nPending++] = this;
mjr 89:c43cd923401c 946
mjr 89:c43cd923401c 947 // note the starting time
mjr 89:c43cd923401c 948 t0 = timer.read_us();
mjr 89:c43cd923401c 949 }
mjr 89:c43cd923401c 950 break;
mjr 89:c43cd923401c 951
mjr 89:c43cd923401c 952 case 1:
mjr 89:c43cd923401c 953 // Initial full-power interval. If the new level is non-zero,
mjr 89:c43cd923401c 954 // simply apply the new level as requested, since there's no
mjr 89:c43cd923401c 955 // limit during this period. If the new level is zero, shut
mjr 89:c43cd923401c 956 // off the output and cancel the pending timer.
mjr 89:c43cd923401c 957 out->set(level);
mjr 89:c43cd923401c 958 if (level == 0)
mjr 89:c43cd923401c 959 {
mjr 89:c43cd923401c 960 // We're switching off. In state 1, we have a pending timer,
mjr 89:c43cd923401c 961 // so we need to remove it from the list.
mjr 89:c43cd923401c 962 for (int i = 0 ; i < nPending ; ++i)
mjr 89:c43cd923401c 963 {
mjr 89:c43cd923401c 964 // is this us?
mjr 89:c43cd923401c 965 if (pending[i] == this)
mjr 89:c43cd923401c 966 {
mjr 89:c43cd923401c 967 // remove myself by replacing the slot with the
mjr 89:c43cd923401c 968 // last list entry
mjr 89:c43cd923401c 969 pending[i] = pending[--nPending];
mjr 89:c43cd923401c 970
mjr 89:c43cd923401c 971 // no need to look any further
mjr 89:c43cd923401c 972 break;
mjr 89:c43cd923401c 973 }
mjr 89:c43cd923401c 974 }
mjr 89:c43cd923401c 975
mjr 89:c43cd923401c 976 // switch to state 0 (off)
mjr 89:c43cd923401c 977 state = 0;
mjr 89:c43cd923401c 978 }
mjr 89:c43cd923401c 979 break;
mjr 89:c43cd923401c 980
mjr 89:c43cd923401c 981 case 2:
mjr 89:c43cd923401c 982 // Hold interval. If the new level is zero, switch to state
mjr 89:c43cd923401c 983 // 0 (off). If the new level is non-zero, stay in the hold
mjr 89:c43cd923401c 984 // state, and set the new level, applying the hold power setting
mjr 89:c43cd923401c 985 // as the upper bound.
mjr 89:c43cd923401c 986 if (level == 0)
mjr 89:c43cd923401c 987 {
mjr 89:c43cd923401c 988 // switching off - turn off the physical output
mjr 89:c43cd923401c 989 out->set(0);
mjr 89:c43cd923401c 990
mjr 89:c43cd923401c 991 // go to state 0 (off)
mjr 89:c43cd923401c 992 state = 0;
mjr 89:c43cd923401c 993 }
mjr 89:c43cd923401c 994 else
mjr 89:c43cd923401c 995 {
mjr 89:c43cd923401c 996 // staying on - set the new physical output power to the
mjr 89:c43cd923401c 997 // lower of the requested power and the hold power
mjr 89:c43cd923401c 998 uint8_t hold = holdPower();
mjr 89:c43cd923401c 999 out->set(level < hold ? level : hold);
mjr 89:c43cd923401c 1000 }
mjr 89:c43cd923401c 1001 break;
mjr 89:c43cd923401c 1002 }
mjr 89:c43cd923401c 1003 }
mjr 89:c43cd923401c 1004
mjr 89:c43cd923401c 1005 // Class initialization
mjr 89:c43cd923401c 1006 static void classInit(Config &cfg)
mjr 89:c43cd923401c 1007 {
mjr 89:c43cd923401c 1008 // Count the Flipper Logic outputs in the configuration. We
mjr 89:c43cd923401c 1009 // need to allocate enough pending timer list space to accommodate
mjr 89:c43cd923401c 1010 // all of these outputs.
mjr 89:c43cd923401c 1011 int n = 0;
mjr 89:c43cd923401c 1012 for (int i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 89:c43cd923401c 1013 {
mjr 89:c43cd923401c 1014 // if this port is active and marked as Flipper Logic, count it
mjr 89:c43cd923401c 1015 if (cfg.outPort[i].typ != PortTypeDisabled
mjr 89:c43cd923401c 1016 && (cfg.outPort[i].flags & PortFlagFlipperLogic) != 0)
mjr 89:c43cd923401c 1017 ++n;
mjr 89:c43cd923401c 1018 }
mjr 89:c43cd923401c 1019
mjr 89:c43cd923401c 1020 // allocate space for the pending timer list
mjr 89:c43cd923401c 1021 pending = new LwFlipperLogicOut*[n];
mjr 89:c43cd923401c 1022
mjr 89:c43cd923401c 1023 // there's nothing in the pending list yet
mjr 89:c43cd923401c 1024 nPending = 0;
mjr 89:c43cd923401c 1025
mjr 89:c43cd923401c 1026 // Start our shared timer. The epoch is arbitrary, since we only
mjr 89:c43cd923401c 1027 // use it to figure elapsed times.
mjr 89:c43cd923401c 1028 timer.start();
mjr 89:c43cd923401c 1029 }
mjr 89:c43cd923401c 1030
mjr 89:c43cd923401c 1031 // Check for ports with pending timers. The main routine should
mjr 89:c43cd923401c 1032 // call this on each iteration to process our state transitions.
mjr 89:c43cd923401c 1033 static void poll()
mjr 89:c43cd923401c 1034 {
mjr 89:c43cd923401c 1035 // note the current time
mjr 89:c43cd923401c 1036 uint32_t t = timer.read_us();
mjr 89:c43cd923401c 1037
mjr 89:c43cd923401c 1038 // go through the timer list
mjr 89:c43cd923401c 1039 for (int i = 0 ; i < nPending ; )
mjr 89:c43cd923401c 1040 {
mjr 89:c43cd923401c 1041 // get the port
mjr 89:c43cd923401c 1042 LwFlipperLogicOut *port = pending[i];
mjr 89:c43cd923401c 1043
mjr 89:c43cd923401c 1044 // assume we'll keep it
mjr 89:c43cd923401c 1045 bool remove = false;
mjr 89:c43cd923401c 1046
mjr 89:c43cd923401c 1047 // check if the port is still on
mjr 89:c43cd923401c 1048 if (port->state != 0)
mjr 89:c43cd923401c 1049 {
mjr 89:c43cd923401c 1050 // it's still on - check if the initial full power time has elapsed
mjr 89:c43cd923401c 1051 if (uint32_t(t - port->t0) > port->fullPowerTime_us())
mjr 89:c43cd923401c 1052 {
mjr 89:c43cd923401c 1053 // done with the full power interval - switch to hold state
mjr 89:c43cd923401c 1054 port->state = 2;
mjr 89:c43cd923401c 1055
mjr 89:c43cd923401c 1056 // set the physical port to the hold power setting or the
mjr 89:c43cd923401c 1057 // client brightness setting, whichever is lower
mjr 89:c43cd923401c 1058 uint8_t hold = port->holdPower();
mjr 89:c43cd923401c 1059 uint8_t val = port->val;
mjr 89:c43cd923401c 1060 port->out->set(val < hold ? val : hold);
mjr 89:c43cd923401c 1061
mjr 89:c43cd923401c 1062 // we're done with the timer
mjr 89:c43cd923401c 1063 remove = true;
mjr 89:c43cd923401c 1064 }
mjr 89:c43cd923401c 1065 }
mjr 89:c43cd923401c 1066 else
mjr 89:c43cd923401c 1067 {
mjr 89:c43cd923401c 1068 // the port was turned off before the timer expired - remove
mjr 89:c43cd923401c 1069 // it from the timer list
mjr 89:c43cd923401c 1070 remove = true;
mjr 89:c43cd923401c 1071 }
mjr 89:c43cd923401c 1072
mjr 89:c43cd923401c 1073 // if desired, remove the port from the timer list
mjr 89:c43cd923401c 1074 if (remove)
mjr 89:c43cd923401c 1075 {
mjr 89:c43cd923401c 1076 // Remove the list entry by overwriting the slot with
mjr 89:c43cd923401c 1077 // the last entry in the list.
mjr 89:c43cd923401c 1078 pending[i] = pending[--nPending];
mjr 89:c43cd923401c 1079
mjr 89:c43cd923401c 1080 // Note that we don't increment the loop counter, since
mjr 89:c43cd923401c 1081 // we now need to revisit this same slot.
mjr 89:c43cd923401c 1082 }
mjr 89:c43cd923401c 1083 else
mjr 89:c43cd923401c 1084 {
mjr 89:c43cd923401c 1085 // we're keeping this item; move on to the next one
mjr 89:c43cd923401c 1086 ++i;
mjr 89:c43cd923401c 1087 }
mjr 89:c43cd923401c 1088 }
mjr 89:c43cd923401c 1089 }
mjr 89:c43cd923401c 1090
mjr 89:c43cd923401c 1091 protected:
mjr 89:c43cd923401c 1092 // underlying physical output
mjr 89:c43cd923401c 1093 LwOut *out;
mjr 89:c43cd923401c 1094
mjr 89:c43cd923401c 1095 // Timestamp on 'timer' of start of full-power interval. We set this
mjr 89:c43cd923401c 1096 // to the current 'timer' timestamp when entering state 1.
mjr 89:c43cd923401c 1097 uint32_t t0;
mjr 89:c43cd923401c 1098
mjr 89:c43cd923401c 1099 // Nominal output level (brightness) last set by the client. During
mjr 89:c43cd923401c 1100 // the initial full-power interval, we replicate the requested level
mjr 89:c43cd923401c 1101 // exactly on the physical output. During the hold interval, we limit
mjr 89:c43cd923401c 1102 // the physical output to the hold power, but use the caller's value
mjr 89:c43cd923401c 1103 // if it's lower.
mjr 89:c43cd923401c 1104 uint8_t val;
mjr 89:c43cd923401c 1105
mjr 89:c43cd923401c 1106 // Current port state:
mjr 89:c43cd923401c 1107 //
mjr 89:c43cd923401c 1108 // 0 = off
mjr 89:c43cd923401c 1109 // 1 = on at initial full power
mjr 89:c43cd923401c 1110 // 2 = on at hold power
mjr 89:c43cd923401c 1111 uint8_t state;
mjr 89:c43cd923401c 1112
mjr 89:c43cd923401c 1113 // Configuration parameters. The high 4 bits encode the initial full-
mjr 89:c43cd923401c 1114 // power time in 50ms units, starting at 0=50ms. The low 4 bits encode
mjr 89:c43cd923401c 1115 // the hold power (applied after the initial time expires if the output
mjr 89:c43cd923401c 1116 // is still on) in units of 6.66%. The resulting percentage is used
mjr 89:c43cd923401c 1117 // for the PWM duty cycle of the physical output.
mjr 89:c43cd923401c 1118 uint8_t params;
mjr 89:c43cd923401c 1119
mjr 99:8139b0c274f4 1120 // Figure the initial full-power time in microseconds: 50ms * (1+N),
mjr 99:8139b0c274f4 1121 // where N is the high 4 bits of the parameter byte.
mjr 99:8139b0c274f4 1122 inline uint32_t fullPowerTime_us() const { return 50000*(1 + ((params >> 4) & 0x0F)); }
mjr 89:c43cd923401c 1123
mjr 89:c43cd923401c 1124 // Figure the hold power PWM level (0-255)
mjr 89:c43cd923401c 1125 inline uint8_t holdPower() const { return (params & 0x0F) * 17; }
mjr 89:c43cd923401c 1126
mjr 89:c43cd923401c 1127 // Timer. This is a shared timer for all of the FL ports. When we
mjr 89:c43cd923401c 1128 // transition from OFF to ON, we note the current time on this timer
mjr 89:c43cd923401c 1129 // (which runs continuously).
mjr 89:c43cd923401c 1130 static Timer timer;
mjr 89:c43cd923401c 1131
mjr 89:c43cd923401c 1132 // Flipper logic pending timer list. Whenever a flipper logic output
mjr 98:4df3c0f7e707 1133 // transitions from OFF to ON, we add it to this list. We scan the
mjr 98:4df3c0f7e707 1134 // list in our polling routine to find ports that have reached the
mjr 98:4df3c0f7e707 1135 // expiration of their initial full-power intervals.
mjr 89:c43cd923401c 1136 static LwFlipperLogicOut **pending;
mjr 89:c43cd923401c 1137 static uint8_t nPending;
mjr 89:c43cd923401c 1138 };
mjr 89:c43cd923401c 1139
mjr 89:c43cd923401c 1140 // Flipper Logic statics
mjr 89:c43cd923401c 1141 Timer LwFlipperLogicOut::timer;
mjr 89:c43cd923401c 1142 LwFlipperLogicOut **LwFlipperLogicOut::pending;
mjr 89:c43cd923401c 1143 uint8_t LwFlipperLogicOut::nPending;
mjr 99:8139b0c274f4 1144
mjr 99:8139b0c274f4 1145 // Chime Logic. This is a filter output that we layer on a physical
mjr 99:8139b0c274f4 1146 // output to set a minimum and maximum ON time for the output.
mjr 99:8139b0c274f4 1147 class LwChimeLogicOut: public LwOut
mjr 98:4df3c0f7e707 1148 {
mjr 98:4df3c0f7e707 1149 public:
mjr 99:8139b0c274f4 1150 // Set up the output. 'params' encodes the minimum and maximum time.
mjr 99:8139b0c274f4 1151 LwChimeLogicOut(LwOut *o, uint8_t params)
mjr 99:8139b0c274f4 1152 : out(o), params(params)
mjr 98:4df3c0f7e707 1153 {
mjr 98:4df3c0f7e707 1154 // initially OFF
mjr 98:4df3c0f7e707 1155 state = 0;
mjr 98:4df3c0f7e707 1156 }
mjr 98:4df3c0f7e707 1157
mjr 98:4df3c0f7e707 1158 virtual void set(uint8_t level)
mjr 98:4df3c0f7e707 1159 {
mjr 98:4df3c0f7e707 1160 // update the physical output according to our current timing state
mjr 98:4df3c0f7e707 1161 switch (state)
mjr 98:4df3c0f7e707 1162 {
mjr 98:4df3c0f7e707 1163 case 0:
mjr 98:4df3c0f7e707 1164 // We're currently off. If the new level is non-zero, switch
mjr 98:4df3c0f7e707 1165 // to state 1 (initial minimum interval) and set the requested
mjr 98:4df3c0f7e707 1166 // level. If the new level is zero, we're switching from off to
mjr 98:4df3c0f7e707 1167 // off, so there's no change.
mjr 98:4df3c0f7e707 1168 if (level != 0)
mjr 98:4df3c0f7e707 1169 {
mjr 98:4df3c0f7e707 1170 // switch to state 1 (initial minimum interval, port is
mjr 98:4df3c0f7e707 1171 // logically on)
mjr 98:4df3c0f7e707 1172 state = 1;
mjr 98:4df3c0f7e707 1173
mjr 98:4df3c0f7e707 1174 // set the requested output level
mjr 98:4df3c0f7e707 1175 out->set(level);
mjr 98:4df3c0f7e707 1176
mjr 98:4df3c0f7e707 1177 // add myself to the pending timer list
mjr 98:4df3c0f7e707 1178 pending[nPending++] = this;
mjr 98:4df3c0f7e707 1179
mjr 98:4df3c0f7e707 1180 // note the starting time
mjr 98:4df3c0f7e707 1181 t0 = timer.read_us();
mjr 98:4df3c0f7e707 1182 }
mjr 98:4df3c0f7e707 1183 break;
mjr 98:4df3c0f7e707 1184
mjr 98:4df3c0f7e707 1185 case 1: // min ON interval, port on
mjr 98:4df3c0f7e707 1186 case 2: // min ON interval, port off
mjr 98:4df3c0f7e707 1187 // We're in the initial minimum ON interval. If the new power
mjr 98:4df3c0f7e707 1188 // level is non-zero, pass it through to the physical port, since
mjr 98:4df3c0f7e707 1189 // the client is allowed to change the power level during the
mjr 98:4df3c0f7e707 1190 // initial ON interval - they just can't turn it off entirely.
mjr 98:4df3c0f7e707 1191 // Set the state to 1 to indicate that the logical port is on.
mjr 98:4df3c0f7e707 1192 //
mjr 98:4df3c0f7e707 1193 // If the new level is zero, leave the underlying port at its
mjr 98:4df3c0f7e707 1194 // current power level, since we're not allowed to turn it off
mjr 98:4df3c0f7e707 1195 // during this period. Set the state to 2 to indicate that the
mjr 98:4df3c0f7e707 1196 // logical port is off even though the physical port has to stay
mjr 98:4df3c0f7e707 1197 // on for the remainder of the interval.
mjr 98:4df3c0f7e707 1198 if (level != 0)
mjr 98:4df3c0f7e707 1199 {
mjr 98:4df3c0f7e707 1200 // client is leaving the port on - pass through the new
mjr 98:4df3c0f7e707 1201 // power level and set state 1 (logically on)
mjr 98:4df3c0f7e707 1202 out->set(level);
mjr 98:4df3c0f7e707 1203 state = 1;
mjr 98:4df3c0f7e707 1204 }
mjr 98:4df3c0f7e707 1205 else
mjr 98:4df3c0f7e707 1206 {
mjr 98:4df3c0f7e707 1207 // Client is turning off the port - leave the underlying port
mjr 98:4df3c0f7e707 1208 // on at its current level and set state 2 (logically off).
mjr 98:4df3c0f7e707 1209 // When the minimum ON time expires, the polling routine will
mjr 98:4df3c0f7e707 1210 // see that we're logically off and will pass that through to
mjr 98:4df3c0f7e707 1211 // the underlying physical port. Until then, though, we have
mjr 98:4df3c0f7e707 1212 // to leave the physical port on to satisfy the minimum ON
mjr 98:4df3c0f7e707 1213 // time requirement.
mjr 98:4df3c0f7e707 1214 state = 2;
mjr 98:4df3c0f7e707 1215 }
mjr 98:4df3c0f7e707 1216 break;
mjr 98:4df3c0f7e707 1217
mjr 98:4df3c0f7e707 1218 case 3:
mjr 99:8139b0c274f4 1219 // We're after the minimum ON interval and before the maximum
mjr 99:8139b0c274f4 1220 // ON time limit. We can set any new level, including fully off.
mjr 99:8139b0c274f4 1221 // Pass the new power level through to the port.
mjr 98:4df3c0f7e707 1222 out->set(level);
mjr 98:4df3c0f7e707 1223
mjr 98:4df3c0f7e707 1224 // if the port is now off, return to state 0 (OFF)
mjr 98:4df3c0f7e707 1225 if (level == 0)
mjr 99:8139b0c274f4 1226 {
mjr 99:8139b0c274f4 1227 // return to the OFF state
mjr 99:8139b0c274f4 1228 state = 0;
mjr 99:8139b0c274f4 1229
mjr 99:8139b0c274f4 1230 // If we have a timer pending, remove it. A timer will be
mjr 99:8139b0c274f4 1231 // pending if we have a non-infinite maximum on time for the
mjr 99:8139b0c274f4 1232 // port.
mjr 99:8139b0c274f4 1233 for (int i = 0 ; i < nPending ; ++i)
mjr 99:8139b0c274f4 1234 {
mjr 99:8139b0c274f4 1235 // is this us?
mjr 99:8139b0c274f4 1236 if (pending[i] == this)
mjr 99:8139b0c274f4 1237 {
mjr 99:8139b0c274f4 1238 // remove myself by replacing the slot with the
mjr 99:8139b0c274f4 1239 // last list entry
mjr 99:8139b0c274f4 1240 pending[i] = pending[--nPending];
mjr 99:8139b0c274f4 1241
mjr 99:8139b0c274f4 1242 // no need to look any further
mjr 99:8139b0c274f4 1243 break;
mjr 99:8139b0c274f4 1244 }
mjr 99:8139b0c274f4 1245 }
mjr 99:8139b0c274f4 1246 }
mjr 99:8139b0c274f4 1247 break;
mjr 99:8139b0c274f4 1248
mjr 99:8139b0c274f4 1249 case 4:
mjr 99:8139b0c274f4 1250 // We're after the maximum ON time. The physical port stays off
mjr 99:8139b0c274f4 1251 // during this interval, so we don't pass any changes through to
mjr 99:8139b0c274f4 1252 // the physical port. When the client sets the level to 0, we
mjr 99:8139b0c274f4 1253 // turn off the logical port and reset to state 0.
mjr 99:8139b0c274f4 1254 if (level == 0)
mjr 98:4df3c0f7e707 1255 state = 0;
mjr 98:4df3c0f7e707 1256 break;
mjr 98:4df3c0f7e707 1257 }
mjr 98:4df3c0f7e707 1258 }
mjr 98:4df3c0f7e707 1259
mjr 98:4df3c0f7e707 1260 // Class initialization
mjr 98:4df3c0f7e707 1261 static void classInit(Config &cfg)
mjr 98:4df3c0f7e707 1262 {
mjr 98:4df3c0f7e707 1263 // Count the Minimum On Time outputs in the configuration. We
mjr 98:4df3c0f7e707 1264 // need to allocate enough pending timer list space to accommodate
mjr 98:4df3c0f7e707 1265 // all of these outputs.
mjr 98:4df3c0f7e707 1266 int n = 0;
mjr 98:4df3c0f7e707 1267 for (int i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 98:4df3c0f7e707 1268 {
mjr 98:4df3c0f7e707 1269 // if this port is active and marked as Flipper Logic, count it
mjr 98:4df3c0f7e707 1270 if (cfg.outPort[i].typ != PortTypeDisabled
mjr 99:8139b0c274f4 1271 && (cfg.outPort[i].flags & PortFlagChimeLogic) != 0)
mjr 98:4df3c0f7e707 1272 ++n;
mjr 98:4df3c0f7e707 1273 }
mjr 98:4df3c0f7e707 1274
mjr 98:4df3c0f7e707 1275 // allocate space for the pending timer list
mjr 99:8139b0c274f4 1276 pending = new LwChimeLogicOut*[n];
mjr 98:4df3c0f7e707 1277
mjr 98:4df3c0f7e707 1278 // there's nothing in the pending list yet
mjr 98:4df3c0f7e707 1279 nPending = 0;
mjr 98:4df3c0f7e707 1280
mjr 98:4df3c0f7e707 1281 // Start our shared timer. The epoch is arbitrary, since we only
mjr 98:4df3c0f7e707 1282 // use it to figure elapsed times.
mjr 98:4df3c0f7e707 1283 timer.start();
mjr 98:4df3c0f7e707 1284 }
mjr 98:4df3c0f7e707 1285
mjr 98:4df3c0f7e707 1286 // Check for ports with pending timers. The main routine should
mjr 98:4df3c0f7e707 1287 // call this on each iteration to process our state transitions.
mjr 98:4df3c0f7e707 1288 static void poll()
mjr 98:4df3c0f7e707 1289 {
mjr 98:4df3c0f7e707 1290 // note the current time
mjr 98:4df3c0f7e707 1291 uint32_t t = timer.read_us();
mjr 98:4df3c0f7e707 1292
mjr 98:4df3c0f7e707 1293 // go through the timer list
mjr 98:4df3c0f7e707 1294 for (int i = 0 ; i < nPending ; )
mjr 98:4df3c0f7e707 1295 {
mjr 98:4df3c0f7e707 1296 // get the port
mjr 99:8139b0c274f4 1297 LwChimeLogicOut *port = pending[i];
mjr 98:4df3c0f7e707 1298
mjr 98:4df3c0f7e707 1299 // assume we'll keep it
mjr 98:4df3c0f7e707 1300 bool remove = false;
mjr 98:4df3c0f7e707 1301
mjr 99:8139b0c274f4 1302 // check our state
mjr 99:8139b0c274f4 1303 switch (port->state)
mjr 98:4df3c0f7e707 1304 {
mjr 99:8139b0c274f4 1305 case 1: // initial minimum ON time, port logically on
mjr 99:8139b0c274f4 1306 case 2: // initial minimum ON time, port logically off
mjr 99:8139b0c274f4 1307 // check if the minimum ON time has elapsed
mjr 98:4df3c0f7e707 1308 if (uint32_t(t - port->t0) > port->minOnTime_us())
mjr 98:4df3c0f7e707 1309 {
mjr 98:4df3c0f7e707 1310 // This port has completed its initial ON interval, so
mjr 98:4df3c0f7e707 1311 // it advances to the next state.
mjr 98:4df3c0f7e707 1312 if (port->state == 1)
mjr 98:4df3c0f7e707 1313 {
mjr 99:8139b0c274f4 1314 // The port is logically on, so advance to state 3.
mjr 99:8139b0c274f4 1315 // The underlying port is already at its proper level,
mjr 99:8139b0c274f4 1316 // since we pass through non-zero power settings to the
mjr 99:8139b0c274f4 1317 // underlying port throughout the initial minimum time.
mjr 99:8139b0c274f4 1318 // The timer stays active into state 3.
mjr 98:4df3c0f7e707 1319 port->state = 3;
mjr 99:8139b0c274f4 1320
mjr 99:8139b0c274f4 1321 // Special case: maximum on time 0 means "infinite".
mjr 99:8139b0c274f4 1322 // There's no need for a timer in this case; we'll
mjr 99:8139b0c274f4 1323 // just stay in state 3 until the client turns the
mjr 99:8139b0c274f4 1324 // port off.
mjr 99:8139b0c274f4 1325 if (port->maxOnTime_us() == 0)
mjr 99:8139b0c274f4 1326 remove = true;
mjr 98:4df3c0f7e707 1327 }
mjr 98:4df3c0f7e707 1328 else
mjr 98:4df3c0f7e707 1329 {
mjr 98:4df3c0f7e707 1330 // The port was switched off by the client during the
mjr 98:4df3c0f7e707 1331 // minimum ON period. We haven't passed the OFF state
mjr 98:4df3c0f7e707 1332 // to the underlying port yet, because the port has to
mjr 98:4df3c0f7e707 1333 // stay on throughout the minimum ON period. So turn
mjr 98:4df3c0f7e707 1334 // the port off now.
mjr 98:4df3c0f7e707 1335 port->out->set(0);
mjr 98:4df3c0f7e707 1336
mjr 98:4df3c0f7e707 1337 // return to state 0 (OFF)
mjr 98:4df3c0f7e707 1338 port->state = 0;
mjr 99:8139b0c274f4 1339
mjr 99:8139b0c274f4 1340 // we're done with the timer
mjr 99:8139b0c274f4 1341 remove = true;
mjr 98:4df3c0f7e707 1342 }
mjr 99:8139b0c274f4 1343 }
mjr 99:8139b0c274f4 1344 break;
mjr 99:8139b0c274f4 1345
mjr 99:8139b0c274f4 1346 case 3: // between minimum ON time and maximum ON time
mjr 99:8139b0c274f4 1347 // check if the maximum ON time has expired
mjr 99:8139b0c274f4 1348 if (uint32_t(t - port->t0) > port->maxOnTime_us())
mjr 99:8139b0c274f4 1349 {
mjr 99:8139b0c274f4 1350 // The maximum ON time has expired. Turn off the physical
mjr 99:8139b0c274f4 1351 // port.
mjr 99:8139b0c274f4 1352 port->out->set(0);
mjr 98:4df3c0f7e707 1353
mjr 99:8139b0c274f4 1354 // Switch to state 4 (logically ON past maximum time)
mjr 99:8139b0c274f4 1355 port->state = 4;
mjr 99:8139b0c274f4 1356
mjr 99:8139b0c274f4 1357 // Remove the timer on this port. This port simply stays
mjr 99:8139b0c274f4 1358 // in state 4 until the client turns off the port.
mjr 98:4df3c0f7e707 1359 remove = true;
mjr 98:4df3c0f7e707 1360 }
mjr 99:8139b0c274f4 1361 break;
mjr 98:4df3c0f7e707 1362 }
mjr 98:4df3c0f7e707 1363
mjr 98:4df3c0f7e707 1364 // if desired, remove the port from the timer list
mjr 98:4df3c0f7e707 1365 if (remove)
mjr 98:4df3c0f7e707 1366 {
mjr 98:4df3c0f7e707 1367 // Remove the list entry by overwriting the slot with
mjr 98:4df3c0f7e707 1368 // the last entry in the list.
mjr 98:4df3c0f7e707 1369 pending[i] = pending[--nPending];
mjr 98:4df3c0f7e707 1370
mjr 98:4df3c0f7e707 1371 // Note that we don't increment the loop counter, since
mjr 98:4df3c0f7e707 1372 // we now need to revisit this same slot.
mjr 98:4df3c0f7e707 1373 }
mjr 98:4df3c0f7e707 1374 else
mjr 98:4df3c0f7e707 1375 {
mjr 98:4df3c0f7e707 1376 // we're keeping this item; move on to the next one
mjr 98:4df3c0f7e707 1377 ++i;
mjr 98:4df3c0f7e707 1378 }
mjr 98:4df3c0f7e707 1379 }
mjr 98:4df3c0f7e707 1380 }
mjr 98:4df3c0f7e707 1381
mjr 98:4df3c0f7e707 1382 protected:
mjr 98:4df3c0f7e707 1383 // underlying physical output
mjr 98:4df3c0f7e707 1384 LwOut *out;
mjr 98:4df3c0f7e707 1385
mjr 98:4df3c0f7e707 1386 // Timestamp on 'timer' of start of full-power interval. We set this
mjr 98:4df3c0f7e707 1387 // to the current 'timer' timestamp when entering state 1.
mjr 98:4df3c0f7e707 1388 uint32_t t0;
mjr 98:4df3c0f7e707 1389
mjr 98:4df3c0f7e707 1390 // Current port state:
mjr 98:4df3c0f7e707 1391 //
mjr 98:4df3c0f7e707 1392 // 0 = off
mjr 99:8139b0c274f4 1393 // 1 = in initial minimum ON interval, logical port is on
mjr 99:8139b0c274f4 1394 // 2 = in initial minimum ON interval, logical port is off
mjr 99:8139b0c274f4 1395 // 3 = in interval between minimum and maximum ON times
mjr 99:8139b0c274f4 1396 // 4 = after the maximum ON interval
mjr 99:8139b0c274f4 1397 //
mjr 99:8139b0c274f4 1398 // The "logical" on/off state of the port is the state set by the
mjr 99:8139b0c274f4 1399 // client. The "physical" state is the state of the underlying port.
mjr 99:8139b0c274f4 1400 // The relationships between logical and physical port state, and the
mjr 99:8139b0c274f4 1401 // effects of updates by the client, are as follows:
mjr 99:8139b0c274f4 1402 //
mjr 99:8139b0c274f4 1403 // State | Logical | Physical | Client set on | Client set off
mjr 99:8139b0c274f4 1404 // -----------------------------------------------------------
mjr 99:8139b0c274f4 1405 // 0 | Off | Off | phys on, -> 1 | no effect
mjr 99:8139b0c274f4 1406 // 1 | On | On | no effect | -> 2
mjr 99:8139b0c274f4 1407 // 2 | Off | On | -> 1 | no effect
mjr 99:8139b0c274f4 1408 // 3 | On | On | no effect | phys off, -> 0
mjr 99:8139b0c274f4 1409 // 4 | On | On | no effect | phys off, -> 0
mjr 99:8139b0c274f4 1410 //
mjr 99:8139b0c274f4 1411 // The polling routine makes the following transitions when the current
mjr 99:8139b0c274f4 1412 // time limit expires:
mjr 99:8139b0c274f4 1413 //
mjr 99:8139b0c274f4 1414 // 1: at end of minimum ON, -> 3 (or 4 if max == infinity)
mjr 99:8139b0c274f4 1415 // 2: at end of minimum ON, port off, -> 0
mjr 99:8139b0c274f4 1416 // 3: at end of maximum ON, port off, -> 4
mjr 98:4df3c0f7e707 1417 //
mjr 98:4df3c0f7e707 1418 uint8_t state;
mjr 98:4df3c0f7e707 1419
mjr 99:8139b0c274f4 1420 // Configuration parameters byte. This encodes the minimum and maximum
mjr 99:8139b0c274f4 1421 // ON times.
mjr 99:8139b0c274f4 1422 uint8_t params;
mjr 98:4df3c0f7e707 1423
mjr 98:4df3c0f7e707 1424 // Timer. This is a shared timer for all of the minimum ON time ports.
mjr 98:4df3c0f7e707 1425 // When we transition from OFF to ON, we note the current time on this
mjr 98:4df3c0f7e707 1426 // timer to establish the start of our minimum ON period.
mjr 98:4df3c0f7e707 1427 static Timer timer;
mjr 98:4df3c0f7e707 1428
mjr 98:4df3c0f7e707 1429 // translaton table from timing parameter in config to minimum ON time
mjr 98:4df3c0f7e707 1430 static const uint32_t paramToTime_us[];
mjr 98:4df3c0f7e707 1431
mjr 99:8139b0c274f4 1432 // Figure the minimum ON time. The minimum ON time is given by the
mjr 99:8139b0c274f4 1433 // low-order 4 bits of the parameters byte, which serves as an index
mjr 99:8139b0c274f4 1434 // into our time table.
mjr 99:8139b0c274f4 1435 inline uint32_t minOnTime_us() const { return paramToTime_us[params & 0x0F]; }
mjr 99:8139b0c274f4 1436
mjr 99:8139b0c274f4 1437 // Figure the maximum ON time. The maximum time is the high 4 bits
mjr 99:8139b0c274f4 1438 // of the parameters byte. This is an index into our time table, but
mjr 99:8139b0c274f4 1439 // 0 has the special meaning "infinite".
mjr 99:8139b0c274f4 1440 inline uint32_t maxOnTime_us() const { return paramToTime_us[((params >> 4) & 0x0F)]; }
mjr 98:4df3c0f7e707 1441
mjr 98:4df3c0f7e707 1442 // Pending timer list. Whenever one of our ports transitions from OFF
mjr 98:4df3c0f7e707 1443 // to ON, we add it to this list. We scan this list in our polling
mjr 98:4df3c0f7e707 1444 // routine to find ports that have reached the ends of their initial
mjr 98:4df3c0f7e707 1445 // ON intervals.
mjr 99:8139b0c274f4 1446 static LwChimeLogicOut **pending;
mjr 98:4df3c0f7e707 1447 static uint8_t nPending;
mjr 98:4df3c0f7e707 1448 };
mjr 98:4df3c0f7e707 1449
mjr 98:4df3c0f7e707 1450 // Min Time Out statics
mjr 99:8139b0c274f4 1451 Timer LwChimeLogicOut::timer;
mjr 99:8139b0c274f4 1452 LwChimeLogicOut **LwChimeLogicOut::pending;
mjr 99:8139b0c274f4 1453 uint8_t LwChimeLogicOut::nPending;
mjr 99:8139b0c274f4 1454 const uint32_t LwChimeLogicOut::paramToTime_us[] = {
mjr 99:8139b0c274f4 1455 0, // for the max time, this means "infinite"
mjr 98:4df3c0f7e707 1456 1000,
mjr 98:4df3c0f7e707 1457 2000,
mjr 98:4df3c0f7e707 1458 5000,
mjr 98:4df3c0f7e707 1459 10000,
mjr 98:4df3c0f7e707 1460 20000,
mjr 98:4df3c0f7e707 1461 40000,
mjr 98:4df3c0f7e707 1462 80000,
mjr 98:4df3c0f7e707 1463 100000,
mjr 98:4df3c0f7e707 1464 200000,
mjr 98:4df3c0f7e707 1465 300000,
mjr 98:4df3c0f7e707 1466 400000,
mjr 98:4df3c0f7e707 1467 500000,
mjr 98:4df3c0f7e707 1468 600000,
mjr 98:4df3c0f7e707 1469 700000,
mjr 98:4df3c0f7e707 1470 800000
mjr 98:4df3c0f7e707 1471 };
mjr 89:c43cd923401c 1472
mjr 35:e959ffba78fd 1473 //
mjr 35:e959ffba78fd 1474 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 1475 // assignments set in config.h.
mjr 33:d832bcab089e 1476 //
mjr 35:e959ffba78fd 1477 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 1478 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 1479 {
mjr 35:e959ffba78fd 1480 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 1481 {
mjr 53:9b2611964afc 1482 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 1483 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 1484 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 1485 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 1486 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 1487 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 1488 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 1489 }
mjr 35:e959ffba78fd 1490 }
mjr 26:cb71c4af2912 1491
mjr 40:cc0d9814522b 1492 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 1493 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 1494 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 1495 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 1496 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 1497 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 1498 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 1499 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 1500 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 1501 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 1502 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 1503 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 1504 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 1505 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 1506 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 1507 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 1508 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 1509 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 1510 };
mjr 40:cc0d9814522b 1511
mjr 40:cc0d9814522b 1512 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 1513 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 1514 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 1515 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 1516 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 1517 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 1518 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 1519 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 1520 // are always 8 bits.
mjr 40:cc0d9814522b 1521 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 1522 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 1523 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 1524 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 1525 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 1526 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 1527 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 1528 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 1529 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 1530 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 1531 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 1532 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 1533 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 1534 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 1535 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 1536 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 1537 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 1538 };
mjr 40:cc0d9814522b 1539
mjr 26:cb71c4af2912 1540 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 1541 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 1542 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 1543 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 1544 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 1545 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 1546 {
mjr 26:cb71c4af2912 1547 public:
mjr 60:f38da020aa13 1548 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1549 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 1550 {
mjr 26:cb71c4af2912 1551 if (val != prv)
mjr 40:cc0d9814522b 1552 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 1553 }
mjr 60:f38da020aa13 1554 uint8_t idx;
mjr 40:cc0d9814522b 1555 uint8_t prv;
mjr 26:cb71c4af2912 1556 };
mjr 26:cb71c4af2912 1557
mjr 40:cc0d9814522b 1558 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 1559 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 1560 {
mjr 40:cc0d9814522b 1561 public:
mjr 60:f38da020aa13 1562 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1563 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 1564 {
mjr 40:cc0d9814522b 1565 if (val != prv)
mjr 40:cc0d9814522b 1566 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 1567 }
mjr 60:f38da020aa13 1568 uint8_t idx;
mjr 40:cc0d9814522b 1569 uint8_t prv;
mjr 40:cc0d9814522b 1570 };
mjr 40:cc0d9814522b 1571
mjr 87:8d35c74403af 1572 //
mjr 87:8d35c74403af 1573 // TLC59116 interface object
mjr 87:8d35c74403af 1574 //
mjr 87:8d35c74403af 1575 TLC59116 *tlc59116 = 0;
mjr 87:8d35c74403af 1576 void init_tlc59116(Config &cfg)
mjr 87:8d35c74403af 1577 {
mjr 87:8d35c74403af 1578 // Create the interface if any chips are enabled
mjr 87:8d35c74403af 1579 if (cfg.tlc59116.chipMask != 0)
mjr 87:8d35c74403af 1580 {
mjr 87:8d35c74403af 1581 // set up the interface
mjr 87:8d35c74403af 1582 tlc59116 = new TLC59116(
mjr 87:8d35c74403af 1583 wirePinName(cfg.tlc59116.sda),
mjr 87:8d35c74403af 1584 wirePinName(cfg.tlc59116.scl),
mjr 87:8d35c74403af 1585 wirePinName(cfg.tlc59116.reset));
mjr 87:8d35c74403af 1586
mjr 87:8d35c74403af 1587 // initialize the chips
mjr 87:8d35c74403af 1588 tlc59116->init();
mjr 87:8d35c74403af 1589 }
mjr 87:8d35c74403af 1590 }
mjr 87:8d35c74403af 1591
mjr 87:8d35c74403af 1592 // LwOut class for TLC59116 outputs. The 'addr' value in the constructor
mjr 87:8d35c74403af 1593 // is low 4 bits of the chip's I2C address; this is the part of the address
mjr 87:8d35c74403af 1594 // that's configurable per chip. 'port' is the output number on the chip
mjr 87:8d35c74403af 1595 // (0-15).
mjr 87:8d35c74403af 1596 //
mjr 87:8d35c74403af 1597 // Note that we don't need a separate gamma-corrected subclass for this
mjr 87:8d35c74403af 1598 // output type, since there's no loss of precision with the standard layered
mjr 87:8d35c74403af 1599 // gamma (it emits 8-bit values, and we take 8-bit inputs).
mjr 87:8d35c74403af 1600 class Lw59116Out: public LwOut
mjr 87:8d35c74403af 1601 {
mjr 87:8d35c74403af 1602 public:
mjr 87:8d35c74403af 1603 Lw59116Out(uint8_t addr, uint8_t port) : addr(addr), port(port) { prv = 0; }
mjr 87:8d35c74403af 1604 virtual void set(uint8_t val)
mjr 87:8d35c74403af 1605 {
mjr 87:8d35c74403af 1606 if (val != prv)
mjr 87:8d35c74403af 1607 tlc59116->set(addr, port, prv = val);
mjr 87:8d35c74403af 1608 }
mjr 87:8d35c74403af 1609
mjr 87:8d35c74403af 1610 protected:
mjr 87:8d35c74403af 1611 uint8_t addr;
mjr 87:8d35c74403af 1612 uint8_t port;
mjr 87:8d35c74403af 1613 uint8_t prv;
mjr 87:8d35c74403af 1614 };
mjr 87:8d35c74403af 1615
mjr 87:8d35c74403af 1616
mjr 87:8d35c74403af 1617 //
mjr 34:6b981a2afab7 1618 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 1619 // config.h.
mjr 87:8d35c74403af 1620 //
mjr 35:e959ffba78fd 1621 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 1622
mjr 35:e959ffba78fd 1623 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 1624 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 1625 {
mjr 35:e959ffba78fd 1626 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 1627 {
mjr 53:9b2611964afc 1628 hc595 = new HC595(
mjr 53:9b2611964afc 1629 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 1630 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 1631 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 1632 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 1633 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 1634 hc595->init();
mjr 35:e959ffba78fd 1635 hc595->update();
mjr 35:e959ffba78fd 1636 }
mjr 35:e959ffba78fd 1637 }
mjr 34:6b981a2afab7 1638
mjr 34:6b981a2afab7 1639 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 1640 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 1641 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 1642 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 1643 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 1644 class Lw595Out: public LwOut
mjr 33:d832bcab089e 1645 {
mjr 33:d832bcab089e 1646 public:
mjr 60:f38da020aa13 1647 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1648 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 1649 {
mjr 34:6b981a2afab7 1650 if (val != prv)
mjr 40:cc0d9814522b 1651 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 1652 }
mjr 60:f38da020aa13 1653 uint8_t idx;
mjr 40:cc0d9814522b 1654 uint8_t prv;
mjr 33:d832bcab089e 1655 };
mjr 33:d832bcab089e 1656
mjr 26:cb71c4af2912 1657
mjr 40:cc0d9814522b 1658
mjr 64:ef7ca92dff36 1659 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 1660 // normalized to 0.0 to 1.0 scale.
mjr 74:822a92bc11d2 1661 static const float dof_to_pwm[] = {
mjr 64:ef7ca92dff36 1662 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 1663 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 1664 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 1665 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 1666 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 1667 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 1668 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 1669 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 1670 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 1671 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 1672 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 1673 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 1674 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 1675 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 1676 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 1677 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 1678 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 1679 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 1680 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 1681 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 1682 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 1683 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 1684 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 1685 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 1686 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 1687 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 1688 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 1689 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 1690 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 1691 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 1692 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 1693 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 1694 };
mjr 26:cb71c4af2912 1695
mjr 64:ef7ca92dff36 1696
mjr 92:f264fbaa1be5 1697 // Conversion table for 8-bit DOF level to pulse width, with gamma correction
mjr 92:f264fbaa1be5 1698 // pre-calculated. The values are normalized duty cycles from 0.0 to 1.0.
mjr 92:f264fbaa1be5 1699 // Note that we could use the layered gamma output on top of the regular
mjr 92:f264fbaa1be5 1700 // LwPwmOut class for this instead of a separate table, but we get much better
mjr 92:f264fbaa1be5 1701 // precision with a dedicated table, because we apply gamma correction to the
mjr 92:f264fbaa1be5 1702 // actual duty cycle values (as 'float') rather than the 8-bit DOF values.
mjr 64:ef7ca92dff36 1703 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 1704 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 1705 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 1706 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 1707 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 1708 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 1709 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 1710 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 1711 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 1712 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 1713 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 1714 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 1715 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 1716 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 1717 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 1718 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 1719 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 1720 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 1721 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 1722 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 1723 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 1724 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 1725 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 1726 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 1727 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 1728 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 1729 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 1730 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 1731 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 1732 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 1733 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 1734 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 1735 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 1736 };
mjr 64:ef7ca92dff36 1737
mjr 77:0b96f6867312 1738 // Polled-update PWM output list
mjr 74:822a92bc11d2 1739 //
mjr 77:0b96f6867312 1740 // This is a workaround for a KL25Z hardware bug/limitation. The bug (more
mjr 77:0b96f6867312 1741 // about this below) is that we can't write to a PWM output "value" register
mjr 77:0b96f6867312 1742 // more than once per PWM cycle; if we do, outputs after the first are lost.
mjr 77:0b96f6867312 1743 // The value register controls the duty cycle, so it's what you have to write
mjr 77:0b96f6867312 1744 // if you want to update the brightness of an output.
mjr 74:822a92bc11d2 1745 //
mjr 92:f264fbaa1be5 1746 // The symptom of the problem, if it's not worked around somehow, is that
mjr 92:f264fbaa1be5 1747 // an output will get "stuck" due to a missed write. This is especially
mjr 92:f264fbaa1be5 1748 // noticeable during a series of updates such as a fade. If the last
mjr 92:f264fbaa1be5 1749 // couple of updates in a fade are lost, the output will get stuck at some
mjr 92:f264fbaa1be5 1750 // value above or below the desired final value. The stuck setting will
mjr 92:f264fbaa1be5 1751 // persist until the output is deliberately changed again later.
mjr 92:f264fbaa1be5 1752 //
mjr 92:f264fbaa1be5 1753 // Our solution: Simply repeat all PWM updates periodically. This way, any
mjr 92:f264fbaa1be5 1754 // lost write will *eventually* take hold on one of the repeats. Repeats of
mjr 92:f264fbaa1be5 1755 // the same value won't change anything and thus won't be noticeable. We do
mjr 92:f264fbaa1be5 1756 // these periodic updates during the main loop, which makes them very low
mjr 92:f264fbaa1be5 1757 // overhead (there's no interrupt overhead; we just do them when convenient
mjr 92:f264fbaa1be5 1758 // in the main loop), and also makes them very frequent. The frequency
mjr 92:f264fbaa1be5 1759 // is crucial because it ensures that updates will never be lost for long
mjr 92:f264fbaa1be5 1760 // enough to become noticeable.
mjr 92:f264fbaa1be5 1761 //
mjr 92:f264fbaa1be5 1762 // The mbed library has its own, different solution to this bug, but the
mjr 92:f264fbaa1be5 1763 // mbed solution isn't really a solution at all because it creates a separate
mjr 100:1ff35c07217c 1764 // problem of its own. The mbed approach is to reset the TPM "count" register
mjr 92:f264fbaa1be5 1765 // on every value register write. The count reset truncates the current
mjr 92:f264fbaa1be5 1766 // PWM cycle, which bypasses the hardware problem. Remember, the hardware
mjr 92:f264fbaa1be5 1767 // problem is that you can only write once per cycle; the mbed "solution" gets
mjr 92:f264fbaa1be5 1768 // around that by making sure the cycle ends immediately after the write.
mjr 92:f264fbaa1be5 1769 // The problem with this approach is that the truncated cycle causes visible
mjr 92:f264fbaa1be5 1770 // flicker if the output is connected to an LED. This is particularly
mjr 92:f264fbaa1be5 1771 // noticeable during fades, when we're updating the value register repeatedly
mjr 92:f264fbaa1be5 1772 // and rapidly: an attempt to fade from fully on to fully off causes rapid
mjr 92:f264fbaa1be5 1773 // fluttering and flashing rather than a smooth brightness fade. That's why
mjr 92:f264fbaa1be5 1774 // I had to come up with something different - the mbed solution just trades
mjr 92:f264fbaa1be5 1775 // one annoying bug for another that's just as bad.
mjr 92:f264fbaa1be5 1776 //
mjr 92:f264fbaa1be5 1777 // The hardware bug, by the way, is a case of good intentions gone bad.
mjr 92:f264fbaa1be5 1778 // The whole point of the staging register is to make things easier for
mjr 92:f264fbaa1be5 1779 // us software writers. In most PWM hardware, software has to coordinate
mjr 92:f264fbaa1be5 1780 // with the PWM duty cycle when updating registers to avoid a glitch that
mjr 92:f264fbaa1be5 1781 // you'd get by scribbling to the duty cycle register mid-cycle. The
mjr 92:f264fbaa1be5 1782 // staging register solves this by letting the software write an update at
mjr 92:f264fbaa1be5 1783 // any time, knowing that the hardware will apply the update at exactly the
mjr 92:f264fbaa1be5 1784 // end of the cycle, ensuring glitch-free updates. It's a great design,
mjr 92:f264fbaa1be5 1785 // except that it doesn't quite work. The problem is that they implemented
mjr 92:f264fbaa1be5 1786 // this clever staging register as a one-element FIFO that refuses any more
mjr 92:f264fbaa1be5 1787 // writes when full. That is, writing a value to the FIFO fills it; once
mjr 92:f264fbaa1be5 1788 // full, it ignores writes until it gets emptied out. How's it emptied out?
mjr 92:f264fbaa1be5 1789 // By the hardware moving the staged value to the real register. Sadly, they
mjr 92:f264fbaa1be5 1790 // didn't provide any way for the software to clear the register, and no way
mjr 92:f264fbaa1be5 1791 // to even tell that it's full. So we don't have glitches on write, but we're
mjr 92:f264fbaa1be5 1792 // back to the original problem that the software has to be aware of the PWM
mjr 92:f264fbaa1be5 1793 // cycle timing, because the only way for the software to know that a write
mjr 92:f264fbaa1be5 1794 // actually worked is to know that it's been at least one PWM cycle since the
mjr 92:f264fbaa1be5 1795 // last write. That largely defeats the whole purpose of the staging register,
mjr 92:f264fbaa1be5 1796 // since the whole point was to free software writers of these timing
mjr 92:f264fbaa1be5 1797 // considerations. It's still an improvement over no staging register at
mjr 92:f264fbaa1be5 1798 // all, since we at least don't have to worry about glitches, but it leaves
mjr 92:f264fbaa1be5 1799 // us with this somewhat similar hassle.
mjr 74:822a92bc11d2 1800 //
mjr 77:0b96f6867312 1801 // So here we have our list of PWM outputs that need to be polled for updates.
mjr 77:0b96f6867312 1802 // The KL25Z hardware only has 10 PWM channels, so we only need a fixed set
mjr 77:0b96f6867312 1803 // of polled items.
mjr 74:822a92bc11d2 1804 static int numPolledPwm;
mjr 74:822a92bc11d2 1805 static class LwPwmOut *polledPwm[10];
mjr 74:822a92bc11d2 1806
mjr 74:822a92bc11d2 1807 // LwOut class for a PWM-capable GPIO port.
mjr 6:cc35eb643e8f 1808 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 1809 {
mjr 6:cc35eb643e8f 1810 public:
mjr 43:7a6364d82a41 1811 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1812 {
mjr 77:0b96f6867312 1813 // add myself to the list of polled outputs for periodic updates
mjr 77:0b96f6867312 1814 if (numPolledPwm < countof(polledPwm))
mjr 74:822a92bc11d2 1815 polledPwm[numPolledPwm++] = this;
mjr 93:177832c29041 1816
mjr 94:0476b3e2b996 1817 // IMPORTANT: Do not set the PWM period (frequency) here explicitly.
mjr 94:0476b3e2b996 1818 // We instead want to accept the current setting for the TPM unit
mjr 94:0476b3e2b996 1819 // we're assigned to. The KL25Z hardware can only set the period at
mjr 94:0476b3e2b996 1820 // the TPM unit level, not per channel, so if we changed the frequency
mjr 94:0476b3e2b996 1821 // here, we'd change it for everything attached to our TPM unit. LW
mjr 94:0476b3e2b996 1822 // outputs don't care about frequency other than that it's fast enough
mjr 94:0476b3e2b996 1823 // that attached LEDs won't flicker. Some other PWM users (IR remote,
mjr 94:0476b3e2b996 1824 // TLC5940) DO care about exact frequencies, because they use the PWM
mjr 94:0476b3e2b996 1825 // as a signal generator rather than merely for brightness control.
mjr 94:0476b3e2b996 1826 // If we changed the frequency here, we could clobber one of those
mjr 94:0476b3e2b996 1827 // carefully chosen frequencies and break the other subsystem. So
mjr 94:0476b3e2b996 1828 // we need to be the "free variable" here and accept whatever setting
mjr 94:0476b3e2b996 1829 // is currently on our assigned unit. To minimize flicker, the main()
mjr 94:0476b3e2b996 1830 // entrypoint sets a default PWM rate of 1kHz on all channels. All
mjr 94:0476b3e2b996 1831 // of the other subsystems that might set specific frequencies will
mjr 94:0476b3e2b996 1832 // set much high frequencies, so that should only be good for us.
mjr 94:0476b3e2b996 1833
mjr 94:0476b3e2b996 1834 // set the initial brightness value
mjr 77:0b96f6867312 1835 set(initVal);
mjr 43:7a6364d82a41 1836 }
mjr 74:822a92bc11d2 1837
mjr 40:cc0d9814522b 1838 virtual void set(uint8_t val)
mjr 74:822a92bc11d2 1839 {
mjr 77:0b96f6867312 1840 // save the new value
mjr 74:822a92bc11d2 1841 this->val = val;
mjr 77:0b96f6867312 1842
mjr 77:0b96f6867312 1843 // commit it to the hardware
mjr 77:0b96f6867312 1844 commit();
mjr 13:72dda449c3c0 1845 }
mjr 74:822a92bc11d2 1846
mjr 74:822a92bc11d2 1847 // handle periodic update polling
mjr 74:822a92bc11d2 1848 void poll()
mjr 74:822a92bc11d2 1849 {
mjr 77:0b96f6867312 1850 commit();
mjr 74:822a92bc11d2 1851 }
mjr 74:822a92bc11d2 1852
mjr 74:822a92bc11d2 1853 protected:
mjr 77:0b96f6867312 1854 virtual void commit()
mjr 74:822a92bc11d2 1855 {
mjr 74:822a92bc11d2 1856 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1857 p.glitchFreeWrite(dof_to_pwm[val]);
mjr 74:822a92bc11d2 1858 }
mjr 74:822a92bc11d2 1859
mjr 77:0b96f6867312 1860 NewPwmOut p;
mjr 77:0b96f6867312 1861 uint8_t val;
mjr 6:cc35eb643e8f 1862 };
mjr 26:cb71c4af2912 1863
mjr 74:822a92bc11d2 1864 // Gamma corrected PWM GPIO output. This works exactly like the regular
mjr 74:822a92bc11d2 1865 // PWM output, but translates DOF values through the gamma-corrected
mjr 74:822a92bc11d2 1866 // table instead of the regular linear table.
mjr 64:ef7ca92dff36 1867 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1868 {
mjr 64:ef7ca92dff36 1869 public:
mjr 64:ef7ca92dff36 1870 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1871 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1872 {
mjr 64:ef7ca92dff36 1873 }
mjr 74:822a92bc11d2 1874
mjr 74:822a92bc11d2 1875 protected:
mjr 77:0b96f6867312 1876 virtual void commit()
mjr 64:ef7ca92dff36 1877 {
mjr 74:822a92bc11d2 1878 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1879 p.glitchFreeWrite(dof_to_gamma_pwm[val]);
mjr 64:ef7ca92dff36 1880 }
mjr 64:ef7ca92dff36 1881 };
mjr 64:ef7ca92dff36 1882
mjr 74:822a92bc11d2 1883 // poll the PWM outputs
mjr 74:822a92bc11d2 1884 Timer polledPwmTimer;
mjr 76:7f5912b6340e 1885 uint64_t polledPwmTotalTime, polledPwmRunCount;
mjr 74:822a92bc11d2 1886 void pollPwmUpdates()
mjr 74:822a92bc11d2 1887 {
mjr 94:0476b3e2b996 1888 // If it's been long enough since the last update, do another update.
mjr 94:0476b3e2b996 1889 // Note that the time limit is fairly arbitrary: it has to be at least
mjr 94:0476b3e2b996 1890 // 1.5X the PWM period, so that we can be sure that at least one PWM
mjr 94:0476b3e2b996 1891 // period has elapsed since the last update, but there's no hard upper
mjr 94:0476b3e2b996 1892 // bound. Instead, it only has to be short enough that fades don't
mjr 94:0476b3e2b996 1893 // become noticeably chunky. The competing interest is that we don't
mjr 94:0476b3e2b996 1894 // want to do this more often than necessary to provide incremental
mjr 94:0476b3e2b996 1895 // benefit, because the polling adds overhead to the main loop and
mjr 94:0476b3e2b996 1896 // takes time away from other tasks we could be performing. The
mjr 94:0476b3e2b996 1897 // shortest time with practical benefit is probably around 50-60Hz,
mjr 94:0476b3e2b996 1898 // since that gives us "video rate" granularity in fades. Anything
mjr 94:0476b3e2b996 1899 // faster wouldn't probably make fades look any smoother to a human
mjr 94:0476b3e2b996 1900 // viewer.
mjr 94:0476b3e2b996 1901 if (polledPwmTimer.read_us() >= 15000)
mjr 74:822a92bc11d2 1902 {
mjr 74:822a92bc11d2 1903 // time the run for statistics collection
mjr 74:822a92bc11d2 1904 IF_DIAG(
mjr 74:822a92bc11d2 1905 Timer t;
mjr 74:822a92bc11d2 1906 t.start();
mjr 74:822a92bc11d2 1907 )
mjr 74:822a92bc11d2 1908
mjr 74:822a92bc11d2 1909 // poll each output
mjr 74:822a92bc11d2 1910 for (int i = numPolledPwm ; i > 0 ; )
mjr 74:822a92bc11d2 1911 polledPwm[--i]->poll();
mjr 74:822a92bc11d2 1912
mjr 74:822a92bc11d2 1913 // reset the timer for the next cycle
mjr 74:822a92bc11d2 1914 polledPwmTimer.reset();
mjr 74:822a92bc11d2 1915
mjr 74:822a92bc11d2 1916 // collect statistics
mjr 74:822a92bc11d2 1917 IF_DIAG(
mjr 76:7f5912b6340e 1918 polledPwmTotalTime += t.read_us();
mjr 74:822a92bc11d2 1919 polledPwmRunCount += 1;
mjr 74:822a92bc11d2 1920 )
mjr 74:822a92bc11d2 1921 }
mjr 74:822a92bc11d2 1922 }
mjr 64:ef7ca92dff36 1923
mjr 26:cb71c4af2912 1924 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1925 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1926 {
mjr 6:cc35eb643e8f 1927 public:
mjr 43:7a6364d82a41 1928 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1929 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1930 {
mjr 13:72dda449c3c0 1931 if (val != prv)
mjr 40:cc0d9814522b 1932 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1933 }
mjr 6:cc35eb643e8f 1934 DigitalOut p;
mjr 40:cc0d9814522b 1935 uint8_t prv;
mjr 6:cc35eb643e8f 1936 };
mjr 26:cb71c4af2912 1937
mjr 29:582472d0bc57 1938 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1939 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1940 // port n (0-based).
mjr 35:e959ffba78fd 1941 //
mjr 35:e959ffba78fd 1942 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1943 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1944 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1945 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1946 // 74HC595 ports).
mjr 33:d832bcab089e 1947 static int numOutputs;
mjr 33:d832bcab089e 1948 static LwOut **lwPin;
mjr 33:d832bcab089e 1949
mjr 38:091e511ce8a0 1950 // create a single output pin
mjr 53:9b2611964afc 1951 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1952 {
mjr 38:091e511ce8a0 1953 // get this item's values
mjr 38:091e511ce8a0 1954 int typ = pc.typ;
mjr 38:091e511ce8a0 1955 int pin = pc.pin;
mjr 38:091e511ce8a0 1956 int flags = pc.flags;
mjr 40:cc0d9814522b 1957 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1958 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1959 int gamma = flags & PortFlagGamma;
mjr 89:c43cd923401c 1960 int flipperLogic = flags & PortFlagFlipperLogic;
mjr 99:8139b0c274f4 1961 int chimeLogic = flags & PortFlagChimeLogic;
mjr 89:c43cd923401c 1962
mjr 89:c43cd923401c 1963 // cancel gamma on flipper logic ports
mjr 89:c43cd923401c 1964 if (flipperLogic)
mjr 89:c43cd923401c 1965 gamma = false;
mjr 38:091e511ce8a0 1966
mjr 38:091e511ce8a0 1967 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1968 LwOut *lwp;
mjr 38:091e511ce8a0 1969 switch (typ)
mjr 38:091e511ce8a0 1970 {
mjr 38:091e511ce8a0 1971 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1972 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1973 if (pin != 0)
mjr 64:ef7ca92dff36 1974 {
mjr 64:ef7ca92dff36 1975 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 1976 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 1977 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 1978 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 1979 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 1980 {
mjr 64:ef7ca92dff36 1981 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 1982 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 1983
mjr 64:ef7ca92dff36 1984 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 1985 gamma = false;
mjr 64:ef7ca92dff36 1986 }
mjr 64:ef7ca92dff36 1987 else
mjr 64:ef7ca92dff36 1988 {
mjr 64:ef7ca92dff36 1989 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 1990 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 1991 }
mjr 64:ef7ca92dff36 1992 }
mjr 48:058ace2aed1d 1993 else
mjr 48:058ace2aed1d 1994 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1995 break;
mjr 38:091e511ce8a0 1996
mjr 38:091e511ce8a0 1997 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1998 // Digital GPIO port
mjr 48:058ace2aed1d 1999 if (pin != 0)
mjr 48:058ace2aed1d 2000 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 2001 else
mjr 48:058ace2aed1d 2002 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2003 break;
mjr 38:091e511ce8a0 2004
mjr 38:091e511ce8a0 2005 case PortTypeTLC5940:
mjr 38:091e511ce8a0 2006 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 2007 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 2008 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 2009 {
mjr 40:cc0d9814522b 2010 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 2011 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 2012 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 2013 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 2014 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 2015 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 2016 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 2017 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 2018 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 2019 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 2020 // for this unlikely case.
mjr 40:cc0d9814522b 2021 if (gamma && !activeLow)
mjr 40:cc0d9814522b 2022 {
mjr 40:cc0d9814522b 2023 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 2024 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 2025
mjr 40:cc0d9814522b 2026 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 2027 gamma = false;
mjr 40:cc0d9814522b 2028 }
mjr 40:cc0d9814522b 2029 else
mjr 40:cc0d9814522b 2030 {
mjr 40:cc0d9814522b 2031 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 2032 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 2033 }
mjr 40:cc0d9814522b 2034 }
mjr 38:091e511ce8a0 2035 else
mjr 40:cc0d9814522b 2036 {
mjr 40:cc0d9814522b 2037 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 2038 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 2039 }
mjr 38:091e511ce8a0 2040 break;
mjr 38:091e511ce8a0 2041
mjr 38:091e511ce8a0 2042 case PortType74HC595:
mjr 87:8d35c74403af 2043 // 74HC595 port (if we don't have an HC595 controller object, or it's not
mjr 87:8d35c74403af 2044 // a valid output number, create a virtual port)
mjr 38:091e511ce8a0 2045 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 2046 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 2047 else
mjr 38:091e511ce8a0 2048 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2049 break;
mjr 87:8d35c74403af 2050
mjr 87:8d35c74403af 2051 case PortTypeTLC59116:
mjr 87:8d35c74403af 2052 // TLC59116 port. The pin number in the config encodes the chip address
mjr 87:8d35c74403af 2053 // in the high 4 bits and the output number on the chip in the low 4 bits.
mjr 87:8d35c74403af 2054 // There's no gamma-corrected version of this output handler, so we don't
mjr 87:8d35c74403af 2055 // need to worry about that here; just use the layered gamma as needed.
mjr 87:8d35c74403af 2056 if (tlc59116 != 0)
mjr 87:8d35c74403af 2057 lwp = new Lw59116Out((pin >> 4) & 0x0F, pin & 0x0F);
mjr 87:8d35c74403af 2058 break;
mjr 38:091e511ce8a0 2059
mjr 38:091e511ce8a0 2060 case PortTypeVirtual:
mjr 43:7a6364d82a41 2061 case PortTypeDisabled:
mjr 38:091e511ce8a0 2062 default:
mjr 38:091e511ce8a0 2063 // virtual or unknown
mjr 38:091e511ce8a0 2064 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2065 break;
mjr 38:091e511ce8a0 2066 }
mjr 38:091e511ce8a0 2067
mjr 40:cc0d9814522b 2068 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 2069 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 2070 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 2071 if (activeLow)
mjr 38:091e511ce8a0 2072 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 2073
mjr 89:c43cd923401c 2074 // Layer on Flipper Logic if desired
mjr 89:c43cd923401c 2075 if (flipperLogic)
mjr 89:c43cd923401c 2076 lwp = new LwFlipperLogicOut(lwp, pc.flipperLogic);
mjr 89:c43cd923401c 2077
mjr 99:8139b0c274f4 2078 // Layer on Chime Logic if desired. Note that Chime Logic and
mjr 99:8139b0c274f4 2079 // Flipper Logic are mutually exclusive, and Flipper Logic takes
mjr 99:8139b0c274f4 2080 // precedence, so ignore the Chime Logic bit if both are set.
mjr 99:8139b0c274f4 2081 if (chimeLogic && !flipperLogic)
mjr 99:8139b0c274f4 2082 lwp = new LwChimeLogicOut(lwp, pc.flipperLogic);
mjr 98:4df3c0f7e707 2083
mjr 89:c43cd923401c 2084 // If it's a noisemaker, layer on a night mode switch
mjr 40:cc0d9814522b 2085 if (noisy)
mjr 40:cc0d9814522b 2086 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 2087
mjr 40:cc0d9814522b 2088 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 2089 if (gamma)
mjr 40:cc0d9814522b 2090 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 2091
mjr 53:9b2611964afc 2092 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 2093 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 2094 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 2095 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 2096 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 2097
mjr 53:9b2611964afc 2098 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 2099 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 2100 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 2101
mjr 38:091e511ce8a0 2102 // turn it off initially
mjr 38:091e511ce8a0 2103 lwp->set(0);
mjr 38:091e511ce8a0 2104
mjr 38:091e511ce8a0 2105 // return the pin
mjr 38:091e511ce8a0 2106 return lwp;
mjr 38:091e511ce8a0 2107 }
mjr 38:091e511ce8a0 2108
mjr 6:cc35eb643e8f 2109 // initialize the output pin array
mjr 35:e959ffba78fd 2110 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 2111 {
mjr 99:8139b0c274f4 2112 // Initialize the Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 2113 LwFlipperLogicOut::classInit(cfg);
mjr 99:8139b0c274f4 2114 LwChimeLogicOut::classInit(cfg);
mjr 89:c43cd923401c 2115
mjr 35:e959ffba78fd 2116 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 2117 // total number of ports.
mjr 35:e959ffba78fd 2118 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 2119 int i;
mjr 35:e959ffba78fd 2120 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 2121 {
mjr 35:e959ffba78fd 2122 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 2123 {
mjr 35:e959ffba78fd 2124 numOutputs = i;
mjr 34:6b981a2afab7 2125 break;
mjr 34:6b981a2afab7 2126 }
mjr 33:d832bcab089e 2127 }
mjr 33:d832bcab089e 2128
mjr 73:4e8ce0b18915 2129 // allocate the pin array
mjr 73:4e8ce0b18915 2130 lwPin = new LwOut*[numOutputs];
mjr 35:e959ffba78fd 2131
mjr 73:4e8ce0b18915 2132 // Allocate the current brightness array
mjr 73:4e8ce0b18915 2133 outLevel = new uint8_t[numOutputs];
mjr 33:d832bcab089e 2134
mjr 73:4e8ce0b18915 2135 // allocate the LedWiz output state arrays
mjr 73:4e8ce0b18915 2136 wizOn = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 2137 wizVal = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 2138
mjr 73:4e8ce0b18915 2139 // initialize all LedWiz outputs to off and brightness 48
mjr 73:4e8ce0b18915 2140 memset(wizOn, 0, numOutputs);
mjr 73:4e8ce0b18915 2141 memset(wizVal, 48, numOutputs);
mjr 73:4e8ce0b18915 2142
mjr 73:4e8ce0b18915 2143 // set all LedWiz virtual unit flash speeds to 2
mjr 73:4e8ce0b18915 2144 for (i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2145 wizSpeed[i] = 2;
mjr 33:d832bcab089e 2146
mjr 35:e959ffba78fd 2147 // create the pin interface object for each port
mjr 35:e959ffba78fd 2148 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 2149 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 2150 }
mjr 6:cc35eb643e8f 2151
mjr 76:7f5912b6340e 2152 // Translate an LedWiz brightness level (0..49) to a DOF brightness
mjr 76:7f5912b6340e 2153 // level (0..255). Note that brightness level 49 isn't actually valid,
mjr 76:7f5912b6340e 2154 // according to the LedWiz API documentation, but many clients use it
mjr 76:7f5912b6340e 2155 // anyway, and the real LedWiz accepts it and seems to treat it as
mjr 76:7f5912b6340e 2156 // equivalent to 48.
mjr 40:cc0d9814522b 2157 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 2158 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 2159 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 2160 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 2161 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 2162 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 2163 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 2164 255, 255
mjr 40:cc0d9814522b 2165 };
mjr 40:cc0d9814522b 2166
mjr 76:7f5912b6340e 2167 // Translate a DOF brightness level (0..255) to an LedWiz brightness
mjr 76:7f5912b6340e 2168 // level (1..48)
mjr 76:7f5912b6340e 2169 static const uint8_t dof_to_lw[] = {
mjr 76:7f5912b6340e 2170 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
mjr 76:7f5912b6340e 2171 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
mjr 76:7f5912b6340e 2172 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9,
mjr 76:7f5912b6340e 2173 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12,
mjr 76:7f5912b6340e 2174 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15,
mjr 76:7f5912b6340e 2175 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18,
mjr 76:7f5912b6340e 2176 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21,
mjr 76:7f5912b6340e 2177 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24,
mjr 76:7f5912b6340e 2178 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27,
mjr 76:7f5912b6340e 2179 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30,
mjr 76:7f5912b6340e 2180 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
mjr 76:7f5912b6340e 2181 33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36,
mjr 76:7f5912b6340e 2182 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
mjr 76:7f5912b6340e 2183 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
mjr 76:7f5912b6340e 2184 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45,
mjr 76:7f5912b6340e 2185 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48
mjr 76:7f5912b6340e 2186 };
mjr 76:7f5912b6340e 2187
mjr 74:822a92bc11d2 2188 // LedWiz flash cycle tables. For efficiency, we use a lookup table
mjr 74:822a92bc11d2 2189 // rather than calculating these on the fly. The flash cycles are
mjr 74:822a92bc11d2 2190 // generated by the following formulas, where 'c' is the current
mjr 74:822a92bc11d2 2191 // cycle counter, from 0 to 255:
mjr 74:822a92bc11d2 2192 //
mjr 74:822a92bc11d2 2193 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 2194 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 2195 // mode 131 = on/ramp down = (c < 128 ? 255 : (255-c)*2)
mjr 74:822a92bc11d2 2196 // mode 132 = ramp up/on = (c < 128 ? c*2 : 255)
mjr 74:822a92bc11d2 2197 //
mjr 74:822a92bc11d2 2198 // To look up the current output value for a given mode and a given
mjr 74:822a92bc11d2 2199 // cycle counter 'c', index the table with ((mode-129)*256)+c.
mjr 74:822a92bc11d2 2200 static const uint8_t wizFlashLookup[] = {
mjr 74:822a92bc11d2 2201 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 2202 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
mjr 74:822a92bc11d2 2203 0x21, 0x23, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f,
mjr 74:822a92bc11d2 2204 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f,
mjr 74:822a92bc11d2 2205 0x61, 0x63, 0x65, 0x67, 0x69, 0x6b, 0x6d, 0x6f, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d, 0x7f,
mjr 74:822a92bc11d2 2206 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d, 0x8f, 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9d, 0x9f,
mjr 74:822a92bc11d2 2207 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad, 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, 0xbf,
mjr 74:822a92bc11d2 2208 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
mjr 74:822a92bc11d2 2209 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb, 0xed, 0xef, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0xfd, 0xff,
mjr 74:822a92bc11d2 2210 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 2211 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 2212 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 2213 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 2214 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 2215 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 2216 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 2217 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 2218
mjr 74:822a92bc11d2 2219 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 2220 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2221 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2222 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2223 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2224 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2225 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2226 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2227 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2228 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2230 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2231 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2232 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2236
mjr 74:822a92bc11d2 2237 // mode 131 = on/ramp down = c < 128 ? 255 : (255 - c)*2
mjr 74:822a92bc11d2 2238 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2239 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2240 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2241 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2242 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2243 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2244 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2246 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 2247 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 2248 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 2249 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 2250 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 2251 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 2252 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 2253 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 2254
mjr 74:822a92bc11d2 2255 // mode 132 = ramp up/on = c < 128 ? c*2 : 255
mjr 74:822a92bc11d2 2256 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
mjr 74:822a92bc11d2 2257 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
mjr 74:822a92bc11d2 2258 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
mjr 74:822a92bc11d2 2259 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
mjr 74:822a92bc11d2 2260 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
mjr 74:822a92bc11d2 2261 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
mjr 74:822a92bc11d2 2262 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
mjr 74:822a92bc11d2 2263 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
mjr 74:822a92bc11d2 2264 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2265 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2266 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2267 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2268 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2269 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2270 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2271 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
mjr 74:822a92bc11d2 2272 };
mjr 74:822a92bc11d2 2273
mjr 74:822a92bc11d2 2274 // LedWiz flash cycle timer. This runs continuously. On each update,
mjr 74:822a92bc11d2 2275 // we use this to figure out where we are on the cycle for each bank.
mjr 74:822a92bc11d2 2276 Timer wizCycleTimer;
mjr 74:822a92bc11d2 2277
mjr 76:7f5912b6340e 2278 // timing statistics for wizPulse()
mjr 76:7f5912b6340e 2279 uint64_t wizPulseTotalTime, wizPulseRunCount;
mjr 76:7f5912b6340e 2280
mjr 76:7f5912b6340e 2281 // LedWiz flash timer pulse. The main loop calls this on each cycle
mjr 76:7f5912b6340e 2282 // to update outputs using LedWiz flash modes. We do one bank of 32
mjr 76:7f5912b6340e 2283 // outputs on each cycle.
mjr 29:582472d0bc57 2284 static void wizPulse()
mjr 29:582472d0bc57 2285 {
mjr 76:7f5912b6340e 2286 // current bank
mjr 76:7f5912b6340e 2287 static int wizPulseBank = 0;
mjr 76:7f5912b6340e 2288
mjr 76:7f5912b6340e 2289 // start a timer for statistics collection
mjr 76:7f5912b6340e 2290 IF_DIAG(
mjr 76:7f5912b6340e 2291 Timer t;
mjr 76:7f5912b6340e 2292 t.start();
mjr 76:7f5912b6340e 2293 )
mjr 76:7f5912b6340e 2294
mjr 76:7f5912b6340e 2295 // Update the current bank's cycle counter: figure the current
mjr 76:7f5912b6340e 2296 // phase of the LedWiz pulse cycle for this bank.
mjr 76:7f5912b6340e 2297 //
mjr 76:7f5912b6340e 2298 // The LedWiz speed setting gives the flash period in 0.25s units
mjr 76:7f5912b6340e 2299 // (speed 1 is a flash period of .25s, speed 7 is a period of 1.75s).
mjr 76:7f5912b6340e 2300 //
mjr 76:7f5912b6340e 2301 // What we're after here is the "phase", which is to say the point
mjr 76:7f5912b6340e 2302 // in the current cycle. If we assume that the cycle has been running
mjr 76:7f5912b6340e 2303 // continuously since some arbitrary time zero in the past, we can
mjr 76:7f5912b6340e 2304 // figure where we are in the current cycle by dividing the time since
mjr 76:7f5912b6340e 2305 // that zero by the cycle period and taking the remainder. E.g., if
mjr 76:7f5912b6340e 2306 // the cycle time is 5 seconds, and the time since t-zero is 17 seconds,
mjr 76:7f5912b6340e 2307 // we divide 17 by 5 to get a remainder of 2. That says we're 2 seconds
mjr 76:7f5912b6340e 2308 // into the current 5-second cycle, or 2/5 of the way through the
mjr 76:7f5912b6340e 2309 // current cycle.
mjr 76:7f5912b6340e 2310 //
mjr 76:7f5912b6340e 2311 // We do this calculation on every iteration of the main loop, so we
mjr 76:7f5912b6340e 2312 // want it to be very fast. To streamline it, we'll use some tricky
mjr 76:7f5912b6340e 2313 // integer arithmetic. The result will be the same as the straightforward
mjr 76:7f5912b6340e 2314 // remainder and fraction calculation we just explained, but we'll get
mjr 76:7f5912b6340e 2315 // there by less-than-obvious means.
mjr 76:7f5912b6340e 2316 //
mjr 76:7f5912b6340e 2317 // Rather than finding the phase as a continuous quantity or floating
mjr 76:7f5912b6340e 2318 // point number, we'll quantize it. We'll divide each cycle into 256
mjr 76:7f5912b6340e 2319 // time units, or quanta. Each quantum is 1/256 of the cycle length,
mjr 76:7f5912b6340e 2320 // so for a 1-second cycle (LedWiz speed 4), each quantum is 1/256 of
mjr 76:7f5912b6340e 2321 // a second, or about 3.9ms. If we express the time since t-zero in
mjr 76:7f5912b6340e 2322 // these units, the time period of one cycle is exactly 256 units, so
mjr 76:7f5912b6340e 2323 // we can calculate our point in the cycle by taking the remainder of
mjr 76:7f5912b6340e 2324 // the time (in our funny units) divided by 256. The special thing
mjr 76:7f5912b6340e 2325 // about making the cycle time equal to 256 units is that "x % 256"
mjr 76:7f5912b6340e 2326 // is exactly the same as "x & 255", which is a much faster operation
mjr 76:7f5912b6340e 2327 // than division on ARM M0+: this CPU has no hardware DIVIDE operation,
mjr 76:7f5912b6340e 2328 // so an integer division takes about 5us. The bit mask operation, in
mjr 76:7f5912b6340e 2329 // contrast, takes only about 60ns - about 100x faster. 5us doesn't
mjr 76:7f5912b6340e 2330 // sound like much, but we do this on every main loop, so every little
mjr 76:7f5912b6340e 2331 // bit counts.
mjr 76:7f5912b6340e 2332 //
mjr 76:7f5912b6340e 2333 // The snag is that our system timer gives us the elapsed time in
mjr 76:7f5912b6340e 2334 // microseconds. We still need to convert this to our special quanta
mjr 76:7f5912b6340e 2335 // of 256 units per cycle. The straightforward way to do that is by
mjr 76:7f5912b6340e 2336 // dividing by (microseconds per quantum). E.g., for LedWiz speed 4,
mjr 76:7f5912b6340e 2337 // we decided that our quantum was 1/256 of a second, or 3906us, so
mjr 76:7f5912b6340e 2338 // dividing the current system time in microseconds by 3906 will give
mjr 76:7f5912b6340e 2339 // us the time in our quantum units. But now we've just substituted
mjr 76:7f5912b6340e 2340 // one division for another!
mjr 76:7f5912b6340e 2341 //
mjr 76:7f5912b6340e 2342 // This is where our really tricky integer math comes in. Dividing
mjr 76:7f5912b6340e 2343 // by X is the same as multiplying by 1/X. In integer math, 1/3906
mjr 76:7f5912b6340e 2344 // is zero, so that won't work. But we can get around that by doing
mjr 76:7f5912b6340e 2345 // the integer math as "fixed point" arithmetic instead. It's still
mjr 76:7f5912b6340e 2346 // actually carried out as integer operations, but we'll scale our
mjr 76:7f5912b6340e 2347 // integers by a scaling factor, then take out the scaling factor
mjr 76:7f5912b6340e 2348 // later to get the final result. The scaling factor we'll use is
mjr 76:7f5912b6340e 2349 // 2^24. So we're going to calculate (time * 2^24/3906), then divide
mjr 76:7f5912b6340e 2350 // the result by 2^24 to get the final answer. I know it seems like
mjr 76:7f5912b6340e 2351 // we're substituting one division for another yet again, but this
mjr 76:7f5912b6340e 2352 // time's the charm, because dividing by 2^24 is a bit shift operation,
mjr 76:7f5912b6340e 2353 // which is another single-cycle operation on M0+. You might also
mjr 76:7f5912b6340e 2354 // wonder how all these tricks don't cause overflows or underflows
mjr 76:7f5912b6340e 2355 // or what not. Well, the multiply by 2^24/3906 will cause an
mjr 76:7f5912b6340e 2356 // overflow, but we don't care, because the overflow will all be in
mjr 76:7f5912b6340e 2357 // the high-order bits that we're going to discard in the final
mjr 76:7f5912b6340e 2358 // remainder calculation anyway.
mjr 76:7f5912b6340e 2359 //
mjr 76:7f5912b6340e 2360 // Each entry in the array below represents 2^24/N for the corresponding
mjr 76:7f5912b6340e 2361 // LedWiz speed, where N is the number of time quanta per cycle at that
mjr 76:7f5912b6340e 2362 // speed. The time quanta are chosen such that 256 quanta add up to
mjr 76:7f5912b6340e 2363 // approximately (LedWiz speed setting * 0.25s).
mjr 76:7f5912b6340e 2364 //
mjr 76:7f5912b6340e 2365 // Note that the calculation has an implicit bit mask (result & 0xFF)
mjr 76:7f5912b6340e 2366 // to get the final result mod 256. But we don't have to actually
mjr 76:7f5912b6340e 2367 // do that work because we're using 32-bit ints and a 2^24 fixed
mjr 76:7f5912b6340e 2368 // point base (X in the narrative above). The final shift right by
mjr 76:7f5912b6340e 2369 // 24 bits to divide out the base will leave us with only 8 bits in
mjr 76:7f5912b6340e 2370 // the result, since we started with 32.
mjr 76:7f5912b6340e 2371 static const uint32_t inv_us_per_quantum[] = { // indexed by LedWiz speed
mjr 76:7f5912b6340e 2372 0, 17172, 8590, 5726, 4295, 3436, 2863, 2454
mjr 76:7f5912b6340e 2373 };
mjr 76:7f5912b6340e 2374 int counter = ((wizCycleTimer.read_us() * inv_us_per_quantum[wizSpeed[wizPulseBank]]) >> 24);
mjr 76:7f5912b6340e 2375
mjr 76:7f5912b6340e 2376 // get the range of 32 output sin this bank
mjr 76:7f5912b6340e 2377 int fromPort = wizPulseBank*32;
mjr 76:7f5912b6340e 2378 int toPort = fromPort+32;
mjr 76:7f5912b6340e 2379 if (toPort > numOutputs)
mjr 76:7f5912b6340e 2380 toPort = numOutputs;
mjr 76:7f5912b6340e 2381
mjr 76:7f5912b6340e 2382 // update all outputs set to flashing values
mjr 76:7f5912b6340e 2383 for (int i = fromPort ; i < toPort ; ++i)
mjr 73:4e8ce0b18915 2384 {
mjr 76:7f5912b6340e 2385 // Update the port only if the LedWiz SBA switch for the port is on
mjr 76:7f5912b6340e 2386 // (wizOn[i]) AND the port is a PBA flash mode in the range 129..132.
mjr 76:7f5912b6340e 2387 // These modes and only these modes have the high bit (0x80) set, so
mjr 76:7f5912b6340e 2388 // we can test for them simply by testing the high bit.
mjr 76:7f5912b6340e 2389 if (wizOn[i])
mjr 29:582472d0bc57 2390 {
mjr 76:7f5912b6340e 2391 uint8_t val = wizVal[i];
mjr 76:7f5912b6340e 2392 if ((val & 0x80) != 0)
mjr 29:582472d0bc57 2393 {
mjr 76:7f5912b6340e 2394 // ook up the value for the mode at the cycle time
mjr 76:7f5912b6340e 2395 lwPin[i]->set(outLevel[i] = wizFlashLookup[((val-129) << 8) + counter]);
mjr 29:582472d0bc57 2396 }
mjr 29:582472d0bc57 2397 }
mjr 76:7f5912b6340e 2398 }
mjr 76:7f5912b6340e 2399
mjr 34:6b981a2afab7 2400 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 2401 if (hc595 != 0)
mjr 35:e959ffba78fd 2402 hc595->update();
mjr 76:7f5912b6340e 2403
mjr 76:7f5912b6340e 2404 // switch to the next bank
mjr 76:7f5912b6340e 2405 if (++wizPulseBank >= MAX_LW_BANKS)
mjr 76:7f5912b6340e 2406 wizPulseBank = 0;
mjr 76:7f5912b6340e 2407
mjr 76:7f5912b6340e 2408 // collect timing statistics
mjr 76:7f5912b6340e 2409 IF_DIAG(
mjr 76:7f5912b6340e 2410 wizPulseTotalTime += t.read_us();
mjr 76:7f5912b6340e 2411 wizPulseRunCount += 1;
mjr 76:7f5912b6340e 2412 )
mjr 1:d913e0afb2ac 2413 }
mjr 38:091e511ce8a0 2414
mjr 76:7f5912b6340e 2415 // Update a port to reflect its new LedWiz SBA+PBA setting.
mjr 76:7f5912b6340e 2416 static void updateLwPort(int port)
mjr 38:091e511ce8a0 2417 {
mjr 76:7f5912b6340e 2418 // check if the SBA switch is on or off
mjr 76:7f5912b6340e 2419 if (wizOn[port])
mjr 76:7f5912b6340e 2420 {
mjr 76:7f5912b6340e 2421 // It's on. If the port is a valid static brightness level,
mjr 76:7f5912b6340e 2422 // set the output port to match. Otherwise leave it as is:
mjr 76:7f5912b6340e 2423 // if it's a flashing mode, the flash mode pulse will update
mjr 76:7f5912b6340e 2424 // it on the next cycle.
mjr 76:7f5912b6340e 2425 int val = wizVal[port];
mjr 76:7f5912b6340e 2426 if (val <= 49)
mjr 76:7f5912b6340e 2427 lwPin[port]->set(outLevel[port] = lw_to_dof[val]);
mjr 76:7f5912b6340e 2428 }
mjr 76:7f5912b6340e 2429 else
mjr 76:7f5912b6340e 2430 {
mjr 76:7f5912b6340e 2431 // the port is off - set absolute brightness zero
mjr 76:7f5912b6340e 2432 lwPin[port]->set(outLevel[port] = 0);
mjr 76:7f5912b6340e 2433 }
mjr 73:4e8ce0b18915 2434 }
mjr 73:4e8ce0b18915 2435
mjr 73:4e8ce0b18915 2436 // Turn off all outputs and restore everything to the default LedWiz
mjr 92:f264fbaa1be5 2437 // state. This sets all outputs to LedWiz profile value 48 (full
mjr 92:f264fbaa1be5 2438 // brightness) and switch state Off, and sets the LedWiz flash rate
mjr 92:f264fbaa1be5 2439 // to 2. This effectively restores the power-on conditions.
mjr 73:4e8ce0b18915 2440 //
mjr 73:4e8ce0b18915 2441 void allOutputsOff()
mjr 73:4e8ce0b18915 2442 {
mjr 92:f264fbaa1be5 2443 // reset all outputs to OFF/48
mjr 73:4e8ce0b18915 2444 for (int i = 0 ; i < numOutputs ; ++i)
mjr 73:4e8ce0b18915 2445 {
mjr 73:4e8ce0b18915 2446 outLevel[i] = 0;
mjr 73:4e8ce0b18915 2447 wizOn[i] = 0;
mjr 73:4e8ce0b18915 2448 wizVal[i] = 48;
mjr 73:4e8ce0b18915 2449 lwPin[i]->set(0);
mjr 73:4e8ce0b18915 2450 }
mjr 73:4e8ce0b18915 2451
mjr 73:4e8ce0b18915 2452 // restore default LedWiz flash rate
mjr 73:4e8ce0b18915 2453 for (int i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2454 wizSpeed[i] = 2;
mjr 38:091e511ce8a0 2455
mjr 73:4e8ce0b18915 2456 // flush changes to hc595, if applicable
mjr 38:091e511ce8a0 2457 if (hc595 != 0)
mjr 38:091e511ce8a0 2458 hc595->update();
mjr 38:091e511ce8a0 2459 }
mjr 38:091e511ce8a0 2460
mjr 74:822a92bc11d2 2461 // Cary out an SBA or SBX message. portGroup is 0 for ports 1-32,
mjr 74:822a92bc11d2 2462 // 1 for ports 33-64, etc. Original protocol SBA messages always
mjr 74:822a92bc11d2 2463 // address port group 0; our private SBX extension messages can
mjr 74:822a92bc11d2 2464 // address any port group.
mjr 74:822a92bc11d2 2465 void sba_sbx(int portGroup, const uint8_t *data)
mjr 74:822a92bc11d2 2466 {
mjr 76:7f5912b6340e 2467 // update all on/off states in the group
mjr 74:822a92bc11d2 2468 for (int i = 0, bit = 1, imsg = 1, port = portGroup*32 ;
mjr 74:822a92bc11d2 2469 i < 32 && port < numOutputs ;
mjr 74:822a92bc11d2 2470 ++i, bit <<= 1, ++port)
mjr 74:822a92bc11d2 2471 {
mjr 74:822a92bc11d2 2472 // figure the on/off state bit for this output
mjr 74:822a92bc11d2 2473 if (bit == 0x100) {
mjr 74:822a92bc11d2 2474 bit = 1;
mjr 74:822a92bc11d2 2475 ++imsg;
mjr 74:822a92bc11d2 2476 }
mjr 74:822a92bc11d2 2477
mjr 74:822a92bc11d2 2478 // set the on/off state
mjr 76:7f5912b6340e 2479 bool on = wizOn[port] = ((data[imsg] & bit) != 0);
mjr 76:7f5912b6340e 2480
mjr 76:7f5912b6340e 2481 // set the output port brightness to match the new setting
mjr 76:7f5912b6340e 2482 updateLwPort(port);
mjr 74:822a92bc11d2 2483 }
mjr 74:822a92bc11d2 2484
mjr 74:822a92bc11d2 2485 // set the flash speed for the port group
mjr 74:822a92bc11d2 2486 if (portGroup < countof(wizSpeed))
mjr 74:822a92bc11d2 2487 wizSpeed[portGroup] = (data[5] < 1 ? 1 : data[5] > 7 ? 7 : data[5]);
mjr 74:822a92bc11d2 2488
mjr 76:7f5912b6340e 2489 // update 74HC959 outputs
mjr 76:7f5912b6340e 2490 if (hc595 != 0)
mjr 76:7f5912b6340e 2491 hc595->update();
mjr 74:822a92bc11d2 2492 }
mjr 74:822a92bc11d2 2493
mjr 74:822a92bc11d2 2494 // Carry out a PBA or PBX message.
mjr 74:822a92bc11d2 2495 void pba_pbx(int basePort, const uint8_t *data)
mjr 74:822a92bc11d2 2496 {
mjr 74:822a92bc11d2 2497 // update each wizVal entry from the brightness data
mjr 76:7f5912b6340e 2498 for (int i = 0, port = basePort ; i < 8 && port < numOutputs ; ++i, ++port)
mjr 74:822a92bc11d2 2499 {
mjr 74:822a92bc11d2 2500 // get the value
mjr 74:822a92bc11d2 2501 uint8_t v = data[i];
mjr 74:822a92bc11d2 2502
mjr 74:822a92bc11d2 2503 // Validate it. The legal values are 0..49 for brightness
mjr 74:822a92bc11d2 2504 // levels, and 128..132 for flash modes. Set anything invalid
mjr 74:822a92bc11d2 2505 // to full brightness (48) instead. Note that 49 isn't actually
mjr 74:822a92bc11d2 2506 // a valid documented value, but in practice some clients send
mjr 74:822a92bc11d2 2507 // this to mean 100% brightness, and the real LedWiz treats it
mjr 74:822a92bc11d2 2508 // as such.
mjr 74:822a92bc11d2 2509 if ((v > 49 && v < 129) || v > 132)
mjr 74:822a92bc11d2 2510 v = 48;
mjr 74:822a92bc11d2 2511
mjr 74:822a92bc11d2 2512 // store it
mjr 76:7f5912b6340e 2513 wizVal[port] = v;
mjr 76:7f5912b6340e 2514
mjr 76:7f5912b6340e 2515 // update the port
mjr 76:7f5912b6340e 2516 updateLwPort(port);
mjr 74:822a92bc11d2 2517 }
mjr 74:822a92bc11d2 2518
mjr 76:7f5912b6340e 2519 // update 74HC595 outputs
mjr 76:7f5912b6340e 2520 if (hc595 != 0)
mjr 76:7f5912b6340e 2521 hc595->update();
mjr 74:822a92bc11d2 2522 }
mjr 74:822a92bc11d2 2523
mjr 77:0b96f6867312 2524 // ---------------------------------------------------------------------------
mjr 77:0b96f6867312 2525 //
mjr 77:0b96f6867312 2526 // IR Remote Control transmitter & receiver
mjr 77:0b96f6867312 2527 //
mjr 77:0b96f6867312 2528
mjr 77:0b96f6867312 2529 // receiver
mjr 77:0b96f6867312 2530 IRReceiver *ir_rx;
mjr 77:0b96f6867312 2531
mjr 77:0b96f6867312 2532 // transmitter
mjr 77:0b96f6867312 2533 IRTransmitter *ir_tx;
mjr 77:0b96f6867312 2534
mjr 77:0b96f6867312 2535 // Mapping from IR commands slots in the configuration to "virtual button"
mjr 77:0b96f6867312 2536 // numbers on the IRTransmitter's "virtual remote". To minimize RAM usage,
mjr 77:0b96f6867312 2537 // we only create virtual buttons on the transmitter object for code slots
mjr 77:0b96f6867312 2538 // that are configured for transmission, which includes slots used for TV
mjr 77:0b96f6867312 2539 // ON commands and slots that can be triggered by button presses. This
mjr 77:0b96f6867312 2540 // means that virtual button numbers won't necessarily match the config
mjr 77:0b96f6867312 2541 // slot numbers. This table provides the mapping:
mjr 77:0b96f6867312 2542 // IRConfigSlotToVirtualButton[n] = ir_tx virtual button number for
mjr 77:0b96f6867312 2543 // configuration slot n
mjr 77:0b96f6867312 2544 uint8_t IRConfigSlotToVirtualButton[MAX_IR_CODES];
mjr 78:1e00b3fa11af 2545
mjr 78:1e00b3fa11af 2546 // IR transmitter virtual button number for ad hoc IR command. We allocate
mjr 78:1e00b3fa11af 2547 // one virtual button for sending ad hoc IR codes, such as through the USB
mjr 78:1e00b3fa11af 2548 // protocol.
mjr 78:1e00b3fa11af 2549 uint8_t IRAdHocBtn;
mjr 78:1e00b3fa11af 2550
mjr 78:1e00b3fa11af 2551 // Staging area for ad hoc IR commands. It takes multiple messages
mjr 78:1e00b3fa11af 2552 // to fill out an IR command, so we store the partial command here
mjr 78:1e00b3fa11af 2553 // while waiting for the rest.
mjr 78:1e00b3fa11af 2554 static struct
mjr 78:1e00b3fa11af 2555 {
mjr 78:1e00b3fa11af 2556 uint8_t protocol; // protocol ID
mjr 78:1e00b3fa11af 2557 uint64_t code; // code
mjr 78:1e00b3fa11af 2558 uint8_t dittos : 1; // using dittos?
mjr 78:1e00b3fa11af 2559 uint8_t ready : 1; // do we have a code ready to transmit?
mjr 78:1e00b3fa11af 2560 } IRAdHocCmd;
mjr 88:98bce687e6c0 2561
mjr 77:0b96f6867312 2562
mjr 77:0b96f6867312 2563 // IR mode timer. In normal mode, this is the time since the last
mjr 77:0b96f6867312 2564 // command received; we use this to handle commands with timed effects,
mjr 77:0b96f6867312 2565 // such as sending a key to the PC. In learning mode, this is the time
mjr 77:0b96f6867312 2566 // since we activated learning mode, which we use to automatically end
mjr 77:0b96f6867312 2567 // learning mode if a decodable command isn't received within a reasonable
mjr 77:0b96f6867312 2568 // amount of time.
mjr 77:0b96f6867312 2569 Timer IRTimer;
mjr 77:0b96f6867312 2570
mjr 77:0b96f6867312 2571 // IR Learning Mode. The PC enters learning mode via special function 65 12.
mjr 77:0b96f6867312 2572 // The states are:
mjr 77:0b96f6867312 2573 //
mjr 77:0b96f6867312 2574 // 0 -> normal operation (not in learning mode)
mjr 77:0b96f6867312 2575 // 1 -> learning mode; reading raw codes, no command read yet
mjr 77:0b96f6867312 2576 // 2 -> learning mode; command received, awaiting auto-repeat
mjr 77:0b96f6867312 2577 // 3 -> learning mode; done, command and repeat mode decoded
mjr 77:0b96f6867312 2578 //
mjr 77:0b96f6867312 2579 // When we enter learning mode, we reset IRTimer to keep track of how long
mjr 77:0b96f6867312 2580 // we've been in the mode. This allows the mode to time out if no code is
mjr 77:0b96f6867312 2581 // received within a reasonable time.
mjr 77:0b96f6867312 2582 uint8_t IRLearningMode = 0;
mjr 77:0b96f6867312 2583
mjr 77:0b96f6867312 2584 // Learning mode command received. This stores the first decoded command
mjr 77:0b96f6867312 2585 // when in learning mode. For some protocols, we can't just report the
mjr 77:0b96f6867312 2586 // first command we receive, because we need to wait for an auto-repeat to
mjr 77:0b96f6867312 2587 // determine what format the remote uses for repeats. This stores the first
mjr 77:0b96f6867312 2588 // command while we await a repeat. This is necessary for protocols that
mjr 77:0b96f6867312 2589 // have "dittos", since some remotes for such protocols use the dittos and
mjr 77:0b96f6867312 2590 // some don't; the only way to find out is to read a repeat code and see if
mjr 77:0b96f6867312 2591 // it's a ditto or just a repeat of the full code.
mjr 77:0b96f6867312 2592 IRCommand learnedIRCode;
mjr 77:0b96f6867312 2593
mjr 78:1e00b3fa11af 2594 // IR command received, as a config slot index, 1..MAX_IR_CODES.
mjr 77:0b96f6867312 2595 // When we receive a command that matches one of our programmed commands,
mjr 77:0b96f6867312 2596 // we note the slot here. We also reset the IR timer so that we know how
mjr 77:0b96f6867312 2597 // long it's been since the command came in. This lets us handle commands
mjr 77:0b96f6867312 2598 // with timed effects, such as PC key input. Note that this is a 1-based
mjr 77:0b96f6867312 2599 // index; 0 represents no command.
mjr 77:0b96f6867312 2600 uint8_t IRCommandIn = 0;
mjr 77:0b96f6867312 2601
mjr 77:0b96f6867312 2602 // "Toggle bit" of last command. Some IR protocols have a toggle bit
mjr 77:0b96f6867312 2603 // that distinguishes an auto-repeating key from a key being pressed
mjr 77:0b96f6867312 2604 // several times in a row. This records the toggle bit of the last
mjr 77:0b96f6867312 2605 // command we received.
mjr 77:0b96f6867312 2606 uint8_t lastIRToggle = 0;
mjr 77:0b96f6867312 2607
mjr 77:0b96f6867312 2608 // Are we in a gap between successive key presses? When we detect that a
mjr 77:0b96f6867312 2609 // key is being pressed multiple times rather than auto-repeated (which we
mjr 77:0b96f6867312 2610 // can detect via a toggle bit in some protocols), we'll briefly stop sending
mjr 77:0b96f6867312 2611 // the associated key to the PC, so that the PC likewise recognizes the
mjr 77:0b96f6867312 2612 // distinct key press.
mjr 77:0b96f6867312 2613 uint8_t IRKeyGap = false;
mjr 77:0b96f6867312 2614
mjr 78:1e00b3fa11af 2615
mjr 77:0b96f6867312 2616 // initialize
mjr 77:0b96f6867312 2617 void init_IR(Config &cfg, bool &kbKeys)
mjr 77:0b96f6867312 2618 {
mjr 77:0b96f6867312 2619 PinName pin;
mjr 77:0b96f6867312 2620
mjr 77:0b96f6867312 2621 // start the IR timer
mjr 77:0b96f6867312 2622 IRTimer.start();
mjr 77:0b96f6867312 2623
mjr 77:0b96f6867312 2624 // if there's a transmitter, set it up
mjr 77:0b96f6867312 2625 if ((pin = wirePinName(cfg.IR.emitter)) != NC)
mjr 77:0b96f6867312 2626 {
mjr 77:0b96f6867312 2627 // no virtual buttons yet
mjr 77:0b96f6867312 2628 int nVirtualButtons = 0;
mjr 77:0b96f6867312 2629 memset(IRConfigSlotToVirtualButton, 0xFF, sizeof(IRConfigSlotToVirtualButton));
mjr 77:0b96f6867312 2630
mjr 77:0b96f6867312 2631 // assign virtual buttons slots for TV ON codes
mjr 77:0b96f6867312 2632 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2633 {
mjr 77:0b96f6867312 2634 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 2635 IRConfigSlotToVirtualButton[i] = nVirtualButtons++;
mjr 77:0b96f6867312 2636 }
mjr 77:0b96f6867312 2637
mjr 77:0b96f6867312 2638 // assign virtual buttons for codes that can be triggered by
mjr 77:0b96f6867312 2639 // real button inputs
mjr 77:0b96f6867312 2640 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 77:0b96f6867312 2641 {
mjr 77:0b96f6867312 2642 // get the button
mjr 77:0b96f6867312 2643 ButtonCfg &b = cfg.button[i];
mjr 77:0b96f6867312 2644
mjr 77:0b96f6867312 2645 // check the unshifted button
mjr 77:0b96f6867312 2646 int c = b.IRCommand - 1;
mjr 77:0b96f6867312 2647 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2648 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2649 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2650
mjr 77:0b96f6867312 2651 // check the shifted button
mjr 77:0b96f6867312 2652 c = b.IRCommand2 - 1;
mjr 77:0b96f6867312 2653 if (c >= 0 && c < MAX_IR_CODES
mjr 77:0b96f6867312 2654 && IRConfigSlotToVirtualButton[c] == 0xFF)
mjr 77:0b96f6867312 2655 IRConfigSlotToVirtualButton[c] = nVirtualButtons++;
mjr 77:0b96f6867312 2656 }
mjr 77:0b96f6867312 2657
mjr 77:0b96f6867312 2658 // allocate an additional virtual button for transmitting ad hoc
mjr 77:0b96f6867312 2659 // codes, such as for the "send code" USB API function
mjr 78:1e00b3fa11af 2660 IRAdHocBtn = nVirtualButtons++;
mjr 77:0b96f6867312 2661
mjr 77:0b96f6867312 2662 // create the transmitter
mjr 77:0b96f6867312 2663 ir_tx = new IRTransmitter(pin, nVirtualButtons);
mjr 77:0b96f6867312 2664
mjr 77:0b96f6867312 2665 // program the commands into the virtual button slots
mjr 77:0b96f6867312 2666 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2667 {
mjr 77:0b96f6867312 2668 // if this slot is assigned to a virtual button, program it
mjr 77:0b96f6867312 2669 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 2670 if (vb != 0xFF)
mjr 77:0b96f6867312 2671 {
mjr 77:0b96f6867312 2672 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2673 uint64_t code = cb.code.lo | (uint64_t(cb.code.hi) << 32);
mjr 77:0b96f6867312 2674 bool dittos = (cb.flags & IRFlagDittos) != 0;
mjr 77:0b96f6867312 2675 ir_tx->programButton(vb, cb.protocol, dittos, code);
mjr 77:0b96f6867312 2676 }
mjr 77:0b96f6867312 2677 }
mjr 77:0b96f6867312 2678 }
mjr 77:0b96f6867312 2679
mjr 77:0b96f6867312 2680 // if there's a receiver, set it up
mjr 77:0b96f6867312 2681 if ((pin = wirePinName(cfg.IR.sensor)) != NC)
mjr 77:0b96f6867312 2682 {
mjr 77:0b96f6867312 2683 // create the receiver
mjr 77:0b96f6867312 2684 ir_rx = new IRReceiver(pin, 32);
mjr 77:0b96f6867312 2685
mjr 77:0b96f6867312 2686 // connect the transmitter (if any) to the receiver, so that
mjr 77:0b96f6867312 2687 // the receiver can suppress reception of our own transmissions
mjr 77:0b96f6867312 2688 ir_rx->setTransmitter(ir_tx);
mjr 77:0b96f6867312 2689
mjr 77:0b96f6867312 2690 // enable it
mjr 77:0b96f6867312 2691 ir_rx->enable();
mjr 77:0b96f6867312 2692
mjr 77:0b96f6867312 2693 // Check the IR command slots to see if any slots are configured
mjr 77:0b96f6867312 2694 // to send a keyboard key on receiving an IR command. If any are,
mjr 77:0b96f6867312 2695 // tell the caller that we need a USB keyboard interface.
mjr 77:0b96f6867312 2696 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2697 {
mjr 77:0b96f6867312 2698 IRCommandCfg &cb = cfg.IRCommand[i];
mjr 77:0b96f6867312 2699 if (cb.protocol != 0
mjr 77:0b96f6867312 2700 && (cb.keytype == BtnTypeKey || cb.keytype == BtnTypeMedia))
mjr 77:0b96f6867312 2701 {
mjr 77:0b96f6867312 2702 kbKeys = true;
mjr 77:0b96f6867312 2703 break;
mjr 77:0b96f6867312 2704 }
mjr 77:0b96f6867312 2705 }
mjr 77:0b96f6867312 2706 }
mjr 77:0b96f6867312 2707 }
mjr 77:0b96f6867312 2708
mjr 77:0b96f6867312 2709 // Press or release a button with an assigned IR function. 'cmd'
mjr 77:0b96f6867312 2710 // is the command slot number (1..MAX_IR_CODES) assigned to the button.
mjr 77:0b96f6867312 2711 void IR_buttonChange(uint8_t cmd, bool pressed)
mjr 77:0b96f6867312 2712 {
mjr 77:0b96f6867312 2713 // only proceed if there's an IR transmitter attached
mjr 77:0b96f6867312 2714 if (ir_tx != 0)
mjr 77:0b96f6867312 2715 {
mjr 77:0b96f6867312 2716 // adjust the command slot to a zero-based index
mjr 77:0b96f6867312 2717 int slot = cmd - 1;
mjr 77:0b96f6867312 2718
mjr 77:0b96f6867312 2719 // press or release the virtual button
mjr 77:0b96f6867312 2720 ir_tx->pushButton(IRConfigSlotToVirtualButton[slot], pressed);
mjr 77:0b96f6867312 2721 }
mjr 77:0b96f6867312 2722 }
mjr 77:0b96f6867312 2723
mjr 78:1e00b3fa11af 2724 // Process IR input and output
mjr 77:0b96f6867312 2725 void process_IR(Config &cfg, USBJoystick &js)
mjr 77:0b96f6867312 2726 {
mjr 78:1e00b3fa11af 2727 // check for transmitter tasks, if there's a transmitter
mjr 78:1e00b3fa11af 2728 if (ir_tx != 0)
mjr 77:0b96f6867312 2729 {
mjr 78:1e00b3fa11af 2730 // If we're not currently sending, and an ad hoc IR command
mjr 78:1e00b3fa11af 2731 // is ready to send, send it.
mjr 78:1e00b3fa11af 2732 if (!ir_tx->isSending() && IRAdHocCmd.ready)
mjr 78:1e00b3fa11af 2733 {
mjr 78:1e00b3fa11af 2734 // program the command into the transmitter virtual button
mjr 78:1e00b3fa11af 2735 // that we reserved for ad hoc commands
mjr 78:1e00b3fa11af 2736 ir_tx->programButton(IRAdHocBtn, IRAdHocCmd.protocol,
mjr 78:1e00b3fa11af 2737 IRAdHocCmd.dittos, IRAdHocCmd.code);
mjr 78:1e00b3fa11af 2738
mjr 78:1e00b3fa11af 2739 // send the command - just pulse the button to send it once
mjr 78:1e00b3fa11af 2740 ir_tx->pushButton(IRAdHocBtn, true);
mjr 78:1e00b3fa11af 2741 ir_tx->pushButton(IRAdHocBtn, false);
mjr 78:1e00b3fa11af 2742
mjr 78:1e00b3fa11af 2743 // we've sent the command, so clear the 'ready' flag
mjr 78:1e00b3fa11af 2744 IRAdHocCmd.ready = false;
mjr 78:1e00b3fa11af 2745 }
mjr 77:0b96f6867312 2746 }
mjr 78:1e00b3fa11af 2747
mjr 78:1e00b3fa11af 2748 // check for receiver tasks, if there's a receiver
mjr 78:1e00b3fa11af 2749 if (ir_rx != 0)
mjr 77:0b96f6867312 2750 {
mjr 78:1e00b3fa11af 2751 // Time out any received command
mjr 78:1e00b3fa11af 2752 if (IRCommandIn != 0)
mjr 78:1e00b3fa11af 2753 {
mjr 80:94dc2946871b 2754 // Time out commands after 200ms without a repeat signal.
mjr 80:94dc2946871b 2755 // Time out the inter-key gap after 50ms.
mjr 78:1e00b3fa11af 2756 uint32_t t = IRTimer.read_us();
mjr 80:94dc2946871b 2757 if (t > 200000)
mjr 78:1e00b3fa11af 2758 IRCommandIn = 0;
mjr 80:94dc2946871b 2759 else if (t > 50000)
mjr 78:1e00b3fa11af 2760 IRKeyGap = false;
mjr 78:1e00b3fa11af 2761 }
mjr 78:1e00b3fa11af 2762
mjr 78:1e00b3fa11af 2763 // Check if we're in learning mode
mjr 78:1e00b3fa11af 2764 if (IRLearningMode != 0)
mjr 78:1e00b3fa11af 2765 {
mjr 78:1e00b3fa11af 2766 // Learning mode. Read raw inputs from the IR sensor and
mjr 78:1e00b3fa11af 2767 // forward them to the PC via USB reports, up to the report
mjr 78:1e00b3fa11af 2768 // limit.
mjr 78:1e00b3fa11af 2769 const int nmax = USBJoystick::maxRawIR;
mjr 78:1e00b3fa11af 2770 uint16_t raw[nmax];
mjr 78:1e00b3fa11af 2771 int n;
mjr 78:1e00b3fa11af 2772 for (n = 0 ; n < nmax && ir_rx->processOne(raw[n]) ; ++n) ;
mjr 77:0b96f6867312 2773
mjr 78:1e00b3fa11af 2774 // if we read any raw samples, report them
mjr 78:1e00b3fa11af 2775 if (n != 0)
mjr 78:1e00b3fa11af 2776 js.reportRawIR(n, raw);
mjr 77:0b96f6867312 2777
mjr 78:1e00b3fa11af 2778 // check for a command
mjr 78:1e00b3fa11af 2779 IRCommand c;
mjr 78:1e00b3fa11af 2780 if (ir_rx->readCommand(c))
mjr 78:1e00b3fa11af 2781 {
mjr 78:1e00b3fa11af 2782 // check the current learning state
mjr 78:1e00b3fa11af 2783 switch (IRLearningMode)
mjr 78:1e00b3fa11af 2784 {
mjr 78:1e00b3fa11af 2785 case 1:
mjr 78:1e00b3fa11af 2786 // Initial state, waiting for the first decoded command.
mjr 78:1e00b3fa11af 2787 // This is it.
mjr 78:1e00b3fa11af 2788 learnedIRCode = c;
mjr 78:1e00b3fa11af 2789
mjr 78:1e00b3fa11af 2790 // Check if we need additional information. If the
mjr 78:1e00b3fa11af 2791 // protocol supports dittos, we have to wait for a repeat
mjr 78:1e00b3fa11af 2792 // to see if the remote actually uses the dittos, since
mjr 78:1e00b3fa11af 2793 // some implementations of such protocols use the dittos
mjr 78:1e00b3fa11af 2794 // while others just send repeated full codes. Otherwise,
mjr 78:1e00b3fa11af 2795 // all we need is the initial code, so we're done.
mjr 78:1e00b3fa11af 2796 IRLearningMode = (c.hasDittos ? 2 : 3);
mjr 78:1e00b3fa11af 2797 break;
mjr 78:1e00b3fa11af 2798
mjr 78:1e00b3fa11af 2799 case 2:
mjr 78:1e00b3fa11af 2800 // Code received, awaiting auto-repeat information. If
mjr 78:1e00b3fa11af 2801 // the protocol has dittos, check to see if we got a ditto:
mjr 78:1e00b3fa11af 2802 //
mjr 78:1e00b3fa11af 2803 // - If we received a ditto in the same protocol as the
mjr 78:1e00b3fa11af 2804 // prior command, the remote uses dittos.
mjr 78:1e00b3fa11af 2805 //
mjr 78:1e00b3fa11af 2806 // - If we received a repeat of the prior command (not a
mjr 78:1e00b3fa11af 2807 // ditto, but a repeat of the full code), the remote
mjr 78:1e00b3fa11af 2808 // doesn't use dittos even though the protocol supports
mjr 78:1e00b3fa11af 2809 // them.
mjr 78:1e00b3fa11af 2810 //
mjr 78:1e00b3fa11af 2811 // - Otherwise, it's not an auto-repeat at all, so we
mjr 78:1e00b3fa11af 2812 // can't decide one way or the other on dittos: start
mjr 78:1e00b3fa11af 2813 // over.
mjr 78:1e00b3fa11af 2814 if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2815 && c.hasDittos
mjr 78:1e00b3fa11af 2816 && c.ditto)
mjr 78:1e00b3fa11af 2817 {
mjr 78:1e00b3fa11af 2818 // success - the remote uses dittos
mjr 78:1e00b3fa11af 2819 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2820 }
mjr 78:1e00b3fa11af 2821 else if (c.proId == learnedIRCode.proId
mjr 78:1e00b3fa11af 2822 && c.hasDittos
mjr 78:1e00b3fa11af 2823 && !c.ditto
mjr 78:1e00b3fa11af 2824 && c.code == learnedIRCode.code)
mjr 78:1e00b3fa11af 2825 {
mjr 78:1e00b3fa11af 2826 // success - it's a repeat of the last code, so
mjr 78:1e00b3fa11af 2827 // the remote doesn't use dittos even though the
mjr 78:1e00b3fa11af 2828 // protocol supports them
mjr 78:1e00b3fa11af 2829 learnedIRCode.hasDittos = false;
mjr 78:1e00b3fa11af 2830 IRLearningMode = 3;
mjr 78:1e00b3fa11af 2831 }
mjr 78:1e00b3fa11af 2832 else
mjr 78:1e00b3fa11af 2833 {
mjr 78:1e00b3fa11af 2834 // It's not a ditto and not a full repeat of the
mjr 78:1e00b3fa11af 2835 // last code, so it's either a new key, or some kind
mjr 78:1e00b3fa11af 2836 // of multi-code key encoding that we don't recognize.
mjr 78:1e00b3fa11af 2837 // We can't use this code, so start over.
mjr 78:1e00b3fa11af 2838 IRLearningMode = 1;
mjr 78:1e00b3fa11af 2839 }
mjr 78:1e00b3fa11af 2840 break;
mjr 78:1e00b3fa11af 2841 }
mjr 77:0b96f6867312 2842
mjr 78:1e00b3fa11af 2843 // If we ended in state 3, we've successfully decoded
mjr 78:1e00b3fa11af 2844 // the transmission. Report the decoded data and terminate
mjr 78:1e00b3fa11af 2845 // learning mode.
mjr 78:1e00b3fa11af 2846 if (IRLearningMode == 3)
mjr 77:0b96f6867312 2847 {
mjr 78:1e00b3fa11af 2848 // figure the flags:
mjr 78:1e00b3fa11af 2849 // 0x02 -> dittos
mjr 78:1e00b3fa11af 2850 uint8_t flags = 0;
mjr 78:1e00b3fa11af 2851 if (learnedIRCode.hasDittos)
mjr 78:1e00b3fa11af 2852 flags |= 0x02;
mjr 78:1e00b3fa11af 2853
mjr 78:1e00b3fa11af 2854 // report the code
mjr 78:1e00b3fa11af 2855 js.reportIRCode(learnedIRCode.proId, flags, learnedIRCode.code);
mjr 78:1e00b3fa11af 2856
mjr 78:1e00b3fa11af 2857 // exit learning mode
mjr 78:1e00b3fa11af 2858 IRLearningMode = 0;
mjr 77:0b96f6867312 2859 }
mjr 77:0b96f6867312 2860 }
mjr 77:0b96f6867312 2861
mjr 78:1e00b3fa11af 2862 // time out of IR learning mode if it's been too long
mjr 78:1e00b3fa11af 2863 if (IRLearningMode != 0 && IRTimer.read_us() > 10000000L)
mjr 77:0b96f6867312 2864 {
mjr 78:1e00b3fa11af 2865 // report the termination by sending a raw IR report with
mjr 78:1e00b3fa11af 2866 // zero data elements
mjr 78:1e00b3fa11af 2867 js.reportRawIR(0, 0);
mjr 78:1e00b3fa11af 2868
mjr 78:1e00b3fa11af 2869
mjr 78:1e00b3fa11af 2870 // cancel learning mode
mjr 77:0b96f6867312 2871 IRLearningMode = 0;
mjr 77:0b96f6867312 2872 }
mjr 77:0b96f6867312 2873 }
mjr 78:1e00b3fa11af 2874 else
mjr 77:0b96f6867312 2875 {
mjr 78:1e00b3fa11af 2876 // Not in learning mode. We don't care about the raw signals;
mjr 78:1e00b3fa11af 2877 // just run them through the protocol decoders.
mjr 78:1e00b3fa11af 2878 ir_rx->process();
mjr 78:1e00b3fa11af 2879
mjr 78:1e00b3fa11af 2880 // Check for decoded commands. Keep going until all commands
mjr 78:1e00b3fa11af 2881 // have been read.
mjr 78:1e00b3fa11af 2882 IRCommand c;
mjr 78:1e00b3fa11af 2883 while (ir_rx->readCommand(c))
mjr 77:0b96f6867312 2884 {
mjr 78:1e00b3fa11af 2885 // We received a decoded command. Determine if it's a repeat,
mjr 78:1e00b3fa11af 2886 // and if so, try to determine whether it's an auto-repeat (due
mjr 78:1e00b3fa11af 2887 // to the remote key being held down) or a distinct new press
mjr 78:1e00b3fa11af 2888 // on the same key as last time. The distinction is significant
mjr 78:1e00b3fa11af 2889 // because it affects the auto-repeat behavior of the PC key
mjr 78:1e00b3fa11af 2890 // input. An auto-repeat represents a key being held down on
mjr 78:1e00b3fa11af 2891 // the remote, which we want to translate to a (virtual) key
mjr 78:1e00b3fa11af 2892 // being held down on the PC keyboard; a distinct key press on
mjr 78:1e00b3fa11af 2893 // the remote translates to a distinct key press on the PC.
mjr 78:1e00b3fa11af 2894 //
mjr 78:1e00b3fa11af 2895 // It can only be a repeat if there's a prior command that
mjr 78:1e00b3fa11af 2896 // hasn't timed out yet, so start by checking for a previous
mjr 78:1e00b3fa11af 2897 // command.
mjr 78:1e00b3fa11af 2898 bool repeat = false, autoRepeat = false;
mjr 78:1e00b3fa11af 2899 if (IRCommandIn != 0)
mjr 77:0b96f6867312 2900 {
mjr 78:1e00b3fa11af 2901 // We have a command in progress. Check to see if the
mjr 78:1e00b3fa11af 2902 // new command is a repeat of the previous command. Check
mjr 78:1e00b3fa11af 2903 // first to see if it's a "ditto", which explicitly represents
mjr 78:1e00b3fa11af 2904 // an auto-repeat of the last command.
mjr 78:1e00b3fa11af 2905 IRCommandCfg &cmdcfg = cfg.IRCommand[IRCommandIn - 1];
mjr 78:1e00b3fa11af 2906 if (c.ditto)
mjr 78:1e00b3fa11af 2907 {
mjr 78:1e00b3fa11af 2908 // We received a ditto. Dittos are always auto-
mjr 78:1e00b3fa11af 2909 // repeats, so it's an auto-repeat as long as the
mjr 78:1e00b3fa11af 2910 // ditto is in the same protocol as the last command.
mjr 78:1e00b3fa11af 2911 // If the ditto is in a new protocol, the ditto can't
mjr 78:1e00b3fa11af 2912 // be for the last command we saw, because a ditto
mjr 78:1e00b3fa11af 2913 // never changes protocols from its antecedent. In
mjr 78:1e00b3fa11af 2914 // such a case, we must have missed the antecedent
mjr 78:1e00b3fa11af 2915 // command and thus don't know what's being repeated.
mjr 78:1e00b3fa11af 2916 repeat = autoRepeat = (c.proId == cmdcfg.protocol);
mjr 78:1e00b3fa11af 2917 }
mjr 78:1e00b3fa11af 2918 else
mjr 78:1e00b3fa11af 2919 {
mjr 78:1e00b3fa11af 2920 // It's not a ditto. The new command is a repeat if
mjr 78:1e00b3fa11af 2921 // it matches the protocol and command code of the
mjr 78:1e00b3fa11af 2922 // prior command.
mjr 78:1e00b3fa11af 2923 repeat = (c.proId == cmdcfg.protocol
mjr 78:1e00b3fa11af 2924 && uint32_t(c.code) == cmdcfg.code.lo
mjr 78:1e00b3fa11af 2925 && uint32_t(c.code >> 32) == cmdcfg.code.hi);
mjr 78:1e00b3fa11af 2926
mjr 78:1e00b3fa11af 2927 // If the command is a repeat, try to determine whether
mjr 78:1e00b3fa11af 2928 // it's an auto-repeat or a new press on the same key.
mjr 78:1e00b3fa11af 2929 // If the protocol uses dittos, it's definitely a new
mjr 78:1e00b3fa11af 2930 // key press, because an auto-repeat would have used a
mjr 78:1e00b3fa11af 2931 // ditto. For a protocol that doesn't use dittos, both
mjr 78:1e00b3fa11af 2932 // an auto-repeat and a new key press just send the key
mjr 78:1e00b3fa11af 2933 // code again, so we can't tell the difference based on
mjr 78:1e00b3fa11af 2934 // that alone. But if the protocol has a toggle bit, we
mjr 78:1e00b3fa11af 2935 // can tell by the toggle bit value: a new key press has
mjr 78:1e00b3fa11af 2936 // the opposite toggle value as the last key press, while
mjr 78:1e00b3fa11af 2937 // an auto-repeat has the same toggle. Note that if the
mjr 78:1e00b3fa11af 2938 // protocol doesn't use toggle bits, the toggle value
mjr 78:1e00b3fa11af 2939 // will always be the same, so we'll simply always treat
mjr 78:1e00b3fa11af 2940 // any repeat as an auto-repeat. Many protocols simply
mjr 78:1e00b3fa11af 2941 // provide no way to distinguish the two, so in such
mjr 78:1e00b3fa11af 2942 // cases it's consistent with the native implementations
mjr 78:1e00b3fa11af 2943 // to treat any repeat as an auto-repeat.
mjr 78:1e00b3fa11af 2944 autoRepeat =
mjr 78:1e00b3fa11af 2945 repeat
mjr 78:1e00b3fa11af 2946 && !(cmdcfg.flags & IRFlagDittos)
mjr 78:1e00b3fa11af 2947 && c.toggle == lastIRToggle;
mjr 78:1e00b3fa11af 2948 }
mjr 78:1e00b3fa11af 2949 }
mjr 78:1e00b3fa11af 2950
mjr 78:1e00b3fa11af 2951 // Check to see if it's a repeat of any kind
mjr 78:1e00b3fa11af 2952 if (repeat)
mjr 78:1e00b3fa11af 2953 {
mjr 78:1e00b3fa11af 2954 // It's a repeat. If it's not an auto-repeat, it's a
mjr 78:1e00b3fa11af 2955 // new distinct key press, so we need to send the PC a
mjr 78:1e00b3fa11af 2956 // momentary gap where we're not sending the same key,
mjr 78:1e00b3fa11af 2957 // so that the PC also recognizes this as a distinct
mjr 78:1e00b3fa11af 2958 // key press event.
mjr 78:1e00b3fa11af 2959 if (!autoRepeat)
mjr 78:1e00b3fa11af 2960 IRKeyGap = true;
mjr 78:1e00b3fa11af 2961
mjr 78:1e00b3fa11af 2962 // restart the key-up timer
mjr 78:1e00b3fa11af 2963 IRTimer.reset();
mjr 78:1e00b3fa11af 2964 }
mjr 78:1e00b3fa11af 2965 else if (c.ditto)
mjr 78:1e00b3fa11af 2966 {
mjr 78:1e00b3fa11af 2967 // It's a ditto, but not a repeat of the last command.
mjr 78:1e00b3fa11af 2968 // But a ditto doesn't contain any information of its own
mjr 78:1e00b3fa11af 2969 // on the command being repeated, so given that it's not
mjr 78:1e00b3fa11af 2970 // our last command, we can't infer what command the ditto
mjr 78:1e00b3fa11af 2971 // is for and thus can't make sense of it. We have to
mjr 78:1e00b3fa11af 2972 // simply ignore it and wait for the sender to start with
mjr 78:1e00b3fa11af 2973 // a full command for a new key press.
mjr 78:1e00b3fa11af 2974 IRCommandIn = 0;
mjr 77:0b96f6867312 2975 }
mjr 77:0b96f6867312 2976 else
mjr 77:0b96f6867312 2977 {
mjr 78:1e00b3fa11af 2978 // It's not a repeat, so the last command is no longer
mjr 78:1e00b3fa11af 2979 // in effect (regardless of whether we find a match for
mjr 78:1e00b3fa11af 2980 // the new command).
mjr 78:1e00b3fa11af 2981 IRCommandIn = 0;
mjr 77:0b96f6867312 2982
mjr 78:1e00b3fa11af 2983 // Check to see if we recognize the new command, by
mjr 78:1e00b3fa11af 2984 // searching for a match in our learned code list.
mjr 78:1e00b3fa11af 2985 for (int i = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 2986 {
mjr 78:1e00b3fa11af 2987 // if the protocol and command code from the code
mjr 78:1e00b3fa11af 2988 // list both match the input, it's a match
mjr 78:1e00b3fa11af 2989 IRCommandCfg &cmdcfg = cfg.IRCommand[i];
mjr 78:1e00b3fa11af 2990 if (cmdcfg.protocol == c.proId
mjr 78:1e00b3fa11af 2991 && cmdcfg.code.lo == uint32_t(c.code)
mjr 78:1e00b3fa11af 2992 && cmdcfg.code.hi == uint32_t(c.code >> 32))
mjr 78:1e00b3fa11af 2993 {
mjr 78:1e00b3fa11af 2994 // Found it! Make this the last command, and
mjr 78:1e00b3fa11af 2995 // remember the starting time.
mjr 78:1e00b3fa11af 2996 IRCommandIn = i + 1;
mjr 78:1e00b3fa11af 2997 lastIRToggle = c.toggle;
mjr 78:1e00b3fa11af 2998 IRTimer.reset();
mjr 78:1e00b3fa11af 2999
mjr 78:1e00b3fa11af 3000 // no need to keep searching
mjr 78:1e00b3fa11af 3001 break;
mjr 78:1e00b3fa11af 3002 }
mjr 77:0b96f6867312 3003 }
mjr 77:0b96f6867312 3004 }
mjr 77:0b96f6867312 3005 }
mjr 77:0b96f6867312 3006 }
mjr 77:0b96f6867312 3007 }
mjr 77:0b96f6867312 3008 }
mjr 77:0b96f6867312 3009
mjr 74:822a92bc11d2 3010
mjr 11:bd9da7088e6e 3011 // ---------------------------------------------------------------------------
mjr 11:bd9da7088e6e 3012 //
mjr 11:bd9da7088e6e 3013 // Button input
mjr 11:bd9da7088e6e 3014 //
mjr 11:bd9da7088e6e 3015
mjr 18:5e890ebd0023 3016 // button state
mjr 18:5e890ebd0023 3017 struct ButtonState
mjr 18:5e890ebd0023 3018 {
mjr 38:091e511ce8a0 3019 ButtonState()
mjr 38:091e511ce8a0 3020 {
mjr 53:9b2611964afc 3021 physState = logState = prevLogState = 0;
mjr 53:9b2611964afc 3022 virtState = 0;
mjr 53:9b2611964afc 3023 dbState = 0;
mjr 38:091e511ce8a0 3024 pulseState = 0;
mjr 53:9b2611964afc 3025 pulseTime = 0;
mjr 38:091e511ce8a0 3026 }
mjr 35:e959ffba78fd 3027
mjr 53:9b2611964afc 3028 // "Virtually" press or un-press the button. This can be used to
mjr 53:9b2611964afc 3029 // control the button state via a software (virtual) source, such as
mjr 53:9b2611964afc 3030 // the ZB Launch Ball feature.
mjr 53:9b2611964afc 3031 //
mjr 53:9b2611964afc 3032 // To allow sharing of one button by multiple virtual sources, each
mjr 53:9b2611964afc 3033 // virtual source must keep track of its own state internally, and
mjr 53:9b2611964afc 3034 // only call this routine to CHANGE the state. This is because calls
mjr 53:9b2611964afc 3035 // to this routine are additive: turning the button ON twice will
mjr 53:9b2611964afc 3036 // require turning it OFF twice before it actually turns off.
mjr 53:9b2611964afc 3037 void virtPress(bool on)
mjr 53:9b2611964afc 3038 {
mjr 53:9b2611964afc 3039 // Increment or decrement the current state
mjr 53:9b2611964afc 3040 virtState += on ? 1 : -1;
mjr 53:9b2611964afc 3041 }
mjr 53:9b2611964afc 3042
mjr 53:9b2611964afc 3043 // DigitalIn for the button, if connected to a physical input
mjr 73:4e8ce0b18915 3044 TinyDigitalIn di;
mjr 38:091e511ce8a0 3045
mjr 65:739875521aae 3046 // Time of last pulse state transition.
mjr 65:739875521aae 3047 //
mjr 65:739875521aae 3048 // Each state change sticks for a minimum period; when the timer expires,
mjr 65:739875521aae 3049 // if the underlying physical switch is in a different state, we switch
mjr 65:739875521aae 3050 // to the next state and restart the timer. pulseTime is the time remaining
mjr 65:739875521aae 3051 // remaining before we can make another state transition, in microseconds.
mjr 65:739875521aae 3052 // The state transitions require a complete cycle, 1 -> 2 -> 3 -> 4 -> 1...;
mjr 65:739875521aae 3053 // this guarantees that the parity of the pulse count always matches the
mjr 65:739875521aae 3054 // current physical switch state when the latter is stable, which makes
mjr 65:739875521aae 3055 // it impossible to "trick" the host by rapidly toggling the switch state.
mjr 65:739875521aae 3056 // (On my original Pinscape cabinet, I had a hardware pulse generator
mjr 65:739875521aae 3057 // for coin door, and that *was* possible to trick by rapid toggling.
mjr 65:739875521aae 3058 // This software system can't be fooled that way.)
mjr 65:739875521aae 3059 uint32_t pulseTime;
mjr 18:5e890ebd0023 3060
mjr 65:739875521aae 3061 // Config key index. This points to the ButtonCfg structure in the
mjr 65:739875521aae 3062 // configuration that contains the PC key mapping for the button.
mjr 65:739875521aae 3063 uint8_t cfgIndex;
mjr 53:9b2611964afc 3064
mjr 53:9b2611964afc 3065 // Virtual press state. This is used to simulate pressing the button via
mjr 53:9b2611964afc 3066 // software inputs rather than physical inputs. To allow one button to be
mjr 53:9b2611964afc 3067 // controlled by mulitple software sources, each source should keep track
mjr 53:9b2611964afc 3068 // of its own virtual state for the button independently, and then INCREMENT
mjr 53:9b2611964afc 3069 // this variable when the source's state transitions from off to on, and
mjr 53:9b2611964afc 3070 // DECREMENT it when the source's state transitions from on to off. That
mjr 53:9b2611964afc 3071 // will make the button's pressed state the logical OR of all of the virtual
mjr 53:9b2611964afc 3072 // and physical source states.
mjr 53:9b2611964afc 3073 uint8_t virtState;
mjr 38:091e511ce8a0 3074
mjr 38:091e511ce8a0 3075 // Debounce history. On each scan, we shift in a 1 bit to the lsb if
mjr 38:091e511ce8a0 3076 // the physical key is reporting ON, and shift in a 0 bit if the physical
mjr 38:091e511ce8a0 3077 // key is reporting OFF. We consider the key to have a new stable state
mjr 38:091e511ce8a0 3078 // if we have N consecutive 0's or 1's in the low N bits (where N is
mjr 38:091e511ce8a0 3079 // a parameter that determines how long we wait for transients to settle).
mjr 53:9b2611964afc 3080 uint8_t dbState;
mjr 38:091e511ce8a0 3081
mjr 65:739875521aae 3082 // current PHYSICAL on/off state, after debouncing
mjr 65:739875521aae 3083 uint8_t physState : 1;
mjr 65:739875521aae 3084
mjr 65:739875521aae 3085 // current LOGICAL on/off state as reported to the host.
mjr 65:739875521aae 3086 uint8_t logState : 1;
mjr 65:739875521aae 3087
mjr 79:682ae3171a08 3088 // Previous logical on/off state, when keys were last processed for USB
mjr 79:682ae3171a08 3089 // reports and local effects. This lets us detect edges (transitions)
mjr 79:682ae3171a08 3090 // in the logical state, for effects that are triggered when the state
mjr 79:682ae3171a08 3091 // changes rather than merely by the button being on or off.
mjr 65:739875521aae 3092 uint8_t prevLogState : 1;
mjr 65:739875521aae 3093
mjr 65:739875521aae 3094 // Pulse state
mjr 65:739875521aae 3095 //
mjr 65:739875521aae 3096 // A button in pulse mode (selected via the config flags for the button)
mjr 65:739875521aae 3097 // transmits a brief logical button press and release each time the attached
mjr 65:739875521aae 3098 // physical switch changes state. This is useful for cases where the host
mjr 65:739875521aae 3099 // expects a key press for each change in the state of the physical switch.
mjr 65:739875521aae 3100 // The canonical example is the Coin Door switch in VPinMAME, which requires
mjr 65:739875521aae 3101 // pressing the END key to toggle the open/closed state. This software design
mjr 65:739875521aae 3102 // isn't easily implemented in a physical coin door, though; the simplest
mjr 65:739875521aae 3103 // physical sensor for the coin door state is a switch that's on when the
mjr 65:739875521aae 3104 // door is open and off when the door is closed (or vice versa, but in either
mjr 65:739875521aae 3105 // case, the switch state corresponds to the current state of the door at any
mjr 65:739875521aae 3106 // given time, rather than pulsing on state changes). The "pulse mode"
mjr 79:682ae3171a08 3107 // option bridges this gap by generating a toggle key event each time
mjr 65:739875521aae 3108 // there's a change to the physical switch's state.
mjr 38:091e511ce8a0 3109 //
mjr 38:091e511ce8a0 3110 // Pulse state:
mjr 38:091e511ce8a0 3111 // 0 -> not a pulse switch - logical key state equals physical switch state
mjr 38:091e511ce8a0 3112 // 1 -> off
mjr 38:091e511ce8a0 3113 // 2 -> transitioning off-on
mjr 38:091e511ce8a0 3114 // 3 -> on
mjr 38:091e511ce8a0 3115 // 4 -> transitioning on-off
mjr 65:739875521aae 3116 uint8_t pulseState : 3; // 5 states -> we need 3 bits
mjr 65:739875521aae 3117
mjr 65:739875521aae 3118 } __attribute__((packed));
mjr 65:739875521aae 3119
mjr 65:739875521aae 3120 ButtonState *buttonState; // live button slots, allocated on startup
mjr 65:739875521aae 3121 int8_t nButtons; // number of live button slots allocated
mjr 65:739875521aae 3122 int8_t zblButtonIndex = -1; // index of ZB Launch button slot; -1 if unused
mjr 18:5e890ebd0023 3123
mjr 66:2e3583fbd2f4 3124 // Shift button state
mjr 66:2e3583fbd2f4 3125 struct
mjr 66:2e3583fbd2f4 3126 {
mjr 66:2e3583fbd2f4 3127 int8_t index; // buttonState[] index of shift button; -1 if none
mjr 78:1e00b3fa11af 3128 uint8_t state; // current state, for "Key OR Shift" mode:
mjr 66:2e3583fbd2f4 3129 // 0 = not shifted
mjr 66:2e3583fbd2f4 3130 // 1 = shift button down, no key pressed yet
mjr 66:2e3583fbd2f4 3131 // 2 = shift button down, key pressed
mjr 78:1e00b3fa11af 3132 // 3 = released, sending pulsed keystroke
mjr 78:1e00b3fa11af 3133 uint32_t pulseTime; // time remaining in pulsed keystroke (state 3)
mjr 66:2e3583fbd2f4 3134 }
mjr 66:2e3583fbd2f4 3135 __attribute__((packed)) shiftButton;
mjr 38:091e511ce8a0 3136
mjr 38:091e511ce8a0 3137 // Button data
mjr 38:091e511ce8a0 3138 uint32_t jsButtons = 0;
mjr 38:091e511ce8a0 3139
mjr 38:091e511ce8a0 3140 // Keyboard report state. This tracks the USB keyboard state. We can
mjr 38:091e511ce8a0 3141 // report at most 6 simultaneous non-modifier keys here, plus the 8
mjr 38:091e511ce8a0 3142 // modifier keys.
mjr 38:091e511ce8a0 3143 struct
mjr 38:091e511ce8a0 3144 {
mjr 38:091e511ce8a0 3145 bool changed; // flag: changed since last report sent
mjr 48:058ace2aed1d 3146 uint8_t nkeys; // number of active keys in the list
mjr 38:091e511ce8a0 3147 uint8_t data[8]; // key state, in USB report format: byte 0 is the modifier key mask,
mjr 38:091e511ce8a0 3148 // byte 1 is reserved, and bytes 2-7 are the currently pressed key codes
mjr 38:091e511ce8a0 3149 } kbState = { false, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
mjr 38:091e511ce8a0 3150
mjr 38:091e511ce8a0 3151 // Media key state
mjr 38:091e511ce8a0 3152 struct
mjr 38:091e511ce8a0 3153 {
mjr 38:091e511ce8a0 3154 bool changed; // flag: changed since last report sent
mjr 38:091e511ce8a0 3155 uint8_t data; // key state byte for USB reports
mjr 38:091e511ce8a0 3156 } mediaState = { false, 0 };
mjr 38:091e511ce8a0 3157
mjr 79:682ae3171a08 3158 // button scan interrupt timer
mjr 79:682ae3171a08 3159 Timeout scanButtonsTimeout;
mjr 38:091e511ce8a0 3160
mjr 38:091e511ce8a0 3161 // Button scan interrupt handler. We call this periodically via
mjr 38:091e511ce8a0 3162 // a timer interrupt to scan the physical button states.
mjr 38:091e511ce8a0 3163 void scanButtons()
mjr 38:091e511ce8a0 3164 {
mjr 79:682ae3171a08 3165 // schedule the next interrupt
mjr 79:682ae3171a08 3166 scanButtonsTimeout.attach_us(&scanButtons, 1000);
mjr 79:682ae3171a08 3167
mjr 38:091e511ce8a0 3168 // scan all button input pins
mjr 73:4e8ce0b18915 3169 ButtonState *bs = buttonState, *last = bs + nButtons;
mjr 73:4e8ce0b18915 3170 for ( ; bs < last ; ++bs)
mjr 38:091e511ce8a0 3171 {
mjr 73:4e8ce0b18915 3172 // Shift the new state into the debounce history
mjr 73:4e8ce0b18915 3173 uint8_t db = (bs->dbState << 1) | bs->di.read();
mjr 73:4e8ce0b18915 3174 bs->dbState = db;
mjr 73:4e8ce0b18915 3175
mjr 73:4e8ce0b18915 3176 // If we have all 0's or 1's in the history for the required
mjr 73:4e8ce0b18915 3177 // debounce period, the key state is stable, so apply the new
mjr 73:4e8ce0b18915 3178 // physical state. Note that the pins are active low, so the
mjr 73:4e8ce0b18915 3179 // new button on/off state is the inverse of the GPIO state.
mjr 73:4e8ce0b18915 3180 const uint8_t stable = 0x1F; // 00011111b -> low 5 bits = last 5 readings
mjr 73:4e8ce0b18915 3181 db &= stable;
mjr 73:4e8ce0b18915 3182 if (db == 0 || db == stable)
mjr 73:4e8ce0b18915 3183 bs->physState = !db;
mjr 38:091e511ce8a0 3184 }
mjr 38:091e511ce8a0 3185 }
mjr 38:091e511ce8a0 3186
mjr 38:091e511ce8a0 3187 // Button state transition timer. This is used for pulse buttons, to
mjr 38:091e511ce8a0 3188 // control the timing of the logical key presses generated by transitions
mjr 38:091e511ce8a0 3189 // in the physical button state.
mjr 38:091e511ce8a0 3190 Timer buttonTimer;
mjr 12:669df364a565 3191
mjr 65:739875521aae 3192 // Count a button during the initial setup scan
mjr 72:884207c0aab0 3193 void countButton(uint8_t typ, uint8_t shiftTyp, bool &kbKeys)
mjr 65:739875521aae 3194 {
mjr 65:739875521aae 3195 // count it
mjr 65:739875521aae 3196 ++nButtons;
mjr 65:739875521aae 3197
mjr 67:c39e66c4e000 3198 // if it's a keyboard key or media key, note that we need a USB
mjr 67:c39e66c4e000 3199 // keyboard interface
mjr 72:884207c0aab0 3200 if (typ == BtnTypeKey || typ == BtnTypeMedia
mjr 72:884207c0aab0 3201 || shiftTyp == BtnTypeKey || shiftTyp == BtnTypeMedia)
mjr 65:739875521aae 3202 kbKeys = true;
mjr 65:739875521aae 3203 }
mjr 65:739875521aae 3204
mjr 11:bd9da7088e6e 3205 // initialize the button inputs
mjr 35:e959ffba78fd 3206 void initButtons(Config &cfg, bool &kbKeys)
mjr 11:bd9da7088e6e 3207 {
mjr 66:2e3583fbd2f4 3208 // presume no shift key
mjr 66:2e3583fbd2f4 3209 shiftButton.index = -1;
mjr 82:4f6209cb5c33 3210 shiftButton.state = 0;
mjr 66:2e3583fbd2f4 3211
mjr 65:739875521aae 3212 // Count up how many button slots we'll need to allocate. Start
mjr 65:739875521aae 3213 // with assigned buttons from the configuration, noting that we
mjr 65:739875521aae 3214 // only need to create slots for buttons that are actually wired.
mjr 65:739875521aae 3215 nButtons = 0;
mjr 65:739875521aae 3216 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 3217 {
mjr 65:739875521aae 3218 // it's valid if it's wired to a real input pin
mjr 65:739875521aae 3219 if (wirePinName(cfg.button[i].pin) != NC)
mjr 72:884207c0aab0 3220 countButton(cfg.button[i].typ, cfg.button[i].typ2, kbKeys);
mjr 65:739875521aae 3221 }
mjr 65:739875521aae 3222
mjr 65:739875521aae 3223 // Count virtual buttons
mjr 65:739875521aae 3224
mjr 65:739875521aae 3225 // ZB Launch
mjr 65:739875521aae 3226 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 65:739875521aae 3227 {
mjr 65:739875521aae 3228 // valid - remember the live button index
mjr 65:739875521aae 3229 zblButtonIndex = nButtons;
mjr 65:739875521aae 3230
mjr 65:739875521aae 3231 // count it
mjr 72:884207c0aab0 3232 countButton(cfg.plunger.zbLaunchBall.keytype, BtnTypeNone, kbKeys);
mjr 65:739875521aae 3233 }
mjr 65:739875521aae 3234
mjr 65:739875521aae 3235 // Allocate the live button slots
mjr 65:739875521aae 3236 ButtonState *bs = buttonState = new ButtonState[nButtons];
mjr 65:739875521aae 3237
mjr 65:739875521aae 3238 // Configure the physical inputs
mjr 65:739875521aae 3239 for (int i = 0 ; i < MAX_BUTTONS ; ++i)
mjr 65:739875521aae 3240 {
mjr 65:739875521aae 3241 PinName pin = wirePinName(cfg.button[i].pin);
mjr 65:739875521aae 3242 if (pin != NC)
mjr 65:739875521aae 3243 {
mjr 65:739875521aae 3244 // point back to the config slot for the keyboard data
mjr 65:739875521aae 3245 bs->cfgIndex = i;
mjr 65:739875521aae 3246
mjr 65:739875521aae 3247 // set up the GPIO input pin for this button
mjr 73:4e8ce0b18915 3248 bs->di.assignPin(pin);
mjr 65:739875521aae 3249
mjr 65:739875521aae 3250 // if it's a pulse mode button, set the initial pulse state to Off
mjr 65:739875521aae 3251 if (cfg.button[i].flags & BtnFlagPulse)
mjr 65:739875521aae 3252 bs->pulseState = 1;
mjr 65:739875521aae 3253
mjr 66:2e3583fbd2f4 3254 // If this is the shift button, note its buttonState[] index.
mjr 66:2e3583fbd2f4 3255 // We have to figure the buttonState[] index separately from
mjr 66:2e3583fbd2f4 3256 // the config index, because the indices can differ if some
mjr 66:2e3583fbd2f4 3257 // config slots are left unused.
mjr 78:1e00b3fa11af 3258 if (cfg.shiftButton.idx == i+1)
mjr 66:2e3583fbd2f4 3259 shiftButton.index = bs - buttonState;
mjr 66:2e3583fbd2f4 3260
mjr 65:739875521aae 3261 // advance to the next button
mjr 65:739875521aae 3262 ++bs;
mjr 65:739875521aae 3263 }
mjr 65:739875521aae 3264 }
mjr 65:739875521aae 3265
mjr 53:9b2611964afc 3266 // Configure the virtual buttons. These are buttons controlled via
mjr 53:9b2611964afc 3267 // software triggers rather than physical GPIO inputs. The virtual
mjr 53:9b2611964afc 3268 // buttons have the same control structures as regular buttons, but
mjr 53:9b2611964afc 3269 // they get their configuration data from other config variables.
mjr 53:9b2611964afc 3270
mjr 53:9b2611964afc 3271 // ZB Launch Ball button
mjr 65:739875521aae 3272 if (cfg.plunger.zbLaunchBall.port != 0)
mjr 11:bd9da7088e6e 3273 {
mjr 65:739875521aae 3274 // Point back to the config slot for the keyboard data.
mjr 66:2e3583fbd2f4 3275 // We use a special extra slot for virtual buttons,
mjr 66:2e3583fbd2f4 3276 // so we also need to set up the slot data by copying
mjr 66:2e3583fbd2f4 3277 // the ZBL config data to our virtual button slot.
mjr 65:739875521aae 3278 bs->cfgIndex = ZBL_BUTTON_CFG;
mjr 65:739875521aae 3279 cfg.button[ZBL_BUTTON_CFG].pin = PINNAME_TO_WIRE(NC);
mjr 65:739875521aae 3280 cfg.button[ZBL_BUTTON_CFG].typ = cfg.plunger.zbLaunchBall.keytype;
mjr 65:739875521aae 3281 cfg.button[ZBL_BUTTON_CFG].val = cfg.plunger.zbLaunchBall.keycode;
mjr 65:739875521aae 3282
mjr 66:2e3583fbd2f4 3283 // advance to the next button
mjr 65:739875521aae 3284 ++bs;
mjr 11:bd9da7088e6e 3285 }
mjr 12:669df364a565 3286
mjr 38:091e511ce8a0 3287 // start the button scan thread
mjr 79:682ae3171a08 3288 scanButtonsTimeout.attach_us(scanButtons, 1000);
mjr 38:091e511ce8a0 3289
mjr 38:091e511ce8a0 3290 // start the button state transition timer
mjr 12:669df364a565 3291 buttonTimer.start();
mjr 11:bd9da7088e6e 3292 }
mjr 11:bd9da7088e6e 3293
mjr 67:c39e66c4e000 3294 // Media key mapping. This maps from an 8-bit USB media key
mjr 67:c39e66c4e000 3295 // code to the corresponding bit in our USB report descriptor.
mjr 67:c39e66c4e000 3296 // The USB key code is the index, and the value at the index
mjr 67:c39e66c4e000 3297 // is the report descriptor bit. See joystick.cpp for the
mjr 67:c39e66c4e000 3298 // media descriptor details. Our currently mapped keys are:
mjr 67:c39e66c4e000 3299 //
mjr 67:c39e66c4e000 3300 // 0xE2 -> Mute -> 0x01
mjr 67:c39e66c4e000 3301 // 0xE9 -> Volume Up -> 0x02
mjr 67:c39e66c4e000 3302 // 0xEA -> Volume Down -> 0x04
mjr 67:c39e66c4e000 3303 // 0xB5 -> Next Track -> 0x08
mjr 67:c39e66c4e000 3304 // 0xB6 -> Previous Track -> 0x10
mjr 67:c39e66c4e000 3305 // 0xB7 -> Stop -> 0x20
mjr 67:c39e66c4e000 3306 // 0xCD -> Play / Pause -> 0x40
mjr 67:c39e66c4e000 3307 //
mjr 67:c39e66c4e000 3308 static const uint8_t mediaKeyMap[] = {
mjr 67:c39e66c4e000 3309 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-0F
mjr 67:c39e66c4e000 3310 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-1F
mjr 67:c39e66c4e000 3311 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-2F
mjr 67:c39e66c4e000 3312 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-3F
mjr 67:c39e66c4e000 3313 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-4F
mjr 67:c39e66c4e000 3314 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-5F
mjr 67:c39e66c4e000 3315 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-6F
mjr 67:c39e66c4e000 3316 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-7F
mjr 67:c39e66c4e000 3317 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
mjr 67:c39e66c4e000 3318 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-9F
mjr 67:c39e66c4e000 3319 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A0-AF
mjr 67:c39e66c4e000 3320 0, 0, 0, 0, 0, 8, 16, 32, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
mjr 67:c39e66c4e000 3321 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, // C0-CF
mjr 67:c39e66c4e000 3322 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D0-DF
mjr 67:c39e66c4e000 3323 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, // E0-EF
mjr 67:c39e66c4e000 3324 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-FF
mjr 77:0b96f6867312 3325 };
mjr 77:0b96f6867312 3326
mjr 77:0b96f6867312 3327 // Keyboard key/joystick button state. processButtons() uses this to
mjr 77:0b96f6867312 3328 // build the set of key presses to report to the PC based on the logical
mjr 77:0b96f6867312 3329 // states of the button iputs.
mjr 77:0b96f6867312 3330 struct KeyState
mjr 77:0b96f6867312 3331 {
mjr 77:0b96f6867312 3332 KeyState()
mjr 77:0b96f6867312 3333 {
mjr 77:0b96f6867312 3334 // zero all members
mjr 77:0b96f6867312 3335 memset(this, 0, sizeof(*this));
mjr 77:0b96f6867312 3336 }
mjr 77:0b96f6867312 3337
mjr 77:0b96f6867312 3338 // Keyboard media keys currently pressed. This is a bit vector in
mjr 77:0b96f6867312 3339 // the format used in our USB keyboard reports (see USBJoystick.cpp).
mjr 77:0b96f6867312 3340 uint8_t mediakeys;
mjr 77:0b96f6867312 3341
mjr 77:0b96f6867312 3342 // Keyboard modifier (shift) keys currently pressed. This is a bit
mjr 77:0b96f6867312 3343 // vector in the format used in our USB keyboard reports (see
mjr 77:0b96f6867312 3344 // USBJoystick.cpp).
mjr 77:0b96f6867312 3345 uint8_t modkeys;
mjr 77:0b96f6867312 3346
mjr 77:0b96f6867312 3347 // Regular keyboard keys currently pressed. Each element is a USB
mjr 77:0b96f6867312 3348 // key code, or 0 for empty slots. Note that the USB report format
mjr 77:0b96f6867312 3349 // theoretically allows a flexible size limit, but the Windows KB
mjr 77:0b96f6867312 3350 // drivers have a fixed limit of 6 simultaneous keys (and won't
mjr 77:0b96f6867312 3351 // accept reports with more), so there's no point in making this
mjr 77:0b96f6867312 3352 // flexible; we'll just use the fixed size dictated by Windows.
mjr 77:0b96f6867312 3353 uint8_t keys[7];
mjr 77:0b96f6867312 3354
mjr 77:0b96f6867312 3355 // number of valid entries in keys[] array
mjr 77:0b96f6867312 3356 int nkeys;
mjr 77:0b96f6867312 3357
mjr 77:0b96f6867312 3358 // Joystick buttons pressed, as a bit vector. Bit n (1 << n)
mjr 77:0b96f6867312 3359 // represents joystick button n, n in 0..31, with 0 meaning
mjr 77:0b96f6867312 3360 // unpressed and 1 meaning pressed.
mjr 77:0b96f6867312 3361 uint32_t js;
mjr 77:0b96f6867312 3362
mjr 77:0b96f6867312 3363
mjr 77:0b96f6867312 3364 // Add a key press. 'typ' is the button type code (ButtonTypeXxx),
mjr 77:0b96f6867312 3365 // and 'val' is the value (the meaning of which varies by type code).
mjr 77:0b96f6867312 3366 void addKey(uint8_t typ, uint8_t val)
mjr 77:0b96f6867312 3367 {
mjr 77:0b96f6867312 3368 // add the key according to the type
mjr 77:0b96f6867312 3369 switch (typ)
mjr 77:0b96f6867312 3370 {
mjr 77:0b96f6867312 3371 case BtnTypeJoystick:
mjr 77:0b96f6867312 3372 // joystick button
mjr 77:0b96f6867312 3373 js |= (1 << (val - 1));
mjr 77:0b96f6867312 3374 break;
mjr 77:0b96f6867312 3375
mjr 77:0b96f6867312 3376 case BtnTypeKey:
mjr 77:0b96f6867312 3377 // Keyboard key. The USB keyboard report encodes regular
mjr 77:0b96f6867312 3378 // keys and modifier keys separately, so we need to check
mjr 77:0b96f6867312 3379 // which type we have. Note that past versions mapped the
mjr 77:0b96f6867312 3380 // Keyboard Volume Up, Keyboard Volume Down, and Keyboard
mjr 77:0b96f6867312 3381 // Mute keys to the corresponding Media keys. We no longer
mjr 77:0b96f6867312 3382 // do this; instead, we have the separate BtnTypeMedia for
mjr 77:0b96f6867312 3383 // explicitly using media keys if desired.
mjr 77:0b96f6867312 3384 if (val >= 0xE0 && val <= 0xE7)
mjr 77:0b96f6867312 3385 {
mjr 77:0b96f6867312 3386 // It's a modifier key. These are represented in the USB
mjr 77:0b96f6867312 3387 // reports with a bit mask. We arrange the mask bits in
mjr 77:0b96f6867312 3388 // the same order as the scan codes, so we can figure the
mjr 77:0b96f6867312 3389 // appropriate bit with a simple shift.
mjr 77:0b96f6867312 3390 modkeys |= (1 << (val - 0xE0));
mjr 77:0b96f6867312 3391 }
mjr 77:0b96f6867312 3392 else
mjr 77:0b96f6867312 3393 {
mjr 77:0b96f6867312 3394 // It's a regular key. Make sure it's not already in the
mjr 77:0b96f6867312 3395 // list, and that the list isn't full. If neither of these
mjr 77:0b96f6867312 3396 // apply, add the key to the key array.
mjr 77:0b96f6867312 3397 if (nkeys < 7)
mjr 77:0b96f6867312 3398 {
mjr 77:0b96f6867312 3399 bool found = false;
mjr 77:0b96f6867312 3400 for (int i = 0 ; i < nkeys ; ++i)
mjr 77:0b96f6867312 3401 {
mjr 77:0b96f6867312 3402 if (keys[i] == val)
mjr 77:0b96f6867312 3403 {
mjr 77:0b96f6867312 3404 found = true;
mjr 77:0b96f6867312 3405 break;
mjr 77:0b96f6867312 3406 }
mjr 77:0b96f6867312 3407 }
mjr 77:0b96f6867312 3408 if (!found)
mjr 77:0b96f6867312 3409 keys[nkeys++] = val;
mjr 77:0b96f6867312 3410 }
mjr 77:0b96f6867312 3411 }
mjr 77:0b96f6867312 3412 break;
mjr 77:0b96f6867312 3413
mjr 77:0b96f6867312 3414 case BtnTypeMedia:
mjr 77:0b96f6867312 3415 // Media control key. The media keys are mapped in the USB
mjr 77:0b96f6867312 3416 // report to bits, whereas the key codes are specified in the
mjr 77:0b96f6867312 3417 // config with their USB usage numbers. E.g., the config val
mjr 77:0b96f6867312 3418 // for Media Next Track is 0xB5, but we encode this in the USB
mjr 77:0b96f6867312 3419 // report as bit 0x08. The mediaKeyMap[] table translates
mjr 77:0b96f6867312 3420 // from the USB usage number to the mask bit. If the key isn't
mjr 77:0b96f6867312 3421 // among the subset we support, the mapped bit will be zero, so
mjr 77:0b96f6867312 3422 // the "|=" will have no effect and the key will be ignored.
mjr 77:0b96f6867312 3423 mediakeys |= mediaKeyMap[val];
mjr 77:0b96f6867312 3424 break;
mjr 77:0b96f6867312 3425 }
mjr 77:0b96f6867312 3426 }
mjr 77:0b96f6867312 3427 };
mjr 67:c39e66c4e000 3428
mjr 67:c39e66c4e000 3429
mjr 38:091e511ce8a0 3430 // Process the button state. This sets up the joystick, keyboard, and
mjr 38:091e511ce8a0 3431 // media control descriptors with the current state of keys mapped to
mjr 38:091e511ce8a0 3432 // those HID interfaces, and executes the local effects for any keys
mjr 38:091e511ce8a0 3433 // mapped to special device functions (e.g., Night Mode).
mjr 53:9b2611964afc 3434 void processButtons(Config &cfg)
mjr 35:e959ffba78fd 3435 {
mjr 77:0b96f6867312 3436 // key state
mjr 77:0b96f6867312 3437 KeyState ks;
mjr 38:091e511ce8a0 3438
mjr 38:091e511ce8a0 3439 // calculate the time since the last run
mjr 53:9b2611964afc 3440 uint32_t dt = buttonTimer.read_us();
mjr 18:5e890ebd0023 3441 buttonTimer.reset();
mjr 66:2e3583fbd2f4 3442
mjr 66:2e3583fbd2f4 3443 // check the shift button state
mjr 66:2e3583fbd2f4 3444 if (shiftButton.index != -1)
mjr 66:2e3583fbd2f4 3445 {
mjr 78:1e00b3fa11af 3446 // get the shift button's physical state object
mjr 66:2e3583fbd2f4 3447 ButtonState *sbs = &buttonState[shiftButton.index];
mjr 78:1e00b3fa11af 3448
mjr 78:1e00b3fa11af 3449 // figure what to do based on the shift button mode in the config
mjr 78:1e00b3fa11af 3450 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 3451 {
mjr 66:2e3583fbd2f4 3452 case 0:
mjr 78:1e00b3fa11af 3453 default:
mjr 78:1e00b3fa11af 3454 // "Shift OR Key" mode. The shift button doesn't send its key
mjr 78:1e00b3fa11af 3455 // immediately when pressed. Instead, we wait to see what
mjr 78:1e00b3fa11af 3456 // happens while it's down. Check the current cycle state.
mjr 78:1e00b3fa11af 3457 switch (shiftButton.state)
mjr 78:1e00b3fa11af 3458 {
mjr 78:1e00b3fa11af 3459 case 0:
mjr 78:1e00b3fa11af 3460 // Not shifted. Check if the button is now down: if so,
mjr 78:1e00b3fa11af 3461 // switch to state 1 (shift button down, no key pressed yet).
mjr 78:1e00b3fa11af 3462 if (sbs->physState)
mjr 78:1e00b3fa11af 3463 shiftButton.state = 1;
mjr 78:1e00b3fa11af 3464 break;
mjr 78:1e00b3fa11af 3465
mjr 78:1e00b3fa11af 3466 case 1:
mjr 78:1e00b3fa11af 3467 // Shift button down, no key pressed yet. If the button is
mjr 78:1e00b3fa11af 3468 // now up, it counts as an ordinary button press instead of
mjr 78:1e00b3fa11af 3469 // a shift button press, since the shift function was never
mjr 78:1e00b3fa11af 3470 // used. Return to unshifted state and start a timed key
mjr 78:1e00b3fa11af 3471 // pulse event.
mjr 78:1e00b3fa11af 3472 if (!sbs->physState)
mjr 78:1e00b3fa11af 3473 {
mjr 78:1e00b3fa11af 3474 shiftButton.state = 3;
mjr 78:1e00b3fa11af 3475 shiftButton.pulseTime = 50000+dt; // 50 ms left on the key pulse
mjr 78:1e00b3fa11af 3476 }
mjr 78:1e00b3fa11af 3477 break;
mjr 78:1e00b3fa11af 3478
mjr 78:1e00b3fa11af 3479 case 2:
mjr 78:1e00b3fa11af 3480 // Shift button down, other key was pressed. If the button is
mjr 78:1e00b3fa11af 3481 // now up, simply clear the shift state without sending a key
mjr 78:1e00b3fa11af 3482 // press for the shift button itself to the PC. The shift
mjr 78:1e00b3fa11af 3483 // function was used, so its ordinary key press function is
mjr 78:1e00b3fa11af 3484 // suppressed.
mjr 78:1e00b3fa11af 3485 if (!sbs->physState)
mjr 78:1e00b3fa11af 3486 shiftButton.state = 0;
mjr 78:1e00b3fa11af 3487 break;
mjr 78:1e00b3fa11af 3488
mjr 78:1e00b3fa11af 3489 case 3:
mjr 78:1e00b3fa11af 3490 // Sending pulsed keystroke. Deduct the current time interval
mjr 78:1e00b3fa11af 3491 // from the remaining pulse timer. End the pulse if the time
mjr 78:1e00b3fa11af 3492 // has expired.
mjr 78:1e00b3fa11af 3493 if (shiftButton.pulseTime > dt)
mjr 78:1e00b3fa11af 3494 shiftButton.pulseTime -= dt;
mjr 78:1e00b3fa11af 3495 else
mjr 78:1e00b3fa11af 3496 shiftButton.state = 0;
mjr 78:1e00b3fa11af 3497 break;
mjr 78:1e00b3fa11af 3498 }
mjr 66:2e3583fbd2f4 3499 break;
mjr 66:2e3583fbd2f4 3500
mjr 66:2e3583fbd2f4 3501 case 1:
mjr 78:1e00b3fa11af 3502 // "Shift AND Key" mode. In this mode, the shift button acts
mjr 78:1e00b3fa11af 3503 // like any other button and sends its mapped key immediately.
mjr 78:1e00b3fa11af 3504 // The state cycle in this case simply matches the physical
mjr 78:1e00b3fa11af 3505 // state: ON -> cycle state 1, OFF -> cycle state 0.
mjr 78:1e00b3fa11af 3506 shiftButton.state = (sbs->physState ? 1 : 0);
mjr 66:2e3583fbd2f4 3507 break;
mjr 66:2e3583fbd2f4 3508 }
mjr 66:2e3583fbd2f4 3509 }
mjr 38:091e511ce8a0 3510
mjr 11:bd9da7088e6e 3511 // scan the button list
mjr 18:5e890ebd0023 3512 ButtonState *bs = buttonState;
mjr 65:739875521aae 3513 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 11:bd9da7088e6e 3514 {
mjr 77:0b96f6867312 3515 // get the config entry for the button
mjr 77:0b96f6867312 3516 ButtonCfg *bc = &cfg.button[bs->cfgIndex];
mjr 77:0b96f6867312 3517
mjr 66:2e3583fbd2f4 3518 // Check the button type:
mjr 66:2e3583fbd2f4 3519 // - shift button
mjr 66:2e3583fbd2f4 3520 // - pulsed button
mjr 66:2e3583fbd2f4 3521 // - regular button
mjr 66:2e3583fbd2f4 3522 if (shiftButton.index == i)
mjr 66:2e3583fbd2f4 3523 {
mjr 78:1e00b3fa11af 3524 // This is the shift button. The logical state handling
mjr 78:1e00b3fa11af 3525 // depends on the mode.
mjr 78:1e00b3fa11af 3526 switch (cfg.shiftButton.mode)
mjr 66:2e3583fbd2f4 3527 {
mjr 78:1e00b3fa11af 3528 case 0:
mjr 78:1e00b3fa11af 3529 default:
mjr 78:1e00b3fa11af 3530 // "Shift OR Key" mode. The logical state is ON only
mjr 78:1e00b3fa11af 3531 // during the timed pulse when the key is released, which
mjr 78:1e00b3fa11af 3532 // is signified by shift button state 3.
mjr 78:1e00b3fa11af 3533 bs->logState = (shiftButton.state == 3);
mjr 78:1e00b3fa11af 3534 break;
mjr 78:1e00b3fa11af 3535
mjr 78:1e00b3fa11af 3536 case 1:
mjr 78:1e00b3fa11af 3537 // "Shif AND Key" mode. The shift button acts like any
mjr 78:1e00b3fa11af 3538 // other button, so it's logically on when physically on.
mjr 78:1e00b3fa11af 3539 bs->logState = bs->physState;
mjr 78:1e00b3fa11af 3540 break;
mjr 66:2e3583fbd2f4 3541 }
mjr 66:2e3583fbd2f4 3542 }
mjr 66:2e3583fbd2f4 3543 else if (bs->pulseState != 0)
mjr 18:5e890ebd0023 3544 {
mjr 38:091e511ce8a0 3545 // if the timer has expired, check for state changes
mjr 53:9b2611964afc 3546 if (bs->pulseTime > dt)
mjr 18:5e890ebd0023 3547 {
mjr 53:9b2611964afc 3548 // not expired yet - deduct the last interval
mjr 53:9b2611964afc 3549 bs->pulseTime -= dt;
mjr 53:9b2611964afc 3550 }
mjr 53:9b2611964afc 3551 else
mjr 53:9b2611964afc 3552 {
mjr 53:9b2611964afc 3553 // pulse time expired - check for a state change
mjr 53:9b2611964afc 3554 const uint32_t pulseLength = 200000UL; // 200 milliseconds
mjr 38:091e511ce8a0 3555 switch (bs->pulseState)
mjr 18:5e890ebd0023 3556 {
mjr 38:091e511ce8a0 3557 case 1:
mjr 38:091e511ce8a0 3558 // off - if the physical switch is now on, start a button pulse
mjr 53:9b2611964afc 3559 if (bs->physState)
mjr 53:9b2611964afc 3560 {
mjr 38:091e511ce8a0 3561 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 3562 bs->pulseState = 2;
mjr 53:9b2611964afc 3563 bs->logState = 1;
mjr 38:091e511ce8a0 3564 }
mjr 38:091e511ce8a0 3565 break;
mjr 18:5e890ebd0023 3566
mjr 38:091e511ce8a0 3567 case 2:
mjr 38:091e511ce8a0 3568 // transitioning off to on - end the pulse, and start a gap
mjr 38:091e511ce8a0 3569 // equal to the pulse time so that the host can observe the
mjr 38:091e511ce8a0 3570 // change in state in the logical button
mjr 38:091e511ce8a0 3571 bs->pulseState = 3;
mjr 38:091e511ce8a0 3572 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 3573 bs->logState = 0;
mjr 38:091e511ce8a0 3574 break;
mjr 38:091e511ce8a0 3575
mjr 38:091e511ce8a0 3576 case 3:
mjr 38:091e511ce8a0 3577 // on - if the physical switch is now off, start a button pulse
mjr 53:9b2611964afc 3578 if (!bs->physState)
mjr 53:9b2611964afc 3579 {
mjr 38:091e511ce8a0 3580 bs->pulseTime = pulseLength;
mjr 38:091e511ce8a0 3581 bs->pulseState = 4;
mjr 53:9b2611964afc 3582 bs->logState = 1;
mjr 38:091e511ce8a0 3583 }
mjr 38:091e511ce8a0 3584 break;
mjr 38:091e511ce8a0 3585
mjr 38:091e511ce8a0 3586 case 4:
mjr 38:091e511ce8a0 3587 // transitioning on to off - end the pulse, and start a gap
mjr 38:091e511ce8a0 3588 bs->pulseState = 1;
mjr 38:091e511ce8a0 3589 bs->pulseTime = pulseLength;
mjr 53:9b2611964afc 3590 bs->logState = 0;
mjr 38:091e511ce8a0 3591 break;
mjr 18:5e890ebd0023 3592 }
mjr 18:5e890ebd0023 3593 }
mjr 38:091e511ce8a0 3594 }
mjr 38:091e511ce8a0 3595 else
mjr 38:091e511ce8a0 3596 {
mjr 38:091e511ce8a0 3597 // not a pulse switch - the logical state is the same as the physical state
mjr 53:9b2611964afc 3598 bs->logState = bs->physState;
mjr 38:091e511ce8a0 3599 }
mjr 77:0b96f6867312 3600
mjr 77:0b96f6867312 3601 // Determine if we're going to use the shifted version of the
mjr 78:1e00b3fa11af 3602 // button. We're using the shifted version if...
mjr 78:1e00b3fa11af 3603 //
mjr 78:1e00b3fa11af 3604 // - the shift button is down, AND
mjr 78:1e00b3fa11af 3605 // - this button isn't itself the shift button, AND
mjr 78:1e00b3fa11af 3606 // - this button has some kind of shifted meaning
mjr 77:0b96f6867312 3607 //
mjr 78:1e00b3fa11af 3608 // A "shifted meaning" means that we have any of the following
mjr 78:1e00b3fa11af 3609 // assigned to the shifted version of the button: a key assignment,
mjr 78:1e00b3fa11af 3610 // (in typ2,key2), an IR command (in IRCommand2), or Night mode.
mjr 78:1e00b3fa11af 3611 //
mjr 78:1e00b3fa11af 3612 // The test for Night Mode is a bit tricky. The shifted version of
mjr 78:1e00b3fa11af 3613 // the button is the Night Mode toggle if the button matches the
mjr 78:1e00b3fa11af 3614 // Night Mode button index, AND its flags are set with "toggle mode
mjr 78:1e00b3fa11af 3615 // ON" (bit 0x02 is on) and "switch mode OFF" (bit 0x01 is off).
mjr 78:1e00b3fa11af 3616 // So (button flags) & 0x03 must equal 0x02.
mjr 77:0b96f6867312 3617 bool useShift =
mjr 77:0b96f6867312 3618 (shiftButton.state != 0
mjr 78:1e00b3fa11af 3619 && shiftButton.index != i
mjr 77:0b96f6867312 3620 && (bc->typ2 != BtnTypeNone
mjr 77:0b96f6867312 3621 || bc->IRCommand2 != 0
mjr 77:0b96f6867312 3622 || (cfg.nightMode.btn == i+1 && (cfg.nightMode.flags & 0x03) == 0x02)));
mjr 77:0b96f6867312 3623
mjr 77:0b96f6867312 3624 // If we're using the shift function, and no other button has used
mjr 77:0b96f6867312 3625 // the shift function yet (shift state 1: "shift button is down but
mjr 77:0b96f6867312 3626 // no one has used the shift function yet"), then we've "consumed"
mjr 77:0b96f6867312 3627 // the shift button press (so go to shift state 2: "shift button has
mjr 77:0b96f6867312 3628 // been used by some other button press that has a shifted meaning").
mjr 78:1e00b3fa11af 3629 if (useShift && shiftButton.state == 1 && bs->logState)
mjr 77:0b96f6867312 3630 shiftButton.state = 2;
mjr 35:e959ffba78fd 3631
mjr 38:091e511ce8a0 3632 // carry out any edge effects from buttons changing states
mjr 53:9b2611964afc 3633 if (bs->logState != bs->prevLogState)
mjr 38:091e511ce8a0 3634 {
mjr 77:0b96f6867312 3635 // check to see if this is the Night Mode button
mjr 53:9b2611964afc 3636 if (cfg.nightMode.btn == i + 1)
mjr 35:e959ffba78fd 3637 {
mjr 77:0b96f6867312 3638 // Check the switch type in the config flags. If flag 0x01 is
mjr 77:0b96f6867312 3639 // set, it's a persistent on/off switch, so the night mode
mjr 77:0b96f6867312 3640 // state simply tracks the current state of the switch.
mjr 77:0b96f6867312 3641 // Otherwise, it's a momentary button, so each button push
mjr 77:0b96f6867312 3642 // (i.e., each transition from logical state OFF to ON) toggles
mjr 77:0b96f6867312 3643 // the night mode state.
mjr 77:0b96f6867312 3644 //
mjr 77:0b96f6867312 3645 // Note that the "shift" flag (0x02) has no effect in switch
mjr 77:0b96f6867312 3646 // mode. Shifting only works for toggle mode.
mjr 82:4f6209cb5c33 3647 if ((cfg.nightMode.flags & 0x01) != 0)
mjr 53:9b2611964afc 3648 {
mjr 77:0b96f6867312 3649 // It's an on/off switch. Night mode simply tracks the
mjr 77:0b96f6867312 3650 // current switch state.
mjr 53:9b2611964afc 3651 setNightMode(bs->logState);
mjr 53:9b2611964afc 3652 }
mjr 82:4f6209cb5c33 3653 else if (bs->logState)
mjr 53:9b2611964afc 3654 {
mjr 77:0b96f6867312 3655 // It's a momentary toggle switch. Toggle the night mode
mjr 77:0b96f6867312 3656 // state on each distinct press of the button: that is,
mjr 77:0b96f6867312 3657 // whenever the button's logical state transitions from
mjr 77:0b96f6867312 3658 // OFF to ON.
mjr 66:2e3583fbd2f4 3659 //
mjr 77:0b96f6867312 3660 // The "shift" flag (0x02) tells us whether night mode is
mjr 77:0b96f6867312 3661 // assigned to the shifted or unshifted version of the
mjr 77:0b96f6867312 3662 // button.
mjr 77:0b96f6867312 3663 bool pressed;
mjr 98:4df3c0f7e707 3664 if (shiftButton.index == i)
mjr 98:4df3c0f7e707 3665 {
mjr 98:4df3c0f7e707 3666 // This button is both the Shift button AND the Night
mjr 98:4df3c0f7e707 3667 // Mode button. This is a special case in that the
mjr 98:4df3c0f7e707 3668 // Shift status is irrelevant, because it's obviously
mjr 98:4df3c0f7e707 3669 // identical to the Night Mode status. So it doesn't
mjr 98:4df3c0f7e707 3670 // matter whether or not the Night Mode button has the
mjr 98:4df3c0f7e707 3671 // shifted flags; the raw button state is all that
mjr 98:4df3c0f7e707 3672 // counts in this case.
mjr 98:4df3c0f7e707 3673 pressed = true;
mjr 98:4df3c0f7e707 3674 }
mjr 98:4df3c0f7e707 3675 else if ((cfg.nightMode.flags & 0x02) != 0)
mjr 66:2e3583fbd2f4 3676 {
mjr 77:0b96f6867312 3677 // Shift bit is set - night mode is assigned to the
mjr 77:0b96f6867312 3678 // shifted version of the button. This is a Night
mjr 77:0b96f6867312 3679 // Mode toggle only if the Shift button is pressed.
mjr 77:0b96f6867312 3680 pressed = (shiftButton.state != 0);
mjr 77:0b96f6867312 3681 }
mjr 77:0b96f6867312 3682 else
mjr 77:0b96f6867312 3683 {
mjr 77:0b96f6867312 3684 // No shift bit - night mode is assigned to the
mjr 77:0b96f6867312 3685 // regular unshifted button. The button press only
mjr 77:0b96f6867312 3686 // applies if the Shift button is NOT pressed.
mjr 77:0b96f6867312 3687 pressed = (shiftButton.state == 0);
mjr 66:2e3583fbd2f4 3688 }
mjr 66:2e3583fbd2f4 3689
mjr 66:2e3583fbd2f4 3690 // if it's pressed (even after considering the shift mode),
mjr 66:2e3583fbd2f4 3691 // toggle night mode
mjr 66:2e3583fbd2f4 3692 if (pressed)
mjr 53:9b2611964afc 3693 toggleNightMode();
mjr 53:9b2611964afc 3694 }
mjr 35:e959ffba78fd 3695 }
mjr 38:091e511ce8a0 3696
mjr 77:0b96f6867312 3697 // press or release IR virtual keys on key state changes
mjr 77:0b96f6867312 3698 uint8_t irc = useShift ? bc->IRCommand2 : bc->IRCommand;
mjr 77:0b96f6867312 3699 if (irc != 0)
mjr 77:0b96f6867312 3700 IR_buttonChange(irc, bs->logState);
mjr 77:0b96f6867312 3701
mjr 38:091e511ce8a0 3702 // remember the new state for comparison on the next run
mjr 53:9b2611964afc 3703 bs->prevLogState = bs->logState;
mjr 38:091e511ce8a0 3704 }
mjr 38:091e511ce8a0 3705
mjr 53:9b2611964afc 3706 // if it's pressed, physically or virtually, add it to the appropriate
mjr 53:9b2611964afc 3707 // key state list
mjr 53:9b2611964afc 3708 if (bs->logState || bs->virtState)
mjr 38:091e511ce8a0 3709 {
mjr 70:9f58735a1732 3710 // Get the key type and code. Start by assuming that we're
mjr 70:9f58735a1732 3711 // going to use the normal unshifted meaning.
mjr 77:0b96f6867312 3712 uint8_t typ, val;
mjr 77:0b96f6867312 3713 if (useShift)
mjr 66:2e3583fbd2f4 3714 {
mjr 77:0b96f6867312 3715 typ = bc->typ2;
mjr 77:0b96f6867312 3716 val = bc->val2;
mjr 66:2e3583fbd2f4 3717 }
mjr 77:0b96f6867312 3718 else
mjr 77:0b96f6867312 3719 {
mjr 77:0b96f6867312 3720 typ = bc->typ;
mjr 77:0b96f6867312 3721 val = bc->val;
mjr 77:0b96f6867312 3722 }
mjr 77:0b96f6867312 3723
mjr 70:9f58735a1732 3724 // We've decided on the meaning of the button, so process
mjr 70:9f58735a1732 3725 // the keyboard or joystick event.
mjr 77:0b96f6867312 3726 ks.addKey(typ, val);
mjr 18:5e890ebd0023 3727 }
mjr 11:bd9da7088e6e 3728 }
mjr 77:0b96f6867312 3729
mjr 77:0b96f6867312 3730 // If an IR input command is in effect, add the IR command's
mjr 77:0b96f6867312 3731 // assigned key, if any. If we're in an IR key gap, don't include
mjr 77:0b96f6867312 3732 // the IR key.
mjr 77:0b96f6867312 3733 if (IRCommandIn != 0 && !IRKeyGap)
mjr 77:0b96f6867312 3734 {
mjr 77:0b96f6867312 3735 IRCommandCfg &irc = cfg.IRCommand[IRCommandIn - 1];
mjr 77:0b96f6867312 3736 ks.addKey(irc.keytype, irc.keycode);
mjr 77:0b96f6867312 3737 }
mjr 77:0b96f6867312 3738
mjr 77:0b96f6867312 3739 // We're finished building the new key state. Update the global
mjr 77:0b96f6867312 3740 // key state variables to reflect the new state.
mjr 77:0b96f6867312 3741
mjr 77:0b96f6867312 3742 // set the new joystick buttons (no need to check for changes, as we
mjr 77:0b96f6867312 3743 // report these on every joystick report whether they changed or not)
mjr 77:0b96f6867312 3744 jsButtons = ks.js;
mjr 77:0b96f6867312 3745
mjr 77:0b96f6867312 3746 // check for keyboard key changes (we only send keyboard reports when
mjr 77:0b96f6867312 3747 // something changes)
mjr 77:0b96f6867312 3748 if (kbState.data[0] != ks.modkeys
mjr 77:0b96f6867312 3749 || kbState.nkeys != ks.nkeys
mjr 77:0b96f6867312 3750 || memcmp(ks.keys, &kbState.data[2], 6) != 0)
mjr 35:e959ffba78fd 3751 {
mjr 35:e959ffba78fd 3752 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3753 kbState.changed = true;
mjr 77:0b96f6867312 3754 kbState.data[0] = ks.modkeys;
mjr 77:0b96f6867312 3755 if (ks.nkeys <= 6) {
mjr 35:e959ffba78fd 3756 // 6 or fewer simultaneous keys - report the key codes
mjr 77:0b96f6867312 3757 kbState.nkeys = ks.nkeys;
mjr 77:0b96f6867312 3758 memcpy(&kbState.data[2], ks.keys, 6);
mjr 35:e959ffba78fd 3759 }
mjr 35:e959ffba78fd 3760 else {
mjr 35:e959ffba78fd 3761 // more than 6 simultaneous keys - report rollover (all '1' key codes)
mjr 35:e959ffba78fd 3762 kbState.nkeys = 6;
mjr 35:e959ffba78fd 3763 memset(&kbState.data[2], 1, 6);
mjr 35:e959ffba78fd 3764 }
mjr 35:e959ffba78fd 3765 }
mjr 35:e959ffba78fd 3766
mjr 77:0b96f6867312 3767 // check for media key changes (we only send media key reports when
mjr 77:0b96f6867312 3768 // something changes)
mjr 77:0b96f6867312 3769 if (mediaState.data != ks.mediakeys)
mjr 35:e959ffba78fd 3770 {
mjr 77:0b96f6867312 3771 // we have changes - set the change flag and store the new key data
mjr 35:e959ffba78fd 3772 mediaState.changed = true;
mjr 77:0b96f6867312 3773 mediaState.data = ks.mediakeys;
mjr 35:e959ffba78fd 3774 }
mjr 11:bd9da7088e6e 3775 }
mjr 11:bd9da7088e6e 3776
mjr 73:4e8ce0b18915 3777 // Send a button status report
mjr 73:4e8ce0b18915 3778 void reportButtonStatus(USBJoystick &js)
mjr 73:4e8ce0b18915 3779 {
mjr 73:4e8ce0b18915 3780 // start with all buttons off
mjr 73:4e8ce0b18915 3781 uint8_t state[(MAX_BUTTONS+7)/8];
mjr 73:4e8ce0b18915 3782 memset(state, 0, sizeof(state));
mjr 73:4e8ce0b18915 3783
mjr 73:4e8ce0b18915 3784 // pack the button states into bytes, one bit per button
mjr 73:4e8ce0b18915 3785 ButtonState *bs = buttonState;
mjr 73:4e8ce0b18915 3786 for (int i = 0 ; i < nButtons ; ++i, ++bs)
mjr 73:4e8ce0b18915 3787 {
mjr 73:4e8ce0b18915 3788 // get the physical state
mjr 73:4e8ce0b18915 3789 int b = bs->physState;
mjr 73:4e8ce0b18915 3790
mjr 73:4e8ce0b18915 3791 // pack it into the appropriate bit
mjr 73:4e8ce0b18915 3792 int idx = bs->cfgIndex;
mjr 73:4e8ce0b18915 3793 int si = idx / 8;
mjr 73:4e8ce0b18915 3794 int shift = idx & 0x07;
mjr 73:4e8ce0b18915 3795 state[si] |= b << shift;
mjr 73:4e8ce0b18915 3796 }
mjr 73:4e8ce0b18915 3797
mjr 73:4e8ce0b18915 3798 // send the report
mjr 73:4e8ce0b18915 3799 js.reportButtonStatus(MAX_BUTTONS, state);
mjr 73:4e8ce0b18915 3800 }
mjr 73:4e8ce0b18915 3801
mjr 5:a70c0bce770d 3802 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 3803 //
mjr 5:a70c0bce770d 3804 // Customization joystick subbclass
mjr 5:a70c0bce770d 3805 //
mjr 5:a70c0bce770d 3806
mjr 5:a70c0bce770d 3807 class MyUSBJoystick: public USBJoystick
mjr 5:a70c0bce770d 3808 {
mjr 5:a70c0bce770d 3809 public:
mjr 35:e959ffba78fd 3810 MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
mjr 90:aa4e571da8e8 3811 bool waitForConnect, bool enableJoystick, int axisFormat, bool useKB)
mjr 90:aa4e571da8e8 3812 : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, axisFormat, useKB)
mjr 5:a70c0bce770d 3813 {
mjr 54:fd77a6b2f76c 3814 sleeping_ = false;
mjr 54:fd77a6b2f76c 3815 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3816 timer_.start();
mjr 54:fd77a6b2f76c 3817 }
mjr 54:fd77a6b2f76c 3818
mjr 54:fd77a6b2f76c 3819 // show diagnostic LED feedback for connect state
mjr 54:fd77a6b2f76c 3820 void diagFlash()
mjr 54:fd77a6b2f76c 3821 {
mjr 54:fd77a6b2f76c 3822 if (!configured() || sleeping_)
mjr 54:fd77a6b2f76c 3823 {
mjr 54:fd77a6b2f76c 3824 // flash once if sleeping or twice if disconnected
mjr 54:fd77a6b2f76c 3825 for (int j = isConnected() ? 1 : 2 ; j > 0 ; --j)
mjr 54:fd77a6b2f76c 3826 {
mjr 54:fd77a6b2f76c 3827 // short red flash
mjr 54:fd77a6b2f76c 3828 diagLED(1, 0, 0);
mjr 54:fd77a6b2f76c 3829 wait_us(50000);
mjr 54:fd77a6b2f76c 3830 diagLED(0, 0, 0);
mjr 54:fd77a6b2f76c 3831 wait_us(50000);
mjr 54:fd77a6b2f76c 3832 }
mjr 54:fd77a6b2f76c 3833 }
mjr 5:a70c0bce770d 3834 }
mjr 5:a70c0bce770d 3835
mjr 5:a70c0bce770d 3836 // are we connected?
mjr 5:a70c0bce770d 3837 int isConnected() { return configured(); }
mjr 5:a70c0bce770d 3838
mjr 54:fd77a6b2f76c 3839 // Are we in sleep mode? If true, this means that the hardware has
mjr 54:fd77a6b2f76c 3840 // detected no activity on the bus for 3ms. This happens when the
mjr 54:fd77a6b2f76c 3841 // cable is physically disconnected, the computer is turned off, or
mjr 54:fd77a6b2f76c 3842 // the connection is otherwise disabled.
mjr 54:fd77a6b2f76c 3843 bool isSleeping() const { return sleeping_; }
mjr 54:fd77a6b2f76c 3844
mjr 54:fd77a6b2f76c 3845 // If necessary, attempt to recover from a broken connection.
mjr 54:fd77a6b2f76c 3846 //
mjr 54:fd77a6b2f76c 3847 // This is a hack, to work around an apparent timing bug in the
mjr 54:fd77a6b2f76c 3848 // KL25Z USB implementation that I haven't been able to solve any
mjr 54:fd77a6b2f76c 3849 // other way.
mjr 54:fd77a6b2f76c 3850 //
mjr 54:fd77a6b2f76c 3851 // The issue: when we have an established connection, and the
mjr 54:fd77a6b2f76c 3852 // connection is broken by physically unplugging the cable or by
mjr 54:fd77a6b2f76c 3853 // rebooting the PC, the KL25Z sometimes fails to reconnect when
mjr 54:fd77a6b2f76c 3854 // the physical connection is re-established. The failure is
mjr 54:fd77a6b2f76c 3855 // sporadic; I'd guess it happens about 25% of the time, but I
mjr 54:fd77a6b2f76c 3856 // haven't collected any real statistics on it.
mjr 54:fd77a6b2f76c 3857 //
mjr 54:fd77a6b2f76c 3858 // The proximate cause of the failure is a deadlock in the SETUP
mjr 54:fd77a6b2f76c 3859 // protocol between the host and device that happens around the
mjr 54:fd77a6b2f76c 3860 // point where the PC is requesting the configuration descriptor.
mjr 54:fd77a6b2f76c 3861 // The exact point in the protocol where this occurs varies slightly;
mjr 54:fd77a6b2f76c 3862 // it can occur a message or two before or after the Get Config
mjr 54:fd77a6b2f76c 3863 // Descriptor packet. No matter where it happens, the nature of
mjr 54:fd77a6b2f76c 3864 // the deadlock is the same: the PC thinks it sees a STALL on EP0
mjr 54:fd77a6b2f76c 3865 // from the device, so it terminates the connection attempt, which
mjr 54:fd77a6b2f76c 3866 // stops further traffic on the cable. The KL25Z USB hardware sees
mjr 54:fd77a6b2f76c 3867 // the lack of traffic and triggers a SLEEP interrupt (a misnomer
mjr 54:fd77a6b2f76c 3868 // for what should have been called a BROKEN CONNECTION interrupt).
mjr 54:fd77a6b2f76c 3869 // Both sides simply stop talking at this point, so the connection
mjr 54:fd77a6b2f76c 3870 // is effectively dead.
mjr 54:fd77a6b2f76c 3871 //
mjr 54:fd77a6b2f76c 3872 // The strange thing is that, as far as I can tell, the KL25Z isn't
mjr 54:fd77a6b2f76c 3873 // doing anything to trigger the STALL on its end. Both the PC
mjr 54:fd77a6b2f76c 3874 // and the KL25Z are happy up until the very point of the failure
mjr 54:fd77a6b2f76c 3875 // and show no signs of anything wrong in the protocol exchange.
mjr 54:fd77a6b2f76c 3876 // In fact, every detail of the protocol exchange up to this point
mjr 54:fd77a6b2f76c 3877 // is identical to every successful exchange that does finish the
mjr 54:fd77a6b2f76c 3878 // whole setup process successfully, on both the KL25Z and Windows
mjr 54:fd77a6b2f76c 3879 // sides of the connection. I can't find any point of difference
mjr 54:fd77a6b2f76c 3880 // between successful and unsuccessful sequences that suggests why
mjr 54:fd77a6b2f76c 3881 // the fateful message fails. This makes me suspect that whatever
mjr 54:fd77a6b2f76c 3882 // is going wrong is inside the KL25Z USB hardware module, which
mjr 54:fd77a6b2f76c 3883 // is a pretty substantial black box - it has a lot of internal
mjr 54:fd77a6b2f76c 3884 // state that's inaccessible to the software. Further bolstering
mjr 54:fd77a6b2f76c 3885 // this theory is a little experiment where I found that I could
mjr 54:fd77a6b2f76c 3886 // reproduce the exact sequence of events of a failed reconnect
mjr 54:fd77a6b2f76c 3887 // attempt in an *initial* connection, which is otherwise 100%
mjr 54:fd77a6b2f76c 3888 // reliable, by inserting a little bit of artifical time padding
mjr 54:fd77a6b2f76c 3889 // (200us per event) into the SETUP interrupt handler. My
mjr 54:fd77a6b2f76c 3890 // hypothesis is that the STALL event happens because the KL25Z
mjr 54:fd77a6b2f76c 3891 // USB hardware is too slow to respond to a message. I'm not
mjr 54:fd77a6b2f76c 3892 // sure why this would only happen after a disconnect and not
mjr 54:fd77a6b2f76c 3893 // during the initial connection; maybe there's some reset work
mjr 54:fd77a6b2f76c 3894 // in the hardware that takes a substantial amount of time after
mjr 54:fd77a6b2f76c 3895 // a disconnect.
mjr 54:fd77a6b2f76c 3896 //
mjr 54:fd77a6b2f76c 3897 // The solution: the problem happens during the SETUP exchange,
mjr 54:fd77a6b2f76c 3898 // after we've been assigned a bus address. It only happens on
mjr 54:fd77a6b2f76c 3899 // some percentage of connection requests, so if we can simply
mjr 54:fd77a6b2f76c 3900 // start over when the failure occurs, we'll eventually succeed
mjr 54:fd77a6b2f76c 3901 // simply because not every attempt fails. The ideal would be
mjr 54:fd77a6b2f76c 3902 // to get the success rate up to 100%, but I can't figure out how
mjr 54:fd77a6b2f76c 3903 // to fix the underlying problem, so this is the next best thing.
mjr 54:fd77a6b2f76c 3904 //
mjr 54:fd77a6b2f76c 3905 // We can detect when the failure occurs by noticing when a SLEEP
mjr 54:fd77a6b2f76c 3906 // interrupt happens while we have an assigned bus address.
mjr 54:fd77a6b2f76c 3907 //
mjr 54:fd77a6b2f76c 3908 // To start a new connection attempt, we have to make the *host*
mjr 54:fd77a6b2f76c 3909 // try again. The logical connection is initiated solely by the
mjr 54:fd77a6b2f76c 3910 // host. Fortunately, it's easy to get the host to initiate the
mjr 54:fd77a6b2f76c 3911 // process: if we disconnect on the device side, it effectively
mjr 54:fd77a6b2f76c 3912 // makes the device look to the PC like it's electrically unplugged.
mjr 54:fd77a6b2f76c 3913 // When we reconnect on the device side, the PC thinks a new device
mjr 54:fd77a6b2f76c 3914 // has been plugged in and initiates the logical connection setup.
mjr 74:822a92bc11d2 3915 // We have to remain disconnected for some minimum interval before
mjr 74:822a92bc11d2 3916 // the host notices; the exact minimum is unclear, but 5ms seems
mjr 74:822a92bc11d2 3917 // reliable in practice.
mjr 54:fd77a6b2f76c 3918 //
mjr 54:fd77a6b2f76c 3919 // Here's the full algorithm:
mjr 54:fd77a6b2f76c 3920 //
mjr 54:fd77a6b2f76c 3921 // 1. In the SLEEP interrupt handler, if we have a bus address,
mjr 54:fd77a6b2f76c 3922 // we disconnect the device. This happens in ISR context, so we
mjr 54:fd77a6b2f76c 3923 // can't wait around for 5ms. Instead, we simply set a flag noting
mjr 54:fd77a6b2f76c 3924 // that the connection has been broken, and we note the time and
mjr 54:fd77a6b2f76c 3925 // return.
mjr 54:fd77a6b2f76c 3926 //
mjr 54:fd77a6b2f76c 3927 // 2. In our main loop, whenever we find that we're disconnected,
mjr 54:fd77a6b2f76c 3928 // we call recoverConnection(). The main loop's job is basically a
mjr 54:fd77a6b2f76c 3929 // bunch of device polling. We're just one more device to poll, so
mjr 54:fd77a6b2f76c 3930 // recoverConnection() will be called soon after a disconnect, and
mjr 54:fd77a6b2f76c 3931 // then will be called in a loop for as long as we're disconnected.
mjr 54:fd77a6b2f76c 3932 //
mjr 54:fd77a6b2f76c 3933 // 3. In recoverConnection(), we check the flag we set in the SLEEP
mjr 54:fd77a6b2f76c 3934 // handler. If set, we wait until 5ms has elapsed from the SLEEP
mjr 54:fd77a6b2f76c 3935 // event time that we noted, then we'll reconnect and clear the flag.
mjr 54:fd77a6b2f76c 3936 // This gives us the required 5ms (or longer) delay between the
mjr 54:fd77a6b2f76c 3937 // disconnect and reconnect, ensuring that the PC will notice and
mjr 54:fd77a6b2f76c 3938 // will start over with the connection protocol.
mjr 54:fd77a6b2f76c 3939 //
mjr 54:fd77a6b2f76c 3940 // 4. The main loop keeps calling recoverConnection() in a loop for
mjr 54:fd77a6b2f76c 3941 // as long as we're disconnected, so if the new connection attempt
mjr 54:fd77a6b2f76c 3942 // triggered in step 3 fails, the SLEEP interrupt will happen again,
mjr 54:fd77a6b2f76c 3943 // we'll disconnect again, the flag will get set again, and
mjr 54:fd77a6b2f76c 3944 // recoverConnection() will reconnect again after another suitable
mjr 54:fd77a6b2f76c 3945 // delay. This will repeat until the connection succeeds or hell
mjr 54:fd77a6b2f76c 3946 // freezes over.
mjr 54:fd77a6b2f76c 3947 //
mjr 54:fd77a6b2f76c 3948 // Each disconnect happens immediately when a reconnect attempt
mjr 54:fd77a6b2f76c 3949 // fails, and an entire successful connection only takes about 25ms,
mjr 54:fd77a6b2f76c 3950 // so our loop can retry at more than 30 attempts per second.
mjr 54:fd77a6b2f76c 3951 // In my testing, lost connections almost always reconnect in
mjr 54:fd77a6b2f76c 3952 // less than second with this code in place.
mjr 54:fd77a6b2f76c 3953 void recoverConnection()
mjr 54:fd77a6b2f76c 3954 {
mjr 54:fd77a6b2f76c 3955 // if a reconnect is pending, reconnect
mjr 54:fd77a6b2f76c 3956 if (reconnectPending_)
mjr 54:fd77a6b2f76c 3957 {
mjr 54:fd77a6b2f76c 3958 // Loop until we reach 5ms after the last sleep event.
mjr 54:fd77a6b2f76c 3959 for (bool done = false ; !done ; )
mjr 54:fd77a6b2f76c 3960 {
mjr 54:fd77a6b2f76c 3961 // If we've reached the target time, reconnect. Do the
mjr 54:fd77a6b2f76c 3962 // time check and flag reset atomically, so that we can't
mjr 54:fd77a6b2f76c 3963 // have another sleep event sneak in after we've verified
mjr 54:fd77a6b2f76c 3964 // the time. If another event occurs, it has to happen
mjr 54:fd77a6b2f76c 3965 // before we check, in which case it'll update the time
mjr 54:fd77a6b2f76c 3966 // before we check it, or after we clear the flag, in
mjr 54:fd77a6b2f76c 3967 // which case it will reset the flag and we'll do another
mjr 54:fd77a6b2f76c 3968 // round the next time we call this routine.
mjr 54:fd77a6b2f76c 3969 __disable_irq();
mjr 54:fd77a6b2f76c 3970 if (uint32_t(timer_.read_us() - lastSleepTime_) > 5000)
mjr 54:fd77a6b2f76c 3971 {
mjr 54:fd77a6b2f76c 3972 connect(false);
mjr 54:fd77a6b2f76c 3973 reconnectPending_ = false;
mjr 54:fd77a6b2f76c 3974 done = true;
mjr 54:fd77a6b2f76c 3975 }
mjr 54:fd77a6b2f76c 3976 __enable_irq();
mjr 54:fd77a6b2f76c 3977 }
mjr 54:fd77a6b2f76c 3978 }
mjr 54:fd77a6b2f76c 3979 }
mjr 5:a70c0bce770d 3980
mjr 5:a70c0bce770d 3981 protected:
mjr 54:fd77a6b2f76c 3982 // Handle a USB SLEEP interrupt. This interrupt signifies that the
mjr 54:fd77a6b2f76c 3983 // USB hardware module hasn't seen any token traffic for 3ms, which
mjr 54:fd77a6b2f76c 3984 // means that we're either physically or logically disconnected.
mjr 54:fd77a6b2f76c 3985 //
mjr 54:fd77a6b2f76c 3986 // Important: this runs in ISR context.
mjr 54:fd77a6b2f76c 3987 //
mjr 54:fd77a6b2f76c 3988 // Note that this is a specialized sense of "sleep" that's unrelated
mjr 54:fd77a6b2f76c 3989 // to the similarly named power modes on the PC. This has nothing
mjr 54:fd77a6b2f76c 3990 // to do with suspend/sleep mode on the PC, and it's not a low-power
mjr 54:fd77a6b2f76c 3991 // mode on the KL25Z. They really should have called this interrupt
mjr 54:fd77a6b2f76c 3992 // DISCONNECT or BROKEN CONNECTION.)
mjr 54:fd77a6b2f76c 3993 virtual void sleepStateChanged(unsigned int sleeping)
mjr 54:fd77a6b2f76c 3994 {
mjr 54:fd77a6b2f76c 3995 // note the new state
mjr 54:fd77a6b2f76c 3996 sleeping_ = sleeping;
mjr 54:fd77a6b2f76c 3997
mjr 54:fd77a6b2f76c 3998 // If we have a non-zero bus address, we have at least a partial
mjr 54:fd77a6b2f76c 3999 // connection to the host (we've made it at least as far as the
mjr 54:fd77a6b2f76c 4000 // SETUP stage). Explicitly disconnect, and the pending reconnect
mjr 54:fd77a6b2f76c 4001 // flag, and remember the time of the sleep event.
mjr 54:fd77a6b2f76c 4002 if (USB0->ADDR != 0x00)
mjr 54:fd77a6b2f76c 4003 {
mjr 54:fd77a6b2f76c 4004 disconnect();
mjr 54:fd77a6b2f76c 4005 lastSleepTime_ = timer_.read_us();
mjr 54:fd77a6b2f76c 4006 reconnectPending_ = true;
mjr 54:fd77a6b2f76c 4007 }
mjr 54:fd77a6b2f76c 4008 }
mjr 54:fd77a6b2f76c 4009
mjr 54:fd77a6b2f76c 4010 // is the USB connection asleep?
mjr 54:fd77a6b2f76c 4011 volatile bool sleeping_;
mjr 54:fd77a6b2f76c 4012
mjr 54:fd77a6b2f76c 4013 // flag: reconnect pending after sleep event
mjr 54:fd77a6b2f76c 4014 volatile bool reconnectPending_;
mjr 54:fd77a6b2f76c 4015
mjr 54:fd77a6b2f76c 4016 // time of last sleep event while connected
mjr 54:fd77a6b2f76c 4017 volatile uint32_t lastSleepTime_;
mjr 54:fd77a6b2f76c 4018
mjr 54:fd77a6b2f76c 4019 // timer to keep track of interval since last sleep event
mjr 54:fd77a6b2f76c 4020 Timer timer_;
mjr 5:a70c0bce770d 4021 };
mjr 5:a70c0bce770d 4022
mjr 5:a70c0bce770d 4023 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 4024 //
mjr 5:a70c0bce770d 4025 // Accelerometer (MMA8451Q)
mjr 5:a70c0bce770d 4026 //
mjr 5:a70c0bce770d 4027
mjr 5:a70c0bce770d 4028 // The MMA8451Q is the KL25Z's on-board 3-axis accelerometer.
mjr 5:a70c0bce770d 4029 //
mjr 5:a70c0bce770d 4030 // This is a custom wrapper for the library code to interface to the
mjr 6:cc35eb643e8f 4031 // MMA8451Q. This class encapsulates an interrupt handler and
mjr 6:cc35eb643e8f 4032 // automatic calibration.
mjr 5:a70c0bce770d 4033 //
mjr 77:0b96f6867312 4034 // We collect data at the device's maximum rate of 800kHz (one sample
mjr 77:0b96f6867312 4035 // every 1.25ms). To keep up with the high data rate, we use the
mjr 77:0b96f6867312 4036 // device's internal FIFO, and drain the FIFO by polling on each
mjr 77:0b96f6867312 4037 // iteration of our main application loop. In the past, we used an
mjr 77:0b96f6867312 4038 // interrupt handler to read the device immediately on the arrival of
mjr 77:0b96f6867312 4039 // each sample, but this created too much latency for the IR remote
mjr 77:0b96f6867312 4040 // receiver, due to the relatively long time it takes to transfer the
mjr 77:0b96f6867312 4041 // accelerometer readings via I2C. The device's on-board FIFO can
mjr 77:0b96f6867312 4042 // store up to 32 samples, which gives us up to about 40ms between
mjr 77:0b96f6867312 4043 // polling iterations before the buffer overflows. Our main loop runs
mjr 77:0b96f6867312 4044 // in under 2ms, so we can easily keep the FIFO far from overflowing.
mjr 77:0b96f6867312 4045 //
mjr 77:0b96f6867312 4046 // The MMA8451Q has three range modes, +/- 2G, 4G, and 8G. The ADC
mjr 77:0b96f6867312 4047 // sample is the same bit width (14 bits) in all modes, so the higher
mjr 77:0b96f6867312 4048 // dynamic range modes trade physical precision for range. For our
mjr 77:0b96f6867312 4049 // purposes, precision is more important than range, so we use the
mjr 77:0b96f6867312 4050 // +/-2G mode. Further, our joystick range is calibrated for only
mjr 77:0b96f6867312 4051 // +/-1G. This was unintentional on my part; I didn't look at the
mjr 77:0b96f6867312 4052 // MMA8451Q library closely enough to realize it was normalizing to
mjr 77:0b96f6867312 4053 // actual "G" units, and assumed that it was normalizing to a -1..+1
mjr 77:0b96f6867312 4054 // scale. In practice, a +/-1G scale seems perfectly adequate for
mjr 77:0b96f6867312 4055 // virtual pinball use, so I'm sticking with that range for now. But
mjr 77:0b96f6867312 4056 // there might be some benefit in renormalizing to a +/-2G range, in
mjr 77:0b96f6867312 4057 // that it would allow for higher dynamic range for very hard nudges.
mjr 77:0b96f6867312 4058 // Everyone would have to tweak their nudge sensitivity in VP if I
mjr 77:0b96f6867312 4059 // made that change, though, so I'm keeping it as is for now; it would
mjr 77:0b96f6867312 4060 // be best to make it a config option ("accelerometer high dynamic range")
mjr 77:0b96f6867312 4061 // rather than change it across the board.
mjr 5:a70c0bce770d 4062 //
mjr 6:cc35eb643e8f 4063 // We automatically calibrate the accelerometer so that it's not
mjr 6:cc35eb643e8f 4064 // necessary to get it exactly level when installing it, and so
mjr 6:cc35eb643e8f 4065 // that it's also not necessary to calibrate it manually. There's
mjr 6:cc35eb643e8f 4066 // lots of experience that tells us that manual calibration is a
mjr 6:cc35eb643e8f 4067 // terrible solution, mostly because cabinets tend to shift slightly
mjr 6:cc35eb643e8f 4068 // during use, requiring frequent recalibration. Instead, we
mjr 6:cc35eb643e8f 4069 // calibrate automatically. We continuously monitor the acceleration
mjr 6:cc35eb643e8f 4070 // data, watching for periods of constant (or nearly constant) values.
mjr 6:cc35eb643e8f 4071 // Any time it appears that the machine has been at rest for a while
mjr 6:cc35eb643e8f 4072 // (about 5 seconds), we'll average the readings during that rest
mjr 6:cc35eb643e8f 4073 // period and use the result as the level rest position. This is
mjr 6:cc35eb643e8f 4074 // is ongoing, so we'll quickly find the center point again if the
mjr 6:cc35eb643e8f 4075 // machine is moved during play (by an especially aggressive bout
mjr 6:cc35eb643e8f 4076 // of nudging, say).
mjr 5:a70c0bce770d 4077 //
mjr 5:a70c0bce770d 4078
mjr 17:ab3cec0c8bf4 4079 // I2C address of the accelerometer (this is a constant of the KL25Z)
mjr 17:ab3cec0c8bf4 4080 const int MMA8451_I2C_ADDRESS = (0x1d<<1);
mjr 17:ab3cec0c8bf4 4081
mjr 17:ab3cec0c8bf4 4082 // SCL and SDA pins for the accelerometer (constant for the KL25Z)
mjr 17:ab3cec0c8bf4 4083 #define MMA8451_SCL_PIN PTE25
mjr 17:ab3cec0c8bf4 4084 #define MMA8451_SDA_PIN PTE24
mjr 17:ab3cec0c8bf4 4085
mjr 17:ab3cec0c8bf4 4086 // Digital in pin to use for the accelerometer interrupt. For the KL25Z,
mjr 17:ab3cec0c8bf4 4087 // this can be either PTA14 or PTA15, since those are the pins physically
mjr 17:ab3cec0c8bf4 4088 // wired on this board to the MMA8451 interrupt controller.
mjr 17:ab3cec0c8bf4 4089 #define MMA8451_INT_PIN PTA15
mjr 17:ab3cec0c8bf4 4090
mjr 17:ab3cec0c8bf4 4091
mjr 6:cc35eb643e8f 4092 // accelerometer input history item, for gathering calibration data
mjr 6:cc35eb643e8f 4093 struct AccHist
mjr 5:a70c0bce770d 4094 {
mjr 77:0b96f6867312 4095 AccHist() { x = y = dsq = 0; xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 4096 void set(int x, int y, AccHist *prv)
mjr 6:cc35eb643e8f 4097 {
mjr 6:cc35eb643e8f 4098 // save the raw position
mjr 6:cc35eb643e8f 4099 this->x = x;
mjr 6:cc35eb643e8f 4100 this->y = y;
mjr 77:0b96f6867312 4101 this->dsq = distanceSquared(prv);
mjr 6:cc35eb643e8f 4102 }
mjr 6:cc35eb643e8f 4103
mjr 6:cc35eb643e8f 4104 // reading for this entry
mjr 77:0b96f6867312 4105 int x, y;
mjr 77:0b96f6867312 4106
mjr 77:0b96f6867312 4107 // (distance from previous entry) squared
mjr 77:0b96f6867312 4108 int dsq;
mjr 5:a70c0bce770d 4109
mjr 6:cc35eb643e8f 4110 // total and count of samples averaged over this period
mjr 77:0b96f6867312 4111 int xtot, ytot;
mjr 6:cc35eb643e8f 4112 int cnt;
mjr 6:cc35eb643e8f 4113
mjr 77:0b96f6867312 4114 void clearAvg() { xtot = ytot = 0; cnt = 0; }
mjr 77:0b96f6867312 4115 void addAvg(int x, int y) { xtot += x; ytot += y; ++cnt; }
mjr 77:0b96f6867312 4116 int xAvg() const { return xtot/cnt; }
mjr 77:0b96f6867312 4117 int yAvg() const { return ytot/cnt; }
mjr 77:0b96f6867312 4118
mjr 77:0b96f6867312 4119 int distanceSquared(AccHist *p)
mjr 77:0b96f6867312 4120 { return square(p->x - x) + square(p->y - y); }
mjr 5:a70c0bce770d 4121 };
mjr 5:a70c0bce770d 4122
mjr 5:a70c0bce770d 4123 // accelerometer wrapper class
mjr 3:3514575d4f86 4124 class Accel
mjr 3:3514575d4f86 4125 {
mjr 3:3514575d4f86 4126 public:
mjr 78:1e00b3fa11af 4127 Accel(PinName sda, PinName scl, int i2cAddr, PinName irqPin,
mjr 78:1e00b3fa11af 4128 int range, int autoCenterMode)
mjr 77:0b96f6867312 4129 : mma_(sda, scl, i2cAddr)
mjr 3:3514575d4f86 4130 {
mjr 5:a70c0bce770d 4131 // remember the interrupt pin assignment
mjr 5:a70c0bce770d 4132 irqPin_ = irqPin;
mjr 77:0b96f6867312 4133
mjr 77:0b96f6867312 4134 // remember the range
mjr 77:0b96f6867312 4135 range_ = range;
mjr 78:1e00b3fa11af 4136
mjr 78:1e00b3fa11af 4137 // set the auto-centering mode
mjr 78:1e00b3fa11af 4138 setAutoCenterMode(autoCenterMode);
mjr 78:1e00b3fa11af 4139
mjr 78:1e00b3fa11af 4140 // no manual centering request has been received
mjr 78:1e00b3fa11af 4141 manualCenterRequest_ = false;
mjr 5:a70c0bce770d 4142
mjr 5:a70c0bce770d 4143 // reset and initialize
mjr 5:a70c0bce770d 4144 reset();
mjr 5:a70c0bce770d 4145 }
mjr 5:a70c0bce770d 4146
mjr 78:1e00b3fa11af 4147 // Request manual centering. This applies the trailing average
mjr 78:1e00b3fa11af 4148 // of recent measurements and applies it as the new center point
mjr 78:1e00b3fa11af 4149 // as soon as we have enough data.
mjr 78:1e00b3fa11af 4150 void manualCenterRequest() { manualCenterRequest_ = true; }
mjr 78:1e00b3fa11af 4151
mjr 78:1e00b3fa11af 4152 // set the auto-centering mode
mjr 78:1e00b3fa11af 4153 void setAutoCenterMode(int mode)
mjr 78:1e00b3fa11af 4154 {
mjr 78:1e00b3fa11af 4155 // remember the mode
mjr 78:1e00b3fa11af 4156 autoCenterMode_ = mode;
mjr 78:1e00b3fa11af 4157
mjr 78:1e00b3fa11af 4158 // Set the time between checks. We check 5 times over the course
mjr 78:1e00b3fa11af 4159 // of the centering time, so the check interval is 1/5 of the total.
mjr 78:1e00b3fa11af 4160 if (mode == 0)
mjr 78:1e00b3fa11af 4161 {
mjr 78:1e00b3fa11af 4162 // mode 0 is the old default of 5 seconds, so check every 1s
mjr 78:1e00b3fa11af 4163 autoCenterCheckTime_ = 1000000;
mjr 78:1e00b3fa11af 4164 }
mjr 78:1e00b3fa11af 4165 else if (mode <= 60)
mjr 78:1e00b3fa11af 4166 {
mjr 78:1e00b3fa11af 4167 // mode 1-60 means reset after 'mode' seconds; the check
mjr 78:1e00b3fa11af 4168 // interval is 1/5 of this
mjr 78:1e00b3fa11af 4169 autoCenterCheckTime_ = mode*200000;
mjr 78:1e00b3fa11af 4170 }
mjr 78:1e00b3fa11af 4171 else
mjr 78:1e00b3fa11af 4172 {
mjr 78:1e00b3fa11af 4173 // Auto-centering is off, but still gather statistics to apply
mjr 78:1e00b3fa11af 4174 // when we get a manual centering request. The check interval
mjr 78:1e00b3fa11af 4175 // in this case is 1/5 of the total time for the trailing average
mjr 78:1e00b3fa11af 4176 // we apply for the manual centering. We want this to be long
mjr 78:1e00b3fa11af 4177 // enough to smooth out the data, but short enough that it only
mjr 78:1e00b3fa11af 4178 // includes recent data.
mjr 78:1e00b3fa11af 4179 autoCenterCheckTime_ = 500000;
mjr 78:1e00b3fa11af 4180 }
mjr 78:1e00b3fa11af 4181 }
mjr 78:1e00b3fa11af 4182
mjr 5:a70c0bce770d 4183 void reset()
mjr 5:a70c0bce770d 4184 {
mjr 6:cc35eb643e8f 4185 // clear the center point
mjr 77:0b96f6867312 4186 cx_ = cy_ = 0;
mjr 6:cc35eb643e8f 4187
mjr 77:0b96f6867312 4188 // start the auto-centering timer
mjr 5:a70c0bce770d 4189 tCenter_.start();
mjr 5:a70c0bce770d 4190 iAccPrv_ = nAccPrv_ = 0;
mjr 6:cc35eb643e8f 4191
mjr 5:a70c0bce770d 4192 // reset and initialize the MMA8451Q
mjr 5:a70c0bce770d 4193 mma_.init();
mjr 77:0b96f6867312 4194
mjr 77:0b96f6867312 4195 // set the range
mjr 77:0b96f6867312 4196 mma_.setRange(
mjr 77:0b96f6867312 4197 range_ == AccelRange4G ? 4 :
mjr 77:0b96f6867312 4198 range_ == AccelRange8G ? 8 :
mjr 77:0b96f6867312 4199 2);
mjr 6:cc35eb643e8f 4200
mjr 77:0b96f6867312 4201 // set the average accumulators to zero
mjr 77:0b96f6867312 4202 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 4203 nSum_ = 0;
mjr 3:3514575d4f86 4204
mjr 3:3514575d4f86 4205 // read the current registers to clear the data ready flag
mjr 6:cc35eb643e8f 4206 mma_.getAccXYZ(ax_, ay_, az_);
mjr 3:3514575d4f86 4207 }
mjr 3:3514575d4f86 4208
mjr 77:0b96f6867312 4209 void poll()
mjr 76:7f5912b6340e 4210 {
mjr 77:0b96f6867312 4211 // read samples until we clear the FIFO
mjr 77:0b96f6867312 4212 while (mma_.getFIFOCount() != 0)
mjr 77:0b96f6867312 4213 {
mjr 77:0b96f6867312 4214 int x, y, z;
mjr 77:0b96f6867312 4215 mma_.getAccXYZ(x, y, z);
mjr 77:0b96f6867312 4216
mjr 77:0b96f6867312 4217 // add the new reading to the running total for averaging
mjr 77:0b96f6867312 4218 xSum_ += (x - cx_);
mjr 77:0b96f6867312 4219 ySum_ += (y - cy_);
mjr 77:0b96f6867312 4220 ++nSum_;
mjr 77:0b96f6867312 4221
mjr 77:0b96f6867312 4222 // store the updates
mjr 77:0b96f6867312 4223 ax_ = x;
mjr 77:0b96f6867312 4224 ay_ = y;
mjr 77:0b96f6867312 4225 az_ = z;
mjr 77:0b96f6867312 4226 }
mjr 76:7f5912b6340e 4227 }
mjr 77:0b96f6867312 4228
mjr 9:fd65b0a94720 4229 void get(int &x, int &y)
mjr 3:3514575d4f86 4230 {
mjr 77:0b96f6867312 4231 // read the shared data and store locally for calculations
mjr 77:0b96f6867312 4232 int ax = ax_, ay = ay_;
mjr 77:0b96f6867312 4233 int xSum = xSum_, ySum = ySum_;
mjr 77:0b96f6867312 4234 int nSum = nSum_;
mjr 6:cc35eb643e8f 4235
mjr 77:0b96f6867312 4236 // reset the average accumulators for the next run
mjr 77:0b96f6867312 4237 xSum_ = ySum_ = 0;
mjr 77:0b96f6867312 4238 nSum_ = 0;
mjr 77:0b96f6867312 4239
mjr 77:0b96f6867312 4240 // add this sample to the current calibration interval's running total
mjr 77:0b96f6867312 4241 AccHist *p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 4242 p->addAvg(ax, ay);
mjr 77:0b96f6867312 4243
mjr 78:1e00b3fa11af 4244 // If we're in auto-centering mode, check for auto-centering
mjr 78:1e00b3fa11af 4245 // at intervals of 1/5 of the overall time. If we're not in
mjr 78:1e00b3fa11af 4246 // auto-centering mode, check anyway at one-second intervals
mjr 78:1e00b3fa11af 4247 // so that we gather averages for manual centering requests.
mjr 78:1e00b3fa11af 4248 if (tCenter_.read_us() > autoCenterCheckTime_)
mjr 77:0b96f6867312 4249 {
mjr 77:0b96f6867312 4250 // add the latest raw sample to the history list
mjr 77:0b96f6867312 4251 AccHist *prv = p;
mjr 77:0b96f6867312 4252 iAccPrv_ = (iAccPrv_ + 1);
mjr 77:0b96f6867312 4253 if (iAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 4254 iAccPrv_ = 0;
mjr 77:0b96f6867312 4255 p = accPrv_ + iAccPrv_;
mjr 77:0b96f6867312 4256 p->set(ax, ay, prv);
mjr 77:0b96f6867312 4257
mjr 78:1e00b3fa11af 4258 // if we have a full complement, check for auto-centering
mjr 77:0b96f6867312 4259 if (nAccPrv_ >= maxAccPrv)
mjr 77:0b96f6867312 4260 {
mjr 78:1e00b3fa11af 4261 // Center if:
mjr 78:1e00b3fa11af 4262 //
mjr 78:1e00b3fa11af 4263 // - Auto-centering is on, and we've been stable over the
mjr 78:1e00b3fa11af 4264 // whole sample period at our spot-check points
mjr 78:1e00b3fa11af 4265 //
mjr 78:1e00b3fa11af 4266 // - A manual centering request is pending
mjr 78:1e00b3fa11af 4267 //
mjr 77:0b96f6867312 4268 static const int accTol = 164*164; // 1% of range, squared
mjr 77:0b96f6867312 4269 AccHist *p0 = accPrv_;
mjr 78:1e00b3fa11af 4270 if (manualCenterRequest_
mjr 78:1e00b3fa11af 4271 || (autoCenterMode_ <= 60
mjr 78:1e00b3fa11af 4272 && p0[0].dsq < accTol
mjr 78:1e00b3fa11af 4273 && p0[1].dsq < accTol
mjr 78:1e00b3fa11af 4274 && p0[2].dsq < accTol
mjr 78:1e00b3fa11af 4275 && p0[3].dsq < accTol
mjr 78:1e00b3fa11af 4276 && p0[4].dsq < accTol))
mjr 77:0b96f6867312 4277 {
mjr 77:0b96f6867312 4278 // Figure the new calibration point as the average of
mjr 77:0b96f6867312 4279 // the samples over the rest period
mjr 77:0b96f6867312 4280 cx_ = (p0[0].xAvg() + p0[1].xAvg() + p0[2].xAvg() + p0[3].xAvg() + p0[4].xAvg())/5;
mjr 77:0b96f6867312 4281 cy_ = (p0[0].yAvg() + p0[1].yAvg() + p0[2].yAvg() + p0[3].yAvg() + p0[4].yAvg())/5;
mjr 78:1e00b3fa11af 4282
mjr 78:1e00b3fa11af 4283 // clear any pending manual centering request
mjr 78:1e00b3fa11af 4284 manualCenterRequest_ = false;
mjr 77:0b96f6867312 4285 }
mjr 77:0b96f6867312 4286 }
mjr 77:0b96f6867312 4287 else
mjr 77:0b96f6867312 4288 {
mjr 77:0b96f6867312 4289 // not enough samples yet; just up the count
mjr 77:0b96f6867312 4290 ++nAccPrv_;
mjr 77:0b96f6867312 4291 }
mjr 6:cc35eb643e8f 4292
mjr 77:0b96f6867312 4293 // clear the new item's running totals
mjr 77:0b96f6867312 4294 p->clearAvg();
mjr 5:a70c0bce770d 4295
mjr 77:0b96f6867312 4296 // reset the timer
mjr 77:0b96f6867312 4297 tCenter_.reset();
mjr 77:0b96f6867312 4298 }
mjr 5:a70c0bce770d 4299
mjr 77:0b96f6867312 4300 // report our integrated velocity reading in x,y
mjr 77:0b96f6867312 4301 x = rawToReport(xSum/nSum);
mjr 77:0b96f6867312 4302 y = rawToReport(ySum/nSum);
mjr 5:a70c0bce770d 4303
mjr 6:cc35eb643e8f 4304 #ifdef DEBUG_PRINTF
mjr 77:0b96f6867312 4305 if (x != 0 || y != 0)
mjr 77:0b96f6867312 4306 printf("%f %f %d %d %f\r\n", vx, vy, x, y, dt);
mjr 6:cc35eb643e8f 4307 #endif
mjr 77:0b96f6867312 4308 }
mjr 29:582472d0bc57 4309
mjr 3:3514575d4f86 4310 private:
mjr 6:cc35eb643e8f 4311 // adjust a raw acceleration figure to a usb report value
mjr 77:0b96f6867312 4312 int rawToReport(int v)
mjr 5:a70c0bce770d 4313 {
mjr 77:0b96f6867312 4314 // Scale to the joystick report range. The accelerometer
mjr 77:0b96f6867312 4315 // readings use the native 14-bit signed integer representation,
mjr 77:0b96f6867312 4316 // so their scale is 2^13.
mjr 77:0b96f6867312 4317 //
mjr 77:0b96f6867312 4318 // The 1G range is special: it uses the 2G native hardware range,
mjr 77:0b96f6867312 4319 // but rescales the result to a 1G range for the joystick reports.
mjr 77:0b96f6867312 4320 // So for that mode, we divide by 4096 rather than 8192. All of
mjr 77:0b96f6867312 4321 // the other modes map use the hardware scaling directly.
mjr 77:0b96f6867312 4322 int i = v*JOYMAX;
mjr 77:0b96f6867312 4323 i = (range_ == AccelRange1G ? i/4096 : i/8192);
mjr 5:a70c0bce770d 4324
mjr 6:cc35eb643e8f 4325 // if it's near the center, scale it roughly as 20*(i/20)^2,
mjr 6:cc35eb643e8f 4326 // to suppress noise near the rest position
mjr 6:cc35eb643e8f 4327 static const int filter[] = {
mjr 6:cc35eb643e8f 4328 -18, -16, -14, -13, -11, -10, -8, -7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 0,
mjr 6:cc35eb643e8f 4329 0,
mjr 6:cc35eb643e8f 4330 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 18
mjr 6:cc35eb643e8f 4331 };
mjr 6:cc35eb643e8f 4332 return (i > 20 || i < -20 ? i : filter[i+20]);
mjr 5:a70c0bce770d 4333 }
mjr 5:a70c0bce770d 4334
mjr 3:3514575d4f86 4335 // underlying accelerometer object
mjr 3:3514575d4f86 4336 MMA8451Q mma_;
mjr 3:3514575d4f86 4337
mjr 77:0b96f6867312 4338 // last raw acceleration readings, on the device's signed 14-bit
mjr 77:0b96f6867312 4339 // scale -8192..+8191
mjr 77:0b96f6867312 4340 int ax_, ay_, az_;
mjr 77:0b96f6867312 4341
mjr 77:0b96f6867312 4342 // running sum of readings since last get()
mjr 77:0b96f6867312 4343 int xSum_, ySum_;
mjr 77:0b96f6867312 4344
mjr 77:0b96f6867312 4345 // number of readings since last get()
mjr 77:0b96f6867312 4346 int nSum_;
mjr 6:cc35eb643e8f 4347
mjr 6:cc35eb643e8f 4348 // Calibration reference point for accelerometer. This is the
mjr 6:cc35eb643e8f 4349 // average reading on the accelerometer when in the neutral position
mjr 6:cc35eb643e8f 4350 // at rest.
mjr 77:0b96f6867312 4351 int cx_, cy_;
mjr 77:0b96f6867312 4352
mjr 77:0b96f6867312 4353 // range (AccelRangeXxx value, from config.h)
mjr 77:0b96f6867312 4354 uint8_t range_;
mjr 78:1e00b3fa11af 4355
mjr 78:1e00b3fa11af 4356 // auto-center mode:
mjr 78:1e00b3fa11af 4357 // 0 = default of 5-second auto-centering
mjr 78:1e00b3fa11af 4358 // 1-60 = auto-center after this many seconds
mjr 78:1e00b3fa11af 4359 // 255 = auto-centering off (manual centering only)
mjr 78:1e00b3fa11af 4360 uint8_t autoCenterMode_;
mjr 78:1e00b3fa11af 4361
mjr 78:1e00b3fa11af 4362 // flag: a manual centering request is pending
mjr 78:1e00b3fa11af 4363 bool manualCenterRequest_;
mjr 78:1e00b3fa11af 4364
mjr 78:1e00b3fa11af 4365 // time in us between auto-centering incremental checks
mjr 78:1e00b3fa11af 4366 uint32_t autoCenterCheckTime_;
mjr 78:1e00b3fa11af 4367
mjr 77:0b96f6867312 4368 // atuo-centering timer
mjr 5:a70c0bce770d 4369 Timer tCenter_;
mjr 6:cc35eb643e8f 4370
mjr 6:cc35eb643e8f 4371 // Auto-centering history. This is a separate history list that
mjr 77:0b96f6867312 4372 // records results spaced out sparsely over time, so that we can
mjr 6:cc35eb643e8f 4373 // watch for long-lasting periods of rest. When we observe nearly
mjr 6:cc35eb643e8f 4374 // no motion for an extended period (on the order of 5 seconds), we
mjr 6:cc35eb643e8f 4375 // take this to mean that the cabinet is at rest in its neutral
mjr 6:cc35eb643e8f 4376 // position, so we take this as the calibration zero point for the
mjr 6:cc35eb643e8f 4377 // accelerometer. We update this history continuously, which allows
mjr 6:cc35eb643e8f 4378 // us to continuously re-calibrate the accelerometer. This ensures
mjr 6:cc35eb643e8f 4379 // that we'll automatically adjust to any actual changes in the
mjr 6:cc35eb643e8f 4380 // cabinet's orientation (e.g., if it gets moved slightly by an
mjr 6:cc35eb643e8f 4381 // especially strong nudge) as well as any systematic drift in the
mjr 6:cc35eb643e8f 4382 // accelerometer measurement bias (e.g., from temperature changes).
mjr 78:1e00b3fa11af 4383 uint8_t iAccPrv_, nAccPrv_;
mjr 78:1e00b3fa11af 4384 static const uint8_t maxAccPrv = 5;
mjr 6:cc35eb643e8f 4385 AccHist accPrv_[maxAccPrv];
mjr 6:cc35eb643e8f 4386
mjr 5:a70c0bce770d 4387 // interurupt pin name
mjr 5:a70c0bce770d 4388 PinName irqPin_;
mjr 3:3514575d4f86 4389 };
mjr 3:3514575d4f86 4390
mjr 5:a70c0bce770d 4391 // ---------------------------------------------------------------------------
mjr 5:a70c0bce770d 4392 //
mjr 14:df700b22ca08 4393 // Clear the I2C bus for the MMA8451Q. This seems necessary some of the time
mjr 5:a70c0bce770d 4394 // for reasons that aren't clear to me. Doing a hard power cycle has the same
mjr 5:a70c0bce770d 4395 // effect, but when we do a soft reset, the hardware sometimes seems to leave
mjr 5:a70c0bce770d 4396 // the MMA's SDA line stuck low. Forcing a series of 9 clock pulses through
mjr 14:df700b22ca08 4397 // the SCL line is supposed to clear this condition. I'm not convinced this
mjr 14:df700b22ca08 4398 // actually works with the way this component is wired on the KL25Z, but it
mjr 14:df700b22ca08 4399 // seems harmless, so we'll do it on reset in case it does some good. What
mjr 14:df700b22ca08 4400 // we really seem to need is a way to power cycle the MMA8451Q if it ever
mjr 14:df700b22ca08 4401 // gets stuck, but this is simply not possible in software on the KL25Z.
mjr 14:df700b22ca08 4402 //
mjr 14:df700b22ca08 4403 // If the accelerometer does get stuck, and a software reboot doesn't reset
mjr 14:df700b22ca08 4404 // it, the only workaround is to manually power cycle the whole KL25Z by
mjr 14:df700b22ca08 4405 // unplugging both of its USB connections.
mjr 5:a70c0bce770d 4406 //
mjr 5:a70c0bce770d 4407 void clear_i2c()
mjr 5:a70c0bce770d 4408 {
mjr 38:091e511ce8a0 4409 // set up general-purpose output pins to the I2C lines
mjr 5:a70c0bce770d 4410 DigitalOut scl(MMA8451_SCL_PIN);
mjr 5:a70c0bce770d 4411 DigitalIn sda(MMA8451_SDA_PIN);
mjr 5:a70c0bce770d 4412
mjr 5:a70c0bce770d 4413 // clock the SCL 9 times
mjr 5:a70c0bce770d 4414 for (int i = 0 ; i < 9 ; ++i)
mjr 5:a70c0bce770d 4415 {
mjr 5:a70c0bce770d 4416 scl = 1;
mjr 5:a70c0bce770d 4417 wait_us(20);
mjr 5:a70c0bce770d 4418 scl = 0;
mjr 5:a70c0bce770d 4419 wait_us(20);
mjr 5:a70c0bce770d 4420 }
mjr 5:a70c0bce770d 4421 }
mjr 76:7f5912b6340e 4422
mjr 76:7f5912b6340e 4423
mjr 14:df700b22ca08 4424 // ---------------------------------------------------------------------------
mjr 14:df700b22ca08 4425 //
mjr 33:d832bcab089e 4426 // Simple binary (on/off) input debouncer. Requires an input to be stable
mjr 33:d832bcab089e 4427 // for a given interval before allowing an update.
mjr 33:d832bcab089e 4428 //
mjr 33:d832bcab089e 4429 class Debouncer
mjr 33:d832bcab089e 4430 {
mjr 33:d832bcab089e 4431 public:
mjr 33:d832bcab089e 4432 Debouncer(bool initVal, float tmin)
mjr 33:d832bcab089e 4433 {
mjr 33:d832bcab089e 4434 t.start();
mjr 33:d832bcab089e 4435 this->stable = this->prv = initVal;
mjr 33:d832bcab089e 4436 this->tmin = tmin;
mjr 33:d832bcab089e 4437 }
mjr 33:d832bcab089e 4438
mjr 33:d832bcab089e 4439 // Get the current stable value
mjr 33:d832bcab089e 4440 bool val() const { return stable; }
mjr 33:d832bcab089e 4441
mjr 33:d832bcab089e 4442 // Apply a new sample. This tells us the new raw reading from the
mjr 33:d832bcab089e 4443 // input device.
mjr 33:d832bcab089e 4444 void sampleIn(bool val)
mjr 33:d832bcab089e 4445 {
mjr 33:d832bcab089e 4446 // If the new raw reading is different from the previous
mjr 33:d832bcab089e 4447 // raw reading, we've detected an edge - start the clock
mjr 33:d832bcab089e 4448 // on the sample reader.
mjr 33:d832bcab089e 4449 if (val != prv)
mjr 33:d832bcab089e 4450 {
mjr 33:d832bcab089e 4451 // we have an edge - reset the sample clock
mjr 33:d832bcab089e 4452 t.reset();
mjr 33:d832bcab089e 4453
mjr 33:d832bcab089e 4454 // this is now the previous raw sample for nxt time
mjr 33:d832bcab089e 4455 prv = val;
mjr 33:d832bcab089e 4456 }
mjr 33:d832bcab089e 4457 else if (val != stable)
mjr 33:d832bcab089e 4458 {
mjr 33:d832bcab089e 4459 // The new raw sample is the same as the last raw sample,
mjr 33:d832bcab089e 4460 // and different from the stable value. This means that
mjr 33:d832bcab089e 4461 // the sample value has been the same for the time currently
mjr 33:d832bcab089e 4462 // indicated by our timer. If enough time has elapsed to
mjr 33:d832bcab089e 4463 // consider the value stable, apply the new value.
mjr 33:d832bcab089e 4464 if (t.read() > tmin)
mjr 33:d832bcab089e 4465 stable = val;
mjr 33:d832bcab089e 4466 }
mjr 33:d832bcab089e 4467 }
mjr 33:d832bcab089e 4468
mjr 33:d832bcab089e 4469 private:
mjr 33:d832bcab089e 4470 // current stable value
mjr 33:d832bcab089e 4471 bool stable;
mjr 33:d832bcab089e 4472
mjr 33:d832bcab089e 4473 // last raw sample value
mjr 33:d832bcab089e 4474 bool prv;
mjr 33:d832bcab089e 4475
mjr 33:d832bcab089e 4476 // elapsed time since last raw input change
mjr 33:d832bcab089e 4477 Timer t;
mjr 33:d832bcab089e 4478
mjr 33:d832bcab089e 4479 // Minimum time interval for stability, in seconds. Input readings
mjr 33:d832bcab089e 4480 // must be stable for this long before the stable value is updated.
mjr 33:d832bcab089e 4481 float tmin;
mjr 33:d832bcab089e 4482 };
mjr 33:d832bcab089e 4483
mjr 33:d832bcab089e 4484
mjr 33:d832bcab089e 4485 // ---------------------------------------------------------------------------
mjr 33:d832bcab089e 4486 //
mjr 33:d832bcab089e 4487 // TV ON timer. If this feature is enabled, we toggle a TV power switch
mjr 33:d832bcab089e 4488 // relay (connected to a GPIO pin) to turn on the cab's TV monitors shortly
mjr 33:d832bcab089e 4489 // after the system is powered. This is useful for TVs that don't remember
mjr 33:d832bcab089e 4490 // their power state and don't turn back on automatically after being
mjr 33:d832bcab089e 4491 // unplugged and plugged in again. This feature requires external
mjr 33:d832bcab089e 4492 // circuitry, which is built in to the expansion board and can also be
mjr 33:d832bcab089e 4493 // built separately - see the Build Guide for the circuit plan.
mjr 33:d832bcab089e 4494 //
mjr 33:d832bcab089e 4495 // Theory of operation: to use this feature, the cabinet must have a
mjr 33:d832bcab089e 4496 // secondary PC-style power supply (PSU2) for the feedback devices, and
mjr 33:d832bcab089e 4497 // this secondary supply must be plugged in to the same power strip or
mjr 33:d832bcab089e 4498 // switched outlet that controls power to the TVs. This lets us use PSU2
mjr 33:d832bcab089e 4499 // as a proxy for the TV power state - when PSU2 is on, the TV outlet is
mjr 33:d832bcab089e 4500 // powered, and when PSU2 is off, the TV outlet is off. We use a little
mjr 33:d832bcab089e 4501 // latch circuit powered by PSU2 to monitor the status. The latch has a
mjr 33:d832bcab089e 4502 // current state, ON or OFF, that we can read via a GPIO input pin, and
mjr 33:d832bcab089e 4503 // we can set the state to ON by pulsing a separate GPIO output pin. As
mjr 33:d832bcab089e 4504 // long as PSU2 is powered off, the latch stays in the OFF state, even if
mjr 33:d832bcab089e 4505 // we try to set it by pulsing the SET pin. When PSU2 is turned on after
mjr 33:d832bcab089e 4506 // being off, the latch starts receiving power but stays in the OFF state,
mjr 33:d832bcab089e 4507 // since this is the initial condition when the power first comes on. So
mjr 33:d832bcab089e 4508 // if our latch state pin is reading OFF, we know that PSU2 is either off
mjr 33:d832bcab089e 4509 // now or *was* off some time since we last checked. We use a timer to
mjr 33:d832bcab089e 4510 // check the state periodically. Each time we see the state is OFF, we
mjr 33:d832bcab089e 4511 // try pulsing the SET pin. If the state still reads as OFF, we know
mjr 33:d832bcab089e 4512 // that PSU2 is currently off; if the state changes to ON, though, we
mjr 33:d832bcab089e 4513 // know that PSU2 has gone from OFF to ON some time between now and the
mjr 33:d832bcab089e 4514 // previous check. When we see this condition, we start a countdown
mjr 33:d832bcab089e 4515 // timer, and pulse the TV switch relay when the countdown ends.
mjr 33:d832bcab089e 4516 //
mjr 40:cc0d9814522b 4517 // This scheme might seem a little convoluted, but it handles a number
mjr 40:cc0d9814522b 4518 // of tricky but likely scenarios:
mjr 33:d832bcab089e 4519 //
mjr 33:d832bcab089e 4520 // - Most cabinets systems are set up with "soft" PC power switches,
mjr 40:cc0d9814522b 4521 // so that the PC goes into "Soft Off" mode when the user turns off
mjr 40:cc0d9814522b 4522 // the cabinet by pushing the power button or using the Shut Down
mjr 40:cc0d9814522b 4523 // command from within Windows. In Windows parlance, this "soft off"
mjr 40:cc0d9814522b 4524 // condition is called ACPI State S5. In this state, the main CPU
mjr 40:cc0d9814522b 4525 // power is turned off, but the motherboard still provides power to
mjr 40:cc0d9814522b 4526 // USB devices. This means that the KL25Z keeps running. Without
mjr 40:cc0d9814522b 4527 // the external power sensing circuit, the only hint that we're in
mjr 40:cc0d9814522b 4528 // this state is that the USB connection to the host goes into Suspend
mjr 40:cc0d9814522b 4529 // mode, but that could mean other things as well. The latch circuit
mjr 40:cc0d9814522b 4530 // lets us tell for sure that we're in this state.
mjr 33:d832bcab089e 4531 //
mjr 33:d832bcab089e 4532 // - Some cabinet builders might prefer to use "hard" power switches,
mjr 33:d832bcab089e 4533 // cutting all power to the cabinet, including the PC motherboard (and
mjr 33:d832bcab089e 4534 // thus the KL25Z) every time the machine is turned off. This also
mjr 33:d832bcab089e 4535 // applies to the "soft" switch case above when the cabinet is unplugged,
mjr 33:d832bcab089e 4536 // a power outage occurs, etc. In these cases, the KL25Z will do a cold
mjr 33:d832bcab089e 4537 // boot when the PC is turned on. We don't know whether the KL25Z
mjr 33:d832bcab089e 4538 // will power up before or after PSU2, so it's not good enough to
mjr 40:cc0d9814522b 4539 // observe the current state of PSU2 when we first check. If PSU2
mjr 40:cc0d9814522b 4540 // were to come on first, checking only the current state would fool
mjr 40:cc0d9814522b 4541 // us into thinking that no action is required, because we'd only see
mjr 40:cc0d9814522b 4542 // that PSU2 is turned on any time we check. The latch handles this
mjr 40:cc0d9814522b 4543 // case by letting us see that PSU2 was indeed off some time before our
mjr 40:cc0d9814522b 4544 // first check.
mjr 33:d832bcab089e 4545 //
mjr 33:d832bcab089e 4546 // - If the KL25Z is rebooted while the main system is running, or the
mjr 40:cc0d9814522b 4547 // KL25Z is unplugged and plugged back in, we'll correctly leave the
mjr 33:d832bcab089e 4548 // TVs as they are. The latch state is independent of the KL25Z's
mjr 33:d832bcab089e 4549 // power or software state, so it's won't affect the latch state when
mjr 33:d832bcab089e 4550 // the KL25Z is unplugged or rebooted; when we boot, we'll see that
mjr 33:d832bcab089e 4551 // the latch is already on and that we don't have to turn on the TVs.
mjr 33:d832bcab089e 4552 // This is important because TV ON buttons are usually on/off toggles,
mjr 33:d832bcab089e 4553 // so we don't want to push the button on a TV that's already on.
mjr 33:d832bcab089e 4554 //
mjr 33:d832bcab089e 4555
mjr 77:0b96f6867312 4556 // Current PSU2 power state:
mjr 33:d832bcab089e 4557 // 1 -> default: latch was on at last check, or we haven't checked yet
mjr 33:d832bcab089e 4558 // 2 -> latch was off at last check, SET pulsed high
mjr 33:d832bcab089e 4559 // 3 -> SET pulsed low, ready to check status
mjr 33:d832bcab089e 4560 // 4 -> TV timer countdown in progress
mjr 33:d832bcab089e 4561 // 5 -> TV relay on
mjr 77:0b96f6867312 4562 // 6 -> sending IR signals designed as TV ON signals
mjr 73:4e8ce0b18915 4563 uint8_t psu2_state = 1;
mjr 73:4e8ce0b18915 4564
mjr 73:4e8ce0b18915 4565 // TV relay state. The TV relay can be controlled by the power-on
mjr 73:4e8ce0b18915 4566 // timer and directly from the PC (via USB commands), so keep a
mjr 73:4e8ce0b18915 4567 // separate state for each:
mjr 73:4e8ce0b18915 4568 // 0x01 -> turned on by power-on timer
mjr 73:4e8ce0b18915 4569 // 0x02 -> turned on by USB command
mjr 73:4e8ce0b18915 4570 uint8_t tv_relay_state = 0x00;
mjr 73:4e8ce0b18915 4571 const uint8_t TV_RELAY_POWERON = 0x01;
mjr 73:4e8ce0b18915 4572 const uint8_t TV_RELAY_USB = 0x02;
mjr 73:4e8ce0b18915 4573
mjr 79:682ae3171a08 4574 // pulse timer for manual TV relay pulses
mjr 79:682ae3171a08 4575 Timer tvRelayManualTimer;
mjr 79:682ae3171a08 4576
mjr 77:0b96f6867312 4577 // TV ON IR command state. When the main PSU2 power state reaches
mjr 77:0b96f6867312 4578 // the IR phase, we use this sub-state counter to send the TV ON
mjr 77:0b96f6867312 4579 // IR signals. We initialize to state 0 when the main state counter
mjr 77:0b96f6867312 4580 // reaches the IR step. In state 0, we start transmitting the first
mjr 77:0b96f6867312 4581 // (lowest numbered) IR command slot marked as containing a TV ON
mjr 77:0b96f6867312 4582 // code, and advance to state 1. In state 1, we check to see if
mjr 77:0b96f6867312 4583 // the transmitter is still sending; if so, we do nothing, if so
mjr 77:0b96f6867312 4584 // we start transmitting the second TV ON code and advance to state
mjr 77:0b96f6867312 4585 // 2. Continue until we run out of TV ON IR codes, at which point
mjr 77:0b96f6867312 4586 // we advance to the next main psu2_state step.
mjr 77:0b96f6867312 4587 uint8_t tvon_ir_state = 0;
mjr 77:0b96f6867312 4588
mjr 77:0b96f6867312 4589 // TV ON switch relay control output pin
mjr 73:4e8ce0b18915 4590 DigitalOut *tv_relay;
mjr 35:e959ffba78fd 4591
mjr 35:e959ffba78fd 4592 // PSU2 power sensing circuit connections
mjr 35:e959ffba78fd 4593 DigitalIn *psu2_status_sense;
mjr 35:e959ffba78fd 4594 DigitalOut *psu2_status_set;
mjr 35:e959ffba78fd 4595
mjr 73:4e8ce0b18915 4596 // Apply the current TV relay state
mjr 73:4e8ce0b18915 4597 void tvRelayUpdate(uint8_t bit, bool state)
mjr 73:4e8ce0b18915 4598 {
mjr 73:4e8ce0b18915 4599 // update the state
mjr 73:4e8ce0b18915 4600 if (state)
mjr 73:4e8ce0b18915 4601 tv_relay_state |= bit;
mjr 73:4e8ce0b18915 4602 else
mjr 73:4e8ce0b18915 4603 tv_relay_state &= ~bit;
mjr 73:4e8ce0b18915 4604
mjr 73:4e8ce0b18915 4605 // set the relay GPIO to the new state
mjr 73:4e8ce0b18915 4606 if (tv_relay != 0)
mjr 73:4e8ce0b18915 4607 tv_relay->write(tv_relay_state != 0);
mjr 73:4e8ce0b18915 4608 }
mjr 35:e959ffba78fd 4609
mjr 86:e30a1f60f783 4610 // Does the current power status allow a reboot? We shouldn't reboot
mjr 86:e30a1f60f783 4611 // in certain power states, because some states are purely internal:
mjr 86:e30a1f60f783 4612 // we can't get enough information from the external power sensor to
mjr 86:e30a1f60f783 4613 // return to the same state later. Code that performs discretionary
mjr 86:e30a1f60f783 4614 // reboots should always check here first, and delay any reboot until
mjr 86:e30a1f60f783 4615 // we say it's okay.
mjr 86:e30a1f60f783 4616 static inline bool powerStatusAllowsReboot()
mjr 86:e30a1f60f783 4617 {
mjr 86:e30a1f60f783 4618 // The only safe state for rebooting is state 1, idle/default.
mjr 86:e30a1f60f783 4619 // In other states, we can't reboot, because the external sensor
mjr 86:e30a1f60f783 4620 // and latch circuit doesn't give us enough information to return
mjr 86:e30a1f60f783 4621 // to the same state later.
mjr 86:e30a1f60f783 4622 return psu2_state == 1;
mjr 86:e30a1f60f783 4623 }
mjr 86:e30a1f60f783 4624
mjr 77:0b96f6867312 4625 // PSU2 Status update routine. The main loop calls this from time
mjr 77:0b96f6867312 4626 // to time to update the power sensing state and carry out TV ON
mjr 77:0b96f6867312 4627 // functions.
mjr 77:0b96f6867312 4628 Timer powerStatusTimer;
mjr 77:0b96f6867312 4629 uint32_t tv_delay_time_us;
mjr 77:0b96f6867312 4630 void powerStatusUpdate(Config &cfg)
mjr 33:d832bcab089e 4631 {
mjr 79:682ae3171a08 4632 // If the manual relay pulse timer is past the pulse time, end the
mjr 79:682ae3171a08 4633 // manual pulse. The timer only runs when a pulse is active, so
mjr 79:682ae3171a08 4634 // it'll never read as past the time limit if a pulse isn't on.
mjr 79:682ae3171a08 4635 if (tvRelayManualTimer.read_us() > 250000)
mjr 79:682ae3171a08 4636 {
mjr 79:682ae3171a08 4637 // turn off the relay and disable the timer
mjr 79:682ae3171a08 4638 tvRelayUpdate(TV_RELAY_USB, false);
mjr 79:682ae3171a08 4639 tvRelayManualTimer.stop();
mjr 79:682ae3171a08 4640 tvRelayManualTimer.reset();
mjr 79:682ae3171a08 4641 }
mjr 79:682ae3171a08 4642
mjr 77:0b96f6867312 4643 // Only update every 1/4 second or so. Note that if the PSU2
mjr 77:0b96f6867312 4644 // circuit isn't configured, the initialization routine won't
mjr 77:0b96f6867312 4645 // start the timer, so it'll always read zero and we'll always
mjr 77:0b96f6867312 4646 // skip this whole routine.
mjr 77:0b96f6867312 4647 if (powerStatusTimer.read_us() < 250000)
mjr 77:0b96f6867312 4648 return;
mjr 77:0b96f6867312 4649
mjr 77:0b96f6867312 4650 // reset the update timer for next time
mjr 77:0b96f6867312 4651 powerStatusTimer.reset();
mjr 77:0b96f6867312 4652
mjr 77:0b96f6867312 4653 // TV ON timer. We start this timer when we detect a change
mjr 77:0b96f6867312 4654 // in the PSU2 status from OFF to ON. When the timer reaches
mjr 77:0b96f6867312 4655 // the configured TV ON delay time, and the PSU2 power is still
mjr 77:0b96f6867312 4656 // on, we'll trigger the TV ON relay and send the TV ON IR codes.
mjr 35:e959ffba78fd 4657 static Timer tv_timer;
mjr 35:e959ffba78fd 4658
mjr 33:d832bcab089e 4659 // Check our internal state
mjr 33:d832bcab089e 4660 switch (psu2_state)
mjr 33:d832bcab089e 4661 {
mjr 33:d832bcab089e 4662 case 1:
mjr 33:d832bcab089e 4663 // Default state. This means that the latch was on last
mjr 33:d832bcab089e 4664 // time we checked or that this is the first check. In
mjr 33:d832bcab089e 4665 // either case, if the latch is off, switch to state 2 and
mjr 33:d832bcab089e 4666 // try pulsing the latch. Next time we check, if the latch
mjr 33:d832bcab089e 4667 // stuck, it means that PSU2 is now on after being off.
mjr 35:e959ffba78fd 4668 if (!psu2_status_sense->read())
mjr 33:d832bcab089e 4669 {
mjr 33:d832bcab089e 4670 // switch to OFF state
mjr 33:d832bcab089e 4671 psu2_state = 2;
mjr 33:d832bcab089e 4672
mjr 33:d832bcab089e 4673 // try setting the latch
mjr 35:e959ffba78fd 4674 psu2_status_set->write(1);
mjr 33:d832bcab089e 4675 }
mjr 77:0b96f6867312 4676 powerTimerDiagState = 0;
mjr 33:d832bcab089e 4677 break;
mjr 33:d832bcab089e 4678
mjr 33:d832bcab089e 4679 case 2:
mjr 33:d832bcab089e 4680 // PSU2 was off last time we checked, and we tried setting
mjr 33:d832bcab089e 4681 // the latch. Drop the SET signal and go to CHECK state.
mjr 35:e959ffba78fd 4682 psu2_status_set->write(0);
mjr 33:d832bcab089e 4683 psu2_state = 3;
mjr 77:0b96f6867312 4684 powerTimerDiagState = 0;
mjr 33:d832bcab089e 4685 break;
mjr 33:d832bcab089e 4686
mjr 33:d832bcab089e 4687 case 3:
mjr 33:d832bcab089e 4688 // CHECK state: we pulsed SET, and we're now ready to see
mjr 40:cc0d9814522b 4689 // if it stuck. If the latch is now on, PSU2 has transitioned
mjr 33:d832bcab089e 4690 // from OFF to ON, so start the TV countdown. If the latch is
mjr 33:d832bcab089e 4691 // off, our SET command didn't stick, so PSU2 is still off.
mjr 35:e959ffba78fd 4692 if (psu2_status_sense->read())
mjr 33:d832bcab089e 4693 {
mjr 33:d832bcab089e 4694 // The latch stuck, so PSU2 has transitioned from OFF
mjr 33:d832bcab089e 4695 // to ON. Start the TV countdown timer.
mjr 33:d832bcab089e 4696 tv_timer.reset();
mjr 33:d832bcab089e 4697 tv_timer.start();
mjr 33:d832bcab089e 4698 psu2_state = 4;
mjr 73:4e8ce0b18915 4699
mjr 73:4e8ce0b18915 4700 // start the power timer diagnostic flashes
mjr 73:4e8ce0b18915 4701 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4702 }
mjr 33:d832bcab089e 4703 else
mjr 33:d832bcab089e 4704 {
mjr 33:d832bcab089e 4705 // The latch didn't stick, so PSU2 was still off at
mjr 87:8d35c74403af 4706 // our last check. Return to idle state.
mjr 87:8d35c74403af 4707 psu2_state = 1;
mjr 33:d832bcab089e 4708 }
mjr 33:d832bcab089e 4709 break;
mjr 33:d832bcab089e 4710
mjr 33:d832bcab089e 4711 case 4:
mjr 77:0b96f6867312 4712 // TV timer countdown in progress. The latch has to stay on during
mjr 77:0b96f6867312 4713 // the countdown; if the latch turns off, PSU2 power must have gone
mjr 77:0b96f6867312 4714 // off again before the countdown finished.
mjr 77:0b96f6867312 4715 if (!psu2_status_sense->read())
mjr 77:0b96f6867312 4716 {
mjr 77:0b96f6867312 4717 // power is off - start a new check cycle
mjr 77:0b96f6867312 4718 psu2_status_set->write(1);
mjr 77:0b96f6867312 4719 psu2_state = 2;
mjr 77:0b96f6867312 4720 break;
mjr 77:0b96f6867312 4721 }
mjr 77:0b96f6867312 4722
mjr 77:0b96f6867312 4723 // Flash the power time diagnostic every two cycles
mjr 77:0b96f6867312 4724 powerTimerDiagState = (powerTimerDiagState + 1) & 0x03;
mjr 77:0b96f6867312 4725
mjr 77:0b96f6867312 4726 // if we've reached the delay time, pulse the relay
mjr 77:0b96f6867312 4727 if (tv_timer.read_us() >= tv_delay_time_us)
mjr 33:d832bcab089e 4728 {
mjr 33:d832bcab089e 4729 // turn on the relay for one timer interval
mjr 73:4e8ce0b18915 4730 tvRelayUpdate(TV_RELAY_POWERON, true);
mjr 33:d832bcab089e 4731 psu2_state = 5;
mjr 77:0b96f6867312 4732
mjr 77:0b96f6867312 4733 // show solid blue on the diagnostic LED while the relay is on
mjr 77:0b96f6867312 4734 powerTimerDiagState = 2;
mjr 33:d832bcab089e 4735 }
mjr 33:d832bcab089e 4736 break;
mjr 33:d832bcab089e 4737
mjr 33:d832bcab089e 4738 case 5:
mjr 33:d832bcab089e 4739 // TV timer relay on. We pulse this for one interval, so
mjr 77:0b96f6867312 4740 // it's now time to turn it off.
mjr 73:4e8ce0b18915 4741 tvRelayUpdate(TV_RELAY_POWERON, false);
mjr 77:0b96f6867312 4742
mjr 77:0b96f6867312 4743 // Proceed to sending any TV ON IR commands
mjr 77:0b96f6867312 4744 psu2_state = 6;
mjr 77:0b96f6867312 4745 tvon_ir_state = 0;
mjr 77:0b96f6867312 4746
mjr 77:0b96f6867312 4747 // diagnostic LEDs off for now
mjr 77:0b96f6867312 4748 powerTimerDiagState = 0;
mjr 77:0b96f6867312 4749 break;
mjr 77:0b96f6867312 4750
mjr 77:0b96f6867312 4751 case 6:
mjr 77:0b96f6867312 4752 // Sending TV ON IR signals. Start with the assumption that
mjr 77:0b96f6867312 4753 // we have no IR work to do, in which case we're done with the
mjr 77:0b96f6867312 4754 // whole TV ON sequence. So by default return to state 1.
mjr 33:d832bcab089e 4755 psu2_state = 1;
mjr 77:0b96f6867312 4756 powerTimerDiagState = 0;
mjr 73:4e8ce0b18915 4757
mjr 77:0b96f6867312 4758 // If we have an IR emitter, check for TV ON IR commands
mjr 77:0b96f6867312 4759 if (ir_tx != 0)
mjr 77:0b96f6867312 4760 {
mjr 77:0b96f6867312 4761 // check to see if the last transmission is still in progress
mjr 77:0b96f6867312 4762 if (ir_tx->isSending())
mjr 77:0b96f6867312 4763 {
mjr 77:0b96f6867312 4764 // We're still sending the last transmission. Stay in
mjr 77:0b96f6867312 4765 // state 6.
mjr 77:0b96f6867312 4766 psu2_state = 6;
mjr 77:0b96f6867312 4767 powerTimerDiagState = 4;
mjr 77:0b96f6867312 4768 break;
mjr 77:0b96f6867312 4769 }
mjr 77:0b96f6867312 4770
mjr 77:0b96f6867312 4771 // The last transmission is done, so check for a new one.
mjr 77:0b96f6867312 4772 // Look for the Nth TV ON IR slot, where N is our state
mjr 77:0b96f6867312 4773 // number.
mjr 77:0b96f6867312 4774 for (int i = 0, n = 0 ; i < MAX_IR_CODES ; ++i)
mjr 77:0b96f6867312 4775 {
mjr 77:0b96f6867312 4776 // is this a TV ON command?
mjr 77:0b96f6867312 4777 if ((cfg.IRCommand[i].flags & IRFlagTVON) != 0)
mjr 77:0b96f6867312 4778 {
mjr 77:0b96f6867312 4779 // It's a TV ON command - check if it's the one we're
mjr 77:0b96f6867312 4780 // looking for.
mjr 77:0b96f6867312 4781 if (n == tvon_ir_state)
mjr 77:0b96f6867312 4782 {
mjr 77:0b96f6867312 4783 // It's the one. Start transmitting it by
mjr 77:0b96f6867312 4784 // pushing its virtual button.
mjr 77:0b96f6867312 4785 int vb = IRConfigSlotToVirtualButton[i];
mjr 77:0b96f6867312 4786 ir_tx->pushButton(vb, true);
mjr 77:0b96f6867312 4787
mjr 77:0b96f6867312 4788 // Pushing the button starts transmission, and once
mjr 88:98bce687e6c0 4789 // started, the transmission runs to completion even
mjr 88:98bce687e6c0 4790 // if the button is no longer pushed. So we can
mjr 88:98bce687e6c0 4791 // immediately un-push the button, since we only need
mjr 88:98bce687e6c0 4792 // to send the code once.
mjr 77:0b96f6867312 4793 ir_tx->pushButton(vb, false);
mjr 77:0b96f6867312 4794
mjr 77:0b96f6867312 4795 // Advance to the next TV ON IR state, where we'll
mjr 77:0b96f6867312 4796 // await the end of this transmission and move on to
mjr 77:0b96f6867312 4797 // the next one.
mjr 77:0b96f6867312 4798 psu2_state = 6;
mjr 77:0b96f6867312 4799 tvon_ir_state++;
mjr 77:0b96f6867312 4800 break;
mjr 77:0b96f6867312 4801 }
mjr 77:0b96f6867312 4802
mjr 77:0b96f6867312 4803 // it's not ours - count it and keep looking
mjr 77:0b96f6867312 4804 ++n;
mjr 77:0b96f6867312 4805 }
mjr 77:0b96f6867312 4806 }
mjr 77:0b96f6867312 4807 }
mjr 33:d832bcab089e 4808 break;
mjr 33:d832bcab089e 4809 }
mjr 77:0b96f6867312 4810
mjr 77:0b96f6867312 4811 // update the diagnostic LEDs
mjr 77:0b96f6867312 4812 diagLED();
mjr 33:d832bcab089e 4813 }
mjr 33:d832bcab089e 4814
mjr 77:0b96f6867312 4815 // Start the power status timer. If the status sense circuit is enabled
mjr 77:0b96f6867312 4816 // in the configuration, we'll set up the pin connections and start the
mjr 77:0b96f6867312 4817 // timer for our periodic status checks. Does nothing if any of the pins
mjr 77:0b96f6867312 4818 // are configured as NC.
mjr 77:0b96f6867312 4819 void startPowerStatusTimer(Config &cfg)
mjr 35:e959ffba78fd 4820 {
mjr 55:4db125cd11a0 4821 // only start the timer if the pins are configured and the delay
mjr 55:4db125cd11a0 4822 // time is nonzero
mjr 77:0b96f6867312 4823 powerStatusTimer.reset();
mjr 77:0b96f6867312 4824 if (cfg.TVON.statusPin != 0xFF
mjr 77:0b96f6867312 4825 && cfg.TVON.latchPin != 0xFF)
mjr 35:e959ffba78fd 4826 {
mjr 77:0b96f6867312 4827 // set up the power sensing circuit connections
mjr 53:9b2611964afc 4828 psu2_status_sense = new DigitalIn(wirePinName(cfg.TVON.statusPin));
mjr 53:9b2611964afc 4829 psu2_status_set = new DigitalOut(wirePinName(cfg.TVON.latchPin));
mjr 77:0b96f6867312 4830
mjr 77:0b96f6867312 4831 // if there's a TV ON relay, set up its control pin
mjr 77:0b96f6867312 4832 if (cfg.TVON.relayPin != 0xFF)
mjr 77:0b96f6867312 4833 tv_relay = new DigitalOut(wirePinName(cfg.TVON.relayPin));
mjr 77:0b96f6867312 4834
mjr 77:0b96f6867312 4835 // Set the TV ON delay time. We store the time internally in
mjr 77:0b96f6867312 4836 // microseconds, but the configuration stores it in units of
mjr 77:0b96f6867312 4837 // 1/100 second = 10ms = 10000us.
mjr 77:0b96f6867312 4838 tv_delay_time_us = cfg.TVON.delayTime * 10000;;
mjr 77:0b96f6867312 4839
mjr 77:0b96f6867312 4840 // Start the TV timer
mjr 77:0b96f6867312 4841 powerStatusTimer.start();
mjr 35:e959ffba78fd 4842 }
mjr 35:e959ffba78fd 4843 }
mjr 35:e959ffba78fd 4844
mjr 73:4e8ce0b18915 4845 // Operate the TV ON relay. This allows manual control of the relay
mjr 73:4e8ce0b18915 4846 // from the PC. See protocol message 65 submessage 11.
mjr 73:4e8ce0b18915 4847 //
mjr 73:4e8ce0b18915 4848 // Mode:
mjr 73:4e8ce0b18915 4849 // 0 = turn relay off
mjr 73:4e8ce0b18915 4850 // 1 = turn relay on
mjr 73:4e8ce0b18915 4851 // 2 = pulse relay
mjr 73:4e8ce0b18915 4852 void TVRelay(int mode)
mjr 73:4e8ce0b18915 4853 {
mjr 73:4e8ce0b18915 4854 // if there's no TV relay control pin, ignore this
mjr 73:4e8ce0b18915 4855 if (tv_relay == 0)
mjr 73:4e8ce0b18915 4856 return;
mjr 73:4e8ce0b18915 4857
mjr 73:4e8ce0b18915 4858 switch (mode)
mjr 73:4e8ce0b18915 4859 {
mjr 73:4e8ce0b18915 4860 case 0:
mjr 73:4e8ce0b18915 4861 // relay off
mjr 73:4e8ce0b18915 4862 tvRelayUpdate(TV_RELAY_USB, false);
mjr 73:4e8ce0b18915 4863 break;
mjr 73:4e8ce0b18915 4864
mjr 73:4e8ce0b18915 4865 case 1:
mjr 73:4e8ce0b18915 4866 // relay on
mjr 73:4e8ce0b18915 4867 tvRelayUpdate(TV_RELAY_USB, true);
mjr 73:4e8ce0b18915 4868 break;
mjr 73:4e8ce0b18915 4869
mjr 73:4e8ce0b18915 4870 case 2:
mjr 79:682ae3171a08 4871 // Turn the relay on and reset the manual TV pulse timer
mjr 73:4e8ce0b18915 4872 tvRelayUpdate(TV_RELAY_USB, true);
mjr 79:682ae3171a08 4873 tvRelayManualTimer.reset();
mjr 79:682ae3171a08 4874 tvRelayManualTimer.start();
mjr 73:4e8ce0b18915 4875 break;
mjr 73:4e8ce0b18915 4876 }
mjr 73:4e8ce0b18915 4877 }
mjr 73:4e8ce0b18915 4878
mjr 73:4e8ce0b18915 4879
mjr 35:e959ffba78fd 4880 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 4881 //
mjr 35:e959ffba78fd 4882 // In-memory configuration data structure. This is the live version in RAM
mjr 35:e959ffba78fd 4883 // that we use to determine how things are set up.
mjr 35:e959ffba78fd 4884 //
mjr 35:e959ffba78fd 4885 // When we save the configuration settings, we copy this structure to
mjr 35:e959ffba78fd 4886 // non-volatile flash memory. At startup, we check the flash location where
mjr 35:e959ffba78fd 4887 // we might have saved settings on a previous run, and it's valid, we copy
mjr 35:e959ffba78fd 4888 // the flash data to this structure. Firmware updates wipe the flash
mjr 35:e959ffba78fd 4889 // memory area, so you have to use the PC config tool to send the settings
mjr 35:e959ffba78fd 4890 // again each time the firmware is updated.
mjr 35:e959ffba78fd 4891 //
mjr 35:e959ffba78fd 4892 NVM nvm;
mjr 35:e959ffba78fd 4893
mjr 86:e30a1f60f783 4894 // Save Config followup time, in seconds. After a successful save,
mjr 86:e30a1f60f783 4895 // we leave the success flag on in the status for this interval. At
mjr 86:e30a1f60f783 4896 // the end of the interval, we reboot the device if requested.
mjr 86:e30a1f60f783 4897 uint8_t saveConfigFollowupTime;
mjr 86:e30a1f60f783 4898
mjr 86:e30a1f60f783 4899 // is a reboot pending at the end of the config save followup interval?
mjr 86:e30a1f60f783 4900 uint8_t saveConfigRebootPending;
mjr 77:0b96f6867312 4901
mjr 79:682ae3171a08 4902 // status flag for successful config save - set to 0x40 on success
mjr 79:682ae3171a08 4903 uint8_t saveConfigSucceededFlag;
mjr 79:682ae3171a08 4904
mjr 86:e30a1f60f783 4905 // Timer for configuration change followup timer
mjr 86:e30a1f60f783 4906 ExtTimer saveConfigFollowupTimer;
mjr 86:e30a1f60f783 4907
mjr 86:e30a1f60f783 4908
mjr 35:e959ffba78fd 4909 // For convenience, a macro for the Config part of the NVM structure
mjr 35:e959ffba78fd 4910 #define cfg (nvm.d.c)
mjr 35:e959ffba78fd 4911
mjr 35:e959ffba78fd 4912 // flash memory controller interface
mjr 35:e959ffba78fd 4913 FreescaleIAP iap;
mjr 35:e959ffba78fd 4914
mjr 79:682ae3171a08 4915 // figure the flash address for the config data
mjr 79:682ae3171a08 4916 const NVM *configFlashAddr()
mjr 76:7f5912b6340e 4917 {
mjr 79:682ae3171a08 4918 // figure the number of sectors we need, rounding up
mjr 79:682ae3171a08 4919 int nSectors = (sizeof(NVM) + SECTOR_SIZE - 1)/SECTOR_SIZE;
mjr 79:682ae3171a08 4920
mjr 79:682ae3171a08 4921 // figure the total size required from the number of sectors
mjr 79:682ae3171a08 4922 int reservedSize = nSectors * SECTOR_SIZE;
mjr 79:682ae3171a08 4923
mjr 79:682ae3171a08 4924 // locate it at the top of memory
mjr 79:682ae3171a08 4925 uint32_t addr = iap.flashSize() - reservedSize;
mjr 79:682ae3171a08 4926
mjr 79:682ae3171a08 4927 // return it as a read-only NVM pointer
mjr 79:682ae3171a08 4928 return (const NVM *)addr;
mjr 35:e959ffba78fd 4929 }
mjr 35:e959ffba78fd 4930
mjr 76:7f5912b6340e 4931 // Load the config from flash. Returns true if a valid non-default
mjr 76:7f5912b6340e 4932 // configuration was loaded, false if we not. If we return false,
mjr 76:7f5912b6340e 4933 // we load the factory defaults, so the configuration object is valid
mjr 76:7f5912b6340e 4934 // in either case.
mjr 76:7f5912b6340e 4935 bool loadConfigFromFlash()
mjr 35:e959ffba78fd 4936 {
mjr 35:e959ffba78fd 4937 // We want to use the KL25Z's on-board flash to store our configuration
mjr 35:e959ffba78fd 4938 // data persistently, so that we can restore it across power cycles.
mjr 35:e959ffba78fd 4939 // Unfortunatly, the mbed platform doesn't explicitly support this.
mjr 35:e959ffba78fd 4940 // mbed treats the on-board flash as a raw storage device for linker
mjr 35:e959ffba78fd 4941 // output, and assumes that the linker output is the only thing
mjr 35:e959ffba78fd 4942 // stored there. There's no file system and no allowance for shared
mjr 35:e959ffba78fd 4943 // use for other purposes. Fortunately, the linker ues the space in
mjr 35:e959ffba78fd 4944 // the obvious way, storing the entire linked program in a contiguous
mjr 35:e959ffba78fd 4945 // block starting at the lowest flash address. This means that the
mjr 35:e959ffba78fd 4946 // rest of flash - from the end of the linked program to the highest
mjr 35:e959ffba78fd 4947 // flash address - is all unused free space. Writing our data there
mjr 35:e959ffba78fd 4948 // won't conflict with anything else. Since the linker doesn't give
mjr 35:e959ffba78fd 4949 // us any programmatic access to the total linker output size, it's
mjr 35:e959ffba78fd 4950 // safest to just store our config data at the very end of the flash
mjr 35:e959ffba78fd 4951 // region (i.e., the highest address). As long as it's smaller than
mjr 35:e959ffba78fd 4952 // the free space, it won't collide with the linker area.
mjr 35:e959ffba78fd 4953
mjr 35:e959ffba78fd 4954 // Figure how many sectors we need for our structure
mjr 79:682ae3171a08 4955 const NVM *flash = configFlashAddr();
mjr 35:e959ffba78fd 4956
mjr 35:e959ffba78fd 4957 // if the flash is valid, load it; otherwise initialize to defaults
mjr 76:7f5912b6340e 4958 bool nvm_valid = flash->valid();
mjr 76:7f5912b6340e 4959 if (nvm_valid)
mjr 35:e959ffba78fd 4960 {
mjr 35:e959ffba78fd 4961 // flash is valid - load it into the RAM copy of the structure
mjr 35:e959ffba78fd 4962 memcpy(&nvm, flash, sizeof(NVM));
mjr 35:e959ffba78fd 4963 }
mjr 35:e959ffba78fd 4964 else
mjr 35:e959ffba78fd 4965 {
mjr 76:7f5912b6340e 4966 // flash is invalid - load factory settings into RAM structure
mjr 35:e959ffba78fd 4967 cfg.setFactoryDefaults();
mjr 35:e959ffba78fd 4968 }
mjr 76:7f5912b6340e 4969
mjr 76:7f5912b6340e 4970 // tell the caller what happened
mjr 76:7f5912b6340e 4971 return nvm_valid;
mjr 35:e959ffba78fd 4972 }
mjr 35:e959ffba78fd 4973
mjr 86:e30a1f60f783 4974 // Save the config. Returns true on success, false on failure.
mjr 86:e30a1f60f783 4975 // 'tFollowup' is the follow-up time in seconds. If the write is
mjr 86:e30a1f60f783 4976 // successful, we'll turn on the success flag in the status reports
mjr 86:e30a1f60f783 4977 // and leave it on for this interval. If 'reboot' is true, we'll
mjr 86:e30a1f60f783 4978 // also schedule a reboot at the end of the followup interval.
mjr 86:e30a1f60f783 4979 bool saveConfigToFlash(int tFollowup, bool reboot)
mjr 33:d832bcab089e 4980 {
mjr 76:7f5912b6340e 4981 // get the config block location in the flash memory
mjr 77:0b96f6867312 4982 uint32_t addr = uint32_t(configFlashAddr());
mjr 79:682ae3171a08 4983
mjr 101:755f44622abc 4984 // save the data
mjr 101:755f44622abc 4985 bool ok = nvm.save(iap, addr);
mjr 101:755f44622abc 4986
mjr 101:755f44622abc 4987 // if the save succeeded, do post-save work
mjr 101:755f44622abc 4988 if (ok)
mjr 86:e30a1f60f783 4989 {
mjr 86:e30a1f60f783 4990 // success - report the successful save in the status flags
mjr 86:e30a1f60f783 4991 saveConfigSucceededFlag = 0x40;
mjr 86:e30a1f60f783 4992
mjr 86:e30a1f60f783 4993 // start the followup timer
mjr 87:8d35c74403af 4994 saveConfigFollowupTime = tFollowup;
mjr 87:8d35c74403af 4995 saveConfigFollowupTimer.reset();
mjr 86:e30a1f60f783 4996 saveConfigFollowupTimer.start();
mjr 86:e30a1f60f783 4997
mjr 86:e30a1f60f783 4998 // if a reboot is pending, flag it
mjr 86:e30a1f60f783 4999 saveConfigRebootPending = reboot;
mjr 86:e30a1f60f783 5000 }
mjr 101:755f44622abc 5001
mjr 101:755f44622abc 5002 // return the success indication
mjr 101:755f44622abc 5003 return ok;
mjr 76:7f5912b6340e 5004 }
mjr 76:7f5912b6340e 5005
mjr 76:7f5912b6340e 5006 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 5007 //
mjr 76:7f5912b6340e 5008 // Host-loaded configuration. The Flash NVM block above is designed to be
mjr 76:7f5912b6340e 5009 // stored from within the firmware; in contrast, the host-loaded config is
mjr 76:7f5912b6340e 5010 // stored by the host, by patching the firwmare binary (.bin) file before
mjr 76:7f5912b6340e 5011 // downloading it to the device.
mjr 76:7f5912b6340e 5012 //
mjr 100:1ff35c07217c 5013 // Ideally, we'd use the host-loaded memory for all configuration updates
mjr 100:1ff35c07217c 5014 // from the host - that is, any time the host wants to update config settings,
mjr 100:1ff35c07217c 5015 // such as via user input in the config tool. In the past, I wanted to do
mjr 100:1ff35c07217c 5016 // it this way because it seemed to be unreliable to write flash memory via
mjr 100:1ff35c07217c 5017 // the device. But that turned out to be due to a bug in the mbed Ticker
mjr 100:1ff35c07217c 5018 // code (of all things!), which we've fixed - since then, flash writing on
mjr 100:1ff35c07217c 5019 // the device has been bulletproof. Even so, doing host-to-device flash
mjr 100:1ff35c07217c 5020 // writing for config updates would be nice just for the sake of speed, as
mjr 100:1ff35c07217c 5021 // the alternative is that we send the variables one at a time by USB, which
mjr 100:1ff35c07217c 5022 // takes noticeable time when reprogramming the whole config set. But
mjr 100:1ff35c07217c 5023 // there's no way to accomplish a single-sector flash write via OpenSDA; you
mjr 100:1ff35c07217c 5024 // can only rewrite the entire flash memory as a unit.
mjr 100:1ff35c07217c 5025 //
mjr 100:1ff35c07217c 5026 // We can at least use this approach to do a fast configuration restore
mjr 100:1ff35c07217c 5027 // when downloading new firmware. In that case, we're rewriting all of
mjr 100:1ff35c07217c 5028 // flash memory anyway, so we might as well include the config data.
mjr 76:7f5912b6340e 5029 //
mjr 76:7f5912b6340e 5030 // The memory here is stored using the same format as the USB "Set Config
mjr 76:7f5912b6340e 5031 // Variable" command. These messages are 8 bytes long and start with a
mjr 76:7f5912b6340e 5032 // byte value 66, followed by the variable ID, followed by the variable
mjr 76:7f5912b6340e 5033 // value data in a format defined separately for each variable. To load
mjr 76:7f5912b6340e 5034 // the data, we'll start at the first byte after the signature, and
mjr 76:7f5912b6340e 5035 // interpret each 8-byte block as a type 66 message. If the first byte
mjr 76:7f5912b6340e 5036 // of a block is not 66, we'll take it as the end of the data.
mjr 76:7f5912b6340e 5037 //
mjr 76:7f5912b6340e 5038 // We provide a block of storage here big enough for 1,024 variables.
mjr 76:7f5912b6340e 5039 // The header consists of a 30-byte signature followed by two bytes giving
mjr 76:7f5912b6340e 5040 // the available space in the area, in this case 8192 == 0x0200. The
mjr 76:7f5912b6340e 5041 // length is little-endian. Note that the linker will implicitly zero
mjr 76:7f5912b6340e 5042 // the rest of the block, so if the host doesn't populate it, we'll see
mjr 76:7f5912b6340e 5043 // that it's empty by virtue of not containing the required '66' byte
mjr 76:7f5912b6340e 5044 // prefix for the first 8-byte variable block.
mjr 76:7f5912b6340e 5045 static const uint8_t hostLoadedConfig[8192+32]
mjr 76:7f5912b6340e 5046 __attribute__ ((aligned(SECTOR_SIZE))) =
mjr 76:7f5912b6340e 5047 "///Pinscape.HostLoadedConfig//\0\040"; // 30 byte signature + 2 byte length
mjr 76:7f5912b6340e 5048
mjr 76:7f5912b6340e 5049 // Get a pointer to the first byte of the configuration data
mjr 76:7f5912b6340e 5050 const uint8_t *getHostLoadedConfigData()
mjr 76:7f5912b6340e 5051 {
mjr 76:7f5912b6340e 5052 // the first configuration variable byte immediately follows the
mjr 76:7f5912b6340e 5053 // 32-byte signature header
mjr 76:7f5912b6340e 5054 return hostLoadedConfig + 32;
mjr 76:7f5912b6340e 5055 };
mjr 76:7f5912b6340e 5056
mjr 76:7f5912b6340e 5057 // forward reference to config var store function
mjr 76:7f5912b6340e 5058 void configVarSet(const uint8_t *);
mjr 76:7f5912b6340e 5059
mjr 76:7f5912b6340e 5060 // Load the host-loaded configuration data into the active (RAM)
mjr 76:7f5912b6340e 5061 // configuration object.
mjr 76:7f5912b6340e 5062 void loadHostLoadedConfig()
mjr 76:7f5912b6340e 5063 {
mjr 76:7f5912b6340e 5064 // Start at the first configuration variable. Each variable
mjr 76:7f5912b6340e 5065 // block is in the format of a Set Config Variable command in
mjr 76:7f5912b6340e 5066 // the USB protocol, so each block starts with a byte value of
mjr 76:7f5912b6340e 5067 // 66 and is 8 bytes long. Continue as long as we find valid
mjr 76:7f5912b6340e 5068 // variable blocks, or reach end end of the block.
mjr 76:7f5912b6340e 5069 const uint8_t *start = getHostLoadedConfigData();
mjr 76:7f5912b6340e 5070 const uint8_t *end = hostLoadedConfig + sizeof(hostLoadedConfig);
mjr 76:7f5912b6340e 5071 for (const uint8_t *p = getHostLoadedConfigData() ; start < end && *p == 66 ; p += 8)
mjr 76:7f5912b6340e 5072 {
mjr 76:7f5912b6340e 5073 // load this variable
mjr 76:7f5912b6340e 5074 configVarSet(p);
mjr 76:7f5912b6340e 5075 }
mjr 35:e959ffba78fd 5076 }
mjr 35:e959ffba78fd 5077
mjr 35:e959ffba78fd 5078 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5079 //
mjr 55:4db125cd11a0 5080 // Pixel dump mode - the host requested a dump of image sensor pixels
mjr 55:4db125cd11a0 5081 // (helpful for installing and setting up the sensor and light source)
mjr 55:4db125cd11a0 5082 //
mjr 55:4db125cd11a0 5083 bool reportPlungerStat = false;
mjr 55:4db125cd11a0 5084 uint8_t reportPlungerStatFlags; // plunger pixel report flag bits (see ccdSensor.h)
mjr 55:4db125cd11a0 5085 uint8_t reportPlungerStatTime; // extra exposure time for plunger pixel report
mjr 101:755f44622abc 5086 uint8_t tReportPlungerStat; // timestamp of most recent plunger status request
mjr 55:4db125cd11a0 5087
mjr 55:4db125cd11a0 5088
mjr 55:4db125cd11a0 5089 // ---------------------------------------------------------------------------
mjr 55:4db125cd11a0 5090 //
mjr 40:cc0d9814522b 5091 // Night mode setting updates
mjr 40:cc0d9814522b 5092 //
mjr 38:091e511ce8a0 5093
mjr 38:091e511ce8a0 5094 // Turn night mode on or off
mjr 38:091e511ce8a0 5095 static void setNightMode(bool on)
mjr 38:091e511ce8a0 5096 {
mjr 77:0b96f6867312 5097 // Set the new night mode flag in the noisy output class. Note
mjr 77:0b96f6867312 5098 // that we use the status report bit flag value 0x02 when on, so
mjr 77:0b96f6867312 5099 // that we can just '|' this into the overall status bits.
mjr 77:0b96f6867312 5100 nightMode = on ? 0x02 : 0x00;
mjr 55:4db125cd11a0 5101
mjr 40:cc0d9814522b 5102 // update the special output pin that shows the night mode state
mjr 53:9b2611964afc 5103 int port = int(cfg.nightMode.port) - 1;
mjr 53:9b2611964afc 5104 if (port >= 0 && port < numOutputs)
mjr 53:9b2611964afc 5105 lwPin[port]->set(nightMode ? 255 : 0);
mjr 76:7f5912b6340e 5106
mjr 76:7f5912b6340e 5107 // Reset all outputs at their current value, so that the underlying
mjr 76:7f5912b6340e 5108 // physical outputs get turned on or off as appropriate for the night
mjr 76:7f5912b6340e 5109 // mode change.
mjr 76:7f5912b6340e 5110 for (int i = 0 ; i < numOutputs ; ++i)
mjr 76:7f5912b6340e 5111 lwPin[i]->set(outLevel[i]);
mjr 76:7f5912b6340e 5112
mjr 76:7f5912b6340e 5113 // update 74HC595 outputs
mjr 76:7f5912b6340e 5114 if (hc595 != 0)
mjr 76:7f5912b6340e 5115 hc595->update();
mjr 38:091e511ce8a0 5116 }
mjr 38:091e511ce8a0 5117
mjr 38:091e511ce8a0 5118 // Toggle night mode
mjr 38:091e511ce8a0 5119 static void toggleNightMode()
mjr 38:091e511ce8a0 5120 {
mjr 53:9b2611964afc 5121 setNightMode(!nightMode);
mjr 38:091e511ce8a0 5122 }
mjr 38:091e511ce8a0 5123
mjr 38:091e511ce8a0 5124
mjr 38:091e511ce8a0 5125 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 5126 //
mjr 35:e959ffba78fd 5127 // Plunger Sensor
mjr 35:e959ffba78fd 5128 //
mjr 35:e959ffba78fd 5129
mjr 35:e959ffba78fd 5130 // the plunger sensor interface object
mjr 35:e959ffba78fd 5131 PlungerSensor *plungerSensor = 0;
mjr 35:e959ffba78fd 5132
mjr 76:7f5912b6340e 5133
mjr 35:e959ffba78fd 5134 // Create the plunger sensor based on the current configuration. If
mjr 35:e959ffba78fd 5135 // there's already a sensor object, we'll delete it.
mjr 35:e959ffba78fd 5136 void createPlunger()
mjr 35:e959ffba78fd 5137 {
mjr 35:e959ffba78fd 5138 // create the new sensor object according to the type
mjr 35:e959ffba78fd 5139 switch (cfg.plunger.sensorType)
mjr 35:e959ffba78fd 5140 {
mjr 82:4f6209cb5c33 5141 case PlungerType_TSL1410R:
mjr 82:4f6209cb5c33 5142 // TSL1410R, shadow edge detector
mjr 35:e959ffba78fd 5143 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 5144 plungerSensor = new PlungerSensorTSL1410R(
mjr 53:9b2611964afc 5145 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 5146 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5147 wirePinName(cfg.plunger.sensorPin[2]));
mjr 35:e959ffba78fd 5148 break;
mjr 35:e959ffba78fd 5149
mjr 82:4f6209cb5c33 5150 case PlungerType_TSL1412S:
mjr 82:4f6209cb5c33 5151 // TSL1412S, shadow edge detector
mjr 82:4f6209cb5c33 5152 // pins are: SI, CLOCK, AO
mjr 53:9b2611964afc 5153 plungerSensor = new PlungerSensorTSL1412R(
mjr 53:9b2611964afc 5154 wirePinName(cfg.plunger.sensorPin[0]),
mjr 53:9b2611964afc 5155 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5156 wirePinName(cfg.plunger.sensorPin[2]));
mjr 35:e959ffba78fd 5157 break;
mjr 35:e959ffba78fd 5158
mjr 35:e959ffba78fd 5159 case PlungerType_Pot:
mjr 82:4f6209cb5c33 5160 // Potentiometer (or any other sensor with a linear analog voltage
mjr 82:4f6209cb5c33 5161 // reading as the proxy for the position)
mjr 82:4f6209cb5c33 5162 // pins are: AO (analog in)
mjr 53:9b2611964afc 5163 plungerSensor = new PlungerSensorPot(
mjr 53:9b2611964afc 5164 wirePinName(cfg.plunger.sensorPin[0]));
mjr 35:e959ffba78fd 5165 break;
mjr 82:4f6209cb5c33 5166
mjr 82:4f6209cb5c33 5167 case PlungerType_OptQuad:
mjr 82:4f6209cb5c33 5168 // Optical quadrature sensor, AEDR8300-K or similar. The -K is
mjr 82:4f6209cb5c33 5169 // designed for a 75 LPI scale, which translates to 300 pulses/inch.
mjr 82:4f6209cb5c33 5170 // Pins are: CHA, CHB (quadrature pulse inputs).
mjr 82:4f6209cb5c33 5171 plungerSensor = new PlungerSensorQuad(
mjr 82:4f6209cb5c33 5172 300,
mjr 82:4f6209cb5c33 5173 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5174 wirePinName(cfg.plunger.sensorPin[1]));
mjr 82:4f6209cb5c33 5175 break;
mjr 82:4f6209cb5c33 5176
mjr 82:4f6209cb5c33 5177 case PlungerType_TSL1401CL:
mjr 82:4f6209cb5c33 5178 // TSL1401CL, absolute position encoder with bar code scale
mjr 82:4f6209cb5c33 5179 // pins are: SI, CLOCK, AO
mjr 82:4f6209cb5c33 5180 plungerSensor = new PlungerSensorTSL1401CL(
mjr 82:4f6209cb5c33 5181 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5182 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5183 wirePinName(cfg.plunger.sensorPin[2]));
mjr 82:4f6209cb5c33 5184 break;
mjr 82:4f6209cb5c33 5185
mjr 82:4f6209cb5c33 5186 case PlungerType_VL6180X:
mjr 82:4f6209cb5c33 5187 // VL6180X time-of-flight IR distance sensor
mjr 82:4f6209cb5c33 5188 // pins are: SDL, SCL, GPIO0/CE
mjr 82:4f6209cb5c33 5189 plungerSensor = new PlungerSensorVL6180X(
mjr 82:4f6209cb5c33 5190 wirePinName(cfg.plunger.sensorPin[0]),
mjr 82:4f6209cb5c33 5191 wirePinName(cfg.plunger.sensorPin[1]),
mjr 82:4f6209cb5c33 5192 wirePinName(cfg.plunger.sensorPin[2]));
mjr 82:4f6209cb5c33 5193 break;
mjr 82:4f6209cb5c33 5194
mjr 100:1ff35c07217c 5195 case PlungerType_AEAT6012:
mjr 100:1ff35c07217c 5196 // Broadcom AEAT-6012-A06 magnetic rotary encoder
mjr 100:1ff35c07217c 5197 // pins are: CS (chip select, dig out), CLK (dig out), DO (data, dig in)
mjr 100:1ff35c07217c 5198 plungerSensor = new PlungerSensorAEAT601X<12>(
mjr 100:1ff35c07217c 5199 wirePinName(cfg.plunger.sensorPin[0]),
mjr 100:1ff35c07217c 5200 wirePinName(cfg.plunger.sensorPin[1]),
mjr 100:1ff35c07217c 5201 wirePinName(cfg.plunger.sensorPin[2]));
mjr 100:1ff35c07217c 5202 break;
mjr 100:1ff35c07217c 5203
mjr 100:1ff35c07217c 5204 case PlungerType_TCD1103:
mjr 100:1ff35c07217c 5205 // Toshiba TCD1103GFG linear CCD, optical edge detection, with
mjr 100:1ff35c07217c 5206 // inverted logic gates.
mjr 100:1ff35c07217c 5207 //
mjr 100:1ff35c07217c 5208 // Pins are: fM (master clock, PWM), OS (sample data, analog in),
mjr 100:1ff35c07217c 5209 // ICG (integration clear gate, dig out), SH (shift gate, dig out)
mjr 100:1ff35c07217c 5210 plungerSensor = new PlungerSensorTCD1103<true>(
mjr 100:1ff35c07217c 5211 wirePinName(cfg.plunger.sensorPin[0]),
mjr 100:1ff35c07217c 5212 wirePinName(cfg.plunger.sensorPin[1]),
mjr 100:1ff35c07217c 5213 wirePinName(cfg.plunger.sensorPin[2]),
mjr 100:1ff35c07217c 5214 wirePinName(cfg.plunger.sensorPin[3]));
mjr 100:1ff35c07217c 5215 break;
mjr 100:1ff35c07217c 5216
mjr 35:e959ffba78fd 5217 case PlungerType_None:
mjr 35:e959ffba78fd 5218 default:
mjr 35:e959ffba78fd 5219 plungerSensor = new PlungerSensorNull();
mjr 35:e959ffba78fd 5220 break;
mjr 35:e959ffba78fd 5221 }
mjr 100:1ff35c07217c 5222
mjr 100:1ff35c07217c 5223 // initialize the plunger from the saved configuration
mjr 100:1ff35c07217c 5224 plungerSensor->restoreCalibration(cfg);
mjr 86:e30a1f60f783 5225
mjr 87:8d35c74403af 5226 // initialize the config variables affecting the plunger
mjr 87:8d35c74403af 5227 plungerSensor->onConfigChange(19, cfg);
mjr 87:8d35c74403af 5228 plungerSensor->onConfigChange(20, cfg);
mjr 33:d832bcab089e 5229 }
mjr 33:d832bcab089e 5230
mjr 52:8298b2a73eb2 5231 // Global plunger calibration mode flag
mjr 52:8298b2a73eb2 5232 bool plungerCalMode;
mjr 52:8298b2a73eb2 5233
mjr 48:058ace2aed1d 5234 // Plunger reader
mjr 51:57eb311faafa 5235 //
mjr 51:57eb311faafa 5236 // This class encapsulates our plunger data processing. At the simplest
mjr 51:57eb311faafa 5237 // level, we read the position from the sensor, adjust it for the
mjr 51:57eb311faafa 5238 // calibration settings, and report the calibrated position to the host.
mjr 51:57eb311faafa 5239 //
mjr 51:57eb311faafa 5240 // In addition, we constantly monitor the data for "firing" motions.
mjr 51:57eb311faafa 5241 // A firing motion is when the user pulls back the plunger and releases
mjr 51:57eb311faafa 5242 // it, allowing it to shoot forward under the force of the main spring.
mjr 51:57eb311faafa 5243 // When we detect that this is happening, we briefly stop reporting the
mjr 51:57eb311faafa 5244 // real physical position that we're reading from the sensor, and instead
mjr 51:57eb311faafa 5245 // report a synthetic series of positions that depicts an idealized
mjr 51:57eb311faafa 5246 // firing motion.
mjr 51:57eb311faafa 5247 //
mjr 51:57eb311faafa 5248 // The point of the synthetic reports is to correct for distortions
mjr 51:57eb311faafa 5249 // created by the joystick interface conventions used by VP and other
mjr 51:57eb311faafa 5250 // PC pinball emulators. The convention they use is simply to have the
mjr 51:57eb311faafa 5251 // plunger device report the instantaneous position of the real plunger.
mjr 51:57eb311faafa 5252 // The PC software polls this reported position periodically, and moves
mjr 51:57eb311faafa 5253 // the on-screen virtual plunger in sync with the real plunger. This
mjr 51:57eb311faafa 5254 // works fine for human-scale motion when the user is manually moving
mjr 51:57eb311faafa 5255 // the plunger. But it doesn't work for the high speed motion of a
mjr 51:57eb311faafa 5256 // release. The plunger simply moves too fast. VP polls in about 10ms
mjr 51:57eb311faafa 5257 // intervals; the plunger takes about 50ms to travel from fully
mjr 51:57eb311faafa 5258 // retracted to the park position when released. The low sampling
mjr 51:57eb311faafa 5259 // rate relative to the rate of change of the sampled data creates
mjr 51:57eb311faafa 5260 // a classic digital aliasing effect.
mjr 51:57eb311faafa 5261 //
mjr 51:57eb311faafa 5262 // The synthetic reporting scheme compensates for the interface
mjr 51:57eb311faafa 5263 // distortions by essentially changing to a coarse enough timescale
mjr 51:57eb311faafa 5264 // that VP can reliably interpret the readings. Conceptually, there
mjr 51:57eb311faafa 5265 // are three steps involved in doing this. First, we analyze the
mjr 51:57eb311faafa 5266 // actual sensor data to detect and characterize the release motion.
mjr 51:57eb311faafa 5267 // Second, once we think we have a release in progress, we fit the
mjr 51:57eb311faafa 5268 // data to a mathematical model of the release. The model we use is
mjr 51:57eb311faafa 5269 // dead simple: we consider the release to have one parameter, namely
mjr 51:57eb311faafa 5270 // the retraction distance at the moment the user lets go. This is an
mjr 51:57eb311faafa 5271 // excellent proxy in the real physical system for the final speed
mjr 51:57eb311faafa 5272 // when the plunger hits the ball, and it also happens to match how
mjr 51:57eb311faafa 5273 // VP models it internally. Third, we construct synthetic reports
mjr 51:57eb311faafa 5274 // that will make VP's internal state match our model. This is also
mjr 51:57eb311faafa 5275 // pretty simple: we just need to send VP the maximum retraction
mjr 51:57eb311faafa 5276 // distance for long enough to be sure that it polls it at least
mjr 51:57eb311faafa 5277 // once, and then send it the park position for long enough to
mjr 51:57eb311faafa 5278 // ensure that VP will complete the same firing motion. The
mjr 51:57eb311faafa 5279 // immediate jump from the maximum point to the zero point will
mjr 51:57eb311faafa 5280 // cause VP to move its simulation model plunger forward from the
mjr 51:57eb311faafa 5281 // starting point at its natural spring acceleration rate, which
mjr 51:57eb311faafa 5282 // is exactly what the real plunger just did.
mjr 51:57eb311faafa 5283 //
mjr 48:058ace2aed1d 5284 class PlungerReader
mjr 48:058ace2aed1d 5285 {
mjr 48:058ace2aed1d 5286 public:
mjr 48:058ace2aed1d 5287 PlungerReader()
mjr 48:058ace2aed1d 5288 {
mjr 48:058ace2aed1d 5289 // not in a firing event yet
mjr 48:058ace2aed1d 5290 firing = 0;
mjr 48:058ace2aed1d 5291 }
mjr 76:7f5912b6340e 5292
mjr 48:058ace2aed1d 5293 // Collect a reading from the plunger sensor. The main loop calls
mjr 48:058ace2aed1d 5294 // this frequently to read the current raw position data from the
mjr 48:058ace2aed1d 5295 // sensor. We analyze the raw data to produce the calibrated
mjr 48:058ace2aed1d 5296 // position that we report to the PC via the joystick interface.
mjr 48:058ace2aed1d 5297 void read()
mjr 48:058ace2aed1d 5298 {
mjr 76:7f5912b6340e 5299 // if the sensor is busy, skip the reading on this round
mjr 76:7f5912b6340e 5300 if (!plungerSensor->ready())
mjr 76:7f5912b6340e 5301 return;
mjr 76:7f5912b6340e 5302
mjr 48:058ace2aed1d 5303 // Read a sample from the sensor
mjr 48:058ace2aed1d 5304 PlungerReading r;
mjr 48:058ace2aed1d 5305 if (plungerSensor->read(r))
mjr 48:058ace2aed1d 5306 {
mjr 53:9b2611964afc 5307 // check for calibration mode
mjr 53:9b2611964afc 5308 if (plungerCalMode)
mjr 53:9b2611964afc 5309 {
mjr 53:9b2611964afc 5310 // Calibration mode. Adjust the calibration bounds to fit
mjr 53:9b2611964afc 5311 // the value. If this value is beyond the current min or max,
mjr 53:9b2611964afc 5312 // expand the envelope to include this new value.
mjr 53:9b2611964afc 5313 if (r.pos > cfg.plunger.cal.max)
mjr 53:9b2611964afc 5314 cfg.plunger.cal.max = r.pos;
mjr 53:9b2611964afc 5315 if (r.pos < cfg.plunger.cal.min)
mjr 53:9b2611964afc 5316 cfg.plunger.cal.min = r.pos;
mjr 76:7f5912b6340e 5317
mjr 76:7f5912b6340e 5318 // update our cached calibration data
mjr 76:7f5912b6340e 5319 onUpdateCal();
mjr 50:40015764bbe6 5320
mjr 53:9b2611964afc 5321 // If we're in calibration state 0, we're waiting for the
mjr 53:9b2611964afc 5322 // plunger to come to rest at the park position so that we
mjr 53:9b2611964afc 5323 // can take a sample of the park position. Check to see if
mjr 53:9b2611964afc 5324 // we've been at rest for a minimum interval.
mjr 53:9b2611964afc 5325 if (calState == 0)
mjr 53:9b2611964afc 5326 {
mjr 53:9b2611964afc 5327 if (abs(r.pos - calZeroStart.pos) < 65535/3/50)
mjr 53:9b2611964afc 5328 {
mjr 53:9b2611964afc 5329 // we're close enough - make sure we've been here long enough
mjr 53:9b2611964afc 5330 if (uint32_t(r.t - calZeroStart.t) > 100000UL)
mjr 53:9b2611964afc 5331 {
mjr 53:9b2611964afc 5332 // we've been at rest long enough - count it
mjr 53:9b2611964afc 5333 calZeroPosSum += r.pos;
mjr 53:9b2611964afc 5334 calZeroPosN += 1;
mjr 53:9b2611964afc 5335
mjr 53:9b2611964afc 5336 // update the zero position from the new average
mjr 53:9b2611964afc 5337 cfg.plunger.cal.zero = uint16_t(calZeroPosSum / calZeroPosN);
mjr 76:7f5912b6340e 5338 onUpdateCal();
mjr 53:9b2611964afc 5339
mjr 53:9b2611964afc 5340 // switch to calibration state 1 - at rest
mjr 53:9b2611964afc 5341 calState = 1;
mjr 53:9b2611964afc 5342 }
mjr 53:9b2611964afc 5343 }
mjr 53:9b2611964afc 5344 else
mjr 53:9b2611964afc 5345 {
mjr 53:9b2611964afc 5346 // we're not close to the last position - start again here
mjr 53:9b2611964afc 5347 calZeroStart = r;
mjr 53:9b2611964afc 5348 }
mjr 53:9b2611964afc 5349 }
mjr 53:9b2611964afc 5350
mjr 53:9b2611964afc 5351 // Rescale to the joystick range, and adjust for the current
mjr 53:9b2611964afc 5352 // park position, but don't calibrate. We don't know the maximum
mjr 53:9b2611964afc 5353 // point yet, so we can't calibrate the range.
mjr 53:9b2611964afc 5354 r.pos = int(
mjr 53:9b2611964afc 5355 (long(r.pos - cfg.plunger.cal.zero) * JOYMAX)
mjr 53:9b2611964afc 5356 / (65535 - cfg.plunger.cal.zero));
mjr 53:9b2611964afc 5357 }
mjr 53:9b2611964afc 5358 else
mjr 53:9b2611964afc 5359 {
mjr 53:9b2611964afc 5360 // Not in calibration mode. Apply the existing calibration and
mjr 53:9b2611964afc 5361 // rescale to the joystick range.
mjr 76:7f5912b6340e 5362 r.pos = applyCal(r.pos);
mjr 53:9b2611964afc 5363
mjr 53:9b2611964afc 5364 // limit the result to the valid joystick range
mjr 53:9b2611964afc 5365 if (r.pos > JOYMAX)
mjr 53:9b2611964afc 5366 r.pos = JOYMAX;
mjr 53:9b2611964afc 5367 else if (r.pos < -JOYMAX)
mjr 53:9b2611964afc 5368 r.pos = -JOYMAX;
mjr 53:9b2611964afc 5369 }
mjr 50:40015764bbe6 5370
mjr 87:8d35c74403af 5371 // Look for a firing event - the user releasing the plunger and
mjr 87:8d35c74403af 5372 // allowing it to shoot forward at full speed. Wait at least 5ms
mjr 87:8d35c74403af 5373 // between samples for this, to help distinguish random motion
mjr 87:8d35c74403af 5374 // from the rapid motion of a firing event.
mjr 50:40015764bbe6 5375 //
mjr 87:8d35c74403af 5376 // There's a trade-off in the choice of minimum sampling interval.
mjr 87:8d35c74403af 5377 // The longer we wait, the more certain we can be of the trend.
mjr 87:8d35c74403af 5378 // But if we wait too long, the user will perceive a delay. We
mjr 87:8d35c74403af 5379 // also want to sample frequently enough to see the release motion
mjr 87:8d35c74403af 5380 // at intermediate steps along the way, so the sampling has to be
mjr 87:8d35c74403af 5381 // considerably faster than the whole travel time, which is about
mjr 87:8d35c74403af 5382 // 25-50ms.
mjr 87:8d35c74403af 5383 if (uint32_t(r.t - prv.t) < 5000UL)
mjr 87:8d35c74403af 5384 return;
mjr 87:8d35c74403af 5385
mjr 87:8d35c74403af 5386 // assume that we'll report this reading as-is
mjr 87:8d35c74403af 5387 z = r.pos;
mjr 87:8d35c74403af 5388
mjr 87:8d35c74403af 5389 // Firing event detection.
mjr 87:8d35c74403af 5390 //
mjr 87:8d35c74403af 5391 // A "firing event" is when the player releases the plunger from
mjr 87:8d35c74403af 5392 // a retracted position, allowing it to shoot forward under the
mjr 87:8d35c74403af 5393 // spring tension.
mjr 50:40015764bbe6 5394 //
mjr 87:8d35c74403af 5395 // We monitor the plunger motion for these events, and when they
mjr 87:8d35c74403af 5396 // occur, we report an "idealized" version of the motion to the
mjr 87:8d35c74403af 5397 // PC. The idealized version consists of a series of readings
mjr 87:8d35c74403af 5398 // frozen at the fully retracted position for the whole duration
mjr 87:8d35c74403af 5399 // of the forward travel, followed by a series of readings at the
mjr 87:8d35c74403af 5400 // fully forward position for long enough for the plunger to come
mjr 87:8d35c74403af 5401 // mostly to rest. The series of frozen readings aren't meant to
mjr 87:8d35c74403af 5402 // be perceptible to the player - we try to keep them short enough
mjr 87:8d35c74403af 5403 // that they're not apparent as delay. Instead, they're for the
mjr 87:8d35c74403af 5404 // PC client software's benefit. PC joystick clients use polling,
mjr 87:8d35c74403af 5405 // so they only see an unpredictable subset of the readings we
mjr 87:8d35c74403af 5406 // send. The only way to be sure that the client sees a particular
mjr 87:8d35c74403af 5407 // reading is to hold it for long enough that the client is sure to
mjr 87:8d35c74403af 5408 // poll within the hold interval. In the case of the plunger
mjr 87:8d35c74403af 5409 // firing motion, it's important that the client sees the *ends*
mjr 87:8d35c74403af 5410 // of the travel - the fully retracted starting position in
mjr 87:8d35c74403af 5411 // particular. If the PC client only polls for a sample while the
mjr 87:8d35c74403af 5412 // plunger is somewhere in the middle of the travel, the PC will
mjr 87:8d35c74403af 5413 // think that the firing motion *started* in that middle position,
mjr 87:8d35c74403af 5414 // so it won't be able to model the right amount of momentum when
mjr 87:8d35c74403af 5415 // the plunger hits the ball. We try to ensure that the PC sees
mjr 87:8d35c74403af 5416 // the right starting point by reporting the starting point for
mjr 87:8d35c74403af 5417 // extra time during the forward motion. By the same token, we
mjr 87:8d35c74403af 5418 // want the PC to know that the plunger has moved all the way
mjr 87:8d35c74403af 5419 // forward, rather than mistakenly thinking that it stopped
mjr 87:8d35c74403af 5420 // somewhere in the middle of the travel, so we freeze at the
mjr 87:8d35c74403af 5421 // forward position for a short time.
mjr 76:7f5912b6340e 5422 //
mjr 87:8d35c74403af 5423 // To detect a firing event, we look for forward motion that's
mjr 87:8d35c74403af 5424 // fast enough to be a firing event. To determine how fast is
mjr 87:8d35c74403af 5425 // fast enough, we use a simple model of the plunger motion where
mjr 87:8d35c74403af 5426 // the acceleration is constant. This is only an approximation,
mjr 87:8d35c74403af 5427 // as the spring force actually varies with spring's compression,
mjr 87:8d35c74403af 5428 // but it's close enough for our purposes here.
mjr 87:8d35c74403af 5429 //
mjr 87:8d35c74403af 5430 // Do calculations in fixed-point 2^48 scale with 64-bit ints.
mjr 87:8d35c74403af 5431 // acc2 = acceleration/2 for 50ms release time, units of unit
mjr 87:8d35c74403af 5432 // distances per microsecond squared, where the unit distance
mjr 87:8d35c74403af 5433 // is the overall travel from the starting retracted position
mjr 87:8d35c74403af 5434 // to the park position.
mjr 87:8d35c74403af 5435 const int32_t acc2 = 112590; // 2^48 scale
mjr 50:40015764bbe6 5436 switch (firing)
mjr 50:40015764bbe6 5437 {
mjr 50:40015764bbe6 5438 case 0:
mjr 87:8d35c74403af 5439 // Not in firing mode. If we're retracted a bit, and the
mjr 87:8d35c74403af 5440 // motion is forward at a fast enough rate to look like a
mjr 87:8d35c74403af 5441 // release, enter firing mode.
mjr 87:8d35c74403af 5442 if (r.pos > JOYMAX/6)
mjr 50:40015764bbe6 5443 {
mjr 87:8d35c74403af 5444 const uint32_t dt = uint32_t(r.t - prv.t);
mjr 87:8d35c74403af 5445 const uint32_t dt2 = dt*dt; // dt^2
mjr 87:8d35c74403af 5446 if (r.pos < prv.pos - int((prv.pos*acc2*uint64_t(dt2)) >> 48))
mjr 87:8d35c74403af 5447 {
mjr 87:8d35c74403af 5448 // Tentatively enter firing mode. Use the prior reading
mjr 87:8d35c74403af 5449 // as the starting point, and freeze reports for now.
mjr 87:8d35c74403af 5450 firingMode(1);
mjr 87:8d35c74403af 5451 f0 = prv;
mjr 87:8d35c74403af 5452 z = f0.pos;
mjr 87:8d35c74403af 5453
mjr 87:8d35c74403af 5454 // if in calibration state 1 (at rest), switch to
mjr 87:8d35c74403af 5455 // state 2 (not at rest)
mjr 87:8d35c74403af 5456 if (calState == 1)
mjr 87:8d35c74403af 5457 calState = 2;
mjr 87:8d35c74403af 5458 }
mjr 50:40015764bbe6 5459 }
mjr 50:40015764bbe6 5460 break;
mjr 50:40015764bbe6 5461
mjr 50:40015764bbe6 5462 case 1:
mjr 87:8d35c74403af 5463 // Tentative firing mode: the plunger was moving forward
mjr 87:8d35c74403af 5464 // at last check. To stay in firing mode, the plunger has
mjr 87:8d35c74403af 5465 // to keep moving forward fast enough to look like it's
mjr 87:8d35c74403af 5466 // moving under spring force. To figure out how fast is
mjr 87:8d35c74403af 5467 // fast enough, we use a simple model where the acceleration
mjr 87:8d35c74403af 5468 // is constant over the whole travel distance and the total
mjr 87:8d35c74403af 5469 // travel time is 50ms. The acceleration actually varies
mjr 87:8d35c74403af 5470 // slightly since it comes from the spring force, which
mjr 87:8d35c74403af 5471 // is linear in the displacement; but the plunger spring is
mjr 87:8d35c74403af 5472 // fairly compressed even when the plunger is all the way
mjr 87:8d35c74403af 5473 // forward, so the difference in tension from one end of
mjr 87:8d35c74403af 5474 // the travel to the other is fairly small, so it's not too
mjr 87:8d35c74403af 5475 // far off to model it as constant. And the real travel
mjr 87:8d35c74403af 5476 // time obviously isn't a constant, but all we need for
mjr 87:8d35c74403af 5477 // that is an upper bound. So: we'll figure the time since
mjr 87:8d35c74403af 5478 // we entered firing mode, and figure the distance we should
mjr 87:8d35c74403af 5479 // have traveled to complete the trip within the maximum
mjr 87:8d35c74403af 5480 // time allowed. If we've moved far enough, we'll stay
mjr 87:8d35c74403af 5481 // in firing mode; if not, we'll exit firing mode. And if
mjr 87:8d35c74403af 5482 // we cross the finish line while still in firing mode,
mjr 87:8d35c74403af 5483 // we'll switch to the next phase of the firing event.
mjr 50:40015764bbe6 5484 if (r.pos <= 0)
mjr 50:40015764bbe6 5485 {
mjr 87:8d35c74403af 5486 // We crossed the park position. Switch to the second
mjr 87:8d35c74403af 5487 // phase of the firing event, where we hold the reported
mjr 87:8d35c74403af 5488 // position at the "bounce" position (where the plunger
mjr 87:8d35c74403af 5489 // is all the way forward, compressing the barrel spring).
mjr 87:8d35c74403af 5490 // We'll stick here long enough to ensure that the PC
mjr 87:8d35c74403af 5491 // client (Visual Pinball or whatever) sees the reading
mjr 87:8d35c74403af 5492 // and processes the release motion via the simulated
mjr 87:8d35c74403af 5493 // physics.
mjr 50:40015764bbe6 5494 firingMode(2);
mjr 53:9b2611964afc 5495
mjr 53:9b2611964afc 5496 // if in calibration mode, and we're in state 2 (moving),
mjr 53:9b2611964afc 5497 // collect firing statistics for calibration purposes
mjr 53:9b2611964afc 5498 if (plungerCalMode && calState == 2)
mjr 53:9b2611964afc 5499 {
mjr 53:9b2611964afc 5500 // collect a new zero point for the average when we
mjr 53:9b2611964afc 5501 // come to rest
mjr 53:9b2611964afc 5502 calState = 0;
mjr 53:9b2611964afc 5503
mjr 87:8d35c74403af 5504 // collect average firing time statistics in millseconds,
mjr 87:8d35c74403af 5505 // if it's in range (20 to 255 ms)
mjr 87:8d35c74403af 5506 const int dt = uint32_t(r.t - f0.t)/1000UL;
mjr 87:8d35c74403af 5507 if (dt >= 15 && dt <= 255)
mjr 53:9b2611964afc 5508 {
mjr 53:9b2611964afc 5509 calRlsTimeSum += dt;
mjr 53:9b2611964afc 5510 calRlsTimeN += 1;
mjr 53:9b2611964afc 5511 cfg.plunger.cal.tRelease = uint8_t(calRlsTimeSum / calRlsTimeN);
mjr 53:9b2611964afc 5512 }
mjr 53:9b2611964afc 5513 }
mjr 87:8d35c74403af 5514
mjr 87:8d35c74403af 5515 // Figure the "bounce" position as forward of the park
mjr 87:8d35c74403af 5516 // position by 1/6 of the starting retraction distance.
mjr 87:8d35c74403af 5517 // This simulates the momentum of the plunger compressing
mjr 87:8d35c74403af 5518 // the barrel spring on the rebound. The barrel spring
mjr 87:8d35c74403af 5519 // can compress by about 1/6 of the maximum retraction
mjr 87:8d35c74403af 5520 // distance, so we'll simply treat its compression as
mjr 87:8d35c74403af 5521 // proportional to the retraction. (It might be more
mjr 87:8d35c74403af 5522 // realistic to use a slightly higher value here, maybe
mjr 87:8d35c74403af 5523 // 1/4 or 1/3 or the retraction distance, capping it at
mjr 87:8d35c74403af 5524 // a maximum of 1/6, because the real plunger probably
mjr 87:8d35c74403af 5525 // compresses the barrel spring by 100% with less than
mjr 87:8d35c74403af 5526 // 100% retraction. But that won't affect the physics
mjr 87:8d35c74403af 5527 // meaningfully, just the animation, and the effect is
mjr 87:8d35c74403af 5528 // small in any case.)
mjr 87:8d35c74403af 5529 z = f0.pos = -f0.pos / 6;
mjr 87:8d35c74403af 5530
mjr 87:8d35c74403af 5531 // reset the starting time for this phase
mjr 87:8d35c74403af 5532 f0.t = r.t;
mjr 50:40015764bbe6 5533 }
mjr 50:40015764bbe6 5534 else
mjr 50:40015764bbe6 5535 {
mjr 87:8d35c74403af 5536 // check for motion since the start of the firing event
mjr 87:8d35c74403af 5537 const uint32_t dt = uint32_t(r.t - f0.t);
mjr 87:8d35c74403af 5538 const uint32_t dt2 = dt*dt; // dt^2
mjr 87:8d35c74403af 5539 if (dt < 50000
mjr 87:8d35c74403af 5540 && r.pos < f0.pos - int((f0.pos*acc2*uint64_t(dt2)) >> 48))
mjr 87:8d35c74403af 5541 {
mjr 87:8d35c74403af 5542 // It's moving fast enough to still be in a release
mjr 87:8d35c74403af 5543 // motion. Continue reporting the start position, and
mjr 87:8d35c74403af 5544 // stay in the first release phase.
mjr 87:8d35c74403af 5545 z = f0.pos;
mjr 87:8d35c74403af 5546 }
mjr 87:8d35c74403af 5547 else
mjr 87:8d35c74403af 5548 {
mjr 87:8d35c74403af 5549 // It's not moving fast enough to be a release
mjr 87:8d35c74403af 5550 // motion. Return to the default state.
mjr 87:8d35c74403af 5551 firingMode(0);
mjr 87:8d35c74403af 5552 calState = 1;
mjr 87:8d35c74403af 5553 }
mjr 50:40015764bbe6 5554 }
mjr 50:40015764bbe6 5555 break;
mjr 50:40015764bbe6 5556
mjr 50:40015764bbe6 5557 case 2:
mjr 87:8d35c74403af 5558 // Firing mode, holding at forward compression position.
mjr 87:8d35c74403af 5559 // Hold here for 25ms.
mjr 87:8d35c74403af 5560 if (uint32_t(r.t - f0.t) < 25000)
mjr 50:40015764bbe6 5561 {
mjr 87:8d35c74403af 5562 // stay here for now
mjr 87:8d35c74403af 5563 z = f0.pos;
mjr 50:40015764bbe6 5564 }
mjr 50:40015764bbe6 5565 else
mjr 50:40015764bbe6 5566 {
mjr 87:8d35c74403af 5567 // advance to the next phase, where we report the park
mjr 87:8d35c74403af 5568 // position until the plunger comes to rest
mjr 50:40015764bbe6 5569 firingMode(3);
mjr 50:40015764bbe6 5570 z = 0;
mjr 87:8d35c74403af 5571
mjr 87:8d35c74403af 5572 // remember when we started
mjr 87:8d35c74403af 5573 f0.t = r.t;
mjr 50:40015764bbe6 5574 }
mjr 50:40015764bbe6 5575 break;
mjr 50:40015764bbe6 5576
mjr 50:40015764bbe6 5577 case 3:
mjr 87:8d35c74403af 5578 // Firing event, holding at park position. Stay here for
mjr 87:8d35c74403af 5579 // a few moments so that the PC client can simulate the
mjr 87:8d35c74403af 5580 // full release motion, then return to real readings.
mjr 87:8d35c74403af 5581 if (uint32_t(r.t - f0.t) < 250000)
mjr 50:40015764bbe6 5582 {
mjr 87:8d35c74403af 5583 // stay here a while longer
mjr 87:8d35c74403af 5584 z = 0;
mjr 50:40015764bbe6 5585 }
mjr 50:40015764bbe6 5586 else
mjr 50:40015764bbe6 5587 {
mjr 87:8d35c74403af 5588 // it's been long enough - return to normal mode
mjr 87:8d35c74403af 5589 firingMode(0);
mjr 50:40015764bbe6 5590 }
mjr 50:40015764bbe6 5591 break;
mjr 50:40015764bbe6 5592 }
mjr 50:40015764bbe6 5593
mjr 82:4f6209cb5c33 5594 // Check for auto-zeroing, if enabled
mjr 82:4f6209cb5c33 5595 if ((cfg.plunger.autoZero.flags & PlungerAutoZeroEnabled) != 0)
mjr 82:4f6209cb5c33 5596 {
mjr 82:4f6209cb5c33 5597 // If we moved since the last reading, reset and restart the
mjr 82:4f6209cb5c33 5598 // auto-zero timer. Otherwise, if the timer has reached the
mjr 82:4f6209cb5c33 5599 // auto-zero timeout, it means we've been motionless for that
mjr 82:4f6209cb5c33 5600 // long, so auto-zero now.
mjr 82:4f6209cb5c33 5601 if (r.pos != prv.pos)
mjr 82:4f6209cb5c33 5602 {
mjr 82:4f6209cb5c33 5603 // movement detected - reset the timer
mjr 82:4f6209cb5c33 5604 autoZeroTimer.reset();
mjr 82:4f6209cb5c33 5605 autoZeroTimer.start();
mjr 82:4f6209cb5c33 5606 }
mjr 82:4f6209cb5c33 5607 else if (autoZeroTimer.read_us() > cfg.plunger.autoZero.t * 1000000UL)
mjr 82:4f6209cb5c33 5608 {
mjr 82:4f6209cb5c33 5609 // auto-zero now
mjr 82:4f6209cb5c33 5610 plungerSensor->autoZero();
mjr 82:4f6209cb5c33 5611
mjr 82:4f6209cb5c33 5612 // stop the timer so that we don't keep repeating this
mjr 82:4f6209cb5c33 5613 // if the plunger stays still for a long time
mjr 82:4f6209cb5c33 5614 autoZeroTimer.stop();
mjr 82:4f6209cb5c33 5615 autoZeroTimer.reset();
mjr 82:4f6209cb5c33 5616 }
mjr 82:4f6209cb5c33 5617 }
mjr 82:4f6209cb5c33 5618
mjr 87:8d35c74403af 5619 // this new reading becomes the previous reading for next time
mjr 87:8d35c74403af 5620 prv = r;
mjr 48:058ace2aed1d 5621 }
mjr 48:058ace2aed1d 5622 }
mjr 48:058ace2aed1d 5623
mjr 48:058ace2aed1d 5624 // Get the current value to report through the joystick interface
mjr 58:523fdcffbe6d 5625 int16_t getPosition()
mjr 58:523fdcffbe6d 5626 {
mjr 86:e30a1f60f783 5627 // return the last reading
mjr 86:e30a1f60f783 5628 return z;
mjr 55:4db125cd11a0 5629 }
mjr 58:523fdcffbe6d 5630
mjr 48:058ace2aed1d 5631 // Set calibration mode on or off
mjr 52:8298b2a73eb2 5632 void setCalMode(bool f)
mjr 48:058ace2aed1d 5633 {
mjr 52:8298b2a73eb2 5634 // check to see if we're entering calibration mode
mjr 52:8298b2a73eb2 5635 if (f && !plungerCalMode)
mjr 52:8298b2a73eb2 5636 {
mjr 52:8298b2a73eb2 5637 // reset the calibration in the configuration
mjr 48:058ace2aed1d 5638 cfg.plunger.cal.begin();
mjr 52:8298b2a73eb2 5639
mjr 52:8298b2a73eb2 5640 // start in state 0 (waiting to settle)
mjr 52:8298b2a73eb2 5641 calState = 0;
mjr 52:8298b2a73eb2 5642 calZeroPosSum = 0;
mjr 52:8298b2a73eb2 5643 calZeroPosN = 0;
mjr 52:8298b2a73eb2 5644 calRlsTimeSum = 0;
mjr 52:8298b2a73eb2 5645 calRlsTimeN = 0;
mjr 52:8298b2a73eb2 5646
mjr 82:4f6209cb5c33 5647 // tell the plunger we're starting calibration
mjr 100:1ff35c07217c 5648 plungerSensor->beginCalibration(cfg);
mjr 82:4f6209cb5c33 5649
mjr 52:8298b2a73eb2 5650 // set the initial zero point to the current position
mjr 52:8298b2a73eb2 5651 PlungerReading r;
mjr 52:8298b2a73eb2 5652 if (plungerSensor->read(r))
mjr 52:8298b2a73eb2 5653 {
mjr 52:8298b2a73eb2 5654 // got a reading - use it as the initial zero point
mjr 52:8298b2a73eb2 5655 cfg.plunger.cal.zero = r.pos;
mjr 76:7f5912b6340e 5656 onUpdateCal();
mjr 52:8298b2a73eb2 5657
mjr 52:8298b2a73eb2 5658 // use it as the starting point for the settling watch
mjr 53:9b2611964afc 5659 calZeroStart = r;
mjr 52:8298b2a73eb2 5660 }
mjr 52:8298b2a73eb2 5661 else
mjr 52:8298b2a73eb2 5662 {
mjr 52:8298b2a73eb2 5663 // no reading available - use the default 1/6 position
mjr 52:8298b2a73eb2 5664 cfg.plunger.cal.zero = 0xffff/6;
mjr 76:7f5912b6340e 5665 onUpdateCal();
mjr 52:8298b2a73eb2 5666
mjr 52:8298b2a73eb2 5667 // we don't have a starting point for the setting watch
mjr 53:9b2611964afc 5668 calZeroStart.pos = -65535;
mjr 53:9b2611964afc 5669 calZeroStart.t = 0;
mjr 53:9b2611964afc 5670 }
mjr 53:9b2611964afc 5671 }
mjr 53:9b2611964afc 5672 else if (!f && plungerCalMode)
mjr 53:9b2611964afc 5673 {
mjr 53:9b2611964afc 5674 // Leaving calibration mode. Make sure the max is past the
mjr 53:9b2611964afc 5675 // zero point - if it's not, we'd have a zero or negative
mjr 53:9b2611964afc 5676 // denominator for the scaling calculation, which would be
mjr 53:9b2611964afc 5677 // physically meaningless.
mjr 53:9b2611964afc 5678 if (cfg.plunger.cal.max <= cfg.plunger.cal.zero)
mjr 53:9b2611964afc 5679 {
mjr 53:9b2611964afc 5680 // bad settings - reset to defaults
mjr 53:9b2611964afc 5681 cfg.plunger.cal.max = 0xffff;
mjr 53:9b2611964afc 5682 cfg.plunger.cal.zero = 0xffff/6;
mjr 52:8298b2a73eb2 5683 }
mjr 100:1ff35c07217c 5684
mjr 100:1ff35c07217c 5685 // finalize the configuration in the plunger object
mjr 100:1ff35c07217c 5686 plungerSensor->endCalibration(cfg);
mjr 100:1ff35c07217c 5687
mjr 100:1ff35c07217c 5688 // update our internal cached information for the new calibration
mjr 100:1ff35c07217c 5689 onUpdateCal();
mjr 52:8298b2a73eb2 5690 }
mjr 52:8298b2a73eb2 5691
mjr 48:058ace2aed1d 5692 // remember the new mode
mjr 52:8298b2a73eb2 5693 plungerCalMode = f;
mjr 48:058ace2aed1d 5694 }
mjr 48:058ace2aed1d 5695
mjr 76:7f5912b6340e 5696 // Cached inverse of the calibration range. This is for calculating
mjr 76:7f5912b6340e 5697 // the calibrated plunger position given a raw sensor reading. The
mjr 76:7f5912b6340e 5698 // cached inverse is calculated as
mjr 76:7f5912b6340e 5699 //
mjr 76:7f5912b6340e 5700 // 64K * JOYMAX / (cfg.plunger.cal.max - cfg.plunger.cal.zero)
mjr 76:7f5912b6340e 5701 //
mjr 76:7f5912b6340e 5702 // To convert a raw sensor reading to a calibrated position, calculate
mjr 76:7f5912b6340e 5703 //
mjr 76:7f5912b6340e 5704 // ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16
mjr 76:7f5912b6340e 5705 //
mjr 76:7f5912b6340e 5706 // That yields the calibration result without performing a division.
mjr 76:7f5912b6340e 5707 int invCalRange;
mjr 76:7f5912b6340e 5708
mjr 76:7f5912b6340e 5709 // apply the calibration range to a reading
mjr 76:7f5912b6340e 5710 inline int applyCal(int reading)
mjr 76:7f5912b6340e 5711 {
mjr 76:7f5912b6340e 5712 return ((reading - cfg.plunger.cal.zero)*invCalRange) >> 16;
mjr 76:7f5912b6340e 5713 }
mjr 76:7f5912b6340e 5714
mjr 76:7f5912b6340e 5715 void onUpdateCal()
mjr 76:7f5912b6340e 5716 {
mjr 76:7f5912b6340e 5717 invCalRange = (JOYMAX << 16)/(cfg.plunger.cal.max - cfg.plunger.cal.zero);
mjr 76:7f5912b6340e 5718 }
mjr 76:7f5912b6340e 5719
mjr 48:058ace2aed1d 5720 // is a firing event in progress?
mjr 53:9b2611964afc 5721 bool isFiring() { return firing == 3; }
mjr 76:7f5912b6340e 5722
mjr 48:058ace2aed1d 5723 private:
mjr 87:8d35c74403af 5724 // current reported joystick reading
mjr 87:8d35c74403af 5725 int z;
mjr 87:8d35c74403af 5726
mjr 87:8d35c74403af 5727 // previous reading
mjr 87:8d35c74403af 5728 PlungerReading prv;
mjr 87:8d35c74403af 5729
mjr 52:8298b2a73eb2 5730 // Calibration state. During calibration mode, we watch for release
mjr 52:8298b2a73eb2 5731 // events, to measure the time it takes to complete the release
mjr 52:8298b2a73eb2 5732 // motion; and we watch for the plunger to come to reset after a
mjr 52:8298b2a73eb2 5733 // release, to gather statistics on the rest position.
mjr 52:8298b2a73eb2 5734 // 0 = waiting to settle
mjr 52:8298b2a73eb2 5735 // 1 = at rest
mjr 52:8298b2a73eb2 5736 // 2 = retracting
mjr 52:8298b2a73eb2 5737 // 3 = possibly releasing
mjr 52:8298b2a73eb2 5738 uint8_t calState;
mjr 52:8298b2a73eb2 5739
mjr 52:8298b2a73eb2 5740 // Calibration zero point statistics.
mjr 52:8298b2a73eb2 5741 // During calibration mode, we collect data on the rest position (the
mjr 52:8298b2a73eb2 5742 // zero point) by watching for the plunger to come to rest after each
mjr 52:8298b2a73eb2 5743 // release. We average these rest positions to get the calibrated
mjr 52:8298b2a73eb2 5744 // zero point. We use the average because the real physical plunger
mjr 52:8298b2a73eb2 5745 // itself doesn't come to rest at exactly the same spot every time,
mjr 52:8298b2a73eb2 5746 // largely due to friction in the mechanism. To calculate the average,
mjr 52:8298b2a73eb2 5747 // we keep a sum of the readings and a count of samples.
mjr 53:9b2611964afc 5748 PlungerReading calZeroStart;
mjr 52:8298b2a73eb2 5749 long calZeroPosSum;
mjr 52:8298b2a73eb2 5750 int calZeroPosN;
mjr 52:8298b2a73eb2 5751
mjr 52:8298b2a73eb2 5752 // Calibration release time statistics.
mjr 52:8298b2a73eb2 5753 // During calibration, we collect an average for the release time.
mjr 52:8298b2a73eb2 5754 long calRlsTimeSum;
mjr 52:8298b2a73eb2 5755 int calRlsTimeN;
mjr 52:8298b2a73eb2 5756
mjr 85:3c28aee81cde 5757 // Auto-zeroing timer
mjr 85:3c28aee81cde 5758 Timer autoZeroTimer;
mjr 85:3c28aee81cde 5759
mjr 48:058ace2aed1d 5760 // set a firing mode
mjr 48:058ace2aed1d 5761 inline void firingMode(int m)
mjr 48:058ace2aed1d 5762 {
mjr 48:058ace2aed1d 5763 firing = m;
mjr 48:058ace2aed1d 5764 }
mjr 48:058ace2aed1d 5765
mjr 48:058ace2aed1d 5766 // Firing event state.
mjr 48:058ace2aed1d 5767 //
mjr 87:8d35c74403af 5768 // 0 - Default state: not in firing event. We report the true
mjr 87:8d35c74403af 5769 // instantaneous plunger position to the joystick interface.
mjr 48:058ace2aed1d 5770 //
mjr 87:8d35c74403af 5771 // 1 - Moving forward at release speed
mjr 48:058ace2aed1d 5772 //
mjr 87:8d35c74403af 5773 // 2 - Firing - reporting the bounce position
mjr 87:8d35c74403af 5774 //
mjr 87:8d35c74403af 5775 // 3 - Firing - reporting the park position
mjr 48:058ace2aed1d 5776 //
mjr 48:058ace2aed1d 5777 int firing;
mjr 48:058ace2aed1d 5778
mjr 87:8d35c74403af 5779 // Starting position for current firing mode phase
mjr 87:8d35c74403af 5780 PlungerReading f0;
mjr 48:058ace2aed1d 5781 };
mjr 48:058ace2aed1d 5782
mjr 48:058ace2aed1d 5783 // plunger reader singleton
mjr 48:058ace2aed1d 5784 PlungerReader plungerReader;
mjr 48:058ace2aed1d 5785
mjr 48:058ace2aed1d 5786 // ---------------------------------------------------------------------------
mjr 48:058ace2aed1d 5787 //
mjr 48:058ace2aed1d 5788 // Handle the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5789 //
mjr 48:058ace2aed1d 5790 // The ZB Launch Ball feature, if enabled, lets the mechanical plunger
mjr 48:058ace2aed1d 5791 // serve as a substitute for a physical Launch Ball button. When a table
mjr 48:058ace2aed1d 5792 // is loaded in VP, and the table has the ZB Launch Ball LedWiz port
mjr 48:058ace2aed1d 5793 // turned on, we'll disable mechanical plunger reports through the
mjr 48:058ace2aed1d 5794 // joystick interface and instead use the plunger only to simulate the
mjr 48:058ace2aed1d 5795 // Launch Ball button. When the mode is active, pulling back and
mjr 48:058ace2aed1d 5796 // releasing the plunger causes a brief simulated press of the Launch
mjr 48:058ace2aed1d 5797 // button, and pushing the plunger forward of the rest position presses
mjr 48:058ace2aed1d 5798 // the Launch button as long as the plunger is pressed forward.
mjr 48:058ace2aed1d 5799 //
mjr 48:058ace2aed1d 5800 // This feature has two configuration components:
mjr 48:058ace2aed1d 5801 //
mjr 48:058ace2aed1d 5802 // - An LedWiz port number. This port is a "virtual" port that doesn't
mjr 48:058ace2aed1d 5803 // have to be attached to any actual output. DOF uses it to signal
mjr 48:058ace2aed1d 5804 // that the current table uses a Launch button instead of a plunger.
mjr 48:058ace2aed1d 5805 // DOF simply turns the port on when such a table is loaded and turns
mjr 48:058ace2aed1d 5806 // it off at all other times. We use it to enable and disable the
mjr 48:058ace2aed1d 5807 // plunger/launch button connection.
mjr 48:058ace2aed1d 5808 //
mjr 48:058ace2aed1d 5809 // - A joystick button ID. We simulate pressing this button when the
mjr 48:058ace2aed1d 5810 // launch feature is activated via the LedWiz port and the plunger is
mjr 48:058ace2aed1d 5811 // either pulled back and releasd, or pushed forward past the rest
mjr 48:058ace2aed1d 5812 // position.
mjr 48:058ace2aed1d 5813 //
mjr 48:058ace2aed1d 5814 class ZBLaunchBall
mjr 48:058ace2aed1d 5815 {
mjr 48:058ace2aed1d 5816 public:
mjr 48:058ace2aed1d 5817 ZBLaunchBall()
mjr 48:058ace2aed1d 5818 {
mjr 48:058ace2aed1d 5819 // start in the default state
mjr 48:058ace2aed1d 5820 lbState = 0;
mjr 53:9b2611964afc 5821 btnState = false;
mjr 48:058ace2aed1d 5822 }
mjr 48:058ace2aed1d 5823
mjr 48:058ace2aed1d 5824 // Update state. This checks the current plunger position and
mjr 48:058ace2aed1d 5825 // the timers to see if the plunger is in a position that simulates
mjr 48:058ace2aed1d 5826 // a Launch Ball button press via the ZB Launch Ball feature.
mjr 48:058ace2aed1d 5827 // Updates the simulated button vector according to the current
mjr 48:058ace2aed1d 5828 // launch ball state. The main loop calls this before each
mjr 48:058ace2aed1d 5829 // joystick update to figure the new simulated button state.
mjr 53:9b2611964afc 5830 void update()
mjr 48:058ace2aed1d 5831 {
mjr 53:9b2611964afc 5832 // If the ZB Launch Ball led wiz output is ON, check for a
mjr 53:9b2611964afc 5833 // plunger firing event
mjr 53:9b2611964afc 5834 if (zbLaunchOn)
mjr 48:058ace2aed1d 5835 {
mjr 53:9b2611964afc 5836 // note the new position
mjr 48:058ace2aed1d 5837 int znew = plungerReader.getPosition();
mjr 53:9b2611964afc 5838
mjr 53:9b2611964afc 5839 // figure the push threshold from the configuration data
mjr 51:57eb311faafa 5840 const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
mjr 53:9b2611964afc 5841
mjr 53:9b2611964afc 5842 // check the state
mjr 48:058ace2aed1d 5843 switch (lbState)
mjr 48:058ace2aed1d 5844 {
mjr 48:058ace2aed1d 5845 case 0:
mjr 53:9b2611964afc 5846 // Default state. If a launch event has been detected on
mjr 53:9b2611964afc 5847 // the plunger, activate a timed pulse and switch to state 1.
mjr 53:9b2611964afc 5848 // If the plunger is pushed forward of the threshold, push
mjr 53:9b2611964afc 5849 // the button.
mjr 53:9b2611964afc 5850 if (plungerReader.isFiring())
mjr 53:9b2611964afc 5851 {
mjr 53:9b2611964afc 5852 // firing event - start a timed Launch button pulse
mjr 53:9b2611964afc 5853 lbTimer.reset();
mjr 53:9b2611964afc 5854 lbTimer.start();
mjr 53:9b2611964afc 5855 setButton(true);
mjr 53:9b2611964afc 5856
mjr 53:9b2611964afc 5857 // switch to state 1
mjr 53:9b2611964afc 5858 lbState = 1;
mjr 53:9b2611964afc 5859 }
mjr 48:058ace2aed1d 5860 else if (znew <= pushThreshold)
mjr 53:9b2611964afc 5861 {
mjr 53:9b2611964afc 5862 // pushed forward without a firing event - hold the
mjr 53:9b2611964afc 5863 // button as long as we're pushed forward
mjr 53:9b2611964afc 5864 setButton(true);
mjr 53:9b2611964afc 5865 }
mjr 53:9b2611964afc 5866 else
mjr 53:9b2611964afc 5867 {
mjr 53:9b2611964afc 5868 // not pushed forward - turn off the Launch button
mjr 53:9b2611964afc 5869 setButton(false);
mjr 53:9b2611964afc 5870 }
mjr 48:058ace2aed1d 5871 break;
mjr 48:058ace2aed1d 5872
mjr 48:058ace2aed1d 5873 case 1:
mjr 53:9b2611964afc 5874 // State 1: Timed Launch button pulse in progress after a
mjr 53:9b2611964afc 5875 // firing event. Wait for the timer to expire.
mjr 53:9b2611964afc 5876 if (lbTimer.read_us() > 200000UL)
mjr 53:9b2611964afc 5877 {
mjr 53:9b2611964afc 5878 // timer expired - turn off the button
mjr 53:9b2611964afc 5879 setButton(false);
mjr 53:9b2611964afc 5880
mjr 53:9b2611964afc 5881 // switch to state 2
mjr 53:9b2611964afc 5882 lbState = 2;
mjr 53:9b2611964afc 5883 }
mjr 48:058ace2aed1d 5884 break;
mjr 48:058ace2aed1d 5885
mjr 48:058ace2aed1d 5886 case 2:
mjr 53:9b2611964afc 5887 // State 2: Timed Launch button pulse done. Wait for the
mjr 53:9b2611964afc 5888 // plunger launch event to end.
mjr 53:9b2611964afc 5889 if (!plungerReader.isFiring())
mjr 53:9b2611964afc 5890 {
mjr 53:9b2611964afc 5891 // firing event done - return to default state
mjr 53:9b2611964afc 5892 lbState = 0;
mjr 53:9b2611964afc 5893 }
mjr 48:058ace2aed1d 5894 break;
mjr 48:058ace2aed1d 5895 }
mjr 53:9b2611964afc 5896 }
mjr 53:9b2611964afc 5897 else
mjr 53:9b2611964afc 5898 {
mjr 53:9b2611964afc 5899 // ZB Launch Ball disabled - turn off the button if it was on
mjr 53:9b2611964afc 5900 setButton(false);
mjr 48:058ace2aed1d 5901
mjr 53:9b2611964afc 5902 // return to the default state
mjr 53:9b2611964afc 5903 lbState = 0;
mjr 48:058ace2aed1d 5904 }
mjr 48:058ace2aed1d 5905 }
mjr 53:9b2611964afc 5906
mjr 53:9b2611964afc 5907 // Set the button state
mjr 53:9b2611964afc 5908 void setButton(bool on)
mjr 53:9b2611964afc 5909 {
mjr 53:9b2611964afc 5910 if (btnState != on)
mjr 53:9b2611964afc 5911 {
mjr 53:9b2611964afc 5912 // remember the new state
mjr 53:9b2611964afc 5913 btnState = on;
mjr 53:9b2611964afc 5914
mjr 53:9b2611964afc 5915 // update the virtual button state
mjr 65:739875521aae 5916 buttonState[zblButtonIndex].virtPress(on);
mjr 53:9b2611964afc 5917 }
mjr 53:9b2611964afc 5918 }
mjr 53:9b2611964afc 5919
mjr 48:058ace2aed1d 5920 private:
mjr 48:058ace2aed1d 5921 // Simulated Launch Ball button state. If a "ZB Launch Ball" port is
mjr 48:058ace2aed1d 5922 // defined for our LedWiz port mapping, any time that port is turned ON,
mjr 48:058ace2aed1d 5923 // we'll simulate pushing the Launch Ball button if the player pulls
mjr 48:058ace2aed1d 5924 // back and releases the plunger, or simply pushes on the plunger from
mjr 48:058ace2aed1d 5925 // the rest position. This allows the plunger to be used in lieu of a
mjr 48:058ace2aed1d 5926 // physical Launch Ball button for tables that don't have plungers.
mjr 48:058ace2aed1d 5927 //
mjr 48:058ace2aed1d 5928 // States:
mjr 48:058ace2aed1d 5929 // 0 = default
mjr 53:9b2611964afc 5930 // 1 = firing (firing event has activated a Launch button pulse)
mjr 53:9b2611964afc 5931 // 2 = firing done (Launch button pulse ended, waiting for plunger
mjr 53:9b2611964afc 5932 // firing event to end)
mjr 53:9b2611964afc 5933 uint8_t lbState;
mjr 48:058ace2aed1d 5934
mjr 53:9b2611964afc 5935 // button state
mjr 53:9b2611964afc 5936 bool btnState;
mjr 48:058ace2aed1d 5937
mjr 48:058ace2aed1d 5938 // Time since last lbState transition. Some of the states are time-
mjr 48:058ace2aed1d 5939 // sensitive. In the "uncocked" state, we'll return to state 0 if
mjr 48:058ace2aed1d 5940 // we remain in this state for more than a few milliseconds, since
mjr 48:058ace2aed1d 5941 // it indicates that the plunger is being slowly returned to rest
mjr 48:058ace2aed1d 5942 // rather than released. In the "launching" state, we need to release
mjr 48:058ace2aed1d 5943 // the Launch Ball button after a moment, and we need to wait for
mjr 48:058ace2aed1d 5944 // the plunger to come to rest before returning to state 0.
mjr 48:058ace2aed1d 5945 Timer lbTimer;
mjr 48:058ace2aed1d 5946 };
mjr 48:058ace2aed1d 5947
mjr 35:e959ffba78fd 5948 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5949 //
mjr 35:e959ffba78fd 5950 // Reboot - resets the microcontroller
mjr 35:e959ffba78fd 5951 //
mjr 54:fd77a6b2f76c 5952 void reboot(USBJoystick &js, bool disconnect = true, long pause_us = 2000000L)
mjr 35:e959ffba78fd 5953 {
mjr 35:e959ffba78fd 5954 // disconnect from USB
mjr 54:fd77a6b2f76c 5955 if (disconnect)
mjr 54:fd77a6b2f76c 5956 js.disconnect();
mjr 35:e959ffba78fd 5957
mjr 35:e959ffba78fd 5958 // wait a few seconds to make sure the host notices the disconnect
mjr 54:fd77a6b2f76c 5959 wait_us(pause_us);
mjr 35:e959ffba78fd 5960
mjr 35:e959ffba78fd 5961 // reset the device
mjr 35:e959ffba78fd 5962 NVIC_SystemReset();
mjr 35:e959ffba78fd 5963 while (true) { }
mjr 35:e959ffba78fd 5964 }
mjr 35:e959ffba78fd 5965
mjr 35:e959ffba78fd 5966 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5967 //
mjr 35:e959ffba78fd 5968 // Translate joystick readings from raw values to reported values, based
mjr 35:e959ffba78fd 5969 // on the orientation of the controller card in the cabinet.
mjr 35:e959ffba78fd 5970 //
mjr 35:e959ffba78fd 5971 void accelRotate(int &x, int &y)
mjr 35:e959ffba78fd 5972 {
mjr 35:e959ffba78fd 5973 int tmp;
mjr 78:1e00b3fa11af 5974 switch (cfg.accel.orientation)
mjr 35:e959ffba78fd 5975 {
mjr 35:e959ffba78fd 5976 case OrientationFront:
mjr 35:e959ffba78fd 5977 tmp = x;
mjr 35:e959ffba78fd 5978 x = y;
mjr 35:e959ffba78fd 5979 y = tmp;
mjr 35:e959ffba78fd 5980 break;
mjr 35:e959ffba78fd 5981
mjr 35:e959ffba78fd 5982 case OrientationLeft:
mjr 35:e959ffba78fd 5983 x = -x;
mjr 35:e959ffba78fd 5984 break;
mjr 35:e959ffba78fd 5985
mjr 35:e959ffba78fd 5986 case OrientationRight:
mjr 35:e959ffba78fd 5987 y = -y;
mjr 35:e959ffba78fd 5988 break;
mjr 35:e959ffba78fd 5989
mjr 35:e959ffba78fd 5990 case OrientationRear:
mjr 35:e959ffba78fd 5991 tmp = -x;
mjr 35:e959ffba78fd 5992 x = -y;
mjr 35:e959ffba78fd 5993 y = tmp;
mjr 35:e959ffba78fd 5994 break;
mjr 35:e959ffba78fd 5995 }
mjr 35:e959ffba78fd 5996 }
mjr 35:e959ffba78fd 5997
mjr 35:e959ffba78fd 5998 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 5999 //
mjr 35:e959ffba78fd 6000 // Calibration button state:
mjr 35:e959ffba78fd 6001 // 0 = not pushed
mjr 35:e959ffba78fd 6002 // 1 = pushed, not yet debounced
mjr 35:e959ffba78fd 6003 // 2 = pushed, debounced, waiting for hold time
mjr 35:e959ffba78fd 6004 // 3 = pushed, hold time completed - in calibration mode
mjr 35:e959ffba78fd 6005 int calBtnState = 0;
mjr 35:e959ffba78fd 6006
mjr 35:e959ffba78fd 6007 // calibration button debounce timer
mjr 35:e959ffba78fd 6008 Timer calBtnTimer;
mjr 35:e959ffba78fd 6009
mjr 35:e959ffba78fd 6010 // calibration button light state
mjr 35:e959ffba78fd 6011 int calBtnLit = false;
mjr 35:e959ffba78fd 6012
mjr 35:e959ffba78fd 6013
mjr 35:e959ffba78fd 6014 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 6015 //
mjr 40:cc0d9814522b 6016 // Configuration variable get/set message handling
mjr 35:e959ffba78fd 6017 //
mjr 40:cc0d9814522b 6018
mjr 40:cc0d9814522b 6019 // Handle SET messages - write configuration variables from USB message data
mjr 40:cc0d9814522b 6020 #define if_msg_valid(test) if (test)
mjr 53:9b2611964afc 6021 #define v_byte(var, ofs) cfg.var = data[ofs]
mjr 91:ae9be42652bf 6022 #define v_byte_wo(var, ofs) cfg.var = data[ofs]
mjr 53:9b2611964afc 6023 #define v_ui16(var, ofs) cfg.var = wireUI16(data+(ofs))
mjr 77:0b96f6867312 6024 #define v_ui32(var, ofs) cfg.var = wireUI32(data+(ofs))
mjr 53:9b2611964afc 6025 #define v_pin(var, ofs) cfg.var = wirePinName(data[ofs])
mjr 53:9b2611964afc 6026 #define v_byte_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 6027 #define v_ui32_ro(val, ofs) // ignore read-only variables on SET
mjr 74:822a92bc11d2 6028 #define VAR_MODE_SET 1 // we're in SET mode
mjr 76:7f5912b6340e 6029 #define v_func configVarSet(const uint8_t *data)
mjr 40:cc0d9814522b 6030 #include "cfgVarMsgMap.h"
mjr 35:e959ffba78fd 6031
mjr 40:cc0d9814522b 6032 // redefine everything for the SET messages
mjr 40:cc0d9814522b 6033 #undef if_msg_valid
mjr 40:cc0d9814522b 6034 #undef v_byte
mjr 40:cc0d9814522b 6035 #undef v_ui16
mjr 77:0b96f6867312 6036 #undef v_ui32
mjr 40:cc0d9814522b 6037 #undef v_pin
mjr 53:9b2611964afc 6038 #undef v_byte_ro
mjr 91:ae9be42652bf 6039 #undef v_byte_wo
mjr 74:822a92bc11d2 6040 #undef v_ui32_ro
mjr 74:822a92bc11d2 6041 #undef VAR_MODE_SET
mjr 40:cc0d9814522b 6042 #undef v_func
mjr 38:091e511ce8a0 6043
mjr 91:ae9be42652bf 6044 // Handle GET messages - read variable values and return in USB message data
mjr 40:cc0d9814522b 6045 #define if_msg_valid(test)
mjr 53:9b2611964afc 6046 #define v_byte(var, ofs) data[ofs] = cfg.var
mjr 53:9b2611964afc 6047 #define v_ui16(var, ofs) ui16Wire(data+(ofs), cfg.var)
mjr 77:0b96f6867312 6048 #define v_ui32(var, ofs) ui32Wire(data+(ofs), cfg.var)
mjr 53:9b2611964afc 6049 #define v_pin(var, ofs) pinNameWire(data+(ofs), cfg.var)
mjr 73:4e8ce0b18915 6050 #define v_byte_ro(val, ofs) data[ofs] = (val)
mjr 74:822a92bc11d2 6051 #define v_ui32_ro(val, ofs) ui32Wire(data+(ofs), val);
mjr 74:822a92bc11d2 6052 #define VAR_MODE_SET 0 // we're in GET mode
mjr 91:ae9be42652bf 6053 #define v_byte_wo(var, ofs) // ignore write-only variables in GET mode
mjr 76:7f5912b6340e 6054 #define v_func configVarGet(uint8_t *data)
mjr 40:cc0d9814522b 6055 #include "cfgVarMsgMap.h"
mjr 40:cc0d9814522b 6056
mjr 35:e959ffba78fd 6057
mjr 35:e959ffba78fd 6058 // ---------------------------------------------------------------------------
mjr 35:e959ffba78fd 6059 //
mjr 101:755f44622abc 6060 // Timer for timestamping input requests
mjr 101:755f44622abc 6061 //
mjr 101:755f44622abc 6062 Timer requestTimestamper;
mjr 101:755f44622abc 6063
mjr 101:755f44622abc 6064 // ---------------------------------------------------------------------------
mjr 101:755f44622abc 6065 //
mjr 35:e959ffba78fd 6066 // Handle an input report from the USB host. Input reports use our extended
mjr 35:e959ffba78fd 6067 // LedWiz protocol.
mjr 33:d832bcab089e 6068 //
mjr 78:1e00b3fa11af 6069 void handleInputMsg(LedWizMsg &lwm, USBJoystick &js, Accel &accel)
mjr 35:e959ffba78fd 6070 {
mjr 38:091e511ce8a0 6071 // LedWiz commands come in two varieties: SBA and PBA. An
mjr 38:091e511ce8a0 6072 // SBA is marked by the first byte having value 64 (0x40). In
mjr 38:091e511ce8a0 6073 // the real LedWiz protocol, any other value in the first byte
mjr 38:091e511ce8a0 6074 // means it's a PBA message. However, *valid* PBA messages
mjr 38:091e511ce8a0 6075 // always have a first byte (and in fact all 8 bytes) in the
mjr 38:091e511ce8a0 6076 // range 0-49 or 129-132. Anything else is invalid. We take
mjr 38:091e511ce8a0 6077 // advantage of this to implement private protocol extensions.
mjr 38:091e511ce8a0 6078 // So our full protocol is as follows:
mjr 38:091e511ce8a0 6079 //
mjr 38:091e511ce8a0 6080 // first byte =
mjr 74:822a92bc11d2 6081 // 0-48 -> PBA
mjr 74:822a92bc11d2 6082 // 64 -> SBA
mjr 38:091e511ce8a0 6083 // 65 -> private control message; second byte specifies subtype
mjr 74:822a92bc11d2 6084 // 129-132 -> PBA
mjr 38:091e511ce8a0 6085 // 200-228 -> extended bank brightness set for outputs N to N+6, where
mjr 38:091e511ce8a0 6086 // N is (first byte - 200)*7
mjr 38:091e511ce8a0 6087 // other -> reserved for future use
mjr 38:091e511ce8a0 6088 //
mjr 39:b3815a1c3802 6089 uint8_t *data = lwm.data;
mjr 74:822a92bc11d2 6090 if (data[0] == 64)
mjr 35:e959ffba78fd 6091 {
mjr 74:822a92bc11d2 6092 // 64 = SBA (original LedWiz command to set on/off switches for ports 1-32)
mjr 74:822a92bc11d2 6093 //printf("SBA %02x %02x %02x %02x, speed %02x\r\n",
mjr 38:091e511ce8a0 6094 // data[1], data[2], data[3], data[4], data[5]);
mjr 74:822a92bc11d2 6095 sba_sbx(0, data);
mjr 74:822a92bc11d2 6096
mjr 74:822a92bc11d2 6097 // SBA resets the PBA port group counter
mjr 38:091e511ce8a0 6098 pbaIdx = 0;
mjr 38:091e511ce8a0 6099 }
mjr 38:091e511ce8a0 6100 else if (data[0] == 65)
mjr 38:091e511ce8a0 6101 {
mjr 38:091e511ce8a0 6102 // Private control message. This isn't an LedWiz message - it's
mjr 38:091e511ce8a0 6103 // an extension for this device. 65 is an invalid PBA setting,
mjr 38:091e511ce8a0 6104 // and isn't used for any other LedWiz message, so we appropriate
mjr 38:091e511ce8a0 6105 // it for our own private use. The first byte specifies the
mjr 38:091e511ce8a0 6106 // message type.
mjr 39:b3815a1c3802 6107 switch (data[1])
mjr 38:091e511ce8a0 6108 {
mjr 39:b3815a1c3802 6109 case 0:
mjr 39:b3815a1c3802 6110 // No Op
mjr 39:b3815a1c3802 6111 break;
mjr 39:b3815a1c3802 6112
mjr 39:b3815a1c3802 6113 case 1:
mjr 38:091e511ce8a0 6114 // 1 = Old Set Configuration:
mjr 38:091e511ce8a0 6115 // data[2] = LedWiz unit number (0x00 to 0x0f)
mjr 38:091e511ce8a0 6116 // data[3] = feature enable bit mask:
mjr 38:091e511ce8a0 6117 // 0x01 = enable plunger sensor
mjr 39:b3815a1c3802 6118 {
mjr 39:b3815a1c3802 6119
mjr 39:b3815a1c3802 6120 // get the new LedWiz unit number - this is 0-15, whereas we
mjr 39:b3815a1c3802 6121 // we save the *nominal* unit number 1-16 in the config
mjr 39:b3815a1c3802 6122 uint8_t newUnitNo = (data[2] & 0x0f) + 1;
mjr 39:b3815a1c3802 6123
mjr 86:e30a1f60f783 6124 // we'll need a reboot if the LedWiz unit number is changing
mjr 86:e30a1f60f783 6125 bool reboot = (newUnitNo != cfg.psUnitNo);
mjr 39:b3815a1c3802 6126
mjr 39:b3815a1c3802 6127 // set the configuration parameters from the message
mjr 39:b3815a1c3802 6128 cfg.psUnitNo = newUnitNo;
mjr 39:b3815a1c3802 6129 cfg.plunger.enabled = data[3] & 0x01;
mjr 39:b3815a1c3802 6130
mjr 77:0b96f6867312 6131 // set the flag to do the save
mjr 86:e30a1f60f783 6132 saveConfigToFlash(0, reboot);
mjr 39:b3815a1c3802 6133 }
mjr 39:b3815a1c3802 6134 break;
mjr 38:091e511ce8a0 6135
mjr 39:b3815a1c3802 6136 case 2:
mjr 38:091e511ce8a0 6137 // 2 = Calibrate plunger
mjr 38:091e511ce8a0 6138 // (No parameters)
mjr 38:091e511ce8a0 6139
mjr 38:091e511ce8a0 6140 // enter calibration mode
mjr 38:091e511ce8a0 6141 calBtnState = 3;
mjr 52:8298b2a73eb2 6142 plungerReader.setCalMode(true);
mjr 38:091e511ce8a0 6143 calBtnTimer.reset();
mjr 39:b3815a1c3802 6144 break;
mjr 39:b3815a1c3802 6145
mjr 39:b3815a1c3802 6146 case 3:
mjr 52:8298b2a73eb2 6147 // 3 = plunger sensor status report
mjr 48:058ace2aed1d 6148 // data[2] = flag bits
mjr 53:9b2611964afc 6149 // data[3] = extra exposure time, 100us (.1ms) increments
mjr 52:8298b2a73eb2 6150 reportPlungerStat = true;
mjr 53:9b2611964afc 6151 reportPlungerStatFlags = data[2];
mjr 53:9b2611964afc 6152 reportPlungerStatTime = data[3];
mjr 38:091e511ce8a0 6153
mjr 101:755f44622abc 6154 // set the extra integration time in the sensor
mjr 101:755f44622abc 6155 plungerSensor->setExtraIntegrationTime(reportPlungerStatTime * 100);
mjr 101:755f44622abc 6156
mjr 101:755f44622abc 6157 // make a note of the request timestamp
mjr 101:755f44622abc 6158 tReportPlungerStat = requestTimestamper.read_us();
mjr 101:755f44622abc 6159
mjr 38:091e511ce8a0 6160 // show purple until we finish sending the report
mjr 38:091e511ce8a0 6161 diagLED(1, 0, 1);
mjr 39:b3815a1c3802 6162 break;
mjr 39:b3815a1c3802 6163
mjr 39:b3815a1c3802 6164 case 4:
mjr 38:091e511ce8a0 6165 // 4 = hardware configuration query
mjr 38:091e511ce8a0 6166 // (No parameters)
mjr 38:091e511ce8a0 6167 js.reportConfig(
mjr 38:091e511ce8a0 6168 numOutputs,
mjr 38:091e511ce8a0 6169 cfg.psUnitNo - 1, // report 0-15 range for unit number (we store 1-16 internally)
mjr 52:8298b2a73eb2 6170 cfg.plunger.cal.zero, cfg.plunger.cal.max, cfg.plunger.cal.tRelease,
mjr 75:677892300e7a 6171 nvm.valid(), // a config is loaded if the config memory block is valid
mjr 75:677892300e7a 6172 true, // we support sbx/pbx extensions
mjr 78:1e00b3fa11af 6173 true, // we support the new accelerometer settings
mjr 82:4f6209cb5c33 6174 true, // we support the "flash write ok" status bit in joystick reports
mjr 92:f264fbaa1be5 6175 true, // we support the configurable joystick report timing features
mjr 99:8139b0c274f4 6176 true, // chime logic is supported
mjr 79:682ae3171a08 6177 mallocBytesFree()); // remaining memory size
mjr 39:b3815a1c3802 6178 break;
mjr 39:b3815a1c3802 6179
mjr 39:b3815a1c3802 6180 case 5:
mjr 38:091e511ce8a0 6181 // 5 = all outputs off, reset to LedWiz defaults
mjr 38:091e511ce8a0 6182 allOutputsOff();
mjr 39:b3815a1c3802 6183 break;
mjr 39:b3815a1c3802 6184
mjr 39:b3815a1c3802 6185 case 6:
mjr 85:3c28aee81cde 6186 // 6 = Save configuration to flash. Optionally reboot after the
mjr 85:3c28aee81cde 6187 // delay time in seconds given in data[2].
mjr 85:3c28aee81cde 6188 //
mjr 85:3c28aee81cde 6189 // data[2] = delay time in seconds
mjr 85:3c28aee81cde 6190 // data[3] = flags:
mjr 85:3c28aee81cde 6191 // 0x01 -> do not reboot
mjr 86:e30a1f60f783 6192 saveConfigToFlash(data[2], !(data[3] & 0x01));
mjr 39:b3815a1c3802 6193 break;
mjr 40:cc0d9814522b 6194
mjr 40:cc0d9814522b 6195 case 7:
mjr 40:cc0d9814522b 6196 // 7 = Device ID report
mjr 53:9b2611964afc 6197 // data[2] = ID index: 1=CPU ID, 2=OpenSDA TUID
mjr 53:9b2611964afc 6198 js.reportID(data[2]);
mjr 40:cc0d9814522b 6199 break;
mjr 40:cc0d9814522b 6200
mjr 40:cc0d9814522b 6201 case 8:
mjr 40:cc0d9814522b 6202 // 8 = Engage/disengage night mode.
mjr 40:cc0d9814522b 6203 // data[2] = 1 to engage, 0 to disengage
mjr 40:cc0d9814522b 6204 setNightMode(data[2]);
mjr 40:cc0d9814522b 6205 break;
mjr 52:8298b2a73eb2 6206
mjr 52:8298b2a73eb2 6207 case 9:
mjr 52:8298b2a73eb2 6208 // 9 = Config variable query.
mjr 52:8298b2a73eb2 6209 // data[2] = config var ID
mjr 52:8298b2a73eb2 6210 // data[3] = array index (for array vars: button assignments, output ports)
mjr 52:8298b2a73eb2 6211 {
mjr 53:9b2611964afc 6212 // set up the reply buffer with the variable ID data, and zero out
mjr 53:9b2611964afc 6213 // the rest of the buffer
mjr 52:8298b2a73eb2 6214 uint8_t reply[8];
mjr 52:8298b2a73eb2 6215 reply[1] = data[2];
mjr 52:8298b2a73eb2 6216 reply[2] = data[3];
mjr 53:9b2611964afc 6217 memset(reply+3, 0, sizeof(reply)-3);
mjr 52:8298b2a73eb2 6218
mjr 52:8298b2a73eb2 6219 // query the value
mjr 52:8298b2a73eb2 6220 configVarGet(reply);
mjr 52:8298b2a73eb2 6221
mjr 52:8298b2a73eb2 6222 // send the reply
mjr 52:8298b2a73eb2 6223 js.reportConfigVar(reply + 1);
mjr 52:8298b2a73eb2 6224 }
mjr 52:8298b2a73eb2 6225 break;
mjr 53:9b2611964afc 6226
mjr 53:9b2611964afc 6227 case 10:
mjr 53:9b2611964afc 6228 // 10 = Build ID query.
mjr 53:9b2611964afc 6229 js.reportBuildInfo(getBuildID());
mjr 53:9b2611964afc 6230 break;
mjr 73:4e8ce0b18915 6231
mjr 73:4e8ce0b18915 6232 case 11:
mjr 73:4e8ce0b18915 6233 // 11 = TV ON relay control.
mjr 73:4e8ce0b18915 6234 // data[2] = operation:
mjr 73:4e8ce0b18915 6235 // 0 = turn relay off
mjr 73:4e8ce0b18915 6236 // 1 = turn relay on
mjr 73:4e8ce0b18915 6237 // 2 = pulse relay (as though the power-on timer fired)
mjr 73:4e8ce0b18915 6238 TVRelay(data[2]);
mjr 73:4e8ce0b18915 6239 break;
mjr 73:4e8ce0b18915 6240
mjr 73:4e8ce0b18915 6241 case 12:
mjr 77:0b96f6867312 6242 // 12 = Learn IR code. This enters IR learning mode. While
mjr 77:0b96f6867312 6243 // in learning mode, we report raw IR signals and the first IR
mjr 77:0b96f6867312 6244 // command decoded through the special IR report format. IR
mjr 77:0b96f6867312 6245 // learning mode automatically ends after a timeout expires if
mjr 77:0b96f6867312 6246 // no command can be decoded within the time limit.
mjr 77:0b96f6867312 6247
mjr 77:0b96f6867312 6248 // enter IR learning mode
mjr 77:0b96f6867312 6249 IRLearningMode = 1;
mjr 77:0b96f6867312 6250
mjr 77:0b96f6867312 6251 // cancel any regular IR input in progress
mjr 77:0b96f6867312 6252 IRCommandIn = 0;
mjr 77:0b96f6867312 6253
mjr 77:0b96f6867312 6254 // reset and start the learning mode timeout timer
mjr 77:0b96f6867312 6255 IRTimer.reset();
mjr 73:4e8ce0b18915 6256 break;
mjr 73:4e8ce0b18915 6257
mjr 73:4e8ce0b18915 6258 case 13:
mjr 73:4e8ce0b18915 6259 // 13 = Send button status report
mjr 73:4e8ce0b18915 6260 reportButtonStatus(js);
mjr 73:4e8ce0b18915 6261 break;
mjr 78:1e00b3fa11af 6262
mjr 78:1e00b3fa11af 6263 case 14:
mjr 78:1e00b3fa11af 6264 // 14 = manually center the accelerometer
mjr 78:1e00b3fa11af 6265 accel.manualCenterRequest();
mjr 78:1e00b3fa11af 6266 break;
mjr 78:1e00b3fa11af 6267
mjr 78:1e00b3fa11af 6268 case 15:
mjr 78:1e00b3fa11af 6269 // 15 = set up ad hoc IR command, part 1. Mark the command
mjr 78:1e00b3fa11af 6270 // as not ready, and save the partial data from the message.
mjr 78:1e00b3fa11af 6271 IRAdHocCmd.ready = 0;
mjr 78:1e00b3fa11af 6272 IRAdHocCmd.protocol = data[2];
mjr 78:1e00b3fa11af 6273 IRAdHocCmd.dittos = (data[3] & IRFlagDittos) != 0;
mjr 78:1e00b3fa11af 6274 IRAdHocCmd.code = wireUI32(&data[4]);
mjr 78:1e00b3fa11af 6275 break;
mjr 78:1e00b3fa11af 6276
mjr 78:1e00b3fa11af 6277 case 16:
mjr 78:1e00b3fa11af 6278 // 16 = send ad hoc IR command, part 2. Fill in the rest
mjr 78:1e00b3fa11af 6279 // of the data from the message and mark the command as
mjr 78:1e00b3fa11af 6280 // ready. The IR polling routine will send this as soon
mjr 78:1e00b3fa11af 6281 // as the IR transmitter is free.
mjr 78:1e00b3fa11af 6282 IRAdHocCmd.code |= (uint64_t(wireUI32(&data[2])) << 32);
mjr 78:1e00b3fa11af 6283 IRAdHocCmd.ready = 1;
mjr 78:1e00b3fa11af 6284 break;
mjr 88:98bce687e6c0 6285
mjr 88:98bce687e6c0 6286 case 17:
mjr 88:98bce687e6c0 6287 // 17 = send pre-programmed IR command. This works just like
mjr 88:98bce687e6c0 6288 // sending an ad hoc command above, but we get the command data
mjr 88:98bce687e6c0 6289 // from an IR slot in the config rather than from the client.
mjr 88:98bce687e6c0 6290 // First make sure we have a valid slot number.
mjr 88:98bce687e6c0 6291 if (data[2] >= 1 && data[2] <= MAX_IR_CODES)
mjr 88:98bce687e6c0 6292 {
mjr 88:98bce687e6c0 6293 // get the IR command slot in the config
mjr 88:98bce687e6c0 6294 IRCommandCfg &cmd = cfg.IRCommand[data[2] - 1];
mjr 88:98bce687e6c0 6295
mjr 88:98bce687e6c0 6296 // copy the IR command data from the config
mjr 88:98bce687e6c0 6297 IRAdHocCmd.protocol = cmd.protocol;
mjr 88:98bce687e6c0 6298 IRAdHocCmd.dittos = (cmd.flags & IRFlagDittos) != 0;
mjr 88:98bce687e6c0 6299 IRAdHocCmd.code = (uint64_t(cmd.code.hi) << 32) | cmd.code.lo;
mjr 88:98bce687e6c0 6300
mjr 88:98bce687e6c0 6301 // mark the command as ready - this will trigger the polling
mjr 88:98bce687e6c0 6302 // routine to send the command as soon as the transmitter
mjr 88:98bce687e6c0 6303 // is free
mjr 88:98bce687e6c0 6304 IRAdHocCmd.ready = 1;
mjr 88:98bce687e6c0 6305 }
mjr 88:98bce687e6c0 6306 break;
mjr 38:091e511ce8a0 6307 }
mjr 38:091e511ce8a0 6308 }
mjr 38:091e511ce8a0 6309 else if (data[0] == 66)
mjr 38:091e511ce8a0 6310 {
mjr 38:091e511ce8a0 6311 // Extended protocol - Set configuration variable.
mjr 38:091e511ce8a0 6312 // The second byte of the message is the ID of the variable
mjr 38:091e511ce8a0 6313 // to update, and the remaining bytes give the new value,
mjr 38:091e511ce8a0 6314 // in a variable-dependent format.
mjr 40:cc0d9814522b 6315 configVarSet(data);
mjr 86:e30a1f60f783 6316
mjr 87:8d35c74403af 6317 // notify the plunger, so that it can update relevant variables
mjr 87:8d35c74403af 6318 // dynamically
mjr 87:8d35c74403af 6319 plungerSensor->onConfigChange(data[1], cfg);
mjr 38:091e511ce8a0 6320 }
mjr 74:822a92bc11d2 6321 else if (data[0] == 67)
mjr 74:822a92bc11d2 6322 {
mjr 74:822a92bc11d2 6323 // SBX - extended SBA message. This is the same as SBA, except
mjr 74:822a92bc11d2 6324 // that the 7th byte selects a group of 32 ports, to allow access
mjr 74:822a92bc11d2 6325 // to ports beyond the first 32.
mjr 74:822a92bc11d2 6326 sba_sbx(data[6], data);
mjr 74:822a92bc11d2 6327 }
mjr 74:822a92bc11d2 6328 else if (data[0] == 68)
mjr 74:822a92bc11d2 6329 {
mjr 74:822a92bc11d2 6330 // PBX - extended PBA message. This is similar to PBA, but
mjr 74:822a92bc11d2 6331 // allows access to more than the first 32 ports by encoding
mjr 74:822a92bc11d2 6332 // a port group byte that selects a block of 8 ports.
mjr 74:822a92bc11d2 6333
mjr 74:822a92bc11d2 6334 // get the port group - the first port is 8*group
mjr 74:822a92bc11d2 6335 int portGroup = data[1];
mjr 74:822a92bc11d2 6336
mjr 74:822a92bc11d2 6337 // unpack the brightness values
mjr 74:822a92bc11d2 6338 uint32_t tmp1 = data[2] | (data[3]<<8) | (data[4]<<16);
mjr 74:822a92bc11d2 6339 uint32_t tmp2 = data[5] | (data[6]<<8) | (data[7]<<16);
mjr 74:822a92bc11d2 6340 uint8_t bri[8] = {
mjr 74:822a92bc11d2 6341 tmp1 & 0x3F, (tmp1>>6) & 0x3F, (tmp1>>12) & 0x3F, (tmp1>>18) & 0x3F,
mjr 74:822a92bc11d2 6342 tmp2 & 0x3F, (tmp2>>6) & 0x3F, (tmp2>>12) & 0x3F, (tmp2>>18) & 0x3F
mjr 74:822a92bc11d2 6343 };
mjr 74:822a92bc11d2 6344
mjr 74:822a92bc11d2 6345 // map the flash levels: 60->129, 61->130, 62->131, 63->132
mjr 74:822a92bc11d2 6346 for (int i = 0 ; i < 8 ; ++i)
mjr 74:822a92bc11d2 6347 {
mjr 74:822a92bc11d2 6348 if (bri[i] >= 60)
mjr 74:822a92bc11d2 6349 bri[i] += 129-60;
mjr 74:822a92bc11d2 6350 }
mjr 74:822a92bc11d2 6351
mjr 74:822a92bc11d2 6352 // Carry out the PBA
mjr 74:822a92bc11d2 6353 pba_pbx(portGroup*8, bri);
mjr 74:822a92bc11d2 6354 }
mjr 38:091e511ce8a0 6355 else if (data[0] >= 200 && data[0] <= 228)
mjr 38:091e511ce8a0 6356 {
mjr 38:091e511ce8a0 6357 // Extended protocol - Extended output port brightness update.
mjr 38:091e511ce8a0 6358 // data[0]-200 gives us the bank of 7 outputs we're setting:
mjr 38:091e511ce8a0 6359 // 200 is outputs 0-6, 201 is outputs 7-13, 202 is 14-20, etc.
mjr 38:091e511ce8a0 6360 // The remaining bytes are brightness levels, 0-255, for the
mjr 38:091e511ce8a0 6361 // seven outputs in the selected bank. The LedWiz flashing
mjr 38:091e511ce8a0 6362 // modes aren't accessible in this message type; we can only
mjr 38:091e511ce8a0 6363 // set a fixed brightness, but in exchange we get 8-bit
mjr 38:091e511ce8a0 6364 // resolution rather than the paltry 0-48 scale that the real
mjr 38:091e511ce8a0 6365 // LedWiz uses. There's no separate on/off status for outputs
mjr 38:091e511ce8a0 6366 // adjusted with this message type, either, as there would be
mjr 38:091e511ce8a0 6367 // for a PBA message - setting a non-zero value immediately
mjr 38:091e511ce8a0 6368 // turns the output, overriding the last SBA setting.
mjr 38:091e511ce8a0 6369 //
mjr 38:091e511ce8a0 6370 // For outputs 0-31, this overrides any previous PBA/SBA
mjr 38:091e511ce8a0 6371 // settings for the port. Any subsequent PBA/SBA message will
mjr 38:091e511ce8a0 6372 // in turn override the setting made here. It's simple - the
mjr 38:091e511ce8a0 6373 // most recent message of either type takes precedence. For
mjr 38:091e511ce8a0 6374 // outputs above the LedWiz range, PBA/SBA messages can't
mjr 38:091e511ce8a0 6375 // address those ports anyway.
mjr 63:5cd1a5f3a41b 6376
mjr 63:5cd1a5f3a41b 6377 // figure the block of 7 ports covered in the message
mjr 38:091e511ce8a0 6378 int i0 = (data[0] - 200)*7;
mjr 38:091e511ce8a0 6379 int i1 = i0 + 7 < numOutputs ? i0 + 7 : numOutputs;
mjr 63:5cd1a5f3a41b 6380
mjr 63:5cd1a5f3a41b 6381 // update each port
mjr 38:091e511ce8a0 6382 for (int i = i0 ; i < i1 ; ++i)
mjr 38:091e511ce8a0 6383 {
mjr 38:091e511ce8a0 6384 // set the brightness level for the output
mjr 40:cc0d9814522b 6385 uint8_t b = data[i-i0+1];
mjr 38:091e511ce8a0 6386 outLevel[i] = b;
mjr 38:091e511ce8a0 6387
mjr 74:822a92bc11d2 6388 // set the port's LedWiz state to the nearest equivalent, so
mjr 74:822a92bc11d2 6389 // that it maintains its current setting if we switch back to
mjr 74:822a92bc11d2 6390 // LedWiz mode on a future update
mjr 76:7f5912b6340e 6391 if (b != 0)
mjr 76:7f5912b6340e 6392 {
mjr 76:7f5912b6340e 6393 // Non-zero brightness - set the SBA switch on, and set the
mjr 76:7f5912b6340e 6394 // PBA brightness to the DOF brightness rescaled to the 1..48
mjr 76:7f5912b6340e 6395 // LedWiz range. If the port is subsequently addressed by an
mjr 76:7f5912b6340e 6396 // LedWiz command, this will carry the current DOF setting
mjr 76:7f5912b6340e 6397 // forward unchanged.
mjr 76:7f5912b6340e 6398 wizOn[i] = 1;
mjr 76:7f5912b6340e 6399 wizVal[i] = dof_to_lw[b];
mjr 76:7f5912b6340e 6400 }
mjr 76:7f5912b6340e 6401 else
mjr 76:7f5912b6340e 6402 {
mjr 76:7f5912b6340e 6403 // Zero brightness. Set the SBA switch off, and leave the
mjr 76:7f5912b6340e 6404 // PBA brightness the same as it was.
mjr 76:7f5912b6340e 6405 wizOn[i] = 0;
mjr 76:7f5912b6340e 6406 }
mjr 74:822a92bc11d2 6407
mjr 38:091e511ce8a0 6408 // set the output
mjr 40:cc0d9814522b 6409 lwPin[i]->set(b);
mjr 38:091e511ce8a0 6410 }
mjr 38:091e511ce8a0 6411
mjr 38:091e511ce8a0 6412 // update 74HC595 outputs, if attached
mjr 38:091e511ce8a0 6413 if (hc595 != 0)
mjr 38:091e511ce8a0 6414 hc595->update();
mjr 38:091e511ce8a0 6415 }
mjr 38:091e511ce8a0 6416 else
mjr 38:091e511ce8a0 6417 {
mjr 74:822a92bc11d2 6418 // Everything else is an LedWiz PBA message. This is a full
mjr 74:822a92bc11d2 6419 // "profile" dump from the host for one bank of 8 outputs. Each
mjr 74:822a92bc11d2 6420 // byte sets one output in the current bank. The current bank
mjr 74:822a92bc11d2 6421 // is implied; the bank starts at 0 and is reset to 0 by any SBA
mjr 74:822a92bc11d2 6422 // message, and is incremented to the next bank by each PBA. Our
mjr 74:822a92bc11d2 6423 // variable pbaIdx keeps track of the current bank. There's no
mjr 74:822a92bc11d2 6424 // direct way for the host to select the bank; it just has to count
mjr 74:822a92bc11d2 6425 // on us staying in sync. In practice, clients always send the
mjr 74:822a92bc11d2 6426 // full set of 4 PBA messages in a row to set all 32 outputs.
mjr 38:091e511ce8a0 6427 //
mjr 38:091e511ce8a0 6428 // Note that a PBA implicitly overrides our extended profile
mjr 38:091e511ce8a0 6429 // messages (message prefix 200-219), because this sets the
mjr 38:091e511ce8a0 6430 // wizVal[] entry for each output, and that takes precedence
mjr 63:5cd1a5f3a41b 6431 // over the extended protocol settings when we're in LedWiz
mjr 63:5cd1a5f3a41b 6432 // protocol mode.
mjr 38:091e511ce8a0 6433 //
mjr 38:091e511ce8a0 6434 //printf("LWZ-PBA[%d] %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
mjr 38:091e511ce8a0 6435 // pbaIdx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
mjr 38:091e511ce8a0 6436
mjr 74:822a92bc11d2 6437 // carry out the PBA
mjr 74:822a92bc11d2 6438 pba_pbx(pbaIdx, data);
mjr 74:822a92bc11d2 6439
mjr 74:822a92bc11d2 6440 // update the PBX index state for the next message
mjr 74:822a92bc11d2 6441 pbaIdx = (pbaIdx + 8) % 32;
mjr 38:091e511ce8a0 6442 }
mjr 38:091e511ce8a0 6443 }
mjr 35:e959ffba78fd 6444
mjr 38:091e511ce8a0 6445 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 6446 //
mjr 5:a70c0bce770d 6447 // Main program loop. This is invoked on startup and runs forever. Our
mjr 5:a70c0bce770d 6448 // main work is to read our devices (the accelerometer and the CCD), process
mjr 5:a70c0bce770d 6449 // the readings into nudge and plunger position data, and send the results
mjr 5:a70c0bce770d 6450 // to the host computer via the USB joystick interface. We also monitor
mjr 5:a70c0bce770d 6451 // the USB connection for incoming LedWiz commands and process those into
mjr 5:a70c0bce770d 6452 // port outputs.
mjr 5:a70c0bce770d 6453 //
mjr 0:5acbbe3f4cf4 6454 int main(void)
mjr 0:5acbbe3f4cf4 6455 {
mjr 60:f38da020aa13 6456 // say hello to the debug console, in case it's connected
mjr 39:b3815a1c3802 6457 printf("\r\nPinscape Controller starting\r\n");
mjr 94:0476b3e2b996 6458
mjr 98:4df3c0f7e707 6459 // Set the default PWM period to 0.5ms = 2 kHz. This will be used
mjr 98:4df3c0f7e707 6460 // for PWM channels on PWM units whose periods aren't changed
mjr 98:4df3c0f7e707 6461 // explicitly, so it'll apply to LW outputs assigned to GPIO pins.
mjr 98:4df3c0f7e707 6462 // The KL25Z only allows the period to be set at the TPM unit
mjr 94:0476b3e2b996 6463 // level, not per channel, so all channels on a given unit will
mjr 94:0476b3e2b996 6464 // necessarily use the same frequency. We (currently) have two
mjr 94:0476b3e2b996 6465 // subsystems that need specific PWM frequencies: TLC5940NT (which
mjr 94:0476b3e2b996 6466 // uses PWM to generate the grayscale clock signal) and IR remote
mjr 94:0476b3e2b996 6467 // (which uses PWM to generate the IR carrier signal). Since
mjr 94:0476b3e2b996 6468 // those require specific PWM frequencies, it's important to assign
mjr 94:0476b3e2b996 6469 // those to separate TPM units if both are in use simultaneously;
mjr 94:0476b3e2b996 6470 // the Config Tool includes checks to ensure that will happen when
mjr 94:0476b3e2b996 6471 // setting a config interactively. In addition, for the greatest
mjr 94:0476b3e2b996 6472 // flexibility, we take care NOT to assign explicit PWM frequencies
mjr 94:0476b3e2b996 6473 // to pins that don't require special frequences. That way, if a
mjr 94:0476b3e2b996 6474 // pin that doesn't need anything special happens to be sharing a
mjr 94:0476b3e2b996 6475 // TPM unit with a pin that does require a specific frequency, the
mjr 94:0476b3e2b996 6476 // two will co-exist peacefully on the TPM.
mjr 94:0476b3e2b996 6477 //
mjr 94:0476b3e2b996 6478 // We set this default first, before we create any PWM GPIOs, so
mjr 94:0476b3e2b996 6479 // that it will apply to all channels by default but won't override
mjr 94:0476b3e2b996 6480 // any channels that need specific frequences. Currently, the only
mjr 94:0476b3e2b996 6481 // frequency-agnostic PWM user is the LW outputs, so we can choose
mjr 94:0476b3e2b996 6482 // the default to be suitable for those. This is chosen to minimize
mjr 94:0476b3e2b996 6483 // flicker on attached LEDs.
mjr 94:0476b3e2b996 6484 NewPwmUnit::defaultPeriod = 0.0005f;
mjr 82:4f6209cb5c33 6485
mjr 76:7f5912b6340e 6486 // clear the I2C connection
mjr 35:e959ffba78fd 6487 clear_i2c();
mjr 82:4f6209cb5c33 6488
mjr 82:4f6209cb5c33 6489 // Elevate GPIO pin interrupt priorities, so that they can preempt
mjr 82:4f6209cb5c33 6490 // other interrupts. This is important for some external peripherals,
mjr 82:4f6209cb5c33 6491 // particularly the quadrature plunger sensors, which can generate
mjr 82:4f6209cb5c33 6492 // high-speed interrupts that need to be serviced quickly to keep
mjr 82:4f6209cb5c33 6493 // proper count of the quadrature position.
mjr 82:4f6209cb5c33 6494 FastInterruptIn::elevatePriority();
mjr 38:091e511ce8a0 6495
mjr 76:7f5912b6340e 6496 // Load the saved configuration. There are two sources of the
mjr 76:7f5912b6340e 6497 // configuration data:
mjr 76:7f5912b6340e 6498 //
mjr 76:7f5912b6340e 6499 // - Look for an NVM (flash non-volatile memory) configuration.
mjr 76:7f5912b6340e 6500 // If this is valid, we'll load it. The NVM is config data that can
mjr 76:7f5912b6340e 6501 // be updated dynamically by the host via USB commands and then stored
mjr 76:7f5912b6340e 6502 // in the flash by the firmware itself. If this exists, it supersedes
mjr 76:7f5912b6340e 6503 // any of the other settings stores. The Windows config tool uses this
mjr 76:7f5912b6340e 6504 // to store user settings updates.
mjr 76:7f5912b6340e 6505 //
mjr 76:7f5912b6340e 6506 // - If there's no NVM, we'll load the factory defaults, then we'll
mjr 76:7f5912b6340e 6507 // load any settings stored in the host-loaded configuration. The
mjr 76:7f5912b6340e 6508 // host can patch a set of configuration variable settings into the
mjr 76:7f5912b6340e 6509 // .bin file when loading new firmware, in the host-loaded config
mjr 76:7f5912b6340e 6510 // area that we reserve for this purpose. This allows the host to
mjr 76:7f5912b6340e 6511 // restore a configuration at the same time it installs firmware,
mjr 76:7f5912b6340e 6512 // without a separate download of the config data.
mjr 76:7f5912b6340e 6513 //
mjr 76:7f5912b6340e 6514 // The NVM supersedes the host-loaded config, since it can be updated
mjr 76:7f5912b6340e 6515 // between firmware updated and is thus presumably more recent if it's
mjr 76:7f5912b6340e 6516 // present. (Note that the NVM and host-loaded config are both in
mjr 76:7f5912b6340e 6517 // flash, so in principle we could just have a single NVM store that
mjr 76:7f5912b6340e 6518 // the host patches. The only reason we don't is that the NVM store
mjr 76:7f5912b6340e 6519 // is an image of our in-memory config structure, which is a native C
mjr 76:7f5912b6340e 6520 // struct, and we don't want the host to have to know the details of
mjr 76:7f5912b6340e 6521 // its byte layout, for obvious reasons. The host-loaded config, in
mjr 76:7f5912b6340e 6522 // contrast, uses the wire protocol format, which has a well-defined
mjr 76:7f5912b6340e 6523 // byte layout that's independent of the firmware version or the
mjr 76:7f5912b6340e 6524 // details of how the C compiler arranges the struct memory.)
mjr 76:7f5912b6340e 6525 if (!loadConfigFromFlash())
mjr 76:7f5912b6340e 6526 loadHostLoadedConfig();
mjr 35:e959ffba78fd 6527
mjr 38:091e511ce8a0 6528 // initialize the diagnostic LEDs
mjr 38:091e511ce8a0 6529 initDiagLEDs(cfg);
mjr 38:091e511ce8a0 6530
mjr 33:d832bcab089e 6531 // we're not connected/awake yet
mjr 33:d832bcab089e 6532 bool connected = false;
mjr 40:cc0d9814522b 6533 Timer connectChangeTimer;
mjr 33:d832bcab089e 6534
mjr 35:e959ffba78fd 6535 // create the plunger sensor interface
mjr 35:e959ffba78fd 6536 createPlunger();
mjr 76:7f5912b6340e 6537
mjr 76:7f5912b6340e 6538 // update the plunger reader's cached calibration data
mjr 76:7f5912b6340e 6539 plungerReader.onUpdateCal();
mjr 33:d832bcab089e 6540
mjr 60:f38da020aa13 6541 // set up the TLC5940 interface, if these chips are present
mjr 35:e959ffba78fd 6542 init_tlc5940(cfg);
mjr 34:6b981a2afab7 6543
mjr 87:8d35c74403af 6544 // initialize the TLC5916 interface, if these chips are present
mjr 87:8d35c74403af 6545 init_tlc59116(cfg);
mjr 87:8d35c74403af 6546
mjr 60:f38da020aa13 6547 // set up 74HC595 interface, if these chips are present
mjr 35:e959ffba78fd 6548 init_hc595(cfg);
mjr 6:cc35eb643e8f 6549
mjr 54:fd77a6b2f76c 6550 // Initialize the LedWiz ports. Note that the ordering here is important:
mjr 54:fd77a6b2f76c 6551 // this has to come after we create the TLC5940 and 74HC595 object instances
mjr 54:fd77a6b2f76c 6552 // (which we just did above), since we need to access those objects to set
mjr 54:fd77a6b2f76c 6553 // up ports assigned to the respective chips.
mjr 35:e959ffba78fd 6554 initLwOut(cfg);
mjr 48:058ace2aed1d 6555
mjr 60:f38da020aa13 6556 // start the TLC5940 refresh cycle clock
mjr 35:e959ffba78fd 6557 if (tlc5940 != 0)
mjr 35:e959ffba78fd 6558 tlc5940->start();
mjr 87:8d35c74403af 6559
mjr 77:0b96f6867312 6560 // Assume that nothing uses keyboard keys. We'll check for keyboard
mjr 77:0b96f6867312 6561 // usage when initializing the various subsystems that can send keys
mjr 77:0b96f6867312 6562 // (buttons, IR). If we find anything that does, we'll create the
mjr 77:0b96f6867312 6563 // USB keyboard interface.
mjr 77:0b96f6867312 6564 bool kbKeys = false;
mjr 77:0b96f6867312 6565
mjr 77:0b96f6867312 6566 // set up the IR remote control emitter & receiver, if present
mjr 77:0b96f6867312 6567 init_IR(cfg, kbKeys);
mjr 77:0b96f6867312 6568
mjr 77:0b96f6867312 6569 // start the power status time, if applicable
mjr 77:0b96f6867312 6570 startPowerStatusTimer(cfg);
mjr 48:058ace2aed1d 6571
mjr 35:e959ffba78fd 6572 // initialize the button input ports
mjr 35:e959ffba78fd 6573 initButtons(cfg, kbKeys);
mjr 38:091e511ce8a0 6574
mjr 60:f38da020aa13 6575 // Create the joystick USB client. Note that the USB vendor/product ID
mjr 60:f38da020aa13 6576 // information comes from the saved configuration. Also note that we have
mjr 60:f38da020aa13 6577 // to wait until after initializing the input buttons (which we just did
mjr 60:f38da020aa13 6578 // above) to set up the interface, since the button setup will determine
mjr 60:f38da020aa13 6579 // whether or not we need to present a USB keyboard interface in addition
mjr 60:f38da020aa13 6580 // to the joystick interface.
mjr 51:57eb311faafa 6581 MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false,
mjr 90:aa4e571da8e8 6582 cfg.joystickEnabled, cfg.joystickAxisFormat, kbKeys);
mjr 51:57eb311faafa 6583
mjr 101:755f44622abc 6584 // start the request timestamp timer
mjr 101:755f44622abc 6585 requestTimestamper.start();
mjr 101:755f44622abc 6586
mjr 60:f38da020aa13 6587 // Wait for the USB connection to start up. Show a distinctive diagnostic
mjr 60:f38da020aa13 6588 // flash pattern while waiting.
mjr 70:9f58735a1732 6589 Timer connTimeoutTimer, connFlashTimer;
mjr 70:9f58735a1732 6590 connTimeoutTimer.start();
mjr 70:9f58735a1732 6591 connFlashTimer.start();
mjr 51:57eb311faafa 6592 while (!js.configured())
mjr 51:57eb311faafa 6593 {
mjr 51:57eb311faafa 6594 // show one short yellow flash at 2-second intervals
mjr 70:9f58735a1732 6595 if (connFlashTimer.read_us() > 2000000)
mjr 51:57eb311faafa 6596 {
mjr 51:57eb311faafa 6597 // short yellow flash
mjr 51:57eb311faafa 6598 diagLED(1, 1, 0);
mjr 54:fd77a6b2f76c 6599 wait_us(50000);
mjr 51:57eb311faafa 6600 diagLED(0, 0, 0);
mjr 51:57eb311faafa 6601
mjr 51:57eb311faafa 6602 // reset the flash timer
mjr 70:9f58735a1732 6603 connFlashTimer.reset();
mjr 51:57eb311faafa 6604 }
mjr 70:9f58735a1732 6605
mjr 77:0b96f6867312 6606 // If we've been disconnected for more than the reboot timeout,
mjr 77:0b96f6867312 6607 // reboot. Some PCs won't reconnect if we were left plugged in
mjr 77:0b96f6867312 6608 // during a power cycle on the PC, but fortunately a reboot on
mjr 77:0b96f6867312 6609 // the KL25Z will make the host notice us and trigger a reconnect.
mjr 86:e30a1f60f783 6610 // Don't do this if we're in a non-recoverable PSU2 power state.
mjr 70:9f58735a1732 6611 if (cfg.disconnectRebootTimeout != 0
mjr 86:e30a1f60f783 6612 && connTimeoutTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 6613 && powerStatusAllowsReboot())
mjr 70:9f58735a1732 6614 reboot(js, false, 0);
mjr 77:0b96f6867312 6615
mjr 77:0b96f6867312 6616 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6617 powerStatusUpdate(cfg);
mjr 51:57eb311faafa 6618 }
mjr 60:f38da020aa13 6619
mjr 60:f38da020aa13 6620 // we're now connected to the host
mjr 54:fd77a6b2f76c 6621 connected = true;
mjr 40:cc0d9814522b 6622
mjr 92:f264fbaa1be5 6623 // Set up a timer for keeping track of how long it's been since we
mjr 92:f264fbaa1be5 6624 // sent the last joystick report. We use this to determine when it's
mjr 92:f264fbaa1be5 6625 // time to send the next joystick report.
mjr 92:f264fbaa1be5 6626 //
mjr 92:f264fbaa1be5 6627 // We have to use a timer for two reasons. The first is that our main
mjr 92:f264fbaa1be5 6628 // loop runs too fast (about .25ms to 2.5ms per loop, depending on the
mjr 92:f264fbaa1be5 6629 // type of plunger sensor attached and other factors) for us to send
mjr 92:f264fbaa1be5 6630 // joystick reports on every iteration. We *could*, but the PC couldn't
mjr 92:f264fbaa1be5 6631 // digest them at that pace. So we need to slow down the reports to a
mjr 92:f264fbaa1be5 6632 // reasonable pace. The second is that VP has some complicated timing
mjr 92:f264fbaa1be5 6633 // issues of its own, so we not only need to slow down the reports from
mjr 92:f264fbaa1be5 6634 // our "natural" pace, but also time them to sync up with VP's input
mjr 92:f264fbaa1be5 6635 // sampling rate as best we can.
mjr 38:091e511ce8a0 6636 Timer jsReportTimer;
mjr 38:091e511ce8a0 6637 jsReportTimer.start();
mjr 38:091e511ce8a0 6638
mjr 92:f264fbaa1be5 6639 // Accelerometer sample "stutter" counter. Each time we send a joystick
mjr 92:f264fbaa1be5 6640 // report, we increment this counter, and check to see if it has reached
mjr 92:f264fbaa1be5 6641 // the threshold set in the configuration. If so, we take a new
mjr 92:f264fbaa1be5 6642 // accelerometer sample and send it with the new joystick report. It
mjr 92:f264fbaa1be5 6643 // not, we don't take a new sample, but simply repeat the last sample.
mjr 92:f264fbaa1be5 6644 //
mjr 92:f264fbaa1be5 6645 // This lets us send joystick reports more frequently than accelerometer
mjr 92:f264fbaa1be5 6646 // samples. The point is to let us slow down accelerometer reports to
mjr 92:f264fbaa1be5 6647 // a pace that matches VP's input sampling frequency, while still sending
mjr 92:f264fbaa1be5 6648 // joystick button updates more frequently, so that other programs that
mjr 92:f264fbaa1be5 6649 // can read input faster will see button changes with less latency.
mjr 92:f264fbaa1be5 6650 int jsAccelStutterCounter = 0;
mjr 92:f264fbaa1be5 6651
mjr 92:f264fbaa1be5 6652 // Last accelerometer report, in joystick units. We normally report the
mjr 92:f264fbaa1be5 6653 // acceleromter reading via the joystick X and Y axes, per the VP
mjr 92:f264fbaa1be5 6654 // convention. We can alternatively report in the RX and RY axes; this
mjr 92:f264fbaa1be5 6655 // can be set in the configuration.
mjr 92:f264fbaa1be5 6656 int x = 0, y = 0;
mjr 92:f264fbaa1be5 6657
mjr 60:f38da020aa13 6658 // Time since we successfully sent a USB report. This is a hacky
mjr 60:f38da020aa13 6659 // workaround to deal with any remaining sporadic problems in the USB
mjr 60:f38da020aa13 6660 // stack. I've been trying to bulletproof the USB code over time to
mjr 60:f38da020aa13 6661 // remove all such problems at their source, but it seems unlikely that
mjr 60:f38da020aa13 6662 // we'll ever get them all. Thus this hack. The idea here is that if
mjr 60:f38da020aa13 6663 // we go too long without successfully sending a USB report, we'll
mjr 60:f38da020aa13 6664 // assume that the connection is broken (and the KL25Z USB hardware
mjr 60:f38da020aa13 6665 // hasn't noticed this), and we'll try taking measures to recover.
mjr 38:091e511ce8a0 6666 Timer jsOKTimer;
mjr 38:091e511ce8a0 6667 jsOKTimer.start();
mjr 35:e959ffba78fd 6668
mjr 55:4db125cd11a0 6669 // Initialize the calibration button and lamp, if enabled. To be enabled,
mjr 55:4db125cd11a0 6670 // the pin has to be assigned to something other than NC (0xFF), AND the
mjr 55:4db125cd11a0 6671 // corresponding feature enable flag has to be set.
mjr 55:4db125cd11a0 6672 DigitalIn *calBtn = 0;
mjr 55:4db125cd11a0 6673 DigitalOut *calBtnLed = 0;
mjr 55:4db125cd11a0 6674
mjr 55:4db125cd11a0 6675 // calibration button input - feature flag 0x01
mjr 55:4db125cd11a0 6676 if ((cfg.plunger.cal.features & 0x01) && cfg.plunger.cal.btn != 0xFF)
mjr 55:4db125cd11a0 6677 calBtn = new DigitalIn(wirePinName(cfg.plunger.cal.btn));
mjr 55:4db125cd11a0 6678
mjr 55:4db125cd11a0 6679 // calibration button indicator lamp output - feature flag 0x02
mjr 55:4db125cd11a0 6680 if ((cfg.plunger.cal.features & 0x02) && cfg.plunger.cal.led != 0xFF)
mjr 55:4db125cd11a0 6681 calBtnLed = new DigitalOut(wirePinName(cfg.plunger.cal.led));
mjr 6:cc35eb643e8f 6682
mjr 35:e959ffba78fd 6683 // initialize the calibration button
mjr 1:d913e0afb2ac 6684 calBtnTimer.start();
mjr 35:e959ffba78fd 6685 calBtnState = 0;
mjr 1:d913e0afb2ac 6686
mjr 1:d913e0afb2ac 6687 // set up a timer for our heartbeat indicator
mjr 1:d913e0afb2ac 6688 Timer hbTimer;
mjr 1:d913e0afb2ac 6689 hbTimer.start();
mjr 1:d913e0afb2ac 6690 int hb = 0;
mjr 5:a70c0bce770d 6691 uint16_t hbcnt = 0;
mjr 1:d913e0afb2ac 6692
mjr 1:d913e0afb2ac 6693 // set a timer for accelerometer auto-centering
mjr 1:d913e0afb2ac 6694 Timer acTimer;
mjr 1:d913e0afb2ac 6695 acTimer.start();
mjr 1:d913e0afb2ac 6696
mjr 0:5acbbe3f4cf4 6697 // create the accelerometer object
mjr 77:0b96f6867312 6698 Accel accel(MMA8451_SCL_PIN, MMA8451_SDA_PIN, MMA8451_I2C_ADDRESS,
mjr 78:1e00b3fa11af 6699 MMA8451_INT_PIN, cfg.accel.range, cfg.accel.autoCenterTime);
mjr 76:7f5912b6340e 6700
mjr 48:058ace2aed1d 6701 // initialize the plunger sensor
mjr 35:e959ffba78fd 6702 plungerSensor->init();
mjr 10:976666ffa4ef 6703
mjr 48:058ace2aed1d 6704 // set up the ZB Launch Ball monitor
mjr 48:058ace2aed1d 6705 ZBLaunchBall zbLaunchBall;
mjr 48:058ace2aed1d 6706
mjr 54:fd77a6b2f76c 6707 // enable the peripheral chips
mjr 54:fd77a6b2f76c 6708 if (tlc5940 != 0)
mjr 54:fd77a6b2f76c 6709 tlc5940->enable(true);
mjr 54:fd77a6b2f76c 6710 if (hc595 != 0)
mjr 54:fd77a6b2f76c 6711 hc595->enable(true);
mjr 87:8d35c74403af 6712 if (tlc59116 != 0)
mjr 87:8d35c74403af 6713 tlc59116->enable(true);
mjr 74:822a92bc11d2 6714
mjr 76:7f5912b6340e 6715 // start the LedWiz flash cycle timer
mjr 74:822a92bc11d2 6716 wizCycleTimer.start();
mjr 74:822a92bc11d2 6717
mjr 74:822a92bc11d2 6718 // start the PWM update polling timer
mjr 74:822a92bc11d2 6719 polledPwmTimer.start();
mjr 43:7a6364d82a41 6720
mjr 1:d913e0afb2ac 6721 // we're all set up - now just loop, processing sensor reports and
mjr 1:d913e0afb2ac 6722 // host requests
mjr 0:5acbbe3f4cf4 6723 for (;;)
mjr 0:5acbbe3f4cf4 6724 {
mjr 74:822a92bc11d2 6725 // start the main loop timer for diagnostic data collection
mjr 76:7f5912b6340e 6726 IF_DIAG(mainLoopTimer.reset(); mainLoopTimer.start();)
mjr 96:68d5621ff49f 6727
mjr 48:058ace2aed1d 6728 // Process incoming reports on the joystick interface. The joystick
mjr 48:058ace2aed1d 6729 // "out" (receive) endpoint is used for LedWiz commands and our
mjr 48:058ace2aed1d 6730 // extended protocol commands. Limit processing time to 5ms to
mjr 48:058ace2aed1d 6731 // ensure we don't starve the input side.
mjr 39:b3815a1c3802 6732 LedWizMsg lwm;
mjr 48:058ace2aed1d 6733 Timer lwt;
mjr 48:058ace2aed1d 6734 lwt.start();
mjr 77:0b96f6867312 6735 IF_DIAG(int msgCount = 0;)
mjr 48:058ace2aed1d 6736 while (js.readLedWizMsg(lwm) && lwt.read_us() < 5000)
mjr 74:822a92bc11d2 6737 {
mjr 78:1e00b3fa11af 6738 handleInputMsg(lwm, js, accel);
mjr 74:822a92bc11d2 6739 IF_DIAG(++msgCount;)
mjr 74:822a92bc11d2 6740 }
mjr 74:822a92bc11d2 6741
mjr 74:822a92bc11d2 6742 // collect performance statistics on the message reader, if desired
mjr 74:822a92bc11d2 6743 IF_DIAG(
mjr 74:822a92bc11d2 6744 if (msgCount != 0)
mjr 74:822a92bc11d2 6745 {
mjr 76:7f5912b6340e 6746 mainLoopMsgTime += lwt.read_us();
mjr 74:822a92bc11d2 6747 mainLoopMsgCount++;
mjr 74:822a92bc11d2 6748 }
mjr 74:822a92bc11d2 6749 )
mjr 74:822a92bc11d2 6750
mjr 77:0b96f6867312 6751 // process IR input
mjr 77:0b96f6867312 6752 process_IR(cfg, js);
mjr 77:0b96f6867312 6753
mjr 77:0b96f6867312 6754 // update the PSU2 power sensing status
mjr 77:0b96f6867312 6755 powerStatusUpdate(cfg);
mjr 77:0b96f6867312 6756
mjr 74:822a92bc11d2 6757 // update flashing LedWiz outputs periodically
mjr 74:822a92bc11d2 6758 wizPulse();
mjr 74:822a92bc11d2 6759
mjr 74:822a92bc11d2 6760 // update PWM outputs
mjr 74:822a92bc11d2 6761 pollPwmUpdates();
mjr 77:0b96f6867312 6762
mjr 99:8139b0c274f4 6763 // update Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 6764 LwFlipperLogicOut::poll();
mjr 99:8139b0c274f4 6765 LwChimeLogicOut::poll();
mjr 89:c43cd923401c 6766
mjr 77:0b96f6867312 6767 // poll the accelerometer
mjr 77:0b96f6867312 6768 accel.poll();
mjr 55:4db125cd11a0 6769
mjr 96:68d5621ff49f 6770 // Note the "effective" plunger enabled status. This has two
mjr 96:68d5621ff49f 6771 // components: the explicit "enabled" bit, and the plunger sensor
mjr 96:68d5621ff49f 6772 // type setting. For most purposes, a plunger type of NONE is
mjr 96:68d5621ff49f 6773 // equivalent to disabled. Set this to explicit 0x01 or 0x00
mjr 96:68d5621ff49f 6774 // so that we can OR the bit into status reports.
mjr 96:68d5621ff49f 6775 uint8_t effectivePlungerEnabled = (cfg.plunger.enabled
mjr 96:68d5621ff49f 6776 && cfg.plunger.sensorType != PlungerType_None) ? 0x01 : 0x00;
mjr 96:68d5621ff49f 6777
mjr 76:7f5912b6340e 6778 // collect diagnostic statistics, checkpoint 0
mjr 76:7f5912b6340e 6779 IF_DIAG(mainLoopIterCheckpt[0] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6780
mjr 55:4db125cd11a0 6781 // send TLC5940 data updates if applicable
mjr 55:4db125cd11a0 6782 if (tlc5940 != 0)
mjr 55:4db125cd11a0 6783 tlc5940->send();
mjr 87:8d35c74403af 6784
mjr 87:8d35c74403af 6785 // send TLC59116 data updates
mjr 87:8d35c74403af 6786 if (tlc59116 != 0)
mjr 87:8d35c74403af 6787 tlc59116->send();
mjr 1:d913e0afb2ac 6788
mjr 76:7f5912b6340e 6789 // collect diagnostic statistics, checkpoint 1
mjr 76:7f5912b6340e 6790 IF_DIAG(mainLoopIterCheckpt[1] += mainLoopTimer.read_us();)
mjr 77:0b96f6867312 6791
mjr 1:d913e0afb2ac 6792 // check for plunger calibration
mjr 17:ab3cec0c8bf4 6793 if (calBtn != 0 && !calBtn->read())
mjr 0:5acbbe3f4cf4 6794 {
mjr 1:d913e0afb2ac 6795 // check the state
mjr 1:d913e0afb2ac 6796 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6797 {
mjr 1:d913e0afb2ac 6798 case 0:
mjr 1:d913e0afb2ac 6799 // button not yet pushed - start debouncing
mjr 1:d913e0afb2ac 6800 calBtnTimer.reset();
mjr 1:d913e0afb2ac 6801 calBtnState = 1;
mjr 1:d913e0afb2ac 6802 break;
mjr 1:d913e0afb2ac 6803
mjr 1:d913e0afb2ac 6804 case 1:
mjr 1:d913e0afb2ac 6805 // pushed, not yet debounced - if the debounce time has
mjr 1:d913e0afb2ac 6806 // passed, start the hold period
mjr 48:058ace2aed1d 6807 if (calBtnTimer.read_us() > 50000)
mjr 1:d913e0afb2ac 6808 calBtnState = 2;
mjr 1:d913e0afb2ac 6809 break;
mjr 1:d913e0afb2ac 6810
mjr 1:d913e0afb2ac 6811 case 2:
mjr 1:d913e0afb2ac 6812 // in the hold period - if the button has been held down
mjr 1:d913e0afb2ac 6813 // for the entire hold period, move to calibration mode
mjr 48:058ace2aed1d 6814 if (calBtnTimer.read_us() > 2050000)
mjr 1:d913e0afb2ac 6815 {
mjr 1:d913e0afb2ac 6816 // enter calibration mode
mjr 1:d913e0afb2ac 6817 calBtnState = 3;
mjr 9:fd65b0a94720 6818 calBtnTimer.reset();
mjr 35:e959ffba78fd 6819
mjr 44:b5ac89b9cd5d 6820 // begin the plunger calibration limits
mjr 52:8298b2a73eb2 6821 plungerReader.setCalMode(true);
mjr 1:d913e0afb2ac 6822 }
mjr 1:d913e0afb2ac 6823 break;
mjr 2:c174f9ee414a 6824
mjr 2:c174f9ee414a 6825 case 3:
mjr 9:fd65b0a94720 6826 // Already in calibration mode - pushing the button here
mjr 9:fd65b0a94720 6827 // doesn't change the current state, but we won't leave this
mjr 9:fd65b0a94720 6828 // state as long as it's held down. So nothing changes here.
mjr 2:c174f9ee414a 6829 break;
mjr 0:5acbbe3f4cf4 6830 }
mjr 0:5acbbe3f4cf4 6831 }
mjr 1:d913e0afb2ac 6832 else
mjr 1:d913e0afb2ac 6833 {
mjr 2:c174f9ee414a 6834 // Button released. If we're in calibration mode, and
mjr 2:c174f9ee414a 6835 // the calibration time has elapsed, end the calibration
mjr 2:c174f9ee414a 6836 // and save the results to flash.
mjr 2:c174f9ee414a 6837 //
mjr 2:c174f9ee414a 6838 // Otherwise, return to the base state without saving anything.
mjr 2:c174f9ee414a 6839 // If the button is released before we make it to calibration
mjr 2:c174f9ee414a 6840 // mode, it simply cancels the attempt.
mjr 48:058ace2aed1d 6841 if (calBtnState == 3 && calBtnTimer.read_us() > 15000000)
mjr 2:c174f9ee414a 6842 {
mjr 2:c174f9ee414a 6843 // exit calibration mode
mjr 1:d913e0afb2ac 6844 calBtnState = 0;
mjr 52:8298b2a73eb2 6845 plungerReader.setCalMode(false);
mjr 2:c174f9ee414a 6846
mjr 6:cc35eb643e8f 6847 // save the updated configuration
mjr 35:e959ffba78fd 6848 cfg.plunger.cal.calibrated = 1;
mjr 86:e30a1f60f783 6849 saveConfigToFlash(0, false);
mjr 2:c174f9ee414a 6850 }
mjr 2:c174f9ee414a 6851 else if (calBtnState != 3)
mjr 2:c174f9ee414a 6852 {
mjr 2:c174f9ee414a 6853 // didn't make it to calibration mode - cancel the operation
mjr 1:d913e0afb2ac 6854 calBtnState = 0;
mjr 2:c174f9ee414a 6855 }
mjr 1:d913e0afb2ac 6856 }
mjr 1:d913e0afb2ac 6857
mjr 1:d913e0afb2ac 6858 // light/flash the calibration button light, if applicable
mjr 1:d913e0afb2ac 6859 int newCalBtnLit = calBtnLit;
mjr 1:d913e0afb2ac 6860 switch (calBtnState)
mjr 0:5acbbe3f4cf4 6861 {
mjr 1:d913e0afb2ac 6862 case 2:
mjr 1:d913e0afb2ac 6863 // in the hold period - flash the light
mjr 48:058ace2aed1d 6864 newCalBtnLit = ((calBtnTimer.read_us()/250000) & 1);
mjr 1:d913e0afb2ac 6865 break;
mjr 1:d913e0afb2ac 6866
mjr 1:d913e0afb2ac 6867 case 3:
mjr 1:d913e0afb2ac 6868 // calibration mode - show steady on
mjr 1:d913e0afb2ac 6869 newCalBtnLit = true;
mjr 1:d913e0afb2ac 6870 break;
mjr 1:d913e0afb2ac 6871
mjr 1:d913e0afb2ac 6872 default:
mjr 1:d913e0afb2ac 6873 // not calibrating/holding - show steady off
mjr 1:d913e0afb2ac 6874 newCalBtnLit = false;
mjr 1:d913e0afb2ac 6875 break;
mjr 1:d913e0afb2ac 6876 }
mjr 3:3514575d4f86 6877
mjr 3:3514575d4f86 6878 // light or flash the external calibration button LED, and
mjr 3:3514575d4f86 6879 // do the same with the on-board blue LED
mjr 1:d913e0afb2ac 6880 if (calBtnLit != newCalBtnLit)
mjr 1:d913e0afb2ac 6881 {
mjr 1:d913e0afb2ac 6882 calBtnLit = newCalBtnLit;
mjr 2:c174f9ee414a 6883 if (calBtnLit) {
mjr 17:ab3cec0c8bf4 6884 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6885 calBtnLed->write(1);
mjr 38:091e511ce8a0 6886 diagLED(0, 0, 1); // blue
mjr 2:c174f9ee414a 6887 }
mjr 2:c174f9ee414a 6888 else {
mjr 17:ab3cec0c8bf4 6889 if (calBtnLed != 0)
mjr 17:ab3cec0c8bf4 6890 calBtnLed->write(0);
mjr 38:091e511ce8a0 6891 diagLED(0, 0, 0); // off
mjr 2:c174f9ee414a 6892 }
mjr 1:d913e0afb2ac 6893 }
mjr 35:e959ffba78fd 6894
mjr 76:7f5912b6340e 6895 // collect diagnostic statistics, checkpoint 2
mjr 76:7f5912b6340e 6896 IF_DIAG(mainLoopIterCheckpt[2] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6897
mjr 48:058ace2aed1d 6898 // read the plunger sensor
mjr 48:058ace2aed1d 6899 plungerReader.read();
mjr 48:058ace2aed1d 6900
mjr 76:7f5912b6340e 6901 // collect diagnostic statistics, checkpoint 3
mjr 76:7f5912b6340e 6902 IF_DIAG(mainLoopIterCheckpt[3] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6903
mjr 53:9b2611964afc 6904 // update the ZB Launch Ball status
mjr 53:9b2611964afc 6905 zbLaunchBall.update();
mjr 37:ed52738445fc 6906
mjr 76:7f5912b6340e 6907 // collect diagnostic statistics, checkpoint 4
mjr 76:7f5912b6340e 6908 IF_DIAG(mainLoopIterCheckpt[4] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6909
mjr 53:9b2611964afc 6910 // process button updates
mjr 53:9b2611964afc 6911 processButtons(cfg);
mjr 53:9b2611964afc 6912
mjr 76:7f5912b6340e 6913 // collect diagnostic statistics, checkpoint 5
mjr 76:7f5912b6340e 6914 IF_DIAG(mainLoopIterCheckpt[5] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6915
mjr 38:091e511ce8a0 6916 // send a keyboard report if we have new data
mjr 37:ed52738445fc 6917 if (kbState.changed)
mjr 37:ed52738445fc 6918 {
mjr 38:091e511ce8a0 6919 // send a keyboard report
mjr 37:ed52738445fc 6920 js.kbUpdate(kbState.data);
mjr 37:ed52738445fc 6921 kbState.changed = false;
mjr 37:ed52738445fc 6922 }
mjr 38:091e511ce8a0 6923
mjr 38:091e511ce8a0 6924 // likewise for the media controller
mjr 37:ed52738445fc 6925 if (mediaState.changed)
mjr 37:ed52738445fc 6926 {
mjr 38:091e511ce8a0 6927 // send a media report
mjr 37:ed52738445fc 6928 js.mediaUpdate(mediaState.data);
mjr 37:ed52738445fc 6929 mediaState.changed = false;
mjr 37:ed52738445fc 6930 }
mjr 38:091e511ce8a0 6931
mjr 76:7f5912b6340e 6932 // collect diagnostic statistics, checkpoint 6
mjr 76:7f5912b6340e 6933 IF_DIAG(mainLoopIterCheckpt[6] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 6934
mjr 38:091e511ce8a0 6935 // flag: did we successfully send a joystick report on this round?
mjr 38:091e511ce8a0 6936 bool jsOK = false;
mjr 55:4db125cd11a0 6937
mjr 55:4db125cd11a0 6938 // figure the current status flags for joystick reports
mjr 77:0b96f6867312 6939 uint16_t statusFlags =
mjr 96:68d5621ff49f 6940 effectivePlungerEnabled // 0x01
mjr 77:0b96f6867312 6941 | nightMode // 0x02
mjr 79:682ae3171a08 6942 | ((psu2_state & 0x07) << 2) // 0x04 0x08 0x10
mjr 79:682ae3171a08 6943 | saveConfigSucceededFlag; // 0x40
mjr 77:0b96f6867312 6944 if (IRLearningMode != 0)
mjr 77:0b96f6867312 6945 statusFlags |= 0x20;
mjr 17:ab3cec0c8bf4 6946
mjr 50:40015764bbe6 6947 // If it's been long enough since our last USB status report, send
mjr 50:40015764bbe6 6948 // the new report. VP only polls for input in 10ms intervals, so
mjr 50:40015764bbe6 6949 // there's no benefit in sending reports more frequently than this.
mjr 50:40015764bbe6 6950 // More frequent reporting would only add USB I/O overhead.
mjr 92:f264fbaa1be5 6951 if (cfg.joystickEnabled && jsReportTimer.read_us() > cfg.jsReportInterval_us)
mjr 17:ab3cec0c8bf4 6952 {
mjr 92:f264fbaa1be5 6953 // Increment the "stutter" counter. If it has reached the
mjr 92:f264fbaa1be5 6954 // stutter threshold, read a new accelerometer sample. If
mjr 92:f264fbaa1be5 6955 // not, repeat the last sample.
mjr 92:f264fbaa1be5 6956 if (++jsAccelStutterCounter >= cfg.accel.stutter)
mjr 92:f264fbaa1be5 6957 {
mjr 92:f264fbaa1be5 6958 // read the accelerometer
mjr 92:f264fbaa1be5 6959 int xa, ya;
mjr 92:f264fbaa1be5 6960 accel.get(xa, ya);
mjr 17:ab3cec0c8bf4 6961
mjr 92:f264fbaa1be5 6962 // confine the results to our joystick axis range
mjr 92:f264fbaa1be5 6963 if (xa < -JOYMAX) xa = -JOYMAX;
mjr 92:f264fbaa1be5 6964 if (xa > JOYMAX) xa = JOYMAX;
mjr 92:f264fbaa1be5 6965 if (ya < -JOYMAX) ya = -JOYMAX;
mjr 92:f264fbaa1be5 6966 if (ya > JOYMAX) ya = JOYMAX;
mjr 92:f264fbaa1be5 6967
mjr 92:f264fbaa1be5 6968 // store the updated accelerometer coordinates
mjr 92:f264fbaa1be5 6969 x = xa;
mjr 92:f264fbaa1be5 6970 y = ya;
mjr 92:f264fbaa1be5 6971
mjr 95:8eca8acbb82c 6972 // rotate X and Y according to the device orientation in the cabinet
mjr 95:8eca8acbb82c 6973 accelRotate(x, y);
mjr 95:8eca8acbb82c 6974
mjr 92:f264fbaa1be5 6975 // reset the stutter counter
mjr 92:f264fbaa1be5 6976 jsAccelStutterCounter = 0;
mjr 92:f264fbaa1be5 6977 }
mjr 17:ab3cec0c8bf4 6978
mjr 48:058ace2aed1d 6979 // Report the current plunger position unless the plunger is
mjr 48:058ace2aed1d 6980 // disabled, or the ZB Launch Ball signal is on. In either of
mjr 48:058ace2aed1d 6981 // those cases, just report a constant 0 value. ZB Launch Ball
mjr 48:058ace2aed1d 6982 // temporarily disables mechanical plunger reporting because it
mjr 21:5048e16cc9ef 6983 // tells us that the table has a Launch Ball button instead of
mjr 48:058ace2aed1d 6984 // a traditional plunger, so we don't want to confuse VP with
mjr 48:058ace2aed1d 6985 // regular plunger inputs.
mjr 92:f264fbaa1be5 6986 int zActual = plungerReader.getPosition();
mjr 96:68d5621ff49f 6987 int zReported = (!effectivePlungerEnabled || zbLaunchOn ? 0 : zActual);
mjr 35:e959ffba78fd 6988
mjr 35:e959ffba78fd 6989 // send the joystick report
mjr 92:f264fbaa1be5 6990 jsOK = js.update(x, y, zReported, jsButtons, statusFlags);
mjr 21:5048e16cc9ef 6991
mjr 17:ab3cec0c8bf4 6992 // we've just started a new report interval, so reset the timer
mjr 38:091e511ce8a0 6993 jsReportTimer.reset();
mjr 17:ab3cec0c8bf4 6994 }
mjr 21:5048e16cc9ef 6995
mjr 52:8298b2a73eb2 6996 // If we're in sensor status mode, report all pixel exposure values
mjr 101:755f44622abc 6997 if (reportPlungerStat && plungerSensor->ready())
mjr 10:976666ffa4ef 6998 {
mjr 17:ab3cec0c8bf4 6999 // send the report
mjr 101:755f44622abc 7000 plungerSensor->sendStatusReport(js, reportPlungerStatFlags);
mjr 17:ab3cec0c8bf4 7001
mjr 10:976666ffa4ef 7002 // we have satisfied this request
mjr 52:8298b2a73eb2 7003 reportPlungerStat = false;
mjr 10:976666ffa4ef 7004 }
mjr 10:976666ffa4ef 7005
mjr 101:755f44622abc 7006 // Reset the plunger status report extra timer after enough time has
mjr 101:755f44622abc 7007 // elapsed to satisfy the request. We don't just do this immediately
mjr 101:755f44622abc 7008 // because of the complexities of the pixel frame buffer pipelines in
mjr 101:755f44622abc 7009 // most of the image sensors. The pipelines delay the effect of the
mjr 101:755f44622abc 7010 // exposure time request by a couple of frames, so we can't be sure
mjr 101:755f44622abc 7011 // exactly when they're applied - meaning we can't consider the
mjr 101:755f44622abc 7012 // delay time to be consumed after a fixed number of frames. Instead,
mjr 101:755f44622abc 7013 // we'll consider it consumed after a long enough time to be sure
mjr 101:755f44622abc 7014 // we've sent a few frames. The extra time value is meant to be an
mjr 101:755f44622abc 7015 // interactive tool for debugging, so it's not important to reset it
mjr 101:755f44622abc 7016 // immediately - the user will probably want to see the effect over
mjr 101:755f44622abc 7017 // many frames, so they're likely to keep sending requests with the
mjr 101:755f44622abc 7018 // time value over and over. They'll eventually shut down the frame
mjr 101:755f44622abc 7019 // viewer and return to normal operation, at which point the requests
mjr 101:755f44622abc 7020 // will stop. So we just have to clear things out after we haven't
mjr 101:755f44622abc 7021 // seen a request with extra time for a little while.
mjr 101:755f44622abc 7022 if (reportPlungerStatTime != 0
mjr 101:755f44622abc 7023 && static_cast<uint32_t>(requestTimestamper.read_us() - tReportPlungerStat) > 1000000)
mjr 101:755f44622abc 7024 {
mjr 101:755f44622abc 7025 reportPlungerStatTime = 0;
mjr 101:755f44622abc 7026 plungerSensor->setExtraIntegrationTime(0);
mjr 101:755f44622abc 7027 }
mjr 101:755f44622abc 7028
mjr 35:e959ffba78fd 7029 // If joystick reports are turned off, send a generic status report
mjr 35:e959ffba78fd 7030 // periodically for the sake of the Windows config tool.
mjr 77:0b96f6867312 7031 if (!cfg.joystickEnabled && jsReportTimer.read_us() > 10000UL)
mjr 21:5048e16cc9ef 7032 {
mjr 55:4db125cd11a0 7033 jsOK = js.updateStatus(statusFlags);
mjr 38:091e511ce8a0 7034 jsReportTimer.reset();
mjr 38:091e511ce8a0 7035 }
mjr 38:091e511ce8a0 7036
mjr 38:091e511ce8a0 7037 // if we successfully sent a joystick report, reset the watchdog timer
mjr 38:091e511ce8a0 7038 if (jsOK)
mjr 38:091e511ce8a0 7039 {
mjr 38:091e511ce8a0 7040 jsOKTimer.reset();
mjr 38:091e511ce8a0 7041 jsOKTimer.start();
mjr 21:5048e16cc9ef 7042 }
mjr 21:5048e16cc9ef 7043
mjr 76:7f5912b6340e 7044 // collect diagnostic statistics, checkpoint 7
mjr 76:7f5912b6340e 7045 IF_DIAG(mainLoopIterCheckpt[7] += mainLoopTimer.read_us();)
mjr 76:7f5912b6340e 7046
mjr 6:cc35eb643e8f 7047 #ifdef DEBUG_PRINTF
mjr 6:cc35eb643e8f 7048 if (x != 0 || y != 0)
mjr 6:cc35eb643e8f 7049 printf("%d,%d\r\n", x, y);
mjr 6:cc35eb643e8f 7050 #endif
mjr 6:cc35eb643e8f 7051
mjr 33:d832bcab089e 7052 // check for connection status changes
mjr 54:fd77a6b2f76c 7053 bool newConnected = js.isConnected() && !js.isSleeping();
mjr 33:d832bcab089e 7054 if (newConnected != connected)
mjr 33:d832bcab089e 7055 {
mjr 54:fd77a6b2f76c 7056 // give it a moment to stabilize
mjr 40:cc0d9814522b 7057 connectChangeTimer.start();
mjr 55:4db125cd11a0 7058 if (connectChangeTimer.read_us() > 1000000)
mjr 33:d832bcab089e 7059 {
mjr 33:d832bcab089e 7060 // note the new status
mjr 33:d832bcab089e 7061 connected = newConnected;
mjr 40:cc0d9814522b 7062
mjr 40:cc0d9814522b 7063 // done with the change timer for this round - reset it for next time
mjr 40:cc0d9814522b 7064 connectChangeTimer.stop();
mjr 40:cc0d9814522b 7065 connectChangeTimer.reset();
mjr 33:d832bcab089e 7066
mjr 54:fd77a6b2f76c 7067 // if we're newly disconnected, clean up for PC suspend mode or power off
mjr 54:fd77a6b2f76c 7068 if (!connected)
mjr 40:cc0d9814522b 7069 {
mjr 54:fd77a6b2f76c 7070 // turn off all outputs
mjr 33:d832bcab089e 7071 allOutputsOff();
mjr 40:cc0d9814522b 7072
mjr 40:cc0d9814522b 7073 // The KL25Z runs off of USB power, so we might (depending on the PC
mjr 40:cc0d9814522b 7074 // and OS configuration) continue to receive power even when the main
mjr 40:cc0d9814522b 7075 // PC power supply is turned off, such as in soft-off or suspend/sleep
mjr 40:cc0d9814522b 7076 // mode. Any external output controller chips (TLC5940, 74HC595) might
mjr 40:cc0d9814522b 7077 // be powered from the PC power supply directly rather than from our
mjr 40:cc0d9814522b 7078 // USB power, so they might be powered off even when we're still running.
mjr 40:cc0d9814522b 7079 // To ensure cleaner startup when the power comes back on, globally
mjr 40:cc0d9814522b 7080 // disable the outputs. The global disable signals come from GPIO lines
mjr 40:cc0d9814522b 7081 // that remain powered as long as the KL25Z is powered, so these modes
mjr 40:cc0d9814522b 7082 // will apply smoothly across power state transitions in the external
mjr 40:cc0d9814522b 7083 // hardware. That is, when the external chips are powered up, they'll
mjr 40:cc0d9814522b 7084 // see the global disable signals as stable voltage inputs immediately,
mjr 40:cc0d9814522b 7085 // which will cause them to suppress any output triggering. This ensures
mjr 40:cc0d9814522b 7086 // that we don't fire any solenoids or flash any lights spuriously when
mjr 40:cc0d9814522b 7087 // the power first comes on.
mjr 40:cc0d9814522b 7088 if (tlc5940 != 0)
mjr 40:cc0d9814522b 7089 tlc5940->enable(false);
mjr 87:8d35c74403af 7090 if (tlc59116 != 0)
mjr 87:8d35c74403af 7091 tlc59116->enable(false);
mjr 40:cc0d9814522b 7092 if (hc595 != 0)
mjr 40:cc0d9814522b 7093 hc595->enable(false);
mjr 40:cc0d9814522b 7094 }
mjr 33:d832bcab089e 7095 }
mjr 33:d832bcab089e 7096 }
mjr 48:058ace2aed1d 7097
mjr 53:9b2611964afc 7098 // if we have a reboot timer pending, check for completion
mjr 86:e30a1f60f783 7099 if (saveConfigFollowupTimer.isRunning()
mjr 87:8d35c74403af 7100 && saveConfigFollowupTimer.read_us() > saveConfigFollowupTime*1000000UL)
mjr 85:3c28aee81cde 7101 {
mjr 85:3c28aee81cde 7102 // if a reboot is pending, execute it now
mjr 86:e30a1f60f783 7103 if (saveConfigRebootPending)
mjr 82:4f6209cb5c33 7104 {
mjr 86:e30a1f60f783 7105 // Only reboot if the PSU2 power state allows it. If it
mjr 86:e30a1f60f783 7106 // doesn't, suppress the reboot for now, but leave the boot
mjr 86:e30a1f60f783 7107 // flags set so that we keep checking on future rounds.
mjr 86:e30a1f60f783 7108 // That way we should eventually reboot when the power
mjr 86:e30a1f60f783 7109 // status allows it.
mjr 86:e30a1f60f783 7110 if (powerStatusAllowsReboot())
mjr 86:e30a1f60f783 7111 reboot(js);
mjr 82:4f6209cb5c33 7112 }
mjr 85:3c28aee81cde 7113 else
mjr 85:3c28aee81cde 7114 {
mjr 86:e30a1f60f783 7115 // No reboot required. Exit the timed post-save state.
mjr 86:e30a1f60f783 7116
mjr 86:e30a1f60f783 7117 // stop and reset the post-save timer
mjr 86:e30a1f60f783 7118 saveConfigFollowupTimer.stop();
mjr 86:e30a1f60f783 7119 saveConfigFollowupTimer.reset();
mjr 86:e30a1f60f783 7120
mjr 86:e30a1f60f783 7121 // clear the post-save success flag
mjr 86:e30a1f60f783 7122 saveConfigSucceededFlag = 0;
mjr 85:3c28aee81cde 7123 }
mjr 77:0b96f6867312 7124 }
mjr 86:e30a1f60f783 7125
mjr 48:058ace2aed1d 7126 // if we're disconnected, initiate a new connection
mjr 51:57eb311faafa 7127 if (!connected)
mjr 48:058ace2aed1d 7128 {
mjr 54:fd77a6b2f76c 7129 // show USB HAL debug events
mjr 54:fd77a6b2f76c 7130 extern void HAL_DEBUG_PRINTEVENTS(const char *prefix);
mjr 54:fd77a6b2f76c 7131 HAL_DEBUG_PRINTEVENTS(">DISC");
mjr 54:fd77a6b2f76c 7132
mjr 54:fd77a6b2f76c 7133 // show immediate diagnostic feedback
mjr 54:fd77a6b2f76c 7134 js.diagFlash();
mjr 54:fd77a6b2f76c 7135
mjr 54:fd77a6b2f76c 7136 // clear any previous diagnostic LED display
mjr 54:fd77a6b2f76c 7137 diagLED(0, 0, 0);
mjr 51:57eb311faafa 7138
mjr 51:57eb311faafa 7139 // set up a timer to monitor the reboot timeout
mjr 70:9f58735a1732 7140 Timer reconnTimeoutTimer;
mjr 70:9f58735a1732 7141 reconnTimeoutTimer.start();
mjr 48:058ace2aed1d 7142
mjr 54:fd77a6b2f76c 7143 // set up a timer for diagnostic displays
mjr 54:fd77a6b2f76c 7144 Timer diagTimer;
mjr 54:fd77a6b2f76c 7145 diagTimer.reset();
mjr 54:fd77a6b2f76c 7146 diagTimer.start();
mjr 74:822a92bc11d2 7147
mjr 74:822a92bc11d2 7148 // turn off the main loop timer while spinning
mjr 74:822a92bc11d2 7149 IF_DIAG(mainLoopTimer.stop();)
mjr 54:fd77a6b2f76c 7150
mjr 54:fd77a6b2f76c 7151 // loop until we get our connection back
mjr 54:fd77a6b2f76c 7152 while (!js.isConnected() || js.isSleeping())
mjr 51:57eb311faafa 7153 {
mjr 54:fd77a6b2f76c 7154 // try to recover the connection
mjr 54:fd77a6b2f76c 7155 js.recoverConnection();
mjr 54:fd77a6b2f76c 7156
mjr 99:8139b0c274f4 7157 // update Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 7158 LwFlipperLogicOut::poll();
mjr 99:8139b0c274f4 7159 LwChimeLogicOut::poll();
mjr 89:c43cd923401c 7160
mjr 55:4db125cd11a0 7161 // send TLC5940 data if necessary
mjr 55:4db125cd11a0 7162 if (tlc5940 != 0)
mjr 55:4db125cd11a0 7163 tlc5940->send();
mjr 87:8d35c74403af 7164
mjr 87:8d35c74403af 7165 // update TLC59116 outputs
mjr 87:8d35c74403af 7166 if (tlc59116 != 0)
mjr 87:8d35c74403af 7167 tlc59116->send();
mjr 55:4db125cd11a0 7168
mjr 54:fd77a6b2f76c 7169 // show a diagnostic flash every couple of seconds
mjr 54:fd77a6b2f76c 7170 if (diagTimer.read_us() > 2000000)
mjr 51:57eb311faafa 7171 {
mjr 54:fd77a6b2f76c 7172 // flush the USB HAL debug events, if in debug mode
mjr 54:fd77a6b2f76c 7173 HAL_DEBUG_PRINTEVENTS(">NC");
mjr 54:fd77a6b2f76c 7174
mjr 54:fd77a6b2f76c 7175 // show diagnostic feedback
mjr 54:fd77a6b2f76c 7176 js.diagFlash();
mjr 51:57eb311faafa 7177
mjr 51:57eb311faafa 7178 // reset the flash timer
mjr 54:fd77a6b2f76c 7179 diagTimer.reset();
mjr 51:57eb311faafa 7180 }
mjr 51:57eb311faafa 7181
mjr 77:0b96f6867312 7182 // If the disconnect reboot timeout has expired, reboot.
mjr 77:0b96f6867312 7183 // Some PC hosts won't reconnect to a device that's left
mjr 77:0b96f6867312 7184 // plugged in through various events on the PC side, such as
mjr 77:0b96f6867312 7185 // rebooting Windows, cycling power on the PC, or just a lost
mjr 77:0b96f6867312 7186 // USB connection. Rebooting the KL25Z seems to be the most
mjr 77:0b96f6867312 7187 // reliable way to get Windows to notice us again after one
mjr 86:e30a1f60f783 7188 // of these events and make it reconnect. Only reboot if
mjr 86:e30a1f60f783 7189 // the PSU2 power status allows it - if not, skip it on this
mjr 86:e30a1f60f783 7190 // round and keep waiting.
mjr 51:57eb311faafa 7191 if (cfg.disconnectRebootTimeout != 0
mjr 86:e30a1f60f783 7192 && reconnTimeoutTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 7193 && powerStatusAllowsReboot())
mjr 54:fd77a6b2f76c 7194 reboot(js, false, 0);
mjr 77:0b96f6867312 7195
mjr 77:0b96f6867312 7196 // update the PSU2 power sensing status
mjr 77:0b96f6867312 7197 powerStatusUpdate(cfg);
mjr 54:fd77a6b2f76c 7198 }
mjr 54:fd77a6b2f76c 7199
mjr 74:822a92bc11d2 7200 // resume the main loop timer
mjr 74:822a92bc11d2 7201 IF_DIAG(mainLoopTimer.start();)
mjr 74:822a92bc11d2 7202
mjr 54:fd77a6b2f76c 7203 // if we made it out of that loop alive, we're connected again!
mjr 54:fd77a6b2f76c 7204 connected = true;
mjr 54:fd77a6b2f76c 7205 HAL_DEBUG_PRINTEVENTS(">C");
mjr 54:fd77a6b2f76c 7206
mjr 54:fd77a6b2f76c 7207 // Enable peripheral chips and update them with current output data
mjr 54:fd77a6b2f76c 7208 if (tlc5940 != 0)
mjr 55:4db125cd11a0 7209 tlc5940->enable(true);
mjr 87:8d35c74403af 7210 if (tlc59116 != 0)
mjr 87:8d35c74403af 7211 tlc59116->enable(true);
mjr 54:fd77a6b2f76c 7212 if (hc595 != 0)
mjr 54:fd77a6b2f76c 7213 {
mjr 55:4db125cd11a0 7214 hc595->enable(true);
mjr 54:fd77a6b2f76c 7215 hc595->update(true);
mjr 51:57eb311faafa 7216 }
mjr 48:058ace2aed1d 7217 }
mjr 43:7a6364d82a41 7218
mjr 6:cc35eb643e8f 7219 // provide a visual status indication on the on-board LED
mjr 48:058ace2aed1d 7220 if (calBtnState < 2 && hbTimer.read_us() > 1000000)
mjr 1:d913e0afb2ac 7221 {
mjr 54:fd77a6b2f76c 7222 if (jsOKTimer.read_us() > 1000000)
mjr 38:091e511ce8a0 7223 {
mjr 39:b3815a1c3802 7224 // USB freeze - show red/yellow.
mjr 40:cc0d9814522b 7225 //
mjr 54:fd77a6b2f76c 7226 // It's been more than a second since we successfully sent a joystick
mjr 54:fd77a6b2f76c 7227 // update message. This must mean that something's wrong on the USB
mjr 54:fd77a6b2f76c 7228 // connection, even though we haven't detected an outright disconnect.
mjr 54:fd77a6b2f76c 7229 // Show a distinctive diagnostic LED pattern when this occurs.
mjr 38:091e511ce8a0 7230 hb = !hb;
mjr 38:091e511ce8a0 7231 diagLED(1, hb, 0);
mjr 54:fd77a6b2f76c 7232
mjr 54:fd77a6b2f76c 7233 // If the reboot-on-disconnect option is in effect, treat this condition
mjr 54:fd77a6b2f76c 7234 // as equivalent to a disconnect, since something is obviously wrong
mjr 54:fd77a6b2f76c 7235 // with the USB connection.
mjr 54:fd77a6b2f76c 7236 if (cfg.disconnectRebootTimeout != 0)
mjr 54:fd77a6b2f76c 7237 {
mjr 54:fd77a6b2f76c 7238 // The reboot timeout is in effect. If we've been incommunicado for
mjr 54:fd77a6b2f76c 7239 // longer than the timeout, reboot. If we haven't reached the time
mjr 54:fd77a6b2f76c 7240 // limit, keep running for now, and leave the OK timer running so
mjr 86:e30a1f60f783 7241 // that we can continue to monitor this. Only reboot if the PSU2
mjr 86:e30a1f60f783 7242 // power status allows it.
mjr 86:e30a1f60f783 7243 if (jsOKTimer.read() > cfg.disconnectRebootTimeout
mjr 86:e30a1f60f783 7244 && powerStatusAllowsReboot())
mjr 54:fd77a6b2f76c 7245 reboot(js, false, 0);
mjr 54:fd77a6b2f76c 7246 }
mjr 54:fd77a6b2f76c 7247 else
mjr 54:fd77a6b2f76c 7248 {
mjr 54:fd77a6b2f76c 7249 // There's no reboot timer, so just keep running with the diagnostic
mjr 54:fd77a6b2f76c 7250 // pattern displayed. Since we're not waiting for any other timed
mjr 54:fd77a6b2f76c 7251 // conditions in this state, stop the timer so that it doesn't
mjr 54:fd77a6b2f76c 7252 // overflow if this condition persists for a long time.
mjr 54:fd77a6b2f76c 7253 jsOKTimer.stop();
mjr 54:fd77a6b2f76c 7254 }
mjr 38:091e511ce8a0 7255 }
mjr 73:4e8ce0b18915 7256 else if (psu2_state >= 4)
mjr 73:4e8ce0b18915 7257 {
mjr 73:4e8ce0b18915 7258 // We're in the TV timer countdown. Skip the normal heartbeat
mjr 73:4e8ce0b18915 7259 // flashes and show the TV timer flashes instead.
mjr 73:4e8ce0b18915 7260 diagLED(0, 0, 0);
mjr 73:4e8ce0b18915 7261 }
mjr 96:68d5621ff49f 7262 else if (effectivePlungerEnabled && !cfg.plunger.cal.calibrated)
mjr 6:cc35eb643e8f 7263 {
mjr 6:cc35eb643e8f 7264 // connected, plunger calibration needed - flash yellow/green
mjr 6:cc35eb643e8f 7265 hb = !hb;
mjr 38:091e511ce8a0 7266 diagLED(hb, 1, 0);
mjr 6:cc35eb643e8f 7267 }
mjr 6:cc35eb643e8f 7268 else
mjr 6:cc35eb643e8f 7269 {
mjr 6:cc35eb643e8f 7270 // connected - flash blue/green
mjr 2:c174f9ee414a 7271 hb = !hb;
mjr 38:091e511ce8a0 7272 diagLED(0, hb, !hb);
mjr 2:c174f9ee414a 7273 }
mjr 1:d913e0afb2ac 7274
mjr 1:d913e0afb2ac 7275 // reset the heartbeat timer
mjr 1:d913e0afb2ac 7276 hbTimer.reset();
mjr 5:a70c0bce770d 7277 ++hbcnt;
mjr 1:d913e0afb2ac 7278 }
mjr 74:822a92bc11d2 7279
mjr 74:822a92bc11d2 7280 // collect statistics on the main loop time, if desired
mjr 74:822a92bc11d2 7281 IF_DIAG(
mjr 76:7f5912b6340e 7282 mainLoopIterTime += mainLoopTimer.read_us();
mjr 74:822a92bc11d2 7283 mainLoopIterCount++;
mjr 74:822a92bc11d2 7284 )
mjr 1:d913e0afb2ac 7285 }
mjr 0:5acbbe3f4cf4 7286 }