/*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; }
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); }
/*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); }
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 }