AceRoutine  1.2.3
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
Coroutine.cpp
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #include <stdint.h> // uintptr_t
26 #include <Arduino.h> // millis(), micros()
27 #include <AceCommon.h> // udiv1000()
28 #include "Coroutine.h"
29 #include "compat.h" // FPSTR()
30 
31 namespace ace_routine {
32 
34  switch (mDelayType) {
35  case kDelayTypeMillis: {
36  uint16_t elapsedMillis = coroutineMillis() - mDelayStart;
37  return elapsedMillis >= mDelayDuration;
38  }
39  case kDelayTypeMicros: {
40  uint16_t elapsedMicros = coroutineMicros() - mDelayStart;
41  return elapsedMicros >= mDelayDuration;
42  }
43  case kDelayTypeSeconds: {
44  uint16_t elapsedSeconds = coroutineSeconds() - mDelayStart;
45  return elapsedSeconds >= mDelayDuration;
46  }
47  default:
48  // This should never happen.
49  return true;
50  }
51 }
52 
53 void Coroutine::setupCoroutine(const char* name) {
54  mName = ace_common::FCString(name);
55  mStatus = kStatusYielding;
56  insertAtRoot();
57 }
58 
59 void Coroutine::setupCoroutine(const __FlashStringHelper* name) {
60  mName = ace_common::FCString(name);
61  mStatus = kStatusYielding;
62  insertAtRoot();
63 }
64 
66  mName = ace_common::FCString(name);
67  mStatus = kStatusYielding;
68  insertSorted();
69 }
70 
71 void Coroutine::setupCoroutineOrderedByName(const __FlashStringHelper* name) {
72  mName = ace_common::FCString(name);
73  mStatus = kStatusYielding;
74  insertSorted();
75 }
76 
77 // Use a static variable inside a function to solve the static initialization
78 // ordering problem.
79 Coroutine** Coroutine::getRoot() {
80  static Coroutine* root;
81  return &root;
82 }
83 
84 void Coroutine::insertSorted() {
85  Coroutine** p = getRoot();
86 
87  // O(N^2) insertion, good enough for small (O(100)?) number of coroutines.
88  while (*p != nullptr) {
89  if (getName().compareTo((*p)->getName()) <= 0) break;
90  p = &(*p)->mNext;
91  }
92 
93  mNext = *p;
94  *p = this;
95 }
96 
97 void Coroutine::insertAtRoot() {
98  Coroutine** root = getRoot();
99  mNext = *root;
100  *root = this;
101 }
102 
104  if (mStatus != kStatusSuspended) return;
105 
106  // We lost the original state of the coroutine when suspend() was called but
107  // the coroutine will automatically go back into the original state when
108  // Coroutine::runCoroutine() is called because COROUTINE_YIELD(),
109  // COROUTINE_DELAY() and COROUTINE_AWAIT() are written to restore their
110  // status.
111  mStatus = kStatusYielding;
112 }
113 
114 unsigned long Coroutine::coroutineMillis() const {
115  return ::millis();
116 }
117 
118 unsigned long Coroutine::coroutineMicros() const {
119  return ::micros();
120 }
121 
122 unsigned long Coroutine::coroutineSeconds() const {
123  unsigned long m = ::millis();
124 #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD) || defined(ESP8266)
125  // No hardware division so the udiv1000() approximation is faster
126  return ace_common::udiv1000(m);
127 #else
128  return m / 1000;
129 #endif
130 }
131 
132 // Create the sStatusStrings lookup table to translate Status integer to a
133 // human-readable string. When it is used, it increases flash memory by 86
134 // bytes, and static RAM by 14 bytes. It is currently only used by
135 // CoroutineScheduler::list() but I think it's worth it to make debugging
136 // easier.
137 
138 static const char kStatusSuspendedString[] PROGMEM = "Suspended";
139 static const char kStatusYieldingString[] PROGMEM = "Yielding";
140 static const char kStatusDelayingString[] PROGMEM = "Delaying";
141 static const char kStatusRunningString[] PROGMEM = "Running";
142 static const char kStatusEndingString[] PROGMEM = "Ending";
143 static const char kStatusTerminatedString[] PROGMEM = "Terminated";
144 
145 const __FlashStringHelper* const Coroutine::sStatusStrings[] = {
146  FPSTR(kStatusSuspendedString),
147  FPSTR(kStatusYieldingString),
148  FPSTR(kStatusDelayingString),
149  FPSTR(kStatusRunningString),
150  FPSTR(kStatusEndingString),
151  FPSTR(kStatusTerminatedString),
152 };
153 
154 }
ace_routine::Coroutine::kDelayTypeSeconds
static const uint8_t kDelayTypeSeconds
Delay using units of seconds.
Definition: Coroutine.h:503
ace_routine::Coroutine::resume
void resume()
Add a Suspended coroutine into the head of the scheduler linked list, and change the state to Yieldin...
Definition: Coroutine.cpp:103
ace_routine::Coroutine::kDelayTypeMillis
static const uint8_t kDelayTypeMillis
Delay using units of millis.
Definition: Coroutine.h:497
FPSTR
#define FPSTR(p)
A macro that converts a const char* that already points to a PROGMEM string to a const __FlashStringH...
Definition: compat.h:87
ace_routine::Coroutine::coroutineMicros
virtual unsigned long coroutineMicros() const
Returns the current millisecond clock.
Definition: Coroutine.cpp:118
ace_routine::Coroutine::kStatusSuspended
static const Status kStatusSuspended
Coroutine has been suspended using suspend() and the scheduler should remove it from the queue upon t...
Definition: Coroutine.h:479
compat.h
ace_routine::Coroutine::coroutineSeconds
virtual unsigned long coroutineSeconds() const
Returns the current clock in unit of seconds, truncated to the lower 16-bits.
Definition: Coroutine.cpp:122
ace_routine::Coroutine::getName
const ace_common::FCString & getName() const
Human-readable name of the coroutine.
Definition: Coroutine.h:272
ace_routine::Coroutine::kDelayTypeMicros
static const uint8_t kDelayTypeMicros
Delay using units of micros.
Definition: Coroutine.h:500
ace_routine::Coroutine::isDelayExpired
bool isDelayExpired() const
Check if delay time is over.
Definition: Coroutine.cpp:33
ace_routine::Coroutine::setupCoroutineOrderedByName
void setupCoroutineOrderedByName(const char *name)
A version of setupCoroutine(const char*) where the ordering of the coroutines executed by CoroutineSc...
Definition: Coroutine.cpp:65
ace_routine::Coroutine
Base class of all coroutines.
Definition: Coroutine.h:265
ace_routine::Coroutine::Coroutine
Coroutine()
Constructor.
Definition: Coroutine.h:515
ace_routine::Coroutine::kStatusYielding
static const Status kStatusYielding
Coroutine returned using the COROUTINE_YIELD() statement.
Definition: Coroutine.h:482
Coroutine.h
ace_routine::Coroutine::coroutineMillis
virtual unsigned long coroutineMillis() const
Returns the current millisecond clock.
Definition: Coroutine.cpp:114
ace_routine::Coroutine::setupCoroutine
void setupCoroutine(const char *name)
Initialize the coroutine for the CoroutineScheduler, set it to Yielding state, and add it to the link...
Definition: Coroutine.cpp:53