Advertisement
S11as

esp32_lab_2

May 8th, 2024
608
0
168 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <vector>
  3. #include <string>
  4. #include "BluetoothSerial.h"
  5.  
  6. // https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
  7.  
  8. class Actor {
  9. public:
  10.     int actPin;
  11.     Actor(int pin): actPin (pin) {}
  12.     virtual void act() {}
  13. };
  14.  
  15. class Clicker {
  16. private:
  17.     int clickerPin;
  18.     std::vector<Actor*> actors;
  19.     bool enabled;
  20. public:
  21.     Clicker(int pin) {
  22.         this->clickerPin = pin;
  23.         this->enabled = true;
  24.     }
  25.    
  26.     bool isClicked() {
  27.         return digitalRead(clickerPin) == LOW;
  28.     }
  29.    
  30.     void addActor(Actor* newActor) {
  31.         this->actors.push_back(newActor);
  32.     }
  33.  
  34.     void watchClicks() {
  35.         Serial.println("watching clicks");
  36.         if (this->isClicked()) {
  37.             for (Actor* actor: this->actors) {
  38.                 actor->act();
  39.             }
  40.         }
  41.     }
  42.  
  43.     void enable() {
  44.         this->enabled = true;
  45.     }
  46.  
  47.     void disable() {
  48.         this->enabled = false;
  49.     }
  50. };
  51.  
  52. class SoundDevice: public Actor {
  53. public:
  54.     SoundDevice(int pin): Actor (pin) { }
  55.  
  56.     void playSound(int del) {
  57.         digitalWrite(this->actPin, HIGH);
  58.         delay(del);
  59.         digitalWrite(this->actPin, LOW);
  60.     }
  61.  
  62.     void playLoudSound(int del) {
  63.         int arr[10] = {25, 50, 25, 50, 25, 50, 25, 50, 25, 50};
  64.         for (int i = 0; i<10; i++) {
  65.             analogWrite(this->actPin, arr[i]);
  66.             delay((int)(del/2));
  67.             analogWrite(this->actPin, 1);
  68.             delay( (int)(del/2));
  69.         }
  70.     }
  71.  
  72.     void act() override {
  73.         Serial.println("sound device acting");
  74.         playSound(150);
  75.     }
  76. };
  77.  
  78.  
  79. class MessageController {
  80. private:
  81.     BluetoothSerial serial;
  82.     Clicker* clicker;
  83.     SoundDevice* soundDevice;
  84.     std::string message;
  85. public:
  86.     MessageController(BluetoothSerial s,
  87.                         Clicker* clicker,
  88.                         SoundDevice* soundDevice):
  89.                         clicker(clicker), soundDevice(soundDevice) {
  90.         message = "";
  91.         this->serial = s;
  92.         this->serial.begin("ESP32Kirill");
  93.     }
  94.  
  95.     void parseCommand() {
  96.         if (this->serial.available()){
  97.             char incomingChar = this->serial.read();
  98.             if (incomingChar != '\n'){
  99.                 this->message += String(incomingChar);
  100.             } else {
  101.                 this->message = "";
  102.             }
  103.             Serial.write("Got char: ");
  104.             Serial.write(incomingChar);
  105.         }
  106.  
  107.         if (message =="buzz") {
  108.             this->soundDevice->playSound(150);
  109.         }
  110.         else if (message =="clicker_on") {
  111.             digitalWrite(ledPin, LOW);
  112.         }
  113.     }
  114. }
  115.  
  116. uint64_t count = 0;
  117. Clicker* clicker;
  118. SoundDevice* soundDevice;
  119. BluetoothSerial SerialBT;
  120. MessageController* messageController;
  121.  
  122. void setup() {
  123.     Serial.begin(115200);
  124.     int clickerPin = 4;
  125.     int soundPin = 13;
  126.     int soundLen = 300;
  127.     pinMode(clickerPin, INPUT);
  128.     pinMode(soundPin, OUTPUT);
  129.  
  130.     clicker = new Clicker(clickerPin);
  131.     soundDevice = new SoundDevice(soundPin);
  132.  
  133.     clicker->addActor(soundDevice);
  134.  
  135.     messageController = new MessageController(
  136.         SerialBT,
  137.         clicker,
  138.         soundDevice,
  139.     );
  140.     Serial.println("Setup finished");
  141. }
  142.  
  143. void loop() {
  144.     messageController->parseCommand();
  145.     clicker->watchClicks();
  146.  
  147.     delay(100)
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement