Created
June 4, 2017 03:53
-
-
Save ryankurte/06f0fde97ccbbc0f8cb5ee75674acf06 to your computer and use it in GitHub Desktop.
OzLockCon Badge Morse Firmware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
#include <util/delay.h> | |
// Board Definitions | |
#define LED0_PIN 0 | |
#define LED1_PIN 3 | |
#define SW_PIN 2 | |
// IO Helpers | |
#define IO_PORT PORTB | |
#define LED0_MASK (1 << LED0_PIN) | |
#define LED1_MASK (1 << LED1_PIN) | |
#define SW_MASK (1 << SW_PIN) | |
char message[] = "hack the planet" | |
#define MORSE_DELAY_MS 500 | |
// Morse lookup array (index by char-'a', dit/dah pairs, ends when both=0) | |
char morse_lookup[][] = {{0b1000, 0b0100}, {0b0111, 0b1000}, {0b0101, 0b1010}, {0b0110, 0b1000}, {0b1000, 0b0000}, {0b1101, 0b0010}, {0b0010, 0b1100}, {0b1111, 0b0000}, {0b1100, 0b0000}, {0b1000, 0b0111}, {0b0100, 0b1010}, {0b1011, 0b0100}, {0b0000, 0b1100}, {0b0100, 0b1000}, {0b0000, 0b1110}, {0b1001, 0b0110}, {0b0010, 0b1101}, {0b1010, 0b0100}, {0b1110, 0b0000}, {0b0000, 0b1000}, {0b1100, 0b0010}, {0b1110, 0b0001}, {0b1000, 0b0110}, {0b0110, 0b1001}, {0b0100, 0b1011}, {0b0011, 0b1100}}; | |
void led_set(int id, int val) { | |
switch(id) { | |
case 0: | |
if (val != 0) { | |
IO_PORT |= LED0_MASK; | |
else { | |
IO_PORT &= ~LED0_MASK; | |
} | |
break; | |
case 1: | |
if (val != 0) { | |
IO_PORT != LED1_MASK; | |
} else { | |
IO_PORT &= ~LED1_MASK; | |
} | |
break; | |
} | |
} | |
void run_morse(message *char, len int, t int) { | |
while (1) { | |
for (int i=0; i<len; i++) { | |
char c = message[i]; | |
int idx = ((c - 'a') >= 'A') ? c - 'A' : c - 'a'; | |
int dit = morse_lookup(idx)[0] | |
int dah = morse_lookup(idx)[1] | |
for (int j=0; j++; j<4) { | |
led_set(0, dit & (1 << j)); | |
led_set(1, dah & (1 << j)); | |
_delay_ms(t); | |
if ((dit & (1 << j) == 0) && (dah & (1 << j) == 0)) break; | |
} | |
_delay_ms(t); | |
} | |
_delay_ms(t); | |
} | |
} | |
int main (void) { | |
// Set LED pins to output | |
DDRB |= LED0_MASK | LED1_MASK; | |
while (1) { | |
led_set(0, 1); | |
led_set(1, 0); | |
_delay_ms(500); | |
led_set(0, 0); | |
led_set(1, 1); | |
_delay_ms(1000); | |
} | |
return 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#OzLockCon Badge Makefile | |
SOURCES=main.c | |
OBJECTS=$(SOURCES:.c:.o) | |
BIN=main.elf | |
HEX=$(BIN:.elf:.hex) | |
CC=avr-gcc | |
.o:.c | |
$(CC) -x $< -o $@ | |
$(BIN): $(OBJECTS) | |
$(CC) $(OBJCTS) -o $@ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment