Time for some math, please check it :)
Each prescaler tick is 0.08333us.
The 1:256 prescaler takes 256 * 0.0833us to increment 1 on the counter (21.3333us per tick)
The 16bit timer0 can count to 65535, so the maximum interval is 1,398,077.8155us
Protocol
The protocol is very simple right now, it might need to be updated in the future.
pulse1-high8 | pulse1-low8 | blank1-high8 | blank1-low8 | ... | blankn-high8 | blankn-low8 |
The first IR pulse starts the counter, it measures the number of 21.3333us periods before the next blank. The IR Toy continues to spit out measurements until there is a full (0xffff) blank period of 1.7seconds. After a full blank period the IR Toy 'sleeps' and won't send any more data until the next IR pulse.
To get the actual width of each pulse or space:
Combine the high and low byte
Multiply by 21.3333us
Here's an example:
{00}{2B}{00}{28}{00}{2A}{00}{27}{00}{2B}{00}{28}{00}{2A}{00}{27}{00}{2B}{00}{27}{00}{2A}{00}{28}{00}{54}{00}{51}{00}{2B}{00}{28}{00}{54}{00}{51}{00}{54}{00}{51}{00}{2A}{FF}{FF}{00}{0A}{FF}{FF}{00}{04}{FF}{FF}
002b (first pulse duration) = 43 = 43 * 21.3333us = 917.3319us
0028 (first blank duration) = 40 = 40 * 21.3333us = 853.332us
002a (second pulse duration) = 42 = 42 * 21.3333us = 895.9986us
Those are all pretty close to the 889us bit period of an RC5 remote.
http://www.sbprojects.com/knowledge/ir/rc5.htmTo enter IR sample mode:
First send 0x00 (raw byte value 0) to reset from any other mode. It might be good to send it 5 times to be sure it's out of SUMP mode too.
Next, send 's' or 'S' to enter IR sample mode. The IR Toy will respond with the protocol version (currently S01).
Now the bytes start flowing.
Potential issues:
1. Which byte is which, tracking the byte stream. There's no easy way to sync to the byte stream unless you track it from the start or wait for the first 0xff 0xff terminator. That's probably not a huge deal in practice, just reset (0x00, 'S') and follow it from the beginning. An alternative would be to use the first bit to flag pulse or blank periods, but there's still the issue of determining which bytes in the stream are the high and low 8bits.
2. IR Toy timeout - The IR Toy will send the 0xff 0xff terminator flag after no pin change is detected for one timer period. It doesn't care if this is a pulse or blank period, and it has no ability to let us know a pulse is longer than 1.7seconds. A continuous IR pulse, like an IR Jammer, would really mess up the bytestream. Update: we made the IR Toy a little more robust against this situation by confirming that a pulse at the start of sampling.
Taking it further
This is just an initial test release. We need to verify the timing against a logic analyzer, and possibly hand-tune the counter and interrupt routines.