-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeds_RGB.c
More file actions
177 lines (146 loc) · 6.9 KB
/
Leds_RGB.c
File metadata and controls
177 lines (146 loc) · 6.9 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "main.h"
#include "ws2812.pio.h"
uint8_t WS2812_Right_Strip_SM = 0;
uint8_t WS2812_Right_Strip_DMA = 0;
uint8_t WS2812_Left_Strip_SM = 0;
uint8_t WS2812_Left_Strip_DMA = 0;
uint8_t WS2812_Matrix_SM = 0;
uint8_t WS2812_Matrix_DMA = 0;
uint32_t RGB_Send_Timer = 0;
uint8_t RGB_Send_State = 0;
uint32_t Right_Strip_GRB_Pix[RGB_STRIP_LENGTH];
uint32_t Left_Strip_GRB_Pix[RGB_STRIP_LENGTH];
uint32_t Matrix_Strip_GRB_Pix[RGB_MATRIX_H*RGB_MATRIX_L];
uint32_t Matrix_RGB_Pix[RGB_MATRIX_H][RGB_MATRIX_L];
// matrice : (TBC)
// - ligne 0 en haut, 7 en bas
// - colonne 0 a gauche, 31 a droite
void RGB_Init(void)
{
uint8_t Dma_Req_from_pio_sm;
dma_channel_config Dma_Conf;
// on charge une seule fois le programme, toutes les SM doivent pouvoir tourner sur le meme
uint32_t offset = pio_add_program(pio0, &ws2812_program);
// Prog PIO
WS2812_Right_Strip_SM = pio_claim_unused_sm(pio0, true);
ws2812_program_init(pio0, WS2812_Right_Strip_SM, offset, RIGHT_RGB_STRIP_PIN, 800000, false);
// Prog DMA
WS2812_Right_Strip_DMA = dma_claim_unused_channel(true);
Dma_Conf = dma_channel_get_default_config(WS2812_Right_Strip_DMA);
channel_config_set_transfer_data_size(&Dma_Conf, DMA_SIZE_32);
channel_config_set_read_increment(&Dma_Conf, true);
channel_config_set_write_increment(&Dma_Conf, false);
// voir "hardware/regs/dreq.h"
// mais les TX du pio0, et les data_request DMA c'est la meme chose
Dma_Req_from_pio_sm = WS2812_Right_Strip_SM;
channel_config_set_dreq(&Dma_Conf, Dma_Req_from_pio_sm);
dma_channel_configure(
WS2812_Right_Strip_DMA,// Channel to be configured
&Dma_Conf, // The configuration we just created
&(pio0->txf[WS2812_Right_Strip_SM]), // The initial write address
&Right_Strip_GRB_Pix[0], // The initial read address
RGB_STRIP_LENGTH, // Number of transfers; in this case each is 1 byte.
false // Start immediately.
);
// Prog PIO
WS2812_Left_Strip_SM = pio_claim_unused_sm(pio0, true);
ws2812_program_init(pio0, WS2812_Left_Strip_SM, offset, LEFT_RGB_STRIP_PIN, 800000, false);
// Prog DMA
WS2812_Left_Strip_DMA = dma_claim_unused_channel(true);
Dma_Conf = dma_channel_get_default_config(WS2812_Left_Strip_DMA);
channel_config_set_transfer_data_size(&Dma_Conf, DMA_SIZE_32);
channel_config_set_read_increment(&Dma_Conf, true);
channel_config_set_write_increment(&Dma_Conf, false);
// voir "hardware/regs/dreq.h"
// mais les TX du pio0, et les data_request DMA c'est la meme chose
Dma_Req_from_pio_sm = WS2812_Left_Strip_SM;
channel_config_set_dreq(&Dma_Conf, Dma_Req_from_pio_sm);
dma_channel_configure(
WS2812_Left_Strip_DMA,// Channel to be configured
&Dma_Conf, // The configuration we just created
&(pio0->txf[WS2812_Left_Strip_SM]), // The initial write address
&Left_Strip_GRB_Pix[0], // The initial read address
RGB_STRIP_LENGTH, // Number of transfers; in this case each is 1 byte.
false // Start immediately.
);
// Prog PIO
WS2812_Matrix_SM = pio_claim_unused_sm(pio0, true);
ws2812_program_init(pio0, WS2812_Matrix_SM, offset, MATRIX_RGB_STRIP_PIN, 800000, false);
// Prog DMA
WS2812_Matrix_DMA = dma_claim_unused_channel(true);
Dma_Conf = dma_channel_get_default_config(WS2812_Matrix_DMA);
channel_config_set_transfer_data_size(&Dma_Conf, DMA_SIZE_32);
channel_config_set_read_increment(&Dma_Conf, true);
channel_config_set_write_increment(&Dma_Conf, false);
// voir "hardware/regs/dreq.h"
// mais les TX du pio0, et les data_request DMA c'est la meme chose
Dma_Req_from_pio_sm = WS2812_Matrix_SM;
channel_config_set_dreq(&Dma_Conf, Dma_Req_from_pio_sm);
dma_channel_configure(
WS2812_Matrix_DMA,// Channel to be configured
&Dma_Conf, // The configuration we just created
&(pio0->txf[WS2812_Matrix_SM]), // The initial write address
&Matrix_Strip_GRB_Pix[0], // The initial read address
RGB_MATRIX_H*RGB_MATRIX_L, // Number of transfers; in this case each is 1 byte.
false // Start immediately.
);
}
void RGB_Loop(void)
{
switch (RGB_Send_State) {
case 0:
if ((Timer_ms1 - RGB_Send_Timer) > 10) {
RGB_Send_Timer = Timer_ms1;
RGB_Anim_Loop();
RGB_Send_State ++;
}
break;
case 1:
for (uint16_t i = 0; i < RGB_STRIP_LENGTH; i++) { // shift avant envoi
Right_Strip_GRB_Pix[i] = Right_Strip_GRB_Pix[i] << 8;
Left_Strip_GRB_Pix[i] = Left_Strip_GRB_Pix[i] << 8;
}
uint16_t Line = 0, Col = 0;
for (uint16_t i = 0; i < RGB_MATRIX_H*RGB_MATRIX_L; i++) { // creation du "rubanc" a partir de la matrice
Matrix_Strip_GRB_Pix[i] = Matrix_RGB_Pix[Line][Col] << 8;
// il faut se balader en serpentin
if (!(Col & 1)) { // si on est sur colonne paire
if (Line == (RGB_MATRIX_H - 1)) { // arrive en bas : decal sur le cote
Col ++;
} else { // sinon descend
Line ++;
}
} else { // colonne impaire
if (Line == 0) { // arrive en haut : decal cote
Col ++;
} else {
Line --; // sinon remonte
}
}
}
RGB_Send_State ++;
break;
case 2:
//dma_channel_start(DMA_Chan_For_WS2812);
dma_channel_transfer_from_buffer_now(WS2812_Right_Strip_DMA, &Right_Strip_GRB_Pix[0], RGB_STRIP_LENGTH);
dma_channel_transfer_from_buffer_now(WS2812_Left_Strip_DMA, &Left_Strip_GRB_Pix[0], RGB_STRIP_LENGTH);
dma_channel_transfer_from_buffer_now(WS2812_Matrix_DMA, &Matrix_Strip_GRB_Pix[0], RGB_MATRIX_H*RGB_MATRIX_L);
RGB_Send_State ++;
break;
case 3:
// attente que les DMA soient finis :
if ( (!dma_channel_is_busy(WS2812_Right_Strip_DMA)) &&
(!dma_channel_is_busy(WS2812_Left_Strip_DMA)) &&
(!dma_channel_is_busy(WS2812_Matrix_DMA)) ) {
RGB_Send_State ++;
}
break;
case 4:
for (uint16_t i = 0; i < RGB_STRIP_LENGTH; i++) { // deshift apres (pour pouvoir jouer plus facilement)
Right_Strip_GRB_Pix[i] = Right_Strip_GRB_Pix[i] >> 8;
Left_Strip_GRB_Pix[i] = Left_Strip_GRB_Pix[i] >> 8;
}
RGB_Send_State = 0;
break;
}
}