Keyboard->NXT

Discussion specific to the intelligent brick, sensors, motors, and more.
gloomyandy
Posts: 323
Joined: 29 Sep 2010, 05:03

Re: Keyboard->NXT

Post by gloomyandy »

doc-helmut wrote: But I also liked to know something specific information about the NXT BT interface you are using - is it external (plugged by I²C) or do you just connect it to via the NXT built-in BT device control (where you connect e.g. to different NXTs)?
The Bluetooth interface being used is the the standard NXT Bluetooth module, it is not an external device....

Andy
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Keyboard->NXT

Post by HaWe »

splendid!
do you think one might get it working using NXC, too, or is this Lejos firmware specific?
gloomyandy
Posts: 323
Joined: 29 Sep 2010, 05:03

Re: Keyboard->NXT

Post by gloomyandy »

Hi Doc,
I'm not sure if it will work with NXC or not, with luck John will comment... Basically you need to be able to open a simple I/O stream to the device. This means having a Bluetooth connection that does not have the Lego 2 byte packet header in use (in leJOS this is known as RAW mode). I'm not sure if NXC supports this (perhaps it does with the enhanced firmware). This is the same sort of connection needed to talk to things like Bluetooth GPS devices, which I think you can do from NXC but I'm not 100% sure...
nxtreme
Posts: 246
Joined: 29 Sep 2010, 03:53
Location: 192.168.1.2

Re: Keyboard->NXT

Post by nxtreme »

You could probably set something up using the Arduino USB Host Shield from SparkFun (again :)) and a Arduino. Of course, you have to program the Arduino in Arduino-speak but if you can program in NXC it shouldn't be too hard. Then, pass the data off to the NXT using RS-485 (thanks to r.ostrich).

Or, if you want to connect to a Bluetooth keyboard use a Bluetooth module instead of the USB Host Shield.
One King to rule them all, One King to find them,
One King to bring them all and in the darkness bind them
On Earth where Shadows lie.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Keyboard->NXT

Post by afanofosc »

gloomyandy wrote: I'm not sure if it will work with NXC or not, with luck John will comment... Basically you need to be able to open a simple I/O stream to the device. This means having a Bluetooth connection that does not have the Lego 2 byte packet header in use (in leJOS this is known as RAW mode). I'm not sure if NXC supports this (perhaps it does with the enhanced firmware). This is the same sort of connection needed to talk to things like Bluetooth GPS devices, which I think you can do from NXC but I'm not 100% sure...
I've introduced a pair of fields to the Comm module IOMap which let you set the firmware into DATA_MODE_NXT (default for Bluetooth), DATA_MODE_RAW (default for RS485), or DATA_MODE_GPS. BtDataMode and HsDataMode. While working on these changes I also finally noticed that my previously added HsMode field (for setting the RS485 serial port mode) had the wrong offset in the NBCCommon.h header file (which explains why configuring the port to anything other than its default 8N1 mode was not working properly). I have tested setting the HsDataMode to DATA_MODE_NXT so that direct and system commands sent from one NXT to another via RS485 are automatically processed on the receiving side just like what normally happens with incoming USB and Bluetooth data. I have not yet properly tested setting the BtDataMode to values other than DATA_MODE_NXT to make sure everything still works right.

Currently DATA_MODE_GPS and DATA_MODE_RAW are exactly the same. I am considering adding some code to process GPS sentences at the firmware level if the data mode is set to DATA_MODE_GPS but it hasn't happened yet. In RAW mode, in theory, it should just receive whatever data (up to 128 bytes) that the Bluecore chip sends over the open stream and copy that data into the Comm module's bluetooth input buffer.

However, even with the standard NXT firmware it is very easy to receive GPS data via Bluetooth even with the way it expects 2 bytes at the start that specify the length and how it assumes the contents are a direct or system command. It isn't 100% flawless so this kind of approach may not work well with a Bluetooth keyboard. Here's a pretty simple program that shows how to open a stream to a non-NXT bluetooth device and read whatever that device sends to the NXT. A lot of this is file I/O. All you really need is the function that opens the stream if it is not already open and the function that reads the data from the bluetooth input buffer.

Code: Select all

byte OpenFile(string afile)
{
  byte handle;
  long file_size;
  unsigned int rtn_code = CreateFile(afile, 10048, handle);
  // If the file already exists, open it with the intent of adding to the data 
  // that is already there.
  if (rtn_code == LDR_FILEEXISTS)
     rtn_code = OpenFileAppend(afile, file_size, handle);
  NumOut(0, LCD_LINE8, rtn_code);
  Wait(SEC_2);
  return handle;
}

void RemoteReadGPSData(byte conn)
{
  // is the connection still streaming?
  if (BTConnectionStreamStatus(conn))
    return;
  // otherwise re-open the stream
  TextOut(0, LCD_LINE7, "open stream");
  Wait(SEC_1);
  RemoteKeepAlive(conn);
  until(RemoteConnectionIdle(conn)) Wait(MS_1);
}

#ifdef __ENHANCED_FIRMWARE
#define ReadBTInputBuffer(_buf) GetBTInputBuffer(0, 128, _buf)
#else
#define ReadBTInputBuffer(_buf) \
do { \
  byte b1[64], b2[64]; \
  GetBTInputBuffer(0, 64, b1); \
  GetBTInputBuffer(64, 64, b2); \
  ArrayBuild(_buf, b1, b2); \
} while (0)
#endif

task main()
{
  byte handle;
  unsigned int cnt;
  unsigned long result;
  handle = OpenFile("gps.txt");
  bool okay = true;
  byte buf[128], oldbuf[128];
  byte CRLF[2] = {0x13, 0x10};
  while (okay) {
    // read bytes from bluetooth input buffer
    RemoteReadGPSData(CONN_BT1);
    ReadBTInputBuffer(buf);
    if (buf != oldbuf)
    {
      ClearScreen();
     // write them to a file
      TextOut(0, LCD_LINE3, "writing");
      result = WriteBytes(handle, buf, cnt);
      NumOut(0, LCD_LINE1, result);
      okay = (result == LDR_SUCCESS);
      cnt = 2;
      WriteBytes(handle, CRLF, cnt);

      oldbuf = buf;
    }
    else
    {
      TextOut(0, LCD_LINE4, "same data");
    }

    Wait(MS_50);
  }
  CloseFile(handle);
  Wait(SEC_5);
}
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Keyboard->NXT

Post by HaWe »

two things I don't understand yet:
1) what is this file needed for if I only want to read a data stream "on the fly"?
2) if I connect to the BT keyboard in raw mode: can I still always keep connected from my master NXT to my 3 slave bricks (e.g. like for my BT mux network which needs a very stabile BT Lego mode (2 byte packet header) connection)?
pbenco
Posts: 71
Joined: 29 Sep 2010, 09:43
Contact:

Re: Keyboard->NXT

Post by pbenco »

Hello doc-helmut
1) if nxc coding, you only need to init a buffer, and start to feed/read it, ig NXG coding, don't know, never give it a try ;-)
2) BT and NXT is a matter of limitations (laws???), and the first of all is "master only can reach 3 slaves", so no, i suppose you can't talk to you keyboard AND talk to you 3 NXT slaves at same time. The problem is that a slave can't initiate a communication with the master.
As mattallen37 already propose, use http://www.extremenxt.com/keyboard.html with a PS/2 keyboard!!! :-D
Regards
pbenco
pbenco.wordpress.com/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Keyboard->NXT

Post by afanofosc »

doc-helmut wrote: 1) what is this file needed for if I only want to read a data stream "on the fly"?
As I mentioned, you don't need any file io to read bluetooth data. I just wanted to write the GPS data I was receiving to a file. You just open the stream by writing some kind of data to the desired connection and then read data from the bluetooth input buffer and process it however you like. I don't have a keyboard like this to test anything with so I can't say this approach, which works with a bluetooth GPS device, will work with one of these SPP bluetooth keyboards.
doc-helmut wrote: 2) if I connect to the BT keyboard in raw mode: can I still always keep connected from my master NXT to my 3 slave bricks (e.g. like for my BT mux network which needs a very stabile BT Lego mode (2 byte packet header) connection)?
If you switch the bluetooth data mode using the enhanced NBC/NXC firmware into RAW mode then it will not automatically process incoming data as a direct command so any bluetooth communication between NXTs while you are running your firmware in RAW data mode would have to be handled entirely by the user program rather than letting the firmware do part of the work (i.e., as it does for MessageRead direct commands or any other direct or system command). But that would not prevent you from implementing a lower level protocol for sending data back and forth between two or more NXTs while in RAW data mode.

As pbenco mentioned, though, you can't connect to more than 3 devices so you could not be simultaneously connected to a bluetooth keyboard and to 3 slave bricks.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: Keyboard->NXT

Post by nxtboyiii »

Maybe you could somehow get a keyboard from an old phone and build an adapter.
Thanks, and have a nice day,
nxtboy III

programnxt.com
pbenco
Posts: 71
Joined: 29 Sep 2010, 09:43
Contact:

Re: Keyboard->NXT

Post by pbenco »

Hello doc-helmut

if you want to use 3 slaves and a master, and a bluetooth keyboard, you can use daisy chaining, to overcome the Bluetooth limitation of 3 slaves/
One BT MASTER, connected to keyboard, One BT MASTER connected to 2 Slaves, and the 2 Masters connected via RS485 (simply connect Sensor_Port4 of both masters).
Just an idea...

[keyboard] ))BT(( [NXT Master1]<____Port sensor4___>[NXT Master2] ))BT(([NXT SLAVE1&2]

One of the main brawback is the 2 bt channels use, is possible collision of packets, perhaps full RS485 will be more reliable, but you will need a special cable or adapter.
pbenco.wordpress.com/
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests