Skip to content

Commit

Permalink
Arduino msg with ArduinoJson
Browse files Browse the repository at this point in the history
  • Loading branch information
wenoptics committed Oct 4, 2019
1 parent cc88730 commit fa7c5be
Show file tree
Hide file tree
Showing 8 changed files with 965 additions and 1,020 deletions.
2 changes: 1 addition & 1 deletion .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"board": "arduino:avr:mega",
"configuration": "cpu=atmega2560",
"port": "/dev/cu.usbmodem14401",
"port": "/dev/cu.usbmodem14201",
"sketch": "Arduino/OdysseyConController/OdysseyConController.ino"
}
154 changes: 45 additions & 109 deletions Arduino/OdysseyConController/OdysseyConController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,130 +8,66 @@
// * 9-24-19 - Unity adoption - Grayson Wen
// * 10-4-19 - Communication protocol; Code refactoring; - Grayson Wen
/////////////////////////////////////////////////////////////////////////////////////////

#include "OdysseyHardware.h"
#include "Sound.h"

#include <ArduinoJson.h>

float update_period = 1.0 / 120.0;

void setup() {
Serial.begin(115200);
Serial.begin(115200);

// Initialize for sound
if (! init_audio() ) {
Serial.println("Audio initialization fail (SD card failed).");
}
// Initialize for sound
if (!init_audio()) {
Serial.println("Audio initialization fail (SD card failed).");
}

// Init P1 and P2 pins, all other spots are implicitly read only
init_player_as_reading(&p1_spot);
init_player_as_reading(&p2_spot);
// Init P1 and P2 pins, all other spots are implicitly read only
init_player_as_reading(&p1_spot);
init_player_as_reading(&p2_spot);
}

// Change the size as needed. A handy link for this: https://arduinojson.org/v6/assistant/
const size_t msgCapacity = JSON_OBJECT_SIZE(9);
DynamicJsonDocument doc(msgCapacity);
int tmp_x, tmp_y;

void loop() {

// Read/Write the player data accordingly
if ( p1_spot.writing ) {

} else {

}

// Construct one piece of JSON message

read_player_position(&p1_spot, &tmp_x, &tmp_y);
Serial.print("{");
Serial.print("\"P1_X_READ\": ");
Serial.print(tmp_x);
Serial.print(", ");

Serial.print("\"P1_Y_READ\": ");
Serial.print(tmp_y);
Serial.print(", ");

// Serial.print("\"P1_ENG_READ\": ");
// Serial.print(0); //todo
// Serial.print(", ");

// Serial.print("\"P1_RESET_READ\": ");
// Serial.print(read_player_reset(&p1_spot));
// Serial.print(", ");

read_player_position(&p2_spot, &tmp_x, &tmp_y);
Serial.print("\"P2_X_READ\": ");
Serial.print(tmp_x);
Serial.print(", ");

Serial.print("\"P2_Y_READ\": ");
Serial.print(tmp_y);
Serial.print(", ");

// Serial.print("\"P2_ENG_READ\": ");
// Serial.print(0); //todo
// Serial.print(", ");

// Serial.print("\"P2_RESET_READ\": ");
// Serial.print(read_player_reset(&p2_spot));
// Serial.print(", ");

read_player_position(&ball_spot, &tmp_x, &tmp_y);
Serial.print("\"BALL_X_READ\": ");
Serial.print(tmp_x);
Serial.print(", ");

Serial.print("\"BALL_Y_READ\": ");
Serial.print(tmp_y);
Serial.print(", ");

// Serial.print("\"BALL_SPEED_READ\": ");
// Serial.print(1); // todo
// Serial.print(", ");

read_player_position(&wall_spot, &tmp_x, &tmp_y);
Serial.print("\"WALL_X_READ\": ");
Serial.print(tmp_x);
// Serial.print(", ");

// Serial.print("\"SELECT_READ\": ");
// Serial.print(0);
// Serial.print(", ");

// Serial.print("\"ENTER_READ\": ");
// Serial.print(0);
// Serial.print(", ");

// Serial.print("\"CROWBAR_READ\": ");
// Serial.print(0);
// Serial.print(", ");

// Serial.print("\"CROWBAR_RESET_READ\": ");
// Serial.print(0);
// Serial.print(", ");

// Serial.print("\"ENCODER_READ\": ");
// Serial.print(0);

Serial.print("}");
Serial.println();

// #define READ_SPOT_PRINT(x) read_player_position(spot, &tmp_x, &tmp_y);
// READ_SPOT_PRINT(&p1_spot)
// READ_SPOT_PRINT(&p2_spot)
// READ_SPOT_PRINT(&p3_spot)
// READ_SPOT_PRINT(&p4_spot)
// READ_SPOT_PRINT(&ball_spot)
// READ_SPOT_PRINT(&wall_spot)
// #define PRINT_DIGITAL(x) Serial.print(#x ": "); Serial.print(x); Serial.print(" ");
// PRINT_DIGITAL(READ_CROWBAR)
// PRINT_DIGITAL(READ_ENGLISH_FLIP_FLOP)
// PRINT_DIGITAL(READ_BALL_FLIP_FLOP)
// PRINT_DIGITAL(read_player_reset(&p1_spot))
// PRINT_DIGITAL(read_player_reset(&p2_spot))
// Serial.println();
// Read/Write the player data accordingly
if (p1_spot.writing) {

} else {

}

// Construct one piece of JSON message
read_player_position(&p1_spot, &tmp_x, &tmp_y);
doc["P1_X_READ"] = tmp_x;
doc["P1_Y_READ"] = tmp_y;

read_player_position(&p2_spot, &tmp_x, &tmp_y);
doc["P2_X_READ"] = tmp_x;
doc["P2_Y_READ"] = tmp_y;

read_player_position(&ball_spot, &tmp_x, &tmp_y);
doc["BALL_X_READ"] = tmp_x;
doc["BALL_Y_READ"] = tmp_y;

read_player_position(&wall_spot, &tmp_x, &tmp_y);
doc["WALL_X_READ"] = tmp_x;

doc["P1_RESET_READ"] = read_player_reset(&p1_spot);
doc["P2_RESET_READ"] = read_player_reset(&p2_spot);

// Write json message to serial (->Unity3D)
serializeJson(doc, Serial);
Serial.println();

serve_audio();
serve_english();

/* // Enter characters in serial monitor to call different functions
if(Serial.available() >= 2){
char cmd = Serial.read();
Expand Down Expand Up @@ -166,5 +102,5 @@ void loop() {

// TODO Replace with true rate limiting
delay(update_period * 1000);

}
8 changes: 8 additions & 0 deletions Arduino/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#Arduino Part

## Dependencies

- todo
- todo


84 changes: 42 additions & 42 deletions OdysseyNow/Assets/Scenes/Calibration.unity
Original file line number Diff line number Diff line change
Expand Up @@ -3677,36 +3677,6 @@ MeshFilter:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1723398820}
m_Mesh: {fileID: 4300000, guid: 620f774168b2d2e41825625cc708ec2d, type: 3}
--- !u!1 &1747015791
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1747015792}
m_Layer: 0
m_Name: GameObject
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1747015792
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1747015791}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 5.9, y: 2, z: -21.8}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1761942359
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -3896,7 +3866,7 @@ MonoBehaviour:
m_RotationOrder: 4
camera3DScene: {fileID: 531728315}
cameraInGame: {fileID: 517545431}
enableCalibrationScene: 0
enableCalibrationScene: 1
textLeft: {fileID: 2069608262}
textRight: {fileID: 1327718453}
textDBP1: {fileID: 1002244835}
Expand Down Expand Up @@ -5168,6 +5138,11 @@ PrefabInstance:
propertyPath: m_CullingMask.m_Bits
value: 55
objectReference: {fileID: 0}
- target: {fileID: 5755280646457188808, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3641995673445440782, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: p1Console
Expand Down Expand Up @@ -5311,57 +5286,62 @@ PrefabInstance:
- target: {fileID: 5755280645911995611, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMax.x
value: 0
value: 0.5
objectReference: {fileID: 0}
- target: {fileID: 5755280645911995611, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5755280646469173685, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMin.x
value: 0
value: 0.5
objectReference: {fileID: 0}
- target: {fileID: 5755280646469173685, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMax.x
value: 0
value: 0.5
objectReference: {fileID: 0}
- target: {fileID: 5755280646469173685, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5755280646924010412, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMax.x
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5755280646924010412, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5755280645387203077, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMin.x
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5755280645387203077, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMax.x
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5755280645387203077, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5755280645187453073, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: cardNumber
value: 1
value: -1
objectReference: {fileID: 0}
- target: {fileID: 5755280645187453073, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: paused
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5755280646830917910, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
Expand Down Expand Up @@ -5573,9 +5553,29 @@ PrefabInstance:
propertyPath: speed
value: 1.36
objectReference: {fileID: 0}
- target: {fileID: 5755280645081009836, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 867101119943316239, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 214443216580657863, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3699030722969737623, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2744987576373696785, guid: 6335a3f5d84d6421eb04d6f447784f85,
type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
Expand Down
Loading

0 comments on commit fa7c5be

Please sign in to comment.