-
Notifications
You must be signed in to change notification settings - Fork 10
Modifying the code
Browse through the RN-42 Command Set and HID User's Manual and you'll find all sorts of settings to play with on the module.
I've written functions to make it easier to change some of the more configurable settings: the name, authentication mode, operation mode, and low-power mode settings.
The makeyMate.begin(...)
function in the makeyMateClass
class takes one paramater - an up-to-20 character string. You can pass an array of any ASCII characters to this function; the only stipulation is that the array must be at most (it can be less) 20 characters, and it must be terminated with a \r
character.
There is a definition in maKeyMate_BT.ino on line 118 which is passed to the makeyMate.begin()
function in the setup()
. That's probably the easiest place to modify the name.
I've tried to keep these settings as general as possible. By default Authentication (which require's the device's pin code to be entered on pairing) is enabled, and the Mode is set to Auto-connect DTR.
It seems like some devices (Androids in particular) require authentication (entering the device's pin code) to be on, in order to work at all. Feel free to play with this though. Authentication enable/disable is set using the setAuthentication(...)
member function, and is called in makeyMate.begin(...)
. You can pass this function either 0 (to disable) or 1 (to enable). Modify this parameter on line 47 of makeyMate.cpp.
The RN-42 has 6 possible modes of operation:
- Slave mode (SM,0): This is the default mode, whereby other Bluetooth devices can discover and connect to the device. Outbound connections can also be made in this mode.
- Master Mode (SM,1): This mode is useful when the device wants to initiate connections (not receive them). In this mode the device will NOT be discoverable or connectable.
- Trigger Master Mode (SM,2): In this mode, the device will automatically connect to the pre configured remote slave address when a character (or characters) are received on the local UART. Connection will remain open until a configurable idle timer (1 to 255 seconds) expires with no data being received, or a configurable BREAK character is seen.
- Auto-connect (Master Mode) (SM,3): If this mode is set, the device will initiate a connection to the pre-stored remote address immediately upon power up. If no address is stored, an inquiry process will be attempted and the first device found that matches the COD will be stored. In this mode, data is passed without being interpreted by the Blueport (high speed), hence the connection cannot be broken via command. If disconnect occurs, the device will attempt to re-connect until successful.
- Auto-connect (DTR Mode) (SM,4): This mode must be set by command. This mode operates like Auto-Connect Master mode, except that the connection and disconnection are controlled by PIO6 on the Bluetooth module. Setting the PIO6 high will initiate auto-connect process, and low will cause a disconnect.
- Auto-connect ANY Mode (SM,5): This mode must be set by command. This mode operates like Auto-connect DTR mode, except that each time the PIO6 is set, an inquiry is performed and the first device found is connected. The stored address is NOT used, and the found address is never stored. (Note: PIO6 is not broken out on the bluetooth mate for those last two modes).
I've found mode 0 (Slave mode) to work best, because that's really what the bluetooth mate is. Feel free to try out any of the other modes, though, by setting the parameter in the setMode(...)
member function to between 0 and 5. See this function call on line 49 of makeyMate.cpp.
If you're trying to optimize for battery life, you may want to mess with the low power setting.
In the makeyMate.begin()
function of the makeyMateClass a call is made to the setSleepMode(...)
member function (see line 55 of makeyMate.cpp). This function makes a call to the RN-42 SNIFF Rate command (SW,...), passing a 4-character array as you would if you were straight-up sending that command.
By default, this function passes "0000"
. Which completely turns off any low power modes. This will optimize for as low-latency as possible, but does consume more current (so your battery won't last as long). I measured this mode to use about 40mA on average.
If you're looking for lower-power consumption, I'd recommend sending "80A0"
as the parameter. This enables deep sleep mode, where the module wakes up every 100ms to send data. This is a pretty dramatic decrease in current-usage. I measure it to use about 25mA on average, so nearly half the "0000" mode. You might notice an increase in latency with this mode though. I've found it harder to keep proper time on my apple drums with that higher latency. So I set the default to disable low-power mode.
See section 5.2 of the RN-42 User's Manual for more info on setting this value.