Bluetooth send float

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Bluetooth send float

Post by mcsummation »

I would like to be able to send a float from an NXT up to the PC using Bluetooth. If someone would tell me where to find the implementation of SendResponseNumber is located, I can implement SendResponseFloat and provide the code for others.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Bluetooth send float

Post by afanofosc »

Here's the underlying implementation from NXTDefs.h:

Code: Select all

#define __sendResponseNumber(_queue, _val, _result) \
  acquire __MWMutex \
  add __MWArgs.QueueID, _queue, 10 \
  mov __SRNTmpVal, _val \
  flatten __MWArgs.Message, __SRNTmpVal \
  syscall MessageWrite, __MWArgs \
  mov _result, __MWArgs.Result \
  release __MWMutex

#define __sendResponseString(_queue, _msg, _result) \
  acquire __MWMutex \
  add __MWArgs.QueueID, _queue, 10 \
  mov __MWArgs.Message, _msg \
  syscall MessageWrite, __MWArgs \
  mov _result, __MWArgs.Result \
  release __MWMutex
You can use something like this:

Code: Select all

#define SendResponseFloat(_queue, _val) \
  asm { \
  acquire __MWMutex \
  add __MWArgs.QueueID, _queue, 10 \
  flatten __MWArgs.Message, _val \
  syscall MessageWrite, __MWArgs \
  mov __RETVAL__, __MWArgs.Result \
  release __MWMutex \
}
The existing SendResponseString could also be used like this:

Code: Select all

inline char SendResponseFloat(byte queue, float val)
{
  string tmp = FlattenVar(val);
  return SendResponseString(queue, tmp);
}
These are completely untested.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth send float

Post by mcsummation »

After crawling through the code dribbled out to the .sym file, I finally came up with a couple of functions:

Code: Select all

char SendResponseFloat(byte queue, float val)
{
     byte _local_buffer[5];
     byte _local_queue = queue + 10;

     asm
     {
        flatten _local_buffer, val
     }
     _local_buffer[4] = 0;
     return(SendMessage (_local_queue, _local_buffer));
}

char ReceiveRemoteFloat(byte queue, bool clear, float & val)
{
     byte _local_buffer[5];
     bool bErr;
     float _local_default = 5;
     char cError;

     cError = ReceiveMessage (queue, clear, _local_buffer);
     asm
     {
        unflatten val, bErr, _local_buffer, _local_default
     }
     return(cError);
}
These may not be as good as yours (I'm still trying to come to grips with NBC), but they are working for me.

I've been writing code for 50 years; however, I can always find new ways to "bother" a computer and its various languages. :geek:
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Bluetooth send float

Post by afanofosc »

You can see the NBC code generated by the compiler if you are using BricxCC without resorting to the .sym file by simply pressing F12.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth send float

Post by mcsummation »

Since the .sym file has line numbers in it, I find it easier to dig through.

I just started on BricxCC, NBC, and NXC last week, so I've still got a LOT to learn. Although, all of these bear striking resemblance to tools/assemblers/compilers I've used in the past.
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth send float

Post by mcsummation »

A problem I ran into was that, using opt=2 in the NXC compiler, caused the brick to hang. I had to use opt=1 (or 0) to get the code to run. Is there some glitch with opt=2? Or did I do some foolish thing?

Edit: Should have included that I'm using BricxCC 3.3.8.9, 1.31 (from Lego) FW, and compiler options of: Use Internal Compiler, Enhanced Firmware, not Ignore system include files, NXT 2.0 compatible firmware, Automatic firmware version, and RICScript.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Bluetooth send float

Post by HaWe »

using NXC it's always better to download the latest enhanced firmware, I would suggest to change this at first.
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth send float

Post by mcsummation »

I downloaded the current "test release" from http://bricxcc.sourceforge.net/test_releases/ and my code works for all opt levels of the NXC compiler.

Thanks for nudging me in the direction of loading a "test release" on my brick + BricxCC.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Bluetooth send float

Post by HaWe »

you're welcom!
:)
I guess "nudging " is something meant to be taken nicely or so although I actually have no idea what that means (even Google translate doesn't) ^^
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth send float

Post by mcsummation »

to nudge: verb
Prod (someone) gently, typically with one's elbow, in order to draw their attention to something.

Yes, it's meant in a friendly way.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests