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 05 00:16:52 2016 +0000
Revision:
52:8298b2a73eb2
Parent:
51:57eb311faafa
Child:
53:9b2611964afc
New calibration procedure - attempt #1, with separate calibration release sensingi

Who changed what in which revision?

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