int get_next_line(int const fd, char **line) { static char *text = NULL; char *n_poz; int result; if ((!(text) && (text = (char*)malloc(sizeof(*text))) == NULL) || !(line) || BUFF_SIZE < 0 || fd < 0) return (-1); n_poz = ft_strchr(text, '\n'); while (n_poz == NULL) { result = read_buff(fd, &text); if (result == 0) { if (ft_strlen(text) == 0) return (0); text = ft_strjoin(text, "\n"); } if (result < 0) return (-1); else n_poz = ft_strchr(text, '\n'); } *line = ft_strsub(text, 0, ft_strlen(text) - ft_strlen(n_poz)); text = ft_strdup(n_poz + 1); return (1); }
int file_buff_write(void * buf,Inode* p_inode,int offset,int count) { // 1、确定操作位置 if(offset < 0 || offset >= FILE_MAX_SIZE) { printk("write pos out of file offset : %x\n",offset); return -1; // 位置超出范围 } // 2、写入。在缓冲区中操作,如果文件不在缓冲区内,则先读入缓冲区 int buff_offset = offset % BUFF_SIZE; // 确定缓冲区中的操作位置 int buf_offset = 0; // 待写数据中的偏移 int cnt = 0; // 已写入字节数 while(count > 0) { // 1、搜索待写文件是否在缓冲区,如果不在就将其读入缓冲区 BUFF_HEAD* p_bh = scan_buff(p_inode,offset); if(p_bh == 0) { p_bh = get_buff(p_inode,offset); // 申请缓冲区 if(p_bh == 0) return -1; // 缓冲区用完 read_buff(p_bh); // 文件读入缓冲区 } // 2、此时待写文件已经在缓冲区,在缓冲区中写入内容 void* buff_base = p_bh->buff_base; // 缓冲区地址 int buff_remin = BUFF_SIZE - buff_offset; // 缓冲区剩余字节数 if(buff_remin >= count) // 当前缓冲区能容纳所有剩余数据 { memcopy(buff_base + buff_offset,buf + buf_offset,count); cnt += count; // 剩余数据全部写入,然后返回 p_bh->dirty = 1; // 文件缓冲“脏”标志 return cnt; } else // 当前缓冲区不能容纳所有剩余数据 { memcopy(buff_base + buff_offset,buf + buf_offset,buff_remin); buff_offset = 0; // 写满当前缓冲区后循环写下一个缓冲区 buf_offset += buff_remin; count -= buff_remin; cnt += buff_remin; offset += buff_remin; p_bh->dirty = 1; // 文件缓冲“脏”标志 } } return 0; // 不可能到达这里 }
int file_buff_read(void * buf,Inode* p_inode,int offset,int count) { // 1、确定操作位置 if(offset < 0 || offset >= FILE_MAX_SIZE) { printk("read pos out of file offset : %x\n",offset); return -1; // 位置超出范围 } // 2、读取。在缓冲区中操作,如果文件不在缓冲区内,则先读入缓冲区 int buff_offset = offset % BUFF_SIZE; // 确定缓冲区中的操作位置 int buf_offset = 0; // 待读数据中的偏移 int cnt = 0; // 已读取字节数 while(count > 0) { // 1、搜索待读文件是否在缓冲区,如果不在就将其读入缓冲区 BUFF_HEAD* p_bh = scan_buff(p_inode,offset); if(p_bh == 0) { p_bh = get_buff(p_inode,offset); // 申请缓冲区 if(p_bh == 0) return -1; // 缓冲区用完 read_buff(p_bh); // 文件读入缓冲区 //printk("111 i_num : %d buff_base:%x offset: %d \n",p_bh->p_inode->i_num,p_bh->buff_base,p_bh->offset); } // 2、此时待读文件已经在缓冲区,在缓冲区中读取内容 void* buff_base = p_bh->buff_base; // 缓冲区地址 int buff_remin = BUFF_SIZE - buff_offset; // 缓冲区剩余字节数 if(buff_remin >= count) // 当前缓冲区能容纳所有剩余数据 { memcopy(buf + buf_offset,buff_base + buff_offset,count); cnt += count; // 剩余数据全部读取,然后返回 return cnt; } else // 当前缓冲区不能容纳所有剩余数据 { memcopy(buf + buf_offset,buff_base + buff_offset,buff_remin); buff_offset = 0; // 读完当前缓冲区后循环读取下一个缓冲区 buf_offset += buff_remin; count -= buff_remin; cnt += buff_remin; offset += buff_remin; } } return 0; // 不可能到达这里 }
void main() { int ldr1,ldr2,ldr1_p,ldr2_p, v, t,count ; char tx[7], a,b; int i; int temp1; char test[32]; CE_DIR=0; CSN_DIR=0; CSK_DIR=0; MOSI_DIR=0; MISO_DIR=1; nrf24_config(); CSN=1; CE=1; ////////////////////////////////////////////// lcd_init(); //trisc=0; trisd=0; portd=0; count=0; while(1) { ////////////////////////////////////////////////////////// temp1=read_add(0x07); if(((temp1&0b01000000)>>6)==1) { CE=0; delay_ms(50); //lcd_out(1,1,"data"); read_buff(0x61,test,32); lcd_out(1,1,test); delay_ms(2000); reset(); CE=1; } else lcd_out(1,1,"no "); /////////////////////////////////////////////////////////////// /*ldr1=adc_read(1); ldr2=adc_read(2); v=adc_read(0); t=0.4887*v; if(t>=28) {portd=0b00110000;} else {portd=0;} a =t/10; b =t%10; lcd_out(1,1,"temp="); lcd_chr(1,7,a+48); lcd_chr(1,8,b+48); if(ldr1>=900&&ldr1_p<800) { count++; } ldr1_p=ldr1; if(ldr2>=400&&ldr2_p<350) { count--; if(count<0) count=0; } ldr2_p=ldr2; a =count/10; b =count%10; lcd_out(2,1,"count="); lcd_chr(2,8,a+48); lcd_chr(2,9,b+48);*/ }
int rmsgpack_read(int fd, struct rmsgpack_read_callbacks *callbacks, void *data) { int rv; uint64_t tmp_len = 0; uint64_t tmp_uint = 0; int64_t tmp_int = 0; uint8_t type = 0; char *buff = NULL; if (read(fd, &type, sizeof(uint8_t)) == -1) return -errno; if (type < MPF_FIXMAP) { if (!callbacks->read_int) return 0; return callbacks->read_int(type, data); } else if (type < MPF_FIXARRAY) { tmp_len = type - MPF_FIXMAP; return read_map(fd, tmp_len, callbacks, data); } else if (type < MPF_FIXSTR) { tmp_len = type - MPF_FIXARRAY; return read_array(fd, tmp_len, callbacks, data); } else if (type < MPF_NIL) { tmp_len = type - MPF_FIXSTR; buff = (char *)calloc(tmp_len + 1, sizeof(char)); if (!buff) return -ENOMEM; if (read(fd, buff, tmp_len) == -1) { free(buff); return -errno; } buff[tmp_len] = '\0'; if (!callbacks->read_string) { free(buff); return 0; } return callbacks->read_string(buff, tmp_len, data); } else if (type > MPF_MAP32) { if (!callbacks->read_int) return 0; return callbacks->read_int(type - 0xff - 1, data); } switch (type) { case 0xc0: if (callbacks->read_nil) return callbacks->read_nil(data); break; case 0xc2: if (callbacks->read_bool) return callbacks->read_bool(0, data); break; case 0xc3: if (callbacks->read_bool) return callbacks->read_bool(1, data); break; case 0xc4: case 0xc5: case 0xc6: if ((rv = read_buff(fd, 1<<(type - 0xc4), &buff, &tmp_len)) < 0) return rv; if (callbacks->read_bin) return callbacks->read_bin(buff, tmp_len, data); break; case 0xcc: case 0xcd: case 0xce: case 0xcf: tmp_len = 1ULL << (type - 0xcc); tmp_uint = 0; if (read_uint(fd, &tmp_uint, tmp_len) == -1) return -errno; if (callbacks->read_uint) return callbacks->read_uint(tmp_uint, data); break; case 0xd0: case 0xd1: case 0xd2: case 0xd3: tmp_len = 1ULL << (type - 0xd0); tmp_int = 0; if (read_int(fd, &tmp_int, tmp_len) == -1) return -errno; if (callbacks->read_int) return callbacks->read_int(tmp_int, data); break; case 0xd9: case 0xda: case 0xdb: if ((rv = read_buff(fd, 1<<(type - 0xd9), &buff, &tmp_len)) < 0) return rv; if (callbacks->read_string) return callbacks->read_string(buff, tmp_len, data); break; case 0xdc: case 0xdd: if (read_uint(fd, &tmp_len, 2<<(type - 0xdc)) == -1) return -errno; return read_array(fd, tmp_len, callbacks, data); case 0xde: case 0xdf: if (read_uint(fd, &tmp_len, 2<<(type - 0xde)) == -1) return -errno; return read_map(fd, tmp_len, callbacks, data); } return 0; }
void main() { int i; int temp1; char test[32]; uart1_init(2400); CE_DIR=0; CSN_DIR=0; CSK_DIR=0; MOSI_DIR=0; MISO_DIR=1; delay_ms(500); nrf24_config(); CSN=1; CE=1; lcd_init(); Lcd_Cmd(_LCD_CURSOR_OFF); //lcd_out(2,1,"welcome"); trisb.f0=0; rb0_bit=0; trisd.f0=0; for(i=0;i<30;i++) { rd0_bit=1; delay_us(400); rd0_bit=0; delay_us(19600); } uart1_write_text("TEST"); uart1_write(13); //lcd_out(1,1,"S"); while(1) { temp1=read_add(0x07); if(((temp1&0b01000000)>>6)==1) { CE=0; delay_ms(50); //lcd_out(1,1,"data"); read_buff(0x61,test,32); uart1_write_text(test); lcd_out(1,1,test); reset(); CE=1; } if(uart1_data_ready()) { rfid[idx]=uart1_read(); ///////////////////////////////////////////////////////////03002e2838 if(rfid[idx]=='0'&&valid2<10) { valid2++; } else if(rfid[idx]=='3'&&valid2<10) { valid2++; } else if(rfid[idx]=='2'&&valid2<10) { valid2++; } else if(rfid[idx]=='E'&&valid2==5) { valid2++; } else if(rfid[idx]=='8'&&valid2<10) { valid2++; } else { valid2=0; } if(rfid[idx]=='0'&&valid1<10) //03002e90b0 { valid1++; } else if(rfid[idx]=='3'&&valid1==1) { valid1++; } else if(rfid[idx]=='2'&&valid1==4) { valid1++; } else if(rfid[idx]=='E'&&valid1==5) { valid1++; } else if(rfid[idx]=='9'&&valid1==6) { valid1++; } else if(rfid[idx]=='B'&&valid1==8) { valid1++; } else { valid1=0; } if(valid2>=10) { lcd_out(2,1,"User2 : 03002E2838"); delay_ms(1000); lcd_cmd(_lcd_clear); lcd_out(2,1,"UnValid"); valid2=0; for(i=0;i<30;i++) { rd0_bit=1; delay_us(400); rd0_bit=0; delay_us(19600); } delay_ms(1000); lcd_cmd(_lcd_clear); } if(valid1>=10) { lcd_out(2,1,"User1 : 03002E90B0"); delay_ms(1000); lcd_cmd(_lcd_clear); lcd_out(2,1,"Valid"); valid1=0; for(i=0;i<30;i++) { rd0_bit=1; delay_us(1500); rd0_bit=0; delay_us(18500); } delay_ms(1000); for(i=0;i<30;i++) { rd0_bit=1; delay_us(400); rd0_bit=0; delay_us(19600); } lcd_cmd(_lcd_clear); } } } }