MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_tiny_printf.h File Reference

Lightweight printf implementation to reduce flash footprint. More...

#include <stdarg.h>
#include <stddef.h>

Go to the source code of this file.

Functions

int meshx_tiny_vsnprintf (char *buf, size_t size, const char *fmt, va_list args)
 Lightweight vsnprintf replacement. Supports: s, d, u, x, X, p, c, %%. Supports padding with '0' and width specifier.
int meshx_tiny_snprintf (char *buf, size_t size, const char *fmt,...)
 Lightweight snprintf replacement.
int meshx_tiny_printf (const char *fmt,...)
 Lightweight printf that writes directly to console.
int meshx_tiny_vprintf (const char *fmt, va_list args)

Detailed Description

Lightweight printf implementation to reduce flash footprint.

Copyright © 2024 - 2025 MeshX

Function Documentation

◆ meshx_tiny_printf()

int meshx_tiny_printf ( const char * fmt,
... )

Lightweight printf that writes directly to console.

166 {
167 va_list args;
168 va_start(args, fmt);
169 int res = meshx_tiny_vprintf(fmt, args);
170 va_end(args);
171 return res;
172}
int meshx_tiny_vprintf(const char *fmt, va_list args)
Definition meshx_tiny_printf.c:157

◆ meshx_tiny_snprintf()

int meshx_tiny_snprintf ( char * buf,
size_t size,
const char * fmt,
... )

Lightweight snprintf replacement.

149 {
150 va_list args;
151 va_start(args, fmt);
152 int res = meshx_tiny_vsnprintf(buf, size, fmt, args);
153 va_end(args);
154 return res;
155}
int meshx_tiny_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
Lightweight vsnprintf replacement. Supports: s, d, u, x, X, p, c, %%. Supports padding with '0' and w...
Definition meshx_tiny_printf.c:74

◆ meshx_tiny_vprintf()

int meshx_tiny_vprintf ( const char * fmt,
va_list args )
157 {
158 char buf[256];
159 int res = meshx_tiny_vsnprintf(buf, sizeof(buf), fmt, args);
160 if (res > 0) {
161 meshx_platform_console_write(buf, (uint16_t)res);
162 }
163 return res;
164}
void meshx_platform_console_write(const char *data, uint16_t len)
Writes data to the console interface.
Definition esp_platform.c:178

◆ meshx_tiny_vsnprintf()

int meshx_tiny_vsnprintf ( char * buf,
size_t size,
const char * fmt,
va_list args )

Lightweight vsnprintf replacement. Supports: s, d, u, x, X, p, c, %%. Supports padding with '0' and width specifier.

74 {
75 char *start = buf;
76 size_t initial_size = size;
77
78 if (size == 0) return 0;
79
80 while (*fmt && size > 1) {
81 if (*fmt == '%') {
82 fmt++;
83 bool left_align = false;
84 if (*fmt == '-') {
85 left_align = true;
86 fmt++;
87 }
88 char pad = ' ';
89 int width = 0;
90 if (*fmt == '0') {
91 pad = '0';
92 fmt++;
93 }
94 while (isdigit((int)*fmt)) {
95 width = width * 10 + (*fmt - '0');
96 fmt++;
97 }
98
99 // Handle long modifiers (e.g., %ld, %lu, %lx)
100 while (*fmt == 'l') fmt++;
101
102 switch (*fmt) {
103 case 's':
104 out_str(&buf, &size, va_arg(args, const char *), width, left_align);
105 break;
106 case 'd':
107 case 'i':
108 out_int(&buf, &size, va_arg(args, int32_t), 10, width, pad);
109 break;
110 case 'u':
111 out_uint(&buf, &size, va_arg(args, uint32_t), 10, width, pad, false);
112 break;
113 case 'x':
114 out_uint(&buf, &size, va_arg(args, uint32_t), 16, width, pad, false);
115 break;
116 case 'X':
117 out_uint(&buf, &size, va_arg(args, uint32_t), 16, width, pad, true);
118 break;
119 case 'p':
120 out_str(&buf, &size, "0x", 0, false);
121 out_uint(&buf, &size, (uint32_t)(uintptr_t)va_arg(args, void *), 16, 8, '0', false);
122 break;
123 case 'c':
124 out_char(&buf, &size, (char)va_arg(args, int));
125 break;
126 case '%':
127 out_char(&buf, &size, '%');
128 break;
129 default:
130 out_char(&buf, &size, '%');
131 out_char(&buf, &size, *fmt);
132 break;
133 }
134 } else {
135 out_char(&buf, &size, *fmt);
136 }
137 fmt++;
138 }
139
140 if (size > 0) {
141 *buf = '\0';
142 } else if (initial_size > 0) {
143 start[initial_size - 1] = '\0';
144 }
145
146 return (int)(buf - start);
147}
static void out_char(char **buf, size_t *size, char c)
Definition meshx_tiny_printf.c:15
static void out_uint(char **buf, size_t *size, uint32_t val, int base, int width, char pad, bool upper)
Definition meshx_tiny_printf.c:41
static void out_int(char **buf, size_t *size, int32_t val, int base, int width, char pad)
Definition meshx_tiny_printf.c:65
static void out_str(char **buf, size_t *size, const char *s, int width, bool left_align)
Definition meshx_tiny_printf.c:23