Skip to content

Commit 77ce640

Browse files
committed
reimplement battery API
1 parent 0dad69b commit 77ce640

File tree

5 files changed

+95
-81
lines changed

5 files changed

+95
-81
lines changed

include/common/result.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ class ResultError {
4141
std::optional<RuntimeData> runtime_data;
4242
};
4343

44+
/**
45+
* @brief Unknown Error
46+
*
47+
*/
48+
class UnknownError : public ResultError {
49+
public:
50+
template<typename T>
51+
requires std::convertible_to<T, std::string>
52+
UnknownError(T&& message)
53+
: message(std::forward<T>(message)) {}
54+
55+
std::string message;
56+
};
57+
4458
/**
4559
* @brief Trait to define a "sentinel" value for types indicating an error state.
4660
* @tparam T Type to provide a sentinel value for.

include/pros/devices/battery.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "common/result.hpp"
4+
5+
namespace zest {
6+
7+
class Battery {
8+
public:
9+
static Result<double, UnknownError> get_capacity();
10+
static Result<double, UnknownError> get_current();
11+
static Result<double, UnknownError> get_temperature();
12+
static Result<double, UnknownError> get_voltage();
13+
};
14+
15+
} // namespace zest

include/pros/devices/devices.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include "common/result.hpp"
4+
5+
namespace zest {
6+
7+
enum class DeviceType {
8+
Battery,
9+
};
10+
11+
/**
12+
* @brief V5 Port Mismatch Error
13+
*
14+
*/
15+
class V5PortMismatchError : public ResultError {
16+
public:
17+
/**
18+
* @brief Construct a new V5 Port Mismatch Error object
19+
*
20+
* @param expected the device expected to be on the port
21+
* @param actual the device that is actually on the port
22+
*/
23+
V5PortMismatchError(DeviceType expected, DeviceType actual)
24+
: expected(expected),
25+
actual(actual) {}
26+
27+
DeviceType expected;
28+
DeviceType actual;
29+
};
30+
} // namespace zest

src/devices/battery.c

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/devices/battery.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
1-
/**
2-
* \file devices/battery.cpp
3-
*
4-
* Contains functions for interacting with the V5 Battery.
5-
*
6-
* \copyright Copyright (c) 2017-2024, Purdue University ACM SIGBots.
7-
* All rights reserved.
8-
*
9-
* This Source Code Form is subject to the terms of the Mozilla Public
10-
* License, v. 2.0. If a copy of the MPL was not distributed with this
11-
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
12-
*/
1+
#include "pros/devices/battery.hpp"
132

14-
#include "pros/misc.h"
3+
#include "src/devices/vdml.hpp"
4+
#include "v5_api_patched.h"
155

16-
namespace pros {
17-
namespace battery {
18-
using namespace pros::c;
6+
#include <mutex>
197

20-
double get_capacity(void) {
21-
return battery_get_capacity();
8+
constexpr uint8_t PORT = 25;
9+
10+
namespace zest {
11+
Result<double, UnknownError> Battery::get_capacity() {
12+
std::lock_guard lock(port_mutex_array.at(PORT));
13+
if (auto res = vexBatteryCapacityGet(); res == std::numeric_limits<typeof(res)>::max()) {
14+
return UnknownError("An unknown error has occurred");
15+
} else {
16+
return res;
17+
}
2218
}
2319

24-
int32_t get_current(void) {
25-
return battery_get_current();
20+
Result<double, UnknownError> Battery::get_current() {
21+
std::lock_guard lock(port_mutex_array.at(PORT));
22+
if (auto res = vexBatteryCurrentGet(); res == std::numeric_limits<typeof(res)>::max()) {
23+
return UnknownError("An unknown error has occurred");
24+
} else {
25+
return res;
26+
}
2627
}
2728

28-
double get_temperature(void) {
29-
return battery_get_temperature();
29+
Result<double, UnknownError> Battery::get_temperature() {
30+
std::lock_guard lock(port_mutex_array.at(PORT));
31+
if (auto res = vexBatteryTemperatureGet(); res == std::numeric_limits<typeof(res)>::max()) {
32+
return UnknownError("An unknown error has occurred");
33+
} else {
34+
return res;
35+
}
3036
}
3137

32-
int32_t get_voltage(void) {
33-
return battery_get_voltage();
38+
Result<double, UnknownError> Battery::get_voltage() {
39+
std::lock_guard lock(port_mutex_array.at(PORT));
40+
if (auto res = vexBatteryVoltageGet(); res == std::numeric_limits<typeof(res)>::max()) {
41+
return UnknownError("An unknown error has occurred");
42+
} else {
43+
return res;
44+
}
3445
}
35-
} // namespace battery
36-
} // namespace pros
46+
} // namespace zest

0 commit comments

Comments
 (0)