summaryrefslogtreecommitdiffstats
path: root/keyboards/clueboard/2x1800/2021/max7219.c
blob: 1ba22362fe4ffa8bc108f4e476a7e4fb35bb6779 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (c) 2021 Zach White <skullydazed@gmail.com>
 * Copyright (c) 2007 Eberhard Fahle
 *
 *    max7219.c - A library for controling Leds with a MAX7219/MAX7221
 *
 *    Permission is hereby granted, free of charge, to any person
 *    obtaining a copy of this software and associated documentation
 *    files (the "Software"), to deal in the Software without
 *    restriction, including without limitation the rights to use,
 *    copy, modify, merge, publish, distribute, sublicense, and/or sell
 *    copies of the Software, and to permit persons to whom the
 *    Software is furnished to do so, subject to the following
 *    conditions:
 *
 *    This permission notice shall be included in all copies or
 *    substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *    OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * This driver started as a port of Arduino's LedControl to QMK. The
 * original Arduino code can be found here:
 *
 * https://github.com/wayoda/LedControl
 *
 * Unlike LedControl we are using the native SPI support, you will need to
 * use the native SPI pins for your MCU. You can set the CS pin with
 * `#define MAX7219_LOAD <pin>`.
 *
 * This has only been tested on AVR, specifically a Teensy 2.0++.
 */

#include "max7219.h"
#include "font.h"

// Datastructures
bool max7219_led_scrolling = true;
uint16_t max7219_buffer_end = 0;
uint8_t max7219_spidata[MAX_BYTES];
uint8_t max7219_led_a[8][MAX7219_BUFFER_SIZE];

/* Write max7219_spidata to all the max7219's
 */
void max7219_write_all(void) {
    dprintf("max7219_write_all()\n");
    if (spi_start(MAX7219_LOAD, false, 0, 8)) {
        for(int i = MAX_BYTES; i>0; i--) {
            dprintf("spi_write(%d)\n", max7219_spidata[i-1]);
            spi_write(max7219_spidata[i-1]);
        }
        spi_stop();
    } else {
        xprintf("Could not spi_start!\n");
    }
}

/* Write the current frame in max7219_led_a to all the max7219's
 */
void max7219_write_frame(void) {
    dprintf("max7219_write_frame()\n");

    // Set our opcode and data
    for (int col=0; col<8; col++) {
        for (int device_num=0; device_num<MAX7219_CONTROLLERS; device_num++) {
            int offset=device_num*2;
            max7219_spidata[offset] = max7219_led_a[col][device_num];
            max7219_spidata[offset+1] = col+1;
        }
        max7219_write_all();
    }
}

/* Stores a message in the sign buffer.
 *
 * message should be a 2d array with the outer array having a length of your
 * message and the inner array having a length of 6. Use the CHR_<letter>
 * macros from font.h to populate your array.
 *
 * Example:
 *
 *      uint8_t message[10][6] = {CHR_INTERROBANG, CHR_C, CHR_l, CHR_u, CHR_e, CHR_b, CHR_o, CHR_a, CHR_r, CHR_d};
 *      max7219_message(message, 10);
 */
void max7219_message_sign(uint8_t message[][6], size_t message_len) {
    uint8_t letter_num = 0;
    uint8_t letter_col = 0;
    max7219_buffer_end = message_len * 6 + 32;

    for (int device_num=0; device_num<MAX7219_BUFFER_SIZE; device_num++) {
        for (int col=0; col<8; col++) {
            if (letter_num >= message_len) {
                max7219_led_a[col][device_num] = 0b00000000;
            } else {
                max7219_led_a[col][device_num] = message[letter_num][letter_col];
                if (letter_col == 5) {
                    letter_num++;
                    letter_col = 0;
                } else {
                    letter_col++;
                }
            }
        }
    }

    max7219_write_frame();
}

/* Scroll the content on the sign left by 1 column.
 *
 * When loop_message is true columns that slide off the left will be added
 * to the right to be displayed again.
 */
void max7219_message_sign_task(bool loop_message) {
    uint8_t left_col = 0b00000000;

    if (!max7219_led_scrolling) {
        return;
    }

    if (loop_message) {
        left_col = max7219_led_a[0][0];
    }

    int i=0;

    for (int device_num=0; device_num<MAX7219_BUFFER_SIZE; device_num++) {
        for (int col=0; col<8; col++) {
            i++;

            if (i == max7219_buffer_end) {
                max7219_led_a[col][device_num] = left_col;
                device_num=MAX7219_BUFFER_SIZE;
                break;
            } else if (col < 7) {
                max7219_led_a[col][device_num] = max7219_led_a[col+1][device_num];
            } else if (device_num == MAX7219_BUFFER_SIZE-1) {
                max7219_led_a[col][device_num] = left_col;
            } else {
                max7219_led_a[col][device_num] = max7219_led_a[0][device_num+1];
            }
        }
    }
    max7219_write_frame();
}

/* Write data to a single max7219
 */
void max7219_write(int device_num, volatile uint8_t opcode, volatile uint8_t data) {
    dprintf("max7219_write(%d, %d, %d)\n", device_num, opcode, data);

    // Clear the data array
    for(int i = MAX_BYTES; i>0; i--) {
        max7219_spidata[i-1]=0;
    }

    // Set our opcode and data
    uint8_t offset = device_num*2;
    max7219_spidata[offset] = data;
    max7219_spidata[offset+1] = opcode;

    // Write the data
    max7219_write_all();
}

/* Turn off all the LEDs
 */
void max7219_clear_display(void) {
    dprintf("max7219_clear_display();\n");

    for (int col=0; col<8; col++) {
        for (int device_num=0; device_num<MAX7219_BUFFER_SIZE; device_num++) {
            max7219_led_a[col][device_num] = 0b00000000;
        }
    }
    max7219_write_frame();
}

/* Enable the display test (IE turn on all 64 LEDs)
 */
void max7219_display_test(int device_num, bool enabled) {
    dprintf("max7219_display_test(%d, %d);\n", device_num, enabled);

    if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
        return;
    }

    max7219_write(device_num, OP_DISPLAYTEST, enabled);
}

/* Initialize the max7219 system and set the controller(s) to a default state.
 */
void max7219_init(void) {
    wait_ms(1500);
    dprintf("max7219_init()\n");

    setPinOutput(MAX7219_LOAD);
    writePinHigh(MAX7219_LOAD);
    spi_init();

    for (int i=0; i<MAX7219_CONTROLLERS; i++) {
        max7219_shutdown(i, true);
    }

    for (int i=0; i<MAX7219_CONTROLLERS; i++) {
        // Reset everything to defaults and enable the display
        max7219_display_test(i, false);
        max7219_set_scan_limit(i, 7);
        max7219_set_decode_mode(i, 0);
        max7219_set_intensity(i, MAX7219_LED_INTENSITY);
    }

    max7219_clear_display();

#ifndef MAX7219_NO_STARTUP_TEST
    for (int i=0; i<MAX7219_CONTROLLERS; i++) {
        // Test this display
        max7219_display_test(i, true);
        wait_ms(75);
        max7219_display_test(i, false);
    }
#endif

    for (int i=0; i<MAX7219_CONTROLLERS; i++) {
        max7219_shutdown(i, false);
    }
}

/* Set the decode mode of the controller. You probably don't want to change this.
 */
void max7219_set_decode_mode(int device_num, int mode) {
    dprintf("max7219_set_decode_mode(%d, %d);\n", device_num, mode);

    if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
        return;
    }

    max7219_write(device_num, OP_DECODEMODE, mode);
}

/* Set the intensity (brightness) for the LEDs.
 */
void max7219_set_intensity(int device_num, int intensity) {
    dprintf("max7219_set_intensity(%d, %d);\n", device_num, intensity);

    if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
        return;
    }

    if (intensity >= 0 && intensity<16) {
            max7219_write(device_num, OP_INTENSITY, intensity);
    }
}

/* Control a single LED.
 */
void max7219_set_led(int row, int column, bool state) {
    dprintf("max7219_set_led(%d, %d, %d);\n", row, column, state);

    if (column<0 || column>8*MAX7219_CONTROLLERS) {
        xprintf("max7219_set_led: column (%d) out of bounds\n", column);
        return;
    }

    if (row<0 || row>7) {
        xprintf("max7219_set_led: row (%d) out of bounds\n", row);
        return;
    }

    /* At this point we reverse the sense of row and column to match the
     * physical layout of my LEDs.
     */
    uint8_t device_num = column / 8;
    uint8_t col = column % 8;
    uint8_t val = 0b10000000 >> row;

    if (state) {
        max7219_led_a[col][device_num] = max7219_led_a[col][device_num]|val;
    } else {
        val = ~val;
        max7219_led_a[col][device_num] = max7219_led_a[col][device_num]&val;
    }
    max7219_write(device_num, col+1, max7219_led_a[col][device_num]);
}

/* Set the number of digits (rows) to be scanned.
 */
void max7219_set_scan_limit(int device_num, int limit) {
    dprintf("max7219_set_scan_limit(%d, %d);\n", device_num, limit);

    if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
        return;
    }

    if (limit >= 0 && limit < 8) {
        max7219_write(device_num, OP_SCANLIMIT, limit);
    }
}

/* Enable (true) or disable (false) the controller.
 */
void max7219_shutdown(int device_num, bool shutdown) {
    dprintf("max7219_shutdown(%d, %d);\n", device_num, shutdown);

    if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
        return;
    }

    max7219_write(device_num, OP_SHUTDOWN, !shutdown);
}