Keresés

Új hozzászólás Aktív témák

  • Janos250

    őstag

    válasz DigitXT #11889 üzenetére

    "az Arduino-féle String implementáció egy nagy sz"
    "nem igazán ajánlott használni."
    Így igaz. A nagybetűs String valóban gyengécske, de azt is mondjuk meg, hogy mit érdemes használni:
    a kisbetűs string viszont jó.
    Ismét mondom, hogy nem ötegapám UNO, NANO, MEGA lapja az, amit érdemes használni, hanem valami korszerűbb,
    ami - mondhatni - nem drágább, de sokkal többet tud, és az UNO-ra, stb.-re írt programok símán áttehetők rá,
    ha nincs benne valami nagyon hardware közeli dolog.
    Mint már többször mondtam, én mostanság az ESP32-t használom.
    5 dollárért megvehető postával együtt.
    https://www.banggood.com/Geekcreit-30-Pin-ESP32-Development-Board-WiFibluetooth-Ultra-Low-Power-Consumption-Dual-Cores-ESP-32-ESP-32S-Board-p-1461896.html?rmmds=search&cur_warehouse=CN
    Vagy az UNO mintájára:
    https://www.banggood.com/LILYGO-TTGO-ESP32-WiFi-bluetooth-Board-4MB-Flash-UNO-D1-R32-Development-Board-p-1163967.html?rmmds=search&cur_warehouse=CN
    Megnéztem a régi Arduino String osztályát. Bizony elég sok tagfüggvény nincs benne, ami a C++ -ban benne van. Példának az appendet vettem,
    hogy a részletesebb infók netről megtekinthetők legyenek.
    Ha már itt tartottam, még pár - szintén netről szedett - példát is beleraktam.
    A mostani C++11 számos olyan dolgot is tartalmaz, amit az ősrégiek nem.
    Használhatjuk az alábbiakat:
    #include <array>
    #include <atomic>
    #include <chrono>
    #include <condition_variable>
    #include <forward_list>
    #include <future>
    #include <initializer_list>
    #include <mutex>
    #include <random>
    #include <ratio>
    #include <regex>
    #include <scoped_allocator>
    #include <system_error>
    #include <thread>
    #include <tuple>
    #include <typeindex>
    #include <type_traits>
    #include <unordered_map>
    #include <unordered_set>
    Ezek közül is raktam be példákat. Hogy miért jobb pl. az array, a chrono, a threadhasználata? Mert ez szabványos, bárhol ugyanígy fut, nem olyasmit tanulunk meg, amit máshol nem tudunk használni.
    Íme:
    Eredetileg ez egyetlen program lenne, de PH szerkesztője valamiért darabokra szedte.
    #define __cplusplus  201103L
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <array>
    #include <iterator>
    #include <chrono>
    #include <ctime>
    #include <thread>
    //ESP32 Dev Module
    void setup() {
     
    //http://www.cplusplus.com/reference/string/string/append/
      std::string str;
      std::string str2="Writing ";
      std::string str3="print 10 and then 5 more";
      // used in the same order as described above:
      str.append(str2);                       // "Writing "
      str.append(str3,6,3);                   // "10 "
      str.append("dots are cool",5);          // "dots "
      str.append("here: ");                   // "here: "
      str.append(10u,'.');                    // ".........."
      str.append(str3.begin()+8,str3.end());  // " and then 5 more"
    //  str.append<int>(5,0x2E);                // "....."
      std::cout << str << '\n';

    /*
    //A String (nagy S) a regi Arduino string kezelese
    // ebben nincs append, forditasi hibat jelez
      String xstr;
      String xstr2="Writing ";
      String xstr3="print 10 and then 5 more";
      // used in the same order as described above:
      xstr.append(xstr2);                       // "Writing "
      xstr.append(xstr3,6,3);                   // "10 "
      xstr.append("dots are cool",5);          // "dots "
      xstr.append("here: ");                   // "here: "
      xstr.append(10u,'.');                    // ".........."
      xstr.append(xstr3.begin()+8,xstr3.end());  // " and then 5 more"
    */

    //Ez is egy netrol vett pelda
    //https://stackoverflow.com/questions/2066184/how-to-use-c-string-streams-to-append-int
        std::stringstream stream("Something ");
        stream.seekp(0, std::ios::end);
        stream << 12345;
        std::cout << stream.str();
        

    // C++11 array
    //https://en.cppreference.com/w/cpp/container/array
        // construction uses aggregate initialization
        std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 prior to the CWG 1270 revision
                                            // (not needed in C++11 after the revision and in C++14 and beyond)
        std::array<int, 3> a2 = {1, 2, 3};  // never required after =
        std::array<std::string, 2> a3 = { std::string("a"), "b" };
     
        // container operations are supported
        std::sort(a1.begin(), a1.end());
        std::reverse_copy(a2.begin(), a2.end(), 
                          std::ostream_iterator<int>(std::cout, " "));
     
        std::cout << '\n';
     
        // ranged for loop is supported
        for(const auto& s: a3)
            std::cout << s << ' ';

    // C++11 chrono
    //https://en.cppreference.com/w/cpp/chrono
         auto start = std::chrono::system_clock::now();
        std::cout << "f(42) = " << fibonacci(42) << '\n';
        auto end = std::chrono::system_clock::now();
     
        std::chrono::duration<double> elapsed_seconds = end-start;
        std::time_t end_time = std::chrono::system_clock::to_time_t(end);
     
        std::cout << "finished computation at " << std::ctime(&end_time)
                  << "elapsed time: " << elapsed_seconds.count() << "s\n";

    // C++11  thread
    //https://en.cppreference.com/w/cpp/thread
    //https://thispointer.com/c-11-multithreading-part-1-three-different-ways-to-create-threads/
        std::thread threadObj1(thread_function);
        std::thread threadObj2(thread_function);
     
        if(threadObj1.get_id() != threadObj2.get_id())
            std::cout<<"Both Threads have different IDs"<<std::endl;
     
            std::cout<<"From Main Thread :: ID of Thread 1 = "<<threadObj1.get_id()<<std::endl;    
        std::cout<<"From Main Thread :: ID of Thread 2 = "<<threadObj2.get_id()<<std::endl;    
     
        threadObj1.join();    
        threadObj2.join(); 

    }
    void loop() {
    }
    // mert a chrono minta hasznalja
    long fibonacci(unsigned n)
    {
        if (n < 2) return n;
        return fibonacci(n-1) + fibonacci(n-2);
    }
    //// mert a thread minta hasznalja
    void thread_function()
    {
        std::cout<<"Inside Thread :: ID  = "<<std::this_thread::get_id()<<std::endl;    
    }

    [ Szerkesztve ]

    Az amerikaiak $ milliókért fejlesztettek golyóstollat űrbéli használatra. Az oroszok ceruzát használnak. Én meg arduinot.

Új hozzászólás Aktív témák