Hyelicht 2.0
Controller application for the Hyelicht shelf. Paint on the shelf with colors, turn on the fireplace mode, and more.
Loading...
Searching...
No Matches
ledstrip.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
3 * SPDX-FileCopyrightText: 2021-2022 Eike Hein <sho@eikehein.com>
4 */
5
6#pragma once
7
8#include <QColor>
9#include <QObject>
10#include <QQmlParserStatus>
11
12#include <linux/spi/spidev.h>
13
15
19#define LED_MAX_BRIGHTNESS 0x1F
20
22
44class LedStrip : public QObject, public QQmlParserStatus
45{
46 Q_OBJECT
47 Q_INTERFACES(QQmlParserStatus)
48
49
59 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
60
62
69 Q_PROPERTY(QString deviceName READ deviceName WRITE setDeviceName NOTIFY deviceNameChanged)
70
72
79 Q_PROPERTY(int frequency READ frequency WRITE setFrequency NOTIFY frequencyChanged)
80
82
88 Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
89
91
97 Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)
98
100
118
120
129 Q_PROPERTY(qreal gamma READ gamma WRITE setGamma NOTIFY gammaChanged)
130
132
152
154
160 Q_PROPERTY(bool canRestore READ canRestore NOTIFY canRestoreChanged)
161
162 public:
168 Q_DECLARE_FLAGS(RestoreOptions, RestoreOption)
169 Q_FLAG(RestoreOptions)
170
171
177 explicit LedStrip(QObject *parent = nullptr);
178
180
184 explicit LedStrip(int count, QObject *parent = nullptr);
185
187
190 ~LedStrip() override;
191
193
199 bool enabled() const;
200
202
207 void setEnabled(bool enabled);
208
210
217 QString deviceName() const;
218
220
226 void setDeviceName(const QString &deviceName);
227
229
236 int frequency() const;
237
239
245 void setFrequency(int frequency);
246
248
255 bool connected() const;
256
258
264 int count() const;
265
267
272 void setCount(int leds);
273
275
290 bool gammaCorrection() const;
291
293
311
313
320 qreal gamma() const;
321
323
331 void setGamma(qreal gamma);
332
334
347 bool hsvBrightness() const;
348
350
369
371
377 Q_INVOKABLE bool setLed(int index, const QColor &color, int brightness);
378
380
387 Q_INVOKABLE bool fill(int first, int last, const QColor &color, int brightness = LED_MAX_BRIGHTNESS);
388
390
394 Q_INVOKABLE QColor color(int index) const;
395
397
402 Q_INVOKABLE QColor colorAverage(int first, int last) const;
403
405
410 Q_INVOKABLE bool setColor(int index, const QColor &color);
411
413
419 Q_INVOKABLE bool setColor(int first, int last, const QColor &color);
420
422
426 Q_INVOKABLE int brightness(int index) const;
427
429
434 Q_INVOKABLE int brightnessAverage(int first, int last) const;
435
437
442 Q_INVOKABLE bool setBrightness(int index, int brightness);
443
445
451 Q_INVOKABLE bool setBrightness(int first, int last, int brightness);
452
454
457 Q_INVOKABLE bool reverse();
458
460
465 Q_INVOKABLE bool clear();
466
468
475 Q_INVOKABLE bool clear(int first, int last);
476
478
487 Q_INVOKABLE bool show();
488
490
496 Q_INVOKABLE void save();
497
499
505 Q_INVOKABLE void forgetSavedData();
506
508
515 Q_INVOKABLE bool canRestore() const;
516
518
527 Q_INVOKABLE bool restore(RestoreOptions options);
528
530 void classBegin() override;
532 void componentComplete() override;
533
534 Q_SIGNALS:
536
540 void enabledChanged() const;
541
543
548
550
554 void frequencyChanged() const;
555
557
564
566
570 void countChanged() const;
571
573
578
580
585
587
592
594
601
602 private:
603 void connect();
604 void disconnect();
605 void updateData(int count);
606 void updateLut();
607 void clearInternal(uint32_t *data, int first, int last);
608
609 bool m_enabled;
610
611 QString m_deviceName;
612 int m_frequency;
613 int m_fd;
614 bool m_connected;
615
616 int m_count;
617
618 bool m_gammaCorrection;
619 long double m_gamma;
620 QByteArray m_lut;
621 uint32_t *m_gammaCorrectedData;
622
623 bool m_hsvBrightness;
624 uint32_t *m_brightnessCorrectedData;
625
626 uint8_t *m_header;
627 spi_ioc_transfer m_message[3];
628 uint8_t *m_footer;
629
630 uint32_t *m_data;
631 uint32_t *m_savedData;
632 int m_savedSize;
633
634 bool m_createdByQml;
635 bool m_complete;
636};
637
638Q_DECLARE_OPERATORS_FOR_FLAGS(LedStrip::RestoreOptions)
Connects to and performs painting operations on a strip of SK9822/APA102 LEDs.
Definition ledstrip.h:45
int frequency
Clock frequency in Hz used for SPI communication with the LEDs.
Definition ledstrip.h:79
Q_INVOKABLE QColor color(int index) const
Retrieves the color of a specific LED.
Definition ledstrip.cpp:278
Q_INVOKABLE bool reverse()
Reverse the LED strip data.
Definition ledstrip.cpp:483
RestoreOption
Used as parameters to restore to choose what saved strip state to restore.
Definition ledstrip.h:164
@ RestoreColor
Restore the color data from the saved strip state.
Definition ledstrip.h:165
@ RestoreBrightness
Restore the brightness data from the saved strip state.
Definition ledstrip.h:166
void frequencyChanged() const
The clock frequency in Hz used for SPI communication with the LEDs has changed.
bool enabled
Toggle SPI-based communication with the LED strip.
Definition ledstrip.h:59
void canRestoreChanged()
Whether there is saved strip data that can be restored has changed.
~LedStrip() override
Cleanup on destruction.
Definition ledstrip.cpp:57
void setGamma(qreal gamma)
Set the gamma correction value.
Definition ledstrip.cpp:190
void setDeviceName(const QString &deviceName)
Set the SPI device filename used to communicate with the LED strip.
Definition ledstrip.cpp:97
Q_INVOKABLE bool clear()
Clear the LED strip.
Definition ledstrip.cpp:509
void classBegin() override
Implements the QQmlParserStatus interface.
Definition ledstrip.cpp:658
void setGammaCorrection(bool gammaCorrection)
Turn gamma correction on or off.
Definition ledstrip.cpp:168
void setFrequency(int frequency)
Set the clock frequency in Hz used for SPI communication with the LEDs.
Definition ledstrip.cpp:115
bool gammaCorrection
Toggle optional gamma correction using embedded LUT.
Definition ledstrip.h:117
bool connected
Whether there is an open SPI connection to the LED strip.
Definition ledstrip.h:88
int count
Number of LEDs in the strip.
Definition ledstrip.h:97
Q_INVOKABLE int brightness(int index) const
Retrieves the brightness of a specific LED.
Definition ledstrip.cpp:382
void deviceNameChanged()
The SPI device filename used to communicate with the LED strip has changed.
void setCount(int leds)
Set the number of LEDs in the strip.
Definition ledstrip.cpp:138
Q_INVOKABLE QColor colorAverage(int first, int last) const
Retrieves the average color of a range of LEDs.
Definition ledstrip.cpp:289
QString deviceName
SPI device filename used to communicate with the LED strip.
Definition ledstrip.h:69
Q_INVOKABLE bool fill(int first, int last, const QColor &color, int brightness=LED_MAX_BRIGHTNESS)
Change a range of LEDs.
Definition ledstrip.cpp:250
void hsvBrightnessChanged()
Whether brightness is based on color HSV value components has changed.
void gammaChanged()
The gamma correction value has changed.
Q_INVOKABLE bool restore(RestoreOptions options)
Restore saved strip data if available.
Definition ledstrip.cpp:624
void componentComplete() override
Implements the QQmlParserStatus interface.
Definition ledstrip.cpp:663
Q_INVOKABLE void save()
Save current strip state for later restoration.
Definition ledstrip.cpp:594
Q_INVOKABLE bool setBrightness(int index, int brightness)
Set the brightness of a specific LED.
Definition ledstrip.cpp:440
Q_INVOKABLE bool show()
Write latest state to the LED strip.
Definition ledstrip.cpp:533
void countChanged() const
The number of LEDs in the strip has changed.
void gammaCorrectionChanged()
Whether gamma correction is turned on has changed.
Q_INVOKABLE void forgetSavedData()
Forget saved strip data.
Definition ledstrip.cpp:612
bool canRestore
Whether there is saved strip state that can be restored by calling restore().
Definition ledstrip.h:160
void setHsvBrightness(bool hsvBrightness)
Set whether brightness is based on color HSV value components.
Definition ledstrip.cpp:212
bool hsvBrightness
Toggle brightness based on color HSV value component.
Definition ledstrip.h:151
Q_INVOKABLE int brightnessAverage(int first, int last) const
Retrieves the average brightness of a range of LEDs.
Definition ledstrip.cpp:393
Q_INVOKABLE bool setLed(int index, const QColor &color, int brightness)
Changes a specific LED.
Definition ledstrip.cpp:229
void connectedChanged()
Whether there is an open SPI connection to the LED strip has changed.
void setEnabled(bool enabled)
Turn SPI-based communication with the LED strip on or off.
Definition ledstrip.cpp:77
Q_INVOKABLE bool setColor(int index, const QColor &color)
Set the color of a specific LED.
Definition ledstrip.cpp:345
void enabledChanged() const
SPI-based communication with the LED strip has turned on or off.
qreal gamma
Gamma correction value.
Definition ledstrip.h:129
#define LED_MAX_BRIGHTNESS
Definition ledstrip.h:19