diff --git a/examples/arduino-uno/.cargo/config.toml b/examples/arduino-uno/.cargo/config.toml new file mode 100644 index 0000000..26730bb --- /dev/null +++ b/examples/arduino-uno/.cargo/config.toml @@ -0,0 +1,8 @@ +[build] +target = "avr-atmega328p.json" + +[target.'cfg(target_arch = "avr")'] +runner = "./uno-runner.sh" + +[unstable] +build-std = ["core"] diff --git a/examples/arduino-uno/Cargo.toml b/examples/arduino-uno/Cargo.toml new file mode 100644 index 0000000..eb47353 --- /dev/null +++ b/examples/arduino-uno/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "arduino-uno-hdd44780" +version = "0.1.0" +authors = ["Oliver Evans ! { + let dp = arduino_uno::Peripherals::take().unwrap(); + let mut pins = arduino_uno::Pins::new(dp.PORTB, dp.PORTC, dp.PORTD); + + let mut delay = Delay::new(); + let d2 = pins.d2.into_output(&mut pins.ddr); + let d3 = pins.d3.into_output(&mut pins.ddr); + let d4 = pins.d4.into_output(&mut pins.ddr); + let d5 = pins.d5.into_output(&mut pins.ddr); + let d11 = pins.d11.into_output(&mut pins.ddr); // enable + let d12 = pins.d12.into_output(&mut pins.ddr); // rs + + let display_mode = DisplayMode { + cursor_visibility: Cursor::Invisible, + cursor_blink: CursorBlink::Off, + display: Display::On, + }; + let mut lcd = HD44780::new_4bit(d12, d11, d5, d4, d3, d2, &mut delay).unwrap(); + + // Configure LCD + lcd.reset(&mut delay).unwrap(); + lcd.clear(&mut delay).unwrap(); + lcd.set_display_mode(display_mode, &mut delay).unwrap(); + + // Write first line + lcd.set_cursor_pos(0, &mut delay).unwrap(); + lcd.write_str("Hello, rust!", &mut delay).unwrap(); + let mut seconds: u16 = 0; + + // Buffer for second line + let mut buffer = [0u8; 16]; + + loop { + // Write second line + lcd.set_cursor_pos(LINE2_POSITION, &mut delay).unwrap(); + let line2_bytes = seconds.numtoa(10, &mut buffer); + lcd.write_bytes(line2_bytes, &mut delay).unwrap(); + + // Sleep + arduino_uno::delay_ms(1000); + seconds += 1; + } +} diff --git a/examples/arduino-uno/uno-runner.sh b/examples/arduino-uno/uno-runner.sh new file mode 100755 index 0000000..db3f694 --- /dev/null +++ b/examples/arduino-uno/uno-runner.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env sh +set -e + +if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + +if [ "$#" -lt 1 ]; then + echo "$0: no ELF file given" >&2 + exit 1 +fi + +NAME="$(basename "$1")" +SIZE_TEXT="$(avr-size "$1" | tail -1 | cut -f1)" +SIZE_DATA="$(avr-size "$1" | tail -1 | cut -f2)" +SIZE_BSS="$(avr-size "$1" | tail -1 | cut -f3)" + +printf "\n" +printf "Program: %s\n" "$NAME" +printf "Size:\n" +printf " .text %s (exact: %d)\n" "$(numfmt --to=si --padding=9 "$SIZE_TEXT")" "$SIZE_TEXT" +printf " .data %s (exact: %d)\n" "$(numfmt --to=si --padding=9 "$SIZE_DATA")" "$SIZE_DATA" +printf " .bss %s (exact: %d)\n" "$(numfmt --to=si --padding=9 "$SIZE_BSS")" "$SIZE_BSS" +printf "\n" +printf "Attempting to flash ...\n" +printf "\n" + +avrdude -q -C/etc/avrdude.conf -patmega328p -carduino -P/dev/ttyACM0 -D "-Uflash:w:$1:e"