70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // 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
 |