- Labornetzteil AliExpress    Werbung      
Ergebnis 1 bis 10 von 35

Thema: std::thread für ESP32 unter Arduino IDE zum Laufen zu kriegen...?

Hybrid-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #1
    HaWe
    Gast
    update: der Code jetzt hier scheint besser zu laufen:



    Code:
    > counter_loop: 0
    > blinker_loop (HIGH) counter: 0
    > main loop: 1
    > blinker_loop (LOW) counter: 0
    > counter_loop: 1
    > counter_loop: 2
    > blinker_loop (HIGH) counter: 1
    > blinker_loop (LOW) counter: 1
    > counter_loop: 3
    > counter_loop: 4
    > blinker_loop (HIGH) counter: 2
    > blinker_loop (LOW) counter: 2
    > counter_loop: 5
    > counter_loop: 6
    > blinker_loop (HIGH) counter: 3
    > blinker_loop (LOW) counter: 3
    > counter_loop: 7
    > counter_loop: 8
    > blinker_loop (HIGH) counter: 4
    > blinker_loop (LOW) counter: 4
    > counter_loop: 9
    > counter_loop: 10
    > blinker_loop (HIGH) counter: 5
    > main loop: 2
    > blinker_loop (LOW) counter: 5
    > counter_loop: 11
    > counter_loop: 12
    > blinker_loop (HIGH) counter: 6
    > blinker_loop (LOW) counter: 6
    > counter_loop: 13
    > counter_loop: 14
    > blinker_loop (HIGH) counter: 7
    > blinker_loop (LOW) counter: 7
    > counter_loop: 15
    > counter_loop: 16
    > blinker_loop (HIGH) counter: 8
    > blinker_loop (LOW) counter: 8
    > counter_loop: 17
    > counter_loop: 18
    > blinker_loop (HIGH) counter: 9
    > blinker_loop (LOW) counter: 9
    > counter_loop: 19
    > counter_loop: 20
    > blinker_loop (HIGH) counter: 10
    > main loop: 3
    > blinker_loop (LOW) counter: 10
    code:

    ```
    Code:
    // std::thread for ESP32, Arduino IDE
    
    #include <Arduino.h>
    #include <thread>
    #include <chrono>
    
    #ifndef LED_BUILTIN
    #define LED_BUILTIN 13
    #endif
    
    const auto one_sec = std::chrono::seconds
    {
        1
    };
    
    void counter_loop() {
        thread_local uint32_t counter = 0;
        while(true) {
            Serial.print("counter_loop: ");
            Serial.println(counter);
            std::this_thread::sleep_for(one_sec);
            counter++;
        }
    }
    
    void blinker_loop() {
        thread_local uint32_t counter = 0;
        while(true) {
            digitalWrite(LED_BUILTIN, HIGH);
            Serial.print("blinker_loop (HIGH) counter: ");
            Serial.println(counter);
            std::this_thread::sleep_for(one_sec);
            
            digitalWrite(LED_BUILTIN, LOW);
            Serial.print("blinker_loop (LOW) counter: ");
            Serial.println(counter);
            std::this_thread::sleep_for(one_sec);
            counter++;
        }
    }
    
    
    std::unique_ptr<std::thread>  counter_loop_thread;
    std::unique_ptr<std::thread>  blinker_loop_thread;
    
    void setup() {
      Serial.begin(115200);
      //debug
      delay(2000);
      
      counter_loop_thread.reset(new std::thread(counter_loop));
      blinker_loop_thread.reset(new std::thread(blinker_loop));
    }
    
    
    uint32_t main_loop_counter = 0;
    void loop() {
        main_loop_counter++;
        Serial.print("main loop: ");
        Serial.println(main_loop_counter);
        delay(10000);
    }
    ```

    - - - Aktualisiert - - -

    update2:
    gleich gutes Ergebnis bei dieser Code-Implementierung:
    Code:
    std::thread *counter_loop_thread;
    std::thread *blinker_loop_thread;
    
    
    void setup() {
      Serial.begin(115200);
      //debug
      delay(2000);
      counter_loop_thread = new std::thread(counter_loop);
      blinker_loop_thread = new std::thread(blinker_loop);
    }
    Geändert von HaWe (24.05.2019 um 16:18 Uhr)

  2. #2
    shedepe
    Gast
    Was du suchst ist ein std::mutex (in der Hoffnung dass der implementiert wurde). Ein Mutex ist eine Synchronisierungsobjekt zwischen Threads.
    D.h. du brauchst in etwas sowas:

    Code:
    std::mutex m_lock;
    
    void myWrite(std::string str)
    {
        m_lock.lock();
        Serial.print(str);
        m_lock.unlock();
    }

  3. #3
    HaWe
    Gast
    Zitat Zitat von shedepe Beitrag anzeigen
    Was du suchst ist ein std::mutex (in der Hoffnung dass der implementiert wurde). Ein Mutex ist eine Synchronisierungsobjekt zwischen Threads.
    D.h. du brauchst in etwas sowas:

    Code:
    std::mutex m_lock;
    
    void myWrite(std::string str)
    {
        m_lock.lock();
        Serial.print(str);
        m_lock.unlock();
    }
    danke für den wertvollen Tipp, das ist sehr hilfreich!
    Für den Augenblick geht es ja auch ohne (s. Serial logs), aber wenn es mal zeitkritischer wird: auf jeden Fall dann!
    Danke!

    Ich gucke später auch mal, ob der tatsächlich implementiert wurde!

    PS:
    auch für dich fände ich es klasse, wenn du dir mal ne portable IDE installieren könntest! Bild  

Ähnliche Themen

  1. Esp32 a2dp in arduino IDE
    Von Flos6323 im Forum Elektronik
    Antworten: 0
    Letzter Beitrag: 27.06.2018, 15:28
  2. Installation des Arduino Core für ESP32 ohne GIT
    Von mischaka im Forum NodeMCU-Board und ESP8266, ESP32-Serie
    Antworten: 0
    Letzter Beitrag: 26.04.2018, 07:20
  3. Arduino Cinque: RISC-V-Prozessor und ESP32 auf einem Board vereint
    Von Roboternetz-News im Forum Neuigkeiten / Technik-News / Nachrichten / Aktuelles
    Antworten: 1
    Letzter Beitrag: 22.05.2017, 16:29
  4. Display für esp32?
    Von NotEvil im Forum NodeMCU-Board und ESP8266, ESP32-Serie
    Antworten: 7
    Letzter Beitrag: 04.12.2016, 16:37
  5. Kugelgelenke - woher kriegen?
    Von Gottfreak im Forum Mechanik
    Antworten: 15
    Letzter Beitrag: 04.01.2005, 17:56

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

    Werbung      12V Akku bauen