// #include <iostream>
// using namespace std;
struct Smartphone {
String name; // stejné jméno jako v jiné struktuře ale je zcela jiná proměnná
int storageSpace;
String color;
float price;
};
struct Person {
String name; // stejné jméno jako v jiné struktuře ale je zcela jiná proměnná
int age;
Smartphone smartphone;
};
void printSmartphoneInfo(Smartphone smartphone) { // vypisuje volanou structuru smartphone
Serial.print("phone name: ");
Serial.println(smartphone.name);
Serial.print("storageSpace: ");
Serial.println(smartphone.storageSpace);
Serial.print("color: ");
Serial.println(smartphone.color);
Serial.print("price: ");
Serial.println(smartphone.price);
}
void printPersonInfo(Person person) { // vypisuje volanou structuru person
Serial.print("person name: ");
Serial.println(person.name);
Serial.print("age: ");
Serial.println(person.age);
printSmartphoneInfo(person.smartphone);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello from ESP32, Structures for beginners");
}
void loop()
{
delay(10); // this speeds up the simulation
Serial.println("");
Smartphone smartphone; // vytvoření Struct smartphone a její naplnění
smartphone.name= "iPhone 12";
smartphone.storageSpace = 32;
smartphone.color = "black";
smartphone.price = 999.99;
Smartphone smartphone2; // vytvoření Struct smartphone2 a její naplnění
smartphone2.name = "Samsung Galaxy S21 Ultra";
smartphone2.storageSpace = 64;
smartphone2.color = "gray";
smartphone2.price = 888.88;
/*
printSmartphoneInfo(smartphone); // tisk obsahu smartphone
printSmartphoneInfo(smartphone2); // tisk obsahu smartphone2
*/
Person p; // vytvoření Struct p a její naplnění
p.name = "Saldina";
p.age = 26;
p.smartphone = smartphone;
Person p2; // vytvoření Struct p a její naplnění
p2.name = "CodeBeauty";
p2.age = 5;
p2.smartphone = smartphone2;
printPersonInfo(p);
Serial.println("");
printPersonInfo(p2);
Serial.println(" ... phone onetime replacement");
// přiřazení jiného Smartphone, v dalším cyklu se opět nastaví jako smartphone
p.smartphone = smartphone2;
printPersonInfo(p);
delay(1000);
}