diff --git a/source/Command.m b/source/Command.m index 663f528c7c10f4dcc57a2391ad23a5fa8f956348..bf20f962eac0182dbd579542499bc157b95b2f7e 100755 --- a/source/Command.m +++ b/source/Command.m @@ -126,6 +126,8 @@ % opCOM_READY @MMI: Adds a opCOM_READY opcode to the command object % opCOMGET_GET_BRICKNAME Adds a opCOMGET opcode with a GET_BRICKNAME subcode to the command object % opCOMSET_SET_BRICKNAME Adds a opCOMSET opcode with a SET_BRICKNAME subcode to the command object +% opCOMGET_NETWORK @MMI: Adds a opCOMGET opcode with a GET_NETWORK subcode to the command object +% opCOMGET_ID @MMI: Adds a opCOMGET opcode with a GET_ID subcode to the command object % % opMAILBOX_WRITE Adds a opMAILBOX_WRITE opcode to the command object % @@ -2543,6 +2545,56 @@ classdef Command < handle cmd.addLCSString(name); end + function opCOMGET_NETWORK(cmd,hardware,length,name,mac,ip) + % Command.opCOMGET_NETWORK Add a opCOMGET_NETWORK + % + % Command.opCOMGET_NETWORK(length,name) adds a opCOMGET + % with a GET_NETWORK subcode to the command object. + % + % Notes:: + % - hardware is the hardware type (should always be 3 for this method) + % - length is the maximal length of string returned (-1 no check) + % - name is the address in response buffer where name-string should begin + % - mac is the address in response buffer where mac-string should begin + % - ip is the address in response buffer where ip-string should begin + % -> more info about response buffer: refer to GV0(cmd,i) + % - opCOMGET_GET_BRICKNAME,LC0(13),LC0(hardware),LC0(length) + % + % Example:: + % cmd.opCOMGET_NETWORK(3,18,0,4,10) + + cmd.addDirectCommand(ByteCodes.COMGet); + cmd.LC0(COMGetSubCodes.GetNetwork); + cmd.LC0(hardware); + cmd.LC0(length); + cmd.GV0(name); + cmd.GV0(mac); + cmd.GV0(ip); + end + + function opCOMGET_ID(cmd,hardware,length,id) + % Command.opCOMGET_ID Add a opCOMGET_ID + % + % Command.opCOMGET_ID(hardware,length,id) adds a opCOMGET + % with a GET_ID subcode to the command object. + % + % Notes:: + % - hardware is the hardware type (should always be 2 for this method) + % - length is the maximal length of string returned (-1 no check) + % - id is the address in response buffer where string should begin + % -> more info about response buffer: refer to GV0(cmd,i) + % - opCOMGET_ID,LC0(13),LC0(hardware),LC0(length),GV0(id) + % + % Example:: + % cmd.opCOMGET_ID(1,20,0) + + cmd.addDirectCommand(ByteCodes.COMGet); + cmd.LC0(COMGetSubCodes.GetID); + cmd.LC0(hardware); + cmd.LC0(length); + cmd.GV0(id); + end + function opMAILBOX_WRITE(cmd,brickname,boxname,type,msg) % Command.opMAILBOX_WRITE Add a opMAILBOX_WRITE % diff --git a/source/CommunicationInterface.m b/source/CommunicationInterface.m index 1eb982a4049d45f501632327142a09fbe7f8413f..855d00b03e1d140660d538195d433461eb31c413 100755 --- a/source/CommunicationInterface.m +++ b/source/CommunicationInterface.m @@ -79,7 +79,8 @@ % communication adapter is ready % comGetBrickName Returns the name of the brick % comSetBrickName Sets the name of the brick -% +% comGetMACAddress@MMI: Returns the MAC-address of the brick +% comGetBTID @MMI: Returns BT-address information % % mailBoxWrite Writes a mailbox message from the brick to another device % fileUpload Uploads a file to the brick @@ -1755,6 +1756,44 @@ classdef CommunicationInterface < handle brick.send(cmd); end + function mac = comGetMACAddress(brick) + % Brick.comGetMACAddress Get brick MAC address + % + % Brick.comGetMACAddress() returns the name of the brick. + % + % Example:: + % mac = b.comGetMACAddress() + + cmd = Command(); + cmd.addHeaderDirectReply(42,36,0); + cmd.opCOMGET_NETWORK(3,36,0,8,20); + cmd.addLength(); + brick.send(cmd); + % receive the command + msg = brick.receive()'; + % return the brick name + mac = sscanf(char(msg(4:10)),'%s'); + end + + function id = comGetBTID(brick) + % Brick.comGetBTID Get brick BT address + % + % Brick.comGetBTID() returns the BT address + % + % Example:: + % mac = b.comGetBTID() + + cmd = Command(); + cmd.addHeaderDirectReply(42,12,0); + cmd.opCOMGET_ID(2,12,0); + cmd.addLength(); + brick.send(cmd); + % receive the command + msg = brick.receive()'; + % return the brick name + id = sscanf(char(msg(6:end)),'%s'); + end + function mailBoxWrite(brick,brickname,boxname,type,msg) % Brick.mailBoxWrite Write a mailbox message % diff --git a/tools/BrickConfig.fig b/tools/BrickConfig.fig new file mode 100644 index 0000000000000000000000000000000000000000..f2ffba1f25515aa2a7758bfcc66d7ce64890c7ab Binary files /dev/null and b/tools/BrickConfig.fig differ diff --git a/tools/BrickConfig.m b/tools/BrickConfig.m new file mode 100644 index 0000000000000000000000000000000000000000..651c7bf7a930af1a2b575b2a92ac3737a2d9c506 --- /dev/null +++ b/tools/BrickConfig.m @@ -0,0 +1,173 @@ +function varargout = BrickConfig(varargin) +% BRICKCONFIG MATLAB code for BrickConfig.fig +% BRICKCONFIG, by itself, creates a new BRICKCONFIG or raises the existing +% singleton*. +% +% H = BRICKCONFIG returns the handle to a new BRICKCONFIG or the handle to +% the existing singleton*. +% +% BRICKCONFIG('CALLBACK',hObject,eventData,handles,...) calls the local +% function named CALLBACK in BRICKCONFIG.M with the given input arguments. +% +% BRICKCONFIG('Property','Value',...) creates a new BRICKCONFIG or raises the +% existing singleton*. Starting from the left, property value pairs are +% applied to the GUI before BrickConfig_OpeningFcn gets called. An +% unrecognized property name or invalid value makes property application +% stop. All inputs are passed to BrickConfig_OpeningFcn via varargin. +% +% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one +% instance to run (singleton)". +% +% See also: GUIDE, GUIDATA, GUIHANDLES + +% Edit the above text to modify the response to help BrickConfig + +% Last Modified by GUIDE v2.5 04-Nov-2016 11:21:20 + +% Begin initialization code - DO NOT EDIT +gui_Singleton = 1; +gui_State = struct('gui_Name', mfilename, ... + 'gui_Singleton', gui_Singleton, ... + 'gui_OpeningFcn', @BrickConfig_OpeningFcn, ... + 'gui_OutputFcn', @BrickConfig_OutputFcn, ... + 'gui_LayoutFcn', [] , ... + 'gui_Callback', []); +if nargin && ischar(varargin{1}) + gui_State.gui_Callback = str2func(varargin{1}); +end + +if nargout + [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); +else + gui_mainfcn(gui_State, varargin{:}); +end +% End initialization code - DO NOT EDIT + + +% --- Executes just before BrickConfig is made visible. +function BrickConfig_OpeningFcn(hObject, eventdata, handles, varargin) +% This function has no output args, see OutputFcn. +% hObject handle to figure +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +% varargin command line arguments to BrickConfig (see VARARGIN) + +% Open connection +[handles.brick, succ] = connect(); +if ~succ + handles.connected = false; +else + handles.connected = true; + updateFigures(handles); + + t = timer; + t.TimerFcn = @updateFigures; + t.Period = 2; + t.StartDelay = 2; + t.ExecutionMode = 'fixedSpacing'; + + handles.timer = t; +end + +% Choose default command line output for BrickConfig +handles.output = hObject; + +% Update handles structure +guidata(hObject, handles); + +% UIWAIT makes BrickConfig wait for user response (see UIRESUME) +% uiwait(handles.figure1); + + +% --- Outputs from this function are returned to the command line. +function varargout = BrickConfig_OutputFcn(hObject, eventdata, handles) +% varargout cell array for returning output args (see VARARGOUT); +% hObject handle to figure +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +% Get default command line output from handles structure +varargout{1} = handles.output; +if ~handles.connected + figure1_CloseRequestFcn(hObject, eventdata, handles); +end + + +% --- Executes on button press in pushbutton_close. +function pushbutton_close_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton_close (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +figure1_CloseRequestFcn(gcbf, eventdata, handles) + + +% --- Executes on button press in pushbutton_rename. +function pushbutton_rename_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton_rename (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +b = handles.brick; +if ~(isa(b, 'CommunicationInterface') && b.isvalid) + return; +end + +answer = inputdlg('Enter Brick name:', 'Rename Brick'); +if ~isempty(answer) + b.comSetBrickName(answer{:}); + updateFigures(handles); +end + +msgbox(['Renaming will be only be fully effective after ', ... + 'executing the btconnect-script with the old name once!']); +figure1_CloseRequestFcn(gcbf, eventdata, handles) + +function [b, succ] = connect() +succ = false; +b = 0; + +l = {'USB', 'Bluetooth: rfcomm0', 'Bluetooth: rfcomm1', 'Bluetooth: rfcomm2'}; +[selection, ok] = listdlg('PromptString', 'Select your connection:', ... + 'SelectionMode', 'single', ... + 'Name', 'Connection', ... + 'ListString', l); + +if ok + try + switch selection + case 1 + b = CommunicationInterface('usb'); + case 2 + b = CommunicationInterface('bt', 'serPort', '/dev/rfcomm0'); + case 3 + b = CommunicationInterface('bt', 'serPort', '/dev/rfcomm1'); + case 4 + b = CommunicationInterface('bt', 'serPort', '/dev/rfcomm2'); + end + succ = true; + catch ME + errordlg('Failed to connect to the Brick!', 'Connection Error'); + end +end + +return; + + +function updateFigures(handles) +if handles.connected + set(handles.text_name, 'String', handles.brick.comGetBrickName()); + set(handles.text_mac, 'String', handles.brick.comGetBTID()); + set(handles.text_charge, 'String', strcat(int2str(handles.brick.uiReadLbatt()),'%')); +end + +% --- Executes when user attempts to close figure1. +function figure1_CloseRequestFcn(hObject, eventdata, handles) +% hObject handle to figure1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +% Hint: delete(hObject) closes the figure +if handles.connected + handles.brick.delete(); +end + +delete(hObject);