void data_conn_handler(struct mg_connection *nc, int ev, void *ev_data) { struct conn_state *cs = (struct conn_state *) nc->user_data; if (cs == NULL) { cs = (struct conn_state *) calloc(1, sizeof(*cs)); nc->user_data = cs; } switch (ev) { case MG_EV_POLL: case MG_EV_TIMER: { const double now = mg_time(); const unsigned char led = GPIO_IF_LedStatus(MCU_RED_LED_GPIO); /* Send if there was a change or repeat last data every second. */ if (s_temp_data.volt != cs->td.volt || s_temp_data.temp != cs->td.temp || now > cs->last_sent_td + 1.0) { memcpy(&cs->td, &s_temp_data, sizeof(cs->td)); send_temp(nc, &cs->td); cs->last_sent_td = now; } if (led != cs->led || now > cs->last_sent_led + 1.0) { send_led(nc, now, led); cs->led = led; cs->last_sent_led = now; } if (s_accel_ctx != NULL) { const struct bm222_sample *ls = s_accel_ctx->data + s_accel_ctx->last_index; if (cs->last_sent_acc == 0) { send_acc_sample(nc, ls); cs->last_sent_acc = ls->ts; } else { double last_sent_ts = send_acc_data_since(nc, s_accel_ctx, cs->last_sent_acc); if (last_sent_ts > 0) cs->last_sent_acc = last_sent_ts; } } nc->ev_timer_time = now + 0.05; break; } case MG_EV_WEBSOCKET_FRAME: { struct websocket_message *wm = (struct websocket_message *) ev_data; process_command(nc, wm->data, wm->size); break; } case MG_EV_CLOSE: { LOG(LL_INFO, ("%p closed", nc)); free(cs); break; } } }
void create_code(FILE *wf) { fprintf(wf, ".data\n"); fprintf(wf, "_prompt: .asciiz \"Enter an integer:\"\n"); fprintf(wf, "_ret: .asciiz \"\\n\"\n"); fprintf(wf, ".globl main\n"); fprintf(wf, ".text\n"); fprintf(wf, "read:\n"); fprintf(wf, "\tli $v0, 4\n"); fprintf(wf, "\tla $a0, _prompt\n"); fprintf(wf, "\tsyscall\n"); fprintf(wf, "\tli $v0, 5\n"); fprintf(wf, "\tsyscall\n"); fprintf(wf, "\tjr $ra\n"); fprintf(wf, "\n"); fprintf(wf, "write:\n"); fprintf(wf, "\tli $v0, 1\n"); fprintf(wf, "\tsyscall\n"); fprintf(wf, "\tli $v0, 4\n"); fprintf(wf, "\tla $a0, _ret\n"); fprintf(wf, "\tsyscall\n"); fprintf(wf, "\tmove $v0, $0\n"); fprintf(wf, "\tjr $ra\n"); InterCode * temp = InterCodeHead; int num = 1; int result = 0; int op1 = 0; int op2 = 0; int arg_num = 0; InterCode * argpos = NULL; int i = 0; for(; temp != NULL ; temp = temp->next) { switch(temp->kind) { case ASSIGN: get_temp(0, temp->u.assign.right, wf); //fprintf(wf, "\tmove $t1, $t0"); send_temp(0, temp->u.assign.left, wf); break; case ADDR: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf,"\tsub $t2, $t0, $t1\n"); fprintf(wf,"\taddi $t2, $t2, -4\n"); send_temp(2,temp->u.addr.result,wf); break; case MEMREAD: get_temp(0, temp->u.assign.right, wf); fprintf(wf,"\tlw $t1, 0($t0)\n"); send_temp(1,temp->u.assign.left,wf); break; case MEMWRITE: get_temp(0, temp->u.assign.right, wf); get_temp(1, temp->u.assign.left, wf); fprintf(wf,"\tsw $t0, 0($t1)\n"); break; case ADD: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf, "\tadd $t2, $t1, $t0\n"); send_temp(2, temp->u.addr.result, wf); break; case SUB: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf, "\tsub $t2, $t0, $t1\n"); send_temp(2, temp->u.addr.result, wf); break; case MUL: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf, "\tmul $t2, $t1, $t0\n"); send_temp(2, temp->u.addr.result, wf); break; case DIV: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf, "\tdiv $t0, $t1\n"); fprintf(wf, "\tmflo $t2\n"); send_temp(2, temp->u.addr.result, wf); break; case LABEL: fprintf(wf,"label%d :\n",temp->u.label.label); break; case FUNCTION: fprintf(wf,"%s:\n",temp->u.func.name); if(strcmp(temp->u.func.name,"main") == 0) { fprintf(wf,"\tmove $fp, $sp\n"); fprintf(wf,"\taddi $sp, $sp, -2048\n"); fprintf(wf, "\tli $t0, 8\n"); fprintf(wf,"\tsw $t0, 4($sp)\n"); } param_total_num = 0; break; case GOTO: fprintf(wf,"\tj label%d\n",temp->u.label.label); break; case IF: get_temp(0, temp->u.ifstruct.left, wf); get_temp(1, temp->u.ifstruct.right, wf); switch(temp->u.ifstruct.relop) { case 3: fprintf(wf,"\tbne "); break; case 4: fprintf(wf,"\tbeq "); break; case 5: fprintf(wf,"\tblt "); break; case 6: fprintf(wf,"\tbgt "); break; case 7: fprintf(wf,"\tbge "); break; case 8: fprintf(wf,"\tble "); break; default: fprintf(stdout,"error\n"); break; } fprintf(wf,"$t0, $t1, label%d\n", temp->u.ifstruct.label); break; case RETURN: get_temp(0, temp->u.arg.result, wf); fprintf(wf, "\tmove $v0, $t0\n"); fprintf(wf, "\tjr $ra\n"); break; case DEC: var_total_num = var_total_num + 4; insert_var(temp->u.dec.dec,var_total_num); fprintf(wf,"\tmove $t0, $fp\n"); fprintf(wf,"\taddi $t0, $t0, %d\n",-var_total_num); fprintf(wf,"\tsw $t0, %d($fp)\n",-var_total_num); var_total_num = var_total_num + temp->u.dec.size; break; case ARG: fprintf(wf,"\tsw $ra, 0($sp)\n"); fprintf(wf,"\taddi $sp, $sp, %d\n",(param_total_num + 2) * 4); //printf("param_total_num----%d\n",param_total_num); argpos = temp; arg_num = 1; for( ; argpos->next->kind == ARG; argpos = argpos->next) { arg_num ++; } i = arg_num; for( ; temp->next->kind == ARG; temp = temp->next) { get_temp(0, temp->u.arg.result, wf); printf("arg: %d\n",i); fprintf(wf,"\tsw $t0, %d($sp)\n", i * 4); i --; } get_temp(0, temp->u.arg.result, wf); //printf("arg: %d\n",i); fprintf(wf,"\tsw $t0, %d($sp)\n", i * 4); //printf("arg_num: %d\n",arg_num); fprintf(wf,"\tli $t0, %d\n", (arg_num + 2) * 4); fprintf(wf,"\tsw $t0, %d($sp)\n", (arg_num + 1) * 4); temp = temp->next; //printf("arg_num: %d----%s\n",arg_num,temp->u.callFunc.func); fprintf(wf, "\tjal %s\n",temp->u.callFunc.func); fprintf(wf, "\tlw $t0, -4($sp)\n"); fprintf(wf,"\tsub $sp, $sp, $t0\n"); fprintf(wf, "\tlw $ra, 0($sp)\n"); fprintf(wf, "\tmove $t0, $v0\n"); send_temp(0, temp->u.callFunc.result,wf); break; case CALL: //printf("%d",num); fprintf(wf,"\tsw $ra, 0($sp)\n"); fprintf(wf, "\tli $t0, 8\n"); fprintf(wf,"\tsw $t0, 4($sp)\n"); fprintf(wf,"\tadd $sp, $sp, $t0\n"); fprintf(wf, "\tjal %s\n",temp->u.callFunc.func); fprintf(wf, "\tlw $t0, -4($sp)\n"); fprintf(wf,"\tsub $sp, $sp, $t0\n"); fprintf(wf, "\tlw $ra, 0($sp)\n"); fprintf(wf, "\tmove $t0, $v0\n"); send_temp(0, temp->u.callFunc.result,wf); break; case PARAM: num = 1; param_total_num = 1; for( ; temp->next->kind == PARAM; temp = temp->next) { param_total_num ++; insert_param(temp->u.arg.result, num); num ++; } insert_param(temp->u.arg.result, num); num ++; break; case READ: fprintf(wf,"\tsw $ra, 0($sp)\n"); fprintf(wf,"\taddi $sp, $sp, 4\n"); fprintf(wf, "\tjal read\n"); fprintf(wf,"\tsub $sp, $sp, 4\n"); fprintf(wf, "\tlw $ra, 0($sp)\n"); fprintf(wf, "\tmove $t0, $v0\n"); send_temp(0, temp->u.arg.result,wf); break; case WRITE: fprintf(wf,"\tsw $ra, 0($sp)\n"); fprintf(wf,"\taddi $sp, $sp, 4\n"); get_temp(0, temp->u.arg.result, wf); fprintf(wf,"\tmove $a0, $t0\n"); fprintf(wf, "\tjal write\n"); fprintf(wf,"\taddi $sp, $sp, -4\n"); fprintf(wf, "\tlw $ra, 0($sp)\n"); break; default: break; } } }
int main (void) { U_INT sense_temp = 999; //initialize to an impossible but non zero value. U_INT temp_5sgone = 1; time_t temp_time = 0; U_INT alram_sel = 0; U_INT motion_det= 0; U_INT alram_1sgone = 1; time_t alram_time = 0; U_INT door_pos = 0; U_INT door_3sgone = 1; time_t door_time = 0; while (1) { if ( 1 == temp_5sgone ) { sense_temp = get_temp(); send_temp(sense_temp); climate_ctrl(sense_temp); temp_time= time(); temp_5sgone = 0; } temp_5sgone = check_sec_pass( 5, temp_time); alram_sel = check_alram_sw(); motion_det = motion_stat(); if (1 == alram_sel ) { if ( 1 == motion_det ) { sound_buzz(1); } } else if ( 0 == alram_sel ) { if ( 1 == alram_1sgone ) { sound_buzz(0); } if ( 1 == motion_det) { sound_buzz(1); alram_time = time(); alram_1sgone = 0; } } alram_1sgone = check_sec_pass( 1 , alram_time); if ( (0 == alram_sel) && (1 == prox_stat) && (0 == door_pos) ) { door_motor(1); door_pos = 1; door_time = time(); door_3sgone = 0; } else if ((0 == alram_sel) && (0 == prox_stat) && (1 == door_pos) ) { door_3sgone = check_sec_pass( 3, door_time); if ( 1 == door_3sgone ) { door_motor(0); door_pos = 0; } } } return 0; }