Skip to content

Instantly share code, notes, and snippets.

@analog-io
Created April 12, 2015 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save analog-io/0002114a780e6ae2a181 to your computer and use it in GitHub Desktop.
Save analog-io/0002114a780e6ae2a181 to your computer and use it in GitHub Desktop.
Another MAG3110 test
// Create a data stream at data.sparkfun.com, then enter your keys here:
local publicKey = "x"
local privateKey = "x"
data_array <- array(800,[0,0,0,0])
name_array <- ["x","y","z"]
total <- 0
local req = http.get("https://data.sparkfun.com/output/"+publicKey+"/latest.json");
local response = req.sendsync()
local data = http.jsondecode(response.body)
total = data[0].cnt_total.tointeger()
server.log(total)
function bufferRx(data) {
data_array <- data
local cnt = data[1][4]
total += cnt
// Prepare the phant headers
local phantHeaders = {"Phant-Private-Key": privateKey, "connection": "close"};
// Create a post request
local req = http.post("http://data.sparkfun.com/input/"+publicKey, phantHeaders,"cnt="+cnt+"&cnt_total="+total);
//Send out the request!
server.log(req.sendsync().statuscode);
}
function request_handler(request, response)
{
// The parameter request is the incoming HTTP request
// The parameter response is an auto-generated HTTP response
try
{
// Process incoming http request
response.header("access-control-allow-origin", "*");
// If everything worked as expected, send a standard 'OK' status code (200)
local r = "{"
local i=0
local k=0
for (k=0;k<name_array.len();k++) {
if (k!=0) {r += ",\n"}
r += "\""+name_array[k]+"\":["
for (i=0;i<data_array[0].len();i++) {
if (i!=0) {r += ","}
r += data_array[0][i][k]
}
r += "]"
}
r += ","
for (k=0;k<name_array.len();k++) {
r += "\""+name_array[k]+"_min\":"+data_array[k+1][0]+","
r += "\""+name_array[k]+"_max\":"+data_array[k+1][1]+","
r += "\""+name_array[k]+"_thresh\":"+data_array[k+1][2]+","
r += "\""+name_array[k]+"_spread\":"+data_array[k+1][3]+","
r += "\""+name_array[k]+"_trigs\":"+data_array[k+1][4]
if (k!=2) {r += ","}
}
r+="}"
response.send(200, r);
}
catch (exception)
{
response.send(500, "Internal Server Error: " + exception);
}
}
device.on("buffer",bufferRx);
http.onrequest(request_handler);
// Define the Sensor Address
const ADDR = 0x1C;
// Define register map
const DR_STATUS = "\x00";
const OUT_X_MSB = "\x01";
const OUT_X_LSB = "\x02";
const OUT_Y_MSB = "\x03";
const OUT_Y_LSB = "\x04";
const OUT_Z_MSB = "\x05";
const OUT_Z_LSB = "\x06";
const WHO_AM_I = "\x07";
const SYSMOD = "\x08";
const OFF_X_MSB = "\x09";
const OFF_X_LSB = "\x0A";
const OFF_Y_MSB = "\x0B";
const OFF_Y_LSB = "\x0C";
const OFF_Z_MSB = "\x0D";
const OFF_Z_LSB = "\x0E";
const DIE_TEMP = "\x0F";
const CTRL_REG1 = "\x10";
const CTRL_REG2 = "\x11";
const DR_1280 = "\x00";
const DR_640 = "\x20";
const DR_320 = "\x40";
const DR_160 = "\x50";
const DR_80 = "\x80";
const DR_40 = "\xA0";
const DR_20 = "\xC0";
const DR_10 = "\xE0";
const OSR_16 = "\x00";
const OSR_32 = "\x08";
const OSR_64 = "\x10";
const OSR_128 = "\x18";
const FAST_READ = "\x04";
const TRIGGER_MEAS = "\x02";
const ACTIVE = "\x01";
const AUTO_MRST_EN = "\x80";
const RAW = "\x20";
const MAG_RST = "\x10";
// Specify the update rate in seconds
const update_rate = 5;
// Define the i2c periphrial being used
i2c <- hardware.i2c89;
int <- hardware.pin7;
x_min <- 32767
x_min_cyc <- 32767
y_min <- 32767
z_min <- 32767
x_max <- -32767
x_max_cyc <- -32767
y_max <- -32767
z_max <- -32767
x_prev <- 0
x_count <- 0
x_int <- 0
x_int_neg <- 0
x_hist <- false
x_dd <- false
x_hf <- false
z_dd <- false
debounce <- 130
x_start <- 0
x_trigs <- 0
x_trigs_hf <- 0
y_trigs <- 0
z_trigs <- 0
x_thresh <- 0
y_thresh <- 0
z_thresh <- 0
x_spread <- 0
y_spread <- 0
z_spread <- 0
calibrate <- 0;
buffer_size <- 800;
index <- 0;
buffer <- array(buffer_size);
// Configure said periphrial
i2c.configure(CLOCK_SPEED_400_KHZ);
i2c.write(ADDR,CTRL_REG2+AUTO_MRST_EN)
i2c.write(ADDR,CTRL_REG1+"\x01")
local data = i2c.read(ADDR,OUT_X_MSB,6);
function mag_isr() {
if (int.read()==1)
{
local data = i2c.read(ADDR,OUT_X_MSB,6);
if (calibrate ==0) {
server.log("calibrate")
calibrate <- 1;
i2c.write(ADDR,OFF_X_MSB+data)
}
else
{
local x = data[0]<<8 | data[1]
if (x & 0x8000) {x = -((~x & 0x7FFF) + 1);}
local y = data[2]<<8 | data[3]
if (y & 0x8000) {y = -((~y & 0x7FFF) + 1);}
local z = data[4]<<8 | data[5]
if (z & 0x8000) {z = -((~z & 0x7FFF) + 1);}
if (x<x_min) {x_min = x; x_spread = x_max-x_min; x_thresh = x_min+x_spread/2}
if (x<x_min_cyc) {x_min_cyc = x}
if (y<y_min) {y_min = y; y_spread = y_max-y_min; y_thresh = y_min+y_spread/2}
if (z<z_min) {z_min = z; z_spread = z_max-z_min; z_thresh = z_min+z_spread/2}
if (x>x_max) {x_max = x; x_spread = x_max-x_min; x_thresh = x_min+x_spread/2}
if (x>x_max_cyc) {x_max_cyc = x}
if (y>y_max) {y_max = y; y_spread = y_max-y_min; y_thresh = y_min+y_spread/2}
if (z>z_max) {z_max = z; z_spread = z_max-z_min; z_thresh = z_min+z_spread/2}
server.log("x: "+x+" | y:"+y+" | z:"+z)
if (x>x_thresh) {
if (!x_hf){
x_hf = true
x_trigs_hf++
}
x_count++
x_int += (x-x_thresh)
if (x_int>debounce) {
if (!x_dd) {
x_trigs++
x_dd=true
x_int_neg = 0
}
}
}
else {
x_hf = false
x_int_neg += (x_thresh-x)
if (x_int_neg>debounce) {
x_int = 0
x_dd = false
}
}
if (index<buffer_size)
{
buffer[index] = [x,y,z]
index++
}
else
{
index = 0
agent.send("buffer",[buffer,[x_min,x_max,x_thresh,x_trigs_hf,x_trigs],[y_min,y_max,y_thresh,x_max_cyc-x_min_cyc,y_trigs],[z_min,z_max,z_thresh,z_spread,z_trigs]])
x_trigs = 0
x_trigs_hf = 0
x_max_cyc = -32767
x_min_cyc = 32767
}
}
}
}
int.configure(DIGITAL_IN, mag_isr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment