LedControl::LedControl(int dataPin, int clkPin, int csPin, int numDevices) { LEDCONTROL_SPI_MOSI=dataPin; LEDCONTROL_SPI_CLK=clkPin; LEDCONTROL_SPI_CS=csPin; if(numDevices<=0 || numDevices>8 ) numDevices=8; maxDevices=numDevices; pinMode(LEDCONTROL_SPI_MOSI,OUTPUT); pinMode(LEDCONTROL_SPI_CLK,OUTPUT); pinMode(LEDCONTROL_SPI_CS,OUTPUT); digitalWrite(LEDCONTROL_SPI_CS,HIGH); LEDCONTROL_SPI_MOSI=dataPin; for(int i=0;i<64;i++) status[i]=0x00; for(int i=0;i<maxDevices;i++) { spiTransfer(i,OP_DISPLAYTEST,0); //scanlimit is set to max on startup setScanLimit(i,7); //decode is done in source spiTransfer(i,OP_DECODEMODE,0); clearDisplay(i); //we go into shutdown-mode on startup shutdown(i,true); } }
LedControl::LedControl(int dataPin, int clkPin, int csPin, int numDevices, bool anode, bool reflect, bool upsidedown) { SPI_MOSI=dataPin; SPI_CLK=clkPin; SPI_CS=csPin; if(numDevices<=0 || numDevices>8 ) numDevices=8; maxDevices=numDevices; anodeMode=anode; reflectMode=reflect; upsidedownMode=upsidedown; pinMode(SPI_MOSI,OUTPUT); pinMode(SPI_CLK,OUTPUT); pinMode(SPI_CS,OUTPUT); digitalWrite(SPI_CS,HIGH); SPI_MOSI=dataPin; for(int i=0;i<64;i++) { status[i]=0x00; statusTransposed[i]=0x00; } for(int i=0;i<maxDevices;i++) { spiTransfer(i,OP_DISPLAYTEST,0); //scanlimit is set to max on startup setScanLimit(i,7); //decode is done in source spiTransfer(i,OP_DECODEMODE,0); clearDisplay(i); //we go into shutdown-mode on startup shutdown(i,true); } }
/* || @constructor || | Initializes the digital pins and the Max7219 || # || || @parameter data The data pin connected to the Max7219 || @parameter clock The clock pin connected to the Max7219 || @parameter load The load pin connected to the Max7219 || @parameter screens The number of screens */ Matrix::Matrix(byte data, byte clock, byte load, byte screens /* = 1 */) { // record pins for sw spi dataPin = data; clockPin = clock; loadPin = load; // set ddr for sw spi pins pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(loadPin, OUTPUT); // allocate screenbuffers numberOfScreens = numberOfScreens; buf = (byte*)calloc(numberOfScreens, 64); maximumX = (numberOfScreens * 8); // initialize registers clear(); // clear display setScanLimit(0x07); // use all rows/digits setBrightness(0x0F); // maximum brightness setRegister(REG_SHUTDOWN, 0x01); // normal operation setRegister(REG_DECODEMODE, 0x00); // pixels not integers setRegister(REG_DISPLAYTEST, 0x00); // not in test mode }
Matrix::Matrix(uint8_t data, uint8_t clock, uint8_t load, uint8_t screens /* = 1 */) { // record pins for sw spi _pinData = data; _pinClock = clock; _pinLoad = load; // set ddr for sw spi pins pinMode(_pinClock, OUTPUT); pinMode(_pinData, OUTPUT); pinMode(_pinLoad, OUTPUT); // allocate screenbuffers _screens = screens; _buffer = (uint8_t*)calloc(_screens, 64); _maximumX = (_screens * 8); // initialize registers clear(); // clear display setScanLimit(0x07); // use all rows/digits setBrightness(0x0F); // maximum brightness setRegister(REG_SHUTDOWN, 0x01); // normal operation setRegister(REG_DECODEMODE, 0x00); // pixels not integers setRegister(REG_DISPLAYTEST, 0x00); // not in test mode }
MAX7219::MAX7219(SPI* spi, int numDevices) { assert(spi != nullptr); this->spi = spi; if (numDevices <= 0 || numDevices > 8) { numDevices = 8; } maxDevices = numDevices; for (uint8_t i = 0; i < 64; i++) { status[i] = 0x00; } for (int i = 0; i < maxDevices; i++) { spiTransfer(i, OP_DISPLAYTEST, 0); //scanlimit is set to max on startup setScanLimit(7, i); //decode is done in source spiTransfer(i, OP_DECODEMODE, 0); clearDisplay(i); //we go into shutdown-mode on startup shutdown(true, i); } }
void max7221::init() { /* * set the pin configuration and default values to initially turn on the chip. * this function has to be called EXACTLY one time before any data is pushed to the chip. * it is NOT a class constructor, because we will use multiple instances of this class */ pinMode( _pinCLK, OUTPUT ); pinMode( _pinDATA, OUTPUT ); pinMode( _pinLOAD, OUTPUT ); for( int i = 0; i < _chipcount; i++ ) { selectChip(i); setDecodeMode( 0x00 ); //use a custom matrix on all registers setIntensity( 0x0f ); //use maximum intensity setScanLimit( 0x07 ); //use all 8 available registers setShutdown( 0x01 ); //wake up setDisplayTest( 0x00 ); //no display test } selectChip(0);//reset to default first chip for backwards compatibility }
void MAX7219::begin(const MAX7219_Topology *topology, const byte length) { MAX7219_Topology *defaultTopo; if(topology) { _topology = topology; _elements = length; } else { //Yes, we are leaking memory here. Yet again, in embedded software //things usually get allocated at start and never die off as there's //no exit(); defaultTopo = (MAX7219_Topology *)malloc(sizeof(MAX7219_Topology) * MAX7219_DEFAULT_LENGTH); MAX7219_DEFAULT_TOPOLOGY(defaultTopo); _topology = defaultTopo; _elements = MAX7219_DEFAULT_LENGTH; }; _chips = 0; for(int i = 0; i < length; i++) if(_topology[i].chipTo > _chips) _chips = _topology[i].chipTo; _chips++; #if defined(MAX7219_DEBUG) Serial.print("Topology has "); Serial.print(_elements); Serial.print(" elements which span "); Serial.print(_chips); Serial.println(" chips in total."); #endif SPI.begin(); SPI.setBitOrder(MSBFIRST); SPI.setDataMode(SPI_MODE0); //1MHz suffices for doing 25fps to 625 chained chips driving 8x8 matrices, //2MHz (as you would get on the Uno/Mega) is twice that and if you find //yourself needing more, you shouldn't be using Arduino anyway SPI.setClockDivider(SPI_CLOCK_DIV8); //Since the MAX7219 does not have a RESET, we must enforce consistency noDisplayTest(MAX7219_CHIP_ALL); setScanLimit(0x07, MAX7219_CHIP_ALL); setIntensity(0x08, MAX7219_CHIP_ALL); writeRegister(MAX7219_REG_DECODEMODE, MAX7219_FLG_DIGIT0_RAW | MAX7219_FLG_DIGIT1_RAW | MAX7219_FLG_DIGIT2_RAW | MAX7219_FLG_DIGIT3_RAW | MAX7219_FLG_DIGIT4_RAW | MAX7219_FLG_DIGIT5_RAW | MAX7219_FLG_DIGIT6_RAW | MAX7219_FLG_DIGIT7_RAW, MAX7219_CHIP_ALL); noShutdown(MAX7219_CHIP_ALL); for(int i = 0; i < _elements; i++) { if(_topology[i].elementType == MAX7219_MODE_NC) setScanLimit(_topology[i].digitFrom - 1, _topology[i].chipFrom); if(_topology[i].elementType == MAX7219_MODE_7SEGMENT) for(int j = 0; j < _topology[i].chipTo - _topology[i].chipFrom + 1; j++) { byte decodemask = 0; for(int k = (j == _topology[i].chipFrom ? _topology[i].digitFrom : 0); k <= (j == _topology[i].chipTo ? _topology[i].digitTo : 7); k++) decodemask |= MAX7219_FLG_DIGIT0_CODEB << k; writeRegister(MAX7219_REG_DECODEMODE, decodemask, j); } clearDisplay(i); } }