Skip to content

Commit 4082ffa

Browse files
szczysDhruvaG2000
authored andcommitted
zephyrSerial: print versus println
Implement versions with and without linefeed Return the char count Signed-off-by: Mike Szczys <[email protected]>
1 parent b667879 commit 4082ffa

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

cores/arduino/zephyrSerial.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,35 @@ size_t arduino::ZephyrSerial::begin(unsigned long int baudrate){
66
return 0;
77
}
88

9+
size_t arduino::ZephyrSerial::print_char(char ch, bool lf){
10+
printk(lf ? "%c\n" : "%c", ch);
11+
return lf ? 2 : 1;
12+
}
13+
14+
size_t arduino::ZephyrSerial::print_str(const char* ptr, bool lf) {
15+
printf(lf ? "%s\n" : "%s", ptr);
16+
return lf ? strlen(ptr)+1 : strlen(ptr);
17+
}
18+
919
size_t arduino::ZephyrSerial::print(char ch){
10-
printk("%c",ch);
11-
return 0;
20+
return print_char(ch, false);
1221
}
13-
size_t arduino::ZephyrSerial::println(const char* ptr) {
14-
printf("\n%s\n", ptr);
15-
return 0;
22+
23+
size_t arduino::ZephyrSerial::println(char ch){
24+
return print_char(ch, true);
1625
}
17-
size_t arduino::ZephyrSerial::println(char c){
18-
// pvt_c = c;
19-
printf("\n%c\n",c);
20-
return 0;
26+
27+
size_t arduino::ZephyrSerial::print(const char* ptr) {
28+
return print_str(ptr, false);
29+
}
30+
31+
size_t arduino::ZephyrSerial::println(const char* ptr){
32+
return print_str(ptr, true);
2133
}
2234

2335
size_t arduino::ZephyrSerial::println(void){
2436
printf("\n");
2537
return 0;
2638
}
2739

28-
arduino::ZephyrSerial Serial;
40+
arduino::ZephyrSerial Serial;

cores/arduino/zephyrSerial.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ namespace arduino {
77

88
class ZephyrSerial {
99
char pvt_c;
10+
11+
private:
12+
size_t print_char(const char c, bool lf);
13+
size_t print_str(const char * ptr, bool lf);
14+
1015
public:
1116
size_t begin(unsigned long int baudrate); //TODO
1217

1318
size_t print(const char c);
14-
19+
size_t print(const char * ptr);
1520
size_t println(const char c);
1621
size_t println(const char* ptr);
1722
size_t println(void);
@@ -20,4 +25,4 @@ class ZephyrSerial {
2025

2126
} // namespace arduino
2227

23-
extern arduino::ZephyrSerial Serial;
28+
extern arduino::ZephyrSerial Serial;

samples/hello_arduino/src/app.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ void setup() {
1212
}
1313
void loop() {
1414
char c = 'D';
15-
Serial.print(c);
16-
Serial.println("Hello, World!");
15+
size_t ret1;
16+
size_t ret2;
17+
ret1 = Serial.print(c);
18+
ret2 = Serial.println("Hello, World!");
19+
printk("Sizes: %d %d\n", ret1, ret2);
20+
Serial.println();
21+
ret1 = Serial.print("My letter is: ");
22+
ret2 = Serial.println(c);
23+
printk("Sizes: %d %d\n", ret1, ret2);
24+
Serial.println();
25+
char myString[] = "Will it print?";
26+
ret1 = Serial.println(myString);
27+
printk("Size: %d \n\n\n", ret1);
1728
delay(1000); // 1 second delay
1829
}

0 commit comments

Comments
 (0)