Function InitGenSeqService(Priority):
- Set MyPriority to Priority.
- Configure PWM channels and timers:
1. Initialize PWM with basic configurations.
2. Assign PWM channels to Timer2.
3. Set Timer2 period to 25,000.
4. Map PWM channels to specific output pins.
- Post an ES_INIT event to this service's queue.
- Return true if the event is posted successfully, otherwise return false.
Function PostGenSeqService(Event):
- Post the given event to this service's queue.
- Return the result of the post operation (true/false).
Function RunGenSeqService(Event):
- Initialize a default ReturnEvent with EventType = ES_NO_EVENT.
**Event Handling:**
- If Event.EventType == PLAY_SEQUENCE:
1. Extract the sequence size from Event.EventParam.
2. Seed the random number generator with the current time.
3. Call generateSeq to generate the sequence arrays.
4. Reset counter and tracker to 0.
5. Set PWM channels to a neutral position (e.g., pulse width = 6500).
6. Start a timer (GENSEQ_TIMER) with a 1-second duration.
- If Event.EventType == ES_TIMEOUT:
1. If counter < (size * 2):
- For even tracker values (sequence "on" state):
- If sequence[counter] == 5:
- Turn on the LED.
- Else:
- Move the corresponding servo to an "on" position.
- For odd tracker values (sequence "off" state):
- If sequence[counter] == 5:
- Turn off the LED.
- Else:
- Move the corresponding servo to an "off" position.
- Post an EXPECTED_SEQUENCE event with the current sequence value.
2. If counter != (size * 2 - 1):
- Restart the timer (GENSEQ_TIMER) with a 650ms duration.
3. Increment counter and tracker.
- Return ReturnEvent.
Function generateSeq(sequence, user_sequence, size, min, max):
- For i = 0 to (size * 2 - 1):
1. Generate a random value within the range [min, max].
2. Assign the value to sequence[i] and sequence[i + 1].
3. Assign the value to user_sequence[i / 2].
4. Increment i by 1 (since pairs are generated).