소스 검색

implementation ready for ESP32

Klaas, Wilfried 2 년 전
부모
커밋
2e0f2eada0
5개의 변경된 파일72개의 추가작업 그리고 37개의 파일을 삭제
  1. 13 35
      SPS/SPS.ino
  2. 3 2
      SPS/debug.h
  3. 11 0
      SPS/esp32.h
  4. 42 0
      SPS/hardware.h
  5. 3 0
      SPS/notes.h

+ 13 - 35
SPS/SPS.ino

@@ -3,7 +3,7 @@
   Version 0.13.0
   15.06.2021
   - fea: ESP32 implementation
-  
+
   Version 0.12.4
   10.06.2021
   - bug: pop not working
@@ -106,18 +106,18 @@
 #ifdef __AVR_ATmega328P__
 //#define SPS_USE_DISPLAY
 //#define SPS_RECEIVER
-//#define SPS_ENHANCEMENT
+#define SPS_ENHANCEMENT
 //#define SPS_SERIAL_PRG
 //#define SPS_SERVO
-//#define SPS_TONE
+#define SPS_TONE
 #endif
 
 #ifdef ESP32
 //#define SPS_RECEIVER (not implementted yet)
 #define SPS_ENHANCEMENT
 #define SPS_SERIAL_PRG
-//#define SPS_SERVO
-//#define SPS_TONE
+#define SPS_SERVO
+#define SPS_TONE
 #endif
 
 #ifdef __AVR_ATtiny84__
@@ -145,13 +145,13 @@
 #include "EEPROM_store.h"
 
 #ifdef ESP32
-//#include <analogWrite.h>
 #include <ESP32Servo.h>
+#ifdef SPS_TONE
+#include <ESP32Tone.h>
+#endif
 #endif
-
 
 #ifdef SPS_SERVO
-
 #if defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny861__) || defined(__AVR_ATtiny4313__)
 #include <Servo.h>
 #endif
@@ -163,9 +163,6 @@
 
 #ifdef SPS_TONE
 #include "notes.h"
-#ifdef ESP32
-#include <ESP32Tone.h>
-#endif
 #endif
 
 #include "hardware.h"
