User Tools

Site Tools


projects:blinker:work_logs:7_code

This is an old revision of the document!


Code

The Plan

The message and the code to display the message on the LED must all live within the same 1024 bytes. I must strike a balance between efficient storage of the message, and efficient code to unpack and display that message. Cleverer code would let me crunch the message into less storage, but the code itself would probably take up more space to store the cleverness.

I aim to hit a middle point of moderate cleverness and medium message storage density. I think a decent approach is to translate the text to Morse code outside the microcontroller, to keep the display code as small and simple as possible.

The Results

Storage

I wrote a python script that converts a provided string into a series of hex bytes in a header file that the microcontroller code uses to store the sequence.

First we take an input message, like sos and translate it to Morse code:

     S            O            S
     
dit dit dit  dah dah dah  dit dit dit

According to the Morse code spec a dit is the shortest time window. The gap between tones (or keys, or flashes) is the same length as a dit. A dah is three times that length. The gap between letters is the same length as a dah. The gap between words is seven times the length of a dit. Taking that all into account we can map a 1 or a 0 to whether the LED should be on at a given point in time:

  1  0  1  0  1  000 111 0 111 0 111 000  1  0  1  0  1  0000000
|dit| |dit| |dit|   |dah| |dah| |dah|   |dit| |dit| |dit|

Then we flip the bit order and store that as hexadecimal in a C byte array:

uint8_t const sequence[] = {0x15, 0x77, 0x47, 0x05, 0x5c, 0x5c, 0xc5, 0x05};

Display

The display code is very simple:

void blink() {
  if (sequence[counter>>3] & (0x01 << counter%8)) {
    LED_ON;
  } else {
    LED_OFF;
  }
  counter = (counter + 1)%(sizeof(sequence)*8);
}

Video

Next Time

I don't think there's much point in trying to further improve energy efficiency of the code. The next steps might be adding features like multiple message sequences, and/or improving the space efficiency to fit more/longer messages.

projects/blinker/work_logs/7_code.1646052314.txt.gz · Last modified: 2022/02/28 12:45 by tjhowse