First upload
Basic port complete - ESP32 specific features coming soon
This commit is contained in:
42
Boss.h
Normal file
42
Boss.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
class Boss
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Spawn();
|
||||||
|
void Hit();
|
||||||
|
void Kill();
|
||||||
|
bool Alive();
|
||||||
|
int _pos;
|
||||||
|
int _lives;
|
||||||
|
int _ticks;
|
||||||
|
private:
|
||||||
|
bool _alive;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Boss::Spawn(){
|
||||||
|
_pos = 800;
|
||||||
|
_lives = 3;
|
||||||
|
_alive = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Boss::Hit(){
|
||||||
|
_lives --;
|
||||||
|
if(_lives == 0) {
|
||||||
|
Kill();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(_lives == 2){
|
||||||
|
_pos = 200;
|
||||||
|
}else if(_lives == 1){
|
||||||
|
_pos = 600;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Boss::Alive(){
|
||||||
|
return _alive;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Boss::Kill(){
|
||||||
|
_alive = 0;
|
||||||
|
}
|
||||||
24
Conveyor.h
Normal file
24
Conveyor.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
#include "settings.h"
|
||||||
|
|
||||||
|
class Conveyor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Spawn(int startPoint, int endPoint, int dir);
|
||||||
|
void Kill();
|
||||||
|
int _startPoint;
|
||||||
|
int _endPoint;
|
||||||
|
int _dir;
|
||||||
|
bool _alive;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Conveyor::Spawn(int startPoint, int endPoint, int dir){
|
||||||
|
_startPoint = startPoint;
|
||||||
|
_endPoint = endPoint;
|
||||||
|
_dir = constrain(dir, -MAX_PLAYER_SPEED+1, MAX_PLAYER_SPEED - 1); // must allow some player speed
|
||||||
|
_alive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Conveyor::Kill(){
|
||||||
|
_alive = false;
|
||||||
|
}
|
||||||
55
Enemy.h
Normal file
55
Enemy.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
class Enemy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Spawn(int pos, int dir, int speed, int wobble);
|
||||||
|
void Tick();
|
||||||
|
void Kill();
|
||||||
|
bool Alive();
|
||||||
|
int _pos;
|
||||||
|
int _wobble;
|
||||||
|
int playerSide;
|
||||||
|
private:
|
||||||
|
int _dir;
|
||||||
|
int _speed;
|
||||||
|
int _alive;
|
||||||
|
int _origin;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Enemy::Spawn(int pos, int dir, int speed, int wobble){
|
||||||
|
_pos = pos;
|
||||||
|
_dir = dir; // 0 = left, 1 = right
|
||||||
|
_wobble = wobble; // 0 = no, >0 = yes, value is width of wobble
|
||||||
|
_origin = pos;
|
||||||
|
_speed = speed;
|
||||||
|
_alive = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemy::Tick(){
|
||||||
|
if(_alive){
|
||||||
|
if(_wobble > 0){
|
||||||
|
_pos = _origin + (sin((millis()/3000.0)*_speed)*_wobble);
|
||||||
|
}else{
|
||||||
|
if(_dir == 0){
|
||||||
|
_pos -= _speed;
|
||||||
|
}else{
|
||||||
|
_pos += _speed;
|
||||||
|
}
|
||||||
|
if(_pos > 1000) {
|
||||||
|
Kill();
|
||||||
|
}
|
||||||
|
if(_pos <= 0) {
|
||||||
|
Kill();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Enemy::Alive(){
|
||||||
|
return _alive;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemy::Kill(){
|
||||||
|
_alive = 0;
|
||||||
|
}
|
||||||
39
Lava.h
Normal file
39
Lava.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
class Lava
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Spawn(int left, int right, int ontime, int offtime, int offset, int state);
|
||||||
|
void Kill();
|
||||||
|
int Alive();
|
||||||
|
int _left;
|
||||||
|
int _right;
|
||||||
|
int _ontime;
|
||||||
|
int _offtime;
|
||||||
|
int _offset;
|
||||||
|
long _lastOn;
|
||||||
|
int _state;
|
||||||
|
static const int OFF = 0;
|
||||||
|
static const int ON = 1;
|
||||||
|
private:
|
||||||
|
int _alive;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Lava::Spawn(int left, int right, int ontime, int offtime, int offset, int state){
|
||||||
|
_left = left;
|
||||||
|
_right = right;
|
||||||
|
_ontime = ontime;
|
||||||
|
_offtime = offtime;
|
||||||
|
_offset = offset;
|
||||||
|
_alive = 1;
|
||||||
|
_lastOn = millis()-offset;
|
||||||
|
_state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lava::Kill(){
|
||||||
|
_alive = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Lava::Alive(){
|
||||||
|
return _alive;
|
||||||
|
}
|
||||||
59
Particle.h
Normal file
59
Particle.h
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
#define FRICTION 1
|
||||||
|
|
||||||
|
class Particle
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Spawn(int pos);
|
||||||
|
void Tick(int USE_GRAVITY);
|
||||||
|
void Kill();
|
||||||
|
bool Alive();
|
||||||
|
int _pos;
|
||||||
|
int _power;
|
||||||
|
private:
|
||||||
|
int _life;
|
||||||
|
int _alive;
|
||||||
|
int _sp;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Particle::Spawn(int pos){
|
||||||
|
_pos = pos;
|
||||||
|
_sp = random(-200, 200);
|
||||||
|
_power = 255;
|
||||||
|
_alive = 1;
|
||||||
|
_life = 220 - abs(_sp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Particle::Tick(int USE_GRAVITY){
|
||||||
|
if(_alive){
|
||||||
|
_life ++;
|
||||||
|
if(_sp > 0){
|
||||||
|
_sp -= _life/10;
|
||||||
|
}else{
|
||||||
|
_sp += _life/10;
|
||||||
|
}
|
||||||
|
if(USE_GRAVITY && _pos > 500) _sp -= 10;
|
||||||
|
_power = 100 - _life;
|
||||||
|
if(_power <= 0){
|
||||||
|
Kill();
|
||||||
|
}else{
|
||||||
|
_pos += _sp/7.0;
|
||||||
|
if(_pos > 1000){
|
||||||
|
_pos = 1000;
|
||||||
|
_sp = 0-(_sp/2);
|
||||||
|
}
|
||||||
|
else if(_pos < 0){
|
||||||
|
_pos = 0;
|
||||||
|
_sp = 0-(_sp/2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Particle::Alive(){
|
||||||
|
return _alive;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Particle::Kill(){
|
||||||
|
_alive = 0;
|
||||||
|
}
|
||||||
106
README.md
Normal file
106
README.md
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
# TWANG32
|
||||||
|
An ESP32 based, 1D, LED strip, dungeon crawler. inspired by Line Wobbler by Robin B
|
||||||
|
|
||||||
|
This was ported from the [TWANG fork](https://github.com/bdring/TWANG) by bdring of [Buildlog.net Blog](http://www.buildlog.net/blog?s=twang)
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Why ESP32?
|
||||||
|
- Lower Cost than Arduino Mega
|
||||||
|
- Faster Processor
|
||||||
|
- More Memory
|
||||||
|
- Smaller (allows a smaller enclosure that is easier to print and more portable)
|
||||||
|
- DAC pins for better sound capabilities.
|
||||||
|
- Wifi and Bluetooth.
|
||||||
|
|
||||||
|
The current state of the code is a basic port of the Arduino version. Several of the libraries did not work, so ESP32 versions are included in the code.
|
||||||
|
|
||||||
|
**Coming Soon:**
|
||||||
|
|
||||||
|
- Wireless features
|
||||||
|
- A wireless version of the serial port features of TWANG. I think the easiest way is to make it a Wifi access point with a simple web page interface. This allows control by any smartphone or computer with no client side work required.
|
||||||
|
- 2 Player features by linking controllers. TBD
|
||||||
|
- Digitized Audio
|
||||||
|
- Currently the port uses the same square wave tones of the the Arduino version.
|
||||||
|
- I want to convert to digitized high quality sound effects.
|
||||||
|
- Possibly mix multiple sounds so things like lava and movement sound good at the same time.
|
||||||
|
- Custom PCB
|
||||||
|
- Make wiring easier.
|
||||||
|
- More robust.
|
||||||
|
- Integrated audio amplifier.
|
||||||
|
- Python (it might be fun to make a python version)
|
||||||
|
|
||||||
|
|
||||||
|
## Required libraries:
|
||||||
|
* [FastLED](http://fastled.io/)
|
||||||
|
* [RunningMedian](http://playground.arduino.cc/Main/RunningMedian)
|
||||||
|
|
||||||
|
## Hardware used:
|
||||||
|
* ESP32, I typically use the NodeMCU 32s module
|
||||||
|
* LED light strip. The more the better, maximum of 1000. Tested with 1x & 2x 144/meter, 12x 60/meter and 5m x 114/meter strips. This has been tested with APA102C and Neopixel type strips. Anything compatible wiht the FastLED library should work.
|
||||||
|
* MPU6050 accelerometer
|
||||||
|
* Spring doorstop, I used [these](http://smile.amazon.com/gp/product/B00J4Y5BU2)
|
||||||
|
* Speaker and amplifier. I use a PAM8403 module. (ESP32 cannot drive a speaker as loudly as an Arduino)
|
||||||
|
|
||||||
|
See [Buildlog.net Blog](http://www.buildlog.net/blog?s=twang) for more details.
|
||||||
|
|
||||||
|
Super easy to use kits and ready to play units will be available on Tindie soon.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Enclosure
|
||||||
|
They will be on Thingiverse soon.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Overview
|
||||||
|
The following is a quick overview of the code to help you understand and tweak the game to your needs.
|
||||||
|
|
||||||
|
The game is played on a 1000 unit line, the position of enemies, the player, lava etc range from 0 to 1000 and the LEDs that represent them are derived using the getLED() function. You don't need to worry about this but it's good to know for things like the width of the attack and player max move speed. Regardles of the number of LEDs, everything takes place in this 1000 unit wide line.
|
||||||
|
|
||||||
|
**LED SETUP** Defines the quantity of LEDs as well as the data and clock pins used. I've tested several APA102-C strips and the color order sometimes changes from BGR to GBR, if the player is not blue, the exit green and the enemies red, this is the bit you want to change. Brightness should range from 50 to 255, use a lower number if playing at night or wanting to use a smaller power supply. "DIRECTION" can be set to 0 or 1 to flip the game orientation. In setup() there is a "FastLED.addLeds()" line, in there you could change it to another brand of LED strip like the cheaper WS2812.
|
||||||
|
|
||||||
|
The game also has 3 regular LEDs for life indicators (the player gets 3 lives which reset each time they level up). The pins for these LEDs are stored in lifeLEDs[] and are updated in the updateLives() function
|
||||||
|
|
||||||
|
**JOYSTICK SETUP** All parameters are commented in the code, you can set it to work in both forward/backward as well as side-to-side mode by changing JOYSTICK_ORIENTATION. Adjust the ATTACK_THRESHOLD if the "Twaning" is overly sensitive and the JOYSTICK_DEADZONE if the player slowly drifts when there is no input (because it's hard to get the MPU6050 dead level)
|
||||||
|
|
||||||
|
**WOBBLE ATTACK** Sets the width, duration (ms) of the attack
|
||||||
|
|
||||||
|
**POOLS** These are the object pools for enemies, particles, lava, conveyors etc. You can modify the quanity of any of them if your levels use more or if you want to save some memory, just remember to update the respective counts to avoid errors.
|
||||||
|
|
||||||
|
**USE_GRAVITY** 0/1 to set if particles created by the player getting killed should fall towards the start point, the BEND_POINT variable can be set to mark the point at which the strip of LEDs goes from been horizontal to vertical. The game is 1000 units wide (regardless of number of LED's) so 500 would be the mid point. If this is confusing just set USE_GRAVITY to 0
|
||||||
|
|
||||||
|
## Modifying / Creating levels
|
||||||
|
Find the loadLevel() function, in there you can see a switch statement with the 10 levels I created.
|
||||||
|
They all call different functions and variables to setup the level. Each one is described below:
|
||||||
|
|
||||||
|
**playerPosition;** Where the player starts on the 0 to 1000 line. If not set it defaults to 0. I set it to 200 in the first level so the player can see movement even if the first action they take is to push the joystick left
|
||||||
|
|
||||||
|
**spawnEnemy(position, direction, speed, wobble);** (10 enemies max)
|
||||||
|
* position: 0 to 1000
|
||||||
|
* direction: 0/1, initial direction of travel
|
||||||
|
* speed: >=0, speed of the enemy, remember the game is 1000 wide and runs at 60fps. I recommend between 1 and 4
|
||||||
|
* wobble: 0=regular moving enemy, 1=sine wave enemy, in this case speed sets the width of the wave
|
||||||
|
|
||||||
|
**spawnPool[poolNumber].Spawn(position, rate, speed, direction);** (2 spawners max)
|
||||||
|
* A spawn pool is a point which spawns enemies forever
|
||||||
|
* position: 0 to 1000
|
||||||
|
* rate: milliseconds between spawns, 1000 = 1 second
|
||||||
|
* speed: speed of the enemis it spawns
|
||||||
|
* direction: 0=towards start, 1=away from start
|
||||||
|
|
||||||
|
**spawnLava(startPoint, endPoint, ontime, offtime, offset);** (4 lava pools max)
|
||||||
|
* startPoint: 0 to 1000
|
||||||
|
* endPoint: 0 to 1000, combined with startPoint this sets the location and size of the lava
|
||||||
|
* ontime: How long (ms) the lava is ON for
|
||||||
|
* offtime: How long the lava is ON for
|
||||||
|
* offset: How long (ms) after the level starts before the lava turns on, use this to create patterns with multiple lavas
|
||||||
|
|
||||||
|
**spawnConveyor(startPoint, endPoint, direction);** (2 conveyors max)
|
||||||
|
* startPoint, endPoint: Same as lava
|
||||||
|
* direction: The direction and speed of the travel. Negative moves to base and positive moves towards exit. Must be less than +/- max player speed.
|
||||||
|
|
||||||
|
**spawnBoss(); ** (only one, don't edit boss level)
|
||||||
|
* There are no parameters for a boss, they always spawn in the same place and have 3 lives. Tweak the values of Boss.h to modify
|
||||||
|
|
||||||
|
Feel free to edit, comment on the YouTube video (link at top) if you have any questions.
|
||||||
1329
SoundData.h
Normal file
1329
SoundData.h
Normal file
File diff suppressed because it is too large
Load Diff
35
Spawner.h
Normal file
35
Spawner.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
class Spawner
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Spawn(int pos, int rate, int sp, int dir, long activate);
|
||||||
|
void Kill();
|
||||||
|
int Alive();
|
||||||
|
int _pos;
|
||||||
|
int _rate;
|
||||||
|
int _sp;
|
||||||
|
int _dir;
|
||||||
|
long _lastSpawned;
|
||||||
|
long _activate;
|
||||||
|
private:
|
||||||
|
int _alive;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Spawner::Spawn(int pos, int rate, int sp, int dir, long activate){
|
||||||
|
_pos = pos;
|
||||||
|
_rate = rate;
|
||||||
|
_sp = sp;
|
||||||
|
_dir = dir;
|
||||||
|
_activate = millis()+activate;
|
||||||
|
_alive = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Spawner::Kill(){
|
||||||
|
_alive = 0;
|
||||||
|
_lastSpawned = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Spawner::Alive(){
|
||||||
|
return _alive;
|
||||||
|
}
|
||||||
1176
TWANG32.ino
Normal file
1176
TWANG32.ino
Normal file
File diff suppressed because it is too large
Load Diff
38
iSin.h
Normal file
38
iSin.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
class iSin
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int convert(long x);
|
||||||
|
private:
|
||||||
|
uint8_t isinTable8[91] = {
|
||||||
|
0, 4, 9, 13, 18, 22, 27, 31, 35, 40, 44,
|
||||||
|
49, 53, 57, 62, 66, 70, 75, 79, 83, 87,
|
||||||
|
91, 96, 100, 104, 108, 112, 116, 120, 124, 128,
|
||||||
|
131, 135, 139, 143, 146, 150, 153, 157, 160, 164,
|
||||||
|
167, 171, 174, 177, 180, 183, 186, 190, 192, 195,
|
||||||
|
198, 201, 204, 206, 209, 211, 214, 216, 219, 221,
|
||||||
|
223, 225, 227, 229, 231, 233, 235, 236, 238, 240,
|
||||||
|
241, 243, 244, 245, 246, 247, 248, 249, 250, 251,
|
||||||
|
252, 253, 253, 254, 254, 254, 255, 255, 255, 255
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
int iSin::convert(long x)
|
||||||
|
{
|
||||||
|
boolean pos = true; // positive - keeps an eye on the sign.
|
||||||
|
if (x < 0)
|
||||||
|
{
|
||||||
|
x = -x;
|
||||||
|
pos = !pos;
|
||||||
|
}
|
||||||
|
if (x >= 360) x %= 360;
|
||||||
|
if (x > 180)
|
||||||
|
{
|
||||||
|
x -= 180;
|
||||||
|
pos = !pos;
|
||||||
|
}
|
||||||
|
if (x > 90) x = 180 - x;
|
||||||
|
if (pos) return isinTable8[x]/2 ;
|
||||||
|
return -isinTable8[x]/2 ;
|
||||||
|
}
|
||||||
15
settings.h
Normal file
15
settings.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// PLAYER
|
||||||
|
#define MAX_PLAYER_SPEED 10 // Max move speed of the player
|
||||||
|
#define LIVES_PER_LEVEL 3 // default lives per level
|
||||||
|
|
||||||
|
// JOYSTICK
|
||||||
|
#define JOYSTICK_ORIENTATION 1 // 0, 1 or 2 to set the axis of the joystick
|
||||||
|
#define JOYSTICK_DIRECTION 1 // 0/1 to flip joystick direction
|
||||||
|
#define ATTACK_THRESHOLD 30000 // The threshold that triggers an attack
|
||||||
|
#define JOYSTICK_DEADZONE 8 // Angle to ignore
|
||||||
|
|
||||||
|
// AUDIO
|
||||||
|
#define MAX_VOLUME 20 // 0 to 255
|
||||||
|
#define DAC_AUDIO_PIN 25 // should be 25 or 26 only
|
||||||
|
|
||||||
|
//TODO ... move all the settings to this file.
|
||||||
86
sound.h
Normal file
86
sound.h
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* This creates sound tones by outputting a square wave on a DAC pin. The
|
||||||
|
* volume of the tone is the level of the DAC pin.
|
||||||
|
*
|
||||||
|
* The square wave is created by a timer. The timer runs at 2x the freq, because
|
||||||
|
* it needs to transition high and then low.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "esp32-hal-timer.h";
|
||||||
|
|
||||||
|
#define ESP32_F_CPU 80000000 // the speed of the processor
|
||||||
|
#define AUDIO_INTERRUPT_PRESCALER 80
|
||||||
|
#define SOUND_TIMER_NO 0
|
||||||
|
//#define AUDIO_PIN 25
|
||||||
|
#define MIN_FREQ 20
|
||||||
|
#define MAX_FREQ 16000
|
||||||
|
|
||||||
|
hw_timer_t * sndTimer = NULL;
|
||||||
|
|
||||||
|
bool sound_on = true;
|
||||||
|
bool sound_wave_high = true; // this toggles to create the high/low transitions of the wave
|
||||||
|
uint8_t sound_volume = 0;
|
||||||
|
|
||||||
|
void sound_init(int pin);
|
||||||
|
bool sound(uint16_t freq, uint8_t volume);
|
||||||
|
void soundOff();
|
||||||
|
|
||||||
|
int dac_pin;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void IRAM_ATTR onSoundTimer()
|
||||||
|
{
|
||||||
|
if (sound_on) {
|
||||||
|
dacWrite(dac_pin, (sound_wave_high?sound_volume:0));
|
||||||
|
sound_wave_high = ! sound_wave_high;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
dacWrite(dac_pin, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sound_init(int pin){ // pin must be a DAC pin number !! (typically 25 or 26)
|
||||||
|
dac_pin = pin;
|
||||||
|
sound_on = false;
|
||||||
|
pinMode(dac_pin, OUTPUT);
|
||||||
|
sound_volume = 0;
|
||||||
|
|
||||||
|
sndTimer = timerBegin(SOUND_TIMER_NO, AUDIO_INTERRUPT_PRESCALER, true);
|
||||||
|
timerAttachInterrupt(sndTimer, &onSoundTimer, true);
|
||||||
|
timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/MIN_FREQ, true); // lower timer freq
|
||||||
|
timerAlarmEnable(sndTimer);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool sound(uint16_t freq, uint8_t volume){
|
||||||
|
if (volume == 0) {
|
||||||
|
soundOff();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (freq < MIN_FREQ || freq > MAX_FREQ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
sound_on = true;
|
||||||
|
sound_volume = volume;
|
||||||
|
timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/(freq * 2), true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void soundOff(){
|
||||||
|
sound_on = false;
|
||||||
|
sound_volume = 0;
|
||||||
|
timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/(MIN_FREQ), true); // lower timer freq
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
44
twang_mpu.h
Normal file
44
twang_mpu.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
// A very simple implementation of the MPU-6050 limited to the
|
||||||
|
// TWANG requirements
|
||||||
|
// I reused the function names to make it compatible
|
||||||
|
// B. Dring 2/2018
|
||||||
|
|
||||||
|
class Twang_MPU
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
void initialize();
|
||||||
|
void getMotion6(int16_t* xAccel, int16_t* yAccel, int16_t* zAccel, int16_t* xGyro, int16_t* yGyro, int16_t* zGyro);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const uint8_t MPU_ADDR = 0x68;
|
||||||
|
static const uint8_t PWR_MGMT_1 = 0x6B;
|
||||||
|
static const uint8_t MPU_DATA_REG_START = 0x3B;
|
||||||
|
static const uint8_t MPU_DATA_LEN = 14;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Twang_MPU::initialize()
|
||||||
|
{
|
||||||
|
Wire.beginTransmission(MPU_ADDR);
|
||||||
|
Wire.write(PWR_MGMT_1); // PWR_MGMT_1 register
|
||||||
|
Wire.write(0); // set to zero (wakes up the MPU-6050)
|
||||||
|
Wire.endTransmission(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Twang_MPU::getMotion6(int16_t* xAccel, int16_t* yAccel, int16_t* zAccel, int16_t* xGyro, int16_t* yGyro, int16_t* zGyro)
|
||||||
|
{
|
||||||
|
|
||||||
|
Wire.beginTransmission(MPU_ADDR);
|
||||||
|
Wire.write(MPU_DATA_REG_START); // starting with register 0x3B (ACCEL_XOUT_H)
|
||||||
|
Wire.endTransmission(false);
|
||||||
|
Wire.requestFrom(MPU_ADDR,MPU_DATA_LEN,true); // read the whole MPU data section
|
||||||
|
*xAccel=Wire.read()<<8|Wire.read(); // x Accel
|
||||||
|
*yAccel=Wire.read()<<8|Wire.read(); // y Accel
|
||||||
|
*zAccel=Wire.read()<<8|Wire.read(); // z Accel
|
||||||
|
Wire.read(); Wire.read(); // Temperature..not used, but need to read it
|
||||||
|
*xGyro=Wire.read()<<8|Wire.read(); // x Gyro
|
||||||
|
*yGyro=Wire.read()<<8|Wire.read(); // y Gyro
|
||||||
|
*zGyro=Wire.read()<<8|Wire.read(); // z Gyro
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user