MDテスト用プログラム
Dependencies: FastPWM GMD_template_lib mbed
Fork of NewMD2 by
main.cpp@4:894559ccce05, 2016-07-03 (annotated)
- Committer:
- inst
- Date:
- Sun Jul 03 02:33:31 2016 +0000
- Revision:
- 4:894559ccce05
- Parent:
- 1:29e84d1e7bcc
- Child:
- 5:9d1b1948a9b6
I2C address ???????; 00XXXX0; ??; XXXX000; ????;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 0:690d8ed9ee20 | 1 | #include "mbed.h" |
inst | 0:690d8ed9ee20 | 2 | #include "GMD.hpp" |
inst | 0:690d8ed9ee20 | 3 | |
inst | 0:690d8ed9ee20 | 4 | char read_address() { |
inst | 0:690d8ed9ee20 | 5 | const size_t din_num = 4; |
inst | 0:690d8ed9ee20 | 6 | DigitalIn din[] = { |
inst | 0:690d8ed9ee20 | 7 | DigitalIn(dp26), |
inst | 0:690d8ed9ee20 | 8 | DigitalIn(dp25), |
inst | 0:690d8ed9ee20 | 9 | DigitalIn(dp18), |
inst | 0:690d8ed9ee20 | 10 | DigitalIn(dp17) |
inst | 0:690d8ed9ee20 | 11 | }; |
inst | 0:690d8ed9ee20 | 12 | |
inst | 0:690d8ed9ee20 | 13 | char address = 0; |
inst | 4:894559ccce05 | 14 | size_t address_offset = 4; |
inst | 0:690d8ed9ee20 | 15 | for (size_t i = 0; i < din_num; ++i) { |
inst | 4:894559ccce05 | 16 | address |= din[i].read() << (i + address_offset); |
inst | 0:690d8ed9ee20 | 17 | } |
inst | 0:690d8ed9ee20 | 18 | return address; |
inst | 0:690d8ed9ee20 | 19 | } |
inst | 0:690d8ed9ee20 | 20 | |
inst | 0:690d8ed9ee20 | 21 | int main(){ |
inst | 0:690d8ed9ee20 | 22 | I2CSlave i2c(dp5, dp27); |
inst | 0:690d8ed9ee20 | 23 | i2c.address(read_address()); |
inst | 1:29e84d1e7bcc | 24 | basic_motor_driver md(dp1, dp2, dp9); |
inst | 0:690d8ed9ee20 | 25 | |
inst | 0:690d8ed9ee20 | 26 | while (true){ |
inst | 0:690d8ed9ee20 | 27 | char buf; |
inst | 0:690d8ed9ee20 | 28 | switch (i2c.receive()){ |
inst | 0:690d8ed9ee20 | 29 | case I2CSlave::ReadAddressed: |
inst | 0:690d8ed9ee20 | 30 | break; |
inst | 0:690d8ed9ee20 | 31 | |
inst | 0:690d8ed9ee20 | 32 | case I2CSlave::WriteGeneral: |
inst | 0:690d8ed9ee20 | 33 | break; |
inst | 0:690d8ed9ee20 | 34 | |
inst | 0:690d8ed9ee20 | 35 | case I2CSlave::WriteAddressed: |
inst | 0:690d8ed9ee20 | 36 | i2c.read(&buf, 1); |
inst | 0:690d8ed9ee20 | 37 | |
inst | 1:29e84d1e7bcc | 38 | float p = (buf & 0x80) ? -1.0f : 1.0f; |
inst | 0:690d8ed9ee20 | 39 | p *= (buf & 0x7F); |
inst | 0:690d8ed9ee20 | 40 | p *= 1.0f / 0x7F; |
inst | 0:690d8ed9ee20 | 41 | |
inst | 0:690d8ed9ee20 | 42 | md.set(p); |
inst | 0:690d8ed9ee20 | 43 | break; |
inst | 0:690d8ed9ee20 | 44 | |
inst | 0:690d8ed9ee20 | 45 | case I2CSlave::NoData: |
inst | 0:690d8ed9ee20 | 46 | break; |
inst | 0:690d8ed9ee20 | 47 | } |
inst | 0:690d8ed9ee20 | 48 | } |
inst | 0:690d8ed9ee20 | 49 | } |