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

297 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){
2015-11-27 18:23:04 +00:00
ports = 0;
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;
wxBoxSizer *mainsizer = new wxBoxSizer(wxHORIZONTAL);
wxSplitterWindow *mainsplitter = new wxSplitterWindow(this,wxID_ANY,wxDefaultPosition, wxSize(1024,700),wxSP_LIVE_UPDATE|wxSP_3DSASH);
2014-10-30 20:17:58 +00:00
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
2016-01-17 15:21:13 +00:00
//top
2014-10-30 20:17:58 +00:00
wxPanel *top = new wxPanel(mainsplitter, wxID_ANY);
wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *leiste = new wxBoxSizer(wxHORIZONTAL);
2015-07-02 21:01:17 +00:00
wxBoxSizer *channelleiste = new wxBoxSizer(wxHORIZONTAL);
2014-10-30 20:17:58 +00:00
choose_port = new wxChoice (top, wxID_ANY);
2015-03-14 23:07:51 +00:00
2014-10-30 20:17:58 +00:00
connectbutton = new wxButton(top, wxID_ANY, wxT("&Connect"));
2015-11-22 13:58:54 +00:00
connectbutton->Disable();
2014-10-30 20:17:58 +00:00
clear = new wxButton(top, wxID_ANY, wxT("Clear"));
refresh = new wxButton(top, wxID_ANY, wxT("&Refresh"));
2016-01-17 15:21:13 +00:00
reset = new wxButton(top, wxID_ANY, wxT("Reset Fault"));
reset->Disable();
2014-10-30 20:17:58 +00:00
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);
2016-01-17 15:21:13 +00:00
reset->Bind(wxEVT_BUTTON, &ServoFrame::OnReset, 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);
2016-01-17 15:21:13 +00:00
leiste->Add(reset,0,wxALIGN_LEFT|wxALL,3);
2014-10-30 20:17:58 +00:00
leiste->Add(uhu,0,wxALIGN_LEFT|wxALL,3);
leiste->Add(stmbl,0,wxALIGN_LEFT|wxALL,3);
topsizer->Add(leiste);
2015-04-07 23:23:23 +00:00
drawpanel = new BasicDrawPane((wxFrame*)top,8);
2015-03-14 23:07:51 +00:00
2015-07-02 21:01:17 +00:00
topsizer->Add(drawpanel, 1,wxEXPAND,0);
2014-11-03 11:35:53 +00:00
wxArrayString waves;
2015-07-02 17:31:10 +00:00
waves.push_back("0");
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-03-14 23:07:51 +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);
2015-07-02 17:31:10 +00:00
channelchoice.push_back(new wxComboBox (top, ++currentID, _T(""), wxDefaultPosition, wxDefaultSize, 0, NULL, wxTE_PROCESS_ENTER));
channelchoice.back()->Bind(wxEVT_COMBOBOX,&ServoFrame::OnChannelChange, this, currentID);
channelchoice.back()->Bind(wxEVT_TEXT_ENTER,&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);
2015-03-14 23:07:51 +00:00
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());
2015-03-14 23:07:51 +00:00
2014-11-13 19:44:14 +00:00
wxBoxSizer *sizer1 = new wxBoxSizer(wxHORIZONTAL);
sizer1->Add(c_panel, 0,wxALIGN_LEFT|wxALL,3);
sizer1->Add(channelchoice.back(), 1,wxALIGN_LEFT|wxALL,3);
channelsizer->Add(sizer1);
channelleiste->Add(channelsizer);
2014-10-31 15:00:08 +00:00
}
2015-03-14 23:07:51 +00:00
2015-07-02 21:01:17 +00:00
topsizer->Add(channelleiste);
2015-03-14 23:07:51 +00:00
2014-10-31 15:00:08 +00:00
top->SetSizer(topsizer);
2014-10-30 20:17:58 +00:00
2016-01-17 15:21:13 +00:00
//bottom
2014-10-30 20:17:58 +00:00
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);
//use monospace font
wxFont font = textinput->GetFont();
font = wxFont(font.GetPointSize(), wxTELETYPE, font.GetStyle(), font.GetWeight(), font.GetUnderlined());
textinput->SetFont(font);
text->SetFont(font);
2015-11-27 18:23:04 +00:00
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){
int channel = (int)((event.GetId()-channelstartID-2));
wxString value = event.GetString();
string df = "term0.wave";
2014-11-13 19:44:14 +00:00
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-11-17 14:25:28 +00:00
int ServoFrame::send(const string& s,bool h){
2015-11-27 00:25:46 +00:00
if(!connected)
connect();
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
}
2015-11-26 22:33:47 +00:00
txqueue.push(s);
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){
2015-11-26 22:33:47 +00:00
if(!txqueue.empty()){
int ret1 = sp_nonblocking_write(port, txqueue.front().c_str(), txqueue.front().length());
2016-04-11 20:02:15 +00:00
int ret2 = sp_nonblocking_write(port, "\n", 1);
2015-11-26 22:33:47 +00:00
if(ret1 != txqueue.front().length() || ret2!=1){
wxMessageBox( wxT("Error while sending"), wxT("Error"), wxICON_EXCLAMATION);
disconnect();
}
txqueue.pop();
}
2014-10-30 20:17:58 +00:00
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)) {
2015-10-08 14:59:32 +00:00
values[0] = (float)(buf[i]-128-64)/64;
drawpanel->plotvalue(values);
2014-11-03 00:29:57 +00:00
}else{
2015-03-28 22:37:41 +00:00
text->AppendText(wxString::FromAscii(buf[i] & 0x7f));
2014-11-03 00:29:57 +00:00
}
}
2015-10-28 16:21:42 +00:00
2014-11-03 00:29:57 +00:00
}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;
2015-04-07 23:23:23 +00:00
if(addr == 8){
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{
2015-03-28 22:37:41 +00:00
text->AppendText(wxString::FromAscii(buf[i] & 0x7f));
2014-11-03 00:29:57 +00:00
}
}
}
}else if(ret < 0){//disconnect on failure
disconnect();
}
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-10-28 16:21:42 +00:00
void ServoFrame::OnReset(wxCommandEvent& WXUNUSED(event)){
2015-11-17 14:25:28 +00:00
send("fault0.reset = 1");
send("fault0.reset = 0");
2015-10-28 16:21:42 +00:00
}
2015-01-27 21:06:24 +00:00
void ServoFrame::listports(){
2015-11-22 14:28:28 +00:00
int selection = 0;
2015-11-22 13:48:31 +00:00
string description;
2015-11-17 14:24:57 +00:00
if (ports) {
2014-10-30 20:17:58 +00:00
sp_free_port_list(ports);
}
if(sp_list_ports(&ports) == SP_OK){
wxString str;
choose_port->Clear();
2015-11-22 13:58:54 +00:00
if(ports){
for (int i = 0; ports[i]; i++) {
description = sp_get_port_description(ports[i]);
choose_port->Append(description);
if(description.find("STMBL") != std::string::npos){
2015-11-22 14:28:28 +00:00
selection = i;
2015-11-22 13:58:54 +00:00
}
2015-11-22 13:25:00 +00:00
}
2015-11-22 14:28:28 +00:00
choose_port->SetSelection(selection);
2015-11-22 13:58:54 +00:00
connectbutton->Enable();
}else{
connectbutton->Disable();
}
2014-10-30 20:17:58 +00:00
}
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()];
2015-11-12 07:14:15 +00:00
if(sp_open(port, SP_MODE_READ_WRITE) == SP_OK){//port available and can be opened
2014-10-30 20:17:58 +00:00
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();
2016-01-17 15:21:13 +00:00
reset->Enable();
2014-10-30 20:17:58 +00:00
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{
2015-11-12 07:14:15 +00:00
wxMessageBox( wxT("Cannot open port"), wxT("Error"), wxICON_EXCLAMATION);
2014-10-30 20:17:58 +00:00
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();
2016-01-17 15:21:13 +00:00
reset->Disable();
2014-10-30 20:17:58 +00:00
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
}
2015-11-22 13:07:52 +00:00
listports();
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
}