123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
-
-
- #include <unistd.h>
- #include <string.h>
- #include <iostream>
- #include <signal.h>
- #include "hmtrp.h"
-
- using namespace std;
-
- bool shouldRun = true;
-
- void sig_handler(int signo)
- {
- if (signo == SIGINT)
- shouldRun = false;
- }
-
- void printUsage()
- {
- cout << "Usage:" << endl;
- cout << "Pass a commandline argument (any argument) to this program"
- << endl;
- cout << "to query the radio configuration and output it. NOTE: the"
- << endl;
- cout << "radio must be in CONFIG mode for this to work."
- << endl;
- cout << endl;
- cout << "Running this program without arguments will simply transmit"
- << endl;
- cout << "'Hello World!' every second, and output any data received from"
- << endl;
- cout << "another radio."
- << endl;
- cout << endl;
- }
-
- const size_t bufferLength = 256;
-
- int main (int argc, char **argv)
- {
- signal(SIGINT, sig_handler);
-
-
-
-
- upm::HMTRP* radio = new upm::HMTRP(0);
-
-
- if (!radio->setupTty(B9600))
- {
- cerr << "Failed to setup tty port parameters" << endl;
- return 1;
- }
-
- printUsage();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (argc > 1)
- {
-
- uint32_t freq;
- uint32_t dataRate;
- uint16_t rxBandwidth;
- uint8_t modulation;
- uint8_t txPower;
- uint32_t uartBaud;
-
- if (radio->getConfig(&freq, &dataRate, &rxBandwidth, &modulation,
- &txPower, &uartBaud))
- {
- cout << "Radio configuration:" << endl;
- cout << "freq: " << freq << " dataRate: " << dataRate
- << " rxBandwidth: " << rxBandwidth << "Khz" << endl;
-
- cout << "modulation: " << int(modulation) << "Khz txPower: "
- << int(txPower) << " uartBaud: " << uartBaud << endl;
- }
- else
- {
- cerr << "getConfig() failed. Make sure the radio is in "
- << "CONFIG mode." << endl;
- }
- }
- else
- {
-
- char radioBuffer[bufferLength];
- int counter = 0;
- cout << "Running in normal read/write mode." << endl;
-
- while (shouldRun)
- {
-
-
- if (radio->dataAvailable())
- {
- memset(radioBuffer, 0, bufferLength);
- int rv = radio->readData(radioBuffer, bufferLength - 1);
-
- if (rv > 0)
- cout << "Received: " << radioBuffer << endl;
-
- if (rv < 0)
- {
- cerr << "Port read error." << endl;
- break;
- }
-
- continue;
- }
-
- usleep(100000);
- counter++;
-
- if (counter > 10)
- {
- static const char *hello = "Hello World!";
- cout << "Transmitting hello world..." << endl;
- radio->writeData((char *)hello, strlen(hello) + 1);
- counter = 0;
- }
- }
- }
-
-
-
- cout << "Exiting..." << endl;
-
- delete radio;
- return 0;
- }
|