parent
2b38adb74d
commit
244f54c36c
@ -0,0 +1,167 @@ |
||||
|
||||
MCU = atmega328p
|
||||
|
||||
F_CPU = 16000000
|
||||
|
||||
LFUSE = 0xFF
|
||||
HFUSE = 0xDE
|
||||
EFUSE = 0x05
|
||||
|
||||
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 = lib/
|
||||
EXTRA_SOURCE_FILES = debounce.c lcd.c adc.c
|
||||
|
||||
EXTRA_CFLAGS =
|
||||
|
||||
##########------------------------------------------------------##########
|
||||
########## Programmer Defaults ##########
|
||||
########## Set up once, then forget about it ##########
|
||||
########## (Can override. See bottom of file.) ##########
|
||||
##########------------------------------------------------------##########
|
||||
#19200
|
||||
PROGRAMMER_TYPE = arduino
|
||||
PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0
|
||||
|
||||
|
||||
##########------------------------------------------------------##########
|
||||
########## 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 = avrdude
|
||||
|
||||
## Compilation options, type man avr-gcc if you're curious.
|
||||
CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(EXTRA_SOURCE_DIR)
|
||||
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
|
||||
CFLAGS += -Wall -Wno-main -Wno-strict-prototypes -Wno-comment
|
||||
CFLAGS += -g2 -Wextra -Wfatal-errors
|
||||
CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax
|
||||
CFLAGS += $(EXTRA_CFLAGS)
|
||||
|
||||
CFLAGS_BUILD = $(CFLAGS) -Os
|
||||
|
||||
# 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)))
|
||||
SRC1 = $(TARGET).c
|
||||
SRC = $(SRC1)
|
||||
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 |
||||
pre: $(TARGET).pre |
||||
|
||||
%.hex: %.elf |
||||
$(OBJCOPY) -R .eeprom -O ihex $< $@
|
||||
|
||||
%.elf: $(SRC) |
||||
$(CC) $(CFLAGS_BUILD) $(SRC) --output $@
|
||||
|
||||
%.pre: $(SRC1) |
||||
$(CC) $(CFLAGS) -E $(SRC1) --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 |
||||
lst: 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_arduino: PROGRAMMER_TYPE = arduino |
||||
flash_arduino: PROGRAMMER_ARGS = |
||||
flash_arduino: flash |
||||
|
||||
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 -U efuse:w:$(EFUSE):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 -U efuse:w:$(EFUSE):m |
||||
set_default_fuses: fuses |
@ -0,0 +1,10 @@ |
||||
Snake for HD44780 |
||||
================= |
||||
|
||||
This is a Snake game (known from old Nokia phones) played on a character display with HD44780. |
||||
|
||||
Program tested on Arduino Pro Mini (flashed with avrdude using the Makefile). |
||||
|
||||
**Connections** are `#define`d in `main.c`, board size and snake speed can also be adjusted. No external parts except the display and buttons required. |
||||
|
||||
Best works with a gamepad, but any buttons will work. |
@ -0,0 +1,5 @@ |
||||
#pragma once |
||||
|
||||
// Config file for debouncer
|
||||
#define DEBO_CHANNELS 6 |
||||
#define DEBO_TICKS 1 |
@ -0,0 +1,13 @@ |
||||
#pragma once |
||||
|
||||
// Config file for LCD.
|
||||
|
||||
#include "lib/arduino_pins.h" |
||||
|
||||
#define LCD_RS D2 |
||||
#define LCD_RW D3 |
||||
#define LCD_E D4 |
||||
#define LCD_D4 D5 |
||||
#define LCD_D5 D6 |
||||
#define LCD_D6 D7 |
||||
#define LCD_D7 D8 |
@ -0,0 +1,46 @@ |
||||
#include <avr/io.h> |
||||
#include <stdbool.h> |
||||
|
||||
#include "calc.h" |
||||
#include "adc.h" |
||||
|
||||
/** Initialize the ADC */ |
||||
void adc_init() |
||||
{ |
||||
ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); // 128 prescaler -> 125 kHz
|
||||
ADMUX |= _BV(REFS0); // Voltage reference
|
||||
sbi(ADCSRA, ADEN); // Enable ADC
|
||||
} |
||||
|
||||
|
||||
/** Disable AD */ |
||||
void adc_disable() |
||||
{ |
||||
cbi(ADCSRA, ADEN); |
||||
} |
||||
|
||||
|
||||
/** Sample analog pin with 8-bit precision */ |
||||
uint8_t adc_read_byte(uint8_t channel) |
||||
{ |
||||
write_low_nibble(ADMUX, channel); // Select channel to sample
|
||||
sbi(ADMUX, ADLAR); // Align result to left
|
||||
sbi(ADCSRA, ADSC); // Start conversion
|
||||
|
||||
while(bit_is_high(ADCSRA, ADSC)); // Wait for it...
|
||||
|
||||
return ADCH; // The upper 8 bits of ADC result
|
||||
} |
||||
|
||||
|
||||
/** Sample analog pin with 10-bit precision */ |
||||
uint16_t adc_read_word(uint8_t channel) |
||||
{ |
||||
write_low_nibble(ADMUX, channel); // Select channel to sample
|
||||
cbi(ADMUX, ADLAR); // Align result to right
|
||||
sbi(ADCSRA, ADSC); // Start conversion
|
||||
|
||||
while(get_bit(ADCSRA, ADSC)); // Wait for it...
|
||||
|
||||
return ADCW; // The whole ADC word (10 bits)
|
||||
} |
@ -0,0 +1,19 @@ |
||||
#pragma once |
||||
|
||||
//
|
||||
// Utilities for build-in A/D converter
|
||||
//
|
||||
|
||||
#include <avr/io.h> |
||||
|
||||
/** Initialize the ADC */ |
||||
void adc_init(); |
||||
|
||||
/** Disable AD (for power saving?) */ |
||||
void adc_disable(); |
||||
|
||||
/** Sample analog pin with 8-bit precision */ |
||||
uint8_t adc_read_byte(uint8_t channel); |
||||
|
||||
/** Sample analog pin with 10-bit precision */ |
||||
uint16_t adc_read_word(uint8_t channel); |
@ -0,0 +1,42 @@ |
||||
#pragma once |
||||
|
||||
//
|
||||
// Pin definitions for Arduino (Pro Mini with ATmega328P)
|
||||
//
|
||||
|
||||
#include "pins.h" |
||||
|
||||
#define D0 D,0 |
||||
#define D1 D,1 |
||||
#define D2 D,2 |
||||
#define D3 D,3 |
||||
#define D4 D,4 |
||||
#define D5 D,5 |
||||
#define D6 D,6 |
||||
#define D7 D,7 |
||||
#define D8 B,0 |
||||
#define D9 B,1 |
||||
#define D10 B,2 |
||||
|
||||
// MOSI MISO SCK - not good for input
|
||||
#define D11 B,3 |
||||
#define D12 B,4 |
||||
#define D13 B,5 |
||||
|
||||
#define D14 C,0 |
||||
#define D15 C,1 |
||||
#define D16 C,2 |
||||
#define D17 C,3 |
||||
#define D18 C,4 |
||||
#define D19 C,5 |
||||
#define D20 C,6 |
||||
#define D21 C,7 |
||||
|
||||
#define A0 C,0 |
||||
#define A1 C,1 |
||||
#define A2 C,2 |
||||
#define A3 C,3 |
||||
#define A4 C,4 |
||||
#define A5 C,5 |
||||
#define A6 C,6 |
||||
#define A7 C,7 |
@ -0,0 +1,91 @@ |
||||
#pragma once |
||||
|
||||
//
|
||||
// Bit and byte manipulation utilities
|
||||
//
|
||||
|
||||
|
||||
// --- Increment in range ---
|
||||
// when overflown, wraps within range. Lower bound < upper bound.
|
||||
// ..., upper bound excluded
|
||||
#define inc_wrap(var, min, max) do { if ((var) >= (max - 1)) { (var) = (min); } else { (var)++; } } while(0) |
||||
// ..., upper bound included
|
||||
#define inc_wrapi(var, min, max) inc_wrap((var), (min), (max) + 1) |
||||
|
||||
|
||||
// --- Decrement in range ---
|
||||
// when underflown, wraps within range. Lower bound < upper bound.
|
||||
// ..., upper bound excluded
|
||||
#define dec_wrap(var, min, max) do { if ((var) <= (min)) { (var) = (max) - 1; } else { (var)--; } } while(0) |
||||
// ..., upper bound included
|
||||
#define dec_wrapi(var, min, max) dec_wrap((var), (min), (max) + 1) |
||||
|
||||
|
||||
// --- Bit manipulation --
|
||||
|
||||
// Set bit
|
||||
#define sbi(reg, bit) do { (reg) |= (1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
// Clear bit
|
||||
#define cbi(reg, bit) do { (reg) &= ~(1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
// Get n-th bit
|
||||
#define read_bit(reg, bit) (((reg) >> (uint8_t)(bit)) & 0x1) |
||||
#define get_bit(reg, bit) read_bit(reg, bit) |
||||
|
||||
// Test n-th bit (Can't use bit_is_set, as it's redefined in sfr_def.h)
|
||||
#define bit_is_high(reg, bit) read_bit(reg, bit) |
||||
#define bit_is_low(reg, bit) (!read_bit(reg, bit)) |
||||
|
||||
// Write value to n-th bit
|
||||
#define write_bit(reg, bit, value) do { (reg) = ((reg) & ~(1 << (uint8_t)(bit))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0) |
||||
#define set_bit(reg, bit, value) write_bit(reg, bit, value) |
||||
|
||||
// Invert n-th bit
|
||||
#define toggle_bit(reg, bit) do { (reg) ^= (1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
|
||||
// --- Bit manipulation with pointer to variable ---
|
||||
|
||||
// Set n-th bit in pointee
|
||||
#define sbi_p(reg_p, bit) do { (*(reg_p)) |= (1 << (uint8_t)(bit)); } while(0) |
||||
// Clear n-th bit in pointee
|
||||
#define cbi_p(reg_p, bit) do { (*(reg_p)) &= ~(1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
// Get n-th bit in pointee
|
||||
#define read_bit_p(reg_p, bit) ((*(reg_p) >> (uint8_t)(bit)) & 0x1) |
||||
#define get_bit_p(reg_p, bit) read_bit_p(reg_p, bit) |
||||
|
||||
// Test n-th bit in pointee (Can't use bit_is_set, as it's redefined in sfr_def.h)
|
||||
#define bit_is_high_p(reg_p, bit) read_bit_p(reg_p, bit) |
||||
#define bit_is_low_p(reg_p, bit) (!read_bit_p(reg_p, bit)) |
||||
|
||||
// Write value to a bit in pointee
|
||||
#define write_bit_p(reg_p, bit, value) do { *(reg_p) = (*(reg_p) & ~(1 << ((uint8_t)(bit) & 0x1))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0) |
||||
#define set_bit_p(reg_p, bit, value) write_bit_p(reg_p, bit, value) |
||||
#define toggle_bit_p(reg_p, bit) do { *(reg_p) ^= (1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
|
||||
// --- Nibble manipulation ---
|
||||
|
||||
// Replace nibble in a byte
|
||||
#define write_low_nibble(reg, value) do { (reg) = ((reg) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0) |
||||
#define write_high_nibble(reg, value) do { (reg) = ((reg) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0) |
||||
|
||||
#define write_low_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0) |
||||
#define write_high_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0) |
||||
|
||||
#define low_nibble(x) ((uint8_t)(x) & 0xF) |
||||
#define high_nibble(x) (((uint8_t)(x) & 0xF0) >> 4) |
||||
|
||||
// --- Range tests ---
|
||||
|
||||
// Test if X is within low..high, regardless of bounds order
|
||||
#define in_range(x, low, high) ((((low) < (high)) && ((x) >= (low) && (x) < (high))) || (((low) > (high)) && ((x) >= (high) && (x) < (low)))) |
||||
// ..., include greater bound
|
||||
#define in_rangei(x, low, high) ((((low) <= (high)) && ((x) >= (low) && (x) <= (high))) || (((low) > (high)) && ((x) >= (high) && (x) <= (low)))) |
||||
|
||||
// Test if X in low..high, wrap around ends if needed.
|
||||
#define in_range_wrap(x, low, high) ((((low) < (high)) && ((x) >= (low) && (x) < (high))) || (((low) > (high)) && ((x) >= (low) || (x) < (high)))) |
||||
// ..., include upper bound
|
||||
#define in_range_wrapi(x, low, high) ((((low) <= (high)) && ((x) >= (low) && (x) <= (high))) || (((low) > (high)) && ((x) >= (low) || (x) <= (high)))) |
@ -0,0 +1,45 @@ |
||||
#include <avr/io.h> |
||||
#include <stdbool.h> |
||||
|
||||
#include "debounce.h" |
||||
#include "calc.h" |
||||
#include "pins.h" |
||||
#include "debo_config.h" |
||||
|
||||
/** Debounce data array */ |
||||
uint8_t debo_next_slot = 0; |
||||
|
||||
uint8_t debo_register(PORT_P reg, uint8_t bit, bool invert) |
||||
{ |
||||
debo_slots[debo_next_slot] = (debo_slot_t){ |
||||
.reg = reg, |
||||
.bit = bit | ((invert & 1) << 7) | (get_bit_p(reg, bit) << 6), // bit 7 = invert, bit 6 = state
|
||||
.count = 0, |
||||
}; |
||||
|
||||
return debo_next_slot++; |
||||
} |
||||
|
||||
|
||||
/** Check debounced pins, should be called periodically. */ |
||||
void debo_tick() |
||||
{ |
||||
for (uint8_t i = 0; i < debo_next_slot; i++) { |
||||
// current pin value (right 3 bits, xored with inverse bit)
|
||||
bool value = get_bit_p(debo_slots[i].reg, debo_slots[i].bit & 0x7); |
||||
|
||||
if (value != get_bit(debo_slots[i].bit, 6)) { |
||||
|
||||
// different pin state than last recorded state
|
||||
if (debo_slots[i].count < DEBO_TICKS) { |
||||
debo_slots[i].count++; |
||||
} else { |
||||
// overflown -> latch value
|
||||
set_bit(debo_slots[i].bit, 6, value); // set state bit
|
||||
debo_slots[i].count = 0; |
||||
} |
||||
} else { |
||||
debo_slots[i].count = 0; // reset the counter
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,64 @@ |
||||
#pragma once |
||||
|
||||
//
|
||||
// An implementation of button debouncer.
|
||||
//
|
||||
// ----
|
||||
//
|
||||
// You must provide a config file debo_config.h (next to your main.c)
|
||||
//
|
||||
// Example:
|
||||
// #pragma once
|
||||
// #define DEBO_CHANNELS 2
|
||||
// #define DDEBO_TICKS 5
|
||||
//
|
||||
// ----
|
||||
//
|
||||
// A pin is registered like this:
|
||||
//
|
||||
// #define BTN1 B,0
|
||||
// #define BTN2 B,1
|
||||
//
|
||||
// debo_add(BTN0); // The function returns number assigned to the pin (0, 1, ...)
|
||||
// debo_add_rev(BTN1); // active low
|
||||
// debo_register(&PINB, PB2, 0); // direct access - register, pin & invert
|
||||
//
|
||||
// Then periodically call the tick function (perhaps in a timer interrupt):
|
||||
//
|
||||
// debo_tick();
|
||||
//
|
||||
// To check if input is active, use
|
||||
//
|
||||
// debo_get_pin(0); // state of input #0 (registered first)
|
||||
// debo_get_pin(1); // state of input #1 (registered second)
|
||||
//
|
||||
|
||||
|
||||
#include <avr/io.h> |
||||
#include <stdbool.h> |
||||
|
||||
#include "calc.h" |
||||
#include "pins.h" |
||||
#include "debo_config.h" |
||||
|
||||
/* Internal deboucer entry */ |
||||
typedef struct { |
||||
PORT_P reg; // pointer to IO register
|
||||
uint8_t bit; // bits 6 and 7 of this hold "state" & "invert" flag
|
||||
uint8_t count; // number of ticks this was in the new state
|
||||
} debo_slot_t; |
||||
|
||||
debo_slot_t debo_slots[DEBO_CHANNELS]; |
||||
|
||||
/** Add a pin for debouncing */ |
||||
#define debo_add_rev(io) debo_register(&io2pin(io_pack(io)), io2n(io_pack(io)), 1) |
||||
#define debo_add(io) debo_register(&io2pin(io_pack(io)), io2n(io_pack(io)), 0) |
||||
|
||||
/** Add a pin for debouncing (low level function) */ |
||||
uint8_t debo_register(PORT_P pin_reg_pointer, uint8_t bit, bool invert); |
||||
|
||||
/** Check debounced pins, should be called periodically. */ |
||||
void debo_tick(); |
||||
|
||||
/** Get a value of debounced pin */ |
||||
#define debo_get_pin(i) (get_bit(debo_slots[i].bit, 6) ^ get_bit(debo_slots[i].bit, 7)) |
@ -0,0 +1,358 @@ |
||||
#include <stdbool.h> |
||||
#include <stdint.h> |
||||
#include <avr/io.h> |
||||
#include <avr/pgmspace.h> |
||||
#include <util/delay.h> |
||||
|
||||
#include "calc.h" |
||||
#include "pins.h" |
||||
#include "nsdelay.h" |
||||
#include "lcd.h" |
||||
#include "lcd_config.h" |
||||
|
||||
// Start address of rows
|
||||
const uint8_t LCD_ROW_ADDR[] = {0x00, 0x40, 0x14, 0x54}; |
||||
|
||||
|
||||
// Shared stream instance
|
||||
static STREAM _lcd_singleton; |
||||
STREAM* lcd; |
||||
|
||||
|
||||
// Internal prototypes
|
||||
void _lcd_mode_r(); |
||||
void _lcd_mode_w(); |
||||
void _lcd_clk(); |
||||
void _lcd_wait_bf(); |
||||
void _lcd_write_byte(uint8_t bb); |
||||
uint8_t _lcd_read_byte(); |
||||
|
||||
|
||||
// Write utilities
|
||||
#define _lcd_write_low(bb) _lcd_write_nibble((bb) & 0x0F) |
||||
#define _lcd_write_high(bb) _lcd_write_nibble(((bb) & 0xF0) >> 4) |
||||
#define _lcd_write_nibble(nib) do { \ |
||||
write_pin(LCD_D7, get_bit((nib), 3)); \
|
||||
write_pin(LCD_D6, get_bit((nib), 2)); \
|
||||
write_pin(LCD_D5, get_bit((nib), 1)); \
|
||||
write_pin(LCD_D4, get_bit((nib), 0)); \
|
||||
} while(0) |
||||
|
||||
|
||||
// 0 W, 1 R
|
||||
bool _lcd_mode; |
||||
|
||||
struct { |
||||
uint8_t x; |
||||
uint8_t y; |
||||
} _pos; |
||||
|
||||
enum { |
||||
TEXT = 0, |
||||
CG = 1 |
||||
} _addrtype; |
||||
|
||||
|
||||
/** Initialize the display */ |
||||
void lcd_init() |
||||
{ |
||||
// configure pins as output
|
||||
as_output(LCD_E); |
||||
as_output(LCD_RW); |
||||
as_output(LCD_RS); |
||||
_lcd_mode = 1; // force data pins to output
|
||||
_lcd_mode_w(); |
||||
|
||||
// Magic sequence to invoke Cthulhu (or enter 4-bit mode)
|
||||
_delay_ms(16); |
||||
_lcd_write_nibble(0b0011); |
||||
_lcd_clk(); |
||||
_delay_ms(5); |
||||
_lcd_clk(); |
||||
_delay_ms(5); |
||||
_lcd_clk(); |
||||
_delay_ms(5); |
||||
_lcd_write_nibble(0b0010); |
||||
_lcd_clk(); |
||||
_delay_us(100); |
||||
|
||||
// Configure the display
|
||||
lcd_command(LCD_IFACE_4BIT_2LINE); |
||||
lcd_command(LCD_DISABLE); |
||||
lcd_command(LCD_CLEAR); |
||||
lcd_command(LCD_MODE_INC); |
||||
|
||||
// mark as enabled
|
||||
lcd_enable(); |
||||
|
||||
_lcd_singleton.tx = &lcd_write; |
||||
_lcd_singleton.rx = &lcd_read; |
||||
|
||||
// Stream
|
||||
lcd = &_lcd_singleton; |
||||
|
||||
_pos.x = 0; |
||||
_pos.y = 0; |
||||
_addrtype = TEXT; |
||||
} |
||||
|
||||
|
||||
/** Send a pulse on the ENABLE line */ |
||||
void _lcd_clk() |
||||
{ |
||||
pin_up(LCD_E); |
||||
delay_ns(450); |
||||
pin_down(LCD_E); |
||||
} |
||||
|
||||
|
||||
/** Enter READ mode */ |
||||
void _lcd_mode_r() |
||||
{ |
||||
if (_lcd_mode == 1) return; // already in R mode
|
||||
|
||||
pin_up(LCD_RW); |
||||
|
||||
as_input_pu(LCD_D7); |
||||
as_input_pu(LCD_D6); |
||||
as_input_pu(LCD_D5); |
||||
as_input_pu(LCD_D4); |
||||
|
||||
_lcd_mode = 1; |
||||
} |
||||
|
||||
|
||||
/** Enter WRITE mode */ |
||||
void _lcd_mode_w() |
||||
{ |
||||
if (_lcd_mode == 0) return; // already in W mode
|
||||
|
||||
pin_down(LCD_RW); |
||||
|
||||
as_output(LCD_D7); |
||||
as_output(LCD_D6); |
||||
as_output(LCD_D5); |
||||
as_output(LCD_D4); |
||||
|
||||
_lcd_mode = 0; |
||||
} |
||||
|
||||
|
||||
/** Read a byte */ |
||||
uint8_t _lcd_read_byte() |
||||
{ |
||||
_lcd_mode_r(); |
||||
|
||||
uint8_t res = 0; |
||||
|
||||
_lcd_clk(); |
||||
res = (read_pin(LCD_D7) << 7) | (read_pin(LCD_D6) << 6) | (read_pin(LCD_D5) << 5) | (read_pin(LCD_D4) << 4); |
||||
|
||||
_lcd_clk(); |
||||
res |= (read_pin(LCD_D7) << 3) | (read_pin(LCD_D6) << 2) | (read_pin(LCD_D5) << 1) | (read_pin(LCD_D4) << 0); |
||||
|
||||
return res; |
||||
} |
||||
|
||||
|
||||
/** Write an instruction byte */ |
||||
void lcd_command(uint8_t bb) |
||||
{ |
||||
_lcd_wait_bf(); |
||||
pin_down(LCD_RS); // select instruction register
|
||||
_lcd_write_byte(bb); // send instruction byte
|
||||
} |
||||
|
||||
|
||||
/** Write a data byte */ |
||||
void lcd_write(uint8_t bb) |
||||
{ |
||||
if (_addrtype == TEXT) { |
||||
if (bb == '\r') { |
||||
// CR
|
||||
_pos.x = 0; |
||||
lcd_xy(_pos.x, _pos.y); |
||||
return; |
||||
} |
||||
|
||||
if (bb == '\n') { |
||||
// LF
|
||||
_pos.y++; |
||||
lcd_xy(_pos.x, _pos.y); |
||||
return; |
||||
} |
||||
|
||||
_pos.x++; |
||||
} |
||||
|
||||
_lcd_wait_bf(); |
||||
pin_up(LCD_RS); // select data register
|
||||
_lcd_write_byte(bb); // send data byte
|
||||
} |
||||
|
||||
|
||||
/** Read BF & Address */ |
||||
uint8_t lcd_read_bf_addr() |
||||
{ |
||||
pin_down(LCD_RS); |
||||
return _lcd_read_byte(); |
||||
} |
||||
|
||||
|
||||
/** Read CGRAM or DDRAM */ |
||||
uint8_t lcd_read() |
||||
{ |
||||
if (_addrtype == TEXT) _pos.x++; |
||||
|
||||
pin_up(LCD_RS); |
||||
return _lcd_read_byte(); |
||||
} |
||||
|
||||
|
||||
/** Write a byte using the 4-bit interface */ |
||||
void _lcd_write_byte(uint8_t bb) |
||||
{ |
||||
_lcd_mode_w(); // enter W mode
|
||||
|
||||
_lcd_write_high(bb); |
||||
_lcd_clk(); |
||||
|
||||
_lcd_write_low(bb); |
||||
_lcd_clk(); |
||||
} |
||||
|
||||
|
||||
|
||||
/** Wait until the device is ready */ |
||||
void _lcd_wait_bf() |
||||
{ |
||||
uint8_t d = 0; |
||||
while(d++ < 120 && lcd_read_bf_addr() & _BV(7)) |
||||
_delay_us(1); |
||||
} |
||||
|
||||
|
||||
/** Send a string to LCD */ |
||||
void lcd_puts(char* str_p) |
||||
{ |
||||
char c; |
||||
while ((c = *str_p++)) |
||||
lcd_putc(c); |
||||
} |
||||
|
||||
|
||||
/** Print from progmem */ |
||||
void lcd_puts_P(const char* str_p) |
||||
{ |
||||
char c; |
||||
while ((c = pgm_read_byte(str_p++))) |
||||
lcd_putc(c); |
||||
} |
||||
|
||||
|
||||
/** Sedn a char to LCD */ |
||||
void lcd_putc(const char c) |
||||
{ |
||||
lcd_write(c); |
||||
} |
||||
|
||||
|
||||
/** Set cursor position */ |
||||
void lcd_xy(const uint8_t x, const uint8_t y) |
||||
{ |
||||
_pos.x = x; |
||||
_pos.y = y; |
||||
lcd_addr(LCD_ROW_ADDR[y] + (x)); |
||||
} |
||||
|
||||
|
||||
uint8_t _lcd_old_cursor = CURSOR_NONE; |
||||
bool _lcd_enabled = false; |
||||
|
||||
/** Set LCD cursor. If not enabled, only remember it. */ |
||||
void lcd_cursor(uint8_t type) |
||||
{ |
||||
_lcd_old_cursor = (type & CURSOR_BOTH); |
||||
|
||||
if (_lcd_enabled) lcd_command(LCD_CURSOR_NONE | _lcd_old_cursor); |
||||
} |
||||
|
||||
|
||||
/** Display display (preserving cursor) */ |
||||
void lcd_disable() |
||||
{ |
||||
lcd_command(LCD_DISABLE); |
||||
_lcd_enabled = false; |
||||
} |
||||
|
||||
|
||||
/** Enable display (restoring cursor) */ |
||||
void lcd_enable() |
||||
{ |
||||
_lcd_enabled = true; |
||||
lcd_cursor(_lcd_old_cursor); |
||||
} |
||||
|
||||
|
||||
/** Go home */ |
||||
void lcd_home() |
||||
{ |
||||
lcd_command(LCD_HOME); |
||||
_pos.x = 0; |
||||
_pos.y = 0; |
||||
_addrtype = TEXT; |
||||
} |
||||
|
||||
|
||||
/** Clear the screen */ |
||||
void lcd_clear() |
||||
{ |
||||
lcd_command(LCD_CLEAR); |
||||
_pos.x = 0; |
||||
_pos.y = 0; |
||||
_addrtype = TEXT; |
||||
} |
||||
|
||||
|
||||
/** Define a glyph */ |
||||
void lcd_glyph(const uint8_t index, const uint8_t* array) |
||||
{ |
||||
lcd_addr_cg(index * 8); |
||||
for (uint8_t i = 0; i < 8; ++i) { |
||||
lcd_write(array[i]); |
||||
} |
||||
|
||||
// restore previous position
|
||||
lcd_xy(_pos.x, _pos.y); |
||||
_addrtype = TEXT; |
||||
} |
||||
|
||||
|
||||
/** Define a glyph */ |
||||
void lcd_glyph_P(const uint8_t index, const uint8_t* array) |
||||
{ |
||||
lcd_addr_cg(index * 8); |
||||
for (uint8_t i = 0; i < 8; ++i) { |
||||
lcd_write(pgm_read_byte(&array[i])); |
||||
} |
||||
|
||||
// restore previous position
|
||||
lcd_xy(_pos.x, _pos.y); |
||||
_addrtype = TEXT; |
||||
} |
||||
|
||||
|
||||
/** Set address in CGRAM */ |
||||
void lcd_addr_cg(const uint8_t acg) |
||||
{ |
||||
_addrtype = CG; |
||||
lcd_command(0b01000000 | ((acg) & 0b00111111)); |
||||
} |
||||
|
||||
|
||||
/** Set address in DDRAM */ |
||||
void lcd_addr(const uint8_t add) |
||||
{ |
||||
_addrtype = TEXT; |
||||
lcd_command(0b10000000 | ((add) & 0b01111111)); |
||||
} |
@ -0,0 +1,146 @@ |
||||
#pragma once |
||||
|
||||
// HD44780 LCD display driver - 4-bit mode
|
||||
//
|
||||
// LCD pins are configured using a file lcd_config.h, which you
|
||||
// have to add next to your main.c file.
|
||||
//
|
||||
// Content can be something like this:
|
||||
//
|
||||
// #pragma once
|
||||
// #include "lib/arduino_pins.h"
|
||||
// #define LCD_RS D10
|
||||
// #define LCD_RW D11
|
||||
// #define LCD_E D12
|
||||
// #define LCD_D4 D13
|
||||
// #define LCD_D5 D14
|
||||
// #define LCD_D6 D15
|
||||
// #define LCD_D7 D16
|
||||
//
|
||||
|
||||
#include <stdint.h> |
||||
#include <stdbool.h> |
||||
|
||||
#include "stream.h" |
||||
|
||||
// File with configs
|
||||
#include "lcd_config.h" |
||||
|
||||
|
||||
|
||||
// Shared LCD stream object
|
||||
// Can be used with functions from stream.h once LCD is initialized
|
||||
extern STREAM* lcd; |
||||
|
||||
|
||||
// --- Commands ---
|
||||
|
||||
// Clear screen (reset)
|
||||
#define LCD_CLEAR 0b00000001 |
||||
// Move cursor to (0,0), unshift...
|
||||
#define LCD_HOME 0b00000010 |
||||
|
||||
// Set mode: Increment + NoShift
|
||||
#define LCD_MODE_INC 0b00000110 |
||||
// Set mode: Increment + Shift
|
||||
#define LCD_MODE_INC_SHIFT 0b00000111 |
||||
|
||||
// Set mode: Decrement + NoShift
|
||||
#define LCD_MODE_DEC 0b00000100 |
||||
// Set mode: Decrement + Shift
|
||||
#define LCD_MODE_DEC_SHIFT 0b00000101 |
||||
|
||||
// Disable display (data remains untouched)
|
||||
#define LCD_DISABLE 0b00001000 |
||||
|
||||
// Disable cursor
|
||||
#define LCD_CURSOR_NONE 0b00001100 |
||||
// Set cursor to still underscore
|
||||
#define LCD_CURSOR_BAR 0b00001110 |
||||
// Set cursor to blinking block
|
||||
#define LCD_CURSOR_BLINK 0b00001101 |
||||
// Set cursor to both of the above at once
|
||||
#define LCD_CURSOR_BOTH (LCD_CURSOR_BAR | LCD_CURSOR_BLINK) |
||||
|
||||
// Move cursor
|
||||
#define LCD_MOVE_LEFT 0b00010000 |
||||
#define LCD_MOVE_RIGHT 0b00010100 |
||||
|
||||
// Shift display
|
||||
#define LCD_SHIFT_LEFT 0b00011000 |
||||
#define LCD_SHIFT_RIGHT 0b00011100 |
||||
|
||||
// Set iface to 5x7 font, 1-line
|
||||
#define LCD_IFACE_4BIT_1LINE 0b00100000 |
||||
#define LCD_IFACE_8BIT_1LINE 0b00110000 |
||||
// Set iface to 5x7 font, 2-line
|
||||
#define LCD_IFACE_4BIT_2LINE 0b00101000 |
||||
#define LCD_IFACE_8BIT_2LINE 0b00111000 |
||||
|
||||
|
||||
/** Initialize the display */ |
||||
void lcd_init(); |
||||
|
||||
/** Write an instruction byte */ |
||||
void lcd_command(uint8_t bb); |
||||
|
||||
/** Write a data byte */ |
||||
void lcd_write(uint8_t bb); |
||||
|
||||
/** Read BF & Address */ |
||||
uint8_t lcd_read_bf_addr(); |
||||
|
||||
/** Read CGRAM or DDRAM */ |
||||
uint8_t lcd_read(); |
||||
|
||||
/** Send a string to LCD */ |
||||
void lcd_puts(char* str_p); |
||||
|
||||
/** Send a string to LCD from program memory */ |
||||
void lcd_puts_P(const char* str_p); |
||||
|
||||
/** Sedn a char to LCD */ |
||||
void lcd_putc(const char c); |
||||
|
||||
/** Show string at X, Y */ |
||||
#define lcd_puts_xy(x, y, str_p) do { lcd_xy((x), (y)); lcd_puts((str_p)); } while(0) |
||||
|
||||
/** Show string at X, Y */ |
||||
#define lcd_puts_xy_P(x, y, str_p) do { lcd_xy((x), (y)); lcd_puts_P((str_p)); } while(0) |
||||
|
||||
/** Show char at X, Y */ |
||||
#define lcd_putc_xy(x, y, c) do { lcd_xy((x), (y)); lcd_putc((c)); } while(0) |
||||
|
||||
/** Set cursor position */ |
||||
void lcd_xy(const uint8_t x, const uint8_t y); |
||||
|
||||
/** Set LCD cursor. If not enabled, only remember it. */ |
||||
#define CURSOR_NONE 0b00 |
||||
#define CURSOR_BAR 0b10 |
||||
#define CURSOR_BLINK 0b01 |
||||
#define CURSOR_BOTH 0b11 |
||||
void lcd_cursor(uint8_t type); |
||||
|
||||
/** Display display (preserving cursor) */ |
||||
void lcd_disable(); |
||||
|
||||
/** Enable display (restoring cursor) */ |
||||
void lcd_enable(); |
||||
|
||||
/** Go home */ |
||||
void lcd_home(); |
||||
|
||||
/** Clear the screen */ |
||||
void lcd_clear(); |
||||
|
||||
/** Define a glyph - 8 bytes, right 5 bits are used */ |
||||
void lcd_glyph(const uint8_t index, const uint8_t* array); |
||||
|
||||
/** Define a glyph that's in PROGMEM */ |
||||
void lcd_glyph_P(const uint8_t index, const uint8_t* array); |
||||
|
||||
/** Set address in CGRAM */ |
||||
void lcd_addr_cg(const uint8_t acg); |
||||
|
||||
/** Set address in DDRAM */ |
||||
void lcd_addr(const uint8_t add); |
@ -0,0 +1,6 @@ |
||||
#pragma once |
||||
|
||||
// Weird constructs for the compiler
|
||||
|
||||
// general macros
|
||||
#define SECTION(pos) __attribute__((naked, used, section(pos))) |
@ -0,0 +1,21 @@ |
||||
#pragma once |
||||
|
||||
//
|
||||
// Functions for precise delays (nanoseconds / cycles)
|
||||
//
|
||||
|
||||
#include <avr/io.h> |
||||
#include <util/delay_basic.h> |
||||
#include <stdint.h> |
||||
|
||||
/* Convert nanoseconds to cycle count */ |
||||
#define ns2cycles(ns) ( (ns) / (1000000000L / (signed long) F_CPU) ) |
||||
|
||||
/** Wait c cycles */ |
||||
#define delay_c(c) (((c) > 0) ? __builtin_avr_delay_cycles(c) : __builtin_avr_delay_cycles(0)) |
||||
|
||||
/** Wait n nanoseconds, plus c cycles */ |
||||
#define delay_ns_c(ns, c) delay_c(ns2cycles(ns) + (c)) |
||||
|
||||
/** Wait n nanoseconds */ |
||||
#define delay_ns(ns) delay_c(ns2cycles(ns)) |
@ -0,0 +1,129 @@ |
||||
#pragma once |
||||
|
||||
//
|
||||
// This file provides macros for pin manipulation.
|
||||
//
|
||||
// You can define your application pins like so:
|
||||
//
|
||||
// // Led at PORTB, pin 1
|
||||
// #define LED B,1
|
||||
//
|
||||
// // Switch at PORTD, pin 7
|
||||
// #define SW1 D,7
|
||||
//
|
||||
// Now you can use macros from this file to wirh with the pins, eg:
|
||||
//
|
||||
// as_output(LED);
|
||||
// as_input(SW1);
|
||||
// pullup_on(SW1);
|
||||
//
|
||||
// toggle_pin(LED);
|
||||
// while (pin_is_low(SW1));
|
||||
//
|
||||
// - The macros io2XXX() can be used to get literal name of register associated with the pin.
|
||||
// - io2n() provides pin number.
|
||||
// - The underscored and _aux macros are internal and should not be used elsewhere.
|
||||
// - The io_pack() macro is used to pass pin (io) to other macro without expanding it.
|
||||
//
|
||||
|
||||
#include <avr/io.h> |
||||
#include "calc.h" |
||||
|
||||
// Helpers
|
||||
// Get particular register associated with the name X (eg. D -> PORTD)
|
||||
#define _reg_ddr(X) DDR ## X |
||||
#define _reg_port(X) PORT ## X |
||||
#define _reg_pin(X) PIN ## X |
||||
#define _io2ddr_aux(reg, bit) _reg_ddr(reg) |
||||
#define _io2port_aux(reg, bit) _reg_port(reg) |
||||
#define _io2pin_aux(reg, bit) _reg_pin(reg) |
||||
#define _io2n_aux(reg, bit) bit |
||||
|
||||
|
||||
// === Convert A,1 to corresponding register and pin number ===
|
||||
|
||||
#define io2ddr(io) _io2ddr_aux(io) |
||||
#define io2port(io) _io2port_aux(io) |
||||
#define io2pin(io) _io2pin_aux(io) |
||||
#define io2n(io) _io2n_aux(io) |
||||
|
||||
// === covert "A", "1" to "A,1" for passing on to another macro ===
|
||||
#define io_pack(port, bit) port, bit |
||||
|
||||
|
||||
// === Useful types for ports and pins ===
|
||||
|
||||
// pointer to port
|
||||
typedef volatile uint8_t* PORT_P; |
||||
// number of bit in port
|
||||
typedef uint8_t BIT_N; |
||||
|
||||
|
||||
// === pin manipulation ===
|
||||
|
||||
// Helpers
|
||||
#define _set_pin_aux(port, bit) sbi(_reg_port(port), (bit)) |
||||
#define _clear_pin_aux(port, bit) cbi(_reg_port(port), (bit)) |
||||
#define _read_pin_aux(port, bit) get_bit(_reg_pin(port), (bit)) |
||||
#define _write_pin_aux(port, bit, value) set_bit(_reg_port(port), (bit), (value)) |
||||
#define _toggle_pin_aux(port, bit) sbi(_reg_pin(port), (bit)) |
||||
|
||||
|
||||
// Set pin to HIGH
|
||||
#define pin_up(io) _set_pin_aux(io) |
||||
#define pin_high(io) _set_pin_aux(io) |
||||
|
||||
// Set pin to LOW
|
||||
#define pin_down(io) _clear_pin_aux(io) |
||||
#define pin_low(io) _clear_pin_aux(io) |
||||
|
||||
// Get input pin value
|
||||
#define get_pin(io) _read_pin_aux(io) |
||||
#define read_pin(io) _read_pin_aux(io) |
||||
|
||||
// Check if pin is low or high
|
||||
#define pin_is_low(io) !_read_pin_aux(io) |
||||
#define pin_is_high(io) _read_pin_aux(io) |
||||
|
||||
// Write a value to pin
|
||||
#define set_pin(io, value) _write_pin_aux(io, (value)) |
||||
#define write_pin(io, value) _write_pin_aux(io, (value)) |
||||
#define toggle_pin(io) _toggle_pin_aux(io) |
||||
|
||||
|
||||
// === Setting pin direction ===
|
||||
|
||||
// Helpers
|
||||
#define _as_input_aux(port, bit) cbi(_reg_ddr(port), (bit)) |
||||
#define _as_output_aux(port, bit) sbi(_reg_ddr(port), (bit)) |
||||
#define _set_dir_aux(port, bit, dir) write_bit(_reg_ddr(port), (bit), (dir)) |
||||
|
||||
|
||||
// Pin as input (_pu ... with pull-up)
|
||||
#define as_input(io) _as_input_aux(io) |
||||
#define as_input_pu(io) do { _as_input_aux(io); _pullup_enable_aux(io); } while(0) |
||||
|
||||
// Pin as output
|
||||
#define as_output(io) _as_output_aux(io) |
||||
|
||||
// Set direction (1 ... output)
|
||||
#define set_dir(io, dir) _set_dir_aux(io, (dir)) |
||||
|
||||
|
||||
// === Setting pullup ===
|
||||
|
||||
// Helpers
|
||||
#define _pullup_enable_aux(port, bit) sbi(_reg_port(port), (bit)) |
||||
#define _pullup_disable_aux(port, bit) cbi(_reg_port(port), (bit)) |
||||
#define _set_pullup_aux(port, bit, on) write_bit(_reg_port(port), (bit), (on)) |
||||
|
||||
// Enable pullup
|
||||
#define pullup_enable(io) _pullup_enable_aux(io) |
||||
#define pullup_on(io) _pullup_enable_aux(io) |
||||
|
||||
// Disable pullup
|
||||
#define pullup_disable(io) _pullup_disable_aux(io) |
||||
#define pullup_off(io) _pullup_disable_aux(io) |
||||
|
||||
// Set pullup to value (1 ... pullup enabled)
|
||||
#define set_pullup(io, on) _set_pullup_aux(io, on) |
@ -0,0 +1,211 @@ |
||||
#include <stdlib.h> |
||||
#include <stdbool.h> |
||||
#include <stdint.h> |
||||
|
||||
#include "stream.h" |
||||
#include "calc.h" |
||||
|
||||
|
||||
static char tmpstr[20]; // buffer for number rendering
|
||||
|
||||
|
||||
void put_str(const STREAM *p, char* str) |
||||
{ |
||||
char c; |
||||
while ((c = *str++)) |
||||
p->tx(c); |
||||
} |
||||
|
||||
|
||||
void put_str_P(const STREAM *p, const char* str) |
||||
{ |
||||
char c; |
||||
while ((c = pgm_read_byte(str++))) |
||||
p->tx(c); |
||||
} |
||||
|
||||
|
||||
static void _putnf(const STREAM *p, const uint8_t places); |
||||
|
||||
|
||||
/** Send signed int8 */ |
||||
void put_u8(const STREAM *p, const uint8_t num) |
||||
{ |
||||
utoa(num, tmpstr, 10); |
||||
put_str(p, tmpstr); |
||||
} |
||||
|
||||
|
||||
/** Send unsigned int8 */ |
||||
void put_i8(const STREAM *p, const int8_t num) |
||||
{ |
||||
itoa(num, tmpstr, 10); |
||||
put_str(p, tmpstr); |
||||
} |
||||
|
||||
|
||||
|
||||
/** Send unsigned int */ |
||||
void put_u16(const STREAM *p, const uint16_t num) |
||||
{ |
||||
utoa(num, tmpstr, 10); |
||||
put_str(p, tmpstr); |
||||
} |
||||
|
||||
|
||||
/** Send signed int */ |
||||
void put_i16(const STREAM *p, const int16_t num) |
||||
{ |
||||
itoa(num, tmpstr, 10); |
||||
put_str(p, tmpstr); |
||||
} |
||||
|
||||
|
||||
/** Send unsigned long */ |
||||
void put_u32(const STREAM *p, const uint32_t num) |
||||
{ |
||||
ultoa(num, tmpstr, 10); |
||||
put_str(p, tmpstr); |
||||
} |
||||
|
||||
|
||||
/** Send signed long */ |
||||
void put_i32(const STREAM *p, const int32_t num) |
||||
{ |
||||
ltoa(num, tmpstr, 10); |
||||
put_str(p, tmpstr); |
||||
} |
||||
|
||||
|
||||
/** Print number as hex */ |
||||
void _print_hex(const STREAM *p, uint8_t* start, uint8_t bytes) |
||||
{ |
||||
for (; bytes > 0; bytes--) { |
||||
uint8_t b = *(start + bytes - 1); |
||||
|
||||
for(uint8_t j = 0; j < 2; j++) { |
||||
uint8_t x = high_nibble(b); |
||||
b = b << 4; |
||||
if (x < 0xA) { |
||||
p->tx('0' + x); |
||||
} else { |
||||
p->tx('A' + (x - 0xA)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
/** Send unsigned int8 */ |
||||
void put_x8(const STREAM *p, const uint8_t num) |
||||
{ |
||||
_print_hex(p, (uint8_t*) &num, 1); |
||||
} |
||||
|
||||
|
||||
/** Send int as hex */ |
||||
void put_x16(const STREAM *p, const uint16_t num) |
||||
{ |
||||
_print_hex(p, (uint8_t*) &num, 2); |
||||
} |
||||
|
||||
|
||||
/** Send long as hex */ |
||||
void put_x32(const STREAM *p, const uint32_t num) |
||||
{ |
||||
_print_hex(p, (uint8_t*) &num, 4); |
||||
} |
||||
|
||||
|
||||
/** Send long long as hex */ |
||||
void put_x64(const STREAM *p, const uint64_t num) |
||||
{ |
||||
_print_hex(p, (uint8_t*) &num, 8); |
||||
} |
||||
|
||||
|
||||
|
||||
// float variant doesn't make sense for 8-bit int
|
||||
|
||||
/** Send unsigned int as float */ |
||||
void put_u16f(const STREAM *p, const uint16_t num, const uint8_t places) |
||||
{ |
||||
utoa(num, tmpstr, 10); |
||||
_putnf(p, places); |
||||
} |
||||
|
||||
|
||||
/** Send signed int as float */ |
||||
void put_i16f(const STREAM *p, const int16_t num, const uint8_t places) |
||||
{ |
||||
if (num < 0) { |
||||
p->tx('-'); |
||||
itoa(-num, tmpstr, 10); |
||||
} else { |
||||
itoa(num, tmpstr, 10); |
||||
} |
||||
|
||||
_putnf(p, places); |
||||
} |
||||
|
||||
|
||||
/** Send unsigned long as float */ |
||||
void put_u32f(const STREAM *p, const uint32_t num, const uint8_t places) |
||||
{ |
||||
ultoa(num, tmpstr, 10); |
||||
_putnf(p, places); |
||||
} |
||||
|
||||
|
||||
/** Send signed long as float */ |
||||
void put_i32f(const STREAM *p, const int32_t num, const uint8_t places) |
||||
{ |
||||
if (num < 0) { |
||||
p->tx('-'); |
||||
ltoa(-num, tmpstr, 10); |
||||
} else { |
||||
ltoa(num, tmpstr, 10); |
||||
} |
||||
|
||||
_putnf(p, places); |
||||
} |
||||
|
||||
|
||||
/** Print number in tmp string as float with given decimal point position */ |
||||
void _putnf(const STREAM *p, const uint8_t places) |
||||
{ |
||||
// measure text length
|
||||
uint8_t len = 0; |
||||
while(tmpstr[len] != 0) len++; |
||||
|
||||
int8_t at = len - places; |
||||
|
||||
// print virtual zeros
|
||||
if (at <= 0) { |
||||
p->tx('0'); |
||||
p->tx('.'); |
||||
while(at <= -1) { |
||||
p->tx('0'); |
||||
at++; |
||||
} |
||||
at = -1; |
||||
} |
||||
|
||||
// print the number
|
||||
uint8_t i = 0; |
||||
while(i < len) { |
||||
if (at-- == 0) { |
||||
p->tx('.'); |
||||
} |
||||
|
||||
p->tx(tmpstr[i++]); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** Print CR LF */ |
||||
void put_nl(const STREAM *p) |
||||
{ |
||||
p->tx(13); |
||||
p->tx(10); |
||||
} |
@ -0,0 +1,99 @@ |
||||
#pragma once |
||||
|
||||
//
|
||||
// Streams -- in this library -- are instances of type STREAM.
|
||||
//
|
||||
// A stream can be used for receiving and sending bytes, generally
|
||||
// it's a pipe to a device.
|
||||
//
|
||||
// They are designed for printing numbers and strings, but can
|
||||
// also be used for general data transfer.
|
||||
//
|
||||
// Examples of streams:
|
||||
// "uart.h" -> declares global variable "uart" which is a pointer to the UART stream
|
||||
// "lcd.h" -> declares a global variable "lcd" (pointer to LCD scho stream)
|
||||
//
|
||||
// Streams help avoid code duplication, since the same functions can be used
|
||||
// to format and print data to different device types.
|
||||
//
|
||||
|
||||
#include <stdlib.h> |
||||
#include <stdbool.h> |
||||
#include <stdint.h> |
||||
#include <avr/pgmspace.h> |
||||
|
||||
/** Stream structure */ |
||||
typedef struct { |
||||
void (*tx) (uint8_t b); |
||||
uint8_t (*rx) (void); |
||||
} STREAM; |
||||
|
||||
|
||||
/** Print string into a stream */ |
||||
void put_str(const STREAM *p, char* str); |
||||
|
||||
|
||||
/** Print a programspace string into a stream */ |
||||
void put_str_P(const STREAM *p, const char* str); |
||||
|
||||
|
||||
/** Send signed int8 */ |
||||
void put_u8(const STREAM *p, const uint8_t num); |
||||
|
||||
|
||||
/** Send unsigned int8 */ |
||||
void put_i8(const STREAM *p, const int8_t num); |
||||
|
||||
|
||||
/** Send unsigned int */ |
||||
void put_u16(const STREAM *p, const uint16_t num); |
||||
|
||||
|
||||
/** Send signed int */ |
||||
void put_i16(const STREAM *p, const int16_t num); |
||||
|
||||
|
||||
/** Send unsigned long */ |
||||
void put_u32(const STREAM *p, const uint32_t num); |
||||
|
||||
|
||||
/** Send signed long */ |
||||
void put_i32(const STREAM *p, const int32_t num); |
||||
|
||||
|
||||
/** Send unsigned int8 */ |
||||
void put_x8(const STREAM *p, const uint8_t num); |
||||
|
||||
|
||||
/** Send int as hex */ |
||||
void put_x16(const STREAM *p, const uint16_t num); |
||||
|
||||
|
||||
/** Send long as hex */ |
||||
void put_x32(const STREAM *p, const uint32_t num); |
||||
|
||||
|
||||
/** Send long long as hex */ |
||||
void put_x64(const STREAM *p, const uint64_t num); |
||||
|
||||
|
||||
// float variant doesn't make sense for 8-bit int
|
||||
|
||||
/** Send unsigned int as float */ |
||||
void put_u16f(const STREAM *p, const uint16_t num, const uint8_t places); |
||||
|
||||
|
||||
/** Send signed int as float */ |
||||
void put_i16f(const STREAM *p, const int16_t num, const uint8_t places); |
||||
|
||||
|
||||
/** Send unsigned long as float */ |
||||
void put_u32f(const STREAM *p, const uint32_t num, const uint8_t places); |
||||
|
||||
|
||||
/** Send signed long as float */ |
||||
void put_i32f(const STREAM *p, const int32_t num, const uint8_t places); |
||||
|
||||
|
||||
/** Print CR LF */ |
||||
void put_nl(const STREAM *p); |
@ -0,0 +1,428 @@ |
||||
#include <avr/io.h> |
||||
#include <avr/interrupt.h> |
||||
#include <avr/pgmspace.h> |
||||
#include <util/delay.h> |
||||
#include <stdbool.h> |
||||
#include <stdint.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include "lib/arduino_pins.h" |
||||
#include "lib/calc.h" |
||||
#include "lib/adc.h" |
||||
#include "lib/lcd.h" |
||||
#include "lib/debounce.h" |
||||
|
||||
// Buttons (to ground)
|
||||
#define BTN_LEFT A0 |
||||
#define BTN_RIGHT A1 |
||||
#define BTN_UP A2 |
||||
#define BTN_DOWN A3 |
||||
#define BTN_PAUSE A4 |
||||
#define BTN_RESTART A5 |
||||
|
||||
// Debouncer channels for buttons
|
||||
// (Must be added in this order to debouncer)
|
||||
#define D_LEFT 0 |
||||
#define D_RIGHT 1 |
||||
#define D_UP 2 |
||||
#define D_DOWN 3 |
||||
#define D_PAUSE 4 |
||||
#define D_RESTART 5 |
||||
|
||||
// Board size (!!! rows = 2x number of display lines, max 2*4 = 8 !!!)
|
||||
#define ROWS 8 |
||||
#define COLS 20 |
||||
|
||||
// Delay between snake steps, in 10 ms
|
||||
#define STEP_DELAY 24 |
||||
|
||||
// proto
|
||||
void update(); |
||||
void init_cgram(); |
||||
void init_gameboard(); |
||||
|
||||
void init() |
||||
{ |
||||
// Randomize RNG
|
||||
adc_init(); |
||||
srand(adc_read_word(3)); |
||||
|
||||
// Init LCD
|
||||
lcd_init(); |
||||
init_cgram(); // load default glyphs
|
||||
|
||||
// Init game board.
|
||||
init_gameboard(); |
||||
|
||||
// gamepad buttons
|
||||
as_input_pu(BTN_LEFT); |
||||
as_input_pu(BTN_RIGHT); |
||||
as_input_pu(BTN_UP); |
||||
as_input_pu(BTN_DOWN); |
||||
as_input_pu(BTN_PAUSE); |
||||
as_input_pu(BTN_RESTART); |
||||
|
||||
// add buttons to debouncer
|
||||
debo_add_rev(BTN_LEFT); |
||||
debo_add_rev(BTN_RIGHT); |
||||
debo_add_rev(BTN_UP); |
||||
debo_add_rev(BTN_DOWN); |
||||
debo_add_rev(BTN_PAUSE); |
||||
debo_add_rev(BTN_RESTART); |
||||
|
||||
// setup timer
|
||||
TCCR0A = _BV(WGM01); // CTC
|
||||
TCCR0B = _BV(CS02) | _BV(CS00); // prescaler 1024
|
||||
OCR0A = 156; // interrupt every 10 ms
|
||||
sbi(TIMSK0, OCIE0A); |
||||
sei(); |
||||
} |
||||
|
||||
|
||||
/** timer 0 interrupt vector */ |
||||
ISR(TIMER0_COMPA_vect) |
||||
{ |
||||
debo_tick(); // poll debouncer
|
||||
update(); // update and display
|
||||
} |
||||
|
||||
|
||||
|
||||
// sub-glyphs
|
||||
#define _HEAD_ 15, 21, 21, 30 |
||||
#define _BODY_ 15, 31, 31, 30 |
||||
#define _FOOD_ 10, 21, 17, 14 |
||||
//14, 17, 17, 14
|
||||
#define _NONE_ 0, 0, 0, 0 |
||||
|
||||
// complete glyphs for loading into memory
|
||||
|
||||
// Only one food & one head glyph have to be loaded at a time.
|
||||
|
||||
// Body - Body
|
||||
const uint8_t SYMBOL_BB[] PROGMEM = {_BODY_, _BODY_}; |
||||
|
||||
// Body - None
|
||||
const uint8_t SYMBOL_BX[] PROGMEM = {_BODY_, _NONE_}; |
||||
const uint8_t SYMBOL_XB[] PROGMEM = {_NONE_, _BODY_}; |
||||
|
||||
// Head - None
|
||||
const uint8_t SYMBOL_HX[] PROGMEM = {_HEAD_, _NONE_}; |
||||
const uint8_t SYMBOL_XH[] PROGMEM = {_NONE_, _HEAD_}; |
||||
|
||||
// Body - Head
|
||||
const uint8_t SYMBOL_BH[] PROGMEM = {_BODY_, _HEAD_}; |
||||
const uint8_t SYMBOL_HB[] PROGMEM = {_HEAD_, _BODY_}; |
||||
|
||||
// Head - Food
|
||||
const uint8_t SYMBOL_HF[] PROGMEM = {_HEAD_, _FOOD_}; |
||||
const uint8_t SYMBOL_FH[] PROGMEM = {_FOOD_, _HEAD_}; |
||||
|
||||
// Food - None
|
||||
const uint8_t SYMBOL_FX[] PROGMEM = {_FOOD_, _NONE_}; |
||||
const uint8_t SYMBOL_XF[] PROGMEM = {_NONE_, _FOOD_}; |
||||
|
||||
// Body - Food
|
||||
const uint8_t SYMBOL_BF[] PROGMEM = {_BODY_, _FOOD_}; |
||||
const uint8_t SYMBOL_FB[] PROGMEM = {_FOOD_, _BODY_}; |
||||
|
||||
|
||||
// board block (snake, food...)
|
||||
typedef enum { |
||||
bEMPTY = 0x00, |
||||
bHEAD = 0x01, |
||||
bFOOD = 0x02, |
||||
bBODY_LEFT = 0x80, |
||||
bBODY_RIGHT = 0x81, |
||||
bBODY_UP = 0x82, |
||||
bBODY_DOWN = 0x83, |
||||
} block_t; |
||||
|
||||
// Snake direction
|
||||
typedef enum { |
||||
dLEFT = 0x00, |
||||
dRIGHT = 0x01, |
||||
dUP = 0x02, |
||||
dDOWN = 0x03, |
||||
} dir_t; |
||||
|
||||
// Coordinate on board
|
||||
typedef struct { |
||||
int8_t x; |
||||
int8_t y; |
||||
} coord_t; |
||||
|
||||
#define is_body(blk) (((blk) & 0x80) != 0) |
||||
#define mk_body_dir(dir) (0x80 + (dir)) |
||||
|
||||
// compare two coords
|
||||
#define coord_eq(a, b) (((a).x == (b).x) && ((a).y == (b).y)) |
||||
|
||||
|
||||
bool crashed; |
||||
uint8_t snake_len; |
||||
|
||||
// y, x indexing
|
||||
block_t board[ROWS][COLS]; |
||||
|
||||
coord_t head_pos; |
||||
coord_t tail_pos; |
||||
dir_t head_dir; |
||||
|
||||
const uint8_t CODE_BB = 0; |
||||
const uint8_t CODE_BX = 1; |
||||
const uint8_t CODE_XB = 2; |
||||
const uint8_t CODE_H = 3; // glyph with head, dynamic
|
||||
const uint8_t CODE_F = 4; // glyph with food, dynamic
|
||||
const uint8_t CODE_XX = 32; // space
|
||||
|
||||
|
||||
// Set a block in board
|
||||
#define set_block_xy(x, y, block) do { board[y][x] = (block); } while(0) |
||||
#define get_block_xy(x, y) board[y][x] |
||||
#define get_block(pos) get_block_xy((pos).x, (pos).y) |
||||
#define set_block(pos, block) set_block_xy((pos).x, (pos).y, (block)) |
||||
|
||||
|
||||
void init_cgram() |
||||
{ |
||||
// those will be always the same
|
||||
lcd_glyph_P(CODE_BB, SYMBOL_BB); |
||||
lcd_glyph_P(CODE_BX, SYMBOL_BX); |
||||
lcd_glyph_P(CODE_XB, SYMBOL_XB); |
||||
lcd_glyph_P(5, SYMBOL_XF); |
||||
} |
||||
|
||||
|
||||
void place_food() |
||||
{ |
||||
while(1) { |
||||
const uint8_t xx = rand() % COLS; |
||||
const uint8_t yy = rand() % ROWS; |
||||
|
||||
if (get_block_xy(xx, yy) == bEMPTY) { |
||||
set_block_xy(xx, yy, bFOOD); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
void init_gameboard() |
||||
{ |
||||
//erase the board
|
||||
for (uint8_t x = 0; x < COLS; x++) { |
||||
for (uint8_t y = 0; y < ROWS; y++) { |
||||
set_block_xy(x, y, bEMPTY); |
||||
} |
||||
} |
||||
|
||||
lcd_clear(); |
||||
|
||||
tail_pos = (coord_t) {.x = 0, .y = 0}; |
||||
|
||||
set_block_xy(0, 0, bBODY_RIGHT); |
||||
set_block_xy(1, 0, bBODY_RIGHT); |
||||
set_block_xy(2, 0, bBODY_RIGHT); |
||||
set_block_xy(3, 0, bHEAD); |
||||
|
||||
head_pos = (coord_t) {.x = 3, .y = 0}; |
||||
|
||||
snake_len = 4; // includes head
|
||||
|
||||
head_dir = dRIGHT; |
||||
crashed = false; |
||||
|
||||
place_food(); |
||||
} |
||||
|
||||
|
||||
uint8_t presc = 0; |
||||
|
||||
bool restart_held; |
||||
bool pause_held; |
||||
bool paused; |
||||
void update() |
||||
{ |
||||
if (debo_get_pin(D_RESTART)) { |
||||
|
||||
if (!restart_held) { |
||||
// restart
|
||||
init_gameboard(); |
||||
presc = 0; |
||||
restart_held = true; |
||||
} |
||||
|
||||
} else { |
||||
restart_held = false; |
||||
} |
||||
|
||||
if (debo_get_pin(D_PAUSE)) { |
||||
|
||||
if (!pause_held) { |
||||
paused ^= true; |
||||
pause_held = true; |
||||
} |
||||
|
||||
} else { |
||||
pause_held = false; |
||||
} |
||||
|
||||
if(!crashed && !paused) { |
||||
|
||||
// resolve movement direction
|
||||
if (debo_get_pin(D_LEFT)) |
||||
head_dir = dLEFT; |
||||
else if (debo_get_pin(D_RIGHT)) |
||||
head_dir = dRIGHT; |
||||
else if (debo_get_pin(D_UP)) |
||||
head_dir = dUP; |
||||
else if (debo_get_pin(D_DOWN)) |
||||
head_dir = dDOWN; |
||||
|
||||
// time's up for a move
|
||||
if (presc++ == STEP_DELAY) { |
||||
presc = 0; |
||||
|
||||
// move snake
|
||||
const coord_t oldpos = head_pos; |
||||
|
||||
switch (head_dir) { |
||||
case dLEFT: head_pos.x--; break; |
||||
case dRIGHT: head_pos.x++; break; |
||||
case dUP: head_pos.y--; break; |
||||
case dDOWN: head_pos.y++; break; |
||||
} |
||||
|
||||
bool do_move = false; |
||||
bool do_grow = false; |
||||
|
||||
if (head_pos.x < 0 || head_pos.x >= COLS || head_pos.y < 0 || head_pos.y >= ROWS) { |
||||
// ouch, a wall!
|
||||
crashed = true; |
||||
} else { |
||||
// check if tile occupied or not
|
||||
if (coord_eq(head_pos, tail_pos)) { |
||||
// head moved in previous tail, that's OK.
|
||||
do_move = true; |
||||
} else { |
||||
// moved to other tile than tail
|
||||
switch (get_block(head_pos)) { |
||||
|
||||
case bFOOD: |
||||
do_grow = true; // fall through
|
||||
case bEMPTY: |
||||
do_move = true; |
||||
break; |
||||
|
||||
default: // includes all BODY_xxx
|
||||
crashed = true; // snake crashed into some block
|
||||
} |
||||
} |
||||
} |
||||
|
||||
if (do_move) { |
||||
// Move tail
|
||||
if (do_grow) { |
||||
// let tail as is
|
||||
snake_len++; // grow the counter
|
||||
} else { |
||||
// tail dir
|
||||
dir_t td = get_block(tail_pos) & 0xF; |
||||
|
||||
// clean tail
|
||||
set_block(tail_pos, bEMPTY); |
||||
|
||||
// move tail based on old direction of tail block
|
||||
switch (td) { |
||||
case dLEFT: tail_pos.x--; break; |
||||
case dRIGHT: tail_pos.x++; break; |
||||
case dUP: tail_pos.y--; break; |
||||
case dDOWN: tail_pos.y++; break; |
||||
} |
||||
} |
||||
|
||||
// Move head
|
||||
set_block(head_pos, bHEAD); // place head in new pos
|
||||
set_block(oldpos, mk_body_dir(head_dir)); // directional body in old head pos
|
||||
|
||||
if (do_grow) { |
||||
// food eaten, place new
|
||||
place_food(); |
||||
} |
||||
} |
||||
} |
||||
} // end !crashed
|
||||
|
||||
|
||||
// Render the board
|
||||
for (uint8_t r = 0; r < ROWS / 2; r++) { |
||||
lcd_xy(0, r); |
||||
for (uint8_t c = 0; c < COLS; c++) { |
||||
const block_t t1 = get_block_xy(c, r * 2); |
||||
const block_t t2 = get_block_xy(c, (r * 2) + 1); |
||||
|
||||
uint8_t code = '!'; // ! marks fail
|
||||
|
||||
if ((t1 == bEMPTY) && (t2 == bEMPTY)) { |
||||
code = CODE_XX; |
||||
if (crashed) code = '*'; |
||||
} else if (is_body(t1) && is_body(t2)) |
||||
code = CODE_BB; |
||||
else if (is_body(t1) && (t2 == bEMPTY)) |
||||
code = CODE_BX; |
||||
else if (t1 == bEMPTY && is_body(t2)) |
||||
code = CODE_XB; |
||||
else if ((t1 == bFOOD) || (t2 == bFOOD)) { |
||||
// one is food
|
||||
|
||||
code = CODE_F; |
||||
|
||||
if (t1 == bFOOD) { |
||||
if (t2 == bEMPTY) |
||||
lcd_glyph_P(code, SYMBOL_FX); |
||||
else if (t2 == bHEAD) |
||||
lcd_glyph_P(code, SYMBOL_FH); |
||||
else if (is_body(t2)) |
||||
lcd_glyph_P(code, SYMBOL_FB); |
||||
} else { // t2 is food
|
||||
if (t1 == bEMPTY) |
||||
lcd_glyph_P(code, SYMBOL_XF); |
||||
else if (t1 == bHEAD) |
||||
lcd_glyph_P(code, SYMBOL_HF); |
||||
else if (is_body(t1)) |
||||
lcd_glyph_P(code, SYMBOL_BF); |
||||
} |
||||
lcd_xy(c,r); |
||||
|
||||
} else if ((t1 == bHEAD )|| (t2 == bHEAD)) { |
||||
// one is head
|
||||
|
||||
code = CODE_H; |
||||
|
||||
if (t1 == bHEAD) { |
||||
if (t2 == bEMPTY) |
||||
lcd_glyph_P(code, SYMBOL_HX); |
||||
else if (is_body(t2)) |
||||
lcd_glyph_P(code, SYMBOL_HB); |
||||
} else { // t2 is head
|
||||
if (t1 == bEMPTY) |
||||
lcd_glyph_P(code, SYMBOL_XH); |
||||
else if (is_body(t1)) |
||||
lcd_glyph_P(code, SYMBOL_BH); |
||||
} |
||||
|
||||
lcd_xy(c,r); |
||||
} |
||||
|
||||
lcd_putc(code); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void main() |
||||
{ |
||||
init(); |
||||
|
||||
while(1); // timer does everything
|
||||
} |
Loading…
Reference in new issue