#include "Arduino.h"
#include "Declares.h"
#include "NRF.h"
#include "Device.h"
Device_class Device1;
Device_class Device2;
TIMER_E_Mill ShowTO;
// ************************ INO as another device*************************
bool showsend; // just testing
bool showRec;// just testing
uint8_t ino_Device_ID = 200;
uint8_t ino_Receivers_ID = 100; // acts if device was registered
enum_NRFRole ino_Role = _Slave;
enum_Transmit_type Ino_Transmit = Receiving;
enum_Transmit_type Ino_Tier1_Transmit = CycleTime;
TIMER_E_Mill ino_Cycle_TO_Receive;
TIMER_E_Mill ino_register_TO_Receive;
bool ino_local_Register;
struct_Device ino_Device[6];
bool ino_isRegister[6];
uint8_t ino_Device_Count = 0;
// ******************************************************************************
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial) {
// some boards need to wait to ensure access to serial over USB
}
Serial.println("-----------------------------");
//**************ino setupe****************************
ino_Cycle_TO_Receive.SetTimeout = 200;
ino_register_TO_Receive.SetTimeout = 600;
//*****************************************************
ShowTO.SetTimeout = 3000;
NRF.SetRole(_Master);
NRF.Set_Transmit(Sending);
NRF.init();
Device1.Set_Transmit(Sending);
Device1.Set_Control(ManualACK);
Device1.Set_Valtypes(isuint8_t);
Device1.Set_Receiver_ID(200);
Device1.Set_SendTimer(5000);
Device2.Set_Receiver_ID(200);
Device2.Set_SendTimer(5000);
NRF.Register_Dev(Device1);
// NRF.Register_Dev(Device2);
NRF.CreateAddress();
}
//----------------------------------------------------------------------
//--------------------------------loop---------------------------------
void loop() {
INO_Update();
NRF.Updates();
}
// ********************************Ino function acting as the other Device*************
void INO_Update()
{
switch (Ino_Transmit) {
case Sending:
if (!showsend) {
Serial.println(F("Ino_Tier1_sending"));
showRec = false;
showsend = true;
}
// Ino_Transmit = Receiving;
break;
case Receiving:
if (!showRec) {
Serial.println(F("Ino_Tier1_Receiving"));
showRec = true;
showsend = false;
}
switch (ino_Role)
{
case _Master:
break;
case _Slave:
Ino_Tier1_Receiving();
break;
}
break;
}
}
//******************* ino_TimeCycle_Receiving()*******************************************
enum_Transmit_type ino_TimeCycle_Receiving2()
{
enum_Transmit_type returnval = CycleTime;
uint8_t Foundpipe;
// fake cycletime received
// Receive_Address[1] would be used at nrf radio side
if (ino_Available_cycletime()) {
Send_TimePack.wassend = false; // reset
Rec_TimePack = Send_TimePack;
if (Rec_TimePack.Device_ID == ino_Receivers_ID)
{
switch (Rec_TimePack.Header) {
case 'T':
Serial.println("Cycletime found") ;
switch (ino_Role)
{
case _Master:
returnval = Sending;
break;
case _Slave:
returnval = Receiving;
break;
}
break;
case 'R':
// NOTE: still send the globaltime similar as in T
//Serial.println("Ino Register found") ;
returnval = Register;
ino_local_Register = true;
break;
}
}
}
return returnval;
}
//******************* ino_Available()*******************************************
// just used for wokwi testing
bool ino_Available_cycletime()
{
bool returnval = false;
if (Send_TimePack.wassend ) {
returnval = true;
}
return returnval;
}
//******************* Ino_Tier1_Receiving()*******************************************
void Ino_Tier1_Receiving() {
static bool Tier1_Finished2;
static enum_Transmit_type Transmitset2 = CycleTime; // not sure if needs static
if (!Tier1_Finished2) {
switch (Ino_Tier1_Transmit) {
// ----------- case CycleTime:
case CycleTime:
ino_Cycle_TO_Receive.Start_Timeout();
if (!ino_Cycle_TO_Receive.Timeout()) {
if (Transmitset2 == CycleTime ) {
Transmitset2 = ino_TimeCycle_Receiving2();
}
} else { // timeout reached
Ino_Tier1_Transmit = Transmitset2;
if (Ino_Tier1_Transmit == CycleTime)
{
Error_H_Index = 4;
}
}
break;
// ----------- case Register:
case Register:
ino_register_TO_Receive.Start_Timeout();
if (!ino_register_TO_Receive.Timeout()) {
ino_Register_rec();
} else { // timeout reached
Tier1_Finished2 = true;
}
break;
// ----------- case Receiving:
case Receiving:
Tier1_Finished2 = true;
break;
}
} else if (Tier1_Finished2) {
Tier1_Finished2 = false;
Ino_Tier1_Transmit = CycleTime;
Transmitset2 = CycleTime;
Ino_Transmit = Sending;
}
}
//*******************ino_Register_rec()*******************************************
bool ino_Register_rec() { /// not needed as return can make void
bool returnval = false;
uint8_t Foundpipe;
if (ino_Available_Registersend()) {
Send_RegisterPack.wassend = false;
Rec_RegisterPack = Send_RegisterPack;
if (Rec_RegisterPack.Device_ID == ino_Receivers_ID)
{
ino_AddDevice(Rec_RegisterPack);
returnval = true;
}
}
return returnval;
}
//*******************ino_Available_Registersend()*******************************************
// just used for wokwi testing
bool ino_Available_Registersend()
{
bool returnval = false;
if (Send_RegisterPack.wassend ) {
returnval = true;
}
return returnval;
}
// --------------- NRF_class::AddDevice(struct_Device InDEv) ------------------------------------
//struct_Device ino_Device[6];
//uint8_t ino_Device_Count = 0;
void ino_AddDevice(struct_RegisterPack InDEv)
{
Serial.println( "Add device") ;
ino_Device_Count++;
ino_Device[ino_Device_Count].Control = InDEv.Control;
ino_Device[ino_Device_Count].Val_Types = InDEv.Val_Types;
ino_Device[ino_Device_Count].Pipe_Index = InDEv.Pipe_Index;
ino_Device[ino_Device_Count].Device_Index = ino_Device_Count;
ino_Device[ino_Device_Count].Device_ID = ino_Device_ID;
ino_Device[ino_Device_Count].Receiver_ID = InDEv.Device_ID;
switch (InDEv.Transmit)
{
case Sending:
ino_Device[ino_Device_Count].Transmit = Receiving;
break;
case Receiving:
ino_Device[ino_Device_Count].Transmit = Sending;
break;
}
ino_isRegister[ino_Device_Count] = true;
for (uint8_t i = 1; i <= ino_Device_Count; i++) {
// Serial.print( "ino_Device Receiver_ID [") ;
// Serial.print( i) ;
// Serial.print( "] = ") ;
// Serial.println(ino_Device[i].Receiver_ID) ;
// Serial.print( "ino_Device Transmit [") ;
// Serial.print( i) ;
// Serial.print( "] = ") ;
// Serial.println(Show_Transmit[ino_Device[i].Transmit]) ;
// Serial.print( "ino_isRegister [") ;
// Serial.print( i) ;
// Serial.print( "] = ") ;
// Serial.println(show_bool[ino_isRegister[ino_Device_Count]]) ;
}
}