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.
Dependents: 2019_SwitchNode_AR ArmNode 2919_LineSensor 2019_MD ... more
Mycan.cpp@7:cac42b73a4f6, 2019-09-02 (annotated)
- Committer:
- Kirua
- Date:
- Mon Sep 02 05:09:45 2019 +0000
- Revision:
- 7:cac42b73a4f6
- Parent:
- 6:9bf1c678d5a4
- Child:
- 8:6b148c4c8dfb
- Child:
- 9:32f2e17ba653
fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kirua | 0:ce8efac4c651 | 1 | #include "Mycan.h" |
Kirua | 0:ce8efac4c651 | 2 | |
Kirua | 4:7aa2809ad177 | 3 | |
Kirua | 4:7aa2809ad177 | 4 | /*----------------------------------------------------*/ |
Kirua | 4:7aa2809ad177 | 5 | |
Kirua | 4:7aa2809ad177 | 6 | //"id"は小さくしたほうがメモリ効率がいいですね |
Kirua | 4:7aa2809ad177 | 7 | //あと、整数で使うときは"num"は1~7、小数は0か1で御願いします |
Kirua | 4:7aa2809ad177 | 8 | |
Kirua | 4:7aa2809ad177 | 9 | /*----------------------------------------------------*/ |
Kirua | 4:7aa2809ad177 | 10 | |
Kirua | 4:7aa2809ad177 | 11 | Mycan::Mycan(PinName _rd, PinName _td, float freq) : can(_rd, _td) |
Kirua | 0:ce8efac4c651 | 12 | { |
Kirua | 4:7aa2809ad177 | 13 | can.frequency(freq); |
Kirua | 0:ce8efac4c651 | 14 | }; |
Kirua | 0:ce8efac4c651 | 15 | |
Kirua | 4:7aa2809ad177 | 16 | void Mycan::setI(uint32_t _id, int _num, int16_t _data) |
Kirua | 0:ce8efac4c651 | 17 | { |
Kirua | 4:7aa2809ad177 | 18 | write_type = 0; |
Kirua | 0:ce8efac4c651 | 19 | td_id = _id; |
Kirua | 0:ce8efac4c651 | 20 | td_num = _num; |
Kirua | 6:9bf1c678d5a4 | 21 | td_data.value[td_num] = _data; |
Kirua | 4:7aa2809ad177 | 22 | |
Kirua | 6:9bf1c678d5a4 | 23 | for (int i = 0; i < 7; i++) |
Kirua | 4:7aa2809ad177 | 24 | { |
Kirua | 6:9bf1c678d5a4 | 25 | if (td_data.value[i] >= 0) |
Kirua | 6:9bf1c678d5a4 | 26 | td_integer.value[7] &= ~(1 << i); |
Kirua | 4:7aa2809ad177 | 27 | else |
Kirua | 6:9bf1c678d5a4 | 28 | td_integer.value[7] |= (1 << i); |
Kirua | 6:9bf1c678d5a4 | 29 | td_integer.value[i] = abs(td_data.value[i]); |
Kirua | 4:7aa2809ad177 | 30 | } |
Kirua | 4:7aa2809ad177 | 31 | } |
Kirua | 4:7aa2809ad177 | 32 | |
Kirua | 4:7aa2809ad177 | 33 | void Mycan::setF(uint32_t _id, int _num, float _data) |
Kirua | 4:7aa2809ad177 | 34 | { |
Kirua | 4:7aa2809ad177 | 35 | write_type = 1; |
Kirua | 4:7aa2809ad177 | 36 | td_id = _id; |
Kirua | 4:7aa2809ad177 | 37 | td_num = _num; |
Kirua | 4:7aa2809ad177 | 38 | td_decimal.value[td_num] = _data; |
Kirua | 0:ce8efac4c651 | 39 | } |
Kirua | 0:ce8efac4c651 | 40 | |
TanakaTarou | 3:055c2d38132f | 41 | bool Mycan::send() |
Kirua | 0:ce8efac4c651 | 42 | { |
Kirua | 4:7aa2809ad177 | 43 | if (!write_type) return can.write(CANMessage(td_id, (char*)&td_integer, 8)); |
Kirua | 4:7aa2809ad177 | 44 | else if (write_type) return can.write(CANMessage(td_id, (char*)&td_decimal, 8)); |
Kirua | 0:ce8efac4c651 | 45 | } |
Kirua | 0:ce8efac4c651 | 46 | |
Kirua | 4:7aa2809ad177 | 47 | void Mycan::readI() |
Kirua | 0:ce8efac4c651 | 48 | { |
Kirua | 4:7aa2809ad177 | 49 | read_type = 0; |
Kirua | 0:ce8efac4c651 | 50 | CANMessage received; |
Kirua | 0:ce8efac4c651 | 51 | can.read(received); |
Kirua | 4:7aa2809ad177 | 52 | rd_integer = *(can_integer*)received.data; |
Kirua | 4:7aa2809ad177 | 53 | |
Kirua | 7:cac42b73a4f6 | 54 | for (int i = 0; i < 7; i++) { |
Kirua | 7:cac42b73a4f6 | 55 | if (rd_integer.value[7] & (1 << i)) |
Kirua | 7:cac42b73a4f6 | 56 | integer_storage.value[i] = rd_integer.value[i] * -1; |
Kirua | 7:cac42b73a4f6 | 57 | else integer_storage.value[i] = rd_integer.value[i]; |
Kirua | 7:cac42b73a4f6 | 58 | } |
Kirua | 6:9bf1c678d5a4 | 59 | integer_values_storage[received.id] = integer_storage; |
Kirua | 0:ce8efac4c651 | 60 | } |
Kirua | 0:ce8efac4c651 | 61 | |
Kirua | 4:7aa2809ad177 | 62 | void Mycan::readF() |
Kirua | 4:7aa2809ad177 | 63 | { |
Kirua | 4:7aa2809ad177 | 64 | read_type = 1; |
Kirua | 4:7aa2809ad177 | 65 | CANMessage received; |
Kirua | 4:7aa2809ad177 | 66 | can.read(received); |
Kirua | 4:7aa2809ad177 | 67 | rd_decimal = *(can_decimal *)received.data; |
Kirua | 4:7aa2809ad177 | 68 | |
Kirua | 4:7aa2809ad177 | 69 | decimal_values_storage[received.id].value[0] = rd_decimal.value[0]; |
Kirua | 4:7aa2809ad177 | 70 | decimal_values_storage[received.id].value[1] = rd_decimal.value[1]; |
Kirua | 4:7aa2809ad177 | 71 | } |
Kirua | 4:7aa2809ad177 | 72 | |
Kirua | 4:7aa2809ad177 | 73 | float Mycan::get(uint32_t _id, int _num) |
Kirua | 0:ce8efac4c651 | 74 | { |
Kirua | 0:ce8efac4c651 | 75 | rd_id = _id; |
Kirua | 0:ce8efac4c651 | 76 | rd_num = _num; |
Kirua | 4:7aa2809ad177 | 77 | if (!read_type) return integer_values_storage[rd_id].value[rd_num]; |
Kirua | 4:7aa2809ad177 | 78 | else if (read_type) return decimal_values_storage[rd_id].value[rd_num]; |
Kirua | 0:ce8efac4c651 | 79 | } |
Kirua | 0:ce8efac4c651 | 80 |