You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MicroMDAEPiano/MicroMDAEPiano.ino

768 lines
21 KiB

/*
MicroMDAEPiano
6 years ago
MicroMDAEPiano is a port of the MDA-EPiano sound engine
(https://sourceforge.net/projects/mda-vst/) for the Teensy-3.5/3.6 with audio shield.
(c)2019 H. Wirtz <wirtz@parasitstudio.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <MIDI.h>
#include <EEPROM.h>
#include <limits.h>
#include "mdaEPiano.h"
#ifdef USE_XFADE_DATA
#include "mdaEPianoDataXfade.h"
#else
#include "mdaEPianoData.h"
#endif
6 years ago
#if defined(MIDI_DEVICE_USB)
#include <midi_UsbTransport.h>
#endif
6 years ago
#ifdef MIDI_DEVICE_USB_HOST
#include <USBHost_t36.h>
#endif
#include <Bounce.h>
6 years ago
#include "Encoder4.h"
#include "LiquidCrystalPlus_I2C.h"
6 years ago
#include "UI.h"
6 years ago
//*************************************************************************************************
//* GLOBAL VARIABLES
//*************************************************************************************************
// Audio configuration
AudioPlayQueue queue_r;
AudioPlayQueue queue_l;
AudioAnalyzePeak peak_r;
AudioAnalyzePeak peak_l;
AudioEffectFreeverb freeverb_r;
AudioEffectFreeverb freeverb_l;
AudioMixer4 mixer_r;
AudioMixer4 mixer_l;
AudioConnection patchCord0(queue_r, peak_r);
AudioConnection patchCord1(queue_l, peak_l);
AudioConnection patchCord4(queue_r, freeverb_r);
AudioConnection patchCord5(queue_l, freeverb_l);
AudioConnection patchCord6(queue_r, 0, mixer_r, 0);
AudioConnection patchCord7(queue_l, 0, mixer_l, 0);
AudioConnection patchCord8(freeverb_r, 0, mixer_r, 1);
AudioConnection patchCord9(freeverb_l, 0, mixer_l, 1);
#if defined(TEENSY_AUDIO_BOARD) || defined(TGA_AUDIO_BOARD)
6 years ago
AudioOutputI2S i2s1;
AudioConnection patchCord110(mixer_r, 0, i2s1, 0);
AudioConnection patchCord111(mixer_l, 0, i2s1, 1);
#ifdef TEENSY_AUDIO_BOARD
6 years ago
AudioControlSGTL5000 sgtl5000_1;
#else
AudioControlWM8731master wm8731_1;
#endif
#else
6 years ago
AudioOutputPT8211 pt8211_1;
AudioAmplifier volume_r;
AudioAmplifier volume_l;
AudioConnection patchCord10(mixer_r, volume_r);
AudioConnection patchCord11(mixer_l, volume_l);
AudioConnection patchCord12(volume_r, 0, pt8211_1, 1);
AudioConnection patchCord13(volume_l, 0, pt8211_1, 0);
#endif
6 years ago
// Objects
mdaEPiano* ep;
6 years ago
6 years ago
#ifdef MIDI_DEVICE_DIN
MIDI_CREATE_INSTANCE(HardwareSerial, MIDI_DEVICE_DIN, midi_serial);
#endif
#ifdef MIDI_DEVICE_USB_HOST
USBHost usb_host;
MIDIDevice midi_usb(usb_host);
#endif
#ifdef MIDI_DEVICE_USB
static const unsigned sUsbTransportBufferSize = 16;
typedef midi::UsbTransport<sUsbTransportBufferSize> UsbTransport;
UsbTransport sUsbTransport;
MIDI_CREATE_INSTANCE(UsbTransport, sUsbTransport, midi_onboard_usb);
#endif
// Variables
uint8_t midi_channel = DEFAULT_MIDI_CHANNEL;
uint32_t xrun = 0;
uint32_t overload = 0;
uint32_t peak = 0;
uint16_t render_time_max = 0;
float vol = VOLUME;
float vol_right = 1.0;
float vol_left = 1.0;
elapsedMicros fill_audio_buffer;
const uint16_t audio_block_time_us = 1000000 / (SAMPLE_RATE / AUDIO_BLOCK_SAMPLES);
#ifdef SHOW_CPU_LOAD_MSEC
elapsedMillis cpu_mem_millis;
#endif
6 years ago
enum MDA_EP_PARAM { DECAY, RELEASE, HARDNESS, TREBLE, PAN_TREM, LFO_RATE, VELOCITY_SENSE, STEREO, MAX_POLY, TUNE, DETUNE, OVERDRIVE };
6 years ago
//*************************************************************************************************
//* SETUP FUNCTION
//*************************************************************************************************
6 years ago
void setup()
{
//while (!Serial) ; // wait for Arduino Serial Monitor
Serial.begin(SERIAL_SPEED);
delay(220);
6 years ago
/*
// LCD display setup
lcd.init();
lcd.blink_off();
lcd.cursor_off();
lcd.backlight();
lcd.noAutoscroll();
lcd.clear();
lcd.display();
lcd.show(0, 0, 20, " MicroMDAEPiano");
lcd.show(1, 0, 16, "(c)parasiTstudio");
// Encoder setup
enc[0].write(INITIAL_ENC_L_VALUE);
enc_val[0] = enc[0].read();
enc[1].write(INITIAL_ENC_R_VALUE);
enc_val[1] = enc[1].read();
but[0].update();
but[1].update();
*/
6 years ago
// Debug output
Serial.println(F("MicroMDAEPiano based on https://sourceforge.net/projects/mda-vst"));
Serial.println(F("(c)2018 H. Wirtz <wirtz@parasitstudio.de>"));
6 years ago
Serial.println(F("https://codeberg.org/dcoredump/MicroMDAEPiano"));
Serial.print(F("Data in PROGMEM: "));
Serial.print(sizeof(epianoDataXfade), DEC);
Serial.println(F(" bytes"));
Serial.println();
Serial.println(F("<setup start>"));
6 years ago
// create EPiano object
ep = new mdaEPiano();
6 years ago
// read initial EEPROM variables
initial_values_from_eeprom();
#ifdef MIDI_DEVICE_DIN
// Start serial MIDI
midi_serial.begin(DEFAULT_MIDI_CHANNEL);
midi_serial.setHandleNoteOn(handleNoteOn_MIDI_DEVICE_DIN);
midi_serial.setHandleNoteOff(handleNoteOff_MIDI_DEVICE_DIN);
midi_serial.setHandleControlChange(handleControlChange_MIDI_DEVICE_DIN);
Serial.println(F("MIDI_DEVICE_DIN enabled"));
#endif
// start up USB host
6 years ago
#ifdef MIDI_DEVICE_USB_HOST
usb_host.begin();
midi_usb.setHandleNoteOn(handleNoteOn_MIDI_DEVICE_USB_HOST);
midi_usb.setHandleNoteOff(handleNoteOff_MIDI_DEVICE_USB_HOST);
midi_usb.setHandleControlChange(handleControlChange_MIDI_DEVICE_USB_HOST);
6 years ago
Serial.println(F("MIDI_DEVICE_USB_HOST enabled."));
#endif
// check for onboard USB-MIDI
6 years ago
#ifdef MIDI_DEVICE_USB
midi_onboard_usb.begin();
midi_onboard_usb.setHandleNoteOn(handleNoteOn_MIDI_DEVICE_USB);
midi_onboard_usb.setHandleNoteOff(handleNoteOff_MIDI_DEVICE_USB);
midi_onboard_usb.setHandleControlChange(handleControlChange_MIDI_DEVICE_USB);
6 years ago
Serial.println(F("MIDI_DEVICE_USB enabled."));
#endif
// start audio card
AudioNoInterrupts();
AudioMemory(AUDIO_MEM);
#ifdef TEENSY_AUDIO_BOARD
sgtl5000_1.enable();
//sgtl5000_1.dacVolumeRamp();
sgtl5000_1.dacVolumeRampLinear();
sgtl5000_1.unmuteHeadphone();
sgtl5000_1.unmuteLineout();
sgtl5000_1.autoVolumeDisable(); // turn off AGC
sgtl5000_1.volume(1.0, 1.0);
#endif
#ifdef TGA_AUDIO_BOARD
wm8731_1.enable();
wm8731_1.volume(1.0);
#endif
//set_volume(vol, vol_left, vol_right);
set_volume(1.0, 1.0, 1.0);
#if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
// Initialize processor and memory measurements
AudioProcessorUsageMaxReset();
AudioMemoryUsageMaxReset();
#endif
Serial.print(F("AUDIO_BLOCK_SAMPLES="));
Serial.print(AUDIO_BLOCK_SAMPLES);
Serial.print(F(" (Time per block="));
Serial.print(1000000 / (SAMPLE_RATE / AUDIO_BLOCK_SAMPLES));
Serial.println(F("ms)"));
ep->setParameter(DECAY, 0.5);
ep->setParameter(RELEASE, 0.5);
6 years ago
ep->setParameter(HARDNESS, 0.7);
ep->setParameter(TREBLE, 0.85);
ep->setParameter(DETUNE, 0.1);
ep->setParameter(VELOCITY_SENSE, 1.0);
6 years ago
ep->setParameter(STEREO, 0.7);
ep->setParameter(OVERDRIVE, 0.3);
freeverb_r.roomsize(0.2);
freeverb_l.roomsize(0.2);
freeverb_r.damping(0.5);
freeverb_l.damping(0.5);
mixer_r.gain(0, 0.7);
mixer_l.gain(0, 0.7);
mixer_r.gain(1, 0.3);
mixer_l.gain(1, 0.3);
AudioNoInterrupts();
Serial.println(F("<setup end>"));
#if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
show_cpu_and_mem_usage();
cpu_mem_millis = 0;
#endif
}
6 years ago
//*************************************************************************************************
//* MAIN LOOP
//*************************************************************************************************
void loop()
{
int16_t* audio_buffer_r; // pointer to AUDIO_BLOCK_SAMPLES * sizeof(int16_t)
int16_t* audio_buffer_l; // pointer to AUDIO_BLOCK_SAMPLES * sizeof(int16_t)
const uint16_t audio_block_time_ms = 1000000 / (SAMPLE_RATE / AUDIO_BLOCK_SAMPLES);
// Main sound calculation
if (queue_r.available() && queue_l.available() && fill_audio_buffer > audio_block_time_us - 10)
{
fill_audio_buffer = 0;
#if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
if (cpu_mem_millis > SHOW_CPU_LOAD_MSEC)
{
show_cpu_and_mem_usage();
cpu_mem_millis = 0;
}
#endif
audio_buffer_r = queue_r.getBuffer();
if (audio_buffer_r == NULL)
{
Serial.println(F("E: audio_buffer_r allocation problems!"));
}
audio_buffer_l = queue_l.getBuffer();
if (audio_buffer_l == NULL)
{
Serial.println(F("E: audio_buffer_l allocation problems!"));
}
elapsedMicros t1;
ep->process(audio_buffer_l, audio_buffer_r);
uint32_t t2 = t1;
if (t2 > audio_block_time_ms) // everything greater 2.9ms is a buffer underrun!
xrun++;
if (t2 > render_time_max)
render_time_max = t2;
if (peak_r.available())
{
if (peak_r.read() > 1.00)
peak++;
}
if (peak_l.available())
{
if (peak_l.read() > 1.00)
peak++;
}
#ifndef TEENSY_AUDIO_BOARD
for (uint8_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
{
audio_buffer_r[i] *= vol;
audio_buffer_l[i] *= vol;
}
#endif
queue_r.playBuffer();
queue_l.playBuffer();
}
#ifdef MIDI_DEVICE_DIN
midi_serial.read();
#endif
#ifdef MIDI_DEVICE_USB_HOST
midi_usb.read();
#endif
#ifdef MIDI_DEVICE_USB
midi_onboard_usb.read();
#endif
}
6 years ago
//*************************************************************************************************
//* PROGRAM FUNCTIONS
//*************************************************************************************************
#ifdef MIDI_DEVICE_DIN
void handleNoteOn_MIDI_DEVICE_DIN(byte inChannel, byte inNumber, byte inVelocity)
{
handleNoteOn(byte inChannel, byte inNumber, byte inVelocity);
#ifdef DEBUG
Serial.print(F("[MIDI_DIN] NoteOn"));
#endif
#ifdef MIDI_MERGE_THRU
#ifdef MIDI_DEVICE_USB_HOST
midi_usb.send(midi::NoteOn, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB_HOST"));
#endif
#endif
#ifdef MIDI_DEVICE_USB
midi_onboard_usb.send(midi::NoteOn, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
}
6 years ago
void handleNoteOff_MIDI_DEVICE_DIN(byte inChannel, byte inNumber, byte inVelocity)
{
handleNoteOff(byte inChannel, byte inNumber, byte inVelocity);
#ifdef DEBUG
Serial.print(F("[MIDI_DIN] NoteOff"));
#endif
#ifdef MIDI_MERGE_THRU
#ifdef MIDI_DEVICE_USB_HOST
midi_usb.send(midi::NoteOff, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB_HOST"));
#endif
#endif
#ifdef MIDI_DEVICE_USB
midi_onboard_usb.send(midi::NoteOff, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
}
void handleControlChange_MIDI_DEVICE_DIN(byte inChannel, byte inData1, byte inData2)
{
handleControlChange(byte inChannel, byte inData1, byte inData2);
#ifdef DEBUG
Serial.print(F("[MIDI_DIN] CC"));
#endif
#ifdef MIDI_MERGE_THRU
6 years ago
#ifdef MIDI_DEVICE_USB_HOST
midi_usb.send(midi::ControlChange, inData1, inData2, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB_HOST"));
#endif
6 years ago
#endif
#ifdef MIDI_DEVICE_USB
midi_onboard_usb.send(midi::ControlChange, inData1, inData2, inChannel);
#ifdef DEBUG
Serial.print(F( "THRU->MIDI_USB"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
}
#endif
#ifdef MIDI_DEVICE_USB_HOST
void handleNoteOn_MIDI_DEVICE_USB_HOST(byte inChannel, byte inNumber, byte inVelocity)
{
handleNoteOn(inChannel, inNumber, inVelocity);
#ifdef DEBUG
Serial.print(F("[MIDI_USB_HOST] NoteOn"));
#endif
#ifdef MIDI_MERGE_THRU
#ifdef MIDI_DEVICE_DIN
midi_serial.send(midi::NoteOn, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_DIN"));
#endif
#endif
6 years ago
#ifdef MIDI_DEVICE_USB
midi_onboard_usb.send(midi::NoteOn, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
6 years ago
}
void handleNoteOff_MIDI_DEVICE_USB_HOST(byte inChannel, byte inNumber, byte inVelocity)
{
handleNoteOff(inChannel, inNumber, inVelocity);
#ifdef DEBUG
Serial.print(F("[MIDI_USB_HOST] NoteOff"));
#endif
#ifdef MIDI_MERGE_THRU
#ifdef MIDI_DEVICE_DIN
midi_serial.send(midi::NoteOff, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_DIN"));
#endif
#endif
#ifdef MIDI_DEVICE_USB
midi_onboard_usb.send(midi::NoteOff, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
}
void handleControlChange_MIDI_DEVICE_USB_HOST(byte inChannel, byte inData1, byte inData2)
{
handleControlChange(inChannel, inData1, inData2);
#ifdef DEBUG
Serial.print(F("[MIDI_USB_HOST] CC"));
#endif
#ifdef MIDI_MERGE_THRU
#ifdef MIDI_DEVICE_DIN
midi_serial.send(midi::ControlChange, inData1, inData2, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_DIN"));
#endif
#endif
#ifdef MIDI_DEVICE_USB
midi_onboard_usb.send(midi::ControlChange, inData1, inData2, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
}
#endif
#ifdef MIDI_DEVICE_USB
void handleNoteOn_MIDI_DEVICE_USB(byte inChannel, byte inNumber, byte inVelocity)
{
handleNoteOn(inChannel, inNumber, inVelocity);
#ifdef DEBUG
Serial.print(F("[MIDI_USB] NoteOn"));
#endif
#ifdef MIDI_MERGE_THRU
#ifdef MIDI_DEVICE_DIN
midi_serial.send(midi::NoteOn, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_DIN"));
#endif
#endif
#ifdef MIDI_DEVICE_USB_HOST
midi_usb.send(midi::NoteOn, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB_HOST"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
}
void handleNoteOff_MIDI_DEVICE_USB(byte inChannel, byte inNumber, byte inVelocity)
{
handleNoteOff(inChannel, inNumber, inVelocity);
#ifdef DEBUG
Serial.print(F("[MIDI_USB] NoteOff"));
#endif
#ifdef MIDI_MERGE_THRU
#ifdef MIDI_DEVICE_DIN
midi_serial.send(midi::NoteOff, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_DIN"));
#endif
#endif
#ifdef MIDI_DEVICE_USB_HOST
midi_usb.send(midi::NoteOff, inNumber, inVelocity, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB_HOST"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
}
void handleControlChange_MIDI_DEVICE_USB(byte inChannel, byte inData1, byte inData2)
{
handleControlChange(inChannel, inData1, inData2);
#ifdef DEBUG
Serial.print(F("[MIDI_USB] CC"));
#endif
#ifdef MIDI_MERGE_THRU
#ifdef MIDI_DEVICE_DIN
midi_serial.send(midi::ControlChange, inData1, inData2, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_DIN"));
#endif
#endif
#ifdef MIDI_DEVICE_USB_HOST
midi_usb.send(midi::ControlChange, inData1, inData2, inChannel);
#ifdef DEBUG
Serial.print(F(" THRU->MIDI_USB_HOST"));
#endif
#endif
#ifdef DEBUG
Serial.println();
#endif
#endif
}
#endif
void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity)
{
if (checkMidiChannel(inChannel))
{
ep->noteOn(inNumber, inVelocity);
}
}
void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity)
{
if (checkMidiChannel(inChannel))
{
ep->noteOn(inNumber, 0);
}
}
void handleControlChange(byte inChannel, byte inData1, byte inData2)
{
if (checkMidiChannel(inChannel))
{
ep->processMidiController(inData1, inData2);
}
}
bool checkMidiChannel(byte inChannel)
{
// check for MIDI channel
if (inChannel == MIDI_CHANNEL_OMNI)
{
return (true);
}
else if (inChannel != midi_channel)
{
#ifdef DEBUG
Serial.print(F("Ignoring MIDI data on channel "));
Serial.print(inChannel);
Serial.print(F("(listening on "));
Serial.print(midi_channel);
Serial.println(F(")"));
#endif
return (false);
}
return (true);
}
void set_volume(float v, float vr, float vl)
{
vol = v;
vol_right = vr;
vol_left = vl;
EEPROM.update(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR, uint8_t(vol * UCHAR_MAX));
EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR, uint8_t(vol_right * UCHAR_MAX));
EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR, uint8_t(vol_left * UCHAR_MAX));
update_eeprom_checksum();
#ifdef DEBUG
uint8_t tmp;
Serial.print(F("Setting volume: VOL="));
Serial.print(v, DEC);
Serial.print(F("["));
tmp = EEPROM.read(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR);
Serial.print(tmp, DEC);
Serial.print(F("/"));
Serial.print(float(tmp) / UCHAR_MAX, DEC);
Serial.print(F("] VOL_L="));
Serial.print(vl, DEC);
Serial.print(F("["));
tmp = EEPROM.read(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR);
Serial.print(tmp, DEC);
Serial.print(F("/"));
Serial.print(float(tmp) / UCHAR_MAX, DEC);
Serial.print(F("] VOL_R="));
Serial.print(vr, DEC);
Serial.print(F("["));
tmp = EEPROM.read(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR);
Serial.print(tmp, DEC);
Serial.print(F("/"));
Serial.print(float(tmp) / UCHAR_MAX, DEC);
Serial.println(F("]"));
#endif
#ifdef TEENSY_AUDIO_BOARD
sgtl5000_1.dacVolume(vol * vol_left, vol * vol_right);
#elif TGA_AUDIO_BOARD
wm8731_1.volume(vol);
#else
volume_r.gain(vr);
volume_l.gain(vl);
#endif
}
6 years ago
//*************************************************************************************************
//* EEPROM FUNCTIONS
//*************************************************************************************************
void initial_values_from_eeprom(void)
{
uint32_t crc_eeprom = read_eeprom_checksum();
uint32_t crc = eeprom_crc32(EEPROM_OFFSET, EEPROM_DATA_LENGTH);
#ifdef DEBUG
Serial.print(F("EEPROM checksum: 0x"));
Serial.print(crc_eeprom, HEX);
Serial.print(F(" / 0x"));
Serial.print(crc, HEX);
#endif
if (crc_eeprom != crc)
{
#ifdef DEBUG
Serial.print(F(" - mismatch -> initializing EEPROM!"));
#endif
EEPROM.update(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR, uint8_t(vol * UCHAR_MAX));
EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR, uint8_t(vol_right * UCHAR_MAX));
EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR, uint8_t(vol_left * UCHAR_MAX));
update_eeprom_checksum();
}
else
{
vol = float(EEPROM.read(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR)) / UCHAR_MAX;
vol_right = float(EEPROM.read(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR)) / UCHAR_MAX;
vol_left = float(EEPROM.read(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR)) / UCHAR_MAX;
}
#ifdef DEBUG
Serial.println();
#endif
}
uint32_t read_eeprom_checksum(void)
{
return (EEPROM.read(EEPROM_CRC32_ADDR) << 24 | EEPROM.read(EEPROM_CRC32_ADDR + 1) << 16 | EEPROM.read(EEPROM_CRC32_ADDR + 2) << 8 | EEPROM.read(EEPROM_CRC32_ADDR + 3));
}
void update_eeprom_checksum(void)
{
write_eeprom_checksum(eeprom_crc32(EEPROM_OFFSET, EEPROM_DATA_LENGTH)); // recalculate crc and write to eeprom
}
void write_eeprom_checksum(uint32_t crc)
{
EEPROM.update(EEPROM_CRC32_ADDR, (crc & 0xff000000) >> 24);
EEPROM.update(EEPROM_CRC32_ADDR + 1, (crc & 0x00ff0000) >> 16);
EEPROM.update(EEPROM_CRC32_ADDR + 2, (crc & 0x0000ff00) >> 8);
EEPROM.update(EEPROM_CRC32_ADDR + 3, crc & 0x000000ff);
}
uint32_t eeprom_crc32(uint16_t calc_start, uint16_t calc_bytes) // base code from https://www.arduino.cc/en/Tutorial/EEPROMCrc
{
const uint32_t crc_table[16] =
{
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
uint32_t crc = ~0L;
if (calc_start + calc_bytes > EEPROM.length())
calc_bytes = EEPROM.length() - calc_start;
for (uint16_t index = calc_start ; index < (calc_start + calc_bytes) ; ++index)
{
crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);
crc = ~crc;
}
return (crc);
}
6 years ago
//*************************************************************************************************
//* DEBUG FUNCTIONS
//*************************************************************************************************
#if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
void show_cpu_and_mem_usage(void)
{
Serial.print(F("CPU: "));
Serial.print(AudioProcessorUsage(), DEC);
Serial.print(F(" CPU MAX: "));
Serial.print(AudioProcessorUsageMax(), DEC);
Serial.print(F(" MEM: "));
Serial.print(AudioMemoryUsage(), DEC);
Serial.print(F(" MEM MAX: "));
Serial.print(AudioMemoryUsageMax(), DEC);
Serial.print(F(" RENDER_TIME_MAX: "));
Serial.print(render_time_max, DEC);
Serial.print(F(" XRUN: "));
Serial.print(xrun, DEC);
Serial.print(F(" OVERLOAD: "));
Serial.print(overload, DEC);
Serial.print(F(" PEAK: "));
Serial.print(peak, DEC);
Serial.println();
AudioProcessorUsageMaxReset();
AudioMemoryUsageMaxReset();
render_time_max = 0;
}
#endif