Skip to content

Commit

Permalink
almost there
Browse files Browse the repository at this point in the history
  • Loading branch information
CoorFun committed Dec 29, 2018
1 parent 0f8047a commit 6addd5f
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 21 deletions.
50 changes: 29 additions & 21 deletions include/cAPA102.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>

#define BOFFSET 1
#define GOFFSET 2
#define ROFFSET 3
#define B_OFF_SET 1
#define G_OFF_SET 2
#define R_OFF_SET 3

#define BITRATE 32000000

/* Beware: you may need to change the formate here */
#define SPI_DEVICE "/dev/spidev%d.%d"

#define RETRY_TIMES 5
#define RETRY_GAP_SEC 10

typedef struct{
uint8_t red;
uint8_t green;
uint8_t blue;
}cAPA102_RGB;

typedef struct{
uint8_t number;
uint8_t spi_bus;
uint8_t spi_dev;
int fd_spi;
uint8_t *pixels;
uint8_t brightness;
uint32_t number;
uint8_t spi_bus;
uint8_t spi_dev;
int fd_spi;
uint8_t *pixels;
uint8_t brightness;
}cAPA102_LEDs;

/**
Expand All @@ -42,7 +45,7 @@ typedef struct{
* @returns boolean
*
*/
uint8_t cAPA102_Init(uint8_t num, uint8_t spi_bus, uint8_t spi_dev, uint8_t brightness);
int cAPA102_Init(uint32_t led_num, uint8_t spi_bus, uint8_t spi_dev, uint8_t brightness);

/**
* @brief Change the brightness and fresh
Expand All @@ -56,7 +59,7 @@ void cAPA102_Change_Brightness(uint8_t brightness);
*
* @return current brightness value (0-31)
*/
uint8_t cAPA102_Get_Brightness(void);
int cAPA102_Get_Brightness(void);

/**
* @brief Set color for a specific pixel
Expand All @@ -67,27 +70,32 @@ uint8_t cAPA102_Get_Brightness(void);
* @param[in] blue Intensity of blue colour (0-255)
*
*/
void cAPA102_Set_Pixel_RGB(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void cAPA102_Set_Pixel_RGB(uint32_t index, uint8_t red, uint8_t green, uint8_t blue);

/**
* @brief Get colour form a specific pixel
*
* @param[in] index Index of the target led (0-255)
* @param[out] red: Intensity of red colour (0-255)
* @param[out] green: Intensity of green colour (0-255)
* @param[out] blue: Intensity of blue colour (0-255)
*
* @return RGB_colour structure with rgb colour value
*/
cAPA102_RGB* cAPA102_Get_Pixel_RGB(uint8_t index);
void cAPA102_Get_Pixel_RGB(uint32_t index, uint8_t *red, uint8_t *green, uint8_t *blue);

/**
* @brief Set color for a specific pixel by using 4byte date
* @brief: Set color for a specific pixel by using 4byte date
*
* @param[in] index Index of the target led (0-255)
* @param[in] red Intensity of red colour (0-255)
* @param[in] green Intensity of green colour (0-255)
* @param[in] blue Intensity of blue colour (0-255)
* @param[in] index: Index of the target led (0-255)
* @param[in] red: Intensity of red colour (0-255)
* @param[in] green: Intensity of green colour (0-255)
* @param[in] blue: Intensity of blue colour (0-255)
*
* @example: set the 2nd pixel to Yellow (#FFFF00)
* cAPA102_Set_Pixel_4byte(2, 0xFFFF00);
*
*/
void cAPA102_Set_Pixel_4byte(uint8_t index, uint32_t colour);
void cAPA102_Set_Pixel_4byte(uint32_t index, uint32_t colour);

/**
* @brief Get colour form a specific pixel
Expand All @@ -96,7 +104,7 @@ void cAPA102_Set_Pixel_4byte(uint8_t index, uint32_t colour);
*
* @return 32 bits colour data
*/
uint32_t cAPA102_Get_Pixel_4byte(uint8_t index);
uint32_t cAPA102_Get_Pixel_4byte(uint32_t index);

/**
* @brief Clear all the pixels
Expand Down
131 changes: 131 additions & 0 deletions src/cAPA102.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "cAPA102.h"

static cAPA102_LEDs cAPA012_BUF = {0, 0, 0, -1, NULL, 31};

/**
* @brief: Try to open the SPI device by given times
*
* @param[in] retry_times
* @param[in] retry_gap_sec:
*
* @returns: \The file descriptor of SPI device or \-1 error
*
*/
static int cAPA102_Try_Open_SPI_Dev(uint8_t retry_times, uint8_t retry_gap_sec);

/**
* @brief Open the SPI device file
*
* @returns \The file descriptor of SPI device or \-1 error
*
*/
static int cAPA102_Open_SPI_Dev(void);

int cAPA102_Init(uint32_t led_num, uint8_t spi_bus, uint8_t spi_dev, uint8_t brightness){
cAPA012_BUF.number = led_num;
cAPA012_BUF.spi_bus = spi_bus;
cAPA012_BUF.spi_dev = spi_dev;
cAPA012_BUF.brightness = brightness;
cAPA012_BUF.pixels = (uint8_t *)malloc(cAPA012_BUF.number * 4);
cAPA012_BUF.fd_spi = cAPA102_Try_Open_SPI_Dev(RETRY_TIMES, RETRY_GAP_SEC);
cAPA102_Clear_All();
}

void cAPA102_Change_Brightness(uint8_t brightness){
cAPA012_BUF.brightness = brightness;
}

int cAPA102_Get_Brightness(void){
return cAPA012_BUF.brightness;
}

void cAPA102_Set_Pixel_RGB(uint32_t index, uint8_t red, uint8_t green, uint8_t blue){
if (index < cAPA012_BUF.number) {
uint8_t *ptr = &cAPA012_BUF.pixels[index * 4];
ptr[R_OFF_SET] = red;
ptr[G_OFF_SET] = green;
ptr[B_OFF_SET] = blue;
}
}


void cAPA102_Get_Pixel_RGB(uint32_t index, uint8_t *red, uint8_t *green, uint8_t *blue){
if (index < cAPA012_BUF.number) {
uint8_t *ptr = &cAPA012_BUF.pixels[index * 4];
red = ptr + R_OFF_SET; //ptr[R_OFF_SET];
green = ptr + G_OFF_SET; //ptr[G_OFF_SET];
blue = ptr + B_OFF_SET; //ptr[B_OFF_SET];
}
}

void cAPA102_Set_Pixel_4byte(uint32_t index, uint32_t colour){
uint8_t r, g, b, br;
r = colour >> 16;
g = colour >> 8;
b = colour;

cAPA102_Set_Pixel_RGB(index, r, g, b);

// if(index < cAPA012_BUF.numLEDs) {
// uint8_t *ptr = &cAPA012_BUF.pixels[index * 4];
// // ptr[ROFFSET] = (uint8_t)(r * br / 255);
// // ptr[GOFFSET] = (uint8_t)(g * br / 255);
// // ptr[BOFFSET] = (uint8_t)(b * br / 255);
// }
}

uint32_t cAPA102_Get_Pixel_4byte(uint32_t index){
uint8_t r, g, b;
uint32_t colour = 0;
cAPA102_Get_Pixel_RGB(index, &r, &g, &b);
r <<= 16;
g <<= 8;
colour = r | g | b;
return colour;
}

void cAPA102_Clear_All(void){

}

void cAPA102_Refresh(void){

}

void cAPA102_Close(void){
cAPA102_Clear_All();
if (cAPA012_BUF.fd_spi != -1)
close(cAPA012_BUF.fd_spi);
if (cAPA012_BUF.pixels)
free(cAPA012_BUF.pixels);
}

static int cAPA102_Try_Open_SPI_Dev(uint8_t retry_times, uint8_t retry_gap_sec){
int i,res;
i = 0;
do{
res = cAPA102_Open_SPI_Dev();
if (-1 != res)
return res;
i++;
if(i > retry_times)
break;
fprintf(stderr, "[Error] Failed to open SPI! Retry [%d] in %d seconds. \n", i, retry_gap_sec);
sleep(retry_gap_sec);
}while(res);
return -1;
}

static int cAPA102_Open_SPI_Dev(void){
char spi_file_buff[50];
int fd_temp;
sprintf(spi_file_buff, SPI_DEVICE, leds.spi_bus, leds.spi_dev);
fd_temp = open(spi_file_buff, O_RDWR);
if(-1 == fd_temp) {
fprintf(stderr, "[Error] Can't open %s (try 'sudo')", spi_file_buff);
return -1;
}
ioctl(fd_temp, SPI_IOC_WR_MODE, SPI_MODE_0 | SPI_NO_CS);
ioctl(fd_temp, SPI_IOC_WR_MAX_SPEED_HZ, BITRATE);
return fd_temp;
}

0 comments on commit 6addd5f

Please sign in to comment.