diff --git a/src/digital-pin.coffee b/src/digital-pin.coffee index 991e756..da30122 100644 --- a/src/digital-pin.coffee +++ b/src/digital-pin.coffee @@ -33,31 +33,43 @@ namespace 'Cylon.IO', -> connect: (mode = null) -> @mode ?= mode - # Creates the GPIO file to read/write from - FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }", (err) => - if(err) - @self.emit('error', 'Error while creating pin files') - else - @self._setMode(@mode, true) - @self.emit('open') + # Check if the pin acceess file is already in the GPIO folder + FS.exists(@_pinPath(), (exists) => + unless exists + # Creates the GPIO file to read/write from + FS.writeFile(@_exportPath(), "#{ @pinNum }", (err) => + if(err) + @self.emit('error', 'Error while creating pin files') + else + @self._setMode(@mode, true) + @self.emit('open') + ) ) close: -> - FS.writeFile("#{ GPIO_PATH }/unexport", "#{ @pinNum }", (err) => - if(err) - console.log("EROR WHILE WRITING TO UNEXPORT FILE!!!") - @self.emit('error', 'Error while closing pin files') - else - console.log("PIN SHOULD BE UNEXPORTED") - @self.emit('close') + FS.writeFile(@unexportPath(), "#{ @pinNum }", (err) => + @_closeCallback() ) + closeSync: -> + FS.writeFileSync("#{ GPIO_PATH }/unexport", "#{ @pinNum }", (err) => + @_closeCallback() + ) + + _closeCallback: (err) -> + if(err) + console.log("EROR WHILE WRITING TO UNEXPORT FILE!!!") + @self.emit('error', 'Error while closing pin files') + else + console.log("PIN SHOULD BE UNEXPORTED") + @self.emit('close') + digitalWrite: (value) -> @self._setMode('w') unless @mode == 'w' @status = if (value == 1) then 'high' else 'low' - FS.writeFile(@pinFile, value, (err) => + FS.writeFile(@valueFile, value, (err) => if (err) @self.emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }") else @@ -69,7 +81,7 @@ namespace 'Cylon.IO', -> readData = null setInterval(() => - FS.readFile(@pinFile, (err, data) => + FS.readFile(@valueFile, (err, data) => if err @self.emit('error', "Error occurred while reading from pin #{ @pinNum }") else @@ -81,23 +93,37 @@ namespace 'Cylon.IO', -> # Sets the mode for the GPIO pin by writing the correct values to the pin reference files _setMode: (mode, emitConnect = false) -> if mode == 'w' - FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_WRITE, (err) => + FS.writeFile(@_directionPath(), GPIO_DIRECTION_WRITE, (err) => if (err) @self.emit('error', "Setting up pin direction failed") else - @pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value" @ready = true @self.emit('connect', mode) if emitConnect ) else if mode =='r' - FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_READ, (err) => + FS.writeFile(@_directionPath(), GPIO_DIRECTION_READ, (err) => if (err) @self.emit('error', "Setting up pin direction failed") else - @pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value" @ready = true @self.emit('connect', mode) if emitConnect ) + @valueFile = @_valuePath() + + _directionPath: () -> + "#{ @pinPath }/direction" + + _valuePath: () -> + "#{ @pinPath }/value" + + _pinPath: () -> + "#{ GPIO_PATH }/gpio#{ @pinNum }" + + _exportPath: () -> + "#{ GPIO_PATH }/export" + + _unexportPath: () -> + "#{ GPIO_PATH }/unexport" setHigh: -> @self.digitalWrite(1)