firmware  v0.1.2
Chromation Spectrometer Dev-Kit
test_VisCmd.c
1 #include "unity.h" // unity macros: TEST_BLAH
2 #include "Mock.h" // record call history in "mock"
3 #include "test_VisCmd.h"
4 #include "VisCmd.h"
5 
6 /* ---Global Queue--- */
7 #include "Queue.h" // VisCmd accesses the Spi Rx Buffer
8 volatile Queue_s * SpiFifo; // extern in VisCmd.h
9 #define max_length_of_queue 5 // bytes
10 
11 /* =====[ Test Helpers ]===== */
12 static void _AssertCall(uint16_t num, char const * name)
13 {
15  // Put num and name in the message displayed if test fails
16  GString *message = g_string_new(NULL);
17  g_string_append_printf(message, "`%s` is not call %d", name, num);
18  // Perform the test
19  TEST_ASSERT_TRUE_MESSAGE(
20  AssertCall(mock, num, name),
21  message->str
22  );
23  // Free memory used by GString
24  g_string_free(message, true);
25 }
26 static void _AssertArg(uint16_t call_n, uint8_t arg_n, void *pval)
27 {
29 
66  GString *msg = g_string_new(NULL);
67  g_string_printf(msg, "Expect different value for call %d arg %d.", call_n, arg_n);
68  // I cannot print the expected value without asking the
69  // caller to include the type as a string. Better to keep the
70  // arg list short. Call and arg number are good enough.
71  TEST_ASSERT_TRUE_MESSAGE(
72  AssertArg(mock, call_n, arg_n, pval),
73  msg->str
74  );
75  g_string_free(msg, true);
76 }
77 static void _AssertArgByteVal(uint16_t call_n, uint8_t arg_n, uint8_t byte_val) { _AssertArg(call_n, arg_n, &byte_val); }
78 static void _test_call_count_is(uint16_t num)
79 {
80  // Print this message if test fails
81  // Note: msg is appended if I use a TEST_ASSERT_EQUAL macro,
82  // so use TEST_FAIL_MESSAGE macro instead.
83  GString *msg = g_string_new(NULL);
84  g_string_printf(msg,
85  "Expect call count is %d, but was %d.",
86  num, NumberOfActualCalls(mock)
87  );
88  // Run test
89  if (num == NumberOfActualCalls(mock)) TEST_PASS();
90  else TEST_FAIL_MESSAGE(msg->str);
91  // Free string-object memory
92  g_string_free(msg, true);
93 }
94 
95 #ifdef LIS
96 /* =====[ LisReadout ]===== */
97 void LisReadout_waits_for_Lis_Sync_to_go_HIGH_then_go_LOW(void)
98 {
99  TEST_PASS();
100 }
101 void LisReadout_reads_one_pixel_on_each_rising_edge_of_Lis_Clk(void)
102 {
103  TEST_PASS();
104 }
105 void LisReadout_LOOP_wait_for_the_rising_edge_of_Lis_Clk(void)
106 {
107  TEST_PASS();
108 }
109 void LisReadout_LOOP_start_the_ADC_conversion(void)
110 {
111  TEST_PASS();
112 }
113 void LisReadout_LOOP_wait_for_45_cycles_of_10MHz_clock(void)
114 {
115  TEST_PASS();
116 }
117 void LisReadout_LOOP_start_ADC_readout(void)
118 {
119  TEST_PASS();
120 }
121 void LisReadout_LOOP_wait_for_most_significant_byte_ADC_readout(void)
122 {
123  TEST_PASS();
124 }
125 void LisReadout_LOOP_save_MSB_to_frame_buffer(void)
126 {
127  TEST_PASS();
128 }
129 void LisReadout_LOOP_wait_for_least_significant_byte_ADC_readout(void)
130 {
131  TEST_PASS();
132 }
133 void LisReadout_LOOP_save_LSB_to_frame_buffer(void)
134 {
135  TEST_PASS();
136 }
137 #endif // ifdef LIS
138 
139 #ifdef S13131
140 void S13131Readout_must_be_called_immediately_after_S13131Expose(void)
141 {
142  TEST_PASS();
143 }
144 void S13131Readout_stores_pixel_values_in_a_global_array_named_frame(void)
145 {
146  TEST_PASS();
147 }
148 void S13131Readout_reads_one_pixel_on_each_falling_edge_of_CLK(void)
149 {
150  TEST_PASS();
151 }
152 void S13131Readout_LOOP_wait_for_a_CLK_falling_edge(void)
153 {
154  /* =====[ Setup ]===== */
155  // Fake that next byte from ADC is always ready for readout
156  SetBit(UartSpi_UCSR0A, UartSpi_RXC0);
157  /* =====[ Operate ]===== */
158  S13131Readout();
159  /* =====[ Test ]===== */
160  _AssertCall(1, "WaitForS13131ClkLow");
161 }
162 void S13131Readout_LOOP_start_the_ADC_conversion(void)
163 {
164  /* =====[ Setup ]===== */
165  // Fake that next byte from ADC is always ready for readout
166  SetBit(UartSpi_UCSR0A, UartSpi_RXC0);
167  /* =====[ Operate ]===== */
168  S13131Readout();
169  /* =====[ Test ]===== */
170  _AssertCall(2, "StartAdcConversion");
171 }
172 void S13131Readout_LOOP_wait_for_45_cycles_of_10MHz_clock(void)
173 {
174  TEST_PASS();
175 }
176 
177 void S13131Readout_LOOP_start_ADC_readout(void)
178 {
179  TEST_PASS();
180 }
181 void S13131Readout_LOOP_wait_for_most_significant_byte_ADC_readout(void)
182 {
183  TEST_PASS();
184 }
185 void S13131Readout_LOOP_save_MSB_to_frame_buffer(void)
186 {
187  TEST_PASS();
188 }
189 void S13131Readout_LOOP_wait_for_least_significant_byte_ADC_readout(void)
190 {
191  TEST_PASS();
192 }
193 void S13131Readout_LOOP_save_LSB_to_frame_buffer(void)
194 {
195  TEST_PASS();
196 }
197 #endif // ifdef S13131
198 
199 /* =====[ GetSensorLED ]===== */
200 void GetSensorLED_waits_for_byte_led_num(void)
201 {
202  TEST_PASS();
203 }
204 void GetSensorLED_reads_byte_led_num(void)
205 {
206  TEST_PASS();
207 }
208 void GetSensorLED_sends_OK_and_LED_SETTING_if_led_num_is_valid(void)
209 {
210  TEST_PASS();
211 }
212 void GetSensorLED_sends_ERROR_and_pads_second_byte_if_led_num_is_invalid(void)
213 {
214  TEST_PASS();
215 }
216 
217 /* =====[ LedNumIsValid ]===== */
218 void LedNumIsValid_returns_TRUE_if_led_num_is_0(void)
219 {
220  /* =====[ Operate and Test]===== */
221  TEST_ASSERT_TRUE(LedNumIsValid(0));
222 }
223 void LedNumIsValid_returns_TRUE_if_led_num_is_1(void)
224 {
225  /* =====[ Operate and Test]===== */
226  TEST_ASSERT_TRUE(LedNumIsValid(1));
227 }
228 void LedNumIsValid_returns_FALSE_if_led_num_is_not_0_or_1(void)
229 {
230  /* =====[ Operate and Test]===== */
231  TEST_ASSERT_FALSE(LedNumIsValid(2));
232 }
233 
234 /* =====[ SetSensorLED ]===== */
235 void SetSensorLED_waits_for_byte_led_num(void)
236 {
237  TEST_PASS();
238 }
239 void SetSensorLED_reads_byte_led_num(void)
240 {
241  TEST_PASS();
242 }
243 void SetSensorLED_waits_for_byte_led_setting(void)
244 {
245  TEST_PASS();
246 }
247 void SetSensorLED_reads_byte_led_setting(void)
248 {
249  TEST_PASS();
250 }
251 void SetSensorLED_sends_ERROR_if_led_num_is_invalid(void)
252 {
253  TEST_PASS();
254 }
255 void SetSensorLED_sends_ERROR_if_led_setting_is_invalid(void)
256 {
257  TEST_PASS();
258 }
259 void SetSensorLED_applies_LED_setting_if_valid(void)
260 {
261  TEST_PASS();
262 }
263 void SetSensorLED_sends_OK_if_num_and_setting_are_valid(void)
264 {
265  TEST_PASS();
266 }
267 
268 #ifdef LIS
269 /* =====[ GetSensorConfig ]===== */
270 void GetSensorConfig_sends_OK(void)
271 {
272  /* =====[ Operate ]===== */
273  /* GetSensorConfig(); */
274  TEST_PASS();
275 }
276 void GetSensorConfig_sends_binning(void)
277 {
278  TEST_PASS();
279 }
280 void GetSensorConfig_sends_gain(void)
281 {
282  TEST_PASS();
283 }
284 void GetSensorConfig_sends_row_bitmap(void)
285 {
286  TEST_PASS();
287 }
288 
289 /* =====[ SetSensorConfig ]===== */
290 void SetSensorConfig_replies_OK_to_confirm_it_recognized_this_command(void)
291 {
292  printf("\nTesting `SetSensorConfig_replies_OK_to_confirm_it_recognized_this_command...`\n");
293  /* =====[ Setup ]===== */
294  // Gotta initialize variables and buffer memory in the SPI Rx
295  // Queue.
296  volatile uint8_t spi_rx_buffer[max_length_of_queue];
298  // Gotta queue up some fake bytes so SetSensorConfig doesn't
299  // hang on an empty queue.
300  QueuePush(SpiFifo, 0xFF);
301  QueuePush(SpiFifo, 0xFF);
302  QueuePush(SpiFifo, 0xFF);
303  // Alternatively, put_config_in_Queue() does all the above
304  // for me, but I wanted to spell it out the memory management
305  // issues for future me troubleshooting segfault core dumps.
306 
307  /* =====[ Operate ]===== */
308  SetSensorConfig();
309 
310  /* =====[ Test ]===== */
311  uint16_t call_n = 1;
312  SilentAssertCall(mock, call_n, "SpiSlaveTxByte");
313  _AssertArgByteVal(call_n, 1, OK);
314 }
315 void SetSensorConfig_receives_three_bytes_of_config_from_Bridge(void)
316 {
320  /* =====[ Setup ]===== */
321  // Fake some initial config values.
322  binning = BINNING_ON;
323  gain = GAIN_1X;
324  active_rows = ALL_ROWS_ACTIVE;
325  // Fake sending new valid config values by queuing them.
326  volatile uint8_t spi_rx_buffer[max_length_of_queue];
328  QueuePush(SpiFifo, BINNING_OFF);
329  QueuePush(SpiFifo, GAIN_5X);
330  QueuePush(SpiFifo, ONLY_ROW_1_ACTIVE);
331  /* =====[ Operate ]===== */
332  SetSensorConfig();
333  /* =====[ Test ]===== */
334  TEST_ASSERT_EQUAL_HEX8_MESSAGE(BINNING_OFF, binning, "binning");
335  TEST_ASSERT_EQUAL_HEX8_MESSAGE(GAIN_5X, gain, "gain");
336  TEST_ASSERT_EQUAL_HEX8_MESSAGE(ONLY_ROW_1_ACTIVE, active_rows, "active_rows");
337 }
338 /* =====[ test Setup helpers ]===== */
339 static bool _binning_is_valid(uint8_t binning)
340 {
341  return (
342  (BINNING_ON == binning) ||
343  (BINNING_OFF == binning)
344  );
345 }
346 static bool _gain_is_valid(uint8_t gain)
347 {
348  return (
349  (GAIN_1X == gain) ||
350  (GAIN_2X5 == gain) ||
351  (GAIN_4X == gain) ||
352  (GAIN_5X == gain)
353  );
354 }
355 static bool _active_rows_is_valid(uint8_t active_rows)
356 {
357  return ( (active_rows & 0xE0) == 0x00 );
358 }
359 static bool _config_is_valid(
360  uint8_t binning,
361  uint8_t gain,
362  uint8_t active_rows)
363 {
364  return (
365  (_binning_is_valid(binning)) &&
366  (_gain_is_valid(gain)) &&
367  (_active_rows_is_valid(active_rows))
368  );
369 }
370 static void _print_list_of_invalid_config_values(
371  uint8_t binning,
372  uint8_t gain,
373  uint8_t active_rows)
374 {
378  // Prepare message listing invalid bytes, if any.
379  GString *msg = g_string_new("Invalid config value in this test: ");
380  if (!_binning_is_valid(binning)) g_string_append_printf(msg, "binning ");
381  if (!_gain_is_valid(gain)) g_string_append_printf(msg, "gain ");
382  if (!_active_rows_is_valid(active_rows)) g_string_append_printf(msg, "active_rows");
383  g_string_append_printf(msg, "\n");
384  // Print INVALID bytes, if any.
385  if (!_config_is_valid(binning, gain, active_rows)) printf("%s", msg->str);
386  // Free memory used by GString
387  g_string_free(msg, true);
388 }
389 static void _print_config_values(
390  uint8_t binning,
391  uint8_t gain,
392  uint8_t active_rows)
393 {
394  printf("binning == %d, " , binning);
395  printf("gain == %d, " , gain);
396  printf("active_rows == %d\n", active_rows);
397 }
398 static void put_config_in_Queue(
399  uint8_t binning,
400  uint8_t gain,
401  uint8_t active_rows)
402 {
403  printf("\n`put_config_in_Queue`...\n");
404  // Flag `INVALID` values
405  _print_list_of_invalid_config_values(binning, gain, active_rows);
406  // List config values used in test
407  _print_config_values(binning, gain, active_rows);
408  // Put config values on the Queue.
409  volatile uint8_t spi_rx_buffer[max_length_of_queue];
411  QueuePush(SpiFifo, binning);
412  QueuePush(SpiFifo, gain);
413  QueuePush(SpiFifo, active_rows);
414 }
415 void SetSensorConfig_does_not_update_config_globals_if_config_is_invalid(void)
416 {
417  /* =====[ Setup ]===== */
418  // Fake some initial config values.
419  binning = BINNING_ON;
420  gain = GAIN_1X;
421  active_rows = ALL_ROWS_ACTIVE;
422  // Fake sending new invalid config values by queuing them.
423  put_config_in_Queue(
424  BINNING_OFF+100, // invalid
425  GAIN_5X, // valid
426  ONLY_ROW_1_ACTIVE // valid
427  );
428  /* =====[ Operate ]===== */
429  SetSensorConfig();
430  /* =====[ Test ]===== */
431  // Assert global config values did not change.
432  TEST_ASSERT_EQUAL_HEX8_MESSAGE(BINNING_ON, binning, "binning");
433  TEST_ASSERT_EQUAL_HEX8_MESSAGE(GAIN_1X, gain, "gain");
434  TEST_ASSERT_EQUAL_HEX8_MESSAGE(ALL_ROWS_ACTIVE, active_rows, "active_rows");
435 }
436 void SetSensorConfig_replies_ERROR_if_binning_is_invalid(void)
437 {
438  printf("\nTesting `SetSensorConfig_replies_ERROR_if_binning_is_invalid...`\n");
439  /* =====[ Setup ]===== */
440  // Fake sending an invalid value for binning
441  put_config_in_Queue(
442  BINNING_OFF+100, // invalid
443  GAIN_5X,
444  ONLY_ROW_1_ACTIVE
445  );
446  /* =====[ Operate ]===== */
447  SetSensorConfig();
448  /* =====[ Test ]===== */
449  uint16_t call_n = 2;
450  SilentAssertCall(mock, call_n, "SpiSlaveTxByte");
451  _AssertArgByteVal(call_n, 1, ERROR);
452 }
453 void SetSensorConfig_replies_ERROR_if_gain_is_invalid(void)
454 {
455  /* =====[ Setup ]===== */
456  printf("\nTesting `SetSensorConfig_replies_ERROR_if_gain_is_invalid`...\n");
457  // Fake sending an invalid value for gain
458  put_config_in_Queue(
459  BINNING_OFF,
460  GAIN_5X+100, // invalid
461  ONLY_ROW_1_ACTIVE
462  );
463  /* =====[ Operate ]===== */
464  SetSensorConfig();
465  /* =====[ Test ]===== */
466  uint16_t call_n = 2;
467  _AssertCall(call_n, "SpiSlaveTxByte");
468  _AssertArgByteVal(call_n, 1, ERROR);
469 }
470 void SetSensorConfig_replies_ERROR_if_active_rows_is_invalid(void)
471 {
472  printf("\nTesting `SetSensorConfig_replies_ERROR_if_active_rows_is_invalid`...\n");
473  /* =====[ Setup ]===== */
474  // Fake sending an invalid value for active_rows
475  put_config_in_Queue(
476  BINNING_OFF,
477  GAIN_5X,
478  0xFF // invalid
479  );
480  /* =====[ Operate ]===== */
481  SetSensorConfig();
482  /* =====[ Test ]===== */
483  uint16_t call_n = 2;
484  _AssertCall(call_n, "SpiSlaveTxByte");
485  _AssertArgByteVal(call_n, 1, ERROR);
486 }
487 void SetSensorConfig_does_not_program_LIS_770i_if_config_is_invalid(void)
488 {
489  /* =====[ Setup ]===== */
490  // Fake sending invalid config
491  put_config_in_Queue(
492  BINNING_OFF+100, // invalid
493  GAIN_5X+100, // invalid
494  0xFF // invalid
495  );
496  /* =====[ Operate ]===== */
497  SetSensorConfig();
498  /* =====[ Test ]===== */
499  // Assert that the function exits after reporting error.
500  uint16_t call_n = 2;
501  _AssertCall(call_n, "SpiSlaveTxByte");
502  _AssertArgByteVal(call_n, 1, ERROR);
503  _test_call_count_is(2);
504 }
505 static void _put_valid_config_in_Queue(void)
506 {
511  volatile uint8_t spi_rx_buffer[max_length_of_queue];
513  QueuePush(SpiFifo, BINNING_OFF);
514  QueuePush(SpiFifo, GAIN_5X);
515  QueuePush(SpiFifo, ONLY_ROW_1_ACTIVE);
516 }
517 void SetSensorConfig_writes_valid_config_to_LIS_770i_programmable_setup_register(void)
518 {
519  /* =====[ Setup ]===== */
520  _put_valid_config_in_Queue();
521  /* =====[ Operate ]===== */
522  SetSensorConfig();
523  /* =====[ Test ]===== */
524  PrintAllCalls(mock);
525  _AssertCall(2, "LisWriteConfig");
526 }
527 void SetSensorConfig_replies_OK_if_all_config_values_are_valid(void)
528 {
529  /* =====[ Setup ]===== */
530  _put_valid_config_in_Queue();
531  /* =====[ Operate ]===== */
532  SetSensorConfig();
533  /* =====[ Test ]===== */
534  uint16_t call_n = 3;
535  _AssertCall(call_n, "SpiSlaveTxByte");
536  _AssertArgByteVal(call_n, 1, OK);
537 }
538 void SetSensorConfig_the_OK_is_sent_after_LIS_is_programmed_with_new_config(void)
539 {
540  /* =====[ Setup ]===== */
541  _put_valid_config_in_Queue();
542  /* =====[ Operate ]===== */
543  SetSensorConfig();
544  /* =====[ Test ]===== */
545  uint16_t call_n = 2;
546  _AssertCall(call_n++, "LisWriteConfig");
547  _AssertCall(call_n, "SpiSlaveTxByte");
548  _AssertArgByteVal(call_n, 1, OK);
549 }
550 #endif
551 /* =====[ GetExposure ]===== */
552 void GetExposure_sends_OK(void)
553 {
554  /* =====[ Operate ]===== */
555  /* GetExposure(); */
556  TEST_PASS();
557 }
558 void GetExposure_sends_MSB_of_exposure_ticks(void)
559 {
560  TEST_PASS();
561 }
562 void GetExposure_sends_LSB_of_exposure_ticks(void)
563 {
564  TEST_PASS();
565 }
566 
567 /* =====[ SetExposure ]===== */
568 void SetExposure_waits_for_byte_exposure_MSB(void)
569 {
570  /* =====[ Operate ]===== */
571  /* SetExposure(); */
572  TEST_PASS();
573 }
574 void SetExposure_reads_byte_exposure_MSB(void)
575 {
576  TEST_PASS();
577 }
578 void SetExposure_waits_for_byte_exposure_LSB(void)
579 {
580  TEST_PASS();
581 }
582 void SetExposure_reads_byte_exposure_LSB(void)
583 {
584  TEST_PASS();
585 }
586 void SetExposure_updates_global_exposure_ticks(void)
587 {
588  TEST_PASS();
589 }
590 void SetExposure_sends_OK(void)
591 {
592  TEST_PASS();
593 }
594 
595 /* =====[ CaptureFrame ]===== */
596 void CaptureFrame_sends_OK(void)
597 {
598  /* =====[ Operate ]===== */
599  /* CaptureFrame(); */
600  /* =====[ Check MAX_NUM_PIXELS ]===== */
601  /* GString *msg = g_string_new(NULL); */
602  /* g_string_printf(msg, */
603  /* "MAX_NUM_PIXELS: %d", */
604  /* MAX_NUM_PIXELS */
605  /* ); */
606  /* TEST_FAIL_MESSAGE(msg->str); */
607  /* // Free string-object memory */
608  /* g_string_free(msg, true); */
609  TEST_PASS();
610 }
611 void CaptureFrame_checks_binning_to_determine_number_of_pixels_in_frame(void)
612 {
613  TEST_PASS();
614 }
615 void CaptureFrame_sends_num_pixels_MSB(void)
616 {
617  TEST_PASS();
618 }
619 void CaptureFrame_sends_num_pixels_LSB(void)
620 {
621  TEST_PASS();
622 }
623 void CaptureFrame_exposes_the_pixels(void)
624 {
625  TEST_PASS();
626 }
627 void CaptureFrame_does_readout_of_num_pixels_into_the_frame_buffer(void)
628 {
629  TEST_PASS();
630 }
631 void CaptureFrame_sends_the_pixel_readings_stored_in_the_frame_buffer(void)
632 {
633  TEST_PASS();
634 }
635 
636 static uint8_t zero_frame[2*MAX_NUM_PIXELS];
638 {
639  uint16_t MSB;
640  uint16_t LSB;
641 };
642 static struct pixel_byte_indices byte_indices_of_pixel(uint16_t pixel_number)
643 {
644  // ---pixel number is byte_index/2 + 1---
645  // byte_index ──┬──────────────┐
646  // byte: 0,1 | pixel 1 -> (0/2)+1 = 0+1, (1/2)+1 = 0+1
647  // byte: 2,3 | pixel 2 -> (2/2)+1 = 1+1, (3/2)+1 = 1+1
648  // byte: 4,5 | pixel 3 -> (4/2)+1 = 2+1, (5/2)+1 = 2+1
649  // byte: 6,7 | pixel 4 -> (6/2)+1 = 3+1, (7/2)+1 = 3+1
650  // ...
651  // byte: 510 | pixel 256 -> (510/2)+1 = 256 (MSB)
652  // byte: 511 | pixel 256 -> (511/2)+1 = 256 (LSB)
653  // byte: 512 | pixel 257 -> (512/2)+1 = 257 (MSB)
654  // ...
655  // byte: 1022 | pixel 512 -> (1022/2)+1 = 512 (MSB)
656  // byte: 1023 | pixel 512 -> (1023/2)+1 = 512 (LSB)
657  /* return (pixel_number-1)*2; // MSB */
658  struct pixel_byte_indices pixel;
659  pixel.MSB = (pixel_number-1)*2; // frame index of MSB
660  pixel.LSB = 1 + pixel.MSB; // frame index of LSB
661  return pixel;
662 }
663 
664 // GetPeak(start_pixel, stop_pixel) -> peak_counts
665 void GetPeak_finds_the_peak_between_start_pixel_and_stop_pixel_inclusive(void)
666 {
667  /* =====[ Setup ]===== */
668  uint16_t _start_pixel = 1;
669  uint16_t _stop_pixel = MAX_NUM_PIXELS;
670  // Empty the global frame buffer.
671  const uint16_t sizeof_frame = 2*MAX_NUM_PIXELS;
672  for (uint16_t byte_index=0; byte_index < sizeof_frame; byte_index++)
673  {
674  frame[byte_index] = 0;
675  }
676  // Assert that it's empty before doing the actual test
677  TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE(
678  zero_frame, // expected
679  frame, // actual
680  sizeof_frame, // num_elements
681  "Cannot run test: setup failed to empty the frame."
682  );
683  // Test 0: Check I can get the byte indices for a given pixel number.
684  struct pixel_byte_indices pixel256 = byte_indices_of_pixel(256);
685  //
686  //
687  TEST_ASSERT_EQUAL_UINT16_MESSAGE(
688  510, pixel256.MSB,
689  "Cannot run test: byte_indices_of_pixel().MSB does not do what you think!"
690  );
691  TEST_ASSERT_EQUAL_UINT16_MESSAGE(
692  511, pixel256.LSB,
693  "Cannot run test: byte_indices_of_pixel().LSB does not do what you think!"
694  );
695  /* =====[ Test and Operate ]===== */
696  // Test 1: Put a peak somehwere in the middle.
697  struct pixel_byte_indices pixel123 = byte_indices_of_pixel(123);
698  frame[pixel123.MSB] = 0xab;
699  frame[pixel123.LSB] = 0xcd;
700  TEST_ASSERT_EQUAL_UINT16(0xabcd, GetPeak(_start_pixel,_stop_pixel));
701  // Test 2: Put a peak at the start pixel
702  struct pixel_byte_indices pixelstart = byte_indices_of_pixel(_start_pixel);
703  frame[pixelstart.MSB] = 0xab;
704  frame[pixelstart.LSB] = 1 + 0xcd;
705  TEST_ASSERT_EQUAL_UINT16(1 + 0xabcd, GetPeak(_start_pixel,_stop_pixel));
706  // Test 2: Put a peak at the stop pixel
707  struct pixel_byte_indices pixelstop = byte_indices_of_pixel(_stop_pixel);
708  frame[pixelstop.MSB] = 0xab;
709  frame[pixelstop.LSB] = 2 + 0xcd;
710  TEST_ASSERT_EQUAL_UINT16(2 + 0xabcd, GetPeak(_start_pixel,_stop_pixel));
711 }
712 void GetPeak_ignores_peaks_at_pixels_before_start_pixel_and_after_stop_pixel(void)
713 {
714  /* =====[ Setup ]===== */
715  // Put pixels 1 and MAX_NUM_PIXELS out of range
716  uint16_t _start_pixel = 2;
717  uint16_t _stop_pixel = MAX_NUM_PIXELS-1;
718  // Empty the global frame buffer.
719  const uint16_t sizeof_frame = 2*MAX_NUM_PIXELS;
720  for (uint16_t byte_index=0; byte_index < sizeof_frame; byte_index++)
721  {
722  frame[byte_index] = 0;
723  }
724  // Test 1: Put a peak at pixel 1
725  struct pixel_byte_indices pixel1 = byte_indices_of_pixel(1);
726  frame[pixel1.MSB] = 0x12;
727  frame[pixel1.LSB] = 0x34;
728  TEST_ASSERT_EQUAL_UINT16_MESSAGE(0, GetPeak(_start_pixel,_stop_pixel),
729  "Peak detected at pixel before start pixel."
730  );
731  // Test 2: Put a peak at pixel MAX_NUM_PIXELS
732  frame[pixel1.MSB] = 0;
733  frame[pixel1.LSB] = 0;
734  struct pixel_byte_indices pixelMAX = byte_indices_of_pixel(MAX_NUM_PIXELS);
735  frame[pixelMAX.MSB] = 0x12;
736  frame[pixelMAX.LSB] = 0x35;
737  TEST_ASSERT_EQUAL_UINT16_MESSAGE(0, GetPeak(_start_pixel,_stop_pixel),
738  "Peak detected at pixel after stop pixel."
739  );
740 }
741 
742 void GetSensorHash_hash_sends_OK(void)
743 {
744  /* =====[ Operate ]===== */
745  GetSensorHash();
746  /* =====[ Test ]===== */
747  uint8_t call_n = 1; uint8_t arg_n = 1; uint8_t byte = OK;
748  _AssertCall(call_n, "SpiSlaveTxByte");
749  _AssertArg(call_n, arg_n, &byte);
750 }
751 void GetSensorHash_sends_first_three_bytes_of_SHA1_hash_of_sensor(void)
752 {
753 #ifdef LIS
754  printf("This is the LIS hash: 0x%x\n", LIS);
755  /* =====[ Operate ]===== */
756  GetSensorHash();
757  /* =====[ Test ]===== */
758  uint8_t arg_n = 1;
759  uint8_t first_byte = 0x35;
760  uint8_t second_byte = 0x1e;
761  uint8_t third_byte = 0xa9;
762  uint8_t call_n = 2;
763  _AssertCall(call_n, "SpiSlaveTxByte");
764  _AssertArg(call_n++, arg_n, &first_byte);
765  _AssertCall(call_n, "SpiSlaveTxByte");
766  _AssertArg(call_n++, arg_n, &second_byte);
767  _AssertCall(call_n, "SpiSlaveTxByte");
768  _AssertArg(call_n, arg_n, &third_byte);
769 #endif
770 #ifdef S13131
771  printf("This is the S13131 hash: 0x%x\n", S13131);
772  /* =====[ Operate ]===== */
773  GetSensorHash();
774  /* =====[ Test ]===== */
775  uint8_t arg_n = 1;
776  uint8_t first_byte = 0x91;
777  uint8_t second_byte = 0xd3;
778  uint8_t third_byte = 0x18;
779  uint8_t call_n = 2;
780  _AssertCall(call_n, "SpiSlaveTxByte");
781  _AssertArg(call_n++, arg_n, &first_byte);
782  _AssertCall(call_n, "SpiSlaveTxByte");
783  _AssertArg(call_n++, arg_n, &second_byte);
784  _AssertCall(call_n, "SpiSlaveTxByte");
785  _AssertArg(call_n, arg_n, &third_byte);
786 #endif
787 }
788 
789 
#define MAX_NUM_PIXELS
LIS-770i maximum number of pixels.
Definition: Lis.h:400
void QueuePush(volatile Queue_s *SpiFifo, uint8_t data_to_push)
Definition: Queue.h:122
volatile Queue_s * QueueInit(volatile uint8_t *buffer, uint16_t const buffer_size_in_bytes)
Definition: Queue.h:51
void SetSensorConfig(void)
Definition: UsbCmd.h:307
uint8_t frame[]
One frame of pixel data is, at most, 1568 bytes.
Definition: VisCmd.c:2
uint16_t GetPeak(uint16_t const, uint16_t const)
Definition: VisCmd.c:29
bool LedNumIsValid(bicolorled_num led_num)
Definition: VisCmd.h:63
Queue uses a byte array as a circular buffer.
Definition: Queue.h:27
#define max_length_of_queue
Maximum size of the Queue's FIFO buffer is 24 bytes.
Definition: vis-spi-out.c:29
volatile uint8_t spi_rx_buffer[max_length_of_queue]
Allocate static memory for the Queue's FIFO buffer.
Definition: vis-spi-out.c:31