Created
November 5, 2015 23:14
-
-
Save ersatzavian/1f2e961de1e3c5ccb645 to your computer and use it in GitHub Desktop.
Electric Imp Factory BlinkUp Box simple 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
// Copyright (c) 2015 Electric Imp | |
// This file is licensed under the MIT License | |
// http://opensource.org/licenses/MIT | |
// Factory Tools Library simplifies detecting Fixture or DUT | |
#require "FactoryTools.device.nut:1.0.0" | |
// CFAx33KL Library abstracts the LCD/Keypad used on the Factory BlinkUp Box | |
#require "CFAx33KL.class.nut:1.0.1" | |
// CONSTS AND GLOBALS --------------------------------------------------------- | |
// Your Factory WiFi credentials go here | |
const SSID = "SSID"; | |
const PASSWORD = "PASSWORD"; | |
// Factory BlinkUp Box Pin Assignments | |
// Pin1: RS-232 CTS | |
// Pin2: RS-232 RTS | |
// Pin5: Front Panel Red LED, active-high | |
// Pin6: UART TX to LCD/Keypad | |
// Pin7: Front Panel Green LED, active-high | |
// Pin8: RS-232 TX | |
// Pin9: RS-232 RX | |
// PinA: Front Panel Green Button, high when pressed | |
// PinB: Optional Footswitch, high when pressed for normally-open footswitch | |
// PinC: BlinkUp Output, Active-Low | |
// PinD: Not Used | |
// PinE: UART RX from LCD/Keypad | |
// how long to wait after triggering BlinkUp before allowing another | |
const blinkUpTime = 10; // seconds | |
// flag used to prevent new BlinkUp triggers while BlinkUp is running | |
local sendingBlinkUp = false; | |
// FACTORY FIXTURE FUNCTIONS AND CLASSES -------------------------------------- | |
function configureBlinkUpTrigger(pin) { | |
// register a state-change callback for BlinkUp Trigger Pins | |
pin.configure(DIGITAL_IN, function() { | |
// trigger only on rising edges, when BlinkUp is not already running | |
if (pin.read() && !sendingBlinkUp) { | |
sendingBlinkUp = true; | |
imp.wakeup(blinkUpTime, function() { | |
sendingBlinkUp = false; | |
}); | |
// send factory BlinkUp | |
server.factoryblinkup(SSID, PASSWORD, blinkup_pin, BLINKUP_FAST); | |
} | |
}); | |
} | |
function configureFactoryFixture() { | |
// assign pins | |
led_red <- hardware.pin5; | |
led_green <- hardware.pin7; | |
blinkup_pin <- hardware.pinC; | |
green_btn <- hardware.pinA; | |
footswitch <- hardware.pinB; | |
// initialize front panel LEDs to Off | |
led_red.configure(DIGITAL_OUT, 0); | |
led_green.configure(DIGITAL_OUT, 0); | |
// intiate factory BlinkUp on either a front-panel button press or footswitch press | |
configureBlinkUpTrigger(green_btn); | |
configureBlinkUpTrigger(footswitch); | |
// print a message on the LCD and store it as default | |
lcd <- CFAx33KL(hardware.uart6E); | |
lcd.clearAll(); | |
lcd.setLine1("Electric Imp"); | |
lcd.setLine2("BlinkUp Fixture"); | |
lcd.setBrightness(100); | |
lcd.storeCurrentStateAsBootState(); | |
} | |
// DEVICE UNDER TEST FUNCTIONS AND CLASSES ------------------------------------ | |
function blessDeviceUnderTest() { | |
// run any tests you require for you DUT | |
local test_success = myTestFunction(); | |
// attempt to bless this device, and send the result to the Factory Test Results Webhook | |
server.bless(test_success, function(bless_success) { | |
// send identifying info and result to the test results webhook | |
local devId = hardware.getdeviceid(); | |
local devMac = imp.getmacaddress(); | |
agent.send("testresult", {device_id = devId, mac = devMac, success = bless_success}); | |
// clear the wifi credentials and reboot if blessing completes successfully | |
if (bless_success) { | |
imp.clearconfiguration(); | |
imp.onidle(function() { | |
// deep sleep for 5 seconds; triggers squirrel reload | |
server.sleepfor(5); | |
}); | |
} | |
}); | |
} | |
// YOUR TEST CODE ------------------------------------------------------------- | |
function myTestFunction() { | |
// run your tests here | |
// returning false will prevent blessing and pass a "FAIL" to the test results webhook | |
return true; | |
} | |
// RUNTIME -------------------------------------------------------------------- | |
if (FactoryTools.isFactoryImp()) { | |
configureFactoryFixture(); | |
} else if (FactoryTools.isDeviceUnderTest()) { | |
blessDeviceUnderTest(); | |
} else { | |
server.log("This firmware is not running in the factory environment"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment