#include "Arduino.h"
#include "Declares.h"
#include "NRF.h"
#include "Device.h"
Device_class Device1;
Device_class Device2;
TIMER_E_Mill ShowTO;
TIMER_E_Mill FakesendDataa;
TIMER_E_Mill FakesendRegister;
// ************************ 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 = 30000;
//*****************************************************
ShowTO.SetTimeout = 3000;
FakesendDataa.SetTimeout = 100;
FakesendRegister.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(240);
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 Receiving:
if (!showRec) {
Serial.println(F("Ino Receiveing"));
showRec = true;
showsend = false;
}
Ino_Tier1_Receiving();
break;
}
}
//******************* ino_TimeCycle_Receiving()*******************************************
bool ino_TimeCycle_Receiving()
{
bool returnval = false;
uint8_t Foundpipe;
// fake cycletime received
// Receive_Address[1] would be used at nrf radio side
if (ino_Available_cycletime()) {
Rec_TimePack = Send_TimePack;
if (Rec_TimePack.Device_ID == ino_Receivers_ID)
{
//Serial.println("Correct Device id found") ;
switch (Rec_TimePack.Header) {
case 'T':
Serial.println("Cycletime found") ;
returnval = true;
break;
case 'R':
// NOTE: still send the globaltime similar as in T
//Serial.println("Ino Register found") ;
ino_local_Register = true;
returnval = 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 bool TimeClock_cycle2;
if (!Tier1_Finished2) {
switch (Ino_Tier1_Transmit) {
case CycleTime:
ino_Cycle_TO_Receive.Start_Timeout();
if (!ino_Cycle_TO_Receive.Timeout()) {
if (ino_Role == _Slave && !TimeClock_cycle2) {
TimeClock_cycle2 = ino_TimeCycle_Receiving();
}
} else { //Cycle_TO.Timeout reached
if (TimeClock_cycle2 ) {
switch (ino_local_Register)
{
case true:
Ino_Tier1_Transmit = Register;
break;
case false:
Ino_Tier1_Transmit = Sending;
break;
}
} else if (!TimeClock_cycle2) {
Serial.println("error hit");
Error_H_Index = 4;
}
}
break;
case Register:
if (ino_Register_rec())
{
Tier1_Finished2 = true;
}
break;
case Sending:
break;
case Receiving:
break;
}
} else if (Tier1_Finished2) {
TimeClock_cycle2 = false; // reset
// Tier1_Finished2 = false;
//Ino_Tier1_Transmit = CycleTime;
// Serial.println(" ino Tier1_Finished finished") ;
}
}
//*******************ino_Register_rec()*******************************************
bool ino_Register_rec() {
bool returnval = false;
uint8_t Foundpipe;
ino_register_TO_Receive.Start_Timeout();
if (!ino_register_TO_Receive.Timeout()) {
if (ino_Available_Registersend()) {
Foundpipe = 1; /// just set for testing
if (Foundpipe == 1) {
Rec_RegisterPack = Send_RegisterPack;
if (Rec_RegisterPack.Device_ID == ino_Receivers_ID)
{
ino_AddDevice(Rec_RegisterPack);
returnval = true;
}
}
}
} else { // from default if (!Act_TO.Timeout()) timeout reached
Error_H_Index = 12; // not using at wokwi test
}
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;
Serial.print( "InDEv.Transmit = ") ;
Serial.println(InDEv.Transmit) ;
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]]) ;
}
}