Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ElectricImpSampleCode/2d74639255a2782fd3937f21b6b5a271 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/2d74639255a2782fd3937f21b6b5a271 to your computer and use it in GitHub Desktop.
Sample fixture factory firmware
// ------------------------------------------------------------------------------
// File: factory.firmware.device.nut
// Version: 1.1.2
//
// Copyright 2015-18 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// ------------------------------------------------------------------------------
// ***************************************
// ***** SETUP *****
// ***************************************
// Load the Factory Tools Library, which simplifies detecting Fixture or DUT
#require "FactoryTools.class.nut:2.1.0"
// Enter your factory WiFi credentials as the values of these constants
const SSID = "SSID";
const PASSWORD = "PASSWORD";
// Sets how long to wait (seconds) after triggering BlinkUp before allowing another
const BLINKUP_TIME = 10;
// Flag used to prevent new BlinkUp triggers while BlinkUp is running
local sendingBlinkUp = false;
local blinkUpCount = 0;
local button = null;
local blinkUpPin = null;
// ***************************************
// ***** FACTORY FIXTURE FUNCTIONS *****
// ***************************************
function configureFactoryFixture() {
// Assign the pins
button = hardware.pinP;
blinkUpPin = hardware.pinA;
// Set up the BlinkUp trigger button
button.configure(DIGITAL_IN_PULLUP, function() {
// Trigger only on rising edges, when BlinkUp is not already running
if (button.read() == 0 && !sendingBlinkUp) {
sendingBlinkUp = true;
imp.wakeup(BLINKUP_TIME, function() {
sendingBlinkUp = false;
});
// Send factory BlinkUp
server.factoryblinkup(SSID, PASSWORD, blinkUpPin, BLINKUP_FAST | BLINKUP_ACTIVEHIGH);
blinkUpCount++;
server.log("BlinkUp number " + blinkUpCount + " performed");
}
});
}
// ***************************************
// ** DEVICE UNDER TEST (DUT) FUNCTIONS **
// ***************************************
function blessDeviceUnderTest() {
// Run any tests you require for you DUT
local testSuccess = myTestFunction();
// Attempt to bless this device, and send the result to the Factory Test Results Webhook
server.bless(testSuccess, function(blessSuccess) {
// Clear the WiFi credentials and reboot if blessing completes successfully
if (blessSuccess) {
imp.clearconfiguration();
// The imp will go idle at this point, allowing impOS to install
// application code and restart the Squirrel VM if the Production Device Group’s
// devices are set to receive application code immediately after blessing
// If you are installing your application upon device activation (first
// end-user BlinkUp), you can power down the device at this point
}
});
}
// ***************************************
// ***** YOUR HARDWARE TEST CODE *****
// ***************************************
function myTestFunction() {
// Run your tests here
// Returning false will prevent blessing and pass a "FAILED" to the test results webhook
return true;
}
// ***************************************
// ***** RUNTIME START *****
// ***************************************
// We use the Factory Tools library's isFactoryImp() call asynchronously in order to be
// sure the impFactory is online and has received device status data from the server
FactoryTools.isFactoryImp(function(isImpFactory) {
if (isImpFactory) {
// Device is an impFactory unit
configureFactoryFixture();
} else {
// The next call need not be asynchronous since we can be sure
// at this point (we're executing in a callback) that the status
// data has now been received by the device (whatever it is)
if (FactoryTools.isDeviceUnderTest()) {
// Device is a production unit - test it
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