forked from IELS1001-23-24/programming-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "FiretruckSim.h" | ||
|
||
const int BOOM_UP_PICKER_PIN = 15; | ||
const int BOOM_DOWN_PICKER_PIN = 16; | ||
const int BOOM_ELEVATION_PIN = 18; | ||
|
||
const int BOOM_LEFT_PICKER_PIN = 19; | ||
const int BOOM_RIGHT_PICKER_PIN = 20; | ||
const int BOOM_ROTATION_PIN = 22; | ||
|
||
const int BOOM_ELBOW_OUT_PICKER_PIN = 23; | ||
const int BOOM_ELBOW_IN_PICKER_PIN = 24; | ||
const int BOOM_ELBOW_PIN = 25; | ||
|
||
const int BOOM_WRIST_PIN = 28; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
} | ||
|
||
void loop() | ||
{ | ||
Serial.println("Hello World!"); | ||
delay(1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
#include <Arduino.h> | ||
#include <ArduinoJson.h> | ||
|
||
// All the pinMode payloads that have | ||
// been sent to the simulator will be stored here. | ||
// If the simulator is reset, it will request the pinModes | ||
// and this string will be used. | ||
String pinModes = ""; | ||
|
||
byte pinValues[100]; | ||
|
||
void syncPinsWithSim() | ||
{ | ||
while (Serial.available() > 0) | ||
{ | ||
DynamicJsonDocument jsonDoc(500); | ||
DeserializationError error = deserializeJson(jsonDoc, Serial); | ||
|
||
if (error != DeserializationError::Ok) | ||
{ | ||
Serial.print("deserializeJson() failed: "); | ||
Serial.println(error.c_str()); | ||
return; | ||
} | ||
|
||
if (jsonDoc.containsKey("p")) | ||
{ | ||
int pin = jsonDoc["p"].as<int>(); | ||
int value = jsonDoc["v"].as<int>(); | ||
pinValues[pin] = value; | ||
} | ||
|
||
if (jsonDoc.containsKey("init")) | ||
{ | ||
// Send the pinModes to the simulator | ||
Serial.print(pinModes); | ||
|
||
// Parse all the json messages in the pinModes string | ||
// and send the pin values for all outputs | ||
// to the simulator. | ||
|
||
String buffer = pinModes; | ||
while (buffer.length() > 0) | ||
{ | ||
DynamicJsonDocument jsonDoc(50); | ||
DeserializationError error = deserializeJson(jsonDoc, buffer); | ||
|
||
// Remove the json message from the buffer | ||
buffer = buffer.substring(buffer.indexOf("}") + 1); | ||
|
||
if (error != DeserializationError::Ok) | ||
{ | ||
Serial.print("deserializeJson() failed: "); | ||
Serial.println(error.c_str()); | ||
continue; | ||
} | ||
|
||
if (jsonDoc.containsKey("m")) | ||
{ | ||
int pin = jsonDoc["p"].as<int>(); | ||
String mode = jsonDoc["m"].as<String>(); | ||
|
||
if (mode == "o") | ||
{ | ||
// Send the pin value to the simulator | ||
DynamicJsonDocument jsonDoc(100); | ||
jsonDoc["p"] = pin; | ||
jsonDoc["v"] = pinValues[pin]; | ||
serializeJson(jsonDoc, Serial); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
int digitalReadSim(uint8_t pin) | ||
{ | ||
syncPinsWithSim(); | ||
return pinValues[pin]; | ||
} | ||
|
||
int analogReadSim(uint8_t pin) | ||
{ | ||
syncPinsWithSim(); | ||
return pinValues[pin]; | ||
} | ||
|
||
void analogWriteSim(uint8_t pin, int val) | ||
{ | ||
syncPinsWithSim(); | ||
|
||
if (val == pinValues[pin]) | ||
{ | ||
return; | ||
} | ||
|
||
pinValues[pin] = val; | ||
|
||
DynamicJsonDocument jsonDoc(50); | ||
|
||
jsonDoc["p"] = pin; | ||
jsonDoc["v"] = val; | ||
|
||
serializeJson(jsonDoc, Serial); | ||
syncPinsWithSim(); | ||
} | ||
|
||
void digitalWriteSim(uint8_t pin, uint8_t val) | ||
{ | ||
analogWriteSim(pin, val); | ||
} | ||
|
||
void pinModeSim(uint8_t pin, uint8_t mode) | ||
{ | ||
syncPinsWithSim(); | ||
|
||
DynamicJsonDocument jsonDoc(50); | ||
|
||
jsonDoc["m"] = (mode == OUTPUT) ? "o" : "i"; | ||
jsonDoc["p"] = pin; | ||
|
||
serializeJson(jsonDoc, Serial); | ||
|
||
// Store the payload in a string for later use | ||
serializeJson(jsonDoc, pinModes); | ||
|
||
syncPinsWithSim(); | ||
} | ||
|
||
#define digitalRead digitalReadSim | ||
#define analogRead analogReadSim | ||
#define digitalWrite digitalWriteSim | ||
#define analogWrite analogWriteSim | ||
|
||
#define pinMode pinModeSim | ||
|
||
size_t printSim(const String &s) | ||
{ | ||
syncPinsWithSim(); | ||
|
||
DynamicJsonDocument jsonDoc(500); | ||
jsonDoc["print"] = s; | ||
|
||
serializeJson(jsonDoc, Serial); | ||
} | ||
|
||
size_t printlnSim(const String &s) | ||
{ | ||
return printSim(s + '\n'); | ||
} | ||
|
||
class HardwareSerialSim | ||
{ | ||
public: | ||
void begin(unsigned long baudrate) | ||
{ | ||
Serial.begin(baudrate); | ||
} | ||
|
||
size_t print(const String &s) | ||
{ | ||
syncPinsWithSim(); | ||
|
||
DynamicJsonDocument jsonDoc(500); | ||
jsonDoc["print"] = s; | ||
|
||
return serializeJson(jsonDoc, Serial); | ||
} | ||
|
||
size_t print(int &s) | ||
{ | ||
return print(String(s)); | ||
} | ||
|
||
size_t println(const String &s) | ||
{ | ||
return print(s + '\n'); | ||
} | ||
|
||
size_t println(int &s) | ||
{ | ||
return println(String(s)); | ||
} | ||
}; | ||
|
||
HardwareSerialSim SerialSim; | ||
|
||
#define Serial SerialSim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[<- Tilbake](/README.md#arbeidskrav) | ||
|
||
# Firetruck Sim: Arm | ||
|
||
Få armen til å bevege seg basert på kontrollpanelet. | ||
|
||
- [Simulering](https://firetruck-sim.vercel.app/) | ||
- [Kode](Boom.ino) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"version": 1, | ||
"author": "wokwi", | ||
"editor": "wokwi", | ||
"parts": [ | ||
{ | ||
"type": "wokwi-arduino-uno", | ||
"id": "uno", | ||
"top": 0, | ||
"left": 0, | ||
"attrs": {} | ||
} | ||
], | ||
"connections": [], | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ArduinoJson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[wokwi] | ||
version = 1 | ||
firmware = "../../../.arduino/output/Boom.ino.hex" | ||
elf = "../../../.arduino/output/Boom.ino.elf" | ||
rfc2217ServerPort = 4_000 | ||
webSocketServerPort = 4_001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "FiretruckSim.h" | ||
|
||
const int HEADLIGHTS_PICKER_PIN = 7; | ||
const int HEADLIGHTS_PIN = 8; | ||
|
||
const int FLASH_LEFT_PICKER_PIN = 9; | ||
const int FLASH_LEFT_PIN = 10; | ||
|
||
const int FLASH_RIGHT_PICKER_PIN = 11; | ||
const int FLASH_RIGHT_PIN = 12; | ||
|
||
const int WIPER_PICKER_PIN = 13; | ||
const int WIPER_PIN = 14; | ||
|
||
const int BOOM_UP_PICKER_PIN = 15; | ||
const int BOOM_DOWN_PICKER_PIN = 16; | ||
const int BOOM_ELEVATION_PIN = 18; | ||
|
||
const int BOOM_LEFT_PICKER_PIN = 19; | ||
const int BOOM_RIGHT_PICKER_PIN = 20; | ||
const int BOOM_ROTATION_PIN = 22; | ||
|
||
const int BOOM_ELBOW_OUT_PICKER_PIN = 23; | ||
const int BOOM_ELBOW_IN_PICKER_PIN = 24; | ||
const int BOOM_ELBOW_PIN = 25; | ||
|
||
const int BOOM_WRIST_PIN = 28; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
} | ||
|
||
void loop() | ||
{ | ||
Serial.println("Hello World!"); | ||
delay(1000); | ||
} |
Oops, something went wrong.