Stairs/module/src/lib/TinyRS485.h

69 lines
1.7 KiB
C

/*
* Stairs lighting
* Copyright 2017 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/Stairs
*
* Straight up port of Nick Gammon's non-blocking
* RS485 library to C and removing the Arduino
* framework dependency.
*
* Source:
* https://www.gammon.com.au/forum/?id=11428
* Devised and written by Nick Gammon.
* Date: 4 December 2012
* Version: 1.0
*/
#ifndef __TinyRS485
#define __TinyRS485
#include <stdint.h>
#include <stdbool.h>
typedef void (*WriteCallback) (uint8_t what); // send a byte to serial port
typedef uint8_t (*AvailableCallback) (void); // return number of bytes available
typedef uint8_t (*ReadCallback) (void); // read a byte from serial port
enum {
STX = '\2', // start of text
ETX = '\3' // end of text
}; // end of enum
// allocate the requested buffer size
void rs485_begin(ReadCallback readCallback,
AvailableCallback availableCallback,
WriteCallback writeCallback,
uint8_t bufferSize);
// get rid of the buffer
void rs485_stop(void);
// handle incoming data, return true if packet ready
bool rs485_update(void);
// reset to no incoming data (eg. after a timeout)
void rs485_reset(void);
// send data
void rs485_sendMsg(uint8_t* data, uint8_t length);
// returns true if packet available
bool rs485_available(void);
// once available, returns the address of the current message
uint8_t* rs485_getData(void);
uint8_t rs485_getLength(void);
// return how many errors we have had
uint16_t rs485_getErrorCount(void);
// return when last packet started
uint16_t rs485_getPacketStartTime(void);
// return true if a packet has started to be received
bool rs485_isPacketStarted(void);
#endif