My small ble testing program
Dependencies: BleSerial Cli ConfigFile MbedJSONValue
source/Hardware.cpp@1:d4e23495812a, 2018-01-03 (annotated)
- Committer:
- twixx
- Date:
- Wed Jan 03 17:08:52 2018 +0000
- Revision:
- 1:d4e23495812a
- Parent:
- 0:1d8829c32cd5
no changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
twixx | 0:1d8829c32cd5 | 1 | #include "Hardware.h" |
twixx | 0:1d8829c32cd5 | 2 | |
twixx | 0:1d8829c32cd5 | 3 | Hardware::Hardware() |
twixx | 0:1d8829c32cd5 | 4 | { |
twixx | 0:1d8829c32cd5 | 5 | |
twixx | 0:1d8829c32cd5 | 6 | } |
twixx | 0:1d8829c32cd5 | 7 | |
twixx | 0:1d8829c32cd5 | 8 | InitResult Hardware::init() |
twixx | 0:1d8829c32cd5 | 9 | { |
twixx | 0:1d8829c32cd5 | 10 | //this->led1 = new DigitalOut(LED1, 1); |
twixx | 0:1d8829c32cd5 | 11 | this->led = new PwmOut(LED1); |
twixx | 0:1d8829c32cd5 | 12 | this->led->period_ms(20); |
twixx | 0:1d8829c32cd5 | 13 | this->led->write(0.0f); |
twixx | 0:1d8829c32cd5 | 14 | this->pc = new Serial(USBTX, USBRX);// tx, rx |
twixx | 0:1d8829c32cd5 | 15 | |
twixx | 0:1d8829c32cd5 | 16 | this->initCli(); |
twixx | 0:1d8829c32cd5 | 17 | |
twixx | 0:1d8829c32cd5 | 18 | MbedJSONValue demo; |
twixx | 0:1d8829c32cd5 | 19 | std::string s; |
twixx | 0:1d8829c32cd5 | 20 | |
twixx | 0:1d8829c32cd5 | 21 | //fill the object |
twixx | 0:1d8829c32cd5 | 22 | demo["my_array"][0] = "demo_string"; |
twixx | 0:1d8829c32cd5 | 23 | demo["my_array"][1] = 10; |
twixx | 0:1d8829c32cd5 | 24 | demo["my_boolean"] = false; |
twixx | 0:1d8829c32cd5 | 25 | |
twixx | 0:1d8829c32cd5 | 26 | //serialize it into a JSON string |
twixx | 0:1d8829c32cd5 | 27 | s = demo.serialize(); |
twixx | 0:1d8829c32cd5 | 28 | this->pc->printf("json: %s\r\n", s.c_str()); |
twixx | 0:1d8829c32cd5 | 29 | |
twixx | 0:1d8829c32cd5 | 30 | return InitResultOk; |
twixx | 0:1d8829c32cd5 | 31 | } |
twixx | 0:1d8829c32cd5 | 32 | |
twixx | 0:1d8829c32cd5 | 33 | InitResult Hardware::initCli(){ |
twixx | 0:1d8829c32cd5 | 34 | //this->pc->printf("INIT CLI\n"); |
twixx | 0:1d8829c32cd5 | 35 | this->myCli = new Cli(this->pc, "#~> ", 50, 10); |
twixx | 0:1d8829c32cd5 | 36 | this->myCli->setEcho(true); |
twixx | 0:1d8829c32cd5 | 37 | this->myCli->appendCmd("print", "prints hello world to stream", static_cast<void*> (this), Hardware::cb_print); |
twixx | 0:1d8829c32cd5 | 38 | this->myCli->welcome(); |
twixx | 0:1d8829c32cd5 | 39 | this->myCli->prompt(); |
twixx | 0:1d8829c32cd5 | 40 | |
twixx | 0:1d8829c32cd5 | 41 | BLE &ble = BLE::Instance(); |
twixx | 0:1d8829c32cd5 | 42 | this->bleSerial = new BleSerial(ble); |
twixx | 0:1d8829c32cd5 | 43 | this->bleCli = new Cli(this->bleSerial, "#~> ", 50, 10); |
twixx | 0:1d8829c32cd5 | 44 | this->bleCli->appendCmd("print", "prints hello world to stream", static_cast<void*> (this), Hardware::cb_print); |
twixx | 0:1d8829c32cd5 | 45 | this->bleCli->setEcho(true); |
twixx | 0:1d8829c32cd5 | 46 | this->bleCli->welcome(); |
twixx | 0:1d8829c32cd5 | 47 | this->bleCli->prompt(); |
twixx | 0:1d8829c32cd5 | 48 | |
twixx | 0:1d8829c32cd5 | 49 | return InitResultOk; |
twixx | 0:1d8829c32cd5 | 50 | } |
twixx | 0:1d8829c32cd5 | 51 | |
twixx | 0:1d8829c32cd5 | 52 | CbResult Hardware::cb_print(void* pObject, va_list args) |
twixx | 0:1d8829c32cd5 | 53 | { |
twixx | 0:1d8829c32cd5 | 54 | if (!pObject) |
twixx | 0:1d8829c32cd5 | 55 | { |
twixx | 0:1d8829c32cd5 | 56 | return CbResultError; |
twixx | 0:1d8829c32cd5 | 57 | } |
twixx | 0:1d8829c32cd5 | 58 | |
twixx | 0:1d8829c32cd5 | 59 | char* s = static_cast<char*>(va_arg(args, char*)); |
twixx | 0:1d8829c32cd5 | 60 | Stream *io = static_cast<Stream*>(va_arg(args, Stream*)); |
twixx | 0:1d8829c32cd5 | 61 | va_end(args); |
twixx | 0:1d8829c32cd5 | 62 | |
twixx | 0:1d8829c32cd5 | 63 | Hardware* mySelf = static_cast<Hardware*> (pObject); |
twixx | 0:1d8829c32cd5 | 64 | |
twixx | 0:1d8829c32cd5 | 65 | //mySelf->pc->printf("HALLO WELT %s\n", s); |
twixx | 0:1d8829c32cd5 | 66 | io->printf("HALLO WELT %s\n", s); |
twixx | 0:1d8829c32cd5 | 67 | |
twixx | 0:1d8829c32cd5 | 68 | return CbResultOk; |
twixx | 0:1d8829c32cd5 | 69 | } |