Created
June 19, 2012 15:18
-
-
Save kschiess/2954735 to your computer and use it in GitHub Desktop.
Coroutines are excellent for encoding state machines. See http://eli.thegreenplace.net/2009/08/29/co-routines-as-an-alternative-to-state-machines/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Encoding: UTF-8 | |
require 'fiber' | |
HEADER = 0x61 | |
FOOTER = 0x62 | |
DLE = 0xAB | |
def after_dle(x) | |
x | |
end | |
# This accepts generated frames and prints them. | |
target = Fiber.new do | |
loop do | |
frame = Fiber.yield | |
puts "Frame received: #{frame}." | |
end | |
end | |
target.resume | |
# This decodes frames and transfers control to the return value of the | |
# first yield, which should be a fiber that handles results. | |
decoder = Fiber.new do |target| | |
loop do | |
byte = Fiber.yield | |
frame = '' | |
if byte == HEADER | |
loop do | |
byte = Fiber.yield | |
case byte | |
when FOOTER | |
target.resume(frame) | |
break | |
when DLE | |
byte = Fiber.yield | |
frame += after_dle(byte).to_s(16) | |
else | |
frame += byte.to_s(16) | |
end | |
end | |
end | |
end | |
end | |
decoder.resume(target) | |
[0x70, 0x24, | |
0x61, 0x99, 0xAF, 0xD1, 0x62, | |
0x56, 0x62, | |
0x61, 0xAB, 0xAB, 0x14, 0x62, | |
0x7].each do |byte| | |
decoder.resume(byte) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Extremely elegantly decoupled. Like it a lot.