Sub programs of serial_transport, PID and motor contrl.

Dependents:   tracking_ball_0516 tracking_ball_0516

Committer:
helenh
Date:
Sun May 30 01:39:35 2021 +0000
Revision:
7:fd405402ff8c
Parent:
4:82765e1fd9db
e

Who changed what in which revision?

UserRevisionLine numberNew contents of line
helenh 2:a668eb71516b 1 #include "Circle_Buffer_Class.h"
helenh 2:a668eb71516b 2
helenh 2:a668eb71516b 3 Circle_Buffer_Class::Circle_Buffer_Class(int size)
helenh 2:a668eb71516b 4 {
helenh 7:fd405402ff8c 5 if(size<10) size=10;
helenh 7:fd405402ff8c 6 else if(size>50) size=50;
helenh 2:a668eb71516b 7 this->buffer_size=size;
helenh 2:a668eb71516b 8 this->pbuffer=new unsigned char[size];
helenh 2:a668eb71516b 9 Init_Circle_Struct();
helenh 2:a668eb71516b 10 }
helenh 2:a668eb71516b 11
helenh 2:a668eb71516b 12 Circle_Buffer_Class::~Circle_Buffer_Class()
helenh 2:a668eb71516b 13 {
helenh 2:a668eb71516b 14 delete this->pbuffer;
helenh 2:a668eb71516b 15 this->pbuffer=NULL;
helenh 2:a668eb71516b 16 }
helenh 2:a668eb71516b 17
helenh 2:a668eb71516b 18 //初始化
helenh 2:a668eb71516b 19 void Circle_Buffer_Class::Init_Circle_Struct()
helenh 2:a668eb71516b 20 {
helenh 2:a668eb71516b 21 this->Write_Index=1;
helenh 2:a668eb71516b 22 this->Read_Index= 0;
helenh 2:a668eb71516b 23 this->pbuffer[0]='\0'; //第一个单元设置为结束符
helenh 2:a668eb71516b 24
helenh 2:a668eb71516b 25 }
helenh 2:a668eb71516b 26
helenh 2:a668eb71516b 27
helenh 2:a668eb71516b 28 //写一个数据到环形数组
helenh 2:a668eb71516b 29 unsigned char Circle_Buffer_Class::Write_To(unsigned char infor)
helenh 2:a668eb71516b 30 {
helenh 2:a668eb71516b 31 unsigned char tem;
helenh 2:a668eb71516b 32 unsigned char count=0;
helenh 2:a668eb71516b 33 //下一个写入位置。保持同Read_Index的距离
helenh 2:a668eb71516b 34 tem=(this->Write_Index+1)%this->buffer_size;
helenh 2:a668eb71516b 35 while(tem==this->Read_Index)
helenh 2:a668eb71516b 36 {
helenh 2:a668eb71516b 37 //
helenh 4:82765e1fd9db 38 ThisThread::sleep_for(100);
helenh 2:a668eb71516b 39 count++;
helenh 2:a668eb71516b 40 if(count>10) break;
helenh 2:a668eb71516b 41 }
helenh 2:a668eb71516b 42
helenh 2:a668eb71516b 43 if(count>10) return 0;
helenh 2:a668eb71516b 44
helenh 2:a668eb71516b 45 //赋值
helenh 2:a668eb71516b 46 this->pbuffer[this->Write_Index]=infor;
helenh 2:a668eb71516b 47 //修改Write_Index,
helenh 2:a668eb71516b 48 this->Write_Index=tem;
helenh 2:a668eb71516b 49 return 1;
helenh 2:a668eb71516b 50 }
helenh 2:a668eb71516b 51
helenh 2:a668eb71516b 52 //从环形数组读取一个数据
helenh 2:a668eb71516b 53 //返回值:0表示没有读到数据,1:读到数据
helenh 2:a668eb71516b 54 //读取的数据在*p.
helenh 2:a668eb71516b 55 unsigned char Circle_Buffer_Class::Read_From(unsigned char *p)
helenh 2:a668eb71516b 56 {
helenh 2:a668eb71516b 57 unsigned char result=0;
helenh 2:a668eb71516b 58 //环形缓冲数组是否为空
helenh 2:a668eb71516b 59 if(this->Read_Index==this->Write_Index)
helenh 2:a668eb71516b 60 {
helenh 2:a668eb71516b 61 //空
helenh 2:a668eb71516b 62 return result;
helenh 2:a668eb71516b 63 }
helenh 2:a668eb71516b 64 //读取值
helenh 2:a668eb71516b 65 *p=this->pbuffer[this->Read_Index];
helenh 2:a668eb71516b 66 //修改指针
helenh 2:a668eb71516b 67 this->Read_Index=(this->Read_Index+1)%buffer_size;
helenh 2:a668eb71516b 68 //设置返回值
helenh 2:a668eb71516b 69 result=1;
helenh 2:a668eb71516b 70 return result;
helenh 2:a668eb71516b 71 }
helenh 2:a668eb71516b 72