Skip to content

Commit fa7934a

Browse files
committed
Group IO transfer code within utility function 'transfer_and_wait'.
This allows to write more compact code while still containing the same semantics
1 parent 350efc4 commit fa7934a

File tree

5 files changed

+77
-11
lines changed

5 files changed

+77
-11
lines changed

Diff for: examples/Threadsafe_IO/Threadsafe_SPI/Threadsafe_SPI.ino

+1-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ byte bmp388_read_reg(byte const reg_addr)
6464
byte read_write_buf[] = {static_cast<byte>(0x80 | reg_addr), 0, 0};
6565

6666
IoRequest req(read_write_buf, sizeof(read_write_buf), nullptr, 0);
67-
IoResponse rsp = bmp388.transfer(req);
68-
69-
/* Do other stuff */
70-
71-
rsp->wait();
67+
IoResponse rsp = transfer_and_wait(bmp388, req);
7268

7369
return read_write_buf[2];
7470
}

Diff for: examples/Threadsafe_IO/Threadsafe_Wire/Threadsafe_Wire.ino

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ void setup()
4545

4646
void loop()
4747
{
48-
48+
/* If we don't hand back control then the main thread
49+
* will hog the CPU and all other thread's won't get
50+
* time to be executed.
51+
*/
52+
rtos::ThisThread::yield();
4953
}
5054

5155
/**************************************************************************************
@@ -61,11 +65,7 @@ byte lsm6dsox_read_reg(byte const reg_addr)
6165
byte read_buf = 0;
6266

6367
IoRequest req(write_buf, read_buf);
64-
IoResponse rsp = lsm6dsox.transfer(req);
65-
66-
/* Optionally do other stuff */
67-
68-
rsp->wait();
68+
IoResponse rsp = transfer_and_wait(lsm6dsox, req);
6969

7070
return read_buf;
7171
}

Diff for: src/Arduino_Threads.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "threading/Shared.hpp"
3232

3333
#include "io/BusDevice.h"
34+
#include "io/util/util.h"
3435
#include "io/spi/SpiBusDevice.h"
3536
#include "io/wire/WireBusDevice.h"
3637
#include "io/serial/SerialDispatcher.h"

Diff for: src/io/util/util.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the Arduino_ThreadsafeIO library.
3+
* Copyright (c) 2021 Arduino SA.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
/**************************************************************************************
20+
* INCLUDE
21+
**************************************************************************************/
22+
23+
#include "util.h"
24+
25+
/**************************************************************************************
26+
* FUNCTION DEFINITION
27+
**************************************************************************************/
28+
29+
IoResponse transfer_and_wait(BusDevice & dev, IoRequest & req)
30+
{
31+
IoResponse rsp = dev.transfer(req);
32+
rsp->wait();
33+
return rsp;
34+
}

Diff for: src/io/util/util.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of the Arduino_ThreadsafeIO library.
3+
* Copyright (c) 2021 Arduino SA.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef ARDUINO_THREADS_UTIL_H_
20+
#define ARDUINO_THREADS_UTIL_H_
21+
22+
/**************************************************************************************
23+
* INCLUDE
24+
**************************************************************************************/
25+
26+
#include "../BusDevice.h"
27+
#include "../IoTransaction.h"
28+
29+
/**************************************************************************************
30+
* FUNCTION DECLARATION
31+
**************************************************************************************/
32+
33+
IoResponse transfer_and_wait(BusDevice & dev, IoRequest & req);
34+
35+
#endif /* ARDUINO_THREADS_UTIL_H_ */

0 commit comments

Comments
 (0)