Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ElectricImpSampleCode/4d7456af31b554297573af9eb15514d6 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/4d7456af31b554297573af9eb15514d6 to your computer and use it in GitHub Desktop.
Example Code: Simple Factory Firmware
// ------------------------------------------------------------------------------
// File: simple.factory.firmware.device.nut
// Version: 2.0.0
//
// Copyright 2015-19 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 *****
// ***************************************
// 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");
}
});
}
// ***************************************
// ***** RUNTIME START *****
// ***************************************
// Wait for idle before proceeding in order to ensure imp.configparams
// has been populated on the device (in case it wakes from deep sleep)
imp.onidle(function() {
// Check that we are running in the factory
if ("factoryfirmware" in imp.configparams) {
// The fixture is running factory firmware, ie. it's in a factory
// (or factory test environment) so configure the fixture for BlinkUp
configureFactoryFixture();
} else {
server.error("This fixture (ID: " + hardware.getdeviceid() + ") is not operating in the factory.");
}
}.bindenv(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment