Skip to content

Commit d38b103

Browse files
committed
Add a printf-style interface for formatted output
1 parent 484ce72 commit d38b103

File tree

8 files changed

+126
-63
lines changed

8 files changed

+126
-63
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ OBJ := $(addprefix $(OBJDIR)/src/, \
7575
pinmux.o \
7676
Producer.o \
7777
Serial.o \
78+
SerialIO.o \
7879
Task.o \
7980
)
8081

src/Producer.c

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
#include "Producer.h"
6-
7-
#include <string.h>
8-
9-
#include "IPCQueue.h"
106
#include "Ptr.h"
7+
#include "SerialIO.h"
118
#include "Task.h"
129
#include "Ticks.h"
1310

@@ -18,36 +15,9 @@ Run(ProducerTask* aProducer)
1815
"This"," is"," a"," SensorWeb"," device"," by"," Mozilla.\n\r"
1916
};
2017

21-
IPCMessage msg;
22-
int res = IPCMessageInit(&msg);
23-
if (res < 0) {
24-
return;
25-
}
26-
2718
for (unsigned long i = 0;; i = (i + 1) % ArrayLength(sMessage)) {
28-
29-
res = IPCMessageProduce(&msg, strlen(sMessage[i]) + 1, (void*)sMessage[i]);
30-
if (res < 0) {
31-
return;
32-
}
33-
/* no need to synchronize over static constant buffer */
34-
msg.mStatus |= IPC_MESSAGE_FLAG_NOWAIT;
35-
36-
res = IPCMessageQueueConsume(aProducer->mSendQueue, &msg);
37-
if (res < 0) {
38-
return;
39-
}
40-
19+
print("%s", sMessage[i]);
4120
vTaskDelay(TicksOfMSecs(200));
42-
43-
/* While we have been waiting in vTaskDelay(), the consumer
44-
* probably processed our message. Waiting for consumption is
45-
* only a formality here, as we set the NOWAIT flag.
46-
*/
47-
res = IPCMessageWaitForConsumption(&msg);
48-
if (res < 0) {
49-
return;
50-
}
5121
}
5222
}
5323

@@ -65,9 +35,8 @@ TaskEntryPoint(void* aParam)
6535
}
6636

