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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TinyDigitalIn.h Source File

TinyDigitalIn.h

00001 #include "mbed.h"
00002 
00003 // TinyDigitalIn - a simpler verison of DigitalIn that takes less
00004 // memory.
00005 //
00006 // This version uses the same mbed library infrastructure as the
00007 // regular DigitalIn, but we save a little memory by storing only
00008 // the minimum set of fields needed to read the pin.  The mbed
00009 // DigitalIn has a larger memory footprint because it stores the 
00010 // full set of machine register pointers for the pin, most of 
00011 // which aren't needed for an input-only pin.
00012 
00013 class TinyDigitalIn
00014 {
00015 public:
00016     TinyDigitalIn(PinName pin) { assignPin(pin); }
00017     TinyDigitalIn() { assignPin(NC); }
00018     
00019     void assignPin(PinName pin)
00020     {
00021         if (pin != NC)
00022         {
00023             // initialize the pin as a GPIO Digital In port
00024             gpio_t gpio;
00025             gpio_init_in(&gpio, pin);
00026             
00027             // get the register input port and mask
00028             pdir = gpio.reg_in;
00029             uint32_t mask = gpio.mask;
00030             
00031             // Figure the bit shift: find how many right shifts it takes
00032             // to shift the mask bit into the 0th bit position.  This lets
00033             // us pull out the result value in read() as a 0 or 1 by shifting
00034             // the register by this same amount and masking it against 0x01.
00035             // The right shift is slightly more efficient than a conditional
00036             // to convert a bit in the middle of the register to a canonical
00037             // 0 or 1 result, and we have to do the mask anyway to pull out
00038             // the one bit, so this makes the overall read slightly faster.
00039             for (shift = 0 ; 
00040                  mask != 0 && (mask & 0x00000001) == 0 ;
00041                  mask >>= 1, shift++) ;
00042          }
00043          else
00044          {
00045              // not connected - point to a dummy port that always reads as 0
00046              pdir = (volatile uint32_t *)&pdir_nc;
00047              shift = 0;
00048          }
00049     }
00050     
00051     inline int read() { return (*pdir >> shift) & 0x00000001; }
00052     inline operator int() { return read(); }
00053     
00054 protected:
00055     volatile uint32_t *pdir;    // pointer to GPIO register for this port 
00056     uint8_t shift;              // number of bits to shift register value to get our port bit
00057     
00058     // pointer to dummy location for NC ports - reads as all 1 bits,
00059     // as though it were wired to a pull-up port that's not connected
00060     // to anything external
00061     static const uint32_t pdir_nc;
00062 } __attribute__((packed));