serial arduino controller
Dependents: serial_arduino 300_PS3Dribbler 300_PS3com Quadrifoglio_PS3COM
SerialArduino.cpp@0:a591557e4771, 2022-06-07 (annotated)
- Committer:
- THtakahiro702286
- Date:
- Tue Jun 07 04:10:16 2022 +0000
- Revision:
- 0:a591557e4771
- Child:
- 1:36350a60662e
first commit
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:a591557e4771 | 6 | mini.attach(callback(this,&SerialArduino::serialLoop), SerialBase::RxIrq); |
THtakahiro702286 | 0:a591557e4771 | 7 | state=0; |
THtakahiro702286 | 0:a591557e4771 | 8 | } |
THtakahiro702286 | 0:a591557e4771 | 9 | |
THtakahiro702286 | 0:a591557e4771 | 10 | void SerialArduino::serialLoop() |
THtakahiro702286 | 0:a591557e4771 | 11 | { |
THtakahiro702286 | 0:a591557e4771 | 12 | mini.read(&hedder1,1); |
THtakahiro702286 | 0:a591557e4771 | 13 | mini.read(&hedder2,1); |
THtakahiro702286 | 0:a591557e4771 | 14 | // mini.read(buff,1); |
THtakahiro702286 | 0:a591557e4771 | 15 | // checkSum = atoi(&buff[2]); |
THtakahiro702286 | 0:a591557e4771 | 16 | if(hedder1==0xff |
THtakahiro702286 | 0:a591557e4771 | 17 | && hedder2==0xee |
THtakahiro702286 | 0:a591557e4771 | 18 | ) |
THtakahiro702286 | 0:a591557e4771 | 19 | { |
THtakahiro702286 | 0:a591557e4771 | 20 | // mini.read(&button,2); |
THtakahiro702286 | 0:a591557e4771 | 21 | mini.read(&b0,1); |
THtakahiro702286 | 0:a591557e4771 | 22 | mini.read(&b1,1); |
THtakahiro702286 | 0:a591557e4771 | 23 | mini.read(&trigger[0],1); |
THtakahiro702286 | 0:a591557e4771 | 24 | mini.read(&trigger[1],1); |
THtakahiro702286 | 0:a591557e4771 | 25 | mini.read(&stick[0],1); |
THtakahiro702286 | 0:a591557e4771 | 26 | mini.read(&stick[1],1); |
THtakahiro702286 | 0:a591557e4771 | 27 | mini.read(&stick[2],1); |
THtakahiro702286 | 0:a591557e4771 | 28 | mini.read(&stick[3],1); |
THtakahiro702286 | 0:a591557e4771 | 29 | // mini.read(&checkSum,1); |
THtakahiro702286 | 0:a591557e4771 | 30 | mini.read(&timeout,1); |
THtakahiro702286 | 0:a591557e4771 | 31 | |
THtakahiro702286 | 0:a591557e4771 | 32 | } |
THtakahiro702286 | 0:a591557e4771 | 33 | mini.write(CRLF,2); |
THtakahiro702286 | 0:a591557e4771 | 34 | |
THtakahiro702286 | 0:a591557e4771 | 35 | } |
THtakahiro702286 | 0:a591557e4771 | 36 | |
THtakahiro702286 | 0:a591557e4771 | 37 | bool SerialArduino::getState() |
THtakahiro702286 | 0:a591557e4771 | 38 | { |
THtakahiro702286 | 0:a591557e4771 | 39 | |
THtakahiro702286 | 0:a591557e4771 | 40 | //button |
THtakahiro702286 | 0:a591557e4771 | 41 | button = b0 << 8 | b1; |
THtakahiro702286 | 0:a591557e4771 | 42 | for(i=11;i>=0;i--) |
THtakahiro702286 | 0:a591557e4771 | 43 | { |
THtakahiro702286 | 0:a591557e4771 | 44 | b[i] = button & high; |
THtakahiro702286 | 0:a591557e4771 | 45 | button = button >> 1; |
THtakahiro702286 | 0:a591557e4771 | 46 | } |
THtakahiro702286 | 0:a591557e4771 | 47 | //timeout |
THtakahiro702286 | 0:a591557e4771 | 48 | if(timeout == past) |
THtakahiro702286 | 0:a591557e4771 | 49 | { |
THtakahiro702286 | 0:a591557e4771 | 50 | if(state) timeoutCount++; |
THtakahiro702286 | 0:a591557e4771 | 51 | } |
THtakahiro702286 | 0:a591557e4771 | 52 | else |
THtakahiro702286 | 0:a591557e4771 | 53 | { |
THtakahiro702286 | 0:a591557e4771 | 54 | timeoutCount = 0; |
THtakahiro702286 | 0:a591557e4771 | 55 | state=1; |
THtakahiro702286 | 0:a591557e4771 | 56 | } |
THtakahiro702286 | 0:a591557e4771 | 57 | if(timeoutCount > 20) |
THtakahiro702286 | 0:a591557e4771 | 58 | { |
THtakahiro702286 | 0:a591557e4771 | 59 | state=0; |
THtakahiro702286 | 0:a591557e4771 | 60 | } |
THtakahiro702286 | 0:a591557e4771 | 61 | past = timeout; |
THtakahiro702286 | 0:a591557e4771 | 62 | |
THtakahiro702286 | 0:a591557e4771 | 63 | return(state); |
THtakahiro702286 | 0:a591557e4771 | 64 | } |
THtakahiro702286 | 0:a591557e4771 | 65 | |
THtakahiro702286 | 0:a591557e4771 | 66 | uint8_t SerialArduino::getHedder1() |
THtakahiro702286 | 0:a591557e4771 | 67 | { |
THtakahiro702286 | 0:a591557e4771 | 68 | return(hedder1); |
THtakahiro702286 | 0:a591557e4771 | 69 | } |
THtakahiro702286 | 0:a591557e4771 | 70 | |
THtakahiro702286 | 0:a591557e4771 | 71 | uint8_t SerialArduino::getHedder2() |
THtakahiro702286 | 0:a591557e4771 | 72 | { |
THtakahiro702286 | 0:a591557e4771 | 73 | return(hedder2); |
THtakahiro702286 | 0:a591557e4771 | 74 | } |
THtakahiro702286 | 0:a591557e4771 | 75 | |
THtakahiro702286 | 0:a591557e4771 | 76 | bool SerialArduino::getButton(int num) |
THtakahiro702286 | 0:a591557e4771 | 77 | { |
THtakahiro702286 | 0:a591557e4771 | 78 | return(b[num]); |
THtakahiro702286 | 0:a591557e4771 | 79 | } |
THtakahiro702286 | 0:a591557e4771 | 80 | |
THtakahiro702286 | 0:a591557e4771 | 81 | uint8_t SerialArduino::getTrigger(int num) |
THtakahiro702286 | 0:a591557e4771 | 82 | { |
THtakahiro702286 | 0:a591557e4771 | 83 | return(trigger[num]); |
THtakahiro702286 | 0:a591557e4771 | 84 | } |
THtakahiro702286 | 0:a591557e4771 | 85 | |
THtakahiro702286 | 0:a591557e4771 | 86 | uint8_t SerialArduino::getStick(int num) |
THtakahiro702286 | 0:a591557e4771 | 87 | { |
THtakahiro702286 | 0:a591557e4771 | 88 | return(stick[num]); |
THtakahiro702286 | 0:a591557e4771 | 89 | } |
THtakahiro702286 | 0:a591557e4771 | 90 | |
THtakahiro702286 | 0:a591557e4771 | 91 | uint16_t SerialArduino::getTimeoutCount() |
THtakahiro702286 | 0:a591557e4771 | 92 | { |
THtakahiro702286 | 0:a591557e4771 | 93 | return(timeout); |
THtakahiro702286 | 0:a591557e4771 | 94 | } |
THtakahiro702286 | 0:a591557e4771 | 95 | |
THtakahiro702286 | 0:a591557e4771 | 96 |