-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLP5018.c
124 lines (116 loc) · 3.47 KB
/
LP5018.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "LP5018.h"
#include "board.h"
#include "uart_term.h"
void LP5018_init(){
bool retVal = false;
I2C_Transaction i2cTransaction;
txBuffer[0] = 0x00;
txBuffer[1] = 0x40;
P6->DIR |= 0x08;
P6->OUT |= 0x08;
i2cTransaction.slaveAddress = 0x28;
i2cTransaction.writeCount = 2;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readCount = 0;
i2cTransaction.readBuf = rxBuffer;
UART_PRINT("Started transfering LP5018\r\n");
do {
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
UART_PRINT("%d\r\n", retVal);
} while (!retVal);
UART_PRINT("Finished initializing LP5018\r\n");
}
void LP5018_read(){
bool retVal = false;
I2C_Transaction i2cTransaction;
uint8_t readBuf[32] = {0};
txBuffer[0] = 0x00;
i2cTransaction.slaveAddress = 0x28;
i2cTransaction.writeCount = 1;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readCount = 30;
i2cTransaction.readBuf = readBuf;
UART_PRINT("Starting LP5018 read\r\n");
do {
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
} while (!retVal);
int i;
UART_PRINT("LP5018 Read: ");
for (i = 0; i < 32; i++)
{
UART_PRINT("%#x, ", readBuf[i]);
}
UART_PRINT("\r\n");
}
void LP5018_setColor(uint8_t led, uint8_t r, uint8_t g, uint8_t b){
bool retVal = false;
I2C_Transaction i2cTransaction;
txBuffer[0] = led + 0x0F;
txBuffer[1] = r;
txBuffer[2] = g;
txBuffer[3] = b;
i2cTransaction.slaveAddress = 0x28;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.writeCount = 4;
i2cTransaction.readCount = 0;
/* Re-try writing to slave till I2C_transfer returns true */
do {
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
} while(!retVal);
}
void LP5018_setAllColor(uint8_t r, uint8_t g, uint8_t b){
bool retVal = false;
I2C_Transaction i2cTransaction;
int i;
txBuffer[0] = 0x0F;
for(i = 0; i < 18;){
txBuffer[++i] = r;
txBuffer[++i] = g;
txBuffer[++i] = b;
}
i2cTransaction.slaveAddress = 0x28;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.writeCount = ++i;
i2cTransaction.readCount = 0;
/* Re-try writing to slave till I2C_transfer returns true */
do {
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
} while(!retVal);
}
void LP5018_setBrightness(uint8_t led, uint8_t brightness){
bool retVal = false;
I2C_Transaction i2cTransaction;
txBuffer[0] = led + 0x07;
txBuffer[1] = brightness;
i2cTransaction.slaveAddress = 0x28;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.writeCount = 2;
i2cTransaction.readCount = 0;
/* Re-try writing to slave till I2C_transfer returns true */
do {
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
} while(!retVal);
}
void LP5018_setAllBrightness(uint8_t brightness){
bool retVal = false;
I2C_Transaction i2cTransaction;
txBuffer[0] = 0x07;
txBuffer[1] = brightness;
txBuffer[2] = brightness;
txBuffer[3] = brightness;
txBuffer[4] = brightness;
txBuffer[5] = brightness;
txBuffer[6] = brightness;
i2cTransaction.slaveAddress = 0x28;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.writeCount = 7;
i2cTransaction.readCount = 0;
/* Re-try writing to slave till I2C_transfer returns true */
do {
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
} while(!retVal);
}