Skip to content
Snippets Groups Projects
Commit 644719cb authored by Alvise de'Faveri's avatar Alvise de'Faveri
Browse files

Clean up ds-gamma test and add min_to_send in gamma868 class

Added some comments and deleted some debug printf in gamma868 related files.
Added the possibility to send only after a certain number of bytes is in the buffer.
parent 1499cfd8
No related branches found
No related tags found
No related merge requests found
......@@ -26,14 +26,13 @@
using namespace std;
using namespace miosix;
#define CMD_LEN 1
#define DATA_LEN 64
/* DISCOVERY F429I*/
typedef Gpio<GPIOG_BASE,13> greenLed;
typedef Gpio<GPIOG_BASE,14> redLed;
typedef Gpio<GPIOA_BASE,0> button;
Gamma868 gamma("/dev/auxtty");
Gamma868 gamma("/dev/auxtty"); //create object
long long sendTime = 0;
int nTentativi = 0;
......@@ -42,6 +41,8 @@ int state = 0; //0 = normal, 1 = sender, 2 = echo receiver
void btnClick(void *arg);
void stdInput(void *arg);
void handleCommand(char *cmd);
void handleData(char *data);
void receiver();
int main() {
......@@ -102,7 +103,7 @@ void stdInput(void *arg){
}
//----------------SEND DATA----------------
gamma.send(DATA_LEN, msg);
gamma.send(strlen(msg), msg);
//-----------------------------------------
printf("Ok \n" );
......
......@@ -24,11 +24,21 @@
using namespace std;
/*
* A serial port attached to the Gamma868 RX and TX pins is expected
* to be passed to the object in order to communicate with the device.
* NOTE that the serial port has to be already open and at the baudrate
* at which the module has been configured (default is 9600 baud).
*
* The object also uses 2 other pins, gammaSwitch and gammaLed, which are
* defined in the gamma_config.h file.
*/
Gamma868::Gamma868(const char *serialPath)
{
fd=open(serialPath,O_RDWR);
if(fd<0) printf("Cannot open %s\n", serialPath);
led::mode(Mode::INPUT);
gammaLed::mode(Mode::INPUT);
}
......@@ -44,9 +54,8 @@ int Gamma868::send(int msg_len, const char *msg)
{
int i = 0;
pthread_mutex_lock(&bufMutex);
printf("Locked buffer mutex (send)\n");
for(i = 0; i < msg_len; i++){
buffer[last] = msg[last];
buffer[last] = msg[i];
last++;
if(last == MAX_BUFFER) last = 0;
if(last == first){
......@@ -55,7 +64,7 @@ int Gamma868::send(int msg_len, const char *msg)
}
}
pthread_mutex_unlock(&bufMutex);
printf("Unocked buffer mutex (send)\n");
retVal = i;
}
return retVal;
......@@ -77,10 +86,8 @@ bool Gamma868::sendCmd(int cmd_len, const char *cmd){
//Send to gamma (synchronized)
pthread_mutex_lock(&writingMutex);
printf("Locked writing mutex (sendcmd)\n");
write(fd, pkt, HEAD_LEN + cmd_len + END_LEN);
pthread_mutex_unlock(&writingMutex);
printf("Unocked writing mutex (sendcmd)\n");
return true;
}
......@@ -96,11 +103,12 @@ bool Gamma868::receive(int bufLen, char *buf)
bool cmd = false;
//Read what you received (synchronized ?)
//First byte: start
while(init != START){
read(fd, &init, 1);
}
pthread_mutex_lock(&readingMutex);
//First byte: start
while(init != START){
read(fd, &init, 1);
}
//Second byte: type of data
read(fd, &type, 1);
if(type == DATA){
......@@ -124,49 +132,54 @@ bool Gamma868::receive(int bufLen, char *buf)
* sends it in packets of fixed dimension, then until the end of transmission.
*/
void Gamma868::readFromBuffer(){
while(1){
if(bufSize() > 0){
int nChar = bufSize() > 64 ? 64 : bufSize();
while(1){ //Continuously check the buffer
//Prepare header
char pkt[HEAD_LEN + DATA_LEN + END_LEN];
pkt[0] = START;
pkt[1] = DATA;
//Copy from circular buffer (Synchronized)
pthread_mutex_lock(&bufMutex);
for(int i = 0; i < nChar; i++){
pkt[i+HEAD_LEN] = buffer[first];
first++;
if(first == MAX_BUFFER)
first = 0;
if(first == last){
break;
}
}
pthread_mutex_unlock(&bufMutex);
pkt[DATA_LEN + HEAD_LEN] = END;
//Send
pthread_mutex_lock(&writingMutex);
printf("Locked writing mutex (cycle)\n");
if(bufSize() > MIN_TO_SEND){ //If there's something in it...
while(bufSize() > 0){ //Read and send all the buffer
//Prepare header
char pkt[HEAD_LEN + DATA_LEN + END_LEN];
pkt[0] = START;
pkt[1] = DATA;
pthread_mutex_lock(&bufMutex); //Lock the buffer
int nChar = bufSize() > 64 ? 64 : bufSize();
//Copy from circular buffer
for(int i = 0; i < nChar; i++){
pkt[i+HEAD_LEN] = buffer[first];
first++;
if(first == MAX_BUFFER)
first = 0;
if(first == last){
break;
}
}
pthread_mutex_unlock(&bufMutex);
pkt[DATA_LEN + HEAD_LEN] = END;
//Start thread that will notify the end of the message sending
Thread::create(&Gamma868::static_waitForLed,
STACK_DEFAULT_FOR_PTHREAD,
MAIN_PRIORITY,
reinterpret_cast<void*>(this));
write(fd, pkt, HEAD_LEN + DATA_LEN + END_LEN);
{
Lock<FastMutex> l(ledMutex);
while(sent==0) ledCond.wait(l);
sent = 0;
}
printf("Unocked writing mutex (cycle)\n");
pthread_mutex_unlock(&writingMutex);
}
Thread::sleep(200);
STACK_DEFAULT_FOR_PTHREAD,
MAIN_PRIORITY,
reinterpret_cast<void*>(this));
//Send
pthread_mutex_lock(&writingMutex);
write(fd, pkt, HEAD_LEN + DATA_LEN + END_LEN);
{
Lock<FastMutex> l(ledMutex);
while(sent==0) ledCond.wait(l);
sent = 0;
}
pthread_mutex_unlock(&writingMutex);
}
Thread::sleep(200); //TODO needed?
}
}
}
/*
......@@ -177,10 +190,10 @@ void Gamma868::readFromBuffer(){
void Gamma868::waitForLed(){
bool sending = false;
while(1){
if(led::value() == 1 && !sending){
if(gammaLed::value() == 1 && !sending){
sending = true;
}
if(sending && led::value() == 0){
if(sending && gammaLed::value() == 0){
Lock<FastMutex> l(ledMutex);
sent++;
ledCond.signal();
......
......@@ -108,6 +108,18 @@ class Gamma868 {
static void* static_waitForLed(void * object){
reinterpret_cast<Gamma868*>(object)->waitForLed();
}
void printBufContent(){
printf("Buffer: first %d last %d occupied %u total size %d\n",
first, last, bufSize(), MAX_BUFFER);
int offset = first;
for (int i = 0; i < MAX_BUFFER; i++){
if (offset + i == MAX_BUFFER) offset = -i;
printf("%c", buffer[i]);
}
printf("\n");
}
};
#endif /* GAMMA868_H */
\ No newline at end of file
......@@ -5,20 +5,22 @@
#endif /* CONFIG_H */
//Object config
#define MAX_BUFFER 128
typedef Gpio<GPIOB_BASE,0> led;
typedef Gpio<GPIOB_BASE,0> gammaLed;
typedef Gpio<GPIOB_BASE,2> gammaSwitch;
struct Configuration{
int local_addr[3] = {127, 127, 127};
int dest_addr[3] = {127, 127, 127};
int lora_mode = 6;
int lora_pow = 15;
int handshake = 0;
int baudrate = 4;
int local_addr[3] = {126, 126, 126};
int dest_addr[3] = {126, 126, 126};
int lora_mode = 1; //SF6
int lora_pow = 15; //+20dbm
int handshake = 0; //No handshake
int baudrate = 0; //9600 baud
};
//Object config
#define MAX_BUFFER 128
#define MIN_TO_SEND 1
//Protocol config
#define HEAD_LEN 2
#define CMD_LEN 1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment