48 Commits

Author SHA1 Message Date
86deb5420b ADD my candidate applications
Some checks failed
Build test application / build-cli-v1 (debug, DISCO_H747I, tests-bike-computer-bike-system) (push) Failing after 2s
Build test application / build-cli-v1 (debug, DISCO_H747I, tests-bike-computer-sensor-device) (push) Failing after 3s
Build test application / build-cli-v1 (debug, DISCO_H747I, tests-bike-computer-speedometer) (push) Failing after 3s
2025-01-04 15:32:00 +01:00
f6e3413cf6 ADD library update-client 2025-01-04 15:31:25 +01:00
488d7532e7 ADD pre-commit config 2025-01-04 15:14:28 +01:00
6e79ced7ff ADD minimal bootloader (main.cpp) 2024-12-31 12:02:40 +01:00
890edc211a ADD minimal bootloader 2024-12-31 11:54:14 +01:00
1f9e4ebd3c FIX cast with chrono duration 2024-12-30 17:02:16 +01:00
7d7fb400ff FIX tests namespaces 2024-12-30 13:57:42 +01:00
db834de1fd ADD cpplint args in action file 2024-12-30 13:57:42 +01:00
84bd49be80 FIX delete flag unused function 2024-12-30 13:57:42 +01:00
ebfcef2c57 ADD suppress args for cppcheck 2024-12-30 13:57:42 +01:00
e7a2a7da59 ADD files to pre-commit 2024-12-30 13:57:42 +01:00
9aa656c7ac ADD test pre-commit 2024-12-30 13:57:42 +01:00
9c104fc3a2 ADD project folders to pre-commit 2024-12-30 13:57:42 +01:00
1ba466569e FIX delta to use GCC rather than ARMC6 2024-11-19 14:38:42 +01:00
60baebd59b Merge branch 'main' into develop 2024-11-19 11:53:56 +01:00
eeb88edba3 UPD CI workflow 2024-11-18 10:13:01 +01:00
ac4067e5a4 RM folder ignore for tests 2024-11-18 10:13:01 +01:00
86e033fe2f ADD answer to questions 2024-11-18 10:13:01 +01:00
f6f3d59ef3 ADD run configurations 2024-11-18 10:13:01 +01:00
20406664c3 FIX reset task 2024-11-18 10:13:01 +01:00
62d7d7b376 FIX Static scheduling with event 2024-11-18 10:13:01 +01:00
35d2d76bca ADD [WIP] Static scheduling with event 2024-11-18 10:13:01 +01:00
0bd561b947 ADD EventQueue for static_scheduling 2024-11-18 10:13:01 +01:00
b52f476124 ADD Thread sleep 2024-11-18 10:13:01 +01:00
c66c8a0963 ADD cpu logger 2024-11-18 10:13:01 +01:00
4c174ce444 ADD bike-system super-loop with while 2024-11-18 10:13:01 +01:00
ed9ab169bb CHG switch CI trigger from push to pull_request 2024-11-12 16:00:13 +01:00
6d0452d9ca FIX ResetDevice
- Initialisation
- Callback
- return of checkReset
2024-11-12 16:00:13 +01:00
874a968f5c ADD ResetDevice 2024-11-12 15:35:40 +01:00
7b62a3634b ADD PedalDevice 2024-11-12 15:35:40 +01:00
a7c28c0e52 FIX distance calculation in speedometer. 2024-11-12 13:57:04 +01:00
69047369f0 FIX GitHub action 2024-11-06 14:40:17 +01:00
af0b6180da FIX raw + unique ptr tests (#12)
FIX issues on raw and unique pointer tests
+ Merge individual test files for unique and raw pointers and merged their test cases into a single test file for simplicity.

Co-authored-by: Fastium <79766552+Fastium@users.noreply.github.com>
2024-11-06 12:35:04 +01:00
43cacadfb7 FIX conflict when merge main to develop 2024-11-05 16:53:08 +01:00
15d1183bac FIX raw + unique ptr tests
FIX issues on raw and unique pointer tests
+ Merge individual test files for unique and raw pointers and merged their test cases into a single test file for simplicity.
2024-11-05 16:36:29 +01:00
7ef2519c9c Merge pull request #9 from Fastium/architecture-bike-computer
ADD architecture (speedometer and sensor device) for the bike computer
2024-11-05 16:32:07 +01:00
dc99ac08b5 ADD speedometer tests in CI for github 2024-11-05 16:21:51 +01:00
ac8e030089 ADD speedometer implementation 2024-11-05 15:30:12 +01:00
0d18073562 ADD (WIP) speedometer implementation 2024-10-29 17:10:58 +01:00
db2d5308bc ADD tests for speedometer and sensor-device 2024-10-29 16:16:30 +01:00
0d4e603a64 ADD speedometer and gear class 2024-10-29 16:15:32 +01:00
b9fda1aefe ADD Tests for the sensor 2024-10-29 16:13:20 +01:00
b1d04badfb ADD sensor device classes 2024-10-23 10:59:38 +02:00
a2d3ca3096 Merge pull request #6 from Fastium/library-disco-sensor
Library for sensor and disco board with tests
2024-10-23 09:24:39 +02:00
0634d16ba3 UPD test for github action (sensor) 2024-10-23 08:54:27 +02:00
5ef0203a27 UPD README.md for wrong symbol to illustrate commands 2024-10-22 15:48:08 +02:00
f8ff28aef6 UDP README.md for additional libraries and test 2024-10-22 15:44:41 +02:00
86ccb3ca17 ADD libraries for disco board and extarnal sensor 2024-10-22 15:37:47 +02:00
53 changed files with 3106 additions and 514 deletions

View File

@ -1,6 +1,5 @@
name: Build test application
on:
pull_request:
push:
jobs:
@ -13,12 +12,11 @@ jobs:
strategy:
matrix:
target: [DISCO_H747I]
profile: [develop, debug, release]
profile: [debug]
tests: [
tests-simple-test-always-succeed,
tests-simple-test-ptr-test,
tests-simple-unique-ptr,
tests-simple-test-raw-ptr
tests-bike-computer-sensor-device,
tests-bike-computer-speedometer,
tests-bike-computer-bike-system,
]

3
.gitignore vendored
View File

@ -4,3 +4,6 @@ projectfiles
*.py*
BUILD
mbed-os
DISCO_h747i
advdembsof_library
update-client

View File

@ -14,4 +14,5 @@ mbed-os/features/FEATURE_BOOTLOADER/*
mbed-os/features/frameworks/mbed-client-cli/*
mbed-os/features/frameworks/COMPONENT_FPGA_CI_TEST_SHIELD/*
mbed-os/platform/randlib/*
mbed-os/storage/kvstore/*
mbed-os/storage/kvstore/*
mbed-os-bootloader/*

View File

@ -1,24 +1,26 @@
files: ^main.cpp
files: ^main.cpp|^static_scheduling|^static_scheduling_with_event|^TESTS
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-yaml
args: [--allow-multiple-documents]
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: 'v14.0.6'
hooks:
- id: clang-format
- repo: https://github.com/cpplint/cpplint
rev: '1.6.1'
hooks:
- id: cpplint
- repo: local
hooks:
- id: cppcheck
name: cppcheck
require_serial: true
entry: cppcheck --enable=all --suppress=missingInclude:* --inline-suppr -i mbed-os --std=c++14 --error-exitcode=1
language: system
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-yaml
args: [--allow-multiple-documents]
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: "v14.0.6"
hooks:
- id: clang-format
- repo: https://github.com/cpplint/cpplint
rev: "1.6.1"
hooks:
- id: cpplint
name: cpplint
entry: cpplint --linelength=90 --filter=-build/include_subdir,-whitespace/indent,-build/namespaces,-build/c++11
- repo: local
hooks:
- id: cppcheck
name: cppcheck
require_serial: true
entry: cppcheck --enable=all --suppress=missingInclude --suppress=missingIncludeSystem --inline-suppr -i mbed-os --std=c++14 --error-exitcode=1
language: system

View File

@ -1,5 +0,0 @@
# Contributing to Mbed OS
Mbed OS is an open-source, device software platform for the Internet of Things. Contributions are an important part of the platform, and our goal is to make it as simple as possible to become a contributor.
To encourage productive collaboration, as well as robust, consistent and maintainable code, we have a set of guidelines for [contributing to Mbed OS](https://os.mbed.com/docs/mbed-os/latest/contributing/index.html).

1
DISCO_H747I.lib Normal file
View File

@ -0,0 +1 @@
https://github.com/SergeAyer/DISCO_H747I/#2b367efbbf0d66205403647c0d0227808d0529ed

165
LICENSE
View File

@ -1,165 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and
distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright
owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities
that control, are controlled by, or are under common control with that entity.
For the purposes of this definition, "control" means (i) the power, direct or
indirect, to cause the direction or management of such entity, whether by
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising
permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including
but not limited to software source code, documentation source, and configuration
files.
"Object" form shall mean any form resulting from mechanical transformation or
translation of a Source form, including but not limited to compiled object code,
generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made
available under the License, as indicated by a copyright notice that is included
in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that
is based on (or derived from) the Work and for which the editorial revisions,
annotations, elaborations, or other modifications represent, as a whole, an
original work of authorship. For the purposes of this License, Derivative Works
shall not include works that remain separable from, or merely link (or bind by
name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version
of the Work and any modifications or additions to that Work or Derivative Works
thereof, that is intentionally submitted to Licensor for inclusion in the Work
by the copyright owner or by an individual or Legal Entity authorized to submit
on behalf of the copyright owner. For the purposes of this definition,
"submitted" means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems, and
issue tracking systems that are managed by, or on behalf of, the Licensor for
the purpose of discussing and improving the Work, but excluding communication
that is conspicuously marked or otherwise designated in writing by the copyright
owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
of whom a Contribution has been received by Licensor and subsequently
incorporated within the Work.
2. Grant of Copyright License.
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the Work and such
Derivative Works in Source or Object form.
3. Grant of Patent License.
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to make, have
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
such license applies only to those patent claims licensable by such Contributor
that are necessarily infringed by their Contribution(s) alone or by combination
of their Contribution(s) with the Work to which such Contribution(s) was
submitted. If You institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
Contribution incorporated within the Work constitutes direct or contributory
patent infringement, then any patent licenses granted to You under this License
for that Work shall terminate as of the date such litigation is filed.
4. Redistribution.
You may reproduce and distribute copies of the Work or Derivative Works thereof
in any medium, with or without modifications, and in Source or Object form,
provided that You meet the following conditions:
You must give any other recipients of the Work or Derivative Works a copy of
this License; and
You must cause any modified files to carry prominent notices stating that You
changed the files; and
You must retain, in the Source form of any Derivative Works that You distribute,
all copyright, patent, trademark, and attribution notices from the Source form
of the Work, excluding those notices that do not pertain to any part of the
Derivative Works; and
If the Work includes a "NOTICE" text file as part of its distribution, then any
Derivative Works that You distribute must include a readable copy of the
attribution notices contained within such NOTICE file, excluding those notices
that do not pertain to any part of the Derivative Works, in at least one of the
following places: within a NOTICE text file distributed as part of the
Derivative Works; within the Source form or documentation, if provided along
with the Derivative Works; or, within a display generated by the Derivative
Works, if and wherever such third-party notices normally appear. The contents of
the NOTICE file are for informational purposes only and do not modify the
License. You may add Your own attribution notices within Derivative Works that
You distribute, alongside or as an addendum to the NOTICE text from the Work,
provided that such additional attribution notices cannot be construed as
modifying the License.
You may add Your own copyright statement to Your modifications and may provide
additional or different license terms and conditions for use, reproduction, or
distribution of Your modifications, or for any such Derivative Works as a whole,
provided Your use, reproduction, and distribution of the Work otherwise complies
with the conditions stated in this License.
5. Submission of Contributions.
Unless You explicitly state otherwise, any Contribution intentionally submitted
for inclusion in the Work by You to the Licensor shall be under the terms and
conditions of this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify the terms of
any separate license agreement you may have executed with Licensor regarding
such Contributions.
6. Trademarks.
This License does not grant permission to use the trade names, trademarks,
service marks, or product names of the Licensor, except as required for
reasonable and customary use in describing the origin of the Work and
reproducing the content of the NOTICE file.
7. Disclaimer of Warranty.
Unless required by applicable law or agreed to in writing, Licensor provides the
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
including, without limitation, any warranties or conditions of TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
solely responsible for determining the appropriateness of using or
redistributing the Work and assume any risks associated with Your exercise of
permissions under this License.
8. Limitation of Liability.
In no event and under no legal theory, whether in tort (including negligence),
contract, or otherwise, unless required by applicable law (such as deliberate
and grossly negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special, incidental,
or consequential damages of any character arising as a result of this License or
out of the use or inability to use the Work (including but not limited to
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
any and all other commercial damages or losses), even if such Contributor has
been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability.
While redistributing the Work or Derivative Works thereof, You may choose to
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
other liability obligations and/or rights consistent with this License. However,
in accepting such obligations, You may act only on Your own behalf and on Your
sole responsibility, not on behalf of any other Contributor, and only if You
agree to indemnify, defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason of your
accepting any such warranty or additional liability.

View File

@ -5,4 +5,86 @@ AdvEmbSoft/HESSO-Master/2024
# BikeComputer
This is a project done in the course of AdvEmbSoft during the master's degree. It contains a program for spinning bikes.
# Configuration
## Libraries
Add disco libraries :
```terminal
mbed add https://github.com/SergeAyer/DISCO_H747I.git
```
Add sensor libraries :
```terminal
mbed add https://github.com/SergeAyer/advdembsof_library.git
```
Test sensor libraries :
```terminal
mbed test -m DISCO_H747I -t GCC_ARM -n advdembsof_library-tests-sensors-hdc1000 --compile --run
```
## Run static scheduling
On `.mbedignore` put at the end of the file
```
static_scheduling_with_event/*
```
On main.cpp include `"static_scheduling/bike_system.hpp"` and use :
```cpp
static_scheduling::BikeSystem bikeSystem;
bikeSystem.start();
```
## Run static scheduling with event queue
On `.mbedignore` put at the end of the file :
```
static_scheduling_with_event/*
```
On main.cpp include `"static_scheduling/bike_system.hpp"` and use :
```cpp
static_scheduling::BikeSystem bikeSystem;
bikeSystem.startWithEventQueue();
```
## Run static scheduling with event scheduling
On `.mbedignore` put at the end of the file
```
static_scheduling/*
```
On main.cpp include `"static_scheduling_with_event/bike_system.hpp"` and use :
```cpp
static_scheduling_with_event::BikeSystem bikeSystem;
bikeSystem.start();
```
# Some questions
## Question 1
`If you print CPU statistics at the end of every major cycle (in the super-loop), what CPU usage do you observe? How can you explain the observed CPU uptime?`
We observe a 100% usage because on each CPU cycle it compare if time is done.
## Question 2
`If you run the program after the change from busy wait to sleep calls, what CPU usage do you observe? How can you explain the observed CPU uptime?`
We can observe only a usage of 75% because the CPU is more on Idle with Thread sleep.
## Question 3
`If you run the static_scheduling_with_event program, what CPU usage do you observe? How can you explain the observed CPU uptime?`
We observe a light usage of 1% of CPU. The CPU is now sleeping all the time and doing small task only on event.
## Question 4
`When you run multiple tests for computing the response time of the reset event, what do you observe? Is there an improvement as compared to the static_scheduling::BikeSystem implementation?`
` - If you do not press long enough on the push button, the event may be missed and no reset happens.`
`Based on the program itself and on the task scheduling, explain these two behaviors. Explain also why such behaviors may be problematic.`
We notice, that we miss such less event when is event driven (or not at all). But with a static scheduling the response time is still long because the reset task is call with a certain period.
# Issues
When compile with GCC, the full loop of static scheduling is 2 to 3 ms faster than expected.
This problem doesn't occur if we compile with ARMC6.
As the acceptable delta is 2ms and the teacher test is done with GCC, we modify the delta on the test to be 3ms

View File

@ -0,0 +1,163 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file main.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Bike computer test suite: scheduling
*
* @date 2023-08-26
* @version 0.1.0
***************************************************************************/
#include <chrono>
#include "greentea-client/test_env.h"
#include "mbed.h"
#include "static_scheduling/bike_system.hpp"
#include "static_scheduling_with_event/bike_system.hpp"
#include "task_logger.hpp"
#include "unity/unity.h"
#include "utest/utest.h"
namespace utest {
namespace v1 {
// test_bike_system handler function
static void test_bike_system() {
// create the BikeSystem instance
static_scheduling::BikeSystem bikeSystem;
// run the bike system in a separate thread
Thread thread;
thread.start(callback(&bikeSystem, &static_scheduling::BikeSystem::start));
// let the bike system run for 20 secs
ThisThread::sleep_for(20s);
// stop the bike system
bikeSystem.stop();
// check whether scheduling was correct
// Order is kGearTaskIndex, kSpeedTaskIndex, kTemperatureTaskIndex,
// kResetTaskIndex, kDisplayTask1Index, kDisplayTask2Index
constexpr std::chrono::microseconds taskComputationTimes[] = {
100000us, 200000us, 100000us, 100000us, 200000us, 100000us};
constexpr std::chrono::microseconds taskPeriods[] = {
800000us, 400000us, 1600000us, 800000us, 1600000us, 1600000us};
// allow for 2 msecs offset
uint64_t deltaUs = 3000;
for (uint8_t taskIndex = 0; taskIndex < advembsof::TaskLogger::kNbrOfTasks;
taskIndex++) {
TEST_ASSERT_UINT64_WITHIN(
deltaUs,
taskPeriods[taskIndex].count(),
bikeSystem.getTaskLogger().getPeriod(taskIndex).count());
TEST_ASSERT_UINT64_WITHIN(
deltaUs,
taskComputationTimes[taskIndex].count(),
bikeSystem.getTaskLogger().getComputationTime(taskIndex).count());
}
}
// test_bike_system_event_queue handler function
static void test_bike_system_event_queue() {
// create the BikeSystem instance
static_scheduling::BikeSystem bikeSystem;
// run the bike system in a separate thread
Thread thread;
thread.start(
callback(&bikeSystem, &static_scheduling::BikeSystem::startWithEventQueue));
// let the bike system run for 20 secs
ThisThread::sleep_for(20s);
// stop the bike system
bikeSystem.stop();
// check whether scheduling was correct
// Order is kGearTaskIndex, kSpeedTaskIndex, kTemperatureTaskIndex,
// kResetTaskIndex, kDisplayTask1Index, kDisplayTask2Index
// When we use the event queue, we do not check the computation time
constexpr std::chrono::microseconds taskPeriods[] = {
800000us, 400000us, 1600000us, 800000us, 1600000us, 1600000us};
// allow for 2 msecs offset (with EventQueue)
uint64_t deltaUs = 3000;
for (uint8_t taskIndex = 0; taskIndex < advembsof::TaskLogger::kNbrOfTasks;
taskIndex++) {
TEST_ASSERT_UINT64_WITHIN(
deltaUs,
taskPeriods[taskIndex].count(),
bikeSystem.getTaskLogger().getPeriod(taskIndex).count());
}
}
// test_bike_system_with_event handler function
static void test_bike_system_with_event() {
// create the BikeSystem instance
static_scheduling_with_event::BikeSystem bikeSystem;
// run the bike system in a separate thread
Thread thread;
thread.start(callback(&bikeSystem, &static_scheduling_with_event::BikeSystem::start));
// let the bike system run for 20 secs
ThisThread::sleep_for(20s);
// stop the bike system
bikeSystem.stop();
// check whether scheduling was correct
// Order is kGearTaskIndex, kSpeedTaskIndex, kTemperatureTaskIndex,
// kResetTaskIndex, kDisplayTask1Index, kDisplayTask2Index
// When we use event handling, we do not check the computation time
constexpr std::chrono::microseconds taskPeriods[] = {
800000us, 400000us, 1600000us, 800000us, 1600000us, 1600000us};
// allow for 2 msecs offset (with EventQueue)
uint64_t deltaUs = 3000;
for (uint8_t taskIndex = 0; taskIndex < advembsof::TaskLogger::kNbrOfTasks;
taskIndex++) {
TEST_ASSERT_UINT64_WITHIN(
deltaUs,
taskPeriods[taskIndex].count(),
bikeSystem.getTaskLogger().getPeriod(taskIndex).count());
}
}
static status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test
// or the name of our Python file)
GREENTEA_SETUP(180, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}
// List of test cases in this file
static Case cases[] = {
Case("test bike system", test_bike_system),
Case("test bike system with event queue", test_bike_system_event_queue),
Case("test bike system with event handling", test_bike_system_with_event),
};
static Specification specification(greentea_setup, cases);
}; // namespace v1
}; // namespace utest
int main() { return !utest::v1::Harness::run(utest::v1::specification); }

View File

@ -0,0 +1,72 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file main.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Bike computer test suite: sensor device
*
* @date 2023-08-26
* @version 0.1.0
***************************************************************************/
#include "greentea-client/test_env.h"
#include "hdc1000.hpp"
#include "mbed.h"
#include "sensor_device.hpp"
#include "unity/unity.h"
#include "utest/utest.h"
namespace utest {
namespace v1 {
// test_hdc1000 test handler function
static control_t test_sensor_device(const size_t call_count) {
// create the SensorDevice instance
bike_computer::SensorDevice sensorDevice;
bool rc = sensorDevice.init();
TEST_ASSERT_TRUE(rc);
float temperature = sensorDevice.readTemperature();
static constexpr float kTemperatureRange = 20.0f;
static constexpr float kMeanTemperature = 15.0f;
TEST_ASSERT_FLOAT_WITHIN(kTemperatureRange, kMeanTemperature, temperature);
float humidity = sensorDevice.readHumidity();
static constexpr float kHumidityRange = 40.0f;
static constexpr float kMeanHumidity = 50.0f;
TEST_ASSERT_FLOAT_WITHIN(kHumidityRange, kMeanHumidity, humidity);
// execute the test only once and move to the next one, without waiting
return CaseNext;
}
static status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test
// or the name of our Python file)
GREENTEA_SETUP(60, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}
// List of test cases in this file
static Case cases[] = {Case("test sensor device", test_sensor_device)};
static Specification specification(greentea_setup, cases);
}; // namespace v1
}; // namespace utest
int main() { return !utest::v1::Harness::run(utest::v1::specification); }

View File

@ -0,0 +1,359 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file main.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Bike computer test suite: speedometer device
*
* @date 2023-08-26
* @version 0.1.0
***************************************************************************/
#include <chrono>
#include "common/constants.hpp"
#include "common/speedometer.hpp"
#include "greentea-client/test_env.h"
#include "mbed.h"
#include "static_scheduling/gear_device.hpp"
#include "unity/unity.h"
#include "utest/utest.h"
// allow for 0.1 km/h difference
static constexpr float kAllowedSpeedDelta = 0.1f;
// allow for 1m difference
static constexpr float kAllowedDistanceDelta = 1.0f / 1000.0;
namespace utest {
namespace v1 {
// function called by test handler functions for verifying the current speed
void check_current_speed(const std::chrono::milliseconds& pedalRotationTime,
uint8_t traySize,
uint8_t gearSize,
float wheelCircumference,
float currentSpeed) {
// compute the number of pedal rotation per hour
uint32_t milliSecondsPerHour = 1000 * 3600;
float pedalRotationsPerHour = static_cast<float>(milliSecondsPerHour) /
static_cast<float>(pedalRotationTime.count());
// compute the expected speed in km / h
// first compute the distance in meter for each pedal turn
float trayGearRatio = static_cast<float>(traySize) / static_cast<float>(gearSize);
float distancePerPedalTurn = trayGearRatio * wheelCircumference;
float expectedSpeed = (distancePerPedalTurn / 1000.0f) * pedalRotationsPerHour;
printf(" Expected speed is %f, current speed is %f\n", expectedSpeed, currentSpeed);
TEST_ASSERT_FLOAT_WITHIN(kAllowedSpeedDelta, expectedSpeed, currentSpeed);
}
// compute the traveled distance for a time interval
float compute_distance(const std::chrono::milliseconds& pedalRotationTime,
uint8_t traySize,
uint8_t gearSize,
float wheelCircumference,
const std::chrono::milliseconds& travelTime) {
// compute the number of pedal rotation during travel time
// both times are expressed in ms
float pedalRotations = static_cast<float>(travelTime.count()) /
static_cast<float>(pedalRotationTime.count());
// compute the distance in meter for each pedal turn
float trayGearRatio = static_cast<float>(traySize) / static_cast<float>(gearSize);
float distancePerPedalTurn = trayGearRatio * wheelCircumference;
// distancePerPedalTurn is expressed in m, divide per 1000 for a distance in
// km
return (distancePerPedalTurn * pedalRotations) / 1000.0;
}
// function called by test handler functions for verifying the distance traveled
void check_distance(const std::chrono::milliseconds& pedalRotationTime,
uint8_t traySize,
uint8_t gearSize,
float wheelCircumference,
const std::chrono::milliseconds& travelTime,
float distance) {
// distancePerPedalTurn is expressed in m, divide per 1000 for a distance in
// km
float expectedDistance = compute_distance(
pedalRotationTime, traySize, gearSize, wheelCircumference, travelTime);
printf(" Expected distance is %f, current distance is %f\n",
expectedDistance,
distance);
TEST_ASSERT_FLOAT_WITHIN(kAllowedDistanceDelta, expectedDistance, distance);
}
// test the speedometer by modifying the gear
static control_t test_gear_size(const size_t call_count) {
// create a timer
Timer timer;
// start the timer
timer.start();
// create a speedometer instance
bike_computer::Speedometer speedometer(timer);
// get speedometer constant values (for this test)
const auto traySize = speedometer.getTraySize();
const auto wheelCircumference = speedometer.getWheelCircumference();
const auto pedalRotationTime = speedometer.getCurrentPedalRotationTime();
for (uint8_t gearSize = bike_computer::kMinGearSize;
gearSize <= bike_computer::kMaxGearSize;
gearSize++) {
// set the gear
printf("Testing gear size %d\n", gearSize);
speedometer.setGearSize(gearSize);
// get the current speed
auto currentSpeed = speedometer.getCurrentSpeed();
// check the speed against the expected one
check_current_speed(
pedalRotationTime, traySize, gearSize, wheelCircumference, currentSpeed);
}
// execute the test only once and move to the next one, without waiting
return CaseNext;
}
// test the speedometer by modifying the pedal rotation speed
static control_t test_rotation_speed(const size_t call_count) {
// create a timer
Timer timer;
// start the timer
timer.start();
// create a speedometer instance
bike_computer::Speedometer speedometer(timer);
// set the gear size
speedometer.setGearSize(bike_computer::kMaxGearSize);
// get speedometer constant values
const auto traySize = speedometer.getTraySize();
const auto wheelCircumference = speedometer.getWheelCircumference();
const auto gearSize = speedometer.getGearSize();
// first test increasing rotation speed (decreasing rotation time)
auto pedalRotationTime = speedometer.getCurrentPedalRotationTime();
while (pedalRotationTime > bike_computer::kMinPedalRotationTime) {
// decrease the pedal rotation time
pedalRotationTime -= bike_computer::kDeltaPedalRotationTime;
speedometer.setCurrentRotationTime(pedalRotationTime);
// get the current speed
const auto currentSpeed = speedometer.getCurrentSpeed();
// check the speed against the expected one
check_current_speed(
pedalRotationTime, traySize, gearSize, wheelCircumference, currentSpeed);
}
// second test decreasing rotation speed (increasing rotation time)
pedalRotationTime = speedometer.getCurrentPedalRotationTime();
while (pedalRotationTime < bike_computer::kMaxPedalRotationTime) {
// increase the pedal rotation time
pedalRotationTime += bike_computer::kDeltaPedalRotationTime;
speedometer.setCurrentRotationTime(pedalRotationTime);
// get the current speed
const auto currentSpeed = speedometer.getCurrentSpeed();
// check the speed against the expected one
check_current_speed(
pedalRotationTime, traySize, gearSize, wheelCircumference, currentSpeed);
}
// execute the test only once and move to the next one, without waiting
return CaseNext;
}
// test the speedometer by modifying the pedal rotation speed
static control_t test_distance(const size_t call_count) {
// create a timer
Timer timer;
// create a speedometer instance
bike_computer::Speedometer speedometer(timer);
// set the gear size
speedometer.setGearSize(bike_computer::kMaxGearSize);
// get speedometer constant values
const auto traySize = speedometer.getTraySize();
const auto wheelCircumference = speedometer.getWheelCircumference();
auto gearSize = speedometer.getGearSize();
auto pedalRotationTime = speedometer.getCurrentPedalRotationTime();
// test different travel times
const std::chrono::milliseconds travelTimes[] = {500ms, 1000ms, 5s, 10s};
const uint8_t nbrOfTravelTimes = sizeof(travelTimes) / sizeof(travelTimes[0]);
// start the timer (for simulating bike start)
timer.start();
// first check travel distance without changing gear and rotation speed
std::chrono::milliseconds totalTravelTime = std::chrono::milliseconds::zero();
for (uint8_t index = 0; index < nbrOfTravelTimes; index++) {
// run for the travel time and get the distance
ThisThread::sleep_for(travelTimes[index]);
// get the distance traveled
const auto distance = speedometer.getDistance();
// accumulate travel time
totalTravelTime += travelTimes[index];
// check the distance vs the expected one
check_distance(pedalRotationTime,
traySize,
gearSize,
wheelCircumference,
totalTravelTime,
distance);
}
// now change gear at each time interval
auto expectedDistance = speedometer.getDistance();
for (uint8_t index = 0; index < nbrOfTravelTimes; index++) {
// update the gear size
gearSize++;
speedometer.setGearSize(gearSize);
// run for the travel time and get the distance
ThisThread::sleep_for(travelTimes[index]);
// compute the expected distance for this time segment
float distance = compute_distance(pedalRotationTime,
traySize,
gearSize,
wheelCircumference,
travelTimes[index]);
expectedDistance += distance;
// get the distance traveled
const auto traveledDistance = speedometer.getDistance();
printf(" Expected distance is %f, current distance is %f\n",
expectedDistance,
traveledDistance);
TEST_ASSERT_FLOAT_WITHIN(
kAllowedDistanceDelta, expectedDistance, traveledDistance);
}
// now change rotation speed at each time interval
expectedDistance = speedometer.getDistance();
for (uint8_t index = 0; index < nbrOfTravelTimes; index++) {
// update the rotation speed
pedalRotationTime += bike_computer::kDeltaPedalRotationTime;
speedometer.setCurrentRotationTime(pedalRotationTime);
// run for the travel time and get the distance
ThisThread::sleep_for(travelTimes[index]);
// compute the expected distance for this time segment
float distance = compute_distance(pedalRotationTime,
traySize,
gearSize,
wheelCircumference,
travelTimes[index]);
expectedDistance += distance;
// get the distance traveled
const auto traveledDistance = speedometer.getDistance();
printf(" Expected distance is %f, current distance is %f\n",
expectedDistance,
traveledDistance);
TEST_ASSERT_FLOAT_WITHIN(
kAllowedDistanceDelta, expectedDistance, traveledDistance);
}
// execute the test only once and move to the next one, without waiting
return CaseNext;
}
// test the speedometer by modifying the pedal rotation speed
static control_t test_reset(const size_t call_count) {
// create a timer instance
Timer timer;
// create a speedometer instance
bike_computer::Speedometer speedometer(timer);
// set the gear size
speedometer.setGearSize(bike_computer::kMinGearSize);
// get speedometer constant values
const auto traySize = speedometer.getTraySize();
const auto wheelCircumference = speedometer.getWheelCircumference();
const auto gearSize = speedometer.getGearSize();
const auto pedalRotationTime = speedometer.getCurrentPedalRotationTime();
// start the timer (for simulating bike start)
timer.start();
// travel for 1 second
const auto travelTime = 1000ms;
ThisThread::sleep_for(travelTime);
// check the expected distaance traveled
const auto expectedDistance = compute_distance(
pedalRotationTime, traySize, gearSize, wheelCircumference, travelTime);
// get the distance traveled
auto traveledDistance = speedometer.getDistance();
printf(" Expected distance is %f, current distance is %f\n",
expectedDistance,
traveledDistance);
TEST_ASSERT_FLOAT_WITHIN(kAllowedDistanceDelta, expectedDistance, traveledDistance);
// reset the speedometer
speedometer.reset();
// traveled distance should now be zero
traveledDistance = speedometer.getDistance();
printf(" Expected distance is %f, current distance is %f\n", 0.0f, traveledDistance);
TEST_ASSERT_FLOAT_WITHIN(kAllowedDistanceDelta, 0.0f, traveledDistance);
// execute the test only once and move to the next one, without waiting
return CaseNext;
}
static status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test
// or the name of our Python file)
GREENTEA_SETUP(180, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}
// List of test cases in this file
static Case cases[] = {
Case("test speedometer gear size change", test_gear_size),
Case("test speedometer rotation speed change", test_rotation_speed),
Case("test speedometer distance", test_distance),
Case("test speedometer reset", test_reset)};
static Specification specification(greentea_setup, cases);
}; // namespace v1
}; // namespace utest
int main() { return !utest::v1::Harness::run(utest::v1::specification); }

View File

@ -27,7 +27,8 @@
#include "unity/unity.h"
#include "utest/utest.h"
using namespace utest::v1;
namespace utest {
namespace v1 {
// test handler function
static control_t always_succeed(const size_t call_count) {
@ -38,9 +39,9 @@ static control_t always_succeed(const size_t call_count) {
return CaseNext;
}
static utest::v1::status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test or the
// name of our Python file)
static status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test
// or the name of our Python file)
GREENTEA_SETUP(60, "default_auto");
return greentea_test_setup_handler(number_of_cases);
@ -51,4 +52,7 @@ static Case cases[] = {Case("always succeed test", always_succeed)};
static Specification specification(greentea_setup, cases);
int main() { return !Harness::run(specification); }
}; // namespace v1
}; // namespace utest
int main() { return !utest::v1::Harness::run(utest::v1::specification); }

View File

@ -15,19 +15,23 @@
/****************************************************************************
* @file main.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
* @author Rémi Heredero <remi@heredero.ch>
*
* @brief Simple example of test program for raw and shared pointers
*
* @date 2022-09-01
* @version 0.1.0
* @date 2024-11-02
* @version 0.2.0
***************************************************************************/
#include "greentea-client/test_env.h"
#include "mbed.h"
#include "unity/unity.h"
#include "utest/utest.h"
#include "greentea-client/test_env.h" // NOLINT
#include "mbed.h" // NOLINT
#include "unity/unity.h" // NOLINT
#include "utest/utest.h" // NOLINT
namespace utest {
namespace v1 {
using namespace utest::v1;
struct Test {
Test() {
_instanceCount++;
@ -46,7 +50,8 @@ struct Test {
uint32_t Test::_instanceCount = 0;
/**
* Test that a shared pointer correctly manages the lifetime of the underlying raw pointer
* Test that a shared pointer correctly manages the lifetime of the underlying
* raw pointer
*/
void test_single_sharedptr_lifetime() {
// Sanity-check value of counter
@ -64,8 +69,8 @@ void test_single_sharedptr_lifetime() {
}
/**
* Test that multiple instances of shared pointers correctly manage the reference count
* to release the object at the correct point
* Test that multiple instances of shared pointers correctly manage the
* reference count to release the object at the correct point
*/
void test_instance_sharing() {
std::shared_ptr<Test> shared_ptr1(nullptr);
@ -94,18 +99,195 @@ void test_instance_sharing() {
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
static utest::v1::status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test or the
// name of our Python file)
/**********************
* UNIQUE PTR EXERCISE *
**********************/
/*
* Check normal lifetime on a unique_ptr
*/
void test_single_unique_ptr_lifetime() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
// create and destroy a unique_ptr
{
std::unique_ptr<Test> p1 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
const uint32_t number = 42;
p1->_value = number;
TEST_ASSERT_EQUAL(number, p1->_value);
p1.reset();
TEST_ASSERT(!p1);
}
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
/*
* Check transfer on a unique_ptr
*/
void test_unique_ptr_transfer() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
{
// create p1
std::unique_ptr<Test> p1 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
// transfer p1 to p2
std::unique_ptr<Test> p2 = std::move(p1);
TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
p2.reset();
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
TEST_ASSERT(!p1); // cppcheck-suppress accessMoved
TEST_ASSERT(!p2);
}
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
/*
* Check the release of a unique ptr
*/
void test_unique_ptr_release() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
{
// create p1
std::unique_ptr<Test> p1 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
// transfer and release p1 to p2
Test* p2 = p1.release();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
delete p2;
p2 = nullptr;
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
TEST_ASSERT(!p1);
TEST_ASSERT(!p2);
}
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
/*
* Check the swap of 2 unique ptr
*/
void test_unique_ptr_swap() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
{
const uint32_t number1 = 65;
const uint32_t number2 = 42;
// create p1
std::unique_ptr<Test> p1 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
p1->_value = number1;
TEST_ASSERT_EQUAL(number1, p1->_value);
// create p2
std::unique_ptr<Test> p2 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value);
TEST_ASSERT_EQUAL(2, Test::_instanceCount);
p2->_value = number2;
TEST_ASSERT_EQUAL(number2, p2->_value);
// swap
p1.swap(p2);
TEST_ASSERT_EQUAL(number1, p2->_value);
TEST_ASSERT_EQUAL(number2, p1->_value);
p1.reset();
p2.reset();
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
TEST_ASSERT(!p1);
TEST_ASSERT(!p2);
}
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
/*******************
* RAW PTR EXERCISE *
*******************/
/**
* Test that a shared pointer correctly manages the lifetime of the underlying
* raw pointer
*/
void test_single_raw_ptr_lifetime() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
// Create and destroy raw pointer in given scope
{
Test t1;
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
TEST_ASSERT_EQUAL(Test::kMagicNumber, t1._value);
Test* p1 = &t1;
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
const uint32_t number1 = 42;
p1->_value = number1;
TEST_ASSERT_EQUAL(number1, p1->_value);
TEST_ASSERT_EQUAL(number1, t1._value);
p1 = nullptr;
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
TEST_ASSERT(!p1);
}
// Destroy shared pointer
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
static status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test
// or the name of our Python file)
GREENTEA_SETUP(60, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}
// List of test cases in this file
static Case cases[] = {
// Shared test pointer
Case("Test single shared pointer instance", test_single_sharedptr_lifetime),
Case("Test instance sharing across multiple shared pointers", test_instance_sharing)};
Case("Test instance sharing across multiple shared pointers", test_instance_sharing),
// Unique test pointer
Case("Test single unique pointer instance", test_single_unique_ptr_lifetime),
Case("Test transfer of unique pointer instance", test_unique_ptr_transfer),
Case("Test release of unique pointer instance", test_unique_ptr_release),
Case("Test swap of 2 unique ptr instance", test_unique_ptr_swap),
// Raw test pointer
Case("Test single raw pointer instance", test_single_raw_ptr_lifetime),
};
static Specification specification(greentea_setup, cases);
int main() { return !Harness::run(specification); }
}; // namespace v1
}; // namespace utest
int main() { return !utest::v1::Harness::run(utest::v1::specification); }

View File

@ -1,96 +0,0 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file main.cpp
* @author Sierro Yann
*
* @brief Test of raw pointer
*
* @date 2022-09-01
* @version 0.1.0
***************************************************************************/
#include "greentea-client/test_env.h"
#include "mbed.h"
#include "unity/unity.h"
#include "utest/utest.h"
using namespace utest::v1;
struct Test {
Test() {
_instanceCount++;
_value = kMagicNumber;
}
~Test() {
_instanceCount--;
_value = 0;
}
int _value;
static constexpr uint32_t kMagicNumber = 33;
static uint32_t _instanceCount;
};
uint32_t Test::_instanceCount = 0;
/**
* Test that a shared pointer correctly manages the lifetime of the underlying raw pointer
*/
void test_single_raw_ptr_lifetime() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
// Create and destroy raw pointer in given scope
{
Test t1;
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
TEST_ASSERT_EQUAL(Test::kMagicNumber, t1._value);
Test * p1 = &t1;
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
const uint32_t number1 = 42;
p1->_value = number1;
TEST_ASSERT_EQUAL(number1, p1->_value);
TEST_ASSERT_EQUAL(number1, t1._value);
p1 = nullptr;
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
TEST_ASSERT(!p1);
t1.~Test();
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
// Destroy shared pointer
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
static utest::v1::status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test or the
// name of our Python file)
GREENTEA_SETUP(60, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}
// List of test cases in this file
static Case cases[] = {
Case("Test single raw pointer instance", test_single_raw_ptr_lifetime)};
static Specification specification(greentea_setup, cases);
int main() { return !Harness::run(specification); }

View File

@ -1,189 +0,0 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file main.cpp
* @author Sierro Yann
*
* @brief Test for std::unique_ptr template class
*
* @date 2024-10-20
* @version 0.1.0
***************************************************************************/
#include "greentea-client/test_env.h"
#include "mbed.h"
#include "unity/unity.h"
#include "utest/utest.h"
#include <memory>
using namespace utest::v1;
struct Test {
Test() {
_instanceCount++;
_value = kMagicNumber;
}
~Test() {
_instanceCount--;
_value = 0;
}
uint32_t _value;
static constexpr uint32_t kMagicNumber = 33;
static uint32_t _instanceCount;
};
uint32_t Test::_instanceCount = 0;
/*
* Check normal lifetime on a unique_ptr
*/
void test_single_unique_ptr_lifetime() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
// create and destroy a unique_ptr
{
std::unique_ptr<Test> p1 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
const uint32_t number = 42;
p1->_value = number;
TEST_ASSERT_EQUAL(number, p1->_value);
p1.reset();
TEST_ASSERT(!p1);
}
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
/*
* Check transfer on a unique_ptr
*/
void test_unique_ptr_transfer() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
{
//create p1
std::unique_ptr<Test> p1 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
//transfer p1 to p2
std::unique_ptr<Test> p2 = std::move(p1);
TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
p2.reset();
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
TEST_ASSERT(!p1);
TEST_ASSERT(!p2);
}
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
/*
* Check the release of a unique ptr
*/
void test_unique_ptr_release() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
{
//create p1
std::unique_ptr<Test> p1 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
//transfer and release p1 to p2
Test * p2 = p1.release();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
delete p2;
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
TEST_ASSERT(!p1);
TEST_ASSERT(!p2);
}
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
/*
* Check the swap of 2 unique ptr
*/
void test_unique_ptr_swap() {
// Sanity-check value of counter
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
{
const uint32_t number1 = 65;
const uint32_t number2 = 42;
//create p1
std::unique_ptr<Test> p1 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
p1->_value = number1;
TEST_ASSERT_EQUAL(number1, p1->_value);
//create p2
std::unique_ptr<Test> p2 = std::make_unique<Test>();
TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value);
TEST_ASSERT_EQUAL(1, Test::_instanceCount);
p2->_value = number2;
TEST_ASSERT_EQUAL(number2, p2->_value);
//swap
p1.swap(p2);
TEST_ASSERT_EQUAL(number1, p2->_value);
TEST_ASSERT_EQUAL(number2, p1->_value);
p1.reset();
p2.reset();
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
TEST_ASSERT(!p1);
TEST_ASSERT(!p2);
}
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
}
static utest::v1::status_t greentea_setup(const size_t number_of_cases) {
// Here, we specify the timeout (60s) and the host test (a built-in host test or the
// name of our Python file)
GREENTEA_SETUP(60, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}
// List of test cases in this file
static Case cases[] = {
Case("Test single unique pointer instance", test_single_unique_ptr_lifetime),
Case("Test transfer of unique pointer instance", test_unique_ptr_transfer),
Case("Test release of unique pointer instance", test_unique_ptr_release),
Case("Test swap of 2 unique ptr instance", test_unique_ptr_swap)
};
static Specification specification(greentea_setup, cases);
int main() { return !Harness::run(specification); }

1
advdembsof_library.lib Normal file
View File

@ -0,0 +1 @@
https://github.com/SergeAyer/advdembsof_library/#c0687fc795de83780fa9e7e935c9f465d697cc2a

60
common/constants.hpp Normal file
View File

@ -0,0 +1,60 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file constants.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Constants definition used for implementing the bike system
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#pragma once
#include <stdint.h>
#include "mbed.h"
namespace bike_computer {
// gear related constants
static constexpr uint8_t kMinGear = 1;
static constexpr uint8_t kMaxGear = 9;
// smallest gear (= 1) corresponds to a gear size of 20
// when the gear increases, the gear size descreases
static constexpr uint8_t kMaxGearSize = 20;
static constexpr uint8_t kMinGearSize = kMaxGearSize - kMaxGear;
// pedal related constants
// When compiling and linking with gcc, we get a link error when using static
// constexpr. The error is related to template instantiation.
// definition of pedal rotation initial time (corresponds to 80 turn / min)
static constexpr std::chrono::milliseconds kInitialPedalRotationTime = 750ms;
// definition of pedal minimal rotation time (corresponds to 160 turn / min)
static constexpr std::chrono::milliseconds kMinPedalRotationTime = 375ms;
// definition of pedal maximal rotation time (corresponds to 10 turn / min)
static constexpr std::chrono::milliseconds kMaxPedalRotationTime = 1500ms;
// definition of pedal rotation time change upon acceleration/deceleration
static constexpr std::chrono::milliseconds kDeltaPedalRotationTime = 25ms;
static constexpr uint32_t kNbrOfSteps = static_cast<uint32_t>(
(
bike_computer::kMaxPedalRotationTime - bike_computer::kMinPedalRotationTime
).count() / bike_computer::kDeltaPedalRotationTime.count()
);
} // namespace bike_computer

21
common/sensor_device.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "sensor_device.hpp"
namespace bike_computer {
SensorDevice::SensorDevice() : _hdc1000(I2C_SDA, I2C_SCL, STMOD_11)
{}
bool SensorDevice::init() {
return this->_hdc1000.probe();
}
float SensorDevice::readTemperature(void) {
return this->_hdc1000.getTemperature();
}
float SensorDevice::readHumidity(void) {
return this->_hdc1000.getHumidity();
}
} // bike_computer

49
common/sensor_device.hpp Normal file
View File

@ -0,0 +1,49 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file sensor_device.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief SensorDevice header file (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#pragma once
#include "hdc1000.hpp"
#include "mbed.h"
namespace bike_computer {
class SensorDevice {
public:
// constructor
SensorDevice();
// method for initializing the device
bool init();
// methods used for
float readTemperature(void);
float readHumidity(void);
private:
// data members
advembsof::HDC1000 _hdc1000;
};
} // namespace bike_computer

144
common/speedometer.cpp Normal file
View File

@ -0,0 +1,144 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file speedometer_device.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief WheelCounterDevice implementation (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#include "speedometer.hpp"
#include "static_scheduling/gear_device.hpp"
#include <chrono>
#include <ratio>
// from disco_h747i/wrappers
#include "joystick.hpp"
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "Speedometer"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace bike_computer {
Speedometer::Speedometer(Timer& timer) : _timer(timer) {
// update _lastTime
_lastTime = _timer.elapsed_time();
}
void Speedometer::setCurrentRotationTime(
const std::chrono::milliseconds& currentRotationTime) {
if (_pedalRotationTime != currentRotationTime) {
// compute distance before changing the rotation time
computeDistance();
// change pedal rotation time
_pedalRotationTime = currentRotationTime;
// compute speed with the new pedal rotation time
computeSpeed();
}
}
void Speedometer::setGearSize(uint8_t gearSize) {
if (_gearSize != gearSize) {
// compute distance before chaning the gear size
computeDistance();
// change gear size
_gearSize = gearSize;
// compute speed with the new gear size
computeSpeed();
}
}
float Speedometer::getCurrentSpeed() const { return _currentSpeed; }
float Speedometer::getDistance() {
// make sure to update the distance traveled
computeDistance();
return _totalDistance;
}
void Speedometer::reset() {
// TODO : done
this->_totalDistanceMutex.lock();
this->_totalDistance = 0.0f;
this->_totalDistanceMutex.unlock();
}
#if defined(MBED_TEST_MODE)
uint8_t Speedometer::getGearSize() const { return _gearSize; }
float Speedometer::getWheelCircumference() const { return kWheelCircumference; }
float Speedometer::getTraySize() const { return kTraySize; }
std::chrono::milliseconds Speedometer::getCurrentPedalRotationTime() const {
return _pedalRotationTime;
}
#endif // defined(MBED_TEST_MODE)
void Speedometer::computeSpeed() {
// For computing the speed given a rear gear (braquet), one must divide the size of
// the tray (plateau) by the size of the rear gear (pignon arrière), and then multiply
// the result by the circumference of the wheel. Example: tray = 50, rear gear = 15.
// Distance run with one pedal turn (wheel circumference = 2.10 m) = 50/15 * 2.1 m
// = 6.99m If you ride at 80 pedal turns / min, you run a distance of 6.99 * 80 / min
// ~= 560 m / min = 33.6 km/h
// TODO : done
//Distance run with one pedal turn = tray size / rear gear size * circumference of the wheel
constexpr float ms_in_hour = static_cast<float>(3600 * 1000);
float pedal_rotation_per_hour = ms_in_hour / std::chrono::duration_cast<std::chrono::milliseconds>(_pedalRotationTime).count();
float gear_ratio = static_cast<float>(kTraySize) / static_cast<float>(this->_gearSize);
float wheel_dist_km = static_cast<float>(this->kWheelCircumference) / 1000.0;
this->_currentSpeed = gear_ratio * wheel_dist_km * pedal_rotation_per_hour;
}
void Speedometer::computeDistance() {
// For computing the speed given a rear gear (braquet), one must divide the size of
// the tray (plateau) by the size of the rear gear (pignon arrière), and then multiply
// the result by the circumference of the wheel. Example: tray = 50, rear gear = 15.
// Distance run with one pedal turn (wheel circumference = 2.10 m) = 50/15 * 2.1 m
// = 6.99m If you ride at 80 pedal turns / min, you run a distance of 6.99 * 80 / min
// ~= 560 m / min = 33.6 km/h. We then multiply the speed by the time for getting the
// distance traveled.
// TODO : done
Speedometer::computeSpeed();
// compute distance
const std::chrono::microseconds timeNow = _timer.elapsed_time();
const std::chrono::microseconds timeDiff = timeNow - _lastTime;
constexpr float ms_in_hour = static_cast<float>(3600 * 1000);
float traveled_dist = _currentSpeed * timeDiff.count() / (ms_in_hour*1000.0/*μs*/);
this->_totalDistanceMutex.lock();
this->_totalDistance += traveled_dist;
this->_totalDistanceMutex.unlock();
_lastTime = _timer.elapsed_time();
}
} // namespace bike_computer

91
common/speedometer.hpp Normal file
View File

@ -0,0 +1,91 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file speedometer_device.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief WheelCounterDevice header file (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#pragma once
#include "constants.hpp"
#include "mbed.h"
namespace bike_computer {
class Speedometer {
public:
explicit Speedometer(Timer& timer); // NOLINT(runtime/references)
// method used for setting the current pedal rotation time
void setCurrentRotationTime(const std::chrono::milliseconds& currentRotationTime);
// method used for setting/getting the current gear
void setGearSize(uint8_t gearSize);
// method called for getting the current speed (expressed in km / h)
float getCurrentSpeed() const;
// method called for getting the current traveled distance (expressed in km)
float getDistance();
// method called for resetting the traveled distance
void reset();
// methods used for tests only
#if defined(MBED_TEST_MODE)
uint8_t getGearSize() const;
float getWheelCircumference() const;
float getTraySize() const;
std::chrono::milliseconds getCurrentPedalRotationTime() const;
void setOnResetCallback(mbed::Callback<void()> cb);
#endif // defined(MBED_TEST_MODE)
private:
// private methods
void computeSpeed();
void computeDistance();
// definition of task period time
static constexpr std::chrono::milliseconds kTaskPeriod = 400ms;
// definition of task execution time
static constexpr std::chrono::microseconds kTaskRunTime = 200000us;
// constants related to speed computation
static constexpr float kWheelCircumference = 2.1f;
static constexpr uint8_t kTraySize = 50;
std::chrono::microseconds _lastTime = std::chrono::microseconds::zero();
std::chrono::milliseconds _pedalRotationTime = kInitialPedalRotationTime;
// data members
Timer& _timer;
LowPowerTicker _ticker;
float _currentSpeed = 0.0f;
Mutex _totalDistanceMutex;
float _totalDistance = 0.0f;
uint8_t _gearSize = 1;
Thread _thread;
#if defined(MBED_TEST_MODE)
mbed::Callback<void()> _cb;
#endif
};
} // namespace bike_computer

View File

@ -6,18 +6,39 @@
#if !MBED_TEST_MODE
#include "mbed.h" // NOLINT
#include "mbed_trace.h"
// #include "static_scheduling/bike_system.hpp"
#include "FlashIAPBlockDevice.h"
#include "static_scheduling_with_event/bike_system.hpp"
#include "update-client/usb_serial_uc.hpp"
// Blinking rate in milliseconds
#define BLINKING_RATE 500ms
#if defined(MBED_CONF_MBED_TRACE_ENABLE)
#define TRACE_GROUP "MAIN"
#endif // MBED_CONF_MBED_TRACE_ENAB
int main() {
// Initialise the digital pin LED1 as an output
#if defined(MBED_CONF_MBED_TRACE_ENABLE)
mbed_trace_init();
#endif
DigitalOut led(LED1);
tr_info("Start");
FlashIAPBlockDevice flashIAPBlockDevice(MBED_ROM_START, MBED_ROM_SIZE);
update_client::USBSerialUC usbSerialUpdateClient(flashIAPBlockDevice);
update_client::UCErrorCode rc = usbSerialUpdateClient.start();
if (rc != update_client::UCErrorCode::UC_ERR_NONE) {
tr_error("Cannot initialize update client: %d", rc);
} else {
tr_info("Update client started");
}
// static_scheduling::BikeSystem bikeSystem;
static_scheduling_with_event::BikeSystem bikeSystem;
tr_debug("Starting Bike System");
// bikeSystem.startWithEventQueue();
bikeSystem.start();
while (true) {
led = !led;
ThisThread::sleep_for(BLINKING_RATE);
}
}

Binary file not shown.

View File

@ -0,0 +1,12 @@
---
BasedOnStyle: Google
IndentWidth: 4
---
Language: Cpp
ColumnLimit: 90
AlignConsecutiveAssignments: true
DerivePointerAlignment: false
PointerAlignment: Left
BinPackArguments: false
BinPackParameters: false
IndentAccessModifiers: false

8
mbed-os-bootloader/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
.build
.mbed
projectfiles
*.py*
mbed-os
BUILD
!BUILD/DISCO_H747I/GCC_ARM/mbed-os-bootloader.bin
update-client

View File

@ -0,0 +1,17 @@
mbed-os/drivers/device_key/*
mbed-os/drivers/source/usb/USBMSD.cpp
mbed-os/drivers/source/SFDP.cpp
mbed-os/connectivity/cellular/*
mbed-os/connectivity/drivers/*
mbed-os/connectivity/FEATURE_BLE/*
mbed-os/connectivity/libraries/*
mbed-os/connectivity/lorawan/*
mbed-os/connectivity/lwipstack/*
mbed-os/connectivity/nanostack/*
mbed-os/connectivity/netsocket/*
mbed-os/connectivity/nfc/*
mbed-os/features/FEATURE_BOOTLOADER/*
mbed-os/features/frameworks/mbed-client-cli/*
mbed-os/features/frameworks/COMPONENT_FPGA_CI_TEST_SHIELD/*
mbed-os/platform/randlib/*
mbed-os/storage/kvstore/*

View File

@ -0,0 +1,26 @@
files: ^main.cpp
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-yaml
args: [--allow-multiple-documents]
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: "v14.0.6"
hooks:
- id: clang-format
- repo: https://github.com/cpplint/cpplint
rev: "1.6.1"
hooks:
- id: cpplint
name: cpplint
entry: cpplint --linelength=90 --filter=-build/include_subdir,-whitespace/indent,-build/namespaces,-build/c++11
- repo: local
hooks:
- id: cppcheck
name: cppcheck
require_serial: true
entry: cppcheck --enable=all --suppress=missingInclude --inline-suppr -i mbed-os --std=c++14 --error-exitcode=1
language: system

View File

@ -0,0 +1,53 @@
#include "mbed.h"
#include "FlashIAPBlockDevice.h"
#include "update-client/block_device_application.hpp"
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "BOOTLOADER"
#endif // MBED_CONF_MBED_TRACE_ENABLE
#if MBED_CONF_MBED_TRACE_ENABLE
static UnbufferedSerial g_uart(CONSOLE_TX, CONSOLE_RX);
// Function that directly outputs to an unbuffered serial port in blocking mode.
static void boot_debug(const char *s) {
size_t len = strlen(s);
g_uart.write(s, len);
g_uart.write("\r\n", 2);
}
#endif
int main() {
#if MBED_CONF_MBED_TRACE_ENABLE
mbed_trace_init();
mbed_trace_print_function_set(boot_debug);
#endif // MBED_CONF_MBED_TRACE_ENABLE
tr_debug("BikeComputer bootloader\r");
FlashIAPBlockDevice flashIAPBlockDevice(MBED_ROM_START, MBED_ROM_SIZE);
int ret = flashIAPBlockDevice.init();
if(ret == 0) {
update_client::BlockDeviceApplication app(flashIAPBlockDevice, HEADER_ADDR - MBED_ROM_START, POST_APPLICATION_ADDR - MBED_ROM_START); //MBED_CONF_UPDATE_CLIENT_STORAGE_ADDRESS);//
tr_debug("Checking active application\n");
tr_debug("MBED_ROM_START : 0x%08x", MBED_ROM_START);
tr_debug("MBED_ROM_SIZE : 0x%08x", MBED_ROM_SIZE);
tr_debug("HEADER_ADDR : 0x%08x", HEADER_ADDR);
tr_debug("APPLICATION_ADDR : 0x%08x", APPLICATION_ADDR);
tr_debug("POST_APPLICATION_ADDR : 0x%08x\n", POST_APPLICATION_ADDR);
update_client::UCErrorCode ret = app.checkApplication();
if(ret == update_client::UCErrorCode::UC_ERR_NONE) {
tr_debug("Application is valid");
void *sp = *((void **) POST_APPLICATION_ADDR + 0); // NOLINT(readability/casting)
void *pc = *((void **) POST_APPLICATION_ADDR + 1); // NOLINT(readability/casting)
tr_debug("Starting application at address 0x%08x (sp 0x%08x, pc 0x%08x)", POST_APPLICATION_ADDR, (uint32_t) sp, (uint32_t) pc);
mbed_start_application(POST_APPLICATION_ADDR);
}
tr_error("Error on check of the application");
}
tr_error("Error on init flash IAP block device");
return -1;
}

View File

@ -0,0 +1 @@
https://github.com/ARMmbed/mbed-os.git#17dc3dc2e6e2817a8bd3df62f38583319f0e4fed

View File

@ -0,0 +1,32 @@
{
"macros": [
"MBED_CONF_MBED_TRACE_FEA_IPV6=0",
"MBED_BOOTLOADER_FLASH_BANK_SIZE=MBED_ROM_SIZE/2"
],
"config": {
"main-stack-size": {
"value": 4096
}
},
"target_overrides": {
"*": {
"mbed-trace.enable": false,
"platform.stdio-convert-newlines": true,
"platform.stdio-baud-rate": 115200,
"platform.default-serial-baud-rate": 115200,
"platform.stdio-buffered-serial": true,
"platform.all-stats-enabled": true,
"target.printf_lib":"minimal-printf",
"platform.minimal-printf-enable-floating-point": true,
"platform.minimal-printf-set-floating-point-max-decimals": 2,
"update-client.storage-address": "(MBED_BOOTLOADER_FLASH_BANK_SIZE)",
"update-client.storage-size": "(MBED_BOOTLOADER_FLASH_BANK_SIZE)",
"update-client.storage-locations": 2
},
"DISCO_H747I": {
"target.restrict_size": "0x20000",
"mbed-trace.enable": true,
"mbed-trace.max-level": "TRACE_LEVEL_DEBUG"
}
}
}

View File

@ -0,0 +1,22 @@
{
"name": "bootloader",
"target_overrides": {
"*": {
"target.header_format": [
["magic", "const", "32be", "0x5a51b3d4"],
["version", "const", "32be", "2"],
["firmwareVersion", "timestamp", "64be", null],
["firmwareSize", "size", "64be", ["application"]],
["firmwareHash", "digest", "SHA256", "application"],
["hashpad", "const", "64be", "0"],
["hashpad", "const", "64be", "0"],
["hashpad", "const", "64be", "0"],
["hashpad", "const", "64be", "0"],
["campaign", "const", "64be", "0"],
["campaign", "const", "64be", "0"],
["firmwareSignatureSize", "const", "32be", "0"],
["headerCRC", "digest", "CRCITT32be", "header"]
]
}
}
}

View File

@ -0,0 +1 @@
https://github.com/SergeAyer/update-client#82fe2add70c714d9ce943f1a7dc5fda7dba75dba

View File

@ -1,6 +1,8 @@
{
"macros": [
"MBED_CONF_MBED_TRACE_FEA_IPV6=0"
"MBED_CONF_MBED_TRACE_FEA_IPV6=0",
"MBED_BOOTLOADER_FLASH_BANK_SIZE=MBED_ROM_SIZE/2",
"USE_USB_SERIAL_UC=1"
],
"config": {
"main-stack-size": {
@ -14,13 +16,19 @@
"platform.stdio-baud-rate": 115200,
"platform.default-serial-baud-rate": 115200,
"platform.stdio-buffered-serial": true,
"platform.all-stats-enabled": true,
"target.printf_lib":"minimal-printf",
"platform.minimal-printf-enable-floating-point": true,
"platform.minimal-printf-set-floating-point-max-decimals": 2
"platform.minimal-printf-set-floating-point-max-decimals": 2,
"update-client.storage-address": "(MBED_BOOTLOADER_FLASH_BANK_SIZE)",
"update-client.storage-size": "(MBED_BOOTLOADER_FLASH_BANK_SIZE)",
"update-client.storage-locations": 2
},
"DISCO_H747I": {
"mbed-trace.enable": true,
"mbed-trace.max-level": "TRACE_LEVEL_DEBUG"
"mbed-trace.max-level": "TRACE_LEVEL_DEBUG",
"target.bootloader_img":"./mbed-os-bootloader/BUILD/DISCO_H747I/GCC_ARM/mbed-os-bootloader.bin",
"target.header_offset": "0x20000"
}
}
}

22
mbed_lib.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "bootloader",
"target_overrides": {
"*": {
"target.header_format": [
["magic", "const", "32be", "0x5a51b3d4"],
["version", "const", "32be", "2"],
["firmwareVersion", "timestamp", "64be", null],
["firmwareSize", "size", "64be", ["application"]],
["firmwareHash", "digest", "SHA256", "application"],
["hashpad", "const", "64be", "0"],
["hashpad", "const", "64be", "0"],
["hashpad", "const", "64be", "0"],
["hashpad", "const", "64be", "0"],
["campaign", "const", "64be", "0"],
["campaign", "const", "64be", "0"],
["firmwareSignatureSize", "const", "32be", "0"],
["headerCRC", "digest", "CRCITT32be", "header"]
]
}
}
}

View File

@ -0,0 +1,83 @@
#include "my_candidate_applications.h"
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "MyCandidateApplications"
#endif // MBED_CONF_MBED_TRACE_ENABLE
update_client::CandidateApplications *createCandidateApplications(
BlockDevice &blockDevice,
mbed::bd_addr_t storageAddr,
mbed::bd_size_t storageSize,
uint32_t headerSize,
uint32_t nSlots
){
tr_debug("Create my candidate application");
update_client::CandidateApplications* candidate = new bike_computer::MyCandidateApplications(blockDevice, storageAddr, storageSize, headerSize, nSlots);
return candidate;
}
namespace bike_computer {
MyCandidateApplications::MyCandidateApplications(
BlockDevice &blockDevice,
mbed::bd_addr_t storageAddr,
mbed::bd_size_t storageSize,
uint32_t headerSize,
uint32_t nSlots
) : update_client::CandidateApplications(blockDevice, storageAddr, storageSize, headerSize, nSlots) {
}
uint32_t MyCandidateApplications::getSlotForCandidate() {
tr_debug("Get slot for candidate");
uint32_t nbrOfSlots = getNbrOfSlots();
for (uint32_t slotIndex = 0; slotIndex < nbrOfSlots; slotIndex++) {
if (! getBlockDeviceApplication(slotIndex).isValid()) {
return slotIndex;
}
}
// return the slot of the oldest firmware candidates
uint32_t oldestSlotIndex = 0;
uint64_t oldestFirmwareVersion = getBlockDeviceApplication(oldestSlotIndex).getFirmwareVersion();
for (uint32_t slotIndex = 1; slotIndex < nbrOfSlots; slotIndex++) {
mbed::bd_addr_t candidateAddress = 0;
mbed::bd_size_t slotSize = 0;
getCandidateAddress(slotIndex, candidateAddress, slotSize);
tr_debug("Checking application at slot %" PRIu32 " (address 0x%" PRIx64 ", size %" PRIu64 ")",
slotIndex, candidateAddress, slotSize);
if (getBlockDeviceApplication(slotIndex).getFirmwareVersion() < oldestFirmwareVersion) {
oldestSlotIndex = slotIndex;
oldestFirmwareVersion = getBlockDeviceApplication(slotIndex).getFirmwareVersion();
}
}
return oldestSlotIndex;
}
//uint32_t MyCandidateApplications::getSlotForCandidate() {
// mbed::bd_addr_t _storageAddress = MBED_CONF_UPDATE_CLIENT_STORAGE_ADDRESS;
// tr_debug("Get slot for candidate");
//
// FlashIAPBlockDevice flashIAPBlockDevice(MBED_ROM_START, MBED_ROM_SIZE);
// int ret = flashIAPBlockDevice.init();
//
// if(ret == 0){
// for (int i = 0; i < getNbrOfSlots(); i++) {
// mbed::bd_addr_t headerAddress = HEADER_ADDR - MBED_ROM_START;
// mbed::bd_addr_t applicationAddress = _storageAddress + i * getSlotSize();
// update_client::BlockDeviceApplication activeApplication(flashIAPBlockDevice, headerAddress, applicationAddress);
// update_client::UCErrorCode rc = activeApplication.checkApplication();
//
// if (update_client::UCErrorCode::UC_ERR_NONE != rc) {
// return _storageAddress + i * getSlotSize();
// }
// }
// }
// return -1;
//}
}

View File

@ -0,0 +1,24 @@
#pragma once
#include "update-client/candidate_applications.hpp"
namespace bike_computer {
class MyCandidateApplications : public update_client::CandidateApplications {
public:
MyCandidateApplications(
BlockDevice &blockDevice,
mbed::bd_addr_t storageAddr,
mbed::bd_size_t storageSize,
uint32_t headerSize,
uint32_t nSlots
);
virtual uint32_t getSlotForCandidate() override;
};
}
update_client::CandidateApplications *createCandidateApplications(
BlockDevice &blockDevice,
mbed::bd_addr_t storageAddr,
mbed::bd_size_t storageSize,
uint32_t headerSize,
uint32_t nSlots
);

View File

@ -0,0 +1,290 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file bike_system.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Bike System implementation (static scheduling)
*
* @date 2023-11-15
* @version 1.1.0
***************************************************************************/
#include "bike_system.hpp"
#include <chrono>
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "BikeSystem"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace static_scheduling {
static constexpr std::chrono::milliseconds kGearTaskPeriod = 800ms;
static constexpr std::chrono::milliseconds kGearTaskDelay = 0ms;
static constexpr std::chrono::milliseconds kGearTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kSpeedDistanceTaskPeriod = 400ms;
static constexpr std::chrono::milliseconds kSpeedDistanceTaskDelay = 0ms; // 0 or 100ms
static constexpr std::chrono::milliseconds kSpeedDistanceTaskComputationTime = 200ms;
static constexpr std::chrono::milliseconds kDisplayTask1Period = 1600ms;
static constexpr std::chrono::milliseconds kDisplayTask1Delay = 300ms;
static constexpr std::chrono::milliseconds kDisplayTask1ComputationTime = 200ms;
static constexpr std::chrono::milliseconds kResetTaskPeriod = 800ms;
static constexpr std::chrono::milliseconds kResetTaskDelay = 700ms;
static constexpr std::chrono::milliseconds kResetTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kTemperatureTaskPeriod = 1600ms;
static constexpr std::chrono::milliseconds kTemperatureTaskDelay = 1100ms;
static constexpr std::chrono::milliseconds kTemperatureTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kDisplayTask2Period = 1600ms;
static constexpr std::chrono::milliseconds kDisplayTask2Delay = 1200ms;
static constexpr std::chrono::milliseconds kDisplayTask2ComputationTime = 100ms;
static constexpr std::chrono::milliseconds kCPUTaskPeriod = 1600ms;
static constexpr std::chrono::milliseconds kCPUTaskDelay = 0ms;
static constexpr std::chrono::milliseconds kCPUTaskComputationTime = 0ms;
BikeSystem::BikeSystem()
: _gearDevice(_timer),
_pedalDevice(_timer),
_resetDevice(_timer),
_speedometer(_timer),
_cpuLogger(_timer) {}
void BikeSystem::start() {
tr_info("Starting Super-Loop without event handling");
init();
while (true) {
auto startTime = _timer.elapsed_time();
gearTask(); // 100ms : 0ms -> 100ms
speedDistanceTask(); // 200ms : 100ms -> 300ms
displayTask1(); // 200ms : 300ms -> 500ms
speedDistanceTask(); // 200ms : 500ms -> 700ms
resetTask(); // 100ms : 700ms -> 800ms
gearTask(); // 100ms : 800ms -> 900ms
speedDistanceTask(); // 200ms : 900ms -> 1100ms
temperatureTask(); // 100ms : 1100ms -> 1200ms
displayTask2(); // 100ms : 1200ms -> 1300ms
speedDistanceTask(); // 200ms : 1300ms -> 1500ms
resetTask(); // 100ms : 1500ms -> 1600ms
// register the time at the end of the cyclic schedule period and print the
// elapsed time for the period
std::chrono::microseconds endTime = _timer.elapsed_time();
const auto cycle =
std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
tr_debug("Repeating cycle time is %" PRIu64 " milliseconds", cycle.count());
bool fStop = false;
core_util_atomic_load(&fStop);
if (fStop) {
break;
}
#if !defined(MBED_TEST_MODE)
_cpuLogger.printStats();
#endif
}
}
void BikeSystem::startWithEventQueue() {
tr_info("Starting Super-Loop with event handling");
init();
EventQueue eventQueue;
Event<void()> gearEvent(&eventQueue, callback(this, &BikeSystem::gearTask));
gearEvent.delay(kGearTaskDelay);
gearEvent.period(kGearTaskPeriod);
gearEvent.post();
Event<void()> speedDistanceEvent(&eventQueue,
callback(this, &BikeSystem::speedDistanceTask));
speedDistanceEvent.delay(kSpeedDistanceTaskDelay);
speedDistanceEvent.period(kSpeedDistanceTaskPeriod);
speedDistanceEvent.post();
Event<void()> display1Event(&eventQueue, callback(this, &BikeSystem::displayTask1));
display1Event.delay(kDisplayTask1Delay);
display1Event.period(kDisplayTask1Period);
display1Event.post();
Event<void()> resetEvent(&eventQueue, callback(this, &BikeSystem::resetTask));
resetEvent.delay(kResetTaskDelay);
resetEvent.period(kResetTaskPeriod);
resetEvent.post();
Event<void()> temperatureEvent(&eventQueue,
callback(this, &BikeSystem::temperatureTask));
temperatureEvent.delay(kTemperatureTaskDelay);
temperatureEvent.period(kTemperatureTaskPeriod);
temperatureEvent.post();
Event<void()> display2Event(&eventQueue, callback(this, &BikeSystem::displayTask2));
display2Event.delay(kDisplayTask2Delay);
display2Event.period(kDisplayTask2Period);
display2Event.post();
#if !defined(MBED_TEST_MODE)
Event<void()> cpuEvent(&eventQueue, callback(this, &BikeSystem::cpuTask));
cpuEvent.delay(kCPUTaskDelay);
cpuEvent.period(kCPUTaskPeriod);
cpuEvent.post();
#endif
eventQueue.dispatch_forever();
}
void BikeSystem::stop() { core_util_atomic_store_bool(&_stopFlag, true); }
#if defined(MBED_TEST_MODE)
const advembsof::TaskLogger& BikeSystem::getTaskLogger() { return _taskLogger; }
#endif // defined(MBED_TEST_MODE)
void BikeSystem::init() {
// start the timer
_timer.start();
// initialize the lcd display
disco::ReturnCode rc = _displayDevice.init();
if (rc != disco::ReturnCode::Ok) {
tr_error("Failed to initialized the lcd display: %d", static_cast<int>(rc));
}
// initialize the sensor device
bool present = _sensorDevice.init();
if (!present) {
tr_error("Sensor not present or initialization failed");
}
// enable/disable task logging
_taskLogger.enable(true);
}
void BikeSystem::gearTask() {
// gear task
auto taskStartTime = _timer.elapsed_time();
// no need to protect access to data members (single threaded)
_currentGear = _gearDevice.getCurrentGear();
_currentGearSize = _gearDevice.getCurrentGearSize();
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kGearTaskIndex, taskStartTime);
}
void BikeSystem::speedDistanceTask() {
// speed and distance task
auto taskStartTime = _timer.elapsed_time();
const auto pedalRotationTime = _pedalDevice.getCurrentRotationTime();
_speedometer.setCurrentRotationTime(pedalRotationTime);
_speedometer.setGearSize(_currentGearSize);
// no need to protect access to data members (single threaded)
_currentSpeed = _speedometer.getCurrentSpeed();
_traveledDistance = _speedometer.getDistance();
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kSpeedTaskIndex, taskStartTime);
}
void BikeSystem::temperatureTask() {
auto taskStartTime = _timer.elapsed_time();
tr_warn("Tick1 %" PRIu64, _timer.elapsed_time().count());
// no need to protect access to data members (single threaded)
_currentTemperature = _sensorDevice.readTemperature();
tr_warn("Tick2 %" PRIu64, _timer.elapsed_time().count());
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kTemperatureTaskComputationTime - (_timer.elapsed_time() - taskStartTime)));
// simulate task computation by waiting for the required task computation time
// std::chrono::microseconds elapsedTime =
// std::chrono::microseconds::zero(); while (elapsedTime <
// kTemperatureTaskComputationTime) {
// elapsedTime = _timer.elapsed_time() - taskStartTime;
// }
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kTemperatureTaskIndex, taskStartTime);
}
void BikeSystem::resetTask() {
auto taskStartTime = _timer.elapsed_time();
if (_resetDevice.checkReset()) {
std::chrono::microseconds responseTime =
_timer.elapsed_time() - _resetDevice.getPressTime();
tr_info("Reset task: response time is %" PRIu64 " usecs", responseTime.count());
_speedometer.reset();
}
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kResetTaskIndex, taskStartTime);
}
void BikeSystem::displayTask1() {
auto taskStartTime = _timer.elapsed_time();
_displayDevice.displayGear(_currentGear);
_displayDevice.displaySpeed(_currentSpeed);
_displayDevice.displayDistance(_traveledDistance);
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kDisplayTask1ComputationTime - (_timer.elapsed_time() - taskStartTime)));
// simulate task computation by waiting for the required task computation time
// std::chrono::microseconds elapsedTime =
// std::chrono::microseconds::zero(); while (elapsedTime <
// kDisplayTask1ComputationTime) {
// elapsedTime = _timer.elapsed_time() - taskStartTime;
// }
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kDisplayTask1Index, taskStartTime);
}
void BikeSystem::displayTask2() {
auto taskStartTime = _timer.elapsed_time();
_displayDevice.displayTemperature(_currentTemperature);
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kDisplayTask2ComputationTime - (_timer.elapsed_time() - taskStartTime)));
// simulate task computation by waiting for the required task computation time
// std::chrono::microseconds elapsedTime =
// std::chrono::microseconds::zero(); while (elapsedTime <
// kDisplayTask2ComputationTime) {
// elapsedTime = _timer.elapsed_time() - taskStartTime;
// }
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kDisplayTask2Index, taskStartTime);
}
void BikeSystem::cpuTask() { _cpuLogger.printStats(); }
} // namespace static_scheduling

View File

@ -0,0 +1,106 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file bike_system.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Bike System header file (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#pragma once
// from advembsof
#include "cpu_logger.hpp"
#include "display_device.hpp"
#include "task_logger.hpp"
// from common
#include "sensor_device.hpp"
#include "speedometer.hpp"
// local
#include "gear_device.hpp"
#include "pedal_device.hpp"
#include "reset_device.hpp"
namespace static_scheduling {
class BikeSystem {
public:
// constructor
BikeSystem();
// make the class non copyable
BikeSystem(BikeSystem&) = delete;
BikeSystem& operator=(BikeSystem&) = delete;
// method called in main() for starting the system
void start();
// method called in main() for starting the sysytem with the event queue
void startWithEventQueue();
// method called for stopping the system
void stop();
#if defined(MBED_TEST_MODE)
const advembsof::TaskLogger& getTaskLogger();
#endif // defined(MBED_TEST_MODE)
private:
// private methods
void init();
void gearTask();
void speedDistanceTask();
void temperatureTask();
void resetTask();
void displayTask1();
void displayTask2();
void cpuTask();
// stop flag, used for stopping the super-loop (set in stop())
bool _stopFlag = false;
// timer instance used for loggint task time and used by ResetDevice
Timer _timer;
// data member that represents the device for manipulating the gear
GearDevice _gearDevice;
uint8_t _currentGear = bike_computer::kMinGear;
uint8_t _currentGearSize = bike_computer::kMinGearSize;
// data member that represents the device for manipulating the pedal rotation
// speed/time
PedalDevice _pedalDevice;
float _currentSpeed = 0.0f;
float _traveledDistance = 0.0f;
// data member that represents the device used for resetting
ResetDevice _resetDevice;
// data member that represents the device display
advembsof::DisplayDevice _displayDevice;
// data member that represents the device for counting wheel rotations
bike_computer::Speedometer _speedometer;
// data member that represents the sensor device
bike_computer::SensorDevice _sensorDevice;
float _currentTemperature = 0.0f;
// used for logging task info
advembsof::TaskLogger _taskLogger;
// cpu logger to measure cpu usage
advembsof::CPULogger _cpuLogger;
};
} // namespace static_scheduling

View File

@ -0,0 +1,83 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file gear_device.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Gear Device implementation (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#include "gear_device.hpp"
// from disco_h747i/wrappers
#include <chrono>
#include "joystick.hpp"
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "GearDevice"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace static_scheduling {
// definition of task execution time
static constexpr std::chrono::microseconds kTaskRunTime = 100000us;
GearDevice::GearDevice(Timer& timer) : _timer(timer) {}
uint8_t GearDevice::getCurrentGear() {
std::chrono::microseconds initialTime = _timer.elapsed_time();
std::chrono::microseconds elapsedTime = std::chrono::microseconds::zero();
// we bound the change to one increment/decrement per call
bool hasChanged = false;
while (elapsedTime < kTaskRunTime) {
if (!hasChanged) {
disco::Joystick::State joystickState =
disco::Joystick::getInstance().getState();
switch (joystickState) {
case disco::Joystick::State::UpPressed:
if (_currentGear < bike_computer::kMaxGear) {
_currentGear++;
}
hasChanged = true;
break;
case disco::Joystick::State::DownPressed:
if (_currentGear > bike_computer::kMinGear) {
_currentGear--;
}
hasChanged = true;
break;
default:
break;
}
}
elapsedTime = _timer.elapsed_time() - initialTime;
}
return _currentGear;
}
uint8_t GearDevice::getCurrentGearSize() const {
// simulate task computation by waiting for the required task run time
// wait_us(kTaskRunTime.count());
return bike_computer::kMaxGearSize - _currentGear;
}
} // namespace static_scheduling

View File

@ -0,0 +1,50 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file gear_device.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Gear Device header file (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#pragma once
#include "constants.hpp"
#include "mbed.h"
namespace static_scheduling {
class GearDevice {
public:
explicit GearDevice(Timer& timer); // NOLINT(runtime/references)
// make the class non copyable
GearDevice(GearDevice&) = delete;
GearDevice& operator=(GearDevice&) = delete;
// method called for updating the bike system
uint8_t getCurrentGear();
uint8_t getCurrentGearSize() const;
private:
// data members
uint8_t _currentGear = bike_computer::kMinGear;
Timer& _timer;
};
} // namespace static_scheduling

View File

@ -0,0 +1,84 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file pedal_device.cpp
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Pedal Device implementation (static scheduling)
* @date 2024-11-09
* @version 0.1.0
****************************************************************************/
#include "pedal_device.hpp"
// from disco_h747i/wrappers
#include <chrono>
#include "joystick.hpp"
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "PedalDevice"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace static_scheduling {
static constexpr std::chrono::microseconds kTaskRunTime = 200000us;
PedalDevice::PedalDevice(Timer& timer) : _timer(timer) {}
std::chrono::milliseconds PedalDevice::getCurrentRotationTime() {
std::chrono::microseconds initialTime = _timer.elapsed_time();
std::chrono::microseconds elapsedTime = std::chrono::microseconds::zero();
// we bound the change to one increment/decrement per call
bool hasChanged = false;
while (elapsedTime < kTaskRunTime) {
if (!hasChanged) {
disco::Joystick::State joystickState =
disco::Joystick::getInstance().getState();
switch (joystickState) {
case disco::Joystick::State::LeftPressed:
if (_pedalRotationTime < bike_computer::kMaxPedalRotationTime) {
decreaseRotationSpeed();
hasChanged = true;
}
break;
case disco::Joystick::State::DownPressed:
if (_pedalRotationTime > bike_computer::kMinPedalRotationTime) {
decreaseRotationSpeed();
hasChanged = true;
}
break;
default:
break;
}
}
elapsedTime = _timer.elapsed_time() - initialTime;
}
return _pedalRotationTime;
}
void PedalDevice::increaseRotationSpeed() {
_pedalRotationTime -= bike_computer::kDeltaPedalRotationTime;
}
void PedalDevice::decreaseRotationSpeed() {
_pedalRotationTime += bike_computer::kDeltaPedalRotationTime;
}
} // namespace static_scheduling

View File

@ -0,0 +1,54 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file pedal_device.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Pedal System header file (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#pragma once
#include "constants.hpp"
#include "mbed.h"
namespace static_scheduling {
class PedalDevice {
public:
explicit PedalDevice(Timer& timer); // NOLINT(runtime/references)
// make the class non copyable
PedalDevice(PedalDevice&) = delete;
PedalDevice& operator=(PedalDevice&) = delete;
// method called for updating the bike system
std::chrono::milliseconds getCurrentRotationTime();
private:
// private methods
void increaseRotationSpeed();
void decreaseRotationSpeed();
// data members
std::chrono::milliseconds _pedalRotationTime =
bike_computer::kInitialPedalRotationTime;
Timer& _timer;
};
} // namespace static_scheduling

View File

@ -0,0 +1,69 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file reset_device.cpp
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Reset Device implementation (static scheduling)
* @date 2024-11-12
* @version 0.1.0
****************************************************************************/
#include "reset_device.hpp"
// from disco_h747i/wrappers
#include <chrono>
#include "joystick.hpp"
#include "mbed_trace.h"
#if defined(TARGET_DISCO_H747I)
#define PUSH_BUTTON BUTTON1
static constexpr uint8_t kPolarityPressed = 1;
#endif
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "ResetDevice"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace static_scheduling {
static constexpr std::chrono::microseconds kTaskRunTime = 100000us;
ResetDevice::ResetDevice(Timer& timer) : _timer(timer), _resetButton(PUSH_BUTTON) {
_resetButton.rise(callback(this, &ResetDevice::onRise));
}
bool ResetDevice::checkReset() {
std::chrono::microseconds initialTime = _timer.elapsed_time();
std::chrono::microseconds elapsedTime = std::chrono::microseconds::zero();
// we bound the change to one increment/decrement per call
bool isPressed = false;
while (elapsedTime < kTaskRunTime) {
if (!isPressed) {
isPressed = _resetButton.read() == kPolarityPressed;
}
elapsedTime = _timer.elapsed_time() - initialTime;
}
return isPressed;
}
std::chrono::microseconds ResetDevice::getPressTime() { return _pressTime; }
void ResetDevice::onRise() { _pressTime = _timer.elapsed_time(); }
} // namespace static_scheduling

View File

@ -0,0 +1,56 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file reset_device.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief ResetDevice header file (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#pragma once
#include "mbed.h"
namespace static_scheduling {
class ResetDevice {
public:
explicit ResetDevice(Timer& timer); // NOLINT(runtime/references)
// make the class non copyable
ResetDevice(ResetDevice&) = delete;
ResetDevice& operator=(ResetDevice&) = delete;
// method called for checking the reset status
bool checkReset();
// for computing the response time
std::chrono::microseconds getPressTime();
private:
// called when the button is pressed
void onRise();
// data members
// instance representing the reset button
InterruptIn _resetButton;
Timer& _timer;
std::chrono::microseconds _pressTime;
};
} // namespace static_scheduling

View File

@ -0,0 +1,239 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file bike_system.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Bike System implementation (static scheduling)
*
* @date 2023-11-15
* @version 1.1.0
***************************************************************************/
#include "bike_system.hpp"
#include <chrono>
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "BikeSystem"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace static_scheduling_with_event {
static constexpr std::chrono::milliseconds kGearTaskPeriod = 800ms;
static constexpr std::chrono::milliseconds kGearTaskDelay = 0ms;
static constexpr std::chrono::milliseconds kGearTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kSpeedDistanceTaskPeriod = 400ms;
static constexpr std::chrono::milliseconds kSpeedDistanceTaskDelay = 100ms;
static constexpr std::chrono::milliseconds kSpeedDistanceTaskComputationTime = 200ms;
static constexpr std::chrono::milliseconds kDisplayTask1Period = 1600ms;
static constexpr std::chrono::milliseconds kDisplayTask1Delay = 300ms;
static constexpr std::chrono::milliseconds kDisplayTask1ComputationTime = 200ms;
static constexpr std::chrono::milliseconds kResetTaskPeriod = 800ms;
static constexpr std::chrono::milliseconds kResetTaskDelay = 700ms;
static constexpr std::chrono::milliseconds kResetTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kTemperatureTaskPeriod = 1600ms;
static constexpr std::chrono::milliseconds kTemperatureTaskDelay = 1100ms;
static constexpr std::chrono::milliseconds kTemperatureTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kDisplayTask2Period = 1600ms;
static constexpr std::chrono::milliseconds kDisplayTask2Delay = 1200ms;
static constexpr std::chrono::milliseconds kDisplayTask2ComputationTime = 100ms;
static constexpr std::chrono::milliseconds kCPUTaskPeriod = 1600ms;
static constexpr std::chrono::milliseconds kCPUTaskDelay = 1200ms;
static constexpr std::chrono::milliseconds kCPUTaskComputationTime = 100ms;
BikeSystem::BikeSystem()
: _gearDevice(),
_pedalDevice(),
_resetDevice(callback(this, &BikeSystem::onReset)),
_speedometer(_timer),
_cpuLogger(_timer) {}
void BikeSystem::start() {
tr_info("Starting Super-Loop with event handling");
init();
EventQueue eventQueue;
Event<void()> gearEvent(&eventQueue, callback(this, &BikeSystem::gearTask));
gearEvent.delay(kGearTaskDelay);
gearEvent.period(kGearTaskPeriod);
gearEvent.post();
Event<void()> speedDistanceEvent(&eventQueue,
callback(this, &BikeSystem::speedDistanceTask));
speedDistanceEvent.delay(kSpeedDistanceTaskDelay);
speedDistanceEvent.period(kSpeedDistanceTaskPeriod);
speedDistanceEvent.post();
Event<void()> display1Event(&eventQueue, callback(this, &BikeSystem::displayTask1));
display1Event.delay(kDisplayTask1Delay);
display1Event.period(kDisplayTask1Period);
display1Event.post();
Event<void()> resetEvent(&eventQueue, callback(this, &BikeSystem::resetTask));
resetEvent.delay(kResetTaskDelay);
resetEvent.period(kResetTaskPeriod);
resetEvent.post();
Event<void()> temperatureEvent(&eventQueue,
callback(this, &BikeSystem::temperatureTask));
temperatureEvent.delay(kTemperatureTaskDelay);
temperatureEvent.period(kTemperatureTaskPeriod);
temperatureEvent.post();
Event<void()> display2Event(&eventQueue, callback(this, &BikeSystem::displayTask2));
display2Event.delay(kDisplayTask2Delay);
display2Event.period(kDisplayTask2Period);
display2Event.post();
#if !defined(MBED_TEST_MODE)
Event<void()> cpuEvent(&eventQueue, callback(this, &BikeSystem::cpuTask));
cpuEvent.delay(kCPUTaskDelay);
cpuEvent.period(kCPUTaskPeriod);
cpuEvent.post();
#endif
eventQueue.dispatch_forever();
}
void BikeSystem::onReset() {
_resetTime = _timer.elapsed_time();
core_util_atomic_store_bool(&_resetFlag, true);
}
void BikeSystem::stop() { core_util_atomic_store_bool(&_stopFlag, true); }
#if defined(MBED_TEST_MODE)
const advembsof::TaskLogger& BikeSystem::getTaskLogger() { return _taskLogger; }
#endif // defined(MBED_TEST_MODE)
void BikeSystem::init() {
// start the timer
_timer.start();
// initialize the lcd display
disco::ReturnCode rc = _displayDevice.init();
if (rc != disco::ReturnCode::Ok) {
tr_error("Failed to initialized the lcd display: %d", static_cast<int>(rc));
}
// initialize the sensor device
bool present = _sensorDevice.init();
if (!present) {
tr_error("Sensor not present or initialization failed");
}
// enable/disable task logging
_taskLogger.enable(true);
}
void BikeSystem::gearTask() {
// gear task
auto taskStartTime = _timer.elapsed_time();
// no need to protect access to data members (single threaded)
_currentGear = _gearDevice.getCurrentGear();
_currentGearSize = _gearDevice.getCurrentGearSize();
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kGearTaskIndex, taskStartTime);
}
void BikeSystem::speedDistanceTask() {
auto taskStartTime = _timer.elapsed_time();
const auto pedalRotationTime = _pedalDevice.getCurrentRotationTime();
_speedometer.setCurrentRotationTime(pedalRotationTime);
_speedometer.setGearSize(_currentGearSize);
_currentSpeed = _speedometer.getCurrentSpeed();
_traveledDistance = _speedometer.getDistance();
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kSpeedTaskIndex, taskStartTime);
}
void BikeSystem::temperatureTask() {
auto taskStartTime = _timer.elapsed_time();
// tr_warn("Tick1 %" PRIu64, _timer.elapsed_time().count());
// no need to protect access to data members (single threaded)
_currentTemperature = _sensorDevice.readTemperature();
// tr_warn("Tick2 %" PRIu64, _timer.elapsed_time().count());
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kTemperatureTaskComputationTime - (_timer.elapsed_time() - taskStartTime)));
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kTemperatureTaskIndex, taskStartTime);
}
void BikeSystem::resetTask() {
auto taskStartTime = _timer.elapsed_time();
if (core_util_atomic_load_bool(&_resetFlag)) {
std::chrono::microseconds responseTime = _timer.elapsed_time() - _resetTime;
tr_info("Reset task: response time is %" PRIu64 " usecs", responseTime.count());
_speedometer.reset();
core_util_atomic_store_bool(&_resetFlag, false);
}
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kResetTaskIndex, taskStartTime);
}
void BikeSystem::displayTask1() {
auto taskStartTime = _timer.elapsed_time();
_displayDevice.displayGear(_currentGear);
_displayDevice.displaySpeed(_currentSpeed);
_displayDevice.displayDistance(_traveledDistance);
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kDisplayTask1ComputationTime - (_timer.elapsed_time() - taskStartTime)));
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kDisplayTask1Index, taskStartTime);
}
void BikeSystem::displayTask2() {
auto taskStartTime = _timer.elapsed_time();
_displayDevice.displayTemperature(_currentTemperature);
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kDisplayTask2ComputationTime - (_timer.elapsed_time() - taskStartTime)));
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kDisplayTask2Index, taskStartTime);
}
void BikeSystem::cpuTask() { _cpuLogger.printStats(); }
} // namespace static_scheduling_with_event

View File

@ -0,0 +1,111 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file bike_system.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
*
* @brief Bike System header file (static scheduling)
*
* @date 2023-08-20
* @version 1.0.0
***************************************************************************/
#pragma once
// from advembsof
#include "cpu_logger.hpp"
#include "display_device.hpp"
#include "task_logger.hpp"
// from common
#include "sensor_device.hpp"
#include "speedometer.hpp"
// local
#include "gear_device.hpp"
#include "pedal_device.hpp"
#include "reset_device.hpp"
namespace static_scheduling_with_event {
class BikeSystem {
public:
// constructor
BikeSystem();
// make the class non copyable
BikeSystem(BikeSystem&) = delete;
BikeSystem& operator=(BikeSystem&) = delete;
// method called in main() for starting the system
void start();
// method called in main() for starting the sysytem with the event queue
void startWithEventQueue();
// method called for stopping the system
void stop();
#if defined(MBED_TEST_MODE)
const advembsof::TaskLogger& getTaskLogger();
#endif // defined(MBED_TEST_MODE)
private:
// private methods
void init();
void onReset();
void gearTask();
void speedDistanceTask();
void temperatureTask();
void resetTask();
void displayTask1();
void displayTask2();
void cpuTask();
// stop flag, used for stopping the super-loop (set in stop())
bool _stopFlag = false;
std::chrono::microseconds _resetTime = std::chrono::microseconds::zero();
volatile bool _resetFlag = false;
// timer instance used for loggint task time and used by ResetDevice
Timer _timer;
// data member that represents the device for manipulating the gear
GearDevice _gearDevice;
uint8_t _currentGear = bike_computer::kMinGear;
uint8_t _currentGearSize = bike_computer::kMinGearSize;
// data member that represents the device for manipulating the pedal rotation
// speed/time
PedalDevice _pedalDevice;
float _currentSpeed = 0.0f;
float _traveledDistance = 0.0f;
// data member that represents the device used for resetting
ResetDevice _resetDevice;
// data member that represents the device display
advembsof::DisplayDevice _displayDevice;
// data member that represents the device for counting wheel rotations
bike_computer::Speedometer _speedometer;
// data member that represents the sensor device
bike_computer::SensorDevice _sensorDevice;
float _currentTemperature = 0.0f;
// used for logging task info
advembsof::TaskLogger _taskLogger;
// cpu logger to measure cpu usage
advembsof::CPULogger _cpuLogger;
};
} // namespace static_scheduling_with_event

View File

@ -0,0 +1,64 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file gear_device.cpp
* @author Serge Ayer <serge.ayer@hefr.ch>
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Gear Device implementation (static scheduling)
*
* @date 2023-11-17
* @version 1.1.0
***************************************************************************/
#include "gear_device.hpp"
// from disco_h747i/wrappers
#include <chrono>
#include "joystick.hpp"
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "GearDevice"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace static_scheduling_with_event {
GearDevice::GearDevice() {
disco::Joystick::getInstance().setUpCallback(callback(this, &GearDevice::onUp));
disco::Joystick::getInstance().setDownCallback(callback(this, &GearDevice::onDown));
}
uint8_t GearDevice::getCurrentGear() { return core_util_atomic_load_u8(&_currentGear); }
uint8_t GearDevice::getCurrentGearSize() const {
return bike_computer::kMaxGearSize - core_util_atomic_load_u8(&_currentGear);
}
void GearDevice::onUp() {
if (_currentGear < bike_computer::kMaxGear) {
core_util_atomic_incr_u8(&_currentGear, 1);
}
}
void GearDevice::onDown() {
if (_currentGear > bike_computer::kMinGear) {
core_util_atomic_decr_u8(&_currentGear, 1);
}
}
} // namespace static_scheduling_with_event

View File

@ -0,0 +1,54 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file gear_device.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Gear Device header file (static scheduling)
*
* @date 2023-11-17
* @version 1.1.0
***************************************************************************/
#pragma once
#include "constants.hpp"
#include "mbed.h"
namespace static_scheduling_with_event {
class GearDevice {
public:
GearDevice(); // NOLINT(runtime/references)
// make the class non copyable
GearDevice(GearDevice&) = delete;
GearDevice& operator=(GearDevice&) = delete;
// method called for updating the bike system
uint8_t getCurrentGear();
uint8_t getCurrentGearSize() const;
void onUp();
void onDown();
private:
// data members
volatile uint8_t _currentGear = bike_computer::kMinGear;
};
} // namespace static_scheduling_with_event

View File

@ -0,0 +1,69 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file pedal_device.cpp
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Pedal Device implementation (static scheduling)
* @date 2024-11-17
* @version 1.1.0
****************************************************************************/
#include "pedal_device.hpp"
// from disco_h747i/wrappers
#include <chrono>
#include "joystick.hpp"
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "PedalDevice"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace static_scheduling_with_event {
PedalDevice::PedalDevice() {
disco::Joystick::getInstance().setLeftCallback(callback(this, &PedalDevice::onLeft));
disco::Joystick::getInstance().setRightCallback(
callback(this, &PedalDevice::onRight));
}
std::chrono::milliseconds PedalDevice::getCurrentRotationTime() {
uint32_t currentStep = core_util_atomic_load_u32(&_currentStep);
return bike_computer::kMinPedalRotationTime +
currentStep * bike_computer::kDeltaPedalRotationTime;
}
void PedalDevice::increaseRotationSpeed() {
uint32_t currentStep = core_util_atomic_load_u32(&_currentStep);
if (currentStep > 0) {
core_util_atomic_decr_u32(&_currentStep, 1);
}
}
void PedalDevice::decreaseRotationSpeed() {
uint32_t currentStep = core_util_atomic_load_u32(&_currentStep);
if (currentStep < bike_computer::kNbrOfSteps) {
core_util_atomic_incr_u32(&_currentStep, 1);
}
}
void PedalDevice::onLeft() { decreaseRotationSpeed(); }
void PedalDevice::onRight() { increaseRotationSpeed(); }
} // namespace static_scheduling_with_event

View File

@ -0,0 +1,59 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file pedal_device.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Pedal System header file (static scheduling)
*
* @date 2023-11-17
* @version 1.1.0
***************************************************************************/
#pragma once
#include "constants.hpp"
#include "mbed.h"
namespace static_scheduling_with_event {
class PedalDevice {
public:
PedalDevice(); // NOLINT(runtime/references)
// make the class non copyable
PedalDevice(PedalDevice&) = delete;
PedalDevice& operator=(PedalDevice&) = delete;
// method called for updating the bike system
std::chrono::milliseconds getCurrentRotationTime();
private:
// private methods
void onLeft();
void onRight();
void increaseRotationSpeed();
void decreaseRotationSpeed();
// data members
volatile uint32_t _currentStep = static_cast<uint32_t>(
(bike_computer::kInitialPedalRotationTime - bike_computer::kMinPedalRotationTime)
.count() /
bike_computer::kDeltaPedalRotationTime.count());
};
} // namespace static_scheduling_with_event

View File

@ -0,0 +1,48 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file reset_device.cpp
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief Reset Device implementation (static scheduling with event)
* @date 2024-11-17
* @version 1.1.0
****************************************************************************/
#include "reset_device.hpp"
// from disco_h747i/wrappers
#include <chrono>
#include "joystick.hpp"
#include "mbed_trace.h"
#if defined(TARGET_DISCO_H747I)
#define PUSH_BUTTON BUTTON1
static constexpr uint8_t kPolarityPressed = 1;
#endif
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "ResetDevice"
#endif // MBED_CONF_MBED_TRACE_ENABLE
namespace static_scheduling_with_event {
ResetDevice::ResetDevice(Callback<void()> cb) : _resetButton(PUSH_BUTTON) {
_resetButton.fall(cb);
}
} // namespace static_scheduling_with_event

View File

@ -0,0 +1,47 @@
// Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/****************************************************************************
* @file reset_device.hpp
* @author Serge Ayer <serge.ayer@hefr.ch>
* @author Rémi Heredero <remi@heredero.ch>
* @author Yann Sierro <yannsierro.pro@gmail.com>
*
* @brief ResetDevice header file (static scheduling with event)
*
* @date 2023-11-17
* @version 1.1.0
***************************************************************************/
#pragma once
#include "mbed.h"
namespace static_scheduling_with_event {
class ResetDevice {
public:
explicit ResetDevice(Callback<void()> cb); // NOLINT(runtime/references)
// make the class non copyable
ResetDevice(ResetDevice&) = delete;
ResetDevice& operator=(ResetDevice&) = delete;
private:
// data members
// instance representing the reset button
InterruptIn _resetButton;
};
} // namespace static_scheduling_with_event

1
update-client.lib Normal file
View File

@ -0,0 +1 @@
https://github.com/SergeAyer/update-client#82fe2add70c714d9ce943f1a7dc5fda7dba75dba