Skip to content

Commit

Permalink
Filling out code for each module
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Aug 4, 2023
1 parent e3c6c14 commit 7560dbf
Show file tree
Hide file tree
Showing 21 changed files with 1,574 additions and 40 deletions.
3 changes: 2 additions & 1 deletion examples/yukon/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include(yukon_simple_enable.cmake)
include(yukon_simple_enable.cmake)
include(yukon_switched_output.cmake)
13 changes: 13 additions & 0 deletions examples/yukon/yukon_switched_output.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(OUTPUT_NAME yukon_switched_output)
add_executable(${OUTPUT_NAME} yukon_switched_output.cpp)

target_link_libraries(${OUTPUT_NAME}
pico_stdlib
yukon
)

# enable usb output, disable uart output (so it doesn't confuse any connected servos)
pico_enable_stdio_usb(${OUTPUT_NAME} 1)
pico_enable_stdio_uart(${OUTPUT_NAME} 0)

pico_add_extra_outputs(${OUTPUT_NAME})
121 changes: 121 additions & 0 deletions examples/yukon/yukon_switched_output.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "pico/stdlib.h"

#include "yukon.hpp"
using namespace pimoroni;
/*
Demonstrates how to create a Servo object and control it.
*/

const std::string OUTPUT_1_NAME = "Small Fan";
const std::string OUTPUT_2_NAME = "Big Fan";
const uint SLOT_ID = 1;

Yukon yukon(12.1f);

#define CHAR_CTRL_C (3)
#define CHAR_CTRL_D (4)

int last_char = PICO_ERROR_TIMEOUT;

bool check_for_ctrlc() {
int c = getchar_timeout_us(0);
if(c == CHAR_CTRL_C) {
printf("CTRL-C\n");
return true;
}
return false;
}

int main() {
stdio_init_all();
yukon.change_logging(3);

try {
// Initialise the servo
yukon.init();

while (!stdio_usb_connected()) {
sleep_ms(100);
}
while(!check_for_ctrlc());
while(!check_for_ctrlc());
printf("stdio_usb_connected()\n");

// Create a DualSwitchedModule and register it with a slot on Yukon
DualSwitchedModule switches;
yukon.register_with_slot(&switches, SLOT_ID);

// Initialise Yukon's registered modules
yukon.initialise_modules();

// Turn on the module power
yukon.enable_main_output();

// Enable the switched outputs
switches.enable(1);
switches.enable(2);

//uint offset = 0;
//bool sw_a_state = false;
//bool sw_b_state = false;
bool last_sw_a_state = false;
bool last_sw_b_state = false;

while(!yukon.is_boot_pressed()) {
bool sw_a_state = yukon.is_pressed(0);
bool sw_b_state = yukon.is_pressed(1);

if(sw_a_state && sw_a_state != last_sw_a_state) {
bool new_state = !switches.read_output(1);
switches.output(1, new_state);
yukon.set_led(0, new_state);
if(new_state)
logging.print(OUTPUT_1_NAME + " = On\n");
else
logging.print(OUTPUT_1_NAME + " = Off\n");
}

if(sw_b_state && sw_b_state != last_sw_b_state) {
bool new_state = not switches.read_output(2);
switches.output(2, new_state);
yukon.set_led(1, new_state);
if(new_state)
logging.print(OUTPUT_2_NAME + " = On\n");
else
logging.print(OUTPUT_2_NAME + " = Off\n");
}

last_sw_a_state = sw_a_state;
last_sw_b_state = sw_b_state;

// Use the Yukon class to sleep, which lets it monitor voltage, current, and temperature
yukon.monitored_sleep_ms(100);

if(stdio_usb_connected()) {
int c = getchar_timeout_us(0);
if(c != PICO_ERROR_TIMEOUT) {
if(c == CHAR_CTRL_C) {
printf("CTRL-C\n");
if(last_char == CHAR_CTRL_C) {
printf("exiting\n");
break;
}
}
if(c == CHAR_CTRL_D) {
printf("CTRL-D\n");
printf("exiting\n");
break;
}
last_char = c;
}
}
}
}
catch(const std::exception &e) {
printf(e.what());
}

// Put the board back into a safe state, regardless of how the program may have ended
yukon.reset();
return 0;
}
114 changes: 114 additions & 0 deletions libraries/yukon/modules/audio_amp/audio_amp.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "audio_amp.hpp"
#include "../../logging.hpp"
#include "../../errors.hpp"

namespace pimoroni {

Expand All @@ -12,4 +14,116 @@ namespace pimoroni {
return adc_level == ADC_FLOAT && slow1 == HIGH && slow2 == HIGH && slow3 == HIGH;
}

AudioAmpModule::AudioAmpModule() :
YukonModule() {
}

AudioAmpModule::~AudioAmpModule() {
}

std::string AudioAmpModule::name() {
return AudioAmpModule::NAME;
}

void AudioAmpModule::initialise(const SLOT& slot, SlotAccessor& accessor) {
// Configure strip and power pins
configure();

// Pass the slot and adc functions up to the parent now that module specific initialisation has finished
YukonModule::initialise(slot, accessor);
}

void AudioAmpModule::configure() {

}

void AudioAmpModule::enable() {

}

void AudioAmpModule::disable() {

}

bool AudioAmpModule::is_enabled() {
return 0; // TODO
}

void AudioAmpModule::exit_soft_shutdown() {

}

void AudioAmpModule::set_volume(float volume) {

}

float AudioAmpModule::read_temperature() {
return __read_adc2_as_temp();
}

void AudioAmpModule::monitor() {
float temperature = read_temperature();
if(temperature > TEMPERATURE_THRESHOLD) {
throw OverTemperatureError(__message_header() + "Temperature of " + std::to_string(temperature) + "°C exceeded the user set level of " + std::to_string(TEMPERATURE_THRESHOLD) + "°C! Turning off output\n");
}

// Run some user action based on the latest readings
//if self.__monitor_action_callback is not None:
// self.__monitor_action_callback(pgood, temperature)

max_temperature = MAX(temperature, max_temperature);
min_temperature = MIN(temperature, min_temperature);
avg_temperature += temperature;
count_avg += 1;
}

std::vector<std::pair<std::string, float>> AudioAmpModule::get_readings() {
std::vector<std::pair<std::string, float>> values;
values.push_back(std::pair("T_max", max_temperature));
values.push_back(std::pair("T_min", min_temperature));
values.push_back(std::pair("T_avg", avg_temperature));
return values;
}

void AudioAmpModule::process_readings() {
if(count_avg > 0) {
avg_temperature /= count_avg;
}
}

void AudioAmpModule::clear_readings() {
max_temperature = -std::numeric_limits<float>::infinity();
min_temperature = std::numeric_limits<float>::infinity();
avg_temperature = 0;
count_avg = 0;
}

void AudioAmpModule::start_i2c() {

}

void AudioAmpModule::end_i2c() {

}

void AudioAmpModule::write_i2c_byte(uint8_t number) {

}

uint8_t AudioAmpModule::read_i2c_byte() {
return 0; // TODO
}

void AudioAmpModule::write_i2c_reg(uint8_t reg, uint8_t data) {

}

uint8_t AudioAmpModule::read_i2c_reg(uint8_t reg) {
return 0; // TODO
}

uint8_t AudioAmpModule::detect_i2c() {
return 0; // TODO
}

}
83 changes: 79 additions & 4 deletions libraries/yukon/modules/audio_amp/audio_amp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,91 @@
namespace pimoroni {

class AudioAmpModule : public YukonModule {
//--------------------------------------------------
// Constants
//--------------------------------------------------
public:
static const std::string NAME;
static const uint8_t AMP_I2C_ADDRESS = 0x38;
static constexpr float TEMPERATURE_THRESHOLD = 50.0f;


//--------------------------------------------------
// Statics
//--------------------------------------------------
static bool is_module(uint adc_level, bool slow1, bool slow2, bool slow3);
TYPE_FUNCTION(AudioAmpModule)

virtual std::string name() {
return AudioAmpModule::NAME;
}

TYPE_FUNCTION(AudioAmpModule)
//--------------------------------------------------
// Variables
//--------------------------------------------------
public:
bool halt_on_not_pgood;

//--------------------------------------------------
private:
bool last_pgood1;
bool last_pgood2;
bool power_good_throughout1;
bool power_good_throughout2;
float max_temperature;
float min_temperature;
float avg_temperature;
float count_avg;

//--------------------------------------------------
public:
uint I2S_DATA;
uint I2S_CLK;
uint I2S_FS;
private:
TCA slow_sda;
TCA slow_scl;
TCA amp_en;



//--------------------------------------------------
// Constructors/Destructor
//--------------------------------------------------
public:
AudioAmpModule();
virtual ~AudioAmpModule();


//--------------------------------------------------
// Methods
//--------------------------------------------------
public:
virtual std::string name();
virtual void initialise(const SLOT& slot, SlotAccessor& accessor);
virtual void configure();

//--------------------------------------------------
void enable();
void disable();
bool is_enabled();

void exit_soft_shutdown();
void set_volume(float volume);
float read_temperature();

//--------------------------------------------------
virtual void monitor();
virtual std::vector<std::pair<std::string, float>> get_readings();
virtual void process_readings();
virtual void clear_readings();

//--------------------------------------------------
private:
void start_i2c();
void end_i2c();
void write_i2c_byte(uint8_t number);
uint8_t read_i2c_byte();
void write_i2c_reg(uint8_t reg, uint8_t data);
uint8_t read_i2c_reg(uint8_t reg);
uint8_t detect_i2c();
};

}
Loading

0 comments on commit 7560dbf

Please sign in to comment.