serial arduino controller

Dependents:   serial_arduino 300_PS3Dribbler 300_PS3com Quadrifoglio_PS3COM

Committer:
piroro4560
Date:
Fri Jun 17 06:24:01 2022 +0000
Revision:
2:3b34130dfdd2
Parent:
1:36350a60662e
adjust 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 {
piroro4560 2:3b34130dfdd2 13 while (1) {
piroro4560 2:3b34130dfdd2 14 mini.read(&hedder1,1);
piroro4560 2:3b34130dfdd2 15 mini.read(&hedder2,1);
piroro4560 2:3b34130dfdd2 16 // mini.read(buff,1);
piroro4560 2:3b34130dfdd2 17 // checkSum = atoi(&buff[2]);
piroro4560 2:3b34130dfdd2 18 if(hedder1==0xff
piroro4560 2:3b34130dfdd2 19 && hedder2==0xee
piroro4560 2:3b34130dfdd2 20 )
piroro4560 2:3b34130dfdd2 21 {
piroro4560 2:3b34130dfdd2 22 // mini.read(&button,2);
piroro4560 2:3b34130dfdd2 23 mini.read(&b0,1);
piroro4560 2:3b34130dfdd2 24 mini.read(&b1,1);
piroro4560 2:3b34130dfdd2 25 mini.read(&trigger[0],1);
piroro4560 2:3b34130dfdd2 26 mini.read(&trigger[1],1);
piroro4560 2:3b34130dfdd2 27 mini.read(&stick[0],1);
piroro4560 2:3b34130dfdd2 28 mini.read(&stick[1],1);
piroro4560 2:3b34130dfdd2 29 mini.read(&stick[2],1);
piroro4560 2:3b34130dfdd2 30 mini.read(&stick[3],1);
piroro4560 2:3b34130dfdd2 31 // mini.read(&checkSum,1);
piroro4560 2:3b34130dfdd2 32 mini.read(&timeout,1);
piroro4560 2:3b34130dfdd2 33
piroro4560 2:3b34130dfdd2 34 }
piroro4560 2:3b34130dfdd2 35 // mini.write(CRLF,2);
piroro4560 2:3b34130dfdd2 36 // ThisThread::sleep_for(1);
THtakahiro702286 0:a591557e4771 37 }
THtakahiro702286 0:a591557e4771 38
THtakahiro702286 0:a591557e4771 39 }
THtakahiro702286 0:a591557e4771 40
THtakahiro702286 0:a591557e4771 41 bool SerialArduino::getState()
THtakahiro702286 0:a591557e4771 42 {
THtakahiro702286 0:a591557e4771 43
THtakahiro702286 0:a591557e4771 44 //button
THtakahiro702286 0:a591557e4771 45 button = b0 << 8 | b1;
THtakahiro702286 0:a591557e4771 46 for(i=11;i>=0;i--)
THtakahiro702286 0:a591557e4771 47 {
THtakahiro702286 0:a591557e4771 48 b[i] = button & high;
THtakahiro702286 0:a591557e4771 49 button = button >> 1;
THtakahiro702286 0:a591557e4771 50 }
THtakahiro702286 0:a591557e4771 51 //timeout
THtakahiro702286 0:a591557e4771 52 if(timeout == past)
THtakahiro702286 0:a591557e4771 53 {
THtakahiro702286 0:a591557e4771 54 if(state) timeoutCount++;
THtakahiro702286 0:a591557e4771 55 }
THtakahiro702286 0:a591557e4771 56 else
THtakahiro702286 0:a591557e4771 57 {
THtakahiro702286 0:a591557e4771 58 timeoutCount = 0;
THtakahiro702286 0:a591557e4771 59 state=1;
THtakahiro702286 0:a591557e4771 60 }
THtakahiro702286 0:a591557e4771 61 if(timeoutCount > 20)
THtakahiro702286 0:a591557e4771 62 {
THtakahiro702286 0:a591557e4771 63 state=0;
THtakahiro702286 0:a591557e4771 64 }
THtakahiro702286 0:a591557e4771 65 past = timeout;
THtakahiro702286 0:a591557e4771 66
THtakahiro702286 0:a591557e4771 67 return(state);
THtakahiro702286 0:a591557e4771 68 }
THtakahiro702286 0:a591557e4771 69
THtakahiro702286 0:a591557e4771 70 uint8_t SerialArduino::getHedder1()
THtakahiro702286 0:a591557e4771 71 {
THtakahiro702286 0:a591557e4771 72 return(hedder1);
THtakahiro702286 0:a591557e4771 73 }
THtakahiro702286 0:a591557e4771 74
THtakahiro702286 0:a591557e4771 75 uint8_t SerialArduino::getHedder2()
THtakahiro702286 0:a591557e4771 76 {
THtakahiro702286 0:a591557e4771 77 return(hedder2);
THtakahiro702286 0:a591557e4771 78 }
THtakahiro702286 0:a591557e4771 79
THtakahiro702286 0:a591557e4771 80 bool SerialArduino::getButton(int num)
THtakahiro702286 0:a591557e4771 81 {
THtakahiro702286 0:a591557e4771 82 return(b[num]);
THtakahiro702286 0:a591557e4771 83 }
THtakahiro702286 0:a591557e4771 84
THtakahiro702286 0:a591557e4771 85 uint8_t SerialArduino::getTrigger(int num)
THtakahiro702286 0:a591557e4771 86 {
THtakahiro702286 0:a591557e4771 87 return(trigger[num]);
THtakahiro702286 0:a591557e4771 88 }
THtakahiro702286 0:a591557e4771 89
THtakahiro702286 0:a591557e4771 90 uint8_t SerialArduino::getStick(int num)
THtakahiro702286 0:a591557e4771 91 {
THtakahiro702286 0:a591557e4771 92 return(stick[num]);
THtakahiro702286 0:a591557e4771 93 }
THtakahiro702286 0:a591557e4771 94
THtakahiro702286 0:a591557e4771 95 uint16_t SerialArduino::getTimeoutCount()
THtakahiro702286 0:a591557e4771 96 {
THtakahiro702286 0:a591557e4771 97 return(timeout);
THtakahiro702286 0:a591557e4771 98 }
THtakahiro702286 0:a591557e4771 99
THtakahiro702286 0:a591557e4771 100