|
| 1 | +var MetaWear = require('../index')//require('metawear'); |
| 2 | +var ref = require('ref'); |
| 3 | +// Store the log event for later download. If your program needs to terminate |
| 4 | +// before performing the log download, you will need to use mbl_mw_metawearboard_serialize |
| 5 | +// to store the device state and that pass that state as the second argument to |
| 6 | +// connectAndSetUp when you are ready to download. Use mbl_mw_logger_lookup_id |
| 7 | +// to retrieve this accelLogger object |
| 8 | +var accelLogger = null; |
| 9 | + |
| 10 | +// If you know the MAC address, you can uncomment this line |
| 11 | +MetaWear.discoverByAddress('c7:06:35:93:2d:12', function (device) { |
| 12 | +//MetaWear.discover(function (device) { |
| 13 | + console.log('discovered ' + device.address); |
| 14 | + device.connectAndSetUp(function (error) { |
| 15 | + if (error) { |
| 16 | + console.log(error); |
| 17 | + process.exit(1); |
| 18 | + } |
| 19 | + console.log('connected ' + device.address); |
| 20 | + // Start logging |
| 21 | + startLogging(device, function (error) { |
| 22 | + if (error) { |
| 23 | + console.log(error); |
| 24 | + process.exit(1); |
| 25 | + } |
| 26 | + // Stop logging after 10 seconds |
| 27 | + setTimeout(function () { |
| 28 | + downloadLog(device, function (error) { |
| 29 | + device.once('disconnect', function (reason) { |
| 30 | + process.exit(0); |
| 31 | + }); |
| 32 | + MetaWear.mbl_mw_debug_reset(device.board); |
| 33 | + }); |
| 34 | + }, 10000); |
| 35 | + }); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +function downloadLog(device, callback) { |
| 40 | + // Shutdown accel |
| 41 | + MetaWear.mbl_mw_acc_stop(device.board); |
| 42 | + MetaWear.mbl_mw_acc_disable_acceleration_sampling(device.board); |
| 43 | + // Shutdown log |
| 44 | + MetaWear.mbl_mw_logging_stop(device.board); |
| 45 | + MetaWear.mbl_mw_logging_flush_page(device.board); |
| 46 | + // Setup handerl for accel data points |
| 47 | + MetaWear.mbl_mw_logger_subscribe(accelLogger, ref.NULL, MetaWear.FnVoid_VoidP_DataP.toPointer(function onSignal(context, dataPtr) { |
| 48 | + var data = dataPtr.deref(); |
| 49 | + var pt = data.parseValue(); |
| 50 | + console.log(data.epoch + ' ' + pt.x + ',' + pt.y + ',' + pt.z); |
| 51 | + })); |
| 52 | + // Setup the handlers for events during the download |
| 53 | + var downloadHandler = new MetaWear.LogDownloadHandler(); |
| 54 | + downloadHandler.received_progress_update = MetaWear.FnVoid_VoidP_UInt_UInt.toPointer(function onSignal(context, entriesLeft, totalEntries) { |
| 55 | + console.log('received_progress_update entriesLeft:' + entriesLeft + ' totalEntries:' + totalEntries); |
| 56 | + if (entriesLeft === 0) { |
| 57 | + // Remove all log entries if told to stop logging |
| 58 | + MetaWear.mbl_mw_metawearboard_tear_down(device.board); |
| 59 | + callback(null); |
| 60 | + } |
| 61 | + }); |
| 62 | + downloadHandler.received_unknown_entry = MetaWear.FnVoid_VoidP_UByte_Long_UByteP_UByte.toPointer(function onSignal(context, id, epoch, data, length) { |
| 63 | + console.log('received_unknown_entry'); |
| 64 | + }); |
| 65 | + downloadHandler.received_unhandled_entry = MetaWear.FnVoid_VoidP_DataP.toPointer(function onSignal(context, dataPtr) { |
| 66 | + var data = dataPtr.deref(); |
| 67 | + var dataPoint = data.parseValue(); |
| 68 | + console.log('received_unhandled_entry: ' + dataPoint); |
| 69 | + }); |
| 70 | + // Actually start the log download, this will cause all the handlers we setup to be invoked |
| 71 | + MetaWear.mbl_mw_logging_download(device.board, 20, downloadHandler.ref()); |
| 72 | +} |
| 73 | + |
| 74 | +function startLogging(device, callback) { |
| 75 | + MetaWear.mbl_mw_acc_set_odr(device.board, 50.0); |
| 76 | + MetaWear.mbl_mw_acc_set_range(device.board, 16.0); |
| 77 | + MetaWear.mbl_mw_acc_write_acceleration_config(device.board); |
| 78 | + |
| 79 | + // See if we already created a logger |
| 80 | + var accSignal = MetaWear.mbl_mw_acc_get_acceleration_data_signal(device.board); |
| 81 | + MetaWear.mbl_mw_datasignal_log(accSignal, ref.NULL, MetaWear.FnVoid_VoidP_DataLoggerP.toPointer(function (context, logger) { |
| 82 | + accelLogger = logger; |
| 83 | + callback(logger.address() ? null : new Error('failed to start logging accel')); |
| 84 | + })); |
| 85 | + |
| 86 | + MetaWear.mbl_mw_logging_start(device.board, 0); |
| 87 | + MetaWear.mbl_mw_acc_enable_acceleration_sampling(device.board); |
| 88 | + MetaWear.mbl_mw_acc_start(device.board); |
| 89 | +} |
0 commit comments