Skip to content

Commit

Permalink
Session 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Mar 17, 2021
1 parent 15ea1a3 commit 0bc3b81
Show file tree
Hide file tree
Showing 5 changed files with 561 additions and 33 deletions.
6 changes: 6 additions & 0 deletions bleepbloopmachine/CMakeLists.txt
@@ -1,5 +1,11 @@
cmake_minimum_required(VERSION 3.19.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
project(bleepbloopmachine CXX)
add_executable(bleepbloopmachine src/main.cc)
add_library(controls src/controls.h src/controls.cc)
target_link_arduino_libraries(bleepbloopmachine AUTO_PUBLIC)
target_link_arduino_libraries(controls AUTO_PUBLIC)
target_link_libraries(bleepbloopmachine PRIVATE controls)
target_enable_arduino_upload(bleepbloopmachine)
96 changes: 96 additions & 0 deletions bleepbloopmachine/src/controls.cc
@@ -0,0 +1,96 @@
#include "controls.h"

void Controls::begin() {
pinMode(BUTTON_CLOCK, OUTPUT);
digitalWrite(BUTTON_CLOCK, HIGH);
pinMode(BUTTON_LATCH, OUTPUT);
digitalWrite(BUTTON_LATCH, HIGH);
pinMode(BUTTON_DATA, INPUT);
}

int16_t Controls::readJoystickX(uint8_t sampling) {
float reading = 0;

if (JOYSTICK_X >= 0) {
for (int i = 0; i < sampling; i++) {
reading += analogRead(JOYSTICK_X);
}
reading /= sampling;

reading -= JOYSTICK_X_CENTER;
}

return reading;
}

int16_t Controls::readJoystickY(uint8_t sampling) {
float reading = 0;

if (JOYSTICK_Y >= 0) {
for (int i = 0; i < sampling; i++) {
reading += analogRead(JOYSTICK_Y);
}
reading /= sampling;

reading -= JOYSTICK_Y_CENTER;
}

return reading;
}

uint32_t Controls::readButtons(void) {
uint32_t buttons = 0;

uint8_t shift_buttons = 0;

digitalWrite(BUTTON_LATCH, LOW);
// delayMicroseconds(1);
digitalWrite(BUTTON_LATCH, HIGH);
// delayMicroseconds(1);

for (int i = 0; i < 8; i++) {
shift_buttons <<= 1;
shift_buttons |= digitalRead(BUTTON_DATA);
digitalWrite(BUTTON_CLOCK, HIGH);
// delayMicroseconds(1);
digitalWrite(BUTTON_CLOCK, LOW);
// delayMicroseconds(1);
}

if (shift_buttons & BUTTON_SHIFTMASK_B) {
buttons |= PAD_B;
}
if (shift_buttons & BUTTON_SHIFTMASK_A) {
buttons |= PAD_A;
}
if (shift_buttons & BUTTON_SHIFTMASK_SELECT) {
buttons |= PAD_SELECT;
}
if (shift_buttons & BUTTON_SHIFTMASK_START) {
buttons |= PAD_START;
}

int16_t x = readJoystickX();
if (x > 350)
buttons |= PAD_RIGHT;
else if (x < -350)
buttons |= PAD_LEFT;
int16_t y = readJoystickY();
if (y > 350)
buttons |= PAD_DOWN;
else if (y < -350)
buttons |= PAD_UP;

// TODO add encoder in here when it's pinned

last_buttons = curr_buttons;
curr_buttons = buttons;
justpressed_buttons = (last_buttons ^ curr_buttons) & curr_buttons;
justreleased_buttons = (last_buttons ^ curr_buttons) & last_buttons;

return buttons;
}

uint32_t Controls::justPressed() { return justpressed_buttons; }

uint32_t Controls::justReleased() { return justreleased_buttons; }
40 changes: 40 additions & 0 deletions bleepbloopmachine/src/controls.h
@@ -0,0 +1,40 @@
#pragma once
#include <Arduino.h>
#define BUTTON_CLOCK 48
#define BUTTON_DATA 49
#define BUTTON_LATCH 50
#define BUTTON_SHIFTMASK_B 0x80
#define BUTTON_SHIFTMASK_A 0x40
#define BUTTON_SHIFTMASK_START 0x20
#define BUTTON_SHIFTMASK_SELECT 0x10

#define JOYSTICK_X A11
#define JOYSTICK_Y A10

#define JOYSTICK_X_CENTER 512
#define JOYSTICK_Y_CENTER 512

#define PAD_A 0x01
#define PAD_B 0x02
#define PAD_SELECT 0x04
#define PAD_START 0x08
#define PAD_UP 0x10
#define PAD_DOWN 0x20
#define PAD_LEFT 0x40
#define PAD_RIGHT 0x80

class Controls {
public:
void begin();
int16_t readJoystickX(uint8_t sampling = 3);
int16_t readJoystickY(uint8_t sampling = 3);
uint32_t readButtons();
uint32_t justPressed();
uint32_t justReleased();

private:
uint32_t last_buttons;
uint32_t curr_buttons;
uint32_t justpressed_buttons;
uint32_t justreleased_buttons;
};
134 changes: 134 additions & 0 deletions bleepbloopmachine/src/freq.h
@@ -0,0 +1,134 @@
float freq[12][9] = {
{
16.3516,
32.7032,
65.40639,
130.8128,
261.6256,
523.2511,
1046.502,
2093.005,
4186.009,
},
{
17.32391,
34.64783,
69.29566,
138.5913,
277.1826,
554.3653,
1108.731,
2217.461,
4434.922,
},
{
18.35405,
36.7081,
73.41619,
146.8324,
293.6648,
587.3295,
1174.659,
2349.318,
4698.636,
},
{
19.44544,
38.89087,
77.78175,
155.5635,
311.127,
622.254,
1244.508,
2489.016,
4978.032,
},
{
20.60172,
41.20344,
82.40689,
164.8138,
329.6276,
659.2551,
1318.51,
2637.02,
5274.041,
},
{
21.82676,
43.65353,
87.30706,
174.6141,
349.2282,
698.4565,
1396.913,
2793.826,
5587.652,
},
{
23.12465,
46.2493,
92.49861,
184.9972,
369.9944,
739.9888,
1479.978,
2959.955,
5919.911,
},
{
24.49971,
48.99943,
97.99886,
195.9977,
391.9954,
783.9909,
1567.982,
3135.963,
6271.927,
},
{
25.95654,
51.91309,
103.8262,
207.6523,
415.3047,
830.6094,
1661.219,
3322.438,
6644.875,
},
{
27.5,
55.0,
110.0,
220.0,
440.0,
880.0,
1760.0,
3520.0,
7040.0,
},
{
29.13524,
58.27047,
116.5409,
233.0819,
466.1638,
932.3275,
1864.655,
3729.31,
7458.62,
},
{
30.86771,
61.73541,
123.4708,
246.9417,
493.8833,
987.7666,
1975.533,
3951.066,
7902.133,
},
};

0 comments on commit 0bc3b81

Please sign in to comment.