Firmata, esempio generico

(non pilota GPIO)

Attenzione che c'è un errore (discussione in aula)

Sketch:

#include <Firmata.h>

#ifdef FIRMATA_SERIAL_FEATURE
SerialFirmata serialFeature;
#endif

void setMapCallback(byte pin, int value) {
    blinkRepeat(10,20);
}

void setup() {
    //Serial.begin(57600);
    //Serial.println("=== booting");
    //delay(5000);

#ifdef ESP8266
    Firmata.setPinMode(1, PIN_MODE_IGNORE);
    Firmata.setPinMode(3, PIN_MODE_IGNORE);
#endif

    pinMode(LED_BUILTIN, OUTPUT);

    // Firmata
    Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
    Firmata.attach(ANALOG_MESSAGE, setMapCallback);
    Firmata.attach(START_SYSEX, sysexCallback);
    Firmata.attach(SYSTEM_RESET, systemResetCallback);

    // to use a port other than Serial, such as Serial1 on an Arduino Leonardo or Mega,
    // Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this:
    // Serial1.begin(57600);
    // Firmata.begin(Serial1);
    // However do not do this if you are using SERIAL_MESSAGE

    //Firmata.begin(115200);
    Firmata.begin(57600);
    while (!Serial) {
        ; // wait for serial port to connect. Needed for ATmega32u4-based boards and Arduino 101
    }

    //systemResetCallback();  // reset to default config
#ifdef FIRMATA_SERIAL_FEATURE
    serialFeature.reset();
#endif

    delay(1000);
    //Serial.println("=== booted");

    blinkRepeat(50,50);
}

void loop() {
    blinkRepeat(1,100);

    // TODO: gestire firmata
    /* STREAMREAD - processing incoming messagse as soon as possible, while still
     * checking digital inputs.  */
    while (Firmata.available())
        Firmata.processInput();

#ifdef FIRMATA_SERIAL_FEATURE
    serialFeature.update();
#endif
}

void blink(int del) {
    digitalWrite(LED_BUILTIN,LOW);
    delay(del);
    digitalWrite(LED_BUILTIN,HIGH);
    delay(del);
}

void blinkRepeat(int times, int del) {
    for(int i=0; i<times; i++) blink(del);
}


void sysexCallback(byte command, byte argc, byte *argv) {
    byte mode;
    byte stopTX;
    byte slaveAddress;
    byte data;
    int slaveRegister;
    unsigned int delayTime;


//TODO: cambiare feedback capabilities, aumentare numero di "analog"

    switch (command) {
    case CAPABILITY_QUERY:
        Firmata.write(START_SYSEX);
        Firmata.write(CAPABILITY_RESPONSE);
        for (byte pin = 0; pin < TOTAL_PINS; pin++) {
            if (IS_PIN_DIGITAL(pin)) {
                Firmata.write((byte)INPUT);
                Firmata.write(1);
                Firmata.write((byte)PIN_MODE_PULLUP);
                Firmata.write(1);
                Firmata.write((byte)OUTPUT);
                Firmata.write(1);
            }
            if (IS_PIN_ANALOG(pin)) {
                Firmata.write(PIN_MODE_ANALOG);
                Firmata.write(10); // 10 = 10-bit resolution
            }
            if (IS_PIN_PWM(pin)) {
                Firmata.write(PIN_MODE_PWM);
                Firmata.write(DEFAULT_PWM_RESOLUTION);
            }
            if (IS_PIN_DIGITAL(pin)) {
                Firmata.write(PIN_MODE_SERVO);
                Firmata.write(14);
            }
            if (IS_PIN_I2C(pin)) {
                Firmata.write(PIN_MODE_I2C);
                Firmata.write(1);  // TODO: could assign a number to map to SCL or SDA
            }
#ifdef FIRMATA_SERIAL_FEATURE
            serialFeature.handleCapability(pin);
#endif
            Firmata.write(127);
        }
        Firmata.write(END_SYSEX);
        break;
    case PIN_STATE_QUERY:
        if (argc > 0) {
            byte pin = argv[0];
            Firmata.write(START_SYSEX);
            Firmata.write(PIN_STATE_RESPONSE);
            Firmata.write(pin);
            if (pin < TOTAL_PINS) {
                Firmata.write(Firmata.getPinMode(pin));
                Firmata.write((byte)Firmata.getPinState(pin) & 0x7F);
                if (Firmata.getPinState(pin) & 0xFF80) Firmata.write((byte)(Firmata.getPinState(pin) >> 7) & 0x7F);
                if (Firmata.getPinState(pin) & 0xC000) Firmata.write((byte)(Firmata.getPinState(pin) >> 14) & 0x7F);
            }
            Firmata.write(END_SYSEX);
        }
        break;
    case ANALOG_MAPPING_QUERY:
        Firmata.write(START_SYSEX);
        Firmata.write(ANALOG_MAPPING_RESPONSE);
        for (byte pin = 0; pin < TOTAL_PINS; pin++) {
            Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127);
        }
        Firmata.write(END_SYSEX);
        break;

    case SERIAL_MESSAGE:
#ifdef FIRMATA_SERIAL_FEATURE
        serialFeature.handleSysex(command, argc, argv);
#endif
        break;
    }
}


void systemResetCallback() {
    // initialize a defalt state
    // TODO: option to load config from EEPROM instead of default

#ifdef FIRMATA_SERIAL_FEATURE
    serialFeature.reset();
#endif

}

Python script:

#!/usr/bin/python
"""
 Copyright (c) 2015-2017 Alan Yorinks All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 Version 3 as published by the Free Software Foundation; either
 or (at your option) any later version.
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


This file demonstrates how to use some of the basic PyMata operations.
"""

import time
import sys
import signal

from PyMata.pymata import PyMata

# Create a PyMata instance
board = PyMata("/dev/ttyUSB0", verbose=True)

def signal_handler(sig, frame):
    print('You pressed Ctrl+C')
    if board is not None:
        board.reset()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# Set digital pin 13 to be an output port
#board.set_pin_mode(BOARD_LED, board.OUTPUT, board.DIGITAL)

time.sleep(2)
print("pattern test...")

# Blink for 10 times
for x in range(30):
    print(x)
    # Set the output to 1 = High
    board.analog_write(x, x*100)
    time.sleep(1)

# Close PyMata when we are done
board.close()
pub/sistemiembedded/firmata_esempio_generico.txt · Last modified: 2018/01/12 15:13 by atrent
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0