updated barcode & line_sensor_config.h

This commit is contained in:
soapysoltionss 2023-11-21 16:57:32 +08:00
parent ac9a3ccc7e
commit 1ff20ddcf3
4 changed files with 75 additions and 40 deletions

View File

@ -1,6 +1,7 @@
/* Barcode sensor */ /* Barcode sensor */
#include "barcode_sensor_init.h" #include "barcode_sensor_init.h"
#include <string.h>
#define MAX_BARCODES 10 // Define the maximum number of barcodes to store #define MAX_BARCODES 10 // Define the maximum number of barcodes to store
#define BARCODE_SENSOR_TIMER_PERIOD_MS 100 // Define the barcode sensor timer period #define BARCODE_SENSOR_TIMER_PERIOD_MS 100 // Define the barcode sensor timer period
@ -8,6 +9,9 @@
// Define the barcode sensor timer // Define the barcode sensor timer
static struct repeating_timer barcode_sensor_timer; static struct repeating_timer barcode_sensor_timer;
// Define the barcode data array
static uint32_t barcode_data_array[MAX_BARCODES];
static int barcode_data_index = 0;
/** /**
* @brief Decode a Code 39 barcode * @brief Decode a Code 39 barcode
@ -15,9 +19,9 @@ static struct repeating_timer barcode_sensor_timer;
* This function decodes a Code 39 barcode represented as a 9-bit binary number. * This function decodes a Code 39 barcode represented as a 9-bit binary number.
* *
* @param barcode_data Binary representation of the barcode data (9 bits) * @param barcode_data Binary representation of the barcode data (9 bits)
* @return Decoded value as an integer * @return Decoded value as a character
*/ */
int code39_decode(uint32_t barcode_data) { char code39_decode(uint32_t barcode_data) {
// Define the binary representations of Code 39 characters // Define the binary representations of Code 39 characters
const uint32_t code39_characters[] = { const uint32_t code39_characters[] = {
0b001001001, // 0 0b001001001, // 0
@ -30,18 +34,50 @@ int code39_decode(uint32_t barcode_data) {
0b001010011, // 7 0b001010011, // 7
0b001011101, // 8 0b001011101, // 8
0b001111001, // 9 0b001111001, // 9
// Add more character representations as needed 0b001010101, // A
0b001010111, // B
0b001011101, // C
0b001101101, // D
0b001110101, // E
0b001110111, // F
0b001111101, // G
0b001101011, // H
0b001011011, // I
0b001010011, // J
0b001101111, // K
0b001111011, // L
0b001110011, // M
0b001110001, // N
0b001101001, // O
0b001011001, // P
0b001010001, // Q
0b001001101, // R
0b001001111, // S
0b001001011, // T
0b001111011, // U
0b001111101, // V
0b001110101, // W
0b001010111, // X
0b001011111, // Y
0b001101101, // Z
0b001110011, // -
0b001011001, // .
0b001101111, // space
0b001111111, // $
0b001010011, // /
0b001010001, // +
0b001001001, // %
}; };
// Compare the barcode data to known Code 39 character representations // Compare the barcode data to known Code 39 character representations
for (int i = 0; i < 10; i++) { for (int i = 0; i < 44; i++) {
if (barcode_data == code39_characters[i]) { if (barcode_data == code39_characters[i]) {
return i; // Return the decoded value (0-9) return (char)(i + 48); // Return the decoded value as a character
} }
} }
// If the barcode data does not match any known character, return -1 to indicate an error // If the barcode data does not match any known character, return '?' to indicate an error
return -1; return '?';
} }
@ -61,7 +97,6 @@ void monitor_barcode_sensor_task(void *params) {
if (xSemaphoreTake(g_barcode_sensor_sem, portMAX_DELAY) == pdTRUE) { if (xSemaphoreTake(g_barcode_sensor_sem, portMAX_DELAY) == pdTRUE) {
// Check the flag or receive the message // Check the flag or receive the message
if (barcode_sensor_triggered == pdTRUE) { if (barcode_sensor_triggered == pdTRUE) {
uint32_t barcode_data = 0;
int bar_width = 0; // Variable to store the width of the current bar int bar_width = 0; // Variable to store the width of the current bar
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
@ -78,23 +113,39 @@ void monitor_barcode_sensor_task(void *params) {
bar_width = 0; // Reset the bar width measurement bar_width = 0; // Reset the bar width measurement
} }
barcode_data |= (1u << i); barcode_data_array[barcode_data_index] |= (1u << i);
} }
} }
printf("Barcode Data (binary): %09b\n", barcode_data); printf("Barcode Data (binary): %09b\n", barcode_data_array[barcode_data_index]);
barcode_data_index++;
if (barcode_data_index >= MAX_BARCODES) {
// Decode the barcode data // Decode the barcode data
int decoded_value = code39_decode(barcode_data); for (int i = 0; i < MAX_BARCODES; i++) {
int ones_count = 0;
uint32_t barcode_data = barcode_data_array[i];
if (decoded_value != -1) { // Count the number of ones in the barcode data
printf("Decoded Value: %d\n", decoded_value); for (int j = 0; j < 9; j++) {
if ((barcode_data >> j) & 1) {
ones_count++;
}
}
char decoded_value = code39_decode(barcode_data);
printf("Decoded Value: %c\n", decoded_value);
// Store or process the decoded value as needed // Store or process the decoded value as needed
// Send the decoded value instead of the raw barcode data // Send the decoded value instead of the raw barcode data
xMessageBufferSend(barcode_sensor_msg_buffer, &decoded_value, sizeof(int), 0); xMessageBufferSend(barcode_sensor_msg_buffer, &decoded_value, sizeof(char), 0);
} else { }
printf("Error: Unable to decode the barcode.\n");
// Reset the barcode data array and index
memset(barcode_data_array, 0, sizeof(barcode_data_array));
barcode_data_index = 0;
} }
// Reset the flag // Reset the flag
@ -103,22 +154,3 @@ void monitor_barcode_sensor_task(void *params) {
} }
} }
} }
/**
* @brief Monitor the barcode sensor
* @param params
*/
void
monitor_barcode_task(__unused void *params) {
state_t barcode_state;
// Receive from Buffer
xMessageBufferReceive(barcode_sensor_msg_buffer,
&barcode_state,
sizeof(state_t),
portMAX_DELAY);
}

View File

@ -2,6 +2,7 @@
#ifndef BARCODE_SENSOR_INIT_H #ifndef BARCODE_SENSOR_INIT_H
#define BARCODE_SENSOR_INIT_H #define BARCODE_SENSOR_INIT_H
#define DEBOUNCE_DELAY_MS 100
#include <stdio.h> #include <stdio.h>
#include "pico/stdlib.h" #include "pico/stdlib.h"
@ -13,9 +14,10 @@
#include "message_buffer.h" #include "message_buffer.h"
#include "semphr.h" #include "semphr.h"
#include "barcode_sensor_config.h" #include "line_sensor_config.h"
#include "line_sensor_init.h" #include "line_sensor_init.h"
// Set barcode time to 0 // Set barcode time to 0
static TickType_t lastBarcodeTime = 0; static TickType_t lastBarcodeTime = 0;

View File

@ -40,7 +40,7 @@ main (void)
printf("Test started!\n"); printf("Test started!\n");
barcode_sensor_setup(); barcode_sensor_setup();
initialize_car_state(); // initialize_car_state();
launch(); launch();

View File

@ -7,5 +7,6 @@
#define LEFT_SENSOR_PIN ( 26 ) #define LEFT_SENSOR_PIN ( 26 )
#define RIGHT_SENSOR_PIN ( 27 ) #define RIGHT_SENSOR_PIN ( 27 )
#define BARCODE_SENSOR_PIN ( 22 )
#endif //CONFIG_H #endif //CONFIG_H