printf(" simuc --board arduino a.out\n\n");
}
+void printAvailableCommands (struct StartParameters ¶ms) {
+ 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[];
}
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();
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;
}
// }
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;
}
}
} 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;
}
#include <simavr/sim_avr.h>
#include <simavr/sim_core.h>
#include <simavr/avr_uart.h>
+#include <simavr/avr_ioport.h>
SimAvr::SimAvr () {
}
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;
}
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;
}
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;
}
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)) {
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;