From fd40a27daefbe8e8bf84053239f9638ff8d6ff30 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Tue, 26 Feb 2019 10:13:00 -0800 Subject: [PATCH] 0.2.1 - fixed bug where decimal was string instead of number add uci logging --- package.json | 2 +- src/byte.js | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index f6e5974..27d4506 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci-utils/byte", - "version": "0.2.0", + "version": "0.2.1", "description": "Byte Class and related functions", "main": "src/byte.js", "scripts": { diff --git a/src/byte.js b/src/byte.js index ad213f2..db1f191 100644 --- a/src/byte.js +++ b/src/byte.js @@ -8,6 +8,10 @@ import writeByte from 'bitwise/byte/write' //local import * as array from './array' +import logger from '@uci-utils/logger' + +let log = logger({package:'@uci-utils/byte'}) + // Classes // Byte Class is a single byte value with an attached format designation which allows easy @@ -106,7 +110,7 @@ class Byte { bwop = bits.and break } - // console.log('byte and state as passed', byte, this.value) + log.debug({msg:'byte and state as passed', byte:byte, value:this.value}) let opbyte = bwop( this.toFmt('ARY'), byteFormat(byte, { in: format.in, out: 'ARY' }) @@ -196,12 +200,10 @@ function stateChanges(curr, prev) { if (!bitchanges) { return false } - console.log(prev) - console.log(curr) - console.log(bitchanges) + log.debug({prev:prev, cur:curr, bitchanges:bitchanges, msg:'state changes'}) let nowon = curr.map((bit, i) => { - console.log(bit, bitchanges[i], bit & bitchanges[i]) + log.debug({bit:bit, bitchangesi:bitchanges[i], msg:'now on state '}) return bit & bitchanges[i] }) nowon = byteFormat(nowon, { @@ -209,7 +211,7 @@ function stateChanges(curr, prev) { out: 'PLC' }) - console.log('nowon', nowon) + log.debug({nowon:nowon, msg:'now on state '}) return byteFormat(bitchanges, { in: 'ARY', @@ -246,7 +248,7 @@ const format = function(byte, fmt) { let ofmt = fmt.out || 'DEC' // default DEC // debug.L2('input: ', byte, ' input format: ', ifmt, ' output format:', ofmt); - let decimal = NaN + let decimal = 0 switch (ifmt) { case 'STR': decimal = writeByte(toBits(byte.padStart(8,'0'))) @@ -255,7 +257,7 @@ const format = function(byte, fmt) { decimal = readUInt(byte, 0, 8) break case 'DEC': - decimal = byte + decimal = +byte // make decimal as string is a number break case 'HEX': decimal = parseInt(byte, 16) @@ -271,6 +273,11 @@ const format = function(byte, fmt) { break } + if ( decimal<0 || decimal >255) { + log.warn ({byte:byte, fmt:fmt, msg:'error - invalid byte - not able to convert to decmial, converting to 0'}) + decimal = 0 + } + let output //TODO only BIT and DEC are working switch (ofmt) { @@ -293,7 +300,7 @@ const format = function(byte, fmt) { output = dec2plc(decimal) break default: - console.log('unknown input or output format') + log.warn({byte:byte, fmt:fmt, msg:'error - unkown output format'}) output = false break }