#include "robocore.h" #include "robomenus.h" #include "robolliurex.h" #include #include #include #include #include #include #include #include #include #include using namespace net::lliurex::robolliurex; using namespace std; /* ************* MainWindow ************ */ MainWindow::MainWindow() : X11Window(800,600,0) { have_batt=false; batt=0.0f; srf_background = RoboCore::GetCore()->rsrc->GetSurface("blueprint.png"); } void MainWindow::Flip() { cairo_set_source_surface(cairo,srf_background,0,0); cairo_paint(cairo); cairo_text_extents_t te; cairo_font_extents_t fe; stringstream ss; ss<<"Version: "< modules; //ask lsf for current kernel modules modules=system::GetModuleList(); usb_tower=false; //look for legousbtower for(int n=0;n towers = filesystem::List("/dev/usb/legousbtower*"); if(towers.size()==0) { cout<<"* Warning, Lego driver is loaded but something failed!"<targets={"text/uri-list"}; window->SetDndTargets(targets); window->SetTitle("Robolliurex"); window->AddLayer(new MainScreen()); window->AddLayer(new SetupScreen()); window->AddLayer(new PilotMenu()); window->AddLayer(new COMScreen()); window->AddLayer(new FirmwareScreen()); window->AddLayer(new TestScreen()); window->AddLayer(new DropScreen()); window->AddLayer(new Pilot1()); window->AddLayer(new Pilot2()); window->AddLayer(new Pilot3()); window->AddLayer(new Pilot4()); } RoboCore::~RoboCore() { cout<<"RoboCore::Destructor"<is_firmware_downloading) { Message * msg = new Message(RBW_MSG_FIRM_STATUS); msg->data["status"]=new MessageDataInt(RBC_FIRM_STATUS_DOWNLOADING); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); usleep(150000); } } void * auxDownloadFirmware(void * param) { /* nqc -Susb -firmware firm328.lgo */ Message * msg; FILE * fp; char buff[32]; int status; string cmd = string("nqc -S") + RoboCore::comm_name[RoboCore::GetCore()->comm_port] + " -firmware " + FIRM_PATH; cout<data["status"]=new MessageDataInt(RBC_FIRM_STATUS_ERROR); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); return nullptr; } status = pclose(fp); RoboCore::GetCore()->is_firmware_downloading=false; if(status==0) { msg = new Message(RBW_MSG_FIRM_STATUS); msg->data["status"]=new MessageDataInt(RBC_FIRM_STATUS_COMPLETED); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); } else { msg = new Message(RBW_MSG_FIRM_STATUS); msg->data["status"]=new MessageDataInt(RBC_FIRM_STATUS_ERROR); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); } return nullptr; } void RoboCore::DownloadFirmware() { if(!is_firmware_downloading) { is_firmware_downloading=true; pthread_t aux_thread; pthread_create(&aux_thread,nullptr,auxDownloadFirmware,nullptr); pthread_t blink_thread; pthread_create(&blink_thread,nullptr,auxDownloadFirmwareBlink,nullptr); } } void * auxTestCommBlink(void * param) { while(RoboCore::GetCore()->is_comm_testing==true) { Message * msg = new Message(RBW_MSG_TEST_STATUS); msg->data["status"]=new MessageDataInt(RBC_TEST_STATUS_RUNNING); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); usleep(150000); } } void * auxTestComm(void * param) { Message * msg; FILE * fp; char buff[32]; int status; string cmd = string("nqc -S") + RoboCore::comm_name[RoboCore::GetCore()->comm_port] + " -raw 10"; cout<is_comm_testing=false; msg = new Message(RBW_MSG_TEST_STATUS); msg->data["status"]=new MessageDataInt(RBC_TEST_STATUS_ERROR); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); return nullptr; } while(fgets(buff, sizeof(buff), fp)!=nullptr) { } status = pclose(fp); if(status==0) { RoboCore::GetCore()->is_comm_testing=false; msg = new Message(RBW_MSG_TEST_STATUS); msg->data["status"]=new MessageDataInt(RBC_TEST_STATUS_OK); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); } else { RoboCore::GetCore()->is_comm_testing=false; msg = new Message(RBW_MSG_TEST_STATUS); msg->data["status"]=new MessageDataInt(RBC_TEST_STATUS_ERROR); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); } return nullptr; } void RoboCore::TestComm() { if(!is_comm_testing) { is_comm_testing=true; pthread_t aux_thread; pthread_create(&aux_thread,nullptr,auxTestComm,nullptr); pthread_t blink_thread; pthread_create(&blink_thread,nullptr,auxTestCommBlink,nullptr); } } void * auxDownloadProgram(void * param) { Message * msg = new Message(RBW_MSG_RUN_STATUS); msg->data["status"]=new MessageDataInt(RBC_RUN_STATUS_DOWNLOADING); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); FILE * fp; char buff[32]; int status=0; string cmd = string("nqc -S") + RoboCore::comm_name[RoboCore::GetCore()->comm_port] + " -TRCX2 -d " + RoboCore::GetCore()->program_path; fp = popen( cmd.c_str(),"r" ); if(fp==nullptr) { msg = new Message(RBW_MSG_RUN_STATUS); msg->data["status"]=new MessageDataInt(RBC_RUN_STATUS_NQC_ERROR); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); RoboCore::GetCore()->is_program_downloading=false; return nullptr; } while(fgets(buff, sizeof(buff), fp)!=nullptr) { cout << buff; } status=pclose(fp); if(status!=0) { msg = new Message(RBW_MSG_RUN_STATUS); msg->data["status"]=new MessageDataInt(RBC_RUN_STATUS_COMM_ERROR); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); RoboCore::GetCore()->is_program_downloading=false; return nullptr; } msg = new Message(RBW_MSG_RUN_STATUS); msg->data["status"]=new MessageDataInt(RBC_RUN_STATUS_COMPLETED); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); RoboCore::GetCore()->is_program_downloading=false; RoboCore::GetCore()->GetBattery(); return nullptr; } void RoboCore::DownloadProgram(string path) { if(!is_program_downloading) { is_program_downloading=true; pthread_t aux_thread; program_path=path; pthread_create(&aux_thread,nullptr,auxDownloadProgram,nullptr); } } void * auxGetBattery(void * param) { FILE * fp; char buff[32]; int status=0; string cmd = string("nqc -S") + RoboCore::comm_name[RoboCore::GetCore()->comm_port] + " -raw 30"; fp = popen( cmd.c_str(),"r" ); if(fp==nullptr) { return nullptr; } while(fgets(buff, sizeof(buff), fp)!=nullptr) { cout << buff; } status=pclose(fp); if(status!=0) { return nullptr; } stringstream ss; int low,high; float batt; ss.str(buff); ss>>hex>>low>>high; batt=((high<<8) + low)/1000.0f; cout<<"Batt: "<data["value"]=new MessageDataFloat(batt); RoboCore::GetCore()->window->SendMessage(nullptr,nullptr,msg); RoboCore::GetCore()->is_battery_requesting=false; return nullptr; } void RoboCore::GetBattery() { if(!is_battery_requesting) { is_battery_requesting=true; pthread_t aux_thread; pthread_create(&aux_thread,nullptr,auxGetBattery,nullptr); } } int RoboCore::CustomEventsDispatch() { int events = 0; while(gtk_events_pending()) { events++; gtk_main_iteration(); } return events; } void RoboCore::OnMessage(BaseWindow * window,Layer * layer, Widget * widget,MessageEvent * event) { Message * msg = event->msg; //catch battery status messages if(msg->id==RBW_MSG_BATTERY_STATUS) { float value; value = (static_cast(msg->data["value"]))->value; this->window->SetBattery(value); } }