Skip to content

Commit

Permalink
use binary instead of linear search (#30)
Browse files Browse the repository at this point in the history
use binary instead of linear search using algorithm from https://en.wikipedia.org/wiki/Binary_search_algorithm
  • Loading branch information
gitdode authored Sep 6, 2023
1 parent 5583921 commit 1499b99
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 40 deletions.
6 changes: 1 addition & 5 deletions thermidity-avr/bitmaps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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];
}*/
7 changes: 2 additions & 5 deletions thermidity-avr/bitmaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

3 changes: 1 addition & 2 deletions thermidity-avr/dejavu.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

#include <stdio.h>
#include <avr/pgmspace.h>
#include "font.h"
#include "dejavu.h"
#include "utils.h"
Expand Down Expand Up @@ -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};
7 changes: 2 additions & 5 deletions thermidity-avr/dejavu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

15 changes: 7 additions & 8 deletions thermidity-avr/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 16 additions & 5 deletions thermidity-avr/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@
#include <stdio.h>
#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);
}
2 changes: 1 addition & 1 deletion thermidity-avr/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
4 changes: 2 additions & 2 deletions thermidity-avr/meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions thermidity-avr/nbproject/configurations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
<executablePath>/home/dode/dev/thermidity/thermidity-avr/thermidity.elf</executablePath>
<cTool>
<standard>3</standard>
<incDir>
<pElem>.</pElem>
</incDir>
Expand Down
1 change: 0 additions & 1 deletion thermidity-avr/thermidity.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <util/atomic.h>

Expand Down
2 changes: 1 addition & 1 deletion thermidity-avr/unifont.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
7 changes: 2 additions & 5 deletions thermidity-avr/unifont.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 1499b99

Please sign in to comment.