
wowow
Dependencies: C12832 ESP-call FXOS8700Q
communication.h
- Committer:
- 201710861
- Date:
- 2022-06-13
- Revision:
- 0:851ebdb20ae7
File content as of revision 0:851ebdb20ae7:
#include "mbed.h" int test; using namespace std; Thread thread1; Thread thread2; Thread thread3; Mutex mutex; #define ligthmultiple 7 int interval = 0; bool ack=false; struct ByteData{ uint8_t* data; int length; ByteData(uint8_t* Data,int Length):data(Data),length(Length){}; ByteData(){}; }; struct Datatype{ char* typename4; char* type2name4; Datatype(char* type1,char* type2):typename4(type1),type2name4(type2){}; Datatype(){}; }; struct Datastorage{ uint8_t* data = new uint8_t[3]; bool check; Datastorage(uint8_t* Data,bool Check):data(Data),check(Check){}; Datastorage(){}; }; ByteData DatatoByte(void* data, int length, int typesize, char* typename4, const char* type2name4) { int a = 0; int Extensionlength2 = sizeof(type2name4); // img pdf uint8_t* byte = new uint8_t[length*typesize + 1 + 8 + 2]; switch (typesize) { case 1: for (; a < length; a++) { byte[a] = *((uint8_t*)data + sizeof(uint8_t) * a); } case 2: for (; a < length; a++) { byte[a] = *((uint16_t*)data + sizeof(uint8_t) * a); byte[a + 1] = *((uint16_t*)data + sizeof(uint8_t) * a) >> 4; a++; } case 4: for (; a < length; a++) { byte[a] = *((uint32_t*)data + sizeof(uint8_t) * a); byte[a + 1] = *((uint32_t*)data + sizeof(uint8_t) * a) >> 4; byte[a + 2] = *((uint32_t*)data + sizeof(uint8_t) * a) >> 8; byte[a + 3] = *((uint32_t*)data + sizeof(uint8_t) * a) >> 12; a += 3; } case 8: for (; a < length; a++) { byte[a] = *((uint64_t*)data + sizeof(uint8_t) * a); byte[a + 1] = *((uint64_t*)data + sizeof(uint8_t) * a) >> 4; byte[a + 2] = *((uint64_t*)data + sizeof(uint8_t) * a) >> 8; byte[a + 3] = *((uint64_t*)data + sizeof(uint8_t) * a) >> 12; byte[a + 4] = *((uint64_t*)data + sizeof(uint8_t) * a) >> 16; byte[a + 5] = *((uint64_t*)data + sizeof(uint8_t) * a) >> 20; byte[a + 6] = *((uint64_t*)data + sizeof(uint8_t) * a) >> 24; byte[a + 7] = *((uint64_t*)data + sizeof(uint8_t) * a) >> 32; a += 7; } } byte[a++] = uint8_t('.'); printf("%0x", uint8_t('.')); for (int i = 0; a < length + 5; a++, i++) { byte[a] = uint8_t((typename4[i])); } for (int i = 0; a < length + 9; a++, i++) { byte[a] = uint8_t((type2name4[i])); } byte[a++] = uint8_t('.'); byte[a++] = uint8_t('.'); for (int i = 0; i < a; i++) { printf("%02x = %c\n", byte[i],char(byte[i])); } return ByteData(byte, a); } Datatype ExtractionDatatype(uint8_t* reciveData,int datalength){ char *type1 = (char*)malloc(sizeof(char)*4); char *type2 = (char*)malloc(sizeof(char)*4); for(int i = datalength - 8,j=0; i < datalength-4;i++,j++){ type1[j] = reciveData[i]; } for(int i = datalength - 4,j=0; i < datalength;i++,j++){ type2[j] = reciveData[i]; } return Datatype(type1,type2); } void Sending(uint8_t Data1, uint8_t Data2, uint8_t Data3,DigitalOut D) { bool* send = new bool[32]; uint8_t checksum = Data1 + Data2 + Data3; printf("%c,%c ,%c\n",(char)Data1,(char)Data2,(char)Data3); uint8_t Data[4] = { Data1, Data2, Data3, checksum}; printf("%s",Data); for (int i = 0,j=0; i < 4; i++) { send[j++] = (Data[i] & 1)/1; send[j++] = (Data[i] & 2)/2; send[j++] = (Data[i] & 4)/4; send[j++] = (Data[i] & 8)/8; send[j++] = (Data[i] & 16)/16; send[j++] = (Data[i] & 32)/32; send[j++] = (Data[i] & 64)/64; send[j++] = (Data[i] & 128)/128; /*send[j++] = (Data[i] & 128)/128; send[j++] = (Data[i] & 64)/64; send[j++] = (Data[i] & 32)/32; send[j++] = (Data[i] & 16)/16; send[j++] = (Data[i] & 8)/8; send[j++] = (Data[i] & 4)/4; send[j++] = (Data[i] & 2)/2; send[j++] = (Data[i] & 1)/1;*/ } printf("now really sendingready\r\n"); D = 1; thread_sleep_for(interval*10); printf("now really sending\r\n"); D = 0; thread_sleep_for(interval); for(int j = 31;j >=0;j--){ D = send[j]; thread_sleep_for(interval); } D = 0; for(int j = 31;j >=0;j--){ printf("%d",send[j]); } } void onoff(DigitalOut* D){ while(true){ thread_sleep_for(interval); *D = 1; thread_sleep_for(interval); *D = 0; } } void Recvack(AnalogIn* A){ float val[8]; bool test = true; uint8_t result = 0; while(true){ printf("start recvack\n\r"); val[0] = A->read() * ligthmultiple; printf("wow\n\r"); for(int i = 0;i<8;i++){ val[i] = A->read() * ligthmultiple; thread_sleep_for(interval); } for(int i = 0;i<8;i+=2){ test &= (val[i] > 0.5 )? true : false; printf("%d",(bool)val[i]); } if(test) printf("wow3\n\r"); return; /*val[0] = (val[0] > 0.5 )? true : false;*/ thread_sleep_for(interval*2); /*if(true == val[0]){ printf("=============="); for(int i = 0;i<8;i++){ val[i] = A->read() * ligthmultiple; val[i] = (val[i] > 0.5 )? true : false; thread_sleep_for(interval); } result |= (int)val[0]; result |= (int)val[1]*2; result |= (int)val[2]*4; result |= (int)val[3]*8; result |= (int)val[4]*16; result |= (int)val[5]*32; result |= (int)val[6]*64; result |= (int)val[7]*128; if(result == 0x55){ printf("scuuscusc"); ack = true; printf("start recvack"); } }*/ } } bool Synchronizesender(DigitalOut D,AnalogIn A){ thread1.start(callback(onoff, &D)); thread2.start(callback(Recvack, &A)); printf("nb"); thread2.join(); D = 0; printf("hie"); thread1.terminate(); D = 0; return true; /*if(ack == true){ ack = false; return true; }else{ return false; } */ } void Findclock(AnalogIn *A){ float val; bool start; bool result; int Cycle; int intervaltime; Timer t; t.reset(); printf("now find interval\n\r"); while(true){ val = A->read() * ligthmultiple; if(val < 0.5){ break; } } while(true){ val = A->read() * ligthmultiple; if(val > 0.5){ t.start(); break; } } while(true){ val = A->read() * ligthmultiple; if(val < 0.5){ t.stop(); interval = t.read_ms(); break; } } t.reset(); while(true){ val = A->read() * ligthmultiple; if(val < 0.5){ break; } } while(true){ val = A->read() * ligthmultiple; if(val > 0.5){ t.start(); break; } } while(true){ val = A->read() * ligthmultiple; if(val < 0.5){ t.stop(); intervaltime = t.read_ms(); break; } } printf("interval time = %d,%d\n\r",intervaltime,interval); printf("now find interval suc\n\r"); if(intervaltime==interval){ interval = interval - (interval %10); printf("return"); } else{ printf("fail"); } } void Findlight(AnalogIn* A){ float val; float val2; while(true){ val = A->read() * ligthmultiple; printf("%2.2f\r\n",val); if(val > 0.5){ break; } thread_sleep_for(100); } /*ack=true*/ // 이거 오류나는데 왜지 ? return; } void SendAck(DigitalOut *D){ printf("start sendack"); *D = 0; for(int i = 0;i < 20; i++ ){ *D = !(*D); thread_sleep_for(interval); } } bool Synchronizereciver(DigitalOut D,AnalogIn A){ float val[8]; Findlight(&A); printf("find!!"); printf("now find interval"); thread3.start(callback(Findclock, &A)); /*Findclock(A); //왜 안되징*/ thread3.join(); printf("error?");//여기가 왜 sendack부터 보내버리고 안에있는걸 출력할까 /*SendAck(D);*/ printf("now send ack\n\r"); thread3.start(callback(SendAck, &D)); thread3.join(); printf("send ack suc\n\r"); ack = false; return true; } Datastorage Recieving(AnalogIn sensor) { float val[32]; uint8_t* Data = new uint8_t[4]; printf("ready\r\n"); printf("now really ready\r\n"); while(true){ if((sensor.read() * ligthmultiple)> 0.5){ break; } } while(true){ if((sensor.read() * ligthmultiple)< 0.5){ thread_sleep_for(interval*1.5); break; } } for(int i = 0;i < 32;i++){ val[i] = sensor.read() * ligthmultiple; thread_sleep_for(interval); } for(int i = 0;i < 32;i++){ val[i] = (val[i] > 0.5 )? true : false; } printf("now receive = "); for(int i = 0;i < 32 ;i++){ printf("%d",(int)val[i]); } int j = 4; for (; j >= 0; j--) { Data[j] = 0; Data[j] |= (int)val[j * 8 + 7]; Data[j] |= (int)val[j * 8 + 6] * 2; Data[j] |= (int)val[j * 8 + 5] * 4; Data[j] |= (int)val[j * 8 + 4] * 8; Data[j] |= (int)val[j * 8 + 3] * 16; Data[j] |= (int)val[j * 8 + 2] * 32; Data[j] |= (int)val[j * 8 + 1] * 64; Data[j] |= (int)val[j * 8] * 128; } printf("%c%c%c",(char)Data[3],(char)Data[2],(char)Data[1]); printf("=%0x%0x%0x",Data[3],Data[2],Data[1]); if (Data[0] == (Data[1] + Data[2] + Data[3])) { //SendSucData(); Datastorage returnvalue; returnvalue.data[0] = Data[3]; returnvalue.data[1] = Data[2]; returnvalue.data[2] = Data[1]; returnvalue.check = true; return returnvalue; } else { return Datastorage(Data,false); //SendFailData(); } //32bit } void send(DigitalOut D,AnalogIn sensor){ interval = 100; //이거 임시 char* inputvalue = new char[50]; char* name = new char[4]; name = (char*)"char"; printf("hellow input here\n\r"); scanf("%s", inputvalue); printf("input interval speed\n\r"); scanf("%d", &interval); printf("success\n"); ByteData send = DatatoByte(inputvalue, strlen(inputvalue),sizeof(inputvalue[0]),name,"null"); if(!Synchronizesender(D,sensor)){ printf("fail sending"); } else{ int nulldata = send.length%3; printf("now sending\n"); thread_sleep_for(interval*5); //scanf("%d",&test); printf("........\n"); for(int j = 0; j < send.length;j++){ printf("{%d}{%c=%0x}", j,send.data[j],send.data[j]); } printf("........\n"); printf("sendlength =%d,cycle = %d",send.length,send.length/3); printf("........\n"); int i = 0; for(; i < send.length-nulldata;){ printf("==%c,%c,%c-cycle = %d-==",send.data[i],send.data[i+1],send.data[i+2],i); Sending(send.data[i],send.data[i+1],send.data[i+2],D); D = 0; Recvack(&sensor); D = 0; if(ack == false){ thread_sleep_for(interval*30); i+=3; continue; } ack = false; i+=3; } for(int j=0;j<1;){ switch(nulldata){ case 0: Sending(0x00,0x00,0x00,D); //Sendcheckbit break; case 1: i++; Sending(send.data[i],0x00,0x00,D); //Sendcheckbit break; case 2: i++; Sending(send.data[i],send.data[i+1],0x00,D); //Sendcheckbit break; } D = 0; Recvack(&sensor); D = 0; if(ack == false){ continue; } ack = false; } printf("send suc\n"); } } void recv(DigitalOut D,AnalogIn sensor){ uint8_t* reciveData = (uint8_t*)malloc(sizeof(uint8_t)*256); Datastorage reciveData3; int length; printf("start\n"); if(Synchronizereciver(D,sensor)){ int i = 0; printf("1now receiveing1\n"); thread_sleep_for(interval*7); while(true){ printf("start recieve"); reciveData3 = Recieving(sensor); if(!(reciveData3.check)){ printf("continue"); SendAck(&D); continue;//연산으로 시간차이때문에 대기시간도 필요한데 } else{ SendAck(&D);//sendfail } if(reciveData3.data[0] == 0x00) length = i; break; if(reciveData3.data[1] == 0x00){ reciveData[i++] = reciveData3.data[0]; length = i; break; } if(reciveData3.data[2] == 0x00){ reciveData[i++] = reciveData3.data[0]; reciveData[i++] = reciveData3.data[1]; length = i; break; } reciveData[i++] = reciveData3.data[0]; reciveData[i++] = reciveData3.data[1]; reciveData[i++] = reciveData3.data[2]; } Datatype type = ExtractionDatatype(reciveData,length); printf("suc here\n"); for(int i = 0;i < length-8;i++){ printf("%c",reciveData[i]); } printf("suc here\n"); scanf("%d",&test); }else{ printf("fail\n"); } }