Skip to content

Commit ce6ca36

Browse files
committed
feat: added SafeSetAngleToRight, SafeSetAngleToLeft, SafeSetDirectionToRight and SafeSetDirectionToLeft methods
1 parent 3996c18 commit ce6ca36

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

interfaces.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ type (
1313
IsAngleCentered() bool
1414
SetAngleToCenter() tinygoerrors.ErrorCode
1515
SetAngleToRight(angle uint16) tinygoerrors.ErrorCode
16+
SafeSetAngleToRight(angle uint16) tinygoerrors.ErrorCode
1617
SetAngleToLeft(angle uint16) tinygoerrors.ErrorCode
18+
SafeSetAngleToLeft(angle uint16) tinygoerrors.ErrorCode
1719
SetDirectionToCenter() tinygoerrors.ErrorCode
1820
SetDirectionToRight(angle uint16) tinygoerrors.ErrorCode
21+
SafeSetDirectionToRight(angle uint16) tinygoerrors.ErrorCode
1922
SetDirectionToLeft(angle uint16) tinygoerrors.ErrorCode
23+
SafeSetDirectionToLeft(angle uint16) tinygoerrors.ErrorCode
2024
}
2125
)

types.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ func (h *DefaultHandler) SetAngleToRight(angle uint16) tinygoerrors.ErrorCode {
221221
return h.SetAngleRelativeToCenter(-int16(angle))
222222
}
223223

224+
// SafeSetAngleToRight sets the servo motor to the right by a specified angle without exceeding limits
225+
//
226+
// Parameters:
227+
//
228+
// angle: The angle value to move the servo to the right, must be between 0 and the right limit
229+
//
230+
// Returns:
231+
//
232+
// An error if the angle is not within the right limit
233+
func (h *DefaultHandler) SafeSetAngleToRight(angle uint16) tinygoerrors.ErrorCode {
234+
if angle > h.maxAngle {
235+
angle = h.maxAngle
236+
}
237+
return h.SetAngleToRight(angle)
238+
}
239+
224240
// SetAngleToLeft sets the servo motor to the left by a specified angle
225241
//
226242
// Parameters:
@@ -234,6 +250,22 @@ func (h *DefaultHandler) SetAngleToLeft(angle uint16) tinygoerrors.ErrorCode {
234250
return h.SetAngleRelativeToCenter(int16(angle))
235251
}
236252

253+
// SafeSetAngleToLeft sets the servo motor to the left by a specified angle without exceeding limits
254+
//
255+
// Parameters:
256+
//
257+
// angle: The angle value to move the servo to the left, must be between 0 and the left limit
258+
//
259+
// Returns:
260+
//
261+
// An error if the angle is not within the left limit
262+
func (h *DefaultHandler) SafeSetAngleToLeft(angle uint16) tinygoerrors.ErrorCode {
263+
if angle > h.maxAngle {
264+
angle = h.maxAngle
265+
}
266+
return h.SetAngleToLeft(angle)
267+
}
268+
237269
// SetDirectionToCenter sets the direction to center
238270
func (h *DefaultHandler) SetDirectionToCenter() tinygoerrors.ErrorCode {
239271
return h.SetAngleToCenter()
@@ -252,6 +284,22 @@ func (h *DefaultHandler) SetDirectionToRight(angle uint16) tinygoerrors.ErrorCod
252284
return h.SetAngleToLeft(angle)
253285
}
254286

287+
// SafeSetDirectionToRight sets the direction to right without exceeding limits
288+
//
289+
// Parameters:
290+
//
291+
// angle: The angle value to move the servo to the left, must be between 0 and the left limit
292+
//
293+
// Returns:
294+
//
295+
// An error if the angle is not within the left limit
296+
func (h *DefaultHandler) SafeSetDirectionToRight(angle uint16) tinygoerrors.ErrorCode {
297+
if angle > h.maxAngle {
298+
angle = h.maxAngle
299+
}
300+
return h.SetDirectionToRight(angle)
301+
}
302+
255303
// SetDirectionToLeft sets the direction to left
256304
//
257305
// Parameters:
@@ -263,4 +311,20 @@ func (h *DefaultHandler) SetDirectionToRight(angle uint16) tinygoerrors.ErrorCod
263311
// An error if the angle is not within the right limit
264312
func (h *DefaultHandler) SetDirectionToLeft(angle uint16) tinygoerrors.ErrorCode {
265313
return h.SetAngleToRight(angle)
314+
}
315+
316+
// SafeSetDirectionToLeft sets the direction to left without exceeding limits
317+
//
318+
// Parameters:
319+
//
320+
// angle: The angle value to move the servo to the right, must be between 0 and the right limit
321+
//
322+
// Returns:
323+
//
324+
// An error if the angle is not within the right limit
325+
func (h *DefaultHandler) SafeSetDirectionToLeft(angle uint16) tinygoerrors.ErrorCode {
326+
if angle > h.maxAngle {
327+
angle = h.maxAngle
328+
}
329+
return h.SetDirectionToLeft(angle)
266330
}

0 commit comments

Comments
 (0)