Skip to content

Commit

Permalink
it's working pretty well!
Browse files Browse the repository at this point in the history
added support for selectively deleting images from RAM after drawing them, instead of caching them. This lets us draw the splash screen then delete it, so there is still RAM leftover for bluetooth, which was failing before.
  • Loading branch information
Marcus10110 committed Apr 10, 2024
1 parent 34966c8 commit 9059741
Show file tree
Hide file tree
Showing 27 changed files with 42 additions and 40 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 7 additions & 17 deletions firmware/include/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@

#include <Adafruit_GFX.h>

#include "fonts/digital_7__mono_40pt7b.h"
#include "Fonts/FreeMono9pt7b.h"
#include "Fonts/FreeMono12pt7b.h"

namespace Display
{
constexpr int16_t TopPadding = 5;
constexpr int16_t BottomPadding = 10;
constexpr int16_t LeftPadding = 0;
constexpr int16_t RightPadding = 0;
constexpr int16_t IconPlaceholderWidth = 70;
constexpr uint16_t DefaultTextColor = 0xFFFF;
constexpr uint16_t DefaultBackgroundColor = 0x0000;

constexpr uint8_t TimeFontSize = 1;
constexpr GFXfont const* TimeFont = &digital_7__mono_40pt7b;
constexpr uint8_t NormalFontSize = 1;
constexpr GFXfont const* NormalFont = &FreeMono9pt7b;
constexpr uint8_t SpeedFontSize = 2;
constexpr uint8_t MusicFontSize = 2;
constexpr uint8_t DefaultFontSize = 1;

enum class HorizontalAlignment
{
Expand All @@ -38,20 +28,20 @@ namespace Display

struct Rect
{
int16_t x;
int16_t y;
int16_t w;
int16_t h;
int16_t x{ 0 };
int16_t y{ 0 };
int16_t w{ 0 };
int16_t h{ 0 };
};

class Display : public GFXcanvas16
{
typedef void ( *ImageLoaderFn )( GFXcanvas16* destination, char* path, int16_t x, int16_t y );
typedef void ( *ImageLoaderFn )( GFXcanvas16* destination, char* path, int16_t x, int16_t y, bool delete_after_draw );

public:
Display( ImageLoaderFn image_loader = nullptr );
void clear();
void DrawBMP( char* path, int16_t x, int16_t y );
void DrawBMP( char* path, int16_t x, int16_t y, bool delete_after_draw = false );
void GetTextLocation( const char* text, HorizontalAlignment horizontal, VerticalAlignment vertical, int16_t* left, int16_t* top,
Rect* within_region = nullptr, Rect* text_region = nullptr );
void WriteAligned( const char* text, HorizontalAlignment horizontal, VerticalAlignment vertical, Rect* within_region = nullptr,
Expand Down
6 changes: 3 additions & 3 deletions firmware/include/motion.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Motion
{
struct State
{
float mForward; // X
float mLeft; // Y
float mUp; // Z
float mForward{ 0 }; // X
float mLeft{ 0 }; // Y
float mUp{ 0 }; // Z
bool mValid{ false };
};

Expand Down
2 changes: 1 addition & 1 deletion firmware/include/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Pin
#ifdef REV2
constexpr int8_t GpsRx = 16;
constexpr int8_t GpsTx = 17;
#elif
#else
// (note, these are the default I2C pins)
constexpr int8_t GpsRx = 22;
constexpr int8_t GpsTx = 23;
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/apple_media_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace AppleMediaService
uint8_t entity_id = data[ 0 ];
uint8_t attribute_id = data[ 1 ];
uint8_t flags = data[ 2 ];
std::string value( ( char* )data + 3, length - 3 );
std::string value( reinterpret_cast<char*>( data ) + 3, length - 3 );
log_d( "entity update. id: %i, attribute: %i, flags: %i, value: %s", entity_id, attribute_id, flags, value.c_str() );
switch( entity_id )
{
Expand Down
3 changes: 3 additions & 0 deletions firmware/src/current_time_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace CurrentTimeService
case CurrentTime::DayOfWeek::Sunday:
return "Sunday";
}
return "Unknown";
}

CurrentTime ParseCurrentTime( const uint8_t* data, size_t length )
Expand Down Expand Up @@ -104,7 +105,9 @@ namespace CurrentTimeService
auto data = gCurrentTimeCharacteristic->readValue();
auto time = ParseCurrentTime( reinterpret_cast<const uint8_t*>( data.data() ), data.size() );
*current_time = time;
return true;
}
return false;
}

bool StartTimeService( BLEClient* client, CurrentTime* current_time )
Expand Down
19 changes: 14 additions & 5 deletions firmware/src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#include <SPIFFS_ImageReader.h>
#include <map>
#include <string>

#include "logger.h"
namespace Display
{
namespace
{
// cache the loaded images, because image load time is SLOW (5 sec for splash)
std::map<std::string, SPIFFS_Image> mLoadedImages;

void DefaultImageLoader( GFXcanvas16* destination, char* path, int16_t x, int16_t y )
void DefaultImageLoader( GFXcanvas16* destination, char* path, int16_t x, int16_t y, bool delete_after_draw )
{
void* raw_canvas = nullptr;
if( mLoadedImages.count( path ) == 0 )
Expand All @@ -21,16 +21,19 @@ namespace Display
if( load_result != IMAGE_SUCCESS )
{
// failed to load image
Serial.printf( "Failed to load image %s, code: %u\n", path, load_result );
return;
}
auto format = image.getFormat();
if( format != IMAGE_16 )
{
Serial.printf( "Unsupported image format %u\n", format );
return;
}
raw_canvas = image.getCanvas();
if( raw_canvas == nullptr )
{
Serial.println( "Failed to get canvas from image" );
return;
}
}
Expand All @@ -39,12 +42,18 @@ namespace Display
raw_canvas = mLoadedImages.at( path ).getCanvas();
if( raw_canvas == nullptr )
{
Serial.println( "(CACHE) Failed to get canvas from image" );
return;
}
}

GFXcanvas16* image_canvas = static_cast<GFXcanvas16*>( raw_canvas );
destination->drawRGBBitmap( x, y, image_canvas->getBuffer(), image_canvas->width(), image_canvas->height() );

if( delete_after_draw )
{
mLoadedImages.erase( path );
}
}
}
Display::Display( ImageLoaderFn image_loader )
Expand All @@ -64,13 +73,13 @@ namespace Display
setTextColor( DefaultTextColor );
setTextWrap( true );
setFont( nullptr );
setTextSize( NormalFontSize );
setTextSize( DefaultFontSize );
}

