Skip to content

Commit

Permalink
create new function puts_rgb8
Browse files Browse the repository at this point in the history
move all color related code into color.*
in preparation for complete color optimization
  • Loading branch information
peterzuger committed Feb 1, 2021
1 parent 1a2c4d3 commit e87e949
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
13 changes: 13 additions & 0 deletions tlc5947/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*/
static int islower(int c){return ((c>='a')&&(c<='z'));}
static int toupper(int c){return islower(c)?(c-32):c;}
static char get_hex(uint8_t i){return "0123456789ABCDEF"[i];}

static uint8_t get_byte(const char* s){
uint8_t i;
Expand All @@ -55,6 +56,10 @@ static uint8_t get_byte(const char* s){
return i;
}

static void put_byte(char* s, uint8_t b){
s[0] = get_hex((b & 0xF0) >> 4);
s[1] = get_hex((b & 0x0F) );
}

rgb12 get_rgb12(const char* s){
return rgb8torgb12(get_rgb8(s));
Expand All @@ -69,6 +74,14 @@ rgb8 get_rgb8(const char* s){
return c;
}

const char* put_rgb8(char* s, rgb8 c){
s[0] = '#';
put_byte(s + 1, c.r);
put_byte(s + 3, c.g);
put_byte(s + 5, c.b);
s[7] = 0;
return s + 7;
}

rgb8 rgb12torgb8(rgb12 c){
rgb8 _c;
Expand Down
8 changes: 8 additions & 0 deletions tlc5947/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ extern "C"{
rgb12 get_rgb12(const char* s);
rgb8 get_rgb8(const char* s);

/**
* writes a string in the format to s:
* "#RRGGBB\0"
* appends the terminating 0 character.
* returns s + 7
*/
const char* put_rgb8(char* s, rgb8 c);

rgb8 rgb12torgb8(rgb12 c)__attribute__ ((const));

rgb12 rgb8torgb12(rgb8 c)__attribute__ ((const));
Expand Down
14 changes: 3 additions & 11 deletions tlc5947/tlc5947.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,6 @@ static bool check_balanced_jumps(const char* s){

static inline int isdigit(int c){return ((c>='0')&&(c<='9'));}
static inline int isxdigit(int c){return (isdigit(c) || ((c>='A')&&(c<='F')) || ((c>='a')&&(c<='f')));}
static inline __attribute__((pure)) char gethex(uint8_t i){
return "0123456789ABCDEF"[i];
}

/**
* This function checks that all colors used
Expand Down Expand Up @@ -1174,14 +1171,9 @@ STATIC mp_obj_t tlc5947_tlc5947_get(mp_obj_t self_in, mp_obj_t led_in){
rgb8 c = rgb12torgb8(get_buffer(self->buffer, led));

char* str = m_malloc(8);
str[0] = '#';
str[1] = gethex((c.r&0xF0)>>4);
str[2] = gethex( c.r&0x0F);
str[3] = gethex((c.g&0xF0)>>4);
str[4] = gethex( c.g&0x0F);
str[5] = gethex((c.b&0xF0)>>4);
str[6] = gethex( c.b&0x0F);
str[7] = 0;

put_rgb8(str, c);

return mp_obj_new_str(str, 7);
}

Expand Down

0 comments on commit e87e949

Please sign in to comment.