mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
start version
This commit is contained in:
56
include/Bus/BusScanner.h
Normal file
56
include/Bus/BusScanner.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class BusScanner {
|
||||
public:
|
||||
BusScanner(const char* tag, String& out, size_t tries) : _found{0},
|
||||
_tries{tries},
|
||||
_out{&out} {
|
||||
_tag = new char(strlen(tag) + 1);
|
||||
strcpy(_tag, tag);
|
||||
}
|
||||
|
||||
void scan() {
|
||||
init();
|
||||
bool res;
|
||||
do {
|
||||
res = syncScan();
|
||||
} while (!res && --_tries);
|
||||
|
||||
if (!_found) {
|
||||
addResult("не найдено");
|
||||
}
|
||||
}
|
||||
|
||||
const char* tag() {
|
||||
return _tag;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void init(){};
|
||||
|
||||
virtual boolean syncScan() = 0;
|
||||
|
||||
protected:
|
||||
void addResult(const String& str) {
|
||||
_out->concat(str);
|
||||
}
|
||||
|
||||
void addResult(uint8_t addr, boolean last = true) {
|
||||
_found++;
|
||||
String str = "0x";
|
||||
if (addr < 16) {
|
||||
str += "0";
|
||||
}
|
||||
str += String(addr, HEX);
|
||||
str += !last ? ", " : ", ";
|
||||
addResult(str);
|
||||
};
|
||||
|
||||
private:
|
||||
char* _tag;
|
||||
size_t _found;
|
||||
size_t _tries;
|
||||
String* _out;
|
||||
};
|
||||
23
include/Bus/BusScannerFactory.h
Normal file
23
include/Bus/BusScannerFactory.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "Bus/BusScanner.h"
|
||||
#include "Bus/I2CScanner.h"
|
||||
#include "Bus/OneWireScanner.h"
|
||||
#include "Consts.h"
|
||||
#include "Utils/JsonUtils.h"
|
||||
|
||||
class BusScannerFactory {
|
||||
public:
|
||||
static BusScanner* get(String& config, BusScanner_t type, String& str) {
|
||||
switch (type) {
|
||||
case BS_I2C:
|
||||
return new I2CScanner(str);
|
||||
case BS_ONE_WIRE: {
|
||||
uint8_t pin = jsonReadInt(config, TAG_ONE_WIRE_PIN);
|
||||
return new OneWireScanner(str, pin);
|
||||
}
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
12
include/Bus/I2CScanner.h
Normal file
12
include/Bus/I2CScanner.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "Bus/BusScanner.h"
|
||||
|
||||
class I2CScanner : public BusScanner {
|
||||
public:
|
||||
I2CScanner(String& out);
|
||||
|
||||
protected:
|
||||
virtual void init() override;
|
||||
virtual boolean syncScan() override;
|
||||
};
|
||||
21
include/Bus/OneWireBus.h
Normal file
21
include/Bus/OneWireBus.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <OneWire.h>
|
||||
|
||||
struct OneWireBus_t {
|
||||
OneWire *bus;
|
||||
uint8_t pin;
|
||||
};
|
||||
|
||||
class OneWireBus {
|
||||
public:
|
||||
OneWireBus();
|
||||
OneWire *get(uint8_t pin);
|
||||
size_t count();
|
||||
|
||||
private:
|
||||
std::vector<OneWireBus_t> _items;
|
||||
};
|
||||
|
||||
extern OneWireBus oneWireBus;
|
||||
15
include/Bus/OneWireScanner.h
Normal file
15
include/Bus/OneWireScanner.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "BusScanner.h"
|
||||
#include "OneWireBus.h"
|
||||
|
||||
class OneWireScanner : public BusScanner {
|
||||
public:
|
||||
OneWireScanner(String& out, uint8_t pin);
|
||||
|
||||
protected:
|
||||
virtual boolean syncScan() override;
|
||||
|
||||
private:
|
||||
OneWire* _bus;
|
||||
};
|
||||
Reference in New Issue
Block a user