Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 51 additions & 17 deletions roxy/axis.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class Axis {
uint32_t axis_debounce_start;

uint32_t axis_time = 0;
uint32_t last_axis = 0;
int8_t last_axis_stae = 0;
int8_t last_axis_state = 0;

uint8_t axis_debounce_time = 0;
uint8_t axis_sustain_time = 0;
Expand All @@ -26,6 +25,7 @@ class Axis {
protected:
uint16_t max_count = 255;
int8_t sensitivity = 0;
uint32_t last_axis = 0;

public:
uint32_t count;
Expand All @@ -37,10 +37,10 @@ class Axis {
axis_debounce_time = _debounce;
axis_sustain_time = _sustain;
reduction_ratio = _reduction;
deadzone_angle = (float)_deadzone / 2.0f;
deadzone_angle = _deadzone / 2.0f;
}

void process() {
virtual void process() {
uint32_t current_time = Time::time();
count = get();

Expand Down Expand Up @@ -138,8 +138,6 @@ class Axis {
} else {
count = last_axis;
}

count -= 128;
}
};

Expand Down Expand Up @@ -261,7 +259,6 @@ class IntAxis : public Axis {
if(count >= max_count) {
count -= max_count;
}

}

virtual uint32_t get() final {
Expand All @@ -273,7 +270,8 @@ class AnalogAxis : public Axis {
private:
ADC_t& adc;
uint32_t ch;

int8_t delta;

public:
AnalogAxis(ADC_t& a, uint32_t c) : adc(a), ch(c) {}

Expand All @@ -282,29 +280,65 @@ class AnalogAxis : public Axis {
adc.CR &= ~((1 << 28) | (1 << 29)); // Reset ADVREGEN
adc.CR |= 1 << 28; // Turn on ADVREGEN
Time::sleep(2); // Wait for regulator to turn on

// Calibrate ADC.
adc.CR &= ~(1 << 30); // ADCALDIF = 0 (single ended)
adc.CR |= 1 << 31; // Enable ADCAL
while(!(adc.CR & (1 << 31))); // Wait for ADCAL to finish
// Configure continous capture on one channel.

// Configure continuous capture on one channel.
adc.CFGR = (1 << 13) | (1 << 12) | (1 << 5); // CONT, OVRMOD, ALIGN
adc.SQR1 = (ch << 6);
// adc.SMPR1 = (7 << (ch * 3)); // 72 MHz / 64 / 614 = apx. 1.8 kHz

// For some reason this is broken, seems to do nothing and breaks QE2?
// adc.SMPR1 = (7 << (ch * 3)); // Max sample time (72 MHz / 64 / 614 = apx. 1.8 kHz)

// Enable ADC.
adc.CR |= 1 << 0; // ADEN
// Wait for ADC to get ready.
while(!(adc.ISR & (1 << 0))); // ADRDY
// Clear ADRDY flag.
adc.ISR = (1 << 0); // ADRDY

// Start conversion.
adc.CR |= 1 << 2; // ADSTART
}

virtual uint32_t get() final {
return adc.DR >> 8;
const uint16_t qe_samples = 128;
uint32_t samples = 0;

for (uint16_t sample = 0; sample < qe_samples; ++sample) {
samples += adc.DR >> 8;
}

return samples / qe_samples;
}

void process() {
count = get();
delta = last_axis - count;
last_axis = count;

if(delta > 0) {
dir_state = 1;
} else if(delta < 0) {
dir_state = -1;
} else {
dir_state = 0;
}

if(sensitivity == -127) {
count *= (256.0f / (600.0f * 4.0f));
} else if(sensitivity == -126) {
count *= (256.0f / (400.0f * 4.0f));
} else if(sensitivity == -125) {
count *= (256.0f / (360.0f * 4.0f));
} else if(sensitivity < 0) {
count /= -sensitivity;
} else if(sensitivity > 0) {
count *= sensitivity;
}
}
};

#endif
#endif
10 changes: 0 additions & 10 deletions roxy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,20 +410,10 @@ int main() {
tcleds.init();
}

// int8_t axis_state[2] = {0, 0}; // Current axis state
// uint32_t axis_sustain_start[2] = {0, 0}; // Clock time a sustain is started
// uint32_t axis_debounce_start[2] = {0, 0};

// uint32_t axis_time[2] = {0, 0};
// uint8_t last_axis[2] = {0, 0};

// int8_t last_axis_state[2] = {0, 0};
// uint32_t qe_count[2] = {0, 0};
uint32_t axis_buttons[4] = {(1 << 12), (1 << 13), (1 << 14), (1 << 15)};

while(1) {
usb->process();
uint32_t current_time = Time::time();

uint16_t buttons = button_manager.read_buttons();

Expand Down
8 changes: 4 additions & 4 deletions roxy/report_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ auto report_desc = joystick(

usage_page(UsagePage::Desktop),
usage(DesktopUsage::X),
logical_minimum(-128),
logical_maximum(127),
logical_minimum(0),
logical_maximum(255),
report_count(1),
report_size(8),
input(0x02),

usage_page(UsagePage::Desktop),
usage(DesktopUsage::Y),
logical_minimum(-128),
logical_maximum(127),
logical_minimum(0),
logical_maximum(255),
report_count(1),
report_size(8),
input(0x02),
Expand Down