From 2b4d907200c9155c196556830f6ce7503aa5ec99 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 18 Oct 2021 13:30:23 +0200 Subject: [PATCH] initial commit --- .gitignore | 4 ++ Makefile | 20 ++++++++++ main.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..468917c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +* +!.gitignore +!main.c +!Makefile \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..403e4ec --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +CXX=avr-gcc +CXXFLAGS=-std=gnu99 -Os -mmcu=atmega644 -DF_CPU=10000000ul +LDFLAGS= +LDLIBS= +SOURCE= main.c +COM=/dev/ttyUSB0 #mit "lsusb" und "sudo dmesg | grep tty" herausfinden +PLATFORM=atmega644 +BOARD=stk500 + +all: clean build push +clean: + rm -f *.elf + rm -f *.hex + +build: + $(CXX) $(CXXFLAGS) main.c -o main.elf + avr-objcopy -O ihex -j .text -j .data main.elf main.hex + +push: + sudo avrdude -p $(PLATFORM) -c $(BOARD) -P $(COM) -U flash:w:main.hex diff --git a/main.c b/main.c new file mode 100644 index 0000000..0287aa2 --- /dev/null +++ b/main.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include + +#define PORT_LED PORTB +#define PIN_SWITCH PIND + +bool do_round(uint8_t *seq, int len); +void display_seq(uint8_t *seq, int len); +bool read_input(uint8_t *seq, int len); + +int main(){ + // Port B LED + DDRB = 0xFF; + + // Port A Switch + DDRD = 0x00; + + // Values from 0-7 representing the LED position + uint8_t seq[] = {4,2,4,5,1,0,3}; + + if (do_round(seq,7)) { + while (true) + { + PORT_LED = 0x55; + _delay_ms(250); + PORT_LED = 0xAA; + _delay_ms(250); + } + }else{ + while (true) + { + PORT_LED = 0xF0; + _delay_ms(250); + PORT_LED = 0x0F; + _delay_ms(250); + } + } + +} + +bool do_round(uint8_t *seq, int len){ + + for (int i = 2; i < len; i++) + { + display_seq(seq,i); + if (!read_input(seq,i)){ + // Failed + return false; + } + } + return true; +} + +void display_seq(uint8_t *seq, int len){ + + // Turn off all LEDs + PORT_LED = 0xFF; + + _delay_ms(1000); + + for (int i = 0; i < len; i++) + { + PORT_LED = (0xFF ^ (1 << seq[i] )); + + _delay_ms(1000); + + // Turn off LEDs + PORT_LED = 0xFF; + _delay_ms(250); + } + + + // Turn off all LEDs + PORT_LED = 0xFF; +} + +bool read_input(uint8_t *seq, int len){ + for (int i = 0; i < len; i++) + { + uint8_t expected = 0xFF ^ (1 << seq[i]); + + bool run = true; + while (run) + { + uint8_t input = PIN_SWITCH; + if (input < 0xFF){ + // A button is pressed + if (input == expected){ + run = false; + + // Wait for button to release + while (PIN_SWITCH != 0xFF) + { + + } + + // "debounce" + _delay_ms(50); + + }else{ + return false; + } + } + } + } + return true; +}