Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code formatting #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Ev3LangScala/src/ev3dev4s/Ev3System.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ object Ev3System {
lazy val portsToMotors: Map[MotorPort, Motor] = MotorPortScanner.scanMotors
lazy val portsToSensors: Map[SensorPort, Sensor[_]] = SensorPortScanner.scanSensors

}
}
8 changes: 4 additions & 4 deletions Ev3LangScala/src/ev3dev4s/JarRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ object JarRunner {
}

@tailrec
def runIt(jarFile:Path,className:String):Unit = {
def runIt(jarFile: Path, className: String): Unit = {
try {
Log.log(s"Start run() of $className from $jarFile")
val classLoader = new URLClassLoader(Array(jarFile.toUri.toURL))
classLoader.loadClass(className + "$").getField("MODULE$").get(Array.empty[Object]).asInstanceOf[Runnable].run()
}
catch {
case x: Throwable =>
Log.log("Caught in top-level",x)
Log.log("Caught in top-level", x)
MotorPortScanner.stopAllMotors()
Ev3Led.writeBothRed()
Lcd.clear()
x.getMessage.grouped(11).take(3).zipWithIndex.foreach { chunk =>
Lcd.set(chunk._2, chunk._1)
}
Lcd.set(3,"Push Button")
Lcd.set(3, "Push Button")
Lcd.flush()
Sound.playTone(55.Hz, 200.milliseconds)
Ev3KeyPad.blockUntilAnyKey()
Expand All @@ -68,4 +68,4 @@ object JarRunner {
}
runIt(jarFile, className)
}
}
}
3 changes: 2 additions & 1 deletion Ev3LangScala/src/ev3dev4s/Log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import java.io.PrintStream
object Log {
private val logStream: PrintStream = Option(System.getProperty("logFile")).map(new PrintStream(_)).getOrElse(System.out)

def log(text: String): Unit = logStream.println(s"${System.currentTimeMillis()} $text")
def log(text: String): Unit = logStream.println(s"${System.currentTimeMillis()} $text")

def log(text: String, x: Throwable): Unit = {
logStream.println(s"${System.currentTimeMillis()} $text")
x.printStackTrace(logStream)
}

}
60 changes: 32 additions & 28 deletions Ev3LangScala/src/ev3dev4s/actuators/Ev3Led.scala
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
package ev3dev4s.actuators

import ev3dev4s.scala2measure.Conversions._
import ev3dev4s.scala2measure.LedIntensity
import ev3dev4s.sysfs.ChannelRewriter

import java.nio.file.Path
import ev3dev4s.scala2measure.LedIntensity
import ev3dev4s.scala2measure.Conversions._

/**
*
*
* @author David Walend
*/

sealed case class Ev3Led(side:Int) extends AutoCloseable {
import ev3dev4s.actuators.Ev3Led.{Color,brightest, darkest,Off,Red,Green,Yellow}
sealed case class Ev3Led(side: Int) extends AutoCloseable {

import ev3dev4s.actuators.Ev3Led.{Color, Off, Red, Green, Yellow} // brightest, darkest

val rootName = "/sys/class"
//noinspection SpellCheckingInspection
val redName = s"leds/led$side:red:brick-status/brightness"
//noinspection SpellCheckingInspection
val greenName = s"leds/led$side:green:brick-status/brightness"

private val redPath = Path.of (rootName, redName)
private val greenPath = Path.of (rootName, greenName)
private val redPath = Path.of(rootName, redName)
private val greenPath = Path.of(rootName, greenName)

private val redWriter: ChannelRewriter = ChannelRewriter (redPath)
private val greenWriter: ChannelRewriter = ChannelRewriter (greenPath)
private val redWriter: ChannelRewriter = ChannelRewriter(redPath)
private val greenWriter: ChannelRewriter = ChannelRewriter(greenPath)
//todo add readers to read brightness from the same paths maybe someday - it will work, not sure if it has any value

def writeBrightness (red: LedIntensity, green: LedIntensity): Unit = this.synchronized {
redWriter.writeAsciiInt (red.round)
greenWriter.writeAsciiInt (green.round)
def writeBrightness(red: LedIntensity, green: LedIntensity): Unit = this.synchronized {
redWriter.writeAsciiInt(red.round)
greenWriter.writeAsciiInt(green.round)
}

override def close (): Unit = this.synchronized {
redWriter.close ()
greenWriter.close ()
override def close(): Unit = this.synchronized {
redWriter.close()
greenWriter.close()
}

def writeColor(color:Color) = {
writeBrightness(color.red,color.green)
def writeColor(color: Color): Unit = {
writeBrightness(color.red, color.green)
}

def writeOff (): Unit = writeColor(Off)
def writeRed (): Unit = writeColor(Red)
def writeGreen (): Unit = writeColor(Green)
def writeYellow (): Unit = writeColor(Yellow)
def writeOff(): Unit = writeColor(Off)

def writeRed(): Unit = writeColor(Red)

def writeGreen(): Unit = writeColor(Green)

def writeYellow(): Unit = writeColor(Yellow)
}

object Ev3Led {
Expand All @@ -57,10 +61,10 @@ object Ev3Led {

case class Color(red: LedIntensity, green: LedIntensity)

val Red: Color = Color(brightest,darkest)
val Yellow: Color = Color(brightest,brightest)
val Green: Color = Color(darkest,brightest)
val Off: Color = Color(darkest,darkest)
val Red: Color = Color(brightest, darkest)
val Yellow: Color = Color(brightest, brightest)
val Green: Color = Color(darkest, brightest)
val Off: Color = Color(darkest, darkest)

def writeBothGreen(): Unit = writeBothColor(Green)

Expand All @@ -81,17 +85,17 @@ object Ev3Led {
* Yellow Red
* Yellow Off
* */
def writeBothColor(leftColor:Color,rightColor:Color):Unit = {
def writeBothColor(leftColor: Color, rightColor: Color): Unit = {
Left.writeColor(leftColor)
Right.writeColor(rightColor)
}

def writeBothColor(colors:(Color,Color)): Unit = {
def writeBothColor(colors: (Color, Color)): Unit = {
Left.writeColor(colors._1)
Right.writeColor(colors._2)
}

def writeBothColor(color:Color):Unit = {
writeBothColor(color,color)
def writeBothColor(color: Color): Unit = {
writeBothColor(color, color)
}
}
78 changes: 41 additions & 37 deletions Ev3LangScala/src/ev3dev4s/actuators/Motor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ import ev3dev4s.scala2measure.MilliSeconds
* @author David Walend
* @since v0.0.0
*/
sealed abstract class Motor(port: MotorPort,motorFS:Option[MotorFS]) extends Gadget(port,motorFS){
//noinspection ScalaUnusedSymbol
sealed abstract class Motor(port: MotorPort, motorFS: Option[MotorFS]) extends Gadget(port, motorFS) {

def writeCommand(command: MotorCommand):Unit = checkPort(_.writeCommand(command))
def writeCommand(command: MotorCommand): Unit = checkPort(_.writeCommand(command))

def writeStopAction(command:MotorStopCommand):Unit = checkPort(_.writeStopAction(command))
def writeStopAction(command: MotorStopCommand): Unit = checkPort(_.writeStopAction(command))

def writeDutyCycle(dutyCycle:DutyCycle):Unit = checkPort(_.writeDutyCycle(dutyCycle))
def writeDutyCycle(dutyCycle: DutyCycle): Unit = checkPort(_.writeDutyCycle(dutyCycle))

def maxSpeed:DegreesPerSecond
def maxSpeed: DegreesPerSecond

def observedMaxSpeed:DegreesPerSecond
def observedMaxSpeed: DegreesPerSecond

def writeSpeed(speed:DegreesPerSecond):Unit = {
val safeSpeed = if(speed.abs < maxSpeed ) speed
def writeSpeed(speed: DegreesPerSecond): Unit = {
val safeSpeed = if (speed.abs < maxSpeed) { speed }
else {
Log.log(s"requested speed $speed is greater than $maxSpeed - using $maxSpeed")
(speed.sign * maxSpeed).degreesPerSecond
Expand All @@ -45,25 +46,25 @@ sealed abstract class Motor(port: MotorPort,motorFS:Option[MotorFS]) extends Gad
checkPort(_.writeRampDownSpeed(fromMaxToZero))
}

def writePosition(degrees:Degrees):Unit = checkPort(_.writePosition(degrees))
def writePosition(degrees: Degrees): Unit = checkPort(_.writePosition(degrees))

def resetPosition():Unit = writePosition(0.degrees)
def resetPosition(): Unit = writePosition(0.degrees)

def writeGoalPosition(degrees:Degrees):Unit = checkPort(_.writeGoalPosition(degrees))
def writeGoalPosition(degrees: Degrees): Unit = checkPort(_.writeGoalPosition(degrees))

def writeDuration(milliseconds:MilliSeconds):Unit = checkPort(_.writeDuration(milliseconds))
def writeDuration(milliseconds: MilliSeconds): Unit = checkPort(_.writeDuration(milliseconds))

/**
* @return position in degrees
*/
def readPosition():Degrees = checkPort(_.readPosition())
def readPosition(): Degrees = checkPort(_.readPosition())

def readState(): Array[MotorState] = checkPort(_.readState())

def readIsStalled():Boolean =
def readIsStalled(): Boolean =
readState().contains(MotorState.STALLED)

def readIsRunning():Boolean =
def readIsRunning(): Boolean =
readState().contains(MotorState.RUNNING)

def coast(): Unit = {
Expand All @@ -75,40 +76,42 @@ sealed abstract class Motor(port: MotorPort,motorFS:Option[MotorFS]) extends Gad
writeStopAction(MotorStopCommand.BRAKE)
writeCommand(MotorCommand.STOP)
}

def hold(): Unit = {
writeStopAction(MotorStopCommand.HOLD)
writeCommand(MotorCommand.STOP)
}
def runDutyCycle(dutyCycle:DutyCycle):Unit = {

def runDutyCycle(dutyCycle: DutyCycle): Unit = {
writeDutyCycle(dutyCycle)
writeCommand(MotorCommand.RUN_DIRECT)
}

def run(speed:DegreesPerSecond):Unit = {
def run(speed: DegreesPerSecond): Unit = {
writeSpeed(speed)
writeCommand(MotorCommand.RUN)
}

def runToAbsolutePosition(speed:DegreesPerSecond,degrees:Degrees):Unit = {
def runToAbsolutePosition(speed: DegreesPerSecond, degrees: Degrees): Unit = {
writeSpeed(speed)
writeGoalPosition(degrees)
writeCommand(MotorCommand.RUN_TO_ABSOLUTE_POSITION)
}

def runToRelativePosition(speed:DegreesPerSecond,degrees:Degrees):Unit = {
def runToRelativePosition(speed: DegreesPerSecond, degrees: Degrees): Unit = {
writeSpeed(speed)
writeGoalPosition(degrees)
writeCommand(MotorCommand.RUN_TO_RELATIVE_POSITION)
}

def runForDuration(speed:DegreesPerSecond,milliseconds:MilliSeconds):Unit = {
def runForDuration(speed: DegreesPerSecond, milliseconds: MilliSeconds): Unit = {
writeSpeed(speed)
writeDuration(milliseconds)
writeCommand(MotorCommand.RUN_TIME)
}
}

sealed case class Ev3LargeMotor(override val port:MotorPort, md: Option[MotorFS]) extends Motor(port,md) {
sealed case class Ev3LargeMotor(override val port: MotorPort, md: Option[MotorFS]) extends Motor(port, md) {
override def findGadgetFS(): Option[MotorFS] =
MotorPortScanner.findGadgetDir(port, Ev3LargeMotor.driverName)
.map(MotorFS)
Expand All @@ -121,7 +124,7 @@ object Ev3LargeMotor {
val driverName = "lego-ev3-l-motor"
}

sealed case class Ev3MediumMotor(override val port:MotorPort, md: Option[MotorFS]) extends Motor(port,md) {
sealed case class Ev3MediumMotor(override val port: MotorPort, md: Option[MotorFS]) extends Motor(port, md) {
override def findGadgetFS(): Option[MotorFS] =
MotorPortScanner.findGadgetDir(port, Ev3MediumMotor.driverName)
.map(MotorFS)
Expand All @@ -134,7 +137,7 @@ object Ev3MediumMotor {
val driverName = "lego-ev3-m-motor"
}

sealed case class MotorCommand(command:String)
sealed case class MotorCommand(command: String)

object MotorCommand {
/**
Expand All @@ -148,11 +151,11 @@ object MotorCommand {
val RUN_TO_ABSOLUTE_POSITION: MotorCommand = MotorCommand("run-to-abs-pos")

/**
run-to-rel-pos: Runs the motor to a position relative to the current position v. The new position will be current position + position_sp. When the new position is reached, the motor will stop using the command specified by stop_action. */
* run-to-rel-pos: Runs the motor to a position relative to the current position v. The new position will be current position + position_sp. When the new position is reached, the motor will stop using the command specified by stop_action. */
val RUN_TO_RELATIVE_POSITION: MotorCommand = MotorCommand("run-to-rel-pos")

/**
run-timed: Run the motor for the amount of time specified in time_sp and then stops the motor using the command specified by stop_action.
* run-timed: Run the motor for the amount of time specified in time_sp and then stops the motor using the command specified by stop_action.
*/
val RUN_TIME: MotorCommand = MotorCommand("run-timed")

Expand All @@ -166,15 +169,16 @@ object MotorCommand {
val STOP: MotorCommand = MotorCommand("stop")

/**
reset: Resets all of the motor parameter attributes to their default values. This will also have the effect of stopping the motor.
* reset: Resets all of the motor parameter attributes to their default values. This will also have the effect of stopping the motor.
*/
}

/**
* Determines the motors behavior when command is set to stop. Possible values are:
*/
sealed case class MotorStopCommand(command:String)
sealed case class MotorStopCommand(command: String)

object MotorStopCommand{
object MotorStopCommand {
/**
* Removes power from the motor. The motor will freely coast to a stop.
*/
Expand All @@ -191,29 +195,29 @@ object MotorStopCommand{
val HOLD: MotorStopCommand = MotorStopCommand("hold")
}

sealed case class MotorState(name:String)
sealed case class MotorState(name: String)

object MotorState{
object MotorState {
/**
running: Power is being sent to the motor.
* running: Power is being sent to the motor.
*/
val RUNNING: MotorState = MotorState("running")
/**
ramping: The motor is ramping up or down and has not yet reached a constant output level.
* ramping: The motor is ramping up or down and has not yet reached a constant output level.
*/
val RAMPING: MotorState = MotorState("ramping")
/**
holding: The motor is not turning, but rather attempting to hold a fixed position.
* holding: The motor is not turning, but rather attempting to hold a fixed position.
*/
val HOLDING: MotorState = MotorState("holding")
/**
overloaded: The motor is turning as fast as possible, but cannot reach its speed_sp.
* overloaded: The motor is turning as fast as possible, but cannot reach its speed_sp.
*/
val OVERLOADED: MotorState = MotorState("overloaded")
/**
stalled: The motor is trying to run but is not turning at all.
* stalled: The motor is trying to run but is not turning at all.
*/
val STALLED: MotorState = MotorState("stalled")

val values: Array[MotorState] = Array(RUNNING,RAMPING,HOLDING,OVERLOADED,STALLED)
}
val values: Array[MotorState] = Array(RUNNING, RAMPING, HOLDING, OVERLOADED, STALLED)
}
Loading