Skip to content

Commit

Permalink
# Version 0.6.1
Browse files Browse the repository at this point in the history
# committed missed nixieos files
  • Loading branch information
lexus2k committed Jul 3, 2017
1 parent bbebe24 commit fca4952
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doxygen.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "Nixie Library"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 0.6.0
PROJECT_NUMBER = 0.6.1

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=NixieLibrary
version=0.6.0
version=0.6.1
author=Alexey Dynda
maintainer=Alexey Dynda <[email protected]>
sentence=Nixie Library is intended for Nixie Clock software development.
Expand Down
270 changes: 270 additions & 0 deletions src/nixieos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
/*
Copyright (C) 2016-2017 Alexey Dynda
This file is part of Nixie Library.
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, see <http://www.gnu.org/licenses/>.
*/

#include "nixieos.h"

// SECTION ======================= TIME ==================================

/* 8 bytes */
uint32_t g_nixieUsEx = 0;
uint32_t g_nixieMsEx = 0;

NixieOsTime __currentTime;

namespace NixieOs
{

/*
void setTimeEx(uint8_t hours, uint8_t minutes, uint8_t seconds)
{
__currentTime.hours = hours;
__currentTime.minutes = minutes;
__currentTime.seconds = seconds;
}
void setTime(NixieOsTime timeRec)
{
__currentTime = timeRec;
}
void getTime(NixieOsTime *timeRec)
{
*timeRec = __currentTime;
} */

/**
* the function is intended for internal use only
*/
void updateTime() __attribute__((weak));
void updateTime()
{

}


}


// SECTION ======================= EVENTS ================================

/* 8 bytes */
static SNixieEvent s_events[MAX_NIXIE_EVENTS];

namespace NixieOs
{

bool sendEvent(uint8_t id, uint8_t param)
{
for (uint8_t i=0; i<MAX_NIXIE_EVENTS; i++)
{
if (s_events[i].event == 0)
{
s_events[i].event = id;
s_events[i].param = param;
return true;
}
}
return false;
}

}

// SECTION ======================= TASKS =================================


void defaultEventHandler(SNixieEvent &event) __attribute__((weak));
void defaultEventHandler(SNixieEvent &event)
{
}


static void emptyEnterFunction() {};
static void emptyStateFunction() {};
static void emptyEventFunction(SNixieEvent &event) { defaultEventHandler(event); };
static void emptyExitFunction() {};

/* (2+2+2+1) + 2 + 1 = 10 bytes */
static SNixieStateInfo s_activeTask = NIXIEOS_TASK(TASK_ID_INVALID, empty);
static const SNixieStateInfo *s_tasks;
static uint8_t s_popTask;

namespace NixieOs
{

void pushTask(uint8_t id)
{
s_popTask = s_activeTask.id;
switchTask(id);
}

void popTask()
{
switchTask(s_popTask);
}

void switchTask(uint8_t id)
{
uint8_t i=0;
for(;;)
{
uint8_t nextId = pgm_read_byte(&s_tasks[i].id);
if (nextId == TASK_ID_INVALID)
{
break;
}
if (nextId == id)
{
s_activeTask.id = nextId;
s_activeTask.enterCb = pgm_read_ptr(&s_tasks[i].enterCb);
s_activeTask.stateCb = pgm_read_ptr(&s_tasks[i].stateCb);
s_activeTask.eventCb = pgm_read_ptr(&s_tasks[i].eventCb);
if (s_activeTask.enterCb)
{
s_activeTask.enterCb();
}
break;
}
i++;
};
}


}

// SECTION ======================= TIMERS =================================

#define NIXIEOS_MAX_TIMERS 2
/* 9*2 = 18 bytes */

/**
* Describes NixieOs timer state
*/
typedef struct
{
/// true if timer is active
bool aimed;
/// timestamp of timer activation
uint32_t startMs;
/// timeout in milliseconds
uint32_t timeoutMs;
} STimerInfo;

static STimerInfo s_timers[NIXIEOS_MAX_TIMERS];

