const debug = require('debug')('hardware:sevenSegment')
const I2C = require('raspi-i2c').I2C
const util = require('util')

const i2c = new I2C()

module.exports = class SevenSegment {
  constructor (name, address = 0x71) {
    this.name = name
    this.address = address

    this.writeByte = util.promisify(i2c.writeByte).bind(i2c) // Bind is required because writeByte refers to
    this.readByte = util.promisify(i2c.readByte).bind(i2c)   // methods from the closing scope. https://github.com/nodejs/node/pull/13440
  }

  async writeNumber (number) {
    let clippedNumber = parseInt(Math.abs(number))
    if (clippedNumber > 9999) {
      debug(this.name + ' seven segment display OVERFLOW')
      // TODO: Display overflow
      // this.displayOverflowSync(0x01)
      return false
    }
    // Save the last four digits and covert to hex
    let clippedString = clippedNumber.toString()
    let cursorPos = parseInt((4 - clippedString.length), 16)
    debug(this.name + ' seven segment sent display: ' + clippedString)

    // Clear the display
    await this.writeByte(this.address, 0x76)

    // Update the cursor to the left position
    await this.writeByte(this.address, 0x79)
    await this.writeByte(this.address, cursorPos)

    // Send characters
    await Promise.all(clippedString.split('').map(async char => {
      await this.writeByte(this.address, parseInt(char, 16))
    }))
    return true
  }

  // displayErrorSync (number) {
    // let
    // parseInt(number) & 0x
    // // Save the last four digits and covert to hex
    // String(number)

    // // Update the cursor to the left position
    // i2c.writeByteSync(this.address, 0x01)

    // i2c.writeByteSync(this.address, 0x01)
  // }
}

@@ -556,29 +556,24 @@
     let 
-clippedNumber
+intInput
  = parse
@@ -606,23 +606,19 @@
 if (
-clippedNumber
+intInput
  %3E
+=
  999
@@ -841,37 +841,27 @@
 let 
-clippedString = clippedNumber
+strInput = intInput
 .toS
@@ -902,29 +902,24 @@
 nt((4 - 
-clippedString
+strInput
 .length)
@@ -980,29 +980,24 @@
 ay: ' + 
-clippedString
+strInput
 )%0A%0A    /
@@ -1235,39 +1235,164 @@
     
-await Promise.all(clippedString
+for (let i = 0; i %3C 4 - cursorPos; ++i) %7B%0A      await this.writeByte(this.address, parseInt(strInput.charAt(i), 16))%0A    %7D%0A    // await Promise.all(strInput
 .spl
@@ -1417,24 +1417,27 @@
 har =%3E %7B%0A   
+ //
    await thi
@@ -1485,16 +1485,19 @@
 16))%0A   
+ //
  %7D))%0A   

Make sure async functions are executed in order