Created
September 25, 2012 12:13
-
-
Save anonymous/3781424 to your computer and use it in GitHub Desktop.
Hannah - button example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IO Expander Class for SX1509 | |
class IoExpander | |
{ | |
i2cPort = null; | |
i2cAddress = null; | |
constructor(port, address) | |
{ | |
if(port == I2C_12) | |
{ | |
// Configure I2C bus on pins 1 & 2 | |
hardware.configure(I2C_12); | |
i2cPort = hardware.i2c12; | |
} | |
else if(port == I2C_89) | |
{ | |
// Configure I2C bus on pins 8 & 9 | |
hardware.configure(I2C_89); | |
i2cPort = hardware.i2c89; | |
} | |
else | |
{ | |
server.log("Invalid I2C port specified."); | |
} | |
i2cAddress = address << 1; | |
} | |
// Read a byte | |
function read(register) | |
{ | |
local data = i2cPort.read(i2cAddress, format("%c", register), 1); | |
if(data == null) | |
{ | |
server.log("I2C Read Failure"); | |
return -1; | |
} | |
return data[0]; | |
} | |
// Write a byte | |
function write(register, data) | |
{ | |
i2cPort.write(i2cAddress, format("%c%c", register, data)); | |
} | |
// Write a bit to a register | |
function writeBit(register, bitn, level) | |
{ | |
local value = read(register); | |
value = (level == 0)?(value & ~(1<<bitn)):(value | (1<<bitn)); | |
write(register, value); | |
} | |
// Write a masked bit pattern | |
function writeMasked(register, data, mask) | |
{ | |
local value = read(register); | |
value = (value & ~mask) | (data & mask); | |
write(register, value); | |
} | |
// Set a GPIO level | |
function setPin(gpio, level) | |
{ | |
writeBit(gpio>=8?0x10:0x11, gpio&7, level?1:0); | |
} | |
// Set a GPIO direction | |
function setDir(gpio, output) | |
{ | |
writeBit(gpio>=8?0x0e:0x0f, gpio&7, output?0:1); | |
} | |
// Set a GPIO internal pull up | |
function setPullUp(gpio, enable) | |
{ | |
writeBit(gpio>=8?0x06:0x07, gpio&7, enable); | |
} | |
// Set GPIO interrupt mask | |
function setIrqMask(gpio, enable) | |
{ | |
writeBit(gpio>=8?0x12:0x13, gpio&7, enable); | |
} | |
// Set GPIO interrupt edges | |
function setIrqEdges(gpio, rising, falling) | |
{ | |
local addr = 0x17 - (gpio>>2); | |
local mask = 0x03 << ((gpio&3)<<1); | |
local data = (2*falling + rising) << ((gpio&3)<<1); | |
writeMasked(addr, data, mask); | |
} | |
// Clear an interrupt | |
function clearIrq(gpio) | |
{ | |
writeBit(gpio>=8?0x18:0x19, gpio&7, 1); | |
} | |
// Get a GPIO input pin level | |
function getPin(gpio) | |
{ | |
return (read(gpio>=8?0x10:0x11)&(1<<(gpio&7)))?1:0; | |
} | |
} | |
class Button extends IoExpander { | |
pin = null; | |
irq = null; | |
constructor(port, address, buttonPin, irqPin) { | |
base.constructor(port, address); | |
pin = buttonPin; | |
irq = irqPin; | |
irqPin.configure(DIGITAL_IN, irqHandler.bindenv(this)); | |
setDir(pin, 0); | |
setPullUp(pin, 1); | |
setIrqMask(pin, 0); | |
setIrqEdges(pin, 1, 1); | |
server.log("New Button on pin " + pin); | |
} | |
function irqHandler() { | |
server.log("Irq handler on pin " + pin); | |
if (irq.read() == 0) { | |
// Interrupt on negative-going edge | |
local state = getPin(pin)?0:1; | |
server.log("Button on pin " + pin + " now " + state); | |
} | |
clearIrq(pin); | |
} | |
} | |
// Register with the server | |
imp.configure("Stupid buttons", [], []); | |
// Instantiate the buttons | |
button1 <- Button(I2C_89, 0x3e, 0, hardware.pin1); | |
button2 <- Button(I2C_89, 0x3e, 1, hardware.pin1); | |
// End of code. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment