• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

blues / note-arduino / 28679653251

03 Jul 2026 07:23PM UTC coverage: 75.549% (-17.5%) from 93.061%
28679653251

Pull #168

github

web-flow
Merge 5b716ba08 into 8e06624fe
Pull Request #168: feat: add Notecard ping

120 of 190 branches covered (63.16%)

Branch coverage included in aggregate %.

20 of 100 new or added lines in 4 files covered. (20.0%)

362 of 448 relevant lines covered (80.8%)

11.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.91
/src/Notecard.cpp
1
/*!
2
 * @file Notecard.cpp
3
 *
4
 * @mainpage Arduino Library for the Notecard
5
 *
6
 * @section intro_sec Introduction
7
 *
8
 * The note-arduino Arduino library for communicating with the
9
 * <a href="https://blues.com">Blues</a>
10
 * Notecard via serial or I2C.
11
 *
12
 * This library allows you to control a Notecard by writing an Arduino sketch in
13
 * C or C++. Your sketch may programmatically configure Notecard and send Notes
14
 * to <a href="https://notehub.io">Notehub.io</a>.
15
 *
16
 * @section dependencies Dependencies
17
 *
18
 * This library is a wrapper around and depends upon the
19
 * <a href="https://github.com/blues/note-c">note-c library</a>, which it
20
 * includes as a git subtree.
21
 *
22
 * In addition, this library requires a physical
23
 * connection to a Notecard over I2C or Serial to be functional.
24
 *
25
 * @section author Author
26
 *
27
 * Written by Ray Ozzie and Brandon Satrom for Blues Inc.
28
 *
29
 * @section license License
30
 *
31
 * Copyright (c) 2019 Blues Inc. MIT License. Use of this source code is
32
 * governed by licenses granted by the copyright holder including that found in
33
 * the
34
 * <a href="https://github.com/blues/note-arduino/blob/master/LICENSE">LICENSE</a>
35
 * file.
36
 *
37
 */
38

39
#include "Notecard.h"
40

41
#include <stdarg.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44

45
#include "NoteTime.h"
46

47
/***************************************************************************
48
 SINGLETON ABSTRACTION (REQUIRED BY NOTE-C)
49
 ***************************************************************************/
50

51
namespace
52
{
53
NoteI2c *noteI2c(nullptr);
54

55
const char *noteI2cReceive(uint16_t device_address_, uint8_t *buffer_, uint16_t size_, uint32_t *available_)
8✔
56
{
57
    const char *result;
58
    if (noteI2c) {
8✔
59
        result = noteI2c->receive(device_address_, buffer_, size_, available_);
6✔
60
    } else {
61
        result = "i2c: A call to Notecard::begin() is required. {io}";
2✔
62
    }
63
    return result;
8✔
64
}
65

66
bool noteI2cReset(uint16_t device_address_)
5✔
67
{
68
    bool result;
69
    if (noteI2c) {
5✔
70
        result = noteI2c->reset(device_address_);
3✔
71
    } else {
72
        result = false;
2✔
73
    }
74
    return result;
5✔
75
}
76

77
const char *noteI2cTransmit(uint16_t device_address_, uint8_t *buffer_, uint16_t size_)
7✔
78
{
79
    const char *result;
80
    if (noteI2c) {
7✔
81
        result = noteI2c->transmit(device_address_, buffer_, size_);
5✔
82
    } else {
83
        result = "i2c: A call to Notecard::begin() is required. {io}";
2✔
84
    }
85
    return result;
7✔
86
}
87

88
NoteLog *noteLog(nullptr);
89

90
size_t noteLogPrint(const char * message_)
5✔
91
{
92
    size_t result;
93
    if (noteLog) {
5✔
94
        result = noteLog->print(message_);
3✔
95
    } else {
96
        result = 0;
2✔
97
    }
98
    return result;
5✔
99
}
100

101
NoteSerial *noteSerial(nullptr);
102

103
bool noteSerialAvailable(void)
4✔
104
{
105
    bool result;
106
    if (noteSerial) {
4✔
107
        result = noteSerial->available();
2✔
108
    } else {
109
        result = false;
2✔
110
    }
111
    return result;
4✔
112
}
113

114
char noteSerialReceive(void)
4✔
115
{
116
    char result;
117
    if (noteSerial) {
4✔
118
        result = noteSerial->receive();
2✔
119
    } else {
120
        result = '\0';
2✔
121
    }
122
    return result;
4✔
123
}
124

125
bool noteSerialReset(void)
4✔
126
{
127
    bool result;
128
    if (noteSerial) {
4✔
129
        result = noteSerial->reset();
2✔
130
    } else {
131
        result = false;
2✔
132
    }
133
    return result;
4✔
134
}
135

136
void noteSerialTransmit(uint8_t *text_, size_t len_, bool flush_)
5✔
137
{
138
    if (noteSerial) {
5✔
139
        noteSerial->transmit(text_, len_, flush_);
4✔
140
    }
141
}
5✔
142

143
NoteTxn *noteTxn(nullptr);
144

145
bool noteTransactionStart (uint32_t timeout_ms_) {
5✔
146
    bool result;
147
    if (noteTxn) {
5✔
148
        result = noteTxn->start(timeout_ms_);
3✔
149
    } else {
150
        // NoteTransaction not set, assume unnecessary
151
        result = true;
2✔
152
    }
153
    return result;
5✔
154
}
155

156
void noteTransactionStop (void) {
2✔
157
    if (noteTxn) {
2✔
158
        noteTxn->stop();
1✔
159
    }
160
}
2✔
161

162
}
163

