This is a driver for a project I made in school. It involves a Raspberry Pi connected to the MBED, which in turn is connected to a Ledcube. The MBED delivers data for the leds to the Ledcube continuously at a 100 Hz rate, using SPI. This data is read from an internal buffer. For animations, the Raspberry Pi just sends data when the current "image" has to change, using I2C.
SPIDriver.cpp
00001 #include "SPIDriver.h" 00002 #include "mbed.h" 00003 #include "rtos.h" 00004 00005 SPIDriver::SPIDriver(PinName pin1, PinName pin2, PinName pin3, PinName latchpin, PinName cspin, const int freq) : spi(pin1, pin2, pin3){ 00006 spi.format(8, 3); 00007 spi.frequency(freq); 00008 00009 latch = new DigitalOut(latchpin); 00010 cs = new DigitalOut(cspin); 00011 00012 *latch = 0; 00013 *cs = 1; 00014 } 00015 00016 SPIDriver::~SPIDriver(){ 00017 } 00018 00019 void SPIDriver::write(uint8_t ledcolor){ 00020 *cs = 0; 00021 spi.write(ledcolor); 00022 *cs = 1; 00023 } 00024 00025 void SPIDriver::reversedwrite(uint8_t ledcolor){ 00026 *cs = 0; 00027 spi.write(reverse_byte(ledcolor)); 00028 *cs = 1; 00029 } 00030 00031 void SPIDriver::pulseLatch(){ 00032 *latch = 1; 00033 wait(0.000001); 00034 *latch = 0; 00035 } 00036 00037 void SPIDriver::sendLayer(LedCube* ledc){ 00038 unsigned char valueled = ledc->getNextValue(); 00039 // printf("%d ", valueled); 00040 write(valueled); //eerst laag selecteren, SPI zendt normaalgezien MSB first 00041 for(int j = 0; j < 24; j++) { 00042 unsigned char valueled = ledc->getNextValue(); 00043 // printf("%d ", valueled); 00044 write(valueled); //ledwaarden zelf nog altijd apart, omdat ze mogelijk gespiegeld moeten doorgestuurd worden 00045 } //(LSB first) 00046 } 00047 00048 void SPIDriver::sendFrame(LedCube* ledc){ 00049 for(int i = 0; i < 8; i++) { 00050 sendLayer(ledc); 00051 wait(0.00025); //voor 100 Hz 00052 pulseLatch(); 00053 }//end outer for loop 00054 // printf("Frame gezonden!!\n\r"); 00055 } 00056 00057 void SPIDriver::sendTestFrame(LedCube* ledc){ 00058 for(int i = 0; i < 8; i++) { 00059 sendLayer(ledc); 00060 wait(0.00125); 00061 pulseLatch(); 00062 } 00063 } 00064 00065 void SPIDriver::sendTest(LedCube* ledc, int frames){ 00066 printf("Testing...\n\r"); 00067 for(int i = 0; i < frames; i++) { 00068 sendTestFrame(ledc); 00069 } 00070 } 00071 00072 void SPIDriver::testPhase(int xframes){ 00073 printf("Initiating Test phase\n\r"); 00074 00075 LedCube* testledcube1 = new LedCube(); 00076 // testledcube1->testMode(LedCube::firsthalfwhite); 00077 sendTest(testledcube1, xframes); 00078 00079 LedCube* testledcube2 = new LedCube(); 00080 // testledcube2->testMode(LedCube::secondhalfwhite); 00081 sendTest(testledcube2, xframes); 00082 00083 printf("Test phase terminated!\n\r"); 00084 } 00085 00086 uint8_t SPIDriver::reverse_byte(uint8_t byte) 00087 { 00088 return (__rbit(byte) >> 24) & 0xFF; // reverse a byte in a 32-bit value, and extract the byte 00089 }
Generated on Thu Aug 25 2022 12:14:09 by
1.7.2