From e5af70e8f23f4f524c7fc43ba7801477d1a5f332 Mon Sep 17 00:00:00 2001 From: h4rm0n1c Date: Fri, 17 Jul 2015 03:09:26 +0800 Subject: [PATCH] Base ESP8266 Support Started over with a fresh fork of the DMD2 library, since I'd borked things up so badly with the old one, developing in my own branch. --- DMD2.cpp | 14 ++++++++++++++ DMD2.h | 8 ++++++-- DMD2_Text.cpp | 2 +- DMD2_Timer.cpp | 33 ++++++++++++++++++++++++++++++++- fonts/Arial14.h | 2 ++ fonts/Arial_Black_16.h | 2 ++ fonts/Droid_Sans_12.h | 2 ++ fonts/Droid_Sans_16.h | 2 ++ fonts/Droid_Sans_24.h | 2 ++ fonts/SystemFont5x7.h | 2 ++ library.properties | 2 +- 11 files changed, 66 insertions(+), 5 deletions(-) diff --git a/DMD2.cpp b/DMD2.cpp index 043e1aa..5f6c103 100644 --- a/DMD2.cpp +++ b/DMD2.cpp @@ -23,7 +23,11 @@ typedef intptr_t port_reg_t; SPIDMD::SPIDMD(byte panelsWide, byte panelsHigh) +#ifdef ESP8266 + : BaseDMD(panelsWide, panelsHigh, 0, 16, 12, 15) +#else : BaseDMD(panelsWide, panelsHigh, 9, 6, 7, 8) +#endif { } @@ -41,6 +45,8 @@ void SPIDMD::beginNoTimer() SPI.setDataMode(SPI_MODE0); // CPOL=0, CPHA=0 #ifdef __AVR__ SPI.setClockDivider(SPI_CLOCK_DIV4); // 4MHz clock. 8MHz (DIV2 not DIV4) is possible if you have short cables. Longer cables may need DIV8/DIV16. +#elif defined(ESP8266) + SPI.setFrequency(4000000); // ESP can run at 80mhz or 160mhz, setting frequency directly is easier, set to 4MHz. #else SPI.setClockDivider(20); // 4.2MHz on Due. Same comment as above applies (lower numbers = less divider = faster speeds.) #endif @@ -96,6 +102,9 @@ void BaseDMD::scanDisplay() analogWrite(pin_noe, brightness); } +#ifdef ESP8266 +// No SoftDMD for ESP8266 for now +#else SoftDMD::SoftDMD(byte panelsWide, byte panelsHigh) : BaseDMD(panelsWide, panelsHigh, 9, 6, 7, 8), pin_clk(13), @@ -152,6 +161,7 @@ void SoftDMD::writeSPIData(volatile uint8_t *rows[4], const int rowsize) softSPITransfer(*(rows[0]++), port_r_data, mask_r_data, port_clk, mask_clk); } } +#endif BaseDMD::BaseDMD(byte panelsWide, byte panelsHigh, byte pin_noe, byte pin_a, byte pin_b, byte pin_sck) : @@ -161,7 +171,11 @@ BaseDMD::BaseDMD(byte panelsWide, byte panelsHigh, byte pin_noe, byte pin_a, byt pin_a(pin_a), pin_b(pin_b), pin_sck(pin_sck), +#ifdef ESP8266 + default_pins(pin_noe == 0 && pin_a == 16 && pin_b == 12 && pin_sck == 15), +#else default_pins(pin_noe == 9 && pin_a == 6 && pin_b == 7 && pin_sck == 8), +#endif pin_other_cs(-1), brightness(255) { diff --git a/DMD2.h b/DMD2.h index 2bb2b59..44fc59c 100644 --- a/DMD2.h +++ b/DMD2.h @@ -141,7 +141,7 @@ class DMDFrame void drawString(int x, int y, const char *bChars, DMDGraphicsMode mode=GRAPHICS_ON, const uint8_t *font = NULL); void drawString(int x, int y, const String &str, DMDGraphicsMode mode=GRAPHICS_ON, const uint8_t *font = NULL); -#ifdef __AVR__ +#if defined(__AVR__) || defined(ESP8266) void drawString_P(int x, int y, const char *flashStr, DMDGraphicsMode mode=GRAPHICS_ON, const uint8_t *font = NULL); inline void drawString(int x, int y, const __FlashStringHelper *flashStr, DMDGraphicsMode mode=GRAPHICS_ON, const uint8_t *font = NULL) { return drawString_P(x,y,(const char*)flashStr, mode, font); @@ -152,7 +152,7 @@ class DMDFrame int charWidth(const char letter, const uint8_t *font = NULL); //Find the width of a string (width of all characters plus 1 pixel "kerning" between each character) -#ifdef __AVR__ +#if defined(__AVR__) || defined(ESP8266) unsigned int stringWidth_P(const char *flashStr, const uint8_t *font = NULL); inline unsigned int stringWidth(const __FlashStringHelper *flashStr, const uint8_t *font = NULL) { return stringWidth_P((const char*)flashStr, font); @@ -262,6 +262,9 @@ class SPIDMD : public BaseDMD void writeSPIData(volatile uint8_t *rows[4], const int rowsize); }; +#ifdef ESP8266 +// No SoftDMD for ESP8266 for now +#else class SoftDMD : public BaseDMD { public: @@ -299,6 +302,7 @@ class DMD_TextBox : public Print { int16_t cur_y; bool pending_newline; }; +#endif // Six byte header at beginning of FontCreator font structure, stored in PROGMEM struct FontHeader { diff --git a/DMD2_Text.cpp b/DMD2_Text.cpp index 5b2660d..331e29b 100644 --- a/DMD2_Text.cpp +++ b/DMD2_Text.cpp @@ -153,7 +153,7 @@ template __attribute__((always_inline)) inline unsigned int _str } -#ifdef __AVR__ +#if defined(__AVR__) || defined (ESP8266) // Small wrapper class to allow indexing of progmem strings via [] (should be inlined out of the actual implementation) class _FlashStringWrapper { const char *str; diff --git a/DMD2_Timer.cpp b/DMD2_Timer.cpp index 86431c7..6f139eb 100644 --- a/DMD2_Timer.cpp +++ b/DMD2_Timer.cpp @@ -90,7 +90,7 @@ void BaseDMD::end() scanDisplay(); } -#else // __ARM__, Due assumed for now +#elif defined (__arm__) // __ARM__, Due assumed for now /* ARM timer callback (ISR context), checks timer status then scans all running DMDs */ void TC7_Handler(){ @@ -128,6 +128,37 @@ void BaseDMD::end() scanDisplay(); } +#elif defined (ESP8266) + +void BaseDMD::begin() +{ + beginNoTimer(); + timer1_disable(); + + register_running_dmd(this); + + timer1_isr_init(); + timer1_attachInterrupt(scan_running_dmds); + timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP); +#if defined(F_CPU) && (F_CPU == 160000000L) + timer1_write(50000); // 50000 ticks = ~5ms with /16 divider at 160MHz +#else + timer1_write(25000); // 25000 ticks = ~5ms with /16 divider at 80MHz +#endif +} + +void BaseDMD::end() +{ + bool still_running = unregister_running_dmd(this); + if(!still_running) + { + timer1_detachInterrupt(); + timer1_disable(); + } + clearScreen(); + scanDisplay(); +} + #endif // ifdef __AVR__ /* Following functions are static non-architecture-specific functions diff --git a/fonts/Arial14.h b/fonts/Arial14.h index a403474..28d920f 100644 --- a/fonts/Arial14.h +++ b/fonts/Arial14.h @@ -39,6 +39,8 @@ #include #ifdef __AVR__ #include +#elif defined (ESP8266) +#include #else #define PROGMEM #endif diff --git a/fonts/Arial_Black_16.h b/fonts/Arial_Black_16.h index d369916..17e00c0 100644 --- a/fonts/Arial_Black_16.h +++ b/fonts/Arial_Black_16.h @@ -37,6 +37,8 @@ #include #ifdef __AVR__ #include +#elif defined (ESP8266) +#include #else #define PROGMEM #endif diff --git a/fonts/Droid_Sans_12.h b/fonts/Droid_Sans_12.h index ecd51fe..dd53e4f 100644 --- a/fonts/Droid_Sans_12.h +++ b/fonts/Droid_Sans_12.h @@ -39,6 +39,8 @@ #include #ifdef __AVR__ #include +#elif defined (ESP8266) +#include #else #define PROGMEM #endif diff --git a/fonts/Droid_Sans_16.h b/fonts/Droid_Sans_16.h index 57646e5..be0ff8e 100644 --- a/fonts/Droid_Sans_16.h +++ b/fonts/Droid_Sans_16.h @@ -39,6 +39,8 @@ #include #ifdef __AVR__ #include +#elif defined (ESP8266) +#include #else #define PROGMEM #endif diff --git a/fonts/Droid_Sans_24.h b/fonts/Droid_Sans_24.h index f3d48f5..1b9ebe3 100644 --- a/fonts/Droid_Sans_24.h +++ b/fonts/Droid_Sans_24.h @@ -39,6 +39,8 @@ #include #ifdef __AVR__ #include +#elif defined (ESP8266) +#include #else #define PROGMEM #endif diff --git a/fonts/SystemFont5x7.h b/fonts/SystemFont5x7.h index 3224b1c..70a1cb7 100644 --- a/fonts/SystemFont5x7.h +++ b/fonts/SystemFont5x7.h @@ -32,6 +32,8 @@ #include #ifdef __AVR__ #include +#elif defined (ESP8266) +#include #else #define PROGMEM #endif diff --git a/library.properties b/library.properties index 4e4ceb2..5e5a735 100644 --- a/library.properties +++ b/library.properties @@ -6,4 +6,4 @@ sentence=Updated (beta) library for Freetronics DMD dot matrix displays. paragraph=Supports graphics operations (test, images, etc.) Still experimental, the stable library is called called "DMD" category=Display url=https://github.com/freetronics/DMD2/ -architectures=avr,sam +architectures=avr,sam,esp8266