serial arduino controller

Dependents:   serial_arduino 300_PS3Dribbler 300_PS3com Quadrifoglio_PS3COM

Committer:
THtakahiro702286
Date:
Fri Jun 17 06:05:57 2022 +0000
Revision:
1:36350a60662e
Parent:
0:a591557e4771
Child:
2:3b34130dfdd2
attach -> thread

Who changed what in which revision?

UserRevisionLine numberNew contents of line
THtakahiro702286 0:a591557e4771 1 #include "SerialArduino.h"
THtakahiro702286 0:a591557e4771 2
THtakahiro702286 0:a591557e4771 3 SerialArduino::SerialArduino(PinName TX,PinName RX, int baud)
THtakahiro702286 0:a591557e4771 4 : mini(TX,RX,baud)
THtakahiro702286 0:a591557e4771 5 {
THtakahiro702286 1:36350a60662e 6 // mini.attach(callback(this,&SerialArduino::serialLoop), SerialBase::RxIrq);
THtakahiro702286 0:a591557e4771 7 state=0;
THtakahiro702286 1:36350a60662e 8 thread.start(callback(this, &SerialArduino::serialLoop));
THtakahiro702286 0:a591557e4771 9 }
THtakahiro702286 0:a591557e4771 10
THtakahiro702286 0:a591557e4771 11 void SerialArduino::serialLoop()
THtakahiro702286 0:a591557e4771 12 {
THtakahiro702286 0:a591557e4771 13 mini.read(&hedder1,1);
THtakahiro702286 0:a591557e4771 14 mini.read(&hedder2,1);
THtakahiro702286 0:a591557e4771 15 // mini.read(buff,1);
THtakahiro702286 0:a591557e4771 16 // checkSum = atoi(&buff[2]);
THtakahiro702286 0:a591557e4771 17 if(hedder1==0xff
THtakahiro702286 0:a591557e4771 18 && hedder2==0xee
THtakahiro702286 0:a591557e4771 19 )
THtakahiro702286 0:a591557e4771 20 {
THtakahiro702286 0:a591557e4771 21 // mini.read(&button,2);
THtakahiro702286 0:a591557e4771 22 mini.read(&b0,1);
THtakahiro702286 0:a591557e4771 23 mini.read(&b1,1);
THtakahiro702286 0:a591557e4771 24 mini.read(&trigger[0],1);
THtakahiro702286 0:a591557e4771 25 mini.read(&trigger[1],1);
THtakahiro702286 0:a591557e4771 26 mini.read(&stick[0],1);
THtakahiro702286 0:a591557e4771 27 mini.read(&stick[1],1);
THtakahiro702286 0:a591557e4771 28 mini.read(&stick[2],1);
THtakahiro702286 0:a591557e4771 29 mini.read(&stick[3],1);
THtakahiro702286 0:a591557e4771 30 // mini.read(&checkSum,1);
THtakahiro702286 0:a591557e4771 31 mini.read(&timeout,1);
THtakahiro702286 0:a591557e4771 32
THtakahiro702286 0:a591557e4771 33 }
THtakahiro702286 0:a591557e4771 34 mini.write(CRLF,2);
THtakahiro702286 1:36350a60662e 35 ThisThread::sleep_for(1);
THtakahiro702286 0:a591557e4771 36
THtakahiro702286 0:a591557e4771 37 }
THtakahiro702286 0:a591557e4771 38
THtakahiro702286 0:a591557e4771 39 bool SerialArduino::getState()
THtakahiro702286 0:a591557e4771 40 {
THtakahiro702286 0:a591557e4771 41
THtakahiro702286 0:a591557e4771 42 //button
THtakahiro702286 0:a591557e4771 43 button = b0 << 8 | b1;
THtakahiro702286 0:a591557e4771 44 for(i=11;i>=0;i--)
THtakahiro702286 0:a591557e4771 45 {
THtakahiro702286 0:a591557e4771 46 b[i] = button & high;
THtakahiro702286 0:a591557e4771 47 button = button >> 1;
THtakahiro702286 0:a591557e4771 48 }
THtakahiro702286 0:a591557e4771 49 //timeout
THtakahiro702286 0:a591557e4771 50 if(timeout == past)
THtakahiro702286 0:a591557e4771 51 {
THtakahiro702286 0:a591557e4771 52 if(state) timeoutCount++;
THtakahiro702286 0:a591557e4771 53 }
THtakahiro702286 0:a591557e4771 54 else
THtakahiro702286 0:a591557e4771 55 {
THtakahiro702286 0:a591557e4771 56 timeoutCount = 0;
THtakahiro702286 0:a591557e4771 57 state=1;
THtakahiro702286 0:a591557e4771 58 }
THtakahiro702286 0:a591557e4771 59 if(timeoutCount > 20)
THtakahiro702286 0:a591557e4771 60 {
THtakahiro702286 0:a591557e4771 61 state=0;
THtakahiro702286 0:a591557e4771 62 }
THtakahiro702286 0:a591557e4771 63 past = timeout;
THtakahiro702286 0:a591557e4771 64
THtakahiro702286 0:a591557e4771 65 return(state);
THtakahiro702286 0:a591557e4771 66 }
THtakahiro702286 0:a591557e4771 67
THtakahiro702286 0:a591557e4771 68 uint8_t SerialArduino::getHedder1()
THtakahiro702286 0:a591557e4771 69 {
THtakahiro702286 0:a591557e4771 70 return(hedder1);
THtakahiro702286 0:a591557e4771 71 }
THtakahiro702286 0:a591557e4771 72
THtakahiro702286 0:a591557e4771 73 uint8_t SerialArduino::getHedder2()
THtakahiro702286 0:a591557e4771 74 {
THtakahiro702286 0:a591557e4771 75 return(hedder2);
THtakahiro702286 0:a591557e4771 76 }
THtakahiro702286 0:a591557e4771 77
THtakahiro702286 0:a591557e4771 78 bool SerialArduino::getButton(int num)
THtakahiro702286 0:a591557e4771 79 {
THtakahiro702286 0:a591557e4771 80 return(b[num]);
THtakahiro702286 0:a591557e4771 81 }
THtakahiro702286 0:a591557e4771 82
THtakahiro702286 0:a591557e4771 83 uint8_t SerialArduino::getTrigger(int num)
THtakahiro702286 0:a591557e4771 84 {
THtakahiro702286 0:a591557e4771 85 return(trigger[num]);
THtakahiro702286 0:a591557e4771 86 }
THtakahiro702286 0:a591557e4771 87
THtakahiro702286 0:a591557e4771 88 uint8_t SerialArduino::getStick(int num)
THtakahiro702286 0:a591557e4771 89 {
THtakahiro702286 0:a591557e4771 90 return(stick[num]);
THtakahiro702286 0:a591557e4771 91 }
THtakahiro702286 0:a591557e4771 92
THtakahiro702286 0:a591557e4771 93 uint16_t SerialArduino::getTimeoutCount()
THtakahiro702286 0:a591557e4771 94 {
THtakahiro702286 0:a591557e4771 95 return(timeout);
THtakahiro702286 0:a591557e4771 96 }
THtakahiro702286 0:a591557e4771 97
THtakahiro702286 0:a591557e4771 98