anchor.diff:  -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
   -export([start_link/1, stop/0, digital_read/1, analog_read/1, pin_mode/2, digital_write/2, analog_write/2, subscribe/3, unsubscribe/3]).
   -define(SPEED, 57600).
  --define(ACC, 20).
   -define(READ_LENGTH, 1).
   -define(READ_TIMEOUT, 10).
   -type dict(Key, Val) :: [{Key, Val}].
  
positive.code:     io:format(standard_error, "Unknown CALL message ~p~n", [Request]),
      {reply, Reply, State}.
  
  handle_cast({mode, Pin, Mode}, State) ->
      rs232:write(<<244:8/integer, Pin:8/integer, Mode:8/integer>>),
      {noreply, State};
  
negative.code: 
  % Private API
  
  read_loop(WaitForData, ExecuteMultiByteCommand, MultiByteChannel, Lsb, Msb) ->
      timer:sleep(?READ_TIMEOUT),
      case rs232:read(?READ_LENGTH, ?READ_TIMEOUT) of
          % Everything is fine
          {Error, [Byte]} when Error == 0 ->
              {NewWaitForData, NewExecuteMultiByteCommand, NewMultiByteChannel, NewLsb, NewMsb} = parse_byte(Byte, WaitForData, ExecuteMultiByteCommand, MultiByteChannel, Lsb, Msb),
              read_loop(NewWaitForData, NewExecuteMultiByteCommand, NewMultiByteChannel, NewLsb, NewMsb);
           % Timeout: we shouldn't worry too much
          {Error, _} when Error == 9 ->
              read_loop(WaitForData, ExecuteMultiByteCommand, MultiByteChannel, Lsb, Msb);
          % Worry
          {Error, Other} ->
              io:format(standard_error, "Error while reading: ~w ~p~n", [Error, Other])
      end.
  
  parse_byte(_Byte, WaitForData, ExecuteMultiByteCommand, MultiByteChannel, Lsb, Msb) when WaitForData == 0 andalso byte < 128 andalso ExecuteMultiByteCommand == 144 ->
      gen_server:cast(?MODULE, {update, digital, MultiByteChannel, (Msb bsl 7) + Lsb}),
      {0, 0, 0, 0, 0};
  parse_byte(_Byte, WaitForData, ExecuteMultiByteCommand, MultiByteChannel, Lsb, Msb) when WaitForData == 0 andalso byte < 128 andalso ExecuteMultiByteCommand == 224 ->
      gen_server:cast(?MODULE, {update, analog, MultiByteChannel, (Msb bsl 7) + Lsb}),
      {0, 0, 0, 0, 0};
  parse_byte(Byte, WaitForData, ExecuteMultiByteCommand, MultiByteChannel, Lsb, _Msb) when WaitForData == 1 andalso byte < 128 ->
      parse_byte(Byte, 0, ExecuteMultiByteCommand, MultiByteChannel, Lsb, Byte);
  parse_byte(Byte, WaitForData, ExecuteMultiByteCommand, MultiByteChannel, _Lsb, _Msb) when WaitForData == 2 andalso byte < 128 ->
      {1, ExecuteMultiByteCommand, MultiByteChannel, Byte, 0};
  parse_byte(Byte, _WaitForData, _ExecuteMultiByteCommand, _MultiByteChannel, _Lsb, _Msb) ->
      {Command, NewMultiByteChannel} = case Byte of
          Byte when Byte < 240 ->
              {Byte band 240, Byte band 15};
          _ ->
              {Byte, 0}
      end,
      {2, Command, NewMultiByteChannel, 0, 0}.
  
  notify(Pin, Value, Subscribers) ->
      sets_foreach(fun(Sub) -> Sub ! {update, Pin, Value} end, Subscribers).
  