diff --git a/lib/stream.c b/lib/stream.c index 5c0ea6c..8e357ee 100644 --- a/lib/stream.c +++ b/lib/stream.c @@ -3,6 +3,7 @@ #include #include "stream.h" +#include "calc.h" static char tmpstr[20]; // buffer for number rendering @@ -76,6 +77,54 @@ void put_i32(const STREAM *p, const int32_t num) } +/** Print number as hex */ +void _print_hex(const STREAM *p, uint8_t* start, uint8_t bytes) +{ + for (; bytes > 0; bytes--) { + uint8_t b = *(start + bytes - 1); + + for(uint8_t j = 0; j < 2; j++) { + uint8_t x = high_nibble(b); + b = b << 4; + if (x < 0xA) { + p->tx('0' + x); + } else { + p->tx('A' + (x - 0xA)); + } + } + } +} + + +/** Send unsigned int8 */ +void put_x8(const STREAM *p, const uint8_t num) +{ + _print_hex(p, (uint8_t*) &num, 1); +} + + +/** Send int as hex */ +void put_x16(const STREAM *p, const uint16_t num) +{ + _print_hex(p, (uint8_t*) &num, 2); +} + + +/** Send long as hex */ +void put_x32(const STREAM *p, const uint32_t num) +{ + _print_hex(p, (uint8_t*) &num, 4); +} + + +/** Send long long as hex */ +void put_x64(const STREAM *p, const uint64_t num) +{ + _print_hex(p, (uint8_t*) &num, 8); +} + + + // float variant doesn't make sense for 8-bit int /** Send unsigned int as float */ diff --git a/lib/stream.h b/lib/stream.h index 3d9feb6..5c7ca32 100644 --- a/lib/stream.h +++ b/lib/stream.h @@ -1,8 +1,20 @@ #pragma once // -// Stream utilities - printing abstraction -// Works with eg. UART +// 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 @@ -49,6 +61,22 @@ void put_u32(const STREAM *p, const uint32_t num); void put_i32(const STREAM *p, const int32_t num); +/** Send unsigned int8 */ +void put_x8(const STREAM *p, const uint8_t num); + + +/** Send int as hex */ +void put_x16(const STREAM *p, const uint16_t num); + + +/** Send long as hex */ +void put_x32(const STREAM *p, const uint32_t num); + + +/** Send long long as hex */ +void put_x64(const STREAM *p, const uint64_t num); + + // float variant doesn't make sense for 8-bit int /** Send unsigned int as float */