{"id":21001215,"url":"https://github.com/waxdred/go-i2c-oled","last_synced_at":"2025-05-14T23:32:34.267Z","repository":{"id":204559262,"uuid":"711239959","full_name":"waxdred/go-i2c-oled","owner":"waxdred","description":"Implementation of I2C-bus written in Golang for Oled Raspberry","archived":false,"fork":false,"pushed_at":"2024-09-30T08:33:57.000Z","size":92,"stargazers_count":10,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-25T04:35:37.345Z","etag":null,"topics":["golang","i2c-bus","oled-display-ssd1306","raspberry-pi","ssd1306-oled"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/waxdred.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-10-28T16:23:37.000Z","updated_at":"2024-12-05T17:20:59.000Z","dependencies_parsed_at":"2025-04-25T04:30:45.506Z","dependency_job_id":null,"html_url":"https://github.com/waxdred/go-i2c-oled","commit_stats":null,"previous_names":["waxdred/go-i2c-oled"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waxdred%2Fgo-i2c-oled","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waxdred%2Fgo-i2c-oled/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waxdred%2Fgo-i2c-oled/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waxdred%2Fgo-i2c-oled/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/waxdred","download_url":"https://codeload.github.com/waxdred/go-i2c-oled/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254248406,"owners_count":22039008,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","i2c-bus","oled-display-ssd1306","raspberry-pi","ssd1306-oled"],"created_at":"2024-11-19T08:14:14.186Z","updated_at":"2025-05-14T23:32:29.181Z","avatar_url":"https://github.com/waxdred.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# goi2coled: A Go library for OLED screens based on SSD1306 via I2C\n\n`goi2coled` is a Go library designed to facilitate interactions with OLED screens that use the SSD1306 controller via the I2C bus. With the integration of Go's image package, this library allows developers to design and display graphics on OLED screens with ease.\n\n## Table of Contents\n- [Features](#Features)\n- [Prerequisites](#Prerequisites)\n- [Usage](#Usage)\n- [Exemple](#Exemple)\n- [Exemple_Stat](#Exemple_Stat)\n- [Notes](#Notes)\n\n## Features\n- Initialisation of I2C connections to the OLED screen.\n- Displaying and clearing content on the screen.\n- Adjusting the display contrast and dimming.\n- Directly drawing images from Go's image.RGBA to the OLED screen.\n- Convert images to data compatible with the OLED screen.\n- Direct communication with the SSD1306 controller using commands and data writes.\n\n## Prerequisites\nAn environment setup to run Go code.\nThe SSD1306 OLED screen and compatible I2C connections.\nDependency: github.com/waxdred/go-i2c-oled/ssd1306\n\n## Usage\nHere's a brief guide on how to use goi2coled:\n\n1) Initialising the I2C connection:\n```go\ni2c, err := NewI2c(vccState, height, width, address, bus)\nif err != nil {\n    log.Fatal(err)\n}\ndefer i2c.Close()\n```\n\n2) Drawing an Image:\n```go\nimg := image.NewRGBA(image.Rect(0, 0, width, height))\n// ... (draw or manipulate the img as needed) ...\ni2c.DrawImage(img)\n```\n\n3) Displaying Buffer to Screen:\n```go\nerr = i2c.Display()\nif err != nil {\n    log.Fatal(err)\n}\n```\n\n4) Adjusting Contrast:\n```go\nerr = i2c.SetContrast(contrastValue)  // contrastValue: 0 to 255\n```\n\n5) Dimming the Display:\n```go\nerr = i2c.SetDim(true)  // Set to true to dim\n```\n\n## Exemple\n```go\nimport (\n\t\"image\"\n\t\"image/color\"\n\t\"image/draw\"\n\n        \"github.com/waxdred/go-i2c-oled\"\n\t\"github.com/waxdred/go-i2c-oled/ssd1306\"\n\t\"golang.org/x/image/font\"\n\t\"golang.org/x/image/font/basicfont\"\n\t\"golang.org/x/image/math/fixed\"\n)\n\nfunc main() {\n\t// Initialize the OLED display with the provided parameters\n\toled, err := goi2coled.NewI2c(ssd1306.SSD1306_SWITCHCAPVCC, 64, 128, 0x3C, 1)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\n    \t// Ensure the OLED is properly closed at the end of the program\n\tdefer oled.Close()\n\n    \t// Define a black color\n\tblack := color.RGBA{0, 0, 0, 255}\n\n    \t// Set the entire OLED image to black\n\tdraw.Draw(oled.Img, oled.Img.Bounds(), \u0026image.Uniform{black}, image.Point{}, draw.Src)\n\n    \t// Define a white color\n\tcolWhite := color.RGBA{255, 255, 255, 255}\n\n    \t// Set the starting point for drawing text\n\tpoint := fixed.Point26_6{fixed.Int26_6(0 * 64), fixed.Int26_6(15 * 64)} // x = 0, y = 15\n\n    \t// Configure the font drawer with the chosen font and color\n\tdrawer := \u0026font.Drawer{\n\t\tDst:  oled.Img,\n\t\tSrc:  \u0026image.Uniform{colWhite},\n\t\tFace: basicfont.Face7x13,\n\t\tDot:  point,\n\t}\n\n    \t// Clear the OLED image (making it all black)\n\tdraw.Draw(oled.Img, oled.Img.Bounds(), \u0026image.Uniform{color.Black}, image.Point{}, draw.Src)\n\n    \t// Draw the text \"Hello\" on the OLED image\n\tdrawer.DrawString(\"Hello\")\n    \n    \t// Move the drawing point down by 10 pixels for the next line of text\n\tdrawer.Dot.Y += fixed.Int26_6(10 * 64)\n\n    \t// Set the drawing point's x coordinate back to 0 for alignment\n\tdrawer.Dot.X = fixed.Int26_6(0 * 64)\n\n    \t// Draw the text \"From golang!\" on the OLED image\n\tdrawer.DrawString(\"From golang!\")\n\n    \t// Clear the OLED's buffer (if applicable to your library)\n\toled.Clear()\n\n    \t// Update the OLED's buffer with the current image data\n\toled.Draw()\n\n    \t// Display the buffered content on the OLED screen\n\terr = oled.Display()\n}\n```\n\n## gif\n\n![](https://i.imgur.com/7ooOKd1.gif)\n\n## Exemple_Stat\n\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"image\"\n\t\"image/color\"\n\t\"image/draw\"\n\t\"image/png\"\n\t\"math/rand\"\n\t\"os\"\n\t\"os/exec\"\n\t\"os/signal\"\n\t\"strings\"\n\t\"syscall\"\n\t\"time\"\n\n\t\"github.com/waxdred/go-i2c-oled\"\n\t\"github.com/waxdred/go-i2c-oled/ssd1306\"\n\t\"golang.org/x/image/font\"\n\t\"golang.org/x/image/font/basicfont\"\n\t\"golang.org/x/image/math/fixed\"\n)\n\ntype Stat struct {\n\tIp   string\n\tCpu  string\n\tMem  string\n\tDisk string\n}\n\nfunc GlitchEffect(img image.Image, intensity float64) image.Image {\n\tbounds := img.Bounds()\n\tglitched := image.NewRGBA(bounds)\n\n\tfor y := bounds.Min.Y; y \u003c bounds.Max.Y; y++ {\n\t\tif rand.Float64() \u003c intensity {\n\t\t\tswitch rand.Intn(3) {\n\t\t\tcase 0: // Décalage horizontal\n\t\t\t\tshift := rand.Intn(20) - 10\n\t\t\t\tfor x := bounds.Min.X; x \u003c bounds.Max.X; x++ {\n\t\t\t\t\tglitched.Set(x, y, img.At((x+shift)%bounds.Max.X, y))\n\t\t\t\t}\n\t\t\tcase 1:\n\t\t\t\tcontinue\n\t\t\tcase 2:\n\t\t\t\tfor x := bounds.Min.X; x \u003c bounds.Max.X; x++ {\n\t\t\t\t\tr, g, b, a := img.At(x, y).RGBA()\n\t\t\t\t\tglitched.Set(\n\t\t\t\t\t\tx,\n\t\t\t\t\t\ty,\n\t\t\t\t\t\tcolor.RGBA{\n\t\t\t\t\t\t\tuint8((r + rand.Uint32()) % 256),\n\t\t\t\t\t\t\tuint8((g + rand.Uint32()) % 256),\n\t\t\t\t\t\t\tuint8((b + rand.Uint32()) % 256),\n\t\t\t\t\t\t\tuint8(a),\n\t\t\t\t\t\t},\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tfor x := bounds.Min.X; x \u003c bounds.Max.X; x++ {\n\t\t\t\tglitched.Set(x, y, img.At(x, y))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn glitched\n}\n\nfunc CreateGlitchAnimation(img image.Image, frames int) []image.Image {\n\tvar sequence []image.Image\n\n\tfor i := 0; i \u003c frames; i++ {\n\t\tintensity := rand.Float64()\n\t\tglitchedFrame := GlitchEffect(img, intensity)\n\t\tsequence = append(sequence, glitchedFrame)\n\t}\n\n\treturn sequence\n}\n\nfunc executeCmd(command string, args ...string) string {\n\tcmd := exec.Command(command, args...)\n\tvar out bytes.Buffer\n\tcmd.Stdout = \u0026out\n\terr := cmd.Run()\n\tif err != nil {\n\t\tfmt.Println(\"Error executing command:\", err)\n\t\treturn \"\"\n\t}\n\treturn strings.TrimSpace(out.String())\n}\n\nfunc GetStat() Stat {\n\treturn Stat{\n\t\tIp:   executeCmd(\"bash\", \"-c\", \"hostname -I | cut -d' ' -f1\"),\n\t\tCpu:  executeCmd(\"bash\", \"-c\", \"top -bn1 | grep load | awk '{printf \\\"CPU Load: %.2f\\\", $(NF-2)}'\"),\n\t\tMem:  executeCmd(\"bash\", \"-c\", \"free -m | awk 'NR==2{printf \\\"Mem: %.2f%%\\\", $3*100/$2 }'\"),\n\t\tDisk: executeCmd(\"bash\", \"-c\", \"df -h | awk '$NF==\\\"/\\\"{printf \\\"Disk: %d/%dGB %s\\\", $3,$2,$5}'\"),\n\t}\n}\n\nfunc main() {\n\t// Initialize the OLED with specific settings\n\toled, err := goi2coled.NewI2c(ssd1306.SSD1306_SWITCHCAPVCC, 64, 128, 0x3C, 1)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer oled.Close()\n\n\tblack := color.RGBA{0, 0, 0, 255}\n\n\t// Define a white color for text and drawings\n\tcolWhite := color.RGBA{255, 255, 255, 255}\n\n\t// Set the starting point for drawing text\n\tpoint := fixed.Point26_6{fixed.Int26_6(0 * 64), fixed.Int26_6(0 * 64)} // x = 0, y = 0 initially\n\n\t// Configure the font drawer with the chosen font and color\n\tdrawer := \u0026font.Drawer{\n\t\tDst:  oled.Img,\n\t\tSrc:  \u0026image.Uniform{colWhite},\n\t\tFace: basicfont.Face7x13,\n\t\tDot:  point,\n\t}\n\n\t// Setting up channel for graceful shutdown\n\tdone := make(chan os.Signal, 1)\n\tstopCh := make(chan bool, 1)\n\n\tsignal.Notify(done, syscall.SIGINT, syscall.SIGTERM)\n\n\t// Load Raspberry Pi logo\n\tlogoFile, err := os.Open(\"./rpi1.png\")\n\tif err != nil {\n\t\tfmt.Println(\"Error opening logo file:\", err)\n\t\treturn\n\t}\n\tdefer logoFile.Close()\n\tlogoImg, err := png.Decode(logoFile)\n\tif err != nil {\n\t\tfmt.Println(\"Error decoding logo image:\", err)\n\t\treturn\n\t}\n\n\tgo func() {\n\t\tfor {\n\t\t\tselect {\n\t\t\tcase \u003c-stopCh:\n\t\t\t\treturn\n\t\t\tdefault:\n\t\t\t\t// Main loop for updating the display\n\t\t\t\t// Set the entire OLED image to black\n\t\t\t\tdraw.Draw(oled.Img, oled.Img.Bounds(), \u0026image.Uniform{black}, image.Point{}, draw.Src)\n\t\t\t\t\n\t\t\t\tdrawer.Dot.Y = fixed.Int26_6(0 * 64)\n\t\t\t\tdrawer.Dot.X = fixed.Int26_6(0 * 64)\n\t\t\t\tdstRect := image.Rect(0, 0, logoImg.Bounds().Dx(), logoImg.Bounds().Dy()+15)\n\t\t\t\tdraw.Draw(oled.Img, dstRect, logoImg, image.Point{}, draw.Over)\n\n\t\t\t\tdrawer.Dot.Y = fixed.Int26_6(14 * 64)\n\t\t\t\tdrawer.Dot.X = fixed.Int26_6(\n\t\t\t\t\t(logoImg.Bounds().Dx() + 10) * 64,\n\t\t\t\t)\n\n\t\t\t\tdrawer.DrawString(\"Rpi 4\")\n\t\t\t\tdrawer.Dot.Y += fixed.Int26_6(30 * 64)\n\t\t\t\tdrawer.Dot.X = fixed.Int26_6(\n\t\t\t\t\t(logoImg.Bounds().Dx() + 10) * 64,\n\t\t\t\t)\n\t\t\t\tdrawer.DrawString(executeCmd(\"hostname\"))\n\t\t\t\toled.Draw()\n\t\t\t\toled.Display()\n\n\t\t\t\ttime.Sleep(time.Second * 10)\n\t\t\t\t\n\t\t\t\ttransistion := CreateGlitchAnimation(oled.Img, 10)\n\t\t\t\tfor _, t := range transistion {\n\t\t\t\t\tdrawer.Dot.Y = fixed.Int26_6(0 * 64)\n\t\t\t\t\tdrawer.Dot.X = fixed.Int26_6(0 * 64)\n\t\t\t\t\tdraw.Draw(oled.Img, dstRect, t, image.Point{}, draw.Over)\n\t\t\t\t\toled.Draw()\n\t\t\t\t\toled.Display()\n\t\t\t\t\ttime.Sleep(time.Microsecond * 2)\n\t\t\t\t}\n\n\t\t\t\tdrawer.Dot.Y = fixed.Int26_6(10 * 64)\n\t\t\t\tdrawer.Dot.X = fixed.Int26_6(0 * 64)\n\t\t\t\tdraw.Draw(oled.Img, oled.Img.Bounds(), \u0026image.Uniform{color.Black}, image.Point{}, draw.Src)\n\t\t\t\tstat := GetStat()\n\t\t\t\tdrawer.DrawString(fmt.Sprintf(\"Ip: %s\", stat.Ip))\n\t\t\t\tdrawer.Dot.Y += fixed.Int26_6(16 * 64)\n\t\t\t\tdrawer.Dot.X = fixed.Int26_6(0 * 64)\n\t\t\t\tdrawer.DrawString(stat.Cpu)\n\t\t\t\tdrawer.Dot.Y += fixed.Int26_6(14 * 64)\n\t\t\t\tdrawer.Dot.X = fixed.Int26_6(0 * 64)\n\t\t\t\tdrawer.DrawString(stat.Mem)\n\t\t\t\tdrawer.Dot.Y += fixed.Int26_6(14 * 64)\n\t\t\t\tdrawer.Dot.X = fixed.Int26_6(0 * 64)\n\t\t\t\tdrawer.DrawString(stat.Disk)\n\t\t\t\toled.Draw()\n\t\t\t\toled.Display()\n\t\t\t\ttime.Sleep(time.Second * 10)\n\t\t\t}\n\t\t}\n\t}()\n\t\u003c-done\n\tstopCh \u003c- true\n\tfmt.Printf(\"Stop programme\")\n}\n\n```\n\n## Notes\n- Ensure your SSD1306 OLED is properly connected via I2C.\n- Be aware of the screen resolution; the provided library assumes specific width and height, which should match your OLED screen.\n\n## Contributions\nContributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaxdred%2Fgo-i2c-oled","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwaxdred%2Fgo-i2c-oled","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaxdred%2Fgo-i2c-oled/lists"}