Refactoring datalog in common packages (#249)
* using datalog from common packages * upgrading common package link * updated block signatures * more docs
This commit is contained in:
parent
104185a41e
commit
16c67f0e30
@ -1,3 +0,0 @@
|
||||
# Datalog
|
||||
|
||||
A tiny libraty to create CSV datalog files.
|
@ -1,11 +1,15 @@
|
||||
{
|
||||
"datalog": "A data logging framework",
|
||||
"datalog.DatalogStorage": "A storage for datalog data",
|
||||
"datalog.DatalogStorage.appendHeaders": "Appends the headers in datalog",
|
||||
"datalog.DatalogStorage.appendRow": "Appends a row of data",
|
||||
"datalog.DatalogStorage.flush": "Flushes any buffered data",
|
||||
"datalog.DatalogStorage.init": "Initializes the storage",
|
||||
"datalog.addRow": "Starts a row of data",
|
||||
"datalog.addValue": "Adds a cell to the row of data",
|
||||
"datalog.addValue|param|name": "name of the cell, eg: \"x\"",
|
||||
"datalog.addValue|param|value": "value of the cell, eg: 0",
|
||||
"datalog.flush": "Commits any buffered row to disk",
|
||||
"datalog.setEnabled": "Turns on or off datalogging",
|
||||
"datalog.setFile": "Starts a new data logger for the given file",
|
||||
"datalog.setFile|param|filename": "the filename, eg: \"datalog.csv\"",
|
||||
"datalog.setStorage": "* @param storage custom storage solution"
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"datalog.addRow|block": "datalog add row",
|
||||
"datalog.addValue|block": "datalog add %name|=%value",
|
||||
"datalog.setEnabled|block": "datalog %enabled",
|
||||
"datalog.setEnabled|block": "datalog %enabled=toggleOnOff",
|
||||
"datalog|block": "datalog",
|
||||
"{id:category}Datalog": "Datalog"
|
||||
}
|
@ -1,122 +0,0 @@
|
||||
//% weight=100 color=#0fbc11 icon=""
|
||||
namespace datalog {
|
||||
let _headers: string[] = undefined;
|
||||
let _headersLength: number;
|
||||
let _values: number[];
|
||||
let _buffer: string = "";
|
||||
let _start: number;
|
||||
let _filename = "datalog.csv";
|
||||
let _storage: storage.Storage = storage.temporary;
|
||||
let _enabled = true;
|
||||
|
||||
function clear() {
|
||||
_headers = undefined;
|
||||
_values = undefined;
|
||||
_buffer = "";
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (!_headers) {
|
||||
_headers = [];
|
||||
_headersLength = 0;
|
||||
_start = control.millis();
|
||||
_storage.remove(_filename);
|
||||
}
|
||||
_values = [];
|
||||
}
|
||||
|
||||
function commit() {
|
||||
// write row if any data
|
||||
if (_values && _values.length > 0) {
|
||||
// write headers for the first row
|
||||
if (!_headersLength) {
|
||||
_storage.appendCSVHeaders(_filename, _headers);
|
||||
_headersLength = _storage.size(_filename);
|
||||
}
|
||||
// commit row data
|
||||
_buffer += storage.toCSV(_values, _storage.csvSeparator);
|
||||
// buffered writes
|
||||
if (_buffer.length > 1024)
|
||||
flush();
|
||||
}
|
||||
|
||||
// clear values
|
||||
_values = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a row of data
|
||||
*/
|
||||
//% weight=100
|
||||
//% blockId=datalogAddRow block="datalog add row"
|
||||
export function addRow(): void {
|
||||
if (!_enabled) return;
|
||||
|
||||
commit();
|
||||
init();
|
||||
const s = (control.millis() - _start) / 1000;
|
||||
addValue("time (s)", s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a cell to the row of data
|
||||
* @param name name of the cell, eg: "x"
|
||||
* @param value value of the cell, eg: 0
|
||||
*/
|
||||
//% weight=99
|
||||
//% blockId=datalogAddValue block="datalog add %name|=%value"
|
||||
export function addValue(name: string, value: number) {
|
||||
if (!_values) return;
|
||||
let i = _headers.indexOf(name);
|
||||
if (i < 0) {
|
||||
_headers.push(name);
|
||||
i = _headers.length - 1;
|
||||
}
|
||||
_values[i] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a new data logger for the given file
|
||||
* @param filename the filename, eg: "datalog.csv"
|
||||
*/
|
||||
//%
|
||||
export function setFile(filename: string) {
|
||||
flush();
|
||||
_filename = filename;
|
||||
clear();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param storage custom storage solution
|
||||
*/
|
||||
//%
|
||||
export function setStorage(storage: storage.Storage) {
|
||||
flush();
|
||||
_storage = storage;
|
||||
clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Commits any buffered row to disk
|
||||
*/
|
||||
//%
|
||||
export function flush() {
|
||||
if (_buffer) {
|
||||
const b = _buffer;
|
||||
_buffer = "";
|
||||
_storage.append(_filename, b);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns on or off datalogging
|
||||
* @param enabled
|
||||
*/
|
||||
//% blockId=datalogEnabled block="datalog %enabled"
|
||||
//% enabled.fieldEditor=fieldonoff
|
||||
export function setEnabled(enabled: boolean) {
|
||||
flush();
|
||||
_enabled = enabled;
|
||||
}
|
||||
}
|
@ -1,16 +1,7 @@
|
||||
{
|
||||
"name": "datalog",
|
||||
"description": "Tiny data logging framework",
|
||||
"files": [
|
||||
"README.md",
|
||||
"datalog.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
"test.ts"
|
||||
],
|
||||
"public": true,
|
||||
"additionalFilePath": "../../node_modules/pxt-common-packages/libs/datalog",
|
||||
"dependencies": {
|
||||
"core": "file:../core",
|
||||
"storage": "file:../storage"
|
||||
"core": "file:../core",
|
||||
"storage": "file:../storage"
|
||||
}
|
||||
}
|
57
libs/datalog/targetoverrides.ts
Normal file
57
libs/datalog/targetoverrides.ts
Normal file
@ -0,0 +1,57 @@
|
||||
namespace datalog.ev3 {
|
||||
/**
|
||||
* A datalog storage for the EV3
|
||||
*/
|
||||
export class EV3DatalogStorage extends DatalogStorage { // implements DatalogStorage {
|
||||
private _filename: string;
|
||||
private _buffer: string;
|
||||
private _storage: storage.Storage;
|
||||
|
||||
/**
|
||||
* Creates a new storage for the datalog
|
||||
* @param storage
|
||||
* @param filename
|
||||
*/
|
||||
constructor(storage: storage.Storage, filename: string) {
|
||||
super();
|
||||
this._filename = filename;
|
||||
this._storage = storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the storage
|
||||
*/
|
||||
init(): void {
|
||||
this._storage.remove(this._filename);
|
||||
this._buffer = "";
|
||||
}
|
||||
/**
|
||||
* Appends the headers in datalog
|
||||
*/
|
||||
appendHeaders(headers: string[]): void {
|
||||
this._storage.appendCSVHeaders(this._filename, headers);
|
||||
}
|
||||
/**
|
||||
* Appends a row of data
|
||||
*/
|
||||
appendRow(values: number[]): void {
|
||||
// commit row data
|
||||
this._buffer += storage.toCSV(values, this._storage.csvSeparator);
|
||||
// buffered writes
|
||||
if (this._buffer.length > 1024)
|
||||
this.flush();
|
||||
}
|
||||
/**
|
||||
* Flushes any buffered data
|
||||
*/
|
||||
flush(): void {
|
||||
if (this._buffer) {
|
||||
const b = this._buffer;
|
||||
this._buffer = "";
|
||||
this._storage.append(this._filename, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
// automatic hook up
|
||||
datalog.setStorage(new datalog.ev3.EV3DatalogStorage(storage.temporary, "datalog.csv"));
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
|
||||
loops.forever(function () {
|
||||
datalog.addRow()
|
||||
datalog.addValue("x", Math.random())
|
||||
datalog.addValue("y", Math.random())
|
||||
})
|
@ -44,7 +44,7 @@
|
||||
"webfonts-generator": "^0.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"pxt-common-packages": "0.17.11",
|
||||
"pxt-common-packages": "0.17.12",
|
||||
"pxt-core": "3.0.26"
|
||||
},
|
||||
"scripts": {
|
||||
|
Loading…
Reference in New Issue
Block a user