Class: TEF::FurComs::Serial

Inherits:
Base
  • Object
show all
Includes:
XasLogger::Mix
Defined in:
lib/tef/furcoms/serial.rb

Overview

Note:

This class provides limited FurComs compatibility. Arbitration handling is not possible, which may cause frequent collisions in busy bus conditions. It is recommended to use a STM32 processor that provides translation between FurComs and the computer.

USB-To-Serial FurComs Bridge.

This class will handle connecting to a FurComs bus using a standard USB-To-UART adapter, such as an FTDI-Chip, in order to send and receive messages.

Direct Known Subclasses

SerialToMQTT

Instance Method Summary collapse

Methods inherited from Base

#on_message

Constructor Details

#initialize(port = '/dev/ttyACM0', baudrate = 115_200) ⇒ Serial

TODO:

Add graceful handling of controller disconnect/reconnect.

Note:

This class can not provide full arbitration handling. This may cause issues in busy bus conditions!

Initialize a Serial bridge class.

This will open the given Serial port and begin reading/writing onto the FurComs bus.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tef/furcoms/serial.rb', line 29

def initialize(port = '/dev/ttyACM0', baudrate = 115_200)
	super();

	@port = SerialPort.new(port);
	@port.baud = baudrate;
	@port.sync = true;

	start_thread();

	init_x_log("FurComs #{port}")
	x_logi('Ready!');
end

Instance Method Details

#send_message(topic, message, priority: 0, chip_id: 0) ⇒ Object

Send a message to the FurComs bus.

This will send a message onto topic, using the given priority and chip_id (defaulting both to 0).

Parameters:

  • topic (String)

    Topic to send the message onto

  • message (String)

    Binary string of data to send, expected to be ASCII-8 encoded, can contain any character (including null)



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tef/furcoms/serial.rb', line 119

def send_message(topic, message, priority: 0, chip_id: 0)
	unless topic =~ /^[\w\s\/]*$/
		raise ArgumentError, 'Topic includes invalid characters!'
	end
	if (topic.length + message.length) > 250
		raise ArgumentError, 'Message packet length exceeded!'
	end

	x_logd("Sending '#{topic}': '#{message}'")

	escaped_str = slip_encode_data "#{topic}\0#{message}"
	out_data = generate_furcom_message priority, chip_id, escaped_str;

	@port.write(out_data.pack('C2S<C*'))

	@port.flush
end