Skip to content

Commit

Permalink
Monkeypatching de toAngle et moveServos.
Browse files Browse the repository at this point in the history
À présent, Processing peut utiliser les mêmes déclarations que celle
utilisées par l'Arduino.

Signed-off-by: Yoan Blanc <[email protected]>
  • Loading branch information
greut committed Dec 4, 2016
1 parent ecdad64 commit 2e8738f
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 117 deletions.
26 changes: 16 additions & 10 deletions src/make_swig.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,39 @@ set -xe

NAME=shared

mkdir -p swig/build \
swig/gen

swig -java -c++ \
-outdir swig/src/ \
-outdir swig/gen \
"swig/${NAME}.i"

g++ -c -fpic -fno-strict-aliasing \
"programme/${NAME}.cpp"
"swig/src/declarations.cpp"

g++ -c -fpic -fno-strict-aliasing \
"swig/${NAME}_wrap.cxx" \
-I/usr/lib/jvm/java-8-openjdk/include \
-I/usr/lib/jvm/java-8-openjdk/include/linux
rm -r "swig/${NAME}_wrap.cxx"
rm -r "swig/${NAME}_wrap.cxx" "swig/${NAME}_wrap.h"

g++ -shared \
"${NAME}.o" \
"${NAME}_wrap.o" \
"declarations.o" \
-o "swig/${NAME}.so"
rm -f "${NAME}.o" "${NAME}_wrap.o"
rm -f "${NAME}.o" "${NAME}_wrap.o" "declarations.o"

mkdir -p swig/build
javac -d swig/build \
-cp /usr/share/processing/core/library/core.jar \
swig/src/*.java
rm -f \
"swig/src/${NAME}.java" \
"swig/src/${NAME}JNI.java" \
swig/src/*_double.java
swig/src/*.java \
swig/gen/*.java
echo rm -f \
"swig/gen/${NAME}.java" \
"swig/gen/${NAME}JNI.java" \
"swig/gen/Callback.java" \
"swig/gen/Caller.java" \
swig/gen/*_double.java

jar cvfm \
"swig/${NAME}.jar" \
Expand Down
1 change: 0 additions & 1 deletion src/programme/cinematique.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

bool isInside(float x, float y){
if( x < MIN_X+COR_X ){
LOG("OUT X", x, "<", MIN_X + COR_X);
Expand Down
2 changes: 0 additions & 2 deletions src/programme/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ bool debug = true; // Affiche les déplacements dans le moniteur série

// DEBOGGAGE

#define DEBUG

#ifdef DEBUG
#define LOG(...) logging(__VA_ARGS__)

Expand Down
19 changes: 1 addition & 18 deletions src/programme/dessins.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,9 @@ void etoile(float cx, float cy, float r) // étoile à 5 branches
LOG("HYPO", t, x, y);
}
}
*/

#include "shared.h"
#include "declarations.h"

void etoile(double cx=0, double cy=100, double r=50) {
double x;
double y;
double position;
double t;

for(t = 0; t < 6*M_PI; t += par) {
position = 6 * M_PI / t;
if (_etoile(x, y, cx, cy, r, position)) {
toAngle(x, y);
moveServos();
LOG("HYPO", t, x, y);
}
}
}

void etoile() { etoile(0, 100, 50); }

Expand Down
32 changes: 17 additions & 15 deletions src/programme/programme.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define DEBUG

#include "declarations.h"
#include "cinematique.h"
#include "liste.h"
Expand All @@ -16,23 +18,23 @@ void loop() {
lectureCorrectif();
dessineListe();

// spirale(-100, 170, 25);
// cercle(-30, 170, 25);
// fleur(40, 170, 25);
// coeur(-100, 100, 25);
// etoile(-30, 100, 25);
// maison(-100, 35, 50);
// float delta=0.0;
// rectangle(MIN_X+delta, MIN_Y+delta, MAX_X-delta, MAX_Y-delta);
// cercle(50, 120, 20);
// hypotrochoide(0,100,50);
// epicycloide(0,130,16,10);
// coeur(0,100,50);
// spirale(-100, 170, 25);
// cercle(-30, 170, 25);
// fleur(40, 170, 25);
// coeur(-100, 100, 25);
// etoile(-30, 100, 25);
// maison(-100, 35, 50);

// float delta=0.0;
// rectangle(MIN_X+delta, MIN_Y+delta, MAX_X-delta, MAX_Y-delta);

// cercle(50, 120, 20);
// hypotrochoide(0,100,50);
// epicycloide(0,130,16,10);
// coeur(0,100,50);

Serial.println("End \n");
Serial.flush();
exit(0);
}


26 changes: 0 additions & 26 deletions src/programme/shared.cpp

This file was deleted.

27 changes: 25 additions & 2 deletions src/programme/shared.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
#include <math.h>

#pragma once
#ifndef LOG
#ifndef DEBUG
#define LOG
#endif
#endif

bool _etoile(float& x, float& y, float cx, float cy, float r, float par);
void etoile(float cx, float cy, float r, float par = -1);

// Étoile à 5 branches
//
// https://en.wikipedia.org/wiki/Hypotrochoid
void etoile(float cx, float cy, float r, float par) // étoile à 5 branches
{
float x, y;
float r1 = r, r2 = r * 3 / 5, d = r;
if (par <= 0) {
par = M_PI / 180;
}
for (float t = 0; t < 6 * M_PI; t += par) {
x = cx + (r1 - r2) * cos(t) + d * cos((r1 - r2) / r2 * t);
y = cy + (r1 - r2) * sin(t) - d * sin((r1 - r2) / r2 * t);
toAngle(x, y);
moveServos();
LOG("HYPO", t, x, y);
}
}
9 changes: 7 additions & 2 deletions src/simulateur/dessins.pde
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
// https://en.wikipedia.org/wiki/Hypotrochoid
void etoile(float cx, float cy, float r) // étoile à 5 branches
{
for(PVector p : Dessin.etoile(cx, cy, r)) {
toAngle(p.x, p.y);
float x,y;
float r1=r, r2=r*3/5, d=r;
for (float t = 0; t<6*PI; t+=par) {
x = cx + (r1-r2)*cos(t) + d*cos((r1-r2)/r2 * t);
y = cy + (r1-r2)*sin(t) - d*sin((r1-r2)/r2 * t);
toAngle(x,y);
moveServos();
if (debug) log("HYPO", t, x, y);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/simulateur/simulateur.pde
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

boolean debug = false;
boolean debug = false;

int dy=60;
float xmm, ymm, xold, yold;
float par = PI/180*1; // angle en radians entre deux calculs de points (1°)
Expand All @@ -9,6 +9,7 @@ float lg1 = 149, lg2 = 130; // longueur des bras (mm) lg1 = AB (bras), lg2 = B
float zoom = 4;

void setup() {
Dessin.setPApplet((PApplet) this, zoom);
// size(pgWidth, pgHeight, PDF, "simulation.pdf");
background(255);
stroke(0);
Expand All @@ -21,14 +22,15 @@ void setup() {
void draw() {
translate(width/2, height);
scale(1, -1);

dessineListe();

//spirale(-100, 170, 25, 5);
//cercle(-30, 170, 25);
//fleur(40, 170, 25, 7);
//coeur(-100, 100, 25);
//etoile(-30, 100, 25);
shared.etoile(-100, 100, 25);
etoile(-30, 100, 25);
//maison(-100, 35, 50);
}

Expand Down
34 changes: 18 additions & 16 deletions src/swig/shared.i
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
%module shared
%module(directors="1") shared;

%include "typemaps.i"
%apply float *OUTPUT { float& x }
%apply float *OUTPUT { float& y }

%{
#include <vector>
#include "src/declarations.h"
#include "src/callback.h"
#include "src/caller.h"
#include "../programme/shared.h"

extern bool _etoile(
float& x,
float& y,
float cx,
float cy,
float r,
float par=-1);
#define LOG

extern Caller* caller;
%}

extern bool _etoile(
float& x,
float& y,
float cx,
float cy,
float r,
float par=-1);
%feature("director") Callback;

%include "src/caller.h"
%include "src/callback.h"

extern void etoile(float cx, float cy, float r, float par=-1);

extern void moveServos();
extern void toAngle(float x, float y);

extern Caller* caller;

%pragma(java) jniclasscode=%{
static {
Expand Down
52 changes: 32 additions & 20 deletions src/swig/src/Dessin.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
import processing.core.PApplet;
import processing.core.PVector;
import java.util.List;
import java.util.ArrayList;


public class Dessin {
public static String VERSION="0.0.4";

public static void main(String[] argv) {
// Ceci est un test.
for(PVector p : etoile(0, 0, 100, 16)) {
System.out.printf("(%.2f; %.2f)\n", p.x, p.y);
}
}
System.out.println("Ceci est un test");
shared.getCaller().setCallback(new Callback(){
@Override
public void moveServos() {
System.out.printf("moveServos\n");
}

@Override
public void toAngle(float x, float y) {
System.out.printf("toAngle %f, %f\n", x, y);
}
});

public static List<PVector> etoile(float cx, float cy, float r) {
// Une puissance de deux c'est bien.
return etoile(cy, cy, r, 256);
shared.etoile(0, 0, 100);
}

public static List<PVector> etoile(float cx, float cy, float r, float steps) {
List<PVector> points = new ArrayList<PVector>();
float[] x = new float[]{0};
float[] y = new float[]{0};
for(float i=0; i <= 1; i += 1/steps) {
if (shared._etoile(x, y, cx, cy, r, i)) {
points.add(new PVector(x[0], y[0]));
} else {
System.err.printf("Appel à etoile(%f, %f, %f, %d) a échoué.", cx, cy, r, i);
return points;
public static void setPApplet(final PApplet pApplet, final float zoom) {
shared.getCaller().setCallback(new Callback(){
private PVector old;
private PVector current = new PVector(0, 0);

@Override
public void moveServos() {
pApplet.line(old.x, old.y, current.x, current.y);
//pApplet.delay(100);
}

@Override
public void toAngle(float x, float y) {
//pApplet.println("to ", x, y);
old = current;
current = new PVector(x * zoom, y * zoom);
}
}
return points;
});
}
}
10 changes: 10 additions & 0 deletions src/swig/src/callback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <iostream>

class Callback {
public:
virtual ~Callback() { std::cout << "Callback::Callback" << std::endl; }
virtual void moveServos() { std::cout << "Callback::moveServos()" << std::endl; }
virtual void toAngle(float x, float y) { std::cout << "Callback::toAngle(" << x << "," << y << ")" << std::endl; }
};
4 changes: 4 additions & 0 deletions src/swig/src/caller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "caller.h"

// global variable...
Caller* caller = new Caller();
14 changes: 14 additions & 0 deletions src/swig/src/caller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "callback.h"

class Caller {
private:
Callback* _callback;
public:
Caller(): _callback(0) {}
~Caller() { delCallback(); }
void delCallback() { delete _callback; _callback = 0; }
void setCallback(Callback* cb) { delCallback(); _callback = cb; }
void moveServos() { if(_callback) { _callback->moveServos(); } }
void toAngle(float x, float y) { if(_callback) { _callback->toAngle(x, y); } }
};
Loading

0 comments on commit 2e8738f

Please sign in to comment.