Created
November 5, 2015 23:16
-
-
Save ersatzavian/235770b54d52ee0bb3a5 to your computer and use it in GitHub Desktop.
Electric Imp Factory Firmware Example
This file contains hidden or 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
// Load in the Electric Imp Factory Tools library | |
#require "FactoryTools.device.nut:1.0.0" | |
// CONSTS AND GLOBALS --------------------------------------------------------- | |
// Replace the following two strings with your factory network’s credentials | |
const SSID = "YOUR_FACTORY_SSID"; | |
const PASSWORD = "YOUR_FACTORY_WIFI_PASSWORD"; | |
const BLINKUP_INTERVAL_SECONDS = 10; | |
// FACTORY FIXTURE FUNCTIONS AND CLASSES -------------------------------------- | |
function configureFactoryFixture() { | |
// You can create global variables for hardware objects using getroottable | |
// This example assumes the basic Electric Imp Factory Fixture design | |
local globalscope = getroottable(); | |
globalscope.btn <- hardware.pin8; | |
globalscope.led <- hardware.pin9; | |
led.configure(DIGITAL_OUT, 0); | |
sendFactoryBlinkUp(); | |
} | |
function sendFactoryBlinkUp() { | |
imp.wakeup(BLINKUP_INTERVAL_SECONDS, sendFactoryBlinkUp); | |
// Use the BLINKUP_ACTIVEHIGH flag if the LED turns on when the LED pin is driven high | |
// Otherwise, Active-Low is assumed (LED on when LED pin is driven low) | |
server.factoryblinkup(SSID, PASSWORD, led, BLINKUP_ACTIVEHIGH); | |
} | |
// DEVICE UNDER TEST FUNCTIONS AND CLASSES ------------------------------------ | |
function blessDeviceUnderTest() { | |
// Call the test suite (dummy function; the code you write will depend on your own device design) | |
local test_success = yourTestFunction(); | |
// Attempt to bless this device, and send the result to the Factory Test Results Webhook | |
server.bless(test_success, function(bless_success) { | |
// If blessing succeeds, clear the configuration so the device will be unconfigured on power cycle | |
if (bless_success) imp.clearconfiguration(); | |
// Log results (only works in developer environment; no logs for devices on the factory line) | |
// this is useful only for developing this factory firmware | |
server.log("Blessing " + (bless_success ? "PASSED" : "FAILED")); | |
// Send results to the Factory Test Result webhook | |
agent.send("testresult", {device_id = deviceid, mac = mac, success = bless_success}); | |
}); | |
} | |
// YOUR TEST CODE ------------------------------------------------------------- | |
function yourTestFunction() { | |
local device_passed = false; | |
// You add code here to test the device’s components and features. The result | |
// will be success (device_passed set to true) or failure (device_passed set to false) | |
return device_passed; | |
} | |
// 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