Skip to content

Commit

Permalink
Update example to use radix
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Jan 10, 2025
1 parent 58fb1b0 commit ec48fe4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 69 deletions.
140 changes: 72 additions & 68 deletions examples/cli/cli.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
libcli::Cli cli;
using State = libcli::Cli::State;

uint8_t radix = 10;

static void handleCommand(char letter, uintptr_t context);

/** print prompt and request letter input */
Expand All @@ -31,46 +33,16 @@ static void prompt() {
/** callback for readDec */
static constexpr int ADD_LEFT = 0;
static constexpr int ADD_RIGHT = 1;
static constexpr uint32_t DEC_LIMIT = 999999999UL;

static void handleAddDec(uint32_t value, uintptr_t context, State state) {
static uint32_t last_num;

if (state == State::CLI_CANCEL) {
prompt();
return;
}
if (state == State::CLI_DELETE) {
if (context == ADD_LEFT)
return;
cli.backspace();
cli.readDec(handleAddDec, ADD_LEFT, DEC_LIMIT, last_num);
return;
}
if (context == ADD_LEFT) {
last_num = value;
if (state == State::CLI_SPACE) {
cli.readDec(handleAddDec, ADD_RIGHT, DEC_LIMIT);
return;
}
value = 1;
}

cli.println();
cli.print(F("add integers: "));
cli.printDec(last_num);
cli.print(F(" + "));
cli.printDec(value);
cli.print(F(" = "));
cli.printlnDec(last_num + value);
prompt();
static constexpr uint32_t OCT_LIMIT = UINT32_C(0777777777);
static constexpr uint32_t DEC_LIMIT = UINT32_C(999999999);
static constexpr uint32_t HEX_LIMIT = UINT32_C(0xFFFFFFF);
uint32_t limit() {
if (radix == 8)
return OCT_LIMIT;
return radix == 10 ? DEC_LIMIT : HEX_LIMIT;
}

/** callback for readHex */
static constexpr int HEX_WIDTH = 7;
static constexpr uint32_t HEX_LIMIT = 0x0FFFFFFFUL;

static void handleAddHex(uint32_t value, uintptr_t context, State state) {
static void handleAdd(uint32_t value, uintptr_t context, State state) {
static uint32_t last_num;

if (state == State::CLI_CANCEL) {
Expand All @@ -81,33 +53,38 @@ static void handleAddHex(uint32_t value, uintptr_t context, State state) {
if (context == ADD_LEFT)
return;
cli.backspace();
cli.readHex(handleAddHex, ADD_LEFT, HEX_LIMIT, last_num);
cli.readNum(handleAdd, ADD_LEFT, radix, limit(), last_num);
return;
}
if (context == ADD_LEFT) {
last_num = value;
if (state == State::CLI_SPACE) {
cli.readHex(handleAddHex, ADD_RIGHT, HEX_LIMIT);
cli.readNum(handleAdd, ADD_RIGHT, radix, limit());
return;
}
value = 1;
}

cli.println();
cli.print(F("add hexadecimal: "));
cli.printHex(last_num, HEX_WIDTH);
cli.print(F("add integers: "));
cli.printNum(last_num, radix);
cli.print(F(" + "));
cli.printHex(value, HEX_WIDTH);
cli.printNum(value, radix);
cli.print(F(" = "));
cli.printlnHex(last_num + value, HEX_WIDTH);
cli.printlnNum(last_num + value, radix);
prompt();
}

/** callback for readHex and readDec */
static constexpr int DUMP_ADDRESS = 0;
static constexpr int DUMP_LENGTH = 1;
static constexpr int DUMP_ADDR_WIDTH = 6;
static constexpr uint32_t DUMP_ADDR_LIMIT = 0xFFFFFFUL;
uint8_t addr_width() {
return radix == 16 ? 6 : 8;
}
uint8_t data_width() {
return radix == 16 ? 2 : 3;
}

static void handleDump(uint32_t value, uintptr_t context, State state) {
static uint32_t last_addr;
Expand All @@ -120,7 +97,7 @@ static void handleDump(uint32_t value, uintptr_t context, State state) {
if (context == DUMP_ADDRESS)
return;
cli.backspace();
cli.readHex(handleDump, DUMP_ADDRESS, DUMP_ADDR_LIMIT, last_addr);
cli.readNum(handleDump, DUMP_ADDRESS, radix, DUMP_ADDR_LIMIT, last_addr);
return;
}
if (context == DUMP_ADDRESS) {
Expand All @@ -134,19 +111,20 @@ static void handleDump(uint32_t value, uintptr_t context, State state) {

cli.println();
cli.print(F("dump memory: "));
cli.printHex(last_addr, DUMP_ADDR_WIDTH);
cli.printNum(last_addr, addr_width());
cli.print(' ');
cli.printlnDec(value);
const auto end = last_addr + value;
for (auto base = last_addr & ~0xF; base < end; base += 16) {
cli.printHex(base, DUMP_ADDR_WIDTH);
cli.print(F(": "));
cli.printNum(base, radix, addr_width());
cli.print(':');
for (auto i = 0; i < 16; i++) {
const auto a = base + i;
cli.print(' ');
if (a < last_addr || a >= end) {
cli.printStr(F("_"), 4);
cli.printStr(F("_"), data_width());
} else {
cli.printDec(static_cast<uint8_t>(a), 4);
cli.printNum(static_cast<uint8_t>(a), radix, data_width());
}
}
cli.println();
Expand All @@ -159,8 +137,8 @@ static constexpr uint16_t MEMORY_ADDRESS = uint16_t(-1);
static uint16_t MEMORY_INDEX(int index) {
return uint16_t(index);
}
static constexpr int MEMORY_ADDR_WIDTH = 5;
static constexpr uint32_t MEMORY_ADDR_LIMIT = 0xFFFFFUL;

static constexpr uint32_t MEMORY_ADDR_LIMIT = 0xFFFFFFUL;

static void handleMemory(uint32_t value, uintptr_t context, State state) {
static uint32_t last_addr;
Expand All @@ -176,33 +154,33 @@ static void handleMemory(uint32_t value, uintptr_t context, State state) {
return;
cli.backspace();
if (index == 0) {
cli.readHex(handleMemory, MEMORY_ADDRESS, MEMORY_ADDR_LIMIT, last_addr);
cli.readNum(handleMemory, MEMORY_ADDRESS, radix, MEMORY_ADDR_LIMIT, last_addr);
return;
}
index--;
cli.readHex(handleMemory, MEMORY_INDEX(index), UINT8_MAX, mem_buffer[index]);
cli.readNum(handleMemory, MEMORY_INDEX(index), radix, UINT8_MAX, mem_buffer[index]);
return;
}
if (context == MEMORY_ADDRESS) {
last_addr = value;
cli.readHex(handleMemory, MEMORY_INDEX(0), UINT8_MAX);
cli.readNum(handleMemory, MEMORY_INDEX(0), radix, UINT8_MAX);
return;
}

mem_buffer[index++] = value;
if (state == State::CLI_SPACE) {
if (index < sizeof(mem_buffer)) {
cli.readHex(handleMemory, MEMORY_INDEX(index), UINT8_MAX);
cli.readNum(handleMemory, MEMORY_INDEX(index), radix, UINT8_MAX);
return;
}
}

cli.println();
cli.print(F("write memory: "));
cli.printHex(last_addr, MEMORY_ADDR_WIDTH);
cli.printNum(last_addr, radix, addr_width());
for (uint8_t i = 0; i < index; i++) {
cli.print(' ');
cli.printHex(mem_buffer[i], 2);
cli.printNum(mem_buffer[i], radix, data_width());
}
cli.println();
prompt();
Expand Down Expand Up @@ -268,19 +246,40 @@ static void handleWord(char *word, uintptr_t context, State state) {
prompt();
}

static void handleRadix(uint32_t value, uintptr_t, State state) {
if (state == State::CLI_CANCEL) {
prompt();
return;
}
if (state == State::CLI_DELETE) {
return;
}
cli.println();
if (value != 8 && value != 10 && value != 16) {
cli.println(F("unnkown radix"));
value = radix;
}
radix = value;
cli.print(F("read/print radix: "));
cli.printDec(radix);
if (value == 8) {
cli.println(F(" octal"));
} else if (value == 10) {
cli.println(F(" decimal"));
} else {
cli.println(F(" hexadecimal"));
}
prompt();
}

/** callback for readLetter */
static void handleCommand(char letter, uintptr_t context) {
if (letter == 's') {
cli.print(F("step"));
}
if (letter == 'a') {
cli.print(F("add decimal "));
cli.readDec(handleAddDec, ADD_LEFT, DEC_LIMIT);
return;
}
if (letter == 'h') {
cli.print(F("add hexadecimal "));
cli.readHex(handleAddHex, ADD_LEFT, HEX_LIMIT);
cli.readNum(handleAdd, ADD_LEFT, radix, limit());
return;
}
if (letter == 'l') {
Expand All @@ -303,18 +302,23 @@ static void handleCommand(char letter, uintptr_t context) {
cli.readHex(handleMemory, MEMORY_ADDRESS, MEMORY_ADDR_LIMIT);
return;
}
if (letter == 'r') {
cli.print(F("radix "));
cli.readDec(handleRadix, 0, 16);
return;
}
if (letter == '?') {
cli.print(F("libcli (version "));
cli.print(LIBCLI_VERSION_STRING);
cli.println(F(") example"));
cli.println(F(" ?: help"));
cli.println(F(" s: step"));
cli.println(F(" a: add decimal"));
cli.println(F(" h: add hexadecimal"));
cli.println(F(" a: add"));
cli.println(F(" l: load <filename>"));
cli.println(F(" w: word <word>..."));
cli.println(F(" d: dump <address> <length>"));
cli.print(F(" m: memory <address> <byte>..."));
cli.println(F(" m: memory <address> <byte>..."));
cli.print(F(" r: radix [8|10|16]"));
}
cli.println();
prompt();
Expand Down
2 changes: 1 addition & 1 deletion examples/cli/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default_envs =

[env]
lib_deps =
libcli@1.3.0
libcli@1.4.0

[env:promicro16]
platform = atmelavr
Expand Down

0 comments on commit ec48fe4

Please sign in to comment.