From 5c6cc3e9f5655c5d8949e06f1d1dee204e9f31e8 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Wed, 24 Jul 2019 23:37:34 +0200
Subject: [PATCH] fix(pycardium): Fix stupid string cooker

Signed-off-by: Rahix <rahix@rahix.de>
---
 pycardium/mphalport.c | 32 +++++++++++++++++++++++++++++++-
 pycardium/mphalport.h |  6 ------
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/pycardium/mphalport.c b/pycardium/mphalport.c
index ee9e2f44f..bc029664b 100644
--- a/pycardium/mphalport.c
+++ b/pycardium/mphalport.c
@@ -1,6 +1,7 @@
 #include <stdint.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "py/lexer.h"
 #include "py/mpconfig.h"
@@ -27,12 +28,36 @@ int mp_hal_stdin_rx_chr(void)
 	return (int)epic_uart_read_chr();
 }
 
-/* Send string of given length */
+/* Send a string */
 void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len)
 {
 	epic_uart_write_str(str, len);
 }
 
+/* Send a string, but replace \n with \n\r */
+void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len)
+{
+	/*
+	 * Only print one line at a time.  Insert `\r` between lines so
+	 * they are properly displayed on the serial console.
+	 */
+	size_t i, last = 0;
+	for (i = 0; i < len; i++) {
+		if (str[i] == '\n') {
+			epic_uart_write_str(&str[last], i - last);
+			epic_uart_write_str("\r", 1);
+			last = i;
+		}
+	}
+	epic_uart_write_str(&str[last], len - last);
+}
+
+/* Send a zero-terminated string */
+void mp_hal_stdout_tx_str(const char *str)
+{
+	mp_hal_stdout_tx_strn(str, strlen(str));
+}
+
 /* Used by MicroPython for debug output */
 int DEBUG_printf(const char *fmt, ...)
 {
@@ -90,6 +115,11 @@ void mp_hal_delay_us(mp_uint_t us)
 	mxc_delay(us);
 }
 
+mp_uint_t mp_hal_ticks_ms(void)
+{
+	return 0;
+}
+
 /******************************************************************************
  * Fatal Errors
  */
diff --git a/pycardium/mphalport.h b/pycardium/mphalport.h
index 79e25cde7..ad34087f9 100644
--- a/pycardium/mphalport.h
+++ b/pycardium/mphalport.h
@@ -1,9 +1,3 @@
 #include "py/mpconfig.h"
 
-/* TODO: Replace this with a proper implementation */
-static inline mp_uint_t mp_hal_ticks_ms(void)
-{
-	return 0;
-}
-
 void mp_hal_set_interrupt_char(char c);
-- 
GitLab