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

Dependencies:   mbed FastIO FastPWM USBDevice

Fork of Pinscape_Controller by Mike R

/media/uploads/mjr/pinscape_no_background_small_L7Miwr6.jpg

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

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

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

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

Downloads

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

Documentation

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

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

System Requirements

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

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

Main Features

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

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

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

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

Expansion Boards

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

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

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

Expansion Board project page

Update notes

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

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

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

New Features

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

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

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

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

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

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

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

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

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

More Downloads

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

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

Copyright and License

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

Warning to VirtuaPin Kit Owners

This software isn't designed as a replacement for the VirtuaPin plunger kit's firmware. If you bought the VirtuaPin kit, I recommend that you don't install this software. The KL25Z can only run one firmware program at a time, so if you install the Pinscape firmware on your KL25Z, it will replace and erase your existing VirtuaPin proprietary firmware. If you do this, the only way to restore your VirtuaPin firmware is to physically ship the KL25Z back to VirtuaPin and ask them to re-flash it. They don't allow you to do this at home, and they don't even allow you to back up your firmware, since they want to protect their proprietary software from copying. For all of these reasons, if you want to run the Pinscape software, I strongly recommend that you buy a "blank" retail KL25Z to use with Pinscape. They only cost about $15 and are available at several online retailers, including Amazon, Mouser, and eBay. The blank retail boards don't come with any proprietary firmware pre-installed, so installing Pinscape won't delete anything that you paid extra for.

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

Revision:
40:cc0d9814522b
Parent:
39:b3815a1c3802
Child:
43:7a6364d82a41
diff -r b3815a1c3802 -r cc0d9814522b main.cpp
--- a/main.cpp	Mon Jan 11 21:08:36 2016 +0000
+++ b/main.cpp	Wed Feb 03 22:57:25 2016 +0000
@@ -85,30 +85,32 @@
 //  - LedWiz emulation.  The KL25Z can appear to the PC as an LedWiz device, and will
 //    accept and process LedWiz commands from the host.  The software can turn digital
 //    output ports on and off, and can set varying PWM intensitiy levels on a subset
-//    of ports.  (The KL25Z can only provide 6 PWM ports.  Intensity level settings on
-//    other ports is ignored, so non-PWM ports can only be used for simple on/off
-//    devices such as contactors and solenoids.)  The KL25Z can only supply 4mA on its
-//    output ports, so external hardware is required to take advantage of the LedWiz
-//    emulation.  Many different hardware designs are possible, but there's a simple
-//    reference design in the documentation that uses a Darlington array IC to
-//    increase the output from each port to 500mA (the same level as the LedWiz),
-//    plus an extended design that adds an optocoupler and MOSFET to provide very
-//    high power handling, up to about 45A or 150W, with voltages up to 100V.
-//    That will handle just about any DC device directly (wtihout relays or other
-//    amplifiers), and switches fast enough to support PWM devices.
+//    of ports.  The KL25Z hardware is limited to 10 PWM ports.  Ports beyond the
+//    10 PWM ports are simple digital on/off ports.  Intensity level settings on 
+//    digital ports is ignored, so such ports can only be used for devices such as 
+//    contactors and solenoids that don't need differeing intensities.
 //
-//    The device can report any desired LedWiz unit number to the host, which makes
-//    it possible to use the LedWiz emulation on a machine that also has one or more
-//    actual LedWiz devices intalled.  The LedWiz design allows for up to 16 units
-//    to be installed in one machine - each one is invidually addressable by its
-//    distinct unit number.
+//    Note that the KL25Z can only supply or sink 4mA on its output ports, so external 
+//    amplifier hardware is required to use the LedWiz emulation.  Many different 
+//    hardware designs are possible, but there's a simple reference design in the 
+//    documentation that uses a Darlington array IC to increase the output from 
+//    each port to 500mA (the same level as the LedWiz), plus an extended design 
+//    that adds an optocoupler and MOSFET to provide very high power handling, up 
+//    to about 45A or 150W, with voltages up to 100V.  That will handle just about 
+//    any DC device directly (wtihout relays or other amplifiers), and switches fast 
+//    enough to support PWM devices.  For example, you can use it to drive a motor at
+//    different speeds via the PWM intensity.
+//
+//    The Controller device can report any desired LedWiz unit number to the host, 
+//    which makes it possible for one or more Pinscape Controller units to coexist
+//    with one more more real LedWiz units in the same machine.  The LedWiz design 
+//    allows for up to 16 units to be installed in one machine.  Each device needs
+//    to have a distinct LedWiz Unit Number, which allows software on the PC to
+//    address each device independently.
 //
 //    The LedWiz emulation features are of course optional.  There's no need to 
 //    build any of the external port hardware (or attach anything to the output 
-//    ports at all) if the LedWiz features aren't needed.  Most people won't have
-//    any use for the LedWiz features.  I built them mostly as a learning exercise,
-//    but with a slight practical need for a handful of extra ports (I'm using the
-//    cutting-edge 10-contactor setup, so my real LedWiz is full!).
+//    ports at all) if the LedWiz features aren't needed.
 //
 //  - Enhanced LedWiz emulation with TLC5940 PWM controller chips.  You can attach
 //    external PWM controller chips for controlling device outputs, instead of using
@@ -219,9 +221,28 @@
 
 // --------------------------------------------------------------------------
 // 
+// Extended verison of Timer class.  This adds the ability to interrogate
+// the running state.
+//
+class Timer2: public Timer
+{
+public:
+    Timer2() : running(false) { }
+
+    void start() { running = true; Timer::start(); }
+    void stop()  { running = false; Timer::stop(); }
+    
+    bool isRunning() const { return running; }
+    
+private:
+    bool running;
+};
+
+// --------------------------------------------------------------------------
+// 
 // USB product version number
 //
-const uint16_t USB_VERSION_NO = 0x0008;
+const uint16_t USB_VERSION_NO = 0x0009;
 
 // --------------------------------------------------------------------------
 //
@@ -232,8 +253,8 @@
 
 // ---------------------------------------------------------------------------
 //
-// Wire protocol value translations.  These translate byte values from
-// the USB protocol to local native format.
+// Wire protocol value translations.  These translate byte values to and
+// from the USB protocol to local native format.
 //
 
 // unsigned 16-bit integer 
@@ -241,33 +262,61 @@
 {
     return b[0] | ((uint16_t)b[1] << 8);
 }
+inline void ui16Wire(uint8_t *b, uint16_t val)
+{
+    b[0] = (uint8_t)(val & 0xff);
+    b[1] = (uint8_t)((val >> 8) & 0xff);
+}
 
 inline int16_t wireI16(const uint8_t *b)
 {
     return (int16_t)wireUI16(b);
 }
+inline void i16Wire(uint8_t *b, int16_t val)
+{
+    ui16Wire(b, (uint16_t)val);
+}
 
 inline uint32_t wireUI32(const uint8_t *b)
 {
     return b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
 }
