-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configuration file | |
general { | |
S:PlayerName=<PlayerName> | |
S:ServerURL=<Address of the raspberry pi, like http://192.168.0.183:5000/> | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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