![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
xDotBridge/src/main.cpp@47:a68747642a7a, 2017-02-10 (annotated)
- Committer:
- Matt Briggs
- Date:
- Fri Feb 10 07:41:16 2017 -0700
- Revision:
- 47:a68747642a7a
- Parent:
- 44:ece6330e9b57
- Child:
- 48:bab9f747d9ed
Added test files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Matt Briggs | 34:5618603e5fc3 | 1 | #include <math.h> |
Matt Briggs | 22:9453658b8d4b | 2 | #include "config.h" |
Matt Briggs | 30:2e673a672884 | 3 | #include "xdot_flash.h" |
Mike Fiore |
11:d2e31743433a | 4 | #include "dot_util.h" |
Mike Fiore |
14:19fae4509473 | 5 | #include "RadioEvent.h" |
Matt Briggs | 47:a68747642a7a | 6 | #include "WinbondSPIFlash.h" |
Matt Briggs | 41:9ef4c4d77711 | 7 | //#include <xdot_low_power.h> |
mfiore | 17:d4f82e16de5f | 8 | |
Matt Briggs | 47:a68747642a7a | 9 | #include "BaseboardIO.h" |
Matt Briggs | 41:9ef4c4d77711 | 10 | #include "CommProtocolPeerBrute.h" |
Matt Briggs | 31:9c535a708ae9 | 11 | |
Matt Briggs | 47:a68747642a7a | 12 | #ifndef __TEST__ // Exclude code for tests |
Mike Fiore |
11:d2e31743433a | 13 | Serial pc(USBTX, USBRX); |
Mike Fiore |
11:d2e31743433a | 14 | |
Matt Briggs | 47:a68747642a7a | 15 | mDot* dot = NULL; // Used by dot-utils |
Matt Briggs | 39:64f79fa6e3cc | 16 | |
Mike Fiore |
11:d2e31743433a | 17 | int main() { |
Matt Briggs | 47:a68747642a7a | 18 | CommProtocolPeerBrute protocol; |
Matt Briggs | 44:ece6330e9b57 | 19 | BaseboardIO bbio; |
Matt Briggs | 31:9c535a708ae9 | 20 | RadioEvent events; // Custom event handler for automatically displaying RX data |
Matt Briggs | 47:a68747642a7a | 21 | WinbondSPIFlash flash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS); |
Matt Briggs | 44:ece6330e9b57 | 22 | // flash.frequency(48e6); // TODO try overridding for faster freq (Default 1MHz) |
Mike Fiore |
11:d2e31743433a | 23 | |
Mike Fiore |
11:d2e31743433a | 24 | pc.baud(115200); |
Mike Fiore |
11:d2e31743433a | 25 | |
Matt Briggs | 27:6b68ff715ae1 | 26 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Matt Briggs | 47:a68747642a7a | 27 | |
Mike Fiore |
11:d2e31743433a | 28 | dot = mDot::getInstance(); |
Mike Fiore |
11:d2e31743433a | 29 | |
Mike Fiore |
16:a3832552dfe1 | 30 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
Mike Fiore |
16:a3832552dfe1 | 31 | |
Mike Fiore |
12:ec9768677cea | 32 | // start from a well-known state |
Mike Fiore |
12:ec9768677cea | 33 | logInfo("defaulting Dot configuration"); |
Mike Fiore |
12:ec9768677cea | 34 | dot->resetConfig(); |
Mike Fiore |
12:ec9768677cea | 35 | |
Mike Fiore |
11:d2e31743433a | 36 | // make sure library logging is turned on |
Matt Briggs | 27:6b68ff715ae1 | 37 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
Mike Fiore |
11:d2e31743433a | 38 | |
Mike Fiore |
11:d2e31743433a | 39 | // attach the custom events handler |
Matt Briggs | 44:ece6330e9b57 | 40 | dot->setEvents(&events); // Little bonus event debug information |
Mike Fiore |
11:d2e31743433a | 41 | |
Matt Briggs | 31:9c535a708ae9 | 42 | // Setup programmable voltage detector |
Matt Briggs | 31:9c535a708ae9 | 43 | // PVD_LEVEL0 Falling 1.85 |
Matt Briggs | 31:9c535a708ae9 | 44 | // PVD_LEVEL1 Falling 2.04 |
Matt Briggs | 31:9c535a708ae9 | 45 | // PVD_LEVEL2 Falling 2.24 |
Matt Briggs | 31:9c535a708ae9 | 46 | // PVD_LEVEL3 Falling 2.44 |
Matt Briggs | 31:9c535a708ae9 | 47 | // PVD_LEVEL4 Falling 2.64 |
Matt Briggs | 31:9c535a708ae9 | 48 | // PVD_LEVEL5 Falling 2.84 |
Matt Briggs | 31:9c535a708ae9 | 49 | // PVD_LEVEL6 Falling 3.05 |
Matt Briggs | 31:9c535a708ae9 | 50 | PWR_PVDTypeDef pvdConfig; |
Matt Briggs | 31:9c535a708ae9 | 51 | pvdConfig.Mode = PWR_PVD_MODE_NORMAL; |
Matt Briggs | 31:9c535a708ae9 | 52 | pvdConfig.PVDLevel = PWR_PVDLEVEL_5; |
Matt Briggs | 31:9c535a708ae9 | 53 | |
Matt Briggs | 31:9c535a708ae9 | 54 | HAL_PWR_ConfigPVD(&pvdConfig); |
Matt Briggs | 31:9c535a708ae9 | 55 | HAL_PWR_EnablePVD(); |
Matt Briggs | 31:9c535a708ae9 | 56 | logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel); |
Matt Briggs | 41:9ef4c4d77711 | 57 | // HAL_PWR_PVDCallback need to define this I think this will override the current implementation |
Matt Briggs | 31:9c535a708ae9 | 58 | |
Matt Briggs | 31:9c535a708ae9 | 59 | // TODO setup IO here |
Matt Briggs | 31:9c535a708ae9 | 60 | |
Matt Briggs | 41:9ef4c4d77711 | 61 | #if BRIDGE_TX_BRUTE |
Matt Briggs | 44:ece6330e9b57 | 62 | protocol.setTx(true); |
Matt Briggs | 41:9ef4c4d77711 | 63 | #else |
Matt Briggs | 44:ece6330e9b57 | 64 | protocol.setTx(false); |
Matt Briggs | 41:9ef4c4d77711 | 65 | #endif |
Matt Briggs | 41:9ef4c4d77711 | 66 | protocol.init(); |
Mike Fiore |
11:d2e31743433a | 67 | |
Matt Briggs | 41:9ef4c4d77711 | 68 | dot->setWakePin(WAKE); |
Matt Briggs | 24:fdf87e4b72e5 | 69 | |
Mike Fiore |
11:d2e31743433a | 70 | // save changes to configuration |
Mike Fiore |
11:d2e31743433a | 71 | logInfo("saving configuration"); |
Mike Fiore |
11:d2e31743433a | 72 | if (!dot->saveConfig()) { |
Mike Fiore |
11:d2e31743433a | 73 | logError("failed to save configuration"); |
Mike Fiore |
11:d2e31743433a | 74 | } |
Mike Fiore |
11:d2e31743433a | 75 | |
Mike Fiore |
11:d2e31743433a | 76 | // display configuration |
Mike Fiore |
11:d2e31743433a | 77 | display_config(); |
Mike Fiore |
11:d2e31743433a | 78 | |
Matt Briggs | 41:9ef4c4d77711 | 79 | uint16_t seqNum=0; |
Matt Briggs | 26:9411b26a5084 | 80 | |
Matt Briggs | 47:a68747642a7a | 81 | /** |
Matt Briggs | 47:a68747642a7a | 82 | * Main Loop |
Matt Briggs | 47:a68747642a7a | 83 | */ |
Mike Fiore |
11:d2e31743433a | 84 | while (true) { |
Matt Briggs | 25:fea776239709 | 85 | std::vector<uint8_t> data; |
Matt Briggs | 47:a68747642a7a | 86 | bbio.ledOff(); |
Matt Briggs | 47:a68747642a7a | 87 | |
Matt Briggs | 47:a68747642a7a | 88 | if (protocol.isTx()) { |
Matt Briggs | 47:a68747642a7a | 89 | #if LED_FEEDBACK |
Matt Briggs | 47:a68747642a7a | 90 | bbio.ledOn(); |
Matt Briggs | 47:a68747642a7a | 91 | #endif |
Matt Briggs | 47:a68747642a7a | 92 | // TODO check for CC_IN |
Matt Briggs | 47:a68747642a7a | 93 | |
Matt Briggs | 47:a68747642a7a | 94 | data.push_back((seqNum >> 8) & 0xFF); |
Matt Briggs | 47:a68747642a7a | 95 | data.push_back(seqNum & 0xFF); |
Matt Briggs | 47:a68747642a7a | 96 | protocol.send(data); |
Matt Briggs | 47:a68747642a7a | 97 | seqNum++; |
Matt Briggs | 47:a68747642a7a | 98 | |
Matt Briggs | 47:a68747642a7a | 99 | #if LED_FEEDBACK |
Matt Briggs | 47:a68747642a7a | 100 | bbio.ledOff(); |
Matt Briggs | 47:a68747642a7a | 101 | #endif |
Matt Briggs | 47:a68747642a7a | 102 | sleep_save_io(); |
Matt Briggs | 47:a68747642a7a | 103 | sleep_configure_io(); |
Matt Briggs | 47:a68747642a7a | 104 | dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 47:a68747642a7a | 105 | sleep_restore_io(); |
Matt Briggs | 47:a68747642a7a | 106 | } |
Mike Fiore |
11:d2e31743433a | 107 | |
Matt Briggs | 47:a68747642a7a | 108 | if (protocol.isRx()) { |
Matt Briggs | 47:a68747642a7a | 109 | bool msgPending; |
Matt Briggs | 47:a68747642a7a | 110 | protocol.listen(msgPending); |
Matt Briggs | 47:a68747642a7a | 111 | if (msgPending) { |
Matt Briggs | 47:a68747642a7a | 112 | protocol.recv(data); |
Matt Briggs | 47:a68747642a7a | 113 | std::string dataStr(data.begin(), data.end()); |
Matt Briggs | 47:a68747642a7a | 114 | logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str()); |
Matt Briggs | 47:a68747642a7a | 115 | // TODO add CC_OUT code here |
Matt Briggs | 47:a68747642a7a | 116 | seqNum++; |
Matt Briggs | 47:a68747642a7a | 117 | #if LED_FEEDBACK |
Matt Briggs | 47:a68747642a7a | 118 | bbio.ledOn(); |
Matt Briggs | 47:a68747642a7a | 119 | wait(0.5); |
Matt Briggs | 47:a68747642a7a | 120 | #endif |
Matt Briggs | 47:a68747642a7a | 121 | } |
Matt Briggs | 47:a68747642a7a | 122 | bbio.ledOff(); |
Matt Briggs | 47:a68747642a7a | 123 | logInfo("Sleeping. Time %d", us_ticker_read()); |
Matt Briggs | 47:a68747642a7a | 124 | sleep_save_io(); |
Matt Briggs | 47:a68747642a7a | 125 | sleep_configure_io(); |
Matt Briggs | 47:a68747642a7a | 126 | // TODO maybe add if statement here to prevent double hits by sleeping for a longer time |
Matt Briggs | 47:a68747642a7a | 127 | dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 47:a68747642a7a | 128 | sleep_restore_io(); |
Matt Briggs | 47:a68747642a7a | 129 | } |
Matt Briggs | 31:9c535a708ae9 | 130 | |
Matt Briggs | 29:e05e35976cfe | 131 | |
Matt Briggs | 44:ece6330e9b57 | 132 | logInfo("================================"); |
Matt Briggs | 44:ece6330e9b57 | 133 | wait(1.0); |
Matt Briggs | 47:a68747642a7a | 134 | // Need to re-implement some of these sleep functions |
Matt Briggs | 41:9ef4c4d77711 | 135 | // sleep_save_io(); |
Matt Briggs | 41:9ef4c4d77711 | 136 | // sleep_configure_io(); |
Matt Briggs | 34:5618603e5fc3 | 137 | |
Matt Briggs | 47:a68747642a7a | 138 | // dot->sleep(2, mDot::INTERRUPT, false); // Go to sleep until wake button |
Mike Fiore |
11:d2e31743433a | 139 | } |
Matt Briggs | 47:a68747642a7a | 140 | |
Mike Fiore |
11:d2e31743433a | 141 | return 0; |
Mike Fiore |
11:d2e31743433a | 142 | } |
Matt Briggs | 47:a68747642a7a | 143 | #endif |
Mike Fiore |
11:d2e31743433a | 144 | |
Matt Briggs | 27:6b68ff715ae1 | 145 |