Skip to content

Commit 1d3c537

Browse files
authored
feat: I2C and Notecard mutex callbacks (#100)
* feat: I2C and Notecard mutex callbacks * appease the comment warrior
1 parent 9d29a7b commit 1d3c537

File tree

6 files changed

+354
-109
lines changed

6 files changed

+354
-109
lines changed

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ responseError KEYWORD2
2121
sendRequest KEYWORD2
2222
sendRequestWithRetry KEYWORD2
2323
setDebugOutputStream KEYWORD2
24+
setFnI2cMutex KEYWORD2
25+
setFnNoteMutex KEYWORD2
2426
setTransactionPins KEYWORD2
2527

2628
########################################

src/Notecard.cpp

+142-109
Original file line numberDiff line numberDiff line change
@@ -252,75 +252,72 @@ void Notecard::begin(NoteSerial * noteSerial_)
252252

253253
/**************************************************************************/
254254
/*!
255-
@brief Set the debug output source.
256-
A NoteLog object will be constructed via `make_note_log()`
257-
using a platform specific logging channel (for example, `Serial`
258-
on Arduino). The specified channel will be configured as the
259-
source for debug messages provided to `notecard.logDebug()`.
260-
@param noteLog
261-
A platform specific log implementation to be used for
262-
debug output.
255+
@brief Clear the debug output source.
263256
*/
264257
/**************************************************************************/
265-
void Notecard::setDebugOutputStream(NoteLog * noteLog_)
258+
void Notecard::clearDebugOutputStream(void)
266259
{
267-
noteLog = noteLog_;
268-
if (noteLog) {
269-
NoteSetFnDebugOutput(noteLogPrint);
270-
} else {
271-
NoteSetFnDebugOutput(nullptr);
272-
}
260+
noteLog = nullptr;
261+
NoteSetFnDebugOutput(nullptr);
273262
}
274263

275264
/**************************************************************************/
276265
/*!
277-
@brief Clear the debug output source.
266+
@brief Periodically show Notecard sync status, returning `TRUE`
267+
if something was displayed to the debug stream.
268+
@param pollFrequencyMs
269+
The frequency to poll the Notecard for sync status.
270+
@param maxLevel
271+
The maximum log level to output to the debug console. Pass
272+
-1 for all.
273+
@return `True` if a pending response was displayed to the debug stream.
278274
*/
279275
/**************************************************************************/
280-
void Notecard::clearDebugOutputStream(void)
276+
bool Notecard::debugSyncStatus(int pollFrequencyMs, int maxLevel)
281277
{
282-
noteLog = nullptr;
283-
NoteSetFnDebugOutput(nullptr);
278+
return NoteDebugSyncStatus(pollFrequencyMs, maxLevel);
284279
}
285280

286281
/**************************************************************************/
287282
/*!
288-
@brief Set the transaction pins.
289-
A NoteTxn object will be constructed via `make_note_txn()`
290-
using a platform specific tuple of digital I/O pins. The
291-
pins are used to send a request to transact and a listen
292-
for the clear to transact signal. Transaction pins are not
293-
necessary on any legacy Notecards, and are only necessary
294-
for certain Notecard SKUs. The pins allow the Notecard to
295-
inform the host it has had time to awaken from deep sleep
296-
and is ready to process commands.
297-
@param noteTxn
298-
A platform specific tuple of digital I/O pins.
283+
@brief Deletes a `J` JSON response object from memory.
284+
@param rsp
285+
A `J` JSON response object.
299286
*/
300287
/**************************************************************************/
301-
void Notecard::setTransactionPins(NoteTxn * noteTxn_) {
302-
noteTxn = noteTxn_; // Set global interface
303-
if (noteTxn_) {
304-
NoteSetFnTransaction(noteTransactionStart, noteTransactionStop);
305-
} else {
306-
make_note_txn(nullptr); // Clear singleton
307-
NoteSetFnTransaction(nullptr, nullptr);
308-
}
288+
void Notecard::deleteResponse(J *rsp)
289+
{
290+
NoteDeleteResponse(rsp);
309291
}
310292

311293
/**************************************************************************/
312294
/*!
313-
@brief Creates a new request object for population by the host.
314-
This function accepts a request string (for example, `"note.add"`)
315-
and initializes a JSON Object to return to the host.
316-
@param request
317-
The request name, for example, `note.add`.
318-
@return A `J` JSON Object populated with the request name.
295+
@brief Write a message to the serial debug stream.
296+
@param message
297+
A string to log to the serial debug stream.
319298
*/
320299
/**************************************************************************/
321-
J *Notecard::newRequest(const char *request)
300+
void Notecard::logDebug(const char *message)
322301
{
323-
return NoteNewRequest(request);
302+
NoteDebug(message);
303+
}
304+
305+
/**************************************************************************/
306+
/*!
307+
@brief Write a formatted message to the serial debug stream.
308+
@param format
309+
A format string to log to the serial debug stream.
310+
@param ... one or more values to interpolate into the format string.
311+
*/
312+
/**************************************************************************/
313+
void Notecard::logDebugf(const char *format, ...)
314+
{
315+
char message[256];
316+
va_list args;
317+
va_start(args, format);
318+
vsnprintf(message, sizeof(message), format, args);
319+
va_end(args);
320+
NoteDebug(message);
324321
}
325322

326323
/**************************************************************************/
@@ -340,35 +337,17 @@ J *Notecard::newCommand(const char *request)
340337

341338
/**************************************************************************/
342339
/*!
343-
@brief Sends a request to the Notecard.
344-
This function takes a populated `J` JSON request object
345-
and sends it to the Notecard.
346-
@param req
347-
A `J` JSON request object.
348-
@return `True` if the message was successfully sent to the Notecard,
349-
`False` if there was an error.
350-
*/
351-
/**************************************************************************/
352-
bool Notecard::sendRequest(J *req)
353-
{
354-
return NoteRequest(req);
355-
}
356-
357-
/**************************************************************************/
358-
/*!
359-
@brief Sends a request to the Notecard, retrying it on failure until the
360-
provided timeout interval lapses.
361-
@param req
362-
A `J` JSON request object.
363-
@param timeoutSeconds
364-
The timeout interval, in seconds.
365-
@return `True` if the message was successfully sent to the Notecard,
366-
`False` if the message couldn't be sent.
340+
@brief Creates a new request object for population by the host.
341+
This function accepts a request string (for example, `"note.add"`)
342+
and initializes a JSON Object to return to the host.
343+
@param request
344+
The request name, for example, `note.add`.
345+
@return A `J` JSON Object populated with the request name.
367346
*/
368347
/**************************************************************************/
369-
bool Notecard::sendRequestWithRetry(J *req, uint32_t timeoutSeconds)
348+
J *Notecard::newRequest(const char *request)
370349
{
371-
return NoteRequestWithRetry(req, timeoutSeconds);
350+
return NoteNewRequest(request);
372351
}
373352

374353
/**************************************************************************/
@@ -404,72 +383,126 @@ J *Notecard::requestAndResponseWithRetry(J *req, uint32_t timeoutSeconds)
404383

405384
/**************************************************************************/
406385
/*!
407-
@brief Deletes a `J` JSON response object from memory.
386+
@brief Determines if there is an error string present in a response object.
408387
@param rsp
409-
A `J` JSON response object.
388+
A `J` JSON Response object.
389+
@return `true` if the response object contains an error.
410390
*/
411391
/**************************************************************************/
412-
void Notecard::deleteResponse(J *rsp)
392+
bool Notecard::responseError(J *rsp)
413393
{
414-
NoteDeleteResponse(rsp);
394+
return NoteResponseError(rsp);
415395
}
416396

417397
/**************************************************************************/
418398
/*!
419-
@brief Write a message to the serial debug stream.
420-
@param message
421-
A string to log to the serial debug stream.
399+
@brief Sends a request to the Notecard.
400+
This function takes a populated `J` JSON request object
401+
and sends it to the Notecard.
402+
@param req
403+
A `J` JSON request object.
404+
@return `True` if the message was successfully sent to the Notecard,
405+
`False` if there was an error.
422406
*/
423407
/**************************************************************************/
424-
void Notecard::logDebug(const char *message)
408+
bool Notecard::sendRequest(J *req)
425409
{
426-
NoteDebug(message);
410+
return NoteRequest(req);
427411
}
428412

429413
/**************************************************************************/
430414
/*!
431-
@brief Write a formatted message to the serial debug stream.
432-
@param format
433-
A format string to log to the serial debug stream.
434-
@param ... one or more values to interpolate into the format string.
415+
@brief Sends a request to the Notecard, retrying it on failure until the
416+
provided timeout interval lapses.
417+
@param req
418+
A `J` JSON request object.
419+
@param timeoutSeconds
420+
The timeout interval, in seconds.
421+
@return `True` if the message was successfully sent to the Notecard,
422+
`False` if the message couldn't be sent.
435423
*/
436424
/**************************************************************************/
437-
void Notecard::logDebugf(const char *format, ...)
425+
bool Notecard::sendRequestWithRetry(J *req, uint32_t timeoutSeconds)
438426
{
439-
char message[256];
440-
va_list args;
441-
va_start(args, format);
442-
vsnprintf(message, sizeof(message), format, args);
443-
va_end(args);
444-
NoteDebug(message);
427+
return NoteRequestWithRetry(req, timeoutSeconds);
445428
}
446429

447430
/**************************************************************************/
448431
/*!
449-
@brief Periodically show Notecard sync status, returning `TRUE`
450-
if something was displayed to the debug stream.
451-
@param pollFrequencyMs
452-
The frequency to poll the Notecard for sync status.
453-
@param maxLevel
454-
The maximum log level to output to the debug console. Pass
455-
-1 for all.
456-
@return `True` if a pending response was displayed to the debug stream.
432+
@brief Set the debug output source.
433+
A NoteLog object will be constructed via `make_note_log()`
434+
using a platform specific logging channel (for example, `Serial`
435+
on Arduino). The specified channel will be configured as the
436+
source for debug messages provided to `notecard.logDebug()`.
437+
@param noteLog
438+
A platform specific log implementation to be used for
439+
debug output.
457440
*/
458441
/**************************************************************************/
459-
bool Notecard::debugSyncStatus(int pollFrequencyMs, int maxLevel)
442+
void Notecard::setDebugOutputStream(NoteLog * noteLog_)
460443
{
461-
return NoteDebugSyncStatus(pollFrequencyMs, maxLevel);
444+
noteLog = noteLog_;
445+
if (noteLog) {
446+
NoteSetFnDebugOutput(noteLogPrint);
447+
} else {
448+
NoteSetFnDebugOutput(nullptr);
449+
}
462450
}
463451

464452
/**************************************************************************/
465453
/*!
466-
@brief Determines if there is an error string present in a response object.
467-
@param rsp
468-
A `J` JSON Response object.
469-
@return `True` if the response object contains an error.
454+
@brief Set the lock/unlock functions the Notecard uses for I2C access.
455+
@param lockI2cFn
456+
A user-defined callback that blocks until access to the I2C
457+
bus has become available, then returns with ownership of the
458+
I2C bus.
459+
@param unlockI2cFn
460+
A user-defined callback that releases ownership of the
461+
I2C bus taken during the call to `lockI2cFn()`.
470462
*/
471463
/**************************************************************************/
472-
bool Notecard::responseError(J *rsp)
473-
{
474-
return NoteResponseError(rsp);
464+
void Notecard::setFnI2cMutex(mutexFn lockI2cFn_, mutexFn unlockI2cFn_) {
465+
NoteSetFnI2CMutex(lockI2cFn_, unlockI2cFn_);
466+
}
467+
468+
/**************************************************************************/
469+
/*!
470+
@brief Set the lock/unlock functions the host MCU uses to ensure
471+
a complete transaction with the Notecard.
472+
@param lockNoteFn
473+
A user-defined callback that blocks until the Notecard has
474+
completed any previous transactions, then returns with
475+
ownership of the next Notecard transaction.
476+
@param unlockNoteFn
477+
A user-defined callback that releases ownership of the
478+
Notecard transaction taken during the call to `lockNoteFn()`.
479+
*/
480+
/**************************************************************************/
481+
void Notecard::setFnNoteMutex(mutexFn lockNoteFn_, mutexFn unlockNoteFn_) {
482+
NoteSetFnNoteMutex(lockNoteFn_, unlockNoteFn_);
483+
}
484+
485+
/**************************************************************************/
486+
/*!
487+
@brief Set the transaction pins.
488+
A NoteTxn object will be constructed via `make_note_txn()`
489+
using a platform specific tuple of digital I/O pins. The
490+
pins are used to send a request to transact and to listen
491+
for the clear to transact signal. Transaction pins are not
492+
necessary on any legacy Notecards, and are only necessary
493+
for certain Notecard SKUs. The pins allow the Notecard to
494+
inform the host it has had time to awaken from deep sleep
495+
and is ready to process commands.
496+
@param noteTxn
497+
A platform specific tuple of digital I/O pins.
498+
*/
499+
/**************************************************************************/
500+
void Notecard::setTransactionPins(NoteTxn * noteTxn_) {
501+
noteTxn = noteTxn_; // Set global interface
502+
if (noteTxn_) {
503+
NoteSetFnTransaction(noteTransactionStart, noteTransactionStop);
504+
} else {
505+
make_note_txn(nullptr); // Clear singleton
506+
NoteSetFnTransaction(nullptr, nullptr);
507+
}
475508
}

src/Notecard.h

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class Notecard
9393
bool sendRequest(J *req);
9494
bool sendRequestWithRetry(J *req, uint32_t timeoutSeconds);
9595
void setDebugOutputStream(NoteLog * noteLog);
96+
void setFnI2cMutex(mutexFn lockI2cFn, mutexFn unlockI2cFn);
97+
void setFnNoteMutex(mutexFn lockNoteFn, mutexFn unlockNoteFn);
9698
void setTransactionPins(NoteTxn * noteTxn);
9799

98100
private:

0 commit comments

Comments
 (0)