164
/**************************************************************************/
165
/*!
166
    @brief  Platform Initialization for the Notecard.
167

168
    This function configures the user agent information and optionally
169
    the default system functions to be used by the Notecard.
170

171
    @param    assignCallbacks
172
              When `true` the system callbacks will be assigned,
173
              when `false` the system callbacks will be cleared.
174
*/
175
/**************************************************************************/
176
void Notecard::platformInit (bool assignCallbacks)
104✔
177
{
178
    NoteSetUserAgent((char *) ("note-arduino " NOTE_ARDUINO_VERSION));
104✔
179
    if (assignCallbacks) {
104✔
180
        NoteSetFnDefault(malloc, free, noteDelay, noteMillis);
65✔
181
    } else {
182
        NoteSetFn(nullptr, nullptr, nullptr, nullptr);  // Force clear
39✔
183
    }
184
}
104✔
185

186
/***************************************************************************
187
 PUBLIC FUNCTIONS
188
 ***************************************************************************/
189

190
Notecard::~Notecard (void)
135✔
191
{
192
    // Delete Singleton(s)
193
    noteI2c = make_note_i2c(nullptr);
135✔
194
    noteLog = make_note_log(nullptr);
135✔
195
    noteSerial = make_note_serial(nullptr);
135✔
196
    noteTxn = make_note_txn(nullptr);
135✔
197
}
135✔
198

199
void Notecard::begin(NoteI2c * noteI2c_, uint32_t i2cAddress_, uint32_t i2cMtu_)
51✔
200
{
201
    noteI2c = noteI2c_;
51✔
202
    platformInit(noteI2c);
51✔
203
    if (noteI2c) {
51✔
204
        NoteSetI2CAddress(i2cAddress_); // Force set user supplied address
35✔
205
        NoteSetI2CMtu(i2cMtu_);         // Force set user supplied MTU
35✔
206
        NoteSetFnI2CDefault(i2cAddress_, i2cMtu_, noteI2cReset,
35✔
207
                            noteI2cTransmit, noteI2cReceive);
208
    } else {
209
        NoteSetFnI2C(0, 0, nullptr, nullptr, nullptr); // Force clear
16✔
210
    }
211
}
51✔
212

213
void Notecard::begin(NoteSerial * noteSerial_)
46✔
214
{
215
    noteSerial = noteSerial_;
46✔
216
    platformInit(noteSerial);
46✔
217
    if (noteSerial) {
46✔
218
        NoteSetFnSerialDefault(noteSerialReset, noteSerialTransmit,
30✔
219
                               noteSerialAvailable, noteSerialReceive);
220
    } else {
221
        NoteSetFnSerial(nullptr, nullptr, nullptr, nullptr); // Force clear
16✔
222
    }
223
}
46✔
224

225
bool Notecard::debugSyncStatus(int pollFrequencyMs, int maxLevel)
3✔
226
{
227
    return NoteDebugSyncStatus(pollFrequencyMs, maxLevel);
3✔
228
}
229

230
void Notecard::deleteResponse(J *rsp) const
1✔
231
{
232
    NoteDeleteResponse(rsp);
1✔
233
}
1✔
234

