From 8b0354b709b917dc25ecd9ae445eb2a70efeadd9 Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Wed, 23 Apr 2025 02:05:37 +0200 Subject: [PATCH] remove itoa hack --- device/serialstream.cc | 59 ++++++++++---------------------------------------- main.cc | 25 +++++++++++---------- 2 files changed, 24 insertions(+), 60 deletions(-) diff --git a/device/serialstream.cc b/device/serialstream.cc index 6d3f7e9..80179a0 100644 --- a/device/serialstream.cc +++ b/device/serialstream.cc @@ -8,55 +8,15 @@ SerialStream::SerialStream(ComPort port, BaudRate baud_rate, DataBits data_bits, SerialStream kout = SerialStream(); void SerialStream::flush() { - print(buffer, pos); - pos = 0; -} - -// https://stackoverflow.com/questions/3440726/what-is-the-proper-way-of-implementing-a-good-itoa-function -// Yet, another good itoa implementation -// returns: the length of the number string -int itoa(int value, char *sp, int radix) -{ - char tmp[16];// be careful with the length of the buffer - char *tp = tmp; - int i; - unsigned v; - - int sign = (radix == 10 && value < 0); - if (sign) - v = -value; - else - v = (unsigned)value; - - while (v || tp == tmp) - { - i = v % radix; - v /= radix; - if (i < 10) - *tp++ = i+'0'; - else - *tp++ = i + 'a' - 10; - } - - int len = tp - tmp; - - if (sign) - { - *sp++ = '-'; - len++; - } - - while (tp > tmp) - *sp++ = *--tp; - - return len; + print(kout.buffer, kout.pos); + kout.pos = 0; } void SerialStream::setForeground(Color c) { write(0x1b); write('['); - write('3'); - write(c + 0x30); + kout << 30+c; + flush(); write('m'); } @@ -82,10 +42,13 @@ void SerialStream::reset() { void SerialStream::setPos(int x, int y) { //char out[] = {0x1b, '[', 0, 0, ';', 0, 0, 'H', 0}; - *this << 0x1b; - *this << '['; - *this << dec << x; - *this << ';' << y << 'H' << endl; + //*this << 0x1b; + write(0x1b); + kout << '['; + kout << dec << x; + kout << ';'; + kout << dec << y; + kout << 'H'; flush(); //itoa(x, &out[2], 10); //itoa(y, &out[5], 10); diff --git a/main.cc b/main.cc index 37e36d6..435920d 100644 --- a/main.cc +++ b/main.cc @@ -38,18 +38,19 @@ extern "C" int main() { //Serial s = Serial(); //s.write('a'); - //// test SerialStream - //SerialStream ss = SerialStream(); - //ss.print("test", 4); - //ss.setAttribute(SerialStream::UNDERSCORE); - //ss.print("test", 4); - //ss.setAttribute(SerialStream::RESET); - //ss.setForeground(SerialStream::MAGENTA); - //ss.print("test", 4); - //ss.setBackground(SerialStream::CYAN); - //ss.print("test", 4); - //ss.setPos(10, 10); - //ss.print("test", 4); + // test SerialStream + SerialStream ss = SerialStream(); + ss.print("test", 4); + ss.setAttribute(SerialStream::UNDERSCORE); + ss.print("test", 4); + ss.setAttribute(SerialStream::RESET); + ss.setForeground(SerialStream::MAGENTA); + ss.print("test", 4); + ss.setBackground(SerialStream::CYAN); + ss.print("test", 4); + ss.setPos(10, 10); + ss.print("test\n", 5); + ss.setBackground(SerialStream::BLACK); kout << "Test -> " << endl; kout << "bool: " << true << " -> true" << endl;