કોડ શોધો

મેકરફેબ્સ MaTouch ESP32-S3 2.8" કેમેરા ESP32-S3 AI વિઝન: કેમેરા પોઈન્ટ કરો અને પૂછો કે તે શું જુએ છે

મેકરફેબ્સ MaTouch ESP32-S3 2.8" કેમેરા ESP32-S3 AI વિઝન: કેમેરા પોઈન્ટ કરો અને પૂછો કે તે શું જુએ છે

બોર્ડ ફોટો લે છે, AI તે શું જુએ છે તેનું વર્ણન કરે છે, અને તે જવાબ મોટેથી બોલે છે

કૅમેરાને કોઈ વસ્તુ તરફ દોરો, એક બટન દબાવો, અને AI તમને કહે છે કે તે શું જોઈ રહ્યું છે - સ્ક્રીન પર છપાયેલું અને સ્પીકર દ્વારા બોલાયેલું. બે મોડ: દૃશ્યમાંની વસ્તુઓનું નામ આપો, અથવા કૅમેરાની સામેની વ્યક્તિનું વર્ણન કરો.

AI Vision - Point It At Something running on the MaTouch AI ESP32-S3 board AI વિઝન - MaTouch AI ESP32-S3 બોર્ડ પર ચાલતું પોઈન્ટ ઈટ એટ સમથિંગ

ચિત્ર ત્યાં કેવી રીતે પહોંચે છે

કૅમેરો લગભગ 20-40 KB નું 800×600 JPEG બનાવે છે. બોર્ડ તેને PSRAM માં base64-એન્કોડ કરે છે, જે તેને ટેક્સ્ટ તરીકે લગભગ ત્રીજા ભાગનું મોટું બનાવે છે, અને તેને ડેટા URI તરીકે વિનંતીમાં એમ્બેડ કરે છે. 8 MB નું PSRAM જ આને આરામદાયક બનાવે છે.

વર્ણન, ક્યારેય ઓળખ નહીં

PERSON મોડ તે શું જોઈ શકે છે તેનું વર્ણન કરે છે - મૂડ, ચશ્મા, કોઈ વ્યક્તિ શું કરી રહી હોય તેવું લાગે છે. તે તમને કહેશે નહીં કે કોઈ કોણ છે, અને તે ઇરાદાપૂર્વક છે. ચહેરા દ્વારા લોકોને ઓળખવું એ OpenAI ની ઉપયોગ નીતિઓની વિરુદ્ધ છે, અને Microsoft ની સમકક્ષ ચહેરા-ઓળખ સેવા એક મંજૂરી પ્રક્રિયા પાછળ બંધ છે જેમાં સામાન્ય વિકાસકર્તાઓ સરળતાથી નોંધણી કરી શકતા નથી. કોઈપણ ટ્યુટોરિયલ જે તમને ક્લાઉડ ચહેરા ઓળખનું વચન આપે છે તે એવી વસ્તુનું વર્ણન કરે છે જે સામાન્ય રીતે ઉપલબ્ધ નથી.

જો તમને ચોક્કસ લોકોને ઓળખતું બોર્ડ જોઈએ છે, તો પ્રોજેક્ટ 03b નો ઉપયોગ કરો - તે તે ઑફલાઇન કરે છે અને તે કોઈનો ચહેરો ક્યાંય મોકલતું નથી.

નેટવર્ક માટે કૅમેરો બંધ થવો પડે છે

પ્રશ્ન પૂછવામાં આવે ત્યારે લાઇવ વ્યૂફાઇન્ડર બંધ કરવામાં આવે છે. આ સૌંદર્યલક્ષી નથી: ચાલતો કૅમેરો ડ્રાઇવર આંતરિક મેમરી ધરાવે છે જે સુરક્ષિત કનેક્શનને જોઈએ છે, અને પ્રીવ્યૂ ચાલુ હોય ત્યારે કનેક્શન ખાલી નિષ્ફળ જાય છે. કૅમેરો બંધ કરવાથી તે મુક્ત થાય છે. તેનો અર્થ એ પણ છે કે જવાબ વાંચી શકાય તેવો રહે છે, સેકન્ડમાં 25 વખત ફરીથી રંગાવવાને બદલે.

જવાબ તમે તેને કાઢી નાખો ત્યાં સુધી રહે છે

જવાબ સ્ક્રીન પર ત્યાં સુધી રહે છે જ્યાં સુધી તમે CLEAR દબાવો નહીં - ત્યાં કોઈ ટાઈમર નથી. તે દર્શાવતી વખતે, એક REPLAY બટન બોલાયેલા જવાબને મેમરીમાંથી ફરીથી વગાડે છે, બીજા API કૉલ અને વધારાના ખર્ચ વિના.

MaTouch AI ESP32-S3 2.8" બોર્ડ વિશે

આ પૃષ્ઠ પરનો દરેક પ્રોજેક્ટ Makerfabs ના MaTouch AI ESP32-S3 2.8" TFT ST7789V પર ચાલે છે. તે એક ઓલ-ઇન-વન બોર્ડ છે: એક રંગીન ટચસ્ક્રીન, એક 3 મેગાપિક્સલ કૅમેરો, બે માઇક્રોફોન અને એક વાસ્તવિક સ્પીકર એમ્પ્લીફાયર, બધા 8 MB PSRAM સાથે ESP32-S3 દ્વારા સંચાલિત. આ સંયોજન જ આ AI પ્રોજેક્ટ્સને બીજું કંઈ જોડ્યા વિના એક જ બોર્ડ પર શક્ય બનાવે છે.

8 MB નું PSRAM અહીં કોઈપણ અન્ય સંખ્યા કરતાં વધુ મહત્વપૂર્ણ છે. તે જ બોર્ડને કૅમેરા ફ્રેમ, થોડી સેકંડની રેકોર્ડ કરેલી ઑડિઓ, અથવા base64-એન્કોડ કરેલો ફોટો એક સાથે મેમરીમાં રાખવા દે છે - જેમાંથી કંઈ પણ ESP32 ની સામાન્ય RAM માં ફિટ થતું નથી.

ઉત્પાદક દસ્તાવેજીકરણ: Makerfabs વિકિ પૃષ્ઠ.

મુખ્ય વિશિષ્ટતાઓ

  • પ્રોસેસર: ESP32-S3, ડ્યુઅલ કોર 240 MHz, WiFi 2.4 GHz + બ્લૂટૂથ 5.0

  • મેમરી: 16 MB ફ્લેશ, 8 MB PSRAM (અહીં લગભગ દરેક પ્રોજેક્ટ માટે જરૂરી)

  • ડિસ્પ્લે: 2.8" IPS, 320×240, ST7789V ડ્રાઇવર, SPI

  • ટચ: GT911 કેપેસિટિવ, એક સાથે 5 આંગળીઓ ટ્રેક કરે છે

  • કૅમેરો: OV3660, 3 મેગાપિક્સલ, 2048×1536 સુધી

  • માઇક્રોફોન: બે INMP441 I2S ડિજિટલ માઇક (એક સાચી સ્ટીરિયો જોડી)

  • સ્પીકર: MAX98357A ક્લાસ-ડી એમ્પ્લીફાયર, 4 Ω માં 3.2 W

  • સ્ટોરેજ: microSD કાર્ડ સ્લોટ (SPI મોડ)

  • પાવર: USB-C, JST બેટરી કનેક્ટર, TP4056 ચાર્જર, પાવર સ્વિચ

  • બોર્ડ પર પણ: WS2812B RGB LED, PCF8563T બેટરી-બેકઅપ રીઅલ-ટાઇમ ક્લોક, અને MAX17048 બેટરી ફ્યુઅલ ગેજ જે સત્તાવાર વિશિષ્ટતાઓમાં સૂચિબદ્ધ નથી

બે USB-C પોર્ટ સમાન નથી. બોર્ડનો સ્પીકર તેના સિગ્નલ પિન (IO19 અને IO20) મૂળ USB પોર્ટ સાથે શેર કરે છે, કારણ કે તે પિન ESP32-S3 ની હાર્ડવાયર્ડ USB ડેટા લાઇન છે. હંમેશા CH340K USB-C પોર્ટ (RESET બટનની બાજુનો) દ્વારા અપલોડ અને પાવર કરો, અને USB CDC On Boot ને Disabled પર સેટ કરો. ખોટા પોર્ટનો ઉપયોગ કરો અને ઑડિઓ ખોટું વર્તન કરશે અથવા અપલોડ નિષ્ફળ જશે.

Arduino IDE સેટિંગ્સ

આ સેટિંગ્સ મહત્વપૂર્ણ છે. આ બોર્ડ સાથે લોકો જે મોટાભાગની સમસ્યાઓ નોંધાવે છે તેમાંથી એક આમાંથી એક ખોટું હોવું છે, અને તે કોર વર્ઝન બદલતી વખતે રીસેટ થાય છે, તેથી કોઈપણ ફેરફાર પછી તેને ફરીથી તપાસો.

સેટિંગ

મૂલ્ય

બોર્ડ

ESP32S3 ડેવ મોડ્યુલ

ESP32 કોર વર્ઝન

2.0.17

PSRAM

OPI PSRAM

ફ્લેશ સાઇઝ

16MB (128Mb)

પાર્ટીશન સ્કીમ

16M ફ્લેશ (3MB APP/9.9MB FATFS)

USB CDC On Boot

Disabled

અપલોડ સ્પીડ

921600

અપલોડ પહેલાં બધી ફ્લેશ ભૂંસી નાખો

Disabled

પોર્ટ

CH340K USB-C પોર્ટ

ESP32 કોર 2.0.17 નો ઉપયોગ કરો, 3.x નહીં. Espressif એ કોર 3 માં ડિવાઇસ પરના ચહેરા શોધ મોડેલો દૂર કર્યા છે, તેથી ચહેરાના પ્રોજેક્ટ્સ ત્યાં કમ્પાઇલ થશે નહીં. 2.0.17 પિન કરવાથી આ પૃષ્ઠ પરનો દરેક પ્રોજેક્ટ એક રૂપરેખાંકન સાથે કાર્યરત રહે છે. બોર્ડ્સ મેનેજરમાં, વર્ઝન ડ્રોપડાઉન તમને જ્યારે ઇચ્છો ત્યારે આગળ અને પાછળ સ્વિચ કરવા દે છે.

Arduino માટે GFX લાઇબ્રેરી આવૃત્તિ 1.5.6 વાપરો, 1.6.x નહીં. 1.6 રિલીઝ ESP32 કોર 3 માટે બનાવવામાં આવી છે અને કોર 2.0.17 પર શરૂઆતમાં અટકી શકે છે. જો અપલોડ કર્યા પછી તમારી સ્ક્રીન કાળી રહે છે, તો આ પહેલી વસ્તુ છે જે તપાસવી જોઈએ.

જરૂરી લાઇબ્રેરીઓ

આને Arduino IDE માં Tools → Manage Libraries દ્વારા ઇન્સ્ટોલ કરો. આવૃત્તિ નંબરો મહત્વપૂર્ણ છે - કૃપા કરીને સૂચિબદ્ધ કરેલા વાપરો.

લાઇબ્રેરી

આવૃત્તિ

લેખક

Arduino માટે GFX લાઇબ્રેરી

1.5.6

moononournation

bb_captouch

1.3.1

Larry Bank

ArduinoJson

7.x

Benoit Blanchon

Adafruit NeoPixel

કોઈપણ તાજેતરની

Adafruit

secrets.h સેટ કરવું

તમારી WiFi વિગતો અને કોઈપણ API કી secrets.h માં જાય છે, જે પ્લેસહોલ્ડર મૂલ્યો સાથે ડાઉનલોડમાં શામેલ છે. Arduino IDE માં તે ટેબ ખોલો અને તેમને તમારી પોતાની વિગતોથી બદલો.

WiFi 2.4 GHz હોવું જોઈએ. ESP32-S3 5 GHz નેટવર્ક જોઈ શકતું નથી. જો તમારું રાઉટર બંને બેન્ડને એક નામ હેઠળ જોડે છે (Asus આને Smart Connect કહે છે), તો કાં તો તે બંધ કરો અથવા 2.4 GHz બેન્ડને તેનું પોતાનું નામ આપો અને તેને secrets.h માં વાપરો.

તમારી API કી મેળવવી

આ પ્રોજેક્ટ ક્લાઉડ AI સેવા સાથે વાત કરે છે, તેથી તમારે તમારી પોતાની કીની જરૂર છે. જો તમે આ પહેલાં ક્યારેય ન કર્યું હોય, તો ચિંતા કરશો નહીં - તે પાસવર્ડ જેવું જ છે જે સેવાને તમારું એકાઉન્ટ ઓળખાવે છે. તે થોડી મિનિટો લે છે, એક વાર.

કી એ વેબસાઇટનું સબ્સ્ક્રિપ્શન નથી. ઉદાહરણ તરીકે, ChatGPT Plus માટે ચૂકવણી કરવાથી તમને API કી નથી મળતી - આ બંને અલગ ઉત્પાદનો છે જેમનું બિલિંગ અલગ છે. તમારે ડેવલપર પ્લેટફોર્મ પર એકાઉન્ટની જરૂર છે, જે નીચે વર્ણવેલ છે.

OpenAI - આંખો

OpenAI વિઝન મોડેલ પ્રદાન કરે છે જે ચિત્રો જુએ છે. ફક્ત એવા પ્રોજેક્ટ્સ કે જે છબી મોકલે છે તેમને આની જરૂર છે.

  1. platform.openai.com/api-keys પર જાઓ અને સાઇન ઇન કરો અથવા એકાઉન્ટ બનાવો.

  2. Create new secret key પર ક્લિક કરો, તેને નામ આપો અને બનાવો.

  3. તે તરત જ કૉપિ કરો - DeepSeek ની જેમ, તે ફક્ત એક જ વાર બતાવવામાં આવે છે.

  4. Billing ખોલો અને થોડી ક્રેડિટ ઉમેરો. API પ્રીપેડ છે અને તમારી પાસે પહેલેથી હોય તેવા કોઈપણ ChatGPT સબ્સ્ક્રિપ્શનથી અલગ છે.

કીને secrets.h માં OPENAI_KEY તરીકે મૂકો.

Microsoft Azure Speech - સાંભળવા અને બોલવા માટે

Azure તમારી વાણીને ટેક્સ્ટમાં ફેરવે છે અને જવાબને ફરીથી અવાજમાં ફેરવે છે. મફત સ્તર આ પૃષ્ઠ પરની દરેક વસ્તુ માટે પૂરતું ઉદાર છે.

  1. portal.azure.com પર જાઓ અને Microsoft એકાઉન્ટથી સાઇન ઇન કરો (મફત એકાઉન્ટ પૂરતું છે).

  2. જો તમે પહેલાં Azure વાપર્યું નથી, તો તમને Welcome to Azure સ્ક્રીન દેખાશે જે ત્રણ વિકલ્પો આપે છે. Start with an Azure free trial પસંદ કરો - Azure તમને કંઈપણ બનાવવા દે તે પહેલાં તમારે સબ્સ્ક્રિપ્શનની જરૂર છે. (વિદ્યાર્થીઓએ તેના બદલે Azure for Students પસંદ કરવું જોઈએ: સમાન પરિણામ, કાર્ડની જરૂર નથી.) Manage Microsoft Entra ID ને અવગણો, જે સંપૂર્ણપણે કંઈક બીજું છે.

  3. Create a resource પર ક્લિક કરો, Speech શોધો અને Microsoft દ્વારા પ્રકાશિત Speech service પસંદ કરો.

  4. ફોર્મ ભરો: કોઈપણ રિસોર્સ ગ્રૂપ, કોઈપણ નામ, અને તમારી નજીકનો Region પસંદ કરો - તે પ્રદેશને બરાબર લખો જેમ તે દેખાય છે, ઉદાહરણ તરીકે eastus.

  5. Pricing tier માટે F0 (Free) પસંદ કરો. તે દર મહિને લગભગ પાંચ કલાક સ્પીચ-ટુ-ટેક્સ્ટ અને અડધા મિલિયન અક્ષરો ટેક્સ્ટ-ટુ-સ્પીચની મંજૂરી આપે છે.

  6. Review + create પર ક્લિક કરો, પછી Create પર. લગભગ એક મિનિટ રાહ જુઓ, પછી Go to resource પર ક્લિક કરો.

  7. ડાબી બાજુના મેનૂમાં Keys and Endpoint ખોલો. KEY 1 અને Location/Region કૉપિ કરો.

તેમને secrets.h માં AZURE_SPEECH_KEY અને AZURE_REGION તરીકે મૂકો. AZURE_STT_HOST માટે, <region>.stt.speech.microsoft.com વાપરો - તેથી eastus પ્રદેશ સાથે તે eastus.stt.speech.microsoft.com છે.

ક્રેડિટ કાર્ડ વિશે. Azure મફત ટ્રાયલ તમારી ઓળખ ચકાસવા માટે કાર્ડ માંગે છે. તે તમારી પાસેથી ચાર્જ લેતું નથી. તમને 30 દિવસ માટે $200 ક્રેડિટ મળે છે, અને તે પછી એકાઉન્ટ Pay-As-You-Go પર જાય છે - પરંતુ F0 Speech સ્તર મફત રહે છે, મહિના પછી મહિને, અને આ પ્રોજેક્ટ્સની દરેક વસ્તુ તેની અંદર આરામથી બંધબેસે છે. જો તમે બિલકુલ કાર્ડ આપવા માંગતા નથી અને તમે વિદ્યાર્છી છો, તો Azure for Students વિકલ્પ તમને તેના વિના ક્રેડિટ આપે છે.

તે "Speech service" રિસોર્સ હોવું જોઈએ. Translator, Language, અથવા સામાન્ય Cognitive Services રિસોર્સની કી સમાન દેખાય છે અને સંપૂર્ણપણે માન્ય છે - પરંતુ દરેક સ્પીચ વિનંતી ભૂલ 401 આપે છે. આ અમને પરીક્ષણ દરમિયાન પકડાયું અને એક કલાકનો ખર્ચ થયો. જો સ્પીચ 401 સાથે નિષ્ફળ જાય જ્યારે કી યોગ્ય લાગે, તો તપાસો કે તમે કયા પ્રકારનું રિસોર્સ બનાવ્યું છે.

ચલાવવાનો ખર્ચ શું છે

ખૂબ ઓછો, પરંતુ તે મફત નથી, અને પ્રોજેક્ટ ચાલુ રાખતા પહેલાં તમારે લગભગ જાણવું જોઈએ કે તમે શું ખર્ચ કરી રહ્યા છો.

સેવા

અંદાજિત કિંમત

Azure સ્પીચ

મફત ટાયર દર મહિને લગભગ 5 કલાક સાંભળવા અને 0.5 M અક્ષરો બોલવા માટે આવરી લે છે

DeepSeek

દરેક જવાબ માટે સેન્ટનો એક અંશ - થોડા ડોલરમાં હજારો જવાબો

OpenAI વિઝન

મોડેલ પર આધાર રાખીને, દરેક ચિત્ર માટે લગભગ એક કે બે સેન્ટ

કિંમતો બદલાય છે, તેથી આને ક્વોટ તરીકે નહીં પણ માર્ગદર્શિકા તરીકે લો. આ દરેક સેવામાં વપરાશ પૃષ્ઠ છે જ્યાં તમે જોઈ શકો છો કે તમે શું ખર્ચ્યું છે, અને તે બધા તમને ખર્ચ મર્યાદા સેટ કરવાની મંજૂરી આપે છે - જે પહેલા દિવસે કરવા યોગ્ય છે.

તમારી કીઓ ખાનગી રાખો. જેની પાસે પણ તે હશે તે તમારા પૈસા ખર્ચી શકે છે. તેમને વિડિયો, સ્ક્રીનશોટ, ફોરમ પોસ્ટ અથવા જાહેર કોડ રિપોઝિટરીમાં ન મૂકો. જો કોઈ કી ક્યારેય ખુલ્લી થાય, તો તેને પ્રદાતાની વેબસાઇટ પર કાઢી નાખો અને નવી બનાવો - તે થોડી સેકંડ લે છે, અને તે એકમાત્ર વાસ્તવિક ઉપાય છે.

સમસ્યા નિવારણ

લક્ષણ

કારણ અને ઉપાય

સ્ક્રીન કાળી રહે છે

ખોટી GFX લાઇબ્રેરી આવૃત્તિ (1.5.6 વાપરો) અથવા ખોટી બોર્ડ સેટિંગ્સ.

PSRAM alloc failed અથવા કેમેરા ભૂલ 0xffffffff

Tools → PSRAM OPI PSRAM પર સેટ નથી.

કંઈ અપલોડ થતું નથી / કોઈ COM પોર્ટ નથી

ખોટું USB-C પોર્ટ, અથવા CH340 ડ્રાઇવર ઇન્સ્ટોલ થયેલ નથી.

કેમેરા નિષ્ફળ જાય છે અને ક્યારેય પુનઃપ્રાપ્ત થતો નથી

કેમેરાની રીસેટ લાઇન બોર્ડના RESET બટન સાથે જોડાયેલી છે, તેથી સોફ્ટવેર તેને ફરીથી શરૂ કરી શકતું નથી. RESET દબાવો. જો તે હજુ પણ નિષ્ફળ જાય, તો કેમેરાની રિબન કેબલ ફરીથી ગોઠવો.

કોડ ડાઉનલોડ કરો

આ પ્રોજેક્ટ માટે સંપૂર્ણ Arduino સ્કેચ, pins.h અને તેને જોઈતી બાકીની દરેક વસ્તુ સાથે, ડાઉનલોડ કરવા માટે મફત છે.

05_Vision_AI ડાઉનલોડ કરો

તેને અનઝિપ કરો, Arduino IDE માં .ino ફાઇલ ખોલો, ઉપરની સેટિંગ્સ તપાસો, અને CH340K USB-C પોર્ટ દ્વારા અપલોડ કરો.

885-Arduin code for MaTouch AI ESP32S3 2.8in AI Camera: AI Vision
ભાષા: C++
/*
 * ===========================================================================
 *  05_Vision_AI  —  MaTouch AI ESP32-S3 2.8" TFT ST7789V
 * ===========================================================================
 *
 ----------
 *  ROBOJAX.COM  -  MaTouch AI ESP32-S3 2.8" project series
 *
 *    WATCH THE VIDEO
 *        https://youtu.be/6AL3g3tC_Hk
 *
 *    WRITTEN TUTORIALS - every project, with photos and full explanation
 *        Camera and touchscreen.... https://robojax.com/RTJ849
 *        Offline face recognition.. https://robojax.com/RTJ850
 *        AI voice assistant........ https://robojax.com/RTJ851
 *        AI vision................. https://robojax.com/RTJ852
 *
 *    GET THE BOARD - SAVE $5 with coupon code:  Robojax_Makerfab
 *        https://www.makerfabs.com/matouch-ai-esp32s3-2-8-tft-st7789v.html
 *        (enter the code at checkout)
 *
 *  All of this code is free. If it helped you, a subscribe on YouTube is
 *  the best way to support more of it.
 *  
 *
 *  Watching the video first will save you time - it shows the Arduino IDE
 *  settings and the library versions being set up step by step.
 *  ---------------------------------------------------------------------------
 *
 *  The board SEES. Point the camera at something, touch a button, and an
 *  OpenAI vision model tells you what it is looking at - drawn on the screen
 *  and (optionally) spoken out of the board's own speaker via Azure TTS.
 *
 *  Two modes, two buttons:
 *
 *      OBJECTS   "What do you see?" - names the things in front of the lens.
 *      PERSON    Describes the person in frame: glasses, expression, what
 *                they are doing. DESCRIPTION ONLY - it will not and must not
 *                try to say WHO someone is. Identifying people by face is
 *                against OpenAI's usage policies, and it is the right call:
 *                say this in the video, it is worth 15 honest seconds.
 *
 *  WHY OPENAI FOR THIS DEMO AND NOT DEEPSEEK: DeepSeek's API is text-only.
 *  It cannot accept an image at all. Azure OpenAI could do it, but plain
 *  OpenAI is one endpoint with no deployment setup - simplest to follow.
 *
 *  HOW THE IMAGE TRAVELS: the OV3660 gives us a JPEG directly (800x600,
 *  ~40 KB). We base64-encode it in PSRAM (~55 KB of text) and embed it in
 *  the JSON request as a data: URI. The 8 MB PSRAM makes this trivial.
 *
 *  ---------------------------------------------------------------------------
 *  FILL IN secrets.h BEFORE FLASHING
 *  (needs WIFI_*, OPENAI_*; AZURE_* only if SPEAK_REPLIES is 1).
 *
 *  BOARD SETTINGS (Tools menu - EVERY line matters, wrong = black screen
 *  or compile errors. These reset when you switch cores - recheck them!)
 *
 *      Board            : ESP32S3 Dev Module
 *      ESP32 core       : 2.0.17
 *      PSRAM            : OPI PSRAM        <-- required, image buffers live there
 *      Flash Size       : 16MB (128Mb)
 *      Partition Scheme : 16M Flash (3MB APP/9.9MB FATFS)
 *      USB CDC On Boot  : Disabled         <-- speaker shares pins with native USB
 *      Upload Speed     : 921600
 *      Port             : the CH340K USB-C port (the one near RESET)
 *
 *  LIBRARIES
 *      GFX Library for Arduino   v1.5.6   (NOT 1.6.x - that pairs with core 3)
 *      bb_captouch               v1.3.1
 *      ArduinoJson               v7.x
 *      Adafruit NeoPixel         any recent
 *
 *  ---------------------------------------------------------------------------
 *  FUNCTIONS IN THIS SKETCH
 *      led(r,g,b)          set the RGB status LED colour
 *      getTouch(&x,&y)     read the touch panel, mapped to screen coordinates
 *      camStart(...)       start the camera in a given format/size
 *      camStartPreview()   start the 240x240 RGB565 live-view camera
 *      captureJpeg(&len)   take one 800x600 JPEG into PSRAM (camera stays OFF
 *                          afterwards - caller restarts the preview)
 *      readHttpResponse()  read an HTTPS reply, de-chunking it properly
 *      askVision(...)      send photo + prompt to OpenAI, return the answer
 *      spkInit()           configure the I2S speaker output
 *      readExact(...)      read exactly N bytes from a TLS connection
 *      speak(text)         Azure TTS -> download voice to PSRAM -> play it
 *      playLastAnswer()    replay the kept voice from PSRAM (REPLAY button)
 *      drawButton(...)     draw one side-column button
 *      drawWifi()          WiFi signal bars + dBm readout
 *      drawButtons()       normal side column (the two ask buttons)
 *      drawClearSide()     answer-mode side column (CLEAR + REPLAY)
 *      showAnswer(text)    word-wrapped answer overlay on the viewfinder
 *      lookAndTell(...)    one full cycle: capture -> ask -> show -> speak
 *      setup() / loop()    boot sequence / viewfinder + touch handling
 *
 *  Robojax.com
 * ===========================================================================
 */

#define SPEAK_REPLIES  1     // 1 = read the answer aloud with Azure TTS, 0 = screen only

/* Status LED brightness, 0-255. The WS2812 runs from the power rail and is
 * uncomfortably bright at full power - 25 is plenty visible on camera. */
#define LED_BRIGHTNESS 15

/* CAMERA ORIENTATION
 * 1 = camera faces the SAME way as the screen (how the board ships) - the
 *     image is mirrored so it looks natural when you point it at yourself.
 * 0 = you folded the ribbon so the lens faces AWAY from the screen
 *     (phone-style, screen to you / camera to the subject).
 * If the picture looks left-right reversed, flip this number. */
#define CAMERA_FACES_USER  1

#include <Arduino_GFX_Library.h>
#include <bb_captouch.h>
#include <Adafruit_NeoPixel.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include "mbedtls/base64.h"
#include "esp_camera.h"
#include "pins.h"
#include "secrets.h"

#if SPEAK_REPLIES
#include "driver/i2s.h"
#define WAV_HEADER_LEN 44
#endif

Arduino_ESP32SPI *bus = new Arduino_ESP32SPI(
    TFT_DC, TFT_CS, TFT_SCLK, TFT_MOSI, TFT_MISO, HSPI, true);
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RES, 1, true);

BBCapTouch bbct;
Adafruit_NeoPixel rgb(RGB_LED_NUM, RGB_LED_PIN, NEO_GRB + NEO_KHZ800);

/* --- the two vision prompts ------------------------------------------------
 * Keep answers short: they must fit a 320x240 screen and, if spoken, must not
 * leave the presenter waiting awkwardly on camera. */
const char *PROMPT_OBJECTS =
    "Look at this photo from a small camera. In ONE short sentence of at most "
    "20 words, plain text only, name the main object(s) you see.";

const char *PROMPT_PERSON =
    "Look at this photo. If there is a person, describe them in ONE short "
    "sentence of at most 20 words: mood, glasses or not, what they are doing. "
    "Never guess who they are. If no person is visible, say so. Plain text only.";

/* --- layout: viewfinder left, buttons right ------------------------------- */
#define BTN_X   242
#define BTN_W    78
#define BTN_H    52
#define BTN_OBJ_Y     4
#define BTN_PERSON_Y 62

