#
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Private timer1 As Timer
Private master As WireMaster
Private const SLAVE_ADDRESS As Byte = 64
Private temp(300) As Float
Private humid(300) As Float
Private counter=0 As Int
Private pin13 As Pin
Private oled As AdafruitSSD1306
Private free As Int
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
master.Initialize
pin13.Initialize(13, pin13.MODE_OUTPUT)
timer1.Initialize("Timer1_Tick", 1000)
oled.InitializeI2C(10,0x3c)
timer1.Enabled = True
RunNative("FreeRam", Null)
Log(free)
End Sub
Sub Timer1_Tick
timer1.Enabled=False
pin13.DigitalWrite(True)
Dim readbyte(3) As Byte
Dim writebyte(1) As Byte
writebyte(0)=0xe3 'read temp register
master.WriteTo2(SLAVE_ADDRESS,True,writebyte)
Delay(50)
readbyte = master.RequestFrom(SLAVE_ADDRESS,3)
If readbyte.Length=3 Then
Dim partial1=(Bit.ShiftLeft(Bit.And(readbyte(0),0xff),8)+Bit.And(readbyte(1),0xfc)) As Int
Dim partial2=-46.85+(175.72*(partial1/65536)) As Float
Else
'Log(readbyte.length)
End If
Delay(50)
writebyte(0)=0xe5 'read humidity register
master.WriteTo2(SLAVE_ADDRESS,True,writebyte)
Delay(50)
readbyte = master.RequestFrom(SLAVE_ADDRESS,3)
If readbyte.Length=3 Then
Dim partial3=(Bit.ShiftLeft(Bit.And(readbyte(0),0xff),8)+Bit.And(readbyte(1),0xfc)) As Int
Dim partial4=-6+(125*(partial3/65536)) As Float
Else
' Log(readbyte.length)
End If
' Log(NumberFormat(partial2*1.8+32,1,1),",",Round(partial4))
If counter >= 299 Then
temp(counter)=partial2*1.8+32
humid(counter)=partial4
Dim sum=0 As Float
For i=0 To 299
sum=sum+temp(i)
Next
Dim avgtemp=(sum/300) As Float
sum=0
For i=0 To 299
sum=sum+humid(i)
Next
Dim avghumid=(sum/300) As Float
counter=0
Log(avgtemp,",",avghumid)
Else
temp(counter)=partial2*1.8+32
humid(counter)=partial4
' Log(counter)
Log(temp(counter))
Log(humid(counter))
oled.ClearDisplay
oled.GFX.SetCursor(0,0)
oled.GFX.ConfigureText(2,oled.WHITE,False)
oled.GFX.DrawText(NumberFormat(temp(counter),1,1)).DrawText(" F").DrawText(CRLF)
oled.GFX.DrawText(temp(counter)).DrawText(" F").DrawText(CRLF)
' oled.GFX.DrawText(NumberFormat(humid(counter),1,1)).DrawText(" %")
oled.Display
counter=counter+1
End If
pin13.DigitalWrite(False)
timer1.Enabled=True
RunNative("FreeRam", Null)
Log("-")
Log (free)
Log(StackBufferUsage)
Log("--")
End Sub
#if C
extern "C" char *sbrk(int i);
void FreeRam (B4R::Object* o) {
char stack_dummy = 0;
b4r_main::_free = &stack_dummy - sbrk(0);
}
#End if