From 21bbbd8fbbc105d1d8902a6eb203472b47631489 Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Wed, 10 Jan 2024 17:35:55 +0200 Subject: [PATCH] py/mpprint.h: Add do_printf() macro. Signed-off-by: IhorNehrutsa --- drivers/esp-hosted/esp_hosted_hal.c | 6 +++--- drivers/esp-hosted/esp_hosted_hal.h | 27 ++------------------------- drivers/ninaw10/nina_bt_hci.c | 5 ++--- drivers/ninaw10/nina_wifi_bsp.c | 8 ++------ drivers/ninaw10/nina_wifi_drv.c | 9 +++------ extmod/network_esp_hosted.c | 6 +++--- extmod/network_ninaw10.c | 4 ++-- ports/mimxrt/sdio.c | 8 ++------ ports/renesas-ra/mpbthciport.c | 5 ++--- ports/rp2/mpbthciport.c | 5 ++--- py/mpprint.h | 26 ++++++++++++++++++++++++++ 11 files changed, 49 insertions(+), 60 deletions(-) diff --git a/drivers/esp-hosted/esp_hosted_hal.c b/drivers/esp-hosted/esp_hosted_hal.c index bac8a89bd0941..3e87db4b8a395 100644 --- a/drivers/esp-hosted/esp_hosted_hal.c +++ b/drivers/esp-hosted/esp_hosted_hal.c @@ -33,15 +33,15 @@ #include #include +#include "esp_hosted_hal.h" +#include "esp_hosted_wifi.h" + #include "py/runtime.h" #include "extmod/modmachine.h" #ifdef MICROPY_HW_WIFI_LED #include "led.h" #endif -#include "esp_hosted_hal.h" -#include "esp_hosted_wifi.h" - extern void mod_network_poll_events(void); STATIC mp_obj_t esp_hosted_pin_irq_callback(mp_obj_t self_in) { diff --git a/drivers/esp-hosted/esp_hosted_hal.h b/drivers/esp-hosted/esp_hosted_hal.h index 0bc7db928235a..76957f9a7b587 100644 --- a/drivers/esp-hosted/esp_hosted_hal.h +++ b/drivers/esp-hosted/esp_hosted_hal.h @@ -32,32 +32,9 @@ #ifndef ESP_HOSTED_DEBUG #define ESP_HOSTED_DEBUG (0) #endif +#define DO_PRINTF (ESP_HOSTED_DEBUG) -#if ESP_HOSTED_DEBUG -#define PROTOBUF_C_UNPACK_ERROR(...) error_printf(__VA_ARGS__); -#endif - -#define ANSI_C_RED "\x1B[31m" -#define ANSI_C_GREEN "\x1B[32m" -#define ANSI_C_YELLOW "\x1B[33m" -#define ANSI_C_BLUE "\x1B[34m" -#define ANSI_C_MAGENTA "\x1B[35m" -#define ANSI_C_CYAN "\x1B[36m" -#define ANSI_C_WHITE "\x1B[37m" -#define ANSI_C_DEFAULT "\x1B[0m" - -#if ESP_HOSTED_DEBUG -#define do_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) -#else -#define do_printf(...) -#endif - -// Logging macros. -#define debug_printf(...) do_printf(ANSI_C_BLUE); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); -#define info_printf(...) do_printf(ANSI_C_GREEN); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); -#define warn_printf(...) do_printf(ANSI_C_YELLOW); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); -#define error_printf(...) do_printf(ANSI_C_RED); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); -#define crit_printf(...) do_printf(ANSI_C_MAGENTA); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); +// #include "py/mpprint.h" typedef enum { ESP_HOSTED_MODE_BT, diff --git a/drivers/ninaw10/nina_bt_hci.c b/drivers/ninaw10/nina_bt_hci.c index f0d1b9bc8986b..9f8be808b1ac4 100644 --- a/drivers/ninaw10/nina_bt_hci.c +++ b/drivers/ninaw10/nina_bt_hci.c @@ -34,6 +34,8 @@ #include #include +// #define DO_PRINTF (1) + #include "py/runtime.h" #include "extmod/mpbthci.h" @@ -50,9 +52,6 @@ #define OCF_SET_EVENT_MASK (0x0001) #define OCF_RESET (0x0003) -#define error_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) -#define debug_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) - // Provided by the port, and also possibly shared with the stack. extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; diff --git a/drivers/ninaw10/nina_wifi_bsp.c b/drivers/ninaw10/nina_wifi_bsp.c index dc71c8260c61f..66de241ee7091 100644 --- a/drivers/ninaw10/nina_wifi_bsp.c +++ b/drivers/ninaw10/nina_wifi_bsp.c @@ -27,6 +27,8 @@ * NINA-W10 driver BSP implementation. */ +#define DO_PRINTF (NINA_DEBUG) + #include "py/mphal.h" #if MICROPY_PY_NETWORK_NINAW10 @@ -40,12 +42,6 @@ #include "nina_bsp.h" #include "nina_wifi_drv.h" -#if NINA_DEBUG -#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) -#else -#define debug_printf(...) -#endif - int nina_bsp_init(void) { mp_hal_pin_output(MICROPY_HW_NINA_GPIO1); mp_hal_pin_input(MICROPY_HW_NINA_ACK); diff --git a/drivers/ninaw10/nina_wifi_drv.c b/drivers/ninaw10/nina_wifi_drv.c index 6e4df84294c3d..8b564cd8fb9fb 100644 --- a/drivers/ninaw10/nina_wifi_drv.c +++ b/drivers/ninaw10/nina_wifi_drv.c @@ -27,6 +27,8 @@ * NINA-W10 WiFi driver. */ +#define DO_PRINTF (NINA_DEBUG) + #include "py/mphal.h" #include "py/mperrno.h" @@ -36,6 +38,7 @@ #include #include +#include "py/mpprint.h" #include "nina_bsp.h" #include "nina_wifi_drv.h" @@ -63,12 +66,6 @@ #define NINA_SSELECT_TIMEOUT (1000) #define NINA_CONNECT_TIMEOUT (10000) -#if NINA_DEBUG -#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) -#else -#define debug_printf(...) -#endif - #ifndef __REVSH #define __REVSH(x) ((((uint16_t)x) << 8) | (((uint16_t)x) >> 8)) #endif diff --git a/extmod/network_esp_hosted.c b/extmod/network_esp_hosted.c index 1a3be3f39a1a1..4f08bccef829b 100644 --- a/extmod/network_esp_hosted.c +++ b/extmod/network_esp_hosted.c @@ -35,6 +35,9 @@ #include #include +#include "esp_hosted_hal.h" +#include "esp_hosted_wifi.h" + #include "py/objtuple.h" #include "py/objlist.h" #include "py/stream.h" @@ -45,9 +48,6 @@ #include "extmod/modnetwork.h" #include "modmachine.h" -#include "esp_hosted_wifi.h" -#include "esp_hosted_hal.h" - typedef struct _esp_hosted_obj_t { mp_obj_base_t base; uint32_t itf; diff --git a/extmod/network_ninaw10.c b/extmod/network_ninaw10.c index 1deb117e70689..71627eb211d8a 100644 --- a/extmod/network_ninaw10.c +++ b/extmod/network_ninaw10.c @@ -30,6 +30,8 @@ #if MICROPY_PY_NETWORK && MICROPY_PY_NETWORK_NINAW10 +// #define DO_PRINTF (1) + #include #include #include @@ -77,8 +79,6 @@ typedef struct _nina_obj_t { #define is_nonblocking_error(errno) ((errno) == MP_EAGAIN || (errno) == MP_EWOULDBLOCK || (errno) == MP_EINPROGRESS) -#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__) - static uint16_t bind_port = BIND_PORT_RANGE_MIN; const mp_obj_type_t mod_network_nic_type_nina; static nina_obj_t network_nina_wl_sta = {{(mp_obj_type_t *)&mod_network_nic_type_nina}, false, false, MOD_NETWORK_STA_IF}; diff --git a/ports/mimxrt/sdio.c b/ports/mimxrt/sdio.c index 2fa0f228fe3a5..1db893d7cf618 100644 --- a/ports/mimxrt/sdio.c +++ b/ports/mimxrt/sdio.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#define DO_PRINTF (SDIO_DEBUG) + #include #include "py/mperrno.h" #include "py/mphal.h" @@ -79,12 +81,6 @@ #define SDMMC_CLOCK_25MHZ (25000000U) #define SDMMC_CLOCK_50MHZ (50000000U) -#if SDIO_DEBUG -#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) -#else -#define debug_printf(...) -#endif - #define DMA_DESCRIPTOR_BUFFER_SIZE (32U) AT_NONCACHEABLE_SECTION_ALIGN( static uint32_t sdio_adma_descriptor_table[DMA_DESCRIPTOR_BUFFER_SIZE], USDHC_ADMA2_ADDRESS_ALIGN); diff --git a/ports/renesas-ra/mpbthciport.c b/ports/renesas-ra/mpbthciport.c index 2e9f6d916698b..017bf916bb754 100644 --- a/ports/renesas-ra/mpbthciport.c +++ b/ports/renesas-ra/mpbthciport.c @@ -25,6 +25,8 @@ * THE SOFTWARE. */ +// #define DO_PRINTF (1) + #include "py/runtime.h" #include "py/stream.h" #include "py/mphal.h" @@ -37,9 +39,6 @@ #if MICROPY_PY_BLUETOOTH -#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) -#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) - uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; STATIC mp_sched_node_t mp_bluetooth_hci_sched_node; diff --git a/ports/rp2/mpbthciport.c b/ports/rp2/mpbthciport.c index 1aacbe881f9f0..8fd77b6e35149 100644 --- a/ports/rp2/mpbthciport.c +++ b/ports/rp2/mpbthciport.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +// #define DO_PRINTF (1) + #include "py/runtime.h" #include "py/stream.h" #include "py/mphal.h" @@ -36,9 +38,6 @@ #if MICROPY_PY_BLUETOOTH -#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) -#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) - uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; // Soft timer and scheduling node for scheduling a HCI poll. diff --git a/py/mpprint.h b/py/mpprint.h index 8383ea85794c2..5b82a347b0708 100644 --- a/py/mpprint.h +++ b/py/mpprint.h @@ -79,4 +79,30 @@ int mp_printf(const mp_print_t *print, const char *fmt, ...); int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args); #endif +#define ANSI_C_RED "\x1B[31m" +#define ANSI_C_GREEN "\x1B[32m" +#define ANSI_C_YELLOW "\x1B[33m" +#define ANSI_C_BLUE "\x1B[34m" +#define ANSI_C_MAGENTA "\x1B[35m" +#define ANSI_C_CYAN "\x1B[36m" +#define ANSI_C_WHITE "\x1B[37m" +#define ANSI_C_DEFAULT "\x1B[0m" + +#ifndef DO_PRINTF +#define DO_PRINTF (0) +#endif + +#if DO_PRINTF +#define do_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) +#else +#define do_printf(...) +#endif + +// Logging macros. +#define debug_printf(...) do_printf(ANSI_C_BLUE); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); +#define info_printf(...) do_printf(ANSI_C_GREEN); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); +#define warn_printf(...) do_printf(ANSI_C_YELLOW); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); +#define error_printf(...) do_printf(ANSI_C_RED); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); +#define crit_printf(...) do_printf(ANSI_C_MAGENTA); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); + #endif // MICROPY_INCLUDED_PY_MPPRINT_H