void led(uint8_t r, uint8_t g, uint8_t b) { rgb.setPixelColor(0, rgb.Color(r, g, b)); rgb.show(); }


/* ===========================================================================
 *  Touch
 * =========================================================================== */
bool getTouch(uint16_t *x, uint16_t *y) {
  TOUCHINFO ti;
  if (!bbct.getSamples(&ti)) return false;
  if (ti.count < 1) return false;
  *x = ti.y[0];
  *y = (ti.x[0] > 240) ? 0 : (240 - ti.x[0]);
  return true;
}


/* ===========================================================================
 *  Camera  —  RGB565 for the live view; JPEG capture happens by restarting
 *  the driver, same technique as sketch 02.
 * =========================================================================== */
bool mirrored = CAMERA_FACES_USER;   // see the define at the top of the file

/* The viewfinder is OFF while a cloud call runs and while the answer is on
 * screen. Two reasons, both learned on the bench:
 *  1. RAM: the live camera driver eats the internal memory a TLS handshake
 *     needs - with the preview running, connecting to OpenAI fails (HTTP -1).
 *  2. Readability: the viewfinder repaints 25x/s and would wipe the answer.
 * The answer STAYS on screen until the user taps CLEAR - no timer. */
bool preview_on = false;

/* The last spoken answer is KEPT in PSRAM so the REPLAY button can play it
 * again without another cloud call. Overwritten by the next answer. */
uint8_t *last_audio = nullptr;
size_t   last_audio_len = 0;

bool camStart(pixformat_t fmt, framesize_t size, int fb_count, int quality) {
  camera_config_t c;
  c.ledc_channel = LEDC_CHANNEL_0;
  c.ledc_timer   = LEDC_TIMER_0;
  c.pin_d0 = CAM_PIN_D0;  c.pin_d1 = CAM_PIN_D1;
  c.pin_d2 = CAM_PIN_D2;  c.pin_d3 = CAM_PIN_D3;
  c.pin_d4 = CAM_PIN_D4;  c.pin_d5 = CAM_PIN_D5;
  c.pin_d6 = CAM_PIN_D6;  c.pin_d7 = CAM_PIN_D7;
  c.pin_xclk     = CAM_PIN_XCLK;
  c.pin_pclk     = CAM_PIN_PCLK;
  c.pin_vsync    = CAM_PIN_VSYNC;
  c.pin_href     = CAM_PIN_HREF;
  /* Share the I2C bus Wire already drives (the touch panel lives there too)
   * instead of letting the camera install a second driver on the same pins -
   * that kills touch. Wire.begin() must run before this function. */
  c.pin_sccb_sda = -1;
  c.pin_sccb_scl = -1;
  c.sccb_i2c_port = 0;               // Wire = I2C port 0
  c.pin_pwdn     = CAM_PIN_PWDN;
  c.pin_reset    = CAM_PIN_RESET;
  c.xclk_freq_hz = 20000000;
  c.frame_size   = size;
  c.pixel_format = fmt;
  c.grab_mode    = CAMERA_GRAB_WHEN_EMPTY;
  c.fb_location  = CAMERA_FB_IN_PSRAM;
  c.jpeg_quality = quality;
  c.fb_count     = fb_count;

  if (esp_camera_init(&c) != ESP_OK) return false;

  sensor_t *s = esp_camera_sensor_get();
  if (s) {
    s->set_hmirror(s, mirrored ? 1 : 0);
    s->set_vflip(s,   mirrored ? 1 : 0);
    s->set_brightness(s, 1);
  }
  return true;
}

bool camStartPreview() { return camStart(PIXFORMAT_RGB565, FRAMESIZE_240X240, 2, 12); }

/* Capture one SVGA JPEG into a PSRAM buffer the caller owns. */
uint8_t *captureJpeg(size_t *len_out) {
  esp_camera_deinit();
  delay(120);
  if (!camStart(PIXFORMAT_JPEG, FRAMESIZE_SVGA, 1, 12)) { *len_out = 0; return nullptr; }

  // a few warm-up frames so exposure settles
  for (int i = 0; i < 3; i++) {
    camera_fb_t *w = esp_camera_fb_get();
    if (w) esp_camera_fb_return(w);
    delay(100);
  }

  uint8_t *copy = nullptr;
  *len_out = 0;
  camera_fb_t *fb = esp_camera_fb_get();
  if (fb && fb->len > 0) {
    copy = (uint8_t *)ps_malloc(fb->len);
    if (copy) { memcpy(copy, fb->buf, fb->len); *len_out = fb->len; }
  }
  if (fb) esp_camera_fb_return(fb);

  /* Deliberately leave the camera OFF here. The caller restarts the preview
   * after the cloud call - a running camera driver starves the TLS handshake
   * of internal RAM (that was the "Vision HTTP -1" bug). */
  esp_camera_deinit();
  delay(120);
  return copy;
}


/* ===========================================================================
 *  HTTP response reader  —  shared by the cloud calls below.
 *  Returns the status code and fills body_out. Handles chunked transfer
 *  encoding PROPERLY: the chunk-size markers must be stripped, or they end
 *  up embedded inside the JSON and the parse fails. (OpenAI pretty-prints
 *  its replies so they span several chunks - this bit us on the bench.)
 * =========================================================================== */
/* Block until the connection has data (or the budget runs out). Every read
 * below goes through this, because the server can go quiet for many seconds
 * while it analyses the image - and a bare read() would simply time out. */
static bool waitData(WiFiClientSecure &c, uint32_t ms) {
  uint32_t t0 = millis();
  while (!c.available()) {
    if (!c.connected()) return false;
    if (millis() - t0 > ms) return false;
    delay(10);
  }
  return true;
}

static int readHttpResponse(WiFiClientSecure &client, String &body_out, uint32_t idle_ms) {
  body_out = "";

  if (!waitData(client, idle_ms)) { Serial.println("HTTP: no response at all"); return 0; }
  String status_line = client.readStringUntil('\n');
  int code = 0;
  sscanf(status_line.c_str(), "HTTP/%*s %d", &code);

  bool chunked = false;
  while (waitData(client, idle_ms)) {
    String h = client.readStringUntil('\n');
    if (h == "\r" || h.length() <= 1) break;         // blank line = end of headers
    h.toLowerCase();
    if (h.startsWith("transfer-encoding:") && h.indexOf("chunked") >= 0) chunked = true;
  }

  if (chunked) {
    int blanks = 0;
    while (true) {
      /* Waiting here - instead of letting read() time out - is essential: a
       * timed-out read looks exactly like "0" (final chunk), which silently
       * truncates the body to nothing while the server is still thinking. */
      if (!waitData(client, idle_ms)) {
        Serial.println("HTTP: timed out waiting for the next chunk");
        break;
      }
      String szline = client.readStringUntil('\n');
      szline.trim();
      if (szline.length() == 0) {
        if (++blanks > 4) break;
        continue;
      }
      blanks = 0;

      long sz = strtol(szline.c_str(), NULL, 16);
      if (sz <= 0) break;                            // genuine final chunk

      long got = 0;
      while (got < sz) {
        if (!waitData(client, idle_ms)) break;
        while (client.available() && got < sz) { body_out += (char)client.read(); got++; }
      }
      if (waitData(client, 3000)) client.readStringUntil('\n');
      if (got < sz) { Serial.println("HTTP: short chunk"); break; }
    }
  } else {
    while (waitData(client, idle_ms))
      while (client.available()) body_out += (char)client.read();
  }
  return code;
}


