Last active
August 29, 2015 14:14
-
-
Save ianjosephwilson/53fcd6372b19d36b41a4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//a data pin | |
//b clock pin | |
//r result | |
//w poll timeout | |
// @TODO: Improvements | |
// If ack fails reboot scout with scout.boot. | |
// If polling fails reboot scout with scout.boot. | |
function sht.temp.report { | |
f = key(arg(1)); | |
hq.report("exttemp", f); | |
free f; | |
} | |
function sht.temp.poll { | |
if (w > 200) { | |
w = 1; return; | |
} | |
if (dr(a) == 0) { | |
w = 0; | |
pinmode(a, 0); pinmode(b, 1); | |
m = custom.shiftin(a, b, 1); | |
pinmode(a, 1); | |
dw(a, 1); | |
dw(a, 0); | |
dw(b, 1); | |
dw(b, 0); | |
pinmode(a, 0); | |
l = custom.shiftin(a, b, 1); | |
r = ((m << 8) | l); | |
r = (r * 18 - 39500) / 1000; | |
sht.temp.report(r); | |
sht.clearcrc(); | |
} else { | |
w = w + 1; | |
scout.delay(10, "sht.temp.poll"); | |
} | |
} | |
function sht.temp.execute { | |
a = 8; b = 4; w = 1; | |
pinmode(a, 1); pinmode(b, 1); | |
dw(a, 1); | |
dw(b, 1); | |
dw(a, 0); | |
dw(b, 0); | |
dw(b, 1); | |
dw(a, 1); | |
dw(b, 0); | |
custom.shiftout(a, b, 1, 0b00000011); | |
dw(b, 1); | |
pinmode(a, 0) | |
if (dr(a) == 1) { | |
print "ACK error 0"; | |
} | |
dw(b, 0); | |
if (dr(a) == 0) { | |
print "ACK error 1"; | |
} | |
scout.delay(10, "sht.temp.poll"); | |
} | |
function custom.shiftout { | |
x = arg(1); | |
y = arg(2); | |
z = arg(3); | |
v = arg(4); | |
if (z) { | |
i = 0b10000000; | |
while (i > 0) { | |
if (v & i) { | |
dw(x, 1); | |
} else { | |
dw(x, 0); | |
} | |
dw(y, 1); | |
dw(y, 0); | |
i = i >> 1; | |
} | |
} else { | |
i = 0b00000001; | |
while (i > 0) { | |
if (v & i) { | |
dw(x, 1); | |
} else { | |
dw(x, 0); | |
} | |
dw(y, 1); | |
dw(y, 0); | |
i = i << 1; | |
} | |
} | |
} | |
function custom.shiftin { | |
x = arg(1); | |
y = arg(2); | |
z = arg(3); | |
v = 0; | |
if (z == 1) { | |
i = 0b10000000; | |
while (i > 0) { | |
if (dr(x) == 1) { | |
v = v | i; | |
} | |
dw(y, 1); | |
dw(y, 0); | |
i = i >> 1; | |
} | |
} else { | |
i = 0b00000001; | |
while (i > 0) { | |
if (dr(x) == 1) { | |
v = v | i; | |
} | |
dw(y, 1); | |
dw(y, 0); | |
i = i << 1; | |
} | |
} | |
return v; | |
} | |
function sht.clearcrc { | |
pinmode(a, 1); | |
pinmode(b, 1); | |
dw(a, 1); | |
dw(b, 1); | |
dw(b, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment