#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "robocore.h" #include "robowidgets.h" #include "rsrcmanager.h" #include "roboslot.h" #include "robopilot.h" #define T(String) gettext(String) #define VERSION "1.0.13" using namespace std; using namespace net::lliurex::lgi; using namespace net::lliurex::robolliurex; X11Window * window; bool quit_request=false; class MainScreen: public RoboMenu { public: Button * btn1; Button * btn2; Button * btn3; MainScreen(): RoboMenu("Start",0.5,0.5,RBW_POSITION_FACTOR,RBW_DISTRIBUTION_VERTICAL) { name ="main_screen"; btn1 = new Button(T("Program")); btn2 = new Button(T("Set up")); btn3 = new Button(T("Exit")); btn1->Init(window->cairo); btn2->Init(window->cairo); btn3->Init(window->cairo); Add(btn1); Add(btn2); Add(btn3); } void OnDestroy(DestroyEvent * event) { cout<<"MainScreen::Destroy"<Init(window->cairo); btn2->Init(window->cairo); btn3->Init(window->cairo); btn4->Init(window->cairo); Add(btn1); Add(btn2); Add(btn3); Add(btn4); Hide(); } void OnMouseClick(Widget * widget,MouseClickEvent * event) { if(widget==btn4) { Show("main_screen"); Hide(); } if(widget==btn1) { Show("com_screen"); Hide(); } if(widget==btn2) { Show("firmware_screen"); Hide(); } if(widget==btn3) { Show("test_screen"); Hide(); } } }; class PilotMenu: public RoboMenu { public: Button * btn1; Button * btn2; Button * btn3; Button * btn4; Button * btn5; PilotMenu(): RoboMenu("Pilots",0.5,0.5,RBW_POSITION_FACTOR,RBW_DISTRIBUTION_VERTICAL) { name="pilot_menu"; btn1 = new Button(T("Pilot 1")); btn2 = new Button(T("Pilot 2")); btn3 = new Button(T("Pilot 3")); btn4 = new Button(T("Pilot 4")); btn5 = new Button(T("<-- Back")); btn1->Init(window->cairo); btn2->Init(window->cairo); btn3->Init(window->cairo); btn4->Init(window->cairo); btn5->Init(window->cairo); Add(btn1); Add(btn2); Add(btn3); Add(btn4); Add(btn5); Hide(); } void OnMouseClick(Widget * widget,MouseClickEvent * event) { if(widget==btn1) { Show("pilot_1"); Hide(); } if(widget==btn2) { Show("pilot_2"); Hide(); } if(widget==btn3) { Show("pilot_3"); Hide(); } if(widget==btn4) { Show("pilot_4"); Hide(); } if(widget==btn5) { Show("main_screen"); Hide(); } } }; class COMScreen: public RoboMenu { public: int value; Switch *sw1; Switch *sw2; Switch *sw3; Button * btn1; COMScreen(): RoboMenu("COM Selection",0.5,0.5,RBW_POSITION_FACTOR,RBW_DISTRIBUTION_VERTICAL) { value = RoboCore::GetCore()->comm_port; name="com_screen"; sw1 = new Switch(T("COM 1"),&value,0); sw2 = new Switch(T("COM 2"),&value,1); sw3 = new Switch(T("USB"),&value,2); btn1 = new Button(T("<-- Back")); btn1->Init(window->cairo); sw1->Init(window->cairo); sw2->Init(window->cairo); sw3->Init(window->cairo); Add(sw1); Add(sw2); Add(sw3); Add(btn1); Hide(); } void OnMouseClick(Widget * widget,MouseClickEvent * event) { if(widget==btn1) { RoboCore::GetCore()->comm_port=value; Show("setup_screen"); Hide(); } } }; class FirmwareScreen: public RoboMenu { public: Label * lbl1; Button * btn1; Button * btn2; Image * img1; int frame; FirmwareScreen(): RoboMenu("Download firmware",0.5,0.5,RBW_POSITION_FACTOR,RBW_DISTRIBUTION_VERTICAL) { name="firmware_screen"; lbl1 = new Label(T("Place the brick in front of the tower, turn it on and press Download to begin the firmware update")); btn1 = new Button(T("Download")); btn2 = new Button(T("<-- Back")); img1 = new Image(RoboCore::GetCore()->rsrc->GetSurface("firm00.png")); frame=0; lbl1->Init(window->cairo); btn1->Init(window->cairo); btn2->Init(window->cairo); Add(lbl1); Add(btn1); Add(img1); Add(btn2); Hide(); } void OnMouseClick(Widget * widget,MouseClickEvent * event) { if(widget==btn1) { btn1->Enable(false); RoboCore::GetCore()->DownloadFirmware(); } if(widget==btn2) { Show("setup_screen"); Hide(); } } void OnMessage(Widget * widget, MessageEvent * event) { if(event->msg->id==RBW_MSG_FIRM_STATUS) { int status = ((MessageDataInt *)event->msg->data["status"])->value; if(status==RBC_FIRM_STATUS_DOWNLOADING) { frame++; frame=frame%10; stringstream ss; ss<<"firm0"<SetImage(RoboCore::GetCore()->rsrc->GetSurface(ss.str())); } if(status==RBC_FIRM_STATUS_COMPLETED) { img1->SetImage(RoboCore::GetCore()->rsrc->GetSurface("firm_ok.png")); btn1->Enable(true); } if(status==RBC_FIRM_STATUS_ERROR) { img1->SetImage(RoboCore::GetCore()->rsrc->GetSurface("firm_error.png")); btn1->Enable(true); } } RoboMenu::OnMessage(widget,event); } }; class TestScreen: public RoboMenu { public: Label * lbl1; Image * img1; Button * btn1; Button * btn2; int frame; TestScreen(): RoboMenu("Test communications",0.5,0.5,RBW_POSITION_FACTOR,RBW_DISTRIBUTION_VERTICAL) { name="test_screen"; frame=0; lbl1 = new Label(T("Place the brick in front of the tower, turn it on and press Start to test the communications")); btn1 = new Button(T("Start")); btn2 = new Button(T("<-- Back")); img1 = new Image(RoboCore::GetCore()->rsrc->GetSurface("firm00.png")); btn1->Init(window->cairo); lbl1->Init(window->cairo); btn2->Init(window->cairo); Add(lbl1); Add(btn1); Add(img1); Add(btn2); Hide(); } void OnMouseClick(Widget * widget,MouseClickEvent * event) { if(widget==btn1) { RoboCore::GetCore()->TestComm(); btn1->Enable(false); } if(widget==btn2) { Show("setup_screen"); Hide(); } } void OnMessage(Widget * widget, MessageEvent * event) { if(event->msg->id==RBW_MSG_TEST_STATUS) { int status = ((MessageDataInt *)event->msg->data["status"])->value; if(status==RBC_TEST_STATUS_RUNNING) { frame++; frame=frame%10; stringstream ss; ss<<"firm0"<SetImage(RoboCore::GetCore()->rsrc->GetSurface(ss.str())); } if(status==RBC_TEST_STATUS_OK) { img1->SetImage(RoboCore::GetCore()->rsrc->GetSurface("firm_ok.png")); btn1->Enable(true); RoboCore::GetCore()->GetBattery(); } if(status==RBC_TEST_STATUS_ERROR) { img1->SetImage(RoboCore::GetCore()->rsrc->GetSurface("firm_error.png")); btn1->Enable(true); } } RoboMenu::OnMessage(widget,event); } }; class DropScreen: public RoboMenu { private: char * filename; int pilot_type; public: Label * lbl1; Button * btn1; Button * btn2; DropScreen(): RoboMenu("Drop",0.5,0.5,RBW_POSITION_FACTOR,RBW_DISTRIBUTION_VERTICAL) { name="drop_screen"; lbl1 = new Label(T("Drop a valid Robolliurex file")); btn1 = new Button(T("Load")); btn2 = new Button(T("Cancel")); btn1->Enable(false); btn1->Init(window->cairo); lbl1->Init(window->cairo); btn2->Init(window->cairo); Add(lbl1); Add(btn1); Add(btn2); Hide(); } void OnMouseClick(Widget * widget,MouseClickEvent * event) { if(widget==btn1) { cout<<"Trying to Load "<data["path"]=new MessageDataString(filename); msg->data["pilot"]=new MessageDataInt(pilot_type); window->SendMessage(nullptr,nullptr,msg); Hide(); } if(widget==btn2) { Show("main_screen"); Hide(); } } void OnMessage(Widget * widget, MessageEvent * event) { RoboMenu::OnMessage(widget,event); } void OnDndEnter(DndEnterEvent * event) { lbl1->SetText(T("Drop a valid Robolliurex file")); lbl1->Init(window->cairo); btn1->Enable(false); Show("drop_screen"); } void OnDndDrop(DndDropEvent * event) { string uri=""; for(int n=0;nsize;n++) { char c = (char)event->data[n]; if(c=='\r' || c=='\n') break; else uri=uri+c; } cout<<"uri:["<SetText(T("It seems a valid Robolliurex file")); lbl1->Init(window->cairo); } else { lbl1->SetText(T("Unknown file format")); lbl1->Init(window->cairo); } btn1->Enable(is_valid); } else { cout<<"Failed to parse uri"<Enable(false); } } }; int main(int argc,char * argv[]) { cairo_t * cairo; cairo_surface_t * srf_background; bool have_batt=false; float batt=0.0f; /* gettext setup*/ setlocale( LC_ALL, "" ); bindtextdomain( "robolliurex", "/usr/share/locale" ); textdomain( "robolliurex" ); //g_type_init(); gtk_init(&argc,&argv); window = new X11Window(800,600,0); RoboCore::GetCore()->window=window; /* Enable drag and drop */ vectortargets={"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()); cout<<"* Robolliurex"<rsrc->GetSurface("blueprint.png"); RawEvent * raw_event; int t; int dp_events; bool is_idle; while(!quit_request) { /* window->DispatchEvents(LGI_DISPATCH_EVENTS_WAIT); while (gtk_events_pending ()) gtk_main_iteration (); */ //custom event dispatcher lbl_dispatch: t = window->GetTicks(); dp_events=0; do { is_idle=false; window->GetEvent(); raw_event=window->PopEvent(); if(raw_event!=NULL) { dp_events++; is_idle=true; //catch messages if(raw_event->event->type==LGI_EVENT_MESSAGE) { MessageEvent * msge = (MessageEvent *)raw_event->event; if(msge->msg->id==RBW_MSG_BATTERY_STATUS) { have_batt=true; batt=((MessageDataFloat * )msge->msg->data["value"])->value; } } window->ProcessEvent(raw_event); //Free event delete raw_event->event; delete raw_event; } if(gtk_events_pending()) { is_idle=true; dp_events++; gtk_main_iteration(); } }while(is_idle); if(dp_events==0) { usleep(15000); goto lbl_dispatch; } cairo = window->cairo; 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: "<Flip(); //usleep(15000); } window->Destroy(); delete window; RoboCore::Destroy(); return 0; }