Updated digital pin and added pwm raspi example.
This commit is contained in:
parent
d1dd1c7df2
commit
0ebdd4fc7d
|
@ -0,0 +1,20 @@
|
|||
Cylon = require('..')
|
||||
|
||||
# Initialize the robot
|
||||
Cylon.robot
|
||||
connection:
|
||||
name: 'raspi', adaptor: 'raspi', port: '/dev/ttyACM0'
|
||||
|
||||
device:
|
||||
name: 'led', driver: 'led', pin: 11
|
||||
|
||||
work: (my) ->
|
||||
# we do our thing here
|
||||
brightness = 0
|
||||
fade = 5
|
||||
every 0.05.seconds(), ->
|
||||
brightness += fade
|
||||
my.led.brightness(brightness)
|
||||
fade = -fade if (brightness is 0) or (brightness is 255)
|
||||
|
||||
.start()
|
|
@ -34,29 +34,29 @@ namespace 'Cylon.IO', ->
|
|||
@mode ?= mode
|
||||
|
||||
# Check if the pin acceess file is already in the GPIO folder
|
||||
FS.exists @_pinPath(), (exists) =>
|
||||
FS.exists(@_pinPath(), (exists) =>
|
||||
if exists then @_openPin() else @_createGPIOPin()
|
||||
)
|
||||
|
||||
close: ->
|
||||
FS.writeFile @_unexportPath(), "#{ @pinNum }", (err) =>
|
||||
FS.writeFile(@_unexportPath(), "#{ @pinNum }", (err) =>
|
||||
@_closeCallback()
|
||||
)
|
||||
|
||||
closeSync: ->
|
||||
FS.writeFileSync @_unexportPath(), "#{ @pinNum }"
|
||||
FS.writeFileSync(@_unexportPath(), "#{ @pinNum }")
|
||||
@_closeCallback false
|
||||
|
||||
digitalWrite: (value) ->
|
||||
@_setMode('w') unless @mode is 'w'
|
||||
@status = if value is 1 then 'high' else 'low'
|
||||
|
||||
FS.writeFile @_valuePath(), value, (err) =>
|
||||
FS.writeFile(@_valuePath(), value, (err) =>
|
||||
if err
|
||||
@emit(
|
||||
'error',
|
||||
"Error occurred while writing value #{value} to pin #{@pinNum}"
|
||||
)
|
||||
@emit('error', "Error occurred while writing value #{value} to pin #{@pinNum}")
|
||||
else
|
||||
@emit 'digitalWrite', value
|
||||
@emit('digitalWrite', value)
|
||||
)
|
||||
|
||||
# Reads the pin input every interval amount of time:
|
||||
# params:
|
||||
|
@ -65,14 +65,15 @@ namespace 'Cylon.IO', ->
|
|||
@_setMode('r') unless @mode is 'r'
|
||||
readData = null
|
||||
|
||||
setInterval =>
|
||||
FS.readFile @_valuePath(), (err, data) =>
|
||||
setInterval(() =>
|
||||
FS.readFile(@_valuePath(), (err, data) =>
|
||||
if err
|
||||
@emit 'error', "Error occurred while reading from pin #{ @pinNum }"
|
||||
@emit('error', "Error occurred while reading from pin #{ @pinNum }")
|
||||
else
|
||||
readData = data
|
||||
@emit 'digitalRead', data
|
||||
, interval
|
||||
@emit('digitalRead', data)
|
||||
)
|
||||
, interval)
|
||||
|
||||
setHigh: ->
|
||||
@digitalWrite 1
|
||||
|
@ -85,35 +86,38 @@ namespace 'Cylon.IO', ->
|
|||
|
||||
# Creates the GPIO file to read/write from
|
||||
_createGPIOPin: () ->
|
||||
FS.writeFile @_exportPath(), "#{ @pinNum }", (err) =>
|
||||
FS.writeFile(@_exportPath(), "#{ @pinNum }", (err) =>
|
||||
if err
|
||||
@emit 'error', 'Error while creating pin files'
|
||||
@emit('error', 'Error while creating pin files')
|
||||
else
|
||||
@_openPin()
|
||||
)
|
||||
|
||||
_openPin: () ->
|
||||
@_setMode @mode, true
|
||||
@emit 'open'
|
||||
@_setMode(@mode, true)
|
||||
@emit('open')
|
||||
|
||||
_closeCallback: (err) ->
|
||||
if err
|
||||
@emit 'error', 'Error while closing pin files'
|
||||
@emit('error', 'Error while closing pin files')
|
||||
else
|
||||
@emit 'close', @pinNum
|
||||
@emit('close', @pinNum)
|
||||
|
||||
# Sets the mode for the GPIO pin by writing the correct values to the pin reference files
|
||||
_setMode: (mode, emitConnect = false) ->
|
||||
@mode = mode
|
||||
if mode is 'w'
|
||||
FS.writeFile @_directionPath(), GPIO_DIRECTION_WRITE, (err) =>
|
||||
FS.writeFile(@_directionPath(), GPIO_DIRECTION_WRITE, (err) =>
|
||||
@_setModeCallback err, emitConnect
|
||||
)
|
||||
else if mode is 'r'
|
||||
FS.writeFile @_directionPath(), GPIO_DIRECTION_READ, (err) =>
|
||||
FS.writeFile(@_directionPath(), GPIO_DIRECTION_READ, (err) =>
|
||||
@_setModeCallback err, emitConnect
|
||||
)
|
||||
|
||||
_setModeCallback: (err, emitConnect) ->
|
||||
if err
|
||||
@emit 'error', "Setting up pin direction failed"
|
||||
@emit('error', "Setting up pin direction failed")
|
||||
else
|
||||
@ready = true
|
||||
@emit('connect', @mode) if emitConnect
|
||||
|
|
Loading…
Reference in New Issue