@@ -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);
0 commit comments