Skip to content

Commit

Permalink
real cube
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsbrenkman committed Jan 16, 2024
1 parent eda248f commit e4b040b
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 118 deletions.
44 changes: 14 additions & 30 deletions src/LedCube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
// Include Particle Device OS APIs
#include "Particle.h"
#include "abstract_program.h"
#include "blink.h"
#include "constants.h"
#include "cube_controller.h"
#include "dogica.h"
#include "font.h"
#include "singletons.h"
#include "ticker.h"

// Let Device OS manage the connection to the Particle Cloud
void setup();
void loop();
#line 19 "/Users/nils/Projects/LedCube/src/LedCube.ino"
#line 21 "/Users/nils/Projects/LedCube/src/LedCube.ino"
SYSTEM_MODE(AUTOMATIC);

// Run the application and system concurrently in separate threads
Expand All @@ -35,7 +37,7 @@ LEDCUBE_NAMESPACE_BEGIN
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

CubeController *cube_controller;
// CubeController *cube_controller_p = &CubeController::cube_controller();

AbstractProgram *program;

Expand All @@ -50,48 +52,30 @@ void setup() {
pinMode(LED_CLOCK_PIN, OUTPUT);
pinMode(LED_LATCH_PIN, OUTPUT);

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

// (CubeController::cube_controller()).print_to_serial();
update({ALL_BITS, ALL_BITS, ALL_BITS}, CLEAR);

delay(500);

program = new Ticker();
program->init(cube_controller);
program = new Blink();

delay(500);
// cube_controller->update({0b01010101, 0b00110011, 0b00001111}, SET);

// 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("");
// }
delay(500);
}

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

cube_controller->refresh();
// cube_controller_p->refresh();

program->loop();

// system_tick_t now = millis();
// cube_controller_p->print_to_serial();

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

LEDCUBE_NAMESPACE_END
48 changes: 19 additions & 29 deletions src/LedCube.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
// Include Particle Device OS APIs
#include "Particle.h"
#include "abstract_program.h"
#include "blink.h"
#include "constants.h"
#include "cube_controller.h"
#include "dogica.h"
#include "font.h"
#include "singletons.h"
#include "ticker.h"

// Let Device OS manage the connection to the Particle Cloud
Expand All @@ -27,7 +29,7 @@ LEDCUBE_NAMESPACE_BEGIN
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

CubeController *cube_controller;
// CubeController *cube_controller_p = &CubeController::cube_controller();

AbstractProgram *program;

Expand All @@ -42,48 +44,36 @@ void setup() {
pinMode(LED_CLOCK_PIN, OUTPUT);
pinMode(LED_LATCH_PIN, OUTPUT);

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

// (CubeController::cube_controller()).print_to_serial();
update({ALL_BITS, ALL_BITS, ALL_BITS}, CLEAR);

delay(500);

program = new Ticker();
program->init(cube_controller);
update({ 0b10101010, ALL_BITS, ALL_BITS}, SET);
update({ ALL_BITS, 0b01010101, ALL_BITS}, TOGGLE);
update({ ALL_BITS, ALL_BITS, 0b01010101}, TOGGLE);

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("");
// }
program = new Blink();


// cube_controller->update({0b01010101, 0b00110011, 0b00001111}, SET);

delay(500);
}

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

cube_controller->refresh();
refresh();

program->loop();

// system_tick_t now = millis();
// cube_controller_p->print_to_serial();

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

LEDCUBE_NAMESPACE_END
5 changes: 0 additions & 5 deletions src/abstract_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

LEDCUBE_NAMESPACE_BEGIN

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

void AbstractProgram::loop() {
if (next_tick < millis()) {
next_tick = millis() + frequency();
Expand Down
4 changes: 1 addition & 3 deletions src/abstract_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ LEDCUBE_NAMESPACE_BEGIN

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

protected:
virtual void tick();
virtual int frequency();

CubeController *cube_controller_p;
uint32_t next_tick;
uint32_t next_tick = 0;
};

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

LEDCUBE_NAMESPACE_BEGIN

Blink::Blink() {
//
}

void Blink::tick() {
Log.info("toggle");
print_to_serial();
update(ALL_CUBE, TOGGLE);
}

int Blink::frequency() { return 1000; }

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

#include "Particle.h"
#include "abstract_program.h"
#include "abstract_program.h"
#include "cube_types.h"
#include "singletons.h"

LEDCUBE_NAMESPACE_BEGIN

class Blink : public AbstractProgram {
public:
Blink();

protected:
void tick();
int frequency();

};

LEDCUBE_NAMESPACE_END

#endif
19 changes: 12 additions & 7 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,33 @@

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

#include "cube_types.h"

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
static const uint8_t LED_CUBE_SIZE = 8;
static const uint16_t 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 NO_BITS 0x00
#define ALL_CUBE \
{ ALL_BITS, ALL_BITS, ALL_BITS }
static const uint8_t ALL_BITS = 0xFF;
static const uint8_t NO_BITS = 0x00;
static const cube_address_t ALL_CUBE = {ALL_BITS, ALL_BITS, ALL_BITS};

static const uint8_t BIT_RANGE[] = {(uint8_t)0, (uint8_t)1, (uint8_t)2,
(uint8_t)3, (uint8_t)4, (uint8_t)5,
(uint8_t)6, (uint8_t)7};

/*
* Bit operations
Expand Down
48 changes: 26 additions & 22 deletions src/cube_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,53 @@

LEDCUBE_NAMESPACE_BEGIN

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

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

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

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));
write_to_register(0x01 << active_layer);
// Serial.printf("%X\n", 0x01 << 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]);
for (int y = 3; y > 0; y--) {
write_to_register(led_cube.z[active_layer].y[y - 1].x);
// Serial.printf("%X\n", led_cube.z[active_layer].y[y - 1].x);
}

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);
bool led(uint8_t x, uint8_t y, uint8_t z) {
return bit_check(led_cube.z[z].y[y].x, x);
}

void CubeController::update(uint8_t x, uint8_t y, uint8_t z,
LedCubeOperation operation) {
void 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(cube_address_t address,
LedCubeOperation operation) {
for (int z = 0; z < LED_CUBE_SIZE; z++) {
void update(cube_address_t address, LedCubeOperation operation) {
for (uint8_t z : BIT_RANGE) {
if (bit_check(address.z, z)) {
for (int y = 0; y < LED_CUBE_SIZE; y++) {
for (uint8_t y : BIT_RANGE) {
if (bit_check(address.y, y)) {
switch (operation) {
case LedCubeOperation::set:
led_cube[y][z] |= address.x;
led_cube.z[z].y[y].x |= address.x;
break;
case LedCubeOperation::toggle:
led_cube[y][z] ^= address.x;
led_cube.z[z].y[y].x ^= address.x;
break;
case LedCubeOperation::clear:
led_cube[y][z] &= ~address.x;
led_cube.z[z].y[y].x &= ~address.x;
break;
}
}
Expand All @@ -63,4 +57,14 @@ void CubeController::update(cube_address_t address,
}
}

void print_to_serial() {
for (uint8_t z : BIT_RANGE) {
Serial.printf("%u | ", z);
for (uint8_t y : BIT_RANGE) {
Serial.printf("%X ", led_cube.z[z].y[y].x);
}
Serial.print("|\n");
}
}

LEDCUBE_NAMESPACE_END
Loading

0 comments on commit e4b040b

Please sign in to comment.