This example demonstrates how to draw on the MKR RGB shield. The circuit: - Arduino MKR board - Arduino MKR RGB shield attached This example code is in the public domain. Orginal code for Arduino - Adaption for ARM MBED compiler (tested on NUCLEO L073RZ and NUCLEO F411RE) - Christian Dupaty
This example demonstrates how to draw on the arduino MKR RGB shield
Orginal code for Arduino : https://docs.arduino.cc/hardware/mkr-rgb-shield
Adaption for ARM MBED compiler (tested on NUCLEO L073RZ and NUCLEO F411RE)
Christian Dupaty oct 2021 see http://genelaix.free.fr
MKRRGBMatrix.cpp@0:10ce458ab6fa, 2021-10-05 (annotated)
- Committer:
- cdupaty
- Date:
- Tue Oct 05 16:07:13 2021 +0000
- Revision:
- 0:10ce458ab6fa
This example demonstrates how to draw on the; MKR RGB shield ; https://store.arduino.cc/products/arduino-mkr-rgb-shield?selectedStore=eu; Orginal code for Arduino; - Adaption for ARM MBED compiler (tested on NUCLEO L073RZ and NUCLEO F411RE); - ...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cdupaty | 0:10ce458ab6fa | 1 | /* |
cdupaty | 0:10ce458ab6fa | 2 | This file is part of the Arduino_MKRRGB library. |
cdupaty | 0:10ce458ab6fa | 3 | Copyright (c) 2019 Arduino SA. All rights reserved. |
cdupaty | 0:10ce458ab6fa | 4 | |
cdupaty | 0:10ce458ab6fa | 5 | This library is free software; you can redistribute it and/or |
cdupaty | 0:10ce458ab6fa | 6 | modify it under the terms of the GNU Lesser General Public |
cdupaty | 0:10ce458ab6fa | 7 | License as published by the Free Software Foundation; either |
cdupaty | 0:10ce458ab6fa | 8 | version 2.1 of the License, or (at your option) any later version. |
cdupaty | 0:10ce458ab6fa | 9 | |
cdupaty | 0:10ce458ab6fa | 10 | This library is distributed in the hope that it will be useful, |
cdupaty | 0:10ce458ab6fa | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
cdupaty | 0:10ce458ab6fa | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
cdupaty | 0:10ce458ab6fa | 13 | Lesser General Public License for more details. |
cdupaty | 0:10ce458ab6fa | 14 | |
cdupaty | 0:10ce458ab6fa | 15 | You should have received a copy of the GNU Lesser General Public |
cdupaty | 0:10ce458ab6fa | 16 | License along with this library; if not, write to the Free Software |
cdupaty | 0:10ce458ab6fa | 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
cdupaty | 0:10ce458ab6fa | 18 | */ |
cdupaty | 0:10ce458ab6fa | 19 | #include "mbed.h" |
cdupaty | 0:10ce458ab6fa | 20 | //#include <spi.h> |
cdupaty | 0:10ce458ab6fa | 21 | //#include <wiring_private.h> |
cdupaty | 0:10ce458ab6fa | 22 | |
cdupaty | 0:10ce458ab6fa | 23 | #include "MKRRGBMatrix.h" |
cdupaty | 0:10ce458ab6fa | 24 | |
cdupaty | 0:10ce458ab6fa | 25 | //static SPIClass SPI_MATRIX(&sercom0, A3, A4, A3, SPI_PAD_0_SCK_1, SERCOM_RX_PAD_0); |
cdupaty | 0:10ce458ab6fa | 26 | |
cdupaty | 0:10ce458ab6fa | 27 | SPI SPI_MATRIX(D11, D12, D13); // MOSI, MISO, CLK,SS |
cdupaty | 0:10ce458ab6fa | 28 | |
cdupaty | 0:10ce458ab6fa | 29 | // This table is based on the formula: gamma = (int)(pow(i / 255.0, gamma) * 255 + offset) |
cdupaty | 0:10ce458ab6fa | 30 | // where gamma = 2.5 and offset is 0.5 |
cdupaty | 0:10ce458ab6fa | 31 | // based on: |
cdupaty | 0:10ce458ab6fa | 32 | // https://github.com/ManiacalLabs/BiblioPixel/blob/master/bibliopixel/colors/gamma.py |
cdupaty | 0:10ce458ab6fa | 33 | static const uint8_t GAMMA_TABLE[] = { |
cdupaty | 0:10ce458ab6fa | 34 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
cdupaty | 0:10ce458ab6fa | 35 | 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
cdupaty | 0:10ce458ab6fa | 36 | 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, |
cdupaty | 0:10ce458ab6fa | 37 | 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, |
cdupaty | 0:10ce458ab6fa | 38 | 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 14, |
cdupaty | 0:10ce458ab6fa | 39 | 14, 15, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 22, |
cdupaty | 0:10ce458ab6fa | 40 | 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32, |
cdupaty | 0:10ce458ab6fa | 41 | 33, 33, 34, 35, 36, 36, 37, 38, 39, 40, 40, 41, 42, 43, 44, 45, |
cdupaty | 0:10ce458ab6fa | 42 | 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, |
cdupaty | 0:10ce458ab6fa | 43 | 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, |
cdupaty | 0:10ce458ab6fa | 44 | 80, 81, 82, 83, 85, 86, 87, 89, 90, 91, 93, 94, 95, 97, 98, 99, |
cdupaty | 0:10ce458ab6fa | 45 | 101, 102, 104, 105, 107, 108, 110, 111, 113, 114, 116, 117, 119, 121, 122, 124, |
cdupaty | 0:10ce458ab6fa | 46 | 125, 127, 129, 130, 132, 134, 135, 137, 139, 141, 142, 144, 146, 148, 150, 151, |
cdupaty | 0:10ce458ab6fa | 47 | 153, 155, 157, 159, 161, 163, 165, 166, 168, 170, 172, 174, 176, 178, 180, 182, |
cdupaty | 0:10ce458ab6fa | 48 | 184, 186, 189, 191, 193, 195, 197, 199, 201, 204, 206, 208, 210, 212, 215, 217, |
cdupaty | 0:10ce458ab6fa | 49 | 219, 221, 224, 226, 228, 231, 233, 235, 238, 240, 243, 245, 248, 250, 253, 255 |
cdupaty | 0:10ce458ab6fa | 50 | }; |
cdupaty | 0:10ce458ab6fa | 51 | |
cdupaty | 0:10ce458ab6fa | 52 | RGBMatrixClass::RGBMatrixClass() : |
cdupaty | 0:10ce458ab6fa | 53 | ArduinoGraphics(RGB_MATRIX_WIDTH, RGB_MATRIX_HEIGHT) |
cdupaty | 0:10ce458ab6fa | 54 | { |
cdupaty | 0:10ce458ab6fa | 55 | SPI_MATRIX.frequency(4000000); |
cdupaty | 0:10ce458ab6fa | 56 | } |
cdupaty | 0:10ce458ab6fa | 57 | |
cdupaty | 0:10ce458ab6fa | 58 | RGBMatrixClass::~RGBMatrixClass() |
cdupaty | 0:10ce458ab6fa | 59 | { |
cdupaty | 0:10ce458ab6fa | 60 | } |
cdupaty | 0:10ce458ab6fa | 61 | |
cdupaty | 0:10ce458ab6fa | 62 | int RGBMatrixClass::begin() |
cdupaty | 0:10ce458ab6fa | 63 | { |
cdupaty | 0:10ce458ab6fa | 64 | if (!ArduinoGraphics::begin()) { |
cdupaty | 0:10ce458ab6fa | 65 | return 0; |
cdupaty | 0:10ce458ab6fa | 66 | } |
cdupaty | 0:10ce458ab6fa | 67 | |
cdupaty | 0:10ce458ab6fa | 68 | textFont(Font_5x7); |
cdupaty | 0:10ce458ab6fa | 69 | |
cdupaty | 0:10ce458ab6fa | 70 | // zero the matrix up to the end frame, and fill the end frame |
cdupaty | 0:10ce458ab6fa | 71 | memset(_buffer, 0x00, 4 + 4 * RGB_MATRIX_WIDTH * RGB_MATRIX_HEIGHT); |
cdupaty | 0:10ce458ab6fa | 72 | memset(_buffer + 4 + 4 * RGB_MATRIX_WIDTH * RGB_MATRIX_HEIGHT, 0xff, sizeof(_buffer) - (4 + 4 * RGB_MATRIX_WIDTH * RGB_MATRIX_HEIGHT)); |
cdupaty | 0:10ce458ab6fa | 73 | |
cdupaty | 0:10ce458ab6fa | 74 | //SPI_MATRIX.begin(); |
cdupaty | 0:10ce458ab6fa | 75 | // SPI_MATRIX.beginTransaction(SPISettings(12e6, MSBFIRST, SPI_MODE0)); |
cdupaty | 0:10ce458ab6fa | 76 | |
cdupaty | 0:10ce458ab6fa | 77 | //pinPeripheral(A3, PIO_SERCOM_ALT); |
cdupaty | 0:10ce458ab6fa | 78 | //pinPeripheral(A4, PIO_SERCOM_ALT); |
cdupaty | 0:10ce458ab6fa | 79 | |
cdupaty | 0:10ce458ab6fa | 80 | brightness(127); |
cdupaty | 0:10ce458ab6fa | 81 | |
cdupaty | 0:10ce458ab6fa | 82 | return 1; |
cdupaty | 0:10ce458ab6fa | 83 | } |
cdupaty | 0:10ce458ab6fa | 84 | |
cdupaty | 0:10ce458ab6fa | 85 | void RGBMatrixClass::end() |
cdupaty | 0:10ce458ab6fa | 86 | { |
cdupaty | 0:10ce458ab6fa | 87 | //pinMode(A3, INPUT); |
cdupaty | 0:10ce458ab6fa | 88 | //pinMode(A4, INPUT); |
cdupaty | 0:10ce458ab6fa | 89 | |
cdupaty | 0:10ce458ab6fa | 90 | //SPI_MATRIX.end(); |
cdupaty | 0:10ce458ab6fa | 91 | |
cdupaty | 0:10ce458ab6fa | 92 | ArduinoGraphics::end(); |
cdupaty | 0:10ce458ab6fa | 93 | } |
cdupaty | 0:10ce458ab6fa | 94 | |
cdupaty | 0:10ce458ab6fa | 95 | |
cdupaty | 0:10ce458ab6fa | 96 | void RGBMatrixClass::brightness(uint8_t brightness) |
cdupaty | 0:10ce458ab6fa | 97 | { |
cdupaty | 0:10ce458ab6fa | 98 | if (brightness != 0 && brightness < 8) { |
cdupaty | 0:10ce458ab6fa | 99 | brightness = 8; |
cdupaty | 0:10ce458ab6fa | 100 | } |
cdupaty | 0:10ce458ab6fa | 101 | |
cdupaty | 0:10ce458ab6fa | 102 | // scale the brightness from: 0 - 255 to 0 - 31 |
cdupaty | 0:10ce458ab6fa | 103 | brightness = 0xe0 | (brightness >> 3); |
cdupaty | 0:10ce458ab6fa | 104 | |
cdupaty | 0:10ce458ab6fa | 105 | for (int i = 0; i < (RGB_MATRIX_WIDTH * RGB_MATRIX_HEIGHT); i++) { |
cdupaty | 0:10ce458ab6fa | 106 | _buffer[4 + i * 4] = brightness; |
cdupaty | 0:10ce458ab6fa | 107 | } |
cdupaty | 0:10ce458ab6fa | 108 | |
cdupaty | 0:10ce458ab6fa | 109 | //SPI_MATRIX.transfer(_buffer, sizeof(_buffer)); |
cdupaty | 0:10ce458ab6fa | 110 | for(int i=0;i<sizeof(_buffer);i++) |
cdupaty | 0:10ce458ab6fa | 111 | { |
cdupaty | 0:10ce458ab6fa | 112 | SPI_MATRIX.write(_buffer[i]); |
cdupaty | 0:10ce458ab6fa | 113 | } |
cdupaty | 0:10ce458ab6fa | 114 | } |
cdupaty | 0:10ce458ab6fa | 115 | |
cdupaty | 0:10ce458ab6fa | 116 | void RGBMatrixClass::beginDraw() |
cdupaty | 0:10ce458ab6fa | 117 | { |
cdupaty | 0:10ce458ab6fa | 118 | ArduinoGraphics::beginDraw(); |
cdupaty | 0:10ce458ab6fa | 119 | } |
cdupaty | 0:10ce458ab6fa | 120 | |
cdupaty | 0:10ce458ab6fa | 121 | void RGBMatrixClass::endDraw() |
cdupaty | 0:10ce458ab6fa | 122 | { |
cdupaty | 0:10ce458ab6fa | 123 | ArduinoGraphics::endDraw(); |
cdupaty | 0:10ce458ab6fa | 124 | |
cdupaty | 0:10ce458ab6fa | 125 | //SPI_MATRIX.transfer(_buffer, sizeof(_buffer)); |
cdupaty | 0:10ce458ab6fa | 126 | for(int i=0;i<sizeof(_buffer);i++) |
cdupaty | 0:10ce458ab6fa | 127 | { |
cdupaty | 0:10ce458ab6fa | 128 | SPI_MATRIX.write(_buffer[i]); |
cdupaty | 0:10ce458ab6fa | 129 | } |
cdupaty | 0:10ce458ab6fa | 130 | } |
cdupaty | 0:10ce458ab6fa | 131 | |
cdupaty | 0:10ce458ab6fa | 132 | void RGBMatrixClass::set(int x, int y, uint8_t r, uint8_t g, uint8_t b) |
cdupaty | 0:10ce458ab6fa | 133 | { |
cdupaty | 0:10ce458ab6fa | 134 | if (x < 0 || x >= RGB_MATRIX_WIDTH || y < 0 || y >= RGB_MATRIX_HEIGHT) { |
cdupaty | 0:10ce458ab6fa | 135 | return; |
cdupaty | 0:10ce458ab6fa | 136 | } |
cdupaty | 0:10ce458ab6fa | 137 | |
cdupaty | 0:10ce458ab6fa | 138 | int n = y * RGB_MATRIX_WIDTH + x; |
cdupaty | 0:10ce458ab6fa | 139 | |
cdupaty | 0:10ce458ab6fa | 140 | _buffer[5 + n * 4] = GAMMA_TABLE[b]; |
cdupaty | 0:10ce458ab6fa | 141 | _buffer[6 + n * 4] = GAMMA_TABLE[g]; |
cdupaty | 0:10ce458ab6fa | 142 | _buffer[7 + n * 4] = GAMMA_TABLE[r]; |
cdupaty | 0:10ce458ab6fa | 143 | } |
cdupaty | 0:10ce458ab6fa | 144 | |
cdupaty | 0:10ce458ab6fa | 145 | RGBMatrixClass MATRIX; |