1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2024-12-19 07:02:13 +00:00
stmbl/term/mainframe.cpp

294 lines
9.3 KiB
C++
Raw Permalink Normal View History

2014-08-19 21:40:31 +00:00
#include "mainframe.hpp"
2014-11-13 19:44:14 +00:00
using std::cout;
using std::endl;
using std::string;
using std::to_string;
2015-01-27 21:06:24 +00:00
ServoFrame::ServoFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title){
2014-11-13 19:44:14 +00:00
currentID = wxID_LOWEST;
2014-11-03 00:29:57 +00:00
addr = -1;
2014-10-30 20:17:58 +00:00
connected = false;
histpos = 0;
config = new wxConfig("Servoterm");
wxBoxSizer *mainsizer = new wxBoxSizer(wxHORIZONTAL);
wxSplitterWindow *mainsplitter = new wxSplitterWindow(this,wxID_ANY,wxDefaultPosition, wxSize(1024,768),wxSP_LIVE_UPDATE|wxSP_3DSASH);
wxImage::AddHandler(new wxGIFHandler);
mainsplitter->SetSashGravity(0);
mainsplitter->SetMinimumPaneSize(100);
mainsizer->Add(mainsplitter, 1,wxEXPAND,0);
2015-01-15 22:26:29 +00:00
timer = new wxTimer(this, wxID_ANY);
2015-01-27 21:06:24 +00:00
Bind(wxEVT_TIMER,&ServoFrame::OnTimer,this,wxID_ANY);
2014-10-30 20:17:58 +00:00
//oben
wxPanel *top = new wxPanel(mainsplitter, wxID_ANY);
wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *leiste = new wxBoxSizer(wxHORIZONTAL);
2014-10-31 15:00:08 +00:00
wxBoxSizer *channelleiste = new wxBoxSizer(wxHORIZONTAL);
2014-10-30 20:17:58 +00:00
choose_port = new wxChoice (top, wxID_ANY);
2014-10-31 15:00:08 +00:00
2014-10-30 20:17:58 +00:00
connectbutton = new wxButton(top, wxID_ANY, wxT("&Connect"));
clear = new wxButton(top, wxID_ANY, wxT("Clear"));
refresh = new wxButton(top, wxID_ANY, wxT("&Refresh"));
uhu = new wxRadioButton(top,wxID_ANY, "UHU");
stmbl = new wxRadioButton(top, wxID_ANY,"STMBL");
stmbl->SetValue(true);
2015-01-27 21:06:24 +00:00
refresh->Bind(wxEVT_BUTTON, &ServoFrame::OnRefresh, this, wxID_ANY);
connectbutton->Bind(wxEVT_BUTTON, &ServoFrame::OnConnect, this, wxID_ANY);
clear->Bind(wxEVT_BUTTON, &ServoFrame::OnClear, this, wxID_ANY);
2014-10-30 20:17:58 +00:00
listports();
leiste->Add(choose_port, 0,wxALIGN_LEFT|wxALL,3);
leiste->Add(connectbutton,0,wxALIGN_LEFT|wxALL,3);
leiste->Add(refresh,0,wxALIGN_LEFT|wxALL,3);
leiste->Add(clear,0,wxALIGN_LEFT|wxALL,3);
leiste->Add(uhu,0,wxALIGN_LEFT|wxALL,3);
leiste->Add(stmbl,0,wxALIGN_LEFT|wxALL,3);
topsizer->Add(leiste);
2014-10-31 15:00:08 +00:00
drawpanel = new BasicDrawPane((wxFrame*)top,4);
topsizer->Add(drawpanel, 1,wxEXPAND,0);
2014-11-03 11:35:53 +00:00
wxArrayString waves;
waves.push_back("-");
2014-11-21 20:59:26 +00:00
waves.push_back("net0.cmd");
waves.push_back("net0.fb");
waves.push_back("pid0.pwm_cmd");
waves.push_back("pos_minus0.out");
2015-01-15 22:26:29 +00:00
2014-11-03 11:35:53 +00:00
//channels
2014-11-13 19:44:14 +00:00
channelstartID = currentID-1;//next ID
2014-10-31 15:00:08 +00:00
for(int i = 0;i<drawpanel->channels;i++){
2014-11-13 19:44:14 +00:00
wxBoxSizer *channelsizer = new wxBoxSizer(wxVERTICAL);
channelchoice.push_back(new wxChoice (top, ++currentID));
2015-01-27 21:06:24 +00:00
channelchoice.back()->Bind(wxEVT_CHOICE,&ServoFrame::OnChannelChange, this, currentID);
2014-11-03 11:35:53 +00:00
channelchoice.back()->Set(waves);
2014-11-13 19:44:14 +00:00
channelchoice.back()->SetSelection(0);
cout << channelchoice.back()->GetId() << endl;
channelpos.push_back(new wxSlider(top, ++currentID, 0, -100, 100));
2015-01-27 21:06:24 +00:00
channelpos.back()->Bind(wxEVT_SLIDER,&ServoFrame::OnChannelChange, this, currentID);
2014-11-13 19:44:14 +00:00
channelgain.push_back(new wxSlider(top, ++currentID, 10, 1, 100));
2015-01-27 21:06:24 +00:00
channelgain.back()->Bind(wxEVT_SLIDER,&ServoFrame::OnChannelChange, this, currentID);
2014-11-03 11:35:53 +00:00
wxPanel *c_panel;
c_panel = new wxPanel(top, wxID_NEW, wxPoint(150, 20), wxSize(20, 20), wxBORDER_NONE);
c_panel->SetBackgroundColour(drawpanel->pen[i].GetColour());
2014-11-13 19:44:14 +00:00
wxBoxSizer *sizer1 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *sizer2 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *sizer3 = new wxBoxSizer(wxHORIZONTAL);
sizer1->Add(c_panel, 0,wxALIGN_LEFT|wxALL,3);
sizer1->Add(channelchoice.back(), 1,wxALIGN_LEFT|wxALL,3);
sizer2->Add(channelpos.back(), 1,wxALIGN_LEFT|wxALL,3);
sizer3->Add(channelgain.back(), 1,wxALIGN_LEFT|wxALL,3);
channelsizer->Add(sizer1);
channelsizer->Add(sizer2);
channelsizer->Add(sizer3);
channelleiste->Add(channelsizer);
2014-10-31 15:00:08 +00:00
}
2014-11-03 11:35:53 +00:00
2014-10-31 15:00:08 +00:00
topsizer->Add(channelleiste);
top->SetSizer(topsizer);
2014-10-30 20:17:58 +00:00
//unten
wxPanel *bottom = new wxPanel(mainsplitter, wxID_ANY);
text = new wxTextCtrl((wxFrame*)bottom,wxID_ANY,wxEmptyString,wxDefaultPosition,wxDefaultSize,wxTE_MULTILINE|wxTE_READONLY);
textinput = new wxTextCtrl((wxFrame*)bottom,wxID_ANY,wxEmptyString,wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER);
2015-01-27 21:06:24 +00:00
textinput->Bind(wxEVT_TEXT_ENTER, &ServoFrame::OnInput, this, wxID_ANY);
textinput->Bind(wxEVT_KEY_DOWN, &ServoFrame::OnKeyDown, this, wxID_ANY);
2014-10-30 20:17:58 +00:00
wxBoxSizer *bottomsizer = new wxBoxSizer(wxVERTICAL);
bottomsizer->Add(text, 1,wxEXPAND|wxALL,3);
bottomsizer->Add(textinput, 0,wxEXPAND|wxALL,3);
bottom->SetSizer(bottomsizer);
2014-11-13 19:44:14 +00:00
mainsplitter->SplitHorizontally(top, bottom,500);
2014-10-30 20:17:58 +00:00
this->SetSizer(mainsizer);
mainsizer->SetSizeHints(this);
2014-09-21 00:32:12 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::OnKeyDown(wxKeyEvent& event){
2014-10-30 20:17:58 +00:00
if(event.GetKeyCode() == 315){//up
if(histpos > 0 && history.size()>0){
textinput->SetValue(history.at(--histpos));
textinput->SetInsertionPointEnd();
}
}
else if(event.GetKeyCode() == 317){//down
if(histpos < history.size()-1 && history.size()>0){
textinput->SetValue(history.at(++histpos));
textinput->SetInsertionPointEnd();
}else if(histpos < history.size()){
histpos++;
textinput->SetValue(wxT(""));
}
2015-01-11 04:22:26 +00:00
}else if (event.GetKeyCode() == 9){}//tab
2014-10-30 20:17:58 +00:00
else{
event.Skip();
}
2014-09-21 02:20:14 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::OnChannelChange(wxCommandEvent& event){
2014-11-13 19:44:14 +00:00
int param = (event.GetId()-channelstartID-2)%3;
2014-11-21 20:59:26 +00:00
int channel = (int)((event.GetId()-channelstartID-2)/3);
string params[] = {"term0.wave","term0.offset","term0.gain"};
wxString value;
2014-11-13 19:44:14 +00:00
switch (param) {
case 0://wave
2014-11-21 20:59:26 +00:00
value = event.GetSelection()==0?"0":event.GetString();
2014-11-13 19:44:14 +00:00
break;
case 1://offset
case 2://gain
2014-11-21 20:59:26 +00:00
value = to_string(event.GetSelection()/10.0f);
2014-11-13 19:44:14 +00:00
break;
default:
break;
}
//cout << "change " << channel << " " << param << " " << value << endl;
string df = params[param];
df += to_string(channel);
df += " = ";
2014-11-21 20:59:26 +00:00
df += value;
2014-11-13 19:44:14 +00:00
send(df);
}
2015-01-27 21:06:24 +00:00
int ServoFrame::send(string& s,bool h){
2014-11-21 20:59:26 +00:00
cout << s << endl;
2014-11-03 11:35:53 +00:00
if(connected){
2014-11-21 20:59:26 +00:00
if(h){//history
if((history.size()==0 || history.back() != s) && !s.empty()){
history.push_back(s);
}
histpos = history.size();
2014-11-03 11:35:53 +00:00
}
2014-11-13 19:44:14 +00:00
int ret1 = sp_nonblocking_write(port, s.c_str(), s.length());
2014-11-03 11:35:53 +00:00
int ret2 = sp_nonblocking_write(port, "\r", 1);
2014-11-13 19:44:14 +00:00
if(ret1 != s.length() || ret2!=1){
2014-11-03 11:35:53 +00:00
wxMessageBox( wxT("Fehler beim senden"), wxT("Error"), wxICON_EXCLAMATION);
disconnect();
2015-01-27 21:06:24 +00:00
return 0;
2014-11-03 11:35:53 +00:00
}
2014-11-13 19:44:14 +00:00
}else{
wxMessageBox( wxT("Nicht verbunden"), wxT("Error"), wxICON_EXCLAMATION);
2015-01-27 21:06:24 +00:00
return 0;
2014-11-03 06:12:57 +00:00
}
2015-01-27 21:06:24 +00:00
return 1;
2014-10-31 15:00:08 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::OnTimer(wxTimerEvent& evt){
2014-10-30 20:17:58 +00:00
int ret;
if(connected){
ret = sp_nonblocking_read(port, buf, bufsize);
if(ret > 0){
buf[ret] = 0;
2014-11-03 00:29:57 +00:00
if(uhu->GetValue()){
for (int i=0; i<ret; i++) {
if ((buf[i]>>7)) {
2014-11-09 19:51:18 +00:00
drawpanel->plotvalue((buf[i]+128/2) / 64.0);
2014-11-03 00:29:57 +00:00
}else{
text->AppendText((char)buf[i]);
}
}
}else if(stmbl->GetValue()){
for (int i=0; i<ret; i++){
2014-11-09 19:02:49 +00:00
if(addr >= 0){
2014-11-09 19:51:18 +00:00
values[addr++] = (buf[i]-128) / 128.0;
2014-11-09 19:02:49 +00:00
if(addr == 4){
2014-11-03 00:29:57 +00:00
drawpanel->plotvalue(values);
2014-11-03 06:12:57 +00:00
addr = -1;
2014-11-03 00:29:57 +00:00
}
2014-11-22 02:39:12 +00:00
}else if (buf[i] == 0xff) {
2014-11-09 19:02:49 +00:00
addr = 0;
2014-11-03 00:29:57 +00:00
}else{
text->AppendText((char)buf[i]);
}
}
}
2014-10-30 20:17:58 +00:00
}
}
2014-08-19 21:40:31 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::OnRefresh(wxCommandEvent& WXUNUSED(event)){
2014-10-30 20:17:58 +00:00
listports();
2014-08-20 17:13:43 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::listports(){
2014-10-30 20:17:58 +00:00
if (!ports) {
sp_free_port_list(ports);
}
if(sp_list_ports(&ports) == SP_OK){
wxString str;
choose_port->Clear();
for (int i = 0; ports[i]; i++) {
choose_port->Append(sp_get_port_description(ports[i]));
if(config->Read("port", &str) && sp_get_port_description(ports[i]) == str){
choose_port->SetSelection(i);
}
}
}
2014-08-20 17:13:43 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::OnClear(wxCommandEvent& WXUNUSED(event)){
2014-10-30 20:17:58 +00:00
text->Clear();
2014-11-03 11:35:53 +00:00
drawpanel->Clear();
2014-09-17 22:35:14 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::OnConnect(wxCommandEvent& WXUNUSED(event)){
2014-10-30 20:17:58 +00:00
if(connected)
disconnect();
else
connect();
2014-09-18 01:16:17 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::connect(){
2014-11-16 14:25:50 +00:00
if(choose_port->IsEmpty())
return;
port = ports[choose_port->GetSelection()];
2014-10-30 20:17:58 +00:00
if(sp_open(port, SP_MODE_READ_WRITE) == SP_OK){//port da und lässt sich öffnen
wxString str;
str = sp_get_port_description(port);
config->Write("port", str);
config->Flush();
sp_set_baudrate(port,38400);
sp_set_bits(port, 8);
sp_set_stopbits(port, 1);
sp_set_parity(port, SP_PARITY_NONE);
sp_set_xon_xoff(port, SP_XONXOFF_DISABLED);
sp_set_flowcontrol(port, SP_FLOWCONTROL_NONE);
connected = true;
connectbutton->SetLabel(wxT("&Disonnect"));
refresh->Disable();
choose_port->Disable();
2014-11-03 11:35:53 +00:00
uhu->Disable();
stmbl->Disable();
2014-10-30 20:17:58 +00:00
textinput->SetFocus();
2015-01-15 22:26:29 +00:00
timer->Start(50);
2014-10-30 20:17:58 +00:00
}else{
wxMessageBox( wxT("Fehler beim Öffnen"), wxT("Error"), wxICON_EXCLAMATION);
listports();
}
2014-09-18 01:16:17 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::disconnect(){
2014-10-30 20:17:58 +00:00
if(sp_close(port) == SP_OK){
connected = false;
connectbutton->SetLabel(wxT("&Connect"));
refresh->Enable();
choose_port->Enable();
2014-11-03 11:35:53 +00:00
uhu->Enable();
stmbl->Enable();
2015-01-15 22:26:29 +00:00
timer->Stop();
2014-10-30 20:17:58 +00:00
}
2014-08-21 01:44:43 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::OnInput(wxCommandEvent& event){
2014-11-13 19:44:14 +00:00
string s =string(textinput->GetValue().mb_str());
2014-11-21 20:59:26 +00:00
send(s,true);
2014-10-30 20:17:58 +00:00
textinput->Clear();
2014-08-20 17:13:43 +00:00
}