Skip to content

Commit

Permalink
Add cube control, programs, fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsbrenkman committed Jan 14, 2024
1 parent bd847f6 commit 1712844
Show file tree
Hide file tree
Showing 16 changed files with 1,462 additions and 129 deletions.
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"C_Cpp.default.configurationProvider": "particle.particle-vscode-core",
"files.associations": {
"*.ino": "cpp",
"chrono": "cpp"
"chrono": "cpp",
"array": "cpp",
"locale": "cpp",
"string": "cpp",
"string_view": "cpp",
"vector": "cpp"
},
"particle.targetDevice": "LedCube1",
"particle.firmwareVersion": "3.3.1",
Expand Down
10 changes: 0 additions & 10 deletions src/Constants.h

This file was deleted.

37 changes: 0 additions & 37 deletions src/CubeController.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/CubeController.h

This file was deleted.

55 changes: 0 additions & 55 deletions src/LedCube.cpp

This file was deleted.

47 changes: 39 additions & 8 deletions src/LedCube.ino
Original file line number Diff line number Diff line change
@@ -1,47 +1,78 @@
/*
/*
* Project myProject
* Author: Your Name
* Date:
* Date:
* For comprehensive documentation and examples, please visit:
* https://docs.particle.io/firmware/best-practices/firmware-template/
*/

// Include Particle Device OS APIs
#include "Particle.h"
// #include "application.h"
#include "Constants.h"
#include "CubeController.h"
#include "constants.h"
#include "cube_controller.h"
#include "dogica.h"
#include "font.h"

// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);

// Run the application and system concurrently in separate threads
SYSTEM_THREAD(ENABLED);

LEDCUBE_NAMESPACE_BEGIN

// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

CubeController * cube_controller;
CubeController *cube_controller;

system_tick_t last_tick;

// setup() runs once, when the device is first turned on
void setup() {

Serial.begin(9600);

pinMode(LED_DATA_PIN, OUTPUT);
pinMode(LED_DATA_PIN, OUTPUT);
pinMode(LED_CLOCK_PIN, OUTPUT);
pinMode(LED_LATCH_PIN, OUTPUT);

cube_controller = new CubeController();
cube_controller->update({0b01010101, ALL_BITS, ALL_BITS}, CLEAR);

delay(5900);

delay(500);
font_glyph_dsc_t glyph = {};
draw_buf_t buffer = {};
font_get_glyph_dsc(&dogica, &glyph, 0x0047);
font_get_glyph_bitmap(&glyph, &buffer);

Serial.printlnf("Found letter width %d", buffer.size);
for (int i = 0; i < 8; i++) {
Serial.print("//");
for (int j = 0; j < buffer.size; j++) {
if (bit_check(buffer.bitmap[j], i)) {
Serial.print(" #");
} else {
Serial.print(" .");
}
}
Serial.println("");
}
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {

cube_controller->refresh();

system_tick_t now = millis();

if (now - last_tick > 500) {
last_tick = now;
cube_controller->update(ALL_CUBE, TOGGLE);
}
}

LEDCUBE_NAMESPACE_END
17 changes: 17 additions & 0 deletions src/abstract_program.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "abstract_program.h"

LEDCUBE_NAMESPACE_BEGIN

void AbstractProgram::init(CubeController *cube_controller_p) {
cube_controller_p = cube_controller_p;
}

void AbstractProgram::loop() {
system_tick_t now = millis();
if (now - last_tick > 500) {
last_tick = now;
tick();
}
}

LEDCUBE_NAMESPACE_END
21 changes: 21 additions & 0 deletions src/abstract_program.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef AbstractProgram_H
#define AbstractProgram_H

#include "cube_controller.h"

LEDCUBE_NAMESPACE_BEGIN

class AbstractProgram {
public:
void init(CubeController *cube_controller_p);
void loop();
virtual void tick();

protected:
CubeController *cube_controller_p;
system_tick_t last_tick;
};

LEDCUBE_NAMESPACE_END

#endif
43 changes: 43 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef Constants_H
#define Constants_H

#ifndef LEDCUBE_NAMESPACE_BEGIN
#define LEDCUBE_NAMESPACE_BEGIN //namespace LedCube {
#define LEDCUBE_NAMESPACE_END //}
#endif

LEDCUBE_NAMESPACE_BEGIN

#define LED_CLOCK_PIN TX
#define LED_DATA_PIN RX
#define LED_LATCH_PIN A7

#define LED_CUBE_SIZE 8
#define REFRESH_RATE 1000

enum class LedCubeOperation { set, clear, toggle };

#define SET LedCubeOperation::set
#define TOGGLE LedCubeOperation::toggle
#define CLEAR LedCubeOperation::clear

#define ALL_BITS 0xFF
#define ALL_CUBE \
{ ALL_BITS, ALL_BITS, ALL_BITS }

/*
* Bit operations
*/
#define bit_set(byte, nbit) ((byte) |= (1 << (nbit)))
#define bit_clear(byte, nbit) ((byte) &= ~(1 << (nbit)))
#define bit_flip(byte, nbit) ((byte) ^= (1 << (nbit)))
#define bit_check(byte, nbit) ((byte) & (1 << (nbit)))

#define idx_to_bit(idx) (uint8_t)(0x01 << idx)

#define write_to_register(out) \
shiftOut(LED_DATA_PIN, LED_CLOCK_PIN, MSBFIRST, out);

LEDCUBE_NAMESPACE_END

#endif
63 changes: 63 additions & 0 deletions src/cube_controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "cube_controller.h"

LEDCUBE_NAMESPACE_BEGIN

CubeController::CubeController() {
active_layer = -1;
update(ALL_CUBE, CLEAR);
}

void CubeController::refresh() {
unsigned long now = micros();
if (now - last_refresh < REFRESH_RATE) {
return;
}

last_refresh = now;
active_layer = (active_layer + 1) % LED_CUBE_SIZE;

digitalWrite(LED_LATCH_PIN, LOW);

// We need to write all 9 regissters in reverse order, starting with the layer register
write_to_register(idx_to_bit(active_layer))

// Then the 8 rows, in reverse order
for (int row = LED_CUBE_SIZE; row > 0; row--) {
write_to_register(led_cube[row - 1][active_layer]);
}

digitalWrite(LED_LATCH_PIN, HIGH);
}

bool CubeController::led(uint8_t x, uint8_t y, uint8_t z) {
byte row = led_cube[y][z];
return bit_check(row, x);
}

void CubeController::update(uint8_t x, uint8_t y, uint8_t z, LedCubeOperation operation) {
update({ idx_to_bit(x), idx_to_bit(y), idx_to_bit(z) }, operation);
}

void CubeController::update(CubeAddress address, LedCubeOperation operation) {
for (int z = 0; z < LED_CUBE_SIZE; z++) {
if (bit_check(address.z, z)) {
for (int y = 0; y < LED_CUBE_SIZE; y++) {
if (bit_check(address.y, y)) {
switch (operation) {
case LedCubeOperation::set:
led_cube[y][z] |= address.x;
break;
case LedCubeOperation::toggle:
led_cube[y][z] ^= address.x;
break;
case LedCubeOperation::clear:
led_cube[y][z] &= ~address.x;
break;
}
}
}
}
}
}

LEDCUBE_NAMESPACE_END
Loading

0 comments on commit 1712844

Please sign in to comment.