summaryrefslogtreecommitdiff
path: root/src/Colours.cpp
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2025-03-14 09:20:28 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2025-03-14 09:20:28 +0100
commit38b9b25d4612298f5599e7471568714ea1fa2a01 (patch)
tree1ad152e92ab481fe63ad75e12eccba9292cbf62b /src/Colours.cpp
parent8f34001c8a2eec5594bee45f8429622b377de084 (diff)
Moved arduino version in its own directory
Diffstat (limited to 'src/Colours.cpp')
-rw-r--r--src/Colours.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/Colours.cpp b/src/Colours.cpp
new file mode 100644
index 0000000..7ae49a2
--- /dev/null
+++ b/src/Colours.cpp
@@ -0,0 +1,86 @@
+/*
+ * Colours - library for managing the colours on RGB LEDs
+ * Copyright 2009, 2013 Elena Grandi
+ *
+ * This file is part of Colours.
+ *
+ * Colours is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * Colours is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Colours. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "Arduino.h"
+#include "Colours.h"
+
+Colours::Colours(int rPin,int gPin,int bPin) {
+ _initPINs(rPin,gPin,bPin);
+ _invert = false;
+}
+
+Colours::Colours(int rPin,int gPin,int bPin,bool invert) {
+ _initPINs(rPin,gPin,bPin);
+ _invert = invert;
+}
+
+void Colours::_initPINs(int rPin,int gPin,int bPin) {
+ _rPin = rPin;
+ _gPin = gPin;
+ _bPin = bPin;
+ pinMode(_rPin,OUTPUT);
+ pinMode(_gPin,OUTPUT);
+ pinMode(_bPin,OUTPUT);
+}
+
+void Colours::writeRGB(unsigned char r,unsigned char g,unsigned char b) {
+ if (_invert) {
+ analogWrite(_rPin, 255-r);
+ analogWrite(_gPin, 255-g);
+ analogWrite(_bPin, 255-b);
+ } else {
+ analogWrite(_rPin, r);
+ analogWrite(_gPin, g);
+ analogWrite(_bPin, b);
+ }
+}
+
+void Colours::writeHSV(unsigned int h,unsigned char s,unsigned char v) {
+ h = h % 360;
+ if (s==0) {
+ writeRGB(v,v,v);
+ } else {
+ unsigned int f,p,q,t;
+ f = 256*(h%60)/60;
+ p = v*(256-s)/256;
+ q = v*(256-f*s/256)/256;
+ t = v*(256-s*(256-f)/256)/256;
+ switch( (h/60) % 6 ) {
+ case 0:
+ writeRGB(v,t,p);
+ break;
+ case 1:
+ writeRGB(q,v,p);
+ break;
+ case 2:
+ writeRGB(p,v,t);
+ break;
+ case 3:
+ writeRGB(p,q,v);
+ break;
+ case 4:
+ writeRGB(t,p,v);
+ break;
+ case 5:
+ writeRGB(v,p,q);
+ break;
+ }
+ }
+}