UNPKG

brickpi-coffeescript

Version:

BrickPi API implementation in CoffeeScript for JS/CS

69 lines (58 loc) 1.74 kB
module.exports = class Buttons # Initialize all the buttons to 0 constructor: (@brickPi) -> @l1 = 0 @l2 = 0 @r1 = 0 @r2 = 0 @a = 0 @b = 0 @c = 0 @d = 0 @tri = 0 @sqr = 0 @cir = 0 @cro = 0 @ljb = 0 @ljx = 0 @ljy = 0 @rjx = 0 @rjy = 0 # Update all the buttons update: (I2C_PORT) -> # For all buttons: # 0: Unpressed # 1: Pressed sensorIn = @brickPi.SensorI2CIn p = I2C_PORT #Left and right joystick: -127 to 127 @ljb =~ (sensorIn[p][0][0] >> 1) & 1 @rjb =~ (sensorIn[p][0][0] >> 2) & 1 #For buttons a,b,c,d @d =~ (sensorIn[p][0][0] >> 4) & 1 @c =~ (sensorIn[p][0][0] >> 5) & 1 @b =~ (sensorIn[p][0][0] >> 6) & 1 @a =~ (sensorIn[p][0][0] >> 7) & 1 #For buttons l1,l2,r1,r2 @l2 =~ (sensorIn[p][0][1]) & 1 @r2 =~ (sensorIn[p][0][1] >> 1) & 1 @l1 =~ (sensorIn[p][0][1] >> 2) & 1 @r1 =~ (sensorIn[p][0][1] >> 3) & 1 #For buttons square,triangle,cross,circle @tri =~ (sensorIn[p][0][1] >> 4) & 1 @cir =~ (sensorIn[p][0][1] >> 5) & 1 @cro =~ (sensorIn[p][0][1] >> 6) & 1 @sqr =~ (sensorIn[p][0][1] >> 7) & 1 #Left joystick x and y , -127 to 127 @ljx = sensorIn[p][0][2] - 128 @ljy =~ sensorIn[p][0][3] + 129 #Right joystick x and y , -127 to 127 @rjx = sensorIn[p][0][4] - 128 @rjy =~ sensorIn[p][0][5] + 129 showValues: -> console.log "ljb rjb d c b a l2 r2 l1 r1 tri cir cro sqr\ \tljx\tljy\trjx\trjy" console.log "#{@ljb} #{@rjb} #{@d} #{@c} #{@b} #{@a} #{@l2} #{@r2} #{@l1} #{@r1} \ #{@tri} #{@cir} #{@cro} #{@sqr} \t#{@ljx} \t#{@rjx} \t#{@ljy} \t#{@rjy}\n"