From 6481532760a9fe7bbb8df7955d9a3b2f2b36d595 Mon Sep 17 00:00:00 2001 From: MightyPork Date: Fri, 19 Dec 2014 12:33:45 +0100 Subject: [PATCH] x --- keyboard-lamp | 1 - keyboard-lampd/Makefile | 153 ++++++++++++++++++++++++++++++++++++++++ keyboard-lampd/main.c | 116 ++++++++++++++++++++++++++++++ keyboard-lampd/utils.h | 14 ++++ 4 files changed, 283 insertions(+), 1 deletion(-) delete mode 160000 keyboard-lamp create mode 100644 keyboard-lampd/Makefile create mode 100644 keyboard-lampd/main.c create mode 100644 keyboard-lampd/utils.h diff --git a/keyboard-lamp b/keyboard-lamp deleted file mode 160000 index 4eaff23..0000000 --- a/keyboard-lamp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4eaff23a1d463fd916f5a983fd202e0707c12af7 diff --git a/keyboard-lampd/Makefile b/keyboard-lampd/Makefile new file mode 100644 index 0000000..9f4c5f1 --- /dev/null +++ b/keyboard-lampd/Makefile @@ -0,0 +1,153 @@ + +MCU = attiny13 + +F_CPU = 9600000 + +LFUSE = 0x72 +HFUSE = 0xFF + +MAIN = main.c + +## If you've split your program into multiple files, +## include the additional .c source (in same directory) here +## (and include the .h files in your foo.c) +LOCAL_SOURCE = + +## Here you can link to one more directory (and multiple .c files) +# EXTRA_SOURCE_DIR = ../AVR-Programming-Library/ +EXTRA_SOURCE_DIR = +EXTRA_SOURCE_FILES = + + + +##########------------------------------------------------------########## +########## Programmer Defaults ########## +########## Set up once, then forget about it ########## +########## (Can override. See bottom of file.) ########## +##########------------------------------------------------------########## + +PROGRAMMER_TYPE = dragon_isp +PROGRAMMER_ARGS = + + +##########------------------------------------------------------########## +########## Makefile Magic! ########## +########## Summary: ########## +########## We want a .hex file ########## +########## Compile source files into .elf ########## +########## Convert .elf file into .hex ########## +########## You shouldn't need to edit below. ########## +##########------------------------------------------------------########## + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = sudo avrdude + +## Compilation options, type man avr-gcc if you're curious. +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -Os -I. -I$(EXTRA_SOURCE_DIR) +CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +CFLAGS += -Wall -Wno-main +CFLAGS += -g2 -ggdb +CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax +CFLAGS += -std=gnu99 +# CFLAGS += -lm +## CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf +## CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf + +## Lump target and extra source files together +TARGET = $(strip $(basename $(MAIN))) +SRC = $(TARGET).c +EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES)) +SRC += $(EXTRA_SOURCE) +SRC += $(LOCAL_SOURCE) + +## List of all header files +HEADERS = $(SRC:.c=.h) + +## For every .c file, compile an .o object file +OBJ = $(SRC:.c=.o) + +## Generic Makefile targets. (Only .hex file is necessary) +all: $(TARGET).hex size + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) + $(CC) $(CFLAGS) $(SRC) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + +# Optionally create listing file from .elf +# This creates approximate assembly-language equivalent of your code. +# Useful for debugging time-sensitive bits, +# or making sure the compiler does what you want. +disassemble: $(TARGET).lst + +dis: disassemble + +eeprom: $(TARGET).eeprom + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Optionally show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +squeaky_clean: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +##########------------------------------------------------------########## +########## Programmer-specific details ########## +########## Flashing code to AVR using avrdude ########## +##########------------------------------------------------------########## + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flash_eeprom: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +terminal: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + + +flash_dragon_isp: PROGRAMMER_TYPE = dragon_isp +flash_dragon_isp: PROGRAMMER_ARGS = +flash_dragon_isp: flash + + +##########------------------------------------------------------########## +########## Fuse settings and suitable defaults ########## +##########------------------------------------------------------########## + +## Generic +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +## Called with no extra definitions, sets to defaults +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m +set_default_fuses: fuses diff --git a/keyboard-lampd/main.c b/keyboard-lampd/main.c new file mode 100644 index 0000000..d0860ca --- /dev/null +++ b/keyboard-lampd/main.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include + +#include "utils.h" + +/** + * +--u--+ + * RST --| |-- Vcc + * SAVE LED : PB3 --| t13 |-- PB2 : BTN: HIGH + * N.C. : PB4 --| |-- PB1 : BTN: LOW + * GND --| |-- PB0 : LED + * +-----+ + */ + + +// pins config +#define PIN_LED PB0 +#define PIN_LOW PB1 +#define PIN_HIGH PB2 +#define PIN_INDICATOR PB3 + +#define btn_low() (0 == get_bit(PINB, PIN_LOW)) +#define btn_high() (0 == get_bit(PINB, PIN_HIGH)) + + +/** Initialize IO ports. */ +void SECTION(".init8") init_io(void) +{ + // set pin directions + DDRB = _BV(PIN_LED) | _BV(PIN_INDICATOR); + + // pullups + PORTB = _BV(PIN_LOW) | _BV(PIN_HIGH); + + // initialize the timer + + // Fast PWM mode, Output to OC0A + OCR0A = 32; + + TCCR0A = _BV(WGM00) | _BV(WGM01) | _BV(COM0A1); + TCCR0B = _BV(CS00); +} + + +#define BRIGHTNESS_LEN 121 + +const uint8_t BRIGHTNESS[] PROGMEM = { + 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, + 6, 6, 6, 7, 7, 8, 8, 8, 9, 10, 10, 10, 11, 12, 13, 14, 14, + 15, 16, 17, 18, 20, 21, 22, 24, 26, 27, 28, 30, 31, 32, 34, + 35, 36, 38, 39, 40, 41, 42, 44, 45, 46, 48, 49, 50, 52, 54, + 56, 58, 59, 61, 63, 65, 67, 69, 71, 72, 74, 76, 78, 80, 82, + 85, 88, 90, 92, 95, 98, 100, 103, 106, 109, 112, 116, 119, + 122, 125, 129, 134, 138, 142, 147, 151, 156, 160, 165, 170, + 175, 180, 185, 190, 195, 200, 207, 214, 221, 228, 234, 241, + 248, 255 +}; + +#define apply_brightness(level) OCR0A = pgm_read_byte( & BRIGHTNESS[level] ) + + +/** + * Main function + */ +void main() +{ + bool changed = 0; + bool changed_since_last_save = 0; + + uint8_t savetimer = 0; + + uint8_t level = eeprom_read_byte((uint8_t *) 0); + + if (level >= BRIGHTNESS_LEN) + level = BRIGHTNESS_LEN - 1; + else if (level == 0) + level = BRIGHTNESS_LEN / 2; + + apply_brightness(level); + + while (1) { + if (btn_high() && level < BRIGHTNESS_LEN - 1) { + level++; + changed = 1; + } + + if (btn_low() && level > 1) { + level--; + changed = 1; + } + + if (changed) { + changed = 0; + savetimer = 0; + changed_since_last_save = 1; + apply_brightness(level); + } + + _delay_ms(20); + savetimer++; + + if (savetimer >= 50 && changed_since_last_save) { + changed_since_last_save = 0; + savetimer = 0; + + sbi(PORTB, PIN_INDICATOR); + eeprom_update_byte((uint8_t*) 0, level); + _delay_ms(10); + cbi(PORTB, PIN_INDICATOR); + } + } +} diff --git a/keyboard-lampd/utils.h b/keyboard-lampd/utils.h new file mode 100644 index 0000000..4ab82a1 --- /dev/null +++ b/keyboard-lampd/utils.h @@ -0,0 +1,14 @@ +#pragma once + +// general macros +#define SECTION(pos) __attribute__((naked, used, section(pos))) + + +// pin manipulation +#define sbi(port, bit) (port) |= _BV(bit) +#define cbi(port, bit) (port) &= ~ _BV(bit) + +#define set_bit(port, bit, value) (port) = (((port) & ~_BV(bit)) | ((value) << (bit))) +#define get_bit(port, bit) (((port) >> (bit)) & 1) + +#define bus_pulse(port, bit) { sbi(port, bit); cbi(port, bit); }