hwcurses.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <algorithm>
  2. #include <chrono>
  3. #include <cstdio>
  4. #include <ctime>
  5. #include <functional>
  6. #include <iostream>
  7. #include <memory>
  8. #include <ncurses.h>
  9. #include <string>
  10. #include <thread>
  11. #include <unistd.h>
  12. #include <vector>
  13. /*
  14. todo
  15. figure out namespaces, using, and auto
  16. formalize timer struct
  17. read about std function and std bind
  18. try to do with objects / classes instead
  19. centralize into event sender and subscriber
  20. */
  21. /*
  22. find a way to eject timer
  23. find a way to group timers into an object
  24. */
  25. struct mysecondsdisplay {
  26. int intervalcount = 10;
  27. void myupdatedisplay() {
  28. intervalcount -= 1;
  29. mvprintw(intervalcount, intervalcount, "hello %d", intervalcount);
  30. if (intervalcount < 1) {
  31. intervalcount = 10;
  32. }
  33. }
  34. };
  35. // calls callbackfunc on specified interval based on accumulated deltatime
  36. class timer {
  37. public:
  38. std::string name;
  39. std::function<void()> callbackfunc;
  40. timer(std::string name, size_t interval_input, std::function<void()> callback)
  41. : name(name), callbackfunc(callback), time_interval(interval_input) {}
  42. // callbackfunc once enough time has accumulated
  43. void updatetime(double DeltaTime) {
  44. accumulator += DeltaTime;
  45. if (accumulator >= time_interval) {
  46. accumulator -= time_interval;
  47. callbackfunc();
  48. }
  49. }
  50. private:
  51. double accumulator = 0;
  52. double time_interval = 1; // only display every 1 seconds
  53. };
  54. // manage collection of timers
  55. class timermanager {
  56. std::vector<std::unique_ptr<timer>> timers;
  57. public:
  58. void addtimer(std::unique_ptr<timer> input_timer) {
  59. timers.push_back(std::move(input_timer)); }
  60. void updatetimers(double dtime) {
  61. for (auto &mytimer : timers) {
  62. mytimer->updatetime(dtime);
  63. mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
  64. };
  65. refresh();
  66. }
  67. };
  68. int main() {
  69. initscr();
  70. nodelay(stdscr, FALSE);
  71. refresh();
  72. // printf("verification\n");
  73. auto starttime = std::chrono::system_clock::now(); // initial starttime
  74. mysecondsdisplay secdisp;
  75. auto clocktimer = std::make_unique<timer>("secondsprinter", 1, [&]() { secdisp.myupdatedisplay(); });
  76. timermanager mytimermanager;
  77. mytimermanager.addtimer(std::move(clocktimer));
  78. mvprintw(5, 5, "timer added");
  79. // calculate delta between current and previous time
  80. while (true) {
  81. auto currenttime = std::chrono::system_clock::now();
  82. std::chrono::duration<double> myDeltaTime = currenttime - starttime;
  83. double dtime = myDeltaTime.count();
  84. // mvprintw(11, 11, "dtime: %f", dtime);
  85. // clocktimer.updatetime(dtime);
  86. mytimermanager.updatetimers(dtime);
  87. starttime = currenttime;
  88. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  89. }
  90. getch();
  91. endwin();
  92. return 0;
  93. }