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

Commit d86dd2f

Browse files
DAS_v0.4.1
- Updated inheritence access, children of ArduinoSimulation_uC32 can now only access its public methods. - Changed output design so it is more readable - Because some people prefer for SW4 to be activated with 1 and some prefer for SW1 to be activated with 1 i added ability to toogle between these modes with F1 button (default is 1,2,3,4 = SW4,SW3,SW2,SW1) - changed Loading/Storing to atomic variables from overloaded operators to .load(), .store() methods, to make it easier to modify in future - Overloaded methods like digitalWrite() and digitalRead() to now be able to handle int (they print adequate messages upon error (incorrect argument)) - fixed bug where digitalRead returned LOW signal if m_bPins[i] was true instead of HIGH
1 parent 2594c27 commit d86dd2f

3 files changed

Lines changed: 90 additions & 27 deletions

File tree

README.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ and of course all in good old console! :D Still work in progres...
66
Which is basically only L01-L08, SW1-SW4, BTN1-BTN4.
77

88
Controls:
9-
- 1,2,3,4 - SW1, SW2, SW3, SW4 (on/off)
9+
- 1,2,3,4 - SW4, SW3, SW2, SW1 (on/off) (or SW1,SW2,SW3,SW4 (press F1 to swap between these))
1010
- Q,W,E,R - BTN4, BTN3, BTN2, BTN1 (hold to on, release to off)
11-
- LEDs are ordered in descending order from left to right L08,L07,...,L01
11+
- LEDs are ordered in descending order from left to right L08,L07,...,L01

das.h

Lines changed: 88 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ namespace das
6262
to call its constructor with different parameters.
6363
6464
@author Danilo Novakoviæ
65-
@version 0.1 4/25/2018
65+
@version 0.4.1 4/29/2018
6666
*/
67-
class ArduinoSimulation_uC32 : protected olcConsoleGameEngine
67+
class ArduinoSimulation_uC32 : private olcConsoleGameEngine
6868
{
6969
protected:
7070
// You MUST implement these!!!
7171
virtual void setup() = 0;
7272
virtual void loop() = 0;
73+
void Start() {
74+
olcConsoleGameEngine::Start();
75+
}
7376
private:
7477
struct sRectangleObject
7578
{
@@ -215,31 +218,45 @@ namespace das
215218
m_cvScheduler.notify_one();
216219
}
217220
}
218-
void HandleInputChipKit() {
221+
void HandleInputChipKit()
222+
{
219223
// HANDLE INPUT
220-
if (m_keys[VK_ESCAPE].bReleased == true) {
224+
if (m_keys[VK_ESCAPE].bReleased) {
221225
exit(EXIT_SUCCESS);
222226
}
223227

224-
for (int i = 1; i <= NUM_SW; ++i) {
225-
// keyboard nums 1-4 are reserved for SW flips (on-off)
226-
if (m_keys[i + '0'].bReleased == true) {
227-
int index = swIndex(NUM_SW - i + 1);
228-
m_bPins[index] = !m_bPins[index];
228+
static bool bToogledF1 = false;
229+
230+
if (m_keys[VK_F1].bReleased) {
231+
bToogledF1 = !bToogledF1;
232+
}
233+
234+
// keys 1,2,3,4 are reserved for SW1,SW2,SW3,SW4 (or SW4,SW3,SW2,SW1 based on bToogledF1)
235+
for (int i = 1; i <= NUM_SW; ++i)
236+
{
237+
if (m_keys[i + '0'].bReleased) {
238+
int index = bToogledF1 == true ? swIndex(NUM_SW - i + 1) : swIndex(i);
239+
m_bPins[index].store(!m_bPins[index].load());
229240
}
230241
}
231242

232-
for (int i = 0; i < NUM_BTN; ++i) {
233-
// keyboard chars Q,W,E,R are reserved for BTN flips (on-off)
243+
// keyboard chars Q,W,E,R are reserved for BTN flips (on-off)
244+
for (int i = 0; i < NUM_BTN; ++i)
245+
{
234246
if (m_keys["QWER"[i]].bHeld || m_keys["qwer"[i]].bHeld) {
235-
m_bPins[btnIndex(NUM_BTN - i)] = true;
247+
m_bPins[btnIndex(NUM_BTN - i)].store(true);
236248
}
237249
else {
238-
m_bPins[btnIndex(NUM_BTN - i)] = false;
250+
m_bPins[btnIndex(NUM_BTN - i)].store(false);
239251
}
240252
}
241253
}
242-
254+
255+
/**
256+
This function was used as prototype for "delay(unsigned int ms)" function that user will call.
257+
Once i create new feature i experiment with delayTest function, and once i'm sure it works as intented
258+
i then update delay(unsigned int ms) properly.
259+
*/
243260
void delayTest(unique_lock<mutex>& ulock, unsigned int ms, const wchar_t* debugMsg)
244261
{
245262
DrawString(0, m_activeThread, debugMsg);
@@ -309,32 +326,49 @@ namespace das
309326
*/
310327
void DrawChipKIT()
311328
{
312-
// Draw SW's
313329
int y = 2, x = 1, i;
314330
static const int SPACE_BETWEEN_PINS(1);
315331
COLOUR colour;
332+
PIXEL_TYPE type;
316333

334+
// Draw SW's
317335
for (i = 1, x = 1; x < ScreenWidth() && i <= NUM_SW; ++i)
318336
{
319-
colour = m_bPins[swIndex(i)] == true ? FG_GREEN : FG_RED;
320-
Fill(x, y, x + m_swModel.width, y + m_swModel.height, PIXEL_SOLID, colour);
337+
if (m_bPins[swIndex(i)].load()) {
338+
colour = FG_GREEN;
339+
type = PIXEL_SOLID;
340+
}
341+
else {
342+
colour = FG_RED;
343+
type = PIXEL_HALF;
344+
}
345+
346+
Fill(x, y, x + m_swModel.width, y + m_swModel.height, type, colour);
321347
x += m_swModel.width + SPACE_BETWEEN_PINS;
322348
}
323349
y += m_swModel.height + SPACE_BETWEEN_PINS * 3;
324350

325351
// Draw BTNs
326352
for (i = 0, x = 1; x < ScreenWidth() && i < NUM_BTN; ++i)
327353
{
328-
PIXEL_TYPE type = m_bPins[btnIndex(NUM_BTN - i)] == true ? PIXEL_SOLID : PIXEL_QUARTER;
354+
type = m_bPins[btnIndex(NUM_BTN - i)].load() == true ? PIXEL_SOLID : PIXEL_QUARTER;
329355
Fill(x, y, x + m_btnModel.width, y + m_btnModel.height, type, FG_GREY);
330356
x += m_btnModel.width + SPACE_BETWEEN_PINS;
331357
}
332358

333359
// Draw LEDs
334360
for (i = 0, x += m_btnModel.width; x < ScreenWidth() && i < NUM_LED; ++i)
335361
{
336-
colour = m_bPins[(int)PIN::L08 - i] == true ? FG_GREEN : FG_RED;
337-
Fill(x, y, x + m_ledModel.width, y + m_ledModel.height, PIXEL_SOLID, colour);
362+
if (m_bPins[(int)PIN::L08 - i].load()) {
363+
colour = FG_GREEN;
364+
type = PIXEL_SOLID;
365+
}
366+
else {
367+
colour = FG_RED;
368+
type = PIXEL_HALF;
369+
}
370+
371+
Fill(x, y, x + m_ledModel.width, y + m_ledModel.height, type, colour);
338372
x += m_ledModel.width + SPACE_BETWEEN_PINS;
339373
}
340374

@@ -348,7 +382,7 @@ namespace das
348382
void executeSoftReset(uint32_t options) {
349383
if (options == RUN_SKETCH_ON_BOOT) {
350384
for (int i = 0; i < MAX_UC32_BASIC_IO_PIN; ++i) {
351-
m_bPins[i] = false;
385+
m_bPins[i].store(false);
352386
}
353387
}
354388
else if (options == ENTER_BOOTLOADER_ON_BOOT) {
@@ -361,8 +395,11 @@ namespace das
361395
@note: INPUT_PULLUP support is not yet implemented.
362396
It doesn't do anything in this current state of simulation class.
363397
*/
398+
void pinMode(int pin, PIN_MODE mode) {
399+
pinMode((PIN)pin, mode);
400+
}
364401
void pinMode(PIN pin, PIN_MODE mode) {
365-
m_PinModes[(int)pin] = mode;
402+
m_PinModes[(int)pin].store(mode);
366403
}
367404

368405
/**
@@ -371,20 +408,40 @@ namespace das
371408
calling this method. If it wasn't set on OUTPUT mode method will throw exception.
372409
(exception won't be thrown if m_bLearningMode is set to off)
373410
*/
411+
void digitalWrite(int pin, int value) {
412+
if (pin < 0 || pin > MAX_UC32_BASIC_IO_PIN) {
413+
wchar_t errBuff[100];
414+
wsprintf(errBuff, L" \"int digitalWrite(%d)\" : argument \"%d\" is invalid!", pin, pin);
415+
exit(Error(errBuff));
416+
}
417+
else {
418+
digitalWrite((PIN)pin, value);
419+
}
420+
}
374421
void digitalWrite(PIN pin, int value)
375422
{
376-
if (m_bLearningMode && m_PinModes[(int)pin] != PIN_MODE::OUTPUT)
423+
if (m_bLearningMode && m_PinModes[(int)pin].load() != PIN_MODE::OUTPUT)
377424
{
378425
exit(Error(L"error: you called digital write on pin that had no OUTPUT mode set!"));
379426
}
380-
m_bPins[(int)pin] = value > 0;
427+
m_bPins[(int)pin].store(value > 0);
381428
}
382429

383430
/**
384431
Reads the state of pin. (ON/OFF)
385432
*/
433+
int digitalRead(int pin) {
434+
if (pin < 0 || pin > MAX_UC32_BASIC_IO_PIN) {
435+
wchar_t errBuff[100];
436+
wsprintf(errBuff, L" \"int digitalRead(%d)\" : argument \"%d\" is invalid!", pin, pin);
437+
exit(Error(errBuff));
438+
}
439+
else {
440+
return m_bPins[pin].load() == true ? HIGH : LOW;
441+
}
442+
}
386443
int digitalRead(PIN pin) {
387-
return m_bPins[(int)pin] == true ? LOW : HIGH;
444+
return m_bPins[(int)pin].load() == true ? HIGH : LOW;
388445
}
389446

390447
/**
@@ -394,6 +451,12 @@ namespace das
394451
void delay(unsigned int ms) {
395452
delay(ms, nullptr);
396453
}
454+
455+
protected:
456+
/**
457+
@note: this version of delay can be used for debuging, although it is not supported by
458+
Arduino (it is custom made)
459+
*/
397460
void delay(unsigned int ms, const wchar_t* debug_msg)
398461
{
399462
unique_lock<mutex> ulock(m_muxPin);

main.cpp

-32 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)