﻿Build1=Default,B4RDev
Group=Default Group
Library1=rcore
Library2=resp8266wifi
Library3=rirremoteesp8266
Library4=rrandomaccessfile
NumberOfFiles=0
NumberOfLibraries=4
NumberOfModules=0
Version=3.9
@EndOfDesignText@
'This project uses ESP8266 
#Region Project Attributes
	#AutoFlushLogs: True
	#CheckArrayBounds: True
	#StackBufferSize: 300
#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 Server As WiFiServerSocket
	Private espWifi As ESP8266WiFi
	Private astream As AsyncStreams
	Private Ser As B4RSerializator
	Private irsend As IrSend
	
	'sendDENON
	
	Dim intVupAdd As UInt = 0x2A4C
	Dim intVup As ULong = 243990661 'result: 7, 243466381 Dernon vol +
	Dim intVdnAdd As UInt = 0x2A4C
	Dim intVdn As ULong = 243466381 'result: 7, 243466381 Denon vol -
	Dim intMuteAdd As UInt = 0x2A4C
	Dim intMute As ULong = 244121731 'result: 7, 244121731 Denon mute
	
	'sendRaw
	'Dim intCablePwr(47) As ULong = Array As ULong(3330,3370, 830,2570, 830,2570, 830,870, 830,2570, 830,2570, 830,2570, 830,2570, 830,2570, 830,820, 830,870, 830,820, 830,870, 830,820, 830,2570, 830,870, 830,820, 830,870, 780,870, 830,870, 780,2620, 780,2620, 780,2620, 830)

	Dim MacArray(6) As Byte
	Private bc As ByteConverter
	
End Sub

Private Sub AppStart
	
	RunNative( "SetIP" , Null )
	espWifi.Disconnect()

#if C
  void SetIP(B4R::Object* o) {

  IPAddress ip(192, 168, 1, 93);  // (Replace xxx with desired IP) ,93
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);

  }
  #end if	
	
	Serial1.Initialize(115200)
	Log("AppStart")
	
	
	If espWifi.Connect2("********","*******") Then
		
		Server.Initialize(51042, "server_NewConnection")
		Server.Listen
		Log("Waiting for connection.")
		
		Log("My ip: ", espWifi.LocalIp)' 192.168.1.95
		Log(bc.HexFromBytes(MacAddress))
		
	Else
		Log("Connection Failed.")
		astream_Error
	End If
	
	irsend.Enable(4,38) 'GPIO-4 D2, 38HZ
	

End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
	astream.Initialize(NewSocket.Stream,"astream_NewData", "astream_Error")
	Log("server new connection")
	
	
	
End Sub

Sub astream_NewData (Buffer() As Byte)
	Dim be(10) As Object 'used as a storage buffer.
	Dim objects() As Object = Ser.ConvertBytesToArray(Buffer, be)
	Log("astream newdata")
	For Each o As Object In objects
		Select o
				
			Case "vup" ' SSTV Volume Up
				Log("sending: vol+ ", intVup)
				irsend.SendPANASONIC(intVupAdd, intVup)'Uint address,  Ulong data
				astream.Write(Ser.ConvertArrayToBytes(Array("vup")))
				
			Case "vdn" ' SSTV Volume Down
				Log("sending: vol- ", intVdn)
				irsend.SendPANASONIC(intVdnAdd, intVdn)'ULong data,  Int nbits
				astream.Write(Ser.ConvertArrayToBytes(Array("vdn")))
				
			Case "mute" ' SSTV Mute
				Log("sending: mute ", intMute)
				irsend.SendPANASONIC(intMuteAdd, intMute)'ULong data,  Int nbits
				astream.Write(Ser.ConvertArrayToBytes(Array("mute")))
			
		End Select
	Next
	
	
End Sub

Sub astream_Error
	Log ("Astream Error")
	'Display.page_ASError
	Delay (4000)
	Server.Listen
	
End Sub

Sub MacAddress() As Byte()
	RunNative("getMac", Null)
	Return MacArray
End Sub
#if C
  #include <ESP8266WiFi.h>
  void getMac(B4R::Object* u) {
  WiFi.macAddress((Byte*)b4r_main::_macarray->data);
  }
#end if


