Skip to content

Commit

Permalink
Merge branch 'MausTec:main' into Orgasm-Modes
Browse files Browse the repository at this point in the history
  • Loading branch information
B4ben-69 authored Mar 25, 2024
2 parents 903a4ea + 4f45b30 commit 779fc42
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 36 deletions.
2 changes: 0 additions & 2 deletions include/accessory_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ extern "C" {
#include <stdint.h>

void accessory_driver_init(void);
void accessory_driver_broadcast_speed(uint8_t speed);
void accessory_driver_broadcast_arousal(uint16_t arousal);

#ifdef __cplusplus
}
Expand Down
84 changes: 84 additions & 0 deletions include/system/event_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#ifndef __system__event_manager_h
#define __system__event_manager_h

#ifdef __cplusplus
extern "C" {
#endif

#include "system/events.h"

#define EVENT_DECL(evt) const char* evt
#define EVENT_DEFINE(evt) const char* evt = #evt

#define EVENT_HANDLER_ARG_TYPE void*

EVENTS(EVENT_DECL);

/**
* @brief Function type for event handlers that you can registers.
*
*/
typedef void (*event_handler_t
)(const char* event,
EVENT_HANDLER_ARG_TYPE event_arg_ptr,
int event_arg_int,
EVENT_HANDLER_ARG_TYPE handler_arg);

typedef struct event_handler_node {
event_handler_t handler;
EVENT_HANDLER_ARG_TYPE handler_arg;
struct event_handler_node* next;
} event_handler_node_t;

typedef struct event_node {
event_handler_node_t* handlers;
struct event_node* next;
char event[];
} event_node_t;

/**
* @brief Registers an event handler.
*
* Event handlers may only be registered once per event. This function will filter through the
* existing event handlers, and if such an event/handler combination is found, will return an error.
*
* @param event Event string
* @param handler Handler function pointer
* @param handler_arg Argument to pass along to the handler_arg param in Handler function
* invocations
*/
event_handler_node_t* event_manager_register_handler(
const char* event, event_handler_t handler, EVENT_HANDLER_ARG_TYPE handler_arg
);

/**
* @brief Unregisters a handler from a specific event.
*
* @param event Event string
* @param handler Handler function pointer
*/
void event_manager_unregister_handler(const char* event, event_handler_t handler);

/**
* @brief Unregisters a handler from all events.
*
* @param handler Handler function pointer
*/
void event_manager_unregister_handler_all(event_handler_t handler);

/**
* @brief Dispatches an event to all handlers registered to it.
*
* @param event Event string
* @param event_arg_ptr Event arg, pointer
* @param event_arg_int Event arg, integer
*/
void event_manager_dispatch(
const char* event, EVENT_HANDLER_ARG_TYPE event_arg_ptr, int event_arg_int
);

#ifdef __cplusplus
}
#endif

#endif
11 changes: 11 additions & 0 deletions include/system/events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef __system__events_h
#define __system__events_h

#define EVENTS(X) \
X(EVT_MODE_SET); \
X(EVT_SPEED_CHANGE); \
X(EVT_AROUSAL_CHANGE); \
X(EVT_ORGASM_DENIAL); \
X(EVT_EDGE_START);

#endif
39 changes: 26 additions & 13 deletions src/accessory_driver.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "eom-hal.h"
#include "esp_log.h"
#include "maus_bus.h"
#include "system/event_manager.h"
#include "tscode.h"
#include <string.h>

Expand All @@ -20,15 +21,6 @@ _accessory_write(uint8_t address, uint8_t subaddress, uint8_t* data, size_t len)
return err;
}

void accessory_driver_init(void) {
maus_bus_config_t config = {
.read = _accessory_read,
.write = _accessory_write,
};

maus_bus_init(&config);
}

static void _device_speed_cb(
maus_bus_driver_t* driver, maus_bus_device_t* device, maus_bus_address_t address, void* ptr
) {
Expand Down Expand Up @@ -56,20 +48,41 @@ static void _device_speed_cb(
}
}

void accessory_driver_broadcast_speed(uint8_t speed) {
static void _evt_speed_change(
const char* event,
EVENT_HANDLER_ARG_TYPE event_arg_ptr,
int speed,
EVENT_HANDLER_ARG_TYPE handler_arg
) {
static uint8_t last_value = 0;
if (speed == last_value) return;
last_value = speed;

ESP_LOGD(TAG, "accessory_driver_broadcast_speed(%d);", speed);
ESP_LOGD(TAG, "_evt_speed_change(%d);", speed);

maus_bus_enumerate_devices(_device_speed_cb, &last_value);
}

void accessory_driver_broadcast_arousal(uint16_t arousal) {
static void _evt_arousal_change(
const char* event,
EVENT_HANDLER_ARG_TYPE event_arg_ptr,
int arousal,
EVENT_HANDLER_ARG_TYPE handler_arg
) {
static uint16_t last_value = 0;
if (arousal == last_value) return;
last_value = arousal;

ESP_LOGD(TAG, "accessory_driver_broadcast_arousal(%d);", arousal);
ESP_LOGD(TAG, "_evt_arousal_change(%d);", arousal);
}

void accessory_driver_init(void) {
maus_bus_config_t config = {
.read = _accessory_read,
.write = _accessory_write,
};

maus_bus_init(&config);

event_manager_register_handler(EVT_SPEED_CHANGE, _evt_speed_change, NULL);
}
52 changes: 33 additions & 19 deletions src/orgasm_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "eom-hal.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "system/event_manager.h"
#include "system/websocket_handler.h"
#include "ui/toast.h"
#include "ui/ui.h"
Expand Down Expand Up @@ -83,6 +84,20 @@ static struct {
} \
}

/**
* @brief Simplified method to set speed, which also handles broadcasting the event.
* @param speed
*/
static void _set_speed(uint8_t speed) {
static uint8_t last_speed = 0;
if (speed == last_speed) return;
last_speed = speed;

eom_hal_set_motor_speed(speed);
event_manager_dispatch(EVT_SPEED_CHANGE, NULL, speed);
bluetooth_driver_broadcast_speed(speed);
}

void orgasm_control_init(void) {
output_state.output_mode = OC_MANUAL_CONTROL;
output_state.vibration_mode = Config.vibration_mode;
Expand Down Expand Up @@ -117,7 +132,7 @@ static void orgasm_control_updateArousal() {
update_check(arousal_state.arousal, arousal_state.arousal * 0.99);

// Acquire new pressure and take average:
update_check(arousal_state.pressure_value, eom_hal_get_pressure_reading());
arousal_state.pressure_value = eom_hal_get_pressure_reading();
running_average_add_value(arousal_state.average, arousal_state.pressure_value);
long p_avg = running_avergae_get_average(arousal_state.average);
long p_check = Config.use_average_values ? p_avg : arousal_state.pressure_value;
Expand Down Expand Up @@ -210,7 +225,7 @@ static void orgasm_control_updateArousal() {

// Update accessories:
if (arousal_state.update_flag) {
accessory_driver_broadcast_arousal(arousal_state.arousal);
event_manager_dispatch(EVT_AROUSAL_CHANGE, NULL, arousal_state.arousal);
bluetooth_driver_broadcast_arousal(arousal_state.arousal);
// websocket_driver_broadcast_arousal(arousal_state.arousal);

Expand Down Expand Up @@ -252,6 +267,8 @@ static void orgasm_control_updateMotorSpeed() {
arousal_state.denial_count++;
arousal_state.update_flag = ocTRUE;

event_manager_dispatch(EVT_ORGASM_DENIAL, NULL, arousal_state.denial_count);

// If Max Additional Delay is not disabled, caculate a new delay every time the motor is
// stopped.
if (Config.max_additional_delay != 0) {
Expand All @@ -273,9 +290,7 @@ static void orgasm_control_updateMotorSpeed() {
// Control motor if we are not manually doing so.
if (output_state.control_motor) {
uint8_t speed = orgasm_control_getMotorSpeed();
eom_hal_set_motor_speed(speed);
accessory_driver_broadcast_speed(speed);
bluetooth_driver_broadcast_speed(speed);
_set_speed(speed);
}
}

Expand Down Expand Up @@ -364,7 +379,7 @@ static void orgasm_control_updateEdgingTime() { // Edging+Orgasm timer
if ((esp_timer_get_time() / 1000UL) < (post_orgasm_state.post_orgasm_start_millis +
post_orgasm_state.post_orgasm_duration_millis)) {
output_state.motor_speed = Config.motor_max_speed;
} else { // Post_orgasm timer reached
} else { // Post_orgasm timer reached
if (output_state.motor_speed > 0) { // Ramp down motor speed to 0
output_state.motor_speed = output_state.motor_speed - 1;
} else if ( (post_orgasm_state.to_orgasm_mode == Milk_o_matic) &&
Expand Down Expand Up @@ -399,19 +414,15 @@ static void orgasm_control_updateEdgingTime() { // Edging+Orgasm timer
post_orgasm_state.menu_is_locked = ocFALSE;
post_orgasm_state.detected_orgasm = ocFALSE;
output_state.motor_speed = 0;
eom_hal_set_motor_speed(output_state.motor_speed);
accessory_driver_broadcast_speed(output_state.motor_speed);
bluetooth_driver_broadcast_speed(output_state.motor_speed);
_set_speed(output_state.motor_speed);
orgasm_control_set_output_mode(OC_MANUAL_CONTROL);
}
}
}
// Control output while motor control is paused
if (output_state.control_motor == OC_MANUAL_CONTROL) {
uint8_t speed = orgasm_control_getMotorSpeed();
eom_hal_set_motor_speed(speed);
accessory_driver_broadcast_speed(speed);
bluetooth_driver_broadcast_speed(speed);
_set_speed(speed);
}
}

Expand Down Expand Up @@ -616,24 +627,27 @@ void orgasm_control_controlMotor(orgasm_output_mode_t control) {
orgasm_control_set_output_mode(control);
}

void orgasm_control_set_output_mode(orgasm_output_mode_t control) {
void orgasm_control_set_output_mode(orgasm_output_mode_t mode) {
orgasm_output_mode_t old = output_state.output_mode;
output_state.output_mode = control;
output_state.control_motor = control != OC_MANUAL_CONTROL;
output_state.output_mode = mode;
output_state.control_motor = mode != OC_MANUAL_CONTROL;

if (old == OC_MANUAL_CONTROL) {
const vibration_mode_controller_t* controller = orgasm_control_getVibrationMode();
output_state.motor_speed = controller->start();
} else if (control == OC_MANUAL_CONTROL) {
} else if (mode == OC_MANUAL_CONTROL) {
const vibration_mode_controller_t* controller = orgasm_control_getVibrationMode();
output_state.motor_speed = controller->stop();
}

// TODO: This is handled by the UI rendering process, I think??
eom_hal_set_encoder_rgb(
(control + 1) & 0x04 ? 0xFF : 0x00,
(control + 1) & 0x02 ? 0xFF : 0x00,
(control + 1) & 0x01 ? 0xFF : 0x00
(mode + 1) & 0x04 ? 0xFF : 0x00,
(mode + 1) & 0x02 ? 0xFF : 0x00,
(mode + 1) & 0x01 ? 0xFF : 0x00
);

event_manager_dispatch(EVT_MODE_SET, NULL, mode);
}

void orgasm_control_pauseControl() {
Expand Down
7 changes: 5 additions & 2 deletions src/pages/edging_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "esp_timer.h"
#include "menus/index.h"
#include "orgasm_control.h"
#include "system/event_manager.h"
#include "ui/toast.h"
#include "ui/ui.h"
#include "util/i18n.h"
Expand Down Expand Up @@ -280,7 +281,7 @@ on_button(eom_hal_button_t button, eom_hal_button_event_t event, void* arg) {

orgasm_control_set_output_mode(OC_MANUAL_CONTROL);
eom_hal_set_motor_speed(0x00);
accessory_driver_broadcast_speed(0x00);
event_manager_dispatch(EVT_SPEED_CHANGE, NULL, 0);
bluetooth_driver_broadcast_speed(0x00);
// websocket_server_broadcast_speed(0x00);
} else if (button == EOM_HAL_BUTTON_OK) {
Expand Down Expand Up @@ -314,10 +315,12 @@ static ui_render_flag_t on_encoder(int delta, void* arg) {
if (mode == OC_MANUAL_CONTROL) {
int speed = eom_hal_get_motor_speed() + delta;
uint8_t speed_byte = speed > 0xFF ? 0xFF : (speed < 0x00 ? 0x00 : speed);

eom_hal_set_motor_speed(speed_byte);
accessory_driver_broadcast_speed(speed_byte);
event_manager_dispatch(EVT_SPEED_CHANGE, NULL, speed_byte);
bluetooth_driver_broadcast_speed(speed_byte);
// websocket_server_broadcast_speed(speed_byte);

state.arousal_change_notice_ms = 0;
state.speed_change_notice_ms = millis + CHANGE_NOTICE_DELAY_MS;
return RENDER;
Expand Down
Loading

0 comments on commit 779fc42

Please sign in to comment.