/* ===========================================================================
 *  OpenAI vision call
 *  The request body is built by hand in PSRAM rather than through ArduinoJson,
 *  because embedding a 55 KB base64 string in a JSON document would mean
 *  holding two copies. Base64 text never needs JSON escaping, so this is safe.
 * =========================================================================== */
bool askVision(const uint8_t *jpg, size_t jpg_len, const char *prompt, String &answer_out) {
  // --- base64 encode the image into PSRAM ---
  size_t b64_cap = ((jpg_len + 2) / 3) * 4 + 16;
  unsigned char *b64 = (unsigned char *)ps_malloc(b64_cap);
  if (!b64) return false;
  size_t b64_len = 0;
  if (mbedtls_base64_encode(b64, b64_cap, &b64_len, jpg, jpg_len) != 0) {
    free(b64);
    return false;
  }

  // --- assemble the JSON request around it ---
  /* Current OpenAI models reject the old "max_tokens" name - it must be
   * "max_completion_tokens" (verified against the live API, July 2026). */
  const char *head_fmt =
      "{\"model\":\"%s\",\"max_completion_tokens\":%d,\"messages\":[{\"role\":\"user\","
      "\"content\":[{\"type\":\"text\",\"text\":\"%s\"},"
      "{\"type\":\"image_url\",\"image_url\":{\"url\":\"data:image/jpeg;base64,";
  const char *tail = "\"}}]}]}";

  char head[640];
  int head_len = snprintf(head, sizeof(head), head_fmt, OPENAI_MODEL, LLM_MAX_TOKENS, prompt);

  size_t body_len = head_len + b64_len + strlen(tail);
  char *body = (char *)ps_malloc(body_len + 1);
  if (!body) { free(b64); return false; }
  memcpy(body, head, head_len);
  memcpy(body + head_len, b64, b64_len);
  strcpy(body + head_len + b64_len, tail);
  free(b64);

  // --- send it: manual HTTP, chunk-wise upload (proven pattern from 04) ---
  WiFiClientSecure client;
  client.setInsecure();
  client.setTimeout(25);

  if (!client.connect(OPENAI_HOST, 443)) {
    Serial.println("Vision: TLS connect failed (is the camera still running?)");
    free(body);
    return false;
  }

  client.print(String("POST /v1/chat/completions HTTP/1.1\r\n"
               "Host: " OPENAI_HOST "\r\n"
               "Authorization: Bearer " OPENAI_KEY "\r\n"
               "Content-Type: application/json\r\n"
               "Connection: close\r\n"
               "Content-Length: ") + String(body_len) + "\r\n\r\n");

  size_t sent = 0;
  while (sent < body_len) {
    size_t n = min((size_t)4096, body_len - sent);
    size_t w = client.write((uint8_t *)body + sent, n);
    if (w == 0) {
      delay(50);
      w = client.write((uint8_t *)body + sent, n);
      if (w == 0) break;
    }
    sent += w;
    yield();
  }
  free(body);
  if (sent < body_len) {
    Serial.printf("Vision: upload stalled at %u/%u bytes\n",
                  (unsigned)sent, (unsigned)body_len);
    client.stop();
    return false;
  }

  /* read the reply with proper de-chunking */
  String resp;
  int code = readHttpResponse(client, resp, 30000);
  client.stop();

  if (code != 200) {
    Serial.printf("Vision HTTP %d: %s\n", code, resp.c_str());
    return false;
  }

  bool ok = false;
  JsonDocument doc;
  if (!deserializeJson(doc, resp)) {
    const char *content = doc["choices"][0]["message"]["content"];
    if (content) {
      answer_out = String(content);
      answer_out.trim();
      ok = answer_out.length() > 0;
    }
  } else {
    Serial.printf("Vision: JSON parse failed, %u bytes received\n", resp.length());
  }
  return ok;
}


/* ===========================================================================
 *  Azure TTS  —  same streaming trick as sketch 04: raw PCM into I2S.
 * =========================================================================== */
#if SPEAK_REPLIES
void spkInit() {
  i2s_config_t cfg = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
    .sample_rate = 16000,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
    .communication_format = I2S_COMM_FORMAT_STAND_I2S,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 8,
    .dma_buf_len = 256,
    .use_apll = false,
    .tx_desc_auto_clear = true,
    .fixed_mclk = 0
  };
  i2s_pin_config_t pins = {
    .mck_io_num = I2S_PIN_NO_CHANGE,
    .bck_io_num = I2S_SPK_BCLK,
    .ws_io_num = I2S_SPK_LRC,
    .data_out_num = I2S_SPK_DOUT,
    .data_in_num = I2S_PIN_NO_CHANGE
  };
  i2s_driver_install(I2S_SPK_PORT, &cfg, 0, NULL);
  i2s_set_pin(I2S_SPK_PORT, &pins);
  i2s_zero_dma_buffer(I2S_SPK_PORT);
}

/* Read exactly n bytes from a client (or until timeout). */
static size_t readExact(WiFiClientSecure &c, uint8_t *dst, size_t n) {
  size_t got = 0;
  uint32_t t0 = millis();
  while (got < n && millis() - t0 < 10000) {
    int r = c.read(dst + got, n - got);
    if (r > 0) { got += r; t0 = millis(); }
    else if (!c.connected() && !c.available()) break;
    else delay(2);
  }
  return got;
}

void playLastAnswer();   // defined below; explicit prototype for the IDE

void speak(const String &text) {
  String safe = text;
  safe.replace("&", "&amp;");
  safe.replace("<", "&lt;");
  safe.replace(">", "&gt;");
  String ssml = "<speak version='1.0' xml:lang='" AZURE_TTS_LANG "'>"
                "<voice name='" AZURE_TTS_VOICE "'>" + safe + "</voice></speak>";

  /* Manual HTTP with proper de-chunking: Azure sends this audio chunked, and
   * HTTPClient's raw stream leaks the ASCII chunk-size lines into the PCM -
   * each one plays as an audible KNOCK. */
  WiFiClientSecure client;
  client.setInsecure();
  client.setTimeout(20);

  const char *host = AZURE_REGION ".tts.speech.microsoft.com";
  if (!client.connect(host, 443)) { Serial.println("TTS: TLS connect failed"); return; }

  client.print(String("POST /cognitiveservices/v1 HTTP/1.1\r\n"
               "Host: ") + host + "\r\n"
               "Ocp-Apim-Subscription-Key: " AZURE_SPEECH_KEY "\r\n"
               "Content-Type: application/ssml+xml\r\n"
               "X-Microsoft-OutputFormat: riff-16khz-16bit-mono-pcm\r\n"
               "User-Agent: MaTouchRobojax\r\n"
               "Connection: close\r\n"
               "Content-Length: " + String(ssml.length()) + "\r\n\r\n");
  client.print(ssml);

  String status_line = client.readStringUntil('\n');
  int code = 0;
  sscanf(status_line.c_str(), "HTTP/%*s %d", &code);
  bool chunked = false;
  long content_len = -1;
  while (client.connected() || client.available()) {
    String h = client.readStringUntil('\n');
    if (h == "\r" || h.length() <= 1) break;
    h.toLowerCase();
    if (h.startsWith("transfer-encoding:") && h.indexOf("chunked") >= 0) chunked = true;
    if (h.startsWith("content-length:")) content_len = h.substring(15).toInt();
  }
  if (code != 200) { Serial.printf("TTS HTTP %d\n", code); client.stop(); return; }

  const size_t AUDIO_CAP = 1200 * 1024;
  uint8_t *audio = (uint8_t *)ps_malloc(AUDIO_CAP);
  if (!audio) { client.stop(); return; }
  size_t alen = 0;

  if (chunked) {
    while (true) {
      String szline = client.readStringUntil('\n');
      long sz = strtol(szline.c_str(), NULL, 16);
      if (sz <= 0) break;
      if (alen + sz > AUDIO_CAP) break;
      size_t got = readExact(client, audio + alen, sz);
      alen += got;
      client.readStringUntil('\n');
      if (got < (size_t)sz) break;
    }
  } else if (content_len > 0) {
    alen = readExact(client, audio, min((size_t)content_len, AUDIO_CAP));
  } else {
    uint32_t idle = millis();
    while ((client.connected() || client.available()) && millis() - idle < 5000) {
      int r = client.read(audio + alen, min((size_t)2048, AUDIO_CAP - alen));
      if (r > 0) { alen += r; idle = millis(); }
      else delay(5);
    }
  }
  client.stop();

  if (alen > WAV_HEADER_LEN) {
    /* keep this answer for the REPLAY button (replacing the previous one),
     * then play it */
    if (last_audio) free(last_audio);
    last_audio = audio;
    last_audio_len = alen;
    playLastAnswer();
  } else {
    free(audio);
  }
}

/* Play the kept answer from PSRAM - used right after download AND by REPLAY. */
void playLastAnswer() {
  if (!last_audio || last_audio_len <= WAV_HEADER_LEN) return;
  static const uint8_t lead_in[640] = {0};     // 20 ms silence pre-roll
  size_t w = 0;
  i2s_write(I2S_SPK_PORT, lead_in, sizeof(lead_in), &w, portMAX_DELAY);
  i2s_write(I2S_SPK_PORT, last_audio + WAV_HEADER_LEN,
            last_audio_len - WAV_HEADER_LEN, &w, portMAX_DELAY);
  i2s_write(I2S_SPK_PORT, lead_in, sizeof(lead_in), &w, portMAX_DELAY);
  delay(150);
  i2s_zero_dma_buffer(I2S_SPK_PORT);
}
#endif  // SPEAK_REPLIES


/* ===========================================================================
 *  UI
 * =========================================================================== */
void drawButton(int y, const char *l1, const char *l2, uint16_t colour) {
  gfx->fillRoundRect(BTN_X, y, BTN_W, BTN_H, 6, colour);
  gfx->drawRoundRect(BTN_X, y, BTN_W, BTN_H, 6, WHITE);
  gfx->setTextSize(1);
  gfx->setTextColor(WHITE);
  gfx->setCursor(BTN_X + 8, y + 14);
  gfx->print(l1);
  gfx->setCursor(BTN_X + 8, y + 28);
  gfx->print(l2);
}

/* WiFi bars + dBm at the bottom of the side column, refreshed from the loop */
void drawWifi() {
  gfx->fillRect(BTN_X, 188, BTN_W, 52, BLACK);
  bool up = (WiFi.status() == WL_CONNECTED);
  long rssi = up ? WiFi.RSSI() : -100;
  int bars = rssi > -55 ? 4 : rssi > -65 ? 3 : rssi > -75 ? 2 : rssi > -85 ? 1 : 0;

  for (int b = 0; b < 4; b++) {
    int bh = 6 + b * 6;
    uint16_t col = (b < bars) ? GREEN : gfx->color565(60, 60, 60);
    gfx->fillRect(BTN_X + 4 + b * 9, 216 - bh, 7, bh, col);
  }
  gfx->setTextSize(1);
  gfx->setCursor(BTN_X + 44, 196);
  if (up) {
    gfx->setTextColor(CYAN);
    gfx->printf("%ld", rssi);
  } else {
    gfx->setTextColor(RED);
    gfx->print("DOWN");
  }
  gfx->setCursor(BTN_X + 44, 208);
  gfx->setTextColor(gfx->color565(120, 120, 120));
  gfx->print("dBm");
  gfx->setCursor(BTN_X + 4, 228);
  gfx->setTextColor(gfx->color565(120, 120, 120));
  gfx->print("WiFi signal");
}

/* normal side column: the two ask buttons */
void drawButtons() {
  gfx->fillRect(BTN_X, 0, BTN_W, 188, BLACK);
  drawButton(BTN_OBJ_Y,    "WHAT DO",  "YOU SEE?", gfx->color565(0, 90, 160));
  drawButton(BTN_PERSON_Y, "DESCRIBE", "PERSON",   gfx->color565(120, 60, 140));

  gfx->setTextColor(CYAN);
  gfx->setCursor(BTN_X + 2, 128);
  gfx->print("OpenAI eyes");
  gfx->setCursor(BTN_X + 2, 140);
  gfx->print("Azure voice");
  gfx->setCursor(BTN_X + 2, 152);
  gfx->print("Robojax.com");

  drawWifi();
}

/* answer-mode side column: CLEAR (back to camera) + REPLAY (say it again).
 * The answer stays on screen until CLEAR is tapped. */
void drawClearSide() {
  gfx->fillRect(BTN_X, 0, BTN_W, 188, BLACK);
  gfx->fillRoundRect(BTN_X, BTN_OBJ_Y, BTN_W, BTN_H, 6, gfx->color565(110, 35, 35));
  gfx->drawRoundRect(BTN_X, BTN_OBJ_Y, BTN_W, BTN_H, 6, WHITE);
  gfx->setTextSize(2);
  gfx->setTextColor(WHITE);
  gfx->setCursor(BTN_X + 10, BTN_OBJ_Y + 14);
  gfx->print("CLEAR");

#if SPEAK_REPLIES
  if (last_audio_len > 0) {
    gfx->fillRoundRect(BTN_X, BTN_PERSON_Y, BTN_W, BTN_H, 6, gfx->color565(0, 110, 60));
    gfx->drawRoundRect(BTN_X, BTN_PERSON_Y, BTN_W, BTN_H, 6, WHITE);
    gfx->setTextSize(1);
    gfx->setTextColor(WHITE);
    gfx->setCursor(BTN_X + 16, BTN_PERSON_Y + 20);
    gfx->print("REPLAY");
  }
#endif

  gfx->setTextSize(1);
  gfx->setTextColor(YELLOW);
  gfx->setCursor(BTN_X + 2, 124);
  gfx->print("CLEAR = camera");
  gfx->setCursor(BTN_X + 2, 136);
  gfx->print("REPLAY = again");

  drawWifi();
}

/* Word-wrapped answer overlay across the bottom of the viewfinder. */
void showAnswer(const String &text) {
  const int max_chars = 38;
  int lines = (text.length() + max_chars - 1) / max_chars;
  if (lines > 6) lines = 6;
  int h = lines * 10 + 8;
  int y = 240 - h;

  gfx->fillRect(0, y, 240, h, gfx->color565(0, 0, 0));
  gfx->drawRect(0, y, 240, h, CYAN);
  gfx->setTextSize(1);
  gfx->setTextColor(WHITE);
  for (int i = 0; i < lines; i++) {
    gfx->setCursor(4, y + 5 + i * 10);
    gfx->print(text.substring(i * max_chars, min((int)text.length(), (i + 1) * max_chars)));
  }
}


/* ===========================================================================
 *  One full "look and tell" cycle
 * =========================================================================== */