namespace NixieOs
{

void resetTimer(uint8_t id)
{
s_timers[id].startMs = g_nixieMsEx;
}


void stopTimer(uint8_t id)
{
s_timers[id].aimed = false;
}


void startTimer(uint8_t id, uint32_t timeoutMs)
{
s_timers[id].aimed = true;
s_timers[id].startMs = g_nixieMsEx;
s_timers[id].timeoutMs = timeoutMs;
}

void restartTimer(uint8_t id)
{
s_timers[id].aimed = true;
s_timers[id].startMs = g_nixieMsEx;
}

}


// SECTION ======================= ENGINE =================================

void initNixieBooster();

namespace NixieOs
{

void setup(const SNixieStateInfo tasks[])
{
initNixieBooster();
s_tasks = tasks;
refresh();
}


void loop()
{
for(;;)
{
run();
}
}


void refresh()
{
g_nixieMsEx = millis();
g_nixieUsEx = micros();
}


void run()
{
uint8_t lms = (uint8_t)g_nixieMs;
g_nixieMsEx = millis();
g_nixieUsEx = micros();
/* Recalculate timers only when milliseconds counter changes */
if (lms != (uint8_t)g_nixieMs)
{
for(uint8_t id=0; id<NIXIEOS_MAX_TIMERS; id++)
{
if (s_timers[id].aimed)
{
if (g_nixieMsEx - s_timers[id].startMs > s_timers[id].timeoutMs)
{
NixieOs::sendEvent(NIXIEOS_EVENT_TIMEOUT, id);
s_timers[id].aimed = false;
}
}
}
if (lms ^ (uint8_t)g_nixieMs & 0x80)
{
updateTime();
}
}
if (s_events[0].event != 0)
{
SNixieEvent event = s_events[0];
for (uint8_t i=0; i<MAX_NIXIE_EVENTS - 1; i++)
{
s_events[i] = s_events[i+1];
}
s_events[MAX_NIXIE_EVENTS - 1].event = 0;
s_activeTask.eventCb(event);
}
s_activeTask.stateCb();
}

}

105 changes: 105 additions & 0 deletions src/nixieos_booster.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright (C) 2016-2017 Alexey Dynda
This file is part of Nixie Library.
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, see <http://www.gnu.org/licenses/>.
*/

#include <arduino.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <nixieos_booster.h>

#if defined(__AVR_ATmega328P__) && !defined(NIXIE_BOOSTER_DISABLE)

#define MAX_ADC_PINS 9
#define ADC_INVALID_VALUE -2

extern uint8_t analog_reference;
volatile static int adcValue[MAX_ADC_PINS];
volatile static uint8_t activePin = 0;

/*ADC Conversion Complete Interrupt Service Routine (ISR)*/
ISR(ADC_vect)
{
adcValue[activePin] = ADCL | (ADCH << 8); // Output ADCH to PortD
uint8_t pin = activePin;
do
{
if (++activePin >= MAX_ADC_PINS) activePin = 0;
/* if somebody requested another pin */
if (adcValue[activePin] == ADC_IN_PROGRESS)
{
/* start next conversion */
ADMUX = (analog_reference << 6) | activePin; // 1 = DEFAULT AVCC REF
ADCSRA |= 1<<ADSC;
break;
}
} while (pin != activePin);
}

namespace NixieOs
{

int analogRead(uint8_t pin)
{
if (pin >= 14) pin-=14;
/* Wait for previous conversion to stop */
cli();
if (adcValue[pin] == ADC_INVALID_VALUE)
{
adcValue[pin] = ADC_IN_PROGRESS;
/* If convertor is not running */
if (!(ADCSRA & (1<<ADSC)))
{
/* Start processing */
ADCSRA = (1<<ADEN) | (1<<ADIE) | (1<<ADPS2) | (0<<ADPS1);
ADMUX = (analog_reference << 6) | pin; // 1 = DEFAULT AVCC REF
sei();
activePin = pin;
ADCSRA |= 1<<ADSC;
return ADC_IN_PROGRESS;
}
}
sei();
if (adcValue[pin] == ADC_IN_PROGRESS)
{
return ADC_IN_PROGRESS;
}
int temp = adcValue[pin];
adcValue[pin] = ADC_INVALID_VALUE;
return temp;
}

}


void initNixieBooster()
{
for(uint8_t pin=0; pin<MAX_ADC_PINS; pin++)
{
adcValue[pin] = ADC_INVALID_VALUE;
}
}


#else

void initNixieBooster()
{
}


#endif
Loading

0 comments on commit fca4952

Please sign in to comment.