Repository navigation
Expand file tree
/
Copy pathQub.cpp
More file actions
197 lines (176 loc) · 3.94 KB
/
Copy pathQub.cpp
File metadata and controls
197 lines (176 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// Qub.cpp
// Qub2 – 3D LED Cube Library
//
// Created by Janik Schmidt on 18.10.18
// Copyright © 2018 Team FESTIVAL. All rights reserved.
//
#include "Qub.h"
static int columns[] = { 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A };
const int layers[] = { 0x0B, 0x0C, 0x0D };
const int sets[][9] = {
{ 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 10, 9, 8, 7, 6, 5, 4, 3, 2 },
{ 4, 7, 10, 3, 6, 9, 2, 5, 8 },
{ 8, 5, 2, 9, 6, 3, 10, 7, 4 },
{ 2, 3, 4, 7, 10, 9, 8, 5, 6 },
{ 6, 3, 4, 7, 10, 9, 8, 5, 2 },
};
/**
* @brief Setup Qub and configure the required pins
*
*/
void Qub::setup() {
// Configure columns as output
for (int c: columns) {
pinMode(c, OUTPUT);
}
// Configure layers as output
for (int l: layers) {
pinMode(l, OUTPUT);
}
}
/**
* @brief Changes the pin definition order by selecting a new set
*
* @param set The set index to select
*/
void Qub::changeSet(int set) {
// Apply each number from the specified set to the columns array
for (int i = 0; i < 9; i++) {
columns[i] = sets[set][i];
}
}
/**
* @brief Enables one of the nine columns of the cube
*
* @param column The column of the cube to enable
*/
void Qub::enableColumn(int column) {
// Check if we have a valid value
if (Qub::inRange(column, 1, 9)) {
// Set the column to HIGH
digitalWrite(columns[column - 1], HIGH);
}
}
/**
* @brief Disables one of the nine columns of the cube
*
* @param column The column of the cube to disable
*/
void Qub::disableColumn(int column) {
// Check if we have a valid value
if (Qub::inRange(column, 1, 9)) {
// Set the column to LOW
digitalWrite(columns[column - 1], LOW);
}
}
/**
* @brief Enables all columns at once
*
*/
void Qub::enableColumns() {
for (int c: columns) {
digitalWrite(c, HIGH);
}
}
/**
* @brief Disables all columns at once
*
*/
void Qub::disableColumns() {
for (int c: columns) {
digitalWrite(c, LOW);
}
}
/**
* @brief Enables one of the three layers of the cube by switching a transistor
*
* @param column The layer of the cube to enable
*/
void Qub::enableLayer(int layer) {
// Check if we have a valid value
if (Qub::inRange(layer, 1, 3)) {
// Set the layer to HIGH
digitalWrite(layers[layer - 1], HIGH);
}
}
/**
* @brief Disables one of the three layers of the cube by switching a transistor
*
* @param column The layer of the cube to disable
*/
void Qub::disableLayer(int layer) {
// Check if we have a valid value
if (Qub::inRange(layer, 1, 3)) {
// Set the layer to LOW
digitalWrite(layers[layer - 1], LOW);
}
}
/**
* @brief Enables all layers at once
*
*/
void Qub::enableLayers() {
for (int l: layers) {
digitalWrite(l, HIGH);
}
}
/**
* @brief Disables all layers at once
*
*/
void Qub::disableLayers() {
for (int l: layers) {
digitalWrite(l, LOW);
}
}
/**
* @brief Enables all columns and layers at once
*
*/
void Qub::enableLEDs() {
Qub::enableLayers();
Qub::enableColumns();
}
/**
* @brief Disables all columns and layers at once
*
*/
void Qub::disableLEDs() {
Qub::disableLayers();
Qub::disableColumns();
}
/**
* @brief Enables a single LED in a specified column, disabling any other LED
*
* @param column The column to enable
* @param layer The layer to enable
*/
void Qub::enableLEDInLayer(int column, int layer) {
Qub::disableLEDs();
Qub::enableColumn(column);
Qub::enableLayer(layer);
}
/**
* @brief Enables any LED in a specified layer, disabling any other layer
*
* @param layer
*/
void Qub::enableLEDsInLayer(int layer) {
Qub::disableLEDs();
Qub::enableColumns();
Qub::enableLayer(layer);
}
/**
* @brief Checks if a value is in a defined range
*
* @param value The value to check
* @param min The lower bound of the range
* @param max The upper bound of the range
* @return true If the value is in between the defined range
* @return false If the value is outside the defined range
*/
bool Qub::inRange(int value, int min, int max) {
return (value >= min && value <= max);
}