Version 4 - funktionerte wohl nicht
This commit is contained in:
parent
d55d2036ed
commit
4c21998682
12 changed files with 121 additions and 922 deletions
|
|
@ -4,11 +4,12 @@
|
||||||
#include "./Timer1.h"
|
#include "./Timer1.h"
|
||||||
#include "CubeRegister.h"
|
#include "CubeRegister.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
CubeRegister myRegister = CubeRegister();
|
CubeRegister myRegister = CubeRegister();
|
||||||
|
|
||||||
#define CUBESIZE 5
|
#define CUBESIZE 5
|
||||||
#define FPS 50
|
#define FPS 60
|
||||||
#define LEDLEVEL 16
|
#define LEDLEVEL 8
|
||||||
|
|
||||||
byte cube[CUBESIZE][CUBESIZE][CUBESIZE];
|
byte cube[CUBESIZE][CUBESIZE][CUBESIZE];
|
||||||
|
|
||||||
|
|
@ -54,46 +55,6 @@ void dimm_cube(int diff = -1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void cube_move_x(int diff) {
|
|
||||||
diff = constrain(diff, -CUBESIZE, CUBESIZE);
|
|
||||||
byte from;
|
|
||||||
for (byte v = 0; v < CUBESIZE; v++) {
|
|
||||||
from = (v + diff) % CUBESIZE;
|
|
||||||
for (byte a = 0; a < CUBESIZE; a++) {
|
|
||||||
for (byte b = 0; b < CUBESIZE; b++) {
|
|
||||||
cube[v][a][b] = cube[from][a][b];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void cube_move_y(int diff) {
|
|
||||||
diff = constrain(diff, -CUBESIZE, CUBESIZE);
|
|
||||||
byte from;
|
|
||||||
for (byte v = 0; v < CUBESIZE; v++) {
|
|
||||||
from = (v + diff) % CUBESIZE;
|
|
||||||
for (byte a = 0; a < CUBESIZE; a++) {
|
|
||||||
for (byte b = 0; b < CUBESIZE; b++) {
|
|
||||||
cube[a][v][b] = cube[a][from][b];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void cube_move_z(int diff) {
|
|
||||||
diff = constrain(diff, -CUBESIZE, CUBESIZE);
|
|
||||||
byte from;
|
|
||||||
for (byte v = 0; v < CUBESIZE; v++) {
|
|
||||||
from = (v + diff) % CUBESIZE;
|
|
||||||
for (byte a = 0; a < CUBESIZE; a++) {
|
|
||||||
for (byte b = 0; b < CUBESIZE; b++) {
|
|
||||||
cube[a][b][v] = cube[a][b][from];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void cube_rotate_cover(int steps, unsigned int frame_delay = 100) {
|
void cube_rotate_cover(int steps, unsigned int frame_delay = 100) {
|
||||||
byte x = 0;
|
byte x = 0;
|
||||||
byte y = 0;
|
byte y = 0;
|
||||||
|
|
|
||||||
|
|
@ -177,161 +177,5 @@ void cube_effect_dots(int duration = 0) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
void cube_life(int duration = 0) {
|
|
||||||
int speed = 50;
|
|
||||||
int value;
|
|
||||||
int min = LEDLEVEL * 2;
|
|
||||||
int max = LEDLEVEL * 4;
|
|
||||||
|
|
||||||
if (duration <= 0)
|
|
||||||
duration = random(MIN_DURATION, MAX_DURATION);
|
|
||||||
|
|
||||||
// mit zufallsmuster füllen
|
|
||||||
fill_cube(0);
|
|
||||||
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
if (random(100) >= 50) {
|
|
||||||
cube[x][y][z] = random(LEDLEVEL + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
effect_ende = millis() + duration * 1000;
|
|
||||||
|
|
||||||
while (millis() < effect_ende) {
|
|
||||||
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
value = 0;
|
|
||||||
value += cube[(x - 1) % CUBESIZE][y][z];
|
|
||||||
value += cube[(x + 1) % CUBESIZE][y][z];
|
|
||||||
value += cube[x][(y - 1) % CUBESIZE][z];
|
|
||||||
value += cube[x][(y + 1) % CUBESIZE][z];
|
|
||||||
value += cube[x][y][(z - 1) % CUBESIZE];
|
|
||||||
value += cube[x][y][(z + 1) % CUBESIZE];
|
|
||||||
if ((value < min) || (value >= max)) {
|
|
||||||
if (cube[x][y][z] > 0) {
|
|
||||||
cube[x][y][z]--;
|
|
||||||
};
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if (cube[x][y][z] < LEDLEVEL) {
|
|
||||||
cube[x][y][z]++;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(speed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Dot {
|
|
||||||
byte x;
|
|
||||||
byte y;
|
|
||||||
byte z;
|
|
||||||
int vx;
|
|
||||||
int vy;
|
|
||||||
int vz;
|
|
||||||
byte value;
|
|
||||||
};
|
|
||||||
|
|
||||||
void cube_moving_dots(int duration = 0) {
|
|
||||||
int speed = 100;
|
|
||||||
int dotcount = 2;
|
|
||||||
Dot dots[dotcount];
|
|
||||||
//Dot dot;
|
|
||||||
for (byte d = 0; d < dotcount; d++) {
|
|
||||||
dots[d].x = random(CUBESIZE) * 10;
|
|
||||||
dots[d].y = random(CUBESIZE) * 10;
|
|
||||||
dots[d].z = random(CUBESIZE) * 10;
|
|
||||||
dots[d].vx = random(20) - 10;
|
|
||||||
dots[d].vy = random(20) - 10;
|
|
||||||
dots[d].vz = random(20) - 10;
|
|
||||||
dots[d].value = random(LEDLEVEL / 2, LEDLEVEL);
|
|
||||||
}
|
|
||||||
// Serial.println("cube: moving "+String(dotcount)+" dots");
|
|
||||||
if (duration <= 0)
|
|
||||||
duration = random(MIN_DURATION, MAX_DURATION);
|
|
||||||
effect_ende = millis() + duration * 1000;
|
|
||||||
|
|
||||||
// mit zufallsmuster füllen
|
|
||||||
fill_cube(0);
|
|
||||||
while (millis() < effect_ende) {
|
|
||||||
// dimm_cube(LEDLEVEL/-4);
|
|
||||||
// fill_cube(0);
|
|
||||||
for (byte d = 0; d < dotcount; d++) {
|
|
||||||
|
|
||||||
cube[dots[d].x / 10][dots[d].y / 10][dots[d].z / 10] = 0;
|
|
||||||
|
|
||||||
if (((dots[d].x + dots[d].vx) <= 0) ||
|
|
||||||
((dots[d].x + dots[d].vx) >= CUBESIZE * 10))
|
|
||||||
dots[d].vx = -dots[d].vx;
|
|
||||||
if (((dots[d].y + dots[d].vy) <= 0) ||
|
|
||||||
((dots[d].y + dots[d].vy) >= CUBESIZE * 10))
|
|
||||||
dots[d].vy = -dots[d].vy;
|
|
||||||
if (((dots[d].z + dots[d].vz) <= 0) ||
|
|
||||||
((dots[d].z + dots[d].vz) >= CUBESIZE * 10))
|
|
||||||
dots[d].vz = -dots[d].vz;
|
|
||||||
|
|
||||||
dots[d].x = (dots[d].x + dots[d].vx);
|
|
||||||
dots[d].y = (dots[d].y + dots[d].vy);
|
|
||||||
dots[d].z = (dots[d].z + dots[d].vz);
|
|
||||||
|
|
||||||
// Serial.println("Dot
|
|
||||||
// ["+String(dot.x)+"]["+String(dot.y)+"]["+String(dot.z)+"] /
|
|
||||||
// ["+String((int)dot.vx)+"]["+String((int)dot.vy)+"]["+String((int)dot.vz)+"]
|
|
||||||
// = "+String(dot.value));
|
|
||||||
cube[dots[d].x / 10][dots[d].y / 10][dots[d].z / 10] = dots[d].value;
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(speed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void cube_shifting_layer(int duration = 0) {
|
|
||||||
int speed = 50;
|
|
||||||
int value,x,y;
|
|
||||||
fill_cube(0);
|
|
||||||
for (x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (y = 0; y < CUBESIZE; y++) {
|
|
||||||
cube[x][y][0] = LEDLEVEL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (duration <= 0)
|
|
||||||
duration = random(MIN_DURATION, MAX_DURATION);
|
|
||||||
effect_ende = millis() + duration * 1000;
|
|
||||||
|
|
||||||
while (millis() < effect_ende) {
|
|
||||||
value = random(4);
|
|
||||||
|
|
||||||
switch (value) {
|
|
||||||
case 0:
|
|
||||||
cube_move_x(1);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
cube_move_x(-1);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
cube_move_y(1);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
cube_move_y(-1);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
cube_move_z(1);
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
cube_move_z(-1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
delay(speed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1,53 +0,0 @@
|
||||||
#include "CubeRegister.h"
|
|
||||||
|
|
||||||
void CubeRegister::reset() {
|
|
||||||
// Port B Pin 8 - 13
|
|
||||||
//Serial.println("reset");
|
|
||||||
DDRB |= _SER; // _SER als Output
|
|
||||||
DDRB |= _OE; // _OE als Output
|
|
||||||
DDRB |= _RCLK; // _RCLK als Output
|
|
||||||
DDRB |= _SRCLK; // _SRCLK als Output
|
|
||||||
DDRB |= _SRCLR; // _SRCLR als Output
|
|
||||||
|
|
||||||
output_disable();
|
|
||||||
clear_register();
|
|
||||||
register_to_output();
|
|
||||||
// Serial.println(PORTB);
|
|
||||||
};
|
|
||||||
|
|
||||||
void CubeRegister::output_enable() {
|
|
||||||
// OUTPUT für OE auf 0 setzen
|
|
||||||
PORTB &= ~_OE; // -> LOW
|
|
||||||
};
|
|
||||||
void CubeRegister::output_disable() {
|
|
||||||
// OUTPUT für OE auf 1 setzen
|
|
||||||
PORTB |= _OE; //-> HIGH
|
|
||||||
};
|
|
||||||
void CubeRegister::clear_register() {
|
|
||||||
PORTB |= _SRCLR; // -> HIGH
|
|
||||||
PORTB &= ~_SRCLK; // -> LOW
|
|
||||||
PORTB &= ~_RCLK; // -> LOW
|
|
||||||
PORTB &= ~_SRCLR; // -> LOW
|
|
||||||
PORTB &= ~_SRCLR; // -> LOW
|
|
||||||
PORTB |= _SRCLR; // -> HIGH
|
|
||||||
};
|
|
||||||
|
|
||||||
void CubeRegister::shift_bit(bool bit) {
|
|
||||||
//Serial.println("shift_bit " + (String)(bit));
|
|
||||||
if (bit == LOW) {
|
|
||||||
PORTB &= ~_SER; // -> LOW
|
|
||||||
} else {
|
|
||||||
PORTB |= _SER; // -> HIGH
|
|
||||||
}
|
|
||||||
PORTB |= _SRCLK; // -> HIGH
|
|
||||||
PORTB &= ~_SRCLK; // -> LOW
|
|
||||||
};
|
|
||||||
|
|
||||||
void CubeRegister::register_to_output() {
|
|
||||||
//Serial.println("register_to_output");
|
|
||||||
// PORTB &= ~_RCLK; // -> LOW
|
|
||||||
PORTB |= _RCLK; // -> HIGH
|
|
||||||
//Serial.println(PORTB);
|
|
||||||
PORTB &= ~_RCLK; // -> LOW
|
|
||||||
// Serial.println(PORTB);
|
|
||||||
};
|
|
||||||
|
|
@ -1,73 +0,0 @@
|
||||||
|
|
||||||
#ifndef __CubeRegister__
|
|
||||||
#define __CubeRegister__
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
class CubeRegister {
|
|
||||||
public:
|
|
||||||
static const byte _SER = 0x10; // 0b00010000;
|
|
||||||
static const byte _OE = 0x08; // 0b00001000;
|
|
||||||
static const byte _RCLK = 0x04; // 0b00000100;
|
|
||||||
static const byte _SRCLK = 0x02; // 0b00000010;
|
|
||||||
static const byte _SRCLR = 0x01; // 0b00000001;
|
|
||||||
|
|
||||||
void reset(void);
|
|
||||||
void output_enable(void);
|
|
||||||
void output_disable(void);
|
|
||||||
void clear_register(void);
|
|
||||||
void shift_bit(bool bit);
|
|
||||||
void register_to_output(void);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
Version with digitalWrite
|
|
||||||
#define PIN_SER 12
|
|
||||||
#define PIN_OE 11
|
|
||||||
#define PIN_RCLK 10
|
|
||||||
#define PIN_SRCLK 9
|
|
||||||
#define PIN_SRCLR 8
|
|
||||||
|
|
||||||
class ShiftRegisterSlow {
|
|
||||||
public:
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
pinMode(PIN_SER, OUTPUT);
|
|
||||||
pinMode(PIN_OE, OUTPUT);
|
|
||||||
pinMode(PIN_RCLK, OUTPUT);
|
|
||||||
pinMode(PIN_SRCLK, OUTPUT);
|
|
||||||
pinMode(PIN_SRCLR, OUTPUT);
|
|
||||||
|
|
||||||
output_disable();
|
|
||||||
clear_register();
|
|
||||||
register_to_output();
|
|
||||||
}
|
|
||||||
|
|
||||||
void output_enable() {
|
|
||||||
digitalWrite(PIN_OE, LOW);
|
|
||||||
}
|
|
||||||
void output_disable() {
|
|
||||||
digitalWrite(PIN_OE, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear_register() {
|
|
||||||
digitalWrite(PIN_SRCLR, HIGH);
|
|
||||||
digitalWrite(PIN_SRCLK, LOW);
|
|
||||||
digitalWrite(PIN_RCLK, LOW);
|
|
||||||
digitalWrite(PIN_SRCLR, LOW);
|
|
||||||
digitalWrite(PIN_SRCLR, LOW);
|
|
||||||
digitalWrite(PIN_SRCLR, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
void shift_bit(bool bit) {
|
|
||||||
digitalWrite(PIN_SER, bit);
|
|
||||||
digitalWrite(PIN_SRCLK, HIGH);
|
|
||||||
digitalWrite(PIN_SRCLK, LOW);
|
|
||||||
}
|
|
||||||
void register_to_output() {
|
|
||||||
digitalWrite(PIN_RCLK, HIGH);
|
|
||||||
digitalWrite(PIN_RCLK, LOW);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
#endif
|
|
||||||
Binary file not shown.
|
|
@ -115,7 +115,7 @@ void cube_text_warp(const char *message) {
|
||||||
for (size_t mp = 0; mp < strlen(message); mp++) {
|
for (size_t mp = 0; mp < strlen(message); mp++) {
|
||||||
int s = CUBESIZE - 1;
|
int s = CUBESIZE - 1;
|
||||||
for (int i = 0; i < (CUBESIZE + LEDLEVEL); i++) {
|
for (int i = 0; i < (CUBESIZE + LEDLEVEL); i++) {
|
||||||
dimm_cube(LEDLEVEL / -4);
|
dimm_cube(LEDLEVEL / -2);
|
||||||
write_char(message[mp], max(s, 0), LEDLEVEL);
|
write_char(message[mp], max(s, 0), LEDLEVEL);
|
||||||
s--;
|
s--;
|
||||||
delay(50);
|
delay(50);
|
||||||
|
|
|
||||||
5
src/src/LED.cpp
Normal file
5
src/src/LED.cpp
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
|
||||||
|
|
||||||
|
class Led{
|
||||||
|
|
||||||
|
};
|
||||||
11
src/src/LED.h
Normal file
11
src/src/LED.h
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
#include "Led.h"
|
||||||
|
|
||||||
|
Led::Led(byte x, byte y, byte z, byte brightness){
|
||||||
|
_x=x;
|
||||||
|
_y=y;
|
||||||
|
_z=z;
|
||||||
|
_brightness = brightness;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,518 +0,0 @@
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
#include "./Timer1.h"
|
|
||||||
|
|
||||||
#include "CubeRegister.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef void(*CubeEffects)(unsigned int frame);
|
|
||||||
|
|
||||||
|
|
||||||
#define CUBESIZE 5
|
|
||||||
|
|
||||||
//ShiftRegisterSlow Register ;
|
|
||||||
CubeRegister Register ;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define FPS 60
|
|
||||||
#define LEDLEVEL 8
|
|
||||||
|
|
||||||
byte cube[CUBESIZE][CUBESIZE][CUBESIZE];
|
|
||||||
volatile unsigned long frame = 0;
|
|
||||||
unsigned long start;
|
|
||||||
unsigned long ende;
|
|
||||||
|
|
||||||
|
|
||||||
class LED {
|
|
||||||
public:
|
|
||||||
byte x;
|
|
||||||
byte y;
|
|
||||||
byte z;
|
|
||||||
byte value;
|
|
||||||
|
|
||||||
void show() {
|
|
||||||
cube[x][y][z] = value;
|
|
||||||
}
|
|
||||||
void hide() {
|
|
||||||
cube[x][y][z] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void randomize() {
|
|
||||||
x = random(CUBESIZE + 1);
|
|
||||||
y = random(CUBESIZE + 1);
|
|
||||||
z = random(CUBESIZE + 1);
|
|
||||||
value = random(LEDLEVEL + 1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Vector {
|
|
||||||
public:
|
|
||||||
int dx;
|
|
||||||
int dy;
|
|
||||||
int dz;
|
|
||||||
};
|
|
||||||
|
|
||||||
void draw_cube_layer(byte layer, byte level) {
|
|
||||||
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
Register.shift_bit(z == layer);
|
|
||||||
}
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
|
|
||||||
Register.shift_bit((cube[x][y][layer] > level));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Register.register_to_output();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Disable Arduino's default millisecond counter (from now on, millis(), micros(),
|
|
||||||
// delay() and delayMicroseconds() will not work)
|
|
||||||
|
|
||||||
Serial.begin(115200);
|
|
||||||
//disableMillis();
|
|
||||||
Register.reset();
|
|
||||||
Register.output_enable();
|
|
||||||
//randomize_cube();
|
|
||||||
|
|
||||||
|
|
||||||
// Prepare Timer1 to count
|
|
||||||
// On 16 MHz Arduino boards, this function has a resolution of 4us
|
|
||||||
// On 8 MHz Arduino boards, this function has a resolution of 8us
|
|
||||||
startTimer1(1000000 / (FPS * LEDLEVEL * CUBESIZE));
|
|
||||||
|
|
||||||
//draw_cube_layer(1, 1);
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
void randomize_cube() {
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
cube[x][y][z] = random(LEDLEVEL + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
void fill_cube(byte brightness) {
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
cube[x][y][z] = min(brightness, LEDLEVEL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
void gravity_cube(int x,int y, int z){
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
cube[x][y][z] = ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
void dimm_cube(int diff = -1) {
|
|
||||||
diff = constrain(diff, -LEDLEVEL, LEDLEVEL);
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
cube[x][y][z] = constrain( cube[x][y][z] + diff, 0, LEDLEVEL);
|
|
||||||
/* if (cube[x][y][z] > LEDLEVEL) {
|
|
||||||
cube[x][y][z] = LEDLEVEL;
|
|
||||||
} else if (cube[x][y][z] < 0) {
|
|
||||||
cube[x][y][z] = 0;
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const unsigned long CHAR_5BIT[42] = {
|
|
||||||
0B0111010001111111000110001, // 0 A (97/65)
|
|
||||||
0B1111110001111101000111111, // 1 B
|
|
||||||
0B0111110000100001000001111, // 2 C
|
|
||||||
0B1111010001100011000111110, // 3 D
|
|
||||||
0B1111110000111101000011111, // 4 E
|
|
||||||
0B1111110000111001000010000, // 5 F
|
|
||||||
0B0111110000100111000101111, // 6 G
|
|
||||||
0B1000110001111111000110001, // 7 H
|
|
||||||
0B0111000100001000010001110, // 8 I
|
|
||||||
0B0001100001000011000101111, // 9 J
|
|
||||||
0B1000110010111001001010001, // 10 K
|
|
||||||
0B1000010000100001000011111, // 11 L
|
|
||||||
0B1000111011101011000110001, // 12 M
|
|
||||||
0B1000111001101011001110001, // 13 N
|
|
||||||
0B0111010001100011000101110, // 14 O
|
|
||||||
0B1111010001111101000010000, // 15 P
|
|
||||||
0B1111110001101011111100010, // 16 Q
|
|
||||||
0B1111010001111101000110001, // 17 R
|
|
||||||
0B0111110000011100000111110, // 18 S
|
|
||||||
0B1111100100001000010000100, // 19 T
|
|
||||||
0B1000110001100011000101110, // 20 U
|
|
||||||
0B1000110001010100101000100, // 21 V
|
|
||||||
0B1000110001101011010101010, // 22 W
|
|
||||||
0B1000101010001000101010001, // 23 X
|
|
||||||
0B1000110001010100010000100, // 24 Y
|
|
||||||
0B1111100010001000100011111, // 25 Z (122/90)
|
|
||||||
0B0111010011101011100101110, // 26 ZERO (48)
|
|
||||||
0B0010001100001000010001110, // 27 ONE (49)
|
|
||||||
0B1111000001011101000011111, // 28 TWO (50)
|
|
||||||
0B1111000001011100000111110, // 29 THREE
|
|
||||||
0B0010001000101001111100100, // 30 FOUR
|
|
||||||
0B1111110000111100000111110, // 31 FIVE
|
|
||||||
0B1111110000111101000111110, // 32 SIX
|
|
||||||
0B1111100001000100010000100, // 33 SEVEN
|
|
||||||
0B0111010001011101000101110, // 34 EIGHT
|
|
||||||
0B0111010001011110000111110, // 35 NINE (57)
|
|
||||||
0B0000000000000000000000000, // 36 SPACE (32)
|
|
||||||
0B0010000100001000000000100, // 37 EXCL (33)
|
|
||||||
0B0000000100000000000000100, // 38 COLON (58)
|
|
||||||
0B0000000000000000000000100, // 39 POINT (46)
|
|
||||||
0B0000000000000000010001000, // 40 COMMA (44)
|
|
||||||
0B0000001010111110111000100 // 41 @ -> Herz (44)
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned long char_to_5bits(char zeichen) {
|
|
||||||
// special chars
|
|
||||||
switch (zeichen) {
|
|
||||||
case 32: return CHAR_5BIT[36];
|
|
||||||
case 33: return CHAR_5BIT[37];
|
|
||||||
case 58: return CHAR_5BIT[38];
|
|
||||||
case 46: return CHAR_5BIT[39];
|
|
||||||
case 44: return CHAR_5BIT[40];
|
|
||||||
case 64: return CHAR_5BIT[41];
|
|
||||||
}
|
|
||||||
|
|
||||||
// upper case letters
|
|
||||||
if ((zeichen >= 65) & (zeichen <= 90)) {
|
|
||||||
return CHAR_5BIT[zeichen - 65];
|
|
||||||
}
|
|
||||||
// lower case letters
|
|
||||||
if ((zeichen >= 97) & (zeichen <= 122)) {
|
|
||||||
return CHAR_5BIT[zeichen - 97];
|
|
||||||
}
|
|
||||||
// digits
|
|
||||||
if ((zeichen >= 48) & (zeichen <= 57)) {
|
|
||||||
return CHAR_5BIT[zeichen - 22];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void write_char(char zeichen, int ebene, byte brightness = LEDLEVEL) {
|
|
||||||
ebene = constrain(ebene, 0, CUBESIZE - 1);
|
|
||||||
unsigned long pattern = char_to_5bits(zeichen);
|
|
||||||
unsigned long mask = 0B1000000000000000000000000;
|
|
||||||
Serial.println("\nPattern: " + (String)(pattern));
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
Serial.print( pattern & mask );
|
|
||||||
if (pattern & mask) {
|
|
||||||
cube[(CUBESIZE - 1) - x][ebene][y] = brightness ;
|
|
||||||
} else {
|
|
||||||
cube[(CUBESIZE - 1) - x][ebene][y] = 0 ;
|
|
||||||
}
|
|
||||||
mask >>= 1;
|
|
||||||
//mask = mask << 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void hello_cube(int duration) {
|
|
||||||
|
|
||||||
const char* message = "mama ist die beste!@!@@ " ;
|
|
||||||
|
|
||||||
for (size_t mp = 0; mp < strlen(message); mp++ ) {
|
|
||||||
int s = CUBESIZE - 1;
|
|
||||||
for (int i = 0; i < (CUBESIZE + LEDLEVEL); i++) {
|
|
||||||
dimm_cube(LEDLEVEL / -2);
|
|
||||||
write_char(message[mp], max(s, 0), LEDLEVEL);
|
|
||||||
s--;
|
|
||||||
delay(50);
|
|
||||||
}
|
|
||||||
delay(200);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void text_cube(const char *message){
|
|
||||||
|
|
||||||
for (size_t mp = 0; mp < strlen(message); mp++ ) {
|
|
||||||
int s = CUBESIZE - 1;
|
|
||||||
for (int i = 0; i < (CUBESIZE + LEDLEVEL); i++) {
|
|
||||||
dimm_cube(LEDLEVEL / -2 );
|
|
||||||
write_char(message[mp], max(s, 0), LEDLEVEL);
|
|
||||||
s--;
|
|
||||||
delay(50);
|
|
||||||
}
|
|
||||||
delay(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void glow_cube(int duration) {
|
|
||||||
ende = millis() + duration * 1000;
|
|
||||||
int glow = 0;
|
|
||||||
int glch = 1;
|
|
||||||
fill_cube(glow);
|
|
||||||
while (millis() < ende) {
|
|
||||||
glow += glch; // ((ende - millis()) / 200) % (LEDLEVEL + 1);
|
|
||||||
if (glow <= 0){
|
|
||||||
glow = 0;
|
|
||||||
glch = 1;
|
|
||||||
} else if (glow >= LEDLEVEL){
|
|
||||||
glow = LEDLEVEL;
|
|
||||||
glch = -1;
|
|
||||||
}
|
|
||||||
fill_cube(glow);
|
|
||||||
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void glitzer_cube(int duration) {
|
|
||||||
start = millis();
|
|
||||||
ende = millis() + duration * 1000;
|
|
||||||
//while ((millis() - start) < 5000 ) {
|
|
||||||
while (millis() < ende) {
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
if (random(2) > 0) {
|
|
||||||
cube[x][y][z] = LEDLEVEL;
|
|
||||||
} else {
|
|
||||||
cube[x][y][z] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delay(50);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void glitzer_cube_levels(int duration) {
|
|
||||||
start = millis();
|
|
||||||
ende = millis() + duration * 1000;
|
|
||||||
//while ((millis() - start) < 5000 ) {
|
|
||||||
while (millis() < ende) {
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
cube[x][y][z] = random(LEDLEVEL + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delay(50);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void glitzer_fade_cube(int duration) {
|
|
||||||
int cleanup = 5;
|
|
||||||
fill_cube(0);
|
|
||||||
/*for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
cube[x][y][z] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
/* for (byte n = 0; n < traces; n++) {
|
|
||||||
punkt.randomize();
|
|
||||||
punkt.show();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
ende = millis() + duration * 1000;
|
|
||||||
while (millis() < ende) {
|
|
||||||
|
|
||||||
//while ((millis() - start) < 10000 ) {
|
|
||||||
//punkt.randomize();
|
|
||||||
//punkt.show();
|
|
||||||
cube[random(CUBESIZE)][random(CUBESIZE)][random(CUBESIZE)] = random(LEDLEVEL + 1);
|
|
||||||
cube[random(CUBESIZE)][random(CUBESIZE)][random(CUBESIZE)] = random(LEDLEVEL + 1);
|
|
||||||
delay(25);
|
|
||||||
cleanup --;
|
|
||||||
if (cleanup <= 0) {
|
|
||||||
cleanup = 5;
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
if (cube[x][y][z] > 0) {
|
|
||||||
cube[x][y][z] --;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void dots_cube(int duration) {
|
|
||||||
byte x;
|
|
||||||
byte y;
|
|
||||||
int speed = 50;
|
|
||||||
fill_cube(0);
|
|
||||||
for ( x = 0; x < CUBESIZE; x++) {
|
|
||||||
for ( y = 0; y < CUBESIZE; y++) {
|
|
||||||
cube[x][y][0] = LEDLEVEL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ende = millis() + duration * 1000;
|
|
||||||
while (millis() < ende) {
|
|
||||||
x = random(CUBESIZE);
|
|
||||||
y = random(CUBESIZE);
|
|
||||||
|
|
||||||
if (cube[x][y][0] == 0) {
|
|
||||||
for (int z = CUBESIZE - 1; z >= 0; --z) {
|
|
||||||
|
|
||||||
if (z <= (CUBESIZE - 2))
|
|
||||||
cube[x][y][z + 2] = 0;
|
|
||||||
cube[x][y][z + 1] = LEDLEVEL / 2;
|
|
||||||
cube[x][y][z] = LEDLEVEL;
|
|
||||||
delay(speed);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
for (int z = 1; z < CUBESIZE; z++) {
|
|
||||||
|
|
||||||
if (z >= 2)
|
|
||||||
cube[x][y][z - 2] = 0;
|
|
||||||
cube[x][y][z - 1] = LEDLEVEL / 2;
|
|
||||||
cube[x][y][z] = LEDLEVEL;
|
|
||||||
delay(speed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (byte z = 1; z < CUBESIZE - 1; z++) {
|
|
||||||
cube[x][y][z] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void template_cube(int duration) {
|
|
||||||
// prepare something
|
|
||||||
ende = millis() + duration * 1000;
|
|
||||||
while (millis() < ende) {
|
|
||||||
// manipulate cube[][][]
|
|
||||||
delay(25);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void traces_cube() {
|
|
||||||
start = millis();
|
|
||||||
int traces = 5;
|
|
||||||
LED origin[traces];
|
|
||||||
LED leds[traces];
|
|
||||||
LED ziel[traces];
|
|
||||||
|
|
||||||
int steps[traces];
|
|
||||||
int pos[traces];
|
|
||||||
|
|
||||||
for (byte n = 0; n < traces; n++) {
|
|
||||||
origin[n] = LED();
|
|
||||||
origin[n].randomize();
|
|
||||||
origin[n].z = 0;
|
|
||||||
leds[n] = LED();
|
|
||||||
ziel[n] = LED();
|
|
||||||
ziel[n].randomize();
|
|
||||||
ziel[n].z = CUBESIZE - 1;
|
|
||||||
steps[n] = random(5, 20);
|
|
||||||
pos[n] = 0;
|
|
||||||
}
|
|
||||||
for (byte x = 0; x < CUBESIZE; x++) {
|
|
||||||
for (byte y = 0; y < CUBESIZE; y++) {
|
|
||||||
for (byte z = 0; z < CUBESIZE; z++) {
|
|
||||||
cube[x][y][z] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((millis() - start) < 5000 ) {
|
|
||||||
for (byte n = 0; n < traces; n++) {
|
|
||||||
|
|
||||||
leds[n].hide();
|
|
||||||
pos[n] ++;
|
|
||||||
leds[n].x = origin[n].x + ( (ziel[n].x - origin[n].x) * pos[n] / steps[n]);
|
|
||||||
leds[n].y = origin[n].y + ( (ziel[n].y - origin[n].y) * pos[n] / steps[n]);
|
|
||||||
leds[n].z = (origin[n]).z + ( ((ziel[n]).z - origin[n].z) * pos[n] / steps[n]);
|
|
||||||
leds[n].show();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
int next_duration = random(5, 10);
|
|
||||||
switch (random(0, 10)) {
|
|
||||||
case 0:
|
|
||||||
glow_cube(next_duration);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
glitzer_cube(next_duration);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
glitzer_cube_levels(next_duration);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
glitzer_fade_cube(next_duration);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
dots_cube(next_duration);
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
text_cube("i@u");
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
text_cube("benjamin");
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
text_cube("annika");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* glitzer_cube(random(5, 20));
|
|
||||||
glitzer_fade_cube(random(5, 20));
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Define the function which will handle the notifications (interrupts)
|
|
||||||
ISR(timer1Event)
|
|
||||||
{
|
|
||||||
// if (Serial)
|
|
||||||
// Serial.println(frame);
|
|
||||||
|
|
||||||
// Reset Timer1 (resetTimer1 should be the first operation for better timer precision)
|
|
||||||
resetTimer1();
|
|
||||||
// For a smaller and faster code, the line above could safely be replaced with a call
|
|
||||||
// to the function resetTimer1Unsafe() as, despite its name, it IS safe to call
|
|
||||||
// that function in here (interrupts are disabled)
|
|
||||||
|
|
||||||
// Make sure to do your work as fast as possible, since interrupts are automatically
|
|
||||||
// disabled when this event happens (refer to interrupts() and noInterrupts() for
|
|
||||||
// more information on that)
|
|
||||||
draw_cube_layer(frame % CUBESIZE, frame % LEDLEVEL);
|
|
||||||
frame ++;
|
|
||||||
}
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
#include "CubeBase.h"
|
|
||||||
#include "CubeEffects.h"
|
|
||||||
#include "CubeTextEffects.h"
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Serial.begin(115200);
|
|
||||||
init_cube();
|
|
||||||
randomSeed(analogRead(A0) + analogRead(A1) + analogRead(A2));
|
|
||||||
// cube_startup();
|
|
||||||
}
|
|
||||||
|
|
||||||
// const char* messages = {"I°U", "mama ist die allerbeste!°!°", "benjamin",
|
|
||||||
// "annika", "5x5x5 led-cube"};
|
|
||||||
//static const char *const messages[] = {"I°U", "mama ist die allerbeste!°!°",
|
|
||||||
// "benjamin", "annika", "5x5x5 led-cube"};
|
|
||||||
static const char *const messages[] = {"silvester", "2017>2018", "2018",
|
|
||||||
"ohh, ahh", "led-cube"};
|
|
||||||
|
|
||||||
int message_count = 5;
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
|
|
||||||
|
|
||||||
// int next_duration = random(10, 20);
|
|
||||||
//switch (9) {
|
|
||||||
switch (random(20)) {
|
|
||||||
case 0:
|
|
||||||
cube_effect_glow();
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
cube_effect_glitzer_fade();
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
cube_effect_glitzer_levels();
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
cube_effect_glitzer();
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
cube_effect_dots();
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
//cube_startup();
|
|
||||||
cube_effect_glitzer_levels();
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
cube_text_banner(messages[random(message_count)]);
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
cube_text_warp(messages[random(message_count)]);
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
cube_life();
|
|
||||||
break;
|
|
||||||
case 9:
|
|
||||||
// Serial.println("cube_moving_dots");
|
|
||||||
cube_moving_dots();
|
|
||||||
break;
|
|
||||||
/*
|
|
||||||
case 8:
|
|
||||||
if (random(10) > 5)
|
|
||||||
text_cube("LED-Cube 5*5*5");
|
|
||||||
else
|
|
||||||
banner_cube("LED-Cube 5*5*5 ");
|
|
||||||
|
|
||||||
*/
|
|
||||||
/*write_char('v', 0);
|
|
||||||
write_char('x', 4);
|
|
||||||
|
|
||||||
rotate_cube(10);
|
|
||||||
fill_cube(0);*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/* glitzer_cube(random(5, 20));
|
|
||||||
glitzer_fade_cube(random(5, 20));
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
101
src/src/TimerOne.cpp
Normal file
101
src/src/TimerOne.cpp
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
/*
|
||||||
|
* Interrupt and PWM utilities for 16 bit Timer1 on ATmega168/328
|
||||||
|
* Original code by Jesse Tane for http://labs.ideo.com August 2008
|
||||||
|
* Modified March 2009 by Jérôme Despatis and Jesse Tane for ATmega328 support
|
||||||
|
* Modified June 2009 by Michael Polli and Jesse Tane to fix a bug in setPeriod() which caused the timer to stop
|
||||||
|
*
|
||||||
|
* This is free software. You can redistribute it and/or modify it under
|
||||||
|
* the terms of Creative Commons Attribution 3.0 United States License.
|
||||||
|
* To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/
|
||||||
|
* or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "TimerOne.h"
|
||||||
|
|
||||||
|
TimerOne Timer1; // preinstatiate
|
||||||
|
|
||||||
|
ISR(TIMER1_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
|
||||||
|
{
|
||||||
|
Timer1.isrCallback();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::initialize(long microseconds)
|
||||||
|
{
|
||||||
|
TCCR1A = 0; // clear control register A
|
||||||
|
TCCR1B = _BV(WGM13); // set mode as phase and frequency correct pwm, stop the timer
|
||||||
|
setPeriod(microseconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::setPeriod(long microseconds)
|
||||||
|
{
|
||||||
|
long cycles = (F_CPU * microseconds) / 2000000; // the counter runs backwards after TOP, interrupt is at BOTTOM so divide microseconds by 2
|
||||||
|
if(cycles < RESOLUTION) clockSelectBits = _BV(CS10); // no prescale, full xtal
|
||||||
|
else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11); // prescale by /8
|
||||||
|
else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11) | _BV(CS10); // prescale by /64
|
||||||
|
else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12); // prescale by /256
|
||||||
|
else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12) | _BV(CS10); // prescale by /1024
|
||||||
|
else cycles = RESOLUTION - 1, clockSelectBits = _BV(CS12) | _BV(CS10); // request was out of bounds, set as maximum
|
||||||
|
ICR1 = pwmPeriod = cycles; // ICR1 is TOP in p & f correct pwm mode
|
||||||
|
TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
|
||||||
|
TCCR1B |= clockSelectBits; // reset clock select register
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::setPwmDuty(char pin, int duty)
|
||||||
|
{
|
||||||
|
unsigned long dutyCycle = pwmPeriod;
|
||||||
|
dutyCycle *= duty;
|
||||||
|
dutyCycle >>= 10;
|
||||||
|
if(pin == 1 || pin == 9) OCR1A = dutyCycle;
|
||||||
|
else if(pin == 2 || pin == 10) OCR1B = dutyCycle;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::pwm(char pin, int duty, long microseconds) // expects duty cycle to be 10 bit (1024)
|
||||||
|
{
|
||||||
|
if(microseconds > 0) setPeriod(microseconds);
|
||||||
|
if(pin == 1 || pin == 9) {
|
||||||
|
DDRB |= _BV(PORTB1); // sets data direction register for pwm output pin
|
||||||
|
TCCR1A |= _BV(COM1A1); // activates the output pin
|
||||||
|
}
|
||||||
|
else if(pin == 2 || pin == 10) {
|
||||||
|
DDRB |= _BV(PORTB2);
|
||||||
|
TCCR1A |= _BV(COM1B1);
|
||||||
|
}
|
||||||
|
setPwmDuty(pin, duty);
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::disablePwm(char pin)
|
||||||
|
{
|
||||||
|
if(pin == 1 || pin == 9) TCCR1A &= ~_BV(COM1A1); // clear the bit that enables pwm on PB1
|
||||||
|
else if(pin == 2 || pin == 10) TCCR1A &= ~_BV(COM1B1); // clear the bit that enables pwm on PB2
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::attachInterrupt(void (*isr)(), long microseconds)
|
||||||
|
{
|
||||||
|
if(microseconds > 0) setPeriod(microseconds);
|
||||||
|
isrCallback = isr; // register the user's callback with the real ISR
|
||||||
|
TIMSK1 = _BV(TOIE1); // sets the timer overflow interrupt enable bit
|
||||||
|
sei(); // ensures that interrupts are globally enabled
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::detachInterrupt()
|
||||||
|
{
|
||||||
|
TIMSK1 &= ~_BV(TOIE1); // clears the timer overflow interrupt enable bit
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::start()
|
||||||
|
{
|
||||||
|
TCCR1B |= clockSelectBits;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::stop()
|
||||||
|
{
|
||||||
|
TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12)); // clears all clock selects bits
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerOne::restart()
|
||||||
|
{
|
||||||
|
TCNT1 = 0;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue