Kazushi Yamanobe
/
f-I2Cmaster
the latest version at 9/26
main.cpp@0:37bb1a446e69, 2021-10-10 (annotated)
- Committer:
- Yamanobe
- Date:
- Sun Oct 10 04:48:19 2021 +0000
- Revision:
- 0:37bb1a446e69
sync with hori;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yamanobe | 0:37bb1a446e69 | 1 | #include "mbed.h" |
Yamanobe | 0:37bb1a446e69 | 2 | |
Yamanobe | 0:37bb1a446e69 | 3 | #define PI 3.1415 |
Yamanobe | 0:37bb1a446e69 | 4 | |
Yamanobe | 0:37bb1a446e69 | 5 | AnalogIn volume(A1); |
Yamanobe | 0:37bb1a446e69 | 6 | I2C i2c(D4, D5); // sda, scl |
Yamanobe | 0:37bb1a446e69 | 7 | Serial pc(USBTX, USBRX, 115200); // tx, rx |
Yamanobe | 0:37bb1a446e69 | 8 | |
Yamanobe | 0:37bb1a446e69 | 9 | int id_list[6] = { |
Yamanobe | 0:37bb1a446e69 | 10 | 0x03, 0x06, 0x14, 0x15, 0x19, 0x20 |
Yamanobe | 0:37bb1a446e69 | 11 | }; |
Yamanobe | 0:37bb1a446e69 | 12 | |
Yamanobe | 0:37bb1a446e69 | 13 | int TxAngleI2C(int id_device, int position){ |
Yamanobe | 0:37bb1a446e69 | 14 | char cmd[4] = {0xFF, 0x00, 0x00, 0xFF}; |
Yamanobe | 0:37bb1a446e69 | 15 | cmd[1] = id_device; |
Yamanobe | 0:37bb1a446e69 | 16 | cmd[2] = position; |
Yamanobe | 0:37bb1a446e69 | 17 | |
Yamanobe | 0:37bb1a446e69 | 18 | wait_ms(10); |
Yamanobe | 0:37bb1a446e69 | 19 | int flag_send = i2c.write(id_device, cmd, 4); |
Yamanobe | 0:37bb1a446e69 | 20 | |
Yamanobe | 0:37bb1a446e69 | 21 | if(flag_send == 0){ |
Yamanobe | 0:37bb1a446e69 | 22 | return 0; |
Yamanobe | 0:37bb1a446e69 | 23 | }else{ |
Yamanobe | 0:37bb1a446e69 | 24 | return 1; |
Yamanobe | 0:37bb1a446e69 | 25 | } |
Yamanobe | 0:37bb1a446e69 | 26 | } |
Yamanobe | 0:37bb1a446e69 | 27 | |
Yamanobe | 0:37bb1a446e69 | 28 | int GetJoyAngle(){ |
Yamanobe | 0:37bb1a446e69 | 29 | float dat_v = volume.read(); |
Yamanobe | 0:37bb1a446e69 | 30 | |
Yamanobe | 0:37bb1a446e69 | 31 | int angle_24p = int(dat_v * 24); |
Yamanobe | 0:37bb1a446e69 | 32 | |
Yamanobe | 0:37bb1a446e69 | 33 | pc.printf("%d\r\n", angle_24p); |
Yamanobe | 0:37bb1a446e69 | 34 | |
Yamanobe | 0:37bb1a446e69 | 35 | return angle_24p; |
Yamanobe | 0:37bb1a446e69 | 36 | } |
Yamanobe | 0:37bb1a446e69 | 37 | |
Yamanobe | 0:37bb1a446e69 | 38 | int main() { |
Yamanobe | 0:37bb1a446e69 | 39 | |
Yamanobe | 0:37bb1a446e69 | 40 | pc.printf("OK\r\n"); |
Yamanobe | 0:37bb1a446e69 | 41 | |
Yamanobe | 0:37bb1a446e69 | 42 | while(1) { |
Yamanobe | 0:37bb1a446e69 | 43 | TxAngleI2C(0x03, GetJoyAngle()+18); |
Yamanobe | 0:37bb1a446e69 | 44 | TxAngleI2C(0x06, GetJoyAngle()+12); |
Yamanobe | 0:37bb1a446e69 | 45 | TxAngleI2C(0x14, GetJoyAngle()+18); |
Yamanobe | 0:37bb1a446e69 | 46 | TxAngleI2C(0x15, GetJoyAngle()+18); |
Yamanobe | 0:37bb1a446e69 | 47 | TxAngleI2C(0x19, GetJoyAngle()+6); |
Yamanobe | 0:37bb1a446e69 | 48 | TxAngleI2C(0x20, GetJoyAngle()+6); |
Yamanobe | 0:37bb1a446e69 | 49 | wait(0.01); |
Yamanobe | 0:37bb1a446e69 | 50 | } |
Yamanobe | 0:37bb1a446e69 | 51 | } |