1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <Adafruit_GFX.h> 21 #include <Adafruit_ST7789.h> 22 #include <Arduino.h> 23 #include <Fonts/FreeSansBold9pt7b.h> 24 #include <SPI.h> 25 #include <esp_heap_caps.h> 26 27 #include "color.h" 28 #include "controls.h" 29 #include "frame_buffer.h" 30 #include "pins.h" 31 #include "scenes/dashboard.h" 32 #include "scenes/fireworks.h" 33 #include "scenes/plasma.h" 34 #include "scenes/torus.h" 35 #include "stats.h" 36 37 namespace { 38 39 constexpr int16_t kW = 320 , kH = 170 ; 40 constexpr uint32_t kSpiHz = 40'000'000 ; 41 constexpr uint32_t kAutoCycleMs = 12'000 ; 42 constexpr uint32_t kBannerMs = 2'000 ; 43 44 Adafruit_ST7789 tft ( & SPI , PIN_TFT_CS , PIN_TFT_DC , PIN_TFT_RST ) ; 45 FrameBuffer fb ( kW , kH ) ; 46 Stats stats ; 47 48 TorusScene torus ; 49 PlasmaScene plasma ; 50 FireworksScene fireworks ; 51 DashboardScene dashboard ( stats ) ; 52 Scene * const scenes [ ] = { & torus , & plasma , & fireworks , & dashboard } ; 53 constexpr int kSceneCount = sizeof ( scenes ) / sizeof ( scenes [ 0 ] ) ; 54 55 struct { 56 int scene = 0 ; 57 uint32_t sceneStartedAt = 0 ; 58 bool autoCycle = true ; 59 bool hud = true ; 60 } state ; 61 62 63 64 void showScene ( int index ) { 65 state . scene = ( index % kSceneCount + kSceneCount ) % kSceneCount ; 66 state . sceneStartedAt = millis ( ) ; 67 scenes [ state . scene ] -> enter ( fb ) ; 68 log_i ( "scene %d/%d: %s" , state . scene + 1 , kSceneCount , scenes [ state . scene ] -> name ( ) ) ; 69 } 70 71 72 73 74 void drawBanner ( uint32_t sinceStart ) { 75 if ( sinceStart >= kBannerMs ) return ; 76 constexpr uint32_t slide = 250 ; 77 float k = 1 ; 78 if ( sinceStart < slide ) k = sinceStart / float ( slide ) ; 79 else if ( sinceStart > kBannerMs - slide ) k = ( kBannerMs - sinceStart ) / float ( slide ) ; 80 k = 1 - ( 1 - k ) * ( 1 - k ) ; 81 82 char text [ 40 ] ; 83 snprintf ( text , sizeof ( text ) , "%d/%d %s" , state . scene + 1 , kSceneCount , scenes [ state . scene ] -> name ( ) ) ; 84 fb . setFont ( & FreeSansBold9pt7b ) ; 85 fb . setTextSize ( 1 ) ; 86 int16_t x1 , y1 ; 87 uint16_t w , h ; 88 fb . getTextBounds ( text , 0 , 0 , & x1 , & y1 , & w , & h ) ; 89 const int bw = w + 28 , bh = 28 , bx = ( kW - bw ) / 2 , by = int ( - bh + ( bh + 8 ) * k ) ; 90 fb . fillRoundRect ( bx , by , bw , bh , 14 , pal :: ink ) ; 91 fb . setTextColor ( pal :: night ) ; 92 fb . setCursor ( bx + 14 - x1 , by + 19 ) ; 93 fb . print ( text ) ; 94 } 95 96 97 void drawHud ( ) { 98 if ( ! state . hud ) return ; 99 char text [ 48 ] ; 100 snprintf ( text , sizeof ( text ) , "%2.0f fps draw %4.1f send %4.1f ms%s" , stats . fps , stats . drawMs , stats . pushMs , 101 state . autoCycle ? " auto" : "" ) ; 102 fb . setFont ( nullptr ) ; 103 fb . setTextSize ( 1 ) ; 104 fb . fillRect ( 0 , kH - 12 , int ( strlen ( text ) ) * 6 + 8 , 12 , pal :: night ) ; 105 fb . setTextColor ( pal :: amber ) ; 106 fb . setCursor ( 4 , kH - 10 ) ; 107 fb . print ( text ) ; 108 } 109 110 111 void drawButtonDot ( ) { 112 if ( ! controls :: showsPressDot ( ) ) return ; 113 fb . fillCircle ( kW - 10 , kH - 10 , 5 , controls :: anyDown ( ) ? pal :: teal : pal :: dim ) ; 114 fb . drawCircle ( kW - 10 , kH - 10 , 6 , pal :: night ) ; 115 } 116 117 118 119 void printHelp ( ) { 120 Serial . println ( ) ; 121 Serial . println ( "Commands: next | prev | scene <1-4> | auto on|off | hud | help" ) ; 122 Serial . println ( controls :: help ( ) ) ; 123 } 124 125 void handleCommand ( String line ) { 126 line . trim ( ) ; 127 line . toLowerCase ( ) ; 128 if ( line . isEmpty ( ) ) return ; 129 if ( line == "next" ) return showScene ( state . scene + 1 ) ; 130 if ( line == "prev" ) return showScene ( state . scene - 1 ) ; 131 if ( line == "hud" ) { 132 state . hud = ! state . hud ; 133 log_i ( "hud %s" , state . hud ? "on" : "off" ) ; 134 return ; 135 } 136 if ( line == "auto on" || line == "auto off" ) { 137 state . autoCycle = line == "auto on" ; 138 log_i ( "auto-cycle %s" , state . autoCycle ? "on" : "off" ) ; 139 return ; 140 } 141 if ( line . startsWith ( "scene " ) ) { 142 const long n = line . substring ( 6 ) . toInt ( ) ; 143 if ( n < 1 || n > kSceneCount ) { 144 log_w ( "scene needs a number 1-%d, got '%s'" , kSceneCount , line . substring ( 6 ) . c_str ( ) ) ; 145 return ; 146 } 147 return showScene ( n - 1 ) ; 148 } 149 if ( line == "help" || line == "?" ) return printHelp ( ) ; 150 log_w ( "unknown command '%s' (type help)" , line . c_str ( ) ) ; 151 } 152 153 void pollSerial ( ) { 154 static String buffer ; 155 while ( Serial . available ( ) ) { 156 const char c = Serial . read ( ) ; 157 if ( c == '\n' || c == '\r' ) { 158 handleCommand ( buffer ) ; 159 buffer = "" ; 160 } else if ( buffer . length ( ) < 32 ) { 161 buffer += c ; 162 } 163 } 164 } 165 166 void pollControls ( ) { 167 switch ( controls :: poll ( ) ) { 168 case Action :: NextScene : 169 showScene ( state . scene + 1 ) ; 170 break ; 171 case Action :: PreviousScene : 172 showScene ( state . scene - 1 ) ; 173 break ; 174 case Action :: ToggleAutoCycle : 175 state . autoCycle = ! state . autoCycle ; 176 log_i ( "auto-cycle %s" , state . autoCycle ? "on" : "off" ) ; 177 break ; 178 case Action :: None : 179 break ; 180 } 181 } 182 183 } 184 185 void setup ( ) { 186 Serial . begin ( 115200 ) ; 187 delay ( 1500 ) ; 188 189 pinMode ( PIN_TFT_BL , OUTPUT ) ; 190 digitalWrite ( PIN_TFT_BL , HIGH ) ; 191 SPI . begin ( PIN_TFT_SCLK , - 1 , PIN_TFT_MOSI , PIN_TFT_CS ) ; 192 tft . init ( 170 , 320 ) ; 193 tft . setSPISpeed ( kSpiHz ) ; 194 tft . setRotation ( 1 ) ; 195 tft . fillScreen ( pal :: night ) ; 196 197 const size_t largest = heap_caps_get_largest_free_block ( MALLOC_CAP_INTERNAL ) ; 198 if ( ! fb . begin ( ) ) { 199 log_e ( "No memory for the %u-byte frame buffer, internal or PSRAM. Stopping." , unsigned ( fb . bytesUsed ( ) ) ) ; 200 return ; 201 } 202 log_i ( "Frame buffer: %u KB in %s (largest free internal block was %u KB)" , unsigned ( fb . bytesUsed ( ) / 1024 ) , 203 fb . internal ( ) ? "internal RAM (fast)" : "PSRAM (slower)" , unsigned ( largest / 1024 ) ) ; 204 205 controls :: begin ( ) ; 206 randomSeed ( esp_random ( ) ) ; 207 printHelp ( ) ; 208 showScene ( 0 ) ; 209 } 210 211 void loop ( ) { 212 if ( fb . pixels ( ) == nullptr ) { 213 delay ( 1000 ) ; 214 return ; 215 } 216 pollSerial ( ) ; 217 pollControls ( ) ; 218 219 static uint32_t lastFrame = millis ( ) , lastLog = 0 , lastTemp = 0 ; 220 const uint32_t now = millis ( ) ; 221 const float dt = ( now - lastFrame ) / 1000.0f ; 222 lastFrame = now ; 223 224 if ( state . autoCycle && now - state . sceneStartedAt >= kAutoCycleMs ) showScene ( state . scene + 1 ) ; 225 if ( now - lastTemp >= 1000 ) { 226 lastTemp = now ; 227 stats . tempC = temperatureRead ( ) ; 228 } 229 230 231 const uint32_t t0 = micros ( ) ; 232 scenes [ state . scene ] -> render ( fb , ( now - state . sceneStartedAt ) / 1000.0f , dt ) ; 233 drawBanner ( now - state . sceneStartedAt ) ; 234 drawHud ( ) ; 235 drawButtonDot ( ) ; 236 const uint32_t t1 = micros ( ) ; 237 tft . drawRGBBitmap ( 0 , 0 , fb . pixels ( ) , kW , kH ) ; 238 const uint32_t t2 = micros ( ) ; 239 stats . add ( ( t1 - t0 ) / 1000.0f , ( t2 - t1 ) / 1000.0f , dt ) ; 240 241 if ( now - lastLog >= 5000 ) { 242 lastLog = now ; 243 log_i ( "%-14s %5.1f fps | draw %5.1f ms | send %5.1f ms | %.1f C" , scenes [ state . scene ] -> name ( ) , stats . fps , 244 stats . drawMs , stats . pushMs , stats . tempC ) ; 245 } 246 }