Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SPIDriver/SPIDriver.cpp
- Committer:
- sNICKer1103
- Date:
- 2014-05-02
- Revision:
- 3:da30c350c339
- Parent:
- 2:f2700008c9d9
- Child:
- 4:a091b8f8216d
File content as of revision 3:da30c350c339:
#include "SPIDriver.h"
#include "mbed.h"
SPIDriver::SPIDriver(PinName pin1, PinName pin2, PinName pin3, PinName latchpin, PinName cspin, const int freq) : spi(pin1, pin2, pin3){
spi.format(8, 3);
spi.frequency(freq);
latch = new DigitalOut(latchpin);
cs = new DigitalOut(cspin);
*latch = 0;
*cs = 1;
}
SPIDriver::~SPIDriver(){
}
void SPIDriver::write(uint8_t ledcolor){
*cs = 0;
spi.write(ledcolor);
*cs = 1;
}
void SPIDriver::reversedwrite(uint8_t ledcolor){
*cs = 0;
spi.write(reverse_byte(ledcolor));
*cs = 1;
}
void SPIDriver::pulseLatch(){
*latch = 1;
*latch = 0;
}
void SPIDriver::sendFrame(LedCube* ledc){
for(int i = 0; i < 8; i++) { //eerst laag selecteren, SPI zendt normaalgezien MSB first
write(ledc->getNextValue());
for(int j = 0; j < 24; j++) {
write(ledc->getNextValue()); //ledwaarden zelf nog altijd apart, omdat ze mogelijk gespiegeld moeten doorgestuurd worden
} //(LSB first)
wait(0.00125); //voor 100 Hz
pulseLatch();
}//end outer for loop
}
void SPIDriver::sendTest(LedCube* ledc, int frames){
printf("Testing...\n\r");
for(int i = 0; i < frames; i++) {
sendFrame(ledc);
}
}
void SPIDriver::testPhase(int xframes){
printf("Initiating Test phase\n\r");
LedCube* testledcube1 = new LedCube();
LedCube* testledcube2 = new LedCube();
testledcube1->testMode(LedCube::firsthalfwhite);
testledcube2->testMode(LedCube::secondhalfwhite);
sendTest(testledcube1, xframes);
sendTest(testledcube2, xframes);
printf("Test phase terminated!\n\r");
}
void SPIDriver::stream(LedCube* ledc){
printf("Initiating stream from following matrices\n\r");
ledc->printAll();
printf("Streaming...\n\r");
while(true){
sendFrame(ledc);
}
}
uint8_t SPIDriver::reverse_byte(uint8_t byte)
{
return (__rbit(byte) >> 24) & 0xFF; // reverse a byte in a 32-bit value, and extract the byte
}