test sending sensor results over lora radio. Accelerometer and temp/pressure.
Serial terminal operates at 115200.
This project provides a text-based menu over serial port.
Operating the program only requires using the arrow keys, enter key to activate a control, or entering numbers.
Two sensors provided:
LIS12DH12
accelerometer operates in a continuous sampling mode. Enable control for accelerometer enables this continuous sampling, approx every 3 seconds.
LPS22HH temperature / pressure sensor operates as single shot, where pressing the control button on terminal causes single sample to be performed.
poll rate
control will enable repeated reading of pressure/temperature-sensor or photo-sensor when poll rate is greater than zero.
target must be: DISCO_L072CZ_LRWAN1
Revision 2:972a5704f152, committed 2019-04-29
- Comitter:
- Wayne Roberts
- Date:
- Mon Apr 29 13:54:35 2019 -0700
- Parent:
- 1:5221b961fd69
- Commit message:
- add Ticker for polling photo-sensor and pressure/temp-sensor
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 5221b961fd69 -r 972a5704f152 main.cpp --- a/main.cpp Wed Apr 24 14:31:20 2019 -0700 +++ b/main.cpp Mon Apr 29 13:54:35 2019 -0700 @@ -19,7 +19,8 @@ CMD_TEMP, CMD_PRES, CMD_ACCEL, - CMD_PHOTOS + CMD_PHOTOS, + CMD_NONE }; #define MAX_MENU_ROWS 16 @@ -38,7 +39,7 @@ uint8_t entry_buf_idx; char entry_buf[64]; -msgType_e msg_type; +msgType_e msg_type = MSG_TYPE_PACKET; const uint8_t PingMsg[] = "PING"; const uint8_t PongMsg[] = "PONG"; @@ -50,14 +51,16 @@ uint8_t send_pong : 1; // 2 uint8_t pingpongEnable : 1; // 3 uint8_t do_next_tx : 1; // 4 + uint8_t txBusy : 1; // 5 + uint8_t measure_tx : 1; // 5 } flags; uint8_t botRow; int regAddr = -1; +#define PHOTOS_PIN PA_0 /* * available pins PA_0, PA_1, PA_2, PA_3, PA_4, PA_5, PA_6, PA_7, PB_0, PB_1, PC_0, PC_1, PC_2 -#define PHOTOS_PIN */ #ifdef PHOTOS_PIN AnalogIn photos(PHOTOS_PIN); @@ -75,7 +78,7 @@ char ret; unsigned foo = uart_rx_buf_in; while (uart_rx_buf_in == foo) { - log_printf("waiting %u %u\r\n", uart_rx_buf_in, foo); + asm("nop"); } ret = uart_rx_buf[uart_rx_buf_out++]; @@ -309,6 +312,7 @@ const menu_t lis12dh12_menu[] = { { {LAST_CHIP_MENU_ROW-1, 1}, "LIS12DH12 ", &accel_enable_item, FLAG_MSGTYPE_ALL }, { {LAST_CHIP_MENU_ROW-1, 24}, NULL, &accel_tx_enable_item, FLAG_MSGTYPE_ALL }, + { {0, 0}, NULL, NULL } }; @@ -319,22 +323,44 @@ log_printf("photos %u\r\n", val); } -void tx_photos_push() +void photos_tx() { - uint16_t val = photos.read_u16(); + uint16_t val; + val = photos.read_u16(); log_printf("Tx photos %u\r\n", val); Radio::radio.tx_buf[0] = CMD_PHOTOS; Radio::set_payload_length(sizeof(uint16_t)+1); memcpy(&Radio::radio.tx_buf[1], &val, sizeof(uint16_t)); + + flags.txBusy = true; Radio::txPkt(); } +unsigned pollTickerRate; +Ticker pollTicker; +uint8_t enabledSensor = CMD_NONE; + +void tx_photos_push() +{ + if (pollTickerRate > 0) { + if (enabledSensor != CMD_PHOTOS) + enabledSensor = CMD_PHOTOS; + else { + enabledSensor = CMD_NONE; + } + return; + } + + photos_tx(); +} + const button_item_t photos_item = { _ITEM_BUTTON, "photos", photos_push }; const button_item_t tx_photos_item = { _ITEM_BUTTON, "tx_photos", tx_photos_push }; const menu_t photos_menu[] = { - { {LAST_CHIP_MENU_ROW-2, 1}, "PHOTOS ", &photos_item, FLAG_MSGTYPE_ALL }, - { {LAST_CHIP_MENU_ROW-2, 24}, NULL, &tx_photos_item, FLAG_MSGTYPE_ALL }, + { {(LAST_CHIP_MENU_ROW-2), 1}, "PHOTOS ", &photos_item, FLAG_MSGTYPE_ALL }, + { {(LAST_CHIP_MENU_ROW-2), 24}, NULL, &tx_photos_item, FLAG_MSGTYPE_ALL }, + { {0, 0}, NULL, NULL } }; #endif /* PHOTOS_PIN */ @@ -350,7 +376,7 @@ demo_sample_pressure(&val); } -void tx_temp_push() +void temp_tx() { displayFloatToInt_t val; demo_sample_temp(&val); @@ -360,10 +386,25 @@ Radio::radio.tx_buf[0] = CMD_TEMP; memcpy(&Radio::radio.tx_buf[1], &val, sizeof(displayFloatToInt_t)); + flags.txBusy = true; Radio::txPkt(); } -void tx_pressure_push() +void tx_temp_push() +{ + if (pollTickerRate > 0) { + if (enabledSensor != CMD_TEMP) + enabledSensor = CMD_TEMP; + else { + enabledSensor = CMD_NONE; + } + return; + } + + temp_tx(); +} + +void pres_tx() { displayFloatToInt_t val; demo_sample_pressure(&val); @@ -373,21 +414,63 @@ Radio::radio.tx_buf[0] = CMD_PRES; memcpy(&Radio::radio.tx_buf[1], &val, sizeof(displayFloatToInt_t)); + flags.txBusy = true; Radio::txPkt(); } +void tx_pressure_push() +{ + if (pollTickerRate > 0) { + if (enabledSensor != CMD_PRES) + enabledSensor = CMD_PRES; + else { + enabledSensor = CMD_NONE; + } + return; + } + + pres_tx(); +} + +void sensorPoll() +{ + if (!flags.txBusy && menuState.mode == MENUMODE_NONE) + flags.measure_tx = 1; +} + +void pollRate_print() +{ + pc.printf("%ums", pollTickerRate / 1000); +} + +bool pollRate_write(const char* str) +{ + sscanf(str, "%u", &pollTickerRate); + + if (pollTickerRate > 0) { + pollTickerRate *= 1000; + pollTicker.attach_us(sensorPoll, pollTickerRate); + } else + pollTicker.detach(); + + log_printf("pollTickerRate %uus\r\n", pollTickerRate); + + return false; +} const button_item_t temperature_item = { _ITEM_BUTTON, "temperature", temperature_push }; const button_item_t pressure_item = { _ITEM_BUTTON, "pressure", pressure_push }; const button_item_t tx_temp_item = { _ITEM_BUTTON, "tx_temp", tx_temp_push }; const button_item_t tx_pressure_item = { _ITEM_BUTTON, "tx_pressure", tx_pressure_push }; - +const value_item_t pollRate_item = { _ITEM_VALUE, 8, pollRate_print, pollRate_write }; const menu_t lps22hh_menu[] = { { {LAST_CHIP_MENU_ROW, 1}, "LPS22HH ", &temperature_item, FLAG_MSGTYPE_ALL }, { {LAST_CHIP_MENU_ROW, 22}, NULL, &pressure_item, FLAG_MSGTYPE_ALL }, { {LAST_CHIP_MENU_ROW, 31}, NULL, &tx_temp_item, FLAG_MSGTYPE_ALL }, { {LAST_CHIP_MENU_ROW, 40}, NULL, &tx_pressure_item, FLAG_MSGTYPE_ALL }, + { {LAST_CHIP_MENU_ROW, 53}, "poll rate:", &pollRate_item, FLAG_MSGTYPE_ALL }, + { {0, 0}, NULL, NULL } }; const menu_t msg_pkt_menu[] = { @@ -839,13 +922,14 @@ pc.printf("noLabel"); pc.printf("\r\n"); #endif /* MENU_DEBUG */ + + } #ifdef MENU_DEBUG pc.printf("hit key:"); wait_uart_rx(); //pc.getc(); #endif /* MENU_DEBUG */ - } void navigate_dropdown(uint8_t ch) @@ -1184,12 +1268,12 @@ #ifdef PHOTOS_PIN menu_init_(photos_menu, &txy); #endif /* PHOTOS_PIN */ - menu_init_(lis12dh12_menu, &txy); - menu_init_(lps22hh_menu, &txy); + menu_init_(lis12dh12_menu, &txy); // accel + menu_init_(lps22hh_menu, &txy); // temp, pressure m = get_msg_menu(); if (m == NULL) { - log_printf("NULL-msgMenu\r\n"); + log_printf("NULL-msgMenu %d\r\n", msg_type); for (;;) asm("nop"); } menu_init_(m, &txy); @@ -1327,7 +1411,6 @@ void txDone() { - if (msg_type == MSG_TYPE_PER) { log_printf("CntPacketTx%u, max:%u ipd%u\r\n", CntPacketTx, MaxNumPacket, tx_ipd_ms); if (++CntPacketTx <= MaxNumPacket) @@ -1339,6 +1422,8 @@ Radio::Rx(); } + + flags.txBusy = false; } static void @@ -1502,8 +1587,6 @@ log_printf("\r\n%d samples in FIFO.\r\n\r\nStarted downloading data from FIFO ...\r\n", samplesToRead); - //wait_ms(1000); - log_printf("\r\n[DATA ##] ACC_X ACC_Y ACC_Z [mg]\r\n"); for (int i = 0; i < samplesToRead; i++) @@ -1558,6 +1641,7 @@ pc.printf("\e[2J"); // erase entire screen full_menu_init(); + //wait_uart_rx(); //pc.getc(); pc.printf("\e[2J"); // erase entire screen @@ -1657,6 +1741,21 @@ else { } + if (flags.measure_tx) { + switch (enabledSensor) { + case CMD_PHOTOS: + photos_tx(); + break; + case CMD_PRES: + pres_tx(); + break; + case CMD_TEMP: + temp_tx(); + break; + } + flags.measure_tx = 0; + } + } // ..for (;;) }