mirror of
https://github.com/Genaker/LoraSA.git
synced 2026-07-05 17:32:15 +02:00
Factor out bus configuration
This commit is contained in:
@@ -170,6 +170,30 @@ bool Config::updateConfig(String key, String value)
|
||||
return true;
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("uart0"))
|
||||
{
|
||||
uart0 = BusConfig::configure(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("uart1"))
|
||||
{
|
||||
uart1 = BusConfig::configure(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("spi1"))
|
||||
{
|
||||
spi1 = BusConfig::configure(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("wire1"))
|
||||
{
|
||||
wire1 = BusConfig::configure(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
UPDATE_BOOL(is_host, key, value);
|
||||
|
||||
UPDATE_BOOL(lora_enabled, key, value);
|
||||
@@ -521,6 +545,11 @@ bool Config::write_config(const char *path)
|
||||
|
||||
f.println("lora_enabled = " + getConfig("lora_enabled"));
|
||||
|
||||
f.println("uart0 = " + getConfig("uart0"));
|
||||
f.println("uart1 = " + getConfig("uart1"));
|
||||
f.println("spi1 = " + getConfig("spi1"));
|
||||
f.println("wire1 = " + getConfig("wire1"));
|
||||
|
||||
f.close();
|
||||
return true;
|
||||
}
|
||||
@@ -572,6 +601,26 @@ String Config::getConfig(String key)
|
||||
return String(is_host ? "true" : "false");
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("uart0"))
|
||||
{
|
||||
return uart0.toStr();
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("uart1"))
|
||||
{
|
||||
return uart1.toStr();
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("spi1"))
|
||||
{
|
||||
return spi1.toStr();
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("wire1"))
|
||||
{
|
||||
return wire1.toStr();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -642,3 +691,127 @@ ParseResult parse_config_line(String ln)
|
||||
|
||||
return ParseResult(k, v);
|
||||
}
|
||||
|
||||
BusConfig BusConfig::configure(String cfg)
|
||||
{
|
||||
BusConfig c;
|
||||
|
||||
if (cfg.equalsIgnoreCase("none"))
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
int begin = 0;
|
||||
int end, i;
|
||||
|
||||
while ((i = findSepa(cfg, ",", begin, end)) >= 0)
|
||||
{
|
||||
String param = cfg.substring(begin, end);
|
||||
begin = i;
|
||||
int j = param.indexOf(":");
|
||||
if (j < 0)
|
||||
{
|
||||
Serial.printf("Expected ':' to be present in '%s' - ignoring config\n",
|
||||
param);
|
||||
continue;
|
||||
}
|
||||
|
||||
String k = param.substring(0, j);
|
||||
param = param.substring(j + 1);
|
||||
|
||||
k.toLowerCase();
|
||||
if (k.equals("enabled"))
|
||||
{
|
||||
c.enabled = !param.equalsIgnoreCase("false");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c.bus_type == SPI && k.equals("clk") || c.bus_type == WIRE && k.equals("scl"))
|
||||
{
|
||||
c.clk = param.toInt();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c.bus_type == SERIAL && k.equals("tx") ||
|
||||
c.bus_type == SPI && k.equals("mosi") ||
|
||||
c.bus_type == WIRE && k.equals("sda"))
|
||||
{
|
||||
c.tx = param.toInt();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c.bus_type == SERIAL && k.equals("rx") ||
|
||||
c.bus_type == SPI && k.equals("miso"))
|
||||
{
|
||||
c.rx = param.toInt();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c.bus_type != NONE)
|
||||
{
|
||||
Serial.printf("Unknown key: '%s'; ignoring config\n", k.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (k.equals("none") || k.length() == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
c.enabled = true;
|
||||
|
||||
char bus_type = k.charAt(0);
|
||||
switch (bus_type)
|
||||
{
|
||||
case 'u':
|
||||
c.bus_type = UART;
|
||||
break;
|
||||
case 's':
|
||||
c.bus_type = SPI;
|
||||
break;
|
||||
case 'w':
|
||||
c.bus_type = WIRE;
|
||||
break;
|
||||
default:
|
||||
c.bus_type = NONE;
|
||||
c.enabled = false;
|
||||
}
|
||||
|
||||
c.bus_num = k.substring(1).toInt();
|
||||
c.clock_freq = param.toInt();
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
String BusConfig::toStr()
|
||||
{
|
||||
if (bus_type == NONE)
|
||||
{
|
||||
return "none";
|
||||
}
|
||||
|
||||
String ret = String(bus_type == UART ? "u"
|
||||
: bus_type == SPI ? "s"
|
||||
: bus_type == WIRE ? "w"
|
||||
: "none") +
|
||||
String(bus_num);
|
||||
|
||||
ret += ":" + String(clock_freq) + (enabled ? "" : ",enabled:false");
|
||||
switch (bus_type)
|
||||
{
|
||||
case UART:
|
||||
ret += ",rx:" + String(rx) + ",tx:" + String(tx);
|
||||
break;
|
||||
case SPI:
|
||||
ret += ",clk:" + String(clk) + ",mosi:" + String(mosi) + ",miso:" + String(miso);
|
||||
break;
|
||||
case WIRE:
|
||||
ret += ",scl:" + String(scl) + ",sda:" + String(sda);
|
||||
break;
|
||||
default:
|
||||
ret = "none";
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user