[WIP] Add example peripheral IOLedMatrix
Simple WIP peripheral to use while developing the I/O capabilites of Ripes. The peripheral is a variable-size LED matrix, supporting 24-bit RGB color.
ADD
src/io/ioledmatrix.cpp
+#include "ioledmatrix.h"
+
+#include <QPainter>
+#include <QPen>
+
+namespace Ripes {
+
+IOLedMatrix::IOLedMatrix(QWidget* parent, uint32_t startAddr, unsigned maxWidth)
+ : IOBase(parent, startAddr), m_maxWidth(maxWidth) {
+ constexpr unsigned defaultWidth = 10;
+
+ // Parameters
+ m_parameters[WIDTH] = IOParam(WIDTH, "Matrix width", defaultWidth, true, 0, m_maxWidth);
+ m_parameters[SIZE] = IOParam(SIZE, "LED size", 25, true, 1, 100);
+
+ m_pen.setWidth(1);
+ m_pen.setColor(Qt::black);
+
+ updateLEDRegs();
+}
+
+const QVariant& IOLedMatrix::setParameter(unsigned ID, const QVariant& value) {
+ return QVariant();
+}
+
+uint32_t IOLedMatrix::ioRead8(uint32_t offset) {
+ return regRead(offset) & 0xFF;
+}
+uint32_t IOLedMatrix::ioRead16(uint32_t offset) {
+ return regRead(offset) & 0xFFFF;
+}
+uint32_t IOLedMatrix::ioRead32(uint32_t offset) {
+ return regRead(offset);
+}
+uint32_t IOLedMatrix::regRead(uint32_t offset) const {
+ return m_ledRegs.at(offset / 4) >> (offset % 4);
+}
+
+void IOLedMatrix::ioWrite8(uint32_t offset, uint32_t value) {}
+void IOLedMatrix::ioWrite16(uint32_t offset, uint32_t value) {}
+void IOLedMatrix::ioWrite32(uint32_t offset, uint32_t value) {}
+
+inline QColor regToColor(uint32_t regVal) {
+ return QColor(regVal >> 16 & 0xFF, regVal >> 8 & 0xFF, regVal & 0xFF);
+}
+
+void IOLedMatrix::updateLEDRegs() {
+ const int width = m_parameters[WIDTH].value.toInt();
+ m_ledRegs.resize(width * width);
+
+ // DEBUG: test pattern
+ for (int y = 0; y < width; y++) {
+ for (int x = 0; x < width; x++) {
+ const int r = (0xFF * x) / width;
+ const int g = (0xFF * y) / width;
+ const int b = (0xFF * (x + y)) / (width + width);
+
+ m_ledRegs.at(y * width + x) = r << 16 | g << 8 | b;
+ }
+ }
+}
+
+QSize IOLedMatrix::minimumSizeHint() const {
+ const int width = m_parameters.at(WIDTH).value.toInt();
+ const int size = m_parameters.at(SIZE).value.toInt();
+ const int pixelWidth = width * (size + 2);
+ return QSize(pixelWidth, pixelWidth);
+}
+
+void IOLedMatrix::paintEvent(QPaintEvent* event) {
+ QPainter painter(this);
+
+ painter.setPen(m_pen);
+
+ const int width = m_parameters[WIDTH].value.toInt();
+ const int size = m_parameters[SIZE].value.toInt();
+ for (int y = 0; y < width; y++) {
+ for (int x = 0; x < width; x++) {
+ QBrush brush(regToColor(m_ledRegs.at(y * width + x)));
+ painter.setBrush(brush);
+
+ const unsigned xpos = x * size + size / 2;
+ const unsigned ypos = y * size + size / 2;
+
+ painter.drawEllipse(xpos, ypos, size, size);
+ }
+ }
+
+ painter.end();
+}
+
+} // namespace Ripes

ADD
src/io/ioledmatrix.h
+#pragma once
+
+#include <QPen>
+#include <QVariant>
+#include <QWidget>
+
+#include "iobase.h"
+
+namespace Ripes {
+
+class IOLedMatrix : public IOBase {
+ Q_OBJECT
+
+ enum Parameters { WIDTH, SIZE };
+
+public:
+ IOLedMatrix(QWidget* parent, uint32_t startAddr, unsigned maxWidth);
+ ~IOLedMatrix(){};
+
+ uint32_t startAddr() const { return m_startAddr; }
+ virtual uint32_t size() const override { return (m_maxWidth * m_maxWidth) * 4; }
+ uint32_t endAddr() const { return startAddr() + size(); }
+
+ virtual const QVariant& setParameter(unsigned ID, const QVariant& value) override;
+
+ /**
+ * Hardware read/write functions
+ */
+ virtual uint32_t ioRead8(uint32_t offset) override;
+ virtual void ioWrite8(uint32_t offset, uint32_t value) override;
+ virtual uint32_t ioRead16(uint32_t offset) override;
+ virtual void ioWrite16(uint32_t offset, uint32_t value) override;
+ virtual uint32_t ioRead32(uint32_t offset) override;
+ virtual void ioWrite32(uint32_t offset, uint32_t value) override;
+
+protected:
+ /**
+ * QWidget drawing
+ */
+ void paintEvent(QPaintEvent* event) override;
+ QSize minimumSizeHint() const override;
+
+private:
+ uint32_t regRead(uint32_t offset) const;
+ void updateLEDRegs();
+
+ unsigned m_maxWidth;
+ std::vector<uint32_t> m_ledRegs;
+
+ QPen m_pen;
+};
+} // namespace Ripes

MODIFY
src/iotab.cpp
src/iotab.cpp
#include "processorhandler.h"
+// IO components
+#include "io/ioledmatrix.h"
+
namespace Ripes {
IOTab::IOTab(QToolBar* toolbar, QWidget* parent) : RipesTab(toolbar, parent), m_ui(new Ui::IOTab) {
m_ui->setupUi(this);
+
+ m_ui->mdiArea->addSubWindow(new IOLedMatrix(this, 0x10000, 25));
}
IOTab::~IOTab() {

[WIP] Add example peripheral IOLedMatrix

Simple WIP peripheral to use while developing the I/O capabilites of Ripes. The peripheral is a variable-size LED matrix, supporting 24-bit RGB color.