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
Diff: Mycan.cpp
- Revision:
- 11:10a25bb14793
- Parent:
- 9:32f2e17ba653
diff -r d6738616d5f9 -r 10a25bb14793 Mycan.cpp
--- a/Mycan.cpp Thu Sep 26 13:50:31 2019 +0000
+++ b/Mycan.cpp Tue Oct 01 14:14:10 2019 +0000
@@ -4,35 +4,37 @@
/*----------------------------------------------------*/
//"id"は小さくしたほうがメモリ効率がいいですね
-//あと、整数で使うときは"num"は1~7、小数は0か1で御願いします
+//あと、整数で使うときは"num"は0~6、小数は0か1で御願いします
/*----------------------------------------------------*/
-Mycan::Mycan(PinName _rd, PinName _td, float freq) : can(_rd, _td)
+Mycan::Mycan(PinName _rd, PinName _td, int freq) : can(_rd, _td, freq) {};
+
+void Mycan::setI(uint32_t _id, uint8_t _num, int16_t _data)
{
- can.frequency(freq);
-};
-
-void Mycan::setI(uint32_t _id, int _num, int16_t _data)
-{
- write_type = 0;
+ if (td_id != _id) {
+ for (int i = 0; i <= 6; i++)
+ td_integer.value[i] = 0;
+ }
+ write_type = INTEGER;
td_id = _id;
td_num = _num;
td_data.value[td_num] = _data;
- for (int i = 0; i < 7; i++)
- {
- if (td_data.value[i] >= 0)
- td_integer.value[7] &= ~(1 << i);
- else
- td_integer.value[7] |= (1 << i);
- td_integer.value[i] = abs(td_data.value[i]);
- }
+ if (td_data.value[td_num] >= 0)
+ td_integer.value[7] &= ~(1 << td_num);
+ else
+ td_integer.value[7] |= (1 << td_num);
+ td_integer.value[td_num] = static_cast<char>(abs(td_data.value[td_num]));
}
-void Mycan::setF(uint32_t _id, int _num, float _data)
+void Mycan::setF(uint32_t _id, uint8_t _num, float _data)
{
- write_type = 1;
+ if (td_id != _id) {
+ for (int i = 0; i <= 1; i++)
+ td_integer.value[i] = 0;
+ }
+ write_type = DECIMAL;
td_id = _id;
td_num = _num;
td_decimal.value[td_num] = _data;
@@ -44,11 +46,11 @@
else if (write_type) return can.write(CANMessage(td_id, (char*)&td_decimal, 8));
}
-void Mycan::readI()
+bool Mycan::readI()
{
- read_type = 0;
+ read_type = INTEGER;
CANMessage received;
- can.read(received);
+ bool success = can.read(received);
rd_integer = *(can_integer*)received.data;
for (int i = 0; i < 7; i++) {
@@ -57,19 +59,23 @@
else integer_storage.value[i] = rd_integer.value[i];
}
integer_values_storage[received.id] = integer_storage;
+
+ return success;
}
-void Mycan::readF()
+bool Mycan::readF()
{
- read_type = 1;
+ read_type = DECIMAL;
CANMessage received;
- can.read(received);
+ bool success = can.read(received);
rd_decimal = *(can_decimal *)received.data;
decimal_values_storage[received.id] = rd_decimal;
+
+ return success;
}
-float Mycan::get(uint32_t _id, int _num)
+float Mycan::get(uint32_t _id, uint8_t _num)
{
rd_id = _id;
rd_num = _num;