Here's something I'm playing with at the moment as a taster..
#include <Bridge.h>
char D3value[5];
char D5value[5];
char D6value[5];
void setup() {
pinMode(12,OUTPUT);
Serial.begin(115200); //115200
Bridge.begin();
}
void loop() {
Bridge.get("D3",D3value,3);
int D3int = atoi(D3value);
analogWrite(3,D3int);
Bridge.get("D5",D5value,3);
int D5int = atoi(D5value);
analogWrite(5,D5int);
Bridge.get("D6",D6value,3);
int D6int = atoi(D6value);
analogWrite(6,D6int);
Serial.print("Fader1");
Serial.print(D3value);
Serial.print(" --");
Serial.println(D3int);
Serial.print("Fader2");
Serial.print(D5value);
Serial.print(" --");
Serial.println(D5int);
Serial.print("Fader3");
Serial.print(D6value);
Serial.print(" --");
Serial.println(D6int);
// Serial.print("analog val A0= ");
// Serial.print("From Port--- ");
// Serial.println(analogRead(A0));
int ana= analogRead(A0);
String ana0 = String(ana,10);
Bridge.put("A1",ana0);
ana= analogRead(A1);
ana0 = String(ana,10);
Bridge.put("A2",ana0);
ana= analogRead(A2);
ana0 = String(ana,10);
Bridge.put("A3",ana0);
ana= analogRead(A3);
ana0 = String(ana,10);
Bridge.put("A4",ana0);
ana= analogRead(A4);
ana0 = String(ana,10);
Bridge.put("A5",ana0);
ana= analogRead(A5);
ana0 = String(ana,10);
Bridge.put("A6",ana0);
char a1val[4];
Bridge.get("A1",a1val,4);
// Serial.print("From Store--- ");
// Serial.println(a1val);
delay(20);
}
This is employed as the sketch to interact with the python driven mf2.py file.
from OSC import OSCServer,OSCClient, OSCMessage
import sys
from time import sleep
import types
sys.path.insert(0, '/usr/lib/python2.7/bridge/') # make sure python can see the tcp module
from tcp import TCPJSONClient
from bridgeclient import BridgeClient as bridgeclient
#set up the json client and the osc client and server. The server should have the ip of your Yun.
#The client.connect should have the ip address of you phone.
json = TCPJSONClient('127.0.0.1', 5700)
server = OSCServer( ("192.168.0.74", 8000) )
client = OSCClient()
client.connect( ("192.168.0.58", 9000) )
value=bridgeclient()
#waits for slider change
def handle_timeout(self):
print ("Timeout")
server.handle_timeout =types.MethodType(handle_timeout,server)
# function to do the work. path refers to the fader name, args refers to the value of the slider
global ad_on
ad_on=0
def fader_callback(path, tags, args, source):
global fader1Feedback
if path=="/1/fader1":
fader1Feedback = float(args[0])
msg = OSCMessage("/1/label3")
msg.insert(0, fader1Feedback)
print "%i" % fader1Feedback
json.send({'command':'put', 'key':'D3', 'value':'%i' % (fader1Feedback)})
client.send(msg)
def fader2_callback(path, tags, args, source):
global fader2Feedback
if path=="/1/fader2":
fader2Feedback = float(args[0])
msg = OSCMessage("/1/label4")
msg.insert(0, fader2Feedback)
print "%i" % fader2Feedback
json.send({'command':'put', 'key':'D5', 'value':'%i' % (fader2Feedback)})
client.send(msg)
def fader3_callback(path, tags, args, source):
global fader3Feedback
if path=="/1/fader3":
fader3Feedback = float(args[0])
msg = OSCMessage("/1/label5")
msg.insert(0, fader3Feedback)
print "%i" % fader3Feedback
json.send({'command':'put', 'key':'D6', 'value':'%i' % (fader3Feedback)})
client.send(msg)
def toggle1_callback (path, tags, args, source):
global toggle1Feedback
global ad_on
if path=="/1/toggle1":
toggle1feedback=float(args[0])
print "%i" % toggle1feedback
#while (toggle1feedback==1) :
if (toggle1feedback==1):
ad_on=1
if (toggle1feedback==0):
ad_on=0
def get_analog():
Text2=value.get("A1")
print "%s" % Text2
msg = OSCMessage("/1/label1")
msg.insert(0,Text2)
client.send(msg)
Text2=value.get("A2")
print "%s" % Text2
msg = OSCMessage("/1/label2")
msg.insert(0,Text2)
client.send(msg)
Text2=value.get("A3")
print "%s" % Text2
msg = OSCMessage("/1/label6")
msg.insert(0,Text2)
client.send(msg)
Text2=value.get("A4")
print "%s" % Text2
msg = OSCMessage("/1/label7")
msg.insert(0,Text2)
client.send(msg)
Text2=value.get("A5")
print "%s" % Text2
msg = OSCMessage("/1/label8")
msg.insert(0,Text2)
client.send(msg)
Text2=value.get("A6")
print "%s" % Text2
msg = OSCMessage("/1/label9")
msg.insert(0,Text2)
client.send(msg)
#execute
server.addMsgHandler( "/1/fader1",fader_callback)
server.addMsgHandler( "/1/fader2",fader2_callback)
server.addMsgHandler( "/1/fader3",fader3_callback)
server.addMsgHandler( "/1/toggle1",toggle1_callback)
while True:
if (ad_on==1):
get_analog()
server.handle_request()
server.close()
The ip adds in the py file must be set, as in the TouchOSC set up file. I used a Toch OSC panel set as 3 faders, 3 labels above the faders, 6 labels down the rhs to display the 6 a/d channels, and a lower button to switch receiving a/d on and off.
As with most bridge involved routines, crashes will occur if non synchronised bidirectional gets/puts etc are going on. That's the bit I'm looking at. If you touch a fader whilst receiving a/d, it's halt time!
Fader response is very 'fast'. A/D return relies on the Timeout to update at the moment.
Instigate the touchosc using Putty.... python mf2.py .... in the directory in which you place the mf2.py file.
In this manner debugging is possible. multiple Ctrl C's will break out.
Hope this is the start of help for you. All credit goes to to those whose parts of code are held in here. I just got it working however I could, (frustrating as it has been so far).
I did try a while loop for the AD, hoping to call for the togglebutton so that faders could be halted while a/d was recieved, then break out on button value change, but couldn't fetch any data within the loop, since it relies on a msg from the button. Faster reception for sure, but no way to halt it.(Yet ... using python for the first time)
MultiFader.touchosc (664 Bytes)
mf2.py (3.08 KB)
touchosc.ino (1.4 KB)