Skip to content

Update MKR RGB lib to enable use on the Portenta H7. (A3->A5) #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ Allows you to draw on your MKR RGB shield. Depends on the ArduinoGraphics librar

For more information about this library please visit us at https://www.arduino.cc/en/Reference/ArduinoMKRRGB

Updated: Works with Portenta H7 - one jumper needed - A3 to A5. A4 can be used as-is (after pinMode change).
#define USING_PORTENTA_H7 - make sure this is uncommented in MKRRGBMatrix.h to use the MKR RGB shield in 'bit-bang' mode.
== License ==

Copyright (c) 2019 Arduino SA. All rights reserved.
Copyright (c) 2020 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down
46 changes: 42 additions & 4 deletions src/MKRRGBMatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of the Arduino_MKRRGB library.
Copyright (c) 2019 Arduino SA. All rights reserved.
Copyright (c) 2020 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -22,7 +22,14 @@

#include "MKRRGBMatrix.h"

#ifndef USING_PORTENTA_H7
static SPIClass SPI_MATRIX(&sercom0, A3, A4, A3, SPI_PAD_0_SCK_1, SERCOM_RX_PAD_0);
#else
#define DATA_PIN A5 // Note - A3 is jumpered to A5 - A3 is input
#define CLOCK_PIN A4
uint16_t loop_var = 0;
#endif


// This table is based on the formula: gamma = (int)(pow(i / 255.0, gamma) * 255 + offset)
// where gamma = 2.5 and offset is 0.5
Expand Down Expand Up @@ -68,29 +75,46 @@ int RGBMatrixClass::begin()
memset(_buffer, 0x00, 4 + 4 * RGB_MATRIX_WIDTH * RGB_MATRIX_HEIGHT);
memset(_buffer + 4 + 4 * RGB_MATRIX_WIDTH * RGB_MATRIX_HEIGHT, 0xff, sizeof(_buffer) - (4 + 4 * RGB_MATRIX_WIDTH * RGB_MATRIX_HEIGHT));

#ifndef USING_PORTENTA_H7
SPI_MATRIX.begin();
SPI_MATRIX.beginTransaction(SPISettings(12e6, MSBFIRST, SPI_MODE0));

pinPeripheral(A3, PIO_SERCOM_ALT);
pinPeripheral(A4, PIO_SERCOM_ALT);

#else
// pinPeripheral(A4, PIO_OUTPUT); // when pinPeripheral() done.
// pinPeripheral(A5, PIO_OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);

#endif

brightness(127);

return 1;
}

void RGBMatrixClass::end()
{

#ifndef USING_PORTENTA_H7
pinMode(A3, INPUT);
pinMode(A4, INPUT);

SPI_MATRIX.end();
#else
pinMode(A5, INPUT);
pinMode(A4, INPUT);

#endif
ArduinoGraphics::end();
}

void RGBMatrixClass::brightness(uint8_t brightness)
{
#ifdef USING_PORTENTA_H7
uint16_t k = 0;
#endif
if (brightness != 0 && brightness < 8) {
brightness = 8;
}
Expand All @@ -101,8 +125,13 @@ void RGBMatrixClass::brightness(uint8_t brightness)
for (int i = 0; i < (RGB_MATRIX_WIDTH * RGB_MATRIX_HEIGHT); i++) {
_buffer[4 + i * 4] = brightness;
}

#ifndef USING_PORTENTA_H7
SPI_MATRIX.transfer(_buffer, sizeof(_buffer));
#else
loop_var = sizeof(_buffer);
for ( k = 0; k < loop_var; k++)
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, (_buffer[k]));
#endif
}

void RGBMatrixClass::beginDraw()
Expand All @@ -112,9 +141,18 @@ void RGBMatrixClass::beginDraw()

void RGBMatrixClass::endDraw()
{
#ifdef USING_PORTENTA_H7
uint16_t k = 0;
#endif
ArduinoGraphics::endDraw();

#ifndef USING_PORTENTA_H7
SPI_MATRIX.transfer(_buffer, sizeof(_buffer));
#else
loop_var = sizeof(_buffer);
for ( k = 0; k < loop_var; k++)
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, (_buffer[k]));

#endif
}

void RGBMatrixClass::set(int x, int y, uint8_t r, uint8_t g, uint8_t b)
Expand Down
6 changes: 5 additions & 1 deletion src/MKRRGBMatrix.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of the Arduino_MKRRGB library.
Copyright (c) 2019 Arduino SA. All rights reserved.
Copyright (c) 2020 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -25,6 +25,10 @@
#define RGB_MATRIX_WIDTH 12
#define RGB_MATRIX_HEIGHT 7

// the following is for bit-bang of A4 and A3 jumpered to A5
// comment if not using the Portenta H7
// #define USING_PORTENTA_H7

class RGBMatrixClass : public ArduinoGraphics {
public:
RGBMatrixClass();
Expand Down