Created
September 11, 2012 21:59
-
-
Save anonymous/3702421 to your computer and use it in GitHub Desktop.
Output potentiometer value as integer
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
// Electric Imp code example - Dimmer control | |
// --- Enable pot --- | |
//I2C Addresses | |
const i2c_ioexp = 0x7C; | |
//---------------------------------------- | |
//-- Configure I2C | |
//---------------------------------------- | |
hardware.configure(I2C_89); | |
local i2c = hardware.i2c89; | |
//---------------------------------------- | |
//-- IO Expander Functions | |
//---------------------------------------- | |
local function ioexp_read(addr) { | |
local result = i2c.read(i2c_ioexp, format("%c", addr), 1); | |
if (result == null) { | |
server.log("i2c read fail"); | |
return -1; | |
} else return result[0]; | |
} | |
local function ioexp_write(addr, data) { | |
i2c.write(i2c_ioexp, format("%c%c",addr, data)); | |
} | |
local function ioexp_writebit(addr, bitn, level) { | |
// read modify write | |
local reg = ioexp_read(addr); | |
reg = (level==0)?(reg&~(1<<bitn)) : (reg | (1<<bitn)); | |
ioexp_write(addr, reg) | |
} | |
local function ioexp_setpin(gpio, level) { | |
ioexp_writebit(gpio>=8?0x10:0x11, gpio&7, level?1:0); | |
} | |
local function ioexp_setdir(gpio, output) { | |
ioexp_writebit(gpio>=8?0x0e:0x0f, gpio&7, output?0:1); | |
} | |
// Enable Potentiometer | |
ioexp_setpin(8, 0); | |
ioexp_setdir(8, 1); | |
hardware.pin2.configure(ANALOG_IN); | |
// --- Now do something with it --- | |
local scanList = []; // List of pot scan event handlers | |
local outputList = []; // List of output objects | |
local count = 0; // Count of instantiated dimmer controls | |
// The control class represents one dimmer control channel | |
class control | |
{ | |
pinPot = null; // Reference of pin to use for rotary pot | |
output = null; // Output object | |
level = null; // Soft copy of current level, 0 - 945ish | |
id = 0; // Our channel ID | |
constructor(name, pin) | |
{ | |
// Save pin assignment and configure the pin for analog input | |
pinPot = pin; | |
pinPot.configure(ANALOG_IN); | |
// Declare a scalar output (on which we'll send floats) | |
output = OutputPort(name, "number"); | |
// Initialise level | |
level = 0; | |
// Register our pot scan event handler | |
scanList.append(scanEvent.bindenv(this)); | |
// Register our output object | |
outputList.append(output); | |
id = count++; | |
} | |
// This method is called periodically to scan a pot | |
function scanEvent() | |
{ | |
// Read the pot, scale to 0 - 945ish | |
local pot = pinPot.read() / 69; | |
// Process if delta exceeds minimum step | |
if(math.fabs(pot - level) > 5) | |
{ | |
// Send to output | |
output.set(945 - pot); | |
// Update soft copy | |
level = pot; | |
} | |
} | |
} | |
// 100ms timer for despatching scan event handlers | |
function scanTimer() | |
{ | |
// Restart timer | |
imp.wakeup(0.1, scanTimer); | |
// Call each scan event handler in turn | |
foreach(handler in scanList) handler(); | |
} | |
// Instantiate controls | |
control0 <- control("Ch 1", hardware.pin2); | |
// Register with the server | |
imp.configure("Dimmer Control", [], outputList); | |
// Start scanning | |
scanTimer(); | |
// End of code. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment