001package com.ctre.phoenix.schedulers; 002import java.util.ArrayList; 003 004/** 005 * Scheduler that will run its ILoopables in sequence 006 */ 007public class SequentialScheduler implements com.ctre.phoenix.ILoopable 008{ 009 boolean _running = false; 010 ArrayList<com.ctre.phoenix.ILoopable> _loops = new ArrayList<com.ctre.phoenix.ILoopable>(); 011 int _periodMs; 012 int _idx; 013 boolean _iterated = false; 014 015 /** 016 * Constructor for SequentialScheduler 017 * @param periodMs Not used, pass 0 018 */ 019 public SequentialScheduler(int periodMs) 020 { 021 _periodMs = periodMs; 022 } 023 /** 024 * Add ILoopable to scheduler 025 * @param aLoop ILoopable to add 026 */ 027 public void add(com.ctre.phoenix.ILoopable aLoop) 028 { 029 _loops.add(aLoop); 030 } 031 032 /** 033 * Get the currently running ILoopable 034 * @return null, not implemented 035 */ 036 public com.ctre.phoenix.ILoopable getCurrent() 037 { 038 return null; 039 } 040 041 /** 042 * Remove all ILoopables 043 */ 044 public void removeAll() 045 { 046 _loops.clear(); 047 } 048 049 /** 050 * Start next ILoopable 051 */ 052 public void start() 053 { 054 _idx = 0; 055 if(_loops.size() > 0) 056 _loops.get(0).onStart(); 057 058 _running = true; 059 } 060 /** 061 * Stop every ILoopable 062 */ 063 public void stop() 064 { 065 for (int i = 0; i < _loops.size(); i++) 066 { 067 _loops.get(i).onStop(); 068 } 069 _running = false; 070 } 071 /** 072 * Process the currently active ILoopable 073 * 074 * Call this every loop 075 */ 076 public void process() 077 { 078 _iterated = false; 079 if (_idx < _loops.size()) 080 { 081 if (_running) 082 { 083 com.ctre.phoenix.ILoopable loop = _loops.get(_idx); 084 loop.onLoop(); 085 if (loop.isDone()) 086 { 087 _iterated = true; 088 ++_idx; 089 if (_idx < _loops.size()) _loops.get(_idx).onStart(); 090 } 091 } 092 } 093 else 094 { 095 _running = false; 096 } 097 } 098 /** 099 * @return true if we've just iterated to the next ILoopable 100 */ 101 public boolean iterated() 102 { 103 return _iterated; 104 } 105 //--- Loopable ---/ 106 /** 107 * Start next ILoopable 108 */ 109 public void onStart() 110 { 111 start(); 112 } 113 114 /** 115 * Process currently active ILoopable 116 */ 117 public void onLoop() 118 { 119 process(); 120 } 121 122 /** 123 * @return true when no longer running 124 */ 125 public boolean isDone() 126 { 127 /* Have to return something to know if we are done */ 128 if (_running == false) 129 return true; 130 else 131 return false; 132 } 133 134 /** 135 * Stop all ILoopables 136 */ 137 public void onStop() 138 { 139 stop(); 140 } 141}