Created
April 25, 2017 17:58
-
-
Save ersatzavian/07bf318b14403a89d013471b5b2ade3c to your computer and use it in GitHub Desktop.
imp002 EVB Test Firmware
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
#require "FactoryTools.device.nut:1.0.0" | |
#require "TMP1x2.class.nut:1.0.3" | |
#require "WS2812.class.nut:2.0.1" | |
#require "LIS3DH.class.nut:1.0.2" | |
// Copyright (c) 2015 Electric Imp | |
// This file is licensed under the MIT License | |
// http://opensource.org/licenses/MIT | |
const SSID = "SSID"; | |
const PASSWORD = "PASSWORD; | |
const BLINKUP_TIME = 5; // seconds; duration of blinkup | |
// average voltage is low because controller pulses VBAT looking for battery | |
const VBAT_THS_LOW = 1.5; | |
const VBAT_THS_HIGH = 4.4; | |
const TIMEOUT_INT = 500; // ms timeout for interrupt check | |
// 8-bit left-justified I2C address (Just an example.) | |
const TMP1x2_ADDR = 0x90; | |
//const LIS3DH_ADDR = 0x30; | |
const LIS3DH_ADDR = 0x32; | |
const NUMPIXELS = 2; | |
const SPICLK = 7500; // kHz | |
function log(msg) { | |
if (server.isconnected()) { | |
server.log(msg); | |
} | |
} | |
function neopixelTest(d = null) { | |
// 0 == green | |
pixelStrip.set(0,[0,40,0]); | |
// 1 == blue | |
pixelStrip.set(1,[0,0,40]); | |
pixelStrip.draw(); | |
} | |
function btn1Pressed() { | |
server.log("Btn1: "+btn1.read()); | |
} | |
function btn2Pressed() { | |
server.log("Btn2: "+btn2.read()); | |
} | |
function beginTone() { | |
led_grn.configure(PWM_OUT, 1.0/500.0, 0.5); | |
} | |
function endTone() { | |
led_grn.configure(DIGITAL_OUT, 0); | |
} | |
function getBatt() { | |
led_red.write(1); | |
local vpin = 0; | |
// take lots of readings because VBAT will pulse while the charger looks for a battery | |
for (local i = 0; i < 10; i++) { | |
vpin += vbat_sns.read(); | |
imp.sleep(0.1); | |
} | |
led_red.write(0); | |
vpin = (vpin / 10) * (hardware.voltage() / 65535.0) * (6.9 / 4.7); | |
return vpin; | |
} | |
function test() { | |
// verify vbat reads ~4.2V | |
local vbat = getBatt(); | |
if (vbat < VBAT_THS_LOW || vbat > VBAT_THS_HIGH) { | |
log("Test FAIL: VBAT out of Range ( "+vbat+" V)"); | |
imp.onidle(function() { server.sleepfor(5); }); | |
return; | |
} | |
log("VBat Check OK ("+vbat+"V)"); | |
// verify accel | |
accel.setDataRate(100); | |
local val = accel.getAccel(); | |
//server.log(format("Acceleration (G): (%0.2f, %0.2f, %0.2f)", val.x, val.y, val.z)); | |
if ("err" in val) { | |
log("Test FAIL: LIS3DH error: "+val.err); | |
imp.onidle(function() { server.sleepfor(5); }); | |
return; | |
} | |
log(format("LIS3DH OK: (%d, %d, %d)",val.x, val.y, val.z)); | |
// verify tmp102 | |
local roomTemp = tempsensor.read(); | |
if ("err" in roomTemp) { | |
log("Test Fail: TMP102 error: "+roomTemp.err); | |
imp.onidle(function() { server.sleepfor(5); }); | |
return; | |
} | |
log("TMP102 OK: "+roomTemp.temp); | |
// disable all interrupts before testing each one | |
tempsensor.setHighThreshold(roomTemp.temp + 20); | |
accel.configureInertialInterrupt(false); | |
accel.getInterruptTable(); | |
if (interrupt.read()) { | |
log("Test Fail: Interrupt asserted before enable"); | |
imp.onidle(function() { server.sleepfor(5); }); | |
return; | |
} | |
// verify accel interrupt | |
accel.configureInertialInterrupt(true, 0.8, 2, LIS3DH.Z_HIGH); | |
local start = hardware.millis(); | |
while (!interrupt.read() && (hardware.millis() - start < TIMEOUT_INT)) { | |
// server.log(interrupt.read()); | |
// val = accel.getAccel(); | |
// log(format("(%d, %d, %d)",val.x, val.y, val.z)); | |
} | |
if (hardware.millis() - start >= TIMEOUT_INT) { | |
log("Test Fail: LIS3DH interrupt timed out"); | |
imp.onidle(function() { server.sleepfor(5); }); | |
return; | |
} | |
log("LIS3DH Interrupt OK"); | |
accel.configureInertialInterrupt(false); | |
accel.getInterruptTable(); | |
// verify tmp102 interrupt | |
tempsensor.setHighThreshold(roomTemp.temp - 5); | |
local start = hardware.millis(); | |
while (!interrupt.read() && (hardware.millis() - start < TIMEOUT_INT)); | |
if (hardware.millis() - start >= TIMEOUT_INT) { | |
log("Test Fail: TMP102 interrupt timed out"); | |
imp.onidle(function() { server.sleepfor(5); }); | |
return; | |
} | |
log("TMP102 Interrupt OK"); | |
// turn that interrupt off | |
tempsensor.setHighThreshold(roomTemp.temp + 20); | |
// start the beep, stop on button 2 press | |
beginTone(); | |
btn2.configure(DIGITAL_IN, function() { | |
if (btn2.read()) { | |
log("Button 2 OK"); | |
endTone(); | |
led_grn.configure(DIGITAL_OUT, 0); | |
// start neopixels on button 2 press | |
neopixelTest(); | |
btn2.configure(DIGITAL_IN); | |
} | |
}); | |
// light red led on button 1 press | |
btn1.configure(DIGITAL_IN, function() { | |
if (btn1.read()) { | |
log("Button 1 OK"); | |
led_red.write(1); | |
log("Test PASS"); | |
btn1.configure(DIGITAL_IN); | |
// clear WiFi. Ready to ship. | |
imp.clearconfiguration(); | |
} | |
}); | |
} | |
/* RUNTIME START -------------------------------------------------------------*/ | |
imp.enableblinkup(true); | |
server.log("Device Online"); | |
function runDUTPath() { | |
log("Device Under Test"); | |
log("SW Version: " +imp.getsoftwareversion()); | |
log("Memory Free: "+imp.getmemoryfree()); | |
// this is the device under test | |
interrupt <- hardware.pin1; | |
btn1 <- hardware.pinD; | |
btn2 <- hardware.pin5; | |
i2c <- hardware.i2c89; | |
spi <- hardware.spi257; | |
led_red <- hardware.pin2; | |
led_grn <- hardware.pinC; | |
vbat_sns <- hardware.pinB; | |
interrupt.configure(DIGITAL_IN); | |
i2c.configure(CLOCK_SPEED_400_KHZ); | |
spi.configure(MSB_FIRST, SPICLK); | |
led_red.configure(DIGITAL_OUT, 0); | |
led_grn.configure(DIGITAL_OUT, 0); | |
vbat_sns.configure(ANALOG_IN); | |
tempsensor <- TMP1x2(hardware.i2c89, TMP1x2_ADDR); | |
accel <- LIS3DH(hardware.i2c89, LIS3DH_ADDR); | |
pixelStrip <- WS2812(spi, NUMPIXELS); | |
test(); | |
} | |
function sendBlinkUp() { | |
// send on button press only, not release | |
if (!btn1.read() && !btn2.read()) { return; } | |
imp.wakeup(0.1, function() { | |
server.log("sending blinkup"); | |
blinkUpPin.write(1); | |
led.write(1); | |
imp.wakeup(BLINKUP_TIME, function() { | |
led.write(0); | |
}); | |
server.factoryblinkup(SSID, PASSWORD, blinkUpPin, BLINKUP_FAST); | |
}); | |
} | |
function runFixturePath() { | |
btn1 <- hardware.pin1; | |
btn2 <- hardware.pin2; | |
blinkUpEn <- hardware.pin7; | |
blinkUpPin <- hardware.pin8; | |
led <- hardware.pin5; | |
btn1.configure(DIGITAL_IN_PULLDOWN, sendBlinkUp); | |
btn2.configure(DIGITAL_IN_PULLDOWN, sendBlinkUp); | |
blinkUpEn.configure(DIGITAL_OUT, 1); | |
blinkUpPin.configure(DIGITAL_OUT, 1); | |
led.configure(DIGITAL_OUT, 0); | |
} | |
if (FactoryTools.isFactoryImp()) { | |
runFixturePath(); | |
} else if (FactoryTools.isDeviceUnderTest()) { | |
runDUTPath(); | |
} else { | |
server.log("This firmware is not running in a factory environment"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment