33// license information.
44
55#include " Avatar.h"
6+
7+ #ifndef PI
8+ #define PI 3.1415926535897932384626433832795
9+ #endif
10+
611namespace m5avatar {
712
813unsigned int seed = 0 ;
914
15+ #ifndef rand_r
16+ #define init_rand () srand(seed)
17+ #define _rand () rand()
18+ #else
19+ #define init_rand () ;
20+ #define _rand () rand_r(&seed)
21+ #endif
22+
23+ #ifdef SDL_h_
24+ #define TaskResult () return 0
25+ #define TaskDelay (ms ) lgfx::delay(ms)
26+ long random (long howbig) {
27+ return std::rand () % howbig;
28+ }
29+ #else
30+ #define TaskResult () vTaskDelete(NULL )
31+ #define TaskDelay (ms ) vTaskDelay(ms/portTICK_PERIOD_MS)
32+ #endif
33+
1034// TODO(meganetaaan): make read-only
1135DriveContext::DriveContext (Avatar *avatar) : avatar{avatar} {}
1236
1337Avatar *DriveContext::getAvatar () { return avatar; }
1438
1539TaskHandle_t drawTaskHandle;
1640
17- void drawLoop (void *args) {
41+ TaskResult_t drawLoop (void *args) {
1842 DriveContext *ctx = reinterpret_cast <DriveContext *>(args);
1943 Avatar *avatar = ctx->getAvatar ();
2044 while (avatar->isDrawing ()) {
2145 if (avatar->isDrawing ()) {
2246 avatar->draw ();
2347 }
24- vTaskDelay (10 /portTICK_PERIOD_MS );
48+ TaskDelay (10 );
2549 }
26- vTaskDelete ( NULL );
50+ TaskResult ( );
2751}
2852
29- void facialLoop (void *args) {
53+ TaskResult_t facialLoop (void *args) {
3054 int c = 0 ;
3155 DriveContext *ctx = reinterpret_cast <DriveContext *>(args);
3256 Avatar *avatar = ctx->getAvatar ();
@@ -38,17 +62,18 @@ void facialLoop(void *args) {
3862 float vertical = 0 .0f ;
3963 float horizontal = 0 .0f ;
4064 float breath = 0 .0f ;
65+ init_rand ();
4166 while (avatar->isDrawing ()) {
4267
43- if ((millis () - last_saccade_millis) > saccade_interval) {
44- vertical = rand_r (&seed ) / (RAND_MAX / 2.0 ) - 1 ;
45- horizontal = rand_r (&seed ) / (RAND_MAX / 2.0 ) - 1 ;
68+ if ((lgfx:: millis () - last_saccade_millis) > saccade_interval) {
69+ vertical = _rand ( ) / (RAND_MAX / 2.0 ) - 1 ;
70+ horizontal = _rand ( ) / (RAND_MAX / 2.0 ) - 1 ;
4671 avatar->setGaze (vertical, horizontal);
4772 saccade_interval = 500 + 100 * random (20 );
48- last_saccade_millis = millis ();
73+ last_saccade_millis = lgfx:: millis ();
4974 }
5075
51- if ((millis ()- last_blink_millis) > blink_interval) {
76+ if ((lgfx:: millis ()- last_blink_millis) > blink_interval) {
5277 if (eye_open) {
5378 avatar->setEyeOpenRatio (1 );
5479 blink_interval = 2500 + 100 * random (20 );
@@ -57,14 +82,14 @@ void facialLoop(void *args) {
5782 blink_interval = 300 + 10 * random (20 );
5883 }
5984 eye_open = !eye_open;
60- last_blink_millis = millis ();
85+ last_blink_millis = lgfx:: millis ();
6186 }
6287 c = (c + 1 ) % 100 ;
6388 breath = sin (c * 2 * PI / 100.0 );
6489 avatar->setBreath (breath);
65- vTaskDelay (33 /portTICK_PERIOD_MS );
90+ TaskDelay (33 );
6691 }
67- vTaskDelete ( NULL );
92+ TaskResult ( );
6893}
6994
7095Avatar::Avatar () : Avatar(new Face()) {}
@@ -100,6 +125,13 @@ void Avatar::addTask(TaskFunction_t f
100125 , TaskHandle_t* const task_handle
101126 , const BaseType_t core_id) {
102127 DriveContext *ctx = new DriveContext (this );
128+ #ifdef SDL_h_
129+ if (task_handle == NULL ) {
130+ SDL_CreateThreadWithStackSize (f, name, stack_size, ctx);
131+ } else {
132+ *task_handle = SDL_CreateThreadWithStackSize (f, name, stack_size, ctx);
133+ }
134+ #else
103135 // TODO(meganetaaan): set a task handler
104136 xTaskCreateUniversal (f, /* Function to implement the task */
105137 name, /* Name of the task */
@@ -108,6 +140,7 @@ void Avatar::addTask(TaskFunction_t f
108140 priority, /* Priority of the task */
109141 task_handle, /* Task handle. */
110142 core_id); /* Core No*/
143+ #endif
111144}
112145
113146void Avatar::init (int colorDepth) {
@@ -118,11 +151,15 @@ void Avatar::init(int colorDepth) {
118151void Avatar::stop () { _isDrawing = false ; }
119152
120153void Avatar::suspend () {
154+ #ifndef SDL_h_
121155 vTaskSuspend (drawTaskHandle);
156+ #endif
122157}
123158
124159void Avatar::resume () {
160+ #ifndef SDL_h_
125161 vTaskResume (drawTaskHandle);
162+ #endif
126163}
127164
128165void Avatar::start (int colorDepth) {
@@ -132,6 +169,10 @@ void Avatar::start(int colorDepth) {
132169
133170 this ->colorDepth = colorDepth;
134171 DriveContext *ctx = new DriveContext (this );
172+ #ifdef SDL_h_
173+ drawTaskHandle = SDL_CreateThreadWithStackSize (drawLoop, " drawLoop" , 2048 , ctx);
174+ SDL_CreateThreadWithStackSize (facialLoop, " facialLoop" , 1024 , ctx);
175+ #else
135176 // TODO(meganetaaan): keep handle of these tasks
136177 xTaskCreateUniversal (drawLoop, /* Function to implement the task */
137178 " drawLoop" , /* Name of the task */
@@ -148,6 +189,7 @@ void Avatar::start(int colorDepth) {
148189 2 , /* Priority of the task */
149190 NULL , /* Task handle. */
150191 APP_CPU_NUM );
192+ #endif
151193}
152194
153195void Avatar::draw () {
0 commit comments