RGB LED cube 5x5x5  1.0
Martin Stejskal, Schmidt Dominik
 All Files Functions Variables Macros Groups Pages
framebuffer.c
Go to the documentation of this file.
1 
15 #include "framebuffer.h"
16 
17 
18 void framebuffer(volatile uint16_t (*p_fb_data)[5][5])
19 {
20  uint8_t i_data_r; // For temporary "red color"
21  uint8_t i_data_g; // For temporary "green color"
22  uint8_t i_data_b; // For temporary "blue color"
23 
24  for(uint8_t i_wall=0 ; i_wall<5 ; i_wall++)
25  {
26  // Before change set all LEDs to off
30 
31  // Set active "wall"
32  set_active_wall( i_wall );
33 
34  for(uint8_t i_column=0 ; i_column<5 ; i_column++)
35  {
36  // Get data for red color
37  i_data_r = ((*p_fb_data)[i_wall][i_column] >> 10) & (0b00011111);
38 
39  // Get data for green color
40  i_data_g = ((*p_fb_data)[i_wall][i_column] >> 5) & (0b00011111);
41 
42  // Get data for blue color
43  i_data_b = ((*p_fb_data)[i_wall][i_column] >> 0) & (0b00011111);
44 
45  // Before change set all LEDs to off
49 
50  // Set active "column"
51  set_active_column( i_column );
52 
53  // And show data
54  load_DATA_RED_on_bus( i_data_r );
55  load_DATA_GREEN_on_bus( i_data_g );
56  load_DATA_BLUE_on_bus( i_data_b );
57 
58 
59  sei(); // Enable interrupts -> time non critical routine
63  _delay_us(column_delay);
64  cli(); // Disable interrupts -> time critical routine
65  }
66  }
67 }
68 
69 
70 
71 
72 
73 void init_framebuffer(void)
74 {
75  // Data - Red color (output, set to 0)
76  io_set_dir_out( D_R0_PORT, D_R0_pin );
77  io_set_0( D_R0_PORT, D_R0_pin );
78  io_set_dir_out( D_R1_PORT, D_R1_pin );
79  io_set_0( D_R1_PORT, D_R1_pin );
80  io_set_dir_out( D_R2_PORT, D_R2_pin );
81  io_set_0( D_R2_PORT, D_R2_pin );
82  io_set_dir_out( D_R3_PORT, D_R3_pin );
83  io_set_0( D_R3_PORT, D_R3_pin );
84  io_set_dir_out( D_R4_PORT, D_R4_pin );
85  io_set_0( D_R4_PORT, D_R4_pin );
86 
87  // Data - Green color (output, set to 0)
88  io_set_dir_out( D_G0_PORT, D_G0_pin );
89  io_set_0( D_G0_PORT, D_G0_pin );
90  io_set_dir_out( D_G1_PORT, D_G1_pin );
91  io_set_0( D_G1_PORT, D_G1_pin );
92  io_set_dir_out( D_G2_PORT, D_G2_pin );
93  io_set_0( D_G2_PORT, D_G2_pin );
94  io_set_dir_out( D_G3_PORT, D_G3_pin );
95  io_set_0( D_G3_PORT, D_G3_pin );
96  io_set_dir_out( D_G4_PORT, D_G4_pin );
97  io_set_0( D_G4_PORT, D_G4_pin );
98 
99  // Data - Blue color (output, set to 0)
100  io_set_dir_out( D_B0_PORT, D_B0_pin );
101  io_set_0( D_B0_PORT, D_B0_pin );
102  io_set_dir_out( D_B1_PORT, D_B1_pin );
103  io_set_0( D_B1_PORT, D_B1_pin );
104  io_set_dir_out( D_B2_PORT, D_B2_pin );
105  io_set_0( D_B2_PORT, D_B2_pin );
106  io_set_dir_out( D_B3_PORT, D_B3_pin );
107  io_set_0( D_B3_PORT, D_B3_pin );
108  io_set_dir_out( D_B4_PORT, D_B4_pin );
109  io_set_0( D_B4_PORT, D_B4_pin );
110 
111 
112  // Wall - all output, set to 0
113  io_set_dir_out( WALL0_PORT, WALL0_pin );
114  io_set_0( WALL0_PORT, WALL0_pin );
115  io_set_dir_out( WALL1_PORT, WALL1_pin );
116  io_set_0( WALL1_PORT, WALL1_pin );
117  io_set_dir_out( WALL2_PORT, WALL2_pin );
118  io_set_0( WALL2_PORT, WALL2_pin );
119  io_set_dir_out( WALL3_PORT, WALL3_pin );
120  io_set_0( WALL3_PORT, WALL3_pin );
121  io_set_dir_out( WALL4_PORT, WALL4_pin );
122  io_set_0( WALL4_PORT, WALL4_pin );
123 
124  // Column - all output, set to "0"
125  io_set_dir_out( COLUMN0_PORT, COLUMN0_pin );
126  io_set_0( COLUMN0_PORT, COLUMN0_pin );
127  io_set_dir_out( COLUMN1_PORT, COLUMN1_pin );
128  io_set_0( COLUMN1_PORT, COLUMN1_pin );
129  io_set_dir_out( COLUMN2_PORT, COLUMN2_pin );
130  io_set_0( COLUMN2_PORT, COLUMN2_pin );
131  io_set_dir_out( COLUMN3_PORT, COLUMN3_pin );
132  io_set_0( COLUMN3_PORT, COLUMN3_pin );
133  io_set_dir_out( COLUMN4_PORT, COLUMN4_pin );
134  io_set_0( COLUMN4_PORT, COLUMN4_pin );
135 }
136 
137 
138 
139 
140 
141 inline void set_active_wall( uint8_t i_wall_number )
142 {
143  switch( i_wall_number )
144  {
145  case 0: // "Wall" is activated by low level
146  io_set_L( WALL0_PORT, WALL0_pin ); // Enable WALL0
147  io_set_H( WALL1_PORT, WALL1_pin ); // Disable WALL1
148  io_set_H( WALL2_PORT, WALL2_pin ); // Disable WALL2
149  io_set_H( WALL3_PORT, WALL3_pin ); // Disable WALL3
150  io_set_H( WALL4_PORT, WALL4_pin ); // Disable WALL4
151  break;
152  case 1: // "Wall" is activated by low level
153  io_set_H( WALL0_PORT, WALL0_pin ); // Disable WALL0
154  io_set_L( WALL1_PORT, WALL1_pin ); // Enable WALL1
155  io_set_H( WALL2_PORT, WALL2_pin ); // Disable WALL2
156  io_set_H( WALL3_PORT, WALL3_pin ); // Disable WALL3
157  io_set_H( WALL4_PORT, WALL4_pin ); // Disable WALL4
158  break;
159  case 2: // "Wall" is activated by low level
160  io_set_H( WALL0_PORT, WALL0_pin ); // Disable WALL0
161  io_set_H( WALL1_PORT, WALL1_pin ); // Disable WALL1
162  io_set_L( WALL2_PORT, WALL2_pin ); // Enable WALL2
163  io_set_H( WALL3_PORT, WALL3_pin ); // Disable WALL3
164  io_set_H( WALL4_PORT, WALL4_pin ); // Disable WALL4
165  break;
166  case 3: // "Wall" is activated by low level
167  io_set_H( WALL0_PORT, WALL0_pin ); // Disable WALL0
168  io_set_H( WALL1_PORT, WALL1_pin ); // Disable WALL1
169  io_set_H( WALL2_PORT, WALL2_pin ); // Disable WALL2
170  io_set_L( WALL3_PORT, WALL3_pin ); // Enable WALL3
171  io_set_H( WALL4_PORT, WALL4_pin ); // Disable WALL4
172  break;
173  case 4: // "Wall" is activated by low level
174  io_set_H( WALL0_PORT, WALL0_pin ); // Disable WALL0
175  io_set_H( WALL1_PORT, WALL1_pin ); // Disable WALL1
176  io_set_H( WALL2_PORT, WALL2_pin ); // Disable WALL2
177  io_set_H( WALL3_PORT, WALL3_pin ); // Disable WALL3
178  io_set_L( WALL4_PORT, WALL4_pin ); // Enable WALL4
179  break;
180  default : // Disable all
181  io_set_H( WALL0_PORT, WALL0_pin ); // Disable WALL0
182  io_set_H( WALL1_PORT, WALL1_pin ); // Disable WALL1
183  io_set_H( WALL2_PORT, WALL2_pin ); // Disable WALL2
184  io_set_H( WALL3_PORT, WALL3_pin ); // Disable WALL3
185  io_set_H( WALL4_PORT, WALL4_pin ); // Disable WALL4
186  }
187 }
188 
189 
190 
191 
192 
193 inline void set_active_column( uint8_t i_column_number )
194 {
195  switch( i_column_number )
196  {
197  case 0: // "Column" is activated by low level
198  io_set_L( COLUMN0_PORT, COLUMN0_pin ); // Enable COLUMN0
199  io_set_H( COLUMN1_PORT, COLUMN1_pin ); // Disable COLUMN1
200  io_set_H( COLUMN2_PORT, COLUMN2_pin ); // Disable COLUMN2
201  io_set_H( COLUMN3_PORT, COLUMN3_pin ); // Disable COLUMN3
202  io_set_H( COLUMN4_PORT, COLUMN4_pin ); // Disable COLUMN4
203  break;
204  case 1: // "Column" is activated by low level
205  io_set_H( COLUMN0_PORT, COLUMN0_pin ); // Disable COLUMN0
206  io_set_L( COLUMN1_PORT, COLUMN1_pin ); // Enable COLUMN1
207  io_set_H( COLUMN2_PORT, COLUMN2_pin ); // Disable COLUMN2
208  io_set_H( COLUMN3_PORT, COLUMN3_pin ); // Disable COLUMN3
209  io_set_H( COLUMN4_PORT, COLUMN4_pin ); // Disable COLUMN4
210  break;
211  case 2: // "Column" is activated by low level
212  io_set_H( COLUMN0_PORT, COLUMN0_pin ); // Disable COLUMN0
213  io_set_H( COLUMN1_PORT, COLUMN1_pin ); // Disable COLUMN1
214  io_set_L( COLUMN2_PORT, COLUMN2_pin ); // Enable COLUMN2
215  io_set_H( COLUMN3_PORT, COLUMN3_pin ); // Disable COLUMN3
216  io_set_H( COLUMN4_PORT, COLUMN4_pin ); // Disable COLUMN4
217  break;
218  case 3: // "Column" is activated by low level
219  io_set_H( COLUMN0_PORT, COLUMN0_pin ); // Disable COLUMN0
220  io_set_H( COLUMN1_PORT, COLUMN1_pin ); // Disable COLUMN1
221  io_set_H( COLUMN2_PORT, COLUMN2_pin ); // Disable COLUMN2
222  io_set_L( COLUMN3_PORT, COLUMN3_pin ); // Enable COLUMN3
223  io_set_H( COLUMN4_PORT, COLUMN4_pin ); // Disable COLUMN4
224  break;
225  case 4: // "Column" is activated by low level
226  io_set_H( COLUMN0_PORT, COLUMN0_pin ); // Disable COLUMN0
227  io_set_H( COLUMN1_PORT, COLUMN1_pin ); // Disable COLUMN1
228  io_set_H( COLUMN2_PORT, COLUMN2_pin ); // Disable COLUMN2
229  io_set_H( COLUMN3_PORT, COLUMN3_pin ); // Disable COLUMN3
230  io_set_L( COLUMN4_PORT, COLUMN4_pin ); // Enable COLUMN4
231  break;
232  default: // "Column" is activated by low level
233  io_set_H( COLUMN0_PORT, COLUMN0_pin ); // Disable COLUMN0
234  io_set_H( COLUMN1_PORT, COLUMN1_pin ); // Disable COLUMN1
235  io_set_H( COLUMN2_PORT, COLUMN2_pin ); // Disable COLUMN2
236  io_set_H( COLUMN3_PORT, COLUMN3_pin ); // Disable COLUMN3
237  io_set_H( COLUMN4_PORT, COLUMN4_pin ); // Disable COLUMN4
238  }
239 }
240 
241 
242 
243 
244 
245 inline void load_DATA_RED_on_bus( uint8_t i_data_r )
246 {
247  // D_R0 - test D_R0 bit in i_data_r
248  if ( (i_data_r & 0b00000001) == 0 )
249  { // If 0 -> set 0
250  io_set_0( D_R0_PORT, D_R0_pin );
251  }
252  else
253  { // Else set 1
254  io_set_1( D_R0_PORT, D_R0_pin );
255  }
256 /*---------------------------------------------------------------------------*/
257  // D_R1 - test D_R1 bit in i_data_r
258  if ( (i_data_r & 0b00000010) == 0 )
259  { // If 0 -> set 0
260  io_set_0( D_R1_PORT, D_R1_pin );
261  }
262  else
263  { // Else set 1
264  io_set_1( D_R1_PORT, D_R1_pin );
265  }
266 /*---------------------------------------------------------------------------*/
267  // D_R2 - test D_R2 bit in i_data_r
268  if ( (i_data_r & 0b00000100) == 0 )
269  { // If 0 -> set 0
270  io_set_0( D_R2_PORT, D_R2_pin );
271  }
272  else
273  { // Else set 1
274  io_set_1( D_R2_PORT, D_R2_pin );
275  }
276 /*---------------------------------------------------------------------------*/
277  // D_R3 - test D_R3 bit in i_data_r
278  if ( (i_data_r & 0b00001000) == 0 )
279  { // If 0 -> set 0
280  io_set_0( D_R3_PORT, D_R3_pin );
281  }
282  else
283  { // Else set 1
284  io_set_1( D_R3_PORT, D_R3_pin );
285  }
286 /*---------------------------------------------------------------------------*/
287  // D_R4 - test D_R4 bit in i_data_r
288  if ( (i_data_r & 0b00010000) == 0 )
289  { // If 0 -> set 0
290  io_set_0( D_R4_PORT, D_R4_pin );
291  }
292  else
293  { // Else set 1
294  io_set_1( D_R4_PORT, D_R4_pin );
295  }
296 }
297 
298 
299 
300 
301 
302 inline void load_DATA_GREEN_on_bus( uint8_t i_data_g )
303 {
304  // D_G0 - test D_G0 bit in i_data_g
305  if ( (i_data_g & 0b00000001) == 0 )
306  { // If 0 -> set 0
307  io_set_0( D_G0_PORT, D_G0_pin );
308  }
309  else
310  { // Else set 1
311  io_set_1( D_G0_PORT, D_G0_pin );
312  }
313 /*---------------------------------------------------------------------------*/
314  // D_G1 - test D_G1 bit in i_data_g
315  if ( (i_data_g & 0b00000010) == 0 )
316  { // If 0 -> set 0
317  io_set_0( D_G1_PORT, D_G1_pin );
318  }
319  else
320  { // Else set 1
321  io_set_1( D_G1_PORT, D_G1_pin );
322  }
323 /*---------------------------------------------------------------------------*/
324  // D_G2 - test D_G2 bit in i_data_g
325  if ( (i_data_g & 0b00000100) == 0 )
326  { // If 0 -> set 0
327  io_set_0( D_G2_PORT, D_G2_pin );
328  }
329  else
330  { // Else set 1
331  io_set_1( D_G2_PORT, D_G2_pin );
332  }
333 /*---------------------------------------------------------------------------*/
334  // D_G3 - test D_G3 bit in i_data_g
335  if ( (i_data_g & 0b00001000) == 0 )
336  { // If 0 -> set 0
337  io_set_0( D_G3_PORT, D_G3_pin );
338  }
339  else
340  { // Else set 1
341  io_set_1( D_G3_PORT, D_G3_pin );
342  }
343 /*---------------------------------------------------------------------------*/
344  // D_G4 - test D_G4 bit in i_data_g
345  if ( (i_data_g & 0b00010000) == 0 )
346  { // If 0 -> set 0
347  io_set_0( D_G4_PORT, D_G4_pin );
348  }
349  else
350  { // Else set 1
351  io_set_1( D_G4_PORT, D_G4_pin );
352  }
353 }
354 
355 
356 
357 
358 
359 inline void load_DATA_BLUE_on_bus( uint8_t i_data_b )
360 {
361  // D_B0 - test D_B0 bit in i_data_r
362  if ( (i_data_b & 0b00000001) == 0 )
363  { // If 0 -> set 0
364  io_set_0( D_B0_PORT, D_B0_pin );
365  }
366  else
367  { // Else set 1
368  io_set_1( D_B0_PORT, D_B0_pin );
369  }
370 /*---------------------------------------------------------------------------*/
371  // D_B1 - test D_B1 bit in i_data_b
372  if ( (i_data_b & 0b00000010) == 0 )
373  { // If 0 -> set 0
374  io_set_0( D_B1_PORT, D_B1_pin );
375  }
376  else
377  { // Else set 1
378  io_set_1( D_B1_PORT, D_B1_pin );
379  }
380 /*---------------------------------------------------------------------------*/
381  // D_B2 - test D_B2 bit in i_data_b
382  if ( (i_data_b & 0b00000100) == 0 )
383  { // If 0 -> set 0
384  io_set_0( D_B2_PORT, D_B2_pin );
385  }
386  else
387  { // Else set 1
388  io_set_1( D_B2_PORT, D_B2_pin );
389  }
390 /*---------------------------------------------------------------------------*/
391  // D_B3 - test D_B3 bit in i_data_b
392  if ( (i_data_b & 0b00001000) == 0 )
393  { // If 0 -> set 0
394  io_set_0( D_B3_PORT, D_B3_pin );
395  }
396  else
397  { // Else set 1
398  io_set_1( D_B3_PORT, D_B3_pin );
399  }
400 /*---------------------------------------------------------------------------*/
401  // D_B4 - test D_B4 bit in i_data_b
402  if ( (i_data_b & 0b00010000) == 0 )
403  { // If 0 -> set 0
404  io_set_0( D_B4_PORT, D_B4_pin );
405  }
406  else
407  { // Else set 1
408  io_set_1( D_B4_PORT, D_B4_pin );
409  }
410 }
411