//This is the adjusted version of Janet's code for Seeed Ultrathin LED Matrix //Janet's original code can be found: https://github.com/jmardjuki/LEDMatrixTest/blob/master/test_ledMatrix.c #include #include #include #include #include #include #include #include "general.h" /*** GLOBAL VARIABLE ***/ /* GPIO PATH */ #define GPIO_PATH "/sys/class/gpio/" /* GPIO Pins Definition */ #define A_PIN 78 #define B_PIN 76 #define C_PIN 74 #define D_PIN 72 #define OE_PIN 70 #define RED_PIN 79 #define BLUE_PIN 77 #define GREEN_PIN 75 #define LATCH_PIN 73 #define CLK_PIN 71 #define S_IWRITE "S_IWUSR" /* TIMING */ #define DELAY_IN_US 5 /* LED Screen Values */ static int screen[32][16]; /* FILES HANDLER */ static int fileDesc_red; static int fileDesc_blue; static int fileDesc_green; static int fileDesc_clk; static int fileDesc_latch; static int fileDesc_oe; static int fileDesc_a; static int fileDesc_b; static int fileDesc_c; static int fileDesc_d; void sleep_usec(long usec) { struct timespec sleep_time; sleep_time.tv_sec = (usec / 1000000); sleep_time.tv_nsec = (usec % 1000000) * 1000; nanosleep(&sleep_time, NULL); } /** * exportAndOut * Export a pin and set the direction to output * @params * int pinNum: the pin number to be exported and set for output */ static void exportAndOut(int pinNum) { // Export the gpio pins FILE *gpioExP = fopen(GPIO_PATH "export", "w"); if ( gpioExP == NULL ){ printf("ERROR: Unable to open export file.\n"); exit(-1); } fprintf(gpioExP, "%d", pinNum); fclose(gpioExP); // Change the direction into out char fileNameBuffer[1024]; sprintf(fileNameBuffer, GPIO_PATH "gpio%d/direction", pinNum); FILE *gpioDirP = fopen(fileNameBuffer, "w"); fprintf(gpioDirP, "out"); fclose(gpioDirP); return; } /** * ledMatrix_setupPins * Setup the pins used by the led matrix, by exporting and set the direction to out */ static void ledMatrix_setupPins(void) { // !- Change GPIO number here // fileDesc_ = open("/sys/class/gpio/gpio/value", O_WRONLY, S_IWRITE); // Row Select exportAndOut(A_PIN); fileDesc_a = open("/sys/class/gpio/gpio78/value", O_WRONLY, S_IWRITE); exportAndOut(B_PIN); fileDesc_b = open("/sys/class/gpio/gpio76/value", O_WRONLY, S_IWRITE); exportAndOut(C_PIN); fileDesc_c = open("/sys/class/gpio/gpio74/value", O_WRONLY, S_IWRITE); exportAndOut(D_PIN); fileDesc_d = open("/sys/class/gpio/gpio72/value", O_WRONLY, S_IWRITE); // Led exportAndOut(RED_PIN); fileDesc_red = open("/sys/class/gpio/gpio79/value", O_WRONLY, S_IWRITE); exportAndOut(GREEN_PIN); fileDesc_green = open("/sys/class/gpio/gpio75/value", O_WRONLY, S_IWRITE); exportAndOut(BLUE_PIN); fileDesc_blue = open("/sys/class/gpio/gpio77/value", O_WRONLY, S_IWRITE); // Timing exportAndOut(CLK_PIN); fileDesc_clk = open("/sys/class/gpio/gpio71/value", O_WRONLY, S_IWRITE); exportAndOut(LATCH_PIN); fileDesc_latch = open("/sys/class/gpio/gpio73/value", O_WRONLY, S_IWRITE); exportAndOut(OE_PIN); fileDesc_oe = open("/sys/class/gpio/gpio70/value", O_WRONLY, S_IWRITE); return; } /** * ledMatrix_writeGpioValue * Write the gpio value to the pin selected * @params: * int pinNum: GPIO pin to write to * int value: Value to be written to the pin * */ static void ledMatrix_writeGpioValue(int pinNum, int value) { char fileNameBuffer[1024]; sprintf(fileNameBuffer, GPIO_PATH "gpio%d/value", pinNum); FILE *gpioValP = fopen(fileNameBuffer, "w"); fprintf(gpioValP, "%d", value); fclose(gpioValP); return; } /** * ledMatrix_clock * Generate the clock pins */ static void ledMatrix_clock(void) { // Bit-bang the clock gpio lseek(fileDesc_clk, 0, SEEK_SET); write(fileDesc_clk, "1", 1); lseek(fileDesc_clk, 0, SEEK_SET); write(fileDesc_clk, "0", 1); return; } /** * ledMatrix_latch * Generate the latch pins */ static void ledMatrix_latch(void) { lseek(fileDesc_latch, 0, SEEK_SET); write(fileDesc_latch, "1", 1); lseek(fileDesc_latch, 0, SEEK_SET); write(fileDesc_latch, "0", 1); return; } static void ledMatrix_threebitsFromInt(int* arr, int input) { arr[0] = input & 1; arr[1] = input & 2; arr[1] = arr[1] >> 1; arr[2] = input & 4; arr[2] = arr[2] >> 2; return; } static void ledMatrix_fourbitsFromInt(int* arr, int input) { arr[0] = input & 1; arr[1] = input & 2; arr[1] = arr[1] >> 1; arr[2] = input & 4; arr[2] = arr[2] >> 2; arr[3] = input & 8; arr[3] = arr[3] >> 3; return; } /** * ledMatrix_setRow * Set LED Matrix row * @params: * int rowNum: the rowNumber to be inputted to row pins */ static void ledMatrix_setRow(int rowNum) { // Convert rowNum single bits from int int arr[4] = {0, 0, 0, 0}; ledMatrix_fourbitsFromInt(arr, rowNum); // Write on the row pins char a_val[2]; sprintf(a_val, "%d", arr[0]); lseek(fileDesc_a, 0, SEEK_SET); write(fileDesc_a, a_val, 1); char b_val[2]; sprintf(b_val, "%d", arr[1]); lseek(fileDesc_b, 0, SEEK_SET); write(fileDesc_b, b_val, 1); char c_val[2]; sprintf(c_val, "%d", arr[2]); lseek(fileDesc_c, 0, SEEK_SET); write(fileDesc_c, c_val, 1); char d_val[2]; sprintf(d_val, "%d", arr[3]); lseek(fileDesc_d, 0, SEEK_SET); write(fileDesc_d, d_val, 1); return; } /** * ledMatrix_setColour * Set the colour of the LED * @params: * int colour: colour to be set */ static void ledMatrix_setColour(int colour) { int arr[3] = {0, 0, 0}; ledMatrix_threebitsFromInt(arr, colour); // Write on the row pins char red_val[2]; sprintf(red_val, "%d", arr[0]); lseek(fileDesc_red, 0, SEEK_SET); write(fileDesc_red, red_val, 1); char green_val[2]; sprintf(green_val, "%d", arr[1]); lseek(fileDesc_green, 0, SEEK_SET); write(fileDesc_green, green_val, 1); char blue_val[2]; sprintf(blue_val, "%d", arr[2]); lseek(fileDesc_blue, 0, SEEK_SET); write(fileDesc_blue, blue_val, 1); return; } /** * ledMatrix_refresh * Fill the LED Matrix with the respective pixel colour */ void ledMatrix_refresh(void) { for ( int rowNum = 0; rowNum < 16; rowNum++ ) { lseek(fileDesc_oe, 0, SEEK_SET); write(fileDesc_oe, "1", 1); ledMatrix_setRow(rowNum); for ( int colNum = 0; colNum < 32; colNum++) { ledMatrix_setColour(screen[colNum][rowNum]); ledMatrix_clock(); } ledMatrix_latch(); lseek(fileDesc_oe, 0, SEEK_SET); write(fileDesc_oe, "0", 1); sleep_usec(DELAY_IN_US); // Sleep for delay } return; } /** * ledMatrix_fillrectangle * Fill the LED Matrix screen with the pixel colour * @params: * int x1: start of the x pixel * int y1: start of the y pixel * int x2: end of the x pixel * int y2: end of the y pixel * int colour: colour of the matrix */ static void ledMatrix_fillRectangle(int x1, int y1, int x2, int y2, int colour) { for ( int x = x1; x < x2; x++) { for ( int y = y1; y < y2; y++) { screen[y][x] = colour; } } return; } /** * ledMatrix_setPixel * Set the pixel selected on LED MAtrix with the colour selected * @params: * int x: x-axis * int y: y-axis * int colour: colour selected */ static void ledMatrix_setPixel(int x, int y, int colour) { screen[y][x] = colour; return; } /** * ledMatrix_printBoard * Print the LED Matrix Board pattern */ static void ledMatrix_printBoard() { for ( int x = 0; x < 16 ; x++ ) { for ( int y = 0; y < 32; y++) { printf("%d ", screen[y][x]); } printf("\n"); } } /*** MAIN ***/ int main() { // Reset the screen memset(screen, 0, sizeof(screen)); // Setup pins ledMatrix_setupPins(); // Fill out the LED Matrix with V pattern for ( int i = 0; i < 16; i++ ) { ledMatrix_setPixel(i, i, 1); ledMatrix_setPixel(i, 32-1-i , 2); } printf("Starting the program\n"); printf("LED PATTERN:\n") ledMatrix_printBoard() // Keep refreshing the LED while(1) { ledMatrix_refresh(); } printf("Ending the programs\n"); return 0; }