From 124d97baa3e2e43b4469ff49f792f7d04bfe32c8 Mon Sep 17 00:00:00 2001 From: hanj-kim Date: Thu, 15 Nov 2018 16:46:27 +0900 Subject: [PATCH] #initial version upload - version 0.0.1 Signed-off-by: tuna --- .gitignore | 15 + .../01.Network/01.Network.ino | 50 + .../02.Environment/02.Environment.ino | 41 + 01.MODI Module example/03.Gyro/03.Gyro.ino | 53 + 01.MODI Module example/04.Mic/04.Mic.ino | 30 + .../05.Button/05.Button.ino | 57 + 01.MODI Module example/06.Dial/06.Dial.ino | 30 + .../07.Ultrasonic/07.Ultrasonic.ino | 27 + .../08.Infrared/08.Infrared.ino | 27 + .../09.Display/09.Display.ino | 53 + 01.MODI Module example/10.Motor/10.Motor.ino | 74 + 01.MODI Module example/11.LED/11.LED.ino | 43 + .../12.Speaker/12.Speaker.ino | 34 + .../01.InUseSign/01.InUseSign.ino | 39 + .../02.CableCar/02.CableCar.ino | 34 + .../03.MovingMouse/03.MovingMouse.ino | 40 + .../04.RotatingPencilStand.ino | 40 + .../05.RouletteWheel/05.RouletteWheel.ino | 38 + .../06.CrocodileDentist.ino | 45 + .../07.RemoteCar/07.RemoteCar.ino | 42 + .../08.SmartSwitch/08.SmartSwitch.ino | 43 + .../09.MoodLamp/09.MoodLamp.ino | 52 + README.md | 52 + keywords.txt | 145 ++ library.properties | 10 + src/MODI.cpp | 1879 +++++++++++++++++ src/MODI.h | 410 ++++ src/MODI_Interface.cpp | 177 ++ src/MODI_Interface.h | 42 + src/MODI_property.h | 184 ++ src/mcp_can.cpp | 1291 +++++++++++ src/mcp_can.h | 133 ++ src/mcp_can_dfs.h | 476 +++++ src/types.h | 193 ++ 34 files changed, 5899 insertions(+) create mode 100644 .gitignore create mode 100644 01.MODI Module example/01.Network/01.Network.ino create mode 100644 01.MODI Module example/02.Environment/02.Environment.ino create mode 100644 01.MODI Module example/03.Gyro/03.Gyro.ino create mode 100644 01.MODI Module example/04.Mic/04.Mic.ino create mode 100644 01.MODI Module example/05.Button/05.Button.ino create mode 100644 01.MODI Module example/06.Dial/06.Dial.ino create mode 100644 01.MODI Module example/07.Ultrasonic/07.Ultrasonic.ino create mode 100644 01.MODI Module example/08.Infrared/08.Infrared.ino create mode 100644 01.MODI Module example/09.Display/09.Display.ino create mode 100644 01.MODI Module example/10.Motor/10.Motor.ino create mode 100644 01.MODI Module example/11.LED/11.LED.ino create mode 100644 01.MODI Module example/12.Speaker/12.Speaker.ino create mode 100644 03.MODI Creation example/01.InUseSign/01.InUseSign.ino create mode 100644 03.MODI Creation example/02.CableCar/02.CableCar.ino create mode 100644 03.MODI Creation example/03.MovingMouse/03.MovingMouse.ino create mode 100644 03.MODI Creation example/04.RotatingPencilStand/04.RotatingPencilStand.ino create mode 100644 03.MODI Creation example/05.RouletteWheel/05.RouletteWheel.ino create mode 100644 03.MODI Creation example/06.CrocodileDentist/06.CrocodileDentist.ino create mode 100644 03.MODI Creation example/07.RemoteCar/07.RemoteCar.ino create mode 100644 03.MODI Creation example/08.SmartSwitch/08.SmartSwitch.ino create mode 100644 03.MODI Creation example/09.MoodLamp/09.MoodLamp.ino create mode 100644 README.md create mode 100644 keywords.txt create mode 100644 library.properties create mode 100644 src/MODI.cpp create mode 100644 src/MODI.h create mode 100644 src/MODI_Interface.cpp create mode 100644 src/MODI_Interface.h create mode 100644 src/MODI_property.h create mode 100644 src/mcp_can.cpp create mode 100644 src/mcp_can.h create mode 100644 src/mcp_can_dfs.h create mode 100644 src/types.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34461a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +#JSON +*.json + +#pdf + +*.pdf + + +# End of https://www.gitignore.io/api/c,c++,visualstudiocode diff --git a/01.MODI Module example/01.Network/01.Network.ino b/01.MODI Module example/01.Network/01.Network.ino new file mode 100644 index 0000000..4221c4c --- /dev/null +++ b/01.MODI Module example/01.Network/01.Network.ino @@ -0,0 +1,50 @@ +#include + +const unsigned int NETWORK_ID = 0xF9; // please modify id to your module's id + +/* Network module */ +Network network1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + network1.begin(NETWORK_ID); + + /* add request */ + network1.addRequest(2); + network1.addRequest(3); +} + +void loop() { + // put your main code here, to run repeatedly: + if(network1.readEvent() == 1) + { + Serial.println("Button Pressed"); + network1.writeEvent(1); // buzzer on + } + else if(network1.readEvent() == 2) // joystick up + { + Serial.println("Joystick Up Pressed"); + } + else if(network1.readEvent() == 3) // joystick down + { + Serial.println("Joystick Down Pressed"); + } + else if(network1.readEvent() == 4) // joystick right + { + Serial.println("Joystick Right Pressed"); + } + else if(network1.readEvent() == 5) // joystick left + { + Serial.println("Joystick Left Pressed"); + } + else + { + network1.writeEvent(0); // buzzer off + } + delay(100); +} \ No newline at end of file diff --git a/01.MODI Module example/02.Environment/02.Environment.ino b/01.MODI Module example/02.Environment/02.Environment.ino new file mode 100644 index 0000000..f6562e4 --- /dev/null +++ b/01.MODI Module example/02.Environment/02.Environment.ino @@ -0,0 +1,41 @@ +#include + +const unsigned int ENV_ID = 0x5A6; // please modify id to your module's id + +/* Environment module */ +Environment env1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + env1.begin(ENV_ID); + + /* add request */ + env1.addRequest(2); + env1.addRequest(3); + env1.addRequest(4); + env1.addRequest(5); + env1.addRequest(6); + env1.addRequest(7); +} + +void loop() { + Serial.println("==============="); + Serial.print("Illuminance"); + Serial.println(env1.readIlluminance()); + Serial.print("red"); + Serial.println(env1.readRed()); + Serial.print("green"); + Serial.println(env1.readGreen()); + Serial.print("blue"); + Serial.println(env1.readBlue()); + Serial.print("temp"); + Serial.println(env1.readTemperature()); + Serial.print("humidity"); + Serial.println(env1.readHumidity()); + delay(100); +} \ No newline at end of file diff --git a/01.MODI Module example/03.Gyro/03.Gyro.ino b/01.MODI Module example/03.Gyro/03.Gyro.ino new file mode 100644 index 0000000..9a1abaa --- /dev/null +++ b/01.MODI Module example/03.Gyro/03.Gyro.ino @@ -0,0 +1,53 @@ +#include + +const unsigned int GYRO_ID = 0xE51; // please modify id to your module's id + +/* Gyro module */ +Gyro gyro1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + gyro1.begin(GYRO_ID); + + /* add request */ + gyro1.addRequest(2); + gyro1.addRequest(3); + gyro1.addRequest(4); + gyro1.addRequest(5); + gyro1.addRequest(6); + gyro1.addRequest(7); + gyro1.addRequest(8); + gyro1.addRequest(9); + gyro1.addRequest(10); + gyro1.addRequest(11); +} + +void loop() { + Serial.println("==============="); + Serial.print("Roll:"); + Serial.println(gyro1.readRoll()); + Serial.print("Pitch:"); + Serial.println(gyro1.readPitch()); + Serial.print("Yaw:"); + Serial.println(gyro1.readYaw()); + Serial.print("Gyro_X axis:"); + Serial.println(gyro1.readGyro_X()); + Serial.print("Gyro_Y axis:"); + Serial.println(gyro1.readGyro_Y()); + Serial.print("Gyro_Z axis:"); + Serial.println(gyro1.readGyro_Z()); + Serial.print("Accel_X axis:"); + Serial.println(gyro1.readAccel_X()); + Serial.print("Accel_Y axis:"); + Serial.println(gyro1.readAccel_Y()); + Serial.print("Accel_Z axis:"); + Serial.println(gyro1.readAccel_Z()); + Serial.print("Vibration:"); + Serial.println(gyro1.readVibration()); + delay(100); +} \ No newline at end of file diff --git a/01.MODI Module example/04.Mic/04.Mic.ino b/01.MODI Module example/04.Mic/04.Mic.ino new file mode 100644 index 0000000..84ca3b9 --- /dev/null +++ b/01.MODI Module example/04.Mic/04.Mic.ino @@ -0,0 +1,30 @@ +#include + +const unsigned int MIC_ID = 0xCEE; // please modify id to your module's id + +/* Mic module */ +Mic mic1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + mic1.begin(MIC_ID); + + /* add request */ + mic1.addRequest(2); + mic1.addRequest(3); + +} + +void loop() { + Serial.println("==============="); + Serial.print("Volume:"); + Serial.println(mic1.readVolume()); + Serial.print("Freq:"); + Serial.println(mic1.readFreq()); + delay(100); +} \ No newline at end of file diff --git a/01.MODI Module example/05.Button/05.Button.ino b/01.MODI Module example/05.Button/05.Button.ino new file mode 100644 index 0000000..47da630 --- /dev/null +++ b/01.MODI Module example/05.Button/05.Button.ino @@ -0,0 +1,57 @@ +#include + +const unsigned int BUTTON_ID = 0xF55; // please modify id to your module's id + +/* Button module */ +Button button1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + button1.begin(BUTTON_ID); + + /* add request */ + button1.addRequest(2); + button1.addRequest(3); + button1.addRequest(4); + button1.addRequest(5); + +} + +void loop() { + Serial.println("==============="); + + if(button1.readClick() > 1.0) + { + Serial.println("Button is clicked"); + } + else + { + Serial.println("Button is unclicked"); + } + if(button1.readDoubleClick() > 1.0) + { + Serial.println("Button is double clicked"); + } + else + { + Serial.println("Button is not double clicked"); + } + if(button1.readPushState() > 1.0) + { + Serial.println("Button is pressed"); + } + else + { + Serial.println("Button is unpressed"); + } + + Serial.println("Button is toggle State:"); + Serial.println(button1.readToggle()); + + delay(100); +} \ No newline at end of file diff --git a/01.MODI Module example/06.Dial/06.Dial.ino b/01.MODI Module example/06.Dial/06.Dial.ino new file mode 100644 index 0000000..5eb0083 --- /dev/null +++ b/01.MODI Module example/06.Dial/06.Dial.ino @@ -0,0 +1,30 @@ +#include + +const unsigned int DIAL_ID = 0xA18; // please modify id to your module's id + +/* Dial module */ +Dial dial1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + dial1.begin(DIAL_ID); + + /* add request */ + dial1.addRequest(2); + dial1.addRequest(3); +} + +void loop() { + Serial.println("==============="); + + Serial.print("Dial :"); + Serial.println(dial1.readDegree()); + Serial.print("Dial Turn Speed :"); + Serial.println(dial1.readTurnSpeed()); + delay(100); +} \ No newline at end of file diff --git a/01.MODI Module example/07.Ultrasonic/07.Ultrasonic.ino b/01.MODI Module example/07.Ultrasonic/07.Ultrasonic.ino new file mode 100644 index 0000000..0204977 --- /dev/null +++ b/01.MODI Module example/07.Ultrasonic/07.Ultrasonic.ino @@ -0,0 +1,27 @@ +#include + +const unsigned int ULTRA_ID = 0x577; // please modify id to your module's id + +/* Ultrasonic module */ +Ultrasonic ultra1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + ultra1.begin(ULTRA_ID); + + /* add request */ + ultra1.addRequest(2); +} + +void loop() { + Serial.println("==============="); + + Serial.print("Dial :"); + Serial.println(ultra1.readDistance()); + delay(100); +} \ No newline at end of file diff --git a/01.MODI Module example/08.Infrared/08.Infrared.ino b/01.MODI Module example/08.Infrared/08.Infrared.ino new file mode 100644 index 0000000..fee2489 --- /dev/null +++ b/01.MODI Module example/08.Infrared/08.Infrared.ino @@ -0,0 +1,27 @@ +#include + +const unsigned int IR_ID = 0xE; // please modify id to your module's id + +/* Infrared module */ +Ir ir1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + ir1.begin(IR_ID); + + /* add request */ + ir1.addRequest(2); +} + +void loop() { + Serial.println("==============="); + + Serial.print("Proximity :"); + Serial.println(ir1.readProximity()); + delay(100); +} \ No newline at end of file diff --git a/01.MODI Module example/09.Display/09.Display.ino b/01.MODI Module example/09.Display/09.Display.ino new file mode 100644 index 0000000..f49e3f1 --- /dev/null +++ b/01.MODI Module example/09.Display/09.Display.ino @@ -0,0 +1,53 @@ +#include + +const unsigned int DISPLAY_ID = 0x3F4; + +const unsigned char image [] = { +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0xFF, 0xFF, 0x00, 0x7E, 0x00, 0xFF, 0x80, 0x3E, 0xFF, 0xFF, 0x01, 0xFF, 0x80, 0xFF, 0xE0, 0x3E, +0xFF, 0xFF, 0x03, 0xFF, 0xC0, 0xFF, 0xF0, 0x3E, 0xFF, 0xFF, 0x07, 0xFF, 0xE0, 0xFF, 0xF8, 0x3E, +0xFF, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0xFC, 0x3E, 0xFF, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0xFC, 0x3E, +0xFF, 0xFF, 0x0F, 0xFF, 0xF8, 0xFF, 0xFE, 0x3E, 0xFF, 0xFF, 0x0F, 0xFF, 0xF8, 0xFF, 0xFE, 0x3E, +0xFF, 0xFF, 0x0F, 0xFF, 0xF8, 0xFF, 0xFE, 0x3E, 0xFF, 0xFF, 0x0F, 0xFF, 0xF8, 0xFF, 0xFE, 0x3E, +0xFF, 0xFF, 0x0F, 0xFF, 0xF8, 0xFF, 0xFE, 0x3E, 0xF3, 0xDF, 0x0F, 0xFF, 0xF0, 0xFF, 0xFC, 0x3E, +0xF1, 0x9F, 0x0F, 0xFF, 0xF0, 0xFF, 0xFC, 0x3E, 0xF0, 0x1F, 0x07, 0xFF, 0xE0, 0xFF, 0xF8, 0x3E, +0xF0, 0x1F, 0x03, 0xFF, 0xC0, 0xFF, 0xF0, 0x3E, 0xF0, 0x1F, 0x01, 0xFF, 0x80, 0xFF, 0xE0, 0x3E, +0xF0, 0x1F, 0x00, 0xFF, 0x00, 0xFF, 0x80, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + + + +/* Display module */ +Display display1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + display1.begin(DISPLAY_ID); + delay(1000); + display1.writeDots(image); +} + +void loop() { + + + +} \ No newline at end of file diff --git a/01.MODI Module example/10.Motor/10.Motor.ino b/01.MODI Module example/10.Motor/10.Motor.ino new file mode 100644 index 0000000..c3e76e9 --- /dev/null +++ b/01.MODI Module example/10.Motor/10.Motor.ino @@ -0,0 +1,74 @@ +#include + +const unsigned int MOTOR_ID = 0xB5; +; // please modify id to your module's id + +/* Motor module */ +Motor motor1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + motor1.begin(MOTOR_ID); + + /* add request */ + motor1.addRequest(2); + motor1.addRequest(3); + motor1.addRequest(4); + motor1.addRequest(5); + motor1.addRequest(6); + motor1.addRequest(7); + motor1.addRequest(8); + motor1.addRequest(9); + motor1.addRequest(10); + motor1.addRequest(11); + motor1.addRequest(12); + motor1.addRequest(13); + motor1.addRequest(14); +} + +void loop() { + Serial.println("==============="); + Serial.print("Upper Motor Torque:"); + Serial.println(motor1.readTorque_Upper()); + Serial.print("Bottom Motor Torque:"); + Serial.println(motor1.readTorque_Bottom()); + Serial.print("Upper Motor Speed:"); + Serial.println(motor1.readSpeed_Upper()); + Serial.print("Bottom Motor Speed:"); + Serial.println(motor1.readSpeed_Bottom()); + Serial.print("Upper Motor Angle:"); + Serial.println(motor1.readAngle_Upper()); + Serial.print("Bottom Motor Angle:"); + Serial.println(motor1.readAngle_Bottom()); + Serial.print("Upper Motor Sense Speed:"); + Serial.println(motor1.readSenseSpeed_Upper()); + Serial.print("Bottom Motor Sense Speed:"); + Serial.println(motor1.readSenseSpeed_Bottom()); + Serial.print("Upper Motor Sense Angle:"); + Serial.println(motor1.readSenseAngle_Upper()); + Serial.print("Bottom Motor Sense Angle:"); + Serial.println(motor1.readSenseAngle_Bottom()); + + + /* Speed */ + motor1.writeSpeed(75,20); + //motor1.writeSpeed_Upper(100); + //motor1.writeSpeed_Bottom(100); + + /* Torque */ + //motor1.writeTorque(60,-95); + //motor1.writeTorque_Upper(25); + //motor1.writeTorque_Bottom(30); + + /* Angle */ + //motor1.writeAngle(50,35); + // motor1.writeAngle_Upper(60); + // motor1.writeAngle_Bottom(0); + + delay(500); +} \ No newline at end of file diff --git a/01.MODI Module example/11.LED/11.LED.ino b/01.MODI Module example/11.LED/11.LED.ino new file mode 100644 index 0000000..7e3669c --- /dev/null +++ b/01.MODI Module example/11.LED/11.LED.ino @@ -0,0 +1,43 @@ +#include + +const unsigned int LED_ID = 0xB73; // please modify id to your module's id + +/* LED module */ +LED led1; + +uint8_t R =random(random(0,101)); +uint8_t G =random(random(0,101)); +uint8_t B =random(random(0,101)); + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + led1.begin(LED_ID); + + /* add request */ + led1.addRequest(2); + led1.addRequest(3); + led1.addRequest(4); +} + +void loop() { + Serial.println("==============="); + + Serial.print("R :"); + Serial.println(led1.readRed()); + Serial.print("G :"); + Serial.println(led1.readGreen()); + Serial.print("B :"); + Serial.println(led1.readBlue()); + delay(1000); + + R =random(random(0,101)); + G =random(random(0,101)); + B =random(random(0,101)); + + led1.writeRGB(R,G,B); +} \ No newline at end of file diff --git a/01.MODI Module example/12.Speaker/12.Speaker.ino b/01.MODI Module example/12.Speaker/12.Speaker.ino new file mode 100644 index 0000000..a165d83 --- /dev/null +++ b/01.MODI Module example/12.Speaker/12.Speaker.ino @@ -0,0 +1,34 @@ +#include + +const unsigned int SPEAKER_ID = 0xB36; // please modify id to your module's id + +/* Speaker module */ +Speaker speaker1; + +uint8_t vol = 0; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + speaker1.begin(SPEAKER_ID); + + /* add request */ + speaker1.addRequest(2); + speaker1.addRequest(3); +} + +void loop() { + Serial.println("==============="); + speaker1.readVolume(); + speaker1.readFreq(); + + speaker1.writeTune(F_MI_6,vol); + + vol = (vol+1)%101; + Serial.println(vol); + delay(100); +} \ No newline at end of file diff --git a/03.MODI Creation example/01.InUseSign/01.InUseSign.ino b/03.MODI Creation example/01.InUseSign/01.InUseSign.ino new file mode 100644 index 0000000..dda43ff --- /dev/null +++ b/03.MODI Creation example/01.InUseSign/01.InUseSign.ino @@ -0,0 +1,39 @@ +#include + +const unsigned int DIAL_ID = 0xA18; // please modify id to your module's id +const unsigned int LED_ID = 0xB73; + +/* module */ +Button button1; +Dial dial1; +LED led1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + dial1.begin(DIAL_ID); + led1.begin(LED_ID); + + /* add request */ + dial1.addRequest(2); +} + +void loop() { + Serial.print("Degree:"); + Serial.println(dial1.readDegree()); + if(dial1.readDegree() <= 50) + { + Serial.println("UNUSE"); + led1.writeRGB(0,100,0); + } + else + { + Serial.println("USE"); + led1.writeRGB(100,0,0); + } + delay(200); +} \ No newline at end of file diff --git a/03.MODI Creation example/02.CableCar/02.CableCar.ino b/03.MODI Creation example/02.CableCar/02.CableCar.ino new file mode 100644 index 0000000..f083183 --- /dev/null +++ b/03.MODI Creation example/02.CableCar/02.CableCar.ino @@ -0,0 +1,34 @@ +#include + +const unsigned int BUTTON_ID = 0xF55; // please modify id to your module's id +const unsigned int MOTOR_ID = 0xB5; + +/* module */ +Button button1; +Motor motor1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + button1.begin(BUTTON_ID); + motor1.begin(MOTOR_ID); + + /* add request */ + button1.addRequest(5); +} + +void loop() { + if(button1.readToggle() >= 10) + { + motor1.writeSpeed(30,30); + } + else + { + motor1.writeSpeed(0,0); + } + delay(200); +} \ No newline at end of file diff --git a/03.MODI Creation example/03.MovingMouse/03.MovingMouse.ino b/03.MODI Creation example/03.MovingMouse/03.MovingMouse.ino new file mode 100644 index 0000000..527c3f7 --- /dev/null +++ b/03.MODI Creation example/03.MovingMouse/03.MovingMouse.ino @@ -0,0 +1,40 @@ +#include + +const unsigned int BUTTON_ID = 0xF55; // please modify id to your module's id +const unsigned int MOTOR_ID = 0xB5; + +/* module */ +Button button1; +Motor motor1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + button1.begin(BUTTON_ID); + motor1.begin(MOTOR_ID); + + /* add request */ + button1.addRequest(5); +} + +void loop() { + Serial.print("Toggle:"); + Serial.println(button1.readToggle()); + + if(button1.readToggle() >= 10) + { + motor1.writeSpeed(50,0); + delay(300); + motor1.writeSpeed(0,-50); + delay(300); + } + else + { + motor1.writeSpeed(0,0); + delay(100); + } +} \ No newline at end of file diff --git a/03.MODI Creation example/04.RotatingPencilStand/04.RotatingPencilStand.ino b/03.MODI Creation example/04.RotatingPencilStand/04.RotatingPencilStand.ino new file mode 100644 index 0000000..5dafd62 --- /dev/null +++ b/03.MODI Creation example/04.RotatingPencilStand/04.RotatingPencilStand.ino @@ -0,0 +1,40 @@ +#include + +const unsigned int BUTTON_ID = 0xF55; // please modify id to your module's id +const unsigned int MOTOR_ID = 0xB5; +const unsigned int LED_ID = 0xB73; + +/* module */ +Button button1; +Motor motor1; +LED led1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + button1.begin(BUTTON_ID); + motor1.begin(MOTOR_ID); + led1.begin(LED_ID); + + /* add request */ + button1.addRequest(4); +} + +void loop() { + Serial.print("PushState:"); + Serial.println(button1.readPushState()); + motor1.writeTorque(0,0); + + while(button1.readPushState() >= 10) + { + motor1.writeTorque(20,0); + led1.writeRGB(0,100,0); + delay(200); + led1.writeRGB(0,0,0); + delay(200); + } +} \ No newline at end of file diff --git a/03.MODI Creation example/05.RouletteWheel/05.RouletteWheel.ino b/03.MODI Creation example/05.RouletteWheel/05.RouletteWheel.ino new file mode 100644 index 0000000..d8af755 --- /dev/null +++ b/03.MODI Creation example/05.RouletteWheel/05.RouletteWheel.ino @@ -0,0 +1,38 @@ +#include + +const unsigned int BUTTON_ID = 0xF55; // please modify id to your module's id +const unsigned int MOTOR_ID = 0xB5; +int random0 = 0; +/* module */ +Button button1; +Motor motor1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + button1.begin(BUTTON_ID); + motor1.begin(MOTOR_ID); + + /* add request */ + button1.addRequest(2); +} + +void loop() { + Serial.print("ClickState:"); + Serial.println(button1.readClick()); + + if(button1.readClick() >= 10) + { + random0 = random(2000,5000+1); + + motor1.writeTorque(100,0); + Serial.print("Random Time:"); + Serial.println(randome0); + delay(random0); + motor1.writeTorque(0,0); + } +} \ No newline at end of file diff --git a/03.MODI Creation example/06.CrocodileDentist/06.CrocodileDentist.ino b/03.MODI Creation example/06.CrocodileDentist/06.CrocodileDentist.ino new file mode 100644 index 0000000..a76ed26 --- /dev/null +++ b/03.MODI Creation example/06.CrocodileDentist/06.CrocodileDentist.ino @@ -0,0 +1,45 @@ +#include + +const unsigned int BUTTON_ID = 0xF55; // please modify id to your module's id +const unsigned int MOTOR_ID = 0xB5; +/* module */ +Button button1; +Motor motor1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + button1.begin(BUTTON_ID); + motor1.begin(MOTOR_ID); + + /* add request */ + button1.addRequest(2); +} + +void loop() { + Serial.print("ClickState:"); + Serial.println(button1.readClick()); + + motor1.writeAngle(50,50); + if(button1.readClick() >= 10) + { + int random0 = random(1,3+1); + + if(random0 == 1) + { + Serial.println("Bite!!"); + motor1.writeAngle(40,60); + delay(2000); + } + else + { + motor1.writeAngle(60,40); + delay(500); + } + } + delay(100); +} \ No newline at end of file diff --git a/03.MODI Creation example/07.RemoteCar/07.RemoteCar.ino b/03.MODI Creation example/07.RemoteCar/07.RemoteCar.ino new file mode 100644 index 0000000..a188ed6 --- /dev/null +++ b/03.MODI Creation example/07.RemoteCar/07.RemoteCar.ino @@ -0,0 +1,42 @@ +#include + +const unsigned int MOTOR_ID = 0xB5; // please modify id to your module's id +const unsigned int NETWORK_ID = 0xF9; + +/* module */ +Motor motor1; +Network network1; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + motor1.begin(MOTOR_ID); + network1.begin(NETWORK_ID); + /* add request */ + network1.addRequest(2); +} + +void loop() { + motor1.writeSpeed(0,0); + while(network1.readEvent == 2) // up + { + motor1.writeSpeed(100,-100); + } + while(network1.readEvent == 3) // down + { + motor1.writeSpeed(-100,100); + } + while(network1.readEvent == 4) // left + { + motor1.writeSpeed(50,25); + } + while(network1.readEvent == 5) // right + { + motor1.writeSpeed(-25,-50); + } + delay(100); +} \ No newline at end of file diff --git a/03.MODI Creation example/08.SmartSwitch/08.SmartSwitch.ino b/03.MODI Creation example/08.SmartSwitch/08.SmartSwitch.ino new file mode 100644 index 0000000..a45665f --- /dev/null +++ b/03.MODI Creation example/08.SmartSwitch/08.SmartSwitch.ino @@ -0,0 +1,43 @@ +#include + +const unsigned int MOTOR_ID = 0xB5; // please modify id to your module's id +const unsigned int NETWORK_ID = 0xF9; +/* module */ +Motor motor1; +Network network1; + +int mode = 0; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + motor1.begin(MOTOR_ID); + network1.begin(NETWORK_ID); + /* add request */ + network1.addRequest(2); +} + +void loop() { + Serial.print("Event:"); + Serial.println(network1.readEvent()); + motor1.writeSpeed(0,0); + if(network1.readEvent() == 1) // button pressed + { + if(mode == 0) + { + mode = 1; + motor1.writeAngle(0,0); + } + else if(mode == 1) + { + mode = 0; + motor1.writeAngle(75,0); + } + delay(500); + } + delay(100); +} \ No newline at end of file diff --git a/03.MODI Creation example/09.MoodLamp/09.MoodLamp.ino b/03.MODI Creation example/09.MoodLamp/09.MoodLamp.ino new file mode 100644 index 0000000..1d971ed --- /dev/null +++ b/03.MODI Creation example/09.MoodLamp/09.MoodLamp.ino @@ -0,0 +1,52 @@ +#include + +const unsigned int NETWORK_ID = 0xF9; // please modify id to your module's id +const unsigned int LED_ID = 0xB72; + +/* module */ +Network network1; +LED led1; + +int mode = 0; + +void setup() { + // put your setup code here, to run once: + delay(1000); + Serial.begin(115200); + Serial.println("welcome to MODI"); + MODI.begin(); + + network1.begin(NETWORK_ID); + led1.begin(LED_ID); + + /* add request */ + network1.addRequest(2); +} + +void loop() { + Serial.print("Event:"); + Serial.println(network1.readEvent()); + if(network1.readEvent() == 1) // button pressed + { + mode = mode + 1; + if(mode == 0) + { + led1.writeRGB(20,20,20); + } + else if(mode == 1) + { + led1.writeRGB(60,60,60); + } + else if(mode == 2) + { + led1.writeRGB(100,100,100); + } + else if(mode == 4) + { + led1.writeRGB(0,0,0); + mode = 0; + } + delay(500); + } + delay(100); +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b1891eb --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +# Luxrobo MODI Library for Arduino + +## Currently supported hardware +----------------- +* Microchips ATmel SAMD Based baords + * [Arduino MKR Wifi 1010](https://store.arduino.cc/arduino-mkr-wifi-1010) +* Microchips ATmel AVR Based boards + * [Arduino UNO R3](https://store.arduino.cc/arduino-uno-rev3) + +## Prerequisities +* MODI Kits +* Arduino +## Sample Instructions +--------------- +[MODI Instruction Guide](https://) + + +## Module Instruction + +Tested on Arduino 1.8.7 + +### Installation on Arduino + +Use the Arduino Library Manager to install and keep it updated. Just look for MODI. Recommend for Arduino 1.8+ + +### Manual installation on Arduino + +To install this library, just place this entire folder as a subfolder in your Arduino installation + +When installed, this library should look like + +| location | label | +| :---------------------------------------------: | :----------------------------------: | +| `Arduino\libraries\MODI` | (this library's folder) | +| `Arduino\libraries\MODI\src\MODI.cpp` | (the library implementation file) | +| `Arduino\libraries\MODI\src\MODI.h` | (the library header file) | +| `Arduino\libraries\MODI\src\MODI_Interface.cpp` | (the library interface file) | +| `Arduino\libraries\MODI\src\MODI_Interface.h` | (the library interface header file) | +| `Arduino\libraries\MODI\src\MODI_Property.h` | (the library property file) | +| `Arduino\libraries\MODI\src\type.h` | (the library type file) | +| `Arduino\libraries\MODI\src\mcp_can.cpp` | (the library CAN implmentation file) | +| `Arduino\libraries\MODI\src\mcp_can.h` | (the library CAN header file): | +| `Arduino\libraries\MODI\src\mcp_can_dfs.h` | (the library CAN header file) | +| `Arduino\libraries\MODI\src\keywords.txt` | (the syntax coloring file) | +| `Arduino\libraries\MODI\examples` | (the examples in the "open" menu) | +| `Arduino\libraries\MODI\readme.md` | (this file) | diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..48696c2 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,145 @@ +####################################### +# Syntax Coloring Map For MODI +####################################### + +####################################### +# Class (KEYWORD1) +####################################### + +# MODI Class +MODI KEYWORD1 + +# Input Module Class +Environment KEYWORD1 +Gyro KEYWORD1 +Mic KEYWORD1 +Button KEYWORD1 +Dial KEYWORD1 +Ultrasonic KEYWORD1 +Infrared KEYWORD1 + +# Output Module Class +Display KEYWORD1 +Motor KEYWORD1 +LED KEYWORD1 +Speaker KEYWORD1 + +# Set up Module Class +Network KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +# methods names in commond +# Common function name in MODI +begin KEYWORD2 +addRequest KEYWORD2 +# Input Module Function +# 1.Environment +readIlluminance KEYWORD2 +readRed KEYWORD2 +readGreen KEYWORD2 +readBlue KEYWORD2 +readTemperature KEYWORD2 +readHumidity KEYWORD2 +# 2.Gyro +readRoll KEYWORD2 +readPitch KEYWORD2 +readYaw KEYWORD2 +readGyro_X KEYWORD2 +readGyro_Y KEYWORD2 +readGyro_Z KEYWORD2 +readAccel_X KEYWORD2 +readAccel_Y KEYWORD2 +readAccel_Z KEYWORD2 +readVibration KEYWORD2 +# 3. Mic +readVolume KEYWORD2 +readFreq KEYWORD2 +# 4. Button +readClick KEYWORD2 +readDoubleClick KEYWORD2 +readPushState KEYWORD2 +readToggle KEYWORD2 +writeToggle KEYWORD2 +# 5. Dial +readDegree KEYWORD2 +readTurnSpeed KEYWORD2 +# 6. Ultrasonic +readDistance KEYWORD2 +# 7. Infrared +readProximity KEYWORD2 +# Output Module Function +# 1. Display +writePosition KEYWORD2 +writeText KEYWORD2 +writeDots KEYWORD2 +writePicture KEYWORD2 +resetDisplay KEYWORD2 +writeVariable KEYWORD2 +writeHorizontal KEYWORD2 +writeVertical KEYWORD2 +# 2. Motor +readTorque_Upper KEYWORD2 +readTorque_Bottom KEYWORD2 +readSpeed_Upper KEYWORD2 +readSpeed_Bottom KEYWORD2 +readAngle_Upper KEYWORD2 +readAngle_Bottom KEYWORD2 +readSenseSpeed_Upper KEYWORD2 +readSenseSpeed_Bottom KEYWORD2 +readSenseAngle_Upper KEYWORD2 +readSenseAngle_Bottom KEYWORD2 +writeTorque_Upper KEYWORD2 +writeTorque_Bottom KEYWORD2 +writeSpeed_Upper KEYWORD2 +writeSpeed_Bottom KEYWORD2 +writeAngle_Upper KEYWORD2 +writeAngle_Bottom KEYWORD2 +writeTorque KEYWORD2 +writeSpeed KEYWORD2 +writeAngle KEYWORD2 +# 3. LED +readRed KEYWORD2 +readGreen KEYWORD2 +readBlue KEYWORD2 +writeRed KEYWORD2 +writeGreen KEYWORD2 +writeBlue KEYWORD2 +writeRGB KEYWORD2 +# 4. Speaker +readVolume KEYWORD2 +readFreq KEYWORD2 +writeVolume KEYWORD2 +writeFreq KEYWORD2 +writeTune KEYWORD2 +writeSoundEffect KEYWORD2 +writeMelody KEYWORD2 +# Setup Module Function +readData KEYWORD2 +readEvent KEYWORD2 +writeData KEYWORD2 +writeEvent KEYWORD2 + + +####################################### +# PREPROCESSOR (KEYWORD3) +####################################### +# Serial output +MODI_PRINT KEYWORD3 +MODI_PRINTLN KEYWORD3 +MODI_PRINT_F KEYWORD3 + +####################################### +# CONSTANTS (LITERAL1) +####################################### +# Debug Mode +MODI_DEBUG_MODE LITERAL1 + +# MODI Pin +MODI_INT_PIN LITERAL1 +MODI_CS_PIN LITERAL1 +MODI_DIR_PIN LITERAL1 + +# Request \ No newline at end of file diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..b47409f --- /dev/null +++ b/library.properties @@ -0,0 +1,10 @@ +name=MODI +version=0.0.1 +author=LUXROBO +maintainer=tuna +sentence=A driver for control MODI on Arduino +paragraph=A driver for control MODI on Arduino +category=Other +url=https://github.com/LUXROBO/MODI-Arduino +architectures=* +includes=MODI.h diff --git a/src/MODI.cpp b/src/MODI.cpp new file mode 100644 index 0000000..ce8825b --- /dev/null +++ b/src/MODI.cpp @@ -0,0 +1,1879 @@ +/* + MODI.c + + Author : Luxrobo Arduino Team + Edited : Tuna (Tuna@luxrobo.com) +*/ + +#include "MODI.h" + + +static void MODI_Message_Handler(void); // CAN Message Handler + +MODI_Manager MODI; + +namespace modi +{ + MCP_CAN CAN(MODI_CS_PIN); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +MODI_Manager::MODI_Manager() +{ + modulecnt = 0; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void MODI_Manager::begin(void) +{ + /* CAN INIT */ + + modi::CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ); + modi::CAN.setMode(MCP_NORMAL); + /* Set SPI INT EXTI */ + pinMode(MODI_INT_PIN,INPUT_PULLUP); + + attachInterrupt(digitalPinToInterrupt(MODI_INT_PIN),MODI_Message_Handler,LOW); + + randomSeed(random(0,0xFFFF)); + /* find module */ + MODI.findModule(); + /* PnP off */ + for(uint16_t cnt = 0; cnt < MODI.modulecnt; cnt++) + { + setModulePnP(MODI.moduleID[cnt],MODULE_PNP_OFF); + } + /* Set Direction pin as input */ + pinMode(MODI_DIR_PIN,INPUT); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void MODI_Manager::begin(MODI_Connect_Apapter_t adapter) +{ + + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void MODI_Manager::findModule(void) +{ + Frame pkt; + pkt.cmd = CMD_FIND_ID; + pkt.sid = 0x000; + pkt.did = 0xFFF; + pkt.len = 0x07; + pkt.data[0] = 0xFF; + pkt.data[1] = 0x0F; + pkt.data[2] = 0x00; + pkt.data[3] = 0x00; + pkt.data[4] = 0x00; + pkt.data[5] = 0x00; + pkt.data[6] = 0x00; + pkt.data[7] = 0x00; + + uint32_t txID = pkt.cmd; + txID = (txID << 12) | pkt.sid; + txID = (txID << 12) | pkt.did; + + modi::CAN.sendMsgBuf(txID,1,pkt.len,pkt.data); + + return; +} + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +bool MODI_Manager::updateModule(uint16_t ID) +{ + uint16_t cnt = 0; + + /* search connected module */ + for(cnt = 0; cnt < modulecnt; cnt++) + { + /* already conencted module */ + if(moduleID[cnt] == ID) + { + break; + } + } + /* add connected module */ + moduleID[modulecnt] = ID; + modulecnt = modulecnt + 0x01; + + /* turn off PnP */ + setModulePnP(moduleID[cnt],MODULE_PNP_OFF); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void MODI_Manager::updatePropterty(uint16_t module_id,uint8_t pNum, uint8_t data[]) +{ + for(uint8_t cnt = 0; cnt < modulecnt; cnt++) + { + if(moduleID[cnt] == module_id) + { + property[cnt].value[pNum] = 0; + size64_val_t value; + + for(uint8_t i = 0 ; i < 8 ; i++) + { + //property[cnt].value[pNum] = (property[cnt].value[pNum] << (8 * i)) | (uint64_t)data[i]; + value.u8[i] = data[i]; + } + property[cnt].value[pNum] = value.u64; + } + } + + return; +} +uint16_t MODI_Manager::findModuleIndex(uint16_t ID) +{ + uint16_t index; + for(uint8_t cnt = 0; cnt < modulecnt; cnt++) + { + if(moduleID[cnt] == ID) + { + index = cnt; + return index; + } + } + return -1; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +bool MODI_Manager::setModulePnP(uint16_t module_id, uint8_t state) +{ + /* + How to control pnp + ---------------------------------- + 1. Send property Set state + 2. Set length 2 + 3. set byte[1] to 1 = turn on pnp + set byte[1] to 0 = turn off pnp + */ + + uint32_t txID = (CMD_SET_STATE & 0x1F); + txID = (txID << 24) | module_id; + uint8_t data[2] = {0,}; + data[0] = 0x00U; /* Run */ + /* turn off PnP*/ + if(state = 0) + { + data[1] = 0x01U; /* turn off PNP */ + modi::CAN.sendMsgBuf(txID,1,2,data); + return true; + } + /* turn on PnP */ + else if(state = 1) + { + data[1] = 0x02U; /* turn on PNP */ + modi::CAN.sendMsgBuf(txID,1,2,data); + return true; + } + /* invaild value */ + else + { + return false; + } + return false; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Module::Module() +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Module::Module(const uint64_t& uuid) : m_uuid(uuid) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Module::begin() +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Module::writeUUID(uint32_t uuid) +{ + m_uuid = uuid; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Module::writeID(uint16_t ID) +{ + m_uuid = (m_uuid & 0xFFFFFFFFFFFFF000) | (ID & 0x0FFF); + return; +} + +namespace math +{ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ + uint32_t getRandom(uint32_t min, uint32_t max) + { + int32_t dif = max - min, temp; + if(dif < 0 ) return 0; + + return random(min,max+1); + } +} + +/* * * * * Part of Module * * * * */ + +/* 0. Network Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Network::Network(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Network::Network(const uint64_t& assign_data) : Module(assign_data) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Network::begin(void) +{ +} + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Network::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Network::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint32_t Network::readData(void) +{ + return getPropertyInt(key(),PROP_NETWORK_DATA); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint32_t Network::readEvent(void) +{ + return getPropertyInt(key(),PROP_NETWORK_EVENT); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Network::writeData(int32_t data) +{ + setProperty(key(), PROP_NETWORK_DATA, data); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Network::writeEvent(int32_t data) +{ + setProperty64(key(),PROP_NETWORK_EVENT,data); + return; +} + +/* 1. Environment Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Environment::Environment() +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Environment::Environment(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Environment::begin(void) +{ + // idx = modulecnt++; + // module[idx].uuid = modi::MODULE_ENVIORNMENT; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Environment::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Environment::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Environment::readIlluminance(void) +{ + return getProperty(key(),PROP_ENV_ILLUMINANCE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Environment::readRed(void) +{ + return getProperty(key(),PROP_ENV_RED); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Environment::readGreen(void) +{ + return getProperty(key(),PROP_ENV_GREEN); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Environment::readBlue(void) +{ + return getProperty(key(),PROP_ENV_BLUE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Environment::readTemperature(void) +{ + return getProperty(key(),PROP_ENV_TEMPERATURE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Environment::readHumidity(void) +{ + return getProperty(key(),PROP_ENV_HUMIDITY); +} + +/* 2. Gyro Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Gyro::Gyro(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Gyro::Gyro(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Gyro::begin(void) +{ + // idx = modulecnt++; + // module[idx].uuid = modi::MODULE_GYRO; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Gyro::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Gyro::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readRoll(void) +{ + return getProperty(key(), PROP_GYRO_ROLL); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readPitch(void) +{ + return getProperty(key(), PROP_GYRO_PITCH); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readYaw(void) +{ + return getProperty(key(), PROP_GYRO_YAW); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readGyro_X(void) +{ + return getProperty(key(), PROP_GYRO_GYRO_X); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readGyro_Y(void) +{ + return getProperty(key(), PROP_GYRO_GYRO_Y); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readGyro_Z(void) +{ + return getProperty(key(), PROP_GYRO_GYRO_Z); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readAccel_X(void) +{ + return getProperty(key(), PROP_GYRO_ACCEL_X); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readAccel_Y(void) +{ + return getProperty(key(), PROP_GYRO_ACCEL_Y); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readAccel_Z(void) +{ + return getProperty(key(), PROP_GYRO_ACCEL_Z); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Gyro::readVibration(void) +{ + return getProperty(key(), PROP_GYRO_VIBRATION); +} + +/* 3. Mic Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Mic::Mic(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Mic::Mic(const uint64_t& assign_data) : Module(assign_data) +{ + +} + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Mic::begin(void) +{ + // idx = modulecnt++; + // module[idx].uuid = modi::MODULE_MIC; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Mic::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Mic::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Mic::readVolume(void) +{ + return getProperty(key(), PROP_MIC_VOLUME); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Mic::readFreq(void) +{ + return getProperty(key(), PROP_MIC_FREQUENCY); +} + +/* 4. Button Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Button::Button(void) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Button::Button(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Button::begin(void) +{ + // idx = modulecnt++; + // module[idx].uuid = modi::MODULE_BUTTON; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Button::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Button::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Button::readClick(void) +{ + return getProperty(key(), PROP_BUTTON_CLICK); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Button::readDoubleClick(void) +{ + return getProperty(key(), PROP_BUTTON_DOUBLE_CLICK); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Button::readPushState(void) +{ + return getProperty(key(), PROP_BUTTON_PUSH_STATE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Button::readToggle(void) +{ + return getProperty(key(), PROP_BUTTON_TOGGLE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Button::writeToggle(int16_t value) +{ + return setProperty(key(),PROP_BUTTON_TOGGLE,(float)value); +} + +/* 5. Dial Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Dial::Dial(void) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Dial::Dial(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Dial::begin(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Dial::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Dial::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Dial::readDegree(void) +{ + return getProperty(key(), PROP_DIAL_TURN); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Dial::readTurnSpeed(void) +{ + return getProperty(key(), PROP_DIAL_TURN_SPEED); +} + +/* 6. Ultrasonic Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Ultrasonic::Ultrasonic(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Ultrasonic::Ultrasonic(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Ultrasonic::begin(void) +{ + // idx = modulecnt++; + // module[idx].uuid = modi::MODULE_ULTRASONIC; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Ultrasonic::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Ultrasonic::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Ultrasonic::readDistance(void) +{ + return getProperty(key(), PROP_ULTRASONIC_DISTANCE); +} + +/* 7. Infrared Module */ + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Ir::Ir(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Ir::Ir(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Ir::begin(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Ir::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Ir::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Ir::readProximity(void) +{ + return getProperty(key(), PROP_IR_DISTANCE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Ir::readIntensity(void) +{ + return getProperty(key(), PROP_IR_INTENSITY); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Ir::readRemote(void) +{ + return getProperty(key(), PROP_IR_REMOTE); +} + +/* 8. Display Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Display::Display(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Display::Display(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Display::begin(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Display::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); + usingVarPropertyNum = 0; +} +void Display::writeState(uint8_t state) +{ + setState(key(),state); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Display::writePosition(int16_t X, int16_t Y) +{ + uint8_t data[8]; + data[0] = ((X >> 0x00FFU) >> 0); + data[1] = ((X >> 0xFF00U) >> 8); + data[2] = ((Y >> 0x00FFU) >> 0); + data[3] = ((Y >> 0xFF00U) >> 8); + setProperty64(key(),PROP_DISPLAY_RESET,data); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Display::writeText(char text[]) +{ + setPropertyString(key(),PROP_DISPLAY_TEXT, text); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Display::writeDots(char dots[]) +{ + resetDisplay(); + delay(10); + setDisplay48(key(),PROP_DISPLAY_DRAW_DATA, dots); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Display::writePicture(char keyword[]) +{ + uint8_t value[8] = {0}; + for(uint8_t i = 0; i < 8; i++) + { + if(keyword[i] != 0) + { + value[i] = keyword[i]; + } + else + { + break; + } + } + setProperty64(key(),PROP_DISPLAY_DRAW_VARIABLE,value); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Display::resetDisplay(void) +{ + displayDrawAddress = 0; + setProperty64(key(),PROP_DISPLAY_RESET,0); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Display::writeVariable(int16_t X, int16_t Y, float var) +{ + size64_val_t data; + data.u16[0] = X; + data.u16[1] = Y; + data.f32[1] = var; + + setProperty64(key(),PROP_DISPLAY_VARIABLE(usingVarPropertyNum),data.u64); + usingVarPropertyNum++; + if(usingVarPropertyNum > 2) + { + usingVarPropertyNum = 0; + } + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Display::writeHorizontal(int16_t val) +{ + uint8_t data[4] = {0}; + data[0] = ((val & 0x00FFU) >> 0); + data[1] = ((val & 0xFF00U) >> 8); + + displayDrawAddress = 0; + setProperty(key(),PROP_DISPLAY_HORIZONTAL,data); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Display::writeVertical(int16_t val) +{ + uint8_t data[4] = {0}; + data[0] = ((val & 0x00FFU) >> 0); + data[1] = ((val & 0xFF00U) >> 8); + displayDrawAddress = 0; + setProperty(key(),PROP_DISPLAY_VERTICAL,data); + return; +} + +/* 9. Motor Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Motor::Motor(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Motor::Motor(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Motor::begin(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Motor::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readTorque_Upper(void) +{ + return getProperty(key(), PROP_MOTOR_UTORQUE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readTorque_Bottom(void) +{ + return getProperty(key(), PROP_MOTOR_BTORQUE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readSpeed_Upper(void) +{ + return getProperty(key(), PROP_MOTOR_USPEED); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readSpeed_Bottom(void) +{ + return getProperty(key(), PROP_MOTOR_BSPEED); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readAngle_Upper(void) +{ + return getProperty(key(), PROP_MOTOR_UANGLE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readAngle_Bottom(void) +{ + return getProperty(key(), PROP_MOTOR_BANGLE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readSenseSpeed_Upper(void) +{ + return getProperty(key(), PROP_MOTOR_USPEED_S); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readSenseSpeed_Bottom(void) +{ + return getProperty(key(), PROP_MOTOR_BSPEED_S); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readSenseAngle_Upper(void) +{ + return getProperty(key(), PROP_MOTOR_UANGLE_S); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Motor::readSenseAngle_Bottom(void) +{ + return getProperty(key(), PROP_MOTOR_BANGLE_S); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeTorque_Upper(float torque) +{ + setProperty(key(),PROP_MOTOR_UTORQUE,torque); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeTorque_Bottom(float torque) +{ + setProperty(key(),PROP_MOTOR_BTORQUE,torque); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeSpeed_Upper(float speed) +{ + setProperty(key(),PROP_MOTOR_USPEED,speed); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeSpeed_Bottom(float speed) +{ + setProperty(key(),PROP_MOTOR_BSPEED,speed); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeAngle_Upper(float angle) +{ + setProperty(key(),PROP_MOTOR_UANGLE,angle); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeAngle_Bottom(float angle) +{ + setProperty(key(),PROP_MOTOR_BANGLE,angle); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeTorque(int16_t UpperTorque, int16_t BottomTorque) +{ + uint8_t val[4] = {0,}; + val[0] = ((UpperTorque & 0x00FFU) >> 0); + val[1] = ((UpperTorque & 0xFF00U) >> 8); + val[2] = ((BottomTorque & 0x00FFU) >> 0); + val[3] = ((BottomTorque & 0xFF00U) >> 8); + setProperty(key(),PROP_MOTOR_TORQUE,val); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeSpeed(int16_t UpperSpeed, int16_t BottomSpeed) +{ + size64_val_t value; + value.i16[0] = UpperSpeed; + value.i16[1] = BottomSpeed; + value.i16[2] = 0; + + setProperty64(key(),PROP_MOTOR_SPEED,value.u64); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Motor::writeAngle(int16_t UpperAngle, int16_t BottomAngle) +{ + uint8_t val[4] = {0,}; + val[0] = ((UpperAngle & 0x00FFU) >> 0); + val[1] = ((UpperAngle & 0xFF00U) >> 8); + val[2] = ((BottomAngle & 0x00FFU) >> 0); + val[3] = ((BottomAngle & 0xFF00U) >> 8); + setProperty(key(),PROP_MOTOR_ANGLE,val); + return; +} + +/* 10. LED Module */ + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +LED::LED(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +LED::LED(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t LED::begin(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t LED::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void LED::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float LED::readRed(void) +{ + return getProperty(key(), PROP_LED_RED); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float LED::readGreen(void) +{ + return getProperty(key(), PROP_LED_GREEN); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float LED::readBlue(void) +{ + return getProperty(key(), PROP_LED_BLUE); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void LED::writeRed(float R) +{ + setProperty(key(),PROP_LED_RED,R); + return; +} + +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void LED::writeGreen(float G) +{ + setProperty(key(),PROP_LED_GREEN,G); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void LED::writeBlue(float B) +{ + setProperty(key(),PROP_LED_BLUE,B); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void LED::writeRGB(int16_t R, int16_t G, int16_t B) +{ + uint64_t RGB; + RGB = B; + RGB = (RGB << 16) | G; + RGB = (RGB << 16) | R; + + setProperty64(key(),PROP_LED_RGB,RGB); + return; +} + +/* 11. Speaker Module */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Speaker::Speaker(void) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +Speaker::Speaker(const uint64_t& assign_data) : Module(assign_data) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Speaker::begin(void) +{ + // idx = modulecnt++; + // module[idx].uuid = modi::MODULE_SPEAKER; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint8_t Speaker::begin(uint16_t ID) +{ + writeID(ID); + MODI.updateModule(ID); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Speaker::addRequest(uint8_t property) +{ + requestPropterty(key(),property,0x60); // period : to be updated; + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Speaker::readVolume(void) +{ + return getProperty(key(), PROP_SPEAKER_VOLUME); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float Speaker::readFreq(void) +{ + return getProperty(key(), PROP_SPEAKER_FREQUENCY); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Speaker::writeVolume(float volume) +{ + setProperty(key(),PROP_SPEAKER_VOLUME,volume); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Speaker::writeFreq(float freq) +{ + setProperty(key(),PROP_SPEAKER_FREQUENCY,freq); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Speaker::writeTune(float freq, float volume) +{ + size64_val_t val; + val.f32[0] = freq; + val.f32[1] = volume; + setProperty64(key(),PROP_SPEAKER_TUNE,val.u64); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Speaker::writeSoundEffect(float sound, float volume) +{ + size64_val_t val; + val.f32[0] = sound; + val.f32[1] = volume; + setProperty64(key(),PROP_SPEAKER_EFFECT,val.u64); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void Speaker::writeMelody(float melody, float volume) +{ + size64_val_t val; + val.f32[0] = melody; + val.f32[1] = volume; + setProperty64(key(),PROP_SPEAKER_MELODY,val.u64); + return; +} + +/* * * * * Part of Module * * * * */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint32_t Module::uuid(void) const +{ + return ((m_uuid & 0x00000000FFFFFFFF) >> 32); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +ModuleKey Module::key(void) const +{ + uint32_t key = (uint32_t)(m_uuid & 0x0000000000000FFF); + return (ModuleKey)key; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +ModuleType Module::type(void) const +{ + return (ModuleType)((m_uuid & 0x0000FFFF00000000) >> 32); +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +AppMode Module::mode(void) const +{ + return m_app_mode; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +ModuleVersion Module::version(void) const +{ + return (ModuleVersion)(m_uuid & 0xFFFF000000000000 >> 48); +} + + +/* * * * * Part of Interrupt * * * * */ + +/* CAN Interrupt */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +static void MODI_Message_Handler(void) +{ + /* Read Message from CAN */ + + uint32_t rxId = 0; + uint32_t ModuleID; + uint8_t len = 0; + Frame pkt; + + modi::CAN.readMsgBuf(&rxId, &len, pkt.data); + + rxId = rxId & 0x1FFFFFFF; // 29 bit MASK + pkt.cmd = rxId >> 24; + pkt.sid = (rxId & 0x00FFF000) >> 12; + pkt.did = rxId & 0x00000FFF; + pkt.len = len; + + uint8_t datatype = 0; + uint16_t propertyNum = 0; + + + /* message */ + switch(pkt.cmd) + { + case 0x00: // case Health + return; + break; + + case 0x05: // case Assign ID + ModuleID = pkt.data[5]; + ModuleID = ModuleID << 8; + ModuleID = ModuleID | pkt.data[4]; + + if (ModuleID == 0x0000) MODI_PRINT_F("AssignID received. Module Network - ID: 0x");//case Network + else if (ModuleID == 0x2000) MODI_PRINT_F("AssignID received. Module Environment - ID: 0x");//case Environment + else if (ModuleID == 0x2010) MODI_PRINT_F("AssignID received. Module Gyroscope - ID: 0x");//case Gyroscope + else if (ModuleID == 0x2020) MODI_PRINT_F("AssignID received. Module Mic - ID: 0x");//case Mic + else if (ModuleID == 0x2030) MODI_PRINT_F("AssignID received. Module Button - ID: 0x");//case Button + else if (ModuleID == 0x2040) MODI_PRINT_F("AssignID received. Module Dial - ID: 0x");//case Dial + else if (ModuleID == 0x2050) MODI_PRINT_F("AssignID received. Module Ultrasonic - ID: 0x");//case Ultrasonic + else if (ModuleID == 0x2060) MODI_PRINT_F("AssignID received. Module IR - ID: 0x");//case IR + else if (ModuleID == 0x4000) MODI_PRINT_F("AssignID received. Module Display - ID: 0x");//case Display + else if (ModuleID == 0x4010) MODI_PRINT_F("AssignID received. Module Motor - ID: 0x");//case Motor + else if (ModuleID == 0x4020) MODI_PRINT_F("AssignID received. Module LED - ID: 0x");//case LED + else if (ModuleID == 0x4030) MODI_PRINT_F("AssignID received. Module Speaker - ID: 0x");//case Speaker + + MODI_PRINTLN(pkt.sid, HEX); + + /* Module Update */ + + return ; + break; + + case 0x1F: // case ChannelProperty + datatype = (pkt.did & 0x0F00) >> 8; + propertyNum = (pkt.did & 0x00FF) >> 0; + MODI.updatePropterty(pkt.sid,propertyNum,pkt.data); + break; + + } + return; +} + +/* End of File */ diff --git a/src/MODI.h b/src/MODI.h new file mode 100644 index 0000000..23b285f --- /dev/null +++ b/src/MODI.h @@ -0,0 +1,410 @@ +/* + MODI.h + + Author : Luxrobo Arduino Team + Edtied : Tuna (Tuna@luxrobo.com) + +*/ + +#ifndef MODI_H_ +#define MODI_H_ + +#include +#include + +#include "mcp_can.h" + +#include "MODI_Interface.h" +#include "MODI_property.h" +#include "types.h" + +/* change to 1 if you want to print information */ +#define MODI_DEBUG_MODE 1 + +/* */ +#define MODI_MAX_MODULE_NUM 5 + +/* Pin out for Arduino MKR Series */ +#if defined(ARDUINO_ARCH_SAMD) + #define MODI_INT_PIN 7 + #define MODI_CS_PIN 3 + #define MODI_DIR_PIN 2 +#endif + +/* Pin out for Arduino UNO */ + #if defined(ARDUINO_ARCH_AVR) + #define MODI_INT_PIN 2 + #define MODI_CS_PIN 10 + #define MODI_DIR_PIN 3 + #endif + +#if MODI_DEBUG_MODE + #if defined(ARDUINO_ARCH_AVR) + #define MODI_PRINT(X,...) Serial.print(X,##__VA_ARGS__) + #define MODI_PRINTLN(X,...) Serial.println(X,##__VA_ARGS__) + #define MODI_PRINT_F(X,...) Serial.print(F(X),##__VA_ARGS__) + #elif defined(ARDUINO_ARCH_SAMD) + #define MODI_PRINT(X,...) Serial.print(X,##__VA_ARGS__) + #define MODI_PRINTLN(X,...) Serial.println(X,##__VA_ARGS__) + #define MODI_PRINT_F(X,...) Serial.print(X,##__VA_ARGS__) + #endif +#else + #define MODI_PRINT(...) + #define MODI_PRINTLN(...) +#endif + +class Module +{ + private: + uint64_t m_uuid; + AppMode m_app_mode; + + public: + Module(); + Module(const uint64_t& uuid); + + uint32_t uuid(void) const; + ModuleKey key(void) const; + ModuleType type(void) const; + AppMode mode(void) const; + ModuleVersion version(void) const; + + void writeUUID(uint32_t uuid); + void writeID(uint16_t ID); + + void begin(void); +}; + +class MODI_Manager +{ +public : + void findModule(void); + bool updateModule(uint16_t ID); + bool setModulePnP(uint16_t module_id, uint8_t state); + uint16_t findModuleIndex(uint16_t ID); + void updatePropterty(uint16_t module_id, uint8_t pNum, uint8_t data[]); + typedef enum + { + MODI_CAN = 0x00, + MODI_SERIAL = 0x01, + MODI_WIFI = 0x02, + MODI_BLE = 0x03 + }MODI_Connect_Apapter_t; + +public : + MODI_Manager(); + void begin(void); + void begin(MODI_Connect_Apapter_t adapter); + + modi_property_t property[MODI_MAX_MODULE_NUM]; +private : + + uint16_t moduleID[MODI_MAX_MODULE_NUM]; + uint16_t modulecnt; + +}; + +/* 0. Network Module */ +class Network :public Module, public PropertySender +{ + private: + + public: + Network(); + Network(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + uint32_t readData(void); + uint32_t readEvent(void); + + void writeData(int32_t data); + void writeEvent(int32_t data); +}; + +/* 1. Environment Module */ + +class Environment : public Module,public PropertySender +{ + private: + + public: + Environment(); + Environment(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readIlluminance(void); + float readRed(void); + float readGreen(void); + float readBlue(void); + float readTemperature(void); + float readHumidity(void); +}; + +/* 2. Gyro Module */ +class Gyro : public Module, public PropertySender +{ + private: + public: + Gyro(); + Gyro (const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readRoll(void); + float readPitch(void); + float readYaw(void); + float readGyro_X(void); + float readGyro_Y(void); + float readGyro_Z(void); + float readAccel_X(void); + float readAccel_Y(void); + float readAccel_Z(void); + float readVibration(void); +}; + + +/* 3. Mic Module */ +class Mic :public Module, public PropertySender +{ + private: + public: + Mic(); + Mic(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readVolume(void); + float readFreq(void); + +}; + +/* 4. Button Module */ +class Button :public Module, public PropertySender +{ + private: + public: + Button(); + Button(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readClick(void); + float readDoubleClick(void); + float readPushState(void); + float readToggle(void); + void writeToggle(int16_t value); +}; + + +/* 5. Dial Module */ +class Dial :public Module, public PropertySender +{ + private: + public: + Dial(); + Dial(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readDegree(void); + float readTurnSpeed(void); +}; + +/* 6. Ultrasonic Module */ +class Ultrasonic :public Module, public PropertySender +{ + private: + public: + Ultrasonic(); + Ultrasonic(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readDistance(void); +}; + + +/* 7. Infrared Module */ +class Ir :public Module, public PropertySender +{ + private: + public: + Ir(); + Ir(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readProximity(void); + float readIntensity(void); + float readRemote(void); +}; + + +/* 8. Display Module */ +class Display :public Module, public PropertySender +{ + private: + uint16_t usingVarPropertyNum; + public: + Display(); + Display(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void writeState(uint8_t state); + + void writePosition(int16_t X, int16_t Y); + void writeText(char text[]); + void writeDots(char dots[]); + void writePicture(char keyword[]); + void resetDisplay(void); + void writeVariable(int16_t X, int16_t Y, float var); + void writeHorizontal(int16_t val); + void writeVertical(int16_t val); + +}; + +/* 9. Motor Module */ +class Motor :public Module, public PropertySender +{ + private: + public: + Motor(); + Motor(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readTorque_Upper(void); + float readTorque_Bottom(void); + float readSpeed_Upper(void); + float readSpeed_Bottom(void); + float readAngle_Upper(void); + float readAngle_Bottom(void); + float readSenseSpeed_Upper(void); + float readSenseSpeed_Bottom(void); + float readSenseAngle_Upper(void); + float readSenseAngle_Bottom(void); + + void writeTorque_Upper(float torque); + void writeTorque_Bottom(float torque); + void writeSpeed_Upper(float speed); + void writeSpeed_Bottom(float speed); + void writeAngle_Upper(float angle); + void writeAngle_Bottom(float angle); + + void writeTorque(int16_t UpperTorque, int16_t BottomTorque); + void writeSpeed(int16_t UpperSpeed, int16_t BottomSpeed); + void writeAngle(int16_t UpperSpeed, int16_t BottomSpeed); + +}; + +/* 10. LED Module */ +class LED :public Module, public PropertySender +{ + private: + public: + LED(); + LED(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readRed(void); + float readGreen(void); + float readBlue(void); + + void writeRed(float R); + void writeGreen(float G); + void writeBlue(float B); + + void writeRGB(int16_t R, int16_t G, int16_t B); +}; + + +/* 11. Speaker Module */ +class Speaker :public Module, public PropertySender +{ + private: + public: + Speaker(); + Speaker(const uint64_t& assign_data); + + uint8_t begin(void); + uint8_t begin(uint16_t uuid); + + void addRequest(uint8_t property); + + void writeState(uint8_t state); + + float readVolume(void); + float readFreq(void); + + void writeVolume(float volume); + void writeFreq(float m_freq); + void writeTune(float freq, float volume); + void writeSoundEffect(float sound, float volume); + void writeMelody(float melody, float volume); +}; + + +namespace modi +{ + extern MCP_CAN CAN; +} +extern MODI_Manager MODI; + +#endif // MODI_H_ + +/* End of File */ diff --git a/src/MODI_Interface.cpp b/src/MODI_Interface.cpp new file mode 100644 index 0000000..2d3baf1 --- /dev/null +++ b/src/MODI_Interface.cpp @@ -0,0 +1,177 @@ +/* + MODI_Interface.c + + Author : Luxrobo Arduino Team + Edited : Tuna (Tuna@luxrobo.com) +*/ + +#include "MODI_Interface.h" +#include "MODI.h" + +/* * * * * Part of PropertySender * * * * */ +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +uint32_t PropertySender::getTxID(uint16_t destID, uint16_t pNum, uint16_t datatype, uint16_t cmd_Property) +{ + uint32_t txID = (cmd_Property & 0x1F); + txID = (txID << 4) | datatype; + txID = (txID << 8) | pNum; + txID = (txID << 12)| destID; + return txID; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void PropertySender::requestPropterty(uint16_t destID,uint8_t pNum, uint16_t period) +{ + uint32_t txID = (CMD_REQUEST_PP & 0x1F); + txID = (txID << 24) | destID ; + uint8_t val[8] = {0,}; + val[0] = pNum; + val[2] = period; + + modi::CAN.sendMsgBuf(txID,1,8,val); // (ID, extended flag, data length, data) + + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void PropertySender::setState(uint16_t destID,uint8_t state) +{ + uint32_t txID = (CMD_SET_STATE & 0x1F); + txID = (txID << 24) | destID; + + uint8_t module_state = state; + + modi::CAN.sendMsgBuf(txID,1,1,&module_state); + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void PropertySender::setProperty64(uint16_t destID, uint16_t pNum, uint64_t value) +{ + uint8_t data[8] = {0,}; + data[0] = ((value >> 0) & 0xFF); + data[1] = ((value >> 8) & 0xFF); + data[2] = ((value >> 16) & 0xFF); + data[3] = ((value >> 24) & 0xFF); + data[4] = ((value >> 32) & 0xFF); + data[5] = ((value >> 40) & 0xFF); + data[6] = ((value >> 48) & 0xFF); + data[7] = ((value >> 56) & 0xFF); + uint32_t txID = getTxID(destID,pNum,DATA_F64,CMD_SET_PP); + + modi::CAN.sendMsgBuf(txID, 1, 8, data);//(ID, extended flag, data length, data) + + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void PropertySender::setProperty(uint16_t destID, uint16_t pNum, float value) +{ + size32_val_t data; + data.f32 = value; + uint32_t txID = getTxID(destID,pNum,DATA_F32,CMD_SET_PP); + + modi::CAN.sendMsgBuf(txID, 1, 8, data.u8);//(ID, extended flag, data length, data) + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void PropertySender::setProperty(uint16_t destID, uint16_t pNum, uint8_t* value) +{ + + uint32_t txID = getTxID(destID,pNum,DATA_I16_I16,CMD_SET_PP); + + modi::CAN.sendMsgBuf(txID, 1, 8, value);//(ID, extended flag, data length, data) + return; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void PropertySender::setPropertyString(uint16_t destID, uint16_t pNum, char * value) +{ +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void PropertySender::setPropertyFunction(uint16_t destID, uint16_t pNum, uint64_t value) +{ + +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +void PropertySender::setDisplay48(uint16_t destID, uint16_t pNum, char value[]) +{ + uint8_t display_1line[8] = {0,}; + for(int i = 0; i < 48; i++) + { + for(int j = 0; j < 8; j++) + { + display_1line[j] = value[j + (i*8)]; + } + setProperty64(destID,pNum,display_1line); + delay(1); + } +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +int32_t PropertySender::getPropertyInt(uint16_t destID, uint16_t pNum) +{ + uint16_t index = MODI.findModuleIndex(destID); + size64_val_t value; + value.u64 = MODI.property[index].value[pNum]; + return value.i32[0]; +} +/* + * @Func : + * @Brief : + * @Param : + * @RetVal: + */ +float PropertySender::getProperty(uint16_t destID, uint16_t pNum) +{ + uint16_t index = MODI.findModuleIndex(destID); + size64_val_t value; + value.u64 = MODI.property[index].value[pNum]; + return value.f32[0]; +} + +/* End of File */ \ No newline at end of file diff --git a/src/MODI_Interface.h b/src/MODI_Interface.h new file mode 100644 index 0000000..ce6e25d --- /dev/null +++ b/src/MODI_Interface.h @@ -0,0 +1,42 @@ +/* + MODI_Interface.h + + Author : Luxrobo Arduino Team + Edited : Tuna (Tuna@luxrobo.com) +*/ + +#ifndef MODI_INTERFACE_H_ +#define MODI_INTERFACE_H_ + +#include +#include + +#include "types.h" + +class PropertySender +{ +private: +public: + uint32_t getTxID(uint16_t destID, uint16_t pNum, uint16_t datatype, uint16_t cmd_Property); + + void requestPropterty(uint16_t destID,uint8_t pNum, uint16_t period); + + void setState(uint16_t destID,uint8_t state); + + void setProperty64(uint16_t destID, uint16_t pNum, uint64_t value); + void setProperty(uint16_t destID, uint16_t pNum, float value); + void setProperty(uint16_t destID, uint16_t pNum, uint8_t value[]); + void setPropertyString(uint16_t destID, uint16_t pNum, char * value); + void setPropertyFunction(uint16_t destID, uint16_t pNum, uint64_t value); + void setDisplay48(uint16_t destID, uint16_t pNum, char * value); + int32_t getPropertyInt(uint16_t destID, uint16_t pNum); + float getProperty(uint16_t destID, uint16_t pNum); +protected: + typedef int PropertyNum; +protected: + int32_t displayDrawAddress; +}; + +#endif // MODI_INTERFACE_H + +/* End of File */ diff --git a/src/MODI_property.h b/src/MODI_property.h new file mode 100644 index 0000000..2f5f098 --- /dev/null +++ b/src/MODI_property.h @@ -0,0 +1,184 @@ +#ifndef MODI_INTERFACE_PROPERTIES_H_ +#define MODI_INTERFACE_PROPERTIES_H_ + +#define PROP_ENV_ILLUMINANCE 2 +#define PROP_ENV_RED 3 +#define PROP_ENV_GREEN 4 +#define PROP_ENV_BLUE 5 +#define PROP_ENV_TEMPERATURE 6 +#define PROP_ENV_HUMIDITY 7 + +#define PROP_GYRO_ROLL 2 +#define PROP_GYRO_PITCH 3 +#define PROP_GYRO_YAW 4 +#define PROP_GYRO_GYRO_X 5 +#define PROP_GYRO_GYRO_Y 6 +#define PROP_GYRO_GYRO_Z 7 +#define PROP_GYRO_ACCEL_X 8 +#define PROP_GYRO_ACCEL_Y 9 +#define PROP_GYRO_ACCEL_Z 10 +#define PROP_GYRO_VIBRATION 11 + +#define PROP_MIC_VOLUME 2 +#define PROP_MIC_FREQUENCY 3 +#define PROP_MIC_50HZ 4 +#define PROP_MIC_100HZ 5 +#define PROP_MIC_150HZ 6 +#define PROP_MIC_200HZ 7 +#define PROP_MIC_250HZ 8 +#define PROP_MIC_300HZ 9 +#define PROP_MIC_350HZ 10 +#define PROP_MIC_400HZ 11 + +#define PROP_BUTTON_CLICK 2 +#define PROP_BUTTON_DOUBLE_CLICK 3 +#define PROP_BUTTON_PUSH_STATE 4 +#define PROP_BUTTON_TOGGLE 5 + +#define PROP_DIAL_TURN 2 +#define PROP_DIAL_TURN_SPEED 3 + + +#define PROP_ULTRASONIC_DISTANCE 2 + +#define PROP_IR_DISTANCE 2 +#define PROP_IR_INTENSITY 3 +#define PROP_IR_REMOTE 4 + +#define PROP_LED_RED 2 +#define PROP_LED_GREEN 3 +#define PROP_LED_BLUE 4 +#define PROP_LED_RGB 16 + +#define PROP_MOTOR_UTORQUE 2 +#define PROP_MOTOR_USPEED 3 +#define PROP_MOTOR_UANGLE 4 +#define PROP_MOTOR_USPEED_S 5 +#define PROP_MOTOR_UANGLE_S 6 +#define PROP_MOTOR_BTORQUE 10 +#define PROP_MOTOR_BSPEED 11 +#define PROP_MOTOR_BANGLE 12 +#define PROP_MOTOR_BSPEED_S 13 +#define PROP_MOTOR_BANGLE_S 14 +#define PROP_MOTOR_TORQUE 16 +#define PROP_MOTOR_SPEED 17 +#define PROP_MOTOR_ANGLE 18 + +#define PROP_SPEAKER_VOLUME 2 +#define PROP_SPEAKER_FREQUENCY 3 +#define PROP_SPEAKER_TUNE 16 +#define PROP_SPEAKER_RESET 17 +#define PROP_SPEAKER_EFFECT 18 +#define PROP_SPEAKER_MELODY 19 + +#define PROP_DISPLAY_POSITION 16 +#define PROP_DISPLAY_TEXT 17 +#define PROP_DISPLAY_DRAW_DATA 18 +#define PROP_DISPLAY_DRAW_VARIABLE 19 +#define PROP_DISPLAY_DATA 20 +#define PROP_DISPLAY_RESET 21 +#define PROP_DISPLAY_VARIABLE1 22 +#define PROP_DISPLAY_VARIABLE2 23 +#define PROP_DISPLAY_VARIABLE3 24 +#define PROP_DISPLAY_HORIZONTAL 25 +#define PROP_DISPLAY_VERTICAL 26 + +#define PROP_DISPLAY_ADD_PICTURE 27 + +#define PROP_DISPLAY_VARIABLE(x) PROP_DISPLAY_VARIABLE1 + x + +#define PROP_NETWORK_DATA 2 +#define PROP_NETWORK_EVENT 3 + +#define MODULE_PNP_OFF 0 +#define MODULE_PNP_ON 1 + + +#define F_DO_1 32 +#define F_RE_1 36 +#define F_MI_1 41 +#define F_PA_1 43 +#define F_SOL_1 48 +#define F_RA_1 55 +#define F_SI_1 61 +#define F_DO_S_1 34 +#define F_RE_S_1 39 +#define F_PA_S_1 46 +#define F_SOL_S_1 52 +#define F_RA_S_1 58 +#define F_DO_2 65 +#define F_RE_2 73 +#define F_MI_2 82 +#define F_PA_2 87 +#define F_SOL_2 97 +#define F_RA_2 110 +#define F_SI_2 123 +#define F_DO_S_2 69 +#define F_RE_S_2 77 +#define F_PA_S_2 92 +#define F_SOL_S_2 103 +#define F_RA_S_2 116 +#define F_DO_3 130 +#define F_RE_3 146 +#define F_MI_3 165 +#define F_PA_3 174 +#define F_SOL_3 196 +#define F_RA_3 220 +#define F_SI_3 247 +#define F_DO_S_3 138 +#define F_RE_S_3 155 +#define F_PA_S_3 185 +#define F_SOL_S_3 207 +#define F_RA_S_3 233 +#define F_DO_4 261 +#define F_RE_4 293 +#define F_MI_4 329 +#define F_PA_4 349 +#define F_SOL_4 392 +#define F_RA_4 440 +#define F_SI_4 493 +#define F_DO_S_4 277 +#define F_RE_S_4 311 +#define F_PA_S_4 369 +#define F_SOL_S_4 415 +#define F_RA_S_4 466 +#define F_DO_5 523 +#define F_RE_5 587 +#define F_MI_5 659 +#define F_PA_5 698 +#define F_SOL_5 783 +#define F_RA_5 880 +#define F_SI_5 988 +#define F_DO_S_5 554 +#define F_RE_S_5 622 +#define F_PA_S_5 739 +#define F_SOL_S_5 830 +#define F_RA_S_5 932 +#define F_DO_6 1046 +#define F_RE_6 1174 +#define F_MI_6 1318 +#define F_PA_6 1397 +#define F_SOL_6 1567 +#define F_RA_6 1760 +#define F_SI_6 1975 +#define F_DO_S_6 1108 +#define F_RE_S_6 1244 +#define F_PA_S_6 1479 +#define F_SOL_S_6 1661 +#define F_RA_S_6 1864 +#define F_DO_7 2093 +#define F_RE_7 2349 +#define F_MI_7 2637 +#define F_PA_7 2793 +#define F_SOL_7 3135 +#define F_RA_7 3520 +#define F_SI_7 3951 +#define F_DO_S_7 2217 +#define F_RE_S_7 2489 +#define F_PA_S_7 2959 +#define F_SOL_S_7 3322 +#define F_RA_S_7 3729 + +#endif // MODI_INTERFACE_PROPERTIES_H_ + +/* End of File */ diff --git a/src/mcp_can.cpp b/src/mcp_can.cpp new file mode 100644 index 0000000..c160939 --- /dev/null +++ b/src/mcp_can.cpp @@ -0,0 +1,1291 @@ +/* + mcp_can.cpp + 2012 Copyright (c) Seeed Technology Inc. All right reserved. + 2017 Copyright (c) Cory J. Fowler All Rights Reserved. + + Author: Loovee + Contributor: Cory J. Fowler + 2017-09-25 + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110- + 1301 USA +*/ +#include "mcp_can.h" + +#define spi_readwrite SPI.transfer +#define spi_read() spi_readwrite(0x00) + +/********************************************************************************************************* +** Function name: mcp2515_reset +** Descriptions: Performs a software reset +*********************************************************************************************************/ +void MCP_CAN::mcp2515_reset(void) +{ + SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)); + MCP2515_SELECT(); + spi_readwrite(MCP_RESET); + MCP2515_UNSELECT(); + SPI.endTransaction(); + delayMicroseconds(10); +} + +/********************************************************************************************************* +** Function name: mcp2515_readRegister +** Descriptions: Read data register +*********************************************************************************************************/ +INT8U MCP_CAN::mcp2515_readRegister(const INT8U address) +{ + INT8U ret; + + SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)); + MCP2515_SELECT(); + spi_readwrite(MCP_READ); + spi_readwrite(address); + ret = spi_read(); + MCP2515_UNSELECT(); + SPI.endTransaction(); + + return ret; +} + +/********************************************************************************************************* +** Function name: mcp2515_readRegisterS +** Descriptions: Reads sucessive data registers +*********************************************************************************************************/ +void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const INT8U n) +{ + INT8U i; + SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)); + MCP2515_SELECT(); + spi_readwrite(MCP_READ); + spi_readwrite(address); + // mcp2515 has auto-increment of address-pointer + for (i=0; i TXBnD7 */ + a1 = MCP_TXB0CTRL; + a2 = MCP_TXB1CTRL; + a3 = MCP_TXB2CTRL; + for (i = 0; i < 14; i++) { /* in-buffer loop */ + mcp2515_setRegister(a1, 0); + mcp2515_setRegister(a2, 0); + mcp2515_setRegister(a3, 0); + a1++; + a2++; + a3++; + } + mcp2515_setRegister(MCP_RXB0CTRL, 0); + mcp2515_setRegister(MCP_RXB1CTRL, 0); +} + +/********************************************************************************************************* +** Function name: mcp2515_init +** Descriptions: Initialize the controller +*********************************************************************************************************/ +INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const INT8U canClock) +{ + + INT8U res; + + mcp2515_reset(); + + mcpMode = MCP_LOOPBACK; + + res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Entering Configuration Mode Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Entering Configuration Mode Successful!\r\n"); +#endif + + // Set Baudrate + if(mcp2515_configRate(canSpeed, canClock)) + { +#if DEBUG_MODE + Serial.print("Setting Baudrate Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Baudrate Successful!\r\n"); +#endif + + if ( res == MCP2515_OK ) { + + /* init canbuffers */ + mcp2515_initCANBuffers(); + + /* interrupt mode */ + mcp2515_setRegister(MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); + + //Sets BF pins as GPO + mcp2515_setRegister(MCP_BFPCTRL,MCP_BxBFS_MASK | MCP_BxBFE_MASK); + //Sets RTS pins as GPI + mcp2515_setRegister(MCP_TXRTSCTRL,0x00); + + switch(canIDMode) + { + case (MCP_ANY): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_ANY); + break; +/* The followingn two functions of the MCP2515 do not work, there is a bug in the silicon. + case (MCP_STD): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STD | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_STD); + break; + + case (MCP_EXT): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_EXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_EXT); + break; +*/ + case (MCP_STDEXT): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_STDEXT); + break; + + default: +#if DEBUG_MODE + Serial.print("`Setting ID Mode Failure...\r\n"); +#endif + return MCP2515_FAIL; + break; +} + + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res) + { +#if DEBUG_MODE + Serial.print("Returning to Previous Mode Failure...\r\n"); +#endif + return res; + } + + } + return res; + +} + +/********************************************************************************************************* +** Function name: mcp2515_write_id +** Descriptions: Write CAN ID +*********************************************************************************************************/ +void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT32U id ) +{ + uint16_t canid; + INT8U tbufdata[4]; + + canid = (uint16_t)(id & 0x0FFFF); + + if ( ext == 1) + { + tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); + tbufdata[MCP_EID8] = (INT8U) (canid >> 8); + canid = (uint16_t)(id >> 16); + tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03); + tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3); + tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M; + tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 ); + } + else + { + tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 ); + tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 ) << 5); + tbufdata[MCP_EID0] = 0; + tbufdata[MCP_EID8] = 0; + } + + mcp2515_setRegisterS( mcp_addr, tbufdata, 4 ); +} + +/********************************************************************************************************* +** Function name: mcp2515_write_mf +** Descriptions: Write Masks and Filters +*********************************************************************************************************/ +void MCP_CAN::mcp2515_write_mf( const INT8U mcp_addr, const INT8U ext, const INT32U id ) +{ + uint16_t canid; + INT8U tbufdata[4]; + + canid = (uint16_t)(id & 0x0FFFF); + + if ( ext == 1) + { + tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); + tbufdata[MCP_EID8] = (INT8U) (canid >> 8); + canid = (uint16_t)(id >> 16); + tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03); + tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3); + tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M; + tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 ); + } + else + { + tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); + tbufdata[MCP_EID8] = (INT8U) (canid >> 8); + canid = (uint16_t)(id >> 16); + tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07) << 5); + tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 ); + } + + mcp2515_setRegisterS( mcp_addr, tbufdata, 4 ); +} + +/********************************************************************************************************* +** Function name: mcp2515_read_id +** Descriptions: Read CAN ID +*********************************************************************************************************/ +void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id ) +{ + INT8U tbufdata[4]; + + *ext = 0; + *id = 0; + + mcp2515_readRegisterS( mcp_addr, tbufdata, 4 ); + + *id = (tbufdata[MCP_SIDH]<<3) + (tbufdata[MCP_SIDL]>>5); + + if ( (tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M ) + { + /* extended id */ + *id = (*id<<2) + (tbufdata[MCP_SIDL] & 0x03); + *id = (*id<<8) + tbufdata[MCP_EID8]; + *id = (*id<<8) + tbufdata[MCP_EID0]; + *ext = 1; + } +} + +/********************************************************************************************************* +** Function name: mcp2515_write_canMsg +** Descriptions: Write message +*********************************************************************************************************/ +void MCP_CAN::mcp2515_write_canMsg( const INT8U buffer_sidh_addr) +{ + INT8U mcp_addr; + mcp_addr = buffer_sidh_addr; + mcp2515_setRegisterS(mcp_addr+5, m_nDta, m_nDlc ); /* write data bytes */ + + if ( m_nRtr == 1) /* if RTR set bit in byte */ + m_nDlc |= MCP_RTR_MASK; + + mcp2515_setRegister((mcp_addr+4), m_nDlc ); /* write the RTR and DLC */ + mcp2515_write_id(mcp_addr, m_nExtFlg, m_nID ); /* write CAN id */ + +} + +/********************************************************************************************************* +** Function name: mcp2515_read_canMsg +** Descriptions: Read message +*********************************************************************************************************/ +void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read can msg */ +{ + INT8U mcp_addr, ctrl; + + mcp_addr = buffer_sidh_addr; + + mcp2515_read_id( mcp_addr, &m_nExtFlg,&m_nID ); + + ctrl = mcp2515_readRegister( mcp_addr-1 ); + m_nDlc = mcp2515_readRegister( mcp_addr+4 ); + + if (ctrl & 0x08) + m_nRtr = 1; + else + m_nRtr = 0; + + m_nDlc &= MCP_DLC_MASK; + mcp2515_readRegisterS( mcp_addr+5, &(m_nDta[0]), m_nDlc ); +} + +/********************************************************************************************************* +** Function name: mcp2515_getNextFreeTXBuf +** Descriptions: Send message +*********************************************************************************************************/ +INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get Next free txbuf */ +{ + INT8U res, i, ctrlval; + INT8U ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL }; + + res = MCP_ALLTXBUSY; + *txbuf_n = 0x00; + + /* check all 3 TX-Buffers */ + for (i=0; i 0){ +#if DEBUG_MODE + Serial.print("Entering Configuration Mode Failure...\r\n"); +#endif + return res; + } + + if (num == 0){ + mcp2515_write_mf(MCP_RXM0SIDH, ext, ulData); + + } + else if(num == 1){ + mcp2515_write_mf(MCP_RXM1SIDH, ext, ulData); + } + else res = MCP2515_FAIL; + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res > 0){ +#if DEBUG_MODE + Serial.print("Entering Previous Mode Failure...\r\nSetting Mask Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Mask Successful!\r\n"); +#endif + return res; +} + +/********************************************************************************************************* +** Function name: init_Mask +** Descriptions: Public function to set mask(s). +*********************************************************************************************************/ +INT8U MCP_CAN::init_Mask(INT8U num, INT32U ulData) +{ + INT8U res = MCP2515_OK; + INT8U ext = 0; +#if DEBUG_MODE + Serial.print("Starting to Set Mask!\r\n"); +#endif + res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); + if(res > 0){ +#if DEBUG_MODE + Serial.print("Entering Configuration Mode Failure...\r\n"); +#endif + return res; +} + + if((ulData & 0x80000000) == 0x80000000) + ext = 1; + + if (num == 0){ + mcp2515_write_mf(MCP_RXM0SIDH, ext, ulData); + + } + else if(num == 1){ + mcp2515_write_mf(MCP_RXM1SIDH, ext, ulData); + } + else res = MCP2515_FAIL; + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res > 0){ +#if DEBUG_MODE + Serial.print("Entering Previous Mode Failure...\r\nSetting Mask Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Mask Successful!\r\n"); +#endif + return res; +} + +/********************************************************************************************************* +** Function name: init_Filt +** Descriptions: Public function to set filter(s). +*********************************************************************************************************/ +INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) +{ + INT8U res = MCP2515_OK; +#if DEBUG_MODE + Serial.print("Starting to Set Filter!\r\n"); +#endif + res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Enter Configuration Mode Failure...\r\n"); +#endif + return res; + } + + switch( num ) + { + case 0: + mcp2515_write_mf(MCP_RXF0SIDH, ext, ulData); + break; + + case 1: + mcp2515_write_mf(MCP_RXF1SIDH, ext, ulData); + break; + + case 2: + mcp2515_write_mf(MCP_RXF2SIDH, ext, ulData); + break; + + case 3: + mcp2515_write_mf(MCP_RXF3SIDH, ext, ulData); + break; + + case 4: + mcp2515_write_mf(MCP_RXF4SIDH, ext, ulData); + break; + + case 5: + mcp2515_write_mf(MCP_RXF5SIDH, ext, ulData); + break; + + default: + res = MCP2515_FAIL; + } + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Entering Previous Mode Failure...\r\nSetting Filter Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Filter Successfull!\r\n"); +#endif + + return res; +} + +/********************************************************************************************************* +** Function name: init_Filt +** Descriptions: Public function to set filter(s). +*********************************************************************************************************/ +INT8U MCP_CAN::init_Filt(INT8U num, INT32U ulData) +{ + INT8U res = MCP2515_OK; + INT8U ext = 0; + +#if DEBUG_MODE + Serial.print("Starting to Set Filter!\r\n"); +#endif + res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Enter Configuration Mode Failure...\r\n"); +#endif + return res; + } + + if((ulData & 0x80000000) == 0x80000000) + ext = 1; + + switch( num ) + { + case 0: + mcp2515_write_mf(MCP_RXF0SIDH, ext, ulData); + break; + + case 1: + mcp2515_write_mf(MCP_RXF1SIDH, ext, ulData); + break; + + case 2: + mcp2515_write_mf(MCP_RXF2SIDH, ext, ulData); + break; + + case 3: + mcp2515_write_mf(MCP_RXF3SIDH, ext, ulData); + break; + + case 4: + mcp2515_write_mf(MCP_RXF4SIDH, ext, ulData); + break; + + case 5: + mcp2515_write_mf(MCP_RXF5SIDH, ext, ulData); + break; + + default: + res = MCP2515_FAIL; + } + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Entering Previous Mode Failure...\r\nSetting Filter Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Filter Successfull!\r\n"); +#endif + + return res; +} + +/********************************************************************************************************* +** Function name: setMsg +** Descriptions: Set can message, such as dlc, id, dta[] and so on +*********************************************************************************************************/ +INT8U MCP_CAN::setMsg(INT32U id, INT8U rtr, INT8U ext, INT8U len, INT8U *pData) +{ + int i = 0; + m_nID = id; + m_nRtr = rtr; + m_nExtFlg = ext; + m_nDlc = len; + for(i = 0; i> 3); +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ diff --git a/src/mcp_can.h b/src/mcp_can.h new file mode 100644 index 0000000..51f2e18 --- /dev/null +++ b/src/mcp_can.h @@ -0,0 +1,133 @@ +/* + mcp_can.h + 2012 Copyright (c) Seeed Technology Inc. All right reserved. + 2017 Copyright (c) Cory J. Fowler All Rights Reserved. + + Author:Loovee + Contributor: Cory J. Fowler + 2017-09-25 + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110- + 1301 USA +*/ +#ifndef _MCP2515_H_ +#define _MCP2515_H_ + +#include "mcp_can_dfs.h" +#define MAX_CHAR_IN_MESSAGE 8 + +class MCP_CAN +{ + private: + + INT8U m_nExtFlg; // Identifier Type + // Extended (29 bit) or Standard (11 bit) + INT32U m_nID; // CAN ID + INT8U m_nDlc; // Data Length Code + INT8U m_nDta[MAX_CHAR_IN_MESSAGE]; // Data array + INT8U m_nRtr; // Remote request flag + INT8U m_nfilhit; // The number of the filter that matched the message + INT8U MCPCS; // Chip Select pin number + INT8U mcpMode; // Mode to return to after configurations are performed. + + +/********************************************************************************************************* + * mcp2515 driver function + *********************************************************************************************************/ + // private: + private: + + void mcp2515_reset(void); // Soft Reset MCP2515 + + INT8U mcp2515_readRegister(const INT8U address); // Read MCP2515 register + + void mcp2515_readRegisterS(const INT8U address, // Read MCP2515 successive registers + INT8U values[], + const INT8U n); + + void mcp2515_setRegister(const INT8U address, // Set MCP2515 register + const INT8U value); + + void mcp2515_setRegisterS(const INT8U address, // Set MCP2515 successive registers + const INT8U values[], + const INT8U n); + + void mcp2515_initCANBuffers(void); + + void mcp2515_modifyRegister(const INT8U address, // Set specific bit(s) of a register + const INT8U mask, + const INT8U data); + + INT8U mcp2515_readStatus(void); // Read MCP2515 Status + INT8U mcp2515_setCANCTRL_Mode(const INT8U newmode); // Set mode + INT8U mcp2515_configRate(const INT8U canSpeed, // Set baud rate + const INT8U canClock); + + INT8U mcp2515_init(const INT8U canIDMode, // Initialize Controller + const INT8U canSpeed, + const INT8U canClock); + + void mcp2515_write_mf( const INT8U mcp_addr, // Write CAN Mask or Filter + const INT8U ext, + const INT32U id ); + + void mcp2515_write_id( const INT8U mcp_addr, // Write CAN ID + const INT8U ext, + const INT32U id ); + + void mcp2515_read_id( const INT8U mcp_addr, // Read CAN ID + INT8U* ext, + INT32U* id ); + + void mcp2515_write_canMsg( const INT8U buffer_sidh_addr ); // Write CAN message + void mcp2515_read_canMsg( const INT8U buffer_sidh_addr); // Read CAN message + INT8U mcp2515_getNextFreeTXBuf(INT8U *txbuf_n); // Find empty transmit buffer + +/********************************************************************************************************* + * CAN operator function + *********************************************************************************************************/ + + INT8U setMsg(INT32U id, INT8U rtr, INT8U ext, INT8U len, INT8U *pData); // Set message + INT8U clearMsg(); // Clear all message to zero + INT8U readMsg(); // Read message + INT8U sendMsg(); // Send message + +public: + MCP_CAN(INT8U _CS); + INT8U begin(INT8U idmodeset, INT8U speedset, INT8U clockset); // Initialize controller parameters + INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData); // Initialize Mask(s) + INT8U init_Mask(INT8U num, INT32U ulData); // Initialize Mask(s) + INT8U init_Filt(INT8U num, INT8U ext, INT32U ulData); // Initialize Filter(s) + INT8U init_Filt(INT8U num, INT32U ulData); // Initialize Filter(s) + INT8U setMode(INT8U opMode); // Set operational mode + INT8U sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf); // Send message to transmit buffer + INT8U sendMsgBuf(INT32U id, INT8U len, INT8U *buf); // Send message to transmit buffer + INT8U readMsgBuf(INT32U *id, INT8U *ext, INT8U *len, INT8U *buf); // Read message from receive buffer + INT8U readMsgBuf(INT32U *id, INT8U *len, INT8U *buf); // Read message from receive buffer + INT8U checkReceive(void); // Check for received data + INT8U checkError(void); // Check for errors + INT8U getError(void); // Check for errors + INT8U errorCountRX(void); // Get error count + INT8U errorCountTX(void); // Get error count + INT8U enOneShotTX(void); // Enable one-shot transmission + INT8U disOneShotTX(void); // Disable one-shot transmission + INT8U abortTX(void); // Abort queued transmission(s) + INT8U setGPO(INT8U data); // Sets GPO + INT8U getGPI(void); // Reads GPI +}; + +#endif +/********************************************************************************************************* + * END FILE + *********************************************************************************************************/ diff --git a/src/mcp_can_dfs.h b/src/mcp_can_dfs.h new file mode 100644 index 0000000..51576cc --- /dev/null +++ b/src/mcp_can_dfs.h @@ -0,0 +1,476 @@ +/* + mcp_can_dfs.h + 2012 Copyright (c) Seeed Technology Inc. All right reserved. + 2017 Copyright (c) Cory J. Fowler All Rights Reserved. + + Author:Loovee + Contributor: Cory J. Fowler + 2017-09-25 + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110- + 1301 USA +*/ +#ifndef _MCP2515DFS_H_ +#define _MCP2515DFS_H_ + +#include +#include +#include + +#ifndef INT32U +#define INT32U unsigned long +#endif + +#ifndef INT8U +#define INT8U byte +#endif + +// if print debug information +#define DEBUG_MODE 0 + +/* + * Begin mt + */ +#define TIMEOUTVALUE 50 +#define MCP_SIDH 0 +#define MCP_SIDL 1 +#define MCP_EID8 2 +#define MCP_EID0 3 + +#define MCP_TXB_EXIDE_M 0x08 /* In TXBnSIDL */ +#define MCP_DLC_MASK 0x0F /* 4 LSBits */ +#define MCP_RTR_MASK 0x40 /* (1<<6) Bit 6 */ + +#define MCP_RXB_RX_ANY 0x60 +#define MCP_RXB_RX_EXT 0x40 +#define MCP_RXB_RX_STD 0x20 +#define MCP_RXB_RX_STDEXT 0x00 +#define MCP_RXB_RX_MASK 0x60 +#define MCP_RXB_BUKT_MASK (1<<2) + +/* +** Bits in the TXBnCTRL registers. +*/ +#define MCP_TXB_TXBUFE_M 0x80 +#define MCP_TXB_ABTF_M 0x40 +#define MCP_TXB_MLOA_M 0x20 +#define MCP_TXB_TXERR_M 0x10 +#define MCP_TXB_TXREQ_M 0x08 +#define MCP_TXB_TXIE_M 0x04 +#define MCP_TXB_TXP10_M 0x03 + +#define MCP_TXB_RTR_M 0x40 /* In TXBnDLC */ +#define MCP_RXB_IDE_M 0x08 /* In RXBnSIDL */ +#define MCP_RXB_RTR_M 0x40 /* In RXBnDLC */ + +#define MCP_STAT_RXIF_MASK (0x03) +#define MCP_STAT_RX0IF (1<<0) +#define MCP_STAT_RX1IF (1<<1) + +#define MCP_EFLG_RX1OVR (1<<7) +#define MCP_EFLG_RX0OVR (1<<6) +#define MCP_EFLG_TXBO (1<<5) +#define MCP_EFLG_TXEP (1<<4) +#define MCP_EFLG_RXEP (1<<3) +#define MCP_EFLG_TXWAR (1<<2) +#define MCP_EFLG_RXWAR (1<<1) +#define MCP_EFLG_EWARN (1<<0) +#define MCP_EFLG_ERRORMASK (0xF8) /* 5 MS-Bits */ + +#define MCP_BxBFS_MASK 0x30 +#define MCP_BxBFE_MASK 0x0C +#define MCP_BxBFM_MASK 0x03 + +#define MCP_BxRTS_MASK 0x38 +#define MCP_BxRTSM_MASK 0x07 + +/* + * Define MCP2515 register addresses + */ +#define MCP_RXF0SIDH 0x00 +#define MCP_RXF0SIDL 0x01 +#define MCP_RXF0EID8 0x02 +#define MCP_RXF0EID0 0x03 +#define MCP_RXF1SIDH 0x04 +#define MCP_RXF1SIDL 0x05 +#define MCP_RXF1EID8 0x06 +#define MCP_RXF1EID0 0x07 +#define MCP_RXF2SIDH 0x08 +#define MCP_RXF2SIDL 0x09 +#define MCP_RXF2EID8 0x0A +#define MCP_RXF2EID0 0x0B +#define MCP_BFPCTRL 0x0C +#define MCP_TXRTSCTRL 0x0D +#define MCP_CANSTAT 0x0E +#define MCP_CANCTRL 0x0F +#define MCP_RXF3SIDH 0x10 +#define MCP_RXF3SIDL 0x11 +#define MCP_RXF3EID8 0x12 +#define MCP_RXF3EID0 0x13 +#define MCP_RXF4SIDH 0x14 +#define MCP_RXF4SIDL 0x15 +#define MCP_RXF4EID8 0x16 +#define MCP_RXF4EID0 0x17 +#define MCP_RXF5SIDH 0x18 +#define MCP_RXF5SIDL 0x19 +#define MCP_RXF5EID8 0x1A +#define MCP_RXF5EID0 0x1B +#define MCP_TEC 0x1C +#define MCP_REC 0x1D +#define MCP_RXM0SIDH 0x20 +#define MCP_RXM0SIDL 0x21 +#define MCP_RXM0EID8 0x22 +#define MCP_RXM0EID0 0x23 +#define MCP_RXM1SIDH 0x24 +#define MCP_RXM1SIDL 0x25 +#define MCP_RXM1EID8 0x26 +#define MCP_RXM1EID0 0x27 +#define MCP_CNF3 0x28 +#define MCP_CNF2 0x29 +#define MCP_CNF1 0x2A +#define MCP_CANINTE 0x2B +#define MCP_CANINTF 0x2C +#define MCP_EFLG 0x2D +#define MCP_TXB0CTRL 0x30 +#define MCP_TXB1CTRL 0x40 +#define MCP_TXB2CTRL 0x50 +#define MCP_RXB0CTRL 0x60 +#define MCP_RXB0SIDH 0x61 +#define MCP_RXB1CTRL 0x70 +#define MCP_RXB1SIDH 0x71 + + +#define MCP_TX_INT 0x1C /* Enable all transmit interrup ts */ +#define MCP_TX01_INT 0x0C /* Enable TXB0 and TXB1 interru pts */ +#define MCP_RX_INT 0x03 /* Enable receive interrupts */ +#define MCP_NO_INT 0x00 /* Disable all interrupts */ + +#define MCP_TX01_MASK 0x14 +#define MCP_TX_MASK 0x54 + +/* + * Define SPI Instruction Set + */ +#define MCP_WRITE 0x02 + +#define MCP_READ 0x03 + +#define MCP_BITMOD 0x05 + +#define MCP_LOAD_TX0 0x40 +#define MCP_LOAD_TX1 0x42 +#define MCP_LOAD_TX2 0x44 + +#define MCP_RTS_TX0 0x81 +#define MCP_RTS_TX1 0x82 +#define MCP_RTS_TX2 0x84 +#define MCP_RTS_ALL 0x87 + +#define MCP_READ_RX0 0x90 +#define MCP_READ_RX1 0x94 + +#define MCP_READ_STATUS 0xA0 + +#define MCP_RX_STATUS 0xB0 + +#define MCP_RESET 0xC0 + + +/* + * CANCTRL Register Values + */ +#define MCP_NORMAL 0x00 +#define MCP_SLEEP 0x20 +#define MCP_LOOPBACK 0x40 +#define MCP_LISTENONLY 0x60 +#define MODE_CONFIG 0x80 +#define MODE_POWERUP 0xE0 +#define MODE_MASK 0xE0 +#define ABORT_TX 0x10 +#define MODE_ONESHOT 0x08 +#define CLKOUT_ENABLE 0x04 +#define CLKOUT_DISABLE 0x00 +#define CLKOUT_PS1 0x00 +#define CLKOUT_PS2 0x01 +#define CLKOUT_PS4 0x02 +#define CLKOUT_PS8 0x03 + + +/* + * CNF1 Register Values + */ +#define SJW1 0x00 +#define SJW2 0x40 +#define SJW3 0x80 +#define SJW4 0xC0 + + +/* + * CNF2 Register Values + */ +#define BTLMODE 0x80 +#define SAMPLE_1X 0x00 +#define SAMPLE_3X 0x40 + + +/* + * CNF3 Register Values + */ +#define SOF_ENABLE 0x80 +#define SOF_DISABLE 0x00 +#define WAKFIL_ENABLE 0x40 +#define WAKFIL_DISABLE 0x00 + + +/* + * CANINTF Register Bits + */ +#define MCP_RX0IF 0x01 +#define MCP_RX1IF 0x02 +#define MCP_TX0IF 0x04 +#define MCP_TX1IF 0x08 +#define MCP_TX2IF 0x10 +#define MCP_ERRIF 0x20 +#define MCP_WAKIF 0x40 +#define MCP_MERRF 0x80 + + +/* + * Speed 8M + */ +#define MCP_8MHz_1000kBPS_CFG1 (0x00) +#define MCP_8MHz_1000kBPS_CFG2 (0xC0) /* Enabled SAM bit */ +#define MCP_8MHz_1000kBPS_CFG3 (0x80) /* Sample point at 75% */ + +#define MCP_8MHz_500kBPS_CFG1 (0x00) +#define MCP_8MHz_500kBPS_CFG2 (0xD1) /* Enabled SAM bit */ +#define MCP_8MHz_500kBPS_CFG3 (0x81) /* Sample point at 75% */ + +#define MCP_8MHz_250kBPS_CFG1 (0x80) /* Increased SJW */ +#define MCP_8MHz_250kBPS_CFG2 (0xE5) /* Enabled SAM bit */ +#define MCP_8MHz_250kBPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_8MHz_200kBPS_CFG1 (0x80) /* Increased SJW */ +#define MCP_8MHz_200kBPS_CFG2 (0xF6) /* Enabled SAM bit */ +#define MCP_8MHz_200kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_8MHz_125kBPS_CFG1 (0x81) /* Increased SJW */ +#define MCP_8MHz_125kBPS_CFG2 (0xE5) /* Enabled SAM bit */ +#define MCP_8MHz_125kBPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_8MHz_100kBPS_CFG1 (0x81) /* Increased SJW */ +#define MCP_8MHz_100kBPS_CFG2 (0xF6) /* Enabled SAM bit */ +#define MCP_8MHz_100kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_8MHz_80kBPS_CFG1 (0x84) /* Increased SJW */ +#define MCP_8MHz_80kBPS_CFG2 (0xD3) /* Enabled SAM bit */ +#define MCP_8MHz_80kBPS_CFG3 (0x81) /* Sample point at 75% */ + +#define MCP_8MHz_50kBPS_CFG1 (0x84) /* Increased SJW */ +#define MCP_8MHz_50kBPS_CFG2 (0xE5) /* Enabled SAM bit */ +#define MCP_8MHz_50kBPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_8MHz_40kBPS_CFG1 (0x84) /* Increased SJW */ +#define MCP_8MHz_40kBPS_CFG2 (0xF6) /* Enabled SAM bit */ +#define MCP_8MHz_40kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_8MHz_33k3BPS_CFG1 (0x85) /* Increased SJW */ +#define MCP_8MHz_33k3BPS_CFG2 (0xF6) /* Enabled SAM bit */ +#define MCP_8MHz_33k3BPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_8MHz_31k25BPS_CFG1 (0x87) /* Increased SJW */ +#define MCP_8MHz_31k25BPS_CFG2 (0xE5) /* Enabled SAM bit */ +#define MCP_8MHz_31k25BPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_8MHz_20kBPS_CFG1 (0x89) /* Increased SJW */ +#define MCP_8MHz_20kBPS_CFG2 (0xF6) /* Enabled SAM bit */ +#define MCP_8MHz_20kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_8MHz_10kBPS_CFG1 (0x93) /* Increased SJW */ +#define MCP_8MHz_10kBPS_CFG2 (0xF6) /* Enabled SAM bit */ +#define MCP_8MHz_10kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_8MHz_5kBPS_CFG1 (0xA7) /* Increased SJW */ +#define MCP_8MHz_5kBPS_CFG2 (0xF6) /* Enabled SAM bit */ +#define MCP_8MHz_5kBPS_CFG3 (0x84) /* Sample point at 75% */ + +/* + * speed 16M + */ +#define MCP_16MHz_1000kBPS_CFG1 (0x00) +#define MCP_16MHz_1000kBPS_CFG2 (0xCA) +#define MCP_16MHz_1000kBPS_CFG3 (0x81) /* Sample point at 75% */ + +#define MCP_16MHz_500kBPS_CFG1 (0x40) /* Increased SJW */ +#define MCP_16MHz_500kBPS_CFG2 (0xE5) +#define MCP_16MHz_500kBPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_16MHz_250kBPS_CFG1 (0x41) +#define MCP_16MHz_250kBPS_CFG2 (0xE5) +#define MCP_16MHz_250kBPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_16MHz_200kBPS_CFG1 (0x41) /* Increased SJW */ +#define MCP_16MHz_200kBPS_CFG2 (0xF6) +#define MCP_16MHz_200kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_16MHz_125kBPS_CFG1 (0x43) /* Increased SJW */ +#define MCP_16MHz_125kBPS_CFG2 (0xE5) +#define MCP_16MHz_125kBPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_16MHz_100kBPS_CFG1 (0x44) /* Increased SJW */ +#define MCP_16MHz_100kBPS_CFG2 (0xE5) +#define MCP_16MHz_100kBPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_16MHz_80kBPS_CFG1 (0x44) /* Increased SJW */ +#define MCP_16MHz_80kBPS_CFG2 (0xF6) +#define MCP_16MHz_80kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_16MHz_50kBPS_CFG1 (0x47) /* Increased SJW */ +#define MCP_16MHz_50kBPS_CFG2 (0xF6) +#define MCP_16MHz_50kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_16MHz_40kBPS_CFG1 (0x49) /* Increased SJW */ +#define MCP_16MHz_40kBPS_CFG2 (0xF6) +#define MCP_16MHz_40kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_16MHz_33k3BPS_CFG1 (0x4E) +#define MCP_16MHz_33k3BPS_CFG2 (0xE5) +#define MCP_16MHz_33k3BPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_16MHz_20kBPS_CFG1 (0x53) /* Increased SJW */ +#define MCP_16MHz_20kBPS_CFG2 (0xF6) +#define MCP_16MHz_20kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_16MHz_10kBPS_CFG1 (0x67) /* Increased SJW */ +#define MCP_16MHz_10kBPS_CFG2 (0xF6) +#define MCP_16MHz_10kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_16MHz_5kBPS_CFG1 (0x3F) +#define MCP_16MHz_5kBPS_CFG2 (0xFF) +#define MCP_16MHz_5kBPS_CFG3 (0x87) /* Sample point at 68% */ + +/* + * speed 20M + */ +#define MCP_20MHz_1000kBPS_CFG1 (0x00) +#define MCP_20MHz_1000kBPS_CFG2 (0xD9) +#define MCP_20MHz_1000kBPS_CFG3 (0x82) /* Sample point at 80% */ + +#define MCP_20MHz_500kBPS_CFG1 (0x40) /* Increased SJW */ +#define MCP_20MHz_500kBPS_CFG2 (0xF6) +#define MCP_20MHz_500kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_20MHz_250kBPS_CFG1 (0x41) /* Increased SJW */ +#define MCP_20MHz_250kBPS_CFG2 (0xF6) +#define MCP_20MHz_250kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_20MHz_200kBPS_CFG1 (0x44) /* Increased SJW */ +#define MCP_20MHz_200kBPS_CFG2 (0xD3) +#define MCP_20MHz_200kBPS_CFG3 (0x81) /* Sample point at 80% */ + +#define MCP_20MHz_125kBPS_CFG1 (0x44) /* Increased SJW */ +#define MCP_20MHz_125kBPS_CFG2 (0xE5) +#define MCP_20MHz_125kBPS_CFG3 (0x83) /* Sample point at 75% */ + +#define MCP_20MHz_100kBPS_CFG1 (0x44) /* Increased SJW */ +#define MCP_20MHz_100kBPS_CFG2 (0xF6) +#define MCP_20MHz_100kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_20MHz_80kBPS_CFG1 (0xC4) /* Increased SJW */ +#define MCP_20MHz_80kBPS_CFG2 (0xFF) +#define MCP_20MHz_80kBPS_CFG3 (0x87) /* Sample point at 68% */ + +#define MCP_20MHz_50kBPS_CFG1 (0x49) /* Increased SJW */ +#define MCP_20MHz_50kBPS_CFG2 (0xF6) +#define MCP_20MHz_50kBPS_CFG3 (0x84) /* Sample point at 75% */ + +#define MCP_20MHz_40kBPS_CFG1 (0x18) +#define MCP_20MHz_40kBPS_CFG2 (0xD3) +#define MCP_20MHz_40kBPS_CFG3 (0x81) /* Sample point at 80% */ + + +#define MCPDEBUG (0) +#define MCPDEBUG_TXBUF (0) +#define MCP_N_TXBUFFERS (3) + +#define MCP_RXBUF_0 (MCP_RXB0SIDH) +#define MCP_RXBUF_1 (MCP_RXB1SIDH) + +#define MCP2515_SELECT() digitalWrite(MCPCS, LOW) +#define MCP2515_UNSELECT() digitalWrite(MCPCS, HIGH) + +#define MCP2515_OK (0) +#define MCP2515_FAIL (1) +#define MCP_ALLTXBUSY (2) + +#define CANDEBUG 1 + +#define CANUSELOOP 0 + +#define CANSENDTIMEOUT (200) /* milliseconds */ + +/* + * initial value of gCANAutoProcess + */ +#define CANAUTOPROCESS (1) +#define CANAUTOON (1) +#define CANAUTOOFF (0) + +#define CAN_STDID (0) +#define CAN_EXTID (1) + +#define CANDEFAULTIDENT (0x55CC) +#define CANDEFAULTIDENTEXT (CAN_EXTID) + +#define MCP_STDEXT 0 /* Standard and Extended */ +#define MCP_STD 1 /* Standard IDs ONLY */ +#define MCP_EXT 2 /* Extended IDs ONLY */ +#define MCP_ANY 3 /* Disables Masks and Filters */ + +#define MCP_20MHZ 0 +#define MCP_16MHZ 1 +#define MCP_8MHZ 2 + +#define CAN_4K096BPS 0 +#define CAN_5KBPS 1 +#define CAN_10KBPS 2 +#define CAN_20KBPS 3 +#define CAN_31K25BPS 4 +#define CAN_33K3BPS 5 +#define CAN_40KBPS 6 +#define CAN_50KBPS 7 +#define CAN_80KBPS 8 +#define CAN_100KBPS 9 +#define CAN_125KBPS 10 +#define CAN_200KBPS 11 +#define CAN_250KBPS 12 +#define CAN_500KBPS 13 +#define CAN_1000KBPS 14 + +#define CAN_OK (0) +#define CAN_FAILINIT (1) +#define CAN_FAILTX (2) +#define CAN_MSGAVAIL (3) +#define CAN_NOMSG (4) +#define CAN_CTRLERROR (5) +#define CAN_GETTXBFTIMEOUT (6) +#define CAN_SENDMSGTIMEOUT (7) +#define CAN_FAIL (0xff) + +#define CAN_MAX_CHAR_IN_MESSAGE (8) + +#endif +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..45ceb32 --- /dev/null +++ b/src/types.h @@ -0,0 +1,193 @@ +/* + Type.h + + Author : Luxrobo Arduino Team + Edited : Tuna (Tuna@luxrobo.com) +*/ + +#ifndef MODI_TYPES_H_ +#define MODI_TYPES_H_ + +#include +#include + +#include +#include +#include + +typedef int Direction; +enum +{ + DIRECTION_UNKNOWN = -1, + DIRECTION_RIGHT, + DIRECTION_TOP, + DIRECTION_LEFT, + DIRECTION_BOTTOM, + DIRECTION_WIRLESS_OFFSET, +}; + +typedef uint16_t ModuleKey; +typedef uint16_t ModuleVersion; +typedef uint16_t ModuleType; + +enum +{ + MODULE_TYPE_NETWORK = 0x0000, + MODULE_TYPE_INPUT = 0x2000, + MODULE_TYPE_OUTPUT = 0x4000, + + MODULE_USB = (0x00) | MODULE_TYPE_NETWORK, + MODULE_ESP = (0x10) | MODULE_TYPE_NETWORK, + //MODULE_ARDUINO = (0x20) | MODULE_TYPE_NETWORK, // To Be Defined + MODULE_ENVIORNMENT = (0x00) | MODULE_TYPE_INPUT, + MODULE_GYRO = (0x10) | MODULE_TYPE_INPUT, + MODULE_MIC = (0x20) | MODULE_TYPE_INPUT, + MODULE_BUTTON = (0x30) | MODULE_TYPE_INPUT, + MODULE_DIAL = (0x40) | MODULE_TYPE_INPUT, + MODULE_ULTRASONIC = (0x50) | MODULE_TYPE_INPUT, + MODULE_IR = (0x60) | MODULE_TYPE_INPUT, + MODULE_DISPLAY = (0x10) | MODULE_TYPE_OUTPUT, + MODULE_MOTOR = (0x20) | MODULE_TYPE_OUTPUT, + MODULE_LED = (0x30) | MODULE_TYPE_OUTPUT, + MODULE_SPEAKER = (0x40) | MODULE_TYPE_OUTPUT, +}; + +template +bool isValidKey(ModuleKey key) +{ + return !(key & 0xF000); +} + +enum +{ + // FIRST = 0x00, + CMD_HEALTH = 0x00, + CMD_ECHO = 0x01, + CMD_MODULE_INFO = 0x02, + CMD_REQUEST_PP = 0x03, + CMD_SET_PP = 0x04, + CMD_ASSIGN_ID = 0x05, + CMD_ASSIGN_ACTION = 0x06, + CMD_TOPOLOGY = 0x07, + CMD_FIND_ID = 0x08, + CMD_SET_STATE = 0x09, + CMD_WARNING = 0x0A, + CMD_FIRMWARE_DATA = 0x0B, + CMD_FIRMWARE_STATE = 0x0C, + CMD_FIRMWARE_COMMAND = 0x0D, + CMD_SAVE = 0x0E, + CMD_STREAM_DATA = 0x10, + CMD_STREAM_STATE = 0x11, + CMD_STREAM_COMMAND = 0x12, + CMD_MONITOR = 0x13, + CMD_BATTERY = 0x14, + CMD_VARIABLE = 0x1C, + CMD_SYNC = 0x1D, + CMD_EVENT = 0x1E, + CMD_CHANNEL = 0x1F, + CMD_EXT_REMODED = 0x20, + // LAST = 0xFF, +}modi_cmd_type_t; + +enum +{ + DATA_F64 = 0x00, + DATA_F32 = 0x01, + DATA_F32_F32 = 0x02, + DATA_U64 = 0x03, + DATA_I64 = 0x04, + DATA_U32 = 0x05, + DATA_U32_U32 = 0x06, + DATA_I32 = 0x07, + DATA_I32_I32 = 0x08, + DATA_U16 = 0x09, + DATA_U16_U16 = 0x0A, + DATA_U16_U16_U16 = 0x0B, + DATA_U16_U16_U16_U16 = 0x0C, + DATA_I16 = 0x0C, + DATA_I16_I16 = 0x0D, + DATA_I16_I16_I16 = 0x0E, + DATA_I16_I16_I16_I16 = 0x0F, +}modi_data_type_t; + +typedef struct +{ + uint64_t value[20]; +}modi_property_t; + +struct Frame +{ + uint16_t cmd; + uint16_t sid; + uint16_t did; + uint16_t len; + uint8_t data[8]; +}; + + +class Message +{ +public: + Message(){} + Message(const Frame& frame): m_frame(frame){} + Message(int cmd, int sid, int did, const uint8_t* data, int len) + { + m_frame.cmd = (uint16_t)cmd; + m_frame.sid = (uint16_t)sid; + m_frame.did = (uint16_t)did; + m_frame.len = (uint16_t)len; + memset (m_frame.data, 0, sizeof(m_frame.data)); + for (int i = 0; i < len; i ++) + m_frame.data[i] = data[i]; + } + +public: + uint16_t cmd() { return m_frame.cmd; } + uint16_t sid() { return m_frame.sid; } + uint16_t did() { return m_frame.did; } + uint16_t len() { return m_frame.len; } + uint8_t* data() { return m_frame.data;} + +private: + Frame m_frame; +}; + +typedef int32_t AppMode; + +enum +{ + APP_MODE_NONE = -1, + APP_MODE_PNP = 2, + APP_MODE_USER = 3, +}; + + +typedef union +{ + uint8_t u8[4]; + uint16_t u16[2]; + uint32_t u32; + int8_t i8[4]; + int16_t i16[2]; + int32_t i32; + float f32; +}size32_val_t; + +typedef union +{ + uint8_t u8[8]; + uint16_t u16[4]; + uint32_t u32[2]; + uint64_t u64; + int8_t i8[8]; + int16_t i16[4]; + int32_t i32[2]; + int64_t i64; + + float f32[2]; + double f64; +}size64_val_t; + +#endif // MODI_TYPES_H_ + +/* End of File */