127. PWM Register Access Control

In embedded firmware, hardware timer peripherals often expose Capture/Compare Registers (CCR) that directly control PWM duty cycles. Writing invalid values to these registers can cause incorrect output waveforms or hardware glitches.

You are given a PWMDriver class that represents a PWM timer driver. The hardware timer has a fixed period of 100 ticks, meaning the valid range for the Capture/Compare Register is 0 to 100 (inclusive).

Currently, the CCR register is publicly accessible, allowing external code to write invalid values (negative values or values greater than 100).

Your task is to refactor the driver to enforce safe access control:

  • Encapsulate the CCR register by making it private
  • Provide a public setter method that updates the register using saturation logic
  • Values greater than 100 must be clamped to 100
  • Values less than 0 must be clamped to 0

This ensures the internal register value always remains valid, regardless of user input.

Program Flow:

  1. Initialize a PWMDriver instance.
  2. Read an integer N representing the number of input values.
  3. Loop N times:
    • Read an integer request_val
    • Attempt to update the PWM duty cycle using the driver interface
    • Print the actual value stored in the CCR register

Input Format:

  • First line: Integer N (number of test inputs)
  • Next N lines: Integer request_val (requested duty cycle in ticks)

Input is provided via standard input (stdin).

Output Format:

For each input, print the stored register value in the following format:

CCR: <value> 

Each output must appear on a new line.

Example:

Input

3
50
150
-20 

Output

CCR: 50
CCR: 100
CCR: 0 

Constraints:

  • N range: 1 to 20
  • request_val range: -1000 to 1000
  • Hardware period limit: 100 (inclusive)
  • Minimum register value: 0 (inclusive)
  • No dynamic memory allocation is allowed

 

 

 

 

Loading...

Input

3 50 150 -20

Expected Output

CCR: 50 CCR: 100 CCR: 0