You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
atmega-geiger/lib/porklib/stream.h

107 lines
2.3 KiB

2 years ago
#pragma once
//
// Streams -- in this library -- are instances of type STREAM.
//
// A stream can be used for receiving and sending bytes, generally
// it's a pipe to a device.
//
// They are designed for printing numbers and strings, but can
// also be used for general data transfer.
//
// Examples of streams:
// "uart.h" -> declares global variable "uart" which is a pointer to the UART stream
// "lcd.h" -> declares a global variable "lcd" (pointer to LCD scho stream)
//
// Streams help avoid code duplication, since the same functions can be used
// to format and print data to different device types.
//
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <avr/pgmspace.h>
/** Stream structure */
typedef struct
{
void (*tx)(uint8_t b);
uint8_t (*rx)(void);
} STREAM;
/** Send bytes to stream */
void put_bytes(const STREAM *p, const uint8_t* str, uint16_t len);
/** Print string into a stream */
void put_str(const STREAM *p, const char *str);
/** Print a programspace string into a stream */
void put_str_P(const STREAM *p, const char* str);
/** Put a char/byte. Basically the same as p->tx() */
void put_c(const STREAM *p, uint8_t c);
/** Send signed int8 */
void put_u8(const STREAM *p, uint8_t num);
/** Send unsigned int8 */
void put_i8(const STREAM *p, int8_t num);
/** Send unsigned int */
void put_u16(const STREAM *p, uint16_t num);
/** Send signed int */
void put_i16(const STREAM *p, int16_t num);
/** Send unsigned long */
void put_u32(const STREAM *p, uint32_t num);
/** Send signed long */
void put_i32(const STREAM *p, int32_t num);
/** Send unsigned int8 */
void put_x8(const STREAM *p, uint8_t num);
/** Send int as hex */
void put_x16(const STREAM *p, uint16_t num);
/** Send long as hex */
void put_x32(const STREAM *p, uint32_t num);
/** Send long long as hex */
void put_x64(const STREAM *p, uint64_t num);
// float variant doesn't make sense for 8-bit int
/** Send unsigned int as float */
void put_u16f(const STREAM *p, uint16_t num, uint8_t places);
/** Send signed int as float */
void put_i16f(const STREAM *p, int16_t num, uint8_t places);
/** Send unsigned long as float */
void put_u32f(const STREAM *p, uint32_t num, uint8_t places);
/** Send signed long as float */
void put_i32f(const STREAM *p, int32_t num, uint8_t places);
/** Print CR LF */
void put_nl(const STREAM *p);