Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move strings to PROGMEM #90

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"context": "..",

// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
//"dockerFile": "../.github/actions/compile-examples/Dockerfile",
"dockerFile": "../.github/actions/run-tests-in-container/Dockerfile",
"dockerFile": "../.github/actions/compile-examples/Dockerfile",
//"dockerFile": "../.github/actions/run-tests-in-container/Dockerfile",

// Set *default* container specific settings.json values on container create.
"settings": {},
Expand Down
64 changes: 36 additions & 28 deletions examples/Example1_NotecardBasics/Example1_NotecardBasics.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// Include the Arduino library for the Notecard

#include <Notecard.h>
#include <Wire.h>

// If the Notecard is connected to a serial port, define it here. For example, if you are using
// the Adafruit Feather NRF52840 Express, the RX/TX pins (and thus the Notecard) are on Serial1.
Expand All @@ -44,7 +43,7 @@

// This is the unique Product Identifier for your device
#ifndef PRODUCT_UID
#define PRODUCT_UID "" // "com.my-company.my-name:my-project"
#define PRODUCT_UID "com.zakoverflow.test" // "com.my-company.my-name:my-project"
#pragma message "PRODUCT_UID is not defined in this example. Please ensure your Notecard has a product identifier set before running this example or define it in code here. More details at https://dev.blues.io/tools-and-sdks/samples/product-uid"
#endif

Expand All @@ -55,6 +54,7 @@ Notecard notecard;
void setup()
{

#ifdef usbSerial
// Set up for debug output. If you open Arduino's serial terminal window, you'll be able to
// watch JSON objects being transferred to and from the Notecard for each request. On most
// Arduino devices, Arduino's serial debug output is on the "Serial" device at 115200.
Expand All @@ -63,55 +63,63 @@ void setup()
// Note that the initial 2.5s delay is required by some Arduino cards before debug
// UART output can be successfully displayed in the Arduino IDE, including the
// Adafruit Feather nRF52840 Express.
#ifdef usbSerial
delay(2500);
usbSerial.begin(115200);
notecard.setDebugOutputStream(usbSerial);
usbSerial.begin(9600);
const size_t start_wait_ms = millis();
while (!usbSerial && ((millis() - start_wait_ms) < 5000));
Serial.println("Serial READY");
#endif

// Initialize the physical I/O channel to the Notecard
#ifdef txRxPinsSerial
notecard.begin(txRxPinsSerial, 9600);
#else
Wire.begin();

notecard.begin();
Serial.println("1");
#endif

// "newRequest()" uses the bundled "J" json package to allocate a "req", which is a JSON object
// for the request to which we will then add Request arguments. The function allocates a "req"
// request structure using malloc() and initializes its "req" field with the type of request.
J *req = notecard.newRequest("hub.set");

// This command (required) causes the data to be delivered to the Project on notehub.io that has claimed
// this Product ID. (see above)
if (myProductID[0]) {
JAddStringToObject(req, "product", myProductID);
Serial.println("2");
if (req) {
// This command (required) causes the data to be delivered to the Project on notehub.io that has claimed
// this Product ID. (see above)
if (myProductID[0]) {
JAddStringToObject(req, "product", myProductID);
Serial.println("3");
}
// This command determines how often the Notecard connects to the service. If "continuous" the Notecard
// immediately establishes a session with the service at notehub.io, and keeps it active continuously.
// Because of the power requirements of a continuous connection, a battery powered device would instead
// only sample its sensors occasionally, and would only upload to the service on a periodic basis.
JAddStringToObject(req, "mode", "continuous");
Serial.println("4");

// Issue the request, telling the Notecard how and how often to access the service.
// This results in a JSON message to Notecard formatted like:
// { "req" : "service.set",
// "product" : myProductID,
// "mode" : "continuous"
// }
// Note that sendRequest() always uses free() to release the request data structure, and it
// returns "true" if success and "false" if there is any failure.
notecard.sendRequest(req);
Serial.println("5");
}
// This command determines how often the Notecard connects to the service. If "continuous" the Notecard
// immediately establishes a session with the service at notehub.io, and keeps it active continuously.
// Because of the power requirements of a continuous connection, a battery powered device would instead
// only sample its sensors occasionally, and would only upload to the service on a periodic basis.
JAddStringToObject(req, "mode", "continuous");

// Issue the request, telling the Notecard how and how often to access the service.
// This results in a JSON message to Notecard formatted like:
// { "req" : "service.set",
// "product" : myProductID,
// "mode" : "continuous"
// }
// Note that sendRequest() always uses free() to release the request data structure, and it
// returns "true" if success and "false" if there is any failure.
notecard.sendRequest(req);

}

// In the Arduino main loop which is called repeatedly, add outbound data every 15 seconds
void loop()
{
Serial.println("Begin `loop`");

// Count the simulated measurements that we send to the cloud, and stop the demo before long.
static unsigned eventCounter = 0;
if (eventCounter++ > 25) {
if (eventCounter > 25) {
++eventCounter;
return;
}

Expand Down
6 changes: 4 additions & 2 deletions src/note-c/n_b64.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@
* Base64 encoder/decoder. Originally Apache file ap_base64.c
*/

#include <avr/pgmspace.h>
#include <string.h>

#include "n_lib.h"

/* aaaack but it's fast and const should make it shared text page. */
static const unsigned char pr2six[256] = {
static const unsigned char pr2six[256] PROGMEM = {
/* ASCII table */
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
Expand Down Expand Up @@ -163,7 +165,7 @@ int JB64Decode(char *bufplain, const char *bufcoded)
return nbytesdecoded;
}

static const char basis_64[] =
static const char basis_64[] PROGMEM =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int JB64EncodeLen(int len)
Expand Down
20 changes: 10 additions & 10 deletions src/note-c/n_cjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static unsigned char* Jstrdup(const unsigned char* string)
return NULL;
}

length = strlen((const char*)string) + sizeof("");
length = strlen((const char*)string) + 1;
copy = (unsigned char*)_Malloc(length);
if (copy == NULL) {
return NULL;
Expand Down Expand Up @@ -412,18 +412,18 @@ static Jbool print_number(const J * const item, printbuffer * const output_buffe
/* This checks for NaN and Infinity */
if ((d * 0) != 0) {
char *nbuf = (char *) number_buffer;
strcpy(nbuf, "null");
strcpy(nbuf, c_null);
length = strlen(nbuf);
} else {
#if !MINIMIZE_CLIB_DEPENDENCIES
JNUMBER test;
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
length = sprintf((char*)number_buffer, "%1.15g", d);
length = sprintf((char*)number_buffer, c_fmt_1_15g, d);

/* Check whether the original double can be recovered */
if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((JNUMBER)test != d)) {
if ((sscanf((char*)number_buffer, c_fmt_lg, &test) != 1) || ((JNUMBER)test != d)) {
/* If not, print with 17 decimal places of precision */
length = sprintf((char*)number_buffer, "%1.17g", d);
length = sprintf((char*)number_buffer, c_fmt_1_17g, d);
}
#else
char *nbuf = (char *) number_buffer;
Expand All @@ -438,7 +438,7 @@ static Jbool print_number(const J * const item, printbuffer * const output_buffe
}

/* reserve appropriate space in the output */
output_pointer = ensure(output_buffer, (size_t)length + sizeof(""));
output_pointer = ensure(output_buffer, (size_t)length + 1);
if (output_pointer == NULL) {
return false;
}
Expand Down Expand Up @@ -870,7 +870,7 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
return NULL;
}

if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) {
if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), c_utf8_bom, 3) == 0)) {
buffer->offset += 3;
}

Expand Down Expand Up @@ -1003,15 +1003,15 @@ static unsigned char *print(const J * const item, Jbool format)
N_CJSON_PUBLIC(char *) JPrint(const J *item)
{
if (item == NULL) {
return (char *)"";
return (char *)c_nullstring;
}
return (char*)print(item, true);
}

N_CJSON_PUBLIC(char *) JPrintUnformatted(const J *item)
{
if (item == NULL) {
return (char *)"";
return (char *)c_nullstring;
}
return (char*)print(item, false);
}
Expand All @@ -1021,7 +1021,7 @@ N_CJSON_PUBLIC(char *) JPrintBuffered(const J *item, int prebuffer, Jbool fmt)
printbuffer p = { 0, 0, 0, 0, 0, 0 };

if (item == NULL) {
return (char *)"";
return (char *)c_nullstring;
}

if (prebuffer < 0) {
Expand Down
20 changes: 10 additions & 10 deletions src/note-c/n_cjson_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ bool JBoolValue(J *item)
char *JStringValue(J *item)
{
if (item == NULL) {
return (char *)"";
return (char *)c_nullstring;
}
return item->valuestring;
}
Expand Down Expand Up @@ -392,7 +392,7 @@ bool JGetBinaryFromObject(J *rsp, const char *fieldName, uint8_t **retBinaryData
const char *JGetItemName(const J * item)
{
if (item == NULL || item->string == NULL) {
return "";
return c_nullstring;
}
return item->string;
}
Expand Down Expand Up @@ -494,25 +494,25 @@ char *JAllocString(uint8_t *buffer, uint32_t len)
const char *JType(J *item)
{
if (item == NULL) {
return "";
return c_nullstring;
}
switch (item->type & 0xff) {
case JTrue:
case JFalse:
return "bool";
return c_bool;
case JNULL:
return "null";
return c_null;
case JNumber:
return "number";
return c_number;
case JRaw:
case JString:
return "string";
return c_string;
case JObject:
return "object";
return c_object;
case JArray:
return "array";
return c_array;
}
return "invalid";
return c_invalid;
}

//**************************************************************************/
Expand Down
Loading