6737
int
68-
ProducerTaskInit(ProducerTask* aProducer, IPCMessageQueue* aSendQueue)
38+
ProducerTaskInit(ProducerTask* aProducer)
6939
{
70-
aProducer->mSendQueue = aSendQueue;
7140
aProducer->mTask = NULL;
7241

7342
return 0;

src/Producer.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55
#pragma once
66

77
#include <FreeRTOS.h>
8-
#include <queue.h>
98
#include <task.h>
109

11-
#include "IPCQueue.h"
12-
1310
typedef struct
1411
{
15-
IPCMessageQueue* mSendQueue;
16-
1712
TaskHandle_t mTask;
1813
} ProducerTask;
1914

2015
int
21-
ProducerTaskInit(ProducerTask* aProducer, IPCMessageQueue* aSendQueue);
16+
ProducerTaskInit(ProducerTask* aProducer);
2217

2318
int
2419
ProducerTaskSpawn(ProducerTask* aProducer);

src/Serial.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@
44

55
#include "Serial.h"
66

7+
#include <FreeRTOS.h>
8+
#include <task.h>
79
#include <uart_if.h>
810

911
#include "Task.h"
1012

13+
/*
14+
* Serial output
15+
*/
16+
17+
typedef struct
18+
{
19+
IPCMessageQueue mRecvQueue;
20+
21+
TaskHandle_t mTask;
22+
} SerialOutTask;
23+
1124
static void
1225
Run(SerialOutTask* aSerialOut)
1326
{
@@ -39,7 +52,7 @@ TaskEntryPoint(void* aParam)
3952
vTaskSuspend(serialOut->mTask);
4053
}
4154

42-
int
55+
static int
4356
SerialOutTaskInit(SerialOutTask* aSerialOut)
4457
{
4558
int res = IPCMessageQueueInit(&aSerialOut->mRecvQueue);
@@ -51,7 +64,7 @@ SerialOutTaskInit(SerialOutTask* aSerialOut)
5164
return 0;
5265
}
5366

54-
int
67+
static int
5568
SerialOutTaskSpawn(SerialOutTask* aSerialOut)
5669
{
5770
BaseType_t res = xTaskCreate(TaskEntryPoint, "serial-out",
@@ -62,3 +75,30 @@ SerialOutTaskSpawn(SerialOutTask* aSerialOut)
6275
}
6376
return 0;
6477
}
78+
79+
/*
80+
* Public interfaces
81+
*/
82+
83+
static SerialOutTask sSerialOutTask;
84+
85+
int
86+
SerialInit()
87+
{
88+
/*
89+
* Create the output task
90+
*/
91+
if (SerialOutTaskInit(&sSerialOutTask) < 0) {
92+
return -1;
93+
}
94+
if (SerialOutTaskSpawn(&sSerialOutTask) < 0) {
95+
return -1;
96+
}
97+
return 0;
98+
}
99+
100+
IPCMessageQueue*
101+
GetSerialOutQueue()
102+
{
103+
return &sSerialOutTask.mRecvQueue;
104+
}

src/Serial.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,13 @@
44

55
#pragma once
66

7-
#include <FreeRTOS.h>
8-
#include <queue.h>
9-
#include <task.h>
10-
117
#include "IPCQueue.h"
128

13-
typedef struct
14-
{
15-
IPCMessageQueue mRecvQueue;
16-
17-
TaskHandle_t mTask;
18-
} SerialOutTask;
19-
209
int
21-
SerialOutTaskInit(SerialOutTask* aSerialOut);
10+
SerialInit(void);
2211

23-
int
24-
SerialOutTaskSpawn(SerialOutTask* aSerialOut);
12+
/* Returns the message queue for output of over the serial line. This
13+
* is a singleton.
14+
*/
15+
IPCMessageQueue*
16+
GetSerialOutQueue(void);

src/SerialIO.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#include "SerialIO.h"
6+
#include <stdio.h>
7+
#include "Serial.h"
8+
9+
static int
10+
Print(uint32_t aLength, void* aBuffer)
11+
{
12+
IPCMessageQueue* queue = GetSerialOutQueue();
13+
if (!queue) {
14+
return -1;
15+
}
16+
17+
IPCMessage msg;
18+
int res = IPCMessageInit(&msg);
19+
if (res < 0) {
20+
return -1;
21+
}
22+
res = IPCMessageProduce(&msg, aLength, aBuffer);
23+
if (res < 0) {
24+
return -1;
25+
}
26+
res = IPCMessageQueueConsume(queue, &msg);
27+
if (res < 0) {
28+
return -1;
29+
}
30+
return IPCMessageWaitForConsumption(&msg);
31+
}
32+
33+
/*
34+
* Libc-like interfaces for easy usage.
35+
*/
36+
37+
int
38+
print(const char* fmt, ...)
39+
{
40+
va_list ap;
41+
42+
va_start(ap, fmt);
43+
int res = vprint(fmt, ap);
44+
va_end(ap);
45+
46+
return res;
47+
}
48+
49+
int
50+
vprint(const char* fmt, va_list ap)
51+
{
52+
char buf[128];
53+
int res = vsnprintf(buf, sizeof(buf), fmt, ap);
54+
if (res < 0) {
55+
return -1;
56+
}
57+
return Print(sizeof(buf), buf);
58+
}

src/SerialIO.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#pragma once
6+
7+
#include <stdarg.h>
8+
9+
int
10+
print(const char* fmt, ...);
11+
12+
int
13+
vprint(const char* fmt, va_list ap);

src/main.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ main(void)
3737
* Create the output task
3838
*/
3939

40-
static SerialOutTask serialOutTask;
41-
42-
if (SerialOutTaskInit(&serialOutTask) < 0) {
43-
return EXIT_FAILURE;
44-
}
45-
if (SerialOutTaskSpawn(&serialOutTask) < 0) {
40+
if (SerialInit() < 0) {
4641
return EXIT_FAILURE;
4742
}
4843

@@ -52,7 +47,7 @@ main(void)
5247

5348
static ProducerTask producerTask;
5449

55-
if (ProducerTaskInit(&producerTask, &serialOutTask.mRecvQueue) < 0) {
50+
if (ProducerTaskInit(&producerTask) < 0) {
5651
return EXIT_FAILURE;
5752
}
5853
if (ProducerTaskSpawn(&producerTask) < 0) {

0 commit comments

Comments
 (0)