Skip to content

Instantly share code, notes, and snippets.

View ElectricImpSampleCode's full-sized avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / sha1.agent.nut
Last active December 11, 2019 15:18
Electric Imp imp API hash.sha1() example code
function bobToHexString(data) {
local s = "0x";
foreach (b in data) s += format("%02X", b);
return s;
}
function testHash() {
local data = "The quick brown fox jumps over the lazy dog";
local hash = http.hash.sha1(data);
@ElectricImpSampleCode
ElectricImpSampleCode / sha512.agent.nut
Last active December 11, 2019 15:18
Electric Imp imp API hash.sha512() example code
function blobToHexString(data) {
local s = "0x";
foreach (b in data) s += format("%02X", b);
return s;
}
function testHash() {
local data = "The quick brown fox jumps over the lazy dog";
local hash = http.hash.sha512(data);
@ElectricImpSampleCode
ElectricImpSampleCode / sha256.agent.nut
Last active December 11, 2019 15:17
Electric Imp imp API hash.sha256() example code
function blobToHexString(data) {
local s = "0x";
foreach (b in data) s += format("%02X", b);
return s;
}
function testHash() {
local data = "The quick brown fox jumps over the lazy dog";
local hash = http.hash.sha256(data);
@ElectricImpSampleCode
ElectricImpSampleCode / server.load.agent.nut
Last active December 2, 2019 11:49
Electric Imp imp API server.load() example code
// Establish the device's settings table with default values
deviceSettings <- {};
deviceSettings.mode <- true;
deviceSettings.dst <- true;
deviceSettings.utc <- false;
deviceSettings.offset <- 12;
deviceSettings.brightness <- 15;
// Load preserved settings in from permanent storage
local loadedSettings = server.load();
@ElectricImpSampleCode
ElectricImpSampleCode / network.interface.check.device.nut
Last active November 28, 2019 10:19
impOS 42 local networking interface availability checker
// The 'interfaces' variables holds references to available interfaces
local interfaces = {};
// Set flags that the app can check to see if an interface can be used
local availableForLocal = { "wifi" : false,
"ethernet" : false,
"cell" : false };
// These globals are used for reporting
local interfaceCount = 0;
@ElectricImpSampleCode
ElectricImpSampleCode / array.apply.device.nut
Last active November 26, 2019 15:07
Squirrel array apply() example
local myArray = [true, true, false, false, true];
myArray.apply(function(value) {
return !value;
});
// myArray now equals [false, false, true, true, false]
@ElectricImpSampleCode
ElectricImpSampleCode / array.clear.device.nut
Last active November 26, 2019 15:05
Squirrel array clear() example
// Get all the local networks
local wlans = imp.scanwifinetworks();
local myWlan = null;
foreach (network in wlans) {
if (network.ssid == "hasdrubals_folly") {
// This is my network, so save it
myWlan = network;
break;
}
@ElectricImpSampleCode
ElectricImpSampleCode / array.filter.device.nut
Last active November 26, 2019 15:01
Squirrel array filter() example
local sourceArray = [10, 21, 30, 43];
// Find the array items that are multiples of ten
local resultArray = sourceArray.filter(function(index, value) {
return (value % 10 == 0);
});
server.log(resultArray.len());
server.log(sourceArray.len());
// Displays "2" and "4"
@ElectricImpSampleCode
ElectricImpSampleCode / array.len.device.nut
Last active November 26, 2019 14:55
Squirrel array len() example
// Constants for the alphanumeric character set
static charset = [
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00], // Space - Ascii 32
[0x00,0x10,0x10,0x10,0x10,0x00,0x10,0x00], // !
[0x00,0x24,0x24,0x00,0x00,0x00,0x00,0x00], // "
...
[0xFF,0xFF,0xFF,0xFF,0x55,0xAA,0x55,0xAA],
[0xAA,0x55,0xAA,0x55,0xFF,0xFF,0xFF,0xFF],
[0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55] // Block Graphic 11
];
@ElectricImpSampleCode
ElectricImpSampleCode / array.map.device.nut
Last active November 26, 2019 14:53
Squirrel array map() example
local anArray = [true, true, false, false, true];
// Apply the data conversion function and save the result in a new array
local newArray = anArray.map(function(value) {
return !value;
});
// newArray equals [false, false, true, true, false]