Skip to content

Instantly share code, notes, and snippets.

Created September 1, 2015 22:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/661ed20053689b326ba7 to your computer and use it in GitHub Desktop.
Save anonymous/661ed20053689b326ba7 to your computer and use it in GitHub Desktop.
Vibrating egg controlled by a Raspberry Pi and regulated by a Minecraft Mod (More XP=More Vibrations, becoming less over time)
package remoteVibratorControl;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.*;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.event.entity.player.PlayerPickupXpEvent;
@Mod(modid="RemoteVibratorControlMod", name="RemoteVibratorControlMod", version="0.1", acceptableRemoteVersions = "*")
public class RemoteVibratorControl {
private VibratorController vibratorController = null;
private String playerName = "*";
private String serverURL = "";
@EventHandler
public void preInit(FMLPreInitializationEvent e)
{
vibratorController = new VibratorController();
Configuration config = new Configuration(e.getSuggestedConfigurationFile());
config.load();
playerName = config.get("General", "PlayerName", "*").getString();
serverURL = config.get("General", "ServerURL", "").getString();
config.save();
vibratorController.setURL(serverURL);
}
@EventHandler
public void init(FMLInitializationEvent e)
{
Console.out().println("Vibrator Controller Initiated!");
}
@EventHandler
public void postInit(FMLPostInitializationEvent e)
{
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void playerPickedUpXPEvent(PlayerPickupXpEvent e)
{
if(playerName.equals("*") || playerName.equals(e.entityPlayer.getCommandSenderName())) {
vibratorController.pushPoints(e.orb.xpValue);
}
}
}
# Configuration file
general {
S:PlayerName=<PlayerName>
S:ServerURL=<Address of the raspberry pi, like http://192.168.0.183:5000/>
}
package remoteVibratorControl;
//This class was taken from http://stackoverflow.com/a/3143189
//Thank you for your kind work!
import java.net.URL;
import java.util.concurrent.Callable;
public class Request implements Callable<Response> {
private URL url;
public Request(URL url) {
this.url = url;
}
@Override
public Response call() throws Exception {
return new Response(url.openStream());
}
}
package remoteVibratorControl;
import java.io.InputStream;
//This class was taken from http://stackoverflow.com/a/3143189
//Thank you for your kind work!
public class Response {
private InputStream body;
public Response(InputStream body) {
this.body = body;
}
public InputStream getBody() {
return body;
}
}
package remoteVibratorControl;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class VibratorController {
private String url = "";
private ExecutorService executor = null;
private int points = 0;
private int pointsReductionBySecond = 2;
public VibratorController() {
executor = Executors.newFixedThreadPool(1);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if(points - pointsReductionBySecond <= 0) {
points = 0;
setRate(points);
} else {
points -= pointsReductionBySecond;
setRate(points);
}
}
}, 1000, 1000);
}
public void setURL(String url) {
this.url = url;
}
public void close() {
executor.shutdown();
}
private static int rateBefore;
public void setRate(int rate) {
if(rate == rateBefore) {
return;
}
rateBefore = rate;
if(rate > 100) {
rate = 100;
} else if(rate < 0) {
rate = 0;
}
try {
executor.submit(new Request(new URL(url + "rate/" + rate + "/")));
System.out.println("Current Value: " + points);
} catch(MalformedURLException e) {
}
}
public void pushPoints(int amount) {
this.points += amount;
this.setRate(this.points);
}
public void dropPoints(int amount) {
this.points -= amount;
this.setRate(this.points);
}
}
# Controlling the vibrating egg
import RPi.GPIO as GPIO
from flask import Flask
app = Flask(__name__)
@app.route("/rate/<int:value>/")
def setRate(value):
if value >= 0 and value <= 100:
pwm.ChangeDutyCycle(value)
return "Value of the servo set to %d" % value
else:
return "%d was too high - has to be between 0 and 100!" % value
if __name__ == "__main__":
GPIO.setmode(GPIO.BCM)
pwmPin = 18
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(pwmPin, 50)
pwm.start(0)
app.run(host='0.0.0.0')
pwm.stop()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment