I didn't translate your text - to my experience google translate mixes and muddles up any foreign language text into something completely unintelligible and meaningless stuff. So I just tried to understand the sense of the English text.
I don't know if I understood something wrong or miss something, but I was searching for "init" in your source code but couldn't find anything like this.
But I usually find it hard to understand "foreign" code and to navigate through it.
Or should I write a routine BT_init by my own where I put in some of your BT functions ? (Which?)
Maybe you could give an example of source code how to use it on the master and a slave to do something more or less useless?
Code: Select all
// file: BT_JHS_TOOLS_H.nxc
// This is a collection of Bluetooth things from James Summers (also known as McSummation).
// This will wait for the Bluetooth link to become idle.
// Use - WaitBT(connection_number);
#define WaitBT(Conn) until (RemoteConnectionIdle(Conn)) Yield()
#ifndef JHS_BT
#define JHS_BT
mutex JHS_BT_mutex;
bool bBT_Stop = false;
// Implement a thread-safe counting semaphore.
mutex McSummation;
inline long JHSSemaphoreUp(long &Sem) {Acquire (McSummation); Sem++; Release (McSummation); return Sem;}
inline long JHSSemaphoreDown( long &Sem) {Acquire (McSummation); Sem--; Release (McSummation); return Sem;}
#endif
// To kill the "receive" routines while they are waiting for a message, use JHSStopBT();
// When the "receive" routine returns, the return code will be "1";
// otherwise, it is the return code from the underlying "receive" routine.
inline bool JHSStopBT() {bBT_Stop = true; return bBT_Stop;}
inline bool JHSIsBtStopping() {return bBT_Stop;}
// These connect/disconnect a Slave which has been paired with this Master over the specified connection.
bool ConnectBT(byte Conn, string SlaveName);
sub DisconnectBT(byte Conn);
// This will send a file from the Master to a Slave NXT.
inline int BTSendFile(byte Conn, string FileName);
// These versions of the Bluetooth mailbox functions are thread-safe.
inline char JHSSendRemoteBool( byte conn, byte queue, bool bval);
inline char JHSReceiveRemoteBool( byte conn, byte queue, bool clear, bool &bval, bool bwait = true);
inline char JHSSendResponseBool( byte conn, byte queue, bool bval);
inline char JHSSendRemoteNumber( byte conn, byte queue, long val);
inline char JHSReceiveRemoteNumber( byte conn, byte queue, bool clear, long &val, bool bwait = true);
inline char JHSSendResponseNumber( byte conn, byte queue, long val);
inline char JHSSendRemoteString( byte conn, byte queue, string val);
inline char JHSReceiveRemoteString( byte conn, byte queue, bool clear, string &val, bool bwait = true);
inline char JHSSendResponseString( byte conn, byte queue, string val);
inline char JHSSendRemoteFloat( byte conn, byte queue, float val);
inline char JHSReceiveRemoteFloat( byte conn, byte queue, bool clear, float &val, bool bwait = true);
inline char JHSSendResponseFloat( byte conn, byte queue, float val);
// These are methods of sending a structure to/from the slave.
#define JHSSendRemoteStruct(__conn, __queue, __struct) { \
string msg; \
msg = FlattenVar(__struct); \
JHSSendRemoteString(__conn, __queue, msg); }
#define JHSReceiveRemoteStruct(__conn, __queue, __struct) \
string msg; \
JHSReceiveRemoteString(__conn, __queue, true, msg); \
UnflattenVar(msg, __struct); \
NumOut(0,LCD_LINE8,StrLen(msg));
#define JHSSendResponseStruct(__conn, __queue, __struct) \
string msg; \
msg = FlattenVar(__struct); \
JHSSendResponseString(__conn, __queue, msg);
// This is used to send a file over the Bluetooth link.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline int BTSendFile(byte Conn, string FileName) {
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifndef JHS_CEFT
#define JHS_CEFT
CommExecuteFunctionType JHS_CEFT_args;
#endif
JHS_CEFT_args.Cmd = INTF_SENDFILE;
JHS_CEFT_args.Param1 = Conn;
JHS_CEFT_args.Name = FileName;
SysCommExecuteFunction(JHS_CEFT_args);
until (RemoteConnectionIdle(Conn)) Yield();
return JHS_CEFT_args.Result;
}
// This links to a Slave NXT.
// It returns true if the link was successful.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool ConnectBT(byte Conn, string SlaveName)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
CommBTConnectionType args;
args.Name = SlaveName; // Name of the slave NXT
args.ConnectionSlot = Conn; // This is the desired connection slot
args.Action = true; // Connect to the slave
if(BluetoothStatus(Conn) != NO_ERR)
{
SysCommBTConnection(args); // try to connect.
for (int i = 0; i < 10000; i++) // Will try for about 10 seconds before failing.
{
if(BluetoothStatus(Conn)==NO_ERR)
return true; // Connect has completed.
Wait(1);
}
}
return false;
}
// This disconnects the Bluetooth link.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sub DisconnectBT(byte Conn)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
CommBTConnectionType args;
args.ConnectionSlot = Conn; // This is the desired connection slot
args.Action = 0; // Disconnect
if(BluetoothStatus(Conn) == NO_ERR)
{
// It is connected, so disconnect it.
SysCommBTConnection(args);
for (int i = 0; i < 10000; i++) // Will try for about 10 seconds.
{
if(BluetoothStatus(Conn)!=NO_ERR)
break; // Disconnect has completed.
Wait(1);
}
}
}
// BT Bool functions.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSSendRemoteBool( byte conn, byte queue, bool bval)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
char rc;
until (false)
{
Acquire(JHS_BT_mutex);
if (RemoteConnectionIdle(conn))
break;
else
{
Release(JHS_BT_mutex);
Yield();
}
}
rc = SendRemoteBool(conn, queue, bval);
Release(JHS_BT_mutex);
return rc;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSReceiveRemoteBool( byte conn, byte queue, bool clear, bool &bval, bool bwait = true)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
queue += (conn << 6);
if (bwait)
{
until (NO_ERR == ReceiveRemoteBool(queue, false, bval))
{
Wait(1);
if (bBT_Stop)
{
return 1;
break;
}
}
}
if (!bBT_Stop)
{ return ReceiveRemoteBool(queue, clear, bval); }
else
{ return 1; }
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSSendResponseBool( byte conn, byte queue, bool bval)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
// This doesn't add any value, but including it makes the function set complete.
// Again, the "conn" doesn't do anything, it makes the functions consistent.
return SendResponseBool( queue, bval);
}
// BT Number functions.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSSendRemoteNumber( byte conn, byte queue, long val)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
char rc;
until (false)
{
Acquire(JHS_BT_mutex);
if (RemoteConnectionIdle(conn))
break;
else
{
Release(JHS_BT_mutex);
Yield();
}
}
rc = SendRemoteNumber(conn, queue, val);
Release(JHS_BT_mutex);
return rc;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSReceiveRemoteNumber( byte conn, byte queue, bool clear, long &val, bool bwait = true)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
queue += (conn << 6);
if (bwait)
{
until (NO_ERR == ReceiveRemoteNumber(queue, false, val))
{
Wait(1);
if (bBT_Stop)
{
return 1;
break;
}
}
}
if (!bBT_Stop)
{ return ReceiveRemoteNumber(queue, clear, val); }
else
{ return 1; }
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSSendResponseNumber( byte conn, byte queue, long val)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
// This doesn't add any value, but including it makes the function set complete.
// Again, the "conn" doesn't do anything, it makes the functions consistent.
return SendResponseNumber( queue, val);
}
// BT String functions.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSSendRemoteString( byte conn, byte queue, string val)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
char rc;
until (false)
{
Acquire(JHS_BT_mutex);
if (RemoteConnectionIdle(conn))
break;
else
{
Release(JHS_BT_mutex);
Yield();
}
}
rc = SendRemoteString(conn, queue, val);
Release(JHS_BT_mutex);
return rc;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSReceiveRemoteString( byte conn, byte queue, bool clear, string &val, bool bwait = true)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
queue += (conn << 6);
if (bwait)
{
until (NO_ERR == ReceiveRemoteString(queue, false, val))
{
Wait(1);
if (bBT_Stop)
{
return 1;
break;
}
}
}
if (!bBT_Stop)
{ return ReceiveRemoteString(queue, clear, val); }
else
{ return 1; }
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSSendResponseString( byte conn, byte queue, string val)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
// This doesn't add any value, but including it makes the function set complete.
// Again, the "conn" doesn't do anything, it makes the functions consistent.
return SendResponseString( queue, val);
}
// BT float functions
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSSendRemoteFloat( byte conn, byte queue, float val)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
char rc;
string sTemp;
until (false)
{
Acquire(JHS_BT_mutex);
if (RemoteConnectionIdle(conn))
break;
else
{
Release(JHS_BT_mutex);
Yield();
}
}
sTemp = FlattenVar(val);
rc = SendRemoteString(conn, queue, sTemp);
Release(JHS_BT_mutex);
return rc;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSReceiveRemoteFloat( byte conn, byte queue, bool clear, float &val, bool bwait = true)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
char rc;
string sTemp;
queue += (conn << 6);
if (bwait)
{
until (NO_ERR == ReceiveRemoteString( queue, false, sTemp))
{
Wait(1);
if (bBT_Stop)
{
return 1;
break;
}
}
}
if (!bBT_Stop)
{
rc = ReceiveRemoteString( queue, clear, sTemp);
UnflattenVar(sTemp, val);
return rc;
}
else
{ return 1; }
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
inline char JHSSendResponseFloat( byte conn, byte queue, float val)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
char rc;
string sTemp;
until (false)
{
Acquire(JHS_BT_mutex);
if (RemoteConnectionIdle(conn))
break;
else
{
Release(JHS_BT_mutex);
Yield();
}
}
sTemp = FlattenVar(val);
rc = SendResponseString(queue, sTemp);
Release(JHS_BT_mutex);
return rc;
}