nathanfiscus / inovelli-notification-calc

Web App (PWA) to build LED notification numbers for Inovelli ZWave Switches
https://nathanfiscus.github.io/inovelli-notification-calc/
26 stars 7 forks source link

Library for use in other apps #2

Closed tekmaven closed 4 years ago

tekmaven commented 4 years ago

I really enjoy this tool! Great job!

I would like to use the logic with my own code. It would be awesome to provide this as an NPM package.

nathanfiscus commented 4 years ago

Which parts of the logic are you looking have in the library?

tekmaven commented 4 years ago

I want to plug in the notification parameters (color, intensity, etc) and get the resulting configuration value returned.

nathanfiscus commented 4 years ago

Sorry this took so long to get back to, but the real core of the notification calculation is these two functions:

const longToByteArray = function (/*long*/ long) {
  // we want to represent the input as a 8-bytes array
  var byteArray = [0, 0, 0, 0];

  for (var index = 0; index < byteArray.length; index++) {
    var byte = long & 0xff;
    byteArray[index] = byte;
    long = (long - byte) / 256;
  }

  return byteArray;
};

const byteArrayToLong = function (/*byte[]*/ byteArray) {
  var value = 0;
  for (var i = byteArray.length - 1; i >= 0; i--) {
    value = value * 256 + byteArray[i];
  }

  return value;
};

You can pass an array of values in the order described in Inovelli's docuemts to the byteArrayToLong function and it will give you the single value, while the other function, longToByteArray, will take a single notification number and transfer it back into an array of configuration values. Since these two functions don't amount to too much I don't feel it necessary to create a library at this time. In the future, I may pursues this, but feel free to re-use any of the code or references if you would like to create a library based on this.