package com.lynbrookrobotics.potassium.events

import com.lynbrookrobotics.potassium.Clock
import com.lynbrookrobotics.potassium.tasks.{ContinuousTask, Task}
import squants.Time

case class EventPolling(clock: Clock, period: Time)

class ContinuousEvent(condition: => Boolean)(implicit polling: EventPolling) {
  val onStartSource = new ImpulseEventSource
  val onEndSource = new ImpulseEventSource

  val onStart = onStartSource.event
  val onEnd = onEndSource.event

  private var tickingCallbacks: List[() => Unit] = List.empty
  private var isRunning = false

  polling.clock(polling.period) { dt =>
    if (condition) {
      tickingCallbacks.foreach(_.apply())

      if (!isRunning) {
        onStartSource.fire()

        isRunning = true
      }
    } else if (isRunning) {
      onEndSource.fire()
      isRunning = false
    }
  }

  def foreach(onTicking: () => Unit): Unit = {
    tickingCallbacks = onTicking :: tickingCallbacks
  }

  def foreach(task: ContinuousTask): Unit = {
    onStart.foreach(() => Task.executeTask(task))
    onEnd.foreach(() => Task.abortTask(task))
  }
}

package com.lynbrookrobotics.potassium.events

import com.lynbrookrobotics.potassium.Clock
import squants.Time

case class EventPolling(clock: Clock, period: Time)

class ContinuousEvent(condition: => Boolean)(implicit polling: EventPolling) {
  val onStartSource = new ImpulseEventSource
  val onEndSource = new ImpulseEventSource

  val onStart = onStartSource.event
  val onEnd = onEndSource.event

  private var tickingCallbacks: List[() => Unit] = List.empty
  private var isRunning = false

  polling.clock(polling.period) { dt =>
    if (condition) {
      tickingCallbacks.foreach(_.apply())

      if (!isRunning) {
        onStartSource.fire()

        isRunning = true
      }
    } else if (isRunning) {
      onEndSource.fire()
      isRunning = false
    }
  }

  def foreach(onTicking: () => Unit): Unit = {
    tickingCallbacks = onTicking :: tickingCallbacks
  }
}

--- 
+++ 
@@ -1,6 +1,7 @@
 package com.lynbrookrobotics.potassium.events
 
 import com.lynbrookrobotics.potassium.Clock
+import com.lynbrookrobotics.potassium.tasks.{ContinuousTask, Task}
 import squants.Time
 
 case class EventPolling(clock: Clock, period: Time)
@@ -33,4 +34,9 @@
   def foreach(onTicking: () => Unit): Unit = {
     tickingCallbacks = onTicking :: tickingCallbacks
   }
+
+  def foreach(task: ContinuousTask): Unit = {
+    onStart.foreach(() => Task.executeTask(task))
+    onEnd.foreach(() => Task.abortTask(task))
+  }
 }
Add ability to trigger a continuous task from a continuous event
