EPPL example libraries  0.1 alpha
 All Files Functions Variables Groups Pages
base/main.c

The main example file.

/*
Copyright (c) 2014, Radoslaw Koppel
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/fuse.h>
#include <util/atomic.h>
#include "config.h"
#include "macros.h"
#include "eppl.h"
#include "alcddrv.h"
#include "spwm.h"
FUSES =
{
(FUSE_SUT0 & FUSE_CKSEL3 & FUSE_CKSEL1 & FUSE_CKSEL0),
(FUSE_SPIEN & FUSE_BOOTSZ1 & FUSE_BOOTSZ0)
};
volatile uint16_t g_animDelay;
const __flash uint8_t g_ledColorTable[][3] =
{
{ 0, 0, 0},
{255, 0, 0},
{255,127, 0},
{255,255, 0},
{ 0,255, 0},
{ 0,255,255},
{ 0, 0,255},
{127, 0,255},
{255, 0,255},
{255,255,255}
};
uint8_t g_animColor = 0;
uint8_t g_animPos = 0;
static inline uint8_t getButtonState(void)
{
return ( (0 == eppl_mapIn(BUTTON1)) ? 1 : 0 );
}
static inline uint8_t animMerge(uint8_t pos, uint8_t from, uint8_t to)
{
return
((uint16_t)from*(ANIMATION_STEPS-pos) +
(uint16_t)to*pos)
/
ANIMATION_STEPS;
}
static inline void animDelaySet(double time) __attribute__((always_inline));
static inline void animDelaySet(double time) __attribute__((always_inline));
void animDelaySet(double time)
{
uint16_t val;
time *= 31250.0;
if(time >= UINT16_MAX)
val = UINT16_MAX;
else if(time <= 1.0)
val = 1;
else
val = (uint16_t)time;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
g_animDelay = val;
}
}
static inline uint8_t animDelayCheck(void)
{
uint8_t ret;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
ret = (0 == g_animDelay) ? 1 : 0;
}
return ret;
}
int main(void)
{
unsigned int loopcnt = 0;
/* Initialize all IO pins */
EPPL_mode_inPU, /* default */
ALCD_PINCONFIG, /* LCD config */
SPWM_PINCONFIG, /* LED */
EPPL_SC(BUTTON1, EPPL_mode_inPU) /* button */
);
/* Setup timer for SPWM */
TIMSK = 1<<TOIE0;
TCCR0 = 1<<CS00;
/* Enable interrupts */
sei();
/* Init LCD */
alcd_turnOnOff(1, 0, 0);
/* Write text on first line */
alcd_puts_P(PSTR("Hello! abc1234567890"));
/* Main loop starts there */
while(1)
{
/* ************************************ */
/* Process LCD */
char buffer[ALCD_SX+1];
uint8_t n;
/* Read first line */
alcd_goto(0, 0);
alcd_read(buffer, ALCD_SX);
buffer[ALCD_SX] = 0;
/* Write it back */
alcd_goto(0, 0);
alcd_puts(buffer);
/* Print loop counter */
alcd_goto(1, 0);
alcd_puts_P(PSTR("Loop: "));
utoa(loopcnt, buffer, 10);
alcd_puts(buffer);
/* Fill with spaces all places that may be used by
* previous loop number
* (we are not cleaning LCD before writting) */
for(n=strlen(buffer); n<5; n++)
alcd_putc(' ');
/* Constant text on 3-rd line, but send every loop */
alcd_goto(2, 0);
alcd_puts_P(PSTR("Third test line..."));
/* Button state on last line */
alcd_goto(3, 0);
if(getButtonState())
alcd_puts_P(PSTR("BUTTON1: Pressed "));
else
alcd_puts_P(PSTR("BUTTON1: Released"));
/* ************************************ */
/* Process RGB animation */
if(animDelayCheck())
{
const __flash uint8_t *pfrom, *pto;
uint8_t n;
animDelaySet(ANIMATION_FRAME_TIME/ANIMATION_STEPS);
/* Data to start from pointer, is a pointer to current color processed */
pfrom = &g_ledColorTable[g_animColor][0];
/* Data to go to pointer, is a pointer to next current color to be processed,
* It is current position incremented by 1, but warps around when table finishes */
pto = &g_ledColorTable[(g_animColor+1)%ELEMS(g_ledColorTable)][0];
/* Process every one color now (RGB bytes) */
for(n=0; n<3; n++)
spwm_set(n, animMerge(g_animPos, *pfrom++, *pto++));
/* Process animation position */
if(++g_animPos >= ANIMATION_STEPS)
{
g_animPos = 0;
if(++g_animColor >= ELEMS(g_ledColorTable))
g_animColor = 0;
}
}
/* Loop counter */
++loopcnt;
}
return 0;
}
ISR(TIMER0_OVF_vect)
{
/* Variable used to speed up operations on volatile variable */
uint16_t animDelay = g_animDelay;
if(0 < animDelay)
g_animDelay = g_animDelay - 1;
}