0.2.1 - fixed bug where decimal was string instead of number

add uci logging
master
David Kebler 2019-02-26 10:13:00 -08:00
parent 80b50b3b9a
commit fd40a27dae
2 changed files with 17 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci-utils/byte", "name": "@uci-utils/byte",
"version": "0.2.0", "version": "0.2.1",
"description": "Byte Class and related functions", "description": "Byte Class and related functions",
"main": "src/byte.js", "main": "src/byte.js",
"scripts": { "scripts": {

View File

@ -8,6 +8,10 @@ import writeByte from 'bitwise/byte/write'
//local //local
import * as array from './array' import * as array from './array'
import logger from '@uci-utils/logger'
let log = logger({package:'@uci-utils/byte'})
// Classes // Classes
// Byte Class is a single byte value with an attached format designation which allows easy // Byte Class is a single byte value with an attached format designation which allows easy
@ -106,7 +110,7 @@ class Byte {
bwop = bits.and bwop = bits.and
break 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( let opbyte = bwop(
this.toFmt('ARY'), this.toFmt('ARY'),
byteFormat(byte, { in: format.in, out: 'ARY' }) byteFormat(byte, { in: format.in, out: 'ARY' })
@ -196,12 +200,10 @@ function stateChanges(curr, prev) {
if (!bitchanges) { if (!bitchanges) {
return false return false
} }
console.log(prev) log.debug({prev:prev, cur:curr, bitchanges:bitchanges, msg:'state changes'})
console.log(curr)
console.log(bitchanges)
let nowon = curr.map((bit, i) => { 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] return bit & bitchanges[i]
}) })
nowon = byteFormat(nowon, { nowon = byteFormat(nowon, {
@ -209,7 +211,7 @@ function stateChanges(curr, prev) {
out: 'PLC' out: 'PLC'
}) })
console.log('nowon', nowon) log.debug({nowon:nowon, msg:'now on state '})
return byteFormat(bitchanges, { return byteFormat(bitchanges, {
in: 'ARY', in: 'ARY',
@ -246,7 +248,7 @@ const format = function(byte, fmt) {
let ofmt = fmt.out || 'DEC' // default DEC let ofmt = fmt.out || 'DEC' // default DEC
// debug.L2('input: ', byte, ' input format: ', ifmt, ' output format:', ofmt); // debug.L2('input: ', byte, ' input format: ', ifmt, ' output format:', ofmt);
let decimal = NaN let decimal = 0
switch (ifmt) { switch (ifmt) {
case 'STR': case 'STR':
decimal = writeByte(toBits(byte.padStart(8,'0'))) decimal = writeByte(toBits(byte.padStart(8,'0')))
@ -255,7 +257,7 @@ const format = function(byte, fmt) {
decimal = readUInt(byte, 0, 8) decimal = readUInt(byte, 0, 8)
break break
case 'DEC': case 'DEC':
decimal = byte decimal = +byte // make decimal as string is a number
break break
case 'HEX': case 'HEX':
decimal = parseInt(byte, 16) decimal = parseInt(byte, 16)
@ -271,6 +273,11 @@ const format = function(byte, fmt) {
break 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 let output
//TODO only BIT and DEC are working //TODO only BIT and DEC are working
switch (ofmt) { switch (ofmt) {
@ -293,7 +300,7 @@ const format = function(byte, fmt) {
output = dec2plc(decimal) output = dec2plc(decimal)
break break
default: default:
console.log('unknown input or output format') log.warn({byte:byte, fmt:fmt, msg:'error - unkown output format'})
output = false output = false
break break
} }