/*JSON{ "type" : "method", "class" : "I2C", "name" : "readFrom", "generate" : "jswrap_i2c_readFrom", "params" : [ ["address","JsVar","The 7 bit address of the device to request bytes from, or an object of the form `{address:12, stop:false}` to send this data without a STOP signal."], ["quantity","int32","The number of bytes to request"] ], "return" : ["JsVar","The data that was returned - as a Uint8Array"], "return_object" : "Uint8Array" } Request bytes from the given slave device, and return them as a Uint8Array (packed array of bytes). This is like using Arduino Wire's requestFrom, available and read functions. Sends a STOP */ JsVar *jswrap_i2c_readFrom(JsVar *parent, JsVar *addressVar, int nBytes) { IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_I2C(device)) return 0; bool sendStop = true; int address = i2c_get_address(addressVar, &sendStop); if (nBytes<=0) return 0; if ((unsigned int)nBytes+256 > jsuGetFreeStack()) { jsExceptionHere(JSET_ERROR, "Not enough free stack to receive this amount of data"); return 0; } unsigned char *buf = (unsigned char *)alloca((size_t)nBytes); jshI2CRead(device, (unsigned char)address, nBytes, buf, sendStop); JsVar *array = jsvNewTypedArray(ARRAYBUFFERVIEW_UINT8, nBytes); if (array) { JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, array, 0); unsigned int i; for (i=0;i<(unsigned)nBytes;i++) { jsvArrayBufferIteratorSetByteValue(&it, (char)buf[i]); jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); } return array; }
/*JSON{ "type":"method", "class": "Serial", "name" : "setup", "description" : ["Setup this Serial port with the given baud rate and options.", "If not specified in options, the default pins are used (usually the lowest numbered pins on the lowest port that supports this peripheral)"], "generate" : "jswrap_serial_setup", "params" : [ [ "baudrate", "JsVar", "The baud rate - the default is 9600"], [ "options", "JsVar", ["An optional structure containing extra information on initialising the serial port.", "```{rx:pin,tx:pin,bytesize:8,parity:null/'none'/'o'/'odd'/'e'/'even',stopbits:1}```", "You can find out which pins to use by looking at [your board's reference page](#boards) and searching for pins with the `UART`/`USART` markers.", "Note that even after changing the RX and TX pins, if you have called setup before then the previous RX and TX pins will still be connected to the Serial port as well - until you set them to something else using digitalWrite" ] ] ] }*/ void jswrap_serial_setup(JsVar *parent, JsVar *baud, JsVar *options) { IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_USART(device)) return; JshUSARTInfo inf; jshUSARTInitInfo(&inf); if (!jsvIsUndefined(baud)) { int b = (int)jsvGetInteger(baud); if (b<=100 || b > 10000000) jsExceptionHere(JSET_ERROR, "Invalid baud rate specified"); else inf.baudRate = b; } if (jsvIsObject(options)) { inf.pinRX = jshGetPinFromVarAndUnLock(jsvObjectGetChild(options, "rx", 0)); inf.pinTX = jshGetPinFromVarAndUnLock(jsvObjectGetChild(options, "tx", 0)); JsVar *v; v = jsvObjectGetChild(options, "bytesize", 0); if (jsvIsInt(v)) inf.bytesize = (unsigned char)jsvGetInteger(v); jsvUnLock(v); inf.parity = 0; v = jsvObjectGetChild(options, "parity", 0); if(jsvIsString(v)) { if(jsvIsStringEqual(v, "o") || jsvIsStringEqual(v, "odd")) inf.parity = 1; else if(jsvIsStringEqual(v, "e") || jsvIsStringEqual(v, "even")) inf.parity = 2; } else if(jsvIsInt(v)) { inf.parity = (unsigned char)jsvGetInteger(v); } jsvUnLock(v); if (inf.parity>2) { jsExceptionHere(JSET_ERROR, "Invalid parity %d", inf.parity); return; } v = jsvObjectGetChild(options, "stopbits", 0); if (jsvIsInt(v)) inf.stopbits = (unsigned char)jsvGetInteger(v); jsvUnLock(v); } jshUSARTSetup(device, &inf); // Set baud rate in object, so we can initialise it on startup if (inf.baudRate != DEFAULT_BAUD_RATE) { jsvUnLock(jsvObjectSetChild(parent, USART_BAUDRATE_NAME, jsvNewFromInteger(inf.baudRate))); } else jsvRemoveNamedChild(parent, USART_BAUDRATE_NAME); // Do the same for options if (options) jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME)); else jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME); }
void jswrap_serial_write(JsVar *parent, JsVar *args) { NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_USART(device)) return; jsvIterateCallback(args, jswrap_serial_write_cb, (void*)&device); }
/*JSON{ "type" : "method", "class" : "SPI", "name" : "setup", "generate" : "jswrap_spi_setup", "params" : [ ["options","JsVar",["An optional structure containing extra information on initialising the SPI port","Please note that baud rate is set to the nearest that can be managed - which may be -+ 50%","```{sck:pin, miso:pin, mosi:pin, baud:integer=100000, mode:integer=0, order:'msb'/'lsb'='msb' }```","If sck,miso and mosi are left out, they will automatically be chosen. However if one or more is specified then the unspecified pins will not be set up.","You can find out which pins to use by looking at [your board's reference page](#boards) and searching for pins with the `SPI` marker.","The SPI ```mode``` is between 0 and 3 - see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Clock_polarity_and_phase","On STM32F1-based parts, you cannot mix AF and non-AF pins (SPI pins are usually grouped on the chip - and you can't mix pins from two groups). Espruino will not warn you about this."]] ] } Set up this SPI port as an SPI Master. */ void jswrap_spi_setup(JsVar *parent, JsVar *options) { IOEventFlags device = jsiGetDeviceFromClass(parent); JshSPIInfo inf; jsspiPopulateSPIInfo(&inf, options); if (DEVICE_IS_SPI(device)) { jshSPISetup(device, &inf); #ifdef LINUX if (jsvIsObject(options)) { jsvUnLock(jsvObjectSetChild(parent, "path", jsvObjectGetChild(options, "path", 0))); } #endif } else if (device == EV_NONE) { // software mode - at least configure pins properly if (inf.pinSCK != PIN_UNDEFINED) jshPinSetState(inf.pinSCK, JSHPINSTATE_GPIO_OUT); if (inf.pinMISO != PIN_UNDEFINED) jshPinSetState(inf.pinMISO, JSHPINSTATE_GPIO_IN); if (inf.pinMOSI != PIN_UNDEFINED) jshPinSetState(inf.pinMOSI, JSHPINSTATE_GPIO_OUT); } else return; // Set up options, so we can initialise it on startup if (options) jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME)); else jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME); }
/*JSON{ "type" : "method", "class" : "SPI", "name" : "write", "generate" : "jswrap_spi_write", "params" : [ ["data","JsVarArray",["One or more items to write. May be ints, strings, arrays, or objects of the form `{data: ..., count:#}`.","If the last argument is a pin, it is taken to be the NSS pin"]] ] } Write a character or array of characters to SPI - without reading the result back. For maximum speeds, please pass either Strings or Typed Arrays as arguments. */ void jswrap_spi_write(JsVar *parent, JsVar *args) { NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); spi_sender spiSend; spi_sender_data spiSendData; if (!jsspiGetSendFunction(parent, &spiSend, &spiSendData)) return; Pin nss_pin = PIN_UNDEFINED; // If the last value is a pin, use it as the NSS pin JsVarInt len = jsvGetArrayLength(args); if (len>0) { JsVar *last = jsvGetArrayItem(args, len-1); // look at the last value if (jsvIsPin(last)) { nss_pin = jshGetPinFromVar(last); jsvUnLock(jsvArrayPop(args)); } jsvUnLock(last); } // we're only sending (no receive) if (DEVICE_IS_SPI(device)) jshSPISetReceive(device, false); // assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, false); // Write data jsvIterateCallback(args, (void (*)(int, void *))spiSend, &spiSendData); // Wait until SPI send is finished, and flush data if (DEVICE_IS_SPI(device)) jshSPIWait(device); // de-assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, true); }
/*JSON{ "type":"method", "class": "Serial", "name" : "setup", "description" : "Setup this Serial port with the given baud rate and options", "generate" : "jswrap_serial_setup", "params" : [ [ "baudrate", "int", "The baud rate - the default is 9600"], [ "options", "JsVar", ["An optional structure containing extra information on initialising the serial port.", "```{rx:pin,tx:pin}```", "Note that even after changing the RX and TX pins, if you have called setup before then the previous RX and TX pins will still be connected to the Serial port as well - until you set them to something else using digitalWrite" ] ] ] }*/ void jswrap_serial_setup(JsVar *parent, JsVarInt baud, JsVar *options) { IOEventFlags device = jsiGetDeviceFromClass(parent); JshUSARTInfo inf; jshUSARTInitInfo(&inf); if (baud>0) inf.baudRate = (int)baud; if (jsvIsObject(options)) { inf.pinRX = jshGetPinFromVarAndUnLock(jsvObjectGetChild(options, "rx", 0)); inf.pinTX = jshGetPinFromVarAndUnLock(jsvObjectGetChild(options, "tx", 0)); inf.bytesize = (unsigned char)jsvGetIntegerAndUnLock(jsvObjectGetChild(options, "bytesize", 0)); JsVar *v; v = jsvObjectGetChild(options, "parity", 0); if(jsvIsNull(v)) { inf.parity = 0; } else if(jsvIsString(v)) { inf.parity = 0xFF; char s[8] = ""; jsvGetString(v, s, sizeof(s) - 1); if(!strcmp(s, "o") || !strcmp(s, "odd")) { inf.parity = 1; } else if(!strcmp(s, "e") || !strcmp(s, "even")) { inf.parity = 2; } } else if(jsvIsInt(v)) { inf.parity = (unsigned char)jsvGetInteger(v); } jsvUnLock(v); v = jsvObjectGetChild(options, "stopbits", 0); inf.stopbits = (unsigned char)jsvGetInteger(v); jsvUnLock(v); } jshUSARTSetup(device, &inf); // Set baud rate in object, so we can initialise it on startup if (baud != DEFAULT_BAUD_RATE) { JsVar *baudVar = jsvNewFromInteger(baud); jsvUnLock(jsvSetNamedChild(parent, baudVar, USART_BAUDRATE_NAME)); jsvUnLock(baudVar); } else jsvRemoveNamedChild(parent, USART_BAUDRATE_NAME); // Do the same for options if (options) jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME)); else jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME); }
/*JSON{ "type":"method", "class": "Serial", "name" : "println", "description" : "Print a line to the serial port (newline character sent are '\r\n')", "generate" : "jswrap_serial_println", "params" : [ [ "string", "JsVar", "A String to print"] ] }*/ void _jswrap_serial_print(JsVar *parent, JsVar *str, bool newLine) { NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); str = jsvAsString(str, false); jsiTransmitStringVar(device,str); jsvUnLock(str); if (newLine) { jshTransmit(device, (unsigned char)'\r'); jshTransmit(device, (unsigned char)'\n'); } }
/*JSON{ "type" : "method", "class" : "SPI", "name" : "send4bit", "generate" : "jswrap_spi_send4bit", "params" : [ ["data","JsVar","The data to send - either an integer, array, or string"], ["bit0","int32","The 4 bits to send for a 0 (MSB first)"], ["bit1","int32","The 4 bits to send for a 1 (MSB first)"], ["nss_pin","pin","An nSS pin - this will be lowered before SPI output and raised afterwards (optional). There will be a small delay between when this is lowered and when sending starts, and also between sending finishing and it being raised."] ] } Send data down SPI, using 4 bits for each 'real' bit (MSB first). This can be useful for faking one-wire style protocols Sending multiple bytes in one call to send is preferable as they can then be transmitted end to end. Using multiple calls to send() will result in significantly slower transmission speeds. */ void jswrap_spi_send4bit(JsVar *parent, JsVar *srcdata, int bit0, int bit1, Pin nss_pin) { NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_SPI(device)) { jsExceptionHere(JSET_ERROR, "SPI.send4bit only works on hardware SPI"); return; } jshSPISet16(device, true); // 16 bit output if (bit0==0 && bit1==0) { bit0 = 0x01; bit1 = 0x03; } bit0 = bit0 & 0x0F; bit1 = bit1 & 0x0F; if (!jshIsDeviceInitialised(device)) { JshSPIInfo inf; jshSPIInitInfo(&inf); jshSPISetup(device, &inf); } // we're just sending (no receive) jshSPISetReceive(device, false); // assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, false); // send data if (jsvIsNumeric(srcdata)) { jsspiSend4bit(device, (unsigned char)jsvGetInteger(srcdata), bit0, bit1); } else if (jsvIsIterable(srcdata)) { jshInterruptOff(); JsvIterator it; jsvIteratorNew(&it, srcdata); while (jsvIteratorHasElement(&it)) { unsigned char in = (unsigned char)jsvIteratorGetIntegerValue(&it); jsspiSend4bit(device, in, bit0, bit1); jsvIteratorNext(&it); } jsvIteratorFree(&it); jshInterruptOn(); } else { jsExceptionHere(JSET_ERROR, "Variable type %t not suited to transmit operation", srcdata); } jshSPIWait(device); // wait until SPI send finished and clear the RX buffer // de-assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, true); jshSPISet16(device, false); // back to 8 bit }
void _jswrap_serial_print(JsVar *parent, JsVar *arg, bool isPrint, bool newLine) { NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_USART(device)) return; if (isPrint) arg = jsvAsString(arg, false); jsvIterateCallback(arg, _jswrap_serial_print_cb, (void*)&device); if (isPrint) jsvUnLock(arg); if (newLine) { _jswrap_serial_print_cb((unsigned char)'\r', (void*)&device); _jswrap_serial_print_cb((unsigned char)'\n', (void*)&device); } }
/*JSON{ "type":"method", "class": "Serial", "name" : "write", "description" : "Write a character or array of characters to the serial port - without a line feed", "generate" : "jswrap_serial_write", "params" : [ [ "data", "JsVar", "A byte, a string, or an array of bytes to write"] ] }*/ void jswrap_serial_write(JsVar *parent, JsVar *data) { NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); if (jsvIsNumeric(data)) { jshTransmit(device, (unsigned char)jsvGetInteger(data)); } else if (jsvIsIterable(data)) { JsvIterator it; jsvIteratorNew(&it, data); while (jsvIteratorHasElement(&it)) { jshTransmit(device, (unsigned char)jsvIteratorGetIntegerValue(&it)); jsvIteratorNext(&it); } jsvIteratorFree(&it); } else { jsWarn("Data supplied was not an integer - or iterable"); } }
void jswrap_i2c_writeTo(JsVar *parent, JsVar *addressVar, JsVar *args) { IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_I2C(device)) return; bool sendStop = true; int address = i2c_get_address(addressVar, &sendStop); size_t l = (size_t)jsvIterateCallbackCount(args); if (l+256 > jsuGetFreeStack()) { jsExceptionHere(JSET_ERROR, "Not enough free stack to send this amount of data"); return; } unsigned char *data = (unsigned char *)alloca(l); jsvIterateCallbackToBytes(args, data, l); jshI2CWrite(device, (unsigned char)address, l, data, sendStop); }
void doMeasurement() { enableDASH(); JsVar *i2c1 = jspGetNamedField(execInfo.root, "I2C1", false); IOEventFlags device = jsiGetDeviceFromClass(i2c1); if (!DEVICE_IS_I2C(device)) { blink(PIN_RED, 100); return; } jsvUnLock(i2c1); unsigned char dataPtr[2]; dataPtr[0] = CMD_MEASURE_TEMPERATURE_HOLD; jshI2CWrite(device, (unsigned char)(CMD_TEMP_ADDR), 1, (unsigned char*)dataPtr, true); int i = 0; for (i=0; i<2; i++) { dataPtr[i] = 0; } jshI2CRead(device, (unsigned char)CMD_TEMP_ADDR, 2, (unsigned char*)dataPtr, true); int temp = ((dataPtr[0] * 256) + dataPtr[1]); /// http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7050-1-3-4-5-A20.pdf#page=14 JsVarFloat realTemp = ((175.72f * temp) / 65536.0f) - 46.85f; char temperatureBuffer[6] = {0}; ftoa_bounded(realTemp, temperatureBuffer, 5); temperatureBuffer[5] = '\0'; char serialNumber[SERIAL_LEN] = {0}; getSerial(serialNumber, SERIAL_LEN); /// Generate the JSON that we send to gateway skyand then to API{\"nodeSerial\":\"%s\",\"channel\":\"temp\",\"value\":\"%s\",\"metric\":\"c\"}');", serialNumber + SERIAL_OFFSE jsvUnLock(messageVar); if (doTimeout) { //regular message messageVar = jsvVarPrintf("{\"ns\":\"%s\",\"ch\":\"t\",\"v\":\"%s\",\"m\":\"c\"}", serialNumber + SERIAL_OFFSET, temperatureBuffer); } else { //this only happens if we forced the read messageVar = jsvVarPrintf("{\"ns\":\"%s\",\"ch\":\"t\",\"v\":\"%s\",\"m\":\"c\",\"f\":\"y\"}", serialNumber + SERIAL_OFFSET, temperatureBuffer); } WDEBUG("sending on DASH: "); WDEBUGSTRVAR(messageVar); WDEBUGLN(""); JsVar *serial = jspGetNamedField(execInfo.root, SERIAL1_DASH7, false); jswrap_serial_print(serial, messageVar); jsvUnLock(serial); transmittingData = true; // see jswrap_wice_idle }
/*JSON{ "type" : "method", "class" : "I2C", "name" : "setup", "generate" : "jswrap_i2c_setup", "params" : [ ["options","JsVar",["An optional structure containing extra information on initialising the I2C port","```{scl:pin, sda:pin, bitrate:100000}```","You can find out which pins to use by looking at [your board's reference page](#boards) and searching for pins with the `I2C` marker. Note that 400000kHz is the maximum bitrate for most parts."]] ] } Set up this I2C port If not specified in options, the default pins are used (usually the lowest numbered pins on the lowest port that supports this peripheral) */ void jswrap_i2c_setup(JsVar *parent, JsVar *options) { IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_I2C(device)) return; JshI2CInfo inf; jshI2CInitInfo(&inf); if (jsvIsObject(options)) { inf.pinSCL = jshGetPinFromVarAndUnLock(jsvObjectGetChild(options, "scl", 0)); inf.pinSDA = jshGetPinFromVarAndUnLock(jsvObjectGetChild(options, "sda", 0)); JsVar *v = jsvObjectGetChild(options, "bitrate", 0); if (v) inf.bitrate = jsvGetIntegerAndUnLock(v); } jshI2CSetup(device, &inf); // Set up options, so we can initialise it on startup if (options) jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME)); else jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME); }
/*JSON{ "type" : "staticmethod", "class" : "CC3000", "name" : "connect", "generate" : "jswrap_cc3000_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default). SPI should be 1,000,000 baud, and set to 'mode 1'"], ["cs", "pin", "The pin to use for Chip Select"], ["en", "pin", "The pin to use for Enable"], ["irq", "pin", "The pin to use for Interrupts"] ], "return" : ["JsVar","A WLAN Object"], "return_object" : "WLAN" } Initialise the CC3000 and return a WLAN object */ JsVar *jswrap_cc3000_connect(JsVar *spi, Pin cs, Pin en, Pin irq) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = WLAN_CLK_PIN; inf.pinMISO = WLAN_MISO_PIN; inf.pinMOSI = WLAN_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_1; // Mode 1 CPOL= 0 CPHA= 1 jshSPISetup(WLAN_SPI, &inf); spiDevice = WLAN_SPI; } if (!jshIsPinValid(cs)) cs = WLAN_CS_PIN; if (!jshIsPinValid(en)) en = WLAN_EN_PIN; if (!jshIsPinValid(irq)) irq = WLAN_IRQ_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_CC3000); net.data.device = spiDevice; net.data.pinCS = cs; net.data.pinEN = en; net.data.pinIRQ = irq; networkSet(&net); JsVar *wlanObj = jspNewObject(0, "WLAN"); cc3000_initialise(wlanObj); networkFree(&net); return wlanObj; }
/*JSON{ "type" : "method", "class" : "SPI", "name" : "setup", "generate" : "jswrap_spi_setup", "params" : [ ["options","JsVar",["An optional structure containing extra information on initialising the SPI port","Please note that baud rate is set to the nearest that can be managed - which may be -+ 50%","```{sck:pin, miso:pin, mosi:pin, baud:integer=100000, mode:integer=0, order:'msb'/'lsb'='msb' }```","If sck,miso and mosi are left out, they will automatically be chosen. However if one or more is specified then the unspecified pins will not be set up.","You can find out which pins to use by looking at [your board's reference page](#boards) and searching for pins with the `SPI` marker.","The SPI ```mode``` is between 0 and 3 - see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Clock_polarity_and_phase","On STM32F1-based parts, you cannot mix AF and non-AF pins (SPI pins are usually grouped on the chip - and you can't mix pins from two groups). Espruino will not warn you about this."]] ] } Set up this SPI port as an SPI Master. */ void jswrap_spi_setup( JsVar *parent, //!< The variable that is the class instance of this function. JsVar *options //!< The options controlling SPI. ) { // // Design: The options variable is a JS Object which contains a series of settings. These // settings are parsed by `jsspiPopulateSPIInfo` to populate a C structure of type // `JshSPIInfo`. // // The options are also hung off the class instance variable in a property symbolically called // DEVICE_OPTIONS_NAME ("_options"). // IOEventFlags device = jsiGetDeviceFromClass(parent); JshSPIInfo inf; // Debug // jsiConsolePrintf("jswrap_spi_setup called parent=%v, options=%v\n", parent, options); jsspiPopulateSPIInfo(&inf, options); if (DEVICE_IS_SPI(device)) { jshSPISetup(device, &inf); #ifdef LINUX if (jsvIsObject(options)) { jsvObjectSetChildAndUnLock(parent, "path", jsvObjectGetChild(options, "path", 0)); } #endif } else if (device == EV_NONE) { // software mode - at least configure pins properly if (inf.pinSCK != PIN_UNDEFINED) jshPinSetState(inf.pinSCK, JSHPINSTATE_GPIO_OUT); if (inf.pinMISO != PIN_UNDEFINED) jshPinSetState(inf.pinMISO, JSHPINSTATE_GPIO_IN); if (inf.pinMOSI != PIN_UNDEFINED) jshPinSetState(inf.pinMOSI, JSHPINSTATE_GPIO_OUT); } else return; // Set up options, so we can initialise it on startup if (options) jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME)); else jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME); }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266", "name" : "connect", "generate" : "jswrap_esp8266_connect_device", "params" : [ ["serial","JsVar","The Serial port used for communications with the ESP8266 (must already be setup)"], ["callback","JsVar","Function to call back when connected"] ], "return" : ["JsVar","An ESP8266 Object"], "return_object" : "ESP8266" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_esp8266_connect_device(JsVar *usart, JsVar *callback) { IOEventFlags usartDevice; usartDevice = jsiGetDeviceFromClass(usart); if (!DEVICE_IS_USART(usartDevice)) { jsExceptionHere(JSET_ERROR, "Expecting USART device, got %q", usart); return 0; } JsNetwork net; networkCreate(&net, JSNETWORKTYPE_ESP8266); net.data.device = usartDevice; networkSet(&net); JsVar *wifiObj = 0; JsVar *cmd = jsvNewFromString("AT+RST\r\n"); esp8266_send(cmd); jsvUnLock(cmd); if (esp8266_wait_for("OK", 100, false)) { if (esp8266_wait_for("ready", 4000, false)) { networkState = NETWORKSTATE_ONLINE; wifiObj = jspNewObject(0, "ESPWifi"); } else { jsExceptionHere(JSET_ERROR, "Module not ready"); } } else { jsExceptionHere(JSET_ERROR, "No Acknowledgement"); } networkFree(&net); if (callback) jsiQueueEvents(callback, 0, 0); return wifiObj; }
void jsfGetJSONWithCallback(JsVar *var, JSONFlags flags, const char *whitespace, vcbprintf_callback user_callback, void *user_data) { JSONFlags nflags = flags + JSON_INDENT; // if we add a newline, make sure we indent any subsequent JSON more if (!whitespace) whitespace=" "; if (jsvIsUndefined(var)) { cbprintf(user_callback, user_data, "undefined"); } else { // Use IS_RECURSING flag to stop recursion if (var->flags & JSV_IS_RECURSING) { cbprintf(user_callback, user_data, " ... "); return; } var->flags |= JSV_IS_RECURSING; if (jsvIsArray(var)) { JsVarInt length = jsvGetArrayLength(var); bool limited = (flags&JSON_LIMIT) && (length>(JsVarInt)JSON_LIMIT_AMOUNT); bool needNewLine = false; cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"[ ":"["); JsVarInt lastIndex = -1; bool numeric = true; bool first = true; JsvObjectIterator it; jsvObjectIteratorNew(&it, var); while (lastIndex+1<length && numeric && !jspIsInterrupted()) { JsVar *key = jsvObjectIteratorGetKey(&it); if (!jsvObjectIteratorHasValue(&it) || jsvIsNumeric(key)) { JsVarInt index = jsvObjectIteratorHasValue(&it) ? jsvGetInteger(key) : length-1; JsVar *item = jsvObjectIteratorGetValue(&it); while (lastIndex < index) { lastIndex++; if (!limited || lastIndex<(JsVarInt)JSON_LIMITED_AMOUNT || lastIndex>=length-(JsVarInt)JSON_LIMITED_AMOUNT) { if (!first) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); first = false; if (limited && lastIndex==length-(JsVarInt)JSON_LIMITED_AMOUNT) cbprintf(user_callback, user_data, JSON_LIMIT_TEXT); bool newNeedsNewLine = ((flags&JSON_SOME_NEWLINES) && jsonNeedsNewLine(item)); if (flags&JSON_ALL_NEWLINES) { needNewLine = true; newNeedsNewLine = true; } if (needNewLine || newNeedsNewLine) { jsonNewLine(nflags, whitespace, user_callback, user_data); needNewLine = false; } if (lastIndex == index) jsfGetJSONWithCallback(item, nflags, whitespace, user_callback, user_data); else cbprintf(user_callback, user_data, (flags&JSON_NO_UNDEFINED)?"null":"undefined"); needNewLine = newNeedsNewLine; } } jsvUnLock(item); jsvObjectIteratorNext(&it); } else { numeric = false; } jsvUnLock(key); } // non-numeric - but NOT for standard JSON if ((flags&JSON_PRETTY)) jsfGetJSONForObjectItWithCallback(&it, flags, whitespace, nflags, user_callback, user_data, first); jsvObjectIteratorFree(&it); if (needNewLine) jsonNewLine(flags, whitespace, user_callback, user_data); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?" ]":"]"); } else if (jsvIsArrayBuffer(var)) { JsvArrayBufferIterator it; bool allZero = true; jsvArrayBufferIteratorNew(&it, var, 0); while (jsvArrayBufferIteratorHasElement(&it)) { if (jsvArrayBufferIteratorGetFloatValue(&it)!=0) allZero = false; jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); bool asArray = flags&JSON_ARRAYBUFFER_AS_ARRAY; if (allZero && !asArray) { cbprintf(user_callback, user_data, "new %s(%d)", jswGetBasicObjectName(var), jsvGetArrayBufferLength(var)); } else { const char *aname = jswGetBasicObjectName(var); /* You can't do `new ArrayBuffer([1,2,3])` so we have to output * `new Uint8Array([1,2,3]).buffer`! */ bool isBasicArrayBuffer = strcmp(aname,"ArrayBuffer")==0; if (isBasicArrayBuffer) { aname="Uint8Array"; } cbprintf(user_callback, user_data, asArray?"[":"new %s([", aname); if (flags&JSON_ALL_NEWLINES) jsonNewLine(nflags, whitespace, user_callback, user_data); size_t length = jsvGetArrayBufferLength(var); bool limited = (flags&JSON_LIMIT) && (length>JSON_LIMIT_AMOUNT); // no newlines needed for array buffers as they only contain simple stuff jsvArrayBufferIteratorNew(&it, var, 0); while (jsvArrayBufferIteratorHasElement(&it) && !jspIsInterrupted()) { if (!limited || it.index<JSON_LIMITED_AMOUNT || it.index>=length-JSON_LIMITED_AMOUNT) { if (it.index>0) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); if (flags&JSON_ALL_NEWLINES) jsonNewLine(nflags, whitespace, user_callback, user_data); if (limited && it.index==length-JSON_LIMITED_AMOUNT) cbprintf(user_callback, user_data, JSON_LIMIT_TEXT); JsVar *item = jsvArrayBufferIteratorGetValue(&it); jsfGetJSONWithCallback(item, nflags, whitespace, user_callback, user_data); jsvUnLock(item); } jsvArrayBufferIteratorNext(&it); } if (flags&JSON_ALL_NEWLINES) jsonNewLine(flags, whitespace, user_callback, user_data); jsvArrayBufferIteratorFree(&it); cbprintf(user_callback, user_data, asArray?"]":"])"); if (isBasicArrayBuffer && !asArray) cbprintf(user_callback, user_data, ".buffer"); } } else if (jsvIsObject(var)) { IOEventFlags device = (flags & JSON_SHOW_DEVICES) ? jsiGetDeviceFromClass(var) : EV_NONE; if (device!=EV_NONE) { cbprintf(user_callback, user_data, "%s", jshGetDeviceString(device)); } else { bool showContents = true; if (flags & JSON_SHOW_OBJECT_NAMES) { JsVar *proto = jsvObjectGetChild(var, JSPARSE_INHERITS_VAR, 0); if (jsvHasChildren(proto)) { JsVar *constr = jsvObjectGetChild(proto, JSPARSE_CONSTRUCTOR_VAR, 0); if (constr) { JsVar *p = jsvGetIndexOf(execInfo.root, constr, true); if (p) cbprintf(user_callback, user_data, "%v: ", p); jsvUnLock2(p,constr); /* We had the constructor - now if there was a non-default toString function * we'll execute it and print the result */ JsVar *toStringFn = jspGetNamedField(var, "toString", false); if (toStringFn && toStringFn->varData.native.ptr != (void (*)(void))jswrap_object_toString) { // Function found and it's not the default one - execute it JsVar *result = jspExecuteFunction(toStringFn,var,0,0); cbprintf(user_callback, user_data, "%v", result); jsvUnLock(result); showContents = false; // we already printed something } jsvUnLock(toStringFn); } } jsvUnLock(proto); } if (showContents) { JsvObjectIterator it; jsvObjectIteratorNew(&it, var); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"{ ":"{"); bool needNewLine = jsfGetJSONForObjectItWithCallback(&it, flags, whitespace, nflags, user_callback, user_data, true); jsvObjectIteratorFree(&it); if (needNewLine) jsonNewLine(flags, whitespace, user_callback, user_data); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?" }":"}"); } } } else if (jsvIsFunction(var)) { if (flags & JSON_IGNORE_FUNCTIONS) { cbprintf(user_callback, user_data, "undefined"); } else { cbprintf(user_callback, user_data, "function "); jsfGetJSONForFunctionWithCallback(var, nflags, user_callback, user_data); } } else if (jsvIsString(var) && !jsvIsName(var)) { if ((flags&JSON_LIMIT) && jsvGetStringLength(var)>JSON_LIMIT_STRING_AMOUNT) { // if the string is too big, split it and put dots in the middle JsVar *var1 = jsvNewFromStringVar(var, 0, JSON_LIMITED_STRING_AMOUNT); JsVar *var2 = jsvNewFromStringVar(var, jsvGetStringLength(var)-JSON_LIMITED_STRING_AMOUNT, JSON_LIMITED_STRING_AMOUNT); cbprintf(user_callback, user_data, "%q%s%q", var1, JSON_LIMIT_TEXT, var2); jsvUnLock2(var1, var2); } else { cbprintf(user_callback, user_data, "%q", var); } } else { cbprintf(user_callback, user_data, "%v", var); } var->flags &= ~JSV_IS_RECURSING; } }
void jsfGetJSONWithCallback(JsVar *var, JSONFlags flags, vcbprintf_callback user_callback, void *user_data) { JSONFlags nflags = flags + JSON_INDENT; // if we add a newline, make sure we indent any subsequent JSON more if (jsvIsUndefined(var)) { cbprintf(user_callback, user_data, "undefined"); } else { // Use IS_RECURSING flag to stop recursion if (var->flags & JSV_IS_RECURSING) { cbprintf(user_callback, user_data, " ... "); return; } var->flags |= JSV_IS_RECURSING; if (jsvIsArray(var)) { size_t length = (size_t)jsvGetArrayLength(var); bool limited = (flags&JSON_LIMIT) && (length>JSON_LIMIT_AMOUNT); bool needNewLine = false; size_t i; cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"[ ":"["); for (i=0;i<length && !jspIsInterrupted();i++) { if (!limited || i<JSON_LIMITED_AMOUNT || i>=length-JSON_LIMITED_AMOUNT) { if (i>0) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); if (limited && i==length-JSON_LIMITED_AMOUNT) { if (needNewLine) jsonNewLine(nflags, user_callback, user_data); cbprintf(user_callback, user_data, JSON_LIMIT_TEXT); } JsVar *item = jsvGetArrayItem(var, (JsVarInt)i); if (jsvIsUndefined(item) && (flags&JSON_NO_UNDEFINED)) item = jsvNewWithFlags(JSV_NULL); bool newNeedsNewLine = (flags&JSON_NEWLINES) && jsonNeedsNewLine(item); if (needNewLine || newNeedsNewLine) { jsonNewLine(nflags, user_callback, user_data); needNewLine = false; } jsfGetJSONWithCallback(item, nflags, user_callback, user_data); needNewLine = newNeedsNewLine; jsvUnLock(item); } } if (needNewLine) jsonNewLine(flags, user_callback, user_data); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?" ]":"]"); } else if (jsvIsArrayBuffer(var)) { JsvArrayBufferIterator it; bool allZero = true; jsvArrayBufferIteratorNew(&it, var, 0); while (jsvArrayBufferIteratorHasElement(&it)) { if (jsvArrayBufferIteratorGetFloatValue(&it)!=0) allZero = false; jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); if (allZero) { cbprintf(user_callback, user_data, "new %s(%d)", jswGetBasicObjectName(var), jsvGetArrayBufferLength(var)); } else { cbprintf(user_callback, user_data, "new %s([", jswGetBasicObjectName(var)); size_t length = jsvGetArrayBufferLength(var); bool limited = (flags&JSON_LIMIT) && (length>JSON_LIMIT_AMOUNT); // no newlines needed for array buffers as they only contain simple stuff jsvArrayBufferIteratorNew(&it, var, 0); while (jsvArrayBufferIteratorHasElement(&it) && !jspIsInterrupted()) { if (!limited || it.index<JSON_LIMITED_AMOUNT || it.index>=length-JSON_LIMITED_AMOUNT) { if (it.index>0) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); if (limited && it.index==length-JSON_LIMITED_AMOUNT) cbprintf(user_callback, user_data, JSON_LIMIT_TEXT); JsVar *item = jsvArrayBufferIteratorGetValue(&it); jsfGetJSONWithCallback(item, nflags, user_callback, user_data); jsvUnLock(item); } jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); cbprintf(user_callback, user_data, "])"); } } else if (jsvIsObject(var)) { IOEventFlags device = (flags & JSON_SHOW_DEVICES) ? jsiGetDeviceFromClass(var) : EV_NONE; if (device!=EV_NONE) { cbprintf(user_callback, user_data, "%s", jshGetDeviceString(device)); } else { bool first = true; bool needNewLine = false; size_t sinceNewLine = 0; JsvObjectIterator it; jsvObjectIteratorNew(&it, var); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"{ ":"{"); while (jsvObjectIteratorHasValue(&it) && !jspIsInterrupted()) { JsVar *index = jsvObjectIteratorGetKey(&it); JsVar *item = jsvObjectIteratorGetValue(&it); bool hidden = jsvIsInternalObjectKey(index) || ((flags & JSON_IGNORE_FUNCTIONS) && jsvIsFunction(item)) || ((flags&JSON_NO_UNDEFINED) && jsvIsUndefined(item)); if (!hidden) { sinceNewLine++; if (!first) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); bool newNeedsNewLine = (flags&JSON_NEWLINES) && jsonNeedsNewLine(item); if ((flags&JSON_NEWLINES) && sinceNewLine>JSON_ITEMS_ON_LINE_OBJECT) needNewLine = true; if (needNewLine || newNeedsNewLine) { jsonNewLine(nflags, user_callback, user_data); needNewLine = false; sinceNewLine = 0; } cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"%q: ":"%q:", index); if (first) first = false; jsfGetJSONWithCallback(item, nflags, user_callback, user_data); needNewLine = newNeedsNewLine; } jsvUnLock(index); jsvUnLock(item); jsvObjectIteratorNext(&it); } jsvObjectIteratorFree(&it); if (needNewLine) jsonNewLine(flags, user_callback, user_data); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?" }":"}"); } } else if (jsvIsFunction(var)) { if (flags & JSON_IGNORE_FUNCTIONS) { cbprintf(user_callback, user_data, "undefined"); } else { cbprintf(user_callback, user_data, "function "); jsfGetJSONForFunctionWithCallback(var, nflags, user_callback, user_data); } } else if (jsvIsString(var) && !jsvIsName(var)) { if ((flags&JSON_LIMIT) && jsvGetStringLength(var)>JSON_LIMIT_STRING_AMOUNT) { // if the string is too big, split it and put dots in the middle JsVar *var1 = jsvNewFromStringVar(var, 0, JSON_LIMITED_STRING_AMOUNT); JsVar *var2 = jsvNewFromStringVar(var, jsvGetStringLength(var)-JSON_LIMITED_STRING_AMOUNT, JSON_LIMITED_STRING_AMOUNT); cbprintf(user_callback, user_data, "%q%s%q", var1, JSON_LIMIT_TEXT, var2); jsvUnLock(var1); jsvUnLock(var2); } else { cbprintf(user_callback, user_data, "%q", var); } } else { cbprintf(user_callback, user_data, "%v", var); } var->flags &= ~JSV_IS_RECURSING; } }
JsVar *jswrap_spi_send(JsVar *parent, JsVar *srcdata, Pin nss_pin) { NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); jswrap_spi_send_data data; if (!jsspiGetSendFunction(parent, &data.spiSend, &data.spiSendData)) return 0; JsVar *dst = 0; // we're sending and receiving if (DEVICE_IS_SPI(device)) jshSPISetReceive(device, true); // assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, false); // send data if (jsvIsNumeric(srcdata)) { int r = data.spiSend((unsigned char)jsvGetInteger(srcdata), &data.spiSendData); if (r<0) r = data.spiSend(-1, &data.spiSendData); dst = jsvNewFromInteger(r); // retrieve the byte (no send!) } else if (jsvIsString(srcdata)) { dst = jsvNewFromEmptyString(); JsvStringIterator it; jsvStringIteratorNew(&it, srcdata, 0); int incount = 0, outcount = 0; while (jsvStringIteratorHasChar(&it) && !jspIsInterrupted()) { unsigned char in = (unsigned char)jsvStringIteratorGetChar(&it); incount++; int out = data.spiSend(in, &data.spiSendData); if (out>=0) { outcount++; char outc = (char)out; jsvAppendStringBuf(dst, (char*)&outc, 1); } jsvStringIteratorNext(&it); } jsvStringIteratorFree(&it); // finally add the remaining bytes (no send!) while (outcount < incount && !jspIsInterrupted()) { outcount++; unsigned char out = (unsigned char)data.spiSend(-1, &data.spiSendData); jsvAppendStringBuf(dst, (char*)&out, 1); } } else { int nBytes = jsvIterateCallbackCount(srcdata); dst = jsvNewTypedArray(ARRAYBUFFERVIEW_UINT8, nBytes); if (dst) { data.rxAmt = data.txAmt = 0; jsvArrayBufferIteratorNew(&data.it, dst, 0); // Write data jsvIterateCallback(srcdata, (void (*)(int, void *))jswrap_spi_send_cb, &data); // Wait until SPI send is finished, and flush data while (data.rxAmt < data.txAmt && !jspIsInterrupted()) jswrap_spi_send_cb(-1, &data); jsvArrayBufferIteratorFree(&data.it); } } // de-assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, true); return dst; }
/*JSON{ "type" : "method", "class" : "Serial", "name" : "setup", "generate" : "jswrap_serial_setup", "params" : [ ["baudrate","JsVar","The baud rate - the default is 9600"], ["options","JsVar",["An optional structure containing extra information on initialising the serial port.","```{rx:pin,tx:pin,bytesize:8,parity:null/'none'/'o'/'odd'/'e'/'even',stopbits:1,flow:null/undefined/'none'/'xon'}```","You can find out which pins to use by looking at [your board's reference page](#boards) and searching for pins with the `UART`/`USART` markers.","Note that even after changing the RX and TX pins, if you have called setup before then the previous RX and TX pins will still be connected to the Serial port as well - until you set them to something else using digitalWrite"]] ] } Setup this Serial port with the given baud rate and options. If not specified in options, the default pins are used (usually the lowest numbered pins on the lowest port that supports this peripheral) */ void jswrap_serial_setup(JsVar *parent, JsVar *baud, JsVar *options) { IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_USART(device)) return; JshUSARTInfo inf; jshUSARTInitInfo(&inf); if (jsvIsUndefined(options)) { options = jsvObjectGetChild(parent, DEVICE_OPTIONS_NAME, 0); } else jsvLockAgain(options); JsVar *parity = 0; JsVar *flow = 0; jsvConfigObject configs[] = { {"rx", JSV_PIN, &inf.pinRX}, {"tx", JSV_PIN, &inf.pinTX}, {"ck", JSV_PIN, &inf.pinCK}, {"bytesize", JSV_INTEGER, &inf.bytesize}, {"stopbits", JSV_INTEGER, &inf.stopbits}, {"parity", JSV_OBJECT /* a variable */, &parity}, {"flow", JSV_OBJECT /* a variable */, &flow}, }; if (!jsvIsUndefined(baud)) { int b = (int)jsvGetInteger(baud); if (b<=100 || b > 10000000) jsExceptionHere(JSET_ERROR, "Invalid baud rate specified"); else inf.baudRate = b; } bool ok = true; if (jsvReadConfigObject(options, configs, sizeof(configs) / sizeof(jsvConfigObject))) { // sort out parity inf.parity = 0; if(jsvIsString(parity)) { if (jsvIsStringEqual(parity, "o") || jsvIsStringEqual(parity, "odd")) inf.parity = 1; else if (jsvIsStringEqual(parity, "e") || jsvIsStringEqual(parity, "even")) inf.parity = 2; } else if (jsvIsInt(parity)) { inf.parity = (unsigned char)jsvGetInteger(parity); } if (inf.parity>2) { jsExceptionHere(JSET_ERROR, "Invalid parity %d", inf.parity); ok = false; } if (ok) { if (jsvIsUndefined(flow) || jsvIsNull(flow) || jsvIsStringEqual(flow, "none")) inf.xOnXOff = false; else if (jsvIsStringEqual(flow, "xon")) inf.xOnXOff = true; else { jsExceptionHere(JSET_ERROR, "Invalid flow control: %q", flow); ok = false; } } #ifdef LINUX if (ok && jsvIsObject(options)) jsvObjectSetChildAndUnLock(parent, "path", jsvObjectGetChild(options, "path", 0)); #endif } jsvUnLock(parity); jsvUnLock(flow); if (!ok) { jsvUnLock(options); return; } jshUSARTSetup(device, &inf); // Set baud rate in object, so we can initialise it on startup jsvObjectSetChildAndUnLock(parent, USART_BAUDRATE_NAME, jsvNewFromInteger(inf.baudRate)); // Do the same for options if (options) jsvObjectSetChildAndUnLock(parent, DEVICE_OPTIONS_NAME, options); else jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME); }
/*JSON{ "type" : "staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default)"], ["cs", "pin", "The pin to use for Chip Select"] ], "return" : ["JsVar","An Ethernet Object"], "return_object" : "Ethernet" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_wiznet_connect(JsVar *spi, Pin cs) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); spiDevice = ETH_SPI; } if (!jshIsPinValid(cs)) cs = ETH_CS_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); net.data.device = spiDevice; net.data.pinCS = cs; networkSet(&net); JsVar *ethObj = jspNewObject(0, "Ethernet"); // CS Configuration jshSetPinStateIsManual(net.data.pinCS, false); jshPinOutput(net.data.pinCS, 1); // de-assert CS // Initialise WIZnet functions reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZnet Initialize failed.\r\n"); networkFree(&net); return 0; } #if _WIZCHIP_ == 5500 /* PHY link status check - W5100 doesn't have this */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); networkFree(&net); return 0; } } while (tmp == PHY_LINK_OFF); #endif networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }
/** * Send data through SPI. * The data can be in a variety of formats including: * * `numeric` - A single byte is transmitted. * * `string` - Each character in the string is transmitted. * * `iterable` - An iterable object is transmitted. * \return the Received bytes (MISO). This is byte array. */ JsVar *jswrap_spi_send( JsVar *parent, //!< A description of the SPI device to send data through. JsVar *srcdata, //!< The data to send through SPI. Pin nss_pin //!< The pin to toggle low then high (CS) ) { // Debug // jsiConsolePrintf("jswrap_spi_send called: parent=%j, srcdata=%j, nss_pin=%p\n", parent, srcdata, nss_pin); NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); jswrap_spi_send_data data; if (!jsspiGetSendFunction(parent, &data.spiSend, &data.spiSendData)) return 0; JsVar *dst = 0; // we're sending and receiving if (DEVICE_IS_SPI(device)) jshSPISetReceive(device, true); // assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, false); // Now that we are setup, we can send the data. // Handle the data being a single byte value if (jsvIsNumeric(srcdata)) { int r = data.spiSend((unsigned char)jsvGetInteger(srcdata), &data.spiSendData); if (r<0) r = data.spiSend(-1, &data.spiSendData); dst = jsvNewFromInteger(r); // retrieve the byte (no send!) } // Handle the data being a string else if (jsvIsString(srcdata)) { dst = jsvNewFromEmptyString(); JsvStringIterator it; jsvStringIteratorNew(&it, srcdata, 0); int incount = 0, outcount = 0; while (jsvStringIteratorHasChar(&it) && !jspIsInterrupted()) { unsigned char in = (unsigned char)jsvStringIteratorGetChar(&it); incount++; int out = data.spiSend(in, &data.spiSendData); if (out>=0) { outcount++; char outc = (char)out; jsvAppendStringBuf(dst, (char*)&outc, 1); } jsvStringIteratorNext(&it); } jsvStringIteratorFree(&it); // finally add the remaining bytes (no send!) while (outcount < incount && !jspIsInterrupted()) { outcount++; unsigned char out = (unsigned char)data.spiSend(-1, &data.spiSendData); jsvAppendStringBuf(dst, (char*)&out, 1); } } // Handle the data being an iterable. else { int nBytes = jsvIterateCallbackCount(srcdata); dst = jsvNewTypedArray(ARRAYBUFFERVIEW_UINT8, nBytes); if (dst) { data.rxAmt = data.txAmt = 0; jsvArrayBufferIteratorNew(&data.it, dst, 0); // Write data jsvIterateCallback(srcdata, (void (*)(int, void *))jswrap_spi_send_cb, &data); // Wait until SPI send is finished, and flush data while (data.rxAmt < data.txAmt && !jspIsInterrupted()) jswrap_spi_send_cb(-1, &data); jsvArrayBufferIteratorFree(&data.it); } } // de-assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, true); return dst; }