Advertisement
CaptainSpaceCat

LedControl.h Backup

Apr 2nd, 2017
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *    LedControl.h - A library for controling Leds with a MAX7219/MAX7221
  3.  *    Copyright (c) 2007 Eberhard Fahle
  4.  *
  5.  *    Permission is hereby granted, free of charge, to any person
  6.  *    obtaining a copy of this software and associated documentation
  7.  *    files (the "Software"), to deal in the Software without
  8.  *    restriction, including without limitation the rights to use,
  9.  *    copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  *    copies of the Software, and to permit persons to whom the
  11.  *    Software is furnished to do so, subject to the following
  12.  *    conditions:
  13.  *
  14.  *    This permission notice shall be included in all copies or
  15.  *    substantial portions of the Software.
  16.  *
  17.  *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18.  *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19.  *    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20.  *    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21.  *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22.  *    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23.  *    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24.  *    OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27. #ifndef LedControl_h
  28. #define LedControl_h
  29.  
  30. #if (ARDUINO >= 100)
  31. #include <Arduino.h>
  32. #else
  33. #include <WProgram.h>
  34. #endif
  35.  
  36. /*
  37.  * Segments to be switched on for characters and digits on
  38.  * 7-Segment Displays
  39.  */
  40. /*const static byte charTable[128] = {
  41.     B01111110,B00110000,B01101101,B01111001,B00110011,B01011011,B01011111,B01110000,
  42.     B01111111,B01111011,B01110111,B00011111,B00001101,B00111101,B01001111,B01000111,
  43.     B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  44.     B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  45.     B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  46.     B00000000,B00000000,B00000000,B00000000,B10000000,B00000001,B10000000,B00000000,
  47.     B01111110,B00110000,B01101101,B01111001,B00110011,B01011011,B01011111,B01110000,
  48.     B01111111,B01111011,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  49.     B00000000,B01110111,B00011111,B00001101,B00111101,B01001111,B01000111,B00000000,
  50.     B00110111,B00000000,B00000000,B00000000,B00001110,B00000000,B00000000,B00000000,
  51.     B01100111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  52.     B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001000,
  53.     B00000000,B01110111,B00011111,B00001101,B00111101,B01001111,B01000111,B00000000,
  54.     B00110111,B00000000,B00000000,B00000000,B00001110,B00000000,B00000000,B00000000,
  55.     B01100111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  56.     B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000
  57. };*/
  58.  
  59. class LedControl {
  60.  private :
  61.     /* The array for shifting the data to the devices */
  62.     byte spidata[16];
  63.     /* Send out a single command to the device */
  64.     void spiTransfer(int addr, byte opcode, byte data);
  65.  
  66.     /* We keep track of the led-status for all 8 devices in this array */
  67.     byte status[64];
  68.     /* Data is shifted out of this pin*/
  69.     int SPI_MOSI;
  70.     /* The clock is signaled on this pin */
  71.     int SPI_CLK;
  72.     /* This one is driven LOW for chip selectzion */
  73.     int SPI_CS;
  74.     /* The maximum number of devices we use */
  75.     int maxDevices;
  76.    
  77.  public:
  78.     /*
  79.      * Create a new controler
  80.      * Params :
  81.      * dataPin      pin on the Arduino where data gets shifted out
  82.      * clockPin     pin for the clock
  83.      * csPin        pin for selecting the device
  84.      * numDevices   maximum number of devices that can be controled
  85.      */
  86.     LedControl(int dataPin, int clkPin, int csPin, int numDevices=1);
  87.  
  88.     /*
  89.      * Gets the number of devices attached to this LedControl.
  90.      * Returns :
  91.      * int  the number of devices on this LedControl
  92.      */
  93.     int getDeviceCount();
  94.  
  95.     /*
  96.      * Set the shutdown (power saving) mode for the device
  97.      * Params :
  98.      * addr The address of the display to control
  99.      * status   If true the device goes into power-down mode. Set to false
  100.      *      for normal operation.
  101.      */
  102.     void shutdown(int addr, bool status);
  103.  
  104.     /*
  105.      * Set the number of digits (or rows) to be displayed.
  106.      * See datasheet for sideeffects of the scanlimit on the brightness
  107.      * of the display.
  108.      * Params :
  109.      * addr address of the display to control
  110.      * limit    number of digits to be displayed (1..8)
  111.      */
  112.     void setScanLimit(int addr, int limit);
  113.  
  114.     /*
  115.      * Set the brightness of the display.
  116.      * Params:
  117.      * addr     the address of the display to control
  118.      * intensity    the brightness of the display. (0..15)
  119.      */
  120.     void setIntensity(int addr, int intensity);
  121.  
  122.     /*
  123.      * Switch all Leds on the display off.
  124.      * Params:
  125.      * addr address of the display to control
  126.      */
  127.     void clearDisplay(int addr);
  128.  
  129.     /*
  130.      * Set the status of a single Led.
  131.      * Params :
  132.      * addr address of the display
  133.      * row  the row of the Led (0..7)
  134.      * col  the column of the Led (0..7)
  135.      * state    If true the led is switched on,
  136.      *      if false it is switched off
  137.      */
  138.     void setLed(int addr, int row, int col, boolean state);
  139.  
  140.     /*
  141.      * Set all 8 Led's in a row to a new state
  142.      * Params:
  143.      * addr address of the display
  144.      * row  row which is to be set (0..7)
  145.      * value    each bit set to 1 will light up the
  146.      *      corresponding Led.
  147.      */
  148.     void setRow(int addr, int row, byte value);
  149.  
  150.     /*
  151.      * Set all 8 Led's in a column to a new state
  152.      * Params:
  153.      * addr address of the display
  154.      * col  column which is to be set (0..7)
  155.      * value    each bit set to 1 will light up the
  156.      *      corresponding Led.
  157.      */
  158.     void setColumn(int addr, int col, byte value);
  159.  
  160.     /*
  161.      * Display a hexadecimal digit on a 7-Segment Display
  162.      * Params:
  163.      * addr address of the display
  164.      * digit    the position of the digit on the display (0..7)
  165.      * value    the value to be displayed. (0x00..0x0F)
  166.      * dp   sets the decimal point.
  167.      */
  168.     void setDigit(int addr, int digit, byte value, boolean dp);
  169.  
  170.     /*
  171.      * Display a character on a 7-Segment display.
  172.      * There are only a few characters that make sense here :
  173.      *  '0','1','2','3','4','5','6','7','8','9','0',
  174.      *  'A','b','c','d','E','F','H','L','P',
  175.      *  '.','-','_',' '
  176.      * Params:
  177.      * addr address of the display
  178.      * digit    the position of the character on the display (0..7)
  179.      * value    the character to be displayed.
  180.      * dp   sets the decimal point.
  181.      */
  182.     void setChar(int addr, int digit, char value, boolean dp);
  183. };
  184.  
  185. #endif  //LedControl.h
Advertisement
RAW Paste Data Copied
Advertisement