Browse Source

Cycle through "animations" on button click

master
Emil Renner Berthing 5 years ago
parent
commit
9651c10bcb
1 changed files with 198 additions and 44 deletions
  1. +198
    -44
      src/main.c

+ 198
- 44
src/main.c View File

@ -5,33 +5,26 @@
#include "em_cmu.h"
#include "hw_pinout.h"
volatile uint32_t msTicks; /* counts 1ms timeTicks */
#define ARRAYLEN(a) (sizeof(a) / sizeof(a[0]))
void Delay(uint32_t dlyTicks);
volatile uint32_t msTicks; /* counts 1ms timeTicks */
static void delay(uint32_t dlyTicks);
void SysTick_Handler(void)
{
msTicks++; /* increment counter necessary in Delay()*/
msTicks++; /* increment counter necessary in delay()*/
}
void Delay(uint32_t dlyTicks)
static void delay(uint32_t dlyTicks)
{
uint32_t curTicks;
curTicks = msTicks;
while ((msTicks - curTicks) < dlyTicks) ;
}
uint32_t curTicks = msTicks;
void Blink(uint32_t port,uint32_t pin, uint32_t dlyTicks)
{
GPIO_PinOutToggle(port, pin);
Delay(dlyTicks);
GPIO_PinOutToggle(port, pin);
Delay(dlyTicks);
while ((msTicks - curTicks) < dlyTicks)
/* do nothing */;
}
int gpio_init(void)
static int gpio_init(void)
{
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
@ -47,45 +40,206 @@ int gpio_init(void)
}
int leds[] = {
static int leds[] = {
LED2,
LED4,
LED1,
LED4,
LED3,
};
int main()
static void debounce(void)
{
int currled=0;
CHIP_Init();
do {
delay(100);
} while (!GPIO_PinInGet(pins[USR1].port, pins[USR1].pin));
}
// Set systick to 1ms interval
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1);
gpio_init();
static void clear(void)
{
unsigned int i;
while(1)
{
pinout_t led = pins[leds[currled]];
while (!GPIO_PinInGet(pins[USR2].port, pins[USR2].pin))
{
GPIO_PinOutToggle(pins[LED1].port, pins[LED1].pin);
GPIO_PinOutToggle(pins[LED2].port, pins[LED2].pin);
GPIO_PinOutToggle(pins[LED3].port, pins[LED3].pin);
GPIO_PinOutToggle(pins[LED4].port, pins[LED4].pin);
Delay(500);
}
for (i = 0; i < ARRAYLEN(leds); i++) {
pinout_t led = pins[leds[i]];
GPIO_PinOutClear(led.port, led.pin);
}
}
static void setall(void)
{
unsigned int i;
for (i = 0; i < ARRAYLEN(leds); i++) {
pinout_t led = pins[leds[i]];
GPIO_PinOutSet(led.port, led.pin);
}
}
static void alllit(void)
{
setall();
while (GPIO_PinInGet(pins[USR1].port, pins[USR1].pin))
/* do nothing */;
}
static void circle(void)
{
unsigned int currled = 0;
clear();
while (GPIO_PinInGet(pins[USR1].port, pins[USR1].pin)) {
pinout_t led = pins[leds[currled++]];
currled %= ARRAYLEN(leds);
GPIO_PinOutSet(led.port, led.pin);
delay(100);
GPIO_PinOutClear(led.port, led.pin);
}
}
static void circle2(void)
{
unsigned int currled = 0;
clear();
while (GPIO_PinInGet(pins[USR1].port, pins[USR1].pin)) {
pinout_t led = pins[leds[currled++]];
currled %= ARRAYLEN(leds);
if (!GPIO_PinInGet(pins[USR1].port, pins[USR1].pin))
{
Blink(led.port, led.pin, 500);
GPIO_PinOutToggle(led.port, led.pin);
delay(100);
}
}
}else{
currled = (currled + 1) % 4;
static void freqs(void)
{
unsigned int state = 0;
unsigned int led = 0;
clear();
while (GPIO_PinInGet(pins[USR1].port, pins[USR1].pin)) {
state++;
GPIO_PinOutToggle(pins[led].port, pins[led].pin);
if (state % 2 == 0)
GPIO_PinOutToggle(pins[(led + 1) % ARRAYLEN(leds)].port, pins[(led + 1) % ARRAYLEN(leds)].pin);
if (state % 4 == 0)
GPIO_PinOutToggle(pins[(led + 2) % ARRAYLEN(leds)].port, pins[(led + 2) % ARRAYLEN(leds)].pin);
if (state % 8 == 0)
GPIO_PinOutToggle(pins[(led + 3) % ARRAYLEN(leds)].port, pins[(led + 3) % ARRAYLEN(leds)].pin);
delay(50);
state %= 64;
if (state == 0)
led = (led + 1) % ARRAYLEN(leds);
}
}
Blink(led.port, led.pin, 500);
static void heartbeat(void)
{
unsigned int i = 0;
clear();
while (GPIO_PinInGet(pins[USR1].port, pins[USR1].pin)) {
i++;
if (i % 10 == 0)
setall();
if (i % 11 == 0)
clear();
if (i % 12 == 0)
setall();
if (i % 13 == 0) {
clear();
i = 0;
}
delay(100);
}
}
return 0;
};
static void sos(void)
{
unsigned int i = 0;
clear();
while (GPIO_PinInGet(pins[USR1].port, pins[USR1].pin)) {
if (i == 0)
setall();
if (i == 1)
clear();
if (i == 2)
setall();
if (i == 3)
clear();
if (i == 4)
setall();
if (i == 5)
clear();
if (i == 6)
setall();
if (i == 9)
clear();
if (i == 10)
setall();
if (i == 13)
clear();
if (i == 14)
setall();
if (i == 17)
clear();
if (i == 18)
setall();
if (i == 19)
clear();
if (i == 20)
setall();
if (i == 21)
clear();
if (i == 22)
setall();
if (i == 23)
clear();
i++;
if (i == 32)
i = 0;
delay(100);
}
}
static void off(void)
{
clear();
while (GPIO_PinInGet(pins[USR1].port, pins[USR1].pin)) {
delay(50);
}
}
int main(void)
{
CHIP_Init();
// Set systick to 1ms interval
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000))
while (1);
gpio_init();
while (1) {
alllit();
debounce();
circle();
debounce();
circle2();
debounce();
freqs();
debounce();
heartbeat();
debounce();
sos();
debounce();
off();
debounce();
}
__builtin_unreachable();
}

Loading…
Cancel
Save