Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
DualShockMod.cpp@0:d14e21c64226, 2019-09-13 (annotated)
- Committer:
- megu29
- Date:
- Fri Sep 13 11:18:21 2019 +0000
- Revision:
- 0:d14e21c64226
- Child:
- 1:8a67adccebd9
0913;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| megu29 | 0:d14e21c64226 | 1 | #include "DualShockMod.h" |
| megu29 | 0:d14e21c64226 | 2 | |
| megu29 | 0:d14e21c64226 | 3 | |
| megu29 | 0:d14e21c64226 | 4 | tDSParm hDS; |
| megu29 | 0:d14e21c64226 | 5 | int posX; |
| megu29 | 0:d14e21c64226 | 6 | int posY; |
| megu29 | 0:d14e21c64226 | 7 | static uint8_t recv_posdata, check = 0; |
| megu29 | 0:d14e21c64226 | 8 | static Serial* ds_serial; //メイン |
| megu29 | 0:d14e21c64226 | 9 | //static float DimensionlessAndBacklash(int8_t dat); |
| megu29 | 0:d14e21c64226 | 10 | static uint8_t getsum(void *buf, size_t size); |
| megu29 | 0:d14e21c64226 | 11 | |
| megu29 | 0:d14e21c64226 | 12 | void ReStartDS(void) { |
| megu29 | 0:d14e21c64226 | 13 | check = 0; |
| megu29 | 0:d14e21c64226 | 14 | // printf("DSint1\r\n"); |
| megu29 | 0:d14e21c64226 | 15 | recv_posdata = (*ds_serial).getc(); |
| megu29 | 0:d14e21c64226 | 16 | // printf("DSint2\r\n"); |
| megu29 | 0:d14e21c64226 | 17 | } |
| megu29 | 0:d14e21c64226 | 18 | |
| megu29 | 0:d14e21c64226 | 19 | uint8_t InitDS(Serial* f_serial) { |
| megu29 | 0:d14e21c64226 | 20 | //huartDS = huart; |
| megu29 | 0:d14e21c64226 | 21 | ds_serial = f_serial; |
| megu29 | 0:d14e21c64226 | 22 | posX=0; //externを実体化するための宣言 |
| megu29 | 0:d14e21c64226 | 23 | posY=0; |
| megu29 | 0:d14e21c64226 | 24 | |
| megu29 | 0:d14e21c64226 | 25 | printf("SUB303 connecting check...\r\n"); |
| megu29 | 0:d14e21c64226 | 26 | ReStartDS(); |
| megu29 | 0:d14e21c64226 | 27 | printf("SUB303 connect successful...\r\n"); |
| megu29 | 0:d14e21c64226 | 28 | return 0; |
| megu29 | 0:d14e21c64226 | 29 | } |
| megu29 | 0:d14e21c64226 | 30 | //&getPOSdata |
| megu29 | 0:d14e21c64226 | 31 | void getPOSdata(void) { |
| megu29 | 0:d14e21c64226 | 32 | |
| megu29 | 0:d14e21c64226 | 33 | static uint8_t dat[6] = {0}; |
| megu29 | 0:d14e21c64226 | 34 | recv_posdata = (*ds_serial).getc(); |
| megu29 | 0:d14e21c64226 | 35 | dat[check] = recv_posdata; |
| megu29 | 0:d14e21c64226 | 36 | |
| megu29 | 0:d14e21c64226 | 37 | //posX ++; //debug |
| megu29 | 0:d14e21c64226 | 38 | |
| megu29 | 0:d14e21c64226 | 39 | |
| megu29 | 0:d14e21c64226 | 40 | switch (check) { |
| megu29 | 0:d14e21c64226 | 41 | case 0: |
| megu29 | 0:d14e21c64226 | 42 | if (dat[0] & 0x80) { // & は両方1なら1を返す |
| megu29 | 0:d14e21c64226 | 43 | check++; |
| megu29 | 0:d14e21c64226 | 44 | } |
| megu29 | 0:d14e21c64226 | 45 | break; |
| megu29 | 0:d14e21c64226 | 46 | case 5: |
| megu29 | 0:d14e21c64226 | 47 | if (dat[5] == (getsum(dat, 5) & 0x7f)) { //ここは0~5までの合計の意味 |
| megu29 | 0:d14e21c64226 | 48 | if(dat[0] & 0b00000010){ //Xが負であるビット |
| megu29 | 0:d14e21c64226 | 49 | posX = (int)((dat[1]<<7) + dat[2])*-1; |
| megu29 | 0:d14e21c64226 | 50 | } |
| megu29 | 0:d14e21c64226 | 51 | else{ |
| megu29 | 0:d14e21c64226 | 52 | posX = ((dat[1]<<7) + dat[2]); |
| megu29 | 0:d14e21c64226 | 53 | } |
| megu29 | 0:d14e21c64226 | 54 | if(dat[0] & 0b00000001){ //Yが負であるビット |
| megu29 | 0:d14e21c64226 | 55 | posY = (int)((dat[3]<<7) + dat[4])*-1; |
| megu29 | 0:d14e21c64226 | 56 | } |
| megu29 | 0:d14e21c64226 | 57 | else{ |
| megu29 | 0:d14e21c64226 | 58 | posY = (dat[3]<<7) + dat[4]; |
| megu29 | 0:d14e21c64226 | 59 | } |
| megu29 | 0:d14e21c64226 | 60 | } |
| megu29 | 0:d14e21c64226 | 61 | dat[0] = 0; |
| megu29 | 0:d14e21c64226 | 62 | check = 0; |
| megu29 | 0:d14e21c64226 | 63 | break; |
| megu29 | 0:d14e21c64226 | 64 | default: |
| megu29 | 0:d14e21c64226 | 65 | check++; |
| megu29 | 0:d14e21c64226 | 66 | break; |
| megu29 | 0:d14e21c64226 | 67 | } |
| megu29 | 0:d14e21c64226 | 68 | } |
| megu29 | 0:d14e21c64226 | 69 | /* |
| megu29 | 0:d14e21c64226 | 70 | static float DimensionlessAndBacklash(int8_t dat) { |
| megu29 | 0:d14e21c64226 | 71 | float val; |
| megu29 | 0:d14e21c64226 | 72 | |
| megu29 | 0:d14e21c64226 | 73 | if ((dat < BACKLASH) && (dat > -BACKLASH)) { |
| megu29 | 0:d14e21c64226 | 74 | dat = 0; |
| megu29 | 0:d14e21c64226 | 75 | } else { |
| megu29 | 0:d14e21c64226 | 76 | dat += (dat > 0) ? (-BACKLASH) : (BACKLASH); |
| megu29 | 0:d14e21c64226 | 77 | } |
| megu29 | 0:d14e21c64226 | 78 | val = (float) dat / (float) (128 - BACKLASH); |
| megu29 | 0:d14e21c64226 | 79 | return val; |
| megu29 | 0:d14e21c64226 | 80 | } |
| megu29 | 0:d14e21c64226 | 81 | */ |
| megu29 | 0:d14e21c64226 | 82 | static uint8_t getsum(void *buf, size_t size) { |
| megu29 | 0:d14e21c64226 | 83 | const uint8_t *p = (uint8_t*) buf; |
| megu29 | 0:d14e21c64226 | 84 | uint8_t ret = 0; |
| megu29 | 0:d14e21c64226 | 85 | |
| megu29 | 0:d14e21c64226 | 86 | for (; size; size--) { |
| megu29 | 0:d14e21c64226 | 87 | ret += *p++; |
| megu29 | 0:d14e21c64226 | 88 | } |
| megu29 | 0:d14e21c64226 | 89 | return ret; |
| megu29 | 0:d14e21c64226 | 90 | } |
| megu29 | 0:d14e21c64226 | 91 |