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 }