Skip to content

Commit 97693c0

Browse files
committed
refactor: macros to simplify duplicates rgb led funcs
1 parent 8089204 commit 97693c0

File tree

2 files changed

+53
-125
lines changed

2 files changed

+53
-125
lines changed

pslab-core.X/helpers/light.c

Lines changed: 50 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,53 @@
44
#include "../registers/system/pin_manager.h"
55
#include "light.h"
66

7-
void LIGHT_RGB(uint8_t red, uint8_t green, uint8_t blue) {
8-
uint8_t data[] = {green, red, blue};
9-
uint8_t location;
10-
11-
RGB_LED_SetLow();
7+
#define SendZero(pin) do {\
8+
pin = 1;\
9+
__asm__ __volatile__("repeat #22");\
10+
Nop();\
11+
pin = 0;\
12+
__asm__ __volatile__("repeat #51");\
13+
Nop();\
14+
} while(0)
15+
16+
#define SendOne(pin) do {\
17+
pin = 1;\
18+
__asm__ __volatile__("repeat #45");\
19+
Nop();\
20+
pin = 0;\
21+
__asm__ __volatile__("repeat #38");\
22+
Nop();\
23+
} while(0)
24+
25+
#define SendLatch(pin) do {\
26+
pin = 0;\
27+
__asm__ volatile ("repeat #3264");\
28+
Nop();\
29+
} while(0)
30+
31+
#define RGBCommon(red, green, blue, pin) do {\
32+
uint8_t data[] = {green, red, blue};\
33+
uint8_t location;\
34+
\
35+
SendLatch(pin);\
36+
\
37+
for (location = 0; location < 3; location++) {\
38+
uint8_t bit;\
39+
bit = data[location];\
40+
uint8_t byte;\
41+
for (byte = 0; byte < 8; byte++) {\
42+
if (bit & 0x80) {\
43+
SendOne(pin);\
44+
} else {\
45+
SendZero(pin);\
46+
}\
47+
bit = bit << 1;\
48+
}\
49+
}\
50+
} while(0)
1251

13-
__asm__ volatile ("repeat #3264");
14-
Nop();
15-
16-
for (location = 0; location < 3; location++) {
17-
uint8_t bit;
18-
bit = data[location];
19-
uint8_t byte;
20-
for (byte = 0; byte < 8; byte++) {
21-
if (bit & 0x80) {
22-
RGB_LED_SetHigh();
23-
__asm__ __volatile__("repeat #45");
24-
Nop();
25-
RGB_LED_SetLow();
26-
__asm__ __volatile__("repeat #38");
27-
Nop();
28-
} else {
29-
RGB_LED_SetHigh();
30-
__asm__ __volatile__("repeat #22");
31-
Nop();
32-
RGB_LED_SetLow();
33-
__asm__ __volatile__("repeat #51");
34-
Nop();
35-
}
36-
bit = bit << 1;
37-
}
38-
}
52+
void LIGHT_RGB(uint8_t red, uint8_t green, uint8_t blue) {
53+
RGBCommon(red, green, blue, RGB_LED_Setter);
3954
}
4055

4156
response_t LIGHT_Onboard(void) {
@@ -51,37 +66,7 @@ response_t LIGHT_Onboard(void) {
5166
INTERRUPT_GlobalDisable();
5267

5368
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-
}
69+
RGBCommon(colors[i+1], colors[i], colors[i+2], RGB_LED_Setter);
8570
}
8671

8772
INTERRUPT_GlobalEnable();
@@ -102,37 +87,7 @@ response_t LIGHT_One(void) {
10287
INTERRUPT_GlobalDisable();
10388

10489
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-
}
90+
RGBCommon(colors[i+1], colors[i], colors[i+2], SQR1_Setter);
13691
}
13792

13893
INTERRUPT_GlobalEnable();
@@ -153,40 +108,10 @@ response_t LIGHT_Two(void) {
153108
INTERRUPT_GlobalDisable();
154109

155110
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-
}
111+
RGBCommon(colors[i+1], colors[i], colors[i+2], SQR2_Setter);
187112
}
188113

189114
INTERRUPT_GlobalEnable();
190115

191116
return SUCCESS;
192-
}
117+
}

pslab-core.X/registers/system/pin_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LED_SetDigitalInput() (_TRISB15 = 1)
1616
#define LED_SetDigitalOutput() (_TRISB15 = 0)
1717

18+
#define RGB_LED_Setter _LATB2
1819
#define RGB_LED_SetHigh() (_LATB2 = 1)
1920
#define RGB_LED_SetLow() (_LATB2 = 0)
2021
#define RGB_LED_Toggle() (_LATB2 ^= 1)
@@ -256,13 +257,15 @@
256257
/*******************************************************************************
257258
* Wave Generator
258259
******************************************************************************/
260+
#define SQR1_Setter _LATC6
259261
#define SQR1_SetHigh() (_LATC6 = 1)
260262
#define SQR1_SetLow() (_LATC6 = 0)
261263
#define SQR1_Toggle() (_LATC6 ^= 1)
262264
#define SQR1_GetValue() _RC6
263265
#define SQR1_SetDigitalInput() (_TRISC6 = 1)
264266
#define SQR1_SetDigitalOutput() (_TRISC6 = 0)
265267

268+
#define SQR2_Setter _LATC7
266269
#define SQR2_SetHigh() (_LATC7 = 1)
267270
#define SQR2_SetLow() (_LATC7 = 0)
268271
#define SQR2_Toggle() (_LATC7 ^= 1)

0 commit comments

Comments
 (0)