CaptainSpaceCat

LedControl.cpp Backup

Apr 2nd, 2017
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *    LedControl.cpp - 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.  
  28. #include "LedControl.h"
  29.  
  30. //the opcodes for the MAX7221 and MAX7219
  31. #define OP_NOOP   0
  32. #define OP_DIGIT0 1
  33. #define OP_DIGIT1 2
  34. #define OP_DIGIT2 3
  35. #define OP_DIGIT3 4
  36. #define OP_DIGIT4 5
  37. #define OP_DIGIT5 6
  38. #define OP_DIGIT6 7
  39. #define OP_DIGIT7 8
  40. #define OP_DECODEMODE  9
  41. #define OP_INTENSITY   10
  42. #define OP_SCANLIMIT   11
  43. #define OP_SHUTDOWN    12
  44. #define OP_DISPLAYTEST 15
  45.  
  46. LedControl::LedControl(int dataPin, int clkPin, int csPin, int numDevices) {
  47.     SPI_MOSI=dataPin;
  48.     SPI_CLK=clkPin;
  49.     SPI_CS=csPin;
  50.     if(numDevices<=0 || numDevices>8 )
  51.     numDevices=8;
  52.     maxDevices=numDevices;
  53.     pinMode(SPI_MOSI,OUTPUT);
  54.     pinMode(SPI_CLK,OUTPUT);
  55.     pinMode(SPI_CS,OUTPUT);
  56.     digitalWrite(SPI_CS,HIGH);
  57.     SPI_MOSI=dataPin;
  58.     for(int i=0;i<64;i++)
  59.     status[i]=0x00;
  60.     for(int i=0;i<maxDevices;i++) {
  61.     spiTransfer(i,OP_DISPLAYTEST,0);
  62.     //scanlimit is set to max on startup
  63.     setScanLimit(i,7);
  64.     //decode is done in source
  65.     spiTransfer(i,OP_DECODEMODE,0);
  66.     clearDisplay(i);
  67.     //we go into shutdown-mode on startup
  68.     shutdown(i,true);
  69.     }
  70. }
  71.  
  72. int LedControl::getDeviceCount() {
  73.     return maxDevices;
  74. }
  75.  
  76. void LedControl::shutdown(int addr, bool b) {
  77.     if(addr<0 || addr>=maxDevices)
  78.     return;
  79.     if(b)
  80.     spiTransfer(addr, OP_SHUTDOWN,0);
  81.     else
  82.     spiTransfer(addr, OP_SHUTDOWN,1);
  83. }
  84.    
  85. void LedControl::setScanLimit(int addr, int limit) {
  86.     if(addr<0 || addr>=maxDevices)
  87.     return;
  88.     if(limit>=0 || limit<8)
  89.         spiTransfer(addr, OP_SCANLIMIT,limit);
  90. }
  91.  
  92. void LedControl::setIntensity(int addr, int intensity) {
  93.     if(addr<0 || addr>=maxDevices)
  94.     return;
  95.     if(intensity>=0 || intensity<16)   
  96.     spiTransfer(addr, OP_INTENSITY,intensity);
  97.    
  98. }
  99.  
  100. void LedControl::clearDisplay(int addr) {
  101.     int offset;
  102.  
  103.     if(addr<0 || addr>=maxDevices)
  104.     return;
  105.     offset=addr*8;
  106.     for(int i=0;i<8;i++) {
  107.     status[offset+i]=0;
  108.     spiTransfer(addr, i+1,status[offset+i]);
  109.     }
  110. }
  111.  
  112. void LedControl::setLed(int addr, int row, int column, boolean state) {
  113.     int offset;
  114.     byte val=0x00;
  115.  
  116.     if(addr<0 || addr>=maxDevices)
  117.     return;
  118.     if(row<0 || row>7 || column<0 || column>7)
  119.     return;
  120.     offset=addr*8;
  121.     val=B10000000 >> column;
  122.     if(state)
  123.     status[offset+row]=status[offset+row]|val;
  124.     else {
  125.     val=~val;
  126.     status[offset+row]=status[offset+row]&val;
  127.     }
  128.     spiTransfer(addr, row+1,status[offset+row]);
  129. }
  130.    
  131. void LedControl::setRow(int addr, int row, byte value) {
  132.     int offset;
  133.     if(addr<0 || addr>=maxDevices)
  134.     return;
  135.     if(row<0 || row>7)
  136.     return;
  137.     offset=addr*8;
  138.     status[offset+row]=value;
  139.     spiTransfer(addr, row+1,status[offset+row]);
  140. }
  141.    
  142. void LedControl::setColumn(int addr, int col, byte value) {
  143.     byte val;
  144.  
  145.     if(addr<0 || addr>=maxDevices)
  146.     return;
  147.     if(col<0 || col>7)
  148.     return;
  149.     for(int row=0;row<8;row++) {
  150.     val=value >> (7-row);
  151.     val=val & 0x01;
  152.     setLed(addr,row,col,val);
  153.     }
  154. }
  155.  
  156. void LedControl::setDigit(int addr, int digit, byte value, boolean dp) {
  157.     int offset;
  158.     byte v;
  159.  
  160.     if(addr<0 || addr>=maxDevices)
  161.     return;
  162.     if(digit<0 || digit>7 || value>15)
  163.     return;
  164.     offset=addr*8;
  165.     //v=charTable[value];
  166.     if(dp)
  167.     v|=B10000000;
  168.     status[offset+digit]=v;
  169.     spiTransfer(addr, digit+1,v);
  170.    
  171. }
  172.  
  173. void LedControl::setChar(int addr, int digit, char value, boolean dp) {
  174.     int offset;
  175.     byte index,v;
  176.  
  177.     if(addr<0 || addr>=maxDevices)
  178.     return;
  179.     if(digit<0 || digit>7)
  180.     return;
  181.     offset=addr*8;
  182.     index=(byte)value;
  183.     if(index >127) {
  184.     //nothing define we use the space char
  185.     value=32;
  186.     }
  187.     //v=charTable[index];
  188.     if(dp)
  189.     v|=B10000000;
  190.     status[offset+digit]=v;
  191.     spiTransfer(addr, digit+1,v);
  192. }
  193.  
  194. void LedControl::spiTransfer(int addr, volatile byte opcode, volatile byte data) {
  195.     //Create an array with the data to shift out
  196.     int offset=addr*2;
  197.     int maxbytes=maxDevices*2;
  198.  
  199.     for(int i=0;i<maxbytes;i++)
  200.     spidata[i]=(byte)0;
  201.     //put our device data into the array
  202.     spidata[offset+1]=opcode;
  203.     spidata[offset]=data;
  204.     //enable the line
  205.     digitalWrite(SPI_CS,LOW);
  206.     //Now shift out the data
  207.     for(int i=maxbytes;i>0;i--)
  208.     shiftOut(SPI_MOSI,SPI_CLK,MSBFIRST,spidata[i-1]);
  209.     //latch the data onto the display
  210.     digitalWrite(SPI_CS,HIGH);
  211. }
RAW Paste Data Copied