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

Dependencies:   mbed FastIO FastPWM USBDevice

Fork of Pinscape_Controller by Mike R

/media/uploads/mjr/pinscape_no_background_small_L7Miwr6.jpg

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

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

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

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

Downloads

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

Documentation

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

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

System Requirements

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

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

Main Features

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

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

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

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

Expansion Boards

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

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

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

Expansion Board project page

Update notes

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

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

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

New Features

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

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

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

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

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

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

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

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

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

More Downloads

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

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

Copyright and License

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

Warning to VirtuaPin Kit Owners

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

Committer:
mjr
Date:
Sat Mar 02 21:05:43 2019 +0000
Revision:
99:8139b0c274f4
Parent:
98:4df3c0f7e707
Child:
100:1ff35c07217c
Added Chime Logic

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 82:4f6209cb5c33 262
mjr 2:c174f9ee414a 263
mjr 21:5048e16cc9ef 264 #define DECL_EXTERNS
mjr 17:ab3cec0c8bf4 265 #include "config.h"
mjr 17:ab3cec0c8bf4 266
mjr 76:7f5912b6340e 267 // forward declarations
mjr 76:7f5912b6340e 268 static void waitPlungerIdle(void);
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 38:091e511ce8a0 519 // check an output port 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 38:091e511ce8a0 526 void check(LedWizPortCfg &pc)
mjr 38:091e511ce8a0 527 {
mjr 38:091e511ce8a0 528 // if it's a GPIO, check to see if it's assigned to one of
mjr 38:091e511ce8a0 529 // our on-board LED segments
mjr 38:091e511ce8a0 530 int t = pc.typ;
mjr 38:091e511ce8a0 531 if (t == PortTypeGPIOPWM || t == PortTypeGPIODig)
mjr 38:091e511ce8a0 532 {
mjr 38:091e511ce8a0 533 // it's a GPIO port - check for a matching pin assignment
mjr 38:091e511ce8a0 534 PinName pin = wirePinName(pc.pin);
mjr 38:091e511ce8a0 535 if (pin == LED1)
mjr 38:091e511ce8a0 536 r = true;
mjr 38:091e511ce8a0 537 else if (pin == LED2)
mjr 38:091e511ce8a0 538 g = true;
mjr 38:091e511ce8a0 539 else if (pin == LED3)
mjr 38:091e511ce8a0 540 b = true;
mjr 38:091e511ce8a0 541 }
mjr 38:091e511ce8a0 542 }
mjr 38:091e511ce8a0 543 };
mjr 38:091e511ce8a0 544
mjr 38:091e511ce8a0 545 // Initialize the diagnostic LEDs. By default, we use the on-board
mjr 38:091e511ce8a0 546 // RGB LED to display the microcontroller status. However, we allow
mjr 38:091e511ce8a0 547 // the user to commandeer the on-board LED as an LedWiz output device,
mjr 38:091e511ce8a0 548 // which can be useful for testing a new installation. So we'll check
mjr 38:091e511ce8a0 549 // for LedWiz outputs assigned to the on-board LED segments, and turn
mjr 38:091e511ce8a0 550 // off the diagnostic use for any so assigned.
mjr 38:091e511ce8a0 551 void initDiagLEDs(Config &cfg)
mjr 38:091e511ce8a0 552 {
mjr 38:091e511ce8a0 553 // run through the configuration list and cross off any of the
mjr 38:091e511ce8a0 554 // LED segments assigned to LedWiz ports
mjr 38:091e511ce8a0 555 LedSeg l;
mjr 38:091e511ce8a0 556 for (int i = 0 ; i < MAX_OUT_PORTS && cfg.outPort[i].typ != PortTypeDisabled ; ++i)
mjr 38:091e511ce8a0 557 l.check(cfg.outPort[i]);
mjr 38:091e511ce8a0 558
mjr 38:091e511ce8a0 559 // We now know which segments are taken for LedWiz use and which
mjr 38:091e511ce8a0 560 // are free. Create diagnostic ports for the ones not claimed for
mjr 38:091e511ce8a0 561 // LedWiz use.
mjr 38:091e511ce8a0 562 if (!l.r) ledR = new DigitalOut(LED1, 1);
mjr 38:091e511ce8a0 563 if (!l.g) ledG = new DigitalOut(LED2, 1);
mjr 38:091e511ce8a0 564 if (!l.b) ledB = new DigitalOut(LED3, 1);
mjr 38:091e511ce8a0 565 }
mjr 38:091e511ce8a0 566
mjr 38:091e511ce8a0 567
mjr 38:091e511ce8a0 568 // ---------------------------------------------------------------------------
mjr 38:091e511ce8a0 569 //
mjr 76:7f5912b6340e 570 // LedWiz emulation
mjr 76:7f5912b6340e 571 //
mjr 76:7f5912b6340e 572
mjr 76:7f5912b6340e 573 // LedWiz output states.
mjr 76:7f5912b6340e 574 //
mjr 76:7f5912b6340e 575 // The LedWiz protocol has two separate control axes for each output.
mjr 76:7f5912b6340e 576 // One axis is its on/off state; the other is its "profile" state, which
mjr 76:7f5912b6340e 577 // is either a fixed brightness or a blinking pattern for the light.
mjr 76:7f5912b6340e 578 // The two axes are independent.
mjr 76:7f5912b6340e 579 //
mjr 76:7f5912b6340e 580 // Even though the original LedWiz protocol can only access 32 ports, we
mjr 76:7f5912b6340e 581 // maintain LedWiz state for every port, even if we have more than 32. Our
mjr 76:7f5912b6340e 582 // extended protocol allows the client to send LedWiz-style messages that
mjr 76:7f5912b6340e 583 // control any set of ports. A replacement LEDWIZ.DLL can make a single
mjr 76:7f5912b6340e 584 // Pinscape unit look like multiple virtual LedWiz units to legacy clients,
mjr 76:7f5912b6340e 585 // allowing them to control all of our ports. The clients will still be
mjr 76:7f5912b6340e 586 // using LedWiz-style states to control the ports, so we need to support
mjr 76:7f5912b6340e 587 // the LedWiz scheme with separate on/off and brightness control per port.
mjr 76:7f5912b6340e 588
mjr 76:7f5912b6340e 589 // On/off state for each LedWiz output
mjr 76:7f5912b6340e 590 static uint8_t *wizOn;
mjr 76:7f5912b6340e 591
mjr 76:7f5912b6340e 592 // LedWiz "Profile State" (the LedWiz brightness level or blink mode)
mjr 76:7f5912b6340e 593 // for each LedWiz output. If the output was last updated through an
mjr 76:7f5912b6340e 594 // LedWiz protocol message, it will have one of these values:
mjr 76:7f5912b6340e 595 //
mjr 76:7f5912b6340e 596 // 0-48 = fixed brightness 0% to 100%
mjr 76:7f5912b6340e 597 // 49 = fixed brightness 100% (equivalent to 48)
mjr 76:7f5912b6340e 598 // 129 = ramp up / ramp down
mjr 76:7f5912b6340e 599 // 130 = flash on / off
mjr 76:7f5912b6340e 600 // 131 = on / ramp down
mjr 76:7f5912b6340e 601 // 132 = ramp up / on
mjr 5:a70c0bce770d 602 //
mjr 76:7f5912b6340e 603 // (Note that value 49 isn't documented in the LedWiz spec, but real
mjr 76:7f5912b6340e 604 // LedWiz units treat it as equivalent to 48, and some PC software uses
mjr 76:7f5912b6340e 605 // it, so we need to accept it for compatibility.)
mjr 76:7f5912b6340e 606 static uint8_t *wizVal;
mjr 76:7f5912b6340e 607
mjr 76:7f5912b6340e 608 // Current actual brightness for each output. This is a simple linear
mjr 76:7f5912b6340e 609 // value on a 0..255 scale. This is EITHER the linear brightness computed
mjr 76:7f5912b6340e 610 // from the LedWiz setting for the port, OR the 0..255 value set explicitly
mjr 76:7f5912b6340e 611 // by the extended protocol:
mjr 76:7f5912b6340e 612 //
mjr 76:7f5912b6340e 613 // - If the last command that updated the port was an extended protocol
mjr 76:7f5912b6340e 614 // SET BRIGHTNESS command, this is the value set by that command. In
mjr 76:7f5912b6340e 615 // addition, wizOn[port] is set to 0 if the brightness is 0, 1 otherwise;
mjr 76:7f5912b6340e 616 // and wizVal[port] is set to the brightness rescaled to the 0..48 range
mjr 76:7f5912b6340e 617 // if the brightness is non-zero.
mjr 76:7f5912b6340e 618 //
mjr 76:7f5912b6340e 619 // - If the last command that updated the port was an LedWiz command
mjr 76:7f5912b6340e 620 // (SBA/PBA/SBX/PBX), this contains the brightness value computed from
mjr 76:7f5912b6340e 621 // the combination of wizOn[port] and wizVal[port]. If wizOn[port] is
mjr 76:7f5912b6340e 622 // zero, this is simply 0, otherwise it's wizVal[port] rescaled to the
mjr 76:7f5912b6340e 623 // 0..255 range.
mjr 26:cb71c4af2912 624 //
mjr 76:7f5912b6340e 625 // - For a port set to wizOn[port]=1 and wizVal[port] in 129..132, this is
mjr 76:7f5912b6340e 626 // also updated continuously to reflect the current flashing brightness
mjr 76:7f5912b6340e 627 // level.
mjr 26:cb71c4af2912 628 //
mjr 76:7f5912b6340e 629 static uint8_t *outLevel;
mjr 76:7f5912b6340e 630
mjr 76:7f5912b6340e 631
mjr 76:7f5912b6340e 632 // LedWiz flash speed. This is a value from 1 to 7 giving the pulse
mjr 76:7f5912b6340e 633 // rate for lights in blinking states. The LedWiz API doesn't document
mjr 76:7f5912b6340e 634 // what the numbers mean in real time units, but by observation, the
mjr 76:7f5912b6340e 635 // "speed" setting represents the period of the flash cycle in 0.25s
mjr 76:7f5912b6340e 636 // units, so speed 1 = 0.25 period = 4Hz, speed 7 = 1.75s period = 0.57Hz.
mjr 76:7f5912b6340e 637 // The period is the full cycle time of the flash waveform.
mjr 76:7f5912b6340e 638 //
mjr 76:7f5912b6340e 639 // Each bank of 32 lights has its independent own pulse rate, so we need
mjr 76:7f5912b6340e 640 // one entry per bank. Each bank has 32 outputs, so we need a total of
mjr 76:7f5912b6340e 641 // ceil(number_of_physical_outputs/32) entries. Note that we could allocate
mjr 76:7f5912b6340e 642 // this dynamically once we know the number of actual outputs, but the
mjr 76:7f5912b6340e 643 // upper limit is low enough that it's more efficient to use a fixed array
mjr 76:7f5912b6340e 644 // at the maximum size.
mjr 76:7f5912b6340e 645 static const int MAX_LW_BANKS = (MAX_OUT_PORTS+31)/32;
mjr 76:7f5912b6340e 646 static uint8_t wizSpeed[MAX_LW_BANKS];
mjr 29:582472d0bc57 647
mjr 26:cb71c4af2912 648 // Current starting output index for "PBA" messages from the PC (using
mjr 26:cb71c4af2912 649 // the LedWiz USB protocol). Each PBA message implicitly uses the
mjr 26:cb71c4af2912 650 // current index as the starting point for the ports referenced in
mjr 26:cb71c4af2912 651 // the message, and increases it (by 8) for the next call.
mjr 0:5acbbe3f4cf4 652 static int pbaIdx = 0;
mjr 0:5acbbe3f4cf4 653
mjr 76:7f5912b6340e 654
mjr 76:7f5912b6340e 655 // ---------------------------------------------------------------------------
mjr 76:7f5912b6340e 656 //
mjr 76:7f5912b6340e 657 // Output Ports
mjr 76:7f5912b6340e 658 //
mjr 76:7f5912b6340e 659 // There are two way to connect outputs. First, you can use the on-board
mjr 76:7f5912b6340e 660 // GPIO ports to implement device outputs: each LedWiz software port is
mjr 76:7f5912b6340e 661 // connected to a physical GPIO pin on the KL25Z. This has some pretty
mjr 76:7f5912b6340e 662 // strict limits, though. The KL25Z only has 10 PWM channels, so only 10
mjr 76:7f5912b6340e 663 // GPIO LedWiz ports can be made dimmable; the rest are strictly on/off.
mjr 76:7f5912b6340e 664 // The KL25Z also simply doesn't have enough exposed GPIO ports overall to
mjr 76:7f5912b6340e 665 // support all of the features the software supports. The software allows
mjr 76:7f5912b6340e 666 // for up to 128 outputs, 48 button inputs, plunger input (requiring 1-5
mjr 76:7f5912b6340e 667 // GPIO pins), and various other external devices. The KL25Z only exposes
mjr 76:7f5912b6340e 668 // about 50 GPIO pins. So if you want to do everything with GPIO ports,
mjr 76:7f5912b6340e 669 // you have to ration pins among features.
mjr 76:7f5912b6340e 670 //
mjr 87:8d35c74403af 671 // To overcome some of these limitations, we also support several external
mjr 76:7f5912b6340e 672 // peripheral controllers that allow adding many more outputs, using only
mjr 87:8d35c74403af 673 // a small number of GPIO pins to interface with the peripherals:
mjr 87:8d35c74403af 674 //
mjr 87:8d35c74403af 675 // - TLC5940 PWM controller chips. Each TLC5940 provides 16 ports with
mjr 87:8d35c74403af 676 // 12-bit PWM, and multiple TLC5940 chips can be daisy-chained. The
mjr 87:8d35c74403af 677 // chips connect via 5 GPIO pins, and since they're daisy-chainable,
mjr 87:8d35c74403af 678 // one set of 5 pins can control any number of the chips. So this chip
mjr 87:8d35c74403af 679 // effectively converts 5 GPIO pins into almost any number of PWM outputs.
mjr 87:8d35c74403af 680 //
mjr 87:8d35c74403af 681 // - TLC59116 PWM controller chips. These are similar to the TLC5940 but
mjr 87:8d35c74403af 682 // a newer generation with an improved design. These use an I2C bus,
mjr 87:8d35c74403af 683 // allowing up to 14 chips to be connected via 3 GPIO pins.
mjr 87:8d35c74403af 684 //
mjr 87:8d35c74403af 685 // - 74HC595 shift register chips. These provide 8 digital (on/off only)
mjr 87:8d35c74403af 686 // outputs per chip. These need 4 GPIO pins, and like the other can be
mjr 87:8d35c74403af 687 // daisy chained to add more outputs without using more GPIO pins. These
mjr 87:8d35c74403af 688 // are advantageous for outputs that don't require PWM, since the data
mjr 87:8d35c74403af 689 // transfer sizes are so much smaller. The expansion boards use these
mjr 87:8d35c74403af 690 // for the chime board outputs.
mjr 76:7f5912b6340e 691 //
mjr 76:7f5912b6340e 692 // Direct GPIO output ports and peripheral controllers can be mixed and
mjr 76:7f5912b6340e 693 // matched in one system. The assignment of pins to ports and the
mjr 76:7f5912b6340e 694 // configuration of peripheral controllers is all handled in the software
mjr 76:7f5912b6340e 695 // setup, so a physical system can be expanded and updated at any time.
mjr 76:7f5912b6340e 696 //
mjr 76:7f5912b6340e 697 // To handle the diversity of output port types, we start with an abstract
mjr 76:7f5912b6340e 698 // base class for outputs. Each type of physical output interface has a
mjr 76:7f5912b6340e 699 // concrete subclass. During initialization, we create the appropriate
mjr 76:7f5912b6340e 700 // subclass for each software port, mapping it to the assigned GPIO pin
mjr 76:7f5912b6340e 701 // or peripheral port. Most of the rest of the software only cares about
mjr 76:7f5912b6340e 702 // the abstract interface, so once the subclassed port objects are set up,
mjr 76:7f5912b6340e 703 // the rest of the system can control the ports without knowing which types
mjr 76:7f5912b6340e 704 // of physical devices they're connected to.
mjr 76:7f5912b6340e 705
mjr 76:7f5912b6340e 706
mjr 26:cb71c4af2912 707 // Generic LedWiz output port interface. We create a cover class to
mjr 26:cb71c4af2912 708 // virtualize digital vs PWM outputs, and on-board KL25Z GPIO vs external
mjr 26:cb71c4af2912 709 // TLC5940 outputs, and give them all a common interface.
mjr 6:cc35eb643e8f 710 class LwOut
mjr 6:cc35eb643e8f 711 {
mjr 6:cc35eb643e8f 712 public:
mjr 40:cc0d9814522b 713 // Set the output intensity. 'val' is 0 for fully off, 255 for
mjr 40:cc0d9814522b 714 // fully on, with values in between signifying lower intensity.
mjr 40:cc0d9814522b 715 virtual void set(uint8_t val) = 0;
mjr 6:cc35eb643e8f 716 };
mjr 26:cb71c4af2912 717
mjr 35:e959ffba78fd 718 // LwOut class for virtual ports. This type of port is visible to
mjr 35:e959ffba78fd 719 // the host software, but isn't connected to any physical output.
mjr 35:e959ffba78fd 720 // This can be used for special software-only ports like the ZB
mjr 35:e959ffba78fd 721 // Launch Ball output, or simply for placeholders in the LedWiz port
mjr 35:e959ffba78fd 722 // numbering.
mjr 35:e959ffba78fd 723 class LwVirtualOut: public LwOut
mjr 33:d832bcab089e 724 {
mjr 33:d832bcab089e 725 public:
mjr 35:e959ffba78fd 726 LwVirtualOut() { }
mjr 40:cc0d9814522b 727 virtual void set(uint8_t ) { }
mjr 33:d832bcab089e 728 };
mjr 26:cb71c4af2912 729
mjr 34:6b981a2afab7 730 // Active Low out. For any output marked as active low, we layer this
mjr 34:6b981a2afab7 731 // on top of the physical pin interface. This simply inverts the value of
mjr 40:cc0d9814522b 732 // the output value, so that 255 means fully off and 0 means fully on.
mjr 34:6b981a2afab7 733 class LwInvertedOut: public LwOut
mjr 34:6b981a2afab7 734 {
mjr 34:6b981a2afab7 735 public:
mjr 34:6b981a2afab7 736 LwInvertedOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 737 virtual void set(uint8_t val) { out->set(255 - val); }
mjr 34:6b981a2afab7 738
mjr 34:6b981a2afab7 739 private:
mjr 53:9b2611964afc 740 // underlying physical output
mjr 34:6b981a2afab7 741 LwOut *out;
mjr 34:6b981a2afab7 742 };
mjr 34:6b981a2afab7 743
mjr 53:9b2611964afc 744 // Global ZB Launch Ball state
mjr 53:9b2611964afc 745 bool zbLaunchOn = false;
mjr 53:9b2611964afc 746
mjr 53:9b2611964afc 747 // ZB Launch Ball output. This is layered on a port (physical or virtual)
mjr 53:9b2611964afc 748 // to track the ZB Launch Ball signal.
mjr 53:9b2611964afc 749 class LwZbLaunchOut: public LwOut
mjr 53:9b2611964afc 750 {
mjr 53:9b2611964afc 751 public:
mjr 53:9b2611964afc 752 LwZbLaunchOut(LwOut *o) : out(o) { }
mjr 53:9b2611964afc 753 virtual void set(uint8_t val)
mjr 53:9b2611964afc 754 {
mjr 53:9b2611964afc 755 // update the global ZB Launch Ball state
mjr 53:9b2611964afc 756 zbLaunchOn = (val != 0);
mjr 53:9b2611964afc 757
mjr 53:9b2611964afc 758 // pass it along to the underlying port, in case it's a physical output
mjr 53:9b2611964afc 759 out->set(val);
mjr 53:9b2611964afc 760 }
mjr 53:9b2611964afc 761
mjr 53:9b2611964afc 762 private:
mjr 53:9b2611964afc 763 // underlying physical or virtual output
mjr 53:9b2611964afc 764 LwOut *out;
mjr 53:9b2611964afc 765 };
mjr 53:9b2611964afc 766
mjr 53:9b2611964afc 767
mjr 40:cc0d9814522b 768 // Gamma correction table for 8-bit input values
mjr 87:8d35c74403af 769 static const uint8_t dof_to_gamma_8bit[] = {
mjr 40:cc0d9814522b 770 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
mjr 40:cc0d9814522b 771 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
mjr 40:cc0d9814522b 772 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
mjr 40:cc0d9814522b 773 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
mjr 40:cc0d9814522b 774 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
mjr 40:cc0d9814522b 775 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
mjr 40:cc0d9814522b 776 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
mjr 40:cc0d9814522b 777 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
mjr 40:cc0d9814522b 778 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
mjr 40:cc0d9814522b 779 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
mjr 40:cc0d9814522b 780 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
mjr 40:cc0d9814522b 781 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
mjr 40:cc0d9814522b 782 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
mjr 40:cc0d9814522b 783 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
mjr 40:cc0d9814522b 784 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
mjr 40:cc0d9814522b 785 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
mjr 40:cc0d9814522b 786 };
mjr 40:cc0d9814522b 787
mjr 40:cc0d9814522b 788 // Gamma-corrected out. This is a filter object that we layer on top
mjr 40:cc0d9814522b 789 // of a physical pin interface. This applies gamma correction to the
mjr 40:cc0d9814522b 790 // input value and then passes it along to the underlying pin object.
mjr 40:cc0d9814522b 791 class LwGammaOut: public LwOut
mjr 40:cc0d9814522b 792 {
mjr 40:cc0d9814522b 793 public:
mjr 40:cc0d9814522b 794 LwGammaOut(LwOut *o) : out(o) { }
mjr 87:8d35c74403af 795 virtual void set(uint8_t val) { out->set(dof_to_gamma_8bit[val]); }
mjr 40:cc0d9814522b 796
mjr 40:cc0d9814522b 797 private:
mjr 40:cc0d9814522b 798 LwOut *out;
mjr 40:cc0d9814522b 799 };
mjr 40:cc0d9814522b 800
mjr 77:0b96f6867312 801 // Global night mode flag. To minimize overhead when reporting
mjr 77:0b96f6867312 802 // the status, we set this to the status report flag bit for
mjr 77:0b96f6867312 803 // night mode, 0x02, when engaged.
mjr 77:0b96f6867312 804 static uint8_t nightMode = 0x00;
mjr 53:9b2611964afc 805
mjr 40:cc0d9814522b 806 // Noisy output. This is a filter object that we layer on top of
mjr 40:cc0d9814522b 807 // a physical pin output. This filter disables the port when night
mjr 40:cc0d9814522b 808 // mode is engaged.
mjr 40:cc0d9814522b 809 class LwNoisyOut: public LwOut
mjr 40:cc0d9814522b 810 {
mjr 40:cc0d9814522b 811 public:
mjr 40:cc0d9814522b 812 LwNoisyOut(LwOut *o) : out(o) { }
mjr 40:cc0d9814522b 813 virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
mjr 40:cc0d9814522b 814
mjr 53:9b2611964afc 815 private:
mjr 53:9b2611964afc 816 LwOut *out;
mjr 53:9b2611964afc 817 };
mjr 53:9b2611964afc 818
mjr 53:9b2611964afc 819 // Night Mode indicator output. This is a filter object that we
mjr 53:9b2611964afc 820 // layer on top of a physical pin output. This filter ignores the
mjr 53:9b2611964afc 821 // host value and simply shows the night mode status.
mjr 53:9b2611964afc 822 class LwNightModeIndicatorOut: public LwOut
mjr 53:9b2611964afc 823 {
mjr 53:9b2611964afc 824 public:
mjr 53:9b2611964afc 825 LwNightModeIndicatorOut(LwOut *o) : out(o) { }
mjr 89:c43cd923401c 826 virtual void set(uint8_t)
mjr 53:9b2611964afc 827 {
mjr 53:9b2611964afc 828 // ignore the host value and simply show the current
mjr 53:9b2611964afc 829 // night mode setting
mjr 53:9b2611964afc 830 out->set(nightMode ? 255 : 0);
mjr 53:9b2611964afc 831 }
mjr 40:cc0d9814522b 832
mjr 40:cc0d9814522b 833 private:
mjr 40:cc0d9814522b 834 LwOut *out;
mjr 40:cc0d9814522b 835 };
mjr 40:cc0d9814522b 836
mjr 26:cb71c4af2912 837
mjr 89:c43cd923401c 838 // Flipper Logic output. This is a filter object that we layer on
mjr 89:c43cd923401c 839 // top of a physical pin output.
mjr 89:c43cd923401c 840 //
mjr 89:c43cd923401c 841 // A Flipper Logic output is effectively a digital output from the
mjr 89:c43cd923401c 842 // client's perspective, in that it ignores the intensity level and
mjr 89:c43cd923401c 843 // only pays attention to the ON/OFF state. 0 is OFF and any other
mjr 89:c43cd923401c 844 // level is ON.
mjr 89:c43cd923401c 845 //
mjr 89:c43cd923401c 846 // In terms of the physical output, though, we do use varying power.
mjr 89:c43cd923401c 847 // It's just that the varying power isn't under the client's control;
mjr 89:c43cd923401c 848 // we control it according to our flipperLogic settings:
mjr 89:c43cd923401c 849 //
mjr 89:c43cd923401c 850 // - When the software port transitions from OFF (0 brightness) to ON
mjr 89:c43cd923401c 851 // (any non-zero brightness level), we set the physical port to 100%
mjr 89:c43cd923401c 852 // power and start a timer.
mjr 89:c43cd923401c 853 //
mjr 89:c43cd923401c 854 // - When the full power time in our flipperLogic settings elapses,
mjr 89:c43cd923401c 855 // if the software port is still ON, we reduce the physical port to
mjr 89:c43cd923401c 856 // the PWM level in our flipperLogic setting.
mjr 89:c43cd923401c 857 //
mjr 89:c43cd923401c 858 class LwFlipperLogicOut: public LwOut
mjr 89:c43cd923401c 859 {
mjr 89:c43cd923401c 860 public:
mjr 89:c43cd923401c 861 // Set up the output. 'params' is the flipperLogic value from
mjr 89:c43cd923401c 862 // the configuration.
mjr 89:c43cd923401c 863 LwFlipperLogicOut(LwOut *o, uint8_t params)
mjr 89:c43cd923401c 864 : out(o), params(params)
mjr 89:c43cd923401c 865 {
mjr 89:c43cd923401c 866 // initially OFF
mjr 89:c43cd923401c 867 state = 0;
mjr 89:c43cd923401c 868 }
mjr 89:c43cd923401c 869
mjr 89:c43cd923401c 870 virtual void set(uint8_t level)
mjr 89:c43cd923401c 871 {
mjr 98:4df3c0f7e707 872 // remember the new nominal level set by the client
mjr 89:c43cd923401c 873 val = level;
mjr 89:c43cd923401c 874
mjr 89:c43cd923401c 875 // update the physical output according to our current timing state
mjr 89:c43cd923401c 876 switch (state)
mjr 89:c43cd923401c 877 {
mjr 89:c43cd923401c 878 case 0:
mjr 89:c43cd923401c 879 // We're currently off. If the new level is non-zero, switch
mjr 89:c43cd923401c 880 // to state 1 (initial full-power interval) and set the requested
mjr 89:c43cd923401c 881 // level. If the new level is zero, we're switching from off to
mjr 89:c43cd923401c 882 // off, so there's no change.
mjr 89:c43cd923401c 883 if (level != 0)
mjr 89:c43cd923401c 884 {
mjr 89:c43cd923401c 885 // switch to state 1 (initial full-power interval)
mjr 89:c43cd923401c 886 state = 1;
mjr 89:c43cd923401c 887
mjr 89:c43cd923401c 888 // set the requested output level - there's no limit during
mjr 89:c43cd923401c 889 // the initial full-power interval, so set the exact level
mjr 89:c43cd923401c 890 // requested
mjr 89:c43cd923401c 891 out->set(level);
mjr 89:c43cd923401c 892
mjr 89:c43cd923401c 893 // add myself to the pending timer list
mjr 89:c43cd923401c 894 pending[nPending++] = this;
mjr 89:c43cd923401c 895
mjr 89:c43cd923401c 896 // note the starting time
mjr 89:c43cd923401c 897 t0 = timer.read_us();
mjr 89:c43cd923401c 898 }
mjr 89:c43cd923401c 899 break;
mjr 89:c43cd923401c 900
mjr 89:c43cd923401c 901 case 1:
mjr 89:c43cd923401c 902 // Initial full-power interval. If the new level is non-zero,
mjr 89:c43cd923401c 903 // simply apply the new level as requested, since there's no
mjr 89:c43cd923401c 904 // limit during this period. If the new level is zero, shut
mjr 89:c43cd923401c 905 // off the output and cancel the pending timer.
mjr 89:c43cd923401c 906 out->set(level);
mjr 89:c43cd923401c 907 if (level == 0)
mjr 89:c43cd923401c 908 {
mjr 89:c43cd923401c 909 // We're switching off. In state 1, we have a pending timer,
mjr 89:c43cd923401c 910 // so we need to remove it from the list.
mjr 89:c43cd923401c 911 for (int i = 0 ; i < nPending ; ++i)
mjr 89:c43cd923401c 912 {
mjr 89:c43cd923401c 913 // is this us?
mjr 89:c43cd923401c 914 if (pending[i] == this)
mjr 89:c43cd923401c 915 {
mjr 89:c43cd923401c 916 // remove myself by replacing the slot with the
mjr 89:c43cd923401c 917 // last list entry
mjr 89:c43cd923401c 918 pending[i] = pending[--nPending];
mjr 89:c43cd923401c 919
mjr 89:c43cd923401c 920 // no need to look any further
mjr 89:c43cd923401c 921 break;
mjr 89:c43cd923401c 922 }
mjr 89:c43cd923401c 923 }
mjr 89:c43cd923401c 924
mjr 89:c43cd923401c 925 // switch to state 0 (off)
mjr 89:c43cd923401c 926 state = 0;
mjr 89:c43cd923401c 927 }
mjr 89:c43cd923401c 928 break;
mjr 89:c43cd923401c 929
mjr 89:c43cd923401c 930 case 2:
mjr 89:c43cd923401c 931 // Hold interval. If the new level is zero, switch to state
mjr 89:c43cd923401c 932 // 0 (off). If the new level is non-zero, stay in the hold
mjr 89:c43cd923401c 933 // state, and set the new level, applying the hold power setting
mjr 89:c43cd923401c 934 // as the upper bound.
mjr 89:c43cd923401c 935 if (level == 0)
mjr 89:c43cd923401c 936 {
mjr 89:c43cd923401c 937 // switching off - turn off the physical output
mjr 89:c43cd923401c 938 out->set(0);
mjr 89:c43cd923401c 939
mjr 89:c43cd923401c 940 // go to state 0 (off)
mjr 89:c43cd923401c 941 state = 0;
mjr 89:c43cd923401c 942 }
mjr 89:c43cd923401c 943 else
mjr 89:c43cd923401c 944 {
mjr 89:c43cd923401c 945 // staying on - set the new physical output power to the
mjr 89:c43cd923401c 946 // lower of the requested power and the hold power
mjr 89:c43cd923401c 947 uint8_t hold = holdPower();
mjr 89:c43cd923401c 948 out->set(level < hold ? level : hold);
mjr 89:c43cd923401c 949 }
mjr 89:c43cd923401c 950 break;
mjr 89:c43cd923401c 951 }
mjr 89:c43cd923401c 952 }
mjr 89:c43cd923401c 953
mjr 89:c43cd923401c 954 // Class initialization
mjr 89:c43cd923401c 955 static void classInit(Config &cfg)
mjr 89:c43cd923401c 956 {
mjr 89:c43cd923401c 957 // Count the Flipper Logic outputs in the configuration. We
mjr 89:c43cd923401c 958 // need to allocate enough pending timer list space to accommodate
mjr 89:c43cd923401c 959 // all of these outputs.
mjr 89:c43cd923401c 960 int n = 0;
mjr 89:c43cd923401c 961 for (int i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 89:c43cd923401c 962 {
mjr 89:c43cd923401c 963 // if this port is active and marked as Flipper Logic, count it
mjr 89:c43cd923401c 964 if (cfg.outPort[i].typ != PortTypeDisabled
mjr 89:c43cd923401c 965 && (cfg.outPort[i].flags & PortFlagFlipperLogic) != 0)
mjr 89:c43cd923401c 966 ++n;
mjr 89:c43cd923401c 967 }
mjr 89:c43cd923401c 968
mjr 89:c43cd923401c 969 // allocate space for the pending timer list
mjr 89:c43cd923401c 970 pending = new LwFlipperLogicOut*[n];
mjr 89:c43cd923401c 971
mjr 89:c43cd923401c 972 // there's nothing in the pending list yet
mjr 89:c43cd923401c 973 nPending = 0;
mjr 89:c43cd923401c 974
mjr 89:c43cd923401c 975 // Start our shared timer. The epoch is arbitrary, since we only
mjr 89:c43cd923401c 976 // use it to figure elapsed times.
mjr 89:c43cd923401c 977 timer.start();
mjr 89:c43cd923401c 978 }
mjr 89:c43cd923401c 979
mjr 89:c43cd923401c 980 // Check for ports with pending timers. The main routine should
mjr 89:c43cd923401c 981 // call this on each iteration to process our state transitions.
mjr 89:c43cd923401c 982 static void poll()
mjr 89:c43cd923401c 983 {
mjr 89:c43cd923401c 984 // note the current time
mjr 89:c43cd923401c 985 uint32_t t = timer.read_us();
mjr 89:c43cd923401c 986
mjr 89:c43cd923401c 987 // go through the timer list
mjr 89:c43cd923401c 988 for (int i = 0 ; i < nPending ; )
mjr 89:c43cd923401c 989 {
mjr 89:c43cd923401c 990 // get the port
mjr 89:c43cd923401c 991 LwFlipperLogicOut *port = pending[i];
mjr 89:c43cd923401c 992
mjr 89:c43cd923401c 993 // assume we'll keep it
mjr 89:c43cd923401c 994 bool remove = false;
mjr 89:c43cd923401c 995
mjr 89:c43cd923401c 996 // check if the port is still on
mjr 89:c43cd923401c 997 if (port->state != 0)
mjr 89:c43cd923401c 998 {
mjr 89:c43cd923401c 999 // it's still on - check if the initial full power time has elapsed
mjr 89:c43cd923401c 1000 if (uint32_t(t - port->t0) > port->fullPowerTime_us())
mjr 89:c43cd923401c 1001 {
mjr 89:c43cd923401c 1002 // done with the full power interval - switch to hold state
mjr 89:c43cd923401c 1003 port->state = 2;
mjr 89:c43cd923401c 1004
mjr 89:c43cd923401c 1005 // set the physical port to the hold power setting or the
mjr 89:c43cd923401c 1006 // client brightness setting, whichever is lower
mjr 89:c43cd923401c 1007 uint8_t hold = port->holdPower();
mjr 89:c43cd923401c 1008 uint8_t val = port->val;
mjr 89:c43cd923401c 1009 port->out->set(val < hold ? val : hold);
mjr 89:c43cd923401c 1010
mjr 89:c43cd923401c 1011 // we're done with the timer
mjr 89:c43cd923401c 1012 remove = true;
mjr 89:c43cd923401c 1013 }
mjr 89:c43cd923401c 1014 }
mjr 89:c43cd923401c 1015 else
mjr 89:c43cd923401c 1016 {
mjr 89:c43cd923401c 1017 // the port was turned off before the timer expired - remove
mjr 89:c43cd923401c 1018 // it from the timer list
mjr 89:c43cd923401c 1019 remove = true;
mjr 89:c43cd923401c 1020 }
mjr 89:c43cd923401c 1021
mjr 89:c43cd923401c 1022 // if desired, remove the port from the timer list
mjr 89:c43cd923401c 1023 if (remove)
mjr 89:c43cd923401c 1024 {
mjr 89:c43cd923401c 1025 // Remove the list entry by overwriting the slot with
mjr 89:c43cd923401c 1026 // the last entry in the list.
mjr 89:c43cd923401c 1027 pending[i] = pending[--nPending];
mjr 89:c43cd923401c 1028
mjr 89:c43cd923401c 1029 // Note that we don't increment the loop counter, since
mjr 89:c43cd923401c 1030 // we now need to revisit this same slot.
mjr 89:c43cd923401c 1031 }
mjr 89:c43cd923401c 1032 else
mjr 89:c43cd923401c 1033 {
mjr 89:c43cd923401c 1034 // we're keeping this item; move on to the next one
mjr 89:c43cd923401c 1035 ++i;
mjr 89:c43cd923401c 1036 }
mjr 89:c43cd923401c 1037 }
mjr 89:c43cd923401c 1038 }
mjr 89:c43cd923401c 1039
mjr 89:c43cd923401c 1040 protected:
mjr 89:c43cd923401c 1041 // underlying physical output
mjr 89:c43cd923401c 1042 LwOut *out;
mjr 89:c43cd923401c 1043
mjr 89:c43cd923401c 1044 // Timestamp on 'timer' of start of full-power interval. We set this
mjr 89:c43cd923401c 1045 // to the current 'timer' timestamp when entering state 1.
mjr 89:c43cd923401c 1046 uint32_t t0;
mjr 89:c43cd923401c 1047
mjr 89:c43cd923401c 1048 // Nominal output level (brightness) last set by the client. During
mjr 89:c43cd923401c 1049 // the initial full-power interval, we replicate the requested level
mjr 89:c43cd923401c 1050 // exactly on the physical output. During the hold interval, we limit
mjr 89:c43cd923401c 1051 // the physical output to the hold power, but use the caller's value
mjr 89:c43cd923401c 1052 // if it's lower.
mjr 89:c43cd923401c 1053 uint8_t val;
mjr 89:c43cd923401c 1054
mjr 89:c43cd923401c 1055 // Current port state:
mjr 89:c43cd923401c 1056 //
mjr 89:c43cd923401c 1057 // 0 = off
mjr 89:c43cd923401c 1058 // 1 = on at initial full power
mjr 89:c43cd923401c 1059 // 2 = on at hold power
mjr 89:c43cd923401c 1060 uint8_t state;
mjr 89:c43cd923401c 1061
mjr 89:c43cd923401c 1062 // Configuration parameters. The high 4 bits encode the initial full-
mjr 89:c43cd923401c 1063 // power time in 50ms units, starting at 0=50ms. The low 4 bits encode
mjr 89:c43cd923401c 1064 // the hold power (applied after the initial time expires if the output
mjr 89:c43cd923401c 1065 // is still on) in units of 6.66%. The resulting percentage is used
mjr 89:c43cd923401c 1066 // for the PWM duty cycle of the physical output.
mjr 89:c43cd923401c 1067 uint8_t params;
mjr 89:c43cd923401c 1068
mjr 99:8139b0c274f4 1069 // Figure the initial full-power time in microseconds: 50ms * (1+N),
mjr 99:8139b0c274f4 1070 // where N is the high 4 bits of the parameter byte.
mjr 99:8139b0c274f4 1071 inline uint32_t fullPowerTime_us() const { return 50000*(1 + ((params >> 4) & 0x0F)); }
mjr 89:c43cd923401c 1072
mjr 89:c43cd923401c 1073 // Figure the hold power PWM level (0-255)
mjr 89:c43cd923401c 1074 inline uint8_t holdPower() const { return (params & 0x0F) * 17; }
mjr 89:c43cd923401c 1075
mjr 89:c43cd923401c 1076 // Timer. This is a shared timer for all of the FL ports. When we
mjr 89:c43cd923401c 1077 // transition from OFF to ON, we note the current time on this timer
mjr 89:c43cd923401c 1078 // (which runs continuously).
mjr 89:c43cd923401c 1079 static Timer timer;
mjr 89:c43cd923401c 1080
mjr 89:c43cd923401c 1081 // Flipper logic pending timer list. Whenever a flipper logic output
mjr 98:4df3c0f7e707 1082 // transitions from OFF to ON, we add it to this list. We scan the
mjr 98:4df3c0f7e707 1083 // list in our polling routine to find ports that have reached the
mjr 98:4df3c0f7e707 1084 // expiration of their initial full-power intervals.
mjr 89:c43cd923401c 1085 static LwFlipperLogicOut **pending;
mjr 89:c43cd923401c 1086 static uint8_t nPending;
mjr 89:c43cd923401c 1087 };
mjr 89:c43cd923401c 1088
mjr 89:c43cd923401c 1089 // Flipper Logic statics
mjr 89:c43cd923401c 1090 Timer LwFlipperLogicOut::timer;
mjr 89:c43cd923401c 1091 LwFlipperLogicOut **LwFlipperLogicOut::pending;
mjr 89:c43cd923401c 1092 uint8_t LwFlipperLogicOut::nPending;
mjr 99:8139b0c274f4 1093
mjr 99:8139b0c274f4 1094 // Chime Logic. This is a filter output that we layer on a physical
mjr 99:8139b0c274f4 1095 // output to set a minimum and maximum ON time for the output.
mjr 99:8139b0c274f4 1096 class LwChimeLogicOut: public LwOut
mjr 98:4df3c0f7e707 1097 {
mjr 98:4df3c0f7e707 1098 public:
mjr 99:8139b0c274f4 1099 // Set up the output. 'params' encodes the minimum and maximum time.
mjr 99:8139b0c274f4 1100 LwChimeLogicOut(LwOut *o, uint8_t params)
mjr 99:8139b0c274f4 1101 : out(o), params(params)
mjr 98:4df3c0f7e707 1102 {
mjr 98:4df3c0f7e707 1103 // initially OFF
mjr 98:4df3c0f7e707 1104 state = 0;
mjr 98:4df3c0f7e707 1105 }
mjr 98:4df3c0f7e707 1106
mjr 98:4df3c0f7e707 1107 virtual void set(uint8_t level)
mjr 98:4df3c0f7e707 1108 {
mjr 98:4df3c0f7e707 1109 // update the physical output according to our current timing state
mjr 98:4df3c0f7e707 1110 switch (state)
mjr 98:4df3c0f7e707 1111 {
mjr 98:4df3c0f7e707 1112 case 0:
mjr 98:4df3c0f7e707 1113 // We're currently off. If the new level is non-zero, switch
mjr 98:4df3c0f7e707 1114 // to state 1 (initial minimum interval) and set the requested
mjr 98:4df3c0f7e707 1115 // level. If the new level is zero, we're switching from off to
mjr 98:4df3c0f7e707 1116 // off, so there's no change.
mjr 98:4df3c0f7e707 1117 if (level != 0)
mjr 98:4df3c0f7e707 1118 {
mjr 98:4df3c0f7e707 1119 // switch to state 1 (initial minimum interval, port is
mjr 98:4df3c0f7e707 1120 // logically on)
mjr 98:4df3c0f7e707 1121 state = 1;
mjr 98:4df3c0f7e707 1122
mjr 98:4df3c0f7e707 1123 // set the requested output level
mjr 98:4df3c0f7e707 1124 out->set(level);
mjr 98:4df3c0f7e707 1125
mjr 98:4df3c0f7e707 1126 // add myself to the pending timer list
mjr 98:4df3c0f7e707 1127 pending[nPending++] = this;
mjr 98:4df3c0f7e707 1128
mjr 98:4df3c0f7e707 1129 // note the starting time
mjr 98:4df3c0f7e707 1130 t0 = timer.read_us();
mjr 98:4df3c0f7e707 1131 }
mjr 98:4df3c0f7e707 1132 break;
mjr 98:4df3c0f7e707 1133
mjr 98:4df3c0f7e707 1134 case 1: // min ON interval, port on
mjr 98:4df3c0f7e707 1135 case 2: // min ON interval, port off
mjr 98:4df3c0f7e707 1136 // We're in the initial minimum ON interval. If the new power
mjr 98:4df3c0f7e707 1137 // level is non-zero, pass it through to the physical port, since
mjr 98:4df3c0f7e707 1138 // the client is allowed to change the power level during the
mjr 98:4df3c0f7e707 1139 // initial ON interval - they just can't turn it off entirely.
mjr 98:4df3c0f7e707 1140 // Set the state to 1 to indicate that the logical port is on.
mjr 98:4df3c0f7e707 1141 //
mjr 98:4df3c0f7e707 1142 // If the new level is zero, leave the underlying port at its
mjr 98:4df3c0f7e707 1143 // current power level, since we're not allowed to turn it off
mjr 98:4df3c0f7e707 1144 // during this period. Set the state to 2 to indicate that the
mjr 98:4df3c0f7e707 1145 // logical port is off even though the physical port has to stay
mjr 98:4df3c0f7e707 1146 // on for the remainder of the interval.
mjr 98:4df3c0f7e707 1147 if (level != 0)
mjr 98:4df3c0f7e707 1148 {
mjr 98:4df3c0f7e707 1149 // client is leaving the port on - pass through the new
mjr 98:4df3c0f7e707 1150 // power level and set state 1 (logically on)
mjr 98:4df3c0f7e707 1151 out->set(level);
mjr 98:4df3c0f7e707 1152 state = 1;
mjr 98:4df3c0f7e707 1153 }
mjr 98:4df3c0f7e707 1154 else
mjr 98:4df3c0f7e707 1155 {
mjr 98:4df3c0f7e707 1156 // Client is turning off the port - leave the underlying port
mjr 98:4df3c0f7e707 1157 // on at its current level and set state 2 (logically off).
mjr 98:4df3c0f7e707 1158 // When the minimum ON time expires, the polling routine will
mjr 98:4df3c0f7e707 1159 // see that we're logically off and will pass that through to
mjr 98:4df3c0f7e707 1160 // the underlying physical port. Until then, though, we have
mjr 98:4df3c0f7e707 1161 // to leave the physical port on to satisfy the minimum ON
mjr 98:4df3c0f7e707 1162 // time requirement.
mjr 98:4df3c0f7e707 1163 state = 2;
mjr 98:4df3c0f7e707 1164 }
mjr 98:4df3c0f7e707 1165 break;
mjr 98:4df3c0f7e707 1166
mjr 98:4df3c0f7e707 1167 case 3:
mjr 99:8139b0c274f4 1168 // We're after the minimum ON interval and before the maximum
mjr 99:8139b0c274f4 1169 // ON time limit. We can set any new level, including fully off.
mjr 99:8139b0c274f4 1170 // Pass the new power level through to the port.
mjr 98:4df3c0f7e707 1171 out->set(level);
mjr 98:4df3c0f7e707 1172
mjr 98:4df3c0f7e707 1173 // if the port is now off, return to state 0 (OFF)
mjr 98:4df3c0f7e707 1174 if (level == 0)
mjr 99:8139b0c274f4 1175 {
mjr 99:8139b0c274f4 1176 // return to the OFF state
mjr 99:8139b0c274f4 1177 state = 0;
mjr 99:8139b0c274f4 1178
mjr 99:8139b0c274f4 1179 // If we have a timer pending, remove it. A timer will be
mjr 99:8139b0c274f4 1180 // pending if we have a non-infinite maximum on time for the
mjr 99:8139b0c274f4 1181 // port.
mjr 99:8139b0c274f4 1182 for (int i = 0 ; i < nPending ; ++i)
mjr 99:8139b0c274f4 1183 {
mjr 99:8139b0c274f4 1184 // is this us?
mjr 99:8139b0c274f4 1185 if (pending[i] == this)
mjr 99:8139b0c274f4 1186 {
mjr 99:8139b0c274f4 1187 // remove myself by replacing the slot with the
mjr 99:8139b0c274f4 1188 // last list entry
mjr 99:8139b0c274f4 1189 pending[i] = pending[--nPending];
mjr 99:8139b0c274f4 1190
mjr 99:8139b0c274f4 1191 // no need to look any further
mjr 99:8139b0c274f4 1192 break;
mjr 99:8139b0c274f4 1193 }
mjr 99:8139b0c274f4 1194 }
mjr 99:8139b0c274f4 1195 }
mjr 99:8139b0c274f4 1196 break;
mjr 99:8139b0c274f4 1197
mjr 99:8139b0c274f4 1198 case 4:
mjr 99:8139b0c274f4 1199 // We're after the maximum ON time. The physical port stays off
mjr 99:8139b0c274f4 1200 // during this interval, so we don't pass any changes through to
mjr 99:8139b0c274f4 1201 // the physical port. When the client sets the level to 0, we
mjr 99:8139b0c274f4 1202 // turn off the logical port and reset to state 0.
mjr 99:8139b0c274f4 1203 if (level == 0)
mjr 98:4df3c0f7e707 1204 state = 0;
mjr 98:4df3c0f7e707 1205 break;
mjr 98:4df3c0f7e707 1206 }
mjr 98:4df3c0f7e707 1207 }
mjr 98:4df3c0f7e707 1208
mjr 98:4df3c0f7e707 1209 // Class initialization
mjr 98:4df3c0f7e707 1210 static void classInit(Config &cfg)
mjr 98:4df3c0f7e707 1211 {
mjr 98:4df3c0f7e707 1212 // Count the Minimum On Time outputs in the configuration. We
mjr 98:4df3c0f7e707 1213 // need to allocate enough pending timer list space to accommodate
mjr 98:4df3c0f7e707 1214 // all of these outputs.
mjr 98:4df3c0f7e707 1215 int n = 0;
mjr 98:4df3c0f7e707 1216 for (int i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 98:4df3c0f7e707 1217 {
mjr 98:4df3c0f7e707 1218 // if this port is active and marked as Flipper Logic, count it
mjr 98:4df3c0f7e707 1219 if (cfg.outPort[i].typ != PortTypeDisabled
mjr 99:8139b0c274f4 1220 && (cfg.outPort[i].flags & PortFlagChimeLogic) != 0)
mjr 98:4df3c0f7e707 1221 ++n;
mjr 98:4df3c0f7e707 1222 }
mjr 98:4df3c0f7e707 1223
mjr 98:4df3c0f7e707 1224 // allocate space for the pending timer list
mjr 99:8139b0c274f4 1225 pending = new LwChimeLogicOut*[n];
mjr 98:4df3c0f7e707 1226
mjr 98:4df3c0f7e707 1227 // there's nothing in the pending list yet
mjr 98:4df3c0f7e707 1228 nPending = 0;
mjr 98:4df3c0f7e707 1229
mjr 98:4df3c0f7e707 1230 // Start our shared timer. The epoch is arbitrary, since we only
mjr 98:4df3c0f7e707 1231 // use it to figure elapsed times.
mjr 98:4df3c0f7e707 1232 timer.start();
mjr 98:4df3c0f7e707 1233 }
mjr 98:4df3c0f7e707 1234
mjr 98:4df3c0f7e707 1235 // Check for ports with pending timers. The main routine should
mjr 98:4df3c0f7e707 1236 // call this on each iteration to process our state transitions.
mjr 98:4df3c0f7e707 1237 static void poll()
mjr 98:4df3c0f7e707 1238 {
mjr 98:4df3c0f7e707 1239 // note the current time
mjr 98:4df3c0f7e707 1240 uint32_t t = timer.read_us();
mjr 98:4df3c0f7e707 1241
mjr 98:4df3c0f7e707 1242 // go through the timer list
mjr 98:4df3c0f7e707 1243 for (int i = 0 ; i < nPending ; )
mjr 98:4df3c0f7e707 1244 {
mjr 98:4df3c0f7e707 1245 // get the port
mjr 99:8139b0c274f4 1246 LwChimeLogicOut *port = pending[i];
mjr 98:4df3c0f7e707 1247
mjr 98:4df3c0f7e707 1248 // assume we'll keep it
mjr 98:4df3c0f7e707 1249 bool remove = false;
mjr 98:4df3c0f7e707 1250
mjr 99:8139b0c274f4 1251 // check our state
mjr 99:8139b0c274f4 1252 switch (port->state)
mjr 98:4df3c0f7e707 1253 {
mjr 99:8139b0c274f4 1254 case 1: // initial minimum ON time, port logically on
mjr 99:8139b0c274f4 1255 case 2: // initial minimum ON time, port logically off
mjr 99:8139b0c274f4 1256 // check if the minimum ON time has elapsed
mjr 98:4df3c0f7e707 1257 if (uint32_t(t - port->t0) > port->minOnTime_us())
mjr 98:4df3c0f7e707 1258 {
mjr 98:4df3c0f7e707 1259 // This port has completed its initial ON interval, so
mjr 98:4df3c0f7e707 1260 // it advances to the next state.
mjr 98:4df3c0f7e707 1261 if (port->state == 1)
mjr 98:4df3c0f7e707 1262 {
mjr 99:8139b0c274f4 1263 // The port is logically on, so advance to state 3.
mjr 99:8139b0c274f4 1264 // The underlying port is already at its proper level,
mjr 99:8139b0c274f4 1265 // since we pass through non-zero power settings to the
mjr 99:8139b0c274f4 1266 // underlying port throughout the initial minimum time.
mjr 99:8139b0c274f4 1267 // The timer stays active into state 3.
mjr 98:4df3c0f7e707 1268 port->state = 3;
mjr 99:8139b0c274f4 1269
mjr 99:8139b0c274f4 1270 // Special case: maximum on time 0 means "infinite".
mjr 99:8139b0c274f4 1271 // There's no need for a timer in this case; we'll
mjr 99:8139b0c274f4 1272 // just stay in state 3 until the client turns the
mjr 99:8139b0c274f4 1273 // port off.
mjr 99:8139b0c274f4 1274 if (port->maxOnTime_us() == 0)
mjr 99:8139b0c274f4 1275 remove = true;
mjr 98:4df3c0f7e707 1276 }
mjr 98:4df3c0f7e707 1277 else
mjr 98:4df3c0f7e707 1278 {
mjr 98:4df3c0f7e707 1279 // The port was switched off by the client during the
mjr 98:4df3c0f7e707 1280 // minimum ON period. We haven't passed the OFF state
mjr 98:4df3c0f7e707 1281 // to the underlying port yet, because the port has to
mjr 98:4df3c0f7e707 1282 // stay on throughout the minimum ON period. So turn
mjr 98:4df3c0f7e707 1283 // the port off now.
mjr 98:4df3c0f7e707 1284 port->out->set(0);
mjr 98:4df3c0f7e707 1285
mjr 98:4df3c0f7e707 1286 // return to state 0 (OFF)
mjr 98:4df3c0f7e707 1287 port->state = 0;
mjr 99:8139b0c274f4 1288
mjr 99:8139b0c274f4 1289 // we're done with the timer
mjr 99:8139b0c274f4 1290 remove = true;
mjr 98:4df3c0f7e707 1291 }
mjr 99:8139b0c274f4 1292 }
mjr 99:8139b0c274f4 1293 break;
mjr 99:8139b0c274f4 1294
mjr 99:8139b0c274f4 1295 case 3: // between minimum ON time and maximum ON time
mjr 99:8139b0c274f4 1296 // check if the maximum ON time has expired
mjr 99:8139b0c274f4 1297 if (uint32_t(t - port->t0) > port->maxOnTime_us())
mjr 99:8139b0c274f4 1298 {
mjr 99:8139b0c274f4 1299 // The maximum ON time has expired. Turn off the physical
mjr 99:8139b0c274f4 1300 // port.
mjr 99:8139b0c274f4 1301 port->out->set(0);
mjr 98:4df3c0f7e707 1302
mjr 99:8139b0c274f4 1303 // Switch to state 4 (logically ON past maximum time)
mjr 99:8139b0c274f4 1304 port->state = 4;
mjr 99:8139b0c274f4 1305
mjr 99:8139b0c274f4 1306 // Remove the timer on this port. This port simply stays
mjr 99:8139b0c274f4 1307 // in state 4 until the client turns off the port.
mjr 98:4df3c0f7e707 1308 remove = true;
mjr 98:4df3c0f7e707 1309 }
mjr 99:8139b0c274f4 1310 break;
mjr 98:4df3c0f7e707 1311 }
mjr 98:4df3c0f7e707 1312
mjr 98:4df3c0f7e707 1313 // if desired, remove the port from the timer list
mjr 98:4df3c0f7e707 1314 if (remove)
mjr 98:4df3c0f7e707 1315 {
mjr 98:4df3c0f7e707 1316 // Remove the list entry by overwriting the slot with
mjr 98:4df3c0f7e707 1317 // the last entry in the list.
mjr 98:4df3c0f7e707 1318 pending[i] = pending[--nPending];
mjr 98:4df3c0f7e707 1319
mjr 98:4df3c0f7e707 1320 // Note that we don't increment the loop counter, since
mjr 98:4df3c0f7e707 1321 // we now need to revisit this same slot.
mjr 98:4df3c0f7e707 1322 }
mjr 98:4df3c0f7e707 1323 else
mjr 98:4df3c0f7e707 1324 {
mjr 98:4df3c0f7e707 1325 // we're keeping this item; move on to the next one
mjr 98:4df3c0f7e707 1326 ++i;
mjr 98:4df3c0f7e707 1327 }
mjr 98:4df3c0f7e707 1328 }
mjr 98:4df3c0f7e707 1329 }
mjr 98:4df3c0f7e707 1330
mjr 98:4df3c0f7e707 1331 protected:
mjr 98:4df3c0f7e707 1332 // underlying physical output
mjr 98:4df3c0f7e707 1333 LwOut *out;
mjr 98:4df3c0f7e707 1334
mjr 98:4df3c0f7e707 1335 // Timestamp on 'timer' of start of full-power interval. We set this
mjr 98:4df3c0f7e707 1336 // to the current 'timer' timestamp when entering state 1.
mjr 98:4df3c0f7e707 1337 uint32_t t0;
mjr 98:4df3c0f7e707 1338
mjr 98:4df3c0f7e707 1339 // Current port state:
mjr 98:4df3c0f7e707 1340 //
mjr 98:4df3c0f7e707 1341 // 0 = off
mjr 99:8139b0c274f4 1342 // 1 = in initial minimum ON interval, logical port is on
mjr 99:8139b0c274f4 1343 // 2 = in initial minimum ON interval, logical port is off
mjr 99:8139b0c274f4 1344 // 3 = in interval between minimum and maximum ON times
mjr 99:8139b0c274f4 1345 // 4 = after the maximum ON interval
mjr 99:8139b0c274f4 1346 //
mjr 99:8139b0c274f4 1347 // The "logical" on/off state of the port is the state set by the
mjr 99:8139b0c274f4 1348 // client. The "physical" state is the state of the underlying port.
mjr 99:8139b0c274f4 1349 // The relationships between logical and physical port state, and the
mjr 99:8139b0c274f4 1350 // effects of updates by the client, are as follows:
mjr 99:8139b0c274f4 1351 //
mjr 99:8139b0c274f4 1352 // State | Logical | Physical | Client set on | Client set off
mjr 99:8139b0c274f4 1353 // -----------------------------------------------------------
mjr 99:8139b0c274f4 1354 // 0 | Off | Off | phys on, -> 1 | no effect
mjr 99:8139b0c274f4 1355 // 1 | On | On | no effect | -> 2
mjr 99:8139b0c274f4 1356 // 2 | Off | On | -> 1 | no effect
mjr 99:8139b0c274f4 1357 // 3 | On | On | no effect | phys off, -> 0
mjr 99:8139b0c274f4 1358 // 4 | On | On | no effect | phys off, -> 0
mjr 99:8139b0c274f4 1359 //
mjr 99:8139b0c274f4 1360 // The polling routine makes the following transitions when the current
mjr 99:8139b0c274f4 1361 // time limit expires:
mjr 99:8139b0c274f4 1362 //
mjr 99:8139b0c274f4 1363 // 1: at end of minimum ON, -> 3 (or 4 if max == infinity)
mjr 99:8139b0c274f4 1364 // 2: at end of minimum ON, port off, -> 0
mjr 99:8139b0c274f4 1365 // 3: at end of maximum ON, port off, -> 4
mjr 98:4df3c0f7e707 1366 //
mjr 98:4df3c0f7e707 1367 uint8_t state;
mjr 98:4df3c0f7e707 1368
mjr 99:8139b0c274f4 1369 // Configuration parameters byte. This encodes the minimum and maximum
mjr 99:8139b0c274f4 1370 // ON times.
mjr 99:8139b0c274f4 1371 uint8_t params;
mjr 98:4df3c0f7e707 1372
mjr 98:4df3c0f7e707 1373 // Timer. This is a shared timer for all of the minimum ON time ports.
mjr 98:4df3c0f7e707 1374 // When we transition from OFF to ON, we note the current time on this
mjr 98:4df3c0f7e707 1375 // timer to establish the start of our minimum ON period.
mjr 98:4df3c0f7e707 1376 static Timer timer;
mjr 98:4df3c0f7e707 1377
mjr 98:4df3c0f7e707 1378 // translaton table from timing parameter in config to minimum ON time
mjr 98:4df3c0f7e707 1379 static const uint32_t paramToTime_us[];
mjr 98:4df3c0f7e707 1380
mjr 99:8139b0c274f4 1381 // Figure the minimum ON time. The minimum ON time is given by the
mjr 99:8139b0c274f4 1382 // low-order 4 bits of the parameters byte, which serves as an index
mjr 99:8139b0c274f4 1383 // into our time table.
mjr 99:8139b0c274f4 1384 inline uint32_t minOnTime_us() const { return paramToTime_us[params & 0x0F]; }
mjr 99:8139b0c274f4 1385
mjr 99:8139b0c274f4 1386 // Figure the maximum ON time. The maximum time is the high 4 bits
mjr 99:8139b0c274f4 1387 // of the parameters byte. This is an index into our time table, but
mjr 99:8139b0c274f4 1388 // 0 has the special meaning "infinite".
mjr 99:8139b0c274f4 1389 inline uint32_t maxOnTime_us() const { return paramToTime_us[((params >> 4) & 0x0F)]; }
mjr 98:4df3c0f7e707 1390
mjr 98:4df3c0f7e707 1391 // Pending timer list. Whenever one of our ports transitions from OFF
mjr 98:4df3c0f7e707 1392 // to ON, we add it to this list. We scan this list in our polling
mjr 98:4df3c0f7e707 1393 // routine to find ports that have reached the ends of their initial
mjr 98:4df3c0f7e707 1394 // ON intervals.
mjr 99:8139b0c274f4 1395 static LwChimeLogicOut **pending;
mjr 98:4df3c0f7e707 1396 static uint8_t nPending;
mjr 98:4df3c0f7e707 1397 };
mjr 98:4df3c0f7e707 1398
mjr 98:4df3c0f7e707 1399 // Min Time Out statics
mjr 99:8139b0c274f4 1400 Timer LwChimeLogicOut::timer;
mjr 99:8139b0c274f4 1401 LwChimeLogicOut **LwChimeLogicOut::pending;
mjr 99:8139b0c274f4 1402 uint8_t LwChimeLogicOut::nPending;
mjr 99:8139b0c274f4 1403 const uint32_t LwChimeLogicOut::paramToTime_us[] = {
mjr 99:8139b0c274f4 1404 0, // for the max time, this means "infinite"
mjr 98:4df3c0f7e707 1405 1000,
mjr 98:4df3c0f7e707 1406 2000,
mjr 98:4df3c0f7e707 1407 5000,
mjr 98:4df3c0f7e707 1408 10000,
mjr 98:4df3c0f7e707 1409 20000,
mjr 98:4df3c0f7e707 1410 40000,
mjr 98:4df3c0f7e707 1411 80000,
mjr 98:4df3c0f7e707 1412 100000,
mjr 98:4df3c0f7e707 1413 200000,
mjr 98:4df3c0f7e707 1414 300000,
mjr 98:4df3c0f7e707 1415 400000,
mjr 98:4df3c0f7e707 1416 500000,
mjr 98:4df3c0f7e707 1417 600000,
mjr 98:4df3c0f7e707 1418 700000,
mjr 98:4df3c0f7e707 1419 800000
mjr 98:4df3c0f7e707 1420 };
mjr 89:c43cd923401c 1421
mjr 35:e959ffba78fd 1422 //
mjr 35:e959ffba78fd 1423 // The TLC5940 interface object. We'll set this up with the port
mjr 35:e959ffba78fd 1424 // assignments set in config.h.
mjr 33:d832bcab089e 1425 //
mjr 35:e959ffba78fd 1426 TLC5940 *tlc5940 = 0;
mjr 35:e959ffba78fd 1427 void init_tlc5940(Config &cfg)
mjr 35:e959ffba78fd 1428 {
mjr 35:e959ffba78fd 1429 if (cfg.tlc5940.nchips != 0)
mjr 35:e959ffba78fd 1430 {
mjr 53:9b2611964afc 1431 tlc5940 = new TLC5940(
mjr 53:9b2611964afc 1432 wirePinName(cfg.tlc5940.sclk),
mjr 53:9b2611964afc 1433 wirePinName(cfg.tlc5940.sin),
mjr 53:9b2611964afc 1434 wirePinName(cfg.tlc5940.gsclk),
mjr 53:9b2611964afc 1435 wirePinName(cfg.tlc5940.blank),
mjr 53:9b2611964afc 1436 wirePinName(cfg.tlc5940.xlat),
mjr 53:9b2611964afc 1437 cfg.tlc5940.nchips);
mjr 35:e959ffba78fd 1438 }
mjr 35:e959ffba78fd 1439 }
mjr 26:cb71c4af2912 1440
mjr 40:cc0d9814522b 1441 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level
mjr 40:cc0d9814522b 1442 static const uint16_t dof_to_tlc[] = {
mjr 40:cc0d9814522b 1443 0, 16, 32, 48, 64, 80, 96, 112, 128, 145, 161, 177, 193, 209, 225, 241,
mjr 40:cc0d9814522b 1444 257, 273, 289, 305, 321, 337, 353, 369, 385, 401, 418, 434, 450, 466, 482, 498,
mjr 40:cc0d9814522b 1445 514, 530, 546, 562, 578, 594, 610, 626, 642, 658, 674, 691, 707, 723, 739, 755,
mjr 40:cc0d9814522b 1446 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012,
mjr 40:cc0d9814522b 1447 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269,
mjr 40:cc0d9814522b 1448 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526,
mjr 40:cc0d9814522b 1449 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783,
mjr 40:cc0d9814522b 1450 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039,
mjr 40:cc0d9814522b 1451 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296,
mjr 40:cc0d9814522b 1452 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553,
mjr 40:cc0d9814522b 1453 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810,
mjr 40:cc0d9814522b 1454 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067,
mjr 40:cc0d9814522b 1455 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324,
mjr 40:cc0d9814522b 1456 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581,
mjr 40:cc0d9814522b 1457 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838,
mjr 40:cc0d9814522b 1458 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
mjr 40:cc0d9814522b 1459 };
mjr 40:cc0d9814522b 1460
mjr 40:cc0d9814522b 1461 // Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with
mjr 40:cc0d9814522b 1462 // gamma correction. Note that the output layering scheme can handle
mjr 40:cc0d9814522b 1463 // this without a separate table, by first applying gamma to the DOF
mjr 40:cc0d9814522b 1464 // level to produce an 8-bit gamma-corrected value, then convert that
mjr 40:cc0d9814522b 1465 // to the 12-bit TLC5940 value. But we get better precision by doing
mjr 40:cc0d9814522b 1466 // the gamma correction in the 12-bit TLC5940 domain. We can only
mjr 40:cc0d9814522b 1467 // get the 12-bit domain by combining both steps into one layering
mjr 40:cc0d9814522b 1468 // object, though, since the intermediate values in the layering system
mjr 40:cc0d9814522b 1469 // are always 8 bits.
mjr 40:cc0d9814522b 1470 static const uint16_t dof_to_gamma_tlc[] = {
mjr 40:cc0d9814522b 1471 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
mjr 40:cc0d9814522b 1472 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
mjr 40:cc0d9814522b 1473 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
mjr 40:cc0d9814522b 1474 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
mjr 40:cc0d9814522b 1475 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
mjr 40:cc0d9814522b 1476 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
mjr 40:cc0d9814522b 1477 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
mjr 40:cc0d9814522b 1478 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
mjr 40:cc0d9814522b 1479 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
mjr 40:cc0d9814522b 1480 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091,
mjr 40:cc0d9814522b 1481 1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427,
mjr 40:cc0d9814522b 1482 1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823,
mjr 40:cc0d9814522b 1483 1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284,
mjr 40:cc0d9814522b 1484 2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813,
mjr 40:cc0d9814522b 1485 2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416,
mjr 40:cc0d9814522b 1486 3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
mjr 40:cc0d9814522b 1487 };
mjr 40:cc0d9814522b 1488
mjr 26:cb71c4af2912 1489 // LwOut class for TLC5940 outputs. These are fully PWM capable.
mjr 26:cb71c4af2912 1490 // The 'idx' value in the constructor is the output index in the
mjr 26:cb71c4af2912 1491 // daisy-chained TLC5940 array. 0 is output #0 on the first chip,
mjr 26:cb71c4af2912 1492 // 1 is #1 on the first chip, 15 is #15 on the first chip, 16 is
mjr 26:cb71c4af2912 1493 // #0 on the second chip, 32 is #0 on the third chip, etc.
mjr 26:cb71c4af2912 1494 class Lw5940Out: public LwOut
mjr 26:cb71c4af2912 1495 {
mjr 26:cb71c4af2912 1496 public:
mjr 60:f38da020aa13 1497 Lw5940Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1498 virtual void set(uint8_t val)
mjr 26:cb71c4af2912 1499 {
mjr 26:cb71c4af2912 1500 if (val != prv)
mjr 40:cc0d9814522b 1501 tlc5940->set(idx, dof_to_tlc[prv = val]);
mjr 26:cb71c4af2912 1502 }
mjr 60:f38da020aa13 1503 uint8_t idx;
mjr 40:cc0d9814522b 1504 uint8_t prv;
mjr 26:cb71c4af2912 1505 };
mjr 26:cb71c4af2912 1506
mjr 40:cc0d9814522b 1507 // LwOut class for TLC5940 gamma-corrected outputs.
mjr 40:cc0d9814522b 1508 class Lw5940GammaOut: public LwOut
mjr 40:cc0d9814522b 1509 {
mjr 40:cc0d9814522b 1510 public:
mjr 60:f38da020aa13 1511 Lw5940GammaOut(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1512 virtual void set(uint8_t val)
mjr 40:cc0d9814522b 1513 {
mjr 40:cc0d9814522b 1514 if (val != prv)
mjr 40:cc0d9814522b 1515 tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
mjr 40:cc0d9814522b 1516 }
mjr 60:f38da020aa13 1517 uint8_t idx;
mjr 40:cc0d9814522b 1518 uint8_t prv;
mjr 40:cc0d9814522b 1519 };
mjr 40:cc0d9814522b 1520
mjr 87:8d35c74403af 1521 //
mjr 87:8d35c74403af 1522 // TLC59116 interface object
mjr 87:8d35c74403af 1523 //
mjr 87:8d35c74403af 1524 TLC59116 *tlc59116 = 0;
mjr 87:8d35c74403af 1525 void init_tlc59116(Config &cfg)
mjr 87:8d35c74403af 1526 {
mjr 87:8d35c74403af 1527 // Create the interface if any chips are enabled
mjr 87:8d35c74403af 1528 if (cfg.tlc59116.chipMask != 0)
mjr 87:8d35c74403af 1529 {
mjr 87:8d35c74403af 1530 // set up the interface
mjr 87:8d35c74403af 1531 tlc59116 = new TLC59116(
mjr 87:8d35c74403af 1532 wirePinName(cfg.tlc59116.sda),
mjr 87:8d35c74403af 1533 wirePinName(cfg.tlc59116.scl),
mjr 87:8d35c74403af 1534 wirePinName(cfg.tlc59116.reset));
mjr 87:8d35c74403af 1535
mjr 87:8d35c74403af 1536 // initialize the chips
mjr 87:8d35c74403af 1537 tlc59116->init();
mjr 87:8d35c74403af 1538 }
mjr 87:8d35c74403af 1539 }
mjr 87:8d35c74403af 1540
mjr 87:8d35c74403af 1541 // LwOut class for TLC59116 outputs. The 'addr' value in the constructor
mjr 87:8d35c74403af 1542 // is low 4 bits of the chip's I2C address; this is the part of the address
mjr 87:8d35c74403af 1543 // that's configurable per chip. 'port' is the output number on the chip
mjr 87:8d35c74403af 1544 // (0-15).
mjr 87:8d35c74403af 1545 //
mjr 87:8d35c74403af 1546 // Note that we don't need a separate gamma-corrected subclass for this
mjr 87:8d35c74403af 1547 // output type, since there's no loss of precision with the standard layered
mjr 87:8d35c74403af 1548 // gamma (it emits 8-bit values, and we take 8-bit inputs).
mjr 87:8d35c74403af 1549 class Lw59116Out: public LwOut
mjr 87:8d35c74403af 1550 {
mjr 87:8d35c74403af 1551 public:
mjr 87:8d35c74403af 1552 Lw59116Out(uint8_t addr, uint8_t port) : addr(addr), port(port) { prv = 0; }
mjr 87:8d35c74403af 1553 virtual void set(uint8_t val)
mjr 87:8d35c74403af 1554 {
mjr 87:8d35c74403af 1555 if (val != prv)
mjr 87:8d35c74403af 1556 tlc59116->set(addr, port, prv = val);
mjr 87:8d35c74403af 1557 }
mjr 87:8d35c74403af 1558
mjr 87:8d35c74403af 1559 protected:
mjr 87:8d35c74403af 1560 uint8_t addr;
mjr 87:8d35c74403af 1561 uint8_t port;
mjr 87:8d35c74403af 1562 uint8_t prv;
mjr 87:8d35c74403af 1563 };
mjr 87:8d35c74403af 1564
mjr 87:8d35c74403af 1565
mjr 87:8d35c74403af 1566 //
mjr 34:6b981a2afab7 1567 // 74HC595 interface object. Set this up with the port assignments in
mjr 34:6b981a2afab7 1568 // config.h.
mjr 87:8d35c74403af 1569 //
mjr 35:e959ffba78fd 1570 HC595 *hc595 = 0;
mjr 35:e959ffba78fd 1571
mjr 35:e959ffba78fd 1572 // initialize the 74HC595 interface
mjr 35:e959ffba78fd 1573 void init_hc595(Config &cfg)
mjr 35:e959ffba78fd 1574 {
mjr 35:e959ffba78fd 1575 if (cfg.hc595.nchips != 0)
mjr 35:e959ffba78fd 1576 {
mjr 53:9b2611964afc 1577 hc595 = new HC595(
mjr 53:9b2611964afc 1578 wirePinName(cfg.hc595.nchips),
mjr 53:9b2611964afc 1579 wirePinName(cfg.hc595.sin),
mjr 53:9b2611964afc 1580 wirePinName(cfg.hc595.sclk),
mjr 53:9b2611964afc 1581 wirePinName(cfg.hc595.latch),
mjr 53:9b2611964afc 1582 wirePinName(cfg.hc595.ena));
mjr 35:e959ffba78fd 1583 hc595->init();
mjr 35:e959ffba78fd 1584 hc595->update();
mjr 35:e959ffba78fd 1585 }
mjr 35:e959ffba78fd 1586 }
mjr 34:6b981a2afab7 1587
mjr 34:6b981a2afab7 1588 // LwOut class for 74HC595 outputs. These are simple digial outs.
mjr 34:6b981a2afab7 1589 // The 'idx' value in the constructor is the output index in the
mjr 34:6b981a2afab7 1590 // daisy-chained 74HC595 array. 0 is output #0 on the first chip,
mjr 34:6b981a2afab7 1591 // 1 is #1 on the first chip, 7 is #7 on the first chip, 8 is
mjr 34:6b981a2afab7 1592 // #0 on the second chip, etc.
mjr 34:6b981a2afab7 1593 class Lw595Out: public LwOut
mjr 33:d832bcab089e 1594 {
mjr 33:d832bcab089e 1595 public:
mjr 60:f38da020aa13 1596 Lw595Out(uint8_t idx) : idx(idx) { prv = 0; }
mjr 40:cc0d9814522b 1597 virtual void set(uint8_t val)
mjr 34:6b981a2afab7 1598 {
mjr 34:6b981a2afab7 1599 if (val != prv)
mjr 40:cc0d9814522b 1600 hc595->set(idx, (prv = val) == 0 ? 0 : 1);
mjr 34:6b981a2afab7 1601 }
mjr 60:f38da020aa13 1602 uint8_t idx;
mjr 40:cc0d9814522b 1603 uint8_t prv;
mjr 33:d832bcab089e 1604 };
mjr 33:d832bcab089e 1605
mjr 26:cb71c4af2912 1606
mjr 40:cc0d9814522b 1607
mjr 64:ef7ca92dff36 1608 // Conversion table - 8-bit DOF output level to PWM duty cycle,
mjr 64:ef7ca92dff36 1609 // normalized to 0.0 to 1.0 scale.
mjr 74:822a92bc11d2 1610 static const float dof_to_pwm[] = {
mjr 64:ef7ca92dff36 1611 0.000000f, 0.003922f, 0.007843f, 0.011765f, 0.015686f, 0.019608f, 0.023529f, 0.027451f,
mjr 64:ef7ca92dff36 1612 0.031373f, 0.035294f, 0.039216f, 0.043137f, 0.047059f, 0.050980f, 0.054902f, 0.058824f,
mjr 64:ef7ca92dff36 1613 0.062745f, 0.066667f, 0.070588f, 0.074510f, 0.078431f, 0.082353f, 0.086275f, 0.090196f,
mjr 64:ef7ca92dff36 1614 0.094118f, 0.098039f, 0.101961f, 0.105882f, 0.109804f, 0.113725f, 0.117647f, 0.121569f,
mjr 64:ef7ca92dff36 1615 0.125490f, 0.129412f, 0.133333f, 0.137255f, 0.141176f, 0.145098f, 0.149020f, 0.152941f,
mjr 64:ef7ca92dff36 1616 0.156863f, 0.160784f, 0.164706f, 0.168627f, 0.172549f, 0.176471f, 0.180392f, 0.184314f,
mjr 64:ef7ca92dff36 1617 0.188235f, 0.192157f, 0.196078f, 0.200000f, 0.203922f, 0.207843f, 0.211765f, 0.215686f,
mjr 64:ef7ca92dff36 1618 0.219608f, 0.223529f, 0.227451f, 0.231373f, 0.235294f, 0.239216f, 0.243137f, 0.247059f,
mjr 64:ef7ca92dff36 1619 0.250980f, 0.254902f, 0.258824f, 0.262745f, 0.266667f, 0.270588f, 0.274510f, 0.278431f,
mjr 64:ef7ca92dff36 1620 0.282353f, 0.286275f, 0.290196f, 0.294118f, 0.298039f, 0.301961f, 0.305882f, 0.309804f,
mjr 64:ef7ca92dff36 1621 0.313725f, 0.317647f, 0.321569f, 0.325490f, 0.329412f, 0.333333f, 0.337255f, 0.341176f,
mjr 64:ef7ca92dff36 1622 0.345098f, 0.349020f, 0.352941f, 0.356863f, 0.360784f, 0.364706f, 0.368627f, 0.372549f,
mjr 64:ef7ca92dff36 1623 0.376471f, 0.380392f, 0.384314f, 0.388235f, 0.392157f, 0.396078f, 0.400000f, 0.403922f,
mjr 64:ef7ca92dff36 1624 0.407843f, 0.411765f, 0.415686f, 0.419608f, 0.423529f, 0.427451f, 0.431373f, 0.435294f,
mjr 64:ef7ca92dff36 1625 0.439216f, 0.443137f, 0.447059f, 0.450980f, 0.454902f, 0.458824f, 0.462745f, 0.466667f,
mjr 64:ef7ca92dff36 1626 0.470588f, 0.474510f, 0.478431f, 0.482353f, 0.486275f, 0.490196f, 0.494118f, 0.498039f,
mjr 64:ef7ca92dff36 1627 0.501961f, 0.505882f, 0.509804f, 0.513725f, 0.517647f, 0.521569f, 0.525490f, 0.529412f,
mjr 64:ef7ca92dff36 1628 0.533333f, 0.537255f, 0.541176f, 0.545098f, 0.549020f, 0.552941f, 0.556863f, 0.560784f,
mjr 64:ef7ca92dff36 1629 0.564706f, 0.568627f, 0.572549f, 0.576471f, 0.580392f, 0.584314f, 0.588235f, 0.592157f,
mjr 64:ef7ca92dff36 1630 0.596078f, 0.600000f, 0.603922f, 0.607843f, 0.611765f, 0.615686f, 0.619608f, 0.623529f,
mjr 64:ef7ca92dff36 1631 0.627451f, 0.631373f, 0.635294f, 0.639216f, 0.643137f, 0.647059f, 0.650980f, 0.654902f,
mjr 64:ef7ca92dff36 1632 0.658824f, 0.662745f, 0.666667f, 0.670588f, 0.674510f, 0.678431f, 0.682353f, 0.686275f,
mjr 64:ef7ca92dff36 1633 0.690196f, 0.694118f, 0.698039f, 0.701961f, 0.705882f, 0.709804f, 0.713725f, 0.717647f,
mjr 64:ef7ca92dff36 1634 0.721569f, 0.725490f, 0.729412f, 0.733333f, 0.737255f, 0.741176f, 0.745098f, 0.749020f,
mjr 64:ef7ca92dff36 1635 0.752941f, 0.756863f, 0.760784f, 0.764706f, 0.768627f, 0.772549f, 0.776471f, 0.780392f,
mjr 64:ef7ca92dff36 1636 0.784314f, 0.788235f, 0.792157f, 0.796078f, 0.800000f, 0.803922f, 0.807843f, 0.811765f,
mjr 64:ef7ca92dff36 1637 0.815686f, 0.819608f, 0.823529f, 0.827451f, 0.831373f, 0.835294f, 0.839216f, 0.843137f,
mjr 64:ef7ca92dff36 1638 0.847059f, 0.850980f, 0.854902f, 0.858824f, 0.862745f, 0.866667f, 0.870588f, 0.874510f,
mjr 64:ef7ca92dff36 1639 0.878431f, 0.882353f, 0.886275f, 0.890196f, 0.894118f, 0.898039f, 0.901961f, 0.905882f,
mjr 64:ef7ca92dff36 1640 0.909804f, 0.913725f, 0.917647f, 0.921569f, 0.925490f, 0.929412f, 0.933333f, 0.937255f,
mjr 64:ef7ca92dff36 1641 0.941176f, 0.945098f, 0.949020f, 0.952941f, 0.956863f, 0.960784f, 0.964706f, 0.968627f,
mjr 64:ef7ca92dff36 1642 0.972549f, 0.976471f, 0.980392f, 0.984314f, 0.988235f, 0.992157f, 0.996078f, 1.000000f
mjr 40:cc0d9814522b 1643 };
mjr 26:cb71c4af2912 1644
mjr 64:ef7ca92dff36 1645
mjr 92:f264fbaa1be5 1646 // Conversion table for 8-bit DOF level to pulse width, with gamma correction
mjr 92:f264fbaa1be5 1647 // pre-calculated. The values are normalized duty cycles from 0.0 to 1.0.
mjr 92:f264fbaa1be5 1648 // Note that we could use the layered gamma output on top of the regular
mjr 92:f264fbaa1be5 1649 // LwPwmOut class for this instead of a separate table, but we get much better
mjr 92:f264fbaa1be5 1650 // precision with a dedicated table, because we apply gamma correction to the
mjr 92:f264fbaa1be5 1651 // actual duty cycle values (as 'float') rather than the 8-bit DOF values.
mjr 64:ef7ca92dff36 1652 static const float dof_to_gamma_pwm[] = {
mjr 64:ef7ca92dff36 1653 0.000000f, 0.000000f, 0.000001f, 0.000004f, 0.000009f, 0.000017f, 0.000028f, 0.000042f,
mjr 64:ef7ca92dff36 1654 0.000062f, 0.000086f, 0.000115f, 0.000151f, 0.000192f, 0.000240f, 0.000296f, 0.000359f,
mjr 64:ef7ca92dff36 1655 0.000430f, 0.000509f, 0.000598f, 0.000695f, 0.000803f, 0.000920f, 0.001048f, 0.001187f,
mjr 64:ef7ca92dff36 1656 0.001337f, 0.001499f, 0.001673f, 0.001860f, 0.002059f, 0.002272f, 0.002498f, 0.002738f,
mjr 64:ef7ca92dff36 1657 0.002993f, 0.003262f, 0.003547f, 0.003847f, 0.004162f, 0.004494f, 0.004843f, 0.005208f,
mjr 64:ef7ca92dff36 1658 0.005591f, 0.005991f, 0.006409f, 0.006845f, 0.007301f, 0.007775f, 0.008268f, 0.008781f,
mjr 64:ef7ca92dff36 1659 0.009315f, 0.009868f, 0.010442f, 0.011038f, 0.011655f, 0.012293f, 0.012954f, 0.013637f,
mjr 64:ef7ca92dff36 1660 0.014342f, 0.015071f, 0.015823f, 0.016599f, 0.017398f, 0.018223f, 0.019071f, 0.019945f,
mjr 64:ef7ca92dff36 1661 0.020844f, 0.021769f, 0.022720f, 0.023697f, 0.024701f, 0.025731f, 0.026789f, 0.027875f,
mjr 64:ef7ca92dff36 1662 0.028988f, 0.030129f, 0.031299f, 0.032498f, 0.033726f, 0.034983f, 0.036270f, 0.037587f,
mjr 64:ef7ca92dff36 1663 0.038935f, 0.040313f, 0.041722f, 0.043162f, 0.044634f, 0.046138f, 0.047674f, 0.049243f,
mjr 64:ef7ca92dff36 1664 0.050844f, 0.052478f, 0.054146f, 0.055847f, 0.057583f, 0.059353f, 0.061157f, 0.062996f,
mjr 64:ef7ca92dff36 1665 0.064870f, 0.066780f, 0.068726f, 0.070708f, 0.072726f, 0.074780f, 0.076872f, 0.079001f,
mjr 64:ef7ca92dff36 1666 0.081167f, 0.083371f, 0.085614f, 0.087895f, 0.090214f, 0.092572f, 0.094970f, 0.097407f,
mjr 64:ef7ca92dff36 1667 0.099884f, 0.102402f, 0.104959f, 0.107558f, 0.110197f, 0.112878f, 0.115600f, 0.118364f,
mjr 64:ef7ca92dff36 1668 0.121170f, 0.124019f, 0.126910f, 0.129844f, 0.132821f, 0.135842f, 0.138907f, 0.142016f,
mjr 64:ef7ca92dff36 1669 0.145170f, 0.148367f, 0.151610f, 0.154898f, 0.158232f, 0.161611f, 0.165037f, 0.168509f,
mjr 64:ef7ca92dff36 1670 0.172027f, 0.175592f, 0.179205f, 0.182864f, 0.186572f, 0.190327f, 0.194131f, 0.197983f,
mjr 64:ef7ca92dff36 1671 0.201884f, 0.205834f, 0.209834f, 0.213883f, 0.217982f, 0.222131f, 0.226330f, 0.230581f,
mjr 64:ef7ca92dff36 1672 0.234882f, 0.239234f, 0.243638f, 0.248094f, 0.252602f, 0.257162f, 0.261774f, 0.266440f,
mjr 64:ef7ca92dff36 1673 0.271159f, 0.275931f, 0.280756f, 0.285636f, 0.290570f, 0.295558f, 0.300601f, 0.305699f,
mjr 64:ef7ca92dff36 1674 0.310852f, 0.316061f, 0.321325f, 0.326645f, 0.332022f, 0.337456f, 0.342946f, 0.348493f,
mjr 64:ef7ca92dff36 1675 0.354098f, 0.359760f, 0.365480f, 0.371258f, 0.377095f, 0.382990f, 0.388944f, 0.394958f,
mjr 64:ef7ca92dff36 1676 0.401030f, 0.407163f, 0.413356f, 0.419608f, 0.425921f, 0.432295f, 0.438730f, 0.445226f,
mjr 64:ef7ca92dff36 1677 0.451784f, 0.458404f, 0.465085f, 0.471829f, 0.478635f, 0.485504f, 0.492436f, 0.499432f,
mjr 64:ef7ca92dff36 1678 0.506491f, 0.513614f, 0.520800f, 0.528052f, 0.535367f, 0.542748f, 0.550194f, 0.557705f,
mjr 64:ef7ca92dff36 1679 0.565282f, 0.572924f, 0.580633f, 0.588408f, 0.596249f, 0.604158f, 0.612133f, 0.620176f,
mjr 64:ef7ca92dff36 1680 0.628287f, 0.636465f, 0.644712f, 0.653027f, 0.661410f, 0.669863f, 0.678384f, 0.686975f,
mjr 64:ef7ca92dff36 1681 0.695636f, 0.704366f, 0.713167f, 0.722038f, 0.730979f, 0.739992f, 0.749075f, 0.758230f,
mjr 64:ef7ca92dff36 1682 0.767457f, 0.776755f, 0.786126f, 0.795568f, 0.805084f, 0.814672f, 0.824334f, 0.834068f,
mjr 64:ef7ca92dff36 1683 0.843877f, 0.853759f, 0.863715f, 0.873746f, 0.883851f, 0.894031f, 0.904286f, 0.914616f,
mjr 64:ef7ca92dff36 1684 0.925022f, 0.935504f, 0.946062f, 0.956696f, 0.967407f, 0.978194f, 0.989058f, 1.000000f
mjr 64:ef7ca92dff36 1685 };
mjr 64:ef7ca92dff36 1686
mjr 77:0b96f6867312 1687 // Polled-update PWM output list
mjr 74:822a92bc11d2 1688 //
mjr 77:0b96f6867312 1689 // This is a workaround for a KL25Z hardware bug/limitation. The bug (more
mjr 77:0b96f6867312 1690 // about this below) is that we can't write to a PWM output "value" register
mjr 77:0b96f6867312 1691 // more than once per PWM cycle; if we do, outputs after the first are lost.
mjr 77:0b96f6867312 1692 // The value register controls the duty cycle, so it's what you have to write
mjr 77:0b96f6867312 1693 // if you want to update the brightness of an output.
mjr 74:822a92bc11d2 1694 //
mjr 92:f264fbaa1be5 1695 // The symptom of the problem, if it's not worked around somehow, is that
mjr 92:f264fbaa1be5 1696 // an output will get "stuck" due to a missed write. This is especially
mjr 92:f264fbaa1be5 1697 // noticeable during a series of updates such as a fade. If the last
mjr 92:f264fbaa1be5 1698 // couple of updates in a fade are lost, the output will get stuck at some
mjr 92:f264fbaa1be5 1699 // value above or below the desired final value. The stuck setting will
mjr 92:f264fbaa1be5 1700 // persist until the output is deliberately changed again later.
mjr 92:f264fbaa1be5 1701 //
mjr 92:f264fbaa1be5 1702 // Our solution: Simply repeat all PWM updates periodically. This way, any
mjr 92:f264fbaa1be5 1703 // lost write will *eventually* take hold on one of the repeats. Repeats of
mjr 92:f264fbaa1be5 1704 // the same value won't change anything and thus won't be noticeable. We do
mjr 92:f264fbaa1be5 1705 // these periodic updates during the main loop, which makes them very low
mjr 92:f264fbaa1be5 1706 // overhead (there's no interrupt overhead; we just do them when convenient
mjr 92:f264fbaa1be5 1707 // in the main loop), and also makes them very frequent. The frequency
mjr 92:f264fbaa1be5 1708 // is crucial because it ensures that updates will never be lost for long
mjr 92:f264fbaa1be5 1709 // enough to become noticeable.
mjr 92:f264fbaa1be5 1710 //
mjr 92:f264fbaa1be5 1711 // The mbed library has its own, different solution to this bug, but the
mjr 92:f264fbaa1be5 1712 // mbed solution isn't really a solution at all because it creates a separate
mjr 92:f264fbaa1be5 1713 // problem of its own. The mbed approach is reset the TPM "count" register
mjr 92:f264fbaa1be5 1714 // on every value register write. The count reset truncates the current
mjr 92:f264fbaa1be5 1715 // PWM cycle, which bypasses the hardware problem. Remember, the hardware
mjr 92:f264fbaa1be5 1716 // problem is that you can only write once per cycle; the mbed "solution" gets
mjr 92:f264fbaa1be5 1717 // around that by making sure the cycle ends immediately after the write.
mjr 92:f264fbaa1be5 1718 // The problem with this approach is that the truncated cycle causes visible
mjr 92:f264fbaa1be5 1719 // flicker if the output is connected to an LED. This is particularly
mjr 92:f264fbaa1be5 1720 // noticeable during fades, when we're updating the value register repeatedly
mjr 92:f264fbaa1be5 1721 // and rapidly: an attempt to fade from fully on to fully off causes rapid
mjr 92:f264fbaa1be5 1722 // fluttering and flashing rather than a smooth brightness fade. That's why
mjr 92:f264fbaa1be5 1723 // I had to come up with something different - the mbed solution just trades
mjr 92:f264fbaa1be5 1724 // one annoying bug for another that's just as bad.
mjr 92:f264fbaa1be5 1725 //
mjr 92:f264fbaa1be5 1726 // The hardware bug, by the way, is a case of good intentions gone bad.
mjr 92:f264fbaa1be5 1727 // The whole point of the staging register is to make things easier for
mjr 92:f264fbaa1be5 1728 // us software writers. In most PWM hardware, software has to coordinate
mjr 92:f264fbaa1be5 1729 // with the PWM duty cycle when updating registers to avoid a glitch that
mjr 92:f264fbaa1be5 1730 // you'd get by scribbling to the duty cycle register mid-cycle. The
mjr 92:f264fbaa1be5 1731 // staging register solves this by letting the software write an update at
mjr 92:f264fbaa1be5 1732 // any time, knowing that the hardware will apply the update at exactly the
mjr 92:f264fbaa1be5 1733 // end of the cycle, ensuring glitch-free updates. It's a great design,
mjr 92:f264fbaa1be5 1734 // except that it doesn't quite work. The problem is that they implemented
mjr 92:f264fbaa1be5 1735 // this clever staging register as a one-element FIFO that refuses any more
mjr 92:f264fbaa1be5 1736 // writes when full. That is, writing a value to the FIFO fills it; once
mjr 92:f264fbaa1be5 1737 // full, it ignores writes until it gets emptied out. How's it emptied out?
mjr 92:f264fbaa1be5 1738 // By the hardware moving the staged value to the real register. Sadly, they
mjr 92:f264fbaa1be5 1739 // didn't provide any way for the software to clear the register, and no way
mjr 92:f264fbaa1be5 1740 // to even tell that it's full. So we don't have glitches on write, but we're
mjr 92:f264fbaa1be5 1741 // back to the original problem that the software has to be aware of the PWM
mjr 92:f264fbaa1be5 1742 // cycle timing, because the only way for the software to know that a write
mjr 92:f264fbaa1be5 1743 // actually worked is to know that it's been at least one PWM cycle since the
mjr 92:f264fbaa1be5 1744 // last write. That largely defeats the whole purpose of the staging register,
mjr 92:f264fbaa1be5 1745 // since the whole point was to free software writers of these timing
mjr 92:f264fbaa1be5 1746 // considerations. It's still an improvement over no staging register at
mjr 92:f264fbaa1be5 1747 // all, since we at least don't have to worry about glitches, but it leaves
mjr 92:f264fbaa1be5 1748 // us with this somewhat similar hassle.
mjr 74:822a92bc11d2 1749 //
mjr 77:0b96f6867312 1750 // So here we have our list of PWM outputs that need to be polled for updates.
mjr 77:0b96f6867312 1751 // The KL25Z hardware only has 10 PWM channels, so we only need a fixed set
mjr 77:0b96f6867312 1752 // of polled items.
mjr 74:822a92bc11d2 1753 static int numPolledPwm;
mjr 74:822a92bc11d2 1754 static class LwPwmOut *polledPwm[10];
mjr 74:822a92bc11d2 1755
mjr 74:822a92bc11d2 1756 // LwOut class for a PWM-capable GPIO port.
mjr 6:cc35eb643e8f 1757 class LwPwmOut: public LwOut
mjr 6:cc35eb643e8f 1758 {
mjr 6:cc35eb643e8f 1759 public:
mjr 43:7a6364d82a41 1760 LwPwmOut(PinName pin, uint8_t initVal) : p(pin)
mjr 43:7a6364d82a41 1761 {
mjr 77:0b96f6867312 1762 // add myself to the list of polled outputs for periodic updates
mjr 77:0b96f6867312 1763 if (numPolledPwm < countof(polledPwm))
mjr 74:822a92bc11d2 1764 polledPwm[numPolledPwm++] = this;
mjr 93:177832c29041 1765
mjr 94:0476b3e2b996 1766 // IMPORTANT: Do not set the PWM period (frequency) here explicitly.
mjr 94:0476b3e2b996 1767 // We instead want to accept the current setting for the TPM unit
mjr 94:0476b3e2b996 1768 // we're assigned to. The KL25Z hardware can only set the period at
mjr 94:0476b3e2b996 1769 // the TPM unit level, not per channel, so if we changed the frequency
mjr 94:0476b3e2b996 1770 // here, we'd change it for everything attached to our TPM unit. LW
mjr 94:0476b3e2b996 1771 // outputs don't care about frequency other than that it's fast enough
mjr 94:0476b3e2b996 1772 // that attached LEDs won't flicker. Some other PWM users (IR remote,
mjr 94:0476b3e2b996 1773 // TLC5940) DO care about exact frequencies, because they use the PWM
mjr 94:0476b3e2b996 1774 // as a signal generator rather than merely for brightness control.
mjr 94:0476b3e2b996 1775 // If we changed the frequency here, we could clobber one of those
mjr 94:0476b3e2b996 1776 // carefully chosen frequencies and break the other subsystem. So
mjr 94:0476b3e2b996 1777 // we need to be the "free variable" here and accept whatever setting
mjr 94:0476b3e2b996 1778 // is currently on our assigned unit. To minimize flicker, the main()
mjr 94:0476b3e2b996 1779 // entrypoint sets a default PWM rate of 1kHz on all channels. All
mjr 94:0476b3e2b996 1780 // of the other subsystems that might set specific frequencies will
mjr 94:0476b3e2b996 1781 // set much high frequencies, so that should only be good for us.
mjr 94:0476b3e2b996 1782
mjr 94:0476b3e2b996 1783 // set the initial brightness value
mjr 77:0b96f6867312 1784 set(initVal);
mjr 43:7a6364d82a41 1785 }
mjr 74:822a92bc11d2 1786
mjr 40:cc0d9814522b 1787 virtual void set(uint8_t val)
mjr 74:822a92bc11d2 1788 {
mjr 77:0b96f6867312 1789 // save the new value
mjr 74:822a92bc11d2 1790 this->val = val;
mjr 77:0b96f6867312 1791
mjr 77:0b96f6867312 1792 // commit it to the hardware
mjr 77:0b96f6867312 1793 commit();
mjr 13:72dda449c3c0 1794 }
mjr 74:822a92bc11d2 1795
mjr 74:822a92bc11d2 1796 // handle periodic update polling
mjr 74:822a92bc11d2 1797 void poll()
mjr 74:822a92bc11d2 1798 {
mjr 77:0b96f6867312 1799 commit();
mjr 74:822a92bc11d2 1800 }
mjr 74:822a92bc11d2 1801
mjr 74:822a92bc11d2 1802 protected:
mjr 77:0b96f6867312 1803 virtual void commit()
mjr 74:822a92bc11d2 1804 {
mjr 74:822a92bc11d2 1805 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1806 p.glitchFreeWrite(dof_to_pwm[val]);
mjr 74:822a92bc11d2 1807 }
mjr 74:822a92bc11d2 1808
mjr 77:0b96f6867312 1809 NewPwmOut p;
mjr 77:0b96f6867312 1810 uint8_t val;
mjr 6:cc35eb643e8f 1811 };
mjr 26:cb71c4af2912 1812
mjr 74:822a92bc11d2 1813 // Gamma corrected PWM GPIO output. This works exactly like the regular
mjr 74:822a92bc11d2 1814 // PWM output, but translates DOF values through the gamma-corrected
mjr 74:822a92bc11d2 1815 // table instead of the regular linear table.
mjr 64:ef7ca92dff36 1816 class LwPwmGammaOut: public LwPwmOut
mjr 64:ef7ca92dff36 1817 {
mjr 64:ef7ca92dff36 1818 public:
mjr 64:ef7ca92dff36 1819 LwPwmGammaOut(PinName pin, uint8_t initVal)
mjr 64:ef7ca92dff36 1820 : LwPwmOut(pin, initVal)
mjr 64:ef7ca92dff36 1821 {
mjr 64:ef7ca92dff36 1822 }
mjr 74:822a92bc11d2 1823
mjr 74:822a92bc11d2 1824 protected:
mjr 77:0b96f6867312 1825 virtual void commit()
mjr 64:ef7ca92dff36 1826 {
mjr 74:822a92bc11d2 1827 // write the current value to the PWM controller if it's changed
mjr 77:0b96f6867312 1828 p.glitchFreeWrite(dof_to_gamma_pwm[val]);
mjr 64:ef7ca92dff36 1829 }
mjr 64:ef7ca92dff36 1830 };
mjr 64:ef7ca92dff36 1831
mjr 74:822a92bc11d2 1832 // poll the PWM outputs
mjr 74:822a92bc11d2 1833 Timer polledPwmTimer;
mjr 76:7f5912b6340e 1834 uint64_t polledPwmTotalTime, polledPwmRunCount;
mjr 74:822a92bc11d2 1835 void pollPwmUpdates()
mjr 74:822a92bc11d2 1836 {
mjr 94:0476b3e2b996 1837 // If it's been long enough since the last update, do another update.
mjr 94:0476b3e2b996 1838 // Note that the time limit is fairly arbitrary: it has to be at least
mjr 94:0476b3e2b996 1839 // 1.5X the PWM period, so that we can be sure that at least one PWM
mjr 94:0476b3e2b996 1840 // period has elapsed since the last update, but there's no hard upper
mjr 94:0476b3e2b996 1841 // bound. Instead, it only has to be short enough that fades don't
mjr 94:0476b3e2b996 1842 // become noticeably chunky. The competing interest is that we don't
mjr 94:0476b3e2b996 1843 // want to do this more often than necessary to provide incremental
mjr 94:0476b3e2b996 1844 // benefit, because the polling adds overhead to the main loop and
mjr 94:0476b3e2b996 1845 // takes time away from other tasks we could be performing. The
mjr 94:0476b3e2b996 1846 // shortest time with practical benefit is probably around 50-60Hz,
mjr 94:0476b3e2b996 1847 // since that gives us "video rate" granularity in fades. Anything
mjr 94:0476b3e2b996 1848 // faster wouldn't probably make fades look any smoother to a human
mjr 94:0476b3e2b996 1849 // viewer.
mjr 94:0476b3e2b996 1850 if (polledPwmTimer.read_us() >= 15000)
mjr 74:822a92bc11d2 1851 {
mjr 74:822a92bc11d2 1852 // time the run for statistics collection
mjr 74:822a92bc11d2 1853 IF_DIAG(
mjr 74:822a92bc11d2 1854 Timer t;
mjr 74:822a92bc11d2 1855 t.start();
mjr 74:822a92bc11d2 1856 )
mjr 74:822a92bc11d2 1857
mjr 74:822a92bc11d2 1858 // poll each output
mjr 74:822a92bc11d2 1859 for (int i = numPolledPwm ; i > 0 ; )
mjr 74:822a92bc11d2 1860 polledPwm[--i]->poll();
mjr 74:822a92bc11d2 1861
mjr 74:822a92bc11d2 1862 // reset the timer for the next cycle
mjr 74:822a92bc11d2 1863 polledPwmTimer.reset();
mjr 74:822a92bc11d2 1864
mjr 74:822a92bc11d2 1865 // collect statistics
mjr 74:822a92bc11d2 1866 IF_DIAG(
mjr 76:7f5912b6340e 1867 polledPwmTotalTime += t.read_us();
mjr 74:822a92bc11d2 1868 polledPwmRunCount += 1;
mjr 74:822a92bc11d2 1869 )
mjr 74:822a92bc11d2 1870 }
mjr 74:822a92bc11d2 1871 }
mjr 64:ef7ca92dff36 1872
mjr 26:cb71c4af2912 1873 // LwOut class for a Digital-Only (Non-PWM) GPIO port
mjr 6:cc35eb643e8f 1874 class LwDigOut: public LwOut
mjr 6:cc35eb643e8f 1875 {
mjr 6:cc35eb643e8f 1876 public:
mjr 43:7a6364d82a41 1877 LwDigOut(PinName pin, uint8_t initVal) : p(pin, initVal ? 1 : 0) { prv = initVal; }
mjr 40:cc0d9814522b 1878 virtual void set(uint8_t val)
mjr 13:72dda449c3c0 1879 {
mjr 13:72dda449c3c0 1880 if (val != prv)
mjr 40:cc0d9814522b 1881 p.write((prv = val) == 0 ? 0 : 1);
mjr 13:72dda449c3c0 1882 }
mjr 6:cc35eb643e8f 1883 DigitalOut p;
mjr 40:cc0d9814522b 1884 uint8_t prv;
mjr 6:cc35eb643e8f 1885 };
mjr 26:cb71c4af2912 1886
mjr 29:582472d0bc57 1887 // Array of output physical pin assignments. This array is indexed
mjr 29:582472d0bc57 1888 // by LedWiz logical port number - lwPin[n] is the maping for LedWiz
mjr 35:e959ffba78fd 1889 // port n (0-based).
mjr 35:e959ffba78fd 1890 //
mjr 35:e959ffba78fd 1891 // Each pin is handled by an interface object for the physical output
mjr 35:e959ffba78fd 1892 // type for the port, as set in the configuration. The interface
mjr 35:e959ffba78fd 1893 // objects handle the specifics of addressing the different hardware
mjr 35:e959ffba78fd 1894 // types (GPIO PWM ports, GPIO digital ports, TLC5940 ports, and
mjr 35:e959ffba78fd 1895 // 74HC595 ports).
mjr 33:d832bcab089e 1896 static int numOutputs;
mjr 33:d832bcab089e 1897 static LwOut **lwPin;
mjr 33:d832bcab089e 1898
mjr 38:091e511ce8a0 1899 // create a single output pin
mjr 53:9b2611964afc 1900 LwOut *createLwPin(int portno, LedWizPortCfg &pc, Config &cfg)
mjr 38:091e511ce8a0 1901 {
mjr 38:091e511ce8a0 1902 // get this item's values
mjr 38:091e511ce8a0 1903 int typ = pc.typ;
mjr 38:091e511ce8a0 1904 int pin = pc.pin;
mjr 38:091e511ce8a0 1905 int flags = pc.flags;
mjr 40:cc0d9814522b 1906 int noisy = flags & PortFlagNoisemaker;
mjr 38:091e511ce8a0 1907 int activeLow = flags & PortFlagActiveLow;
mjr 40:cc0d9814522b 1908 int gamma = flags & PortFlagGamma;
mjr 89:c43cd923401c 1909 int flipperLogic = flags & PortFlagFlipperLogic;
mjr 99:8139b0c274f4 1910 int chimeLogic = flags & PortFlagChimeLogic;
mjr 89:c43cd923401c 1911
mjr 89:c43cd923401c 1912 // cancel gamma on flipper logic ports
mjr 89:c43cd923401c 1913 if (flipperLogic)
mjr 89:c43cd923401c 1914 gamma = false;
mjr 38:091e511ce8a0 1915
mjr 38:091e511ce8a0 1916 // create the pin interface object according to the port type
mjr 38:091e511ce8a0 1917 LwOut *lwp;
mjr 38:091e511ce8a0 1918 switch (typ)
mjr 38:091e511ce8a0 1919 {
mjr 38:091e511ce8a0 1920 case PortTypeGPIOPWM:
mjr 48:058ace2aed1d 1921 // PWM GPIO port - assign if we have a valid pin
mjr 48:058ace2aed1d 1922 if (pin != 0)
mjr 64:ef7ca92dff36 1923 {
mjr 64:ef7ca92dff36 1924 // If gamma correction is to be used, and we're not inverting the output,
mjr 64:ef7ca92dff36 1925 // use the combined Pwmout + Gamma output class; otherwise use the plain
mjr 64:ef7ca92dff36 1926 // PwmOut class. We can't use the combined class for inverted outputs
mjr 64:ef7ca92dff36 1927 // because we have to apply gamma correction before the inversion.
mjr 64:ef7ca92dff36 1928 if (gamma && !activeLow)
mjr 64:ef7ca92dff36 1929 {
mjr 64:ef7ca92dff36 1930 // use the gamma-corrected PwmOut type
mjr 64:ef7ca92dff36 1931 lwp = new LwPwmGammaOut(wirePinName(pin), 0);
mjr 64:ef7ca92dff36 1932
mjr 64:ef7ca92dff36 1933 // don't apply further gamma correction to this output
mjr 64:ef7ca92dff36 1934 gamma = false;
mjr 64:ef7ca92dff36 1935 }
mjr 64:ef7ca92dff36 1936 else
mjr 64:ef7ca92dff36 1937 {
mjr 64:ef7ca92dff36 1938 // no gamma correction - use the standard PwmOut class
mjr 64:ef7ca92dff36 1939 lwp = new LwPwmOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 64:ef7ca92dff36 1940 }
mjr 64:ef7ca92dff36 1941 }
mjr 48:058ace2aed1d 1942 else
mjr 48:058ace2aed1d 1943 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1944 break;
mjr 38:091e511ce8a0 1945
mjr 38:091e511ce8a0 1946 case PortTypeGPIODig:
mjr 38:091e511ce8a0 1947 // Digital GPIO port
mjr 48:058ace2aed1d 1948 if (pin != 0)
mjr 48:058ace2aed1d 1949 lwp = new LwDigOut(wirePinName(pin), activeLow ? 255 : 0);
mjr 48:058ace2aed1d 1950 else
mjr 48:058ace2aed1d 1951 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1952 break;
mjr 38:091e511ce8a0 1953
mjr 38:091e511ce8a0 1954 case PortTypeTLC5940:
mjr 38:091e511ce8a0 1955 // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
mjr 38:091e511ce8a0 1956 // output port number on the chips we have, create a virtual port)
mjr 38:091e511ce8a0 1957 if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
mjr 40:cc0d9814522b 1958 {
mjr 40:cc0d9814522b 1959 // If gamma correction is to be used, and we're not inverting the output,
mjr 40:cc0d9814522b 1960 // use the combined TLC4950 + Gamma output class. Otherwise use the plain
mjr 40:cc0d9814522b 1961 // TLC5940 output. We skip the combined class if the output is inverted
mjr 40:cc0d9814522b 1962 // because we need to apply gamma BEFORE the inversion to get the right
mjr 40:cc0d9814522b 1963 // results, but the combined class would apply it after because of the
mjr 40:cc0d9814522b 1964 // layering scheme - the combined class is a physical device output class,
mjr 40:cc0d9814522b 1965 // and a physical device output class is necessarily at the bottom of
mjr 40:cc0d9814522b 1966 // the stack. We don't have a combined inverted+gamma+TLC class, because
mjr 40:cc0d9814522b 1967 // inversion isn't recommended for TLC5940 chips in the first place, so
mjr 40:cc0d9814522b 1968 // it's not worth the extra memory footprint to have a dedicated table
mjr 40:cc0d9814522b 1969 // for this unlikely case.
mjr 40:cc0d9814522b 1970 if (gamma && !activeLow)
mjr 40:cc0d9814522b 1971 {
mjr 40:cc0d9814522b 1972 // use the gamma-corrected 5940 output mapper
mjr 40:cc0d9814522b 1973 lwp = new Lw5940GammaOut(pin);
mjr 40:cc0d9814522b 1974
mjr 40:cc0d9814522b 1975 // DON'T apply further gamma correction to this output
mjr 40:cc0d9814522b 1976 gamma = false;
mjr 40:cc0d9814522b 1977 }
mjr 40:cc0d9814522b 1978 else
mjr 40:cc0d9814522b 1979 {
mjr 40:cc0d9814522b 1980 // no gamma - use the plain (linear) 5940 output class
mjr 40:cc0d9814522b 1981 lwp = new Lw5940Out(pin);
mjr 40:cc0d9814522b 1982 }
mjr 40:cc0d9814522b 1983 }
mjr 38:091e511ce8a0 1984 else
mjr 40:cc0d9814522b 1985 {
mjr 40:cc0d9814522b 1986 // no TLC5940 chips, or invalid port number - use a virtual out
mjr 38:091e511ce8a0 1987 lwp = new LwVirtualOut();
mjr 40:cc0d9814522b 1988 }
mjr 38:091e511ce8a0 1989 break;
mjr 38:091e511ce8a0 1990
mjr 38:091e511ce8a0 1991 case PortType74HC595:
mjr 87:8d35c74403af 1992 // 74HC595 port (if we don't have an HC595 controller object, or it's not
mjr 87:8d35c74403af 1993 // a valid output number, create a virtual port)
mjr 38:091e511ce8a0 1994 if (hc595 != 0 && pin < cfg.hc595.nchips*8)
mjr 38:091e511ce8a0 1995 lwp = new Lw595Out(pin);
mjr 38:091e511ce8a0 1996 else
mjr 38:091e511ce8a0 1997 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 1998 break;
mjr 87:8d35c74403af 1999
mjr 87:8d35c74403af 2000 case PortTypeTLC59116:
mjr 87:8d35c74403af 2001 // TLC59116 port. The pin number in the config encodes the chip address
mjr 87:8d35c74403af 2002 // in the high 4 bits and the output number on the chip in the low 4 bits.
mjr 87:8d35c74403af 2003 // There's no gamma-corrected version of this output handler, so we don't
mjr 87:8d35c74403af 2004 // need to worry about that here; just use the layered gamma as needed.
mjr 87:8d35c74403af 2005 if (tlc59116 != 0)
mjr 87:8d35c74403af 2006 lwp = new Lw59116Out((pin >> 4) & 0x0F, pin & 0x0F);
mjr 87:8d35c74403af 2007 break;
mjr 38:091e511ce8a0 2008
mjr 38:091e511ce8a0 2009 case PortTypeVirtual:
mjr 43:7a6364d82a41 2010 case PortTypeDisabled:
mjr 38:091e511ce8a0 2011 default:
mjr 38:091e511ce8a0 2012 // virtual or unknown
mjr 38:091e511ce8a0 2013 lwp = new LwVirtualOut();
mjr 38:091e511ce8a0 2014 break;
mjr 38:091e511ce8a0 2015 }
mjr 38:091e511ce8a0 2016
mjr 40:cc0d9814522b 2017 // If it's Active Low, layer on an inverter. Note that an inverter
mjr 40:cc0d9814522b 2018 // needs to be the bottom-most layer, since all of the other filters
mjr 40:cc0d9814522b 2019 // assume that they're working with normal (non-inverted) values.
mjr 38:091e511ce8a0 2020 if (activeLow)
mjr 38:091e511ce8a0 2021 lwp = new LwInvertedOut(lwp);
mjr 40:cc0d9814522b 2022
mjr 89:c43cd923401c 2023 // Layer on Flipper Logic if desired
mjr 89:c43cd923401c 2024 if (flipperLogic)
mjr 89:c43cd923401c 2025 lwp = new LwFlipperLogicOut(lwp, pc.flipperLogic);
mjr 89:c43cd923401c 2026
mjr 99:8139b0c274f4 2027 // Layer on Chime Logic if desired. Note that Chime Logic and
mjr 99:8139b0c274f4 2028 // Flipper Logic are mutually exclusive, and Flipper Logic takes
mjr 99:8139b0c274f4 2029 // precedence, so ignore the Chime Logic bit if both are set.
mjr 99:8139b0c274f4 2030 if (chimeLogic && !flipperLogic)
mjr 99:8139b0c274f4 2031 lwp = new LwChimeLogicOut(lwp, pc.flipperLogic);
mjr 98:4df3c0f7e707 2032
mjr 89:c43cd923401c 2033 // If it's a noisemaker, layer on a night mode switch
mjr 40:cc0d9814522b 2034 if (noisy)
mjr 40:cc0d9814522b 2035 lwp = new LwNoisyOut(lwp);
mjr 40:cc0d9814522b 2036
mjr 40:cc0d9814522b 2037 // If it's gamma-corrected, layer on a gamma corrector
mjr 40:cc0d9814522b 2038 if (gamma)
mjr 40:cc0d9814522b 2039 lwp = new LwGammaOut(lwp);
mjr 53:9b2611964afc 2040
mjr 53:9b2611964afc 2041 // If this is the ZB Launch Ball port, layer a monitor object. Note
mjr 64:ef7ca92dff36 2042 // that the nominal port numbering in the config starts at 1, but we're
mjr 53:9b2611964afc 2043 // using an array index, so test against portno+1.
mjr 53:9b2611964afc 2044 if (portno + 1 == cfg.plunger.zbLaunchBall.port)
mjr 53:9b2611964afc 2045 lwp = new LwZbLaunchOut(lwp);
mjr 53:9b2611964afc 2046
mjr 53:9b2611964afc 2047 // If this is the Night Mode indicator port, layer a night mode object.
mjr 53:9b2611964afc 2048 if (portno + 1 == cfg.nightMode.port)
mjr 53:9b2611964afc 2049 lwp = new LwNightModeIndicatorOut(lwp);
mjr 38:091e511ce8a0 2050
mjr 38:091e511ce8a0 2051 // turn it off initially
mjr 38:091e511ce8a0 2052 lwp->set(0);
mjr 38:091e511ce8a0 2053
mjr 38:091e511ce8a0 2054 // return the pin
mjr 38:091e511ce8a0 2055 return lwp;
mjr 38:091e511ce8a0 2056 }
mjr 38:091e511ce8a0 2057
mjr 6:cc35eb643e8f 2058 // initialize the output pin array
mjr 35:e959ffba78fd 2059 void initLwOut(Config &cfg)
mjr 6:cc35eb643e8f 2060 {
mjr 99:8139b0c274f4 2061 // Initialize the Flipper Logic and Chime Logic outputs
mjr 89:c43cd923401c 2062 LwFlipperLogicOut::classInit(cfg);
mjr 99:8139b0c274f4 2063 LwChimeLogicOut::classInit(cfg);
mjr 89:c43cd923401c 2064
mjr 35:e959ffba78fd 2065 // Count the outputs. The first disabled output determines the
mjr 35:e959ffba78fd 2066 // total number of ports.
mjr 35:e959ffba78fd 2067 numOutputs = MAX_OUT_PORTS;
mjr 33:d832bcab089e 2068 int i;
mjr 35:e959ffba78fd 2069 for (i = 0 ; i < MAX_OUT_PORTS ; ++i)
mjr 6:cc35eb643e8f 2070 {
mjr 35:e959ffba78fd 2071 if (cfg.outPort[i].typ == PortTypeDisabled)
mjr 34:6b981a2afab7 2072 {
mjr 35:e959ffba78fd 2073 numOutputs = i;
mjr 34:6b981a2afab7 2074 break;
mjr 34:6b981a2afab7 2075 }
mjr 33:d832bcab089e 2076 }
mjr 33:d832bcab089e 2077
mjr 73:4e8ce0b18915 2078 // allocate the pin array
mjr 73:4e8ce0b18915 2079 lwPin = new LwOut*[numOutputs];
mjr 35:e959ffba78fd 2080
mjr 73:4e8ce0b18915 2081 // Allocate the current brightness array
mjr 73:4e8ce0b18915 2082 outLevel = new uint8_t[numOutputs];
mjr 33:d832bcab089e 2083
mjr 73:4e8ce0b18915 2084 // allocate the LedWiz output state arrays
mjr 73:4e8ce0b18915 2085 wizOn = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 2086 wizVal = new uint8_t[numOutputs];
mjr 73:4e8ce0b18915 2087
mjr 73:4e8ce0b18915 2088 // initialize all LedWiz outputs to off and brightness 48
mjr 73:4e8ce0b18915 2089 memset(wizOn, 0, numOutputs);
mjr 73:4e8ce0b18915 2090 memset(wizVal, 48, numOutputs);
mjr 73:4e8ce0b18915 2091
mjr 73:4e8ce0b18915 2092 // set all LedWiz virtual unit flash speeds to 2
mjr 73:4e8ce0b18915 2093 for (i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2094 wizSpeed[i] = 2;
mjr 33:d832bcab089e 2095
mjr 35:e959ffba78fd 2096 // create the pin interface object for each port
mjr 35:e959ffba78fd 2097 for (i = 0 ; i < numOutputs ; ++i)
mjr 53:9b2611964afc 2098 lwPin[i] = createLwPin(i, cfg.outPort[i], cfg);
mjr 6:cc35eb643e8f 2099 }
mjr 6:cc35eb643e8f 2100
mjr 76:7f5912b6340e 2101 // Translate an LedWiz brightness level (0..49) to a DOF brightness
mjr 76:7f5912b6340e 2102 // level (0..255). Note that brightness level 49 isn't actually valid,
mjr 76:7f5912b6340e 2103 // according to the LedWiz API documentation, but many clients use it
mjr 76:7f5912b6340e 2104 // anyway, and the real LedWiz accepts it and seems to treat it as
mjr 76:7f5912b6340e 2105 // equivalent to 48.
mjr 40:cc0d9814522b 2106 static const uint8_t lw_to_dof[] = {
mjr 40:cc0d9814522b 2107 0, 5, 11, 16, 21, 27, 32, 37,
mjr 40:cc0d9814522b 2108 43, 48, 53, 58, 64, 69, 74, 80,
mjr 40:cc0d9814522b 2109 85, 90, 96, 101, 106, 112, 117, 122,
mjr 40:cc0d9814522b 2110 128, 133, 138, 143, 149, 154, 159, 165,
mjr 40:cc0d9814522b 2111 170, 175, 181, 186, 191, 197, 202, 207,
mjr 40:cc0d9814522b 2112 213, 218, 223, 228, 234, 239, 244, 250,
mjr 40:cc0d9814522b 2113 255, 255
mjr 40:cc0d9814522b 2114 };
mjr 40:cc0d9814522b 2115
mjr 76:7f5912b6340e 2116 // Translate a DOF brightness level (0..255) to an LedWiz brightness
mjr 76:7f5912b6340e 2117 // level (1..48)
mjr 76:7f5912b6340e 2118 static const uint8_t dof_to_lw[] = {
mjr 76:7f5912b6340e 2119 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
mjr 76:7f5912b6340e 2120 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
mjr 76:7f5912b6340e 2121 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9,
mjr 76:7f5912b6340e 2122 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12,
mjr 76:7f5912b6340e 2123 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15,
mjr 76:7f5912b6340e 2124 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18,
mjr 76:7f5912b6340e 2125 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21,
mjr 76:7f5912b6340e 2126 21, 21, 21, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24,
mjr 76:7f5912b6340e 2127 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27,
mjr 76:7f5912b6340e 2128 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30,
mjr 76:7f5912b6340e 2129 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33, 33, 33,
mjr 76:7f5912b6340e 2130 33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36,
mjr 76:7f5912b6340e 2131 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
mjr 76:7f5912b6340e 2132 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
mjr 76:7f5912b6340e 2133 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45,
mjr 76:7f5912b6340e 2134 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48
mjr 76:7f5912b6340e 2135 };
mjr 76:7f5912b6340e 2136
mjr 74:822a92bc11d2 2137 // LedWiz flash cycle tables. For efficiency, we use a lookup table
mjr 74:822a92bc11d2 2138 // rather than calculating these on the fly. The flash cycles are
mjr 74:822a92bc11d2 2139 // generated by the following formulas, where 'c' is the current
mjr 74:822a92bc11d2 2140 // cycle counter, from 0 to 255:
mjr 74:822a92bc11d2 2141 //
mjr 74:822a92bc11d2 2142 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 2143 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 2144 // mode 131 = on/ramp down = (c < 128 ? 255 : (255-c)*2)
mjr 74:822a92bc11d2 2145 // mode 132 = ramp up/on = (c < 128 ? c*2 : 255)
mjr 74:822a92bc11d2 2146 //
mjr 74:822a92bc11d2 2147 // To look up the current output value for a given mode and a given
mjr 74:822a92bc11d2 2148 // cycle counter 'c', index the table with ((mode-129)*256)+c.
mjr 74:822a92bc11d2 2149 static const uint8_t wizFlashLookup[] = {
mjr 74:822a92bc11d2 2150 // mode 129 = sawtooth = (c < 128 ? c*2 + 1 : (255-c)*2)
mjr 74:822a92bc11d2 2151 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f,
mjr 74:822a92bc11d2 2152 0x21, 0x23, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f,
mjr 74:822a92bc11d2 2153 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f,
mjr 74:822a92bc11d2 2154 0x61, 0x63, 0x65, 0x67, 0x69, 0x6b, 0x6d, 0x6f, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d, 0x7f,
mjr 74:822a92bc11d2 2155 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d, 0x8f, 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9d, 0x9f,
mjr 74:822a92bc11d2 2156 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad, 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, 0xbf,
mjr 74:822a92bc11d2 2157 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
mjr 74:822a92bc11d2 2158 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb, 0xed, 0xef, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0xfd, 0xff,
mjr 74:822a92bc11d2 2159 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 2160 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 2161 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 2162 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 2163 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 2164 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 2165 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 2166 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 2167
mjr 74:822a92bc11d2 2168 // mode 130 = flash on/off = (c < 128 ? 255 : 0)
mjr 74:822a92bc11d2 2169 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2170 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2171 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2172 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2173 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2174 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2176 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2178 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2179 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2180 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2181 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2182 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2183 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2184 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mjr 74:822a92bc11d2 2185
mjr 74:822a92bc11d2 2186 // mode 131 = on/ramp down = c < 128 ? 255 : (255 - c)*2
mjr 74:822a92bc11d2 2187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2189 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2191 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2192 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2193 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2194 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2195 0xfe, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
mjr 74:822a92bc11d2 2196 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
mjr 74:822a92bc11d2 2197 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
mjr 74:822a92bc11d2 2198 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
mjr 74:822a92bc11d2 2199 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
mjr 74:822a92bc11d2 2200 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
mjr 74:822a92bc11d2 2201 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
mjr 74:822a92bc11d2 2202 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00,
mjr 74:822a92bc11d2 2203
mjr 74:822a92bc11d2 2204 // mode 132 = ramp up/on = c < 128 ? c*2 : 255
mjr 74:822a92bc11d2 2205 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
mjr 74:822a92bc11d2 2206 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
mjr 74:822a92bc11d2 2207 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
mjr 74:822a92bc11d2 2208 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
mjr 74:822a92bc11d2 2209 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
mjr 74:822a92bc11d2 2210 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
mjr 74:822a92bc11d2 2211 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
mjr 74:822a92bc11d2 2212 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
mjr 74:822a92bc11d2 2213 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2214 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2215 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2216 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2217 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2218 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2219 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mjr 74:822a92bc11d2 2220 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
mjr 74:822a92bc11d2 2221 };
mjr 74:822a92bc11d2 2222
mjr 74:822a92bc11d2 2223 // LedWiz flash cycle timer. This runs continuously. On each update,
mjr 74:822a92bc11d2 2224 // we use this to figure out where we are on the cycle for each bank.
mjr 74:822a92bc11d2 2225 Timer wizCycleTimer;
mjr 74:822a92bc11d2 2226
mjr 76:7f5912b6340e 2227 // timing statistics for wizPulse()
mjr 76:7f5912b6340e 2228 uint64_t wizPulseTotalTime, wizPulseRunCount;
mjr 76:7f5912b6340e 2229
mjr 76:7f5912b6340e 2230 // LedWiz flash timer pulse. The main loop calls this on each cycle
mjr 76:7f5912b6340e 2231 // to update outputs using LedWiz flash modes. We do one bank of 32
mjr 76:7f5912b6340e 2232 // outputs on each cycle.
mjr 29:582472d0bc57 2233 static void wizPulse()
mjr 29:582472d0bc57 2234 {
mjr 76:7f5912b6340e 2235 // current bank
mjr 76:7f5912b6340e 2236 static int wizPulseBank = 0;
mjr 76:7f5912b6340e 2237
mjr 76:7f5912b6340e 2238 // start a timer for statistics collection
mjr 76:7f5912b6340e 2239 IF_DIAG(
mjr 76:7f5912b6340e 2240 Timer t;
mjr 76:7f5912b6340e 2241 t.start();
mjr 76:7f5912b6340e 2242 )
mjr 76:7f5912b6340e 2243
mjr 76:7f5912b6340e 2244 // Update the current bank's cycle counter: figure the current
mjr 76:7f5912b6340e 2245 // phase of the LedWiz pulse cycle for this bank.
mjr 76:7f5912b6340e 2246 //
mjr 76:7f5912b6340e 2247 // The LedWiz speed setting gives the flash period in 0.25s units
mjr 76:7f5912b6340e 2248 // (speed 1 is a flash period of .25s, speed 7 is a period of 1.75s).
mjr 76:7f5912b6340e 2249 //
mjr 76:7f5912b6340e 2250 // What we're after here is the "phase", which is to say the point
mjr 76:7f5912b6340e 2251 // in the current cycle. If we assume that the cycle has been running
mjr 76:7f5912b6340e 2252 // continuously since some arbitrary time zero in the past, we can
mjr 76:7f5912b6340e 2253 // figure where we are in the current cycle by dividing the time since
mjr 76:7f5912b6340e 2254 // that zero by the cycle period and taking the remainder. E.g., if
mjr 76:7f5912b6340e 2255 // the cycle time is 5 seconds, and the time since t-zero is 17 seconds,
mjr 76:7f5912b6340e 2256 // we divide 17 by 5 to get a remainder of 2. That says we're 2 seconds
mjr 76:7f5912b6340e 2257 // into the current 5-second cycle, or 2/5 of the way through the
mjr 76:7f5912b6340e 2258 // current cycle.
mjr 76:7f5912b6340e 2259 //
mjr 76:7f5912b6340e 2260 // We do this calculation on every iteration of the main loop, so we
mjr 76:7f5912b6340e 2261 // want it to be very fast. To streamline it, we'll use some tricky
mjr 76:7f5912b6340e 2262 // integer arithmetic. The result will be the same as the straightforward
mjr 76:7f5912b6340e 2263 // remainder and fraction calculation we just explained, but we'll get
mjr 76:7f5912b6340e 2264 // there by less-than-obvious means.
mjr 76:7f5912b6340e 2265 //
mjr 76:7f5912b6340e 2266 // Rather than finding the phase as a continuous quantity or floating
mjr 76:7f5912b6340e 2267 // point number, we'll quantize it. We'll divide each cycle into 256
mjr 76:7f5912b6340e 2268 // time units, or quanta. Each quantum is 1/256 of the cycle length,
mjr 76:7f5912b6340e 2269 // so for a 1-second cycle (LedWiz speed 4), each quantum is 1/256 of
mjr 76:7f5912b6340e 2270 // a second, or about 3.9ms. If we express the time since t-zero in
mjr 76:7f5912b6340e 2271 // these units, the time period of one cycle is exactly 256 units, so
mjr 76:7f5912b6340e 2272 // we can calculate our point in the cycle by taking the remainder of
mjr 76:7f5912b6340e 2273 // the time (in our funny units) divided by 256. The special thing
mjr 76:7f5912b6340e 2274 // about making the cycle time equal to 256 units is that "x % 256"
mjr 76:7f5912b6340e 2275 // is exactly the same as "x & 255", which is a much faster operation
mjr 76:7f5912b6340e 2276 // than division on ARM M0+: this CPU has no hardware DIVIDE operation,
mjr 76:7f5912b6340e 2277 // so an integer division takes about 5us. The bit mask operation, in
mjr 76:7f5912b6340e 2278 // contrast, takes only about 60ns - about 100x faster. 5us doesn't
mjr 76:7f5912b6340e 2279 // sound like much, but we do this on every main loop, so every little
mjr 76:7f5912b6340e 2280 // bit counts.
mjr 76:7f5912b6340e 2281 //
mjr 76:7f5912b6340e 2282 // The snag is that our system timer gives us the elapsed time in
mjr 76:7f5912b6340e 2283 // microseconds. We still need to convert this to our special quanta
mjr 76:7f5912b6340e 2284 // of 256 units per cycle. The straightforward way to do that is by
mjr 76:7f5912b6340e 2285 // dividing by (microseconds per quantum). E.g., for LedWiz speed 4,
mjr 76:7f5912b6340e 2286 // we decided that our quantum was 1/256 of a second, or 3906us, so
mjr 76:7f5912b6340e 2287 // dividing the current system time in microseconds by 3906 will give
mjr 76:7f5912b6340e 2288 // us the time in our quantum units. But now we've just substituted
mjr 76:7f5912b6340e 2289 // one division for another!
mjr 76:7f5912b6340e 2290 //
mjr 76:7f5912b6340e 2291 // This is where our really tricky integer math comes in. Dividing
mjr 76:7f5912b6340e 2292 // by X is the same as multiplying by 1/X. In integer math, 1/3906
mjr 76:7f5912b6340e 2293 // is zero, so that won't work. But we can get around that by doing
mjr 76:7f5912b6340e 2294 // the integer math as "fixed point" arithmetic instead. It's still
mjr 76:7f5912b6340e 2295 // actually carried out as integer operations, but we'll scale our
mjr 76:7f5912b6340e 2296 // integers by a scaling factor, then take out the scaling factor
mjr 76:7f5912b6340e 2297 // later to get the final result. The scaling factor we'll use is
mjr 76:7f5912b6340e 2298 // 2^24. So we're going to calculate (time * 2^24/3906), then divide
mjr 76:7f5912b6340e 2299 // the result by 2^24 to get the final answer. I know it seems like
mjr 76:7f5912b6340e 2300 // we're substituting one division for another yet again, but this
mjr 76:7f5912b6340e 2301 // time's the charm, because dividing by 2^24 is a bit shift operation,
mjr 76:7f5912b6340e 2302 // which is another single-cycle operation on M0+. You might also
mjr 76:7f5912b6340e 2303 // wonder how all these tricks don't cause overflows or underflows
mjr 76:7f5912b6340e 2304 // or what not. Well, the multiply by 2^24/3906 will cause an
mjr 76:7f5912b6340e 2305 // overflow, but we don't care, because the overflow will all be in
mjr 76:7f5912b6340e 2306 // the high-order bits that we're going to discard in the final
mjr 76:7f5912b6340e 2307 // remainder calculation anyway.
mjr 76:7f5912b6340e 2308 //
mjr 76:7f5912b6340e 2309 // Each entry in the array below represents 2^24/N for the corresponding
mjr 76:7f5912b6340e 2310 // LedWiz speed, where N is the number of time quanta per cycle at that
mjr 76:7f5912b6340e 2311 // speed. The time quanta are chosen such that 256 quanta add up to
mjr 76:7f5912b6340e 2312 // approximately (LedWiz speed setting * 0.25s).
mjr 76:7f5912b6340e 2313 //
mjr 76:7f5912b6340e 2314 // Note that the calculation has an implicit bit mask (result & 0xFF)
mjr 76:7f5912b6340e 2315 // to get the final result mod 256. But we don't have to actually
mjr 76:7f5912b6340e 2316 // do that work because we're using 32-bit ints and a 2^24 fixed
mjr 76:7f5912b6340e 2317 // point base (X in the narrative above). The final shift right by
mjr 76:7f5912b6340e 2318 // 24 bits to divide out the base will leave us with only 8 bits in
mjr 76:7f5912b6340e 2319 // the result, since we started with 32.
mjr 76:7f5912b6340e 2320 static const uint32_t inv_us_per_quantum[] = { // indexed by LedWiz speed
mjr 76:7f5912b6340e 2321 0, 17172, 8590, 5726, 4295, 3436, 2863, 2454
mjr 76:7f5912b6340e 2322 };
mjr 76:7f5912b6340e 2323 int counter = ((wizCycleTimer.read_us() * inv_us_per_quantum[wizSpeed[wizPulseBank]]) >> 24);
mjr 76:7f5912b6340e 2324
mjr 76:7f5912b6340e 2325 // get the range of 32 output sin this bank
mjr 76:7f5912b6340e 2326 int fromPort = wizPulseBank*32;
mjr 76:7f5912b6340e 2327 int toPort = fromPort+32;
mjr 76:7f5912b6340e 2328 if (toPort > numOutputs)
mjr 76:7f5912b6340e 2329 toPort = numOutputs;
mjr 76:7f5912b6340e 2330
mjr 76:7f5912b6340e 2331 // update all outputs set to flashing values
mjr 76:7f5912b6340e 2332 for (int i = fromPort ; i < toPort ; ++i)
mjr 73:4e8ce0b18915 2333 {
mjr 76:7f5912b6340e 2334 // Update the port only if the LedWiz SBA switch for the port is on
mjr 76:7f5912b6340e 2335 // (wizOn[i]) AND the port is a PBA flash mode in the range 129..132.
mjr 76:7f5912b6340e 2336 // These modes and only these modes have the high bit (0x80) set, so
mjr 76:7f5912b6340e 2337 // we can test for them simply by testing the high bit.
mjr 76:7f5912b6340e 2338 if (wizOn[i])
mjr 29:582472d0bc57 2339 {
mjr 76:7f5912b6340e 2340 uint8_t val = wizVal[i];
mjr 76:7f5912b6340e 2341 if ((val & 0x80) != 0)
mjr 29:582472d0bc57 2342 {
mjr 76:7f5912b6340e 2343 // ook up the value for the mode at the cycle time
mjr 76:7f5912b6340e 2344 lwPin[i]->set(outLevel[i] = wizFlashLookup[((val-129) << 8) + counter]);
mjr 29:582472d0bc57 2345 }
mjr 29:582472d0bc57 2346 }
mjr 76:7f5912b6340e 2347 }
mjr 76:7f5912b6340e 2348
mjr 34:6b981a2afab7 2349 // flush changes to 74HC595 chips, if attached
mjr 35:e959ffba78fd 2350 if (hc595 != 0)
mjr 35:e959ffba78fd 2351 hc595->update();
mjr 76:7f5912b6340e 2352
mjr 76:7f5912b6340e 2353 // switch to the next bank
mjr 76:7f5912b6340e 2354 if (++wizPulseBank >= MAX_LW_BANKS)
mjr 76:7f5912b6340e 2355 wizPulseBank = 0;
mjr 76:7f5912b6340e 2356
mjr 76:7f5912b6340e 2357 // collect timing statistics
mjr 76:7f5912b6340e 2358 IF_DIAG(
mjr 76:7f5912b6340e 2359 wizPulseTotalTime += t.read_us();
mjr 76:7f5912b6340e 2360 wizPulseRunCount += 1;
mjr 76:7f5912b6340e 2361 )
mjr 1:d913e0afb2ac 2362 }
mjr 38:091e511ce8a0 2363
mjr 76:7f5912b6340e 2364 // Update a port to reflect its new LedWiz SBA+PBA setting.
mjr 76:7f5912b6340e 2365 static void updateLwPort(int port)
mjr 38:091e511ce8a0 2366 {
mjr 76:7f5912b6340e 2367 // check if the SBA switch is on or off
mjr 76:7f5912b6340e 2368 if (wizOn[port])
mjr 76:7f5912b6340e 2369 {
mjr 76:7f5912b6340e 2370 // It's on. If the port is a valid static brightness level,
mjr 76:7f5912b6340e 2371 // set the output port to match. Otherwise leave it as is:
mjr 76:7f5912b6340e 2372 // if it's a flashing mode, the flash mode pulse will update
mjr 76:7f5912b6340e 2373 // it on the next cycle.
mjr 76:7f5912b6340e 2374 int val = wizVal[port];
mjr 76:7f5912b6340e 2375 if (val <= 49)
mjr 76:7f5912b6340e 2376 lwPin[port]->set(outLevel[port] = lw_to_dof[val]);
mjr 76:7f5912b6340e 2377 }
mjr 76:7f5912b6340e 2378 else
mjr 76:7f5912b6340e 2379 {
mjr 76:7f5912b6340e 2380 // the port is off - set absolute brightness zero
mjr 76:7f5912b6340e 2381 lwPin[port]->set(outLevel[port] = 0);
mjr 76:7f5912b6340e 2382 }
mjr 73:4e8ce0b18915 2383 }
mjr 73:4e8ce0b18915 2384
mjr 73:4e8ce0b18915 2385 // Turn off all outputs and restore everything to the default LedWiz
mjr 92:f264fbaa1be5 2386 // state. This sets all outputs to LedWiz profile value 48 (full
mjr 92:f264fbaa1be5 2387 // brightness) and switch state Off, and sets the LedWiz flash rate
mjr 92:f264fbaa1be5 2388 // to 2. This effectively restores the power-on conditions.
mjr 73:4e8ce0b18915 2389 //
mjr 73:4e8ce0b18915 2390 void allOutputsOff()
mjr 73:4e8ce0b18915 2391 {
mjr 92:f264fbaa1be5 2392 // reset all outputs to OFF/48
mjr 73:4e8ce0b18915 2393 for (int i = 0 ; i < numOutputs ; ++i)
mjr 73:4e8ce0b18915 2394 {
mjr 73:4e8ce0b18915 2395 outLevel[i] = 0;
mjr 73:4e8ce0b18915 2396 wizOn[i] = 0;
mjr 73:4e8ce0b18915 2397 wizVal[i] = 48;
mjr 73:4e8ce0b18915 2398 lwPin[i]->set(0);
mjr 73:4e8ce0b18915 2399 }
mjr 73:4e8ce0b18915 2400
mjr 73:4e8ce0b18915 2401 // restore default LedWiz flash rate
mjr 73:4e8ce0b18915 2402 for (int i = 0 ; i < countof(wizSpeed) ; ++i)
mjr 73:4e8ce0b18915 2403 wizSpeed[i] = 2;
mjr 38:091e511ce8a0 2404
mjr 73:4e8ce0b18915 2405 // flush changes to hc595, if applicable
mjr 38:091e511ce8a0 2406 if (hc595 != 0)
mjr 38:091e511ce8a0 2407 hc595->update();
mjr 38:091e511ce8a0 2408 }
mjr 38:091e511ce8a0 2409
mjr 74:822a92bc11d2 2410 // Cary out an SBA or SBX message. portGroup is 0 for ports 1-32,
mjr 74:822a92bc11d2 2411 // 1 for ports 33-64, etc. Original protocol SBA messages always
mjr 74:822a92bc11d2 2412 // address port group 0; our private SBX extension messages can
mjr 74:822a92bc11d2 2413 // address any port group.
mjr 74:822a92bc11d2 2414 void sba_sbx(int portGroup, const uint8_t *data)
mjr 74:822a92bc11d2 2415 {
mjr 76:7f5912b6340e 2416 // update all on/off states in the group
mjr 74:822a92bc11d2 2417 for (int i = 0, bit = 1, imsg = 1, port = portGroup*32 ;
mjr 74:822a92bc11d2 2418 i < 32 && port < numOutputs ;
mjr 74:822a92bc11d2 2419 ++i, bit <<= 1, ++port)
mjr 74:822a92bc11d2 2420 {
mjr 74:822a92bc11d2 2421 // figure the on/off state bit for this output
mjr 74:822a92bc11d2 2422 if (bit == 0x100) {
mjr 74:822a92bc11d2 2423 bit = 1;
mjr 74:822a92bc11d2 2424 ++imsg;
mjr 74:822a92bc11d2 2425 }
mjr 74:822a92bc11d2 2426
mjr 74:822a92bc11d2 2427 // set the on/off state
mjr 76:7f5912b6340e 2428 bool on = wizOn[port] = ((data[imsg] & bit) != 0);
mjr 76:7f5912b6340e 2429
mjr 76:7f5912b6340e 2430 // set the output port brightness to match the new setting
mjr 76:7f5912b6340e 2431 updateLwPort(port);
mjr 74:822a92bc11d2 2432 }
mjr 74:822a92bc11d2 2433
mjr 74:822a92bc11d2 2434 // set the flash speed for the port group
mjr 74:822a92bc11d2 2435 if (portGroup < countof(wizSpeed))
mjr 74:822a92bc11d2 2436 wizSpeed[portGroup] = (data[5] < 1 ? 1 : data[5] > 7 ? 7 : data[5]);
mjr 74:822a92bc11d2 2437
mjr 76:7f5912b6340e 2438 // update 74HC959 outputs
mjr 76:7f5912b6340e 2439 if (hc595 != 0)
mjr 76:7f5912b6340e 2440 hc595->update();
mjr 74:822a92bc11d2 2441 }
mjr 74:822a92bc11d2 2442
mjr 74:822a92bc11d2 2443 // Carry out a PBA or PBX message.
mjr 74:822a92bc11d2 2444 void pba_pbx(int basePort, const uint8_t *data)
mjr 74:822a92bc11d2 2445 {
mjr 74:822a92bc11d2 2446 // update each wizVal entry from the brightness data
mjr 76:7f5912b6340e 2447 for (int i = 0, port = basePort ; i < 8 && port < numOutputs ; ++i, ++port)
mjr 74:822a92bc11d2 2448 {
mjr 74:822a92bc11d2 2449 // get the value
mjr 74:822a92bc11d2 2450 uint8_t v = data[i];
mjr 74:822a92bc11d2 2451
mjr 74:822a92bc11d2 2452 // Validate it. The legal values are 0..49 for brightness
mjr 74:822a92bc11d2 2453 // levels, and 128..132 for flash modes. Set anything invalid
mjr 74:822a92bc11d2 2454 // to full brightness (48) instead. Note that 49 isn't actually
mjr 74:822a92bc11d2 2455 // a valid documented value, but in practice some clients send
mjr 74:822a92bc11d2 2456 // this to mean 100% brightness, and the real LedWiz treats it
mjr 74:822a92bc11d2 2457 // as such.
mjr 74:822a92bc11d2 2458 if ((v > 49 && v < 129) || v > 132)
mjr 74:822a92bc11d2 2459 v = 48;
mjr 74:822a92bc11d2 2460
mjr 74:822a92bc11d2 2461 // store it
mjr 76:7f5912b6340e 2462 wizVal[port] = v;
mjr 76:7f5912b6340e 2463
mjr 76:7f5912b6340e 2464 // update the port
mjr 76:7f5912b6340e 2465 updateLwPort(port);
mjr 74:822a92bc11d2 2466 }
mjr 74:822a92bc11d2 2467
mjr 76:7f5912b6340e 2468 // update 74HC595 outputs
mjr 76:7f5912b6340e 2469 if (hc595 != 0)
mjr 76:7f5912b6340e 2470 hc595->update();
mjr 74:822a92bc11d2 2471 }
mjr 74:822a92bc11d2 2472
mjr 77:0b96f6867312 2473 // ---------------------------------------------------------------------------
mjr 77:0b96f6867312 2474 //
mjr 77:0b96f6867312 2475 // IR Remote Control transmitter & receiver
mjr 77:0b96f6867312 2476 //
mjr 77:0b96f6867312 2477
mjr 77:0b96f6867312 2478 // receiver
mjr 77:0b96f6867312 2479 IRReceiver *ir_rx;
mjr 77:0b96f6867312 2480
mjr 77:0b96f6867312 2481 // transmitter
mjr 77:0b96f6867312 2482 IRTransmitter *ir_tx;
mjr 77:0b96f6867312 2483
mjr 77:0b96f6867312 2484 // Mapping from IR commands slots in the configuration to "virtual button"
mjr 77:0b96f6867312 2485 // numbers on the IRTransmitter's "virtual remote". To minimize RAM usage,
mjr 77:0b96f6867312 2486 // we only create virtual buttons on the transmitter object for code slots
mjr 77:0b96f6867312 2487 // that are configured for transmission, which includes slots used for TV
mjr 77:0b96f6867312 2488 // ON commands and slots that can be triggered by button presses. This
mjr 77:0b96f6867312 2489 // means that virtual button numbers won't necessarily match the config
mjr 77:0b96f6867312 2490 // slot numbers. This table provides the mapping:
mjr 77:0b96f6867312 2491 // IRConfigSlotToVirtualButton[n] = ir_tx virtual button number for
mjr 77:0b96f6867312 2492 // configuration slot n
mjr 77:0b96f6867312 2493 uint8_t IRConfigSlotToVirtualButton[MAX_IR_CODES];
mjr 78:1e00b3fa11af 2494
mjr 78:1e00b3fa11af 2495 // IR transmitter virtual button number for ad hoc IR command. We allocate
mjr 78:1e00b3fa11af 2496 // one virtual button for sending ad hoc IR codes, such as through the USB
mjr 78:1e00b3fa11af 2497 // protocol.
mjr 78:1e00b3fa11af 2498 uint8_t IRAdHocBtn;
mjr 78:1e00b3fa11af 2499
mjr 78:1e00b3fa11af 2500 // Staging area for ad hoc IR commands. It takes multiple messages
mjr 78:1e00b3fa11af 2501 // to fill out an IR command, so we store the partial command here
mjr 78:1e00b3fa11af 2502 // while waiting for the rest.
mjr 78:1e00b3fa11af 2503 static struct
mjr 78:1e00b3fa11af 2504 {
mjr 78:1e00b3fa11af 2505 uint8_t protocol; // protocol ID
mjr 78:1e00b3fa11af 2506 uint64_t code; // code
mjr 78:1e00b3fa11af 2507 uint8_t dittos : 1; // using dittos?
mjr 78:1e00b3fa11af 2508 uint8_t ready : 1; // do we have a code ready to transmit?
mjr 78:1e00b3fa11af 2509 } IRAdHocCmd;
mjr 88:98bce687e6c0 2510
mjr 77:0b96f6867312 2511
mjr 77:0b96f6867312 2512 // IR mode timer. In normal mode, this is the time since the last
mjr 77:0b96f6867312 2513 // command received; we use this to handle commands with timed effects,
mjr 77:0b96f6867312 2514 // such as sending a key to the PC. In learning mode, this is the time
mjr 77:0b96f6867312 2515 // since we activated learning mode, which we use to automatically end
mjr 77:0b96f6867312 2516 // learning mode if a decodable command isn't received within a reasonable
mjr 77:0b96f6867312 2517 // amount of time.
mjr 77:0b96f6867312 2518 Timer IRTimer;
mjr 77:0b96f6867312 2519
mjr 77:0b96f6867312 2520 // IR Learning Mode. The PC enters learning mode via special function 65 12.
mjr 77:0b96f6867312 2521 // The states are:
mjr 77:0b96f6867312 2522 //
mjr 77:0b96f6867312 2523 // 0 -> normal operation (not in learning mode)
mjr 77:0b96f6867312 2524 // 1 -> learning mode; reading raw codes, no command read yet
mjr 77:0b96f6867312 2525 // 2 -> learning mode; command received, awaiting auto-repeat
mjr 77:0b96f6867312 2526 // 3 -> learning mode; done, command and repeat mode decoded
mjr 77:0b96f6867312 2527 //
mjr 77:0b96f6867312 2528 // When we enter learning mode, we reset IRTimer to keep track of how long
mjr 77:0b96f6867312 2529 // we've been in the mode. This allows the mode to time out if no code is
mjr 77:0b96f6867312 2530 // received within a reasonable time.
mjr 77:0b96f6867312 2531 uint8_t IRLearningMode = 0;
mjr 77:0b96f6867312 2532
mjr 77:0b96f6867312 2533 // Learning mode command received. This stores the first decoded command
mjr 77:0b96f6867312 2534 // when in learning mode. For some protocols, we can't just report the
mjr 77:0b96f6867312 2535 // first command we receive, because we need to wait for an auto-repeat to
mjr 77:0b96f6867312 2536 // determine what format the remote uses for repeats. This stores the first
mjr 77:0b96f6867312 2537 // command while we await a repeat. This is necessary for protocols that
mjr 77:0b96f6867312 2538 // have "dittos", since some remotes for such protocols use the dittos and
mjr 77:0b96f6867312 2539 // some don't; the only way to find out is to read a repeat code and see if
mjr 77:0b96f6867312 2540 // it's a ditto or just a repeat of the full code.
mjr 77:0b96f6867312 2541 IRCommand learnedIRCode;
mjr 77:0b96f6867312 2542
mjr 78:1e00b3fa11af 2543 // IR command received, as a config slot index, 1..MAX_IR_CODES.
mjr 77:0b96f6867312 2544 // When we receive a command that matches one of our programmed commands,
mjr 77:0b96f6867312 2545 // we note the slot here. We also reset the IR timer so that we know how
mjr 77:0b96f6867312 2546 // long it's been since the command came in. This lets us handle commands
mjr 77:0b96f6867312 2547 // with timed effects, such as PC key input. Note that this is a 1-based
mjr 77:0b96f6867312 2548 // index; 0 represents no command.
mjr 77:0b96f6867312 2549 uint8_t IRCommandIn = 0;
mjr 77:0b96f6867312 2550
mjr 77:0b96f6867312 2551 // "Toggle bit" of last command. Some IR protocols have a toggle bit
mjr 77:0b96f6867312 2552 // that distinguishes an auto-repeating key from a key being pressed
mjr 77:0b96f6867312 2553 // several times in a row. This records the toggle bit of the last
mjr 77:0b96f6867312 2554 // command we received.
mjr 77:0b96f6867312 2555 uint8_t lastIRToggle = 0;
mjr 77:0b96f6867312 2556
mjr