See also http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1253105481 for more details about MUX ...
So I've come up with a code, that should let me understand a bit how the MUX works while being sure to reset parameters to default each time I reset the board ... (with the help of mikeT and different threads)
I have not tried it yet as I don't have any ArduinoBT working (should be ready tomorrow).
here is the code ! (If anyone sees a big mistake .. do tell me)
/*
* Simon Ruffieux
* Project Arduino Bluetooth Switch - Test MUX
* Sept 2009
* EIA-FR
*/
#include <stdlib.h>
#include <string.h>
#define LED_PIN 13
#define RESET 7
//Frame {0xBF, 0xFF,0x00, dataLength, data, link^0xFF }
byte disableMuxMode[] = {0xBF, 0xFF, 0x00, 0x11, 0x53, 0x45, 0x54, 0x20, 0x43, 0x4f, 0x4e, 0x54, 0x52, 0x4f, 0x4c, 0x20, 0x4d, 0x55, 0x58, 0x20, 0x30, 0x00};
void resetBTModule(){
digitalWrite(RESET, HIGH);
delay(10);
digitalWrite(RESET, LOW);
delay(2000);
}
void setup(){
pinMode(LED_PIN,OUTPUT);
pinMode(RESET, OUTPUT);
Serial.begin(115200);
// Reset the bluetooth interface
resetBTModule();
//Config Bluetooth module
Serial.println("SET CONTROL ESCAPE 43 00 1"); // set escape character to + and 00 = DTR bitmask; 1 = return to command mode when DTR dropped
//Serial.println("SET BT NAME BTSWITCH"); // just change name
Serial.println("SET CONTROL BIND 0 20 RISE SET CONTROL MUX 0"); // in case the command below does not work, i can save the board with the GPIO5
Serial.flush(); // empty serial
int i;
for(i=0;i<sizeof(disableMuxMode);i++){ //send the mux command to disable MUX MODE (note that I could also use the sendMuxcommand .. but as I am not sure the function works ...)
Serial.print(disableMuxMode[i]);
}
}
byte buffer[50];
int bufCounter;
byte c;
int muxMode =0;
void loop(){
// If there is something to read in the serial
if(Serial.available() > 0){
bufCounter =0;
while((c = Serial.read()) != -1){ // as long as there is something to read, put it in buffer
delay(100); // delay a bit just to be sure not to miss something ...
buffer[bufCounter] = c;
bufCounter++;
}
if(!muxMode){
Serial.print("Received: ");
for(int i =0; i<bufCounter; i++){
Serial.print((unsigned char)buffer[i]);
}
Serial.println();
}else{ // If mux mode is o, try to send the received message in a frame to link 00 (link 00 should be the terminal, hopefully)
sendMuxCommand(buffer, 0x00); //Maybe I should convert the buffer in char .. first check if the terminal will receive the response ...
}
//processData();
}
}
/*
* Send a command when MUX is in OFF: Switch to COMMAND mode, send a command and switch back to DATA mode
*/
void sendCommand(char cmd){
//Switch to COMMAND mode
digitalWrite(LED_PIN, HIGH); // set led HIGH
delay(2000);
Serial.print("+++");
delay(2000);
//Send the command
switch(cmd){
case 'n':
{
Serial.println("SET BT NAME BTSWITCH");
break;
}
case 'm':
{
Serial.println("SET CONTROL MUX 1");
muxMode = 1;
//Try to send a MuxMessage to link 0-8 to say mux mode is on (= SET MUX DONE)
byte cmd[] = {0x53, 0x45, 0x54, 0x20, 0x4d, 0x55, 0x58, 0x20, 0x44, 0x4f, 0x4e, 0x45};
sendMuxCommand(cmd,0x00);
break;
}
case 'p':
{
Serial.println("SET BT PAGEMODE 3 2000 2");
//Send a MuxMessage to link 0-8 to say mux mode is on with link
break;
}
default:
{
break;
}
}
//Switch back to DATA mode
if(!muxMode){
delay(2000);
Serial.print("+++");
delay(2000);
}
digitalWrite(LED_PIN, LOW); // set led HIGH
}
/*
* When in MUX mode: Send a command (link = 0xFF) or data (link = 0x00-0x08) encapsulated in a frame
*/
void sendMuxCommand(byte cmd[], byte link){
int cmdLen;
for(cmdLen =0; cmd[cmdLen] != 0x00; cmdLen++){
if(cmdLen > 100){
cmdLen = 0;
break;
}
}
Serial.print(0xBF); //Start of Frame
Serial.print(link); //Link id ( 0x00-0x08 pour les connecs (data), control = 0xFF)
Serial.print(0x00); //Frame flags
Serial.print((unsigned char) cmdLen); // Data length
int i;
for(i=0;i<cmdLen;i++){
Serial.print(cmd[i]); // data
}
Serial.print((unsigned char) (link^0xFF)); //nLink
}
/*
* Not used now ... maybe later if MUX is working and I can receive messages in MUX mode and decode them
*/
void processData(char cmd){
switch(c){
case 'm': //SET MUX MODE 0
{
//set control mux 0
byte cmd[] = {0x53, 0x45, 0x54, 0x20, 0x43, 0x4f, 0x4e, 0x54, 0x52, 0x4f, 0x4c, 0x20, 0x4d, 0x55, 0x58, 0x20, 0x30};
sendMuxCommand(cmd, 0xFF);
sendMuxCommand(cmd, 0x00);
break;
}
case 'n':
{
//set bt name test
byte cmd[] = {0x53, 0x45, 0x54, 0x20,0x42,0x54,0x20,0x4e,0x41,0x4d,0x45,0x20,0x54,0x45,0x53,0x54};
sendMuxCommand(cmd, 0xFF);
sendMuxCommand(cmd, 0x00);
break;
}
case 's':
{
//set link to be slave
break;
}
case 'p':
{
//park a link
break;
}
default:
{
//Send something to th serial link 00 to see what has been received !
break;
}
}
}