+inline void ui32Wire(uint8_t *b, uint32_t val)
+{
+    b[0] = (uint8_t)(val & 0xff);
+    b[1] = (uint8_t)((val >> 8) & 0xff);    
+    b[2] = (uint8_t)((val >> 16) & 0xff);    
+    b[3] = (uint8_t)((val >> 24) & 0xff);    
+}
 
 inline int32_t wireI32(const uint8_t *b)
 {
     return (int32_t)wireUI32(b);
 }
 
+static const PinName pinNameMap[] =  {
+    NC,    PTA1,  PTA2,  PTA4,  PTA5,  PTA12, PTA13, PTA16, PTA17, PTB0,    // 0-9
+    PTB1,  PTB2,  PTB3,  PTB8,  PTB9,  PTB10, PTB11, PTB18, PTB19, PTC0,    // 10-19
+    PTC1,  PTC2,  PTC3,  PTC4,  PTC5,  PTC6,  PTC7,  PTC8,  PTC9,  PTC10,   // 20-29
+    PTC11, PTC12, PTC13, PTC16, PTC17, PTD0,  PTD1,  PTD2,  PTD3,  PTD4,    // 30-39
+    PTD5,  PTD6,  PTD7,  PTE0,  PTE1,  PTE2,  PTE3,  PTE4,  PTE5,  PTE20,   // 40-49
+    PTE21, PTE22, PTE23, PTE29, PTE30, PTE31                                // 50-55
+};
 inline PinName wirePinName(int c)
 {
-    static const PinName p[] =  {
-        NC,    PTA1,  PTA2,  PTA4,  PTA5,  PTA12, PTA13, PTA16, PTA17, PTB0,    // 0-9
-        PTB1,  PTB2,  PTB3,  PTB8,  PTB9,  PTB10, PTB11, PTB18, PTB19, PTC0,    // 10-19
-        PTC1,  PTC2,  PTC3,  PTC4,  PTC5,  PTC6,  PTC7,  PTC8,  PTC9,  PTC10,   // 20-29
-        PTC11, PTC12, PTC13, PTC16, PTC17, PTD0,  PTD1,  PTD2,  PTD3,  PTD4,    // 30-39
-        PTD5,  PTD6,  PTD7,  PTE0,  PTE1,  PTE2,  PTE3,  PTE4,  PTE5,  PTE20,   // 40-49
-        PTE21, PTE22, PTE23, PTE29, PTE30, PTE31                                // 50-55
-    };
-    return (c < countof(p) ? p[c] : NC);
+    return (c < countof(pinNameMap) ? pinNameMap[c] : NC);
+}
+inline void pinNameWire(uint8_t *b, PinName n)
+{
+    b[0] = 0; // presume invalid -> NC
+    for (int i = 0 ; i < countof(pinNameMap) ; ++i)
+    {
+        if (pinNameMap[i] == n)
+        {
+            b[0] = i;
+            return;
+        }
+    }
 }
 
 
@@ -381,9 +430,9 @@
 class LwOut
 {
 public:
-    // Set the output intensity.  'val' is 0.0 for fully off, 1.0 for
-    // fully on, and fractional values for intermediate intensities.
-    virtual void set(float val) = 0;
+    // Set the output intensity.  'val' is 0 for fully off, 255 for
+    // fully on, with values in between signifying lower intensity.
+    virtual void set(uint8_t val) = 0;
 };
 
 // LwOut class for virtual ports.  This type of port is visible to
@@ -395,22 +444,73 @@
 {
 public:
     LwVirtualOut() { }
-    virtual void set(float val) { }
+    virtual void set(uint8_t ) { }
 };
 
 // Active Low out.  For any output marked as active low, we layer this
 // on top of the physical pin interface.  This simply inverts the value of
-// the output value, so that 1.0 means fully off and 0.0 means fully on.
+// the output value, so that 255 means fully off and 0 means fully on.
 class LwInvertedOut: public LwOut
 {
 public:
     LwInvertedOut(LwOut *o) : out(o) { }
-    virtual void set(float val) { out->set(1.0 - val); }
+    virtual void set(uint8_t val) { out->set(255 - val); }
     
 private:
     LwOut *out;
 };
 
+// Gamma correction table for 8-bit input values
+static const uint8_t gamma[] = {
+      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1,   1, 
+      1,   1,   1,   1,   1,   1,   1,   1,   1,   2,   2,   2,   2,   2,   2,   2, 
+      2,   3,   3,   3,   3,   3,   3,   3,   4,   4,   4,   4,   4,   5,   5,   5, 
+      5,   6,   6,   6,   6,   7,   7,   7,   7,   8,   8,   8,   9,   9,   9,  10, 
+     10,  10,  11,  11,  11,  12,  12,  13,  13,  13,  14,  14,  15,  15,  16,  16, 
+     17,  17,  18,  18,  19,  19,  20,  20,  21,  21,  22,  22,  23,  24,  24,  25, 
+     25,  26,  27,  27,  28,  29,  29,  30,  31,  32,  32,  33,  34,  35,  35,  36, 
+     37,  38,  39,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  50, 
+     51,  52,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  66,  67,  68, 
+     69,  70,  72,  73,  74,  75,  77,  78,  79,  81,  82,  83,  85,  86,  87,  89, 
+     90,  92,  93,  95,  96,  98,  99, 101, 102, 104, 105, 107, 109, 110, 112, 114, 
+    115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142, 
+    144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175, 
+    177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213, 
+    215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
+};
+
+// Gamma-corrected out.  This is a filter object that we layer on top
+// of a physical pin interface.  This applies gamma correction to the
+// input value and then passes it along to the underlying pin object.
+class LwGammaOut: public LwOut
+{
+public:
+    LwGammaOut(LwOut *o) : out(o) { }
+    virtual void set(uint8_t val) { out->set(gamma[val]); }
+    
+private:
+    LwOut *out;
+};
+
+// Noisy output.  This is a filter object that we layer on top of
+// a physical pin output.  This filter disables the port when night
+// mode is engaged.
+class LwNoisyOut: public LwOut
+{
+public:
+    LwNoisyOut(LwOut *o) : out(o) { }
+    virtual void set(uint8_t val) { out->set(nightMode ? 0 : val); }
+    
+    static bool nightMode;
+
+private:
+    LwOut *out;
+};
+
+// global night mode flag
+bool LwNoisyOut::nightMode = false;
+
 
 //
 // The TLC5940 interface object.  We'll set this up with the port 
@@ -426,6 +526,55 @@
     }
 }
 
+// Conversion table for 8-bit DOF level to 12-bit TLC5940 level
+static const uint16_t dof_to_tlc[] = {
+       0,   16,   32,   48,   64,   80,   96,  112,  128,  145,  161,  177,  193,  209,  225,  241, 
+     257,  273,  289,  305,  321,  337,  353,  369,  385,  401,  418,  434,  450,  466,  482,  498, 
+     514,  530,  546,  562,  578,  594,  610,  626,  642,  658,  674,  691,  707,  723,  739,  755, 
+     771,  787,  803,  819,  835,  851,  867,  883,  899,  915,  931,  947,  964,  980,  996, 1012, 
+    1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269, 
+    1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526, 
+    1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783, 
+    1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039, 
+    2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296, 
+    2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553, 
+    2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810, 
+    2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067, 
+    3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324, 
+    3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581, 
+    3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838, 
+    3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095
+};
+
+// Conversion table for 8-bit DOF level to 12-bit TLC5940 level, with 
+// gamma correction.  Note that the output layering scheme can handle
+// this without a separate table, by first applying gamma to the DOF
+// level to produce an 8-bit gamma-corrected value, then convert that
+// to the 12-bit TLC5940 value.  But we get better precision by doing
+// the gamma correction in the 12-bit TLC5940 domain.  We can only
+// get the 12-bit domain by combining both steps into one layering
+// object, though, since the intermediate values in the layering system
+// are always 8 bits.
+static const uint16_t dof_to_gamma_tlc[] = {
+      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1,   1,   1, 
+      2,   2,   2,   3,   3,   4,   4,   5,   5,   6,   7,   8,   8,   9,  10,  11, 
+     12,  13,  15,  16,  17,  18,  20,  21,  23,  25,  26,  28,  30,  32,  34,  36, 
+     38,  40,  43,  45,  48,  50,  53,  56,  59,  62,  65,  68,  71,  75,  78,  82, 
+     85,  89,  93,  97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154, 
+    159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258, 
+    266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399, 
+    409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582, 
+    594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811, 
+    827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998, 1016, 1034, 1053, 1072, 1091, 
+    1110, 1130, 1150, 1170, 1190, 1210, 1231, 1252, 1273, 1294, 1316, 1338, 1360, 1382, 1404, 1427, 
+    1450, 1473, 1497, 1520, 1544, 1568, 1593, 1617, 1642, 1667, 1693, 1718, 1744, 1770, 1797, 1823, 
+    1850, 1877, 1905, 1932, 1960, 1988, 2017, 2045, 2074, 2103, 2133, 2162, 2192, 2223, 2253, 2284, 
+    2315, 2346, 2378, 2410, 2442, 2474, 2507, 2540, 2573, 2606, 2640, 2674, 2708, 2743, 2778, 2813, 
+    2849, 2884, 2920, 2957, 2993, 3030, 3067, 3105, 3143, 3181, 3219, 3258, 3297, 3336, 3376, 3416, 
+    3456, 3496, 3537, 3578, 3619, 3661, 3703, 3745, 3788, 3831, 3874, 3918, 3962, 4006, 4050, 4095
+};
+
+
 // LwOut class for TLC5940 outputs.  These are fully PWM capable.
 // The 'idx' value in the constructor is the output index in the
 // daisy-chained TLC5940 array.  0 is output #0 on the first chip,
@@ -434,16 +583,31 @@
 class Lw5940Out: public LwOut
 {
 public:
-    Lw5940Out(int idx) : idx(idx) { prv = -1; }
-    virtual void set(float val)
+    Lw5940Out(int idx) : idx(idx) { prv = 0; }
+    virtual void set(uint8_t val)
     {
         if (val != prv)
-           tlc5940->set(idx, (int)((prv = val) * 4095.0f));
+           tlc5940->set(idx, dof_to_tlc[prv = val]);
     }
     int idx;
-    float prv;
+    uint8_t prv;
 };
 
+// LwOut class for TLC5940 gamma-corrected outputs.
+class Lw5940GammaOut: public LwOut
+{
+public:
+    Lw5940GammaOut(int idx) : idx(idx) { prv = 0; }
+    virtual void set(uint8_t val)
+    {
+        if (val != prv)
+           tlc5940->set(idx, dof_to_gamma_tlc[prv = val]);
+    }
+    int idx;
+    uint8_t prv;
+};
+
+
 
 // 74HC595 interface object.  Set this up with the port assignments in
 // config.h.
@@ -468,51 +632,81 @@
 class Lw595Out: public LwOut
 {
 public:
-    Lw595Out(int idx) : idx(idx) { prv = -1; }
-    virtual void set(float val)
+    Lw595Out(int idx) : idx(idx) { prv = 0; }
+    virtual void set(uint8_t val)
     {
         if (val != prv)
-           hc595->set(idx, (prv = val) == 0.0 ? 0 : 1);
+           hc595->set(idx, (prv = val) == 0 ? 0 : 1);
     }
     int idx;
-    float prv;
+    uint8_t prv;
 };
 
 
-// 
-// Default LedWiz mode - using on-board GPIO ports.  In this mode, we
-// assign a KL25Z GPIO port to each LedWiz output.  We have to use a
-// mix of PWM-capable and Digital-Only ports in this configuration, 
-// since the KL25Z hardware only has 10 PWM channels, which isn't
-// enough to fill out the full complement of 32 LedWiz outputs.
-//
+
+// Conversion table - 8-bit DOF output level to PWM float level
+// (normalized to 0.0..1.0 scale)
+static const float pwm_level[] = {
+    0.000000, 0.003922, 0.007843, 0.011765, 0.015686, 0.019608, 0.023529, 0.027451, 
+    0.031373, 0.035294, 0.039216, 0.043137, 0.047059, 0.050980, 0.054902, 0.058824, 
+    0.062745, 0.066667, 0.070588, 0.074510, 0.078431, 0.082353, 0.086275, 0.090196, 
+    0.094118, 0.098039, 0.101961, 0.105882, 0.109804, 0.113725, 0.117647, 0.121569, 
+    0.125490, 0.129412, 0.133333, 0.137255, 0.141176, 0.145098, 0.149020, 0.152941, 
+    0.156863, 0.160784, 0.164706, 0.168627, 0.172549, 0.176471, 0.180392, 0.184314, 
+    0.188235, 0.192157, 0.196078, 0.200000, 0.203922, 0.207843, 0.211765, 0.215686, 
+    0.219608, 0.223529, 0.227451, 0.231373, 0.235294, 0.239216, 0.243137, 0.247059, 
+    0.250980, 0.254902, 0.258824, 0.262745, 0.266667, 0.270588, 0.274510, 0.278431, 
+    0.282353, 0.286275, 0.290196, 0.294118, 0.298039, 0.301961, 0.305882, 0.309804, 
+    0.313725, 0.317647, 0.321569, 0.325490, 0.329412, 0.333333, 0.337255, 0.341176, 
+    0.345098, 0.349020, 0.352941, 0.356863, 0.360784, 0.364706, 0.368627, 0.372549, 
+    0.376471, 0.380392, 0.384314, 0.388235, 0.392157, 0.396078, 0.400000, 0.403922, 
+    0.407843, 0.411765, 0.415686, 0.419608, 0.423529, 0.427451, 0.431373, 0.435294, 
+    0.439216, 0.443137, 0.447059, 0.450980, 0.454902, 0.458824, 0.462745, 0.466667, 
+    0.470588, 0.474510, 0.478431, 0.482353, 0.486275, 0.490196, 0.494118, 0.498039, 
+    0.501961, 0.505882, 0.509804, 0.513725, 0.517647, 0.521569, 0.525490, 0.529412, 
+    0.533333, 0.537255, 0.541176, 0.545098, 0.549020, 0.552941, 0.556863, 0.560784, 
+    0.564706, 0.568627, 0.572549, 0.576471, 0.580392, 0.584314, 0.588235, 0.592157, 
+    0.596078, 0.600000, 0.603922, 0.607843, 0.611765, 0.615686, 0.619608, 0.623529, 
+    0.627451, 0.631373, 0.635294, 0.639216, 0.643137, 0.647059, 0.650980, 0.654902, 
+    0.658824, 0.662745, 0.666667, 0.670588, 0.674510, 0.678431, 0.682353, 0.686275, 
+    0.690196, 0.694118, 0.698039, 0.701961, 0.705882, 0.709804, 0.713725, 0.717647, 
+    0.721569, 0.725490, 0.729412, 0.733333, 0.737255, 0.741176, 0.745098, 0.749020, 
+    0.752941, 0.756863, 0.760784, 0.764706, 0.768627, 0.772549, 0.776471, 0.780392, 
+    0.784314, 0.788235, 0.792157, 0.796078, 0.800000, 0.803922, 0.807843, 0.811765, 
+    0.815686, 0.819608, 0.823529, 0.827451, 0.831373, 0.835294, 0.839216, 0.843137, 
+    0.847059, 0.850980, 0.854902, 0.858824, 0.862745, 0.866667, 0.870588, 0.874510, 
+    0.878431, 0.882353, 0.886275, 0.890196, 0.894118, 0.898039, 0.901961, 0.905882, 
+    0.909804, 0.913725, 0.917647, 0.921569, 0.925490, 0.929412, 0.933333, 0.937255, 
+    0.941176, 0.945098, 0.949020, 0.952941, 0.956863, 0.960784, 0.964706, 0.968627, 
+    0.972549, 0.976471, 0.980392, 0.984314, 0.988235, 0.992157, 0.996078, 1.000000
+};
 
 // LwOut class for a PWM-capable GPIO port
 class LwPwmOut: public LwOut
 {
 public:
-    LwPwmOut(PinName pin) : p(pin) { prv = -1; }
-    virtual void set(float val) 
+    LwPwmOut(PinName pin) : p(pin) { prv = 0; }
+    virtual void set(uint8_t val) 
     { 
         if (val != prv)
-            p.write(prv = val); 
+            p.write(pwm_level[prv = val]); 
     }
     PwmOut p;
-    float prv;
+    uint8_t prv;
 };
 
 // LwOut class for a Digital-Only (Non-PWM) GPIO port
 class LwDigOut: public LwOut
 {
 public:
-    LwDigOut(PinName pin) : p(pin) { prv = -1; }
-    virtual void set(float val) 
+    LwDigOut(PinName pin) : p(pin) { prv = 0; }
+    virtual void set(uint8_t val) 
     {
          if (val != prv)
-            p.write((prv = val) == 0.0 ? 0 : 1); 
+            p.write((prv = val) == 0 ? 0 : 1); 
     }
     DigitalOut p;
-    float prv;
+    uint8_t prv;
 };
 
 // Array of output physical pin assignments.  This array is indexed
@@ -532,6 +726,7 @@
 //    [0] = Night Mode indicator light
 //
 static LwOut *specialPin[1];
+const int SPECIAL_PIN_NIGHTMODE = 0;
 
 
 // Number of LedWiz emulation outputs.  This is the number of ports
@@ -541,20 +736,11 @@
 // lower of 32 or the actual number of outputs.
 static int numLwOutputs;
 
-// Current absolute brightness level for an output.  This is a float
-// value from 0.0 for fully off to 1.0 for fully on.  This is used
-// for all extended ports (33 and above), and for any LedWiz port
-// with wizVal == 255.
-static float *outLevel;
-
-// Day/night mode override for an output.  For each output, this is
-// set to 1 if the output is enabled and 0 if the output is disabled
-// by a global mode control, such as Night Mode (currently Night Mode
-// is the only such global mode, but the idea could be extended to
-// other similar controls if other needs emerge).  To get the final
-// output level for each output, we simply multiply the outLevel value
-// for the port by this override vlaue.
-static uint8_t *modeLevel;
+// Current absolute brightness level for an output.  This is a DOF
+// brightness level value, from 0 for fully off to 255 for fully on.  
+// This is used for all extended ports (33 and above), and for any 
+// LedWiz port with wizVal == 255.
+static uint8_t *outLevel;
 
 // create a single output pin
 LwOut *createLwPin(LedWizPortCfg &pc, Config &cfg)
@@ -563,7 +749,9 @@
     int typ = pc.typ;
     int pin = pc.pin;
     int flags = pc.flags;
+    int noisy = flags & PortFlagNoisemaker;
     int activeLow = flags & PortFlagActiveLow;
+    int gamma = flags & PortFlagGamma;
 
     // create the pin interface object according to the port type        
     LwOut *lwp;
@@ -583,9 +771,37 @@
         // TLC5940 port (if we don't have a TLC controller object, or it's not a valid
         // output port number on the chips we have, create a virtual port)
         if (tlc5940 != 0 && pin < cfg.tlc5940.nchips*16)
-            lwp = new Lw5940Out(pin);
+        {
+            // If gamma correction is to be used, and we're not inverting the output,
+            // use the combined TLC4950 + Gamma output class.  Otherwise use the plain 
+            // TLC5940 output.  We skip the combined class if the output is inverted
+            // because we need to apply gamma BEFORE the inversion to get the right
+            // results, but the combined class would apply it after because of the
+            // layering scheme - the combined class is a physical device output class,
+            // and a physical device output class is necessarily at the bottom of 
+            // the stack.  We don't have a combined inverted+gamma+TLC class, because
+            // inversion isn't recommended for TLC5940 chips in the first place, so
+            // it's not worth the extra memory footprint to have a dedicated table
+            // for this unlikely case.
+            if (gamma && !activeLow)
+            {
+                // use the gamma-corrected 5940 output mapper
+                lwp = new Lw5940GammaOut(pin);
+                
+                // DON'T apply further gamma correction to this output
+                gamma = false;
+            }
+            else
+            {
+                // no gamma - use the plain (linear) 5940 output class
+                lwp = new Lw5940Out(pin);
+            }
+        }
         else
+        {
+            // no TLC5940 chips, or invalid port number - use a virtual out
             lwp = new LwVirtualOut();
+        }
         break;
     
     case PortType74HC595:
@@ -604,9 +820,20 @@
         break;
     }
     
-    // if it's Active Low, layer on an inverter
+    // If it's Active Low, layer on an inverter.  Note that an inverter
+    // needs to be the bottom-most layer, since all of the other filters
+    // assume that they're working with normal (non-inverted) values.
     if (activeLow)
         lwp = new LwInvertedOut(lwp);
+        
+    // If it's a noisemaker, layer on a night mode switch.  Note that this
+    // needs to be 
+    if (noisy)
+        lwp = new LwNoisyOut(lwp);
+        
+    // If it's gamma-corrected, layer on a gamma corrector
+    if (gamma)
+        lwp = new LwGammaOut(lwp);
 
     // turn it off initially      
     lwp->set(0);
@@ -643,13 +870,7 @@
     // allocate the full set of actual ports if we have more than the
     // LedWiz complement.
     int minOuts = numOutputs < 32 ? 32 : numOutputs;
-    outLevel = new float[minOuts];
-    
-    // Allocate the mode override array
-    modeLevel = new uint8_t[minOuts];
-    
-    // start with all modeLevel values set to ON
-    memset(modeLevel, 1, minOuts);
+    outLevel = new uint8_t[minOuts];
     
     // create the pin interface object for each port
     for (i = 0 ; i < numOutputs ; ++i)
@@ -674,22 +895,24 @@
 // on/off state for each LedWiz output
 static uint8_t wizOn[32];
 
-// Profile (brightness/blink) state for each LedWiz output.  If the
-// output was last updated through an LedWiz protocol message, it
-// will have one of these values:
+// LedWiz "Profile State" (the LedWiz brightness level or blink mode)
+// for each LedWiz output.  If the output was last updated through an 
+// LedWiz protocol message, it will have one of these values:
 //
 //   0-48 = fixed brightness 0% to 100%
+//   49  = fixed brightness 100% (equivalent to 48)
 //   129 = ramp up / ramp down
 //   130 = flash on / off
 //   131 = on / ramp down
 //   132 = ramp up / on
 //
-// Special value 255:  If the output was updated through the 
-// extended protocol, we'll set the wizVal entry to 255, which has 
-// no meaning in the LedWiz protocol.  This tells us that the value 
-// in outLevel[] was set directly from the extended protocol, so it 
-// shouldn't be derived from wizVal[].
+// If the output was last updated through an extended protocol message,
+// it will have the special value 255.  This means that we use the
+// outLevel[] value for the port instead of an LedWiz setting.
 //
+// (Note that value 49 isn't documented in the LedWiz spec, but real
+// LedWiz units treat it as equivalent to 48, and some PC software uses
+// it, so we need to accept it for compatibility.)
 static uint8_t wizVal[32] = {
     48, 48, 48, 48, 48, 48, 48, 48,
     48, 48, 48, 48, 48, 48, 48, 48,
@@ -701,11 +924,24 @@
 // rate for lights in blinking states.
 static uint8_t wizSpeed = 2;
 
-// Current LedWiz flash cycle counter.
+// Current LedWiz flash cycle counter.  This runs from 0 to 255
+// during each cycle.
 static uint8_t wizFlashCounter = 0;
 
-// Get the current brightness level for an LedWiz output.
-static float wizState(int idx)
+// translate an LedWiz brightness level (0-49) to a DOF brightness
+// level (0-255)
+static const uint8_t lw_to_dof[] = {
+       0,    5,   11,   16,   21,   27,   32,   37, 
+      43,   48,   53,   58,   64,   69,   74,   80, 
+      85,   90,   96,  101,  106,  112,  117,  122, 
+     128,  133,  138,  143,  149,  154,  159,  165, 
+     170,  175,  181,  186,  191,  197,  202,  207, 
+     213,  218,  223,  228,  234,  239,  244,  250, 
+     255,  255
+};
+
+// Translate an LedWiz output (ports 1-32) to a DOF brightness level.
+static uint8_t wizState(int idx)
 {
     // if the output was last set with an extended protocol message,
     // use the value set there, ignoring the output's LedWiz state
@@ -718,7 +954,7 @@
 
     // check the state
     uint8_t val = wizVal[idx];
-    if (val <= 48)
+    if (val <= 49)
     {
         // PWM brightness/intensity level.  Rescale from the LedWiz
         // 0..48 integer range to our internal PwmOut 0..1 float range.
@@ -740,40 +976,34 @@
         // makes us work properly with software that's expecting the
         // documented LedWiz behavior and therefore uses level 48 to
         // turn a contactor or relay fully on.
-        return val/48.0f;
-    }
-    else if (val == 49)
-    {
-        // 49 is undefined in the LedWiz documentation, but actually
-        // means 100% on.  The documentation says that levels 1-48 are
-        // the full PWM range, but empirically it appears that the real
-        // range implemented in the firmware is 1-49.  Some software on
-        // the PC side (notably DOF) is aware of this and uses level 49
-        // to mean "100% on".  To ensure compatibility with existing 
-        // PC-side software, we need to recognize level 49.
-        return 1.0f;
+        //
+        // Note that value 49 is undefined in the LedWiz documentation,
+        // but real LedWiz units treat it as 100%, equivalent to 48.
+        // Some software on the PC side uses this, so we need to treat
+        // it the same way for compatibility.
+        return lw_to_dof[val];
     }
     else if (val == 129)
     {
-        //   129 = ramp up / ramp down
+        // 129 = ramp up / ramp down
         return wizFlashCounter < 128 
-            ? wizFlashCounter/128.0f 
-            : (256 - wizFlashCounter)/128.0f;
+            ? wizFlashCounter*2 + 1
+            : (255 - wizFlashCounter)*2;
     }
     else if (val == 130)
     {
-        //   130 = flash on / off
-        return wizFlashCounter < 128 ? 1.0f : 0.0f;
+        // 130 = flash on / off
+        return wizFlashCounter < 128 ? 255 : 0;
     }
     else if (val == 131)
     {
-        //   131 = on / ramp down
-        return wizFlashCounter < 128 ? 1.0f : (255 - wizFlashCounter)/128.0f;
+        // 131 = on / ramp down
+        return wizFlashCounter < 128 ? 255 : (255 - wizFlashCounter)*2;
     }
     else if (val == 132)
     {
-        //   132 = ramp up / on
-        return wizFlashCounter < 128 ? wizFlashCounter/128.0f : 1.0f;
+        // 132 = ramp up / on
+        return wizFlashCounter < 128 ? wizFlashCounter*2 : 255;
     }
     else
     {
@@ -782,7 +1012,7 @@
         // LedWiz unit exhibits in response is accidental and could change
         // in a future version.  We'll treat all undefined values as equivalent 
         // to 48 (fully on).
-        return 1.0f;
+        return 255;
     }
 }
 
@@ -812,7 +1042,7 @@
             uint8_t s = wizVal[i];
             if (s >= 129 && s <= 132)
             {
-                lwPin[i]->set(wizState(i) * modeLevel[i]);
+                lwPin[i]->set(wizState(i));
                 ena = true;
             }
         }
@@ -837,7 +1067,7 @@
     for (int i = 0 ; i < numLwOutputs ; ++i)
     {
         pulse |= (wizVal[i] >= 129 && wizVal[i] <= 132);
-        lwPin[i]->set(wizState(i) * modeLevel[i]);
+        lwPin[i]->set(wizState(i));
     }
     
     // if any outputs are set to flashing mode, and the pulse timer
@@ -856,11 +1086,11 @@
 {
     // uddate each LedWiz output
     for (int i = 0 ; i < numLwOutputs ; ++i)
-        lwPin[i]->set(wizState(i) * modeLevel[i]);
+        lwPin[i]->set(wizState(i));
         
     // update each extended output
     for (int i = 33 ; i < numOutputs ; ++i)
-        lwPin[i]->set(outLevel[i] * modeLevel[i]);
+        lwPin[i]->set(outLevel[i]);
         
     // flush 74HC595 changes, if necessary
     if (hc595 != 0)
@@ -1677,7 +1907,7 @@
     }
     
     // reset all extended outputs (ports >32) to full off (brightness 0)
-    for (int i = 32 ; i < numOutputs ; ++i)
+    for (int i = numLwOutputs ; i < numOutputs ; ++i)
     {
         outLevel[i] = 0;
         lwPin[i]->set(0);
@@ -1723,16 +1953,20 @@
 // previous check.  When we see this condition, we start a countdown
 // timer, and pulse the TV switch relay when the countdown ends.
 //
-// This scheme might seem a little convoluted, but it neatly handles
-// all of the different cases that can occur:
+// This scheme might seem a little convoluted, but it handles a number
+// of tricky but likely scenarios:
 //
 // - Most cabinets systems are set up with "soft" PC power switches, 
-//   so that the PC goes into "Soft Off" mode (ACPI state S5, in Windows
-//   parlance) when the user turns off the cabinet.  In this state, the
-//   motherboard supplies power to USB devices, so the KL25Z continues
-//   running without interruption.  The latch system lets us monitor
-//   the power state even when we're never rebooted, since the latch
-//   will turn off when PSU2 is off regardless of what the KL25Z is doing.
+//   so that the PC goes into "Soft Off" mode when the user turns off
+//   the cabinet by pushing the power button or using the Shut Down
+//   command from within Windows.  In Windows parlance, this "soft off"
+//   condition is called ACPI State S5.  In this state, the main CPU
+//   power is turned off, but the motherboard still provides power to
+//   USB devices.  This means that the KL25Z keeps running.  Without
+//   the external power sensing circuit, the only hint that we're in 
+//   this state is that the USB connection to the host goes into Suspend
+//   mode, but that could mean other things as well.  The latch circuit
+//   lets us tell for sure that we're in this state.
 //
 // - Some cabinet builders might prefer to use "hard" power switches,
 //   cutting all power to the cabinet, including the PC motherboard (and
@@ -1741,14 +1975,15 @@
 //   a power outage occurs, etc.  In these cases, the KL25Z will do a cold
 //   boot when the PC is turned on.  We don't know whether the KL25Z
 //   will power up before or after PSU2, so it's not good enough to 
-//   observe the *current* state of PSU2 when we first check - if PSU2
-//   were to come on first, checking the current state alone would fool
-//   us into thinking that no action is required, because we would never
-//   have known that PSU2 was ever off.  The latch handles this case by
-//   letting us see that PSU2 *was* off before we checked.
+//   observe the current state of PSU2 when we first check.  If PSU2
+//   were to come on first, checking only the current state would fool
+//   us into thinking that no action is required, because we'd only see
+//   that PSU2 is turned on any time we check.  The latch handles this 
+//   case by letting us see that PSU2 was indeed off some time before our
+//   first check.
 //
 // - If the KL25Z is rebooted while the main system is running, or the 
-//   KL25Z is unplugged and plugged back in, we will correctly leave the 
+//   KL25Z is unplugged and plugged back in, we'll correctly leave the 
 //   TVs as they are.  The latch state is independent of the KL25Z's 
 //   power or software state, so it's won't affect the latch state when
 //   the KL25Z is unplugged or rebooted; when we boot, we'll see that 
@@ -1756,7 +1991,6 @@
 //   This is important because TV ON buttons are usually on/off toggles,
 //   so we don't want to push the button on a TV that's already on.
 //   
-//
 
 // Current PSU2 state:
 //   1 -> default: latch was on at last check, or we haven't checked yet
@@ -1764,7 +1998,6 @@
 //   3 -> SET pulsed low, ready to check status
 //   4 -> TV timer countdown in progress
 //   5 -> TV relay on
-//   
 int psu2_state = 1;
 
 // PSU2 power sensing circuit connections
@@ -1810,7 +2043,7 @@
         
     case 3:
         // CHECK state: we pulsed SET, and we're now ready to see
-        // if that stuck.  If the latch is now on, PSU2 has transitioned
+        // if it stuck.  If the latch is now on, PSU2 has transitioned
         // from OFF to ON, so start the TV countdown.  If the latch is
         // off, our SET command didn't stick, so PSU2 is still off.
         if (psu2_status_sense->read())
@@ -1863,7 +2096,7 @@
         psu2_status_sense = new DigitalIn(cfg.TVON.statusPin);
         psu2_status_set = new DigitalOut(cfg.TVON.latchPin);
         tv_relay = new DigitalOut(cfg.TVON.relayPin);
-        tv_delay_time = cfg.TVON.delayTime;
+        tv_delay_time = cfg.TVON.delayTime/100.0;
     
         // Set up our time routine to run every 1/4 second.  
         tv_ticker.attach(&TVTimerInt, 0.25);
@@ -1957,43 +2190,26 @@
 
 // ---------------------------------------------------------------------------
 //
-// NIGHT MODE flag.  When night mode is on, we disable all outputs
-// marked as "noisemakers" in the output configuration flags.
-int nightMode;
-
-// Update the global output mode settings
-static void globalOutputModeChange()
-{
-    // set the global modeLevel[] 
-    for (int i = 0 ; i < numOutputs ; ++i)
-    {
-        // assume the port will be on
-        uint8_t f = 1;
-        
-        // if night mode is in effect, and this is a noisemaker, disable it
-        if (nightMode && (cfg.outPort[i].flags & PortFlagNoisemaker) != 0)
-            f = 0;
-            
-        // set the final output port override value
-        modeLevel[i] = f;
-    }
-    
-    // update all outputs for the mode change
-    updateAllOuts();
-}
+// Night mode setting updates
+//
 
 // Turn night mode on or off
 static void setNightMode(bool on)
 {
-    nightMode = on;
-    globalOutputModeChange();
-    specialPin[0]->set(on ? 255.0 : 0.0);
+    // set the new night mode flag in the noisy output class
+    LwNoisyOut::nightMode = on;
+
+    // update the special output pin that shows the night mode state
+    specialPin[SPECIAL_PIN_NIGHTMODE]->set(on ? 255 : 0);
+
+    // update all outputs for the mode change
+    updateAllOuts();
 }
 
 // Toggle night mode
 static void toggleNightMode()
 {
-    setNightMode(!nightMode);
+    setNightMode(!LwNoisyOut::nightMode);
 }
 
 
@@ -2131,144 +2347,32 @@
 
 // ---------------------------------------------------------------------------
 //
-// Handle a configuration variable update.  'data' is the USB message we
-// received from the host.
+// Configuration variable get/set message handling
 //
-void configVarMsg(uint8_t *data)
-{
-    switch (data[1])
-    {
-    case 1:
-        // USB identification (Vendor ID, Product ID)
-        cfg.usbVendorID = wireUI16(data+2);
-        cfg.usbProductID = wireUI16(data+4);
-        break;
-        
-    case 2:
-        // Pinscape Controller unit number - note that data[2] contains
-        // the nominal unit number, 1-16
-        if (data[2] >= 1 && data[2] <= 16)
-            cfg.psUnitNo = data[2];
-        break;
-        
-    case 3:
-        // Enable/disable joystick
-        cfg.joystickEnabled = data[2];
-        break;
-        
-    case 4:
-        // Accelerometer orientation
-        cfg.orientation = data[2];
-        break;
+
+// Handle SET messages - write configuration variables from USB message data
+#define if_msg_valid(test)  if (test)
+#define v_byte(var, ofs)   cfg.var = data[ofs]
+#define v_ui16(var, ofs)   cfg.var = wireUI16(data+ofs)
+#define v_pin(var, ofs)    cfg.var = wirePinName(data[ofs])
+#define v_func configVarSet
+#include "cfgVarMsgMap.h"
 
-    case 5:
-        // Plunger sensor type
-        cfg.plunger.sensorType = data[2];
-        break;
-        
-    case 6:
-        // Set plunger pin assignments
-        cfg.plunger.sensorPin[0] = wirePinName(data[2]);
-        cfg.plunger.sensorPin[1] = wirePinName(data[3]);
-        cfg.plunger.sensorPin[2] = wirePinName(data[4]);
-        cfg.plunger.sensorPin[3] = wirePinName(data[5]);
-        break;
-        
-    case 7:
-        // Plunger calibration button and indicator light pin assignments
-        cfg.plunger.cal.btn = wirePinName(data[2]);
-        cfg.plunger.cal.led = wirePinName(data[3]);
-        break;
-        
-    case 8:
-        // ZB Launch Ball setup
-        cfg.plunger.zbLaunchBall.port = (int)(unsigned char)data[2];
-        cfg.plunger.zbLaunchBall.btn = (int)(unsigned char)data[3];
-        cfg.plunger.zbLaunchBall.pushDistance = (float)wireUI16(data+4) / 1000.0;
-        break;
-        
-    case 9:
-        // TV ON setup
-        cfg.TVON.statusPin = wirePinName(data[2]);
-        cfg.TVON.latchPin = wirePinName(data[3]);
-        cfg.TVON.relayPin = wirePinName(data[4]);
-        cfg.TVON.delayTime = (float)wireUI16(data+5) / 100.0;
-        break;
-        
-    case 10:
-        // TLC5940NT PWM controller chip setup
-        cfg.tlc5940.nchips = (int)(unsigned char)data[2];
-        cfg.tlc5940.sin = wirePinName(data[3]);
-        cfg.tlc5940.sclk = wirePinName(data[4]);
-        cfg.tlc5940.xlat = wirePinName(data[5]);
-        cfg.tlc5940.blank = wirePinName(data[6]);
-        cfg.tlc5940.gsclk = wirePinName(data[7]);
-        break;
-        
-    case 11:
-        // 74HC595 shift register chip setup
-        cfg.hc595.nchips = (int)(unsigned char)data[2];
-        cfg.hc595.sin = wirePinName(data[3]);
-        cfg.hc595.sclk = wirePinName(data[4]);
-        cfg.hc595.latch = wirePinName(data[5]);
-        cfg.hc595.ena = wirePinName(data[6]);
-        break;
-        
-    case 12:
-        // button setup
-        {
-            // get the button number
-            int idx = data[2];
-            
-            // if it's in range, set the button data
-            if (idx > 0 && idx <= MAX_BUTTONS)
-            {
-                // adjust to an array index
-                --idx;
-                
-                // set the values
-                cfg.button[idx].pin = data[3];
-                cfg.button[idx].typ = data[4];
-                cfg.button[idx].val = data[5];
-                cfg.button[idx].flags = data[6];
-            }
-        }
-        break;
-        
-    case 13:
-        // LedWiz output port setup
-        {
-            // get the port number
-            int idx = data[2];
-            
-            // if it's in range, set the port data
-            if (idx > 0 && idx <= MAX_OUT_PORTS)
-            {
-                // adjust to an array index
-                --idx;
-                
-                // set the values
-                cfg.outPort[idx].typ = data[3];
-                cfg.outPort[idx].pin = data[4];
-                cfg.outPort[idx].flags = data[5];
-            }
-            else if (idx == 254)
-            {
-                // special ports
-                idx -= 254;
-                cfg.specialPort[idx].typ = data[3];
-                cfg.specialPort[idx].pin = data[4];
-                cfg.specialPort[idx].flags = data[5];
-            }
-        }
-        break;
+// redefine everything for the SET messages
+#undef if_msg_valid
+#undef v_byte
+#undef v_ui16
+#undef v_pin
+#undef v_func
 
-    case 14:
-        // engage/cancel Night Mode
-        setNightMode(data[2]);
-        break;
-    }
-}
+// Handle GET messages - read variable values and return in USB message daa
+#define if_msg_valid(test)
+#define v_byte(var, ofs)   data[ofs] = cfg.var
+#define v_ui16(var, ofs)   ui16Wire(data+ofs, cfg.var)
+#define v_pin(var, ofs)    pinNameWire(data+ofs, cfg.var)
+#define v_func  configVarGet
+#include "cfgVarMsgMap.h"
+
 
 // ---------------------------------------------------------------------------
 //
@@ -2326,7 +2430,7 @@
             // states are independent, so an SBA just turns an output
             // on or off but retains its last brightness level.
             if (wizVal[i] == 255)
-                wizVal[i] = (uint8_t)round(outLevel[i]*48);
+                wizVal[i] = (uint8_t)round(outLevel[i]/255.0 * 48.0);
         }
         
         // set the flash speed - enforce the value range 1-7
@@ -2416,7 +2520,8 @@
             js.reportConfig(
                 numOutputs, 
                 cfg.psUnitNo - 1,   // report 0-15 range for unit number (we store 1-16 internally)
-                cfg.plunger.cal.zero, cfg.plunger.cal.max);
+                cfg.plunger.cal.zero, cfg.plunger.cal.max,
+                nvm.valid());
             break;
             
         case 5:
@@ -2434,6 +2539,18 @@
             // really needed.
             reboot(js);
             break;
+            
+        case 7:
+            // 7 = Device ID report
+            // (No parameters)
+            js.reportID();
+            break;
+            
+        case 8:
+            // 8 = Engage/disengage night mode.
+            //     data[2] = 1 to engage, 0 to disengage
+            setNightMode(data[2]);
+            break;
         }
     }
     else if (data[0] == 66)
@@ -2442,7 +2559,7 @@
         // The second byte of the message is the ID of the variable
         // to update, and the remaining bytes give the new value,
         // in a variable-dependent format.
-        configVarMsg(data);
+        configVarSet(data);
     }
     else if (data[0] >= 200 && data[0] <= 228)
     {
@@ -2470,7 +2587,7 @@
         for (int i = i0 ; i < i1 ; ++i)
         {
             // set the brightness level for the output
-            float b = data[i-i0+1]/255.0;
+            uint8_t b = data[i-i0+1];
             outLevel[i] = b;
             
             // if it's in the basic LedWiz output set, set the LedWiz
@@ -2479,7 +2596,7 @@
                 wizVal[i] = 255;
                 
             // set the output
-            lwPin[i]->set(b * modeLevel[i]);
+            lwPin[i]->set(b);
         }
         
         // update 74HC595 outputs, if attached
@@ -2567,12 +2684,9 @@
     Ticker preConnectTicker;
     preConnectTicker.attach(preConnectFlasher, 3);
 
-    // start the TV timer, if applicable
-    startTVTimer(cfg);
-    
     // we're not connected/awake yet
     bool connected = false;
-    time_t connectChangeTime = time(0);
+    Timer connectChangeTimer;
 
     // create the plunger sensor interface
     createPlunger();
@@ -2594,6 +2708,9 @@
     if (tlc5940 != 0)
         tlc5940->start();
         
+    // start the TV timer, if applicable
+    startTVTimer(cfg);
+    
     // initialize the button input ports
     bool kbKeys = false;
     initButtons(cfg, kbKeys);
@@ -2604,7 +2721,7 @@
     
     // we're now connected - kill the pre-connect ticker
     preConnectTicker.detach();
-        
+    
     // Last report timer for the joytick interface.  We use the joystick timer 
     // to throttle the report rate, because VP doesn't benefit from reports any 
     // faster than about every 10ms.
@@ -2980,7 +3097,7 @@
             if (cfg.plunger.zbLaunchBall.port != 0)
             {
                 const int cockThreshold = JOYMAX/3;
-                const int pushThreshold = int(-JOYMAX/3 * cfg.plunger.zbLaunchBall.pushDistance);
+                const int pushThreshold = int(-JOYMAX/3.0 * cfg.plunger.zbLaunchBall.pushDistance/1000.0);
                 int newState = lbState;
                 switch (lbState)
                 {
@@ -3293,20 +3410,70 @@
 #endif
 
         // check for connection status changes
-        int newConnected = js.isConnected() && !js.isSuspended();
+        bool newConnected = js.isConnected() && !js.isSuspended();
         if (newConnected != connected)
         {
             // give it a few seconds to stabilize
-            time_t tc = time(0);
-            if (tc - connectChangeTime > 3)
+            connectChangeTimer.start();
+            if (connectChangeTimer.read() > 3)
             {
                 // note the new status
                 connected = newConnected;
-                connectChangeTime = tc;
+                
+                // done with the change timer for this round - reset it for next time
+                connectChangeTimer.stop();
+                connectChangeTimer.reset();
                 
-                // if we're no longer connected, turn off all outputs
-                if (!connected)
+                // adjust to the new status
+                if (connected)
+                {
+                    // We're newly connected.  This means we just powered on, we were
+                    // just plugged in to the PC USB port after being unplugged, or the
+                    // PC just came out of sleep/suspend mode and resumed the connection.
+                    // In any of these cases, we can now assume that the PC power supply
+                    // is on (the PC must be on for the USB connection to be running, and
+                    // if the PC is on, its power supply is on).  This also means that 
+                    // power to any external output controller chips (TLC5940, 74HC595)
+                    // is now on, because those have to be powered from the PC power
+                    // supply to allow for a reliable data connection to the KL25Z.
+                    // We can thus now set clear initial output state in those chips and
+                    // enable their outputs.
+                    if (tlc5940 != 0)
+                    {
+                        tlc5940->update(true);
+                        tlc5940->enable(true);
+                    }
+                    if (hc595 != 0)
+                    {
+                        hc595->update(true);
+                        hc595->enable(true);
+                    }
+                }
+                else
+                {
+                    // We're no longer connected.  Turn off all outputs.
                     allOutputsOff();
+                    
+                    // The KL25Z runs off of USB power, so we might (depending on the PC
+                    // and OS configuration) continue to receive power even when the main
+                    // PC power supply is turned off, such as in soft-off or suspend/sleep
+                    // mode.  Any external output controller chips (TLC5940, 74HC595) might
+                    // be powered from the PC power supply directly rather than from our
+                    // USB power, so they might be powered off even when we're still running.
+                    // To ensure cleaner startup when the power comes back on, globally
+                    // disable the outputs.  The global disable signals come from GPIO lines
+                    // that remain powered as long as the KL25Z is powered, so these modes
+                    // will apply smoothly across power state transitions in the external
+                    // hardware.  That is, when the external chips are powered up, they'll
+                    // see the global disable signals as stable voltage inputs immediately,
+                    // which will cause them to suppress any output triggering.  This ensures
+                    // that we don't fire any solenoids or flash any lights spuriously when
+                    // the power first comes on.
+                    if (tlc5940 != 0)
+                        tlc5940->enable(false);
+                    if (hc595 != 0)
+                        hc595->enable(false);
+                }
             }
         }
         
@@ -3335,23 +3502,19 @@
             else if (jsOKTimer.read() > 5)
             {
                 // USB freeze - show red/yellow.
-                //  Our outgoing joystick messages aren't going through, even though we
+                // Our outgoing joystick messages aren't going through, even though we
                 // think we're still connected.  This indicates that one or more of our
                 // USB endpoints have stopped working, which can happen as a result of
                 // bugs in the USB HAL or latency responding to a USB IRQ.  Show a
                 // distinctive diagnostic flash to signal the error.  I haven't found a 
                 // way to recover from this class of error other than rebooting the MCU, 
-                // so the goal is to fix the HAL so that this error never happens.  This
-                // flash pattern is thus for debugging purposes only; hopefully it won't
-                // ever occur in a real installation.
-                static bool dumped;
-                if (!dumped) {
-                    // If we haven't already, dump the USB HAL status to the debug console,
-                    // in case it helps identify the reason for the endpoint failure.
-                    extern void USBDeviceStatusDump(void);
-                    USBDeviceStatusDump();
-                    dumped = true;
-                }
+                // so the goal is to fix the HAL so that this error never happens.  
+                //
+                // NOTE!  This diagnostic code *hopefully* shouldn't occur.  It happened
+                // in the past due to a number of bugs in the mbed KL25Z USB HAL that
+                // I've since fixed.  I think I found all of the cases that caused it,
+                // but I'm leaving the diagnostics here in case there are other bugs
+                // still lurking that can trigger the same symptoms.
                 jsOKTimer.stop();
                 hb = !hb;
                 diagLED(1, hb, 0);