void lookAndTell(const char *prompt, const char *label) {
  led(255, 120, 0);                                        // amber: working
  preview_on = false;                                      // camera goes OFF here

  gfx->fillRect(0, 0, 240, 240, BLACK);
  gfx->setTextColor(YELLOW);
  gfx->setTextSize(1);
  gfx->setCursor(30, 110);
  gfx->printf("capturing photo (%s)...", label);

  size_t jpg_len = 0;
  uint32_t t_cap = millis();
  uint8_t *jpg = captureJpeg(&jpg_len);
  t_cap = millis() - t_cap;

  if (!jpg || jpg_len == 0) {
    if (jpg) free(jpg);
    showAnswer("Capture failed - tap CLEAR to retry.");
    led(255, 0, 0);
    drawClearSide();
    return;
  }
  Serial.printf("Captured %u KB in %lu ms\n", (unsigned)(jpg_len / 1024), (unsigned long)t_cap);

  gfx->setCursor(30, 124);
  gfx->printf("asking OpenAI (%uKB)...", (unsigned)(jpg_len / 1024));

  String answer;
  uint32_t t_ai = millis();
  bool ok = askVision(jpg, jpg_len, prompt, answer);
  t_ai = millis() - t_ai;
  free(jpg);

  if (!ok) {
    showAnswer("No answer - see serial monitor for the reason.");
    led(255, 0, 0);
    drawClearSide();
    return;
  }

  Serial.printf("Vision (%lu ms): %s\n", (unsigned long)t_ai, answer.c_str());
  showAnswer(answer);
  drawClearSide();                                         // CLEAR replaces the ask buttons

#if SPEAK_REPLIES
  led(0, 255, 40);                                         // green: speaking
  speak(answer);                                           // answer stays on screen
#endif
  led(0, 0, 0);
  /* the answer now stays until the user taps CLEAR */
}


/* ===========================================================================
 *  SETUP
 * =========================================================================== */
void setup() {
  Serial.begin(115200);
  delay(400);
  Serial.println("\n=== 05 Vision AI  |  Robojax.com ===");

  pinMode(TFT_BLK, OUTPUT);
  digitalWrite(TFT_BLK, LOW);
  pinMode(SD_CS, OUTPUT);
  digitalWrite(SD_CS, HIGH);

  gfx->begin();
  gfx->fillScreen(BLACK);
  digitalWrite(TFT_BLK, HIGH);

  bbct.init(TOUCH_SDA, TOUCH_SCL, TOUCH_RST, TOUCH_INT);
  delay(50);

  // The camera's SCCB shares this bus, so Wire must be up before the camera
  Wire.begin(I2C_SDA, I2C_SCL, 100000);
  delay(20);

  rgb.begin();
  rgb.setBrightness(LED_BRIGHTNESS);
  led(0, 0, 0);

#if SPEAK_REPLIES
  spkInit();
#endif

  gfx->setTextSize(1);
  gfx->setTextColor(YELLOW);
  gfx->setCursor(4, 4);
  gfx->printf("Connecting to %s ...", WIFI_SSID);

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < 20000) delay(300);

  gfx->setCursor(4, 16);
  if (WiFi.status() == WL_CONNECTED) {
    gfx->setTextColor(GREEN);
    gfx->print("WiFi ok");
  } else {
    gfx->setTextColor(RED);
    gfx->print("WiFi FAILED (2.4GHz only! check secrets.h)");
  }

  gfx->setCursor(4, 28);
  gfx->setTextColor(YELLOW);
  gfx->print("starting camera...");
  if (!camStartPreview()) {
    gfx->fillScreen(RED);
    gfx->setTextColor(WHITE);
    gfx->setTextSize(2);
    gfx->setCursor(20, 100);
    gfx->print("CAMERA FAILED");
    gfx->setTextSize(1);
    gfx->setCursor(20, 130);
    gfx->print("Press RESET (camera reset = board reset)");
    while (1) delay(1000);
  }

  delay(400);
  gfx->fillScreen(BLACK);
  drawButtons();
  preview_on = true;
  Serial.println("Running. Touch a button to have the AI look through the camera.");
}


/* ===========================================================================
 *  LOOP
 * =========================================================================== */
void loop() {
  static uint32_t last_touch = 0;

  if (preview_on) {
    camera_fb_t *fb = esp_camera_fb_get();
    if (fb) {
      gfx->draw16bitBeRGBBitmap(0, 0, (uint16_t *)fb->buf, fb->width, fb->height);
      esp_camera_fb_return(fb);
    }
  } else {
    delay(20);                               // answer on screen, waiting for CLEAR
  }

  /* live WiFi signal, refreshed every 2 s in both modes */
  static uint32_t last_wifi = 0;
  if (millis() - last_wifi > 2000) {
    last_wifi = millis();
    drawWifi();
  }

  /* Edge-detected touch: an action fires only on a NEW finger-down, and the
   * finger must fully lift (4 consecutive empty reads) before anything can
   * fire again. This is what stops one tap on CLEAR from also triggering the
   * capture button that appears in the same spot a moment later. */
  static bool    touch_down = false;
  static uint8_t release_count = 0;

  uint16_t x, y;
  if (getTouch(&x, &y)) {
    release_count = 0;
    if (!touch_down) {
      touch_down = true;                     // new tap - act exactly once

      if (!preview_on) {
        /* answer mode: CLEAR returns to the camera, REPLAY says it again */
        if (x >= BTN_X && y >= BTN_OBJ_Y && y < BTN_OBJ_Y + BTN_H) {
          if (camStartPreview()) {
            preview_on = true;
            drawButtons();
          } else {
            showAnswer("Camera restart failed - press RESET.");
          }
        }
#if SPEAK_REPLIES
        else if (x >= BTN_X && y >= BTN_PERSON_Y && y < BTN_PERSON_Y + BTN_H) {
          led(0, 255, 40);                   // green while speaking
          playLastAnswer();
          led(0, 0, 0);
        }
#endif
      } else if (x >= BTN_X && y >= BTN_OBJ_Y && y < BTN_OBJ_Y + BTN_H) {
        lookAndTell(PROMPT_OBJECTS, "objects");
      } else if (x >= BTN_X && y >= BTN_PERSON_Y && y < BTN_PERSON_Y + BTN_H) {
        lookAndTell(PROMPT_PERSON, "person");
      }
    }
  } else if (touch_down) {
    if (++release_count >= 4) { touch_down = false; release_count = 0; }
  }
  (void)last_touch;
}

તમને જોઈતી વસ્તુઓ

સંસાધનો અને સંદર્ભો

ફાઇલો📁

જરૂરી ફાઇલ (.h)

  • secrets.h
    file for Makerfabs MaTouch AI ESP32S3 2.8" TFT Camera module
    secrets.h 0.01 MB

અન્ય ફાઇલો

  • pins.h
    pins file for MaTouch AI ESP32S3 2.8" camera LCD touch screen.
    pins.h 0.01 MB

સ્કીમેટિક

  • MaTouch_AI 2.8“ MaTouch AI ESP32S3 2.8" TFT ST7789V schematic
    The latest MaTouch AI board integrate I2S voice input/I2S speaker/ 3 million camera OV3660/ 320*240 resolution display, with ESP32S3 strong processor& Wifi ability, to make this board a good tool/platform for AI development with ESP32.
    MaTouch_AI 2.8“ SPI TFT ST7789V V1.1.PDF 0.15 MB