avr-something

Something something AVR side…
git clone git://henryandlizzy.uk/avr-something
Log | Files | Refs | README

commit 20ae01c1b3562c740b1ec60c4230780833a6503e
parent b16c6796f167e253997ec3aea8d925553cffe9e1
Author: Henry Wilson <m3henry@googlemail.com>
Date:   Fri,  2 Mar 2018 17:57:22 +0000

progmem version of puts

Diffstat:
Mlcd.cpp | 26++++++++++++++++++++++----
Mmain.cpp | 62++++----------------------------------------------------------
Mutils.h | 34++++++++++++++++++++++------------
3 files changed, 48 insertions(+), 74 deletions(-)

diff --git a/lcd.cpp b/lcd.cpp @@ -92,7 +92,19 @@ void test() }; uint16_t word; - pixel() : word(0) {} + constexpr pixel(uint16_t const val = 0) + : word(val) + {} + + constexpr pixel(uint8_t const red, uint8_t const green, uint8_t const blue) + : r(red) + , g(green) + , b(blue) + {} + static constexpr auto grey(uint8_t val) + { + return pixel(val >> 1, val, val >> 1); + } } p; static_assert(sizeof(pixel) == sizeof(uint16_t), "not correct size"); @@ -161,7 +173,7 @@ struct auto g = drawSequence(); - auto bytePtr = flashPtr(fonts::mash[c - ' ']); + auto bytePtr = flashPtr<uint8_t>(fonts::mash[c - ' ']); uint8_t x = 8; do @@ -188,10 +200,14 @@ struct if (x >= 320 - 8) newline(); else x += count * 8; } - void puts(char const* str) + void puts_ram(char const* str) { while (auto c = *str++) putc(c); } + void puts(flashPtr<char> str) + { + while (auto c = str.load_post_inc()) putc(c); + } uint16_t x = 0, y = 0; uint16_t foreground = white; @@ -199,9 +215,11 @@ struct fonts::glyph const* glyphs = fonts::mash; } lout; +char const testStr[] [[gnu::__progmem__]] = "Hello, World!\nIt is a very nice day today, what are you up to this afternoon?"; + void testFont() { - lout.puts("Hello, World!\nIt is a very nice day today, what are you up to today?"); + lout.puts(testStr); } } diff --git a/main.cpp b/main.cpp @@ -1,63 +1,14 @@ #include "hidModule.h" #include "lcd.h" +#include "timer.h" #include "ctrl.h" #include <avr/interrupt.h> #include <util/delay.h> -namespace timer -{ -namespace one -{ -auto& control() -{ - return memory<uint16_t>(0x80); -} -auto& inputCapture() -{ - return memory<uint16_t>(0x86); -} -auto& outputCompareA() -{ - return memory<uint16_t>(0x88); -} -auto& outputCompareB() -{ - return memory<uint16_t>(0x8A); -} -} - -namespace two -{ -auto& control() -{ - return memory<uint16_t>(0xB0); -} -auto& value() -{ - return memory(0xB2); -} -auto& outputCompareA() -{ - return memory(0xB3); -} -auto& outputCompareB() -{ - return memory(0xB4); -} -auto& interruptMask() -{ - return memory(0x70); -} -auto const& interruptFlags() -{ - return memory(0x37); -} -} -} -uint8_t const sinTable[] [[gnu::progmem]] = +int8_t const sinTable[] [[gnu::progmem]] = { 0, 3, 6, 9, 12, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, @@ -79,11 +30,6 @@ int main(void) io::direction::D() |= bits(5, 6, 7); - /*timer::one::outputCompareA() = 0x0080; - timer::one::outputCompareB() = 0x8000; - timer::one::inputCapture() = 0x0100; - timer::one::control() = 0b00'0'10'001'1111'00'00;*/ - timer::two::outputCompareA() = 0x80; timer::two::outputCompareB() = 0x80; timer::two::control() = 0b00'00'0'001'1111'00'11; @@ -91,9 +37,9 @@ int main(void) for (;;) { constexpr uint8_t midPoint = 127; - constexpr auto begin = flashPtr(sinTable); + constexpr auto begin = flashPtr<int8_t>(sinTable); constexpr auto end = begin + 64; - auto ptr = flashPtr(sinTable); + auto ptr = flashPtr<int8_t>(sinTable); do { auto val = midPoint + ptr.load_post_inc(); diff --git a/utils.h b/utils.h @@ -36,12 +36,22 @@ inline constexpr uint8_t mix(const uint8_t a, const uint8_t b, const uint8_t mas return (a & ~mask) | (b & mask); } +template <typename T> struct flashPtr { - inline constexpr flashPtr(uint8_t const* const p) : ptr(p) {} - inline uint8_t load() const + static_assert(sizeof(T) == 1, "flashPtr operates on single bytes"); + + constexpr flashPtr(T const* const p) + : ptr(p) + {} + template <uint16_t S> + constexpr flashPtr(const T(& a)[S]) + : ptr(static_cast<T const*>(a)) + {} + + T load() const { - uint8_t result; + T result; asm volatile ( "lpm %0, Z" @@ -51,9 +61,9 @@ struct flashPtr ); return result; } - inline uint8_t load_post_inc() + T load_post_inc() { - uint8_t result; + T result; asm volatile ( "lpm %0, Z+" @@ -63,33 +73,33 @@ struct flashPtr ); return result; } - inline constexpr flashPtr& operator ++() + constexpr flashPtr& operator ++() { ++ptr; return *this; } - inline constexpr flashPtr& operator --() + constexpr flashPtr& operator --() { --ptr; return *this; } - inline constexpr flashPtr operator +(const uint16_t delta) const + constexpr flashPtr operator +(const uint16_t delta) const { return flashPtr(ptr + delta); } - inline constexpr flashPtr operator -(const uint16_t delta) const + constexpr flashPtr operator -(const uint16_t delta) const { return flashPtr(ptr - delta); } - inline constexpr bool operator !=(const flashPtr& that) const + constexpr bool operator !=(const flashPtr& that) const { return ptr != that.ptr; } - static_assert(sizeof(uint16_t) == sizeof(uint8_t const *), "Flash ptr should be a word"); + static_assert(sizeof(uint16_t) == sizeof(T const *), "Flash ptr should be a word"); private: - uint8_t const* ptr; + T const* ptr; };