forked from armageddon421/blinkenpi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgblib.c
55 lines (38 loc) · 947 Bytes
/
rgblib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <string.h>
#include <math.h>
#include <wiringPiSPI.h>
#include "rgblib.h"
//output buffer
unsigned char output[LED_NUM*3];
int spi_init(){
if (wiringPiSPISetup (SPI_CHANNEL, 1000000) < 0){
printf("SPI Setup error\n");
return 1;
}
int i;
for(i=0;i<LED_NUM;i++){
led_set(i,0,0,0);
}
update();
return 0;
}
void led_set(int id, unsigned char r, unsigned char g, unsigned char b){
output[id*3 + 0] = (r^255) & 0b01111111;
output[id*3 + 1] = (g^255) & 0b01111111;
output[id*3 + 2] = (b^255) & 0b01111111;
}
void update(){
unsigned char temp[LED_NUM*3];
unsigned char latch[6] = {255,255,255,255,255,255};
memcpy(temp,output, LED_NUM*3);
wiringPiSPIDataRW (SPI_CHANNEL, temp, LED_NUM*3) ;
//usleep(100000);
wiringPiSPIDataRW (SPI_CHANNEL, latch, 6) ;
}