Benjamin Ramberg Møklegård / ShiftOut Featured
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ShiftOut.cpp Source File

ShiftOut.cpp

00001 #include "mbed.h"
00002 #include "ShiftOut.h"
00003 
00004 
00005 #define SET_LATCH() (LATCH = 0)
00006 #define RESET_LATCH() (LATCH = 1)
00007 
00008 #define ENABLE_RESET() (RESET = 0)
00009 #define DISABLE_RESET() (RESET = 1)
00010 
00011 static char stateArr[8*REGISTER_CNT] = {0};
00012 static bool hasChanged = false;
00013 
00014 ShiftOut::ShiftOut(PinName ser, PinName srclk, PinName rclk, 
00015                    PinName oe, PinName reset) : DSERIAL(ser), SRCLK(srclk), RCLK(rclk), LATCH(oe), RESET(reset)
00016 {
00017     writeByte(0x00); // Reset the values of the registers to 0  
00018     if(RESET != NC){  
00019     DISABLE_RESET();
00020     }
00021 }
00022 
00023 //Pulses the register
00024 void ShiftOut::updateRegister(){
00025     SRCLK = 1;
00026     wait_us(2);
00027     SRCLK = 0;    
00028 }
00029 //Updates the output register
00030 void ShiftOut::updateOutput(){
00031     RCLK = 1;
00032     wait_us(2);
00033     RCLK = 0;    
00034 }
00035 //Writes a byte to the shift register
00036 void ShiftOut::writeByte(unsigned char byte){
00037     hasChanged = true;
00038     for(int i = 0; i<8*REGISTER_CNT; i++){
00039         DSERIAL = (byte & 0x01<<i)>>i;
00040         updateRegister();    
00041     } 
00042     updateOutput();   
00043 }
00044 //Writes a bit to the shift register
00045 void ShiftOut::writeBit(unsigned char bit){
00046     DSERIAL = bit & 0x01;
00047     updateRegister();
00048     updateOutput();
00049     } 
00050 //Writes multiple bits from an array to create an animation
00051 void ShiftOut::animate(int arr[][8], int lines, int delay_ms){
00052     hasChanged = true;
00053     for(int i = 0; i < lines; i++){
00054         for(int j = 0; j < 8; j++){
00055            writeBit(arr[i][j]); 
00056         } 
00057         wait_ms(delay_ms);   
00058     }
00059 }
00060 
00061 void ShiftOut::animationExample(){
00062     hasChanged = true;
00063     int strobe[][8]= {{1,0,0,0,0,0,0,0},
00064                      {0,1,0,0,0,0,0,0},
00065                      {0,0,1,0,0,0,0,0},
00066                      {0,0,0,1,0,0,0,0},
00067                      {0,0,0,0,1,0,0,0},
00068                      {0,0,0,0,0,1,0,0},
00069                      {0,0,0,0,0,0,1,0},
00070                      {0,0,0,0,0,0,0,1}};
00071                      
00072     int nightrider[18][8]= {{1,0,0,0,0,0,0,0},
00073                            {1,1,0,0,0,0,0,0},
00074                            {1,1,1,0,0,0,0,0},
00075                            {0,1,1,1,0,0,0,0},
00076                            {0,0,1,1,1,0,0,0},
00077                            {0,0,0,1,1,1,0,0},
00078                            {0,0,0,0,1,1,1,0},
00079                            {0,0,0,0,0,1,1,1},
00080                            {0,0,0,0,0,0,1,1},
00081                            {0,0,0,0,0,0,0,1},
00082                            {0,0,0,0,0,0,1,1},
00083                            {0,0,0,0,0,1,1,1},
00084                            {0,0,0,0,1,1,1,0},
00085                            {0,0,0,1,1,1,0,0},
00086                            {0,0,1,1,1,0,0,0},
00087                            {0,1,1,1,0,0,0,0},
00088                            {1,1,1,0,0,0,0,0},
00089                            {1,1,0,0,0,0,0,0}};
00090                         
00091         animate(nightrider, 18, 50);
00092         wait(1);
00093         animate(strobe, 8, 200);
00094     }
00095     
00096 void ShiftOut::writeBitAtPos(unsigned char pin, bool state){
00097     if(hasChanged){
00098         clearStateArray();
00099         hasChanged = false;
00100     } 
00101     if(pin < 8*REGISTER_CNT){
00102         stateArr[pin] = state;        
00103     }
00104     writeArray(stateArr);
00105 }
00106 
00107 void ShiftOut::writeArray(char arr[8*REGISTER_CNT]){
00108     for(int i = (8*REGISTER_CNT)-1; i >= 0; i--) {
00109         writeBit(arr[i]);
00110     }       
00111     
00112 }
00113 
00114 void ShiftOut::clearStateArray(){
00115     for(int i = 0; i < 8*REGISTER_CNT; i++){
00116         stateArr[i] = 0;
00117         }
00118     }