Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: BMP085Test
Fetching contributors…

Cannot retrieve contributors at this time

97 lines (80 sloc) 2.829 kb
//*****************************************************************************
//
// File Name : 'queue.c'
// Title : Queuing system for executing tasks at a given scheduled
// time.
// Original Author : Brad Luyster, LVL1 White Star Balloon Project
// Created : 2010.12.2
// Modified : I won't keep up with this
// Target MCU : Atmel AVR series
//
// http://www.whitestarballoon.com/
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
//*****************************************************************************
// TODO
//
// - Failure mode for full queue
// - Flush Queue function
//*****************************************************************************
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include "dataStructures.h"
#include "queue.h"
scheduledFunction scheduledFunctionArray[QUEUEDEPTH] = {NULL};
uint32_t scheduledFunctionTimes[QUEUEDEPTH];
//This is a circular buffer. Initialize start and end this way.
uint8_t queueStart=0;
uint8_t queueEnd=0;
void scheduleQueueAdd(scheduledFunction thisFunction, uint32_t epochTime)
{
//Honestly, this algorithm is straight from wikipedia.
if ((queueEnd + 1) % QUEUEDEPTH != queueStart) {
scheduledFunctionArray[queueEnd] = thisFunction;
scheduledFunctionTimes[queueEnd] = epochTime;
queueEnd = (queueEnd + 1) % QUEUEDEPTH;
} else {
lprintf("Q BUF FULL\n");
}
}
int8_t scheduleQueueGetTop(scheduledFunction *returnFunction, uint32_t *returnTime)
{
if (queueEnd != queueStart) {
scheduledFunction tempF = scheduledFunctionArray[queueStart];
uint32_t tempT = scheduledFunctionTimes[queueStart];
//This Algorithm too.
queueStart = (queueStart + 1) % QUEUEDEPTH;
*returnFunction = tempF;
*returnTime = tempT;
return 0;
}
//otherwise, the buffer is empty; return an error code
return 1;
}
int8_t scheduleQueueAlterTime(scheduledFunction thisFunction, uint32_t newTime)
{
for(int i = 0; i < QUEUEDEPTH; i++)
{
uint32_t scheduleTime;
scheduledFunction storageFunction;
volatile int8_t error;
error = scheduleQueueGetTop(&storageFunction, &scheduleTime);
//lprintf("PTR: %p time: %lud now: %lud\n", ptrToFunction, scheduleTime, rightNow);
if(error == 0 && storageFunction == thisFunction)
{
/*#ifdef FCPUDEBUG
lprintf_P(PSTR("Running some function\n"));
#endif*/
scheduleQueueAdd(storageFunction, newTime);
return 0;
} else {
scheduleQueueAdd(storageFunction, scheduleTime);
}
}
return 1;
}
Jump to Line
Something went wrong with that request. Please try again.