@@ -245,12 +242,7 @@ void setup() {
   pinMode(SW_PRG, INPUT_PULLUP);
   pinMode(SW_SEL, INPUT_PULLUP);
 
-#ifdef ESP32
-  ESP32PWM::allocateTimer(0);
-  ESP32PWM::allocateTimer(1);
-  ESP32PWM::allocateTimer(2);
-  ESP32PWM::allocateTimer(3);
-#endif
+  initHardware();
 
   digitalWrite(Dout_1, 1);
   delay(1000);
@@ -260,9 +252,7 @@ void setup() {
 #endif
 
   // Serielle Schnittstelle einstellen
-#ifndef __AVR_ATtiny84__
   initDebug();
-#endif
 
   prgDemoPrg();
   doReset();
@@ -350,33 +340,21 @@ void readProgram() {
     if ((cmd == IS_A) && (data == 0x0B)) {
       if (!servo1.attached()) {
         dbgOut(": attach Srv1");
-#ifdef ESP32
-        servo1.setPeriodHertz(50);
-#endif
         servo1.attach(SERVO_1);
       }
     } else if ((cmd == CMD_BYTE) && (data == 0x06)) {
       if (!servo1.attached()) {
         dbgOut(": attach Srv1");
-#ifdef ESP32
-        servo1.setPeriodHertz(50);
-#endif
         servo1.attach(SERVO_1);
       }
     } else if ((cmd == IS_A) && (data == 0x0C)) {
       if (!servo2.attached()) {
         dbgOut(": attach Srv2");
-#ifdef ESP32
-        servo2.setPeriodHertz(50);
-#endif
         servo2.attach(SERVO_2);
       }
     } else if ((cmd == CMD_BYTE) && (data == 0x07)) {
       if (!servo2.attached()) {
         dbgOut(": attach Srv2");
-#ifdef ESP32
-        servo2.setPeriodHertz(50);
-#endif
         servo2.attach(SERVO_2);
       }
     }
@@ -806,7 +784,7 @@ void doCalc(byte data) {
     default:
       break;
   }
-  a = a & 0xFF; 
+  a = a & 0xFF;
 #ifndef SPS_ENHANCEMENT
   a = a & 15;
 #endif
@@ -1033,16 +1011,16 @@ void doByte(byte data) {
     case 8:
       if (a == 0) {
         dbgOutLn("Tone off");
-        noTone(PWM_2);
+        noTone(TONE_OUT);
       } else {
         if (between(a, MIDI_START, MIDI_START + MIDI_NOTES)) {
-          word frequenz = pgm_read_word(a - MIDI_START + midiNoteToFreq);
+          word frequenz = getFrequency(a);
           dbgOut("Tone on: midi ");
           dbgOut2(a, DEC);
           dbgOut(", ");
           dbgOut2(frequenz, DEC);
           dbgOutLn("Hz");
-          tone(PWM_2, frequenz);
+          tone(TONE_OUT, frequenz);
         }
       }
       break;

+ 3 - 2
SPS/debug.h

@@ -23,9 +23,9 @@
 #include <Arduino.h>
 #include <inttypes.h>
 #ifndef debug_lib
-
 #define debug_lib
-#ifdef debug
+
+#if defined(debug) && !defined(__AVR_ATtiny84__)
 #define dbgOut(S) \
 Serial.print(S); 
 #define dbgOut2(S,P) \
@@ -44,6 +44,7 @@ Serial.println(S,P);
 #define dbgOutLn(S)
 #define dbgOutLn2(S,P)
 #define initDebug()
+
 #endif
 
 /* 

+ 11 - 0
SPS/esp32.h

@@ -0,0 +1,11 @@
+
+#ifdef ESP32
+
+void initESP32() {
+  ESP32PWM::allocateTimer(0);
+  ESP32PWM::allocateTimer(1);
+  ESP32PWM::allocateTimer(2);
+  ESP32PWM::allocateTimer(3);
+}
+
+#endif

+ 42 - 0
SPS/hardware.h

@@ -17,6 +17,10 @@ const byte ADC_1 = 1; //(16)
 const byte PWM_1 = 9;
 const byte PWM_2 = 10;
 
+#ifdef SPS_TONE
+const byte TONE_OUT = PWM_2;
+#endif
+
 #ifdef SPS_RCRECEIVER
 const byte RC_0 = 18;
 const byte RC_1 = 19;
@@ -34,7 +38,10 @@ const byte SW_SEL = 11;
 const byte DIGIT_DATA_IO = 12;
 const byte DIGIT_CLOCK = 13;
 #endif
+
 #define getAnalog(pin) (analogRead(pin) >> 2)
+#define initHardware()
+
 #endif
 
 #ifdef __AVR_ATtiny84__
@@ -52,6 +59,11 @@ const byte ADC_0 = 0;
 const byte ADC_1 = 1;
 const byte PWM_1 = 2;
 const byte PWM_2 = 3;
+
+#ifdef SPS_TONE
+const byte TONE_OUT = PWM_2;
+#endif
+
 #ifdef SPS_RCRECEIVER
 const byte RC_0 = 10;
 const byte RC_1 = 9;
@@ -69,7 +81,10 @@ const byte SW_SEL = 8;
 const byte DIGIT_DATA_IO = 4;
 const byte DIGIT_CLOCK = 5;
 #endif
+
 #define getAnalog(pin) (analogRead(pin) >> 2)
+#define initHardware()
+
 #endif
 
 #ifdef __AVR_ATtiny4313__
@@ -88,6 +103,10 @@ const byte ADC_1 = 14;
 const byte PWM_1 = 11;
 const byte PWM_2 = 12;
 
+#ifdef SPS_TONE
+const byte TONE_OUT = PWM_2;
+#endif
+
 #ifdef SPS_RCRECEIVER
 const byte RC_0 = 15;
 const byte RC_1 = 16;
@@ -102,6 +121,8 @@ const byte SW_PRG = 9;
 const byte SW_SEL = 8;
 
 #define getAnalog(pin) (analogRead(pin) >> 2)
+#define initHardware()
+
 #endif
 
 #ifdef __AVR_ATtiny861__
@@ -120,6 +141,10 @@ const byte ADC_1 = 14;
 const byte PWM_1 = 11;
 const byte PWM_2 = 12;
 
+#ifdef SPS_TONE
+const byte TONE_OUT = PWM_2;
+#endif
+
 #ifdef SPS_RCRECEIVER
 const byte RC_0 = 15;
 const byte RC_1 = 16;
@@ -134,6 +159,8 @@ const byte SW_PRG = 9;
 const byte SW_SEL = 8;
 
 #define getAnalog(pin) (analogRead(pin) >> 2)
+#define initHardware()
+
 #endif
 
 #ifdef _MICROBIT_V2_
@@ -153,6 +180,10 @@ const byte ADC_1 = 1; //(16)
 const byte PWM_1 = 9;
 const byte PWM_2 = 10;
 
+#ifdef SPS_TONE
+const byte TONE_OUT = PWM_2;
+#endif
+
 #ifdef SPS_RCRECEIVER
 const byte RC_0 = 18;
 const byte RC_1 = 19;
@@ -170,11 +201,17 @@ const byte SW_SEL = 11;
 const byte DIGIT_DATA_IO = 12;
 const byte DIGIT_CLOCK = 13;
 #endif
+
 #define getAnalog(pin) (analogRead(pin) >> 2)
+#define initHardware()
+
 #endif
 
 
 #ifdef ESP32
+
+#include "esp32.h"
+
 // ESP32 Hardware
 const byte Din_1 = 26;
 const byte Din_2 = 18;
@@ -191,6 +228,10 @@ const byte ADC_1 = 39;
 const byte PWM_1 = 27;
 const byte PWM_2 = 25;
 
+#ifdef SPS_TONE
+const byte TONE_OUT = PWM_2;
+#endif
+
 #ifdef SPS_RCRECEIVER
 const byte RC_0 = 34;
 const byte RC_1 = 35;
@@ -210,4 +251,5 @@ const byte DIGIT_CLOCK = 33;
 #endif
 
 #define getAnalog(pin) (analogRead(pin) >> 4)
+#define initHardware() initESP32()
 #endif

+ 3 - 0
SPS/notes.h

@@ -21,6 +21,8 @@ const PROGMEM unsigned int midiNoteToFreq[MIDI_NOTES] =
   2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
   4186 //C8
 };
+
+#define getFrequency(n) (pgm_read_word(n - MIDI_START + midiNoteToFreq))
 #endif
 
 #ifdef ESP32
@@ -40,4 +42,5 @@ const unsigned int midiNoteToFreq[MIDI_NOTES] =
   2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
   4186 //C8
 };
+#define getFrequency(n) (midiNoteToFreq[n-MIDI_START])
 #endif