void Display::DrawBMP( char* path, int16_t x, int16_t y )
void Display::DrawBMP( char* path, int16_t x, int16_t y, bool delete_after_draw )
{
assert( mImageLoader != nullptr );
mImageLoader( this, path, x, y );
mImageLoader( this, path, x, y, delete_after_draw );
}

// get text position based on some settings. Uses current font settings, etc.
Expand Down
11 changes: 7 additions & 4 deletions firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ void setup()
Serial.begin( 115200 );
Serial.println( "Del Sol Clock Booting" );

Serial.println( "setup, bluetooth begin..." );
Bluetooth::Begin( BluetoothDeviceName );

// Required before we can handle the lights-only power mode.
CarIO::Setup();
Expand All @@ -64,13 +62,18 @@ void setup()
Motion::Begin();

{
// show splash screen.
// write the splash screen to the display buffer before starting BLE. this uses a huge amount of RAM temporarily.
Screens::Splash splash;
splash.Draw( &gDisplay );
gTft->DrawCanvas( &gDisplay );
delay( 3000 );
}

Serial.println( "setup, bluetooth begin..." );
Bluetooth::Begin( BluetoothDeviceName );
// leave splash screen up for a few seconds.
delay( 3000 );


#ifdef DEMO_MODE
while( 1 )
{
Expand Down
15 changes: 6 additions & 9 deletions firmware/src/screens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@
#include <SPIFFS_ImageReader.h>
#include <string>

#include "Fonts/FreeMono9pt7b.h"
#include "Fonts/FreeMono12pt7b.h"
#include "fonts/JetBrainsMono_Regular8pt7b.h"
#include "fonts/JetBrainsMono_Regular10pt7b.h"
#include "fonts/JetBrainsMono_Regular12pt7b.h"
#include "fonts/digital_7__mono_40pt7b.h"

#include "fonts/JetBrainsMono_Thin6pt7b.h"
#include "fonts/JetBrainsMono_Thin7pt7b.h"
#include "fonts/JetBrainsMono_Thin8pt7b.h"
#include "fonts/JetBrainsMono_Thin9pt7b.h"
#include "fonts/JetBrainsMono_Thin10pt7b.h"
#include "fonts/JetBrainsMono_Thin12pt7b.h"
#include "fonts/JetBrainsMono_Thin14pt7b.h"
#include "fonts/JetBrainsMono_Thin16pt7b.h"
#include "fonts/JetBrainsMono_Thin18pt7b.h"

using namespace Display;

namespace Screens
{
void Splash::Draw( Display::Display* display )
{
display->clear();
display->DrawBMP( "/OldSols.bmp", 0, 0 );
display->DrawBMP( "/OldSols.bmp", 0, 0, true );
}

void Clock::Draw( Display::Display* display )
Expand All @@ -53,7 +48,7 @@ namespace Screens

char display_string[ 128 ] = { 0 };
snprintf( display_string, sizeof( display_string ), "%i:%02i", hours12, mMinutes );
display->setFont( TimeFont );
display->setFont( &digital_7__mono_40pt7b );
display->setTextSize( 1 );
display->setTextWrap( false ); // disable wrap just for this function.
int16_t x, y;
Expand Down Expand Up @@ -325,6 +320,7 @@ namespace Screens

void FontTest::Draw( Display::Display* display )
{
#if 0
display->clear();
display->setFont( nullptr );
int16_t x = 0;
Expand Down Expand Up @@ -365,5 +361,6 @@ namespace Screens
test( "FreeMono 9 0.123", &FreeMono9pt7b, 1 );
y += 10;
test( "FreeMono 12 0.123", &FreeMono12pt7b, 1 );
#endif
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 9059741

Please sign in to comment.