謙一 永井
/
SRC2018Auto
0910
DualShockMod.cpp@0:2a0c62e53e9c, 2018-09-12 (annotated)
- Committer:
- Hase_jun
- Date:
- Wed Sep 12 09:07:30 2018 +0000
- Revision:
- 0:2a0c62e53e9c
0912
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hase_jun | 0:2a0c62e53e9c | 1 | #include "DualShockMod.h" |
Hase_jun | 0:2a0c62e53e9c | 2 | |
Hase_jun | 0:2a0c62e53e9c | 3 | |
Hase_jun | 0:2a0c62e53e9c | 4 | tDSParm hDS; |
Hase_jun | 0:2a0c62e53e9c | 5 | int posX; |
Hase_jun | 0:2a0c62e53e9c | 6 | int posY; |
Hase_jun | 0:2a0c62e53e9c | 7 | static uint8_t recv_posdata, check = 0; |
Hase_jun | 0:2a0c62e53e9c | 8 | static Serial* ds_serial; //メイン |
Hase_jun | 0:2a0c62e53e9c | 9 | //static float DimensionlessAndBacklash(int8_t dat); |
Hase_jun | 0:2a0c62e53e9c | 10 | static uint8_t getsum(void *buf, size_t size); |
Hase_jun | 0:2a0c62e53e9c | 11 | |
Hase_jun | 0:2a0c62e53e9c | 12 | void ReStartDS(void) { |
Hase_jun | 0:2a0c62e53e9c | 13 | check = 0; |
Hase_jun | 0:2a0c62e53e9c | 14 | // printf("DSint1\r\n"); |
Hase_jun | 0:2a0c62e53e9c | 15 | recv_posdata = (*ds_serial).getc(); |
Hase_jun | 0:2a0c62e53e9c | 16 | // printf("DSint2\r\n"); |
Hase_jun | 0:2a0c62e53e9c | 17 | } |
Hase_jun | 0:2a0c62e53e9c | 18 | |
Hase_jun | 0:2a0c62e53e9c | 19 | uint8_t InitDS(Serial* f_serial) { |
Hase_jun | 0:2a0c62e53e9c | 20 | //huartDS = huart; |
Hase_jun | 0:2a0c62e53e9c | 21 | ds_serial = f_serial; |
Hase_jun | 0:2a0c62e53e9c | 22 | posX=0; //externを実体化するための宣言 |
Hase_jun | 0:2a0c62e53e9c | 23 | posY=0; |
Hase_jun | 0:2a0c62e53e9c | 24 | |
Hase_jun | 0:2a0c62e53e9c | 25 | printf("SUB303 connecting check...\r\n"); |
Hase_jun | 0:2a0c62e53e9c | 26 | ReStartDS(); |
Hase_jun | 0:2a0c62e53e9c | 27 | printf("SUB303 connect successful...\r\n"); |
Hase_jun | 0:2a0c62e53e9c | 28 | return 0; |
Hase_jun | 0:2a0c62e53e9c | 29 | } |
Hase_jun | 0:2a0c62e53e9c | 30 | //&getPOSdata |
Hase_jun | 0:2a0c62e53e9c | 31 | void getPOSdata(void) { |
Hase_jun | 0:2a0c62e53e9c | 32 | |
Hase_jun | 0:2a0c62e53e9c | 33 | static uint8_t dat[6] = {0}; |
Hase_jun | 0:2a0c62e53e9c | 34 | recv_posdata = (*ds_serial).getc(); |
Hase_jun | 0:2a0c62e53e9c | 35 | dat[check] = recv_posdata; |
Hase_jun | 0:2a0c62e53e9c | 36 | |
Hase_jun | 0:2a0c62e53e9c | 37 | //posX ++; //debug |
Hase_jun | 0:2a0c62e53e9c | 38 | |
Hase_jun | 0:2a0c62e53e9c | 39 | |
Hase_jun | 0:2a0c62e53e9c | 40 | switch (check) { |
Hase_jun | 0:2a0c62e53e9c | 41 | case 0: |
Hase_jun | 0:2a0c62e53e9c | 42 | if (dat[0] & 0x80) { // & は両方1なら1を返す |
Hase_jun | 0:2a0c62e53e9c | 43 | check++; |
Hase_jun | 0:2a0c62e53e9c | 44 | } |
Hase_jun | 0:2a0c62e53e9c | 45 | break; |
Hase_jun | 0:2a0c62e53e9c | 46 | case 5: |
Hase_jun | 0:2a0c62e53e9c | 47 | if (dat[5] == (getsum(dat, 5) & 0x7f)) { //ここは0~5までの合計の意味 |
Hase_jun | 0:2a0c62e53e9c | 48 | if(dat[0] & 0b00000010){ //Xが負であるビット |
Hase_jun | 0:2a0c62e53e9c | 49 | posX = (int)((dat[1]<<7) + dat[2])*-1; |
Hase_jun | 0:2a0c62e53e9c | 50 | } |
Hase_jun | 0:2a0c62e53e9c | 51 | else{ |
Hase_jun | 0:2a0c62e53e9c | 52 | posX = ((dat[1]<<7) + dat[2]); |
Hase_jun | 0:2a0c62e53e9c | 53 | } |
Hase_jun | 0:2a0c62e53e9c | 54 | if(dat[0] & 0b00000001){ //Yが負であるビット |
Hase_jun | 0:2a0c62e53e9c | 55 | posY = (int)((dat[3]<<7) + dat[4])*-1; |
Hase_jun | 0:2a0c62e53e9c | 56 | } |
Hase_jun | 0:2a0c62e53e9c | 57 | else{ |
Hase_jun | 0:2a0c62e53e9c | 58 | posY = (dat[3]<<7) + dat[4]; |
Hase_jun | 0:2a0c62e53e9c | 59 | } |
Hase_jun | 0:2a0c62e53e9c | 60 | } |
Hase_jun | 0:2a0c62e53e9c | 61 | dat[0] = 0; |
Hase_jun | 0:2a0c62e53e9c | 62 | check = 0; |
Hase_jun | 0:2a0c62e53e9c | 63 | break; |
Hase_jun | 0:2a0c62e53e9c | 64 | default: |
Hase_jun | 0:2a0c62e53e9c | 65 | check++; |
Hase_jun | 0:2a0c62e53e9c | 66 | break; |
Hase_jun | 0:2a0c62e53e9c | 67 | } |
Hase_jun | 0:2a0c62e53e9c | 68 | } |
Hase_jun | 0:2a0c62e53e9c | 69 | /* |
Hase_jun | 0:2a0c62e53e9c | 70 | static float DimensionlessAndBacklash(int8_t dat) { |
Hase_jun | 0:2a0c62e53e9c | 71 | float val; |
Hase_jun | 0:2a0c62e53e9c | 72 | |
Hase_jun | 0:2a0c62e53e9c | 73 | if ((dat < BACKLASH) && (dat > -BACKLASH)) { |
Hase_jun | 0:2a0c62e53e9c | 74 | dat = 0; |
Hase_jun | 0:2a0c62e53e9c | 75 | } else { |
Hase_jun | 0:2a0c62e53e9c | 76 | dat += (dat > 0) ? (-BACKLASH) : (BACKLASH); |
Hase_jun | 0:2a0c62e53e9c | 77 | } |
Hase_jun | 0:2a0c62e53e9c | 78 | val = (float) dat / (float) (128 - BACKLASH); |
Hase_jun | 0:2a0c62e53e9c | 79 | return val; |
Hase_jun | 0:2a0c62e53e9c | 80 | } |
Hase_jun | 0:2a0c62e53e9c | 81 | */ |
Hase_jun | 0:2a0c62e53e9c | 82 | static uint8_t getsum(void *buf, size_t size) { |
Hase_jun | 0:2a0c62e53e9c | 83 | const uint8_t *p = (uint8_t*) buf; |
Hase_jun | 0:2a0c62e53e9c | 84 | uint8_t ret = 0; |
Hase_jun | 0:2a0c62e53e9c | 85 | |
Hase_jun | 0:2a0c62e53e9c | 86 | for (; size; size--) { |
Hase_jun | 0:2a0c62e53e9c | 87 | ret += *p++; |
Hase_jun | 0:2a0c62e53e9c | 88 | } |
Hase_jun | 0:2a0c62e53e9c | 89 | return ret; |
Hase_jun | 0:2a0c62e53e9c | 90 | } |
Hase_jun | 0:2a0c62e53e9c | 91 |