Skip to content

Commit

Permalink
lib is finished and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
CoorFun committed Jan 1, 2019
1 parent d4c6216 commit a9950a8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 71 deletions.
66 changes: 26 additions & 40 deletions include/cAPA102.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef __cAPA102_H__
#define __cAPA102_H__
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
Expand All @@ -19,67 +21,57 @@
#define RETRY_TIMES 5
#define RETRY_GAP_SEC 10

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

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

/**
* @brief Initialise a set of apa102 leds
* @brief: Initialise a set of apa102 LEDs
*
* @param[in] num Number of leds (0-255)
* @param[in] spi_bus SPI bus number (0-255)
* @param[in] spi_dev SPI device number (0-255)
* @param[in] brightness Initial brightness (0-31)
*
* @returns boolean
* @param[in] led_num: Number of leds (0-255)
* @param[in] spi_bus: SPI bus number (0-255)
* @param[in] spi_dev: SPI device number (0-255)
* @param[in] brightness: Global brightness (0-31)
*
* @returns: 0\ Success
* -1\ Error
*/
int cAPA102_Init(uint32_t led_num, uint8_t spi_bus, uint8_t spi_dev, uint8_t brightness);

/**
* @brief Change the brightness and fresh
* @brief: Change the global brightness and fresh
*
* @param[in] brightness Initial brightness
* @param[in] brightness: New brightness value
*/
void cAPA102_Change_Brightness(uint8_t brightness);

/**
* @brief Change the brightness and fresh
* @brief: Get the brightness
*
* @return current brightness value (0-31)
*/
int cAPA102_Get_Brightness(void);

/**
* @brief Set color for a specific pixel
*
* @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)
* @brief: Set color for a specific pixel by giving R, G and B value separately
*
* @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)
*/
void cAPA102_Set_Pixel_RGB(uint32_t index, uint8_t red, uint8_t green, uint8_t blue);

/**
* @brief Get colour form a specific pixel
* @brief: Get colour form a specific pixel for R, G and B separately
*
* @param[in] index Index of the target led (0-255)
* @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)
*
*/
void cAPA102_Get_Pixel_RGB(uint32_t index, uint8_t *red, uint8_t *green, uint8_t *blue);

Expand All @@ -91,37 +83,31 @@ void cAPA102_Get_Pixel_RGB(uint32_t index, uint8_t *red, uint8_t *green, uint8_t
* @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);
*
* @example: cAPA102_Get_Pixel_RGB(1, 0xFF0000) sets the 1st LED to red colour
*/
void cAPA102_Set_Pixel_4byte(uint32_t index, uint32_t colour);

/**
* @brief Get colour form a specific pixel
* @brief: Get colour form a specific pixel
*
* @param[in] index Index of the target led (0-255)
* @param[in] index: Index of the target led (0-255)
*
* @return 32 bits colour data
* @returns: 32 bits colour data
*/
uint32_t cAPA102_Get_Pixel_4byte(uint32_t index);

/**
* @brief Clear all the pixels
*
* @brief: Clear all the pixels
*/
void cAPA102_Clear_All(void);

/**
* @brief Refresh display (After modifing pixel colour or changing brightness)
*
* @brief: Refresh display (After modifing pixel colour)
*/
void cAPA102_Refresh(void);

/**
* @brief Close SPI file, release memory
*
* @brief: Close SPI file, release memory
*/
void cAPA102_Close(void);

#endif
99 changes: 68 additions & 31 deletions src/cAPA102.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
#include "cAPA102.h"

static cAPA102_LEDs cAPA012_BUF = {0, 0, 0, -1, NULL, 31};
static cAPA102_LEDs cAPA012_BUF = {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
*
* @returns: int\ 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);
static int cAPA102_Try_Open_SPI_Dev(uint8_t retry_times, uint8_t retry_gap_sec, uint8_t spi_bus, uint8_t spi_dev);

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

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;
if (brightness > 31)
cAPA012_BUF.brightness = 0xFF;
else
cAPA012_BUF.brightness = 0xE0 | (0x1F & 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);
cAPA012_BUF.fd_spi = cAPA102_Try_Open_SPI_Dev(RETRY_TIMES, RETRY_GAP_SEC, spi_bus, spi_dev);
if (cAPA012_BUF.fd_spi)
return -1;
cAPA102_Clear_All();
return 0;
}

void cAPA102_Change_Brightness(uint8_t brightness){
cAPA012_BUF.brightness = brightness;
if (brightness > 31)
cAPA012_BUF.brightness = 0xFF;
else
cAPA012_BUF.brightness = 0xE0 | (0x1F & brightness);
cAPA102_Refresh();
}

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

void cAPA102_Set_Pixel_RGB(uint32_t index, uint8_t red, uint8_t green, uint8_t blue){
Expand All @@ -48,30 +56,21 @@ void cAPA102_Set_Pixel_RGB(uint32_t index, uint8_t red, uint8_t green, uint8_t b
}
}


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];
red = ptr + R_OFF_SET;
green = ptr + G_OFF_SET;
blue = ptr + B_OFF_SET;
}
}

void cAPA102_Set_Pixel_4byte(uint32_t index, uint32_t colour){
uint8_t r, g, b, br;
uint8_t r, g, b;
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){
Expand All @@ -85,11 +84,49 @@ uint32_t cAPA102_Get_Pixel_4byte(uint32_t index){
}

void cAPA102_Clear_All(void){

uint8_t *ptr;
uint32_t i;
for(ptr = cAPA012_BUF.pixels, i=0; i<cAPA012_BUF.number; i++, ptr += 4) {
ptr[1] = 0x00;
ptr[2] = 0x00;
ptr[3] = 0x00;
}
cAPA102_Refresh();
}

void cAPA102_Refresh(void){
int ret;
uint32_t i;
uint32_t buf_len = 4 + 4 * cAPA012_BUF.number + (cAPA012_BUF.number + 15) / 16 + 1;
uint8_t *ptr, *qtr;
uint8_t *tx = (uint8_t *)malloc(buf_len);

struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.len = buf_len,
.speed_hz = BITRATE,
.bits_per_word = 8,
};

for (i = 0; i < 4; i++)
*(tx + i) = 0x00;
qtr = tx + 4;

for(ptr = cAPA012_BUF.pixels, i=0; i<cAPA012_BUF.number; i++, ptr += 4, qtr += 4) {
qtr[0] = cAPA012_BUF.brightness;
qtr[1] = ptr[1];
qtr[2] = ptr[2];
qtr[3] = ptr[3];
}

for (i = cAPA012_BUF.number * 4 + 4; i < buf_len; i++)
*(tx + i) = 0x01;

ret = ioctl(cAPA012_BUF.fd_spi, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
fprintf(stdout, "[Error] can't send spi message\n");

free(tx);
}

void cAPA102_Close(void){
Expand All @@ -100,11 +137,11 @@ void cAPA102_Close(void){
free(cAPA012_BUF.pixels);
}

static int cAPA102_Try_Open_SPI_Dev(uint8_t retry_times, uint8_t retry_gap_sec){
static int cAPA102_Try_Open_SPI_Dev(uint8_t retry_times, uint8_t retry_gap_sec, uint8_t spi_bus, uint8_t spi_dev){
int i,res;
i = 0;
do{
res = cAPA102_Open_SPI_Dev();
res = cAPA102_Open_SPI_Dev(spi_bus, spi_dev);
if (-1 != res)
return res;
i++;
Expand All @@ -116,10 +153,10 @@ static int cAPA102_Try_Open_SPI_Dev(uint8_t retry_times, uint8_t retry_gap_sec){
return -1;
}

static int cAPA102_Open_SPI_Dev(void){
static int cAPA102_Open_SPI_Dev(uint8_t spi_bus, uint8_t spi_dev){
char spi_file_buff[50];
int fd_temp;
sprintf(spi_file_buff, SPI_DEVICE, leds.spi_bus, leds.spi_dev);
sprintf(spi_file_buff, SPI_DEVICE, spi_bus, 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);
Expand Down

0 comments on commit a9950a8

Please sign in to comment.