//* value to LED bar converter ideas and their test program *

#include <stdint.h> #include <stdio.h> uint8_t LED_bar_linear1(uint8_t val) { uint8_t bar=0; if (0xC0<val) bar = 0b00011111; else if (0xA0<val) bar = 0b00001111; else if (0x80<val) bar = 0b00000111; else if (0x40<val) bar = 0b00000011; else if (0x20<val) bar = 0b00000001; // else bar=0; // but it is already preinitialized // Comment: but that would be quite laborious and large in code size to expand to longer LED bars // Note: needs to store return uint16_t data for 9-16 LED bars and possibly later on divide between two IO ports return(bar); } uint8_t LED_bar_linear2(uint8_t val) { static const uint8_t levels[]={ 0xC0, 0xA0, 0x80, 0x40, 0x20 }; // in 256./5. increments static const uint8_t values[]={ 0x1F, 0x0F, 0x07, 0x03, 0x01 }; uint8_t bar=0; for (uint8_t i=0; i<8; ++i) { if (val>levels[i]) { bar= values[i]; break; } } return(bar); } uint8_t LED_bar_logarithmic(uint8_t val) { uint8_t bar=0; while (val!=0) { bar = bar << 1; bar = bar | 0x01; val = val >> 2; } return(bar); } void setLEDS(uint8_t byte) { for (uint8_t i=5; i>0; --i) { if ( 0 != (byte & 0x10) ) printf("x"); else printf("."); byte = byte << 1; } printf("\n"); } // Testing the functions on a computer int main() { printf("-----\n"); for (unsigned int i=0; i<256; ++i) setLEDS( LED_bar_linear1( (uint8_t)i ) ); printf("-----\n"); for (unsigned int i=0; i<256; ++i) setLEDS( LED_bar_linear1( (uint8_t)i ) ); printf("-----\n"); for (unsigned int i=0; i<256; ++i) setLEDS( LED_bar_logarithmic( (uint8_t)i ) ); printf("-----\n"); return(0); }