uint8_t dev_manager_adc(uint8_t state,uint8_t opt,uint8_t *buffer,uint8_t size) { uint8_t count=0; switch(state) { case INIT_STATE: init_adc(); return 1; case OPEN_STATE: { switch(opt) { case READ_STATE: //sensors_on(); return 1; case WRITE_STATE: //sensors_on(); return 2; case RW_STATE: return 3; default: //printf("option for device does not exist\n"); nrk_kernel_error_add(NRK_DEVICE_DRIVER,0); return -1; } } case READ_STATE: for(count=0; count<size; count++) { /* Conversion to 8-bit value*/ uint16_t val=get_adc_val(); buffer[count]=val >>2 & 0xFF; } return count; case CLOSE_STATE: //sensors_off(); return 1; /*SET STATUS AND GET STATUS CALLS DEFINED HERE*/ case GET_ADC_STATE: return 1; default: nrk_kernel_error_add(NRK_DEVICE_DRIVER,0); return 0; } }
void appcall(void) { u16 i; switch(uip_conn->lport) { /* http port */ case HTONS(80): /* when syn_rcv the uip_connected() return not zero ,it means client connect to host*/ if(uip_connected()) { /* init the byte count */ hs.count = 0; return; } else if(uip_poll()) { /* If we are polled ten times, we abort the connection. This is because we don't want connections lingering indefinately in the system. */ if(hs.count++ >= 10) { uip_abort(); } return; } else if(uip_newdata()) { if(uip_appdata[0]!='G' || uip_appdata[1]!='E' || uip_appdata[2]!='T' || uip_appdata[3]!=' ') uip_abort(); /* control the led on and off */ /* 0123456789abcdef */ /* GET /swc.html?sw7=1 */ if(uip_appdata[4]=='/' && uip_appdata[5]=='s' && uip_appdata[6]=='w' && uip_appdata[7]=='c') { if(uip_appdata[16]=='7') { if(uip_appdata[18]=='1') { P1_7=0; htmlpage[0x146]='0'; } else { P1_7=1; htmlpage[0x146]='1'; } } if(uip_appdata[16]=='6') { if(uip_appdata[18]=='1') { P1_6=0; htmlpage[0x17d]='0'; } else { P1_6=1; htmlpage[0x17d]='1'; } } } { xdata u8 val[16]; sprintf(val,"%04.2f",get_adc_val()*5.0/1024); memcpy(&htmlpage[0x1ae],val,5); } // tt = 0; // htmlpage[0x1ae]=(unsigned char)(tt>>8)/10+'0'; //整数 // htmlpage[0x1af]=(unsigned char)(tt>>8)%10+'0'; // // htmlpage[0x1af+2]=(tt & 0x00ff)/10+'0'; //小数 // htmlpage[0x1af+3]=(tt & 0x00ff)%10+'0'; hs.dataptr=htmlpage; hs.count=sizeof(htmlpage)-1; } //若收到client的ack包,说明先前的数据已成功发送,进行剩下的数据处理,若没数据要发,就关闭连接 if(uip_acked()) {//若没发送完,长度和开始指针还是存在hs->count 和hs->dataptr中 if(hs.count >= uip_conn->len) { /* uip_conn.len contain the bytes sent just now */ hs.count -= uip_conn->len; hs.dataptr += uip_conn->len; } else { hs.count = 0; } if(hs.count == 0) { uip_close(); /* 数据发送完成,关闭连接 */ } } /* 在没有重发数据且在ESTABLISHED状态时,才会出现uip_flags = UIP_POLL; */ if(!uip_poll()) { /* Send a piece of data, but not more than the MSS of the connection. */ uip_send(hs.dataptr, hs.count); } return; case HTONS(8079): //*串口参数配置 if (uip_newdata()) //*UIP_CONNECTED UIP_NEWDATA UIP_ACKDATA { if(uip_appdata[0]=='s' && uip_appdata[1]=='e' && uip_appdata[2]=='t') if(uip_appdata[3]=='c' && uip_appdata[4]=='o' && uip_appdata[5]=='m') { // u8 *str[]={"24","48","96","192","288","384","576","1152"}; u8 code tab[] = {112,184,220,238,244,247,250,253}; xputs("set ok, please change buard rate\r\n"); Delay_x(500); i = uip_appdata[6] - '0'; if(i >= 0 && i <=7) { TH1 = tab[i]; TL1=TH1; } // switch(uip_appdata[6]) // { case '0': //2400bps // TH1 = 112; // //RCAP2H=0xf7; // //RCAP2L=0x00; // //puts("300bps\r\n"); // break; // case '1': // TH1 = 184; //4800bps // //RCAP2H=0xfb; // //RCAP2L=0x80; // //puts("600bps\r\n"); // break; // case '2': // TH1 = 220; //9600bps // //RCAP2H=0xfd; // //RCAP2L=0xc0; // //puts("1200bps\r\n"); // break; // case '3': // TH1 = // //RCAP2H=0xfe; // //RCAP2L=0xe0; // //puts("2400bps\r\n"); // break; // case '4': // //RCAP2H=0xff; // //RCAP2L=0x70; // //puts("4800bps\r\n"); // break; // case '5': // //RCAP2H=0xff; // //RCAP2L=0xb8; // //puts("9600bps\r\n"); // break; // case '6': // //RCAP2H=0xff; // //RCAP2L=0xdc; // //puts("19200bps\r\n"); // break; // case '7': // //RCAP2H=0xff; // //RCAP2L=0xee; // //puts("38400bps\r\n"); // break; // case '8': // //RCAP2H=0xff; // //RCAP2L=0xf4; // //puts("57600bps\r\n"); // break; // case '9': // //RCAP2H=0xff; // //RCAP2L=0xfa; // //puts("115200bps\r\n"); // break; // default: // //RCAP2H=0xff; // //RCAP2L=0xb8; // //puts("9600bps\r\n"); // break; // } } } return; /*---------------------------------------------------------------------------------*/ //用于数据传送的端口 case HTONS(8080): if (uip_newdata()) //UIP_CONNECTED UIP_NEWDATA UIP_ACKDATA { if(uip_len) { for(i=0; i<uip_len; i++ ) { SendChar(uip_appdata[i]); // if(-1 != SendChar(uip_appdata[i])) // i++; } } } if(uip_poll()) { u16 dt_len = SIO_RBUFLEN; if(dt_len > 0) { for(i=0; i<dt_len; i++) { tx_buf[i] = GetKey(); } uip_slen = dt_len; uip_send(tx_buf,uip_slen); } } return; /*---------------------------------------------------------------------------------*/ //采用client时,这部分需要取消 // default: // uip_abort(); // break; } if(uip_conn->rport == HTONS(8888)) { if(uip_connected()) { code char hello[] = "Hello world\r\n"; xlog("建立连接\r\n"); uip_slen = strlen(hello); uip_send(hello,uip_slen); client_connect_flag = 1; } else if(uip_newdata()) { uip_send(uip_appdata,uip_len); //照原样输出 } else if(uip_aborted()) { client_connect_flag = 0; xlog("rst "); } else if(uip_closed()) { client_connect_flag = 0; xlog("disc "); } } }
uint8_t dev_manager_ff_sensors(uint8_t action,uint8_t opt,uint8_t *buffer,uint8_t size) { uint8_t count=0; // key and value get passed as opt and size uint8_t key=opt; uint8_t value=size; switch(action) { case INIT: // Set the pwr ctrl pin as output LPC_SC->PCONP |= (1<<15); // power up GPIO LPC_GPIO0->FIODIR |= (1<<6); // puts P0.6 into output mode (p8) LPC_GPIO0->FIOSET |= (1<<6); init_adc(); is_open=0; return 1; case OPEN: if(is_open==1) return NRK_ERROR; is_open=1; if(opt&READ_FLAG) { // Turn on Sensor Node Power LPC_GPIO0->FIOCLR |= (1<<6); channel2=0; set_adc_chan(0); //nrk_spin_wait_us(ADC_STARTUP_DELAY); nrk_spin_wait_us(250); return NRK_OK; } if(opt&WRITE_FLAG) { return NRK_ERROR; } if(opt&APPEND_FLAG) { return NRK_ERROR; } if(((opt)&(READ_FLAG|WRITE_FLAG|APPEND_FLAG))==0) return NRK_ERROR; else return NRK_OK; case READ: count=0; if(size!=1 && size!=2) return 0; if(channel2!=BAT && channel2<7) { /* Conversion to 8-bit value*/ uint16_t val=get_adc_val(); if(size==2) { buffer[count]=val & 0xFF; count++; buffer[count]=(val>>8) & 0xFF; } if(size==1) { buffer[count]=(val>>2) & 0xFF; }