#platform "uOLED-32028-PMD3T" // Note that 4DGL has built in sine table, this is just for #DATA access example. #inherit "4DGL_16bitColours.fnc" //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* #CONST LEFT 15 TOP 15 // tables V_TAB 4 //number of rectangles MAX_WIDTH 239 MAX_HEIGHT 319 LAST_ROW 19 VOLT_HEIGHT 30 VOLT_WIDTH 59// width for voltage color field BUTTONWIDTH 60 BUTTONHEIGHT 30 // positions VOLT 0 VOLT1 1 STATE 2 NMB_BT 8 #END #CONST NO 0 YES 1 Y 2 X 1 #END #DATA // button Y positions for control page word dY 3,33,63,93,123,153,183,213,293 word colors RED,GREEN,ORANGE word collor BLUE,BLUE,CRIMSON,CRIMSON,YELLOW,YELLOW,GREEN,INDIGO,GRAY byte commands 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88 byte Text_row 0,2,3 byte Text_col 1,0 byte Text_pos 1,6,11,16,1,6,11,16 byte rec_pos_X 0,60,120,180 byte rec_pos_Y 18,48,66,96 byte rec_X 1,61,121,181,1,61,121,181 byte rec_Y 67,67,67,67,97,97,97,97 byte comm_chars '-', '|', '/', '-', '\\', '|' #END #CONST COMPS 0x81 VOLTAGES 0x82 MAIN_FSM 0x83 MOVE_FSM 0x84 ACT_FSM 0x85 LOCATION 0x86 KEEPALIVE 0x87 CORNS 0x88 DRESS 0x89 #END #CONST MOT 0 ODO 1 CAM 2 PWR 3 HOK 4 APP 5 VID 6 STA 7 #END #CONST VOLT33 0 VOLT50 1 VOLT80 2 VOLT_BAT 3 #END var RX_buffer[200];//var work_buffer[200]; // voltage strings var voltage_3V3[4]; var voltage_5V0[4]; var voltage_8V0[4]; var voltage_BAT[5]; // states strings var act_fsm_state[20]; var move_fsm_state[20]; var main_fsm_state[20]; // pozition var position[9]; var voltages[17]; var comp_state[10]; // var firstState, firstCtrl; var keyval, page; // - 1 if no key pressed else 0-15 var BT_text[10]; var BT_state[10]; var BT_OLDstate[10]; var names[8]; var buffer[100];//[30]; var comp_collor; var volt_state; var corns; var states; var rx_ch,index,id; var comm_idx; var dress_color; //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* func data_init() var idx; idx := 0; repeat voltages[idx] := 'x'; idx++; until(idx == 17); position[0] := 'x'; position[1] := 'x'; position[2] := 'x'; position[3] := 'y'; position[4] := 'y'; position[5] := 'y'; position[6] := 'a'; position[7] := 'a'; position[8] := 'a'; // component names array act_fsm_state[0] := 'I'; act_fsm_state[1] := 'n'; act_fsm_state[2] := 'i'; act_fsm_state[3] := 't'; act_fsm_state[4] := '\0'; move_fsm_state[0] := 'I'; move_fsm_state[1] := 'n'; move_fsm_state[2] := 'i'; move_fsm_state[3] := 't'; move_fsm_state[4] := '\0'; main_fsm_state[0] := 'I'; main_fsm_state[1] := 'n'; main_fsm_state[2] := 'i'; main_fsm_state[3] := 't'; main_fsm_state[4] := '\0'; names[0] := "MOT"; names[1] := "ODO"; names[2] := "CAM"; names[3] := "PWR"; names[4] := "HOK"; names[5] := "APP"; names[6] := "VID"; names[7] := "STA"; idx := 0; repeat comp_state[idx] := (idx+1) & 0x0f; idx++; until(idx==8); corns := 0; dress_color := BLUE; endfunc //********************************************************************************************* //********************************************************************************************* func paint_voltage()//select variable is chooseing which voltage will be rewrite. CAN BE VALUE 0, to 5. 5 is ALL var cntX, cntY, x, y, v_tab, idx; cntX:=0; cntY:=0; v_tab:=4; gfx_Set(PEN_SIZE, 1); //draw table gfx_Rectangle(0, 0, MAX_WIDTH, 18, WHITE);//table while(cntX < v_tab) x := rec_pos_X[cntX]; y := rec_pos_Y[cntY]; gfx_Rectangle(x, y, x+VOLT_WIDTH, y+VOLT_HEIGHT, WHITE);//table cntX++; wend //write legend gfx_Set(PEN_SIZE, 0); txt_Set(FONT_SIZE, 3); txt_Set(TEXT_COLOUR, GRAY); txt_Set(TEXT_OPACITY, 0); txt_MoveCursor(Text_row[VOLT],0); print ("Voltage indicator:"); endfunc //********************************************************************************************* //********************************************************************************************* func redraw_voltage()//(var voltage) var idx, color; gfx_Set(PEN_SIZE, 0); txt_Set(TEXT_COLOUR, BLACK); color := RED; if((voltages[0] & 0x08) != 0x08) color := GREEN; endif gfx_Rectangle(1, 19, 58, 47, color);//table txt_MoveCursor(2,0); print([CHR]voltages[1], [CHR]voltages[2],".", [CHR]voltages[3],[CHR]voltages[4], "\n"); color := RED; if((voltages[0] & 0x04) != 0x04) color := GREEN; endif gfx_Rectangle(61,19,118,47, color);//table txt_MoveCursor(2,5); print([CHR]voltages[5], [CHR]voltages[6],".", [CHR]voltages[7],[CHR]voltages[8], "\n"); color := RED; if((voltages[0] & 0x02) != 0x02) color := GREEN; endif gfx_Rectangle(121, 19, 178,47, color);//table txt_MoveCursor(2,10); print([CHR]voltages[9], [CHR]voltages[10], ".",[CHR]voltages[11],[CHR]voltages[12], "\n"); color := RED; if((voltages[0] & 0x01) != 0x01) color := GREEN; endif gfx_Rectangle(181,19,238, 47, color);//table txt_MoveCursor(2,15); print([CHR]voltages[13],[CHR]voltages[14], ".",[CHR]voltages[15],[CHR]voltages[16], "\n"); paint_voltage(); endfunc //********************************************************************************************* //********************************************************************************************* func paint_comm() gfx_Set(PEN_SIZE, 1);//table gfx_Rectangle(180, 174, 239, 192, WHITE);//table txt_MoveCursor(11,15); txt_Set(TEXT_COLOUR, GRAY); print("Comm:"); endfunc func redraw_comm() if(comm_idx >= 6) comm_idx := 0; endif gfx_Set(PEN_SIZE, 0);//solid rec gfx_Rectangle(181, 191, 238, 221, GREEN);//table txt_MoveCursor(13,17); txt_Set(TEXT_COLOUR, BLACK); print([CHR]comm_chars[comm_idx]); endfunc //********************************************************************************************* //********************************************************************************************* func draw_error() sys_SetTimer(0,3000); gfx_Set(PEN_SIZE, 0);//solid rec gfx_Rectangle(181, 191, 238, 221, BLACK);//table txt_MoveCursor(13,16); txt_Set(TEXT_COLOUR, RED); print("ERR"); endfunc //********************************************************************************************* //********************************************************************************************* func redraw_comp_state(var data) var Y_axes, X_axes,X_end, Y_end, comp_nmb, col_idx, text_Y; comp_nmb := ((data & 0x1f)-1); col_idx := (data & 0x60) >> 5; if((comp_nmb >7)||(col_idx > 2) ||comp_nmb<0) return; endif Y_axes := rec_Y[comp_nmb]; X_axes := rec_X[comp_nmb]; X_end := X_axes + 57; Y_end := Y_axes + 28; text_Y := 5; if(comp_nmb >3) text_Y := 7; endif gfx_Set(PEN_SIZE, 0);//solid rectangle gfx_Rectangle(X_axes, Y_axes, X_end, Y_end, colors[col_idx]);//table txt_MoveCursor(text_Y,Text_pos[comp_nmb]); txt_Set(TEXT_COLOUR, BLACK); print([STR]names[comp_nmb]); comp_state[comp_nmb] := data; endfunc //********************************************************************************************* //********************************************************************************************* func paint_comp_states(var select) var cntX,cntY,x,y, v_tab, row; cntX := 0; cntY := 1; row := 0; v_tab:=4; gfx_Set(PEN_SIZE, 1); //draw table for components gfx_Rectangle(0, rec_pos_Y[cntY], MAX_WIDTH, 66, WHITE);//table cntY++; while(cntX < v_tab) x := rec_pos_X[cntX]; y := rec_pos_Y[cntY]; gfx_Rectangle(x, y, x+VOLT_WIDTH, y+VOLT_HEIGHT, WHITE);//table cntX++; wend cntY := 3; cntX := 0; while(cntX < v_tab) x := rec_pos_X[cntX]; y := rec_pos_Y[cntY]; gfx_Rectangle(x, y, x+VOLT_WIDTH, y+VOLT_HEIGHT, WHITE);//table cntX++; wend txt_MoveCursor(Text_row[STATE],0); txt_Set(TEXT_COLOUR, GRAY); print("Component indicator:") endfunc //********************************************************************************************* //********************************************************************************************* func paint_corn() gfx_Set(PEN_SIZE, 1);//table gfx_Rectangle(180, 126, 239, 222, WHITE);//table gfx_Rectangle(180, 126, 239, 144, WHITE);//table gfx_Rectangle(179, 126, 239, 222, WHITE);//table txt_MoveCursor(8,15); txt_Set(TEXT_COLOUR, GRAY); print("Corn:") endfunc //********************************************************************************************* //********************************************************************************************* func redraw_corns() txt_MoveCursor(10,17); txt_Set(TEXT_COLOUR, BLACK); gfx_Set(PEN_SIZE, 0);//solid rectangle gfx_Rectangle(181, 145, 237, 173, dress_color);//table print(corns&0x0f); endfunc //********************************************************************************************* //********************************************************************************************* func redraw_move_state() var idx; gfx_Set(PEN_SIZE, 0);//table gfx_Rectangle(0, 159, 178, 190, BLACK);//table txt_MoveCursor(10,0); txt_Set(TEXT_COLOUR, OLIVE); idx :=0; print("MO:"); repeat if(idx == 11) print("-\n "); endif putch(move_fsm_state[idx]); idx++; until(move_fsm_state[idx] == '\0'); endfunc //********************************************************************************************* //********************************************************************************************* func redraw_act_state() var idx; gfx_Set(PEN_SIZE, 0);//table gfx_Rectangle(0, 191, 178, 221, BLACK);//table txt_MoveCursor(12,0); txt_Set(TEXT_COLOUR, BROWN); idx :=0; print("AC:"); repeat if(idx == 11) print("-\n "); endif putch(act_fsm_state[idx]); idx++; until(act_fsm_state[idx] == '\0'); endfunc //********************************************************************************************* //********************************************************************************************* func redraw_main_state() var idx; idx :=0; gfx_Rectangle(0, 127, 178, 158, BLACK);//table txt_MoveCursor(8,0); txt_Set(TEXT_COLOUR, TEAL); gfx_Set(PEN_SIZE, 0);//table print("MA:"); repeat if(idx == 11) print("-\n "); endif putch(main_fsm_state[idx]); idx++; until(main_fsm_state[idx] == '\0'); endfunc //********************************************************************************************* //********************************************************************************************* func paint_position() gfx_Set(PEN_SIZE, 0);//solid rectangle gfx_Rectangle(0, 223, 239, 255, MAGENTA);//table gfx_Rectangle(0, 256, 239, 291, MAGENTA);//table endfunc //********************************************************************************************* //********************************************************************************************* func redraw_position() gfx_Set(PEN_SIZE, 0);//solid rectangle gfx_Rectangle(60, 239, 110, 255, MAGENTA);//table gfx_Rectangle(160, 239, 220, 255,MAGENTA);//table gfx_Rectangle(0, 272, 47, 286, MAGENTA);//table txt_MoveCursor(14,0); txt_Set(TEXT_COLOUR, BLACK); print("Position:\n X = ", [CHR]position[0],".",[CHR]position[1],[CHR]position[2]); print(" Y = ", [CHR]position[3],".",[CHR]position[4], [CHR]position[5], "\n"); print("Angle: ","\n"," ", [CHR]position[6],[CHR]position[7],[CHR]position[8]," Deg \n"); endfunc //********************************************************************************************* //********************************************************************************************* func draw_keys() var i; i:=0; repeat gfx_Button(BT_state[i], 3, dY[i], collor[i], BLACK, FONT2, 2, 2, BT_text[i]); until(++i == NMB_BT); gfx_Button(BT_state[i], 3, dY[i], collor[i], BLACK, FONT1, 2, 2, BT_text[i]); endfunc //********************************************************************************************* //********************************************************************************************* func redraw() if(keyval == 8)//||(keyval == 8))//do nothing return; endif if(keyval < 9) gfx_Button(BT_state[keyval], 3, dY[keyval], collor[keyval], BLACK, FONT2, 2, 2, BT_text[keyval]); endif endfunc //********************************************************************************************* //********************************************************************************************* func send_cmd() if(keyval == 8)//do nothing return; endif if(keyval < 9) to(COM0); putch(commands[keyval]); to(TEXT); endif endfunc //********************************************************************************************* //********************************************************************************************* func read_key() var x, y; y := touch_Get(Y); y := y / (BUTTONHEIGHT+1); keyval := y; if(keyval == 8) keyval := -1; endif endfunc //********************************************************************************************* //********************************************************************************************* func clear_page() gfx_Set(0,0); gfx_Rectangle(0,0,MAX_WIDTH,MAX_HEIGHT,BLACK); endfunc //********************************************************************************************* //********************************************************************************************* // function for debugging only func send_axis() var y; y := touch_Get(Y); txt_MoveCursor(16,1); to(COM0),print(y); to(COM0),print("\n"); to(COM0),print(keyval); to(COM0),print("\n"); endfunc //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* func read_serial() var cnt_idx; rx_ch := serin(); if(rx_ch < 0) goto out; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if((rx_ch & 0x80) == 0x80) id := rx_ch; index:=0; if (id == KEEPALIVE) sys_SetTimer(0,3000);// 3 seconds comm_idx++; redraw_comm(); id:=0; endif; goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if (index < 100) // 100 is buffer capacity RX_buffer[index] := rx_ch; index++; else index := 0; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if(id == COMPS && index == 1) redraw_comp_state(RX_buffer[0]);//draw_status(); id:=0; goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if(id == CORNS)// && index == 1) corns := RX_buffer[0]; redraw_corns();//draw_status(); id:=0; goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if(id == ACT_FSM && rx_ch == '\0') cnt_idx := 0; repeat act_fsm_state[cnt_idx] := RX_buffer[cnt_idx]; cnt_idx++; until(RX_buffer[cnt_idx] == '\0') act_fsm_state[cnt_idx] := '\0'; redraw_act_state(); id:=0; goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if(id == MOVE_FSM && rx_ch == '\0') cnt_idx := 0; repeat move_fsm_state[cnt_idx] := RX_buffer[cnt_idx]; cnt_idx++; until(RX_buffer[cnt_idx] == '\0') move_fsm_state[cnt_idx] := '\0'; redraw_move_state(); goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if(id == MAIN_FSM && rx_ch == '\0') cnt_idx := 0; repeat main_fsm_state[cnt_idx] := RX_buffer[cnt_idx]; cnt_idx++; until(RX_buffer[cnt_idx] == '\0') main_fsm_state[cnt_idx] := '\0'; redraw_main_state(); id:=0; goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if (id == VOLTAGES && index == 17) cnt_idx :=0; repeat voltages[cnt_idx] := RX_buffer[cnt_idx]; cnt_idx++; until(cnt_idx == 17) redraw_voltage(); id:=0; goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if(id == LOCATION && index == 9) cnt_idx := 0; repeat position[cnt_idx] := RX_buffer[cnt_idx]; cnt_idx++; until(cnt_idx == 9) redraw_position(); id:=0; goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` if(id == DRESS && index == 1) if(RX_buffer[0] == 1) dress_color := YELLOW; endif if(RX_buffer[0] == 0) dress_color := BLUE; endif redraw_corns(); id:=0; goto out;//return; endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` out: endfunc //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* func STATE_page() var state, cnt,idx1,tmr; sys_SetTimer(0,3000); paint_voltage(); cnt:=0; redraw_voltage(); paint_comp_states(0); paint_corn(); com_Init(buffer,100,0); cnt := 0; //draw states for all components repeat redraw_comp_state(comp_state[cnt]); until(cnt++ == 8) paint_position(); redraw_position(); redraw_corns(); redraw_main_state(); redraw_move_state(); redraw_act_state(); paint_comm(); redraw_comm(); gfx_Button(UP,0, MAX_HEIGHT-27, GRAY, BLACK,FONT1,2,2, " To control screen "); touch_DetectRegion(0, MAX_HEIGHT-30, MAX_WIDTH, MAX_HEIGHT); touch_Set(TOUCH_ENABLE); repeat state := touch_Get(TOUCH_STATUS); if(state == TOUCH_PRESS) gfx_Button(DOWN,0, MAX_HEIGHT-27, GRAY, BLUE,FONT1, 2, 2, " To control screen "); endif if(state == TOUCH_RELEASE) gfx_Button(UP,0, MAX_HEIGHT-27, GRAY, BLUE,FONT1, 2, 2, " To control screen "); page := 1; break; endif read_serial(); if(sys_GetTimer(0) == 0) idx1 := 0; repeat idx1++; comp_state[idx1] := idx1; until(idx1==8); cnt := 0; //draw states for all components repeat redraw_comp_state(comp_state[cnt]); until(cnt++ == 8) draw_error(); endif forever endfunc //********************************************************************************************* //********************************************************************************************* func CTRL_page() var n, state, x; n :=0; keyval := -1; // initate key value BT_text[0] := " FORK DOWN "; //set texts for all buttons BT_text[1] := " FORK UP "; BT_text[2] := " DOOR OPEN "; BT_text[3] := " DOOR CLOSE "; BT_text[4] := " START ROLL "; BT_text[5] := " STOP ROLL "; BT_text[6] := "SEND ALL STATE"; BT_text[7] := " SWITCH COLOR "; BT_text[8] := " To state screen "; touch_Set(TOUCH_ENABLE); // enable the touch screen touch_DetectRegion(0, 0, MAX_WIDTH, MAX_HEIGHT); //********************************************************************************************* repeat BT_state[n] := UP; //set state for all buttons BT_OLDstate[n] := DOWN; until(++n == 9); //********************************************************************************************* draw_keys(); repeat sys_SetTimer(0,3000); state := touch_Get(TOUCH_STATUS); if(state == TOUCH_PRESS) read_key(); // send_axis(); //debugging only if(keyval != -1) if(keyval == 10 || keyval == 9) BT_state[8] := DOWN; gfx_Button(BT_state[8], 3, dY[8], collor[8], BLUE, FONT1, 2, 2, BT_text[8]);// misto redraw(); else BT_state[keyval] := DOWN; // set that button is pushed redraw(); endif; endif endif if(state == TOUCH_RELEASE) if(keyval != -1) BT_state[keyval] := UP; //button was released page := 0; redraw(); if(keyval == 10 || keyval == 9) gfx_Button(BT_state[8], 3, dY[8], collor[8], BLACK, FONT1, 2, 2, BT_text[8]);// misto redraw(); keyval := -1; break; endif; send_cmd(); endif endif forever endfunc //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* //********************************************************************************************* func main() page := 0; firstState := 0; firstCtrl := 0; comp_collor := RED; data_init(); voltages[0]:=0xff;// voltage color initate comm_idx := 3; repeat if(!page)// view state page clear_page(); STATE_page(); else// view ctrl page clear_page(); CTRL_page(); endif forever endfunc