analogWrite is not implemented on the ESP32

See: https://github.com/espressif/arduino-esp32/issues/4
This commit is contained in:
Kristian Sloth Lauszus 2018-01-14 18:44:38 +01:00
parent cf1b69513f
commit bfac192732

View file

@ -10,7 +10,7 @@
USB Usb; USB Usb;
USBHub hub0(&Usb); USBHub hub0(&Usb);
USBHub hub1(&Usb); USBHub hub1(&Usb);
ADK adk(&Usb,"Google, Inc.", ADK adk(&Usb, "Google, Inc.",
"DemoKit", "DemoKit",
"DemoKit Arduino Board", "DemoKit Arduino Board",
"1.0", "1.0",
@ -22,6 +22,10 @@ uint8_t b, b1;
#define LED1_RED 3 #define LED1_RED 3
#define BUTTON1 2 #define BUTTON1 2
#ifdef ESP32
#define LED1_RED_CHANNEL 0
#endif
void init_buttons() void init_buttons()
{ {
pinMode(BUTTON1, INPUT); pinMode(BUTTON1, INPUT);
@ -34,7 +38,12 @@ void init_leds()
{ {
digitalWrite(LED1_RED, 0); digitalWrite(LED1_RED, 0);
#ifdef ESP32
ledcAttachPin(LED1_RED, LED1_RED_CHANNEL); // Assign LED pin to channel 0
ledcSetup(LED1_RED_CHANNEL, 12000, 8); // 12 kHz PWM, 8-bit resolution
#else
pinMode(LED1_RED, OUTPUT); pinMode(LED1_RED, OUTPUT);
#endif
} }
void setup() void setup()
@ -47,10 +56,9 @@ void setup()
if (Usb.Init() == -1) { if (Usb.Init() == -1) {
Serial.println("OSCOKIRQ failed to assert"); Serial.println("OSCOKIRQ failed to assert");
while(1); //halt while (1); //halt
}//if (Usb.Init() == -1... }//if (Usb.Init() == -1...
init_leds(); init_leds();
init_buttons(); init_buttons();
b1 = digitalRead(BUTTON1); b1 = digitalRead(BUTTON1);
@ -62,23 +70,31 @@ void loop()
uint8_t msg[3] = { 0x00 }; uint8_t msg[3] = { 0x00 };
Usb.Task(); Usb.Task();
if( adk.isReady() == false ) { if ( adk.isReady() == false ) {
#ifdef ESP32
ledcWrite(LED1_RED_CHANNEL, 255);
#else
analogWrite(LED1_RED, 255); analogWrite(LED1_RED, 255);
#endif
return; return;
} }
uint16_t len = sizeof(msg); uint16_t len = sizeof(msg);
rcode = adk.RcvData(&len, msg); rcode = adk.RcvData(&len, msg);
if( rcode ) { if ( rcode ) {
USBTRACE2("Data rcv. :", rcode ); USBTRACE2("Data rcv. :", rcode );
} }
if(len > 0) { if (len > 0) {
USBTRACE("\r\nData Packet."); USBTRACE("\r\nData Packet.");
// assumes only one command per packet // assumes only one command per packet
if (msg[0] == 0x2) { if (msg[0] == 0x2) {
switch( msg[1] ) { switch ( msg[1] ) {
case 0: case 0:
#ifdef ESP32
ledcWrite(LED1_RED_CHANNEL, 255 - msg[2]);
#else
analogWrite(LED1_RED, 255 - msg[2]); analogWrite(LED1_RED, 255 - msg[2]);
#endif
break; break;
}//switch( msg[1]... }//switch( msg[1]...
}//if (msg[0] == 0x2... }//if (msg[0] == 0x2...
@ -92,7 +108,7 @@ void loop()
msg[1] = 0; msg[1] = 0;
msg[2] = b ? 0 : 1; msg[2] = b ? 0 : 1;
rcode = adk.SndData( 3, msg ); rcode = adk.SndData( 3, msg );
if( rcode ) { if ( rcode ) {
USBTRACE2("Button send: ", rcode ); USBTRACE2("Button send: ", rcode );
} }
b1 = b; b1 = b;