|
| 1 | +/** |
| 2 | + * |
| 3 | + * @license MIT License |
| 4 | + * |
| 5 | + * Copyright (c) 2025 lewis he |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all |
| 15 | + * copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + * SOFTWARE. |
| 24 | + * |
| 25 | + * @file SensorWireHelper.ino |
| 26 | + * @author Lewis He ([email protected]) |
| 27 | + * @date 2025-01-15 |
| 28 | + * |
| 29 | + */ |
| 30 | +#include <SPI.h> |
| 31 | +#include <SensorWireHelper.h> |
| 32 | +#include <Commander.h> //Deplib https://github.com/CreativeRobotics/Commander |
| 33 | + |
| 34 | +#ifndef SerialMon |
| 35 | +#define SerialMon Serial |
| 36 | +#endif |
| 37 | + |
| 38 | +#ifndef SENSOR_SDA |
| 39 | +#define SENSOR_SDA 2 |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifndef SENSOR_SCL |
| 43 | +#define SENSOR_SCL 3 |
| 44 | +#endif |
| 45 | + |
| 46 | +Commander cmd; |
| 47 | + |
| 48 | +bool dumpReg(Commander &Cmdr); |
| 49 | +bool dumpDevices(Commander &Cmdr); |
| 50 | +bool helpHandler(Commander &Cmdr); |
| 51 | + |
| 52 | +//COMMAND ARRAY ------------------------------------------------------------------------------ |
| 53 | +const commandList_t masterCommands[] = { |
| 54 | + {"help", helpHandler, "help"}, |
| 55 | + {"dump reg", dumpReg, "dump reg"}, |
| 56 | + {"dump devices", dumpDevices, "dump devices"}, |
| 57 | + |
| 58 | +}; |
| 59 | + |
| 60 | +bool helpHandler(Commander &Cmdr) |
| 61 | +{ |
| 62 | + SerialMon.println("Help:"); |
| 63 | + SerialMon.println("\tdump reg [device address] [reg address] [request read len]"); |
| 64 | + SerialMon.println("\tdump devices"); |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | +void setup() |
| 69 | +{ |
| 70 | + Serial.begin(115200); |
| 71 | + while (!Serial); |
| 72 | + Serial.println("Start!"); |
| 73 | + |
| 74 | +#if defined(ARDUINO_ARCH_RP2040) |
| 75 | + Wire.setSCL(SENSOR_SCL); |
| 76 | + Wire.setSDA(SENSOR_SDA); |
| 77 | + Wire.begin(); |
| 78 | +#elif defined(NRF52840_XXAA) || defined(NRF52832_XXAA) |
| 79 | + Wire.setPins(SENSOR_SDA, SENSOR_SCL); |
| 80 | + Wire.begin(); |
| 81 | +#else |
| 82 | + Wire.begin(SENSOR_SDA, SENSOR_SCL); |
| 83 | +#endif |
| 84 | + |
| 85 | + // Initialise Commander |
| 86 | + cmd.begin(&SerialMon, masterCommands, sizeof(masterCommands)); |
| 87 | + cmd.commandPrompt(ON); //enable the command prompt |
| 88 | + cmd.echo(true); //Echo incoming characters to theoutput port |
| 89 | + cmd.errorMessages(ON); //error messages are enabled - it will tell us if we issue any unrecognised commands |
| 90 | + //Error messaged do NOT work for quick set and get commands |
| 91 | + |
| 92 | + cmd.printCommandPrompt(); |
| 93 | +} |
| 94 | + |
| 95 | +void loop() |
| 96 | +{ |
| 97 | + //Call the update functions using the activeCommander pointer |
| 98 | + cmd.update(); |
| 99 | +} |
| 100 | + |
| 101 | +uint8_t toInt(String data) |
| 102 | +{ |
| 103 | + uint8_t retVal = 0x0; |
| 104 | + if (data.startsWith("0x")) { |
| 105 | + retVal = (uint8_t)strtol(data.c_str(), NULL, 16); |
| 106 | + } else { |
| 107 | + retVal = atoi(data.c_str()); |
| 108 | + } |
| 109 | + return retVal; |
| 110 | +} |
| 111 | + |
| 112 | +bool dumpReg(Commander &Cmdr) |
| 113 | +{ |
| 114 | + int items = Cmdr.countItems(); |
| 115 | + if (items < 3) { |
| 116 | + return 0; |
| 117 | + } |
| 118 | + String dev_address_str, reg_str, request_read_len_str; |
| 119 | + uint8_t dev_address = 0, reg = 0, request_read_len = 0; |
| 120 | + |
| 121 | + Cmdr.getString(dev_address_str); |
| 122 | + Cmdr.getString(reg_str); |
| 123 | + Cmdr.getString(request_read_len_str); |
| 124 | + |
| 125 | + dev_address = toInt(dev_address_str); |
| 126 | + reg = toInt(reg_str); |
| 127 | + request_read_len = toInt(request_read_len_str); |
| 128 | + |
| 129 | + SerialMon.printf("Reg : 0x%02X - start:0x%02X end:0x%02X\n", dev_address, reg, request_read_len); |
| 130 | + int ret = SensorWireHelper::regdump(Wire, SerialMon, dev_address, reg, request_read_len); |
| 131 | + if (ret == -1) { |
| 132 | + SerialMon.println("ERROR!"); |
| 133 | + } |
| 134 | + return false; |
| 135 | +} |
| 136 | + |
| 137 | +bool dumpDevices(Commander &Cmdr) |
| 138 | +{ |
| 139 | + SensorWireHelper::dumpDevices(Wire, SerialMon); |
| 140 | + return false; |
| 141 | +} |
| 142 | + |
0 commit comments