From 1499b997c70af17cf163ecf085727163f64aa039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=B6mer?= Date: Wed, 6 Sep 2023 21:11:35 +0200 Subject: [PATCH] use binary instead of linear search (#30) use binary instead of linear search using algorithm from https://en.wikipedia.org/wiki/Binary_search_algorithm --- thermidity-avr/bitmaps.c | 6 +----- thermidity-avr/bitmaps.h | 7 ++----- thermidity-avr/dejavu.c | 3 +-- thermidity-avr/dejavu.h | 7 ++----- thermidity-avr/display.c | 15 +++++++-------- thermidity-avr/font.c | 21 ++++++++++++++++----- thermidity-avr/font.h | 2 +- thermidity-avr/meter.c | 4 ++-- thermidity-avr/nbproject/configurations.xml | 1 + thermidity-avr/thermidity.c | 1 - thermidity-avr/unifont.c | 2 +- thermidity-avr/unifont.h | 7 ++----- 12 files changed, 36 insertions(+), 40 deletions(-) diff --git a/thermidity-avr/bitmaps.c b/thermidity-avr/bitmaps.c index 8ef52dd..788058a 100644 --- a/thermidity-avr/bitmaps.c +++ b/thermidity-avr/bitmaps.c @@ -108,7 +108,7 @@ const __flash uint8_t BAT_100PCT_DATA[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -const __flash Bitmap AllBitmaps[] = { +const __flash Bitmap bitmaps[] = { {32, 16, BAT_0PCT_DATA}, {32, 16, BAT_13PCT_DATA}, {32, 16, BAT_25PCT_DATA}, @@ -119,7 +119,3 @@ const __flash Bitmap AllBitmaps[] = { {32, 16, BAT_88PCT_DATA}, {32, 16, BAT_100PCT_DATA} }; - -/*const __flash Bitmap* getBitmapAddress (uint8_t index) { - return & AllBitmaps[index]; - }*/ diff --git a/thermidity-avr/bitmaps.h b/thermidity-avr/bitmaps.h index aea5d73..7136d79 100644 --- a/thermidity-avr/bitmaps.h +++ b/thermidity-avr/bitmaps.h @@ -33,12 +33,9 @@ typedef struct { } Bitmap; /** - * Returns the flash adress bitmap at the given index. - * @param index - * @return Bitmap + * Available bitmaps. */ - -extern const __flash Bitmap AllBitmaps[]; +extern const __flash Bitmap bitmaps[]; #endif /* BITMAPS_H */ diff --git a/thermidity-avr/dejavu.c b/thermidity-avr/dejavu.c index 7596b55..f05c636 100644 --- a/thermidity-avr/dejavu.c +++ b/thermidity-avr/dejavu.c @@ -7,7 +7,6 @@ */ #include -#include #include "font.h" #include "dejavu.h" #include "utils.h" @@ -794,4 +793,4 @@ const __flash Glyph glyphs[] = { {0x00b0, 16, DEGREE_SIGN} }; -const __flash Font theDejaVu = { glyphs, ARRAY_LENGTH(glyphs), HEIGHT }; +const __flash Font dejaVuFont = {glyphs, ARRAY_LENGTH(glyphs), HEIGHT}; diff --git a/thermidity-avr/dejavu.h b/thermidity-avr/dejavu.h index e061fbe..a4b45c4 100644 --- a/thermidity-avr/dejavu.h +++ b/thermidity-avr/dejavu.h @@ -12,12 +12,9 @@ #include "font.h" /** - * Returns an instance of the font derived from DejaVu. - * @return unifont + * DejaVu font. */ -const Font getDejaVu(void); - -extern const __flash Font theDejaVu; +extern const __flash Font dejaVuFont; #endif /* DEJAVU_H */ diff --git a/thermidity-avr/display.c b/thermidity-avr/display.c index 5055a51..580c69e 100644 --- a/thermidity-avr/display.c +++ b/thermidity-avr/display.c @@ -48,9 +48,9 @@ static void bufferByte(uint16_t index, uint16_t *address, uint16_t height, * @param width * @param height */ -static void bufferBitmap (uint8_t row, uint16_t col, - const __flash uint8_t *bitmap, - uint16_t width, uint16_t height) { +static void bufferBitmap(uint8_t row, uint16_t col, + const __flash uint8_t *bitmap, + uint16_t width, uint16_t height) { uint16_t size = width * height / 8; uint16_t origin = DISPLAY_WIDTH * DISPLAY_H_BYTES + row - col * DISPLAY_H_BYTES; @@ -115,16 +115,15 @@ void setFrame(uint8_t byte) { } uint8_t writeBitmap(uint16_t row, uint16_t col, uint16_t index) { - const __flash Bitmap *bitmap = & AllBitmaps[index]; + const __flash Bitmap *bitmap = & bitmaps[index]; bufferBitmap (row, col, bitmap->bitmap, bitmap->width, bitmap->height); return bitmap->width; } -uint8_t writeGlyph(uint16_t row, uint16_t col, const __flash Font *font, uint16_t code) -{ - const __flash Glyph *glyph = getGlyphAddress (font, code); - bufferBitmap (row, col, glyph->bitmap, glyph->width, font->height); +uint8_t writeGlyph(uint16_t row, uint16_t col, const __flash Font *font, uint16_t code) { + const __flash Glyph *glyph = getGlyphAddress(font, code); + bufferBitmap(row, col, glyph->bitmap, glyph->width, font->height); return glyph->width; } diff --git a/thermidity-avr/font.c b/thermidity-avr/font.c index 69407c5..70cc819 100644 --- a/thermidity-avr/font.c +++ b/thermidity-avr/font.c @@ -8,14 +8,25 @@ #include #include "font.h" -const __flash Glyph* getGlyphAddress (const __flash Font *font, uint16_t code) { - const __flash Glyph *pglyph = & font->glyphs[0]; - for (size_t i = 0; i < font->length; i++, pglyph++) { - if (pglyph->code == code) { +const __flash Glyph* getGlyphAddress(const __flash Font *font, uint16_t code) { + + // https://en.wikipedia.org/wiki/Binary_search_algorithm + int16_t l = 0; + int16_t r = font->length - 1; + + while (l <= r) { + uint8_t m = (l + r) / 2; + const __flash Glyph *pglyph = &font->glyphs[m]; + if (pglyph->code < code) { + l = m + 1; + } else if (pglyph->code > code) { + r = m - 1; + } else { + // found code point, return address of glyph return pglyph; } } // return question mark if unknown code point - return getGlyphAddress (font, 0x003f); + return getGlyphAddress(font, 0x003f); } diff --git a/thermidity-avr/font.h b/thermidity-avr/font.h index 38ff92c..e10d13a 100644 --- a/thermidity-avr/font.h +++ b/thermidity-avr/font.h @@ -41,6 +41,6 @@ typedef struct { * @param code * @return Glyph */ -const __flash Glyph* getGlyphAddress (const __flash Font *font, uint16_t code); +const __flash Glyph* getGlyphAddress(const __flash Font *font, uint16_t code); #endif /* FONT_H */ diff --git a/thermidity-avr/meter.c b/thermidity-avr/meter.c index d3a5933..80a7ba1 100644 --- a/thermidity-avr/meter.c +++ b/thermidity-avr/meter.c @@ -160,8 +160,8 @@ bool displayValues(bool fast) { prevRh = rh; prevVBatx10 = vBatx10; - const __flash Font *unifont = & theUnifont; - const __flash Font *dejavu = & theDejaVu; + const __flash Font *unifont = &unifontFont; + const __flash Font *dejavu = &dejaVuFont; // clear frame setFrame(0x00); diff --git a/thermidity-avr/nbproject/configurations.xml b/thermidity-avr/nbproject/configurations.xml index 3b40972..d8c47b5 100644 --- a/thermidity-avr/nbproject/configurations.xml +++ b/thermidity-avr/nbproject/configurations.xml @@ -47,6 +47,7 @@ ${MAKE} -f Makefile clean /home/dode/dev/thermidity/thermidity-avr/thermidity.elf + 3 . diff --git a/thermidity-avr/thermidity.c b/thermidity-avr/thermidity.c index 0c75c77..eef5629 100644 --- a/thermidity-avr/thermidity.c +++ b/thermidity-avr/thermidity.c @@ -20,7 +20,6 @@ #include #include #include -#include #include #include diff --git a/thermidity-avr/unifont.c b/thermidity-avr/unifont.c index e2b5f7b..3026ec6 100644 --- a/thermidity-avr/unifont.c +++ b/thermidity-avr/unifont.c @@ -970,4 +970,4 @@ static const __flash Glyph glyphs[] = { {0x00ff, WIDTH, y_diaeresis} }; -const __flash Font theUnifont = { glyphs, ARRAY_LENGTH(glyphs), HEIGHT }; +const __flash Font unifontFont = {glyphs, ARRAY_LENGTH(glyphs), HEIGHT}; diff --git a/thermidity-avr/unifont.h b/thermidity-avr/unifont.h index bdb6839..aa1c37e 100644 --- a/thermidity-avr/unifont.h +++ b/thermidity-avr/unifont.h @@ -14,12 +14,9 @@ #define UNIFONT_DEMO_SIZE 7 /** - * Returns an instance of the Unifont font. - * @return unifont + * Unifont font. */ -const Font getUnifont(void); - -extern const __flash Font theUnifont; +extern const __flash Font unifontFont; #endif /* UNIFONT_H */