Krolyxon
Commit 1d55752

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8U8g2srcclibu8x8_d_max7219.c

459 lines · 16.2 KB · C

1/*
2
3 u8x8_d_max7219.c
4
5 Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
6
7 Copyright (c) 2017, olikraus@gmail.com
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without modification,
11 are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright notice, this list
14 of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above copyright notice, this
17 list of conditions and the following disclaimer in the documentation and/or other
18 materials provided with the distribution.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36
37#include "u8x8.h"
38
39
40
41static const uint8_t u8x8_d_max7219_init_seq[] = {
42
43 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
44 U8X8_CA(15, 0), /* test mode off */
45 U8X8_CA(15, 0), /* test mode off */
46 U8X8_CA(15, 0), /* test mode off */
47 U8X8_CA(15, 0), /* test mode off */
48 U8X8_END_TRANSFER(), /* disable chip */
49
50 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
51 U8X8_CA(12, 0), /* */
52 U8X8_CA(12, 0), /* */
53 U8X8_CA(12, 0), /* */
54 U8X8_CA(12, 0), /* */
55 U8X8_END_TRANSFER(), /* disable chip */
56
57 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
58 U8X8_CA(9, 0), /* decode mode: graphics */
59 U8X8_CA(9, 0), /* decode mode: graphics */
60 U8X8_CA(9, 0), /* decode mode: graphics */
61 U8X8_CA(9, 0), /* decode mode: graphics */
62 U8X8_END_TRANSFER(), /* disable chip */
63
64 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
65 U8X8_CA(10, 10), /* medium high intensity */
66 U8X8_CA(10, 10), /* medium high intensity */
67 U8X8_CA(10, 10), /* medium high intensity */
68 U8X8_CA(10, 10), /* medium high intensity */
69 U8X8_END_TRANSFER(), /* disable chip */
70
71 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
72 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
73 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
74 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
75 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
76 U8X8_END_TRANSFER(), /* disable chip */
77
78
79 //U8X8_CA(12, 0), /* shutdown */
80
81 //U8X8_END_TRANSFER(), /* disable chip */
82 U8X8_END() /* end of sequence */
83};
84
85static const uint8_t u8x8_d_max7219_powersave0_seq[] = {
86 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
87 U8X8_CA(12, 1), /* display on */
88 U8X8_CA(12, 1), /* display on */
89 U8X8_CA(12, 1), /* display on */
90 U8X8_CA(12, 1), /* display on */
91 U8X8_END_TRANSFER(), /* disable chip */
92 U8X8_END() /* end of sequence */
93};
94
95static const uint8_t u8x8_d_max7219_powersave1_seq[] = {
96 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
97 U8X8_CA(12, 0), /* shutdown */
98 U8X8_CA(12, 0), /* shutdown */
99 U8X8_CA(12, 0), /* shutdown */
100 U8X8_CA(12, 0), /* shutdown */
101 U8X8_END_TRANSFER(), /* disable chip */
102 U8X8_END() /* end of sequence */
103};
104
105
106static uint8_t u8x8_d_max7219_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
107{
108 uint8_t c, j, i;
109 uint8_t *ptr;
110 switch(msg)
111 {
112 /* handled by the calling function
113 case U8X8_MSG_DISPLAY_SETUP_MEMORY:
114 u8x8_d_helper_display_setup_memory(u8x8, &u8x8_pcf8812_96x65_display_info);
115 break;
116 case U8X8_MSG_DISPLAY_INIT:
117 u8x8_d_helper_display_init(u8x8);
118 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_init_seq);
119 break;
120 case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
121 if ( arg_int == 0 )
122 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave0_seq);
123 else
124 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave1_seq);
125 break;
126 */
127/* not supported by MAX7219
128 case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
129 break;
130*/
131#ifdef U8X8_WITH_SET_CONTRAST
132 case U8X8_MSG_DISPLAY_SET_CONTRAST:
133 u8x8_cad_StartTransfer(u8x8);
134 for( i = 0; i < u8x8->display_info->tile_width; i++ )
135 {
136 u8x8_cad_SendCmd(u8x8, 10 ); /* brightness */
137 u8x8_cad_SendArg(u8x8, (arg_int>>4) ); /* 0..15 for contrast */
138 }
139 u8x8_cad_EndTransfer(u8x8);
140 break;
141#endif
142 case U8X8_MSG_DISPLAY_DRAW_TILE:
143 /* transfer always has to start at x pos 0 (u8x8 is not supported) */
144 /* also y pos has to be 0 */
145 /* arg_int is ignored */
146 //x = ((u8x8_tile_t *)arg_ptr)->x_pos;
147
148 c = ((u8x8_tile_t *)arg_ptr)->cnt; /* number of tiles */
149 ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr; /* data ptr to the tiles */
150 for( i = 0; i < 8; i++ )
151 {
152 u8x8_cad_StartTransfer(u8x8);
153 for( j = 0; j < c; j++ )
154 {
155 u8x8_cad_SendCmd(u8x8, i+1); /* commands 1..8 select the byte */
156 u8x8_cad_SendArg(u8x8, *ptr );
157 ptr++;
158 }
159 u8x8_cad_EndTransfer(u8x8);
160 }
161
162 break;
163 default:
164 return 0;
165 }
166 return 1;
167}
168
169/*==============================*/
170
171static const u8x8_display_info_t u8x8_max7219_32x8_display_info =
172{
173 /* chip_enable_level = */ 0,
174 /* chip_disable_level = */ 1,
175
176 /* post_chip_enable_wait_ns = */ 100,
177 /* pre_chip_disable_wait_ns = */ 100,
178 /* reset_pulse_width_ms = */ 100,
179 /* post_reset_wait_ms = */ 100,
180 /* sda_setup_time_ns = */ 100,
181 /* sck_pulse_width_ns = */ 100,
182 /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
183 /* spi_mode = */ 0, /* active high, rising edge */
184 /* i2c_bus_clock_100kHz = */ 4,
185 /* data_setup_time_ns = */ 40,
186 /* write_pulse_width_ns = */ 150,
187 /* tile_width = */ 4,
188 /* tile_height = */ 1,
189 /* default_x_offset = */ 0,
190 /* flipmode_x_offset = */ 0,
191 /* pixel_width = */ 32,
192 /* pixel_height = */ 8
193};
194
195uint8_t u8x8_d_max7219_32x8(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
196{
197 switch(msg)
198 {
199 case U8X8_MSG_DISPLAY_SETUP_MEMORY :
200 u8x8_d_helper_display_setup_memory(u8x8, &u8x8_max7219_32x8_display_info);
201 return 1;
202 case U8X8_MSG_DISPLAY_INIT:
203 u8x8_d_helper_display_init(u8x8);
204 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_init_seq);
205 return 1;
206 case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
207 if ( arg_int == 0 )
208 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave0_seq);
209 else
210 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave1_seq);
211 return 1;
212 }
213 return u8x8_d_max7219_generic(u8x8, msg, arg_int, arg_ptr);
214}
215
216/*==============================*/
217
218static const u8x8_display_info_t u8x8_max7219_16x16_display_info =
219{
220 /* chip_enable_level = */ 0,
221 /* chip_disable_level = */ 1,
222
223 /* post_chip_enable_wait_ns = */ 100,
224 /* pre_chip_disable_wait_ns = */ 100,
225 /* reset_pulse_width_ms = */ 100,
226 /* post_reset_wait_ms = */ 100,
227 /* sda_setup_time_ns = */ 100,
228 /* sck_pulse_width_ns = */ 100,
229 /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
230 /* spi_mode = */ 0, /* active high, rising edge */
231 /* i2c_bus_clock_100kHz = */ 4,
232 /* data_setup_time_ns = */ 40,
233 /* write_pulse_width_ns = */ 150,
234 /* tile_width = */ 2,
235 /* tile_height = */ 2,
236 /* default_x_offset = */ 0,
237 /* flipmode_x_offset = */ 0,
238 /* pixel_width = */ 16,
239 /* pixel_height = */ 16
240};
241
242/*
243 Multiple page rows are not supported, so 16x16 will not work.
244 Due to the hardware structure of such displays all tiles of the display
245 must be written at once.
246 This is not possible with the current u8g2 structure.
247 So u8x8_d_max7219_16x16 will not work.
248*/
249
250uint8_t u8x8_d_max7219_16x16(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
251{
252 switch(msg)
253 {
254 case U8X8_MSG_DISPLAY_SETUP_MEMORY :
255 u8x8_d_helper_display_setup_memory(u8x8, &u8x8_max7219_16x16_display_info);
256 return 1;
257 case U8X8_MSG_DISPLAY_INIT:
258 u8x8_d_helper_display_init(u8x8);
259 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_init_seq);
260 return 1;
261 case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
262 if ( arg_int == 0 )
263 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave0_seq);
264 else
265 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave1_seq);
266 return 1;
267 }
268 return u8x8_d_max7219_generic(u8x8, msg, arg_int, arg_ptr);
269}
270
271/*==============================*/
272
273static const u8x8_display_info_t u8x8_max7219_8x8_display_info =
274{
275 /* chip_enable_level = */ 0,
276 /* chip_disable_level = */ 1,
277
278 /* post_chip_enable_wait_ns = */ 100,
279 /* pre_chip_disable_wait_ns = */ 100,
280 /* reset_pulse_width_ms = */ 100,
281 /* post_reset_wait_ms = */ 100,
282 /* sda_setup_time_ns = */ 100,
283 /* sck_pulse_width_ns = */ 100,
284 /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
285 /* spi_mode = */ 0, /* active high, rising edge */
286 /* i2c_bus_clock_100kHz = */ 4,
287 /* data_setup_time_ns = */ 40,
288 /* write_pulse_width_ns = */ 150,
289 /* tile_width = */ 1,
290 /* tile_height = */ 1,
291 /* default_x_offset = */ 0,
292 /* flipmode_x_offset = */ 0,
293 /* pixel_width = */ 8,
294 /* pixel_height = */ 8
295};
296
297uint8_t u8x8_d_max7219_8x8(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
298{
299 switch(msg)
300 {
301 case U8X8_MSG_DISPLAY_SETUP_MEMORY :
302 u8x8_d_helper_display_setup_memory(u8x8, &u8x8_max7219_8x8_display_info);
303 return 1;
304 case U8X8_MSG_DISPLAY_INIT:
305 u8x8_d_helper_display_init(u8x8);
306 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_init_seq);
307 return 1;
308 case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
309 if ( arg_int == 0 )
310 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave0_seq);
311 else
312 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave1_seq);
313 return 1;
314 }
315 return u8x8_d_max7219_generic(u8x8, msg, arg_int, arg_ptr);
316}
317
318
319/*==============================*/
320
321static const uint8_t u8x8_d_max7219_8_init_seq[] = {
322
323 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
324 U8X8_CA(15, 0), /* test mode off */
325 U8X8_CA(15, 0), /* test mode off */
326 U8X8_CA(15, 0), /* test mode off */
327 U8X8_CA(15, 0), /* test mode off */
328 U8X8_CA(15, 0), /* test mode off */
329 U8X8_CA(15, 0), /* test mode off */
330 U8X8_CA(15, 0), /* test mode off */
331 U8X8_CA(15, 0), /* test mode off */
332 U8X8_END_TRANSFER(), /* disable chip */
333
334 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
335 U8X8_CA(12, 0), /* */
336 U8X8_CA(12, 0), /* */
337 U8X8_CA(12, 0), /* */
338 U8X8_CA(12, 0), /* */
339 U8X8_CA(12, 0), /* */
340 U8X8_CA(12, 0), /* */
341 U8X8_CA(12, 0), /* */
342 U8X8_CA(12, 0), /* */
343 U8X8_END_TRANSFER(), /* disable chip */
344
345 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
346 U8X8_CA(9, 0), /* decode mode: graphics */
347 U8X8_CA(9, 0), /* decode mode: graphics */
348 U8X8_CA(9, 0), /* decode mode: graphics */
349 U8X8_CA(9, 0), /* decode mode: graphics */
350 U8X8_CA(9, 0), /* decode mode: graphics */
351 U8X8_CA(9, 0), /* decode mode: graphics */
352 U8X8_CA(9, 0), /* decode mode: graphics */
353 U8X8_CA(9, 0), /* decode mode: graphics */
354 U8X8_END_TRANSFER(), /* disable chip */
355
356 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
357 U8X8_CA(10, 10), /* medium high intensity */
358 U8X8_CA(10, 10), /* medium high intensity */
359 U8X8_CA(10, 10), /* medium high intensity */
360 U8X8_CA(10, 10), /* medium high intensity */
361 U8X8_CA(10, 10), /* medium high intensity */
362 U8X8_CA(10, 10), /* medium high intensity */
363 U8X8_CA(10, 10), /* medium high intensity */
364 U8X8_CA(10, 10), /* medium high intensity */
365 U8X8_END_TRANSFER(), /* disable chip */
366
367 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
368 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
369 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
370 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
371 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
372 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
373 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
374 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
375 U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
376 U8X8_END_TRANSFER(), /* disable chip */
377
378
379 //U8X8_CA(12, 0), /* shutdown */
380
381 //U8X8_END_TRANSFER(), /* disable chip */
382 U8X8_END() /* end of sequence */
383};
384
385static const uint8_t u8x8_d_max7219_8_powersave0_seq[] = {
386 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
387 U8X8_CA(12, 1), /* display on */
388 U8X8_CA(12, 1), /* display on */
389 U8X8_CA(12, 1), /* display on */
390 U8X8_CA(12, 1), /* display on */
391 U8X8_CA(12, 1), /* display on */
392 U8X8_CA(12, 1), /* display on */
393 U8X8_CA(12, 1), /* display on */
394 U8X8_CA(12, 1), /* display on */
395 U8X8_END_TRANSFER(), /* disable chip */
396 U8X8_END() /* end of sequence */
397};
398
399static const uint8_t u8x8_d_max7219_8_powersave1_seq[] = {
400 U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
401 U8X8_CA(12, 0), /* shutdown */
402 U8X8_CA(12, 0), /* shutdown */
403 U8X8_CA(12, 0), /* shutdown */
404 U8X8_CA(12, 0), /* shutdown */
405 U8X8_CA(12, 0), /* shutdown */
406 U8X8_CA(12, 0), /* shutdown */
407 U8X8_CA(12, 0), /* shutdown */
408 U8X8_CA(12, 0), /* shutdown */
409 U8X8_END_TRANSFER(), /* disable chip */
410 U8X8_END() /* end of sequence */
411};
412
413
414static const u8x8_display_info_t u8x8_max7219_64x8_display_info =
415{
416 /* chip_enable_level = */ 0,
417 /* chip_disable_level = */ 1,
418
419 /* post_chip_enable_wait_ns = */ 100,
420 /* pre_chip_disable_wait_ns = */ 100,
421 /* reset_pulse_width_ms = */ 100,
422 /* post_reset_wait_ms = */ 100,
423 /* sda_setup_time_ns = */ 100,
424 /* sck_pulse_width_ns = */ 100,
425 /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
426 /* spi_mode = */ 0, /* active high, rising edge */
427 /* i2c_bus_clock_100kHz = */ 4,
428 /* data_setup_time_ns = */ 40,
429 /* write_pulse_width_ns = */ 150,
430 /* tile_width = */ 8,
431 /* tile_height = */ 1,
432 /* default_x_offset = */ 0,
433 /* flipmode_x_offset = */ 0,
434 /* pixel_width = */ 64,
435 /* pixel_height = */ 8
436};
437
438uint8_t u8x8_d_max7219_64x8(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
439{
440 switch(msg)
441 {
442 case U8X8_MSG_DISPLAY_SETUP_MEMORY :
443 u8x8_d_helper_display_setup_memory(u8x8, &u8x8_max7219_64x8_display_info);
444 return 1;
445 case U8X8_MSG_DISPLAY_INIT:
446 u8x8_d_helper_display_init(u8x8);
447 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_8_init_seq);
448 return 1;
449 case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
450 if ( arg_int == 0 )
451 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_8_powersave0_seq);
452 else
453 u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_8_powersave1_seq);
454 return 1;
455 }
456 return u8x8_d_max7219_generic(u8x8, msg, arg_int, arg_ptr);
457}
458
459