Skip to content

Commit

Permalink
Initial project layout and MoppyMessage implementation
Browse files Browse the repository at this point in the history
Created folder structure, MoppyLib project, and MoppyMessage implementation
  • Loading branch information
Sammy1Am committed Oct 29, 2017
1 parent 6ec6618 commit a002dfd
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
*.tar.gz
*.rar

# Gradle Artifacts #
**/.gradle/**

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/Java/MoppyLib/.nb-gradle/
/Java/MoppyLib/build/
2 changes: 2 additions & 0 deletions Arduino/FloppyDrives/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# MoppyLib
Library for core Moppy logic.
2 changes: 2 additions & 0 deletions Java/MoppyControlGUI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# MoppyControlGUI
Java GUI for controlling a Moppy network.
2 changes: 2 additions & 0 deletions Java/MoppyDeviceGUI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# MoppyDeviceGUI
A simulated moppy device that can be used for testing.
2 changes: 2 additions & 0 deletions Java/MoppyLib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# FloppyDrives.ino
Arduino sketch for controlling floppy drives using Moppy.
29 changes: 29 additions & 0 deletions Java/MoppyLib/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'java'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
ext.mainClass = ''
}

repositories {
mavenCentral()
// You may define additional repositories, or even remove "mavenCentral()".
// Read more about repositories here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}

dependencies {
// TODO: Add dependencies here ...
// You can read more about how to add dependency here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
testCompile group: 'junit', name: 'junit', version: '4.10'
}
1 change: 1 addition & 0 deletions Java/MoppyLib/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'MoppyLib'
74 changes: 74 additions & 0 deletions Java/MoppyLib/src/main/java/com/moppy/core/comm/MoppyMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.moppy.core.comm;

import java.util.Arrays;

/**
* Represents a message suitable for sending to a Moppy network.
*
* NOTE: Java treats ALL bytes as signed, but we're using them unsigned. Casts / conversions will be required!
*/
public class MoppyMessage {
private final byte[] messageBytes;

public static final byte START_BYTE = (byte)0x4d;
public static final byte SYSTEM_ADDRESS = (byte)0x00;

public static final MoppyMessage SYS_PING = new MoppyMessage(new byte[]{
START_BYTE, // Start byte
SYSTEM_ADDRESS, // System address
0x01, // Message body size
CommandByte.SYS_PING}); // Ping command
public static final MoppyMessage SYS_RESET = new MoppyMessage(new byte[]{START_BYTE, SYSTEM_ADDRESS, 0x01, CommandByte.SYS_RESET});
public static final MoppyMessage SYS_START = new MoppyMessage(new byte[]{START_BYTE, SYSTEM_ADDRESS, 0x01, CommandByte.SYS_START});
public static final MoppyMessage SYS_STOP = new MoppyMessage(new byte[]{START_BYTE, SYSTEM_ADDRESS, 0x01, CommandByte.SYS_STOP});


protected MoppyMessage(byte[] messageBytes) {
if (messageBytes.length < 4) {
throw new IllegalArgumentException("Not enough bytes for a MoppyMessage!");
}

this.messageBytes = messageBytes;
}

public static class CommandByte {
public static byte SYS_PING = (byte)0x80;
public static byte SYS_PONG = (byte)0x81;
public static byte SYS_RESET = (byte)0x82;
public static byte SYS_START = (byte)0x83;
public static byte SYS_STOP = (byte)0x84;

public static byte DEV_RESET = 0x00;
public static byte DEV_PLAYNOTE = 0x01;
public static byte DEV_STOPNOTE = 0x02;
public static byte DEV_BENDPITCH = 0x03;
}

public byte[] getMessageBytes(){
return messageBytes;
}

public byte getDeviceAddress() {
return messageBytes[1];
}

public boolean isSystemMessage() {
return getDeviceAddress() == SYSTEM_ADDRESS;
}

public byte getSubAddress() {
if (isSystemMessage()) {
throw new IllegalStateException("This is a system message and has no sub-address");
} else {
return messageBytes[2];
}
}

public byte[] getMessageBody() {
int bodyLength = isSystemMessage() ? messageBytes[2] : messageBytes[3];
int bodyStart = isSystemMessage() ? 3 : 4;
return Arrays.copyOfRange(messageBytes, bodyStart, bodyStart + bodyLength);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.moppy.core.comm;

import static com.moppy.core.comm.MoppyMessage.CommandByte.DEV_PLAYNOTE;
import static com.moppy.core.comm.MoppyMessage.CommandByte.DEV_RESET;
import static com.moppy.core.comm.MoppyMessage.CommandByte.DEV_STOPNOTE;
import static com.moppy.core.comm.MoppyMessage.CommandByte.SYS_PONG;
import static com.moppy.core.comm.MoppyMessage.START_BYTE;
import static com.moppy.core.comm.MoppyMessage.SYSTEM_ADDRESS;

/**
* Class for building common MoppyMessages.
*/
public class MoppyMessageFactory {
public static MoppyMessage systemPong(byte deviceAddress, byte numberOfSubAddresses) {
return new MoppyMessage(new byte[]{START_BYTE, SYSTEM_ADDRESS, 0x03, SYS_PONG, deviceAddress, numberOfSubAddresses});
}

public static MoppyMessage deviceReset(byte deviceAddress) {
return new MoppyMessage(new byte[]{START_BYTE, deviceAddress, 0x00, 0x01, DEV_RESET});
}

public static MoppyMessage devicePlayNote(byte deviceAddress, byte subAddress, byte noteNumber) {
return new MoppyMessage(new byte[]{START_BYTE, deviceAddress, subAddress, 0x02, DEV_PLAYNOTE, noteNumber});
}

public static MoppyMessage deviceStopNote(byte deviceAddress, byte subAddress, byte noteNumber) {
return new MoppyMessage(new byte[]{START_BYTE, deviceAddress, subAddress, 0x02, DEV_STOPNOTE, noteNumber});
}

public static MoppyMessage devicePitchBend(byte deviceAddress, byte subAddress, short bendAmount) {
return new MoppyMessage(new byte[]{START_BYTE, deviceAddress, subAddress, 0x03, DEV_STOPNOTE,
(byte)((bendAmount >> 8) & 0xff), (byte)(bendAmount & 0xff)});
}
}
1 change: 1 addition & 0 deletions Python/delete.me
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Placeholder to show where Python Moppy projects should go. Can be removed once the first project exists.
1 change: 1 addition & 0 deletions RaspPi/delete.me
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Placeholder to show where Raspberry Pi Moppy projects should go. Can be removed once the first project exists.

0 comments on commit a002dfd

Please sign in to comment.