@@ -43,7 +43,9 @@ CHANGE = exports.CHANGE = "both";
4343RISING = exports . RISING = "rising" ;
4444FALLING = exports . FALLING = "falling" ;
4545
46+ // Keep track of allocated resources
4647var gpio = [ ] ;
48+ var pwm = [ ] ;
4749
4850getPinMode = exports . getPinMode = function ( pin , callback ) {
4951 var muxFile = '/sys/kernel/debug/omap_mux/' + pin . mux ;
@@ -133,8 +135,7 @@ getPinMode = exports.getPinMode = function(pin, callback) {
133135 }
134136} ;
135137
136- pinMode = exports . pinMode = function ( pin , direction , mux , pullup , slew , callback )
137- {
138+ pinMode = exports . pinMode = function ( pin , direction , mux , pullup , slew , callback ) {
138139 pullup = pullup || 'disabled' ;
139140 slew = slew || 'fast' ;
140141 mux = mux || 7 ; // default to GPIO mode
@@ -222,8 +223,7 @@ pinMode = exports.pinMode = function(pin, direction, mux, pullup, slew, callback
222223 return ( true ) ;
223224} ;
224225
225- digitalWrite = exports . digitalWrite = function ( pin , value , callback )
226- {
226+ digitalWrite = exports . digitalWrite = function ( pin , value , callback ) {
227227 if ( callback ) {
228228 fs . writeFile ( gpio [ pin . gpio ] . path , '' + value , null , callback ) ;
229229 } else {
@@ -232,8 +232,7 @@ digitalWrite = exports.digitalWrite = function(pin, value, callback)
232232 return ( true ) ;
233233} ;
234234
235- digitalRead = exports . digitalRead = function ( pin , callback )
236- {
235+ digitalRead = exports . digitalRead = function ( pin , callback ) {
237236 if ( callback ) {
238237 var readFile = function ( err , data ) {
239238 callback ( { 'value' :data } ) ;
@@ -244,8 +243,7 @@ digitalRead = exports.digitalRead = function(pin, callback)
244243 return ( fs . readFileSync ( gpio [ pin . gpio ] . path ) ) ;
245244} ;
246245
247- analogRead = exports . analogRead = function ( pin , callback )
248- {
246+ analogRead = exports . analogRead = function ( pin , callback ) {
249247 var ainFile = '/sys/bus/platform/devices/tsc/ain' + ( pin . ain + 1 ) ;
250248 if ( callback ) {
251249 var readFile = function ( err , data ) {
@@ -257,8 +255,7 @@ analogRead = exports.analogRead = function(pin, callback)
257255 return ( fs . readFileSync ( ainFile ) ) ;
258256} ;
259257
260- shiftOut = exports . shiftOut = function ( dataPin , clockPin , bitOrder , val , callback )
261- {
258+ shiftOut = exports . shiftOut = function ( dataPin , clockPin , bitOrder , val , callback ) {
262259 var i ;
263260 var bit ;
264261 for ( i = 0 ; i < 8 ; i ++ )
@@ -320,6 +317,43 @@ attachInterrupt = exports.attachInterrupt = function(pin, handler, mode) {
320317 }
321318} ;
322319
320+ // See http://processors.wiki.ti.com/index.php/AM335x_PWM_Driver's_Guide
321+ analogWrite = exports . analogWrite = function ( pin , value , freq , callback ) {
322+ freq = freq || 1000 ;
323+ var curMode = getPinMode ( pin ) ;
324+ if ( curMode . direction != OUTPUT ) {
325+ throw ( pin . key + ' must be configured as OUTPUT for analogWrite()' ) ;
326+ }
327+ if ( ! pin . pwm ) {
328+ throw ( pin . key + ' does not support analogWrite()' ) ;
329+ }
330+ if ( pwm [ pin . pwm . path ] && pwm [ pin . pwm . path ] . key ) {
331+ if ( pwm [ pin . pwm . path ] . key != pin . key ) {
332+ throw ( pin . key + ' requires pwm ' + pin . pwm . name +
333+ ' but it is already in use by ' +
334+ pwm [ pin . pwm ] . key
335+ ) ;
336+ }
337+ } else {
338+ pwm [ pin . pwm . path ] . key = pin . key ;
339+ pwm [ pin . pwm . path ] . freq = freq ;
340+ pinMode ( pin , OUTPUT , pin . pwm . muxmode , 'disabled' , 'fast' ) ;
341+ var path = '/sys/class/pwm/' + pin . pwm . path ;
342+ fs . writeFileSync ( path + '/request' , '1' ) ;
343+ fs . writeFileSync ( path + '/period_freq' , freq ) ;
344+ fs . writeFileSync ( path + '/polarity' , '1' ) ;
345+ fs . writeFileSync ( path + '/run' , '1' ) ;
346+ }
347+ if ( pwm [ pin . pwm . path ] . freq != freq ) {
348+ fs . writeFileSync ( path + '/run' , '0' ) ;
349+ fs . writeFileSync ( path + '/duty_percent' , 0 ) ;
350+ fs . writeFileSync ( path + '/period_freq' , freq ) ;
351+ fs . writeFileSync ( path + '/run' , '1' ) ;
352+ pwm [ pin . pwm . path ] . freq = freq ;
353+ }
354+ fs . writeFileSync ( path + '/duty_percent' , value / 255 ) ;
355+ } ;
356+
323357getEeproms = exports . getEeproms = function ( callback ) {
324358 var EepromFiles = {
325359 '/sys/bus/i2c/drivers/at24/1-0050/eeprom' : { type : 'bone' } ,
@@ -359,8 +393,7 @@ if(fibers.exists) {
359393
360394// This is where everything is meant to happen
361395if ( fibers . exists ) {
362- run = exports . run = function ( )
363- {
396+ run = exports . run = function ( ) {
364397 Fiber ( function ( ) {
365398 var fiber = Fiber . current ;
366399 setup ( ) ;
@@ -376,8 +409,7 @@ if(fibers.exists) {
376409 } ) . run ( ) ;
377410 } ;
378411} else {
379- run = exports . run = function ( )
380- {
412+ run = exports . run = function ( ) {
381413 setup ( ) ;
382414 if ( typeof loop === "function" ) {
383415 var repeat = function repeat ( ) {
0 commit comments