44
55#include " Avatar.h"
66namespace m5avatar {
7- const uint32_t DEFAULT_STACK_SIZE = 2048 ;
7+ const uint32_t DEFAULT_STACK_SIZE = 1024 ; // 2048;
88
99unsigned int seed = 0 ;
1010
@@ -15,19 +15,6 @@ Avatar *DriveContext::getAvatar() { return avatar; }
1515
1616TaskHandle_t drawTaskHandle;
1717
18- void updateBreath (void *args) {
19- int c = 0 ;
20- DriveContext *ctx = reinterpret_cast <DriveContext *>(args);
21- Avatar *avatar = ctx->getAvatar ();
22- while (avatar->isDrawing ()) {
23- c = c + 1 % 100 ;
24- float f = sin (c * 2 * PI / 100.0 );
25- avatar->setBreath (f);
26- vTaskDelay (33 );
27- }
28- vTaskDelete (NULL );
29- }
30-
3118void drawLoop (void *args) {
3219 DriveContext *ctx = reinterpret_cast <DriveContext *>(args);
3320 Avatar *avatar = ctx->getAvatar ();
@@ -40,28 +27,41 @@ void drawLoop(void *args) {
4027 vTaskDelete (NULL );
4128}
4229
43- void saccade (void *args) {
30+ void facialLoop (void *args) {
31+ int c = 0 ;
4432 DriveContext *ctx = reinterpret_cast <DriveContext *>(args);
4533 Avatar *avatar = ctx->getAvatar ();
46- while (avatar->isDrawing ()) {
47- float vertical = rand_r (&seed) / (RAND_MAX / 2.0 ) - 1 ;
48- float horizontal = rand_r (&seed) / (RAND_MAX / 2.0 ) - 1 ;
49- avatar->setGaze (vertical, horizontal);
50- vTaskDelay (500 + 100 * random (20 ));
51- }
52- vTaskDelete (NULL );
53- }
34+ uint32_t saccade_interval = 1000 ;
35+ uint32_t blink_interval = 1000 ;
36+ unsigned long last_saccade_millis = 0 ;
37+ unsigned long last_blink_millis = 0 ;
38+ bool eye_open = true ;
39+ for (;;) {
40+
41+ if ((millis () - last_saccade_millis) > saccade_interval) {
42+ float vertical = rand_r (&seed) / (RAND_MAX / 2.0 ) - 1 ;
43+ float horizontal = rand_r (&seed) / (RAND_MAX / 2.0 ) - 1 ;
44+ avatar->setGaze (vertical, horizontal);
45+ saccade_interval = 500 + 100 * random (20 );
46+ last_saccade_millis = millis ();
47+ }
5448
55- void blink (void *args) {
56- DriveContext *ctx = reinterpret_cast <DriveContext *>(args);
57- Avatar *avatar = ctx->getAvatar ();
58- while (avatar->isDrawing ()) {
59- avatar->setEyeOpenRatio (1 );
60- vTaskDelay (2500 + 100 * random (20 ));
61- avatar->setEyeOpenRatio (0 );
62- vTaskDelay (300 + 10 * random (20 ));
49+ if ((millis ()- last_blink_millis) > blink_interval) {
50+ if (eye_open) {
51+ avatar->setEyeOpenRatio (1 );
52+ blink_interval = 2500 + 100 * random (20 );
53+ } else {
54+ avatar->setEyeOpenRatio (0 );
55+ blink_interval = 300 + 10 * random (20 );
56+ }
57+ eye_open = !eye_open;
58+ last_blink_millis = millis ();
59+ }
60+ c = c + 1 % 100 ;
61+ float f = sin (c * 2 * PI / 100.0 );
62+ avatar->setBreath (f);
63+ vTaskDelay (33 );
6364 }
64- vTaskDelete (NULL );
6565}
6666
6767Avatar::Avatar () : Avatar(new Face()) {}
@@ -93,7 +93,7 @@ void Avatar::addTask(TaskFunction_t f, const char* name) {
9393 name, /* Name of the task */
9494 DEFAULT_STACK_SIZE , /* Stack size in words */
9595 ctx, /* Task input parameter */
96- 1 , /* P2014riority of the task */
96+ 3 , /* P2014riority of the task */
9797 NULL ); /* Task handle. */
9898 // xTaskCreatePinnedToCore(f, /* Function to implement the task */
9999 // name, /* Name of the task */
@@ -127,30 +127,21 @@ void Avatar::start(int colorDepth) {
127127 this ->colorDepth = colorDepth;
128128 DriveContext *ctx = new DriveContext (this );
129129 // TODO(meganetaaan): keep handle of these tasks
130- xTaskCreate (drawLoop, /* Function to implement the task */
130+ xTaskCreateUniversal (drawLoop, /* Function to implement the task */
131131 " drawLoop" , /* Name of the task */
132132 2048 , /* Stack size in words */
133133 ctx, /* Task input parameter */
134- 1 , /* Priority of the task */
135- &drawTaskHandle); /* Task handle. */
136- xTaskCreate (saccade, /* Function to implement the task */
137- " saccade" , /* Name of the task */
138- 1024 , /* Stack size in words */
139- ctx, /* Task input parameter */
140- 2 , /* Priority of the task */
141- NULL ); /* Task handle. */
142- xTaskCreate (updateBreath, /* Function to implement the task */
143- " breath" , /* Name of the task */
144- 1024 , /* Stack size in words */
145- ctx, /* Task input parameter */
146134 2 , /* Priority of the task */
147- NULL ); /* Task handle. */
148- xTaskCreate (blink, /* Function to implement the task */
149- " blink" , /* Name of the task */
135+ &drawTaskHandle, /* Task handle. */
136+ APP_CPU_NUM );
137+
138+ xTaskCreateUniversal (facialLoop, /* Function to implement the task */
139+ " facialLoop" , /* Name of the task */
150140 1024 , /* Stack size in words */
151141 ctx, /* Task input parameter */
152- 2 , /* Priority of the task */
153- NULL ); /* Task handle. */
142+ 3 , /* Priority of the task */
143+ NULL , /* Task handle. */
144+ APP_CPU_NUM );
154145}
155146
156147void Avatar::draw () {
0 commit comments