Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
1.55 Changes fix bug for Duration parameter in Tone.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYsLab authored and MrYsLab committed Mar 22, 2014
1 parent 931cd2b commit 3455985
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 23 deletions.
151 changes: 131 additions & 20 deletions ArduinoSketch/FirmataPlus/examples/FirmataPlus/FirmataPlus.ino
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@

#define REGISTER_NOT_SPECIFIED -1

#define ENCODER_NOT_PRESENT 0
#define ENCODER_IS_PRESENT 1

#define TONE_TONE 0
#define TONE_NO_TONE 1

Expand All @@ -61,6 +58,11 @@

#define INTER_PING_INTERVAL 40 // 40 ms.

// encoder configuration specifiers
# define ENCODER_IRQ_ATTACH 0
# define ENCODER_IRQ_DETACH 1
# define ENCODER_COUNTER_CLEAR 2


/*==============================================================================
* GLOBAL VARIABLES
Expand Down Expand Up @@ -117,6 +119,17 @@ uint8_t currentSonar = 0; // Keeps track of which sensor is active.
uint8_t pingInterval = 33 ; // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
byte sonarMSB, sonarLSB ;

// encoder counter variables
uint8_t encoderPin1 = IGNORE ;
uint8_t encoderPin2 = IGNORE ;
uint8_t encoderIRQ1 = IGNORE ;
uint8_t encoderIRQ2 = IGNORE ;
byte encoder1MSB, encoder1LSB ;
byte encoder2MSB, encoder2LSB ;

volatile int encoder1Counter = 0 ;
volatile int encoder2Counter = 0 ;

/*==============================================================================
* FUNCTIONS
*============================================================================*/
Expand Down Expand Up @@ -285,6 +298,8 @@ void setPinModeCallback(byte pin, int mode)
case SONAR:
pinConfig[pin] = SONAR ;
break ;
case ENCODER:
pinConfig[pin] = ENCODER ;
default:
Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM
}
Expand Down Expand Up @@ -380,7 +395,6 @@ void sysexCallback(byte command, byte argc, byte *argv)
int frequency ;
int duration ;


switch (command) {
case I2C_REQUEST:
mode = argv[1] & I2C_READ_WRITE_MODE_MASK;
Expand Down Expand Up @@ -567,8 +581,66 @@ void sysexCallback(byte command, byte argc, byte *argv)


case ENCODER_CONFIG:
printData("ENCODER NOT SUPPORTED IN THIS VERSION", 0) ;
break ;
byte irq, epin ;

// get irq identifier
irq = argv[1] ;
// switch on encoder command
switch (argv[0])
{
case ENCODER_IRQ_ATTACH:
{
epin = argv[2] ;
// find first open encoder slot
if ( encoderPin1 == IGNORE )
{
encoderPin1 = epin ;
encoderIRQ1 = irq ;
encoder1Counter = 0 ;
attachInterrupt(irq, encoder1Count, CHANGE) ;
}
else if ( encoderPin2 == IGNORE )
{
encoderPin2 = epin ;
encoderIRQ2 = irq ;
encoder2Counter = 0 ;
attachInterrupt(irq, encoder2Count, CHANGE) ;
}
else
Firmata.sendString("Encoder_IRQ_Attach: all slots taken") ;
break ;
}
case ENCODER_IRQ_DETACH:
{
detachInterrupt( irq ) ;
if ( irq == encoderIRQ1)
{
encoderIRQ1 = IGNORE ;
encoderPin1 = IGNORE ;
}
if ( irq == encoderIRQ2)
{
encoderIRQ2 = IGNORE ;
encoderPin2 = IGNORE ;
}
else
Firmata.sendString("Encoder Detach: unknown pin or irq") ;
break ;

}
case ENCODER_COUNTER_CLEAR:
{
encoder1Counter = 0 ;
encoder2Counter = 0 ;
break ;
}
default:
Firmata.sendString("Unknown Encoder_Config command") ;
break ;
}


// break ;

case TONE_DATA:

Expand Down Expand Up @@ -619,7 +691,6 @@ void sysexCallback(byte command, byte argc, byte *argv)
setPinModeCallback(sonarEchoPin, SONAR);
sonars[numActiveSonars] = new NewPing(sonarTriggerPin, sonarEchoPin, pingInterval) ;
numActiveSonars++ ;
//printData("numActiveSonars", numActiveSonars) ;
}
else {
Firmata.sendString("PING_CONFIG Error: Exceeded number of supported ping devices");
Expand Down Expand Up @@ -695,6 +766,20 @@ void systemResetCallback()
}
numActiveSonars = 0 ;

// detach any interrupts that were attached
if ( encoderIRQ1 != IGNORE )
detachInterrupt(encoderIRQ1) ;
if ( encoderIRQ2 != IGNORE)
detachInterrupt(encoderIRQ1) ;

encoderPin1 = IGNORE ;
encoderPin2 = IGNORE ;
encoderIRQ1 = IGNORE ;
encoderIRQ2 = IGNORE ;

encoder1Counter = 0 ;
encoder2Counter = 0 ;

// by default, do not report any analog inputs
analogInputsToReport = 0;

Expand Down Expand Up @@ -790,24 +875,40 @@ void loop()
Firmata.write(END_SYSEX);
}
}
if ( (encoderPin1 != IGNORE ) || (encoderPin2 != IGNORE))
{
encoder1LSB = encoder1Counter & 0x7f ;
encoder1MSB = encoder1Counter >> 7 & 0x7f ;

encoder2LSB = encoder2Counter & 0x7f ;
encoder2MSB = encoder2Counter >> 7 & 0x7f ;
Firmata.write(START_SYSEX) ;
Firmata.write(ENCODER_DATA) ;
Firmata.write(encoderPin1) ;
Firmata.write(encoder1LSB) ;
Firmata.write(encoder1MSB) ;
Firmata.write(encoderPin2) ;
Firmata.write(encoder2LSB) ;
Firmata.write(encoder2MSB) ;
Firmata.write(END_SYSEX);
}

}
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
for (pin = 0; pin < TOTAL_PINS; pin++) {
if (IS_PIN_ANALOG(pin) && pinConfig[pin] == ANALOG) {
analogPin = PIN_TO_ANALOG(pin);
if (analogInputsToReport & (1 << analogPin)) {
Firmata.sendAnalog(analogPin, analogRead(analogPin));
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
for (pin = 0; pin < TOTAL_PINS; pin++) {
if (IS_PIN_ANALOG(pin) && pinConfig[pin] == ANALOG) {
analogPin = PIN_TO_ANALOG(pin);
if (analogInputsToReport & (1 << analogPin)) {
Firmata.sendAnalog(analogPin, analogRead(analogPin));
}
}
}
}
// report i2c data for all device with read continuous mode enabled
if (queryIndex > -1) {
for (byte i = 0; i < queryIndex + 1; i++) {
readAndReportData(query[i].addr, query[i].reg, query[i].bytes);
// report i2c data for all device with read continuous mode enabled
if (queryIndex > -1) {
for (byte i = 0; i < queryIndex + 1; i++) {
readAndReportData(query[i].addr, query[i].reg, query[i].bytes);
}
}
}

}


Expand All @@ -822,3 +923,13 @@ void printData(char * id, unsigned long data)
Firmata.sendString(myArray);
}

void encoder1Count()
{
encoder1Counter++ ;
}

void encoder2Count()
{
encoder2Counter++ ;
}

4 changes: 2 additions & 2 deletions PyMata/pymata.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,10 @@ def play_tone(self, pin, tone_command, frequency, duration):
if tone_command == self.TONE_TONE:
# duration is specified
if duration:
data = [tone_command, pin, frequency & 0x7f, frequency >> 7, duration & 0x7f, frequency >> 7]
data = [tone_command, pin, frequency & 0x7f, frequency >> 7, duration & 0x7f, duration >> 7]

else:
data = [tone_command, pin, frequency & 0x7f, frequency >> 7]
data = [tone_command, pin, frequency & 0x7f, frequency >> 7, 0, 0]

self._command_handler.digital_response_table[pin][self._command_handler.RESPONSE_TABLE_MODE] = \
self.TONE
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
PyMata
======
Version 1.55
------------
This is a minor bug fix release that only affects Tone support. The duration field was not being populated
correctly and is now working properly.

Version 1.54
------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
setup(
name='PyMata',
packages=['PyMata'],
version='1.54',
version='1.55',
description="A Python Protocol Abstraction Library For Arduino Firmata",
author='Alan Yorinks',
author_email='MisterYsLab@gmail.com',
Expand Down

0 comments on commit 3455985

Please sign in to comment.