235
void Notecard::end(void)
7✔
236
{
237
    // Clear I2C Interface
238
    i2cTransmitFn i2c_is_set = nullptr;
7✔
239
    NoteGetFnI2C(nullptr, nullptr, nullptr, &i2c_is_set, nullptr);
7✔
240
    if (i2c_is_set) {
7✔
241
        // Delete Singletons
242
        noteI2c = make_note_i2c(nullptr);
2✔
243
        NoteSetFnI2C(0, 0, nullptr, nullptr, nullptr);
2✔
244
    }
245

246
    // Clear Serial Interface
247
    serialTransmitFn serial_is_set = nullptr;
7✔
248
    NoteGetFnSerial(nullptr, &serial_is_set, nullptr, nullptr);
7✔
249
    if (serial_is_set) {
7✔
250
        // Delete Singletons
251
        noteSerial = make_note_serial(nullptr);
3✔
252
        NoteSetFnSerial(nullptr, nullptr, nullptr, nullptr);
3✔
253
    }
254

255
    // Clear Platform Callbacks
256
    platformInit(false);
7✔
257
}
7✔
258

259
NOTE_ARDUINO_DEPRECATED void Notecard::logDebug(const char *message) const
1✔
260
{
261
#ifdef NOTE_ARDUINO_NO_DEPRECATED_ATTR
262
    NOTE_C_LOG_WARN("logDebug is deprecated.")
263
#endif
264
    NoteDebug(message);
1✔
265
}
1✔
266

267
NOTE_ARDUINO_DEPRECATED void Notecard::logDebugf(const char *format, ...) const
1✔
268
{
269
    char message[256];
270
    va_list args;
271
    va_start(args, format);
1✔
272
    vsnprintf(message, sizeof(message), format, args);
1✔
273
    va_end(args);
1✔
274

275
#ifdef NOTE_ARDUINO_NO_DEPRECATED_ATTR
276
    NOTE_C_LOG_WARN("logDebugf is deprecated.")
277
#endif
278
    NoteDebug(message);
1✔
279
}
1✔
280

NEW
281
NoteSerial *Notecard::getNoteSerial(void) const
×
282
{
NEW
283
    return noteSerial;
×
284
}
285

286
J *Notecard::newCommand(const char *request) const
2✔
287
{
288
    return NoteNewCommand(request);
2✔
289
}
290

291
J *Notecard::newRequest(const char *request) const
2✔
292
{
293
    return NoteNewRequest(request);
2✔
294
}
295

296
J *Notecard::requestAndResponse(J *req) const
2✔
297
{
298
    return NoteRequestResponse(req);
2✔
299
}
300

301
J *Notecard::requestAndResponseWithRetry(J *req, uint32_t timeoutSeconds) const
3✔
302
{
303
    return NoteRequestResponseWithRetry(req, timeoutSeconds);
3✔
304
}
305

306
bool Notecard::responseError(J *rsp) const
2✔
307
{
308
    return NoteResponseError(rsp);
2✔
309
}
310

311
bool Notecard::sendRequest(J *req) const
2✔
312
{
313
    return NoteRequest(req);
2✔
314
}
315

316
bool Notecard::sendRequestWithRetry(J *req, uint32_t timeoutSeconds) const
3✔
317
{
318
    return NoteRequestWithRetry(req, timeoutSeconds);
3✔
319
}
320

321
void Notecard::setDebugOutputStream(NoteLog * noteLog_)
10✔
322
{
323
    noteLog = noteLog_;
10✔
324
    if (noteLog) {
10✔
325
        NoteSetFnDebugOutput(noteLogPrint);
6✔
326
    } else {
327
        make_note_log(nullptr);  // Clear singleton
4✔
328
        NoteSetFnDebugOutput(nullptr);
4✔
329
    }
330
}
10✔
331

332
void Notecard::setFn(mallocFn mallocHook, freeFn freeHook, delayMsFn delayMsHook, getMsFn getMsHook) {
4✔
333
    NoteSetFn(mallocHook, freeHook, delayMsHook, getMsHook);
4✔
334
}
4✔
335

336
void Notecard::setFnI2cMutex(mutexFn lockI2cFn_, mutexFn unlockI2cFn_) {
2✔
337
    NoteSetFnI2CMutex(lockI2cFn_, unlockI2cFn_);
2✔
338
}
2✔
339

340
void Notecard::setFnNoteMutex(mutexFn lockNoteFn_, mutexFn unlockNoteFn_) {
2✔
341
    NoteSetFnNoteMutex(lockNoteFn_, unlockNoteFn_);
2✔
342
}
2✔
343

344
void Notecard::setTransactionPins(NoteTxn * noteTxn_) {
14✔
345
    noteTxn = noteTxn_;  // Set global interface
14✔
346
    if (noteTxn_) {
14✔
347
        NoteSetFnTransaction(noteTransactionStart, noteTransactionStop);
9✔
348
    } else {
349
        make_note_txn(nullptr);  // Clear singleton
5✔
350
        NoteSetFnTransaction(nullptr, nullptr);
5✔
351
    }
352
}
14✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc