Skip to content

Commit 8089204

Browse files
committed
feat: rgb led functions
1 parent 21d5a67 commit 8089204

File tree

5 files changed

+240
-28
lines changed

5 files changed

+240
-28
lines changed

pslab-core.X/bus/i2c/i2c.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ response_t I2C_CommandEnableSMBus(void) {
800800
I2C2CONbits.SMEN = 1;
801801
// Enables I2C2 module and configure SDA2 and SCL2 as serial port pins
802802
I2C2CONbits.I2CEN = 1;
803+
return SUCCESS;
803804
}
804805

805806
response_t I2C_CommandDisableSMBus(void) {

pslab-core.X/commands.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "bus/i2c/i2c.h"
33
#include "helpers/buffer.h"
44
#include "helpers/device.h"
5+
#include "helpers/light.h"
56
#include "helpers/rtc.h"
67
#include "instruments/multimeter.h"
78
#include "instruments/oscilloscope.h"
@@ -238,20 +239,20 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
238239
Undefined, Undefined, Undefined, Undefined,
239240
},
240241
{ // 11 COMMON
241-
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
242-
Undefined, Unimplemented, MULTIMETER_GetCapacitance, Unimplemented,
243-
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
244-
Unimplemented, DEVICE_GetVersion, Undefined, Undefined,
245-
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
246-
BUFFER_Retrieve, Unimplemented, Unimplemented, Unimplemented,
247-
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
248-
Removed, Removed, DEVICE_ReadRegisterData, DEVICE_WriteRegisterData,
249-
// 16 GET_CAP_RANGE 17 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
250-
Unimplemented, Unimplemented, Unimplemented, DEVICE_Reset,
251-
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
252-
Unimplemented, Undefined, Unimplemented, Unimplemented,
253-
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
254-
Unimplemented, SENSORS_StartCounter, SENSORS_GetCounter, Unimplemented,
242+
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
243+
Undefined, Unimplemented, MULTIMETER_GetCapacitance, Unimplemented,
244+
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
245+
Unimplemented, DEVICE_GetVersion, Undefined, Undefined,
246+
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
247+
BUFFER_Retrieve, Unimplemented, Unimplemented, LIGHT_Onboard,
248+
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
249+
Removed, Removed, DEVICE_ReadRegisterData, DEVICE_WriteRegisterData,
250+
// 16 GET_CAP_RANGE 17 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
251+
Unimplemented, LIGHT_One, Removed, DEVICE_Reset,
252+
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
253+
Unimplemented, Undefined, LIGHT_Two, Unimplemented,
254+
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
255+
Unimplemented, SENSORS_StartCounter, SENSORS_GetCounter, Unimplemented,
255256
},
256257
{ // 12 PASSTHROUGH
257258
// 0 1 2 3

pslab-core.X/commands.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern "C" {
1717
* This is used to set the dimensions of the command table, and to sanitize
1818
* received commands.
1919
*/
20-
#define NUM_PRIMARY_CMDS 12
20+
#define NUM_PRIMARY_CMDS 13
2121
#define NUM_FLASH_CMDS 4
2222
#define NUM_ADC_CMDS 23
2323
#define NUM_SPI_CMDS 7

pslab-core.X/helpers/light.c

Lines changed: 159 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
#include <xc.h>
2+
#include "../bus/uart/uart1.h"
3+
#include "../registers/system/interrupt_manager.h"
24
#include "../registers/system/pin_manager.h"
5+
#include "light.h"
36

47
void LIGHT_RGB(uint8_t red, uint8_t green, uint8_t blue) {
5-
68
uint8_t data[] = {green, red, blue};
7-
uint8_t location = 0;
8-
uint8_t byte = 0;
9-
uint8_t bit = 0;
9+
uint8_t location;
1010

1111
RGB_LED_SetLow();
1212

1313
__asm__ volatile ("repeat #3264");
1414
Nop();
1515

1616
for (location = 0; location < 3; location++) {
17+
uint8_t bit;
1718
bit = data[location];
19+
uint8_t byte;
1820
for (byte = 0; byte < 8; byte++) {
1921
if (bit & 0x80) {
2022
RGB_LED_SetHigh();
@@ -35,3 +37,156 @@ void LIGHT_RGB(uint8_t red, uint8_t green, uint8_t blue) {
3537
}
3638
}
3739
}
40+
41+
response_t LIGHT_Onboard(void) {
42+
43+
uint8_t count = UART1_Read();
44+
uint8_t colors[count];
45+
46+
uint8_t i;
47+
for (i = 0; i < count; i++) {
48+
colors[i] = UART1_Read();
49+
}
50+
51+
INTERRUPT_GlobalDisable();
52+
53+
for (i = 0; i < count; i = i + 3) {
54+
uint8_t data[] = {colors[i+1], colors[i], colors[i+2]};
55+
uint8_t location;
56+
57+
RGB_LED_SetLow();
58+
59+
__asm__ volatile ("repeat #3264");
60+
Nop();
61+
62+
for (location = 0; location < 3; location++) {
63+
uint8_t bit;
64+
bit = data[location];
65+
uint8_t byte;
66+
for (byte = 0; byte < 8; byte++) {
67+
if (bit & 0x80) {
68+
RGB_LED_SetHigh();
69+
__asm__ __volatile__("repeat #45");
70+
Nop();
71+
RGB_LED_SetLow();
72+
__asm__ __volatile__("repeat #38");
73+
Nop();
74+
} else {
75+
RGB_LED_SetHigh();
76+
__asm__ __volatile__("repeat #22");
77+
Nop();
78+
RGB_LED_SetLow();
79+
__asm__ __volatile__("repeat #51");
80+
Nop();
81+
}
82+
bit = bit << 1;
83+
}
84+
}
85+
}
86+
87+
INTERRUPT_GlobalEnable();
88+
89+
return SUCCESS;
90+
}
91+
92+
response_t LIGHT_One(void) {
93+
94+
uint8_t count = UART1_Read();
95+
uint8_t colors[count];
96+
97+
uint8_t i;
98+
for (i = 0; i < count; i++) {
99+
colors[i] = UART1_Read();
100+
}
101+
102+
INTERRUPT_GlobalDisable();
103+
104+
for (i = 0; i < count; i = i + 3) {
105+
uint8_t data[] = {colors[i+1], colors[i], colors[i+2]};
106+
uint8_t location;
107+
108+
SQR1_SetLow();
109+
110+
__asm__ volatile ("repeat #3264");
111+
Nop();
112+
113+
for (location = 0; location < 3; location++) {
114+
uint8_t bit;
115+
bit = data[location];
116+
uint8_t byte;
117+
for (byte = 0; byte < 8; byte++) {
118+
if (bit & 0x80) {
119+
SQR1_SetHigh();
120+
__asm__ __volatile__("repeat #45");
121+
Nop();
122+
SQR1_SetLow();
123+
__asm__ __volatile__("repeat #38");
124+
Nop();
125+
} else {
126+
SQR1_SetHigh();
127+
__asm__ __volatile__("repeat #22");
128+
Nop();
129+
SQR1_SetLow();
130+
__asm__ __volatile__("repeat #51");
131+
Nop();
132+
}
133+
bit = bit << 1;
134+
}
135+
}
136+
}
137+
138+
INTERRUPT_GlobalEnable();
139+
140+
return SUCCESS;
141+
}
142+
143+
response_t LIGHT_Two(void) {
144+
145+
uint8_t count = UART1_Read();
146+
uint8_t colors[count];
147+
148+
uint8_t i;
149+
for (i = 0; i < count; i++) {
150+
colors[i] = UART1_Read();
151+
}
152+
153+
INTERRUPT_GlobalDisable();
154+
155+
for (i = 0; i < count; i = i + 3) {
156+
uint8_t data[] = {colors[i+1], colors[i], colors[i+2]};
157+
uint8_t location;
158+
159+
SQR2_SetLow();
160+
161+
__asm__ volatile ("repeat #3264");
162+
Nop();
163+
164+
for (location = 0; location < 3; location++) {
165+
uint8_t bit;
166+
bit = data[location];
167+
uint8_t byte;
168+
for (byte = 0; byte < 8; byte++) {
169+
if (bit & 0x80) {
170+
SQR2_SetHigh();
171+
__asm__ __volatile__("repeat #45");
172+
Nop();
173+
SQR2_SetLow();
174+
__asm__ __volatile__("repeat #38");
175+
Nop();
176+
} else {
177+
SQR2_SetHigh();
178+
__asm__ __volatile__("repeat #22");
179+
Nop();
180+
SQR2_SetLow();
181+
__asm__ __volatile__("repeat #51");
182+
Nop();
183+
}
184+
bit = bit << 1;
185+
}
186+
}
187+
}
188+
189+
INTERRUPT_GlobalEnable();
190+
191+
return SUCCESS;
192+
}

pslab-core.X/helpers/light.h

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,77 @@
44
#ifdef __cplusplus
55
extern "C" {
66
#endif
7-
7+
88
/**
9-
* @Param
10-
* red, green, blue in that order
11-
*
12-
* @Returns
13-
* none
9+
* @brief Control the color of RGB LED
1410
*
15-
* @Description
11+
* @description
1612
* Lights up the RGB LED. This method takes around 61.5 us to finish.
1713
*
18-
* @Example
14+
* @param
15+
* red: red color level (0-255)
16+
* green: green color level (0-255)
17+
* blue: blue color level (0-255)
18+
*
19+
* @example
1920
* Light_RGB(255, 0, 0); will light up red color
2021
*/
2122
void LIGHT_RGB(uint8_t red, uint8_t green, uint8_t blue);
22-
23+
24+
/**
25+
* @brief Controls the on-board RGB LED
26+
*
27+
* @description
28+
* This routine takes two types of arguments over serial.
29+
* 1. (uint8) count
30+
* Number of color levels. This always has to be 3. It is implemented this
31+
* way to have backward compatibility with old firmware and python backend.
32+
* 2. (uint8 [count]) color levels
33+
* This a sequence of bytes with the amount determined by count variable.
34+
* In this case it will be three bytes in series. These will define red, green
35+
* and blue colors of the LED.
36+
*
37+
* It will not return anything over serial. An acknowledgment will be passed.
38+
*
39+
* @return none
40+
*/
41+
response_t LIGHT_Onboard(void);
42+
43+
/**
44+
* @brief Controls an RGB LED stripe from SQR1 pin
45+
*
46+
* @description
47+
* This routine takes two types of arguments over serial.
48+
* 1. (uint8) count
49+
* Number of color levels. This has to be a multiple of 3 since each LED
50+
* has three colors to control.
51+
* 2. (uint8 [count]) color levels
52+
* This a sequence of bytes with the amount determined by count variable.
53+
* These will define red, green and blue colors of each of the LEDs.
54+
*
55+
* It will not return anything over serial. An acknowledgment will be passed.
56+
*
57+
* @return none
58+
*/
59+
response_t LIGHT_One(void);
60+
61+
/**
62+
* @brief Controls an RGB LED stripe from SQR2 pin
63+
*
64+
* @description
65+
* This routine takes two types of arguments over serial.
66+
* 1. (uint8) count
67+
* Number of color levels. This has to be a multiple of 3 since each LED
68+
* has three colors to control.
69+
* 2. (uint8 [count]) color levels
70+
* This a sequence of bytes with the amount determined by count variable.
71+
* These will define red, green and blue colors of each of the LEDs.
72+
*
73+
* It will not return anything over serial. An acknowledgment will be passed.
74+
*
75+
* @return none
76+
*/
77+
response_t LIGHT_Two(void);
2378

2479
#ifdef __cplusplus
2580
}

0 commit comments

Comments
 (0)