/** University of Washington
* ECE/CSE 474, May 1 2024
*
* Kinner Parikh
* Lynden Huang
*
* Lab 2 - Digital I/O and Timers
* Task 4.1 - Joystick LED Matrix
*
* Acknowledgements: Ishaan Bhimani
*/
#define OP_DECODEMODE 8
#define OP_SCANLIMIT 10
#define OP_SHUTDOWN 11
#define OP_DISPLAYTEST 14
#define OP_INTENSITY 10
// Joystick input pins
#define X A0
#define Y A1
// LED Matrix are connected to these pins
#define DIN 30
#define CS 31
#define CLK 32
/**
* Function Prototype - spiTransfer
* Parameters:
* - volatile byte row - row in LED matrix
* - volatile byte data - bit representation of LEDs in a given row; 1 indicates ON, 0 indicates OFF
* Returns: None
*
* Description - Transfers 1 SPI command to LED Matrix for given row
*
* Authors - Ishaan Bhimani
*/
void spiTransfer(volatile byte row, volatile byte data);
byte spidata[2]; //spi shift register uses 16 bits, 8 for ctrl and 8 for data
/**
* Function - setup
* Parameters:
* - None
* Returns: None
*
* Description - This function is called once at the beginning of the program.
* It sets up the pin mode for the DIN, CS, and CLK pins as output.
*
* Authors - Ishaan Bhimani
*/
void setup(){
//must do this setup
pinMode(DIN, OUTPUT);
pinMode(CS, OUTPUT);
pinMode(CLK, OUTPUT);
digitalWrite(CS, HIGH);
spiTransfer(OP_DISPLAYTEST,0);
spiTransfer(OP_SCANLIMIT,7);
spiTransfer(OP_DECODEMODE,0);
spiTransfer(OP_SHUTDOWN,1);
}
/**
* Function - loop
* Parameters:
* - None
* Returns: None
*
* Description - This function is called repeatedly in a loop. It reads the X and Y values
* from the joystick and sets the corresponding LED in the LED matrix.
*
* Authors - Kinner Parikh, Lynden Huang
*/
void loop(){
int xValue = analogRead(X);
int yValue = analogRead(Y);
int row = 7 - (yValue / 128);
int col = 7 - (xValue / 128);
unsigned int output;
for (int r = 0; r < 8; r++){ //for each row, set the LEDs
output = 0x00;
if (r == row) {
output |= (1 << col);
}
spiTransfer(r, output);
}
}
void spiTransfer(volatile byte opcode, volatile byte data){
int offset = 0; //only 1 device
int maxbytes = 2; //16 bits per SPI command
for(int i = 0; i < maxbytes; i++) { //zero out spi data
spidata[i] = (byte)0;
}
//load in spi data
spidata[offset+1] = opcode+1;
spidata[offset] = data;
digitalWrite(CS, LOW); //
for(int i=maxbytes;i>0;i--)
shiftOut(DIN,CLK,MSBFIRST,spidata[i-1]); //shift out 1 byte of data starting with leftmost bit
digitalWrite(CS,HIGH);
}