Commit bfdaec3c3756b6b6bc7c391b16e482ee584d5de5
receivedWed, 17. Jul 2024, 17:58:39 (by user sx)
Wed, 17 Jul 2024 15:58:39 +0000 (17:58 +0200)
authorManfred Steiner <sx@htl-kaindorf.at>
Wed, 17 Jul 2024 15:58:33 +0000 (17:58 +0200)
committerManfred Steiner <sx@htl-kaindorf.at>
Wed, 17 Jul 2024 15:58:33 +0000 (17:58 +0200)
3 files changed:
examples/simuc/src/main.cpp
examples/simuc/src/simavr/simavr.cpp
examples/simuc/src/simavr/simavr.h

index 0b98f1cb7f11bc64fa6830b57e281c2b53c1db6a..6123098a2431cf00ddfd3044e502f19915ba6a5b 100644 (file)
@@ -31,6 +31,13 @@ void printHelp () {
     printf("    simuc --board arduino a.out\n\n");
 }
 
+void printAvailableCommands (struct StartParameters &params) {
+    printf(" b (break), c (continue), s (stack), r (reset), p (power), q (quit)\n");
+    if (params.board == BoardNano644) {
+        printf(" 1 (SW2 not pressed), 2 (SW2 pressed)\n");
+    }
+}
+
 struct SimUcInit {
     int argc;
     char *argv[];
@@ -216,7 +223,11 @@ int main (int argc, char **argv) {
     }
     printf("----------------------------------------------------------------------\n");
     printf("available commands:\n");
-    printf(" b (break), c (continue), s (stack), r (reset), p (power), q (quit)\n");
+    printAvailableCommands(params);
+    // printf(" b (break), c (continue), s (stack), r (reset), p (power), q (quit)\n");
+    // if (params.board == BoardNano644) {
+    //     printf(" 1 (SW2 not pressed), 2 (SW2 pressed)\n");
+    // }
     printf("----------------------------------------------------------------------\n");
     printf("init done - press key to start\n");
     char c = getchar();
@@ -231,6 +242,12 @@ int main (int argc, char **argv) {
         case 'r': start(CommandReset, NULL); break;
         case 'p': start(CommandPower, NULL); break;
         case 'q': return 0;
+        case '1': case '2': {
+            if (params.board == BoardNano644) {
+
+                break;
+            }
+        }
         default: start(ReadyForNewCommand, NULL); break;
     }
     
@@ -250,41 +267,55 @@ int main (int argc, char **argv) {
         // }
 
         if (getline(&line, &size, stdin) > 0) {
-            const char *commands[] = { "quit", "break", "continue", "stack", "reset", "power" };
             try {
-                int foundIndex = -1;
-                int foundCnt = 0;
-                size_t length = strlen(line) - 1;
-                if (length > 0 && size >= (length + 1)) {
-                    line[length] = 0;
-                    for (long unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) {
-                        const char *cmd = commands[i];
-                        size_t max = strlen(cmd);
-                        bool ok = true;
-                        for (size_t j = 0; j < length; j++) {
-                            if (j >= max || line[j] != cmd[j]) {
-                                ok = false;
-                                break;
+                EnumSimAvrCommand simCmd = ReadyForNewCommand;
+                
+                if (strcmp(line, "1\n") == 0) {
+                    simCmd = CommandSW2Released;
+                
+                } else if (strcmp(line, "2\n") == 0) {
+                    simCmd = CommandSW2Pressed;
+                
+                } else {
+                    const char *commands[] = { "quit", "break", "continue", "stack", "reset", "power" };
+                    int foundIndex = -1;
+                    int foundCnt = 0;
+                    size_t length = strlen(line) - 1;
+                    if (length > 0 && size >= (length + 1)) {
+                        line[length] = 0;
+                        for (long unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) {
+                            const char *cmd = commands[i];
+                            size_t max = strlen(cmd);
+                            bool ok = true;
+                            for (size_t j = 0; j < length; j++) {
+                                if (j >= max || line[j] != cmd[j]) {
+                                    ok = false;
+                                    break;
+                                }
+                            }
+                            if (ok) {
+                                foundCnt++;
+                                foundIndex = i;
                             }
                         }
-                        if (ok) {
-                            foundCnt++;
-                            foundIndex = i;
-                        }
+                    }
+                    
+                    // printf("foundCnt=%d  foundIndex=%d  command=%s\n", foundCnt, foundIndex, foundIndex >= 0 ? commands[foundIndex] : "");
+                    if (foundCnt == 1 || length == 0) {
+                        simCmd = (EnumSimAvrCommand)(foundCnt > 0 ? foundIndex + 2 : 1);
                     }
                 }
-                // printf("foundCnt=%d  foundIndex=%d  command=%s\n", foundCnt, foundIndex, foundIndex >= 0 ? commands[foundIndex] : "");
-                if (foundCnt == 1 || length == 0) {
-                    EnumSimAvrCommand cmd = (EnumSimAvrCommand)(foundCnt > 0 ? foundIndex + 2 : 1);
+
+                if (simCmd != ReadyForNewCommand) {
                     struct SimAvrStatus status = getStatus();
-                    if (cmd == CommandStatus && status.state == StateStopped) { 
+                    if (simCmd == CommandStatus && status.state == StateStopped) { 
                         // ignore status command, because status was already printed
                         // only line feed visible on screen
                     } else {
-                        setCommand(cmd, NULL);
+                        setCommand(simCmd, NULL);
                         while (1) {
                             struct SimAvrEvent ev = waitForEvent();
-                            if (ev.event == EventShutdown || (cmd == CommandQuit && ev.event == EventCommandExecuted)) {
+                            if (ev.event == EventShutdown || (simCmd == CommandQuit && ev.event == EventCommandExecuted)) {
                                 quit = 1;
                                 break;
                             }
@@ -295,10 +326,8 @@ int main (int argc, char **argv) {
                     }
 
                 } else {
-                    printf("invalid command, valid commands are <Enter> for status and:");
-                    for (long unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) {
-                        printf(" %s", commands[i]);
-                    }
+                    printf("invalid command, valid commands are <Enter> for status and:\n");
+                    printAvailableCommands(params);
                     printf("\n");
                     continue;
                 }
index 2b90430b5cfb78241f247e3b0d602012a25c5c61..29d2d0ee1fe3f0ff264e10b041185af6f858890f 100644 (file)
@@ -17,6 +17,7 @@
 #include <simavr/sim_avr.h>
 #include <simavr/sim_core.h>
 #include <simavr/avr_uart.h>
+#include <simavr/avr_ioport.h>
 
 
 SimAvr::SimAvr () {
@@ -517,9 +518,12 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) {
                        }
                        case BoardNano644: {
                                static int8_t led[3] = { -1, -1, -1 }; // pin floating
+                               static int8_t sw2 = -1;
                                static uint8_t mask[3] = { 0x04, 0x08, 0x10 }; // Bitmask for LED Red, Yellow, Green
                                int nextLed[3] = { -1, -1, -1 };
+                               int nextSw2 = -1;
                                char cLed[3];
+                               uint8_t pinc = avr->data[0x26];
                                uint8_t ddrc = avr->data[0x27];
                                uint8_t portc = avr->data[0x28];
                                int change = 0;
@@ -531,12 +535,27 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) {
                                        }
                                        if (!onlyOnChange || nextLed[i] != led[i]) {
                                                change++;
-                                               rv = true;
                                        }
                                }
+
+                               if ((ddrc & 0x20) == 0 && ((portc & 0x20) == 0x20)) {
+                                       nextSw2 = (pinc & 0x20) == 0 ? 1 : 0; // 1 == pressed
+                               } else {
+                                       nextSw2 = -1;
+                               }
+                               if (!onlyOnChange || nextSw2 != sw2) {
+                                       change++;
+                               }
+
                                int l = 0;
                                if (change) {
-                                       l += snprintf(s, size, "LED RGB = ");
+                                       rv = true;
+                                       switch (nextSw2) {
+                                               case 0: l += snprintf(&s[l], size - l, "SW2 ."); break;
+                                               case 1: l += snprintf(&s[l], size - l, "SW2 X"); break;
+                                               default: l += snprintf(&s[l], size - l, "SW2 ?"); break;
+                                       }
+                                       l += snprintf(&s[l], size - l, "  LED RGB = ");
                                        for (int i = 0; i < 3; i++) {
                                                switch (nextLed[i]) {
                                                        case -1: cLed[i] = '-'; break;
@@ -546,16 +565,21 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) {
                                                }
                                                l += snprintf(&s[l], size - l, "%c", cLed[i]);
                                        }
+                                       
                                        for (int i = 0; i < 3; i++) {
                                                if (nextLed[i] != led[i]) {
                                                        led[i] = nextLed[i];
                                                        switch (i) {
-                                                               case 0: l += snprintf(&s[l], l, "  -> Red = %c", cLed[i]); break;
-                                                               case 1: l += snprintf(&s[l], l, "  -> Yellow = %c", cLed[i]); break;
-                                                               case 2: l += snprintf(&s[l], l, "  -> Green = %c", cLed[i]); break;
+                                                               case 0: l += snprintf(&s[l], size - l, "  -> Red = %c", cLed[i]); break;
+                                                               case 1: l += snprintf(&s[l], size - l, "  -> Yellow = %c", cLed[i]); break;
+                                                               case 2: l += snprintf(&s[l], size - l, "  -> Green = %c", cLed[i]); break;
                                                        }
                                                }
                                        }
+                                       if (nextSw2 != sw2) {
+                                               l += snprintf(&s[l], size - l, "  SW2 %s", nextSw2 ? "pressed" : "not pressed");
+                                               sw2 = nextSw2;
+                                       }
                                }
                                break;
                        }
@@ -649,7 +673,7 @@ void SimAvr::avrRun () {
        long cnt = 0;
        int lastAvrState = -1;
        _avr_sp_set(avr, 0);
-       char led[80];
+       char led[120];
        while (1) {
                try {
                        if (sprintfLedStatus(led, sizeof(led), true)) {
@@ -897,6 +921,24 @@ void SimAvr::avrRun () {
                                                break;
                                        }
 
+                                       case CommandSW2Released: {
+                                               if (startParameters->board == BoardNano644) {
+                                                       avr_ioport_external_t io_ext = { 'C', 0x20, 0x20 }; // PC5 ... SW2
+                                                       avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext);
+                                               }
+                                               nextCommand = ReadyForNewCommand;
+                                               break;
+                                       }
+
+                                       case CommandSW2Pressed: {
+                                               if (startParameters->board == BoardNano644) {
+                                                       avr_ioport_external_t io_ext = { 'C', 0x20, 0x0 }; // PC5 ... SW2
+                                                       avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext);
+                                               }
+                                               nextCommand = ReadyForNewCommand;
+                                               break;
+                                       }
+
                                        default: {
                                                nextCommand = ReadyForNewCommand;
                                                break;
index 7c993e753f7b3bb4a45e265622bc1c65ffd94a3e..999beb9ec9700896f2eba9b8ef83fee89b456eaf 100644 (file)
@@ -64,7 +64,9 @@ typedef enum {
        CommandContinue,
        CommandStack,
        CommandReset,
-       CommandPower
+       CommandPower,
+       CommandSW2Released,
+       CommandSW2Pressed,
 } EnumSimAvrCommand;
 
 struct SimAvrStatus {