Adding automatic NFC start after save, and let Puck.js advertise on NFC 'out of the box'

This commit is contained in:
Gordon Williams 2016-10-31 10:37:51 +00:00
parent 2ee56b1570
commit 10a518113f
5 changed files with 62 additions and 16 deletions

View File

@ -483,6 +483,7 @@ OPTIMIZEFLAGS+=-O3
USE_BLUETOOTH=1
USE_NET=1
USE_GRAPHICS=1
USE_NFC=1
DEFINES += -DBOARD_PCA10040 -DPCA10040
# DFU_UPDATE_BUILD=1 # Uncomment this to build Espruino for a device firmware update over the air.
@ -494,10 +495,10 @@ OPTIMIZEFLAGS+=-O3
USE_BLUETOOTH=1
USE_NET=1
USE_GRAPHICS=1
#USE_HASHLIB=1
USE_HASHLIB=1
USE_FILESYSTEM=1
USE_CRYPTO=1
#USE_TLS=1
USE_TLS=1
USE_NFC=1
else ifdef ECU

View File

@ -83,6 +83,34 @@ void bleCompleteTaskFail(BleTask task, JsVar *data) {
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
/*JSON{
"type" : "init",
"generate" : "jswrap_nrf_init"
}*/
void jswrap_nrf_init() {
/* if (jsiStatus & JSIS_COMPLETELY_RESET) {
uint32_t addr0 = NRF_FICR->DEVICEADDR[0];
uint32_t addr1 = NRF_FICR->DEVICEADDR[1];
JsVar *uri = jsvVarPrintf("https://puck-js.com/go?mac=%02x:%02x:%02x:%02x:%02x:%02x",
((addr1>>8 )&0xFF)|0xC0,
((addr1 )&0xFF),
((addr0>>24)&0xFF),
((addr0>>16)&0xFF),
((addr0>>8 )&0xFF),
((addr0 )&0xFF));
jswrap_nrf_nfcURL(uri);
jsvUnLock(uri);
} else {
// start NFC, if it had been set
JsVar *flatStr = jsvObjectGetChild(execInfo.hiddenRoot, "NFC", 0);
if (flatStr) {
uint8_t *flatStrPtr = (uint8_t*)jsvGetFlatStringPointer(flatStr);
if (flatStrPtr) jsble_nfc_start(flatStrPtr, jsvGetLength(flatStr));
jsvUnLock(flatStr);
}
} */
}
/*JSON{
"type" : "idle",
"generate" : "jswrap_nrf_idle"
@ -96,6 +124,9 @@ bool jswrap_nrf_idle() {
"generate" : "jswrap_nrf_kill"
}*/
void jswrap_nrf_kill() {
// stop NFC emulation
jsble_nfc_stop(); // not a problem to call this if NFC isn't started
// stop any BLE tasks
bleTask = BLETASK_NONE;
if (blePromise) jsvUnLock(blePromise);
blePromise = 0;
@ -820,10 +851,21 @@ void jswrap_nrf_nfcURL(JsVar *url) {
if (!urlPtr || !urlLen)
return jsExceptionHere(JSET_ERROR, "Unable to get URL data");
nfc_uri_id_t uriType = NFC_URI_NONE;
if (memcmp(urlPtr, "http://", 7)==0) {
urlPtr+=7;
urlLen-=7;
uriType = NFC_URI_HTTP;
} else if (memcmp(urlPtr, "https://", 8)==0) {
urlPtr+=8;
urlLen-=8;
uriType = NFC_URI_HTTPS;
}
uint8_t msg_buf[256];
uint32_t len = sizeof(msg_buf);
/* Encode URI message into buffer */
err_code = nfc_uri_msg_encode( NFC_URI_NONE, // TODO: could auto-prepend http/etc.
err_code = nfc_uri_msg_encode( uriType, // TODO: could auto-prepend http/etc.
(uint8_t *)urlPtr,
urlLen,
msg_buf,

View File

@ -29,6 +29,7 @@ bool bleNewTask(BleTask task);
void bleCompleteTaskSuccess(BleTask task, JsVar *data);
void bleCompleteTaskFail(BleTask task, JsVar *data);
// ------------------------------------------------------------------------------
void jswrap_nrf_init();
bool jswrap_nrf_idle();
void jswrap_nrf_kill();
// ------------------------------------------------------------------------------

View File

@ -738,6 +738,8 @@ void jsiSoftKill() {
jsvObjectSetChild(execInfo.hiddenRoot, JSI_INIT_CODE_NAME, initCode);
jsvUnLock(initCode);
}
// If we're here we're loading, saving or resetting - board is no longer at power-on state
jsiStatus &= ~JSIS_COMPLETELY_RESET; // loading code, remove this flag
}
void jsiSemiInit(bool autoLoad) {
@ -753,6 +755,7 @@ void jsiSemiInit(bool autoLoad) {
Try and load from it... */
bool loadFlash = autoLoad && jsfFlashContainsCode();
if (loadFlash) {
jsiStatus &= ~JSIS_COMPLETELY_RESET; // loading code, remove this flag
jspSoftKill();
jsvSoftKill();
jsfLoadStateFromFlash();
@ -808,7 +811,7 @@ void jsiSemiInit(bool autoLoad) {
// The 'proper' init function - this should be called only once at bootup
void jsiInit(bool autoLoad) {
jsiStatus = JSIS_NONE;
jsiStatus = JSIS_COMPLETELY_RESET;
#if defined(LINUX) || !defined(USB)
consoleDevice = DEFAULT_CONSOLE_DEVICE;
@ -2031,7 +2034,6 @@ void jsiIdle() {
}
if ((s&JSIS_TODO_FLASH_SAVE) == JSIS_TODO_FLASH_SAVE) {
jsiStatus &= (JsiStatus)~JSIS_TODO_FLASH_SAVE;
jsvGarbageCollect(); // nice to have everything all tidy!
jsiSoftKill();
jspSoftKill();
@ -2044,7 +2046,6 @@ void jsiIdle() {
}
if ((s&JSIS_TODO_FLASH_LOAD) == JSIS_TODO_FLASH_LOAD) {
jsiStatus &= (JsiStatus)~JSIS_TODO_FLASH_LOAD;
jsiSoftKill();
jspSoftKill();
jsvSoftKill();

View File

@ -129,20 +129,21 @@ void jsiSetSleep(JsiSleepType isSleep);
typedef enum {
JSIS_NONE,
JSIS_ECHO_OFF = 1, ///< do we provide any user feedback? OFF=no
JSIS_ECHO_OFF_FOR_LINE = 2,
JSIS_ALLOW_DEEP_SLEEP = 4, // can we go into proper deep sleep?
JSIS_ECHO_OFF_FOR_LINE = 2, ///< Echo is off just for one line, then back on
JSIS_ALLOW_DEEP_SLEEP = 4, ///< can we go into proper deep sleep?
JSIS_TIMERS_CHANGED = 8,
#ifdef USE_DEBUGGER
JSIS_IN_DEBUGGER = 16, // We're inside the debug loop
JSIS_EXIT_DEBUGGER = 32, // we've been asked to exit the debug loop
JSIS_IN_DEBUGGER = 16, ///< We're inside the debug loop
JSIS_EXIT_DEBUGGER = 32, ///< we've been asked to exit the debug loop
#endif
JSIS_TODO_FLASH_SAVE = 64, // save to flash
JSIS_TODO_FLASH_LOAD = 128, // load from flash
JSIS_TODO_RESET = 256, // reset the board, don't load anything
JSIS_TODO_FLASH_SAVE = 64, ///< save to flash
JSIS_TODO_FLASH_LOAD = 128, ///< load from flash
JSIS_TODO_RESET = 256, ///< reset the board, don't load anything
JSIS_TODO_MASK = JSIS_TODO_FLASH_SAVE|JSIS_TODO_FLASH_LOAD|JSIS_TODO_RESET,
JSIS_CONSOLE_FORCED = 512, // see jsiSetConsoleDevice
JSIS_WATCHDOG_AUTO = 1024, // Automatically kick the watchdog timer on idle
JSIS_PASSWORD_PROTECTED = 2048, // Password protected
JSIS_CONSOLE_FORCED = 512, ///< see jsiSetConsoleDevice
JSIS_WATCHDOG_AUTO = 1024, ///< Automatically kick the watchdog timer on idle
JSIS_PASSWORD_PROTECTED = 2048, ///< Password protected
JSIS_COMPLETELY_RESET = 4096, ///< Has the board powered on, having not loaded anything from flash
JSIS_ECHO_OFF_MASK = JSIS_ECHO_OFF|JSIS_ECHO_OFF_FOR_LINE,
JSIS_SOFTINIT_MASK = JSIS_PASSWORD_PROTECTED // stuff that DOESN'T get reset on softinit