/* ------------------------------------------------------------------------------------
* Skeleton project for setting up ATTiny85 with an OLED SSD1306
* Basic display function for numbers
* Auth: EA
* Last rev: 2023-12-29
* ------------------------------------------------------------------------------------
*/
#include <TinyWireM.h>
#include "SSD1306_config.h"
// Sparkfun: P1 - 1
const byte SPEAKER_PIN = 1; // 0 = Pin 8, 4 = Pin 3
const byte BTN_PIN = 4;
// Not PROGMEMming this right now
byte digits[10][4] =
{
{ 0x7e, 0x81, 0x81, 0x7e },
{ 0x82, 0xff, 0x80, 0x00 },
{ 0xc2, 0xa1, 0x91, 0x8f },
{ 0x42, 0x91, 0x91, 0x6e },
{ 0x1f, 0x10, 0x10, 0xff },
{ 0x4f, 0x91, 0x91, 0x61 },
{ 0x7c, 0x92, 0x91, 0x61 },
{ 0x01, 0xf1, 0x09, 0x07 },
{ 0x6e, 0x91, 0x91, 0x6e },
{ 0x0e, 0x91, 0x51, 0x3e }
};
// ------------------------------------------------------------------------------------
// SETUP
// ------------------------------------------------------------------------------------
void setup() {
TinyWireM.begin();
config_display();
pinMode(SPEAKER_PIN, OUTPUT);
cursorTo(0,0);
send_data(0b11001100);
send_data(0b00110011);
send_data(0b11001100);
send_data(0b00110011);
send_data(0b11001100);
send_data(0b00110011);
delay(1000);
}
// ------------------------------------------------------------------------------------
// LOOP
// ------------------------------------------------------------------------------------
void loop() {
beep(420, 50);
displayNumberAt(10, 10, 1);
delay(1000);
displayNumberAt(10, 10, 23);
delay(1000);
displayNumberAt(10, 10, 856);
delay(1000);
displayNumberAt(10, 10, 1000);
delay(1000);
displayNumberAt(10, 10, 9876);
delay(1000);
displayNumberAt(10, 10, 543);
delay(1000);
displayNumberAt(10, 10, 210);
delay(1000);
displayNumberAt(10, 10, 0001);
delay(1000);
}
void displayNumberAt(uint8_t x, uint8_t y, int number) {
beep(660, 40);
cursorTo(x, y);
// Count number of digits
int c = 0; // Digit position
int n = number;
while (n != 0) {
n /= 10;
c++;
}
int numberArray[c];
c = 0;
n = number;
// Extract each digit
while (n != 0) {
numberArray[c] = n % 10;
n /= 10;
c++;
}
// Send digit data, end with space (0x00).
for (uint8_t i=0; i<c; i++) {
send_data(digits[numberArray[c-i-1]][0]);
send_data(digits[numberArray[c-i-1]][1]);
send_data(digits[numberArray[c-i-1]][2]);
send_data(digits[numberArray[c-i-1]][3]);
send_data(0x00);
/*
// This is probably faster.
TinyWireM.beginTransmission(0x3C);
TinyWireM.write(0x40); // data mode
TinyWireM.write( digits[numberArray[c-i-1]][0] );
TinyWireM.write( digits[numberArray[c-i-1]][1] );
TinyWireM.write( digits[numberArray[c-i-1]][2] );
TinyWireM.write( digits[numberArray[c-i-1]][3] );
TinyWireM.write( 0x00 );
TinyWireM.endTransmission();
*/
}
}
void beep (int frequencyInHertz, byte timeInMilliseconds)
{
int delayAmount = (long)(1000000 / frequencyInHertz);
int loopTime = (int)((timeInMilliseconds*10) / (delayAmount * 0.002));
for (int x = 0; x < loopTime; x++) {
digitalWrite(SPEAKER_PIN, HIGH);
delayMicroseconds(delayAmount);
digitalWrite(SPEAKER_PIN, LOW);
delayMicroseconds(delayAmount);
}
}
void cursorTo(byte x, byte page) {
send_command(0x22);
send_command(page);
send_command(7);
send_command(0x21);
send_command(x);
send_command(127);
}
void config_display() {
for(int i=0; i<sizeof(initializeCmds); i++) {
send_command(pgm_read_byte(initializeCmds+i));
}
}
void send_command(unsigned char cmd) {
TinyWireM.beginTransmission(0x3c);
TinyWireM.write(0x00);
TinyWireM.write(cmd);
TinyWireM.endTransmission();
}
void send_data(byte slice) {
TinyWireM.beginTransmission(0x3C);
TinyWireM.write(0x40);
TinyWireM.write(slice);
TinyWireM.endTransmission();
}
void bhm_line(int x1,int y1,int x2,int y2,int c)
{
int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i;
dx=x2-x1;
dy=y2-y1;
dx1=fabs(dx);
dy1=fabs(dy);
px=2*dy1-dx1;
py=2*dx1-dy1;
if(dy1<=dx1)
{
if(dx>=0)
{
x=x1;
y=y1;
xe=x2;
}
else
{
x=x2;
y=y2;
xe=x1;
}
putpixel(x,y,c);
for(i=0;x<xe;i++)
{
x=x+1;
if(px<0)
{
px=px+2*dy1;
}
else
{
if((dx<0 && dy<0) || (dx>0 && dy>0))
{
y=y+1;
}
else
{
y=y-1;
}
px=px+2*(dy1-dx1);
}
delay(0);
putpixel(x,y,c);
}
}
else
{
if(dy>=0)
{
x=x1;
y=y1;
ye=y2;
}
else
{
x=x2;
y=y2;
ye=y1;
}
putpixel(x,y,c);
for(i=0;y<ye;i++)
{
y=y+1;
if(py<=0)
{
py=py+2*dx1;
}
else
{
if((dx<0 && dy<0) || (dx>0 && dy>0))
{
x=x+1;
}
else
{
x=x-1;
}
py=py+2*(dx1-dy1);
}
delay(0);
putpixel(x,y,c);
}
}
}