projects:blinker:work_logs:7_code
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
projects:blinker:work_logs:7_code [2022/02/28 12:37] – tjhowse | projects:blinker:work_logs:7_code [2022/03/03 07:17] (current) – tjhowse | ||
---|---|---|---|
Line 8: | Line 8: | ||
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. | 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 am 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, | + | 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, |
===== The Results ===== | ===== The Results ===== | ||
Line 16: | Line 16: | ||
I wrote a [[https:// | I wrote a [[https:// | ||
- | The input `sos nbn` translates | + | First we take an input message, like **sos** and translate it to Morse code: |
- | < | + | < |
- | uint8_t const sequence[] = {0x15, 0x77, 0x47, 0x05, 0x5c, 0x5c, 0xc5, 0x05}; | + | S O S |
+ | |||
+ | dit dit dit dah dah dah dit dit dit | ||
</ | </ | ||
- | If we write that out in binary in one solid blob (reversing | + | 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: |
< | < | ||
- | 1010100011101110111000101010000000111010001110101010001110100000 | + | 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| | ||
</ | </ | ||
- | This is the sequence of on and off states for the LED. We can split it up like this: | + | Then we flip the bit order and store that as hexadecimal in a C byte array: |
- | < | + | < |
- | | + | uint8_t const sequence[] = {0x15, 0x77, 0x47, 0x05, 0x00}; |
- | |dit| |dit| |dit| |dah| |dah| |dah| |dit| |dit| |dit| | + | |
</ | </ | ||
+ | ==== Display ==== | ||
+ | The display code is very simple: | ||
+ | <code c> | ||
+ | void blink() { | ||
+ | if (sequence[counter>> | ||
+ | LED_ON; | ||
+ | } else { | ||
+ | LED_OFF; | ||
+ | } | ||
+ | counter = (counter + 1)%(sizeof(sequence)*8); | ||
+ | } | ||
+ | </ | ||
+ | There is some bit-shifty nonsense going on to pick the correct bit out of the sequence. Then we either turn the LED on or off based on the value of the current bit of interest. Then we increment the counter and wrap it back to zero if it went past the end of the sequence. | ||
+ | ==== Power saving ==== | ||
+ | The Attiny10 has some options for reducing its power consumption. We can shut down the analogue to digital converter (ADC) since we're not reading any analogue values, and we can stop TIMER0 as we won't be using it. | ||
+ | <code c> | ||
+ | // Disable ADC: | ||
+ | ADCSRA = 0; | ||
+ | // Shut down ADC and timer0 | ||
+ | PRR = 0b11; | ||
+ | </ | ||
+ | The biggest power saving came from putting the microcontroller to sleep between " | ||
+ | <code c> | ||
+ | void sleep() { | ||
+ | // Tell the watchdog timer to wake us up in 120ms. | ||
+ | wdt_enable(WDTO_120MS); | ||
+ | WDTCSR |= (1<< | ||
+ | // Go to sleep. | ||
+ | set_sleep_mode(SLEEP_MODE_PWR_DOWN); | ||
+ | sleep_enable(); | ||
+ | sleep_cpu(); | ||
+ | } | ||
+ | // This fires when the watchdog timer expires and wakes up the CPU. | ||
+ | ISR(WDT_vect) { | ||
+ | sleep_disable(); | ||
+ | } | ||
+ | </ | ||
+ | This code is called after the **blink()** function above. It recruits the watchdog timer to fire an interrupt event in 120ms, then goes to sleep. This stops everything in the CPU except the watchdog timer. The contents of RAM are conserved. When the watchdog timer goes off it calls the interrupt service routine (ISR) which disables sleep mode. Then **blink()** fires again and we go back to sleep. | ||
- | ===== Video ===== | + | This approach reduced power consumption by the CPU and LED [[projects: |
- | {{ youtube> | + | ==== Testing ==== |
- | ===== Next Time ===== | + | I don't (yet) know how to read Morse code, but I wanted to validate whether I was blinking out the message correctly, and looping around at the end. I wrote some [[https:// |
+ | |||
+ | {{: | ||
+ | |||
+ | It worked really well! I fed some [[https:// | ||
+ | |||
+ | < | ||
+ | lorem ipsum dolor sit amet consectetur adipiscing elit donec faucibus | ||
+ | quis iaculis volutpat | ||
+ | est fusce elementum vitae risus non aliquam | ||
+ | scelerisque orci egestas | ||
+ | pellentesque lobortis | ||
+ | sagittis congue massa quis tincidunt felis nunc et orci eget mi mollis viverra | ||
+ | non in nunc nullam dapibus pulvinar lectus | ||
+ | tellus id enim posuere sagittis | ||
+ | qwerty asdfg zxcvb | ||
+ | </ | ||
+ | |||
+ | Switching from a Logitech C922 Pro to a PS3 eye let me reduce the dit interval to the final value of 125ms and still decode it perfectly. Fun! | ||
+ | |||
+ | ===== Video ===== | ||
- | I don't think there' | + | {{ youtube> |
[<6>] | [<6>] | ||
projects/blinker/work_logs/7_code.1646051829.txt.gz · Last modified: 2022/02/28 12:37 by tjhowse