#include <math.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//#include "Buttons.h"
//Buttons Button1(A0, INPUT_PULLUP);
//Buttons Button2(A1, INPUT_PULLUP);
//Buttons Button3(A2, INPUT_PULLUP);
//Buttons Button4(A3, INPUT_PULLUP);
//Buttons Button5(12, INPUT_PULLUP);
long frequency = 1;
byte table[256];
word smp;
word wav;
int curs;
bool irq = true;
unsigned long timer;
volatile bool interrupt_01 = false;
volatile unsigned long bounce_01 = 0;
volatile unsigned long lpress_01 = 0;
volatile bool interrupt_02 = false;
volatile unsigned long bounce_02 = 0;
volatile unsigned long lpress_02 = 0;
volatile bool interrupt_03 = false;
volatile unsigned long bounce_03 = 0;
volatile unsigned long lpress_03 = 0;
volatile bool interrupt_04 = false;
volatile unsigned long bounce_04 = 0;
volatile unsigned long lpress_04 = 0;
volatile bool interrupt_05 = false;
volatile unsigned long bounce_05 = 0;
volatile unsigned long lpress_05 = 0;
void setup()
{
Serial.begin(38400);
while (!Serial) {;}
pinMode(12, INPUT_PULLUP);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
lcd.init();
lcd.clear();
lcd.backlight();
DDRD |= B11110000;
DDRB |= B00001111;
cli(); //disable interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // set entire TCCR1B register to 0
OCR1A = int(16000000 / 1 / smp / frequency) - 1 ; //set compare match register ??khz to start
TCCR1B |= (1 << WGM12); //turn on CTC mode
TCCR1B |= (1 << CS10); // Set CS10 bit for 1 prescaler
TIMSK1 |= (0 << OCIE1A); // enable timer compare interrupt
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), button_interrupt_handler, FALLING);
sei(); //enable interrupts
// OCR1A: minimum = 255 (256-1) z.B (16000000 / (smp 1) / (frequency 62500)) -1 = 255
// OCR1A: maximum = 65535 (65536-1)
display(0);
}
ISR(TIMER1_COMPA_vect)
{
static int t = -1;
t++; if (t >= smp)
{
t = 0;
}
PORTD = (PORTD & 0x0F) | (table[t] << 4);
PORTB = (PORTB & 0xF0) | (table[t] >> 4);
}
void button_interrupt_handler()
{
irq = true; bitClear(TIMSK1, 1); timer = millis();
if (!digitalRead(A0)) {
interrupt_01 = true;
bounce_01 = millis();
lpress_01 = millis();
}
if (!digitalRead(A1)) {
interrupt_02 = true;
bounce_02 = millis();
lpress_02 = millis();
}
if (!digitalRead(A2)) {
interrupt_03 = true;
bounce_03 = millis();
lpress_03 = millis();
}
if (!digitalRead(A3)) {
interrupt_04 = true;
bounce_04 = millis();
lpress_04 = millis();
}
if (!digitalRead(12)) {
interrupt_05 = true;
bounce_05 = millis();
lpress_05 = millis();
}
}
void loop()
{
if (irq)
{
buttons();
samples();
release();
}
}
void buttons()
{
static word incr = 1;
int i;
i = button02();//Button2.longpress0(50, 200);
if (i)
{
wav++;
;
if (wav > 3)
{
wav = 0;
}
display(1);
}
i = button03();//Button3.longpress0(50, 200);
if (i)
{
frequency -= incr;
;
if (frequency < 1)
{
frequency = 1;
}
display(2);
}
i = button01();//Button1.longpress0(50, 200);
if (i)
{
frequency += incr;
;
if (frequency > 62500)
{
frequency = 62500;
}
display(2);
}
i = button05();//Button5.longpress0(50, 200);
if (i)
{
curs++;
;
if (curs > 4)
{
curs = 0;
}
incr = round(pow(10, curs));
;
display(4);
}
i = button04();//Button4.longpress0(50, 200);
if (i)
{
curs--;
;
if (curs < 0)
{
curs = 4;
}
incr = round(pow(10, curs));
;
display(4);
}
}
void samples()
{
static word rate = 0;
if (frequency <= 16000000 / 65536)
{
smp = 256;
}
else
{
smp = 16000000.0 / float(frequency) / 256.0;
}
if (rate != smp)
{
rate = smp; display(3);
}
}
void release()
{
if (!digitalRead(2))
{
timer = millis();
}
else if (millis() - timer > 500)
{
Tabelle(); irq = false; OCR1A = int(16000000.0 / frequency / smp ) - 1; ; bitSet(TIMSK1, 1);
}
}
void Tabelle()
{
static int s = -1;
static int w = -1;
if (s == smp)
{
if (w == wav)
{
return;
}
}
s = smp;
w = wav;
int i;
int j = smp;
char q[99]; sprintf(q, "wav: %1d smp: %3d\n", wav, smp); Serial.print(q);
switch (wav)
{
case 0: //sinus
for (i = 0; i < j; i++)
{
table[i] = round(127.5 + 127.5 * sin(2 * M_PI * i / j));
}
break;
case 1: //triangle
for (i = 0; i < j; i++)
{
table[i] = round(127.5 + 255.0 * asin(sin(2 * M_PI * i / j)) / M_PI);
}
break;
case 2: //sawtooth
for (i = 0; i < j; i++)
{
// table[i] = round(127.5 + 255.0 * (atan(1 / tan((2 * M_PI * i / j) / 2 ))) / M_PI);
table[i] = round(127.5 + 255.0 * (atan( tan((2 * M_PI * i / j) / 2 ))) / M_PI);
}
break;
case 3: //square
for (i = 0; i < j; i++)
{
table[i] = 255 * (1 - round(float(i) / float(j)));
}
break;
}
}
void display(int q)
{
static char wavform[ 9];
static char display[17];
switch (wav)
{
case 0: snprintf(wavform, 9, "sinewave"); break;
case 1: snprintf(wavform, 9, "triangle"); break;
case 2: snprintf(wavform, 9, "sawtooth"); break;
case 3: snprintf(wavform, 9, "square.."); break;
}
lcd.noCursor();
switch (q)
{
case 0:
sprintf(display, "%s %05uHz", wavform, frequency);
lcd.setCursor(0, 0);
lcd.print(display);
sprintf(display, "samplerate = %3u", smp);
lcd.setCursor(0, 1);
lcd.print(display);
break;
case 1:
sprintf(display, "%s", wavform);
lcd.setCursor(0, 0);
lcd.print(display);
break;
case 2:
sprintf(display, "%05uHz", frequency);
lcd.setCursor(9, 0);
lcd.print(display);
break;
case 3:
sprintf(display, "samplerate = %3u", smp);
lcd.setCursor(0, 1);
lcd.print(display);
break;
case 4:
// dummy (set cursor)
break;
}
lcd.setCursor(13 - curs, 0);
lcd.cursor();
}
int button01() {
int impulse = 0;
static bool longpress = false;
noInterrupts();
if (digitalRead(A0)) lpress_01 = millis();
else bounce_01 = millis();
if (interrupt_01) {
if (millis() - bounce_01 > 20) {
interrupt_01 = false;
impulse = !longpress; //impulse shortpress
longpress = false;
}
}
if (!longpress) {
if (millis() - lpress_01 > 600) {
longpress = true;
lpress_01 = millis();
impulse = 1; //impulse longpress(once)
}
}
if (longpress) {
if (millis() - lpress_01 > 300) {
lpress_01 = millis();
impulse = 1; //impulse longpress(repeat)
}
}
interrupts();
return impulse;
}
int button02() {
int impulse = 0;
static bool longpress = false;
noInterrupts();
if (digitalRead(A1)) lpress_02 = millis();
else bounce_02 = millis();
if (interrupt_02) {
if (millis() - bounce_02 > 20) {
interrupt_02 = false;
impulse = !longpress; //impulse shortpress
longpress = false;
}
}
if (!longpress) {
if (millis() - lpress_02 > 600) {
longpress = true;
lpress_02 = millis();
impulse = 1; //impulse longpress(once)
}
}
if (longpress) {
if (millis() - lpress_02 > 300) {
lpress_02 = millis();
impulse = 1; //impulse longpress(repeat)
}
}
interrupts();
return impulse;
}
int button03() {
int impulse = 0;
static bool longpress = false;
noInterrupts();
if (digitalRead(A2)) lpress_03 = millis();
else bounce_03 = millis();
if (interrupt_03) {
if (millis() - bounce_03 > 20) {
interrupt_03 = false;
impulse = !longpress; //impulse shortpress
longpress = false;
}
}
if (!longpress) {
if (millis() - lpress_03 > 600) {
longpress = true;
lpress_03 = millis();
impulse = 1; //impulse longpress(once)
}
}
if (longpress) {
if (millis() - lpress_03 > 300) {
lpress_03 = millis();
impulse = 1; //impulse longpress(repeat)
}
}
interrupts();
return impulse;
}
int button04() {
int impulse = 0;
static bool longpress = false;
noInterrupts();
if (digitalRead(A3)) lpress_04 = millis();
else bounce_04 = millis();
if (interrupt_04) {
if (millis() - bounce_04 > 20) {
interrupt_04 = false;
impulse = !longpress; //impulse shortpress
longpress = false;
}
}
if (!longpress) {
if (millis() - lpress_04 > 600) {
longpress = true;
lpress_04 = millis();
impulse = 1; //impulse longpress(once)
}
}
if (longpress) {
if (millis() - lpress_04 > 300) {
lpress_04 = millis();
impulse = 1; //impulse longpress(repeat)
}
}
interrupts();
return impulse;
}
int button05() {
int impulse = 0;
static bool longpress = false;
noInterrupts();
if (digitalRead(12)) lpress_05 = millis();
else bounce_05 = millis();
if (interrupt_05) {
if (millis() - bounce_05 > 20) {
interrupt_05 = false;
impulse = !longpress; //impulse shortpress
longpress = false;
}
}
if (!longpress) {
if (millis() - lpress_05 > 600) {
longpress = true;
lpress_05 = millis();
impulse = 1; //impulse longpress(once)
}
}
if (longpress) {
if (millis() - lpress_05 > 300) {
lpress_05 = millis();
impulse = 1; //impulse longpress(repeat)
}
}
interrupts();
return impulse;
}