// Copyright (c) 2019, Andrew Kay // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #pragma once #define ERROR_TX_RECEIVER_ACTIVE -1 #define ERROR_RX_RECEIVER_ACTIVE -5 #define ERROR_RX_TIMEOUT -2 #define ERROR_RX_OVERFLOW -3 class NewCoaxReceiver; class NewCoaxDataBus; class NewCoaxTransmitter { public: NewCoaxTransmitter(NewCoaxDataBus &dataBus, NewCoaxReceiver &receiver) : _dataBus(dataBus), _receiver(receiver) { }; void begin(); int transmit(uint16_t *buffer, size_t bufferCount); private: NewCoaxDataBus &_dataBus; NewCoaxReceiver &_receiver; void reset(); void write(uint16_t word); }; enum NewCoaxReceiverState { Disabled, Idle, Receiving, Received }; class NewCoaxReceiver { public: NewCoaxReceiver(NewCoaxDataBus &dataBus) : _dataBus(dataBus) { }; void begin(); void enable(); void disable(); int receive(uint16_t *buffer, size_t bufferSize, uint16_t timeout); void activeInterrupt(); void dataAvailableInterrupt(); void errorInterrupt(); private: NewCoaxDataBus &_dataBus; volatile NewCoaxReceiverState _state = Disabled; volatile uint16_t _error; volatile uint16_t *_buffer; volatile size_t _bufferSize; volatile int _bufferCount; void reset(); uint16_t read(); }; class NewCoaxDataBus { public: void setMode(uint8_t mode, bool force); uint16_t read(); void write(uint16_t word); private: int _mode = -1; uint16_t encode(uint16_t word); uint16_t decode(uint16_t word); };