Skip to content
This repository was archived by the owner on Aug 19, 2023. It is now read-only.

Commit 74dde12

Browse files
committed
Escape key support; Fix Task.getParent(); Calculate mcu load; Add TerminalEditor example
1 parent 92e12a4 commit 74dde12

File tree

11 files changed

+337
-53
lines changed

11 files changed

+337
-53
lines changed

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Commands.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace Nexus {
3737

3838
stream << F("Up for ") << millis() / 1000 << F(" seconds.") << endl;
3939
stream << availableMemory << F(" bytes free.") << endl;
40+
stream << F("System load: ") << Scheduler.getLoad() << F("%") << endl;
4041

4142
return NULL;
4243
}
@@ -48,7 +49,7 @@ namespace Nexus {
4849
for (Task* task = Scheduler.getTasks(); task != NULL; task = task->getNext())
4950
{
5051
//stream << column(task->getName(), 16) << (void*) task << endl;
51-
parent->getTerminal()->send(PrintEvent(task->getName(), task->getID()));
52+
stream << column((int) task, 5) << column(task->getName(), 16) << endl;
5253
}
5354

5455
return NULL;

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Scheduler.h

+22
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ namespace Nexus {
4141

4242
public:
4343

44+
Scheduler() : _idle(0), _total(0), _time(0) { }
45+
4446
void addCoro(Coro* coro)
4547
{
4648
coro->_next = _coros;
@@ -72,6 +74,7 @@ namespace Nexus {
7274
// }
7375

7476
Task* getTasks() { return _tasks; }
77+
unsigned getLoad() { return _load; }
7578

7679
void send(Task* task, const Message& message)
7780
{
@@ -92,12 +95,17 @@ namespace Nexus {
9295
coro->_run(coro, message);
9396
}
9497

98+
int count = 0;
99+
95100
for (Task* prev = NULL, * task = _tasks; task != NULL; task = task->getNext())
96101
{
97102
if (msecs >= task->_timeout)
98103
{
99104
task->_run(task, message);
100105
}
106+
else _idle += 1;
107+
108+
_total += 1;
101109

102110
if (task->_context == NULL)
103111
{
@@ -114,13 +122,27 @@ namespace Nexus {
114122
if (prev) task = prev;
115123
}
116124
else prev = task;
125+
126+
count += 1;
127+
}
128+
129+
if (millis() > _time)
130+
{
131+
_load = (100 - (_idle * 100 / _total)) * count;
132+
_idle = _total = 0;
133+
134+
_time = millis() + 1000;
117135
}
118136
}
119137

120138
private:
121139

122140
Coro* _coros;
123141
Task* _tasks;
142+
unsigned _load;
143+
unsigned long _idle;
144+
unsigned long _total;
145+
unsigned long _time;
124146

125147
};
126148

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Shell.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Nexus {
1313

1414
};
1515

16+
template<int Size>
1617
class Buffer {
1718

1819
public:
@@ -43,7 +44,7 @@ namespace Nexus {
4344
return false;
4445
}
4546

46-
const char *value()
47+
const char* value()
4748
{
4849
_buffer[_index] = '\0';
4950

@@ -60,10 +61,15 @@ namespace Nexus {
6061
_index = 0;
6162
}
6263

64+
char& operator [](size_t index)
65+
{
66+
return _buffer[index];
67+
}
68+
6369
private:
6470

65-
char _buffer[30 + 1];
66-
uint8_t _index;
71+
char _buffer[Size + 1];
72+
int _index;
6773

6874
};
6975

@@ -174,7 +180,7 @@ namespace Nexus {
174180
return NULL;
175181
}
176182

177-
Task *executeCommand(const char* name)
183+
Task* executeCommand(const char* name)
178184
{
179185
if (CommandFunc func = findCommand(name))
180186
{
@@ -189,7 +195,7 @@ namespace Nexus {
189195
private:
190196

191197
const Command* _commands;
192-
Buffer _buffer;
198+
Buffer<30> _buffer;
193199

194200
};
195201

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Storage.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Nexus {
99
{
1010
if (!_ready)
1111
{
12-
_driver.initialize();
12+
_driver.setup();
1313

1414
_ready = true;
1515
}
@@ -20,8 +20,6 @@ namespace Nexus {
2020
template<typename Driver, int Size>
2121
void Storage<Driver, Size>::format()
2222
{
23-
Driver& driver = getDriver();
24-
2523
setLastOffset(0);
2624
}
2725

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Storage.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace Nexus {
1111

12-
#include <SPI.h>
13-
#include <Adafruit_FRAM_SPI.h>
12+
// #include <SPI.h>
13+
// #include <Adafruit_FRAM_SPI.h>
1414

1515
template<int CS_Pin>
1616
class FRAM_Driver {
@@ -19,7 +19,7 @@ namespace Nexus {
1919

2020
FRAM_Driver() : _fram(CS_Pin) { }
2121

22-
void initialize()
22+
void setup()
2323
{
2424
if (!_fram.begin())
2525
{
@@ -76,14 +76,14 @@ namespace Nexus {
7676

7777
void setLastOffset(uint16_t lastOffset)
7878
{
79-
_driver.write(Size - 2, reinterpret_cast<uint8_t*>(&lastOffset), 2);
79+
getDriver().write(Size - 2, reinterpret_cast<uint8_t*>(&lastOffset), 2);
8080
}
8181

8282
uint16_t getLastOffset()
8383
{
8484
uint16_t lastOffset;
8585

86-
_driver.read(Size - 2, reinterpret_cast<uint8_t*>(&lastOffset), 2);
86+
getDriver().read(Size - 2, reinterpret_cast<uint8_t*>(&lastOffset), 2);
8787

8888
return lastOffset;
8989
}

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Symbol.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ namespace Nexus {
1919

2020
friend Stream& operator <<(Stream& stream, symbol symbol);
2121

22-
symbol(const __FlashStringHelper* string) :
23-
//_string(reinterpret_cast<const char *>(string))
22+
symbol(const __FlashStringHelper* string = NULL) :
2423
_string(string)
2524
{ }
2625

27-
//operator const char *() const { return _string; }
2826
operator const __FlashStringHelper*() const { return _string; }
2927

3028
uint8_t size() const { return strlen_P(reinterpret_cast<const char*>(_string)); }

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Task.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ namespace Nexus {
4545

4646
typedef void (*TickFunc)(Coro* coro, const Message& message);
4747

48-
Coro(TickFunc run) : _run(run), _context(NULL), _next(NULL) { }
48+
Coro(TickFunc run) : _run(run), _context(NULL), _next(NULL), _parent(NULL) { }
4949

50-
Coro* getNext() { return _next; }
50+
Coro* getNext() const { return _next; }
51+
Coro* getParent() const { return _parent; }
5152

5253
protected:
5354

@@ -95,10 +96,11 @@ namespace Nexus {
9596

9697
void send(const Message& message);
9798

98-
Task* getNext() { return static_cast<Task*>(Coro::getNext()); }
99-
Task* getParent() { return static_cast<Task*>(Coro::getNext()); }
99+
Task* getNext() const { return static_cast<Task*>(Coro::getNext()); }
100+
Task* getParent() const { return static_cast<Task*>(Coro::getParent()); }
100101

101-
symbol getName() { return _name; }
102+
symbol getName() const { return _name; }
103+
uintptr_t getID() const { return reinterpret_cast<uintptr_t>(this); }
102104

103105
Terminal* getTerminal();
104106
Stream& getStream();

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Terminal.h

+53-29
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace Nexus {
3030

3131
KeyEvent() { }
3232

33-
int key;
33+
char key;
3434

3535
};
3636

@@ -59,46 +59,49 @@ namespace Nexus {
5959
{
6060
task_wait();
6161

62-
c = _stream.read();
63-
64-
if (c == 10) continue;
65-
66-
if (c == 13)
67-
{
68-
key = KeyEvent::KeyEnter;
69-
}
70-
else if (c == 8 || c == 127)
71-
{
72-
key = KeyEvent::KeyDelete;
73-
}
74-
else if (c == 27)
62+
if (message.get<StreamEvent>())
7563
{
76-
task_wait4(10);
64+
c = _stream.read();
7765

78-
if (message.get<StreamEvent>())
66+
if (c == 10) continue;
67+
68+
if (c == 13)
69+
{
70+
key = KeyEvent::KeyEnter;
71+
}
72+
else if (c == 8 || c == 127)
73+
{
74+
key = KeyEvent::KeyDelete;
75+
}
76+
else if (c == 27)
7977
{
80-
c = _stream.read();
78+
task_wait4(10);
8179

82-
if (c == '[')
80+
if (message.get<StreamEvent>())
8381
{
84-
task_wait();
85-
8682
c = _stream.read();
8783

88-
switch (c)
84+
if (c == '[')
8985
{
90-
break; case 'A': key = KeyEvent::KeyUp;
91-
break; case 'B': key = KeyEvent::KeyDown;
92-
break; case 'C': key = KeyEvent::KeyRight;
93-
break; case 'D': key = KeyEvent::KeyLeft;
86+
task_wait();
87+
88+
c = _stream.read();
89+
90+
switch (c)
91+
{
92+
break; case 'A': key = KeyEvent::KeyUp;
93+
break; case 'B': key = KeyEvent::KeyDown;
94+
break; case 'C': key = KeyEvent::KeyRight;
95+
break; case 'D': key = KeyEvent::KeyLeft;
96+
}
9497
}
9598
}
99+
else key = 27;
96100
}
97-
else key = 27;
98-
}
99-
else key = c;
101+
else key = c;
100102

101-
if (_task) _task->send(KeyEvent(key));
103+
if (_task) _task->send(KeyEvent(key));
104+
}
102105
}
103106

104107
task_exit;
@@ -112,3 +115,24 @@ namespace Nexus {
112115
};
113116

114117
}
118+
119+
// struct Messenger : public Coro {
120+
121+
// Messenger(Task* task, Stream& stream) : Coro(&run),
122+
// task(task), stream(stream)
123+
// { }
124+
125+
// Task* task;
126+
// Stream& stream;
127+
128+
// static void run(Coro* coro_, const Message& message)
129+
// {
130+
// Messenger* coro = (Messenger*) coro_;
131+
132+
// if (coro->stream.available() > 0)
133+
// {
134+
// coro->task->send(StreamEvent(coro->stream));
135+
// }
136+
// }
137+
138+
// };

Arduino/libraries/Nexus_OS/Nexus_OS/Nexus_Tuple.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ namespace Nexus {
6363

6464
operator bool() const { return _ptr != NULL; }
6565
const T *operator ->() const { return &_value; }
66+
T operator *() { return _value; }
6667

6768
private:
6869

0 commit comments

Comments
 (0)