Skip to content

Commit

Permalink
implement delayTask
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed Jul 10, 2020
1 parent b91a957 commit a03403f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 41 deletions.
68 changes: 35 additions & 33 deletions BoxDAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,17 @@ void BoxDAC::begin() {
}

void BoxDAC::loop() {
fillBuffer(50);
fillBuffer(100);
}
void BoxDAC::fillBuffer(uint16_t timeoutMs) {
BoxTimer timeout;
timeout.setTimer(timeoutMs);
unsigned int bufferFree;
unsigned int bufferFree = GetBufferEmptySize(pPlayBuffer);
//if (bufferFree > PLAY_BUFFER_SIZE-(PLAY_BUFFER_SIZE/20))
// Log.info("Playbuffer (nearly) empty (%i/%i)", PLAY_BUFFER_SIZE-bufferFree, PLAY_BUFFER_SIZE);
//if (bufferFree < 256)
// Log.info("Playbuffer (nearly) full (%i/%i)", PLAY_BUFFER_SIZE-bufferFree, PLAY_BUFFER_SIZE);
while(timeout.isRunning()) {
bufferFree = GetBufferEmptySize(pPlayBuffer);
if (bufferFree<4) //Crashes when full?
break;

Expand All @@ -229,9 +232,8 @@ void BoxDAC::fillBuffer(uint16_t timeoutMs) {

count++;
timeout.tick();
bufferFree = GetBufferEmptySize(pPlayBuffer);
}
//if (bufferFree>4)
// Log.info("bufferFree=%i", bufferFree);
}

void BoxDAC::dmaPingPingComplete() {
Expand Down Expand Up @@ -277,57 +279,57 @@ void BoxDAC::beepTest() {
uint16_t pauseLen = 50;
uint8_t baseNote = 48 + 1*12;
beepMidi(baseNote, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+2, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+4, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+5, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+7, 2*pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+7, 2*pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+9, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+9, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+9, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+9, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+7, 4*pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+9, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+9, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+9, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+9, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+7, 4*pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+5, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+5, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+5, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+5, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+4, 2*pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+4, 2*pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+7, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+7, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+7, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote+7, pauseLen);
delay(pauseLen);
Box.delayTask(pauseLen);
beepMidi(baseNote, 4*pauseLen);
}

Expand Down Expand Up @@ -435,7 +437,7 @@ bool BoxDAC::increaseVolume() {
result = true;
} else {
beepRaw(0x30F9, 0x763F, 0x000140); //16kHz
delay(50);
Box.delayTask(50);
beepRaw(0x30F9, 0x763F, 0x000140); //16kHz
//beepMidi(84,50,true);
Log.info("Maximum volume reached.");
Expand All @@ -453,7 +455,7 @@ bool BoxDAC::decreaseVolume() {
result = true;
} else {
beepRaw(0x0F0A, 0x7F1A, 0x000140); //16kHz
delay(50);
Box.delayTask(50);
beepRaw(0x0F0A, 0x7F1A, 0x000140); //16kHz
//beepMidi(62, 50, true);
Log.info("Minimal volume reached.");
Expand Down
2 changes: 1 addition & 1 deletion BoxDAC.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BoxDAC : public EnhancedThread {
void dmaPingPingComplete();
const static uint16_t I2S_PACKET_SIZE = 2 * 256; //TODO
const static uint16_t I2S_PACKET_ELEMENTS = I2S_PACKET_SIZE / 2;
const static uint16_t PLAY_BUFFER_SIZE = 10*I2S_PACKET_SIZE; //TODO //70 in Example
const static uint16_t PLAY_BUFFER_SIZE = 32*I2S_PACKET_SIZE; //TODO //70 in Example

unsigned char aZeroBuffer[I2S_PACKET_SIZE];
tCircularBuffer* pPlayBuffer;
Expand Down
11 changes: 7 additions & 4 deletions BoxRFID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void BoxRFID::loop() {
// The VCD should wait at least 1 ms after it activated the
// powering field before sending the first request, to
// ensure that the VICCs are ready to receive it. (ISO15693-3)
delay(20); //not 1 ms?!
Box.delayTask(20); //not 1 ms?!
ISO15693_RESULT result;
uint32_t knownPasswords[3] = { 0x7FFD6E5B, 0x0F0F0F0F, 0x00000000 };

Expand All @@ -48,7 +48,7 @@ void BoxRFID::loop() {
} else if (result == ISO15693_RESULT::SET_PASSWORD_INCORRECT) {
Log.info("Password %X (i=%i) was incorrect", knownPasswords[i], i);
writeRegister(REGISTER::CHIP_STATUS_CONTROL, 0b00000001); //turnRfOff();
delay(20);
Box.delayTask(20);
reinitRFID();
} else {
break;
Expand Down Expand Up @@ -496,7 +496,7 @@ void BoxRFID::reinitRFID() {
trfRxLength = 0;
trfStatus = TRF_STATUS::TRF_IDLE;
writeRegister(REGISTER::CHIP_STATUS_CONTROL, 0b00100001); //turnRfOn();
delay(20);
Box.delayTask(20);
}

uint8_t BoxRFID::readIrqRegister() {
Expand Down Expand Up @@ -548,6 +548,7 @@ void BoxRFID::waitTxIRQ(uint8_t txTimeout) {
BoxTimer timer;
timer.setTimer(txTimeout);
while (!readInterrupt() && timer.isRunning()) {
Box.delayTask(1);
timer.tick();
}
if (!timer.isRunning()) {
Expand All @@ -571,6 +572,7 @@ void BoxRFID::waitRxIRQ(uint8_t rxTimeout) {
BoxTimer timer;
timer.setTimer(rxTimeout);
while (!readInterrupt() && timer.isRunning()) {
Box.delayTask(1);
timer.tick();
}
if (!timer.isRunning()) {
Expand All @@ -581,6 +583,7 @@ void BoxRFID::waitRxIRQ(uint8_t rxTimeout) {
clearInterrupt();
timer.setTimer(5); //from firmware example
while (!readInterrupt() && timer.isRunning()) {
Box.delayTask(1);
timer.tick();
}
if (!timer.isRunning()) {
Expand Down Expand Up @@ -611,7 +614,7 @@ void BoxRFID::resetRFID() {
//Log.info("resetRFID();");
sendCommand(DIRECT_COMMANDS::SOFT_INIT);
sendCommand(DIRECT_COMMANDS::IDLING);
delay(1);
Box.delayTask(1);
clearInterrupt();
sendCommand(DIRECT_COMMANDS::RESET_FIFO);
trfOffset = 0;
Expand Down
10 changes: 8 additions & 2 deletions BoxTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ void BoxTimer::tick() {
if (!_isRunning) {
_hasChanged = false;
} else {
unsigned long currentMillis = millis();
if (_endMillis <= currentMillis) {
_currentMillis = millis();
if (_endMillis <= _currentMillis) {
_isRunning = false;
_hasChanged = true;
}
}
}

unsigned long BoxTimer::getTimeTillEnd() {
if (!_isRunning)
return 0;
return _endMillis - _currentMillis;
}

bool BoxTimer::isRunning() {
return _isRunning;
}
Expand Down
2 changes: 2 additions & 0 deletions BoxTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ class BoxTimer {
void tick();
bool isRunning();
bool wasRunning();
unsigned long getTimeTillEnd();
private:
bool _isRunning;
bool _hasChanged;
unsigned long _currentMillis;
unsigned long _endMillis;
};

Expand Down
23 changes: 22 additions & 1 deletion Hackiebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ void Hackiebox::setup() {
watchdog_stop();
//reset box?!
}


inDelayTask = true;
logStreamMulti.setSlot(&logStreamSd, 0);
logStreamMulti.setSlot(&logStreamSse, 1);
Log.init(LOG_LEVEL_VERBOSE, 115200, &logStreamMulti);
Expand Down Expand Up @@ -88,6 +89,26 @@ void Hackiebox::setup() {

boxLEDs.defaultIdleAnimation();
Log.info("Hackiebox started! Free MEM=%ib...", freeMemory());
inDelayTask = false;
}

void Hackiebox::delayTask(uint16_t millis) {
if (!inDelayTask) {
inDelayTask = true;
BoxTimer timer;
timer.setTimer(millis);

//Work start
boxDAC.fillBuffer(millis);
//Work end

timer.tick();
delay(timer.getTimeTillEnd());
//Log.info("delayTask(%i), restDelay=%i", millis, timer.getTimeTillEnd());
inDelayTask = false;
} else {
delay(millis);
}
}

void Hackiebox::loop() {
Expand Down
3 changes: 3 additions & 0 deletions Hackiebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class Hackiebox {
watchdog_feed(),
watchdog_unfeed();

bool inDelayTask;
void delayTask(uint16_t millis);


ThreadController threadController;

Expand Down

0 comments on commit a03403f

Please sign in to comment.