1 /* TSI 2023.xmo */
2 /*******************************************************************************
3  * Copyright (c) 2023 Think Silicon Single Member PC
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this header file and/or associated documentation files to use, copy,
7  * modify, merge, publish, distribute, sublicense, and/or sell copies of the
8  * Materials, and to permit persons to whom the Materials are furnished to do
9  * so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Materials.
13  *
14  * MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15  * NEMAGFX API. THE UNMODIFIED, NORMATIVE VERSIONS OF THINK-SILICON NEMAGFX
16  * SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT:
17  *   https://think-silicon.com/products/software/nemagfx-api
18  *
19  *  The software is provided 'as is', without warranty of any kind, express or
20  *  implied, including but not limited to the warranties of merchantability,
21  *  fitness for a particular purpose and noninfringement. In no event shall
22  *  Think Silicon Single Member PC be liable for any claim, damages or other
23  *  liability, whether in an action of contract, tort or otherwise, arising
24  *  from, out of or in connection with the software or the use or other dealings
25  *  in the software.
26  ******************************************************************************/
27 
28 
29 #ifndef NEMA_EASING_H__
30 #define NEMA_EASING_H__
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 //Linear
37 
38 // Modeled after the line y = x
39 /** \brief Linear easing, no acceleration
40  *
41  * \param p Input value, typically within the [0, 1] range
42  * \return Eased value
43  *
44  */
45 float nema_ez_linear(float p);
46 
47 //Quadratic
48 
49 // Modeled after the parabola y = x^2
50 /** \brief Quadratic easing in, accelerate from zero
51  *
52  * \param p Input value, typically within the [0, 1] range
53  * \return Eased value
54  *
55  */
56 float nema_ez_quad_in(float p);
57 
58 // Modeled after the parabola y = -x^2 + 2x
59 /** \brief Quadratic easing out, decelerate to zero velocity
60  *
61  * \param p Input value, typically within the [0, 1] range
62  * \return Eased value
63  *
64  */
65 float nema_ez_quad_out(float p);
66 
67 // Modeled after the piecewise quadratic
68 // y = (1/2)((2x)^2)             ; [0, 0.5)
69 // y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
70 /** \brief Quadratic easing in and out, accelerate to halfway, then decelerate
71  *
72  * \param p Input value, typically within the [0, 1] range
73  * \return Eased value
74  *
75  */
76 float nema_ez_quad_in_out(float p);
77 
78 //Cubic
79 
80 // Modeled after the cubic y = x^3
81 /** \brief Cubic easing in, accelerate from zero
82  *
83  * \param p Input value, typically within the [0, 1] range
84  * \return Eased value
85  *
86  */
87 float nema_ez_cub_in(float p);
88 
89 // Modeled after the cubic y = (x - 1)^3 + 1
90 /** \brief Cubic easing out, decelerate to zero velocity
91  *
92  * \param p Input value, typically within the [0, 1] range
93  * \return Eased value
94  *
95  */
96 float nema_ez_cub_out(float p);
97 
98 // Modeled after the piecewise cubic
99 // y = (1/2)((2x)^3)       ; [0, 0.5)
100 // y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
101 /** \brief Cubic easing in and out, accelerate to halfway, then decelerate
102  *
103  * \param p Input value, typically within the [0, 1] range
104  * \return Eased value
105  *
106  */
107 float nema_ez_cub_in_out(float p);
108 
109 //Quartic
110 
111 // Modeled after the quartic x^4
112 /** \brief Quartic easing in, accelerate from zero
113  *
114  * \param p Input value, typically within the [0, 1] range
115  * \return Eased value
116  *
117  */
118 float nema_ez_quar_in(float p);
119 
120 // Modeled after the quartic y = 1 - (x - 1)^4
121 /** \brief Quartic easing out, decelerate to zero velocity
122  *
123  * \param p Input value, typically within the [0, 1] range
124  * \return Eased value
125  *
126  */
127 float nema_ez_quar_out(float p);
128 
129 // Modeled after the piecewise quartic
130 // y = (1/2)((2x)^4)        ; [0, 0.5)
131 // y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
132 /** \brief Quartic easing in and out, accelerate to halfway, then decelerate
133  *
134  * \param p Input value, typically within the [0, 1] range
135  * \return Eased value
136  *
137  */
138 float nema_ez_quar_in_out(float p);
139 
140 //Quintic
141 
142 // Modeled after the quintic y = x^5
143 /** \brief Quintic easing in, accelerate from zero
144  *
145  * \param p Input value, typically within the [0, 1] range
146  * \return Eased value
147  *
148  */
149 float nema_ez_quin_in(float p);
150 
151 // Modeled after the quintic y = (x - 1)^5 + 1
152 /** \brief Quintic easing out, decelerate to zero velocity
153  *
154  * \param p Input value, typically within the [0, 1] range
155  * \return Eased value
156  *
157  */
158 float nema_ez_quin_out(float p);
159 
160 // Modeled after the piecewise quintic
161 // y = (1/2)((2x)^5)       ; [0, 0.5)
162 // y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
163 /** \brief Quintic easing in and out, accelerate to halfway, then decelerate
164  *
165  * \param p Input value, typically within the [0, 1] range
166  * \return Eased value
167  *
168  */
169 float nema_ez_quin_in_out(float p);
170 
171 //Sin
172 
173 // Modeled after quarter-cycle of sine wave
174 /** \brief Sinusoidal easing in, accelerate from zero
175  *
176  * \param p Input value, typically within the [0, 1] range
177  * \return Eased value
178  *
179  */
180 float nema_ez_sin_in(float p);
181 
182 // Modeled after quarter-cycle of sine wave (different phase)
183 /** \brief Sinusoidal easing out, decelerate to zero velocity
184  *
185  * \param p Input value, typically within the [0, 1] range
186  * \return Eased value
187  *
188  */
189 float nema_ez_sin_out(float p);
190 
191 // Modeled after half sine wave
192 /** \brief Sinusoidal easing in and out, accelerate to halfway, then decelerate
193  *
194  * \param p Input value, typically within the [0, 1] range
195  * \return Eased value
196  *
197  */
198 float nema_ez_sin_in_out(float p);
199 
200 //Circular
201 
202 // Modeled after shifted quadrant IV of unit circle
203 /** \brief Circular easing in, accelerate from zero
204  *
205  * \param p Input value, typically within the [0, 1] range
206  * \return Eased value
207  *
208  */
209 float nema_ez_circ_in(float p);
210 
211 // Modeled after shifted quadrant II of unit circle
212 /** \brief Circular easing out, decelerate to zero velocity
213  *
214  * \param p Input value, typically within the [0, 1] range
215  * \return Eased value
216  *
217  */
218 float nema_ez_circ_out(float p);
219 
220 // Modeled after the piecewise circular function
221 // y = (1/2)(1 - sqrt(1 - 4x^2))           ; [0, 0.5)
222 // y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
223 /** \brief Circular easing in and out, accelerate to halfway, then decelerate
224  *
225  * \param p Input value, typically within the [0, 1] range
226  * \return Eased value
227  *
228  */
229 float nema_ez_circ_in_out(float p);
230 
231 //Exponential
232 
233 // Modeled after the exponential function y = 2^(10(x - 1))
234 /** \brief Exponential easing in, accelerate from zero
235  *
236  * \param p Input value, typically within the [0, 1] range
237  * \return Eased value
238  *
239  */
240 float nema_ez_exp_in(float p);
241 
242 // Modeled after the exponential function y = -2^(-10x) + 1
243 /** \brief Exponential easing out, decelerate to zero velocity
244  *
245  * \param p Input value, typically within the [0, 1] range
246  * \return Eased value
247  *
248  */
249 float nema_ez_exp_out(float p);
250 
251 // Modeled after the piecewise exponential
252 // y = (1/2)2^(10(2x - 1))         ; [0,0.5)
253 // y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
254 /** \brief Exponential easing in and out, accelerate to halfway, then decelerate
255  *
256  * \param p Input value, typically within the [0, 1] range
257  * \return Eased value
258  *
259  */
260 float nema_ez_exp_in_out(float p);
261 
262 //Elastic
263 // Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
264 
265 /** \brief Elastic easing in, accelerate from zero
266  *
267  * \param p Input value, typically within the [0, 1] range
268  * \return Eased value
269  *
270  */
271 float nema_ez_elast_in(float p);
272 
273 // Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1
274 /** \brief Elastic easing out, decelerate to zero velocity
275  *
276  * \param p Input value, typically within the [0, 1] range
277  * \return Eased value
278  *
279  */
280 float nema_ez_elast_out(float p);
281 
282 // Modeled after the piecewise exponentially-damped sine wave:
283 // y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1))      ; [0,0.5)
284 // y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
285 /** \brief Elastic easing in and out, accelerate to halfway, then decelerate
286  *
287  * \param p Input value, typically within the [0, 1] range
288  * \return Eased value
289  *
290  */
291 float nema_ez_elast_in_out(float p);
292 
293 //Back
294 
295 // Modeled after the overshooting cubic y = x^3-x*sin(x*pi)
296 /** \brief Overshooting easing in, accelerate from zero
297  *
298  * \param p Input value, typically within the [0, 1] range
299  * \return Eased value
300  *
301  */
302 float nema_ez_back_in(float p);
303 
304 // Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
305 /** \brief Overshooting easing out, decelerate to zero velocity
306  *
307  * \param p Input value, typically within the [0, 1] range
308  * \return Eased value
309  *
310  */
311 float nema_ez_back_out(float p);
312 
313 // Modeled after the piecewise overshooting cubic function:
314 // y = (1/2)*((2x)^3-(2x)*sin(2*x*pi))           ; [0, 0.5)
315 // y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
316 /** \brief Overshooting easing in and out, accelerate to halfway, then decelerate
317  *
318  * \param p Input value, typically within the [0, 1] range
319  * \return Eased value
320  *
321  */
322 float nema_ez_back_in_out(float p);
323 
324 //Bounce
325 
326 /** \brief Bouncing easing in, accelerate from zero
327  *
328  * \param p Input value, typically within the [0, 1] range
329  * \return Eased value
330  *
331  */
332 float nema_ez_bounce_out(float p);
333 
334 /** \brief Bouncing easing out, decelerate to zero velocity
335  *
336  * \param p Input value, typically within the [0, 1] range
337  * \return Eased value
338  *
339  */
340 float nema_ez_bounce_in(float p);
341 
342 /** \brief Bouncing easing in and out, accelerate to halfway, then decelerate
343  *
344  * \param p Input value, typically within the [0, 1] range
345  * \return Eased value
346  *
347  */
348 float nema_ez_bounce_in_out(float p);
349 
350 
351 /** \brief Convenience function to perform easing between two values given number of steps, current step and easing function
352  *
353  * \param A         Initial value within range [0, 1]
354  * \param B         Finale value within range [0, 1]
355  * \param steps     Total number of steps
356  * \param cur_step  Current Step
357  * \param ez_func   pointer to the desired easing function
358  * \return Eased value
359  *
360  */
361 
362 
363 float nema_ez(float A, float B, float steps, float cur_step, float (*ez_func)(float p));
364 
365 #ifdef __cplusplus
366 }
367 #endif
368 
369 #endif
370