-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart_cart_final.cpp
203 lines (181 loc) · 5.85 KB
/
smart_cart_final.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <qrcode.h>
//TFT Pins Definintion
#define TFT_CS 10
#define TFT_RST 8 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC
//Instance of adafruit st7735 class for controlling the tft display
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
//Instance of Software serial class for serial communication with EM-18 reader
SoftwareSerial mySerial(2, 3); // RX, TX
//Buttons for add/remove item and qr code generator
const int button_pin1 = 4;
const int button_pin2 = 5;
// specific id for each items
#define rice_id 1
#define sugar_id 2
#define coffee_id 3
//assigning different rfid uid numbers to each items
String rice_tag = "270012EC2DF4";
String sugar_tag = "5900D4EC6A0B";
String coffee_tag = "5900D4D56830";
//array for item list,quantites and price
String item_array[4] = { "", "Rice", "Sugar", "Coffee" };//element at 0 index is given as empty string so that the counting starts from 1 intead of 0 which is more natural to people
int quantity_array[4] = { 0, 0, 0, 0 };
int price_array[4] = { 0, 100, 50, 150 };
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
int rxcount = 0; //recieved item's count
int selected_item_id = 0;
int total_price = 0;
void setup() {
Serial.begin(9600); // Initialize serial communication
mySerial.begin(9600);
pinMode(button_pin1, INPUT_PULLUP);
pinMode(button_pin2, INPUT_PULLUP);
inputString.reserve(200); //reserving 200 bits of space in the memory for the inputString value
/
init_display();
delay(5000);
display_add_item();
delay(5000);
display_item_list_heading();
delay(5000);
//update_display_item_list();
Serial.println("Started..");
//while (1)
// ;
}
void loop() {
myserialEvent();
if (stringComplete) {
Serial.println(inputString);
if (inputString == rice_tag) {
selected_item_id = rice_id;
} else if (inputString == sugar_tag) {
selected_item_id = sugar_id;
} else if (inputString == coffee_tag) {
selected_item_id = coffee_id;
} else {
selected_item_id = 0;
}
// clear the string:
inputString = "";
stringComplete = false;
rxcount = 0;
}
if (selected_item_id > 0) {
Serial.println(item_array[selected_item_id]);
if (digitalRead(button_pin1) == 0) {
if (quantity_array[selected_item_id] > 0) {
quantity_array[selected_item_id]--;
}
} else {
quantity_array[selected_item_id]++;
}
update_display_item_list();
selected_item_id = 0;
}
if (digitalRead(button_pin2) == 0) {
if (total_price > 0) {
generate_qrcode();
while(1);
}
}
}
void generate_qrcode() {
tft.fillScreen(ST7735_BLACK);
String qrText = "You have been purchased for the amount of \n";
qrText += "\n\t\tRs.";
qrText += String(total_price);
qrText += ".\n";
qrText += "\n Please pay the amount.\n";
QRCode qrcode;
uint8_t qrcodeData[qrcode_getBufferSize(5)];
qrcode_initText(&qrcode, qrcodeData, 5, 0, qrText.c_str());
for (int8_t y = 0; y < qrcode.size; y++) {
for (int8_t x = 0; x < qrcode.size; x++) {
if (qrcode_getModule(&qrcode, x, y)) {
tft.fillRect(x * 3, y * 3, 3, 3, ST7735_WHITE);
}
}
}
}
void myserialEvent() {
while (mySerial.available()) {
// get the new byte:
char inChar = (char)mySerial.read();
//Serial.print(inChar);
inputString += inChar;
rxcount++;
if (inChar == '\n' || rxcount >= 12) {
stringComplete = true;
}
}
}
void init_display() {
tft.initR(INITR_BLACKTAB); // Initialize TFT display
tft.setRotation(1); // Rotate the display if needed
tft.fillScreen(ST7735_BLACK); // Fill the screen with black color
// Initialize TFT display with introductory text
int x = 30, y = 15, width = 105, height = 100, border = 2;
tft.drawRect(x, y, width, height, ST7735_WHITE); // Draw the outer rectangle
tft.fillRect(x + border, y + border, width - 2 * border, height - 2 * border, ST7735_BLACK); // Draw the inner rectangle for the border
tft.setTextSize(3);
tft.setCursor(41, 30);
tft.println("SMART");
tft.setCursor(36, 60);
tft.setTextSize(2.5);
tft.println("SHOPPING");
tft.setCursor(50, 80);
tft.setTextSize(3);
tft.println("CART");
}
void display_add_item() {
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 20);
tft.setTextSize(2.5);
tft.println("Please add");
tft.println("item..");
}
void display_item_list_heading() {
tft.fillScreen(ST7735_BLACK);
tft.setTextSize(1);
int x_loc = 20;
int y_loc = 10;
tft.setCursor(x_loc, y_loc);
tft.println("Items");
tft.setCursor(x_loc * 3, y_loc);
tft.println("Qty");
tft.setCursor(x_loc * 5, y_loc);
tft.println("Price");
tft.drawFastHLine(0, 22, tft.width(), ST7735_WHITE);
}
void update_display_item_list() {
//tft.fillScreen(ST7735_BLACK);
int x_loc = 20;
int y_loc = 25;
tft.fillRect(x_loc, y_loc, x_loc * 6, y_loc * 5, ST7735_BLACK);
tft.setTextSize(1);
total_price = 0;
for (int i = 0; i <= 3; i++) {
if (quantity_array[i] > 0) {
tft.setCursor(x_loc, y_loc * i);
tft.println(item_array[i]);
tft.setCursor(x_loc * 3.5, y_loc * i);
tft.println(String(quantity_array[i]));
tft.setCursor(x_loc * 5, y_loc * i);
tft.println(String(quantity_array[i] * price_array[i]));
total_price += (quantity_array[i] * price_array[i]);
}
}
tft.setTextSize(1.5);
tft.setCursor(x_loc, y_loc * 4);
tft.println("Total Price: ");
tft.setCursor(x_loc * 5, y_loc * 4);
tft.println(String(total_price));
}